1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2009 InspIRCd Development Team
6 * See: http://wiki.inspircd.org/Credits
8 * This program is free but copyrighted software; see
9 * the file COPYING for details.
11 * ---------------------------------------------------
17 #include "testsuite.h"
18 #include "threadengine.h"
23 class TestSuiteThread : public Thread
26 TestSuiteThread() : Thread()
30 virtual ~TestSuiteThread()
36 while (GetExitFlag() == false)
38 cout << "Test suite thread run...\n";
44 TestSuite::TestSuite()
46 cout << "\n\n*** STARTING TESTSUITE ***\n";
53 cout << "(1) Call all module OnRunTestSuite() methods\n";
54 cout << "(2) Load a module\n";
55 cout << "(3) Unload a module\n";
56 cout << "(4) Threading tests\n";
57 cout << "(5) Wildcard and CIDR tests\n";
58 cout << "(6) Comma sepstream tests\n";
59 cout << "(7) Space sepstream tests\n";
61 cout << endl << "(X) Exit test suite\n";
63 cout << "\nChoices (Enter one or more options as a list then press enter, e.g. 15X): ";
72 FOREACH_MOD(I_OnRunTestSuite, OnRunTestSuite());
75 cout << "Enter module filename to load: ";
77 cout << (ServerInstance->Modules->Load(modname.c_str()) ? "\nSUCCESS!\n" : "\nFAILURE\n");
80 cout << "Enter module filename to unload: ";
83 Module* m = ServerInstance->Modules->Find(modname);
84 cout << (ServerInstance->Modules->Unload(m) ? "\nSUCCESS!\n" : "\nFAILURE\n");
85 ServerInstance->AtomicActions.Run();
89 cout << (DoThreadTests() ? "\nSUCCESS!\n" : "\nFAILURE\n");
92 cout << (DoWildTests() ? "\nSUCCESS!\n" : "\nFAILURE\n");
95 cout << (DoCommaSepStreamTests() ? "\nSUCCESS!\n" : "\nFAILURE\n");
98 cout << (DoSpaceSepStreamTests() ? "\nSUCCESS!\n" : "\nFAILURE\n");
104 cout << "Invalid option\n";
111 /* Test that x matches y with match() */
112 #define WCTEST(x, y) cout << "match(\"" << x << "\",\"" << y "\") " << ((passed = (InspIRCd::Match(x, y, NULL))) ? " SUCCESS!\n" : " FAILURE\n")
113 /* Test that x does not match y with match() */
114 #define WCTESTNOT(x, y) cout << "!match(\"" << x << "\",\"" << y "\") " << ((passed = ((!InspIRCd::Match(x, y, NULL)))) ? " SUCCESS!\n" : " FAILURE\n")
116 /* Test that x matches y with match() and cidr enabled */
117 #define CIDRTEST(x, y) cout << "match(\"" << x << "\",\"" << y "\", true) " << ((passed = (InspIRCd::MatchCIDR(x, y, NULL))) ? " SUCCESS!\n" : " FAILURE\n")
118 /* Test that x does not match y with match() and cidr enabled */
119 #define CIDRTESTNOT(x, y) cout << "!match(\"" << x << "\",\"" << y "\", true) " << ((passed = ((!InspIRCd::MatchCIDR(x, y, NULL)))) ? " SUCCESS!\n" : " FAILURE\n")
121 bool TestSuite::DoWildTests()
123 cout << "\n\nWildcard and CIDR tests\n\n";
126 WCTEST("foobar", "*");
127 WCTEST("foobar", "foo*");
128 WCTEST("foobar", "*bar");
129 WCTEST("foobar", "foo??r");
130 WCTEST("foobar.test", "fo?bar.*t");
131 WCTEST("foobar.test", "fo?bar.t*t");
132 WCTEST("foobar.tttt", "fo?bar.**t");
133 WCTEST("foobar", "foobar");
134 WCTEST("foobar", "foo***bar");
135 WCTEST("foobar", "*foo***bar");
136 WCTEST("foobar", "**foo***bar");
137 WCTEST("foobar", "**foobar*");
138 WCTEST("foobar", "**foobar**");
139 WCTEST("foobar", "**foobar");
140 WCTEST("foobar", "**f?*?ar");
141 WCTEST("foobar", "**f?*b?r");
142 WCTEST("foofar", "**f?*f*r");
143 WCTEST("foofar", "**f?*f*?");
146 WCTEST("test@foo.bar.test", "*@*.bar.test");
147 WCTEST("test@foo.bar.test", "*test*@*.bar.test");
148 WCTEST("test@foo.bar.test", "*@*test");
153 WCTEST("aaaa", "*a");
154 WCTEST("aaaaa", "*a");
155 WCTEST("aaaaaa", "*a");
156 WCTEST("aaaaaaa", "*a");
157 WCTEST("aaaaaaaa", "*a");
158 WCTEST("aaaaaaaaa", "*a");
159 WCTEST("aaaaaaaaaa", "*a");
160 WCTEST("aaaaaaaaaaa", "*a");
162 WCTESTNOT("foobar", "bazqux");
163 WCTESTNOT("foobar", "*qux");
164 WCTESTNOT("foobar", "foo*x");
165 WCTESTNOT("foobar", "baz*");
166 WCTESTNOT("foobar", "foo???r");
167 WCTESTNOT("foobar", "foobars");
168 WCTESTNOT("foobar", "**foobar**h");
169 WCTESTNOT("foobar", "**foobar**h*");
170 WCTESTNOT("foobar", "**f??*bar?");
171 WCTESTNOT("foobar", "");
172 WCTESTNOT("", "foobar");
173 WCTESTNOT("OperServ", "O");
174 WCTESTNOT("O", "OperServ");
175 WCTESTNOT("foobar.tst", "fo?bar.*g");
176 WCTESTNOT("foobar.test", "fo?bar.*tt");
178 CIDRTEST("brain@1.2.3.4", "*@1.2.0.0/16");
179 CIDRTEST("brain@1.2.3.4", "*@1.2.3.0/24");
180 CIDRTEST("192.168.3.97", "192.168.3.0/24");
182 CIDRTESTNOT("brain@1.2.3.4", "x*@1.2.0.0/16");
183 CIDRTESTNOT("brain@1.2.3.4", "*@1.3.4.0/24");
184 CIDRTESTNOT("1.2.3.4", "1.2.4.0/24");
185 CIDRTESTNOT("brain@1.2.3.4", "*@/24");
186 CIDRTESTNOT("brain@1.2.3.4", "@1.2.3.4/9");
187 CIDRTESTNOT("brain@1.2.3.4", "@");
188 CIDRTESTNOT("brain@1.2.3.4", "");
194 #define STREQUALTEST(x, y) cout << "==(\"" << x << ",\"" << y "\") " << ((passed = (x == y)) ? "SUCCESS\n" : "FAILURE\n")
196 bool TestSuite::DoCommaSepStreamTests()
199 irc::commasepstream items("this,is,a,comma,stream");
203 while (items.GetToken(item))
210 STREQUALTEST(item, "this");
213 STREQUALTEST(item, "is");
216 STREQUALTEST(item, "a");
219 STREQUALTEST(item, "comma");
222 STREQUALTEST(item, "stream");
225 cout << "COMMASEPSTREAM: FAILURE: Got an index too many! " << idx << " items\n";
233 bool TestSuite::DoSpaceSepStreamTests()
237 irc::spacesepstream list("this is a space stream");
241 while (list.GetToken(item))
248 STREQUALTEST(item, "this");
251 STREQUALTEST(item, "is");
254 STREQUALTEST(item, "a");
257 STREQUALTEST(item, "space");
260 STREQUALTEST(item, "stream");
263 cout << "SPACESEPSTREAM: FAILURE: Got an index too many! " << idx << " items\n";
270 bool TestSuite::DoThreadTests()
272 std::string anything;
273 ThreadEngine* te = NULL;
275 cout << "Creating new ThreadEngine class...\n";
278 te = new ThreadEngine;
282 cout << "Creation failed, test failure.\n";
285 cout << "Creation success, type " << te->GetName() << "\n";
287 cout << "Allocate: new TestSuiteThread...\n";
288 TestSuiteThread* tst = new TestSuiteThread();
290 cout << "ThreadEngine::Create on TestSuiteThread...\n";
297 catch (CoreException &ce)
299 cout << "Failure: " << ce.GetReason() << endl;
304 cout << "Failure, unhandled exception\n";
307 cout << "Type any line and press enter to end test.\n";
310 /* Thread engine auto frees thread on delete */
311 cout << "Waiting for thread to exit... " << flush;
315 cout << "Delete ThreadEngine... ";
322 TestSuite::~TestSuite()
324 cout << "\n\n*** END OF TEST SUITE ***\n";