X-Git-Url: http://developer.intra2net.com/git/?p=libt2n;a=blobdiff_plain;f=test%2Fsimplecmd.cpp;fp=test%2Fsimplecmd.cpp;h=b48fab6def40a0e96a4c4243ee6502c1e64985f5;hp=0000000000000000000000000000000000000000;hb=d184c64894e6c4f3adb9467078acfc9e7446664a;hpb=7087e18783f91d2b889e880462d1d1da24831c28 diff --git a/test/simplecmd.cpp b/test/simplecmd.cpp new file mode 100644 index 0000000..b48fab6 --- /dev/null +++ b/test/simplecmd.cpp @@ -0,0 +1,169 @@ +/*************************************************************************** + * Copyright (C) 2004 by Intra2net AG * + * info@intra2net.com * + * * + ***************************************************************************/ + +#include +#include +#include +#include +#include + +#include +#include +#include +#include + +#include +#include +#include + +#include +#include +#include +#include +#include + +#include +#include +#include +#include + +using namespace std; +using namespace libt2n; +using namespace CppUnit; + +string testfunc(const string& str) +{ + string ret(str); + ret+=", testfunc() was here"; + return ret; +} + +class testfunc_res : public result +{ + private: + string res; + + friend class boost::serialization::access; + template + void serialize(Archive & ar, const unsigned int version) + { + ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(result); + ar & BOOST_SERIALIZATION_NVP(res); + } + + public: + testfunc_res() + { } + + testfunc_res(const string& str) + { + res=str; + } + + string get_data() + { + return res; + } +}; + +class testfunc_cmd : public libt2n::command +{ + private: + string param; + + friend class boost::serialization::access; + template + void serialize(Archive & ar, const unsigned int version) + { + cerr << "ser: testfunc_cmd" << endl; + ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(libt2n::command); + ar & BOOST_SERIALIZATION_NVP(param); + } + + public: + testfunc_cmd() + { } + + testfunc_cmd(const string& str) + { + param=str; + } + + result* operator()() + { + return new testfunc_res(testfunc(param)); + } +}; + + +BOOST_CLASS_EXPORT(testfunc_cmd) +BOOST_CLASS_EXPORT(testfunc_res) + +class test_simplecmd : public TestFixture +{ + CPPUNIT_TEST_SUITE(test_simplecmd); + + CPPUNIT_TEST(SimpleCmd); + + CPPUNIT_TEST_SUITE_END(); + + public: + + void setUp() + { +} + + void tearDown() + { } + + void SimpleCmd() + { + 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); + command_server cs(ss); + + // max 10 sec + for (int i=0; i < 10; i++) + cs.handle(1000000); + + // don't call atexit and stuff + _exit(0); + } + + default: + // parent + { + // wait till server is up + sleep(1); + socket_client_connection sc("./socket"); + command_client cc(sc); + + result_container rc; + cc.send_command(new testfunc_cmd("hello"),rc); + + string ret=dynamic_cast(rc.get_result())->get_data(); + + CPPUNIT_ASSERT_EQUAL(string("hello, testfunc() was here"),ret); + } + } + } + + +}; + +CPPUNIT_TEST_SUITE_REGISTRATION(test_simplecmd);