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/>.
22 #ifdef INSPIRCD_ENABLE_TESTSUITE
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): ";
80 const ModuleManager::ModuleMap& mods = ServerInstance->Modules->GetModules();
81 for (ModuleManager::ModuleMap::const_iterator i = mods.begin(); i != mods.end(); ++i)
82 i->second->OnRunTestSuite();
86 std::cout << "Enter module filename to load: ";
88 std::cout << (ServerInstance->Modules->Load(modname.c_str()) ? "\nSUCCESS!\n" : "\nFAILURE\n");
91 std::cout << "Enter module filename to unload: ";
94 Module* m = ServerInstance->Modules->Find(modname);
95 std::cout << (ServerInstance->Modules->Unload(m) ? "\nSUCCESS!\n" : "\nFAILURE\n");
96 ServerInstance->AtomicActions.Run();
100 std::cout << (DoThreadTests() ? "\nSUCCESS!\n" : "\nFAILURE\n");
103 std::cout << (DoWildTests() ? "\nSUCCESS!\n" : "\nFAILURE\n");
106 std::cout << (DoCommaSepStreamTests() ? "\nSUCCESS!\n" : "\nFAILURE\n");
109 std::cout << (DoSpaceSepStreamTests() ? "\nSUCCESS!\n" : "\nFAILURE\n");
112 std::cout << (DoGenerateUIDTests() ? "\nSUCCESS!\n" : "\nFAILURE\n");
118 std::cout << "Invalid option\n";
121 std::cout << std::endl;
125 /* Test that x matches y with match() */
126 #define WCTEST(x, y) std::cout << "match(\"" << x << "\",\"" << y "\") " << ((passed = (InspIRCd::Match(x, y, NULL))) ? " SUCCESS!\n" : " FAILURE\n")
127 /* Test that x does not match y with match() */
128 #define WCTESTNOT(x, y) std::cout << "!match(\"" << x << "\",\"" << y "\") " << ((passed = ((!InspIRCd::Match(x, y, NULL)))) ? " SUCCESS!\n" : " FAILURE\n")
130 /* Test that x matches y with match() and cidr enabled */
131 #define CIDRTEST(x, y) std::cout << "match(\"" << x << "\",\"" << y "\", true) " << ((passed = (InspIRCd::MatchCIDR(x, y, NULL))) ? " SUCCESS!\n" : " FAILURE\n")
132 /* Test that x does not match y with match() and cidr enabled */
133 #define CIDRTESTNOT(x, y) std::cout << "!match(\"" << x << "\",\"" << y "\", true) " << ((passed = ((!InspIRCd::MatchCIDR(x, y, NULL)))) ? " SUCCESS!\n" : " FAILURE\n")
135 bool TestSuite::DoWildTests()
137 std::cout << "\n\nWildcard and CIDR tests\n\n";
140 WCTEST("foobar", "*");
141 WCTEST("foobar", "foo*");
142 WCTEST("foobar", "*bar");
143 WCTEST("foobar", "foo??r");
144 WCTEST("foobar.test", "fo?bar.*t");
145 WCTEST("foobar.test", "fo?bar.t*t");
146 WCTEST("foobar.tttt", "fo?bar.**t");
147 WCTEST("foobar", "foobar");
148 WCTEST("foobar", "foo***bar");
149 WCTEST("foobar", "*foo***bar");
150 WCTEST("foobar", "**foo***bar");
151 WCTEST("foobar", "**foobar*");
152 WCTEST("foobar", "**foobar**");
153 WCTEST("foobar", "**foobar");
154 WCTEST("foobar", "**f?*?ar");
155 WCTEST("foobar", "**f?*b?r");
156 WCTEST("foofar", "**f?*f*r");
157 WCTEST("foofar", "**f?*f*?");
160 WCTEST("test@foo.bar.test", "*@*.bar.test");
161 WCTEST("test@foo.bar.test", "*test*@*.bar.test");
162 WCTEST("test@foo.bar.test", "*@*test");
167 WCTEST("aaaa", "*a");
168 WCTEST("aaaaa", "*a");
169 WCTEST("aaaaaa", "*a");
170 WCTEST("aaaaaaa", "*a");
171 WCTEST("aaaaaaaa", "*a");
172 WCTEST("aaaaaaaaa", "*a");
173 WCTEST("aaaaaaaaaa", "*a");
174 WCTEST("aaaaaaaaaaa", "*a");
176 WCTESTNOT("foobar", "bazqux");
177 WCTESTNOT("foobar", "*qux");
178 WCTESTNOT("foobar", "foo*x");
179 WCTESTNOT("foobar", "baz*");
180 WCTESTNOT("foobar", "foo???r");
181 WCTESTNOT("foobar", "foobars");
182 WCTESTNOT("foobar", "**foobar**h");
183 WCTESTNOT("foobar", "**foobar**h*");
184 WCTESTNOT("foobar", "**f??*bar?");
185 WCTESTNOT("foobar", "");
186 WCTESTNOT("", "foobar");
187 WCTESTNOT("OperServ", "O");
188 WCTESTNOT("O", "OperServ");
189 WCTESTNOT("foobar.tst", "fo?bar.*g");
190 WCTESTNOT("foobar.test", "fo?bar.*tt");
192 CIDRTEST("brain@1.2.3.4", "*@1.2.0.0/16");
193 CIDRTEST("brain@1.2.3.4", "*@1.2.3.0/24");
194 CIDRTEST("192.168.3.97", "192.168.3.0/24");
196 CIDRTESTNOT("brain@1.2.3.4", "x*@1.2.0.0/16");
197 CIDRTESTNOT("brain@1.2.3.4", "*@1.3.4.0/24");
198 CIDRTESTNOT("1.2.3.4", "1.2.4.0/24");
199 CIDRTESTNOT("brain@1.2.3.4", "*@/24");
200 CIDRTESTNOT("brain@1.2.3.4", "@1.2.3.4/9");
201 CIDRTESTNOT("brain@1.2.3.4", "@");
202 CIDRTESTNOT("brain@1.2.3.4", "");
208 #define STREQUALTEST(x, y) std::cout << "==(\"" << x << ",\"" << y "\") " << ((passed = (x == y)) ? "SUCCESS\n" : "FAILURE\n")
210 bool TestSuite::DoCommaSepStreamTests()
213 irc::commasepstream items("this,is,a,comma,stream");
217 while (items.GetToken(item))
224 STREQUALTEST(item, "this");
227 STREQUALTEST(item, "is");
230 STREQUALTEST(item, "a");
233 STREQUALTEST(item, "comma");
236 STREQUALTEST(item, "stream");
239 std::cout << "COMMASEPSTREAM: FAILURE: Got an index too many! " << idx << " items\n";
247 bool TestSuite::DoSpaceSepStreamTests()
251 irc::spacesepstream list("this is a space stream");
255 while (list.GetToken(item))
262 STREQUALTEST(item, "this");
265 STREQUALTEST(item, "is");
268 STREQUALTEST(item, "a");
271 STREQUALTEST(item, "space");
274 STREQUALTEST(item, "stream");
277 std::cout << "SPACESEPSTREAM: FAILURE: Got an index too many! " << idx << " items\n";
284 bool TestSuite::DoThreadTests()
286 std::string anything;
287 ThreadEngine* te = NULL;
289 std::cout << "Creating new ThreadEngine class...\n";
292 te = new ThreadEngine;
296 std::cout << "Creation failed, test failure.\n";
299 std::cout << "Creation success, type " << te->GetName() << "\n";
301 std::cout << "Allocate: new TestSuiteThread...\n";
302 TestSuiteThread* tst = new TestSuiteThread();
304 std::cout << "ThreadEngine::Create on TestSuiteThread...\n";
311 catch (CoreException &ce)
313 std::cout << "Failure: " << ce.GetReason() << std::endl;
318 std::cout << "Failure, unhandled exception\n";
321 std::cout << "Type any line and press enter to end test.\n";
322 std::cin >> anything;
324 /* Thread engine auto frees thread on delete */
325 std::cout << "Waiting for thread to exit... " << std::flush;
327 std::cout << "Done!\n";
329 std::cout << "Delete ThreadEngine... ";
331 std::cout << "Done!\n";
336 bool TestSuite::DoGenerateUIDTests()
338 const unsigned int UUID_LENGTH = UIDGenerator::UUID_LENGTH;
340 uidgen.init(ServerInstance->Config->GetSID());
341 std::string first_uid = uidgen.GetUID();
343 if (first_uid.length() != UUID_LENGTH)
345 std::cout << "GENERATEUID: Generated UID is " << first_uid.length() << " characters long instead of " << UUID_LENGTH-1 << std::endl;
349 if (uidgen.current_uid.c_str()[UUID_LENGTH] != '\0')
351 std::cout << "GENERATEUID: The null terminator is missing from the end of current_uid" << std::endl;
355 // The correct UID when generating one for the first time is ...AAAAAA
356 std::string correct_uid = ServerInstance->Config->sid + std::string(UUID_LENGTH - 3, 'A');
357 if (first_uid != correct_uid)
359 std::cout << "GENERATEUID: Generated an invalid first UID: " << first_uid << " instead of " << correct_uid << std::endl;
363 // Set current_uid to be ...Z99999
364 uidgen.current_uid[3] = 'Z';
365 for (unsigned int i = 4; i < UUID_LENGTH; i++)
366 uidgen.current_uid[i] = '9';
368 // Store the UID we'll be incrementing so we can display what's wrong later if necessary
369 std::string before_increment(uidgen.current_uid);
370 std::string generated_uid = uidgen.GetUID();
372 // Correct UID after incrementing ...Z99999 is ...0AAAAA
373 correct_uid = ServerInstance->Config->sid + "0" + std::string(UUID_LENGTH - 4, 'A');
375 if (generated_uid != correct_uid)
377 std::cout << "GENERATEUID: Generated an invalid UID after incrementing " << before_increment << ": " << generated_uid << " instead of " << correct_uid << std::endl;
381 // Set current_uid to be ...999999 to see if it rolls over correctly
382 for (unsigned int i = 3; i < UUID_LENGTH; i++)
383 uidgen.current_uid[i] = '9';
385 before_increment.assign(uidgen.current_uid);
386 generated_uid = uidgen.GetUID();
388 // Correct UID after rolling over is the first UID we've generated (...AAAAAA)
389 if (generated_uid != first_uid)
391 std::cout << "GENERATEUID: Generated an invalid UID after incrementing " << before_increment << ": " << generated_uid << " instead of " << first_uid << std::endl;
398 TestSuite::~TestSuite()
400 std::cout << "\n\n*** END OF TEST SUITE ***\n";