]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/testsuite.cpp
Make irc::sockets::* parameters consistent, add irc::sockets::mask
[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                                 {
83                                         Module* m = ServerInstance->Modules->Find(modname);
84                                         cout << (ServerInstance->Modules->Unload(m) ? "\nSUCCESS!\n" : "\nFAILURE\n");
85                                         ServerInstance->AtomicActions.Run();
86                                 }
87                                 break;
88                         case '4':
89                                 cout << (DoThreadTests() ? "\nSUCCESS!\n" : "\nFAILURE\n");
90                                 break;
91                         case '5':
92                                 cout << (DoWildTests() ? "\nSUCCESS!\n" : "\nFAILURE\n");
93                                 break;
94                         case '6':
95                                 cout << (DoCommaSepStreamTests() ? "\nSUCCESS!\n" : "\nFAILURE\n");
96                                 break;
97                         case '7':
98                                 cout << (DoSpaceSepStreamTests() ? "\nSUCCESS!\n" : "\nFAILURE\n");
99                                 break;
100                         case 'X':
101                                 return;
102                                 break;
103                         default:
104                                 cout << "Invalid option\n";
105                                 break;
106                 }
107                 cout << endl;
108         }
109 }
110
111 /* Test that x matches y with match() */
112 #define WCTEST(x, y) cout << "match(\"" << x << "\",\"" << y "\") " << ((passed = (InspIRCd::Match(x, y, NULL))) ? " SUCCESS!\n" : " FAILURE\n")
113 /* Test that x does not match y with match() */
114 #define WCTESTNOT(x, y) cout << "!match(\"" << x << "\",\"" << y "\") " << ((passed = ((!InspIRCd::Match(x, y, NULL)))) ? " SUCCESS!\n" : " FAILURE\n")
115
116 /* Test that x matches y with match() and cidr enabled */
117 #define CIDRTEST(x, y) cout << "match(\"" << x << "\",\"" << y "\", true) " << ((passed = (InspIRCd::MatchCIDR(x, y, NULL))) ? " SUCCESS!\n" : " FAILURE\n")
118 /* Test that x does not match y with match() and cidr enabled */
119 #define CIDRTESTNOT(x, y) cout << "!match(\"" << x << "\",\"" << y "\", true) " << ((passed = ((!InspIRCd::MatchCIDR(x, y, NULL)))) ? " SUCCESS!\n" : " FAILURE\n")
120
121 bool TestSuite::DoWildTests()
122 {
123         cout << "\n\nWildcard and CIDR tests\n\n";
124         bool passed = false;
125
126         WCTEST("foobar", "*");
127         WCTEST("foobar", "foo*");
128         WCTEST("foobar", "*bar");
129         WCTEST("foobar", "foo??r");
130         WCTEST("foobar.test", "fo?bar.*t");
131         WCTEST("foobar.test", "fo?bar.t*t");
132         WCTEST("foobar.tttt", "fo?bar.**t");
133         WCTEST("foobar", "foobar");
134         WCTEST("foobar", "foo***bar");
135         WCTEST("foobar", "*foo***bar");
136         WCTEST("foobar", "**foo***bar");
137         WCTEST("foobar", "**foobar*");
138         WCTEST("foobar", "**foobar**");
139         WCTEST("foobar", "**foobar");
140         WCTEST("foobar", "**f?*?ar");
141         WCTEST("foobar", "**f?*b?r");
142         WCTEST("foofar", "**f?*f*r");
143         WCTEST("foofar", "**f?*f*?");
144         WCTEST("r", "*");
145         WCTEST("", "");
146         WCTEST("test@foo.bar.test", "*@*.bar.test");
147         WCTEST("test@foo.bar.test", "*test*@*.bar.test");
148         WCTEST("test@foo.bar.test", "*@*test");
149
150         WCTEST("a", "*a");
151         WCTEST("aa", "*a");
152         WCTEST("aaa", "*a");
153         WCTEST("aaaa", "*a");
154         WCTEST("aaaaa", "*a");
155         WCTEST("aaaaaa", "*a");
156         WCTEST("aaaaaaa", "*a");
157         WCTEST("aaaaaaaa", "*a");
158         WCTEST("aaaaaaaaa", "*a");
159         WCTEST("aaaaaaaaaa", "*a");
160         WCTEST("aaaaaaaaaaa", "*a");
161
162         WCTESTNOT("foobar", "bazqux");
163         WCTESTNOT("foobar", "*qux");
164         WCTESTNOT("foobar", "foo*x");
165         WCTESTNOT("foobar", "baz*");
166         WCTESTNOT("foobar", "foo???r");
167         WCTESTNOT("foobar", "foobars");
168         WCTESTNOT("foobar", "**foobar**h");
169         WCTESTNOT("foobar", "**foobar**h*");
170         WCTESTNOT("foobar", "**f??*bar?");
171         WCTESTNOT("foobar", "");
172         WCTESTNOT("", "foobar");
173         WCTESTNOT("OperServ", "O");
174         WCTESTNOT("O", "OperServ");
175         WCTESTNOT("foobar.tst", "fo?bar.*g");
176         WCTESTNOT("foobar.test", "fo?bar.*tt");
177
178         CIDRTEST("brain@1.2.3.4", "*@1.2.0.0/16");
179         CIDRTEST("brain@1.2.3.4", "*@1.2.3.0/24");
180         CIDRTEST("192.168.3.97", "192.168.3.0/24");
181
182         CIDRTESTNOT("brain@1.2.3.4", "x*@1.2.0.0/16");
183         CIDRTESTNOT("brain@1.2.3.4", "*@1.3.4.0/24");
184         CIDRTESTNOT("1.2.3.4", "1.2.4.0/24");
185         CIDRTESTNOT("brain@1.2.3.4", "*@/24");
186         CIDRTESTNOT("brain@1.2.3.4", "@1.2.3.4/9");
187         CIDRTESTNOT("brain@1.2.3.4", "@");
188         CIDRTESTNOT("brain@1.2.3.4", "");
189
190         return true;
191 }
192
193
194 #define STREQUALTEST(x, y) cout << "==(\"" << x << ",\"" << y "\") " << ((passed = (x == y)) ? "SUCCESS\n" : "FAILURE\n")
195
196 bool TestSuite::DoCommaSepStreamTests()
197 {
198         bool passed = false;
199         irc::commasepstream items("this,is,a,comma,stream");
200         std::string item;
201         int idx = 0;
202
203         while (items.GetToken(item))
204         {
205                 idx++;
206
207                 switch (idx)
208                 {
209                         case 1:
210                                 STREQUALTEST(item, "this");
211                                 break;
212                         case 2:
213                                 STREQUALTEST(item, "is");
214                                 break;
215                         case 3:
216                                 STREQUALTEST(item, "a");
217                                 break;
218                         case 4:
219                                 STREQUALTEST(item, "comma");
220                                 break;
221                         case 5:
222                                 STREQUALTEST(item, "stream");
223                                 break;
224                         default:
225                                 cout << "COMMASEPSTREAM: FAILURE: Got an index too many! " << idx << " items\n";
226                                 break;
227                 }
228         }
229
230         return true;
231 }
232
233 bool TestSuite::DoSpaceSepStreamTests()
234 {
235         bool passed = false;
236
237         irc::spacesepstream list("this is a space stream");
238         std::string item;
239         int idx = 0;
240
241         while (list.GetToken(item))
242         {
243                 idx++;
244
245                 switch (idx)
246                 {
247                         case 1:
248                                 STREQUALTEST(item, "this");
249                                 break;
250                         case 2:
251                                 STREQUALTEST(item, "is");
252                                 break;
253                         case 3:
254                                 STREQUALTEST(item, "a");
255                                 break;
256                         case 4:
257                                 STREQUALTEST(item, "space");
258                                 break;
259                         case 5:
260                                 STREQUALTEST(item, "stream");
261                                 break;
262                         default:
263                                 cout << "SPACESEPSTREAM: FAILURE: Got an index too many! " << idx << " items\n";
264                                 break;
265                 }
266         }
267         return true;
268 }
269
270 bool TestSuite::DoThreadTests()
271 {
272         std::string anything;
273         ThreadEngine* te = NULL;
274
275         cout << "Creating new ThreadEngine class...\n";
276         try
277         {
278                 te = new ThreadEngine;
279         }
280         catch (...)
281         {
282                 cout << "Creation failed, test failure.\n";
283                 return false;
284         }
285         cout << "Creation success, type " << te->GetName() << "\n";
286
287         cout << "Allocate: new TestSuiteThread...\n";
288         TestSuiteThread* tst = new TestSuiteThread();
289
290         cout << "ThreadEngine::Create on TestSuiteThread...\n";
291         try
292         {
293                 try
294                 {
295                         te->Start(tst);
296                 }
297                 catch (CoreException &ce)
298                 {
299                         cout << "Failure: " << ce.GetReason() << endl;
300                 }
301         }
302         catch (...)
303         {
304                 cout << "Failure, unhandled exception\n";
305         }
306
307         cout << "Type any line and press enter to end test.\n";
308         cin >> anything;
309
310         /* Thread engine auto frees thread on delete */
311         cout << "Waiting for thread to exit... " << flush;
312         delete tst;
313         cout << "Done!\n";
314
315         cout << "Delete ThreadEngine... ";
316         delete te;
317         cout << "Done!\n";
318
319         return true;
320 }
321
322 TestSuite::~TestSuite()
323 {
324         cout << "\n\n*** END OF TEST SUITE ***\n";
325 }
326