2 * InspIRCd -- Internet Relay Chat Daemon
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>
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.
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
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/>.
25 #include "testsuite.h"
26 #include "threadengine.h"
31 class TestSuiteThread : public Thread
34 TestSuiteThread() : Thread()
38 virtual ~TestSuiteThread()
44 while (GetExitFlag() == false)
46 cout << "Test suite thread run...\n";
52 TestSuite::TestSuite()
54 cout << "\n\n*** STARTING TESTSUITE ***\n";
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";
70 cout << endl << "(X) Exit test suite\n";
72 cout << "\nChoices (Enter one or more options as a list then press enter, e.g. 15X): ";
81 FOREACH_MOD(I_OnRunTestSuite, OnRunTestSuite());
84 cout << "Enter module filename to load: ";
86 cout << (ServerInstance->Modules->Load(modname.c_str()) ? "\nSUCCESS!\n" : "\nFAILURE\n");
89 cout << "Enter module filename to unload: ";
92 Module* m = ServerInstance->Modules->Find(modname);
93 cout << (ServerInstance->Modules->Unload(m) ? "\nSUCCESS!\n" : "\nFAILURE\n");
94 ServerInstance->AtomicActions.Run();
98 cout << (DoThreadTests() ? "\nSUCCESS!\n" : "\nFAILURE\n");
101 cout << (DoWildTests() ? "\nSUCCESS!\n" : "\nFAILURE\n");
104 cout << (DoCommaSepStreamTests() ? "\nSUCCESS!\n" : "\nFAILURE\n");
107 cout << (DoSpaceSepStreamTests() ? "\nSUCCESS!\n" : "\nFAILURE\n");
110 cout << (DoGenerateUIDTests() ? "\nSUCCESS!\n" : "\nFAILURE\n");
116 cout << "Invalid option\n";
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")
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")
133 bool TestSuite::DoWildTests()
135 cout << "\n\nWildcard and CIDR tests\n\n";
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*?");
158 WCTEST("test@foo.bar.test", "*@*.bar.test");
159 WCTEST("test@foo.bar.test", "*test*@*.bar.test");
160 WCTEST("test@foo.bar.test", "*@*test");
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");
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");
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");
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", "");
206 #define STREQUALTEST(x, y) cout << "==(\"" << x << ",\"" << y "\") " << ((passed = (x == y)) ? "SUCCESS\n" : "FAILURE\n")
208 bool TestSuite::DoCommaSepStreamTests()
211 irc::commasepstream items("this,is,a,comma,stream");
215 while (items.GetToken(item))
222 STREQUALTEST(item, "this");
225 STREQUALTEST(item, "is");
228 STREQUALTEST(item, "a");
231 STREQUALTEST(item, "comma");
234 STREQUALTEST(item, "stream");
237 cout << "COMMASEPSTREAM: FAILURE: Got an index too many! " << idx << " items\n";
245 bool TestSuite::DoSpaceSepStreamTests()
249 irc::spacesepstream list("this is a space stream");
253 while (list.GetToken(item))
260 STREQUALTEST(item, "this");
263 STREQUALTEST(item, "is");
266 STREQUALTEST(item, "a");
269 STREQUALTEST(item, "space");
272 STREQUALTEST(item, "stream");
275 cout << "SPACESEPSTREAM: FAILURE: Got an index too many! " << idx << " items\n";
282 bool TestSuite::DoThreadTests()
284 std::string anything;
285 ThreadEngine* te = NULL;
287 cout << "Creating new ThreadEngine class...\n";
290 te = new ThreadEngine;
294 cout << "Creation failed, test failure.\n";
297 cout << "Creation success, type " << te->GetName() << "\n";
299 cout << "Allocate: new TestSuiteThread...\n";
300 TestSuiteThread* tst = new TestSuiteThread();
302 cout << "ThreadEngine::Create on TestSuiteThread...\n";
309 catch (CoreException &ce)
311 cout << "Failure: " << ce.GetReason() << endl;
316 cout << "Failure, unhandled exception\n";
319 cout << "Type any line and press enter to end test.\n";
322 /* Thread engine auto frees thread on delete */
323 cout << "Waiting for thread to exit... " << flush;
327 cout << "Delete ThreadEngine... ";
334 bool TestSuite::DoGenerateUIDTests()
336 bool success = RealGenerateUIDTests();
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';
344 ServerInstance->current_uid[UUID_LENGTH-1] = '\0';
349 bool TestSuite::RealGenerateUIDTests()
351 std::string first_uid = ServerInstance->GetUID();
352 if (first_uid.length() != UUID_LENGTH-1)
354 cout << "GENERATEUID: Generated UID is " << first_uid.length() << " characters long instead of " << UUID_LENGTH-1 << endl;
358 if (ServerInstance->current_uid[UUID_LENGTH-1] != '\0')
360 cout << "GENERATEUID: The null terminator is missing from the end of current_uid" << endl;
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)
368 cout << "GENERATEUID: Generated an invalid first UID: " << first_uid << " instead of " << correct_uid << endl;
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';
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();
381 // Correct UID after incrementing ...Z99999 is ...0AAAAA
382 correct_uid = ServerInstance->Config->sid + "0" + std::string(UUID_LENGTH - 5, 'A');
384 if (generated_uid != correct_uid)
386 cout << "GENERATEUID: Generated an invalid UID after incrementing " << before_increment << ": " << generated_uid << " instead of " << correct_uid << endl;
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';
394 before_increment.assign(ServerInstance->current_uid);
395 generated_uid = ServerInstance->GetUID();
397 // Correct UID after rolling over is the first UID we've generated (...AAAAAA)
398 if (generated_uid != first_uid)
400 cout << "GENERATEUID: Generated an invalid UID after incrementing " << before_increment << ": " << generated_uid << " instead of " << first_uid << endl;
407 TestSuite::~TestSuite()
409 cout << "\n\n*** END OF TEST SUITE ***\n";