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");
100 socket_server ss("./socket");
103 hello << "T2Nv" << PROTOCOL_VERSION << ';';
104 int byteordercheck=1;
105 hello.write((char*)&byteordercheck,sizeof(byteordercheck));
108 ss.add_callback(new_connection,bind(&test_hello::send_hello, boost::ref(*this), hello.str(),&ss, _1));
111 for (int i=0; i < 10; i++)
112 ss.fill_buffer(1000000);
113 // don't call atexit and stuff
122 // wait till server is up
124 socket_client_connection sc("./socket");
125 command_client cc(&sc);
132 switch(child_pid=fork())
136 CPPUNIT_FAIL("fork error");
142 socket_server ss("./socket");
147 ss.add_callback(new_connection,bind(&test_hello::send_hello, boost::ref(*this), hello.str(),&ss, _1));
150 for (int i=0; i < 10; i++)
151 ss.fill_buffer(1000000);
152 // don't call atexit and stuff
161 // wait till server is up
163 socket_client_connection sc("./socket");
165 command_client cc(&sc);
167 t2n_exception* ep=cc.get_constuctor_exception();
173 CPPUNIT_ASSERT_EQUAL(string("illegal hello received (T2N)"),errormsg);
180 switch(child_pid=fork())
184 CPPUNIT_FAIL("fork error");
190 socket_server ss("./socket");
193 // lets hope we don't ever get near such a version number...
194 hello << "T2Nv" << 4982271 << ';';
195 int byteordercheck=1;
196 hello.write((char*)&byteordercheck,sizeof(byteordercheck));
199 ss.add_callback(new_connection,bind(&test_hello::send_hello, boost::ref(*this), hello.str(),&ss, _1));
202 for (int i=0; i < 10; i++)
203 ss.fill_buffer(1000000);
204 // don't call atexit and stuff
213 // wait till server is up
215 socket_client_connection sc("./socket");
217 command_client cc(&sc);
219 t2n_exception* ep=cc.get_constuctor_exception();
225 CPPUNIT_ASSERT_EQUAL(string("not compatible with the server protocol version"),errormsg);
230 void SeparatorMissing()
232 switch(child_pid=fork())
236 CPPUNIT_FAIL("fork error");
242 socket_server ss("./socket");
245 hello << "T2Nv" << PROTOCOL_VERSION;
246 int byteordercheck=1;
247 hello.write((char*)&byteordercheck,sizeof(byteordercheck));
250 ss.add_callback(new_connection,bind(&test_hello::send_hello, boost::ref(*this), hello.str(),&ss, _1));
253 for (int i=0; i < 10; i++)
254 ss.fill_buffer(1000000);
255 // don't call atexit and stuff
264 // wait till server is up
266 socket_client_connection sc("./socket");
268 command_client cc(&sc);
270 t2n_exception* ep=cc.get_constuctor_exception();
276 CPPUNIT_ASSERT_EQUAL(string("illegal hello received (1. ;)"),errormsg);
281 void WrongByteOrder()
283 switch(child_pid=fork())
287 CPPUNIT_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::send_hello, boost::ref(*this), hello.str(),&ss, _1));
313 for (int i=0; i < 10; i++)
314 ss.fill_buffer(1000000);
315 // don't call atexit and stuff
324 // wait till server is up
326 socket_client_connection sc("./socket");
328 command_client cc(&sc);
330 t2n_exception* ep=cc.get_constuctor_exception();
336 CPPUNIT_ASSERT_EQUAL(string("host byte order not matching"),errormsg);
341 void OtherServerBig()
343 switch(child_pid=fork())
347 CPPUNIT_FAIL("fork error");
353 socket_server ss("./socket");
356 // hmm, we got the wrong socket
357 hello << "* OK intradev.net.lan Cyrus IMAP4 v2.2.13 server ready";
359 ss.add_callback(new_connection,bind(&test_hello::send_raw_socket, boost::ref(*this), hello.str(),&ss, _1));
362 for (int i=0; i < 3; i++)
363 ss.fill_buffer(1000000);
364 // don't call atexit and stuff
373 // wait till server is up
375 socket_client_connection sc("./socket");
377 command_client cc(&sc);
379 t2n_exception* ep=cc.get_constuctor_exception();
385 CPPUNIT_ASSERT_EQUAL(string("illegal hello received (T2N)"),errormsg);
390 void OtherServerSmall()
392 switch(child_pid=fork())
396 CPPUNIT_FAIL("fork error");
402 socket_server ss("./socket");
405 // hmm, we got the wrong socket
408 ss.add_callback(new_connection,bind(&test_hello::send_raw_socket, boost::ref(*this), hello.str(),&ss, _1));
411 for (int i=0; i < 3; i++)
412 ss.fill_buffer(1000000);
413 // don't call atexit and stuff
422 // wait till server is up
424 socket_client_connection sc("./socket");
426 command_client cc(&sc);
428 t2n_exception* ep=cc.get_constuctor_exception();
434 CPPUNIT_ASSERT_EQUAL(string("illegal hello received (T2N)"),errormsg);
441 CPPUNIT_TEST_SUITE_REGISTRATION(test_hello);