Fix 'occurred' typo
[libt2n] / test / newserver.cpp
1 /*
2 Copyright (C) 2004 by Intra2net AG
3
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 */
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 #define BOOST_TEST_MAIN
34 #define BOOST_TEST_DYN_LINK
35 #include <boost/test/unit_test.hpp>
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
49 #include "test_fixtures.hxx"
50
51 using namespace std;
52
53 int newserver_func(int i)
54 {
55     return 1;
56 }
57
58 class newserver_res : public libt2n::result
59 {
60     private:
61         int res;
62
63         friend class boost::serialization::access;
64         template<class Archive>
65         void serialize(Archive & ar, const unsigned int version)
66         {
67             ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(libt2n::result);
68             ar & BOOST_SERIALIZATION_NVP(res);
69         }
70
71     public:
72         newserver_res()
73             { }
74
75         newserver_res(int i)
76         {
77             res=i;
78         }
79
80         int get_data()
81         {
82             return res;
83         }
84 };
85
86
87 class newserver_cmd : public libt2n::command
88 {
89     private:
90         int param;
91
92         friend class boost::serialization::access;
93         template<class Archive>
94         void serialize(Archive & ar, const unsigned int version)
95         {
96             ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(libt2n::command);
97             ar & BOOST_SERIALIZATION_NVP(param);
98         }
99
100     public:
101         newserver_cmd()
102             { }
103
104         newserver_cmd(int i)
105         {
106             param=i;
107         }
108
109         libt2n::result* operator()()
110         {
111             return new newserver_res(newserver_func(param));
112         }
113 };
114
115 #include <boost/serialization/export.hpp>
116
117 BOOST_CLASS_EXPORT(newserver_cmd)
118 BOOST_CLASS_EXPORT(newserver_res)
119
120 using namespace libt2n;
121
122 BOOST_FIXTURE_TEST_SUITE(test_newserver, KillChildOnShutdownFixture)
123
124 BOOST_AUTO_TEST_CASE(NewServerSocket)
125 {
126     switch(child_pid=fork())
127     {
128         case -1:
129         {
130             BOOST_FAIL("fork error");
131             break;
132         }
133         case 0:
134         // child
135         {
136             try
137             {
138                 {
139                     socket_server ss("./socket");
140                     // ss.set_logging(&cerr,debug);
141                     command_server cs(ss);
142
143                     // handle new connection and just one command
144                     cs.handle(10000000);
145                     cs.handle(10000000);
146                 }
147
148                 sleep(1);
149
150                 // close socket, create new one
151                 {
152                     socket_server ss("./socket");
153                     // ss.set_logging(&cerr,debug);
154                     command_server cs(ss);
155
156                     // max 30 sec
157                     for (int i=0; i < 30; i++)
158                         cs.handle(1000000);
159                 }
160             } catch(...)
161             {
162                 std::cerr << "exception in child. ignoring\n";
163             }
164
165             // don't call atexit and stuff
166             _exit(0);
167         }
168
169         default:
170         // parent
171         {
172             // don't kill us on broken pipe
173             signal(SIGPIPE, SIG_IGN);
174
175             // wait till server is up
176             sleep(1);
177             socket_client_connection sc("./socket");
178             sc.set_logging(&cerr,debug);
179             command_client cc(&sc);
180
181             result_container rc;
182             cc.send_command(new newserver_cmd(1),rc);
183
184             // very short sleep to make sure new server socket is up
185             sleep(1);
186
187             // still has connection to the old server-socket
188             string errormsg;
189
190             try
191             {
192                 sc.write("some stuff");
193             }
194             catch(t2n_transfer_error &e)
195                 { errormsg=e.what(); }
196
197             bool test_fine=false;
198             if (errormsg == "write() returned Bad file descriptor"
199                 || errormsg == "write() returned Broken pipe")
200                 test_fine = true;
201
202             if (!test_fine)
203             {
204                 std::cerr << "NewServerSocket() test failed. ignoring as the test is very fragile.\n";
205             }
206
207             BOOST_CHECK_EQUAL(1, 1);
208         }
209     }
210 }
211
212 BOOST_AUTO_TEST_SUITE_END()