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/>.
23 #include "testsuite.h"
24 #include "threadengine.h"
27 class TestSuiteThread : public Thread
30 TestSuiteThread() : Thread()
34 virtual ~TestSuiteThread()
40 while (GetExitFlag() == false)
42 std::cout << "Test suite thread run...\n";
48 TestSuite::TestSuite()
50 std::cout << "\n\n*** STARTING TESTSUITE ***\n";
57 std::cout << "(1) Call all module OnRunTestSuite() methods\n";
58 std::cout << "(2) Load a module\n";
59 std::cout << "(3) Unload a module\n";
60 std::cout << "(4) Threading tests\n";
61 std::cout << "(5) Wildcard and CIDR tests\n";
62 std::cout << "(6) Comma sepstream tests\n";
63 std::cout << "(7) Space sepstream tests\n";
64 std::cout << "(8) UID generation tests\n";
66 std::cout << std::endl << "(X) Exit test suite\n";
68 std::cout << "\nChoices (Enter one or more options as a list then press enter, e.g. 15X): ";
77 FOREACH_MOD(I_OnRunTestSuite, OnRunTestSuite());
80 std::cout << "Enter module filename to load: ";
82 std::cout << (ServerInstance->Modules->Load(modname.c_str()) ? "\nSUCCESS!\n" : "\nFAILURE\n");
85 std::cout << "Enter module filename to unload: ";
88 Module* m = ServerInstance->Modules->Find(modname);
89 std::cout << (ServerInstance->Modules->Unload(m) ? "\nSUCCESS!\n" : "\nFAILURE\n");
90 ServerInstance->AtomicActions.Run();
94 std::cout << (DoThreadTests() ? "\nSUCCESS!\n" : "\nFAILURE\n");
97 std::cout << (DoWildTests() ? "\nSUCCESS!\n" : "\nFAILURE\n");
100 std::cout << (DoCommaSepStreamTests() ? "\nSUCCESS!\n" : "\nFAILURE\n");
103 std::cout << (DoSpaceSepStreamTests() ? "\nSUCCESS!\n" : "\nFAILURE\n");
106 std::cout << (DoGenerateUIDTests() ? "\nSUCCESS!\n" : "\nFAILURE\n");
112 std::cout << "Invalid option\n";
115 std::cout << std::endl;
119 /* Test that x matches y with match() */
120 #define WCTEST(x, y) std::cout << "match(\"" << x << "\",\"" << y "\") " << ((passed = (InspIRCd::Match(x, y, NULL))) ? " SUCCESS!\n" : " FAILURE\n")
121 /* Test that x does not match y with match() */
122 #define WCTESTNOT(x, y) std::cout << "!match(\"" << x << "\",\"" << y "\") " << ((passed = ((!InspIRCd::Match(x, y, NULL)))) ? " SUCCESS!\n" : " FAILURE\n")
124 /* Test that x matches y with match() and cidr enabled */
125 #define CIDRTEST(x, y) std::cout << "match(\"" << x << "\",\"" << y "\", true) " << ((passed = (InspIRCd::MatchCIDR(x, y, NULL))) ? " SUCCESS!\n" : " FAILURE\n")
126 /* Test that x does not match y with match() and cidr enabled */
127 #define CIDRTESTNOT(x, y) std::cout << "!match(\"" << x << "\",\"" << y "\", true) " << ((passed = ((!InspIRCd::MatchCIDR(x, y, NULL)))) ? " SUCCESS!\n" : " FAILURE\n")
129 bool TestSuite::DoWildTests()
131 std::cout << "\n\nWildcard and CIDR tests\n\n";
134 WCTEST("foobar", "*");
135 WCTEST("foobar", "foo*");
136 WCTEST("foobar", "*bar");
137 WCTEST("foobar", "foo??r");
138 WCTEST("foobar.test", "fo?bar.*t");
139 WCTEST("foobar.test", "fo?bar.t*t");
140 WCTEST("foobar.tttt", "fo?bar.**t");
141 WCTEST("foobar", "foobar");
142 WCTEST("foobar", "foo***bar");
143 WCTEST("foobar", "*foo***bar");
144 WCTEST("foobar", "**foo***bar");
145 WCTEST("foobar", "**foobar*");
146 WCTEST("foobar", "**foobar**");
147 WCTEST("foobar", "**foobar");
148 WCTEST("foobar", "**f?*?ar");
149 WCTEST("foobar", "**f?*b?r");
150 WCTEST("foofar", "**f?*f*r");
151 WCTEST("foofar", "**f?*f*?");
154 WCTEST("test@foo.bar.test", "*@*.bar.test");
155 WCTEST("test@foo.bar.test", "*test*@*.bar.test");
156 WCTEST("test@foo.bar.test", "*@*test");
161 WCTEST("aaaa", "*a");
162 WCTEST("aaaaa", "*a");
163 WCTEST("aaaaaa", "*a");
164 WCTEST("aaaaaaa", "*a");
165 WCTEST("aaaaaaaa", "*a");
166 WCTEST("aaaaaaaaa", "*a");
167 WCTEST("aaaaaaaaaa", "*a");
168 WCTEST("aaaaaaaaaaa", "*a");
170 WCTESTNOT("foobar", "bazqux");
171 WCTESTNOT("foobar", "*qux");
172 WCTESTNOT("foobar", "foo*x");
173 WCTESTNOT("foobar", "baz*");
174 WCTESTNOT("foobar", "foo???r");
175 WCTESTNOT("foobar", "foobars");
176 WCTESTNOT("foobar", "**foobar**h");
177 WCTESTNOT("foobar", "**foobar**h*");
178 WCTESTNOT("foobar", "**f??*bar?");
179 WCTESTNOT("foobar", "");
180 WCTESTNOT("", "foobar");
181 WCTESTNOT("OperServ", "O");
182 WCTESTNOT("O", "OperServ");
183 WCTESTNOT("foobar.tst", "fo?bar.*g");
184 WCTESTNOT("foobar.test", "fo?bar.*tt");
186 CIDRTEST("brain@1.2.3.4", "*@1.2.0.0/16");
187 CIDRTEST("brain@1.2.3.4", "*@1.2.3.0/24");
188 CIDRTEST("192.168.3.97", "192.168.3.0/24");
190 CIDRTESTNOT("brain@1.2.3.4", "x*@1.2.0.0/16");
191 CIDRTESTNOT("brain@1.2.3.4", "*@1.3.4.0/24");
192 CIDRTESTNOT("1.2.3.4", "1.2.4.0/24");
193 CIDRTESTNOT("brain@1.2.3.4", "*@/24");
194 CIDRTESTNOT("brain@1.2.3.4", "@1.2.3.4/9");
195 CIDRTESTNOT("brain@1.2.3.4", "@");
196 CIDRTESTNOT("brain@1.2.3.4", "");
202 #define STREQUALTEST(x, y) std::cout << "==(\"" << x << ",\"" << y "\") " << ((passed = (x == y)) ? "SUCCESS\n" : "FAILURE\n")
204 bool TestSuite::DoCommaSepStreamTests()
207 irc::commasepstream items("this,is,a,comma,stream");
211 while (items.GetToken(item))
218 STREQUALTEST(item, "this");
221 STREQUALTEST(item, "is");
224 STREQUALTEST(item, "a");
227 STREQUALTEST(item, "comma");
230 STREQUALTEST(item, "stream");
233 std::cout << "COMMASEPSTREAM: FAILURE: Got an index too many! " << idx << " items\n";
241 bool TestSuite::DoSpaceSepStreamTests()
245 irc::spacesepstream list("this is a space stream");
249 while (list.GetToken(item))
256 STREQUALTEST(item, "this");
259 STREQUALTEST(item, "is");
262 STREQUALTEST(item, "a");
265 STREQUALTEST(item, "space");
268 STREQUALTEST(item, "stream");
271 std::cout << "SPACESEPSTREAM: FAILURE: Got an index too many! " << idx << " items\n";
278 bool TestSuite::DoThreadTests()
280 std::string anything;
281 ThreadEngine* te = NULL;
283 std::cout << "Creating new ThreadEngine class...\n";
286 te = new ThreadEngine;
290 std::cout << "Creation failed, test failure.\n";
293 std::cout << "Creation success, type " << te->GetName() << "\n";
295 std::cout << "Allocate: new TestSuiteThread...\n";
296 TestSuiteThread* tst = new TestSuiteThread();
298 std::cout << "ThreadEngine::Create on TestSuiteThread...\n";
305 catch (CoreException &ce)
307 std::cout << "Failure: " << ce.GetReason() << std::endl;
312 std::cout << "Failure, unhandled exception\n";
315 std::cout << "Type any line and press enter to end test.\n";
316 std::cin >> anything;
318 /* Thread engine auto frees thread on delete */
319 std::cout << "Waiting for thread to exit... " << std::flush;
321 std::cout << "Done!\n";
323 std::cout << "Delete ThreadEngine... ";
325 std::cout << "Done!\n";
330 bool TestSuite::DoGenerateUIDTests()
332 const unsigned int UUID_LENGTH = UIDGenerator::UUID_LENGTH;
334 uidgen.init(ServerInstance->Config->GetSID());
335 std::string first_uid = uidgen.GetUID();
337 if (first_uid.length() != UUID_LENGTH)
339 std::cout << "GENERATEUID: Generated UID is " << first_uid.length() << " characters long instead of " << UUID_LENGTH-1 << std::endl;
343 if (uidgen.current_uid.c_str()[UUID_LENGTH] != '\0')
345 std::cout << "GENERATEUID: The null terminator is missing from the end of current_uid" << std::endl;
349 // The correct UID when generating one for the first time is ...AAAAAA
350 std::string correct_uid = ServerInstance->Config->sid + std::string(UUID_LENGTH - 3, 'A');
351 if (first_uid != correct_uid)
353 std::cout << "GENERATEUID: Generated an invalid first UID: " << first_uid << " instead of " << correct_uid << std::endl;
357 // Set current_uid to be ...Z99999
358 uidgen.current_uid[3] = 'Z';
359 for (unsigned int i = 4; i < UUID_LENGTH; i++)
360 uidgen.current_uid[i] = '9';
362 // Store the UID we'll be incrementing so we can display what's wrong later if necessary
363 std::string before_increment(uidgen.current_uid);
364 std::string generated_uid = uidgen.GetUID();
366 // Correct UID after incrementing ...Z99999 is ...0AAAAA
367 correct_uid = ServerInstance->Config->sid + "0" + std::string(UUID_LENGTH - 4, 'A');
369 if (generated_uid != correct_uid)
371 std::cout << "GENERATEUID: Generated an invalid UID after incrementing " << before_increment << ": " << generated_uid << " instead of " << correct_uid << std::endl;
375 // Set current_uid to be ...999999 to see if it rolls over correctly
376 for (unsigned int i = 3; i < UUID_LENGTH; i++)
377 uidgen.current_uid[i] = '9';
379 before_increment.assign(uidgen.current_uid);
380 generated_uid = uidgen.GetUID();
382 // Correct UID after rolling over is the first UID we've generated (...AAAAAA)
383 if (generated_uid != first_uid)
385 std::cout << "GENERATEUID: Generated an invalid UID after incrementing " << before_increment << ": " << generated_uid << " instead of " << first_uid << std::endl;
392 TestSuite::~TestSuite()
394 std::cout << "\n\n*** END OF TEST SUITE ***\n";