]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/testsuite.cpp
Add commasepstream test case.
[user/henk/code/inspircd.git] / src / testsuite.cpp
1 /*         +------------------------------------+
2  *         | Inspire Internet Relay Chat Daemon |
3  *         +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2009 InspIRCd Development Team
6  * See: http://wiki.inspircd.org/Credits
7  *
8  * This program is free but copyrighted software; see
9  *                the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 /* $Core */
15
16 #include "inspircd.h"
17 #include "testsuite.h"
18 #include "threadengine.h"
19 #include <iostream>
20
21 using namespace std;
22
23 class TestSuiteThread : public Thread
24 {
25  public:
26         TestSuiteThread() : Thread()
27         {
28         }
29
30         virtual ~TestSuiteThread()
31         {
32         }
33
34         virtual void Run()
35         {
36                 while (GetExitFlag() == false)
37                 {
38                         cout << "Test suite thread run...\n";
39                         sleep(5);
40                 }
41         }
42 };
43
44 TestSuite::TestSuite(InspIRCd* Instance) : ServerInstance(Instance)
45 {
46         cout << "\n\n*** STARTING TESTSUITE ***\n";
47
48         std::string modname;
49         char choice;
50
51         while (1)
52         {
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
60                 cout << endl << "(X) Exit test suite\n";
61
62                 cout << "\nChoices (Enter one or more options as a list then press enter, e.g. 15X): ";
63                 cin >> choice;
64
65                 if (!choice)
66                         continue;
67
68                 switch (choice)
69                 {
70                         case '1':
71                                 FOREACH_MOD(I_OnRunTestSuite, OnRunTestSuite());
72                                 break;
73                         case '2':
74                                 cout << "Enter module filename to load: ";
75                                 cin >> modname;
76                                 cout << (Instance->Modules->Load(modname.c_str()) ? "\nSUCCESS!\n" : "\nFAILURE\n");
77                                 break;
78                         case '3':
79                                 cout << "Enter module filename to unload: ";
80                                 cin >> modname;
81                                 cout << (Instance->Modules->Unload(modname.c_str()) ? "\nSUCCESS!\n" : "\nFAILURE\n");
82                                 break;
83                         case '4':
84                                 cout << (DoThreadTests() ? "\nSUCCESS!\n" : "\nFAILURE\n");
85                                 break;
86                         case '5':
87                                 cout << (DoWildTests() ? "\nSUCCESS!\n" : "\nFAILURE\n");
88                                 break;
89                         case '6':
90                                 cout << (DoCommaSepStreamTests() ? "\nSUCCESS!\n" : "\nFAILURE\n");
91                                 break;
92                         case 'X':
93                                 return;
94                                 break;
95                         default:
96                                 cout << "Invalid option\n";
97                                 break;
98                 }
99                 cout << endl;
100         }
101 }
102
103 /* Test that x matches y with match() */
104 #define WCTEST(x, y) cout << "match(\"" << x << "\",\"" << y "\") " << ((passed = (InspIRCd::Match(x, y, NULL))) ? " SUCCESS!\n" : " FAILURE\n")
105 /* Test that x does not match y with match() */
106 #define WCTESTNOT(x, y) cout << "!match(\"" << x << "\",\"" << y "\") " << ((passed = ((!InspIRCd::Match(x, y, NULL)))) ? " SUCCESS!\n" : " FAILURE\n")
107
108 /* Test that x matches y with match() and cidr enabled */
109 #define CIDRTEST(x, y) cout << "match(\"" << x << "\",\"" << y "\", true) " << ((passed = (InspIRCd::MatchCIDR(x, y, NULL))) ? " SUCCESS!\n" : " FAILURE\n")
110 /* Test that x does not match y with match() and cidr enabled */
111 #define CIDRTESTNOT(x, y) cout << "!match(\"" << x << "\",\"" << y "\", true) " << ((passed = ((!InspIRCd::MatchCIDR(x, y, NULL)))) ? " SUCCESS!\n" : " FAILURE\n")
112
113 bool TestSuite::DoWildTests()
114 {
115         cout << "\n\nWildcard and CIDR tests\n\n";
116         bool passed = false;
117
118         WCTEST("foobar", "*");
119         WCTEST("foobar", "foo*");
120         WCTEST("foobar", "*bar");
121         WCTEST("foobar", "foo??r");
122         WCTEST("foobar.test", "fo?bar.*t");
123         WCTEST("foobar.test", "fo?bar.t*t");
124         WCTEST("foobar.tttt", "fo?bar.**t");
125         WCTEST("foobar", "foobar");
126         WCTEST("foobar", "foo***bar");
127         WCTEST("foobar", "*foo***bar");
128         WCTEST("foobar", "**foo***bar");
129         WCTEST("foobar", "**foobar*");
130         WCTEST("foobar", "**foobar**");
131         WCTEST("foobar", "**foobar");
132         WCTEST("foobar", "**f?*?ar");
133         WCTEST("foobar", "**f?*b?r");
134         WCTEST("foofar", "**f?*f*r");
135         WCTEST("foofar", "**f?*f*?");
136         WCTEST("r", "*");
137         WCTEST("", "");
138         WCTEST("test@foo.bar.test", "*@*.bar.test");
139         WCTEST("test@foo.bar.test", "*test*@*.bar.test");
140         WCTEST("test@foo.bar.test", "*@*test");
141
142         WCTEST("a", "*a");
143         WCTEST("aa", "*a");
144         WCTEST("aaa", "*a");
145         WCTEST("aaaa", "*a");
146         WCTEST("aaaaa", "*a");
147         WCTEST("aaaaaa", "*a");
148         WCTEST("aaaaaaa", "*a");
149         WCTEST("aaaaaaaa", "*a");
150         WCTEST("aaaaaaaaa", "*a");
151         WCTEST("aaaaaaaaaa", "*a");
152         WCTEST("aaaaaaaaaaa", "*a");
153
154         WCTESTNOT("foobar", "bazqux");
155         WCTESTNOT("foobar", "*qux");
156         WCTESTNOT("foobar", "foo*x");
157         WCTESTNOT("foobar", "baz*");
158         WCTESTNOT("foobar", "foo???r");
159         WCTESTNOT("foobar", "foobars");
160         WCTESTNOT("foobar", "**foobar**h");
161         WCTESTNOT("foobar", "**foobar**h*");
162         WCTESTNOT("foobar", "**f??*bar?");
163         WCTESTNOT("foobar", "");
164         WCTESTNOT("", "foobar");
165         WCTESTNOT("OperServ", "O");
166         WCTESTNOT("O", "OperServ");
167         WCTESTNOT("foobar.tst", "fo?bar.*g");
168         WCTESTNOT("foobar.test", "fo?bar.*tt");
169
170         CIDRTEST("brain@1.2.3.4", "*@1.2.0.0/16");
171         CIDRTEST("brain@1.2.3.4", "*@1.2.3.0/24");
172         CIDRTEST("192.168.3.97", "192.168.3.0/24");
173
174         CIDRTESTNOT("brain@1.2.3.4", "x*@1.2.0.0/16");
175         CIDRTESTNOT("brain@1.2.3.4", "*@1.3.4.0/24");
176         CIDRTESTNOT("1.2.3.4", "1.2.4.0/24");
177         CIDRTESTNOT("brain@1.2.3.4", "*@/24");
178         CIDRTESTNOT("brain@1.2.3.4", "@1.2.3.4/9");
179         CIDRTESTNOT("brain@1.2.3.4", "@");
180         CIDRTESTNOT("brain@1.2.3.4", "");
181
182         return true;
183 }
184
185
186 #define STREQUALTEST(x, y) cout << "commasepstreamtoken(\"" << x << ",\"" << y "\") " << ((passed = (x == y)) ? "SUCCESS\n" : "FAILURE\n")
187
188 bool TestSuite::DoCommaSepStreamTests()
189 {
190         bool passed = false;
191         irc::commasepstream items("this,is,a,comma,stream");
192         std::string item;
193         int idx = 0;
194
195         while (items.GetToken(item))
196         {
197                 idx++;
198
199                 switch (idx)
200                 {
201                         case 1:
202                                 STREQUALTEST(item, "this");
203                                 break;
204                         case 2:
205                                 STREQUALTEST(item, "is");
206                                 break;
207                         case 3:
208                                 STREQUALTEST(item, "a");
209                                 break;
210                         case 4:
211                                 STREQUALTEST(item, "comma");
212                                 break;
213                         case 5:
214                                 STREQUALTEST(item, "stream");
215                                 break;
216                         default:
217                                 cout << "COMMASEPSTREAM: FAILURE: Got an index too many! " << idx << " items\n";
218                                 break;
219                 }
220         }
221
222         return true;
223 }
224
225 bool TestSuite::DoThreadTests()
226 {
227         std::string anything;
228         ThreadEngine* te = NULL;
229
230         cout << "Creating new ThreadEngine class...\n";
231         try
232         {
233                 te = new ThreadEngine(ServerInstance);
234         }
235         catch (...)
236         {
237                 cout << "Creation failed, test failure.\n";
238                 return false;
239         }
240         cout << "Creation success, type " << te->GetName() << "\n";
241
242         cout << "Allocate: new TestSuiteThread...\n";
243         TestSuiteThread* tst = new TestSuiteThread();
244
245         cout << "ThreadEngine::Create on TestSuiteThread...\n";
246         try
247         {
248                 try
249                 {
250                         te->Start(tst);
251                 }
252                 catch (CoreException &ce)
253                 {
254                         cout << "Failure: " << ce.GetReason() << endl;
255                 }
256         }
257         catch (...)
258         {
259                 cout << "Failure, unhandled exception\n";
260         }
261
262         cout << "Type any line and press enter to end test.\n";
263         cin >> anything;
264
265         /* Thread engine auto frees thread on delete */
266         cout << "Waiting for thread to exit... " << flush;
267         delete tst;
268         cout << "Done!\n";
269
270         cout << "Delete ThreadEngine... ";
271         delete te;
272         cout << "Done!\n";
273
274         return true;
275 }
276
277 TestSuite::~TestSuite()
278 {
279         cout << "\n\n*** END OF TEST SUITE ***\n";
280 }
281