]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/testsuite.cpp
Fix test suite to not have bugs itself (oops) and fix bugs in new cidr matching metho...
[user/henk/code/inspircd.git] / src / testsuite.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2008 InspIRCd Development Team
6  * See: http://www.inspircd.org/wiki/index.php/Credits
7  *
8  * This program is free but copyrighted software; see
9  *          the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 /* $Core: libIRCDtestsuite */
15
16 #include "inspircd.h"
17 #include "testsuite.h"
18 #include "threadengine.h"
19 #include "wildcard.h"
20 #include <iostream>
21
22 using namespace std;
23
24 class TestSuiteThread : public Thread
25 {
26  public:
27         TestSuiteThread() : Thread()
28         {
29         }
30
31         virtual ~TestSuiteThread()
32         {
33         }
34
35         virtual void Run()
36         {
37                 while (GetExitFlag() == false)
38                 {
39                         cout << "Test suite thread run...\n";
40                         sleep(5);
41                 }
42         }
43 };
44
45 TestSuite::TestSuite(InspIRCd* Instance) : ServerInstance(Instance)
46 {
47         cout << "\n\n*** STARTING TESTSUITE ***\n";
48
49         std::string modname;
50         char choice;
51
52         while (1)
53         {
54                 cout << "(1) Call all module OnRunTestSuite() methods\n";
55                 cout << "(2) Load a module\n";
56                 cout << "(3) Unload a module\n";
57                 cout << "(4) Threading tests\n";
58                 cout << "(5) Wildcard and CIDR tests\n";
59
60                 cout << endl << "(X) Exit test suite\n";
61
62                 cout << "\nChoice: ";
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 'X':
90                                 return;
91                         break;
92                         default:
93                                 cout << "Invalid option\n";
94                         break;
95                 }
96                 cout << endl;
97         }
98 }
99
100 /* Test that x matches y with match() */
101 #define WCTEST(x, y) cout << "match(\"" << x << "\",\"" << y "\") " << ((passed = (match(x, y))) ? " SUCCESS!\n" : " FAILURE\n")
102 /* Test that x does not match y with match() */
103 #define WCTESTNOT(x, y) cout << "!match(\"" << x << "\",\"" << y "\") " << ((passed = ((!match(x, y)))) ? " SUCCESS!\n" : " FAILURE\n")
104
105 /* Test that x matches y with match() and cidr enabled */
106 #define CIDRTEST(x, y) cout << "match(\"" << x << "\",\"" << y "\", true) " << ((passed = (match(x, y, true))) ? " SUCCESS!\n" : " FAILURE\n")
107 /* Test that x does not match y with match() and cidr enabled */
108 #define CIDRTESTNOT(x, y) cout << "!match(\"" << x << "\",\"" << y "\", true) " << ((passed = ((!match(x, y, true)))) ? " SUCCESS!\n" : " FAILURE\n")
109
110 bool TestSuite::DoWildTests()
111 {
112         cout << "\n\nWildcard and CIDR tests\n\n";
113         bool passed = false;
114
115         WCTEST("foobar", "*");
116         WCTEST("foobar", "foo*");
117         WCTEST("foobar", "*bar");
118         WCTEST("foobar", "foo??r");
119
120         WCTESTNOT("foobar", "bazqux");
121         WCTESTNOT("foobar", "*qux");
122         WCTESTNOT("foobar", "foo*x");
123         WCTESTNOT("foobar", "baz*");
124         WCTESTNOT("foobar", "foo???r");
125         WCTESTNOT("foobar", "");
126
127         CIDRTEST("brain@1.2.3.4", "*@1.2.0.0/16");
128         CIDRTEST("brain@1.2.3.4", "*@1.2.3.0/24");
129
130         CIDRTEST("192.168.3.97", "192.168.3.0/24");
131
132         CIDRTESTNOT("brain@1.2.3.4", "x*@1.2.0.0/16");
133         CIDRTESTNOT("brain@1.2.3.4", "*@1.3.4.0/24");
134
135         CIDRTESTNOT("1.2.3.4", "1.2.4.0/24");
136
137         CIDRTESTNOT("brain@1.2.3.4", "*@/24");
138         CIDRTESTNOT("brain@1.2.3.4", "@1.2.3.4/9");
139         CIDRTESTNOT("brain@1.2.3.4", "@");
140         CIDRTESTNOT("brain@1.2.3.4", "");
141
142         return true;
143 }
144
145 bool TestSuite::DoThreadTests()
146 {
147         std::string anything;
148         ThreadEngine* te = NULL;
149
150         cout << "Creating new ThreadEngine class...\n";
151         try
152         {
153                 ThreadEngineFactory* tef = new ThreadEngineFactory();
154                 te = tef->Create(ServerInstance);
155                 delete tef;
156         }
157         catch (...)
158         {
159                 cout << "Creation failed, test failure.\n";
160                 return false;
161         }
162         cout << "Creation success, type " << te->GetName() << "\n";
163
164         cout << "Allocate: new TestSuiteThread...\n";
165         TestSuiteThread* tst = new TestSuiteThread();
166
167         cout << "ThreadEngine::Create on TestSuiteThread...\n";
168         try
169         {
170                 try
171                 {
172                         te->Create(tst);
173                 }
174                 catch (CoreException &ce)
175                 {
176                         cout << "Failure: " << ce.GetReason() << endl;
177                 }
178         }
179         catch (...)
180         {
181                 cout << "Failure, unhandled exception\n";
182         }
183
184         cout << "Type any line and press enter to end test.\n";
185         cin >> anything;
186
187         /* Thread engine auto frees thread on delete */
188         cout << "Waiting for thread to exit... " << flush;
189         delete tst;
190         cout << "Done!\n";
191
192         cout << "Delete ThreadEngine... ";
193         delete te;
194         cout << "Done!\n";
195
196         return true;
197 }
198
199 TestSuite::~TestSuite()
200 {
201         cout << "\n\n*** END OF TEST SUITE ***\n";
202 }
203