libt2n: (gerd) client callbacks
[libt2n] / test / simplecmd.cpp
CommitLineData
d184c648
GE
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
a7170401
GE
22#include <boost/archive/binary_oarchive.hpp>
23#include <boost/archive/binary_iarchive.hpp>
d535333f
GE
24#include <boost/archive/xml_oarchive.hpp>
25#include <boost/archive/xml_iarchive.hpp>
a7170401
GE
26#include <boost/serialization/serialization.hpp>
27
d184c648
GE
28#include <container.hxx>
29#include <socket_client.hxx>
30#include <socket_server.hxx>
31#include <command_client.hxx>
32#include <command_server.hxx>
33
d184c648 34using namespace std;
d184c648
GE
35using namespace CppUnit;
36
37string testfunc(const string& str)
38{
e453407d
GE
39 if (str=="throw")
40 throw libt2n::t2n_runtime_error("throw me around");
d184c648
GE
41 string ret(str);
42 ret+=", testfunc() was here";
43 return ret;
44}
45
d535333f 46class testfunc_res : public libt2n::result
d184c648
GE
47{
48 private:
49 string res;
50
51 friend class boost::serialization::access;
52 template<class Archive>
53 void serialize(Archive & ar, const unsigned int version)
54 {
d535333f 55 ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(libt2n::result);
d184c648
GE
56 ar & BOOST_SERIALIZATION_NVP(res);
57 }
58
59 public:
60 testfunc_res()
61 { }
62
63 testfunc_res(const string& str)
64 {
65 res=str;
66 }
67
68 string get_data()
69 {
70 return res;
71 }
72};
73
a7170401 74
d184c648
GE
75class testfunc_cmd : public libt2n::command
76{
77 private:
78 string param;
79
80 friend class boost::serialization::access;
81 template<class Archive>
82 void serialize(Archive & ar, const unsigned int version)
83 {
d184c648
GE
84 ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(libt2n::command);
85 ar & BOOST_SERIALIZATION_NVP(param);
86 }
87
88 public:
89 testfunc_cmd()
90 { }
91
92 testfunc_cmd(const string& str)
93 {
94 param=str;
95 }
96
d535333f 97 libt2n::result* operator()()
d184c648
GE
98 {
99 return new testfunc_res(testfunc(param));
100 }
101};
102
a7170401 103#include <boost/serialization/export.hpp>
d184c648
GE
104
105BOOST_CLASS_EXPORT(testfunc_cmd)
106BOOST_CLASS_EXPORT(testfunc_res)
107
d535333f
GE
108using namespace libt2n;
109
d184c648
GE
110class test_simplecmd : public TestFixture
111{
112 CPPUNIT_TEST_SUITE(test_simplecmd);
113
114 CPPUNIT_TEST(SimpleCmd);
e453407d 115 CPPUNIT_TEST(SimpleException);
d184c648
GE
116
117 CPPUNIT_TEST_SUITE_END();
118
119 public:
120
121 void setUp()
122 {
123}
124
125 void tearDown()
126 { }
127
128 void SimpleCmd()
129 {
130 pid_t pid;
131
132 switch(pid=fork())
133 {
134 case -1:
135 {
136 CPPUNIT_FAIL("fork error");
137 break;
138 }
139 case 0:
140 // child
141 {
142 socket_server ss("./socket");
d184c648
GE
143 command_server cs(ss);
144
145 // max 10 sec
146 for (int i=0; i < 10; i++)
147 cs.handle(1000000);
148
149 // don't call atexit and stuff
150 _exit(0);
151 }
152
153 default:
154 // parent
155 {
156 // wait till server is up
157 sleep(1);
158 socket_client_connection sc("./socket");
a7170401 159 sc.set_logging(&cerr,debug);
d184c648
GE
160 command_client cc(sc);
161
162 result_container rc;
163 cc.send_command(new testfunc_cmd("hello"),rc);
164
165 string ret=dynamic_cast<testfunc_res*>(rc.get_result())->get_data();
166
167 CPPUNIT_ASSERT_EQUAL(string("hello, testfunc() was here"),ret);
168 }
169 }
170 }
171
e453407d
GE
172 void SimpleException()
173 {
174 pid_t pid;
175
176 switch(pid=fork())
177 {
178 case -1:
179 {
180 CPPUNIT_FAIL("fork error");
181 break;
182 }
183 case 0:
184 // child
185 {
186 socket_server ss("./socket");
187 command_server cs(ss);
188
189 // max 10 sec
190 for (int i=0; i < 10; i++)
191 cs.handle(1000000);
192
193 // don't call atexit and stuff
194 _exit(0);
195 }
196
197 default:
198 // parent
199 {
200 // wait till server is up
201 sleep(1);
202 socket_client_connection sc("./socket");
203 sc.set_logging(&cerr,debug);
204 command_client cc(sc);
205
206 result_container rc;
207 cc.send_command(new testfunc_cmd("throw"),rc);
208
209 string ret;
210
211 try
212 {
213 ret=dynamic_cast<testfunc_res*>(rc.get_result())->get_data();
214 }
215 catch(t2n_runtime_error &e)
216 { ret=e.what(); }
217 catch(...)
218 { throw; }
219
220 CPPUNIT_ASSERT_EQUAL(string("throw me around"),ret);
221 }
222 }
223 }
d184c648
GE
224
225};
226
227CPPUNIT_TEST_SUITE_REGISTRATION(test_simplecmd);