Fix 'occurred' typo
[libt2n] / test / serialize.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#include "test_fixtures.hxx"
34
35#define BOOST_TEST_DYN_LINK
36#include <boost/test/unit_test.hpp>
37
38#include <boost/archive/binary_oarchive.hpp>
39#include <boost/archive/binary_iarchive.hpp>
40#include <boost/archive/xml_oarchive.hpp>
41#include <boost/archive/xml_iarchive.hpp>
42#include <boost/serialization/serialization.hpp>
43
44#include <container.hxx>
45#include <socket_client.hxx>
46#include <socket_server.hxx>
47#include <command_client.hxx>
48#include <command_server.hxx>
49
50using namespace std;
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
122BOOST_FIXTURE_TEST_SUITE(test_serialize, KillChildOnShutdownFixture)
123// TODO: Server Deserialization Error
124// TODO: Server Serialization Error
125// TODO: Client Deserialization Error
126// but those probably need separate client/server binaries
127
128BOOST_AUTO_TEST_CASE(ClientSerializeErr)
129{
130 switch(child_pid=fork())
131 {
132 case -1:
133 {
134 BOOST_FAIL("fork error");
135 break;
136 }
137 case 0:
138 // child
139 {
140 try
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 } catch(...)
149 {
150 std::cerr << "exception in child. ignoring\n";
151 }
152
153 // don't call atexit and stuff
154 _exit(0);
155 }
156
157 default:
158 // parent
159 {
160 // wait till server is up
161 sleep(1);
162 socket_client_connection sc("./socket");
163 command_client cc(&sc);
164
165 string errormsg;
166
167 result_container rc;
168 try
169 {
170 cc.send_command(new testfunc3_cmd("xyz"),rc);
171 }
172 catch(t2n_serialization_error &e)
173 { errormsg=e.what(); }
174
175 BOOST_CHECK_EQUAL(string("archive_exception while serializing on client-side, code 2 (unregistered class - derived class not registered or exported)"),errormsg);
176 }
177 }
178}
179
180BOOST_AUTO_TEST_SUITE_END()