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>
33 #include <cppunit/extensions/TestFactoryRegistry.h>
34 #include <cppunit/ui/text/TestRunner.h>
35 #include <cppunit/extensions/HelperMacros.h>
37 #include <boost/archive/binary_oarchive.hpp>
38 #include <boost/archive/binary_iarchive.hpp>
39 #include <boost/archive/xml_oarchive.hpp>
40 #include <boost/archive/xml_iarchive.hpp>
41 #include <boost/serialization/serialization.hpp>
42 #include <boost/serialization/export.hpp>
44 #include <container.hxx>
45 #include <socket_client.hxx>
46 #include <socket_server.hxx>
47 #include <command_client.hxx>
48 #include <command_server.hxx>
51 using namespace CppUnit;
52 using namespace libt2n;
54 string testfunc4(const string& str)
57 throw libt2n::t2n_runtime_error("throw me around");
59 ret+=", testfunc() was here";
63 class testfunc4_res : public libt2n::result
68 friend class boost::serialization::access;
69 template<class Archive>
70 void serialize(Archive & ar, const unsigned int version)
72 ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(libt2n::result);
73 ar & BOOST_SERIALIZATION_NVP(res);
80 testfunc4_res(const string& str)
91 class cmd_group_a : public command
94 friend class boost::serialization::access;
95 template<class Archive>
96 void serialize(Archive & ar, const unsigned int version)
98 ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(libt2n::command);
102 class cmd_group_b : public command
105 friend class boost::serialization::access;
106 template<class Archive>
107 void serialize(Archive & ar, const unsigned int version)
109 ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(libt2n::command);
113 class testfunc4a_cmd : public cmd_group_a
118 friend class boost::serialization::access;
119 template<class Archive>
120 void serialize(Archive & ar, const unsigned int version)
122 ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(cmd_group_a);
123 ar & BOOST_SERIALIZATION_NVP(param);
130 testfunc4a_cmd(const string& str)
135 libt2n::result* operator()()
137 return new testfunc4_res(testfunc4(param));
141 class testfunc4b_cmd : public cmd_group_b
146 friend class boost::serialization::access;
147 template<class Archive>
148 void serialize(Archive & ar, const unsigned int version)
150 ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(cmd_group_b);
151 ar & BOOST_SERIALIZATION_NVP(param);
158 testfunc4b_cmd(const string& str)
163 libt2n::result* operator()()
165 return new testfunc4_res(testfunc4(param));
170 BOOST_CLASS_EXPORT(testfunc4_res)
171 BOOST_CLASS_EXPORT(cmd_group_a)
172 BOOST_CLASS_EXPORT(cmd_group_b)
173 BOOST_CLASS_EXPORT(testfunc4a_cmd)
174 BOOST_CLASS_EXPORT(testfunc4b_cmd)
176 class test_cmdgroup : public TestFixture
178 CPPUNIT_TEST_SUITE(test_cmdgroup);
180 CPPUNIT_TEST(GroupOk);
181 CPPUNIT_TEST(WrongGroup);
183 CPPUNIT_TEST_SUITE_END();
194 // make sure the server-child is dead before the next test runs
195 kill(child_pid,SIGKILL);
201 switch(child_pid=fork())
205 CPPUNIT_FAIL("fork error");
213 socket_server ss("./socket");
214 group_command_server<cmd_group_a> cs(ss);
217 for (int i=0; i < 10; i++)
221 std::cerr << "exception in child. ignoring\n";
224 // don't call atexit and stuff
231 // wait till server is up
233 socket_client_connection sc("./socket");
234 command_client cc(&sc);
237 cc.send_command(new testfunc4a_cmd("hello"),rc);
239 string ret=dynamic_cast<testfunc4_res*>(rc.get_result())->get_data();
241 CPPUNIT_ASSERT_EQUAL(string("hello, testfunc() was here"),ret);
248 switch(child_pid=fork())
252 CPPUNIT_FAIL("fork error");
260 socket_server ss("./socket");
261 group_command_server<cmd_group_b> cs(ss);
264 for (int i=0; i < 10; i++)
268 std::cerr << "exception in child. ignoring\n";
271 // don't call atexit and stuff
278 // wait till server is up
280 socket_client_connection sc("./socket");
281 command_client cc(&sc);
284 cc.send_command(new testfunc4a_cmd("hello"),rc);
290 ret=dynamic_cast<testfunc4_res*>(rc.get_result())->get_data();
292 catch(t2n_command_error &e)
297 string expected_what="illegal command of type ";
299 CPPUNIT_ASSERT_EQUAL(expected_what,ret.substr(0,expected_what.size()));
306 CPPUNIT_TEST_SUITE_REGISTRATION(test_cmdgroup);