2 Copyright (C) 2004 by Intra2net AG
4 The software in this package is distributed under the GNU General
5 Public License version 2 (with a special exception described below).
7 A copy of GNU General Public License (GPL) is included in this distribution,
8 in the file COPYING.GPL.
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.
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.
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.
22 #include <sys/types.h>
34 #include <cppunit/extensions/TestFactoryRegistry.h>
35 #include <cppunit/ui/text/TestRunner.h>
36 #include <cppunit/extensions/HelperMacros.h>
38 #include <socket_client.hxx>
39 #include <socket_server.hxx>
42 using namespace libt2n;
43 using namespace CppUnit;
46 class test_comm : public TestFixture
48 CPPUNIT_TEST_SUITE(test_comm);
50 CPPUNIT_TEST(UnixCommToServer);
51 CPPUNIT_TEST(UnixCommToServerAndBack);
52 CPPUNIT_TEST(UnixCommToServerAndBackBig);
53 CPPUNIT_TEST(IPCommToServer);
54 CPPUNIT_TEST(IPCommToServerAndBack);
55 CPPUNIT_TEST(IPCommToServerAndBackBig);
57 CPPUNIT_TEST_SUITE_END();
68 // make sure the server-child is dead before the next test runs
69 kill(child_pid,SIGKILL);
73 void UnixCommToServer()
77 switch(child_pid=fork())
81 CPPUNIT_FAIL("fork error");
89 // wait till server is up
91 socket_client_connection sc("./socket");
95 std::cerr << "exception in child. ignoring\n";
98 // don't call atexit and stuff
105 socket_server ss("./socket");
107 time_t t0 = time(NULL);
110 while (time(NULL) < t0 + 10 )
112 ss.fill_buffer(1000000);
114 if(ss.get_packet(data))
119 CPPUNIT_ASSERT_EQUAL(string("hello"),data);
122 void UnixCommToServerAndBack()
124 switch(child_pid=fork())
128 CPPUNIT_FAIL("fork error");
136 socket_server ss("./socket");
137 ss.set_logging(&cerr,debug);
139 time_t t0 = time(NULL);
142 while (time(NULL) < t0 + 10 )
144 ss.fill_buffer(1000000);
149 if(ss.get_packet(data,cid))
151 server_connection* con=ss.get_connection(cid);
164 std::cerr << "exception in child. ignoring\n";
167 // don't call atexit and stuff
176 // wait till server is up
178 socket_client_connection sc("./socket");
182 sc.fill_buffer(1000000);
185 CPPUNIT_ASSERT_EQUAL(string("DEF"),data);
189 sc.fill_buffer(1000000);
192 CPPUNIT_ASSERT_EQUAL(string("xyz"),data);
199 void UnixCommToServerAndBackBig()
201 switch(child_pid=fork())
205 CPPUNIT_FAIL("fork error");
213 socket_server ss("./socket");
214 ss.set_logging(&cerr,debug);
216 time_t t0 = time(NULL);
219 while (time(NULL) < t0 + 10 )
221 ss.fill_buffer(1000000);
226 if(ss.get_packet(data,cid))
228 server_connection* con=ss.get_connection(cid);
233 con->write(string().insert(0,100*1024,'y'));
236 std::cerr << "child: OVER" << std::endl;
239 std::cerr << "exception in child. ignoring\n";
242 // don't call atexit and stuff
251 // wait till server is up
253 socket_client_connection sc("./socket");
255 sc.write(string().insert(0,100*1024,'x'));
257 while (!sc.get_packet(data))
258 sc.fill_buffer(1000000);
260 CPPUNIT_ASSERT_EQUAL(string().insert(0,100*1024,'y'),data);
267 void IPCommToServer()
271 switch(child_pid=fork())
275 CPPUNIT_FAIL("fork error");
283 // wait till server is up
285 socket_client_connection sc(6666);
289 std::cerr << "exception in child. ignoring\n";
292 // don't call atexit and stuff
299 socket_server ss(6666);
301 time_t t0 = time(NULL);
304 while (time(NULL) < t0 + 10 )
306 ss.fill_buffer(1000000);
308 if(ss.get_packet(data))
313 CPPUNIT_ASSERT_EQUAL(string("hello"),data);
316 void IPCommToServerAndBack()
318 switch(child_pid=fork())
322 CPPUNIT_FAIL("fork error");
330 socket_server ss(6666);
331 ss.set_logging(&cerr,debug);
333 time_t t0 = time(NULL);
336 while (time(NULL) < t0 + 10 )
338 ss.fill_buffer(1000000);
343 if(ss.get_packet(data,cid))
345 server_connection* con=ss.get_connection(cid);
358 std::cerr << "exception in child. ignoring\n";
360 // don't call atexit and stuff
369 // wait till server is up
371 socket_client_connection sc(6666);
374 sc.fill_buffer(1000000);
377 CPPUNIT_ASSERT_EQUAL(string("DEF"),data);
381 sc.fill_buffer(1000000);
384 CPPUNIT_ASSERT_EQUAL(string("xyz"),data);
392 void IPCommToServerAndBackBig()
394 switch(child_pid=fork())
398 CPPUNIT_FAIL("fork error");
406 socket_server ss(6666);
407 ss.set_logging(&cerr,debug);
409 time_t t0 = time(NULL);
412 while (time(NULL) < t0 + 10 )
414 ss.fill_buffer(1000000);
419 if(ss.get_packet(data,cid))
421 server_connection* con=ss.get_connection(cid);
423 socket_handler* alias= dynamic_cast< socket_handler* >(con);
428 alias->set_write_block_size( 4093 );
429 con->write(string().insert(0,2048*1024,'y'));
434 std::cerr << "exception in child. ignoring\n";
436 // don't call atexit and stuff
445 // wait till server is up
447 socket_client_connection sc(6666);
449 sc.write(string().insert(0,100*1024,'x'));
451 while (!sc.get_packet(data))
452 sc.fill_buffer(1000000);
454 CPPUNIT_ASSERT_EQUAL(string().insert(0,2048*1024,'y'),data);
459 } // eo IPCommToServerAndBackBig()
462 CPPUNIT_TEST_SUITE_REGISTRATION(test_comm);