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