client_wrapper.hxx, socket_wrapper.hxx: reorder member initialization order
[libt2n] / test / hello.cpp
... / ...
CommitLineData
1/*
2Copyright (C) 2004 by Intra2net AG
3
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*/
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
35#define BOOST_TEST_DYN_LINK
36#include <boost/test/unit_test.hpp>
37
38#include <t2n_exception.hxx>
39#include <socket_client.hxx>
40#include <socket_server.hxx>
41#include <command_client.hxx>
42
43#include "test_fixtures.hxx"
44
45#include <config.h>
46
47using namespace std;
48using namespace libt2n;
49
50
51BOOST_FIXTURE_TEST_SUITE(test_hello, KillChildOnShutdownFixture)
52
53BOOST_AUTO_TEST_CASE(HelloOk)
54{
55 switch(child_pid=fork())
56 {
57 case -1:
58 {
59 BOOST_FAIL("fork error");
60 break;
61 }
62 case 0:
63 // child
64 {
65 try
66 {
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(...)
81 {
82 std::cerr << "exception in child. ignoring\n";
83 }
84
85 // don't call atexit and stuff
86 _exit(0);
87 }
88
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);
101 }
102 }
103}
104
105BOOST_AUTO_TEST_CASE(BadTag)
106{
107 switch(child_pid=fork())
108 {
109 case -1:
110 {
111 BOOST_FAIL("fork error");
112 break;
113 }
114 case 0:
115 // child
116 {
117 try
118 {
119 socket_server ss("./socket");
120
121 ostringstream hello;
122 hello << "XYZ 123";
123
124 ss.add_callback(new_connection,bind(&test_hello::BadTag::send_hello, boost::ref(*this), hello.str(),&ss, _1));
125
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";
132 }
133
134 // don't call atexit and stuff
135 _exit(0);
136 }
137
138 default:
139 // parent
140 {
141 string data;
142
143 // wait till server is up
144 sleep(1);
145 socket_client_connection sc("./socket");
146
147 command_client cc(&sc);
148
149 t2n_exception* ep=cc.get_constuctor_exception();
150
151 string errormsg;
152 if (ep)
153 errormsg=ep->what();
154
155 BOOST_CHECK_EQUAL(string("illegal hello received (T2N)"),errormsg);
156 }
157 }
158}
159
160BOOST_AUTO_TEST_CASE(BadVersion)
161{
162 switch(child_pid=fork())
163 {
164 case -1:
165 {
166 BOOST_FAIL("fork error");
167 break;
168 }
169 case 0:
170 // child
171 {
172 try
173 {
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(...)
189 {
190 std::cerr << "exception in child. ignoring\n";
191 }
192
193 // don't call atexit and stuff
194 _exit(0);
195 }
196
197 default:
198 // parent
199 {
200 string data;
201
202 // wait till server is up
203 sleep(1);
204 socket_client_connection sc("./socket");
205
206 command_client cc(&sc);
207
208 t2n_exception* ep=cc.get_constuctor_exception();
209
210 string errormsg;
211 if (ep)
212 errormsg=ep->what();
213
214 BOOST_CHECK_EQUAL(string("not compatible with the server protocol version"),errormsg);
215 }
216 }
217}
218
219BOOST_AUTO_TEST_CASE(SeparatorMissing)
220{
221 switch(child_pid=fork())
222 {
223 case -1:
224 {
225 BOOST_FAIL("fork error");
226 break;
227 }
228 case 0:
229 // child
230 {
231 try
232 {
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(...)
247 {
248 std::cerr << "exception in child. ignoring\n";
249 }
250
251 // don't call atexit and stuff
252 _exit(0);
253 }
254
255 default:
256 // parent
257 {
258 string data;
259
260 // wait till server is up
261 sleep(1);
262 socket_client_connection sc("./socket");
263
264 command_client cc(&sc);
265
266 t2n_exception* ep=cc.get_constuctor_exception();
267
268 string errormsg;
269 if (ep)
270 errormsg=ep->what();
271
272 BOOST_CHECK_EQUAL(string("illegal hello received (1. ;)"),errormsg);
273 }
274 }
275}
276
277BOOST_AUTO_TEST_CASE(WrongByteOrder)
278{
279 switch(child_pid=fork())
280 {
281 case -1:
282 {
283 BOOST_FAIL("fork error");
284 break;
285 }
286 case 0:
287 // child
288 {
289 try
290 {
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(...)
314 {
315 std::cerr << "exception in child. ignoring\n";
316 }
317
318 // don't call atexit and stuff
319 _exit(0);
320 }
321
322 default:
323 // parent
324 {
325 string data;
326
327 // wait till server is up
328 sleep(1);
329 socket_client_connection sc("./socket");
330
331 command_client cc(&sc);
332
333 t2n_exception* ep=cc.get_constuctor_exception();
334
335 string errormsg;
336 if (ep)
337 errormsg=ep->what();
338
339 BOOST_CHECK_EQUAL(string("host byte order not matching"),errormsg);
340 }
341 }
342}
343
344BOOST_AUTO_TEST_CASE(OtherServerBig)
345{
346 switch(child_pid=fork())
347 {
348 case -1:
349 {
350 BOOST_FAIL("fork error");
351 break;
352 }
353 case 0:
354 // child
355 {
356 try
357 {
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(...)
370 {
371 std::cerr << "exception in child. ignoring\n";
372 }
373
374 // don't call atexit and stuff
375 _exit(0);
376 }
377
378 default:
379 // parent
380 {
381 string data;
382
383 // wait till server is up
384 sleep(1);
385 socket_client_connection sc("./socket");
386
387 command_client cc(&sc);
388
389 t2n_exception* ep=cc.get_constuctor_exception();
390
391 string errormsg;
392 if (ep)
393 errormsg=ep->what();
394
395 BOOST_CHECK_EQUAL(string("illegal hello received (T2N)"),errormsg);
396 }
397 }
398}
399
400BOOST_AUTO_TEST_CASE(OtherServerSmall)
401{
402 switch(child_pid=fork())
403 {
404 case -1:
405 {
406 BOOST_FAIL("fork error");
407 break;
408 }
409 case 0:
410 // child
411 {
412 try
413 {
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(...)
426 {
427 std::cerr << "exception in child. ignoring\n";
428 }
429
430 // don't call atexit and stuff
431 _exit(0);
432 }
433
434 default:
435 // parent
436 {
437 string data;
438
439 // wait till server is up
440 sleep(1);
441 socket_client_connection sc("./socket");
442
443 command_client cc(&sc);
444
445 t2n_exception* ep=cc.get_constuctor_exception();
446
447 string errormsg;
448 if (ep)
449 errormsg=ep->what();
450
451 BOOST_CHECK_EQUAL(string("illegal hello received (T2N)"),errormsg);
452 }
453 }
454}
455
456BOOST_AUTO_TEST_SUITE_END()