]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/testsuite.cpp
ThreadEngine: remove MutexFactory, mutexes should be constructed using their constructor
[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
59                 cout << endl << "(X) Exit test suite\n";
60
61                 cout << "\nChoices (Enter one or more options as a list then press enter, e.g. 15X): ";
62                 cin >> choice;
63
64                 if (!choice)
65                         continue;
66
67                 switch (choice)
68                 {
69                         case '1':
70                                 FOREACH_MOD(I_OnRunTestSuite, OnRunTestSuite());
71                         break;
72                         case '2':
73                                 cout << "Enter module filename to load: ";
74                                 cin >> modname;
75                                 cout << (Instance->Modules->Load(modname.c_str()) ? "\nSUCCESS!\n" : "\nFAILURE\n");
76                         break;
77                         case '3':
78                                 cout << "Enter module filename to unload: ";
79                                 cin >> modname;
80                                 cout << (Instance->Modules->Unload(modname.c_str()) ? "\nSUCCESS!\n" : "\nFAILURE\n");
81                         break;
82                         case '4':
83                                 cout << (DoThreadTests() ? "\nSUCCESS!\n" : "\nFAILURE\n");
84                         break;
85                         case '5':
86                                 cout << (DoWildTests() ? "\nSUCCESS!\n" : "\nFAILURE\n");
87                         break;
88                         case 'X':
89                                 return;
90                         break;
91                         default:
92                                 cout << "Invalid option\n";
93                         break;
94                 }
95                 cout << endl;
96         }
97 }
98
99 /* Test that x matches y with match() */
100 #define WCTEST(x, y) cout << "match(\"" << x << "\",\"" << y "\") " << ((passed = (InspIRCd::Match(x, y, NULL))) ? " SUCCESS!\n" : " FAILURE\n")
101 /* Test that x does not match y with match() */
102 #define WCTESTNOT(x, y) cout << "!match(\"" << x << "\",\"" << y "\") " << ((passed = ((!InspIRCd::Match(x, y, NULL)))) ? " SUCCESS!\n" : " FAILURE\n")
103
104 /* Test that x matches y with match() and cidr enabled */
105 #define CIDRTEST(x, y) cout << "match(\"" << x << "\",\"" << y "\", true) " << ((passed = (InspIRCd::MatchCIDR(x, y, NULL))) ? " SUCCESS!\n" : " FAILURE\n")
106 /* Test that x does not match y with match() and cidr enabled */
107 #define CIDRTESTNOT(x, y) cout << "!match(\"" << x << "\",\"" << y "\", true) " << ((passed = ((!InspIRCd::MatchCIDR(x, y, NULL)))) ? " SUCCESS!\n" : " FAILURE\n")
108
109 bool TestSuite::DoWildTests()
110 {
111         cout << "\n\nWildcard and CIDR tests\n\n";
112         bool passed = false;
113
114         WCTEST("foobar", "*");
115         WCTEST("foobar", "foo*");
116         WCTEST("foobar", "*bar");
117         WCTEST("foobar", "foo??r");
118         WCTEST("foobar.test", "fo?bar.*t");
119         WCTEST("foobar.test", "fo?bar.t*t");
120         WCTEST("foobar.tttt", "fo?bar.**t");
121         WCTEST("foobar", "foobar");
122         WCTEST("foobar", "foo***bar");
123         WCTEST("foobar", "*foo***bar");
124         WCTEST("foobar", "**foo***bar");
125         WCTEST("foobar", "**foobar*");
126         WCTEST("foobar", "**foobar**");
127         WCTEST("foobar", "**foobar");
128         WCTEST("foobar", "**f?*?ar");
129         WCTEST("foobar", "**f?*b?r");
130         WCTEST("foofar", "**f?*f*r");
131         WCTEST("foofar", "**f?*f*?");
132         WCTEST("r", "*");
133         WCTEST("", "");
134         WCTEST("test@foo.bar.test", "*@*.bar.test");
135         WCTEST("test@foo.bar.test", "*test*@*.bar.test");
136         WCTEST("test@foo.bar.test", "*@*test");
137
138         WCTEST("a", "*a");
139         WCTEST("aa", "*a");
140         WCTEST("aaa", "*a");
141         WCTEST("aaaa", "*a");
142         WCTEST("aaaaa", "*a");
143         WCTEST("aaaaaa", "*a");
144         WCTEST("aaaaaaa", "*a");
145         WCTEST("aaaaaaaa", "*a");
146         WCTEST("aaaaaaaaa", "*a");
147         WCTEST("aaaaaaaaaa", "*a");
148         WCTEST("aaaaaaaaaaa", "*a");
149
150         WCTESTNOT("foobar", "bazqux");
151         WCTESTNOT("foobar", "*qux");
152         WCTESTNOT("foobar", "foo*x");
153         WCTESTNOT("foobar", "baz*");
154         WCTESTNOT("foobar", "foo???r");
155         WCTESTNOT("foobar", "foobars");
156         WCTESTNOT("foobar", "**foobar**h");
157         WCTESTNOT("foobar", "**foobar**h*");
158         WCTESTNOT("foobar", "**f??*bar?");
159         WCTESTNOT("foobar", "");
160         WCTESTNOT("", "foobar");
161         WCTESTNOT("OperServ", "O");
162         WCTESTNOT("O", "OperServ");
163         WCTESTNOT("foobar.tst", "fo?bar.*g");
164         WCTESTNOT("foobar.test", "fo?bar.*tt");
165
166         CIDRTEST("brain@1.2.3.4", "*@1.2.0.0/16");
167         CIDRTEST("brain@1.2.3.4", "*@1.2.3.0/24");
168         CIDRTEST("192.168.3.97", "192.168.3.0/24");
169
170         CIDRTESTNOT("brain@1.2.3.4", "x*@1.2.0.0/16");
171         CIDRTESTNOT("brain@1.2.3.4", "*@1.3.4.0/24");
172         CIDRTESTNOT("1.2.3.4", "1.2.4.0/24");
173         CIDRTESTNOT("brain@1.2.3.4", "*@/24");
174         CIDRTESTNOT("brain@1.2.3.4", "@1.2.3.4/9");
175         CIDRTESTNOT("brain@1.2.3.4", "@");
176         CIDRTESTNOT("brain@1.2.3.4", "");
177
178         return true;
179 }
180
181 bool TestSuite::DoThreadTests()
182 {
183         std::string anything;
184         ThreadEngine* te = NULL;
185
186         cout << "Creating new ThreadEngine class...\n";
187         try
188         {
189                 te = new ThreadEngine(ServerInstance);
190         }
191         catch (...)
192         {
193                 cout << "Creation failed, test failure.\n";
194                 return false;
195         }
196         cout << "Creation success, type " << te->GetName() << "\n";
197
198         cout << "Allocate: new TestSuiteThread...\n";
199         TestSuiteThread* tst = new TestSuiteThread();
200
201         cout << "ThreadEngine::Create on TestSuiteThread...\n";
202         try
203         {
204                 try
205                 {
206                         te->Start(tst);
207                 }
208                 catch (CoreException &ce)
209                 {
210                         cout << "Failure: " << ce.GetReason() << endl;
211                 }
212         }
213         catch (...)
214         {
215                 cout << "Failure, unhandled exception\n";
216         }
217
218         cout << "Type any line and press enter to end test.\n";
219         cin >> anything;
220
221         /* Thread engine auto frees thread on delete */
222         cout << "Waiting for thread to exit... " << flush;
223         delete tst;
224         cout << "Done!\n";
225
226         cout << "Delete ThreadEngine... ";
227         delete te;
228         cout << "Done!\n";
229
230         return true;
231 }
232
233 TestSuite::~TestSuite()
234 {
235         cout << "\n\n*** END OF TEST SUITE ***\n";
236 }
237