1 /***************************************************************************
2 * Copyright (C) 2004 by Intra2net AG *
5 ***************************************************************************/
18 #include <boost/bind.hpp>
20 #include <cppunit/extensions/TestFactoryRegistry.h>
21 #include <cppunit/ui/text/TestRunner.h>
22 #include <cppunit/extensions/HelperMacros.h>
24 #include <t2n_exception.hxx>
25 #include <socket_client.hxx>
26 #include <socket_server.hxx>
27 #include <command_client.hxx>
34 using namespace libt2n;
35 using namespace CppUnit;
37 // this is an evil hack to get access to real_write, don't ever do this in an app!!!
38 class real_write_connection: public socket_server_connection
41 void real_write(const std::string& data)
42 { socket_write(data); }
45 class test_hello : public TestFixture
47 CPPUNIT_TEST_SUITE(test_hello);
49 CPPUNIT_TEST(HelloOk);
51 CPPUNIT_TEST(BadVersion);
52 CPPUNIT_TEST(SeparatorMissing);
53 CPPUNIT_TEST(WrongByteOrder);
54 CPPUNIT_TEST(OtherServerBig);
55 CPPUNIT_TEST(OtherServerSmall);
57 CPPUNIT_TEST_SUITE_END();
68 // make sure the server-child is dead before the next test runs
69 kill(child_pid,SIGKILL);
73 void send_hello(string hello_string, socket_server* ss, int conn_id)
75 server_connection *sc=ss->get_connection(conn_id);
76 sc->write(hello_string);
79 void send_raw_socket(string hello_string, socket_server* ss, int conn_id)
81 socket_server_connection *ssc=dynamic_cast<socket_server_connection*>(ss->get_connection(conn_id));
83 // this is an evil hack to get access to real_write, don't ever do this in an app!!!
84 real_write_connection *rwc=(real_write_connection*)ssc;
85 rwc->real_write(hello_string);
90 switch(child_pid=fork())
94 CPPUNIT_FAIL("fork error");
102 socket_server ss("./socket");
105 hello << "T2Nv" << PROTOCOL_VERSION << ';';
106 int byteordercheck=1;
107 hello.write((char*)&byteordercheck,sizeof(byteordercheck));
110 ss.add_callback(new_connection,bind(&test_hello::send_hello, boost::ref(*this), hello.str(),&ss, _1));
113 for (int i=0; i < 10; i++)
114 ss.fill_buffer(1000000);
117 std::cerr << "exception in child. ignoring\n";
120 // don't call atexit and stuff
129 // wait till server is up
131 socket_client_connection sc("./socket");
132 command_client cc(&sc);
139 switch(child_pid=fork())
143 CPPUNIT_FAIL("fork error");
151 socket_server ss("./socket");
156 ss.add_callback(new_connection,bind(&test_hello::send_hello, boost::ref(*this), hello.str(),&ss, _1));
159 for (int i=0; i < 10; i++)
160 ss.fill_buffer(1000000);
163 std::cerr << "exception in child. ignoring\n";
166 // don't call atexit and stuff
175 // wait till server is up
177 socket_client_connection sc("./socket");
179 command_client cc(&sc);
181 t2n_exception* ep=cc.get_constuctor_exception();
187 CPPUNIT_ASSERT_EQUAL(string("illegal hello received (T2N)"),errormsg);
194 switch(child_pid=fork())
198 CPPUNIT_FAIL("fork error");
206 socket_server ss("./socket");
209 // lets hope we don't ever get near such a version number...
210 hello << "T2Nv" << 4982271 << ';';
211 int byteordercheck=1;
212 hello.write((char*)&byteordercheck,sizeof(byteordercheck));
215 ss.add_callback(new_connection,bind(&test_hello::send_hello, boost::ref(*this), hello.str(),&ss, _1));
218 for (int i=0; i < 10; i++)
219 ss.fill_buffer(1000000);
222 std::cerr << "exception in child. ignoring\n";
225 // don't call atexit and stuff
234 // wait till server is up
236 socket_client_connection sc("./socket");
238 command_client cc(&sc);
240 t2n_exception* ep=cc.get_constuctor_exception();
246 CPPUNIT_ASSERT_EQUAL(string("not compatible with the server protocol version"),errormsg);
251 void SeparatorMissing()
253 switch(child_pid=fork())
257 CPPUNIT_FAIL("fork error");
265 socket_server ss("./socket");
268 hello << "T2Nv" << PROTOCOL_VERSION;
269 int byteordercheck=1;
270 hello.write((char*)&byteordercheck,sizeof(byteordercheck));
273 ss.add_callback(new_connection,bind(&test_hello::send_hello, boost::ref(*this), hello.str(),&ss, _1));
276 for (int i=0; i < 10; i++)
277 ss.fill_buffer(1000000);
280 std::cerr << "exception in child. ignoring\n";
283 // don't call atexit and stuff
292 // wait till server is up
294 socket_client_connection sc("./socket");
296 command_client cc(&sc);
298 t2n_exception* ep=cc.get_constuctor_exception();
304 CPPUNIT_ASSERT_EQUAL(string("illegal hello received (1. ;)"),errormsg);
309 void WrongByteOrder()
311 switch(child_pid=fork())
315 CPPUNIT_FAIL("fork error");
323 socket_server ss("./socket");
326 hello << "T2Nv" << PROTOCOL_VERSION << ';';
327 int byteordercheck=1;
329 char* si=(char*)&byteordercheck;
330 char* di=(char*)&dst;
337 hello.write((char*)&dst,sizeof(dst));
340 ss.add_callback(new_connection,bind(&test_hello::send_hello, boost::ref(*this), hello.str(),&ss, _1));
343 for (int i=0; i < 10; i++)
344 ss.fill_buffer(1000000);
347 std::cerr << "exception in child. ignoring\n";
350 // don't call atexit and stuff
359 // wait till server is up
361 socket_client_connection sc("./socket");
363 command_client cc(&sc);
365 t2n_exception* ep=cc.get_constuctor_exception();
371 CPPUNIT_ASSERT_EQUAL(string("host byte order not matching"),errormsg);
376 void OtherServerBig()
378 switch(child_pid=fork())
382 CPPUNIT_FAIL("fork error");
390 socket_server ss("./socket");
393 // hmm, we got the wrong socket
394 hello << "* OK intradev.net.lan Cyrus IMAP4 v2.2.13 server ready";
396 ss.add_callback(new_connection,bind(&test_hello::send_raw_socket, boost::ref(*this), hello.str(),&ss, _1));
399 for (int i=0; i < 3; i++)
400 ss.fill_buffer(1000000);
403 std::cerr << "exception in child. ignoring\n";
406 // don't call atexit and stuff
415 // wait till server is up
417 socket_client_connection sc("./socket");
419 command_client cc(&sc);
421 t2n_exception* ep=cc.get_constuctor_exception();
427 CPPUNIT_ASSERT_EQUAL(string("illegal hello received (T2N)"),errormsg);
432 void OtherServerSmall()
434 switch(child_pid=fork())
438 CPPUNIT_FAIL("fork error");
446 socket_server ss("./socket");
449 // hmm, we got the wrong socket
452 ss.add_callback(new_connection,bind(&test_hello::send_raw_socket, boost::ref(*this), hello.str(),&ss, _1));
455 for (int i=0; i < 3; i++)
456 ss.fill_buffer(1000000);
459 std::cerr << "exception in child. ignoring\n";
462 // don't call atexit and stuff
471 // wait till server is up
473 socket_client_connection sc("./socket");
475 command_client cc(&sc);
477 t2n_exception* ep=cc.get_constuctor_exception();
483 CPPUNIT_ASSERT_EQUAL(string("illegal hello received (T2N)"),errormsg);
490 CPPUNIT_TEST_SUITE_REGISTRATION(test_hello);