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"
29 class TestSuiteThread : public Thread
32 TestSuiteThread() : Thread()
36 virtual ~TestSuiteThread()
42 while (GetExitFlag() == false)
44 std::cout << "Test suite thread run...\n";
50 TestSuite::TestSuite()
52 std::cout << "\n\n*** STARTING TESTSUITE ***\n";
59 std::cout << "(1) Call all module OnRunTestSuite() methods\n";
60 std::cout << "(2) Load a module\n";
61 std::cout << "(3) Unload a module\n";
62 std::cout << "(4) Threading tests\n";
63 std::cout << "(5) Wildcard and CIDR tests\n";
64 std::cout << "(6) Comma sepstream tests\n";
65 std::cout << "(7) Space sepstream tests\n";
66 std::cout << "(8) UID generation tests\n";
68 std::cout << std::endl << "(X) Exit test suite\n";
70 std::cout << "\nChoices (Enter one or more options as a list then press enter, e.g. 15X): ";
79 FOREACH_MOD(I_OnRunTestSuite, OnRunTestSuite());
82 std::cout << "Enter module filename to load: ";
84 std::cout << (ServerInstance->Modules->Load(modname.c_str()) ? "\nSUCCESS!\n" : "\nFAILURE\n");
87 std::cout << "Enter module filename to unload: ";
90 Module* m = ServerInstance->Modules->Find(modname);
91 std::cout << (ServerInstance->Modules->Unload(m) ? "\nSUCCESS!\n" : "\nFAILURE\n");
92 ServerInstance->AtomicActions.Run();
96 std::cout << (DoThreadTests() ? "\nSUCCESS!\n" : "\nFAILURE\n");
99 std::cout << (DoWildTests() ? "\nSUCCESS!\n" : "\nFAILURE\n");
102 std::cout << (DoCommaSepStreamTests() ? "\nSUCCESS!\n" : "\nFAILURE\n");
105 std::cout << (DoSpaceSepStreamTests() ? "\nSUCCESS!\n" : "\nFAILURE\n");
108 std::cout << (DoGenerateUIDTests() ? "\nSUCCESS!\n" : "\nFAILURE\n");
114 std::cout << "Invalid option\n";
117 std::cout << std::endl;
121 /* Test that x matches y with match() */
122 #define WCTEST(x, y) std::cout << "match(\"" << x << "\",\"" << y "\") " << ((passed = (InspIRCd::Match(x, y, NULL))) ? " SUCCESS!\n" : " FAILURE\n")
123 /* Test that x does not match y with match() */
124 #define WCTESTNOT(x, y) std::cout << "!match(\"" << x << "\",\"" << y "\") " << ((passed = ((!InspIRCd::Match(x, y, NULL)))) ? " SUCCESS!\n" : " FAILURE\n")
126 /* Test that x matches y with match() and cidr enabled */
127 #define CIDRTEST(x, y) std::cout << "match(\"" << x << "\",\"" << y "\", true) " << ((passed = (InspIRCd::MatchCIDR(x, y, NULL))) ? " SUCCESS!\n" : " FAILURE\n")
128 /* Test that x does not match y with match() and cidr enabled */
129 #define CIDRTESTNOT(x, y) std::cout << "!match(\"" << x << "\",\"" << y "\", true) " << ((passed = ((!InspIRCd::MatchCIDR(x, y, NULL)))) ? " SUCCESS!\n" : " FAILURE\n")
131 bool TestSuite::DoWildTests()
133 std::cout << "\n\nWildcard and CIDR tests\n\n";
136 WCTEST("foobar", "*");
137 WCTEST("foobar", "foo*");
138 WCTEST("foobar", "*bar");
139 WCTEST("foobar", "foo??r");
140 WCTEST("foobar.test", "fo?bar.*t");
141 WCTEST("foobar.test", "fo?bar.t*t");
142 WCTEST("foobar.tttt", "fo?bar.**t");
143 WCTEST("foobar", "foobar");
144 WCTEST("foobar", "foo***bar");
145 WCTEST("foobar", "*foo***bar");
146 WCTEST("foobar", "**foo***bar");
147 WCTEST("foobar", "**foobar*");
148 WCTEST("foobar", "**foobar**");
149 WCTEST("foobar", "**foobar");
150 WCTEST("foobar", "**f?*?ar");
151 WCTEST("foobar", "**f?*b?r");
152 WCTEST("foofar", "**f?*f*r");
153 WCTEST("foofar", "**f?*f*?");
156 WCTEST("test@foo.bar.test", "*@*.bar.test");
157 WCTEST("test@foo.bar.test", "*test*@*.bar.test");
158 WCTEST("test@foo.bar.test", "*@*test");
163 WCTEST("aaaa", "*a");
164 WCTEST("aaaaa", "*a");
165 WCTEST("aaaaaa", "*a");
166 WCTEST("aaaaaaa", "*a");
167 WCTEST("aaaaaaaa", "*a");
168 WCTEST("aaaaaaaaa", "*a");
169 WCTEST("aaaaaaaaaa", "*a");
170 WCTEST("aaaaaaaaaaa", "*a");
172 WCTESTNOT("foobar", "bazqux");
173 WCTESTNOT("foobar", "*qux");
174 WCTESTNOT("foobar", "foo*x");
175 WCTESTNOT("foobar", "baz*");
176 WCTESTNOT("foobar", "foo???r");
177 WCTESTNOT("foobar", "foobars");
178 WCTESTNOT("foobar", "**foobar**h");
179 WCTESTNOT("foobar", "**foobar**h*");
180 WCTESTNOT("foobar", "**f??*bar?");
181 WCTESTNOT("foobar", "");
182 WCTESTNOT("", "foobar");
183 WCTESTNOT("OperServ", "O");
184 WCTESTNOT("O", "OperServ");
185 WCTESTNOT("foobar.tst", "fo?bar.*g");
186 WCTESTNOT("foobar.test", "fo?bar.*tt");
188 CIDRTEST("brain@1.2.3.4", "*@1.2.0.0/16");
189 CIDRTEST("brain@1.2.3.4", "*@1.2.3.0/24");
190 CIDRTEST("192.168.3.97", "192.168.3.0/24");
192 CIDRTESTNOT("brain@1.2.3.4", "x*@1.2.0.0/16");
193 CIDRTESTNOT("brain@1.2.3.4", "*@1.3.4.0/24");
194 CIDRTESTNOT("1.2.3.4", "1.2.4.0/24");
195 CIDRTESTNOT("brain@1.2.3.4", "*@/24");
196 CIDRTESTNOT("brain@1.2.3.4", "@1.2.3.4/9");
197 CIDRTESTNOT("brain@1.2.3.4", "@");
198 CIDRTESTNOT("brain@1.2.3.4", "");
204 #define STREQUALTEST(x, y) std::cout << "==(\"" << x << ",\"" << y "\") " << ((passed = (x == y)) ? "SUCCESS\n" : "FAILURE\n")
206 bool TestSuite::DoCommaSepStreamTests()
209 irc::commasepstream items("this,is,a,comma,stream");
213 while (items.GetToken(item))
220 STREQUALTEST(item, "this");
223 STREQUALTEST(item, "is");
226 STREQUALTEST(item, "a");
229 STREQUALTEST(item, "comma");
232 STREQUALTEST(item, "stream");
235 std::cout << "COMMASEPSTREAM: FAILURE: Got an index too many! " << idx << " items\n";
243 bool TestSuite::DoSpaceSepStreamTests()
247 irc::spacesepstream list("this is a space stream");
251 while (list.GetToken(item))
258 STREQUALTEST(item, "this");
261 STREQUALTEST(item, "is");
264 STREQUALTEST(item, "a");
267 STREQUALTEST(item, "space");
270 STREQUALTEST(item, "stream");
273 std::cout << "SPACESEPSTREAM: FAILURE: Got an index too many! " << idx << " items\n";
280 bool TestSuite::DoThreadTests()
282 std::string anything;
283 ThreadEngine* te = NULL;
285 std::cout << "Creating new ThreadEngine class...\n";
288 te = new ThreadEngine;
292 std::cout << "Creation failed, test failure.\n";
295 std::cout << "Creation success, type " << te->GetName() << "\n";
297 std::cout << "Allocate: new TestSuiteThread...\n";
298 TestSuiteThread* tst = new TestSuiteThread();
300 std::cout << "ThreadEngine::Create on TestSuiteThread...\n";
307 catch (CoreException &ce)
309 std::cout << "Failure: " << ce.GetReason() << std::endl;
314 std::cout << "Failure, unhandled exception\n";
317 std::cout << "Type any line and press enter to end test.\n";
318 std::cin >> anything;
320 /* Thread engine auto frees thread on delete */
321 std::cout << "Waiting for thread to exit... " << std::flush;
323 std::cout << "Done!\n";
325 std::cout << "Delete ThreadEngine... ";
327 std::cout << "Done!\n";
332 bool TestSuite::DoGenerateUIDTests()
334 bool success = RealGenerateUIDTests();
336 // Reset the UID generation state so running the tests multiple times won't mess things up
337 for (unsigned int i = 0; i < 3; i++)
338 ServerInstance->current_uid[i] = ServerInstance->Config->sid[i];
339 for (unsigned int i = 3; i < UUID_LENGTH-1; i++)
340 ServerInstance->current_uid[i] = '9';
342 ServerInstance->current_uid[UUID_LENGTH-1] = '\0';
347 bool TestSuite::RealGenerateUIDTests()
349 std::string first_uid = ServerInstance->GetUID();
350 if (first_uid.length() != UUID_LENGTH-1)
352 std::cout << "GENERATEUID: Generated UID is " << first_uid.length() << " characters long instead of " << UUID_LENGTH-1 << std::endl;
356 if (ServerInstance->current_uid[UUID_LENGTH-1] != '\0')
358 std::cout << "GENERATEUID: The null terminator is missing from the end of current_uid" << std::endl;
362 // The correct UID when generating one for the first time is ...AAAAAA
363 std::string correct_uid = ServerInstance->Config->sid + std::string(UUID_LENGTH - 4, 'A');
364 if (first_uid != correct_uid)
366 std::cout << "GENERATEUID: Generated an invalid first UID: " << first_uid << " instead of " << correct_uid << std::endl;
370 // Set current_uid to be ...Z99999
371 ServerInstance->current_uid[3] = 'Z';
372 for (unsigned int i = 4; i < UUID_LENGTH-1; i++)
373 ServerInstance->current_uid[i] = '9';
375 // Store the UID we'll be incrementing so we can display what's wrong later if necessary
376 std::string before_increment(ServerInstance->current_uid);
377 std::string generated_uid = ServerInstance->GetUID();
379 // Correct UID after incrementing ...Z99999 is ...0AAAAA
380 correct_uid = ServerInstance->Config->sid + "0" + std::string(UUID_LENGTH - 5, 'A');
382 if (generated_uid != correct_uid)
384 std::cout << "GENERATEUID: Generated an invalid UID after incrementing " << before_increment << ": " << generated_uid << " instead of " << correct_uid << std::endl;
388 // Set current_uid to be ...999999 to see if it rolls over correctly
389 for (unsigned int i = 3; i < UUID_LENGTH-1; i++)
390 ServerInstance->current_uid[i] = '9';
392 before_increment.assign(ServerInstance->current_uid);
393 generated_uid = ServerInstance->GetUID();
395 // Correct UID after rolling over is the first UID we've generated (...AAAAAA)
396 if (generated_uid != first_uid)
398 std::cout << "GENERATEUID: Generated an invalid UID after incrementing " << before_increment << ": " << generated_uid << " instead of " << first_uid << std::endl;
405 TestSuite::~TestSuite()
407 std::cout << "\n\n*** END OF TEST SUITE ***\n";