2 Copyright (C) 2004 by Intra2net AG
4 The software in this package is distributed under the GNU General
5 Public License version 2 (with a special exception described below).
7 A copy of GNU General Public License (GPL) is included in this distribution,
8 in the file COPYING.GPL.
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.
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.
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.
22 #include <sys/types.h>
33 #include <boost/bind.hpp>
35 #define BOOST_TEST_DYN_LINK
36 #include <boost/test/unit_test.hpp>
38 #include <t2n_exception.hxx>
39 #include <socket_client.hxx>
40 #include <socket_server.hxx>
41 #include <command_client.hxx>
43 #include "test_fixtures.hxx"
48 using namespace libt2n;
51 BOOST_FIXTURE_TEST_SUITE(test_hello, KillChildOnShutdownFixture)
53 BOOST_AUTO_TEST_CASE(HelloOk)
55 switch(child_pid=fork())
59 BOOST_FAIL("fork error");
67 socket_server ss("./socket");
70 hello << "T2Nv" << PROTOCOL_VERSION << ';';
72 hello.write((char*)&byteordercheck,sizeof(byteordercheck));
75 ss.add_callback(new_connection,bind(&test_hello::HelloOk::send_hello, boost::ref(*this), hello.str(),&ss, _1));
78 for (int i=0; i < 10; i++)
79 ss.fill_buffer(1000000);
82 std::cerr << "exception in child. ignoring\n";
85 // don't call atexit and stuff
94 // wait till server is up
96 socket_client_connection sc("./socket");
97 command_client cc(&sc);
99 // All fine we reached this point
105 BOOST_AUTO_TEST_CASE(BadTag)
107 switch(child_pid=fork())
111 BOOST_FAIL("fork error");
119 socket_server ss("./socket");
124 ss.add_callback(new_connection,bind(&test_hello::BadTag::send_hello, boost::ref(*this), hello.str(),&ss, _1));
127 for (int i=0; i < 10; i++)
128 ss.fill_buffer(1000000);
131 std::cerr << "exception in child. ignoring\n";
134 // don't call atexit and stuff
143 // wait till server is up
145 socket_client_connection sc("./socket");
147 command_client cc(&sc);
149 t2n_exception* ep=cc.get_constuctor_exception();
155 BOOST_CHECK_EQUAL(string("illegal hello received (T2N)"),errormsg);
160 BOOST_AUTO_TEST_CASE(BadVersion)
162 switch(child_pid=fork())
166 BOOST_FAIL("fork error");
174 socket_server ss("./socket");
177 // lets hope we don't ever get near such a version number...
178 hello << "T2Nv" << 4982271 << ';';
179 int byteordercheck=1;
180 hello.write((char*)&byteordercheck,sizeof(byteordercheck));
183 ss.add_callback(new_connection,bind(&test_hello::BadVersion::send_hello, boost::ref(*this), hello.str(),&ss, _1));
186 for (int i=0; i < 10; i++)
187 ss.fill_buffer(1000000);
190 std::cerr << "exception in child. ignoring\n";
193 // don't call atexit and stuff
202 // wait till server is up
204 socket_client_connection sc("./socket");
206 command_client cc(&sc);
208 t2n_exception* ep=cc.get_constuctor_exception();
214 BOOST_CHECK_EQUAL(string("not compatible with the server protocol version"),errormsg);
219 BOOST_AUTO_TEST_CASE(SeparatorMissing)
221 switch(child_pid=fork())
225 BOOST_FAIL("fork error");
233 socket_server ss("./socket");
236 hello << "T2Nv" << PROTOCOL_VERSION;
237 int byteordercheck=1;
238 hello.write((char*)&byteordercheck,sizeof(byteordercheck));
241 ss.add_callback(new_connection,bind(&test_hello::SeparatorMissing::send_hello, boost::ref(*this), hello.str(),&ss, _1));
244 for (int i=0; i < 10; i++)
245 ss.fill_buffer(1000000);
248 std::cerr << "exception in child. ignoring\n";
251 // don't call atexit and stuff
260 // wait till server is up
262 socket_client_connection sc("./socket");
264 command_client cc(&sc);
266 t2n_exception* ep=cc.get_constuctor_exception();
272 BOOST_CHECK_EQUAL(string("illegal hello received (1. ;)"),errormsg);
277 BOOST_AUTO_TEST_CASE(WrongByteOrder)
279 switch(child_pid=fork())
283 BOOST_FAIL("fork error");
291 socket_server ss("./socket");
294 hello << "T2Nv" << PROTOCOL_VERSION << ';';
295 int byteordercheck=1;
297 char* si=(char*)&byteordercheck;
298 char* di=(char*)&dst;
305 hello.write((char*)&dst,sizeof(dst));
308 ss.add_callback(new_connection,bind(&test_hello::WrongByteOrder::send_hello, boost::ref(*this), hello.str(),&ss, _1));
311 for (int i=0; i < 10; i++)
312 ss.fill_buffer(1000000);
315 std::cerr << "exception in child. ignoring\n";
318 // don't call atexit and stuff
327 // wait till server is up
329 socket_client_connection sc("./socket");
331 command_client cc(&sc);
333 t2n_exception* ep=cc.get_constuctor_exception();
339 BOOST_CHECK_EQUAL(string("host byte order not matching"),errormsg);
344 BOOST_AUTO_TEST_CASE(OtherServerBig)
346 switch(child_pid=fork())
350 BOOST_FAIL("fork error");
358 socket_server ss("./socket");
361 // hmm, we got the wrong socket
362 hello << "* OK intradev.net.lan Cyrus IMAP4 v2.2.13 server ready";
364 ss.add_callback(new_connection,bind(&test_hello::OtherServerBig::send_raw_socket, boost::ref(*this), hello.str(),&ss, _1));
367 for (int i=0; i < 3; i++)
368 ss.fill_buffer(1000000);
371 std::cerr << "exception in child. ignoring\n";
374 // don't call atexit and stuff
383 // wait till server is up
385 socket_client_connection sc("./socket");
387 command_client cc(&sc);
389 t2n_exception* ep=cc.get_constuctor_exception();
395 BOOST_CHECK_EQUAL(string("illegal hello received (T2N)"),errormsg);
400 BOOST_AUTO_TEST_CASE(OtherServerSmall)
402 switch(child_pid=fork())
406 BOOST_FAIL("fork error");
414 socket_server ss("./socket");
417 // hmm, we got the wrong socket
420 ss.add_callback(new_connection,bind(&test_hello::OtherServerSmall::send_raw_socket, boost::ref(*this), hello.str(),&ss, _1));
423 for (int i=0; i < 3; i++)
424 ss.fill_buffer(1000000);
427 std::cerr << "exception in child. ignoring\n";
430 // don't call atexit and stuff
439 // wait till server is up
441 socket_client_connection sc("./socket");
443 command_client cc(&sc);
445 t2n_exception* ep=cc.get_constuctor_exception();
451 BOOST_CHECK_EQUAL(string("illegal hello received (T2N)"),errormsg);
456 BOOST_AUTO_TEST_SUITE_END()