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(OtherServer);
56 CPPUNIT_TEST_SUITE_END();
66 void send_hello(string hello_string, socket_server* ss, int conn_id)
68 server_connection *sc=ss->get_connection(conn_id);
69 sc->write(hello_string);
72 void send_raw_socket(string hello_string, socket_server* ss, int conn_id)
74 socket_server_connection *ssc=dynamic_cast<socket_server_connection*>(ss->get_connection(conn_id));
76 // this is an evil hack to get access to real_write, don't ever do this in an app!!!
77 real_write_connection *rwc=(real_write_connection*)ssc;
78 rwc->real_write(hello_string);
89 CPPUNIT_FAIL("fork error");
95 socket_server ss("./socket");
98 hello << "T2Nv" << PROTOCOL_VERSION << ';';
100 hello.write((char*)&byteordercheck,sizeof(byteordercheck));
103 ss.add_callback(new_connection,bind(&test_hello::send_hello, boost::ref(*this), hello.str(),&ss, _1));
106 for (int i=0; i < 10; i++)
107 ss.fill_buffer(1000000);
108 // don't call atexit and stuff
117 // wait till server is up
119 socket_client_connection sc("./socket");
120 command_client cc(sc);
133 CPPUNIT_FAIL("fork error");
139 socket_server ss("./socket");
144 ss.add_callback(new_connection,bind(&test_hello::send_hello, boost::ref(*this), hello.str(),&ss, _1));
147 for (int i=0; i < 10; i++)
148 ss.fill_buffer(1000000);
149 // don't call atexit and stuff
158 // wait till server is up
160 socket_client_connection sc("./socket");
166 command_client cc(sc);
168 catch(t2n_version_mismatch &e)
169 { errormsg=e.what(); }
173 CPPUNIT_ASSERT_EQUAL(string("illegal hello received (T2N)"),errormsg);
186 CPPUNIT_FAIL("fork error");
192 socket_server ss("./socket");
195 // lets hope we don't ever get near such a version number...
196 hello << "T2Nv" << 4982271 << ';';
197 int byteordercheck=1;
198 hello.write((char*)&byteordercheck,sizeof(byteordercheck));
201 ss.add_callback(new_connection,bind(&test_hello::send_hello, boost::ref(*this), hello.str(),&ss, _1));
204 for (int i=0; i < 10; i++)
205 ss.fill_buffer(1000000);
206 // don't call atexit and stuff
215 // wait till server is up
217 socket_client_connection sc("./socket");
223 command_client cc(sc);
225 catch(t2n_version_mismatch &e)
226 { errormsg=e.what(); }
230 CPPUNIT_ASSERT_EQUAL(string("not compatible with the server protocol version"),errormsg);
235 void SeparatorMissing()
243 CPPUNIT_FAIL("fork error");
249 socket_server ss("./socket");
252 hello << "T2Nv" << PROTOCOL_VERSION;
253 int byteordercheck=1;
254 hello.write((char*)&byteordercheck,sizeof(byteordercheck));
257 ss.add_callback(new_connection,bind(&test_hello::send_hello, boost::ref(*this), hello.str(),&ss, _1));
260 for (int i=0; i < 10; i++)
261 ss.fill_buffer(1000000);
262 // don't call atexit and stuff
271 // wait till server is up
273 socket_client_connection sc("./socket");
279 command_client cc(sc);
281 catch(t2n_version_mismatch &e)
282 { errormsg=e.what(); }
286 CPPUNIT_ASSERT_EQUAL(string("illegal hello received (1. ;)"),errormsg);
291 void WrongByteOrder()
299 CPPUNIT_FAIL("fork error");
305 socket_server ss("./socket");
308 hello << "T2Nv" << PROTOCOL_VERSION << ';';
309 int byteordercheck=1;
311 char* si=(char*)&byteordercheck;
312 char* di=(char*)&dst;
319 hello.write((char*)&dst,sizeof(dst));
322 ss.add_callback(new_connection,bind(&test_hello::send_hello, boost::ref(*this), hello.str(),&ss, _1));
325 for (int i=0; i < 10; i++)
326 ss.fill_buffer(1000000);
327 // don't call atexit and stuff
336 // wait till server is up
338 socket_client_connection sc("./socket");
344 command_client cc(sc);
346 catch(t2n_version_mismatch &e)
347 { errormsg=e.what(); }
351 CPPUNIT_ASSERT_EQUAL(string("host byte order not matching"),errormsg);
364 CPPUNIT_FAIL("fork error");
370 socket_server ss("./socket");
373 // hmm, we got the wrong socket
374 hello << "* OK intradev.net.lan Cyrus IMAP4 v2.2.13 server ready";
376 ss.add_callback(new_connection,bind(&test_hello::send_raw_socket, boost::ref(*this), hello.str(),&ss, _1));
379 for (int i=0; i < 3; i++)
380 ss.fill_buffer(1000000);
381 // don't call atexit and stuff
390 // wait till server is up
392 socket_client_connection sc("./socket");
398 command_client cc(sc);
400 catch(t2n_version_mismatch &e)
401 { errormsg=e.what(); }
405 CPPUNIT_ASSERT_EQUAL(string("illegal hello received (T2N)"),errormsg);
412 CPPUNIT_TEST_SUITE_REGISTRATION(test_hello);