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";
69 cout << endl << "(X) Exit test suite\n";
71 cout << "\nChoices (Enter one or more options as a list then press enter, e.g. 15X): ";
80 FOREACH_MOD(I_OnRunTestSuite, OnRunTestSuite());
83 cout << "Enter module filename to load: ";
85 cout << (ServerInstance->Modules->Load(modname.c_str()) ? "\nSUCCESS!\n" : "\nFAILURE\n");
88 cout << "Enter module filename to unload: ";
91 Module* m = ServerInstance->Modules->Find(modname);
92 cout << (ServerInstance->Modules->Unload(m) ? "\nSUCCESS!\n" : "\nFAILURE\n");
93 ServerInstance->AtomicActions.Run();
97 cout << (DoThreadTests() ? "\nSUCCESS!\n" : "\nFAILURE\n");
100 cout << (DoWildTests() ? "\nSUCCESS!\n" : "\nFAILURE\n");
103 cout << (DoCommaSepStreamTests() ? "\nSUCCESS!\n" : "\nFAILURE\n");
106 cout << (DoSpaceSepStreamTests() ? "\nSUCCESS!\n" : "\nFAILURE\n");
112 cout << "Invalid option\n";
119 /* Test that x matches y with match() */
120 #define WCTEST(x, y) 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) 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) 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) cout << "!match(\"" << x << "\",\"" << y "\", true) " << ((passed = ((!InspIRCd::MatchCIDR(x, y, NULL)))) ? " SUCCESS!\n" : " FAILURE\n")
129 bool TestSuite::DoWildTests()
131 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) 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 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 cout << "SPACESEPSTREAM: FAILURE: Got an index too many! " << idx << " items\n";
278 bool TestSuite::DoThreadTests()
280 std::string anything;
281 ThreadEngine* te = NULL;
283 cout << "Creating new ThreadEngine class...\n";
286 te = new ThreadEngine;
290 cout << "Creation failed, test failure.\n";
293 cout << "Creation success, type " << te->GetName() << "\n";
295 cout << "Allocate: new TestSuiteThread...\n";
296 TestSuiteThread* tst = new TestSuiteThread();
298 cout << "ThreadEngine::Create on TestSuiteThread...\n";
305 catch (CoreException &ce)
307 cout << "Failure: " << ce.GetReason() << endl;
312 cout << "Failure, unhandled exception\n";
315 cout << "Type any line and press enter to end test.\n";
318 /* Thread engine auto frees thread on delete */
319 cout << "Waiting for thread to exit... " << flush;
323 cout << "Delete ThreadEngine... ";
330 TestSuite::~TestSuite()
332 cout << "\n\n*** END OF TEST SUITE ***\n";