]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/testsuite.cpp
a4d8c871c29f507cb6a5412307e24640c85a3d92
[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) || passed)) ? " 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)) || passed)) ? " 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) || passed)) ? " 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)) || passed)) ? " 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
125         CIDRTEST("brain@1.2.3.4", "*@1.2.0.0/16");
126         CIDRTEST("brain@1.2.3.4", "*@1.2.3.0/24");
127
128         CIDRTESTNOT("brain@1.2.3.4", "x*@1.2.0.0/16");
129         CIDRTESTNOT("brain@1.2.3.4", "*@1.3.4.0/24");
130
131         return passed;
132 }
133
134 bool TestSuite::DoThreadTests()
135 {
136         std::string anything;
137         ThreadEngine* te = NULL;
138
139         cout << "Creating new ThreadEngine class...\n";
140         try
141         {
142                 ThreadEngineFactory* tef = new ThreadEngineFactory();
143                 te = tef->Create(ServerInstance);
144                 delete tef;
145         }
146         catch (...)
147         {
148                 cout << "Creation failed, test failure.\n";
149                 return false;
150         }
151         cout << "Creation success, type " << te->GetName() << "\n";
152
153         cout << "Allocate: new TestSuiteThread...\n";
154         TestSuiteThread* tst = new TestSuiteThread();
155
156         cout << "ThreadEngine::Create on TestSuiteThread...\n";
157         try
158         {
159                 try
160                 {
161                         te->Create(tst);
162                 }
163                 catch (CoreException &ce)
164                 {
165                         cout << "Failure: " << ce.GetReason() << endl;
166                 }
167         }
168         catch (...)
169         {
170                 cout << "Failure, unhandled exception\n";
171         }
172
173         cout << "Type any line and press enter to end test.\n";
174         cin >> anything;
175
176         /* Thread engine auto frees thread on delete */
177         cout << "Waiting for thread to exit... " << flush;
178         delete tst;
179         cout << "Done!\n";
180
181         cout << "Delete ThreadEngine... ";
182         delete te;
183         cout << "Done!\n";
184
185         return true;
186 }
187
188 TestSuite::~TestSuite()
189 {
190         cout << "\n\n*** END OF TEST SUITE ***\n";
191 }
192