Commit | Line | Data |
---|---|---|
19facd85 TJ |
1 | /* |
2 | Copyright (C) 2004 by Intra2net AG | |
04d86ba4 | 3 | |
19facd85 TJ |
4 | The software in this package is distributed under the GNU General |
5 | Public License version 2 (with a special exception described below). | |
6 | ||
7 | A copy of GNU General Public License (GPL) is included in this distribution, | |
8 | in the file COPYING.GPL. | |
9 | ||
10 | As a special exception, if other files instantiate templates or use macros | |
11 | or inline functions from this file, or you compile this file and link it | |
12 | with other works to produce a work based on this file, this file | |
13 | does not by itself cause the resulting work to be covered | |
14 | by the GNU General Public License. | |
15 | ||
16 | However the source code for this file must still be made available | |
17 | in accordance with section (3) of the GNU General Public License. | |
18 | ||
19 | This exception does not invalidate any other reasons why a work based | |
20 | on this file might be covered by the GNU General Public License. | |
21 | */ | |
04d86ba4 GE |
22 | #include <sys/types.h> |
23 | #include <unistd.h> | |
24 | #include <errno.h> | |
25 | #include <signal.h> | |
26 | #include <stdio.h> | |
27 | ||
28 | #include <iostream> | |
29 | #include <string> | |
30 | #include <sstream> | |
31 | #include <stdexcept> | |
32 | ||
33 | #include <boost/bind.hpp> | |
34 | ||
307b5e74 TJ |
35 | #define BOOST_TEST_DYN_LINK |
36 | #include <boost/test/unit_test.hpp> | |
04d86ba4 GE |
37 | |
38 | #include <t2n_exception.hxx> | |
39 | #include <socket_client.hxx> | |
40 | #include <socket_server.hxx> | |
41 | #include <command_client.hxx> | |
42 | ||
307b5e74 TJ |
43 | #include "test_fixtures.hxx" |
44 | ||
04d86ba4 | 45 | #include <config.h> |
04d86ba4 GE |
46 | |
47 | using namespace std; | |
48 | using namespace libt2n; | |
04d86ba4 | 49 | |
307b5e74 | 50 | |
df94ded3 | 51 | BOOST_FIXTURE_TEST_SUITE(test_hello, KillChildOnShutdownFixture) |
307b5e74 TJ |
52 | |
53 | BOOST_AUTO_TEST_CASE(HelloOk) | |
54 | { | |
55 | switch(child_pid=fork()) | |
56 | { | |
57 | case -1: | |
04d86ba4 | 58 | { |
307b5e74 TJ |
59 | BOOST_FAIL("fork error"); |
60 | break; | |
61 | } | |
62 | case 0: | |
63 | // child | |
64 | { | |
65 | try | |
04d86ba4 | 66 | { |
307b5e74 TJ |
67 | socket_server ss("./socket"); |
68 | ||
69 | ostringstream hello; | |
70 | hello << "T2Nv" << PROTOCOL_VERSION << ';'; | |
71 | int byteordercheck=1; | |
72 | hello.write((char*)&byteordercheck,sizeof(byteordercheck)); | |
73 | hello << ';'; | |
74 | ||
75 | ss.add_callback(new_connection,bind(&test_hello::HelloOk::send_hello, boost::ref(*this), hello.str(),&ss, _1)); | |
76 | ||
77 | // max 10 sec | |
78 | for (int i=0; i < 10; i++) | |
79 | ss.fill_buffer(1000000); | |
80 | } catch(...) | |
04d86ba4 | 81 | { |
307b5e74 | 82 | std::cerr << "exception in child. ignoring\n"; |
04d86ba4 GE |
83 | } |
84 | ||
307b5e74 TJ |
85 | // don't call atexit and stuff |
86 | _exit(0); | |
87 | } | |
04d86ba4 | 88 | |
307b5e74 TJ |
89 | default: |
90 | // parent | |
91 | { | |
92 | string data; | |
93 | ||
94 | // wait till server is up | |
95 | sleep(1); | |
96 | socket_client_connection sc("./socket"); | |
97 | command_client cc(&sc); | |
98 | ||
99 | // All fine we reached this point | |
100 | BOOST_CHECK(true); | |
04d86ba4 GE |
101 | } |
102 | } | |
307b5e74 | 103 | } |
04d86ba4 | 104 | |
307b5e74 TJ |
105 | BOOST_AUTO_TEST_CASE(BadTag) |
106 | { | |
107 | switch(child_pid=fork()) | |
04d86ba4 | 108 | { |
307b5e74 | 109 | case -1: |
04d86ba4 | 110 | { |
307b5e74 TJ |
111 | BOOST_FAIL("fork error"); |
112 | break; | |
113 | } | |
114 | case 0: | |
115 | // child | |
116 | { | |
117 | try | |
04d86ba4 | 118 | { |
307b5e74 | 119 | socket_server ss("./socket"); |
04d86ba4 | 120 | |
307b5e74 TJ |
121 | ostringstream hello; |
122 | hello << "XYZ 123"; | |
441d41fe | 123 | |
307b5e74 | 124 | ss.add_callback(new_connection,bind(&test_hello::BadTag::send_hello, boost::ref(*this), hello.str(),&ss, _1)); |
04d86ba4 | 125 | |
307b5e74 TJ |
126 | // max 10 sec |
127 | for (int i=0; i < 10; i++) | |
128 | ss.fill_buffer(1000000); | |
129 | } catch(...) | |
130 | { | |
131 | std::cerr << "exception in child. ignoring\n"; | |
04d86ba4 GE |
132 | } |
133 | ||
307b5e74 TJ |
134 | // don't call atexit and stuff |
135 | _exit(0); | |
136 | } | |
04d86ba4 | 137 | |
307b5e74 TJ |
138 | default: |
139 | // parent | |
140 | { | |
141 | string data; | |
04d86ba4 | 142 | |
307b5e74 TJ |
143 | // wait till server is up |
144 | sleep(1); | |
145 | socket_client_connection sc("./socket"); | |
b5922184 | 146 | |
307b5e74 | 147 | command_client cc(&sc); |
04d86ba4 | 148 | |
307b5e74 | 149 | t2n_exception* ep=cc.get_constuctor_exception(); |
04d86ba4 | 150 | |
307b5e74 TJ |
151 | string errormsg; |
152 | if (ep) | |
153 | errormsg=ep->what(); | |
154 | ||
155 | BOOST_CHECK_EQUAL(string("illegal hello received (T2N)"),errormsg); | |
04d86ba4 GE |
156 | } |
157 | } | |
307b5e74 | 158 | } |
04d86ba4 | 159 | |
307b5e74 TJ |
160 | BOOST_AUTO_TEST_CASE(BadVersion) |
161 | { | |
162 | switch(child_pid=fork()) | |
04d86ba4 | 163 | { |
307b5e74 | 164 | case -1: |
04d86ba4 | 165 | { |
307b5e74 TJ |
166 | BOOST_FAIL("fork error"); |
167 | break; | |
168 | } | |
169 | case 0: | |
170 | // child | |
171 | { | |
172 | try | |
04d86ba4 | 173 | { |
307b5e74 TJ |
174 | socket_server ss("./socket"); |
175 | ||
176 | ostringstream hello; | |
177 | // lets hope we don't ever get near such a version number... | |
178 | hello << "T2Nv" << 4982271 << ';'; | |
179 | int byteordercheck=1; | |
180 | hello.write((char*)&byteordercheck,sizeof(byteordercheck)); | |
181 | hello << ';'; | |
182 | ||
183 | ss.add_callback(new_connection,bind(&test_hello::BadVersion::send_hello, boost::ref(*this), hello.str(),&ss, _1)); | |
184 | ||
185 | // max 10 sec | |
186 | for (int i=0; i < 10; i++) | |
187 | ss.fill_buffer(1000000); | |
188 | } catch(...) | |
04d86ba4 | 189 | { |
307b5e74 | 190 | std::cerr << "exception in child. ignoring\n"; |
04d86ba4 GE |
191 | } |
192 | ||
307b5e74 TJ |
193 | // don't call atexit and stuff |
194 | _exit(0); | |
195 | } | |
196 | ||
197 | default: | |
198 | // parent | |
199 | { | |
200 | string data; | |
04d86ba4 | 201 | |
307b5e74 TJ |
202 | // wait till server is up |
203 | sleep(1); | |
204 | socket_client_connection sc("./socket"); | |
04d86ba4 | 205 | |
307b5e74 | 206 | command_client cc(&sc); |
04d86ba4 | 207 | |
307b5e74 | 208 | t2n_exception* ep=cc.get_constuctor_exception(); |
b5922184 | 209 | |
307b5e74 TJ |
210 | string errormsg; |
211 | if (ep) | |
212 | errormsg=ep->what(); | |
04d86ba4 | 213 | |
307b5e74 | 214 | BOOST_CHECK_EQUAL(string("not compatible with the server protocol version"),errormsg); |
04d86ba4 GE |
215 | } |
216 | } | |
307b5e74 | 217 | } |
04d86ba4 | 218 | |
307b5e74 TJ |
219 | BOOST_AUTO_TEST_CASE(SeparatorMissing) |
220 | { | |
221 | switch(child_pid=fork()) | |
04d86ba4 | 222 | { |
307b5e74 | 223 | case -1: |
04d86ba4 | 224 | { |
307b5e74 TJ |
225 | BOOST_FAIL("fork error"); |
226 | break; | |
227 | } | |
228 | case 0: | |
229 | // child | |
230 | { | |
231 | try | |
04d86ba4 | 232 | { |
307b5e74 TJ |
233 | socket_server ss("./socket"); |
234 | ||
235 | ostringstream hello; | |
236 | hello << "T2Nv" << PROTOCOL_VERSION; | |
237 | int byteordercheck=1; | |
238 | hello.write((char*)&byteordercheck,sizeof(byteordercheck)); | |
239 | hello << ';'; | |
240 | ||
241 | ss.add_callback(new_connection,bind(&test_hello::SeparatorMissing::send_hello, boost::ref(*this), hello.str(),&ss, _1)); | |
242 | ||
243 | // max 10 sec | |
244 | for (int i=0; i < 10; i++) | |
245 | ss.fill_buffer(1000000); | |
246 | } catch(...) | |
04d86ba4 | 247 | { |
307b5e74 | 248 | std::cerr << "exception in child. ignoring\n"; |
04d86ba4 GE |
249 | } |
250 | ||
307b5e74 TJ |
251 | // don't call atexit and stuff |
252 | _exit(0); | |
253 | } | |
04d86ba4 | 254 | |
307b5e74 TJ |
255 | default: |
256 | // parent | |
257 | { | |
258 | string data; | |
04d86ba4 | 259 | |
307b5e74 TJ |
260 | // wait till server is up |
261 | sleep(1); | |
262 | socket_client_connection sc("./socket"); | |
04d86ba4 | 263 | |
307b5e74 | 264 | command_client cc(&sc); |
b5922184 | 265 | |
307b5e74 | 266 | t2n_exception* ep=cc.get_constuctor_exception(); |
04d86ba4 | 267 | |
307b5e74 TJ |
268 | string errormsg; |
269 | if (ep) | |
270 | errormsg=ep->what(); | |
271 | ||
272 | BOOST_CHECK_EQUAL(string("illegal hello received (1. ;)"),errormsg); | |
04d86ba4 GE |
273 | } |
274 | } | |
307b5e74 | 275 | } |
04d86ba4 | 276 | |
307b5e74 TJ |
277 | BOOST_AUTO_TEST_CASE(WrongByteOrder) |
278 | { | |
279 | switch(child_pid=fork()) | |
04d86ba4 | 280 | { |
307b5e74 TJ |
281 | case -1: |
282 | { | |
283 | BOOST_FAIL("fork error"); | |
284 | break; | |
285 | } | |
286 | case 0: | |
287 | // child | |
04d86ba4 | 288 | { |
307b5e74 | 289 | try |
04d86ba4 | 290 | { |
307b5e74 TJ |
291 | socket_server ss("./socket"); |
292 | ||
293 | ostringstream hello; | |
294 | hello << "T2Nv" << PROTOCOL_VERSION << ';'; | |
295 | int byteordercheck=1; | |
296 | int dst; | |
297 | char* si=(char*)&byteordercheck; | |
298 | char* di=(char*)&dst; | |
299 | ||
300 | di[0]=si[3]; | |
301 | di[1]=si[2]; | |
302 | di[2]=si[1]; | |
303 | di[3]=si[0]; | |
304 | ||
305 | hello.write((char*)&dst,sizeof(dst)); | |
306 | hello << ';'; | |
307 | ||
308 | ss.add_callback(new_connection,bind(&test_hello::WrongByteOrder::send_hello, boost::ref(*this), hello.str(),&ss, _1)); | |
309 | ||
310 | // max 10 sec | |
311 | for (int i=0; i < 10; i++) | |
312 | ss.fill_buffer(1000000); | |
313 | } catch(...) | |
04d86ba4 | 314 | { |
307b5e74 | 315 | std::cerr << "exception in child. ignoring\n"; |
04d86ba4 GE |
316 | } |
317 | ||
307b5e74 TJ |
318 | // don't call atexit and stuff |
319 | _exit(0); | |
320 | } | |
04d86ba4 | 321 | |
307b5e74 TJ |
322 | default: |
323 | // parent | |
324 | { | |
325 | string data; | |
04d86ba4 | 326 | |
307b5e74 TJ |
327 | // wait till server is up |
328 | sleep(1); | |
329 | socket_client_connection sc("./socket"); | |
04d86ba4 | 330 | |
307b5e74 | 331 | command_client cc(&sc); |
b5922184 | 332 | |
307b5e74 | 333 | t2n_exception* ep=cc.get_constuctor_exception(); |
04d86ba4 | 334 | |
307b5e74 TJ |
335 | string errormsg; |
336 | if (ep) | |
337 | errormsg=ep->what(); | |
338 | ||
339 | BOOST_CHECK_EQUAL(string("host byte order not matching"),errormsg); | |
04d86ba4 GE |
340 | } |
341 | } | |
307b5e74 | 342 | } |
04d86ba4 | 343 | |
307b5e74 TJ |
344 | BOOST_AUTO_TEST_CASE(OtherServerBig) |
345 | { | |
346 | switch(child_pid=fork()) | |
04d86ba4 | 347 | { |
307b5e74 TJ |
348 | case -1: |
349 | { | |
350 | BOOST_FAIL("fork error"); | |
351 | break; | |
352 | } | |
353 | case 0: | |
354 | // child | |
04d86ba4 | 355 | { |
307b5e74 | 356 | try |
04d86ba4 | 357 | { |
307b5e74 TJ |
358 | socket_server ss("./socket"); |
359 | ||
360 | ostringstream hello; | |
361 | // hmm, we got the wrong socket | |
362 | hello << "* OK intradev.net.lan Cyrus IMAP4 v2.2.13 server ready"; | |
363 | ||
364 | ss.add_callback(new_connection,bind(&test_hello::OtherServerBig::send_raw_socket, boost::ref(*this), hello.str(),&ss, _1)); | |
365 | ||
366 | // max 3 sec | |
367 | for (int i=0; i < 3; i++) | |
368 | ss.fill_buffer(1000000); | |
369 | } catch(...) | |
04d86ba4 | 370 | { |
307b5e74 | 371 | std::cerr << "exception in child. ignoring\n"; |
04d86ba4 GE |
372 | } |
373 | ||
307b5e74 TJ |
374 | // don't call atexit and stuff |
375 | _exit(0); | |
376 | } | |
04d86ba4 | 377 | |
307b5e74 TJ |
378 | default: |
379 | // parent | |
380 | { | |
381 | string data; | |
04d86ba4 | 382 | |
307b5e74 TJ |
383 | // wait till server is up |
384 | sleep(1); | |
385 | socket_client_connection sc("./socket"); | |
b5922184 | 386 | |
307b5e74 | 387 | command_client cc(&sc); |
04d86ba4 | 388 | |
307b5e74 | 389 | t2n_exception* ep=cc.get_constuctor_exception(); |
04d86ba4 | 390 | |
307b5e74 TJ |
391 | string errormsg; |
392 | if (ep) | |
393 | errormsg=ep->what(); | |
394 | ||
395 | BOOST_CHECK_EQUAL(string("illegal hello received (T2N)"),errormsg); | |
04d86ba4 GE |
396 | } |
397 | } | |
307b5e74 | 398 | } |
04d86ba4 | 399 | |
307b5e74 TJ |
400 | BOOST_AUTO_TEST_CASE(OtherServerSmall) |
401 | { | |
402 | switch(child_pid=fork()) | |
b604df5f | 403 | { |
307b5e74 | 404 | case -1: |
b604df5f | 405 | { |
307b5e74 TJ |
406 | BOOST_FAIL("fork error"); |
407 | break; | |
408 | } | |
409 | case 0: | |
410 | // child | |
411 | { | |
412 | try | |
b604df5f | 413 | { |
307b5e74 TJ |
414 | socket_server ss("./socket"); |
415 | ||
416 | ostringstream hello; | |
417 | // hmm, we got the wrong socket | |
418 | hello << "READY"; | |
419 | ||
420 | ss.add_callback(new_connection,bind(&test_hello::OtherServerSmall::send_raw_socket, boost::ref(*this), hello.str(),&ss, _1)); | |
421 | ||
422 | // max 3 sec | |
423 | for (int i=0; i < 3; i++) | |
424 | ss.fill_buffer(1000000); | |
425 | } catch(...) | |
b604df5f | 426 | { |
307b5e74 | 427 | std::cerr << "exception in child. ignoring\n"; |
b604df5f GE |
428 | } |
429 | ||
307b5e74 TJ |
430 | // don't call atexit and stuff |
431 | _exit(0); | |
432 | } | |
b604df5f | 433 | |
307b5e74 TJ |
434 | default: |
435 | // parent | |
436 | { | |
437 | string data; | |
b604df5f | 438 | |
307b5e74 TJ |
439 | // wait till server is up |
440 | sleep(1); | |
441 | socket_client_connection sc("./socket"); | |
b604df5f | 442 | |
307b5e74 | 443 | command_client cc(&sc); |
b5922184 | 444 | |
307b5e74 | 445 | t2n_exception* ep=cc.get_constuctor_exception(); |
b604df5f | 446 | |
307b5e74 TJ |
447 | string errormsg; |
448 | if (ep) | |
449 | errormsg=ep->what(); | |
450 | ||
451 | BOOST_CHECK_EQUAL(string("illegal hello received (T2N)"),errormsg); | |
b604df5f GE |
452 | } |
453 | } | |
307b5e74 | 454 | } |
b604df5f | 455 | |
307b5e74 | 456 | BOOST_AUTO_TEST_SUITE_END() |