]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/testsuite.cpp
bc68cb5f108c360ee95eb025bddcf076547e6d67
[user/henk/code/inspircd.git] / src / testsuite.cpp
1 /*         +------------------------------------+
2  *         | Inspire Internet Relay Chat Daemon |
3  *         +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2009 InspIRCd Development Team
6  * See: http://wiki.inspircd.org/Credits
7  *
8  * This program is free but copyrighted software; see
9  *                the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 /* $Core */
15
16 #include "inspircd.h"
17 #include "testsuite.h"
18 #include "threadengine.h"
19 #include <iostream>
20
21 using namespace std;
22
23 class TestSuiteThread : public Thread
24 {
25  public:
26         TestSuiteThread() : Thread()
27         {
28         }
29
30         virtual ~TestSuiteThread()
31         {
32         }
33
34         virtual void Run()
35         {
36                 while (GetExitFlag() == false)
37                 {
38                         cout << "Test suite thread run...\n";
39                         sleep(5);
40                 }
41         }
42 };
43
44 TestSuite::TestSuite()
45 {
46         cout << "\n\n*** STARTING TESTSUITE ***\n";
47
48         std::string modname;
49         char choice;
50
51         while (1)
52         {
53                 cout << "(1) Call all module OnRunTestSuite() methods\n";
54                 cout << "(2) Load a module\n";
55                 cout << "(3) Unload a module\n";
56                 cout << "(4) Threading tests\n";
57                 cout << "(5) Wildcard and CIDR tests\n";
58                 cout << "(6) Comma sepstream tests\n";
59                 cout << "(7) Space sepstream tests\n";
60
61                 cout << endl << "(X) Exit test suite\n";
62
63                 cout << "\nChoices (Enter one or more options as a list then press enter, e.g. 15X): ";
64                 cin >> choice;
65
66                 if (!choice)
67                         continue;
68
69                 switch (choice)
70                 {
71                         case '1':
72                                 FOREACH_MOD(I_OnRunTestSuite, OnRunTestSuite());
73                                 break;
74                         case '2':
75                                 cout << "Enter module filename to load: ";
76                                 cin >> modname;
77                                 cout << (ServerInstance->Modules->Load(modname.c_str()) ? "\nSUCCESS!\n" : "\nFAILURE\n");
78                                 break;
79                         case '3':
80                                 cout << "Enter module filename to unload: ";
81                                 cin >> modname;
82                                 cout << (ServerInstance->Modules->Unload(modname.c_str()) ? "\nSUCCESS!\n" : "\nFAILURE\n");
83                                 break;
84                         case '4':
85                                 cout << (DoThreadTests() ? "\nSUCCESS!\n" : "\nFAILURE\n");
86                                 break;
87                         case '5':
88                                 cout << (DoWildTests() ? "\nSUCCESS!\n" : "\nFAILURE\n");
89                                 break;
90                         case '6':
91                                 cout << (DoCommaSepStreamTests() ? "\nSUCCESS!\n" : "\nFAILURE\n");
92                                 break;
93                         case '7':
94                                 cout << (DoSpaceSepStreamTests() ? "\nSUCCESS!\n" : "\nFAILURE\n");
95                                 break;
96                         case 'X':
97                                 return;
98                                 break;
99                         default:
100                                 cout << "Invalid option\n";
101                                 break;
102                 }
103                 cout << endl;
104         }
105 }
106
107 /* Test that x matches y with match() */
108 #define WCTEST(x, y) cout << "match(\"" << x << "\",\"" << y "\") " << ((passed = (InspIRCd::Match(x, y, NULL))) ? " SUCCESS!\n" : " FAILURE\n")
109 /* Test that x does not match y with match() */
110 #define WCTESTNOT(x, y) cout << "!match(\"" << x << "\",\"" << y "\") " << ((passed = ((!InspIRCd::Match(x, y, NULL)))) ? " SUCCESS!\n" : " FAILURE\n")
111
112 /* Test that x matches y with match() and cidr enabled */
113 #define CIDRTEST(x, y) cout << "match(\"" << x << "\",\"" << y "\", true) " << ((passed = (InspIRCd::MatchCIDR(x, y, NULL))) ? " SUCCESS!\n" : " FAILURE\n")
114 /* Test that x does not match y with match() and cidr enabled */
115 #define CIDRTESTNOT(x, y) cout << "!match(\"" << x << "\",\"" << y "\", true) " << ((passed = ((!InspIRCd::MatchCIDR(x, y, NULL)))) ? " SUCCESS!\n" : " FAILURE\n")
116
117 bool TestSuite::DoWildTests()
118 {
119         cout << "\n\nWildcard and CIDR tests\n\n";
120         bool passed = false;
121
122         WCTEST("foobar", "*");
123         WCTEST("foobar", "foo*");
124         WCTEST("foobar", "*bar");
125         WCTEST("foobar", "foo??r");
126         WCTEST("foobar.test", "fo?bar.*t");
127         WCTEST("foobar.test", "fo?bar.t*t");
128         WCTEST("foobar.tttt", "fo?bar.**t");
129         WCTEST("foobar", "foobar");
130         WCTEST("foobar", "foo***bar");
131         WCTEST("foobar", "*foo***bar");
132         WCTEST("foobar", "**foo***bar");
133         WCTEST("foobar", "**foobar*");
134         WCTEST("foobar", "**foobar**");
135         WCTEST("foobar", "**foobar");
136         WCTEST("foobar", "**f?*?ar");
137         WCTEST("foobar", "**f?*b?r");
138         WCTEST("foofar", "**f?*f*r");
139         WCTEST("foofar", "**f?*f*?");
140         WCTEST("r", "*");
141         WCTEST("", "");
142         WCTEST("test@foo.bar.test", "*@*.bar.test");
143         WCTEST("test@foo.bar.test", "*test*@*.bar.test");
144         WCTEST("test@foo.bar.test", "*@*test");
145
146         WCTEST("a", "*a");
147         WCTEST("aa", "*a");
148         WCTEST("aaa", "*a");
149         WCTEST("aaaa", "*a");
150         WCTEST("aaaaa", "*a");
151         WCTEST("aaaaaa", "*a");
152         WCTEST("aaaaaaa", "*a");
153         WCTEST("aaaaaaaa", "*a");
154         WCTEST("aaaaaaaaa", "*a");
155         WCTEST("aaaaaaaaaa", "*a");
156         WCTEST("aaaaaaaaaaa", "*a");
157
158         WCTESTNOT("foobar", "bazqux");
159         WCTESTNOT("foobar", "*qux");
160         WCTESTNOT("foobar", "foo*x");
161         WCTESTNOT("foobar", "baz*");
162         WCTESTNOT("foobar", "foo???r");
163         WCTESTNOT("foobar", "foobars");
164         WCTESTNOT("foobar", "**foobar**h");
165         WCTESTNOT("foobar", "**foobar**h*");
166         WCTESTNOT("foobar", "**f??*bar?");
167         WCTESTNOT("foobar", "");
168         WCTESTNOT("", "foobar");
169         WCTESTNOT("OperServ", "O");
170         WCTESTNOT("O", "OperServ");
171         WCTESTNOT("foobar.tst", "fo?bar.*g");
172         WCTESTNOT("foobar.test", "fo?bar.*tt");
173
174         CIDRTEST("brain@1.2.3.4", "*@1.2.0.0/16");
175         CIDRTEST("brain@1.2.3.4", "*@1.2.3.0/24");
176         CIDRTEST("192.168.3.97", "192.168.3.0/24");
177
178         CIDRTESTNOT("brain@1.2.3.4", "x*@1.2.0.0/16");
179         CIDRTESTNOT("brain@1.2.3.4", "*@1.3.4.0/24");
180         CIDRTESTNOT("1.2.3.4", "1.2.4.0/24");
181         CIDRTESTNOT("brain@1.2.3.4", "*@/24");
182         CIDRTESTNOT("brain@1.2.3.4", "@1.2.3.4/9");
183         CIDRTESTNOT("brain@1.2.3.4", "@");
184         CIDRTESTNOT("brain@1.2.3.4", "");
185
186         return true;
187 }
188
189
190 #define STREQUALTEST(x, y) cout << "==(\"" << x << ",\"" << y "\") " << ((passed = (x == y)) ? "SUCCESS\n" : "FAILURE\n")
191
192 bool TestSuite::DoCommaSepStreamTests()
193 {
194         bool passed = false;
195         irc::commasepstream items("this,is,a,comma,stream");
196         std::string item;
197         int idx = 0;
198
199         while (items.GetToken(item))
200         {
201                 idx++;
202
203                 switch (idx)
204                 {
205                         case 1:
206                                 STREQUALTEST(item, "this");
207                                 break;
208                         case 2:
209                                 STREQUALTEST(item, "is");
210                                 break;
211                         case 3:
212                                 STREQUALTEST(item, "a");
213                                 break;
214                         case 4:
215                                 STREQUALTEST(item, "comma");
216                                 break;
217                         case 5:
218                                 STREQUALTEST(item, "stream");
219                                 break;
220                         default:
221                                 cout << "COMMASEPSTREAM: FAILURE: Got an index too many! " << idx << " items\n";
222                                 break;
223                 }
224         }
225
226         return true;
227 }
228
229 bool TestSuite::DoSpaceSepStreamTests()
230 {
231         bool passed = false;
232
233         irc::spacesepstream list("this is a space stream");
234         std::string item;
235         int idx = 0;
236
237         while (list.GetToken(item))
238         {
239                 idx++;
240
241                 switch (idx)
242                 {
243                         case 1:
244                                 STREQUALTEST(item, "this");
245                                 break;
246                         case 2:
247                                 STREQUALTEST(item, "is");
248                                 break;
249                         case 3:
250                                 STREQUALTEST(item, "a");
251                                 break;
252                         case 4:
253                                 STREQUALTEST(item, "space");
254                                 break;
255                         case 5:
256                                 STREQUALTEST(item, "stream");
257                                 break;
258                         default:
259                                 cout << "SPACESEPSTREAM: FAILURE: Got an index too many! " << idx << " items\n";
260                                 break;
261                 }
262         }
263         return true;
264 }
265
266 bool TestSuite::DoThreadTests()
267 {
268         std::string anything;
269         ThreadEngine* te = NULL;
270
271         cout << "Creating new ThreadEngine class...\n";
272         try
273         {
274                 te = new ThreadEngine;
275         }
276         catch (...)
277         {
278                 cout << "Creation failed, test failure.\n";
279                 return false;
280         }
281         cout << "Creation success, type " << te->GetName() << "\n";
282
283         cout << "Allocate: new TestSuiteThread...\n";
284         TestSuiteThread* tst = new TestSuiteThread();
285
286         cout << "ThreadEngine::Create on TestSuiteThread...\n";
287         try
288         {
289                 try
290                 {
291                         te->Start(tst);
292                 }
293                 catch (CoreException &ce)
294                 {
295                         cout << "Failure: " << ce.GetReason() << endl;
296                 }
297         }
298         catch (...)
299         {
300                 cout << "Failure, unhandled exception\n";
301         }
302
303         cout << "Type any line and press enter to end test.\n";
304         cin >> anything;
305
306         /* Thread engine auto frees thread on delete */
307         cout << "Waiting for thread to exit... " << flush;
308         delete tst;
309         cout << "Done!\n";
310
311         cout << "Delete ThreadEngine... ";
312         delete te;
313         cout << "Done!\n";
314
315         return true;
316 }
317
318 TestSuite::~TestSuite()
319 {
320         cout << "\n\n*** END OF TEST SUITE ***\n";
321 }
322