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