installation and .pc file generation
[libt2n] / test / simplecmd.cpp
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 <boost/archive/binary_oarchive.hpp>
23 #include <boost/archive/binary_iarchive.hpp>
24 #include <boost/archive/xml_oarchive.hpp>
25 #include <boost/archive/xml_iarchive.hpp>
26 #include <boost/serialization/serialization.hpp>
27
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
34 using namespace std;
35 using namespace CppUnit;
36
37 string testfunc(const string& str)
38 {
39     string ret;
40     if (str=="throw")
41         throw libt2n::t2n_runtime_error("throw me around");
42     if (str=="big")
43         ret.insert(0,100*1024,'x');
44     else
45         ret=str+", testfunc() was here";
46     return ret;
47 }
48
49 class testfunc_res : public libt2n::result
50 {
51     private:
52         string res;
53
54         friend class boost::serialization::access;
55         template<class Archive>
56         void serialize(Archive & ar, const unsigned int version)
57         {
58             ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(libt2n::result);
59             ar & BOOST_SERIALIZATION_NVP(res);
60         }
61
62     public:
63         testfunc_res()
64             { }
65
66         testfunc_res(const string& str)
67         {
68             res=str;
69         }
70
71         string get_data()
72         {
73             return res;
74         }
75 };
76
77
78 class testfunc_cmd : public libt2n::command
79 {
80     private:
81         string param;
82
83         friend class boost::serialization::access;
84         template<class Archive>
85         void serialize(Archive & ar, const unsigned int version)
86         {
87             ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(libt2n::command);
88             ar & BOOST_SERIALIZATION_NVP(param);
89         }
90
91     public:
92         testfunc_cmd()
93             { }
94
95         testfunc_cmd(const string& str)
96         {
97             param=str;
98         }
99
100         libt2n::result* operator()()
101         {
102             return new testfunc_res(testfunc(param));
103         }
104 };
105
106 #include <boost/serialization/export.hpp>
107
108 BOOST_CLASS_EXPORT(testfunc_cmd)
109 BOOST_CLASS_EXPORT(testfunc_res)
110
111 using namespace libt2n;
112
113 class test_simplecmd : public TestFixture
114 {
115     CPPUNIT_TEST_SUITE(test_simplecmd);
116
117     CPPUNIT_TEST(SimpleCmd);
118     CPPUNIT_TEST(SimpleException);
119     CPPUNIT_TEST(BigReturn);
120     CPPUNIT_TEST(BigParameter);
121
122     CPPUNIT_TEST_SUITE_END();
123
124     public:
125
126     void setUp()
127     {
128 }
129
130     void tearDown()
131     { }
132
133     void SimpleCmd()
134     {
135         pid_t pid;
136
137         switch(pid=fork())
138         {
139             case -1:
140             {
141                 CPPUNIT_FAIL("fork error");
142                 break;
143             }
144             case 0:
145             // child
146             {
147                 socket_server ss("./socket");
148                 command_server cs(ss);
149
150                 // max 10 sec
151                 for (int i=0; i < 10; i++)
152                     cs.handle(1000000);
153
154                 // don't call atexit and stuff
155                 _exit(0);
156             }
157
158             default:
159             // parent
160             {
161                 // wait till server is up
162                 sleep(1);
163                 socket_client_connection sc("./socket");
164                 sc.set_logging(&cerr,debug);
165                 command_client cc(sc);
166
167                 result_container rc;
168                 cc.send_command(new testfunc_cmd("hello"),rc);
169
170                 string ret=dynamic_cast<testfunc_res*>(rc.get_result())->get_data();
171
172                 CPPUNIT_ASSERT_EQUAL(string("hello, testfunc() was here"),ret);
173             }
174         }
175     }
176
177     void SimpleException()
178     {
179         pid_t pid;
180
181         switch(pid=fork())
182         {
183             case -1:
184             {
185                 CPPUNIT_FAIL("fork error");
186                 break;
187             }
188             case 0:
189             // child
190             {
191                 socket_server ss("./socket");
192                 command_server cs(ss);
193
194                 // max 10 sec
195                 for (int i=0; i < 10; i++)
196                     cs.handle(1000000);
197
198                 // don't call atexit and stuff
199                 _exit(0);
200             }
201
202             default:
203             // parent
204             {
205                 // wait till server is up
206                 sleep(1);
207                 socket_client_connection sc("./socket");
208                 sc.set_logging(&cerr,debug);
209                 command_client cc(sc);
210
211                 result_container rc;
212                 cc.send_command(new testfunc_cmd("throw"),rc);
213
214                 string ret;
215
216                 try
217                 {
218                     ret=dynamic_cast<testfunc_res*>(rc.get_result())->get_data();
219                 }
220                 catch(t2n_runtime_error &e)
221                     { ret=e.what(); }
222                 catch(...)
223                     { throw; }
224
225                 CPPUNIT_ASSERT_EQUAL(string("throw me around"),ret);
226             }
227         }
228     }
229
230     void BigReturn()
231     {
232         pid_t pid;
233
234         switch(pid=fork())
235         {
236             case -1:
237             {
238                 CPPUNIT_FAIL("fork error");
239                 break;
240             }
241             case 0:
242             // child
243             {
244                 socket_server ss("./socket");
245                 command_server cs(ss);
246
247                 // max 10 sec
248                 for (int i=0; i < 10; i++)
249                     cs.handle(1000000);
250
251                 // don't call atexit and stuff
252                 _exit(0);
253             }
254
255             default:
256             // parent
257             {
258                 // wait till server is up
259                 sleep(1);
260                 socket_client_connection sc("./socket");
261                 command_client cc(sc);
262
263                 result_container rc;
264                 cc.send_command(new testfunc_cmd("big"),rc);
265
266                 string ret=dynamic_cast<testfunc_res*>(rc.get_result())->get_data();
267
268                 CPPUNIT_ASSERT_EQUAL(string().insert(0,100*1024,'x'),ret);
269             }
270         }
271     }
272
273     void BigParameter()
274     {
275         pid_t pid;
276
277         switch(pid=fork())
278         {
279             case -1:
280             {
281                 CPPUNIT_FAIL("fork error");
282                 break;
283             }
284             case 0:
285             // child
286             {
287                 socket_server ss("./socket");
288                 command_server cs(ss);
289
290                 // max 10 sec
291                 for (int i=0; i < 10; i++)
292                     cs.handle(1000000);
293
294                 // don't call atexit and stuff
295                 _exit(0);
296             }
297
298             default:
299             // parent
300             {
301                 // wait till server is up
302                 sleep(1);
303                 socket_client_connection sc("./socket");
304                 command_client cc(sc);
305
306                 result_container rc;
307                 cc.send_command(new testfunc_cmd(string().insert(0,100*1024,'y')),rc);
308
309                 string ret=dynamic_cast<testfunc_res*>(rc.get_result())->get_data();
310
311                 CPPUNIT_ASSERT_EQUAL(string().insert(0,100*1024,'y')+", testfunc() was here",ret);
312             }
313         }
314     }
315
316 };
317
318 CPPUNIT_TEST_SUITE_REGISTRATION(test_simplecmd);