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"
28 class TestSuiteThread : public Thread
31 TestSuiteThread() : Thread()
35 virtual ~TestSuiteThread()
41 while (GetExitFlag() == false)
43 std::cout << "Test suite thread run...\n";
49 TestSuite::TestSuite()
51 std::cout << "\n\n*** STARTING TESTSUITE ***\n";
58 std::cout << "(1) Call all module OnRunTestSuite() methods\n";
59 std::cout << "(2) Load a module\n";
60 std::cout << "(3) Unload a module\n";
61 std::cout << "(4) Threading tests\n";
62 std::cout << "(5) Wildcard and CIDR tests\n";
63 std::cout << "(6) Comma sepstream tests\n";
64 std::cout << "(7) Space sepstream tests\n";
65 std::cout << "(8) UID generation tests\n";
67 std::cout << std::endl << "(X) Exit test suite\n";
69 std::cout << "\nChoices (Enter one or more options as a list then press enter, e.g. 15X): ";
79 const ModuleManager::ModuleMap& mods = ServerInstance->Modules->GetModules();
80 for (ModuleManager::ModuleMap::const_iterator i = mods.begin(); i != mods.end(); ++i)
81 i->second->OnRunTestSuite();
85 std::cout << "Enter module filename to load: ";
87 std::cout << (ServerInstance->Modules->Load(modname.c_str()) ? "\nSUCCESS!\n" : "\nFAILURE\n");
90 std::cout << "Enter module filename to unload: ";
93 Module* m = ServerInstance->Modules->Find(modname);
94 std::cout << (ServerInstance->Modules->Unload(m) ? "\nSUCCESS!\n" : "\nFAILURE\n");
95 ServerInstance->AtomicActions.Run();
99 std::cout << (DoThreadTests() ? "\nSUCCESS!\n" : "\nFAILURE\n");
102 std::cout << (DoWildTests() ? "\nSUCCESS!\n" : "\nFAILURE\n");
105 std::cout << (DoCommaSepStreamTests() ? "\nSUCCESS!\n" : "\nFAILURE\n");
108 std::cout << (DoSpaceSepStreamTests() ? "\nSUCCESS!\n" : "\nFAILURE\n");
111 std::cout << (DoGenerateUIDTests() ? "\nSUCCESS!\n" : "\nFAILURE\n");
117 std::cout << "Invalid option\n";
120 std::cout << std::endl;
124 /* Test that x matches y with match() */
125 #define WCTEST(x, y) std::cout << "match(\"" << x << "\",\"" << y "\") " << ((passed = (InspIRCd::Match(x, y, NULL))) ? " SUCCESS!\n" : " FAILURE\n")
126 /* Test that x does not match y with match() */
127 #define WCTESTNOT(x, y) std::cout << "!match(\"" << x << "\",\"" << y "\") " << ((passed = ((!InspIRCd::Match(x, y, NULL)))) ? " SUCCESS!\n" : " FAILURE\n")
129 /* Test that x matches y with match() and cidr enabled */
130 #define CIDRTEST(x, y) std::cout << "match(\"" << x << "\",\"" << y "\", true) " << ((passed = (InspIRCd::MatchCIDR(x, y, NULL))) ? " SUCCESS!\n" : " FAILURE\n")
131 /* Test that x does not match y with match() and cidr enabled */
132 #define CIDRTESTNOT(x, y) std::cout << "!match(\"" << x << "\",\"" << y "\", true) " << ((passed = ((!InspIRCd::MatchCIDR(x, y, NULL)))) ? " SUCCESS!\n" : " FAILURE\n")
134 bool TestSuite::DoWildTests()
136 std::cout << "\n\nWildcard and CIDR tests\n\n";
139 WCTEST("foobar", "*");
140 WCTEST("foobar", "foo*");
141 WCTEST("foobar", "*bar");
142 WCTEST("foobar", "foo??r");
143 WCTEST("foobar.test", "fo?bar.*t");
144 WCTEST("foobar.test", "fo?bar.t*t");
145 WCTEST("foobar.tttt", "fo?bar.**t");
146 WCTEST("foobar", "foobar");
147 WCTEST("foobar", "foo***bar");
148 WCTEST("foobar", "*foo***bar");
149 WCTEST("foobar", "**foo***bar");
150 WCTEST("foobar", "**foobar*");
151 WCTEST("foobar", "**foobar**");
152 WCTEST("foobar", "**foobar");
153 WCTEST("foobar", "**f?*?ar");
154 WCTEST("foobar", "**f?*b?r");
155 WCTEST("foofar", "**f?*f*r");
156 WCTEST("foofar", "**f?*f*?");
159 WCTEST("test@foo.bar.test", "*@*.bar.test");
160 WCTEST("test@foo.bar.test", "*test*@*.bar.test");
161 WCTEST("test@foo.bar.test", "*@*test");
166 WCTEST("aaaa", "*a");
167 WCTEST("aaaaa", "*a");
168 WCTEST("aaaaaa", "*a");
169 WCTEST("aaaaaaa", "*a");
170 WCTEST("aaaaaaaa", "*a");
171 WCTEST("aaaaaaaaa", "*a");
172 WCTEST("aaaaaaaaaa", "*a");
173 WCTEST("aaaaaaaaaaa", "*a");
175 WCTESTNOT("foobar", "bazqux");
176 WCTESTNOT("foobar", "*qux");
177 WCTESTNOT("foobar", "foo*x");
178 WCTESTNOT("foobar", "baz*");
179 WCTESTNOT("foobar", "foo???r");
180 WCTESTNOT("foobar", "foobars");
181 WCTESTNOT("foobar", "**foobar**h");
182 WCTESTNOT("foobar", "**foobar**h*");
183 WCTESTNOT("foobar", "**f??*bar?");
184 WCTESTNOT("foobar", "");
185 WCTESTNOT("", "foobar");
186 WCTESTNOT("OperServ", "O");
187 WCTESTNOT("O", "OperServ");
188 WCTESTNOT("foobar.tst", "fo?bar.*g");
189 WCTESTNOT("foobar.test", "fo?bar.*tt");
191 CIDRTEST("brain@1.2.3.4", "*@1.2.0.0/16");
192 CIDRTEST("brain@1.2.3.4", "*@1.2.3.0/24");
193 CIDRTEST("192.168.3.97", "192.168.3.0/24");
195 CIDRTESTNOT("brain@1.2.3.4", "x*@1.2.0.0/16");
196 CIDRTESTNOT("brain@1.2.3.4", "*@1.3.4.0/24");
197 CIDRTESTNOT("1.2.3.4", "1.2.4.0/24");
198 CIDRTESTNOT("brain@1.2.3.4", "*@/24");
199 CIDRTESTNOT("brain@1.2.3.4", "@1.2.3.4/9");
200 CIDRTESTNOT("brain@1.2.3.4", "@");
201 CIDRTESTNOT("brain@1.2.3.4", "");
207 #define STREQUALTEST(x, y) std::cout << "==(\"" << x << ",\"" << y "\") " << ((passed = (x == y)) ? "SUCCESS\n" : "FAILURE\n")
209 bool TestSuite::DoCommaSepStreamTests()
212 irc::commasepstream items("this,is,a,comma,stream");
216 while (items.GetToken(item))
223 STREQUALTEST(item, "this");
226 STREQUALTEST(item, "is");
229 STREQUALTEST(item, "a");
232 STREQUALTEST(item, "comma");
235 STREQUALTEST(item, "stream");
238 std::cout << "COMMASEPSTREAM: FAILURE: Got an index too many! " << idx << " items\n";
246 bool TestSuite::DoSpaceSepStreamTests()
250 irc::spacesepstream list("this is a space stream");
254 while (list.GetToken(item))
261 STREQUALTEST(item, "this");
264 STREQUALTEST(item, "is");
267 STREQUALTEST(item, "a");
270 STREQUALTEST(item, "space");
273 STREQUALTEST(item, "stream");
276 std::cout << "SPACESEPSTREAM: FAILURE: Got an index too many! " << idx << " items\n";
283 bool TestSuite::DoThreadTests()
285 std::string anything;
286 ThreadEngine* te = NULL;
288 std::cout << "Creating new ThreadEngine class...\n";
291 te = new ThreadEngine;
295 std::cout << "Creation failed, test failure.\n";
298 std::cout << "Creation success, type " << te->GetName() << "\n";
300 std::cout << "Allocate: new TestSuiteThread...\n";
301 TestSuiteThread* tst = new TestSuiteThread();
303 std::cout << "ThreadEngine::Create on TestSuiteThread...\n";
310 catch (CoreException &ce)
312 std::cout << "Failure: " << ce.GetReason() << std::endl;
317 std::cout << "Failure, unhandled exception\n";
320 std::cout << "Type any line and press enter to end test.\n";
321 std::cin >> anything;
323 /* Thread engine auto frees thread on delete */
324 std::cout << "Waiting for thread to exit... " << std::flush;
326 std::cout << "Done!\n";
328 std::cout << "Delete ThreadEngine... ";
330 std::cout << "Done!\n";
335 bool TestSuite::DoGenerateUIDTests()
337 const unsigned int UUID_LENGTH = UIDGenerator::UUID_LENGTH;
339 uidgen.init(ServerInstance->Config->GetSID());
340 std::string first_uid = uidgen.GetUID();
342 if (first_uid.length() != UUID_LENGTH)
344 std::cout << "GENERATEUID: Generated UID is " << first_uid.length() << " characters long instead of " << UUID_LENGTH-1 << std::endl;
348 if (uidgen.current_uid.c_str()[UUID_LENGTH] != '\0')
350 std::cout << "GENERATEUID: The null terminator is missing from the end of current_uid" << std::endl;
354 // The correct UID when generating one for the first time is ...AAAAAA
355 std::string correct_uid = ServerInstance->Config->sid + std::string(UUID_LENGTH - 3, 'A');
356 if (first_uid != correct_uid)
358 std::cout << "GENERATEUID: Generated an invalid first UID: " << first_uid << " instead of " << correct_uid << std::endl;
362 // Set current_uid to be ...Z99999
363 uidgen.current_uid[3] = 'Z';
364 for (unsigned int i = 4; i < UUID_LENGTH; i++)
365 uidgen.current_uid[i] = '9';
367 // Store the UID we'll be incrementing so we can display what's wrong later if necessary
368 std::string before_increment(uidgen.current_uid);
369 std::string generated_uid = uidgen.GetUID();
371 // Correct UID after incrementing ...Z99999 is ...0AAAAA
372 correct_uid = ServerInstance->Config->sid + "0" + std::string(UUID_LENGTH - 4, 'A');
374 if (generated_uid != correct_uid)
376 std::cout << "GENERATEUID: Generated an invalid UID after incrementing " << before_increment << ": " << generated_uid << " instead of " << correct_uid << std::endl;
380 // Set current_uid to be ...999999 to see if it rolls over correctly
381 for (unsigned int i = 3; i < UUID_LENGTH; i++)
382 uidgen.current_uid[i] = '9';
384 before_increment.assign(uidgen.current_uid);
385 generated_uid = uidgen.GetUID();
387 // Correct UID after rolling over is the first UID we've generated (...AAAAAA)
388 if (generated_uid != first_uid)
390 std::cout << "GENERATEUID: Generated an invalid UID after incrementing " << before_increment << ": " << generated_uid << " instead of " << first_uid << std::endl;
397 TestSuite::~TestSuite()
399 std::cout << "\n\n*** END OF TEST SUITE ***\n";