1 /***************************************************************************
2 * Copyright (C) 2004 by Intra2net AG *
5 ***************************************************************************/
18 #include <cppunit/extensions/TestFactoryRegistry.h>
19 #include <cppunit/ui/text/TestRunner.h>
20 #include <cppunit/extensions/HelperMacros.h>
22 #include <boost/archive/binary_oarchive.hpp>
23 #include <boost/archive/binary_iarchive.hpp>
24 #include <boost/archive/xml_oarchive.hpp>
25 #include <boost/archive/xml_iarchive.hpp>
26 #include <boost/serialization/serialization.hpp>
28 #include <container.hxx>
29 #include <socket_client.hxx>
30 #include <socket_server.hxx>
31 #include <command_client.hxx>
32 #include <command_server.hxx>
35 using namespace CppUnit;
37 string testfunc(const string& str)
41 throw libt2n::t2n_runtime_error("throw me around");
43 ret.insert(0,100*1024,'x');
45 ret=str+", testfunc() was here";
49 class testfunc_res : public libt2n::result
54 friend class boost::serialization::access;
55 template<class Archive>
56 void serialize(Archive & ar, const unsigned int version)
58 ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(libt2n::result);
59 ar & BOOST_SERIALIZATION_NVP(res);
66 testfunc_res(const string& str)
78 class testfunc_cmd : public libt2n::command
83 friend class boost::serialization::access;
84 template<class Archive>
85 void serialize(Archive & ar, const unsigned int version)
87 ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(libt2n::command);
88 ar & BOOST_SERIALIZATION_NVP(param);
95 testfunc_cmd(const string& str)
100 libt2n::result* operator()()
102 return new testfunc_res(testfunc(param));
106 #include <boost/serialization/export.hpp>
108 BOOST_CLASS_EXPORT(testfunc_cmd)
109 BOOST_CLASS_EXPORT(testfunc_res)
111 using namespace libt2n;
113 class test_simplecmd : public TestFixture
115 CPPUNIT_TEST_SUITE(test_simplecmd);
117 CPPUNIT_TEST(SimpleCmd);
118 CPPUNIT_TEST(SimpleException);
119 CPPUNIT_TEST(BigReturn);
120 CPPUNIT_TEST(BigParameter);
122 CPPUNIT_TEST_SUITE_END();
133 // make sure the server-child is dead before the next test runs
134 kill(child_pid,SIGKILL);
140 switch(child_pid=fork())
144 CPPUNIT_FAIL("fork error");
150 socket_server ss("./socket");
151 command_server cs(ss);
154 for (int i=0; i < 10; i++)
157 // don't call atexit and stuff
164 // wait till server is up
166 socket_client_connection sc("./socket");
167 sc.set_logging(&cerr,debug);
168 command_client cc(&sc);
171 cc.send_command(new testfunc_cmd("hello"),rc);
173 string ret=dynamic_cast<testfunc_res*>(rc.get_result())->get_data();
175 CPPUNIT_ASSERT_EQUAL(string("hello, testfunc() was here"),ret);
180 void SimpleException()
182 switch(child_pid=fork())
186 CPPUNIT_FAIL("fork error");
192 socket_server ss("./socket");
193 command_server cs(ss);
196 for (int i=0; i < 10; i++)
199 // don't call atexit and stuff
206 // wait till server is up
208 socket_client_connection sc("./socket");
209 sc.set_logging(&cerr,debug);
210 command_client cc(&sc);
213 cc.send_command(new testfunc_cmd("throw"),rc);
219 ret=dynamic_cast<testfunc_res*>(rc.get_result())->get_data();
221 catch(t2n_runtime_error &e)
226 CPPUNIT_ASSERT_EQUAL(string("throw me around"),ret);
233 switch(child_pid=fork())
237 CPPUNIT_FAIL("fork error");
243 socket_server ss("./socket");
244 command_server cs(ss);
247 for (int i=0; i < 10; i++)
250 // don't call atexit and stuff
257 // wait till server is up
259 socket_client_connection sc("./socket");
260 command_client cc(&sc);
263 cc.send_command(new testfunc_cmd("big"),rc);
265 string ret=dynamic_cast<testfunc_res*>(rc.get_result())->get_data();
267 CPPUNIT_ASSERT_EQUAL(string().insert(0,100*1024,'x'),ret);
274 switch(child_pid=fork())
278 CPPUNIT_FAIL("fork error");
284 socket_server ss("./socket");
285 command_server cs(ss);
288 for (int i=0; i < 10; i++)
291 // don't call atexit and stuff
298 // wait till server is up
300 socket_client_connection sc("./socket");
301 command_client cc(&sc);
304 cc.send_command(new testfunc_cmd(string().insert(0,100*1024,'y')),rc);
306 string ret=dynamic_cast<testfunc_res*>(rc.get_result())->get_data();
308 CPPUNIT_ASSERT_EQUAL(string().insert(0,100*1024,'y')+", testfunc() was here",ret);
315 CPPUNIT_TEST_SUITE_REGISTRATION(test_simplecmd);