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();
67 void send_hello(string hello_string, socket_server* ss, int conn_id)
69 server_connection *sc=ss->get_connection(conn_id);
70 sc->write(hello_string);
73 void send_raw_socket(string hello_string, socket_server* ss, int conn_id)
75 socket_server_connection *ssc=dynamic_cast<socket_server_connection*>(ss->get_connection(conn_id));
77 // this is an evil hack to get access to real_write, don't ever do this in an app!!!
78 real_write_connection *rwc=(real_write_connection*)ssc;
79 rwc->real_write(hello_string);
90 CPPUNIT_FAIL("fork error");
96 socket_server ss("./socket");
99 hello << "T2Nv" << PROTOCOL_VERSION << ';';
100 int byteordercheck=1;
101 hello.write((char*)&byteordercheck,sizeof(byteordercheck));
104 ss.add_callback(new_connection,bind(&test_hello::send_hello, boost::ref(*this), hello.str(),&ss, _1));
107 for (int i=0; i < 10; i++)
108 ss.fill_buffer(1000000);
109 // don't call atexit and stuff
118 // wait till server is up
120 socket_client_connection sc("./socket");
121 command_client cc(sc);
134 CPPUNIT_FAIL("fork error");
140 socket_server ss("./socket");
145 ss.add_callback(new_connection,bind(&test_hello::send_hello, boost::ref(*this), hello.str(),&ss, _1));
148 for (int i=0; i < 10; i++)
149 ss.fill_buffer(1000000);
150 // don't call atexit and stuff
159 // wait till server is up
161 socket_client_connection sc("./socket");
167 command_client cc(sc);
169 catch(t2n_version_mismatch &e)
170 { errormsg=e.what(); }
174 CPPUNIT_ASSERT_EQUAL(string("illegal hello received (T2N)"),errormsg);
187 CPPUNIT_FAIL("fork error");
193 socket_server ss("./socket");
196 // lets hope we don't ever get near such a version number...
197 hello << "T2Nv" << 4982271 << ';';
198 int byteordercheck=1;
199 hello.write((char*)&byteordercheck,sizeof(byteordercheck));
202 ss.add_callback(new_connection,bind(&test_hello::send_hello, boost::ref(*this), hello.str(),&ss, _1));
205 for (int i=0; i < 10; i++)
206 ss.fill_buffer(1000000);
207 // don't call atexit and stuff
216 // wait till server is up
218 socket_client_connection sc("./socket");
224 command_client cc(sc);
226 catch(t2n_version_mismatch &e)
227 { errormsg=e.what(); }
231 CPPUNIT_ASSERT_EQUAL(string("not compatible with the server protocol version"),errormsg);
236 void SeparatorMissing()
244 CPPUNIT_FAIL("fork error");
250 socket_server ss("./socket");
253 hello << "T2Nv" << PROTOCOL_VERSION;
254 int byteordercheck=1;
255 hello.write((char*)&byteordercheck,sizeof(byteordercheck));
258 ss.add_callback(new_connection,bind(&test_hello::send_hello, boost::ref(*this), hello.str(),&ss, _1));
261 for (int i=0; i < 10; i++)
262 ss.fill_buffer(1000000);
263 // don't call atexit and stuff
272 // wait till server is up
274 socket_client_connection sc("./socket");
280 command_client cc(sc);
282 catch(t2n_version_mismatch &e)
283 { errormsg=e.what(); }
287 CPPUNIT_ASSERT_EQUAL(string("illegal hello received (1. ;)"),errormsg);
292 void WrongByteOrder()
300 CPPUNIT_FAIL("fork error");
306 socket_server ss("./socket");
309 hello << "T2Nv" << PROTOCOL_VERSION << ';';
310 int byteordercheck=1;
312 char* si=(char*)&byteordercheck;
313 char* di=(char*)&dst;
320 hello.write((char*)&dst,sizeof(dst));
323 ss.add_callback(new_connection,bind(&test_hello::send_hello, boost::ref(*this), hello.str(),&ss, _1));
326 for (int i=0; i < 10; i++)
327 ss.fill_buffer(1000000);
328 // don't call atexit and stuff
337 // wait till server is up
339 socket_client_connection sc("./socket");
345 command_client cc(sc);
347 catch(t2n_version_mismatch &e)
348 { errormsg=e.what(); }
352 CPPUNIT_ASSERT_EQUAL(string("host byte order not matching"),errormsg);
357 void OtherServerBig()
365 CPPUNIT_FAIL("fork error");
371 socket_server ss("./socket");
374 // hmm, we got the wrong socket
375 hello << "* OK intradev.net.lan Cyrus IMAP4 v2.2.13 server ready";
377 ss.add_callback(new_connection,bind(&test_hello::send_raw_socket, boost::ref(*this), hello.str(),&ss, _1));
380 for (int i=0; i < 3; i++)
381 ss.fill_buffer(1000000);
382 // don't call atexit and stuff
391 // wait till server is up
393 socket_client_connection sc("./socket");
399 command_client cc(sc);
401 catch(t2n_version_mismatch &e)
402 { errormsg=e.what(); }
406 CPPUNIT_ASSERT_EQUAL(string("illegal hello received (T2N)"),errormsg);
411 void OtherServerSmall()
419 CPPUNIT_FAIL("fork error");
425 socket_server ss("./socket");
428 // hmm, we got the wrong socket
431 ss.add_callback(new_connection,bind(&test_hello::send_raw_socket, boost::ref(*this), hello.str(),&ss, _1));
434 for (int i=0; i < 3; i++)
435 ss.fill_buffer(1000000);
436 // don't call atexit and stuff
445 // wait till server is up
447 socket_client_connection sc("./socket");
453 command_client cc(sc);
455 catch(t2n_version_mismatch &e)
456 { errormsg=e.what(); }
460 CPPUNIT_ASSERT_EQUAL(string("illegal hello received (T2N)"),errormsg);
467 CPPUNIT_TEST_SUITE_REGISTRATION(test_hello);