Change license from LGPL to GPL version 2 + linking exception. This fixes C++ templat...
[libt2n] / test / serialize.cpp
CommitLineData
19facd85
TJ
1/*
2Copyright (C) 2004 by Intra2net AG
01a46463 3
19facd85
TJ
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*/
01a46463
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
33#include <cppunit/extensions/TestFactoryRegistry.h>
34#include <cppunit/ui/text/TestRunner.h>
35#include <cppunit/extensions/HelperMacros.h>
36
37#include <boost/archive/binary_oarchive.hpp>
38#include <boost/archive/binary_iarchive.hpp>
39#include <boost/archive/xml_oarchive.hpp>
40#include <boost/archive/xml_iarchive.hpp>
41#include <boost/serialization/serialization.hpp>
42
43#include <container.hxx>
44#include <socket_client.hxx>
45#include <socket_server.hxx>
46#include <command_client.hxx>
47#include <command_server.hxx>
48
49using namespace std;
50using namespace CppUnit;
51
52string testfunc3(const string& str)
53{
54 if (str=="throw")
55 throw libt2n::t2n_runtime_error("throw me around");
56 string ret(str);
57 ret+=", testfunc() was here";
58 return ret;
59}
60
61class testfunc3_res : public libt2n::result
62{
63 private:
64 string res;
65
66 friend class boost::serialization::access;
67 template<class Archive>
68 void serialize(Archive & ar, const unsigned int version)
69 {
70 ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(libt2n::result);
71 ar & BOOST_SERIALIZATION_NVP(res);
72 }
73
74 public:
75 testfunc3_res()
76 { }
77
78 testfunc3_res(const string& str)
79 {
80 res=str;
81 }
82
83 string get_data()
84 {
85 return res;
86 }
87};
88
89
90class testfunc3_cmd : public libt2n::command
91{
92 private:
93 string param;
94
95 friend class boost::serialization::access;
96 template<class Archive>
97 void serialize(Archive & ar, const unsigned int version)
98 {
99 ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(libt2n::command);
100 ar & BOOST_SERIALIZATION_NVP(param);
101 }
102
103 public:
104 testfunc3_cmd()
105 { }
106
107 testfunc3_cmd(const string& str)
108 {
109 param=str;
110 }
111
112 libt2n::result* operator()()
113 {
114 return new testfunc3_res(testfunc3(param));
115 }
116};
117
118#include <boost/serialization/export.hpp>
119
120using namespace libt2n;
121
122class test_serialize : public TestFixture
123{
124 CPPUNIT_TEST_SUITE(test_serialize);
125
126 CPPUNIT_TEST(ClientSerializeErr);
127
128 // TODO: Server Deserialization Error
129 // TODO: Server Serialization Error
130 // TODO: Client Deserialization Error
131 // but those probably need separate client/server binaries
132
133 CPPUNIT_TEST_SUITE_END();
134
b5922184
GE
135 pid_t child_pid;
136
01a46463
GE
137 public:
138
139 void setUp()
56f3994d
TJ
140 {
141 }
01a46463
GE
142
143 void tearDown()
b5922184
GE
144 {
145 // make sure the server-child is dead before the next test runs
146 kill(child_pid,SIGKILL);
147 sleep(1);
148 }
01a46463
GE
149
150 void ClientSerializeErr()
151 {
b5922184 152 switch(child_pid=fork())
01a46463
GE
153 {
154 case -1:
155 {
156 CPPUNIT_FAIL("fork error");
157 break;
158 }
159 case 0:
160 // child
161 {
441d41fe
TJ
162 try
163 {
164 socket_server ss("./socket");
165 command_server cs(ss);
01a46463 166
441d41fe
TJ
167 // max 10 sec
168 for (int i=0; i < 10; i++)
169 cs.handle(1000000);
170 } catch(...)
171 {
172 std::cerr << "exception in child. ignoring\n";
173 }
01a46463
GE
174
175 // don't call atexit and stuff
176 _exit(0);
177 }
178
179 default:
180 // parent
181 {
182 // wait till server is up
183 sleep(1);
184 socket_client_connection sc("./socket");
fb3345ad 185 command_client cc(&sc);
01a46463
GE
186
187 string errormsg;
188
189 result_container rc;
190 try
191 {
192 cc.send_command(new testfunc3_cmd("xyz"),rc);
193 }
194 catch(t2n_serialization_error &e)
195 { errormsg=e.what(); }
196 catch(...)
197 { throw; }
198
199 CPPUNIT_ASSERT_EQUAL(string("archive_exception while serializing on client-side, code 2 (unregistered class)"),errormsg);
200 }
201 }
202 }
203
204};
205
206CPPUNIT_TEST_SUITE_REGISTRATION(test_serialize);