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