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;
53 BOOST_FIXTURE_TEST_SUITE(test_hello, KillChildOnShutdownFixture)
55 BOOST_AUTO_TEST_CASE(HelloOk)
57 switch(child_pid=fork())
61 BOOST_FAIL("fork error");
69 socket_server ss("./socket");
72 hello << "T2Nv" << PROTOCOL_VERSION << ';';
74 hello.write((char*)&byteordercheck,sizeof(byteordercheck));
77 ss.add_callback(new_connection,bind(&test_hello::HelloOk::send_hello, boost::ref(*this), hello.str(),&ss, _1));
80 for (int i=0; i < 10; i++)
81 ss.fill_buffer(1000000);
84 std::cerr << "exception in child. ignoring\n";
87 // don't call atexit and stuff
96 // wait till server is up
98 socket_client_connection sc("./socket");
99 command_client cc(&sc);
101 // All fine we reached this point
107 BOOST_AUTO_TEST_CASE(BadTag)
109 switch(child_pid=fork())
113 BOOST_FAIL("fork error");
121 socket_server ss("./socket");
126 ss.add_callback(new_connection,bind(&test_hello::BadTag::send_hello, boost::ref(*this), hello.str(),&ss, _1));
129 for (int i=0; i < 10; i++)
130 ss.fill_buffer(1000000);
133 std::cerr << "exception in child. ignoring\n";
136 // don't call atexit and stuff
145 // wait till server is up
147 socket_client_connection sc("./socket");
149 command_client cc(&sc);
151 t2n_exception* ep=cc.get_constuctor_exception();
157 BOOST_CHECK_EQUAL(string("illegal hello received (T2N)"),errormsg);
162 BOOST_AUTO_TEST_CASE(BadVersion)
164 switch(child_pid=fork())
168 BOOST_FAIL("fork error");
176 socket_server ss("./socket");
179 // lets hope we don't ever get near such a version number...
180 hello << "T2Nv" << 4982271 << ';';
181 int byteordercheck=1;
182 hello.write((char*)&byteordercheck,sizeof(byteordercheck));
185 ss.add_callback(new_connection,bind(&test_hello::BadVersion::send_hello, boost::ref(*this), hello.str(),&ss, _1));
188 for (int i=0; i < 10; i++)
189 ss.fill_buffer(1000000);
192 std::cerr << "exception in child. ignoring\n";
195 // don't call atexit and stuff
204 // wait till server is up
206 socket_client_connection sc("./socket");
208 command_client cc(&sc);
210 t2n_exception* ep=cc.get_constuctor_exception();
216 BOOST_CHECK_EQUAL(string("not compatible with the server protocol version"),errormsg);
221 BOOST_AUTO_TEST_CASE(SeparatorMissing)
223 switch(child_pid=fork())
227 BOOST_FAIL("fork error");
235 socket_server ss("./socket");
238 hello << "T2Nv" << PROTOCOL_VERSION;
239 int byteordercheck=1;
240 hello.write((char*)&byteordercheck,sizeof(byteordercheck));
243 ss.add_callback(new_connection,bind(&test_hello::SeparatorMissing::send_hello, boost::ref(*this), hello.str(),&ss, _1));
246 for (int i=0; i < 10; i++)
247 ss.fill_buffer(1000000);
250 std::cerr << "exception in child. ignoring\n";
253 // don't call atexit and stuff
262 // wait till server is up
264 socket_client_connection sc("./socket");
266 command_client cc(&sc);
268 t2n_exception* ep=cc.get_constuctor_exception();
274 BOOST_CHECK_EQUAL(string("illegal hello received (1. ;)"),errormsg);
279 BOOST_AUTO_TEST_CASE(WrongByteOrder)
281 switch(child_pid=fork())
285 BOOST_FAIL("fork error");
293 socket_server ss("./socket");
296 hello << "T2Nv" << PROTOCOL_VERSION << ';';
297 int byteordercheck=1;
299 char* si=(char*)&byteordercheck;
300 char* di=(char*)&dst;
307 hello.write((char*)&dst,sizeof(dst));
310 ss.add_callback(new_connection,bind(&test_hello::WrongByteOrder::send_hello, boost::ref(*this), hello.str(),&ss, _1));
313 for (int i=0; i < 10; i++)
314 ss.fill_buffer(1000000);
317 std::cerr << "exception in child. ignoring\n";
320 // don't call atexit and stuff
329 // wait till server is up
331 socket_client_connection sc("./socket");
333 command_client cc(&sc);
335 t2n_exception* ep=cc.get_constuctor_exception();
341 BOOST_CHECK_EQUAL(string("host byte order not matching"),errormsg);
346 BOOST_AUTO_TEST_CASE(OtherServerBig)
348 switch(child_pid=fork())
352 BOOST_FAIL("fork error");
360 socket_server ss("./socket");
363 // hmm, we got the wrong socket
364 hello << "* OK intradev.net.lan Cyrus IMAP4 v2.2.13 server ready";
366 ss.add_callback(new_connection,bind(&test_hello::OtherServerBig::send_raw_socket, boost::ref(*this), hello.str(),&ss, _1));
369 for (int i=0; i < 3; i++)
370 ss.fill_buffer(1000000);
373 std::cerr << "exception in child. ignoring\n";
376 // don't call atexit and stuff
385 // wait till server is up
387 socket_client_connection sc("./socket");
389 command_client cc(&sc);
391 t2n_exception* ep=cc.get_constuctor_exception();
397 BOOST_CHECK_EQUAL(string("illegal hello received (T2N)"),errormsg);
402 BOOST_AUTO_TEST_CASE(OtherServerSmall)
404 switch(child_pid=fork())
408 BOOST_FAIL("fork error");
416 socket_server ss("./socket");
419 // hmm, we got the wrong socket
422 ss.add_callback(new_connection,bind(&test_hello::OtherServerSmall::send_raw_socket, boost::ref(*this), hello.str(),&ss, _1));
425 for (int i=0; i < 3; i++)
426 ss.fill_buffer(1000000);
429 std::cerr << "exception in child. ignoring\n";
432 // don't call atexit and stuff
441 // wait till server is up
443 socket_client_connection sc("./socket");
445 command_client cc(&sc);
447 t2n_exception* ep=cc.get_constuctor_exception();
453 BOOST_CHECK_EQUAL(string("illegal hello received (T2N)"),errormsg);
458 BOOST_AUTO_TEST_SUITE_END()