]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/testsuite.cpp
match() is no longer a function+no header, now a static method of InspIRCd class...
[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 */
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", "foobar");
120         WCTEST("foobar", "foo***bar");
121
122         WCTESTNOT("foobar", "bazqux");
123         WCTESTNOT("foobar", "*qux");
124         WCTESTNOT("foobar", "foo*x");
125         WCTESTNOT("foobar", "baz*");
126         WCTESTNOT("foobar", "foo???r");
127         WCTESTNOT("foobar", "");
128         WCTESTNOT("", "foobar");
129         WCTESTNOT("OperServ", "O");
130         WCTESTNOT("O", "OperServ");
131         WCTESTNOT("foobar.tst", "fo?bar.*g");
132
133         CIDRTEST("brain@1.2.3.4", "*@1.2.0.0/16");
134         CIDRTEST("brain@1.2.3.4", "*@1.2.3.0/24");
135
136         CIDRTEST("192.168.3.97", "192.168.3.0/24");
137
138         CIDRTESTNOT("brain@1.2.3.4", "x*@1.2.0.0/16");
139         CIDRTESTNOT("brain@1.2.3.4", "*@1.3.4.0/24");
140
141         CIDRTESTNOT("1.2.3.4", "1.2.4.0/24");
142
143         CIDRTESTNOT("brain@1.2.3.4", "*@/24");
144         CIDRTESTNOT("brain@1.2.3.4", "@1.2.3.4/9");
145         CIDRTESTNOT("brain@1.2.3.4", "@");
146         CIDRTESTNOT("brain@1.2.3.4", "");
147
148         return true;
149 }
150
151 bool TestSuite::DoThreadTests()
152 {
153         std::string anything;
154         ThreadEngine* te = NULL;
155
156         cout << "Creating new ThreadEngine class...\n";
157         try
158         {
159                 ThreadEngineFactory* tef = new ThreadEngineFactory();
160                 te = tef->Create(ServerInstance);
161                 delete tef;
162         }
163         catch (...)
164         {
165                 cout << "Creation failed, test failure.\n";
166                 return false;
167         }
168         cout << "Creation success, type " << te->GetName() << "\n";
169
170         cout << "Allocate: new TestSuiteThread...\n";
171         TestSuiteThread* tst = new TestSuiteThread();
172
173         cout << "ThreadEngine::Create on TestSuiteThread...\n";
174         try
175         {
176                 try
177                 {
178                         te->Create(tst);
179                 }
180                 catch (CoreException &ce)
181                 {
182                         cout << "Failure: " << ce.GetReason() << endl;
183                 }
184         }
185         catch (...)
186         {
187                 cout << "Failure, unhandled exception\n";
188         }
189
190         cout << "Type any line and press enter to end test.\n";
191         cin >> anything;
192
193         /* Thread engine auto frees thread on delete */
194         cout << "Waiting for thread to exit... " << flush;
195         delete tst;
196         cout << "Done!\n";
197
198         cout << "Delete ThreadEngine... ";
199         delete te;
200         cout << "Done!\n";
201
202         return true;
203 }
204
205 TestSuite::~TestSuite()
206 {
207         cout << "\n\n*** END OF TEST SUITE ***\n";
208 }
209