Commit | Line | Data |
---|---|---|
19facd85 TJ |
1 | /* |
2 | Copyright (C) 2004 by Intra2net AG | |
d184c648 | 3 | |
19facd85 TJ |
4 | The software in this package is distributed under the GNU General |
5 | Public License version 2 (with a special exception described below). | |
6 | ||
7 | A copy of GNU General Public License (GPL) is included in this distribution, | |
8 | in the file COPYING.GPL. | |
9 | ||
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. | |
15 | ||
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. | |
18 | ||
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. | |
21 | */ | |
d184c648 GE |
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 | ||
307b5e74 TJ |
33 | #define BOOST_TEST_DYN_LINK |
34 | #include <boost/test/unit_test.hpp> | |
d184c648 | 35 | |
a7170401 GE |
36 | #include <boost/archive/binary_oarchive.hpp> |
37 | #include <boost/archive/binary_iarchive.hpp> | |
d535333f GE |
38 | #include <boost/archive/xml_oarchive.hpp> |
39 | #include <boost/archive/xml_iarchive.hpp> | |
a7170401 GE |
40 | #include <boost/serialization/serialization.hpp> |
41 | ||
d184c648 GE |
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 | ||
307b5e74 TJ |
48 | #include "test_fixtures.hxx" |
49 | ||
d184c648 | 50 | using namespace std; |
d184c648 GE |
51 | |
52 | string testfunc(const string& str) | |
53 | { | |
58b327c6 | 54 | string ret; |
e453407d GE |
55 | if (str=="throw") |
56 | throw libt2n::t2n_runtime_error("throw me around"); | |
58b327c6 GE |
57 | if (str=="big") |
58 | ret.insert(0,100*1024,'x'); | |
59 | else | |
60 | ret=str+", testfunc() was here"; | |
d184c648 GE |
61 | return ret; |
62 | } | |
63 | ||
d535333f | 64 | class testfunc_res : public libt2n::result |
d184c648 GE |
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 | { | |
d535333f | 73 | ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(libt2n::result); |
d184c648 GE |
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 | ||
a7170401 | 92 | |
d184c648 GE |
93 | class 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 | { | |
d184c648 GE |
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 | ||
d535333f | 115 | libt2n::result* operator()() |
d184c648 GE |
116 | { |
117 | return new testfunc_res(testfunc(param)); | |
118 | } | |
119 | }; | |
120 | ||
a7170401 | 121 | #include <boost/serialization/export.hpp> |
d184c648 GE |
122 | |
123 | BOOST_CLASS_EXPORT(testfunc_cmd) | |
124 | BOOST_CLASS_EXPORT(testfunc_res) | |
125 | ||
d535333f GE |
126 | using namespace libt2n; |
127 | ||
307b5e74 TJ |
128 | BOOST_FIXTURE_TEST_SUITE(test_simplecmd, KillChildOnShutdownFixture) |
129 | ||
130 | BOOST_AUTO_TEST_CASE(SimpleCmd) | |
d184c648 | 131 | { |
307b5e74 TJ |
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); | |
d184c648 | 146 | |
307b5e74 TJ |
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 | } | |
d184c648 | 154 | |
307b5e74 TJ |
155 | // don't call atexit and stuff |
156 | _exit(0); | |
157 | } | |
d184c648 | 158 | |
307b5e74 TJ |
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); | |
b5922184 | 167 | |
307b5e74 TJ |
168 | result_container rc; |
169 | cc.send_command(new testfunc_cmd("hello"),rc); | |
d184c648 | 170 | |
307b5e74 | 171 | string ret=dynamic_cast<testfunc_res*>(rc.get_result())->get_data(); |
d184c648 | 172 | |
307b5e74 TJ |
173 | BOOST_CHECK_EQUAL(string("hello, testfunc() was here"),ret); |
174 | } | |
b5922184 | 175 | } |
307b5e74 | 176 | } |
d184c648 | 177 | |
307b5e74 TJ |
178 | BOOST_AUTO_TEST_CASE(SimpleException) |
179 | { | |
180 | switch(child_pid=fork()) | |
d184c648 | 181 | { |
307b5e74 TJ |
182 | case -1: |
183 | { | |
184 | BOOST_FAIL("fork error"); | |
185 | break; | |
186 | } | |
187 | case 0: | |
188 | // child | |
d184c648 | 189 | { |
307b5e74 | 190 | try |
d184c648 | 191 | { |
307b5e74 TJ |
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(...) | |
d184c648 | 199 | { |
307b5e74 | 200 | std::cerr << "exception in child. ignoring\n"; |
d184c648 GE |
201 | } |
202 | ||
307b5e74 TJ |
203 | // don't call atexit and stuff |
204 | _exit(0); | |
205 | } | |
d184c648 | 206 | |
307b5e74 TJ |
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); | |
d184c648 | 215 | |
307b5e74 TJ |
216 | result_container rc; |
217 | cc.send_command(new testfunc_cmd("throw"),rc); | |
d184c648 | 218 | |
307b5e74 | 219 | string ret; |
d184c648 | 220 | |
307b5e74 | 221 | try |
e453407d | 222 | { |
307b5e74 | 223 | ret=dynamic_cast<testfunc_res*>(rc.get_result())->get_data(); |
e453407d | 224 | } |
307b5e74 TJ |
225 | catch(t2n_runtime_error &e) |
226 | { ret=e.what(); } | |
e453407d | 227 | |
307b5e74 | 228 | BOOST_CHECK_EQUAL(string("throw me around"),ret); |
e453407d GE |
229 | } |
230 | } | |
307b5e74 | 231 | } |
d184c648 | 232 | |
307b5e74 TJ |
233 | BOOST_AUTO_TEST_CASE(BigReturn) |
234 | { | |
235 | switch(child_pid=fork()) | |
58b327c6 | 236 | { |
307b5e74 TJ |
237 | case -1: |
238 | { | |
239 | BOOST_FAIL("fork error"); | |
240 | break; | |
241 | } | |
242 | case 0: | |
243 | // child | |
58b327c6 | 244 | { |
307b5e74 | 245 | try |
58b327c6 | 246 | { |
307b5e74 TJ |
247 | socket_server ss("./socket"); |
248 | command_server cs(ss); | |
249 | ||
250 | // max 10 sec | |
251 | for (int i=0; i < 10; i++) | |
252 | cs.handle(1000000); | |
253 | } catch(...) | |
58b327c6 | 254 | { |
307b5e74 | 255 | std::cerr << "exception in child. ignoring\n"; |
58b327c6 GE |
256 | } |
257 | ||
307b5e74 TJ |
258 | // don't call atexit and stuff |
259 | _exit(0); | |
260 | } | |
58b327c6 | 261 | |
307b5e74 TJ |
262 | default: |
263 | // parent | |
264 | { | |
265 | // wait till server is up | |
266 | sleep(1); | |
267 | socket_client_connection sc("./socket"); | |
268 | command_client cc(&sc); | |
58b327c6 | 269 | |
307b5e74 TJ |
270 | result_container rc; |
271 | cc.send_command(new testfunc_cmd("big"),rc); | |
58b327c6 | 272 | |
307b5e74 TJ |
273 | string ret=dynamic_cast<testfunc_res*>(rc.get_result())->get_data(); |
274 | ||
275 | BOOST_CHECK_EQUAL(string().insert(0,100*1024,'x'),ret); | |
58b327c6 GE |
276 | } |
277 | } | |
307b5e74 | 278 | } |
58b327c6 | 279 | |
307b5e74 TJ |
280 | BOOST_AUTO_TEST_CASE(BigParameter) |
281 | { | |
282 | switch(child_pid=fork()) | |
58b327c6 | 283 | { |
307b5e74 | 284 | case -1: |
58b327c6 | 285 | { |
307b5e74 TJ |
286 | BOOST_FAIL("fork error"); |
287 | break; | |
288 | } | |
289 | case 0: | |
290 | // child | |
291 | { | |
292 | try | |
58b327c6 | 293 | { |
307b5e74 TJ |
294 | socket_server ss("./socket"); |
295 | command_server cs(ss); | |
296 | ||
297 | // max 60 sec - we need atleast 28 handle calls to transfer the buffer | |
298 | for (int i=0; i < 60; i++) { | |
299 | cs.handle(1000000); | |
58b327c6 | 300 | } |
307b5e74 | 301 | } catch(...) |
58b327c6 | 302 | { |
307b5e74 | 303 | std::cerr << "exception in child. ignoring\n"; |
58b327c6 GE |
304 | } |
305 | ||
307b5e74 TJ |
306 | // don't call atexit and stuff |
307 | _exit(0); | |
308 | } | |
309 | ||
310 | default: | |
311 | // parent | |
312 | { | |
313 | // wait till server is up | |
314 | sleep(1); | |
315 | socket_client_connection sc("./socket"); | |
316 | command_client cc(&sc); | |
58b327c6 | 317 | |
307b5e74 TJ |
318 | result_container rc; |
319 | cc.send_command(new testfunc_cmd(string().insert(0,100*1024,'y')),rc); | |
58b327c6 | 320 | |
307b5e74 | 321 | string ret=dynamic_cast<testfunc_res*>(rc.get_result())->get_data(); |
58b327c6 | 322 | |
307b5e74 | 323 | BOOST_CHECK_EQUAL(string().insert(0,100*1024,'y')+", testfunc() was here",ret); |
58b327c6 GE |
324 | } |
325 | } | |
307b5e74 | 326 | } |
58b327c6 | 327 | |
307b5e74 | 328 | BOOST_AUTO_TEST_SUITE_END() |