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