Update to newer boost autoconf macros
[libt2n] / test / comm.cpp
CommitLineData
19facd85
TJ
1/*
2Copyright (C) 2004 by Intra2net AG
07e98688 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*/
07e98688
GE
22#include <sys/types.h>
23#include <unistd.h>
24#include <errno.h>
25#include <signal.h>
26#include <stdio.h>
d40453e8 27#include <time.h>
07e98688
GE
28
29#include <iostream>
30#include <string>
31#include <sstream>
32#include <stdexcept>
33
34#include <cppunit/extensions/TestFactoryRegistry.h>
35#include <cppunit/ui/text/TestRunner.h>
36#include <cppunit/extensions/HelperMacros.h>
37
38#include <socket_client.hxx>
39#include <socket_server.hxx>
40
41using namespace std;
42using namespace libt2n;
43using namespace CppUnit;
44
517d1214 45
07e98688
GE
46class test_comm : public TestFixture
47{
48 CPPUNIT_TEST_SUITE(test_comm);
49
50 CPPUNIT_TEST(UnixCommToServer);
51 CPPUNIT_TEST(UnixCommToServerAndBack);
58b327c6 52 CPPUNIT_TEST(UnixCommToServerAndBackBig);
cc68aabb 53 CPPUNIT_TEST(IPCommToServer);
7087e187 54 CPPUNIT_TEST(IPCommToServerAndBack);
517d1214 55 CPPUNIT_TEST(IPCommToServerAndBackBig);
07e98688
GE
56
57 CPPUNIT_TEST_SUITE_END();
58
b5922184
GE
59 pid_t child_pid;
60
07e98688
GE
61 public:
62
63 void setUp()
64 { }
65
66 void tearDown()
b5922184
GE
67 {
68 // make sure the server-child is dead before the next test runs
69 kill(child_pid,SIGKILL);
70 sleep(1);
71 }
07e98688
GE
72
73 void UnixCommToServer()
74 {
07e98688
GE
75 string data;
76
b5922184 77 switch(child_pid=fork())
07e98688
GE
78 {
79 case -1:
80 {
81 CPPUNIT_FAIL("fork error");
82 break;
83 }
84 case 0:
85 // child
86 {
441d41fe
TJ
87 try
88 {
89 // wait till server is up
90 sleep(1);
91 socket_client_connection sc("./socket");
92 sc.write("hello");
93 } catch(...)
94 {
95 std::cerr << "exception in child. ignoring\n";
96 }
97
07e98688
GE
98 // don't call atexit and stuff
99 _exit(0);
100 }
101
102 default:
103 // parent
104 {
105 socket_server ss("./socket");
106
d40453e8
RP
107 time_t t0 = time(NULL);
108
07e98688 109 // max 10 sec
d40453e8 110 while (time(NULL) < t0 + 10 )
07e98688
GE
111 {
112 ss.fill_buffer(1000000);
113
114 if(ss.get_packet(data))
115 break;
116 }
117 }
118 }
119 CPPUNIT_ASSERT_EQUAL(string("hello"),data);
120 }
121
122 void UnixCommToServerAndBack()
123 {
b5922184 124 switch(child_pid=fork())
07e98688
GE
125 {
126 case -1:
127 {
128 CPPUNIT_FAIL("fork error");
129 break;
130 }
131 case 0:
132 // child
133 {
441d41fe 134 try
07e98688 135 {
441d41fe
TJ
136 socket_server ss("./socket");
137 ss.set_logging(&cerr,debug);
07e98688 138
441d41fe 139 time_t t0 = time(NULL);
07e98688 140
441d41fe
TJ
141 // max 10 sec
142 while (time(NULL) < t0 + 10 )
07e98688 143 {
441d41fe 144 ss.fill_buffer(1000000);
07e98688 145
441d41fe
TJ
146 string data;
147 unsigned int cid;
07e98688 148
441d41fe
TJ
149 if(ss.get_packet(data,cid))
150 {
151 server_connection* con=ss.get_connection(cid);
152
153 if (data=="QUIT")
154 break;
155
156 if (data=="ABC")
157 con->write("DEF");
158 else
159 con->write("xyz");
160 }
07e98688 161 }
441d41fe
TJ
162 } catch(...)
163 {
164 std::cerr << "exception in child. ignoring\n";
07e98688 165 }
441d41fe 166
07e98688
GE
167 // don't call atexit and stuff
168 _exit(0);
169 }
170
171 default:
172 // parent
173 {
174 string data;
175
176 // wait till server is up
177 sleep(1);
178 socket_client_connection sc("./socket");
179
180 sc.write("ABC");
181
182 sc.fill_buffer(1000000);
183 sc.get_packet(data);
184
185 CPPUNIT_ASSERT_EQUAL(string("DEF"),data);
186
187 sc.write("HAHA");
188
189 sc.fill_buffer(1000000);
190 sc.get_packet(data);
191
192 CPPUNIT_ASSERT_EQUAL(string("xyz"),data);
193
194 sc.write("QUIT");
195 }
196 }
197 }
198
58b327c6
GE
199 void UnixCommToServerAndBackBig()
200 {
b5922184 201 switch(child_pid=fork())
58b327c6
GE
202 {
203 case -1:
204 {
205 CPPUNIT_FAIL("fork error");
206 break;
207 }
208 case 0:
209 // child
210 {
441d41fe 211 try
58b327c6 212 {
441d41fe
TJ
213 socket_server ss("./socket");
214 ss.set_logging(&cerr,debug);
58b327c6 215
441d41fe 216 time_t t0 = time(NULL);
58b327c6 217
441d41fe
TJ
218 // max 10 sec
219 while (time(NULL) < t0 + 10 )
58b327c6 220 {
441d41fe 221 ss.fill_buffer(1000000);
58b327c6 222
441d41fe
TJ
223 string data;
224 unsigned int cid;
58b327c6 225
441d41fe
TJ
226 if(ss.get_packet(data,cid))
227 {
228 server_connection* con=ss.get_connection(cid);
229
230 if (data=="QUIT")
231 break;
232
233 con->write(string().insert(0,100*1024,'y'));
234 }
58b327c6 235 }
441d41fe
TJ
236 std::cerr << "child: OVER" << std::endl;
237 } catch(...)
238 {
239 std::cerr << "exception in child. ignoring\n";
58b327c6 240 }
441d41fe 241
58b327c6
GE
242 // don't call atexit and stuff
243 _exit(0);
244 }
245
246 default:
247 // parent
248 {
249 string data;
250
251 // wait till server is up
252 sleep(1);
253 socket_client_connection sc("./socket");
254
255 sc.write(string().insert(0,100*1024,'x'));
256
257 while (!sc.get_packet(data))
258 sc.fill_buffer(1000000);
259
260 CPPUNIT_ASSERT_EQUAL(string().insert(0,100*1024,'y'),data);
261
262 sc.write("QUIT");
263 }
264 }
265 }
266
cc68aabb
GE
267 void IPCommToServer()
268 {
cc68aabb
GE
269 string data;
270
b5922184 271 switch(child_pid=fork())
cc68aabb
GE
272 {
273 case -1:
274 {
275 CPPUNIT_FAIL("fork error");
276 break;
277 }
278 case 0:
279 // child
280 {
441d41fe
TJ
281 try
282 {
283 // wait till server is up
284 sleep(1);
285 socket_client_connection sc(6666);
286 sc.write("hello");
287 } catch(...)
288 {
289 std::cerr << "exception in child. ignoring\n";
290 }
291
cc68aabb
GE
292 // don't call atexit and stuff
293 _exit(0);
294 }
295
296 default:
297 // parent
298 {
299 socket_server ss(6666);
300
d40453e8
RP
301 time_t t0 = time(NULL);
302
cc68aabb 303 // max 10 sec
d40453e8 304 while (time(NULL) < t0 + 10 )
cc68aabb
GE
305 {
306 ss.fill_buffer(1000000);
307
308 if(ss.get_packet(data))
309 break;
310 }
311 }
312 }
313 CPPUNIT_ASSERT_EQUAL(string("hello"),data);
314 }
315
7087e187
GE
316 void IPCommToServerAndBack()
317 {
b5922184 318 switch(child_pid=fork())
7087e187
GE
319 {
320 case -1:
321 {
322 CPPUNIT_FAIL("fork error");
323 break;
324 }
325 case 0:
326 // child
327 {
441d41fe 328 try
7087e187 329 {
441d41fe
TJ
330 socket_server ss(6666);
331 ss.set_logging(&cerr,debug);
7087e187 332
441d41fe 333 time_t t0 = time(NULL);
7087e187 334
441d41fe
TJ
335 // max 10 sec
336 while (time(NULL) < t0 + 10 )
7087e187 337 {
441d41fe
TJ
338 ss.fill_buffer(1000000);
339
340 string data;
341 unsigned int cid;
7087e187 342
441d41fe
TJ
343 if(ss.get_packet(data,cid))
344 {
345 server_connection* con=ss.get_connection(cid);
7087e187 346
441d41fe
TJ
347 if (data=="QUIT")
348 break;
349
350 if (data=="ABC")
351 con->write("DEF");
352 else
353 con->write("xyz");
354 }
7087e187 355 }
441d41fe
TJ
356 } catch(...)
357 {
358 std::cerr << "exception in child. ignoring\n";
7087e187
GE
359 }
360 // don't call atexit and stuff
361 _exit(0);
362 }
363
364 default:
365 // parent
366 {
367 string data;
368
369 // wait till server is up
370 sleep(1);
b604df5f 371 socket_client_connection sc(6666);
7087e187
GE
372 sc.write("ABC");
373
374 sc.fill_buffer(1000000);
375 sc.get_packet(data);
376
377 CPPUNIT_ASSERT_EQUAL(string("DEF"),data);
378
379 sc.write("HAHA");
380
381 sc.fill_buffer(1000000);
382 sc.get_packet(data);
383
384 CPPUNIT_ASSERT_EQUAL(string("xyz"),data);
385
386 sc.write("QUIT");
387 }
388 }
389 }
390
517d1214
RP
391
392 void IPCommToServerAndBackBig()
393 {
b5922184 394 switch(child_pid=fork())
517d1214
RP
395 {
396 case -1:
397 {
398 CPPUNIT_FAIL("fork error");
399 break;
400 }
401 case 0:
402 // child
403 {
441d41fe 404 try
517d1214 405 {
441d41fe
TJ
406 socket_server ss(6666);
407 ss.set_logging(&cerr,debug);
517d1214 408
441d41fe 409 time_t t0 = time(NULL);
517d1214 410
441d41fe
TJ
411 // max 10 sec
412 while (time(NULL) < t0 + 10 )
517d1214 413 {
441d41fe 414 ss.fill_buffer(1000000);
517d1214 415
441d41fe
TJ
416 string data;
417 unsigned int cid;
517d1214 418
441d41fe
TJ
419 if(ss.get_packet(data,cid))
420 {
421 server_connection* con=ss.get_connection(cid);
517d1214 422
441d41fe
TJ
423 socket_handler* alias= dynamic_cast< socket_handler* >(con);
424
425 if (data=="QUIT")
426 break;
427
428 alias->set_write_block_size( 4093 );
429 con->write(string().insert(0,2048*1024,'y'));
430 }
517d1214 431 }
441d41fe
TJ
432 } catch(...)
433 {
434 std::cerr << "exception in child. ignoring\n";
517d1214
RP
435 }
436 // don't call atexit and stuff
437 _exit(0);
438 }
439
440 default:
441 // parent
442 {
443 string data;
444
445 // wait till server is up
446 sleep(1);
447 socket_client_connection sc(6666);
448
449 sc.write(string().insert(0,100*1024,'x'));
450
451 while (!sc.get_packet(data))
452 sc.fill_buffer(1000000);
453
454 CPPUNIT_ASSERT_EQUAL(string().insert(0,2048*1024,'y'),data);
455
456 sc.write("QUIT");
457 }
458 }
459 } // eo IPCommToServerAndBackBig()
07e98688
GE
460};
461
462CPPUNIT_TEST_SUITE_REGISTRATION(test_comm);