]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/testsuite.cpp
m_dccallow Fix unsafe iteration
[user/henk/code/inspircd.git] / src / testsuite.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2009 Robin Burchell <robin+git@viroteck.net>
5  *   Copyright (C) 2008 Craig Edwards <craigedwards@brainbox.cc>
6  *   Copyright (C) 2008 Dennis Friis <peavey@inspircd.org>
7  *
8  * This file is part of InspIRCd.  InspIRCd is free software: you can
9  * redistribute it and/or modify it under the terms of the GNU General Public
10  * License as published by the Free Software Foundation, version 2.
11  *
12  * This program is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
15  * details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19  */
20
21
22 /* $Core */
23
24 #include "inspircd.h"
25 #include "testsuite.h"
26 #include "threadengine.h"
27 #include <iostream>
28
29 using namespace std;
30
31 class TestSuiteThread : public Thread
32 {
33  public:
34         TestSuiteThread() : Thread()
35         {
36         }
37
38         virtual ~TestSuiteThread()
39         {
40         }
41
42         virtual void Run()
43         {
44                 while (GetExitFlag() == false)
45                 {
46                         cout << "Test suite thread run...\n";
47                         sleep(5);
48                 }
49         }
50 };
51
52 TestSuite::TestSuite()
53 {
54         cout << "\n\n*** STARTING TESTSUITE ***\n";
55
56         std::string modname;
57         char choice;
58
59         while (1)
60         {
61                 cout << "(1) Call all module OnRunTestSuite() methods\n";
62                 cout << "(2) Load a module\n";
63                 cout << "(3) Unload a module\n";
64                 cout << "(4) Threading tests\n";
65                 cout << "(5) Wildcard and CIDR tests\n";
66                 cout << "(6) Comma sepstream tests\n";
67                 cout << "(7) Space sepstream tests\n";
68                 cout << "(8) UID generation tests\n";
69
70                 cout << endl << "(X) Exit test suite\n";
71
72                 cout << "\nChoices (Enter one or more options as a list then press enter, e.g. 15X): ";
73                 cin >> choice;
74
75                 if (!choice)
76                         continue;
77
78                 switch (choice)
79                 {
80                         case '1':
81                                 FOREACH_MOD(I_OnRunTestSuite, OnRunTestSuite());
82                                 break;
83                         case '2':
84                                 cout << "Enter module filename to load: ";
85                                 cin >> modname;
86                                 cout << (ServerInstance->Modules->Load(modname.c_str()) ? "\nSUCCESS!\n" : "\nFAILURE\n");
87                                 break;
88                         case '3':
89                                 cout << "Enter module filename to unload: ";
90                                 cin >> modname;
91                                 {
92                                         Module* m = ServerInstance->Modules->Find(modname);
93                                         cout << (ServerInstance->Modules->Unload(m) ? "\nSUCCESS!\n" : "\nFAILURE\n");
94                                         ServerInstance->AtomicActions.Run();
95                                 }
96                                 break;
97                         case '4':
98                                 cout << (DoThreadTests() ? "\nSUCCESS!\n" : "\nFAILURE\n");
99                                 break;
100                         case '5':
101                                 cout << (DoWildTests() ? "\nSUCCESS!\n" : "\nFAILURE\n");
102                                 break;
103                         case '6':
104                                 cout << (DoCommaSepStreamTests() ? "\nSUCCESS!\n" : "\nFAILURE\n");
105                                 break;
106                         case '7':
107                                 cout << (DoSpaceSepStreamTests() ? "\nSUCCESS!\n" : "\nFAILURE\n");
108                                 break;
109                         case '8':
110                                 cout << (DoGenerateUIDTests() ? "\nSUCCESS!\n" : "\nFAILURE\n");
111                                 break;
112                         case 'X':
113                                 return;
114                                 break;
115                         default:
116                                 cout << "Invalid option\n";
117                                 break;
118                 }
119                 cout << endl;
120         }
121 }
122
123 /* Test that x matches y with match() */
124 #define WCTEST(x, y) cout << "match(\"" << x << "\",\"" << y "\") " << ((passed = (InspIRCd::Match(x, y, NULL))) ? " SUCCESS!\n" : " FAILURE\n")
125 /* Test that x does not match y with match() */
126 #define WCTESTNOT(x, y) cout << "!match(\"" << x << "\",\"" << y "\") " << ((passed = ((!InspIRCd::Match(x, y, NULL)))) ? " SUCCESS!\n" : " FAILURE\n")
127
128 /* Test that x matches y with match() and cidr enabled */
129 #define CIDRTEST(x, y) cout << "match(\"" << x << "\",\"" << y "\", true) " << ((passed = (InspIRCd::MatchCIDR(x, y, NULL))) ? " SUCCESS!\n" : " FAILURE\n")
130 /* Test that x does not match y with match() and cidr enabled */
131 #define CIDRTESTNOT(x, y) cout << "!match(\"" << x << "\",\"" << y "\", true) " << ((passed = ((!InspIRCd::MatchCIDR(x, y, NULL)))) ? " SUCCESS!\n" : " FAILURE\n")
132
133 bool TestSuite::DoWildTests()
134 {
135         cout << "\n\nWildcard and CIDR tests\n\n";
136         bool passed = false;
137
138         WCTEST("foobar", "*");
139         WCTEST("foobar", "foo*");
140         WCTEST("foobar", "*bar");
141         WCTEST("foobar", "foo??r");
142         WCTEST("foobar.test", "fo?bar.*t");
143         WCTEST("foobar.test", "fo?bar.t*t");
144         WCTEST("foobar.tttt", "fo?bar.**t");
145         WCTEST("foobar", "foobar");
146         WCTEST("foobar", "foo***bar");
147         WCTEST("foobar", "*foo***bar");
148         WCTEST("foobar", "**foo***bar");
149         WCTEST("foobar", "**foobar*");
150         WCTEST("foobar", "**foobar**");
151         WCTEST("foobar", "**foobar");
152         WCTEST("foobar", "**f?*?ar");
153         WCTEST("foobar", "**f?*b?r");
154         WCTEST("foofar", "**f?*f*r");
155         WCTEST("foofar", "**f?*f*?");
156         WCTEST("r", "*");
157         WCTEST("", "");
158         WCTEST("test@foo.bar.test", "*@*.bar.test");
159         WCTEST("test@foo.bar.test", "*test*@*.bar.test");
160         WCTEST("test@foo.bar.test", "*@*test");
161
162         WCTEST("a", "*a");
163         WCTEST("aa", "*a");
164         WCTEST("aaa", "*a");
165         WCTEST("aaaa", "*a");
166         WCTEST("aaaaa", "*a");
167         WCTEST("aaaaaa", "*a");
168         WCTEST("aaaaaaa", "*a");
169         WCTEST("aaaaaaaa", "*a");
170         WCTEST("aaaaaaaaa", "*a");
171         WCTEST("aaaaaaaaaa", "*a");
172         WCTEST("aaaaaaaaaaa", "*a");
173
174         WCTESTNOT("foobar", "bazqux");
175         WCTESTNOT("foobar", "*qux");
176         WCTESTNOT("foobar", "foo*x");
177         WCTESTNOT("foobar", "baz*");
178         WCTESTNOT("foobar", "foo???r");
179         WCTESTNOT("foobar", "foobars");
180         WCTESTNOT("foobar", "**foobar**h");
181         WCTESTNOT("foobar", "**foobar**h*");
182         WCTESTNOT("foobar", "**f??*bar?");
183         WCTESTNOT("foobar", "");
184         WCTESTNOT("", "foobar");
185         WCTESTNOT("OperServ", "O");
186         WCTESTNOT("O", "OperServ");
187         WCTESTNOT("foobar.tst", "fo?bar.*g");
188         WCTESTNOT("foobar.test", "fo?bar.*tt");
189
190         CIDRTEST("brain@1.2.3.4", "*@1.2.0.0/16");
191         CIDRTEST("brain@1.2.3.4", "*@1.2.3.0/24");
192         CIDRTEST("192.168.3.97", "192.168.3.0/24");
193
194         CIDRTESTNOT("brain@1.2.3.4", "x*@1.2.0.0/16");
195         CIDRTESTNOT("brain@1.2.3.4", "*@1.3.4.0/24");
196         CIDRTESTNOT("1.2.3.4", "1.2.4.0/24");
197         CIDRTESTNOT("brain@1.2.3.4", "*@/24");
198         CIDRTESTNOT("brain@1.2.3.4", "@1.2.3.4/9");
199         CIDRTESTNOT("brain@1.2.3.4", "@");
200         CIDRTESTNOT("brain@1.2.3.4", "");
201
202         return true;
203 }
204
205
206 #define STREQUALTEST(x, y) cout << "==(\"" << x << ",\"" << y "\") " << ((passed = (x == y)) ? "SUCCESS\n" : "FAILURE\n")
207
208 bool TestSuite::DoCommaSepStreamTests()
209 {
210         bool passed = false;
211         irc::commasepstream items("this,is,a,comma,stream");
212         std::string item;
213         int idx = 0;
214
215         while (items.GetToken(item))
216         {
217                 idx++;
218
219                 switch (idx)
220                 {
221                         case 1:
222                                 STREQUALTEST(item, "this");
223                                 break;
224                         case 2:
225                                 STREQUALTEST(item, "is");
226                                 break;
227                         case 3:
228                                 STREQUALTEST(item, "a");
229                                 break;
230                         case 4:
231                                 STREQUALTEST(item, "comma");
232                                 break;
233                         case 5:
234                                 STREQUALTEST(item, "stream");
235                                 break;
236                         default:
237                                 cout << "COMMASEPSTREAM: FAILURE: Got an index too many! " << idx << " items\n";
238                                 break;
239                 }
240         }
241
242         return true;
243 }
244
245 bool TestSuite::DoSpaceSepStreamTests()
246 {
247         bool passed = false;
248
249         irc::spacesepstream list("this is a space stream");
250         std::string item;
251         int idx = 0;
252
253         while (list.GetToken(item))
254         {
255                 idx++;
256
257                 switch (idx)
258                 {
259                         case 1:
260                                 STREQUALTEST(item, "this");
261                                 break;
262                         case 2:
263                                 STREQUALTEST(item, "is");
264                                 break;
265                         case 3:
266                                 STREQUALTEST(item, "a");
267                                 break;
268                         case 4:
269                                 STREQUALTEST(item, "space");
270                                 break;
271                         case 5:
272                                 STREQUALTEST(item, "stream");
273                                 break;
274                         default:
275                                 cout << "SPACESEPSTREAM: FAILURE: Got an index too many! " << idx << " items\n";
276                                 break;
277                 }
278         }
279         return true;
280 }
281
282 bool TestSuite::DoThreadTests()
283 {
284         std::string anything;
285         ThreadEngine* te = NULL;
286
287         cout << "Creating new ThreadEngine class...\n";
288         try
289         {
290                 te = new ThreadEngine;
291         }
292         catch (...)
293         {
294                 cout << "Creation failed, test failure.\n";
295                 return false;
296         }
297         cout << "Creation success, type " << te->GetName() << "\n";
298
299         cout << "Allocate: new TestSuiteThread...\n";
300         TestSuiteThread* tst = new TestSuiteThread();
301
302         cout << "ThreadEngine::Create on TestSuiteThread...\n";
303         try
304         {
305                 try
306                 {
307                         te->Start(tst);
308                 }
309                 catch (CoreException &ce)
310                 {
311                         cout << "Failure: " << ce.GetReason() << endl;
312                 }
313         }
314         catch (...)
315         {
316                 cout << "Failure, unhandled exception\n";
317         }
318
319         cout << "Type any line and press enter to end test.\n";
320         cin >> anything;
321
322         /* Thread engine auto frees thread on delete */
323         cout << "Waiting for thread to exit... " << flush;
324         delete tst;
325         cout << "Done!\n";
326
327         cout << "Delete ThreadEngine... ";
328         delete te;
329         cout << "Done!\n";
330
331         return true;
332 }
333
334 bool TestSuite::DoGenerateUIDTests()
335 {
336         bool success = RealGenerateUIDTests();
337
338         // Reset the UID generation state so running the tests multiple times won't mess things up
339         for (unsigned int i = 0; i < 3; i++)
340                 ServerInstance->current_uid[i] = ServerInstance->Config->sid[i];
341         for (unsigned int i = 3; i < UUID_LENGTH-1; i++)
342                 ServerInstance->current_uid[i] = '9';
343
344         ServerInstance->current_uid[UUID_LENGTH-1] = '\0';
345
346         return success;
347 }
348
349 bool TestSuite::RealGenerateUIDTests()
350 {
351         std::string first_uid = ServerInstance->GetUID();
352         if (first_uid.length() != UUID_LENGTH-1)
353         {
354                 cout << "GENERATEUID: Generated UID is " << first_uid.length() << " characters long instead of " << UUID_LENGTH-1 << endl;
355                 return false;
356         }
357
358         if (ServerInstance->current_uid[UUID_LENGTH-1] != '\0')
359         {
360                 cout << "GENERATEUID: The null terminator is missing from the end of current_uid" << endl;
361                 return false;
362         }
363
364         // The correct UID when generating one for the first time is ...AAAAAA
365         std::string correct_uid = ServerInstance->Config->sid + std::string(UUID_LENGTH - 4, 'A');
366         if (first_uid != correct_uid)
367         {
368                 cout << "GENERATEUID: Generated an invalid first UID: " << first_uid << " instead of " << correct_uid << endl;
369                 return false;
370         }
371
372         // Set current_uid to be ...Z99999
373         ServerInstance->current_uid[3] = 'Z';
374         for (unsigned int i = 4; i < UUID_LENGTH-1; i++)
375                 ServerInstance->current_uid[i] = '9';
376
377         // Store the UID we'll be incrementing so we can display what's wrong later if necessary
378         std::string before_increment(ServerInstance->current_uid);
379         std::string generated_uid = ServerInstance->GetUID();
380
381         // Correct UID after incrementing ...Z99999 is ...0AAAAA
382         correct_uid = ServerInstance->Config->sid + "0" + std::string(UUID_LENGTH - 5, 'A');
383
384         if (generated_uid != correct_uid)
385         {
386                 cout << "GENERATEUID: Generated an invalid UID after incrementing " << before_increment << ": " << generated_uid << " instead of " << correct_uid << endl;
387                 return false;
388         }
389
390         // Set current_uid to be ...999999 to see if it rolls over correctly
391         for (unsigned int i = 3; i < UUID_LENGTH-1; i++)
392                 ServerInstance->current_uid[i] = '9';
393
394         before_increment.assign(ServerInstance->current_uid);
395         generated_uid = ServerInstance->GetUID();
396
397         // Correct UID after rolling over is the first UID we've generated (...AAAAAA)
398         if (generated_uid != first_uid)
399         {
400                 cout << "GENERATEUID: Generated an invalid UID after incrementing " << before_increment << ": " << generated_uid << " instead of " << first_uid << endl;
401                 return false;
402         }
403
404         return true;
405 }
406
407 TestSuite::~TestSuite()
408 {
409         cout << "\n\n*** END OF TEST SUITE ***\n";
410 }
411