X-Git-Url: http://developer.intra2net.com/git/?p=libt2n;a=blobdiff_plain;f=test%2Fcomm.cpp;fp=test%2Fcomm.cpp;h=60907b206656a260a21dc1dcb067396e47698068;hp=0000000000000000000000000000000000000000;hb=07e98688a1a8c3e915ce923f79261a88251a9edd;hpb=644c4d262aab14ee4ec1cfade83a2e99568098d3 diff --git a/test/comm.cpp b/test/comm.cpp new file mode 100644 index 0000000..60907b2 --- /dev/null +++ b/test/comm.cpp @@ -0,0 +1,159 @@ +/*************************************************************************** + * Copyright (C) 2004 by Intra2net AG * + * info@intra2net.com * + * * + ***************************************************************************/ + +#include +#include +#include +#include +#include + +#include +#include +#include +#include + +#include +#include +#include + +#include +#include + +using namespace std; +using namespace libt2n; +using namespace CppUnit; + +class test_comm : public TestFixture +{ + CPPUNIT_TEST_SUITE(test_comm); + + CPPUNIT_TEST(UnixCommToServer); + CPPUNIT_TEST(UnixCommToServerAndBack); + + CPPUNIT_TEST_SUITE_END(); + + public: + + void setUp() + { } + + void tearDown() + { } + + void UnixCommToServer() + { + pid_t pid; + string data; + + switch(pid=fork()) + { + case -1: + { + CPPUNIT_FAIL("fork error"); + break; + } + case 0: + // child + { + // wait till server is up + sleep(1); + socket_client_connection sc("./socket"); + sc.write("hello"); + // don't call atexit and stuff + _exit(0); + } + + default: + // parent + { + socket_server ss("./socket"); + + // max 10 sec + for (int i=0; i < 10; i++) + { + ss.fill_buffer(1000000); + + if(ss.get_packet(data)) + break; + } + } + } + CPPUNIT_ASSERT_EQUAL(string("hello"),data); + } + + void UnixCommToServerAndBack() + { + pid_t pid; + + switch(pid=fork()) + { + case -1: + { + CPPUNIT_FAIL("fork error"); + break; + } + case 0: + // child + { + socket_server ss("./socket"); + ss.set_logging(&cerr,debug); + + // max 10 sec + for (int i=0; i < 10; i++) + { + ss.fill_buffer(1000000); + + string data; + unsigned int cid; + + if(ss.get_packet(data,cid)) + { + server_connection* con=ss.get_connection(cid); + + if (data=="QUIT") + break; + + if (data=="ABC") + con->write("DEF"); + else + con->write("xyz"); + } + } + // don't call atexit and stuff + _exit(0); + } + + default: + // parent + { + string data; + + // wait till server is up + sleep(1); + socket_client_connection sc("./socket"); + + sc.write("ABC"); + + sc.fill_buffer(1000000); + sc.get_packet(data); + + CPPUNIT_ASSERT_EQUAL(string("DEF"),data); + + sc.write("HAHA"); + + sc.fill_buffer(1000000); + sc.get_packet(data); + + CPPUNIT_ASSERT_EQUAL(string("xyz"),data); + + sc.write("QUIT"); + } + } + } + +}; + +CPPUNIT_TEST_SUITE_REGISTRATION(test_comm);