]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/testsuite.cpp
71ace375e295a7ebe60513051a9b5202695322a3
[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                 cout << "(6) irc::string -> std::string dynamic cast\n";
60
61                 cout << endl << "(X) Exit test suite\n";
62
63                 cout << "\nChoice: ";
64                 cin >> choice;
65
66                 if (!choice)
67                         continue;
68
69                 switch (choice)
70                 {
71                         case '1':
72                                 FOREACH_MOD(I_OnRunTestSuite, OnRunTestSuite());
73                         break;
74                         case '2':
75                                 cout << "Enter module filename to load: ";
76                                 cin >> modname;
77                                 cout << (Instance->Modules->Load(modname.c_str()) ? "\nSUCCESS!\n" : "\nFAILURE\n");
78                         break;
79                         case '3':
80                                 cout << "Enter module filename to unload: ";
81                                 cin >> modname;
82                                 cout << (Instance->Modules->Unload(modname.c_str()) ? "\nSUCCESS!\n" : "\nFAILURE\n");
83                         break;
84                         case '4':
85                                 cout << (DoThreadTests() ? "\nSUCCESS!\n" : "\nFAILURE\n");
86                         break;
87                         case '5':
88                                 cout << (DoWildTests() ? "\nSUCCESS!\n" : "\nFAILURE\n");
89                         break;
90                         case '6':
91                                 {
92                                         std::string* a = new std::string("test");
93                                         irc::string* b = (irc::string)a;
94                                         cout << "std::string value: '" << *a << "' irc::string value: '" << *b << "'" << endl;
95                                         delete a;
96                                 }
97                         break;
98                         case 'X':
99                                 return;
100                         break;
101                         default:
102                                 cout << "Invalid option\n";
103                         break;
104                 }
105                 cout << endl;
106         }
107 }
108
109 /* Test that x matches y with match() */
110 #define WCTEST(x, y) cout << "match(\"" << x << "\",\"" << y "\") " << ((passed = (match(x, y))) ? " SUCCESS!\n" : " FAILURE\n")
111 /* Test that x does not match y with match() */
112 #define WCTESTNOT(x, y) cout << "!match(\"" << x << "\",\"" << y "\") " << ((passed = ((!match(x, y)))) ? " SUCCESS!\n" : " FAILURE\n")
113
114 /* Test that x matches y with match() and cidr enabled */
115 #define CIDRTEST(x, y) cout << "match(\"" << x << "\",\"" << y "\", true) " << ((passed = (match(x, y, true))) ? " SUCCESS!\n" : " FAILURE\n")
116 /* Test that x does not match y with match() and cidr enabled */
117 #define CIDRTESTNOT(x, y) cout << "!match(\"" << x << "\",\"" << y "\", true) " << ((passed = ((!match(x, y, true)))) ? " SUCCESS!\n" : " FAILURE\n")
118
119 bool TestSuite::DoWildTests()
120 {
121         cout << "\n\nWildcard and CIDR tests\n\n";
122         bool passed = false;
123
124         WCTEST("foobar", "*");
125         WCTEST("foobar", "foo*");
126         WCTEST("foobar", "*bar");
127         WCTEST("foobar", "foo??r");
128
129         WCTESTNOT("foobar", "bazqux");
130         WCTESTNOT("foobar", "*qux");
131         WCTESTNOT("foobar", "foo*x");
132         WCTESTNOT("foobar", "baz*");
133         WCTESTNOT("foobar", "foo???r");
134         WCTESTNOT("foobar", "");
135
136         CIDRTEST("brain@1.2.3.4", "*@1.2.0.0/16");
137         CIDRTEST("brain@1.2.3.4", "*@1.2.3.0/24");
138
139         CIDRTEST("192.168.3.97", "192.168.3.0/24");
140
141         CIDRTESTNOT("brain@1.2.3.4", "x*@1.2.0.0/16");
142         CIDRTESTNOT("brain@1.2.3.4", "*@1.3.4.0/24");
143
144         CIDRTESTNOT("1.2.3.4", "1.2.4.0/24");
145
146         CIDRTESTNOT("brain@1.2.3.4", "*@/24");
147         CIDRTESTNOT("brain@1.2.3.4", "@1.2.3.4/9");
148         CIDRTESTNOT("brain@1.2.3.4", "@");
149         CIDRTESTNOT("brain@1.2.3.4", "");
150
151         return true;
152 }
153
154 bool TestSuite::DoThreadTests()
155 {
156         std::string anything;
157         ThreadEngine* te = NULL;
158
159         cout << "Creating new ThreadEngine class...\n";
160         try
161         {
162                 ThreadEngineFactory* tef = new ThreadEngineFactory();
163                 te = tef->Create(ServerInstance);
164                 delete tef;
165         }
166         catch (...)
167         {
168                 cout << "Creation failed, test failure.\n";
169                 return false;
170         }
171         cout << "Creation success, type " << te->GetName() << "\n";
172
173         cout << "Allocate: new TestSuiteThread...\n";
174         TestSuiteThread* tst = new TestSuiteThread();
175
176         cout << "ThreadEngine::Create on TestSuiteThread...\n";
177         try
178         {
179                 try
180                 {
181                         te->Create(tst);
182                 }
183                 catch (CoreException &ce)
184                 {
185                         cout << "Failure: " << ce.GetReason() << endl;
186                 }
187         }
188         catch (...)
189         {
190                 cout << "Failure, unhandled exception\n";
191         }
192
193         cout << "Type any line and press enter to end test.\n";
194         cin >> anything;
195
196         /* Thread engine auto frees thread on delete */
197         cout << "Waiting for thread to exit... " << flush;
198         delete tst;
199         cout << "Done!\n";
200
201         cout << "Delete ThreadEngine... ";
202         delete te;
203         cout << "Done!\n";
204
205         return true;
206 }
207
208 TestSuite::~TestSuite()
209 {
210         cout << "\n\n*** END OF TEST SUITE ***\n";
211 }
212