1 /***************************************************************************
2 * Copyright (C) 2004 by Intra2net AG *
5 ***************************************************************************/
19 #include <cppunit/extensions/TestFactoryRegistry.h>
20 #include <cppunit/ui/text/TestRunner.h>
21 #include <cppunit/extensions/HelperMacros.h>
23 #include <socket_client.hxx>
24 #include <socket_server.hxx>
27 using namespace libt2n;
28 using namespace CppUnit;
31 class test_comm : public TestFixture
33 CPPUNIT_TEST_SUITE(test_comm);
35 CPPUNIT_TEST(UnixCommToServer);
36 CPPUNIT_TEST(UnixCommToServerAndBack);
37 CPPUNIT_TEST(UnixCommToServerAndBackBig);
38 CPPUNIT_TEST(IPCommToServer);
39 CPPUNIT_TEST(IPCommToServerAndBack);
40 CPPUNIT_TEST(IPCommToServerAndBackBig);
42 CPPUNIT_TEST_SUITE_END();
53 // make sure the server-child is dead before the next test runs
54 kill(child_pid,SIGKILL);
58 void UnixCommToServer()
62 switch(child_pid=fork())
66 CPPUNIT_FAIL("fork error");
74 // wait till server is up
76 socket_client_connection sc("./socket");
80 std::cerr << "exception in child. ignoring\n";
83 // don't call atexit and stuff
90 socket_server ss("./socket");
92 time_t t0 = time(NULL);
95 while (time(NULL) < t0 + 10 )
97 ss.fill_buffer(1000000);
99 if(ss.get_packet(data))
104 CPPUNIT_ASSERT_EQUAL(string("hello"),data);
107 void UnixCommToServerAndBack()
109 switch(child_pid=fork())
113 CPPUNIT_FAIL("fork error");
121 socket_server ss("./socket");
122 ss.set_logging(&cerr,debug);
124 time_t t0 = time(NULL);
127 while (time(NULL) < t0 + 10 )
129 ss.fill_buffer(1000000);
134 if(ss.get_packet(data,cid))
136 server_connection* con=ss.get_connection(cid);
149 std::cerr << "exception in child. ignoring\n";
152 // don't call atexit and stuff
161 // wait till server is up
163 socket_client_connection sc("./socket");
167 sc.fill_buffer(1000000);
170 CPPUNIT_ASSERT_EQUAL(string("DEF"),data);
174 sc.fill_buffer(1000000);
177 CPPUNIT_ASSERT_EQUAL(string("xyz"),data);
184 void UnixCommToServerAndBackBig()
186 switch(child_pid=fork())
190 CPPUNIT_FAIL("fork error");
198 socket_server ss("./socket");
199 ss.set_logging(&cerr,debug);
201 time_t t0 = time(NULL);
204 while (time(NULL) < t0 + 10 )
206 ss.fill_buffer(1000000);
211 if(ss.get_packet(data,cid))
213 server_connection* con=ss.get_connection(cid);
218 con->write(string().insert(0,100*1024,'y'));
221 std::cerr << "child: OVER" << std::endl;
224 std::cerr << "exception in child. ignoring\n";
227 // don't call atexit and stuff
236 // wait till server is up
238 socket_client_connection sc("./socket");
240 sc.write(string().insert(0,100*1024,'x'));
242 while (!sc.get_packet(data))
243 sc.fill_buffer(1000000);
245 CPPUNIT_ASSERT_EQUAL(string().insert(0,100*1024,'y'),data);
252 void IPCommToServer()
256 switch(child_pid=fork())
260 CPPUNIT_FAIL("fork error");
268 // wait till server is up
270 socket_client_connection sc(6666);
274 std::cerr << "exception in child. ignoring\n";
277 // don't call atexit and stuff
284 socket_server ss(6666);
286 time_t t0 = time(NULL);
289 while (time(NULL) < t0 + 10 )
291 ss.fill_buffer(1000000);
293 if(ss.get_packet(data))
298 CPPUNIT_ASSERT_EQUAL(string("hello"),data);
301 void IPCommToServerAndBack()
303 switch(child_pid=fork())
307 CPPUNIT_FAIL("fork error");
315 socket_server ss(6666);
316 ss.set_logging(&cerr,debug);
318 time_t t0 = time(NULL);
321 while (time(NULL) < t0 + 10 )
323 ss.fill_buffer(1000000);
328 if(ss.get_packet(data,cid))
330 server_connection* con=ss.get_connection(cid);
343 std::cerr << "exception in child. ignoring\n";
345 // don't call atexit and stuff
354 // wait till server is up
356 socket_client_connection sc(6666);
359 sc.fill_buffer(1000000);
362 CPPUNIT_ASSERT_EQUAL(string("DEF"),data);
366 sc.fill_buffer(1000000);
369 CPPUNIT_ASSERT_EQUAL(string("xyz"),data);
377 void IPCommToServerAndBackBig()
379 switch(child_pid=fork())
383 CPPUNIT_FAIL("fork error");
391 socket_server ss(6666);
392 ss.set_logging(&cerr,debug);
394 time_t t0 = time(NULL);
397 while (time(NULL) < t0 + 10 )
399 ss.fill_buffer(1000000);
404 if(ss.get_packet(data,cid))
406 server_connection* con=ss.get_connection(cid);
408 socket_handler* alias= dynamic_cast< socket_handler* >(con);
413 alias->set_write_block_size( 4093 );
414 con->write(string().insert(0,2048*1024,'y'));
419 std::cerr << "exception in child. ignoring\n";
421 // don't call atexit and stuff
430 // wait till server is up
432 socket_client_connection sc(6666);
434 sc.write(string().insert(0,100*1024,'x'));
436 while (!sc.get_packet(data))
437 sc.fill_buffer(1000000);
439 CPPUNIT_ASSERT_EQUAL(string().insert(0,2048*1024,'y'),data);
444 } // eo IPCommToServerAndBackBig()
447 CPPUNIT_TEST_SUITE_REGISTRATION(test_comm);