Migrate from cppunit to Boost.test
[libt2n] / test / simplecmd.cpp
... / ...
CommitLineData
1/*
2Copyright (C) 2004 by Intra2net AG
3
4The software in this package is distributed under the GNU General
5Public License version 2 (with a special exception described below).
6
7A copy of GNU General Public License (GPL) is included in this distribution,
8in the file COPYING.GPL.
9
10As a special exception, if other files instantiate templates or use macros
11or inline functions from this file, or you compile this file and link it
12with other works to produce a work based on this file, this file
13does not by itself cause the resulting work to be covered
14by the GNU General Public License.
15
16However the source code for this file must still be made available
17in accordance with section (3) of the GNU General Public License.
18
19This exception does not invalidate any other reasons why a work based
20on this file might be covered by the GNU General Public License.
21*/
22#include <sys/types.h>
23#include <unistd.h>
24#include <errno.h>
25#include <signal.h>
26#include <stdio.h>
27
28#include <iostream>
29#include <string>
30#include <sstream>
31#include <stdexcept>
32
33#define BOOST_TEST_DYN_LINK
34#include <boost/test/unit_test.hpp>
35
36#include <boost/archive/binary_oarchive.hpp>
37#include <boost/archive/binary_iarchive.hpp>
38#include <boost/archive/xml_oarchive.hpp>
39#include <boost/archive/xml_iarchive.hpp>
40#include <boost/serialization/serialization.hpp>
41
42#include <container.hxx>
43#include <socket_client.hxx>
44#include <socket_server.hxx>
45#include <command_client.hxx>
46#include <command_server.hxx>
47
48#include "test_fixtures.hxx"
49
50using namespace std;
51
52string testfunc(const string& str)
53{
54 string ret;
55 if (str=="throw")
56 throw libt2n::t2n_runtime_error("throw me around");
57 if (str=="big")
58 ret.insert(0,100*1024,'x');
59 else
60 ret=str+", testfunc() was here";
61 return ret;
62}
63
64class testfunc_res : public libt2n::result
65{
66 private:
67 string res;
68
69 friend class boost::serialization::access;
70 template<class Archive>
71 void serialize(Archive & ar, const unsigned int version)
72 {
73 ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(libt2n::result);
74 ar & BOOST_SERIALIZATION_NVP(res);
75 }
76
77 public:
78 testfunc_res()
79 { }
80
81 testfunc_res(const string& str)
82 {
83 res=str;
84 }
85
86 string get_data()
87 {
88 return res;
89 }
90};
91
92
93class testfunc_cmd : public libt2n::command
94{
95 private:
96 string param;
97
98 friend class boost::serialization::access;
99 template<class Archive>
100 void serialize(Archive & ar, const unsigned int version)
101 {
102 ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(libt2n::command);
103 ar & BOOST_SERIALIZATION_NVP(param);
104 }
105
106 public:
107 testfunc_cmd()
108 { }
109
110 testfunc_cmd(const string& str)
111 {
112 param=str;
113 }
114
115 libt2n::result* operator()()
116 {
117 return new testfunc_res(testfunc(param));
118 }
119};
120
121#include <boost/serialization/export.hpp>
122
123BOOST_CLASS_EXPORT(testfunc_cmd)
124BOOST_CLASS_EXPORT(testfunc_res)
125
126using namespace libt2n;
127
128BOOST_FIXTURE_TEST_SUITE(test_simplecmd, KillChildOnShutdownFixture)
129
130BOOST_AUTO_TEST_CASE(SimpleCmd)
131{
132 switch(child_pid=fork())
133 {
134 case -1:
135 {
136 BOOST_FAIL("fork error");
137 break;
138 }
139 case 0:
140 // child
141 {
142 try
143 {
144 socket_server ss("./socket");
145 command_server cs(ss);
146
147 // max 10 sec
148 for (int i=0; i < 10; i++)
149 cs.handle(1000000);
150 } catch(...)
151 {
152 std::cerr << "exception in child. ignoring\n";
153 }
154
155 // don't call atexit and stuff
156 _exit(0);
157 }
158
159 default:
160 // parent
161 {
162 // wait till server is up
163 sleep(1);
164 socket_client_connection sc("./socket");
165 sc.set_logging(&cerr,debug);
166 command_client cc(&sc);
167
168 result_container rc;
169 cc.send_command(new testfunc_cmd("hello"),rc);
170
171 string ret=dynamic_cast<testfunc_res*>(rc.get_result())->get_data();
172
173 BOOST_CHECK_EQUAL(string("hello, testfunc() was here"),ret);
174 }
175 }
176}
177
178BOOST_AUTO_TEST_CASE(SimpleException)
179{
180 switch(child_pid=fork())
181 {
182 case -1:
183 {
184 BOOST_FAIL("fork error");
185 break;
186 }
187 case 0:
188 // child
189 {
190 try
191 {
192 socket_server ss("./socket");
193 command_server cs(ss);
194
195 // max 10 sec
196 for (int i=0; i < 10; i++)
197 cs.handle(1000000);
198 } catch(...)
199 {
200 std::cerr << "exception in child. ignoring\n";
201 }
202
203 // don't call atexit and stuff
204 _exit(0);
205 }
206
207 default:
208 // parent
209 {
210 // wait till server is up
211 sleep(1);
212 socket_client_connection sc("./socket");
213 sc.set_logging(&cerr,debug);
214 command_client cc(&sc);
215
216 result_container rc;
217 cc.send_command(new testfunc_cmd("throw"),rc);
218
219 string ret;
220
221 try
222 {
223 ret=dynamic_cast<testfunc_res*>(rc.get_result())->get_data();
224 }
225 catch(t2n_runtime_error &e)
226 { ret=e.what(); }
227 catch(...)
228 { throw; }
229
230 BOOST_CHECK_EQUAL(string("throw me around"),ret);
231 }
232 }
233}
234
235BOOST_AUTO_TEST_CASE(BigReturn)
236{
237 switch(child_pid=fork())
238 {
239 case -1:
240 {
241 BOOST_FAIL("fork error");
242 break;
243 }
244 case 0:
245 // child
246 {
247 try
248 {
249 socket_server ss("./socket");
250 command_server cs(ss);
251
252 // max 10 sec
253 for (int i=0; i < 10; i++)
254 cs.handle(1000000);
255 } catch(...)
256 {
257 std::cerr << "exception in child. ignoring\n";
258 }
259
260 // don't call atexit and stuff
261 _exit(0);
262 }
263
264 default:
265 // parent
266 {
267 // wait till server is up
268 sleep(1);
269 socket_client_connection sc("./socket");
270 command_client cc(&sc);
271
272 result_container rc;
273 cc.send_command(new testfunc_cmd("big"),rc);
274
275 string ret=dynamic_cast<testfunc_res*>(rc.get_result())->get_data();
276
277 BOOST_CHECK_EQUAL(string().insert(0,100*1024,'x'),ret);
278 }
279 }
280}
281
282BOOST_AUTO_TEST_CASE(BigParameter)
283{
284 switch(child_pid=fork())
285 {
286 case -1:
287 {
288 BOOST_FAIL("fork error");
289 break;
290 }
291 case 0:
292 // child
293 {
294 try
295 {
296 socket_server ss("./socket");
297 command_server cs(ss);
298
299 // max 60 sec - we need atleast 28 handle calls to transfer the buffer
300 for (int i=0; i < 60; i++) {
301 cs.handle(1000000);
302 }
303 } catch(...)
304 {
305 std::cerr << "exception in child. ignoring\n";
306 }
307
308 // don't call atexit and stuff
309 _exit(0);
310 }
311
312 default:
313 // parent
314 {
315 // wait till server is up
316 sleep(1);
317 socket_client_connection sc("./socket");
318 command_client cc(&sc);
319
320 result_container rc;
321 cc.send_command(new testfunc_cmd(string().insert(0,100*1024,'y')),rc);
322
323 string ret=dynamic_cast<testfunc_res*>(rc.get_result())->get_data();
324
325 BOOST_CHECK_EQUAL(string().insert(0,100*1024,'y')+", testfunc() was here",ret);
326 }
327 }
328}
329
330BOOST_AUTO_TEST_SUITE_END()