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"
50 using namespace libt2n;
52 // this is an evil hack to get access to real_write, don't ever do this in an app!!!
53 class real_write_connection: public socket_server_connection
56 void real_write(const std::string& data)
57 { socket_write(data); }
60 class test_helloFixture : public KillChildOnShutdownFixture
63 void send_hello(string hello_string, socket_server* ss, int conn_id)
65 server_connection *sc=ss->get_connection(conn_id);
66 sc->write(hello_string);
69 void send_raw_socket(string hello_string, socket_server* ss, int conn_id)
71 socket_server_connection *ssc=dynamic_cast<socket_server_connection*>(ss->get_connection(conn_id));
73 // this is an evil hack to get access to real_write, don't ever do this in an app!!!
74 real_write_connection *rwc=(real_write_connection*)ssc;
75 rwc->real_write(hello_string);
88 BOOST_FIXTURE_TEST_SUITE(test_hello, test_helloFixture)
90 BOOST_AUTO_TEST_CASE(HelloOk)
92 switch(child_pid=fork())
96 BOOST_FAIL("fork error");
104 socket_server ss("./socket");
107 hello << "T2Nv" << PROTOCOL_VERSION << ';';
108 int byteordercheck=1;
109 hello.write((char*)&byteordercheck,sizeof(byteordercheck));
112 ss.add_callback(new_connection,bind(&test_hello::HelloOk::send_hello, boost::ref(*this), hello.str(),&ss, _1));
115 for (int i=0; i < 10; i++)
116 ss.fill_buffer(1000000);
119 std::cerr << "exception in child. ignoring\n";
122 // don't call atexit and stuff
131 // wait till server is up
133 socket_client_connection sc("./socket");
134 command_client cc(&sc);
136 // All fine we reached this point
142 BOOST_AUTO_TEST_CASE(BadTag)
144 switch(child_pid=fork())
148 BOOST_FAIL("fork error");
156 socket_server ss("./socket");
161 ss.add_callback(new_connection,bind(&test_hello::BadTag::send_hello, boost::ref(*this), hello.str(),&ss, _1));
164 for (int i=0; i < 10; i++)
165 ss.fill_buffer(1000000);
168 std::cerr << "exception in child. ignoring\n";
171 // don't call atexit and stuff
180 // wait till server is up
182 socket_client_connection sc("./socket");
184 command_client cc(&sc);
186 t2n_exception* ep=cc.get_constuctor_exception();
192 BOOST_CHECK_EQUAL(string("illegal hello received (T2N)"),errormsg);
197 BOOST_AUTO_TEST_CASE(BadVersion)
199 switch(child_pid=fork())
203 BOOST_FAIL("fork error");
211 socket_server ss("./socket");
214 // lets hope we don't ever get near such a version number...
215 hello << "T2Nv" << 4982271 << ';';
216 int byteordercheck=1;
217 hello.write((char*)&byteordercheck,sizeof(byteordercheck));
220 ss.add_callback(new_connection,bind(&test_hello::BadVersion::send_hello, boost::ref(*this), hello.str(),&ss, _1));
223 for (int i=0; i < 10; i++)
224 ss.fill_buffer(1000000);
227 std::cerr << "exception in child. ignoring\n";
230 // don't call atexit and stuff
239 // wait till server is up
241 socket_client_connection sc("./socket");
243 command_client cc(&sc);
245 t2n_exception* ep=cc.get_constuctor_exception();
251 BOOST_CHECK_EQUAL(string("not compatible with the server protocol version"),errormsg);
256 BOOST_AUTO_TEST_CASE(SeparatorMissing)
258 switch(child_pid=fork())
262 BOOST_FAIL("fork error");
270 socket_server ss("./socket");
273 hello << "T2Nv" << PROTOCOL_VERSION;
274 int byteordercheck=1;
275 hello.write((char*)&byteordercheck,sizeof(byteordercheck));
278 ss.add_callback(new_connection,bind(&test_hello::SeparatorMissing::send_hello, boost::ref(*this), hello.str(),&ss, _1));
281 for (int i=0; i < 10; i++)
282 ss.fill_buffer(1000000);
285 std::cerr << "exception in child. ignoring\n";
288 // don't call atexit and stuff
297 // wait till server is up
299 socket_client_connection sc("./socket");
301 command_client cc(&sc);
303 t2n_exception* ep=cc.get_constuctor_exception();
309 BOOST_CHECK_EQUAL(string("illegal hello received (1. ;)"),errormsg);
314 BOOST_AUTO_TEST_CASE(WrongByteOrder)
316 switch(child_pid=fork())
320 BOOST_FAIL("fork error");
328 socket_server ss("./socket");
331 hello << "T2Nv" << PROTOCOL_VERSION << ';';
332 int byteordercheck=1;
334 char* si=(char*)&byteordercheck;
335 char* di=(char*)&dst;
342 hello.write((char*)&dst,sizeof(dst));
345 ss.add_callback(new_connection,bind(&test_hello::WrongByteOrder::send_hello, boost::ref(*this), hello.str(),&ss, _1));
348 for (int i=0; i < 10; i++)
349 ss.fill_buffer(1000000);
352 std::cerr << "exception in child. ignoring\n";
355 // don't call atexit and stuff
364 // wait till server is up
366 socket_client_connection sc("./socket");
368 command_client cc(&sc);
370 t2n_exception* ep=cc.get_constuctor_exception();
376 BOOST_CHECK_EQUAL(string("host byte order not matching"),errormsg);
381 BOOST_AUTO_TEST_CASE(OtherServerBig)
383 switch(child_pid=fork())
387 BOOST_FAIL("fork error");
395 socket_server ss("./socket");
398 // hmm, we got the wrong socket
399 hello << "* OK intradev.net.lan Cyrus IMAP4 v2.2.13 server ready";
401 ss.add_callback(new_connection,bind(&test_hello::OtherServerBig::send_raw_socket, boost::ref(*this), hello.str(),&ss, _1));
404 for (int i=0; i < 3; i++)
405 ss.fill_buffer(1000000);
408 std::cerr << "exception in child. ignoring\n";
411 // don't call atexit and stuff
420 // wait till server is up
422 socket_client_connection sc("./socket");
424 command_client cc(&sc);
426 t2n_exception* ep=cc.get_constuctor_exception();
432 BOOST_CHECK_EQUAL(string("illegal hello received (T2N)"),errormsg);
437 BOOST_AUTO_TEST_CASE(OtherServerSmall)
439 switch(child_pid=fork())
443 BOOST_FAIL("fork error");
451 socket_server ss("./socket");
454 // hmm, we got the wrong socket
457 ss.add_callback(new_connection,bind(&test_hello::OtherServerSmall::send_raw_socket, boost::ref(*this), hello.str(),&ss, _1));
460 for (int i=0; i < 3; i++)
461 ss.fill_buffer(1000000);
464 std::cerr << "exception in child. ignoring\n";
467 // don't call atexit and stuff
476 // wait till server is up
478 socket_client_connection sc("./socket");
480 command_client cc(&sc);
482 t2n_exception* ep=cc.get_constuctor_exception();
488 BOOST_CHECK_EQUAL(string("illegal hello received (T2N)"),errormsg);
493 BOOST_AUTO_TEST_SUITE_END()