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