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