libt2n: (gerd) command handling, unit-test still bailing out
[libt2n] / test / simplecmd.cpp
... / ...
CommitLineData
1/***************************************************************************
2 * Copyright (C) 2004 by Intra2net AG *
3 * info@intra2net.com *
4 * *
5 ***************************************************************************/
6
7#include <sys/types.h>
8#include <unistd.h>
9#include <errno.h>
10#include <signal.h>
11#include <stdio.h>
12
13#include <iostream>
14#include <string>
15#include <sstream>
16#include <stdexcept>
17
18#include <cppunit/extensions/TestFactoryRegistry.h>
19#include <cppunit/ui/text/TestRunner.h>
20#include <cppunit/extensions/HelperMacros.h>
21
22#include <container.hxx>
23#include <socket_client.hxx>
24#include <socket_server.hxx>
25#include <command_client.hxx>
26#include <command_server.hxx>
27
28#include <boost/serialization/serialization.hpp>
29#include <boost/serialization/level.hpp>
30#include <boost/serialization/tracking.hpp>
31#include <boost/serialization/export.hpp>
32
33using namespace std;
34using namespace libt2n;
35using namespace CppUnit;
36
37string testfunc(const string& str)
38{
39 string ret(str);
40 ret+=", testfunc() was here";
41 return ret;
42}
43
44class testfunc_res : public result
45{
46 private:
47 string res;
48
49 friend class boost::serialization::access;
50 template<class Archive>
51 void serialize(Archive & ar, const unsigned int version)
52 {
53 ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(result);
54 ar & BOOST_SERIALIZATION_NVP(res);
55 }
56
57 public:
58 testfunc_res()
59 { }
60
61 testfunc_res(const string& str)
62 {
63 res=str;
64 }
65
66 string get_data()
67 {
68 return res;
69 }
70};
71
72class testfunc_cmd : public libt2n::command
73{
74 private:
75 string param;
76
77 friend class boost::serialization::access;
78 template<class Archive>
79 void serialize(Archive & ar, const unsigned int version)
80 {
81 cerr << "ser: testfunc_cmd" << endl;
82 ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(libt2n::command);
83 ar & BOOST_SERIALIZATION_NVP(param);
84 }
85
86 public:
87 testfunc_cmd()
88 { }
89
90 testfunc_cmd(const string& str)
91 {
92 param=str;
93 }
94
95 result* operator()()
96 {
97 return new testfunc_res(testfunc(param));
98 }
99};
100
101
102BOOST_CLASS_EXPORT(testfunc_cmd)
103BOOST_CLASS_EXPORT(testfunc_res)
104
105class test_simplecmd : public TestFixture
106{
107 CPPUNIT_TEST_SUITE(test_simplecmd);
108
109 CPPUNIT_TEST(SimpleCmd);
110
111 CPPUNIT_TEST_SUITE_END();
112
113 public:
114
115 void setUp()
116 {
117}
118
119 void tearDown()
120 { }
121
122 void SimpleCmd()
123 {
124 pid_t pid;
125
126 switch(pid=fork())
127 {
128 case -1:
129 {
130 CPPUNIT_FAIL("fork error");
131 break;
132 }
133 case 0:
134 // child
135 {
136 socket_server ss("./socket");
137 ss.set_logging(&cerr,debug);
138 command_server cs(ss);
139
140 // max 10 sec
141 for (int i=0; i < 10; i++)
142 cs.handle(1000000);
143
144 // don't call atexit and stuff
145 _exit(0);
146 }
147
148 default:
149 // parent
150 {
151 // wait till server is up
152 sleep(1);
153 socket_client_connection sc("./socket");
154 command_client cc(sc);
155
156 result_container rc;
157 cc.send_command(new testfunc_cmd("hello"),rc);
158
159 string ret=dynamic_cast<testfunc_res*>(rc.get_result())->get_data();
160
161 CPPUNIT_ASSERT_EQUAL(string("hello, testfunc() was here"),ret);
162 }
163 }
164 }
165
166
167};
168
169CPPUNIT_TEST_SUITE_REGISTRATION(test_simplecmd);