b2b5bbdca2f8f1c0883d469150aa508a42c62eb7
[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     if (str=="throw")
40         throw libt2n::t2n_runtime_error("throw me around");
41     string ret(str);
42     ret+=", testfunc() was here";
43     return ret;
44 }
45
46 class testfunc_res : public libt2n::result
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         {
55             ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(libt2n::result);
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
74
75 class 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         {
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
97         libt2n::result* operator()()
98         {
99             return new testfunc_res(testfunc(param));
100         }
101 };
102
103 #include <boost/serialization/export.hpp>
104
105 BOOST_CLASS_EXPORT(testfunc_cmd)
106 BOOST_CLASS_EXPORT(testfunc_res)
107
108 using namespace libt2n;
109
110 class test_simplecmd : public TestFixture
111 {
112     CPPUNIT_TEST_SUITE(test_simplecmd);
113
114     CPPUNIT_TEST(SimpleCmd);
115     CPPUNIT_TEST(SimpleException);
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");
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");
159                 sc.set_logging(&cerr,debug);
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
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     }
224
225 };
226
227 CPPUNIT_TEST_SUITE_REGISTRATION(test_simplecmd);