-/***************************************************************************
- * Copyright (C) 2004 by Intra2net AG *
- * info@intra2net.com *
- * *
- ***************************************************************************/
+/*
+Copyright (C) 2004 by Intra2net AG
+The software in this package is distributed under the GNU General
+Public License version 2 (with a special exception described below).
+
+A copy of GNU General Public License (GPL) is included in this distribution,
+in the file COPYING.GPL.
+
+As a special exception, if other files instantiate templates or use macros
+or inline functions from this file, or you compile this file and link it
+with other works to produce a work based on this file, this file
+does not by itself cause the resulting work to be covered
+by the GNU General Public License.
+
+However the source code for this file must still be made available
+in accordance with section (3) of the GNU General Public License.
+
+This exception does not invalidate any other reasons why a work based
+on this file might be covered by the GNU General Public License.
+*/
#include <sys/types.h>
#include <unistd.h>
#include <errno.h>
#include <sstream>
#include <stdexcept>
-#include <cppunit/extensions/TestFactoryRegistry.h>
-#include <cppunit/ui/text/TestRunner.h>
-#include <cppunit/extensions/HelperMacros.h>
+#define BOOST_TEST_DYN_LINK
+#include <boost/test/unit_test.hpp>
#include <socket_client.hxx>
#include <socket_server.hxx>
-#ifdef HAVE_CONFIG_H
+#include "test_fixtures.hxx"
+
#include <config.h>
-#endif
using namespace std;
using namespace libt2n;
-using namespace CppUnit;
-// this is an evil hack to get access to real_write, don't ever do this in an app!!!
-class real_write_connection: public socket_server_connection
-{
- public:
- void real_write(const std::string& data)
- { socket_write(data); }
-};
+BOOST_FIXTURE_TEST_SUITE(test_reconnect, KillChildOnShutdownFixture)
-class test_reconnect : public TestFixture
+BOOST_AUTO_TEST_CASE(simple_reconnect)
{
- CPPUNIT_TEST_SUITE(test_reconnect);
-
- CPPUNIT_TEST(simple_reconnect);
- CPPUNIT_TEST(reconnect_with_close);
- CPPUNIT_TEST(reconnect_buffer_complete);
- CPPUNIT_TEST(reconnect_buffer_several_complete);
- CPPUNIT_TEST(reconnect_buffer_no_incomplete1);
- CPPUNIT_TEST(reconnect_buffer_no_incomplete2);
-
- CPPUNIT_TEST_SUITE_END();
-
- pid_t child_pid;
-
- public:
-
- void setUp()
- { }
-
- void tearDown()
- {
- // make sure the server-child is dead before the next test runs
- kill(child_pid,SIGKILL);
- sleep(1);
- }
-
- void send_raw_socket(string hello_string, socket_server* ss, int conn_id)
+ switch(child_pid=fork())
{
- socket_server_connection *ssc=dynamic_cast<socket_server_connection*>(ss->get_connection(conn_id));
-
- // this is an evil hack to get access to real_write, don't ever do this in an app!!!
- real_write_connection *rwc=(real_write_connection*)ssc;
- rwc->real_write(hello_string);
- }
-
- void simple_reconnect()
- {
- switch(child_pid=fork())
+ case -1:
{
- case -1:
- {
- CPPUNIT_FAIL("fork error");
- break;
- }
- case 0:
- // child
+ BOOST_FAIL("fork error");
+ break;
+ }
+ case 0:
+ // child
+ {
+ try
{
- try
- {
- socket_server ss("./socket");
+ socket_server ss("./socket");
- time_t t0 = time(NULL);
+ time_t t0 = time(NULL);
- // max 10 sec
- while (time(NULL) < t0 + 10 )
- {
- ss.fill_buffer(1000000);
+ // max 10 sec
+ while (time(NULL) < t0 + 10 )
+ {
+ ss.fill_buffer(1000000);
- string data;
- unsigned int cid;
+ string data;
+ unsigned int cid;
- if(ss.get_packet(data,cid))
- {
- server_connection* con=ss.get_connection(cid);
+ if(ss.get_packet(data,cid))
+ {
+ server_connection* con=ss.get_connection(cid);
- if (data=="QUIT")
- break;
+ if (data=="QUIT")
+ break;
- if (data=="x")
- con->write(string().insert(0,100,'X'));
- else
- con->write(string().insert(0,100,'Y'));
- }
+ if (data=="x")
+ con->write(string().insert(0,100,'X'));
+ else
+ con->write(string().insert(0,100,'Y'));
}
- } catch(...)
- {
- std::cerr << "exception in child. ignoring\n";
}
-
- // don't call atexit and stuff
- _exit(0);
+ } catch(...)
+ {
+ std::cerr << "exception in child. ignoring\n";
}
- default:
- // parent
- {
- // don't kill us on broken pipe
- signal(SIGPIPE, SIG_IGN);
+ // don't call atexit and stuff
+ _exit(0);
+ }
- // wait till server is up
- sleep(1);
- socket_client_connection sc("./socket");
+ default:
+ // parent
+ {
+ // don't kill us on broken pipe
+ signal(SIGPIPE, SIG_IGN);
- sc.write("abc");
+ // wait till server is up
+ sleep(1);
+ socket_client_connection sc("./socket");
- string data;
+ sc.write("abc");
- while (!sc.get_packet(data))
- sc.fill_buffer(1000000);
+ string data;
- sc.reconnect();
+ while (!sc.get_packet(data))
+ sc.fill_buffer(1000000);
- sc.write("x");
+ sc.reconnect();
- while (!sc.get_packet(data))
- sc.fill_buffer(1000000);
+ sc.write("x");
- CPPUNIT_ASSERT_EQUAL(string().insert(0,100,'X'),data);
- }
+ while (!sc.get_packet(data))
+ sc.fill_buffer(1000000);
+
+ BOOST_CHECK_EQUAL(string().insert(0,100,'X'),data);
}
}
+}
- void reconnect_with_close()
+BOOST_AUTO_TEST_CASE(reconnect_with_close)
+{
+ switch(child_pid=fork())
{
- switch(child_pid=fork())
+ case -1:
{
- case -1:
- {
- CPPUNIT_FAIL("fork error");
- break;
- }
- case 0:
- // child
+ BOOST_FAIL("fork error");
+ break;
+ }
+ case 0:
+ // child
+ {
+ try
{
- try
- {
- socket_server ss("./socket");
+ socket_server ss("./socket");
- time_t t0 = time(NULL);
+ time_t t0 = time(NULL);
- // max 10 sec
- while (time(NULL) < t0 + 10 )
- {
- ss.fill_buffer(1000000);
+ // max 10 sec
+ while (time(NULL) < t0 + 10 )
+ {
+ ss.fill_buffer(1000000);
- string data;
- unsigned int cid;
+ string data;
+ unsigned int cid;
- if(ss.get_packet(data,cid))
- {
- server_connection* con=ss.get_connection(cid);
+ if(ss.get_packet(data,cid))
+ {
+ server_connection* con=ss.get_connection(cid);
- if (data=="QUIT")
- break;
+ if (data=="QUIT")
+ break;
- if (data=="x")
- con->write(string().insert(0,100,'X'));
- else
- con->write(string().insert(0,100,'Y'));
- }
+ if (data=="x")
+ con->write(string().insert(0,100,'X'));
+ else
+ con->write(string().insert(0,100,'Y'));
}
- } catch(...)
- {
- std::cerr << "exception in child. ignoring\n";
}
-
- // don't call atexit and stuff
- _exit(0);
+ } catch(...)
+ {
+ std::cerr << "exception in child. ignoring\n";
}
- default:
- // parent
- {
- // don't kill us on broken pipe
- signal(SIGPIPE, SIG_IGN);
+ // don't call atexit and stuff
+ _exit(0);
+ }
- // wait till server is up
- sleep(1);
- socket_client_connection sc("./socket");
+ default:
+ // parent
+ {
+ // don't kill us on broken pipe
+ signal(SIGPIPE, SIG_IGN);
- sc.write("abc");
+ // wait till server is up
+ sleep(1);
+ socket_client_connection sc("./socket");
- string data;
+ sc.write("abc");
- while (!sc.get_packet(data))
- sc.fill_buffer(1000000);
+ string data;
- sc.close();
+ while (!sc.get_packet(data))
+ sc.fill_buffer(1000000);
- // empty buffer
- sc.get_packet(data);
+ sc.close();
- sc.reconnect();
+ // empty buffer
+ sc.get_packet(data);
- sc.write("x");
+ sc.reconnect();
- while (!sc.get_packet(data))
- sc.fill_buffer(1000000);
+ sc.write("x");
- CPPUNIT_ASSERT_EQUAL(string().insert(0,100,'X'),data);
- }
+ while (!sc.get_packet(data))
+ sc.fill_buffer(1000000);
+
+ BOOST_CHECK_EQUAL(string().insert(0,100,'X'),data);
}
}
+}
- void reconnect_buffer_complete()
+BOOST_AUTO_TEST_CASE(reconnect_buffer_complete)
+{
+ switch(child_pid=fork())
{
- switch(child_pid=fork())
+ case -1:
{
- case -1:
- {
- CPPUNIT_FAIL("fork error");
- break;
- }
- case 0:
- // child
+ BOOST_FAIL("fork error");
+ break;
+ }
+ case 0:
+ // child
+ {
+ try
{
- try
- {
- socket_server ss("./socket");
+ socket_server ss("./socket");
- time_t t0 = time(NULL);
+ time_t t0 = time(NULL);
- // max 10 sec
- while (time(NULL) < t0 + 10 )
- {
- ss.fill_buffer(1000000);
+ // max 10 sec
+ while (time(NULL) < t0 + 10 )
+ {
+ ss.fill_buffer(1000000);
- string data;
- unsigned int cid;
+ string data;
+ unsigned int cid;
- if(ss.get_packet(data,cid))
- {
- server_connection* con=ss.get_connection(cid);
+ if(ss.get_packet(data,cid))
+ {
+ server_connection* con=ss.get_connection(cid);
- if (data=="QUIT")
- break;
+ if (data=="QUIT")
+ break;
- if (data=="x")
- con->write(string().insert(0,100,'X'));
- }
+ if (data=="x")
+ con->write(string().insert(0,100,'X'));
}
- } catch(...)
- {
- std::cerr << "exception in child. ignoring\n";
}
-
- // don't call atexit and stuff
- _exit(0);
+ } catch(...)
+ {
+ std::cerr << "exception in child. ignoring\n";
}
- default:
- // parent
- {
- // don't kill us on broken pipe
- signal(SIGPIPE, SIG_IGN);
+ // don't call atexit and stuff
+ _exit(0);
+ }
- // wait till server is up
- sleep(1);
- socket_client_connection sc("./socket");
+ default:
+ // parent
+ {
+ // don't kill us on broken pipe
+ signal(SIGPIPE, SIG_IGN);
- sc.write("x");
+ // wait till server is up
+ sleep(1);
+ socket_client_connection sc("./socket");
- string data;
+ sc.write("x");
- while (!sc.packet_available())
- sc.fill_buffer(1000000);
+ string data;
- sc.reconnect();
+ while (!sc.packet_available())
+ sc.fill_buffer(1000000);
- CPPUNIT_ASSERT_EQUAL_MESSAGE("packet not there",true,sc.get_packet(data));
+ sc.reconnect();
- CPPUNIT_ASSERT_EQUAL_MESSAGE("data incorrect",string().insert(0,100,'X'),data);
- }
+ BOOST_CHECK_MESSAGE(sc.get_packet(data) == true, "packet not there");
+
+ BOOST_CHECK_MESSAGE(data == string().insert(0,100,'X'), "data incorrect");
}
}
+}
- void reconnect_buffer_several_complete()
- {
- const int packets=3;
+BOOST_AUTO_TEST_CASE(reconnect_buffer_several_complete)
+{
+ const int packets=3;
- switch(child_pid=fork())
+ switch(child_pid=fork())
+ {
+ case -1:
{
- case -1:
- {
- CPPUNIT_FAIL("fork error");
- break;
- }
- case 0:
- // child
+ BOOST_FAIL("fork error");
+ break;
+ }
+ case 0:
+ // child
+ {
+ try
{
- try
+ socket_server ss("./socket");
+
+ time_t t0 = time(NULL);
+
+ // max 10 sec
+ while (time(NULL) < t0 + 10 )
{
- socket_server ss("./socket");
+ ss.fill_buffer(1000000);
- time_t t0 = time(NULL);
+ string data;
+ unsigned int cid;
- // max 10 sec
- while (time(NULL) < t0 + 10 )
+ if(ss.get_packet(data,cid))
{
- ss.fill_buffer(1000000);
+ server_connection* con=ss.get_connection(cid);
- string data;
- unsigned int cid;
+ if (data=="QUIT")
+ break;
- if(ss.get_packet(data,cid))
+ if (data=="x")
{
- server_connection* con=ss.get_connection(cid);
-
- if (data=="QUIT")
- break;
-
- if (data=="x")
- {
- for (int i=0; i<packets; i++)
- con->write(string().insert(0,100,'X'));
- }
+ for (int i=0; i<packets; i++)
+ con->write(string().insert(0,100,'X'));
}
}
- } catch(...)
- {
- std::cerr << "exception in child. ignoring\n";
}
-
- // don't call atexit and stuff
- _exit(0);
+ } catch(...)
+ {
+ std::cerr << "exception in child. ignoring\n";
}
- default:
- // parent
- {
- // don't kill us on broken pipe
- signal(SIGPIPE, SIG_IGN);
+ // don't call atexit and stuff
+ _exit(0);
+ }
- // wait till server is up
- sleep(1);
- socket_client_connection sc("./socket");
+ default:
+ // parent
+ {
+ // don't kill us on broken pipe
+ signal(SIGPIPE, SIG_IGN);
- sc.write("x");
+ // wait till server is up
+ sleep(1);
+ socket_client_connection sc("./socket");
- // retrieve packets for 3 seconds
- time_t t0 = time(NULL);
+ sc.write("x");
- // max 3 sec
- while (time(NULL) < t0 + 3 )
- sc.fill_buffer(1000000);
+ // retrieve packets for 3 seconds
+ time_t t0 = time(NULL);
- // we now should have packets complete packets in the buffer
+ // max 3 sec
+ while (time(NULL) < t0 + 3 )
+ sc.fill_buffer(1000000);
- sc.reconnect();
+ // we now should have packets complete packets in the buffer
- // are these packets still there?
+ sc.reconnect();
- for (int i=0; i < packets; i++)
- {
- string data;
+ // are these packets still there?
- ostringstream os;
- os << "packet " << i << " not there";
+ for (int i=0; i < packets; i++)
+ {
+ string data;
- CPPUNIT_ASSERT_EQUAL_MESSAGE(os.str(),true,sc.get_packet(data));
+ ostringstream os;
+ os << "packet " << i << " not there";
- os.str("");
- os << "packet " << i << " incorrect";
+ BOOST_CHECK_MESSAGE(sc.get_packet(data) == true, os.str());
- CPPUNIT_ASSERT_EQUAL_MESSAGE(os.str(),string().insert(0,100,'X'),data);
- }
+ os.str("");
+ os << "packet " << i << " incorrect";
+
+ BOOST_CHECK_MESSAGE(string().insert(0,100,'X') == data, os.str());
}
}
}
+}
- void reconnect_buffer_no_incomplete1()
+BOOST_AUTO_TEST_CASE(reconnect_buffer_no_incomplete1)
+{
+ switch(child_pid=fork())
{
- switch(child_pid=fork())
+ case -1:
{
- case -1:
- {
- CPPUNIT_FAIL("fork error");
- break;
- }
- case 0:
- // child
+ BOOST_FAIL("fork error");
+ break;
+ }
+ case 0:
+ // child
+ {
+ try
{
- try
+ socket_server ss("./socket");
+
+ time_t t0 = time(NULL);
+
+ // max 10 sec
+ while (time(NULL) < t0 + 10 )
{
- socket_server ss("./socket");
+ ss.fill_buffer(1000000);
- time_t t0 = time(NULL);
+ string data;
+ unsigned int cid;
- // max 10 sec
- while (time(NULL) < t0 + 10 )
+ if(ss.get_packet(data,cid))
{
- ss.fill_buffer(1000000);
+ server_connection* con=ss.get_connection(cid);
- string data;
- unsigned int cid;
+ if (data=="QUIT")
+ break;
- if(ss.get_packet(data,cid))
+ if (data=="x")
{
- server_connection* con=ss.get_connection(cid);
-
- if (data=="QUIT")
- break;
-
- if (data=="x")
- {
- con->write(string().insert(0,100,'X'));
- send_raw_socket("aaaab",&ss,cid);
- }
+ con->write(string().insert(0,100,'X'));
+ send_raw_socket("aaaab",&ss,cid);
}
}
- } catch(...)
- {
- std::cerr << "exception in child. ignoring\n";
}
-
- // don't call atexit and stuff
- _exit(0);
+ } catch(...)
+ {
+ std::cerr << "exception in child. ignoring\n";
}
- default:
- // parent
- {
- // don't kill us on broken pipe
- signal(SIGPIPE, SIG_IGN);
+ // don't call atexit and stuff
+ _exit(0);
+ }
- // wait till server is up
- sleep(1);
- socket_client_connection sc("./socket");
+ default:
+ // parent
+ {
+ // don't kill us on broken pipe
+ signal(SIGPIPE, SIG_IGN);
- sc.write("x");
+ // wait till server is up
+ sleep(1);
+ socket_client_connection sc("./socket");
- // retrieve packets for 3 seconds
- time_t t0 = time(NULL);
+ sc.write("x");
- // max 3 sec
- while (time(NULL) < t0 + 3 )
- sc.fill_buffer(1000000);
+ // retrieve packets for 3 seconds
+ time_t t0 = time(NULL);
- // we now should have one complete packet and some stuff in the buffer
+ // max 3 sec
+ while (time(NULL) < t0 + 3 )
+ sc.fill_buffer(1000000);
- string data;
- sc.get_packet(data);
+ // we now should have one complete packet and some stuff in the buffer
- CPPUNIT_ASSERT_EQUAL_MESSAGE("no incomplete packet",true,(sc.peek_packet(data))>0);
+ string data;
+ sc.get_packet(data);
- sc.reconnect();
+ BOOST_CHECK_MESSAGE((sc.peek_packet(data))>0, "no incomplete packet");
- CPPUNIT_ASSERT_EQUAL_MESSAGE("incomplete packet not removed",0,(int)sc.peek_packet(data));
- }
+ sc.reconnect();
+
+ BOOST_CHECK_MESSAGE((int)sc.peek_packet(data) == 0, "incomplete packet not removed");
}
}
+}
- void reconnect_buffer_no_incomplete2()
+BOOST_AUTO_TEST_CASE(reconnect_buffer_no_incomplete2)
+{
+ switch(child_pid=fork())
{
- switch(child_pid=fork())
+ case -1:
{
- case -1:
- {
- CPPUNIT_FAIL("fork error");
- break;
- }
- case 0:
- // child
+ BOOST_FAIL("fork error");
+ break;
+ }
+ case 0:
+ // child
+ {
+ try
{
- try
+ socket_server ss("./socket");
+
+ time_t t0 = time(NULL);
+
+ // max 10 sec
+ while (time(NULL) < t0 + 10 )
{
- socket_server ss("./socket");
+ ss.fill_buffer(1000000);
- time_t t0 = time(NULL);
+ string data;
+ unsigned int cid;
- // max 10 sec
- while (time(NULL) < t0 + 10 )
+ if(ss.get_packet(data,cid))
{
- ss.fill_buffer(1000000);
+ server_connection* con=ss.get_connection(cid);
- string data;
- unsigned int cid;
+ if (data=="QUIT")
+ break;
- if(ss.get_packet(data,cid))
+ if (data=="x")
{
- server_connection* con=ss.get_connection(cid);
+ con->write(string().insert(0,100,'X'));
- if (data=="QUIT")
- break;
+ string blob=string().insert(0,100,'Y');
- if (data=="x")
- {
- con->write(string().insert(0,100,'X'));
-
- string blob=string().insert(0,100,'Y');
-
- // one byte will be missing...
- int size=blob.size()+1;
- char sizetransfer[sizeof(int)+1];
- memcpy(sizetransfer,(void*)&size,sizeof(int));
- sizetransfer[sizeof(int)+1]=0;
+ // one byte will be missing...
+ int size=blob.size()+1;
+ char sizetransfer[sizeof(int)+1];
+ memcpy(sizetransfer,(void*)&size,sizeof(int));
+ sizetransfer[sizeof(int)+1]=0;
- string packet=string(sizetransfer)+blob;
+ string packet=string(sizetransfer)+blob;
- send_raw_socket(packet,&ss,cid);
- }
+ send_raw_socket(packet,&ss,cid);
}
}
- } catch(...)
- {
- std::cerr << "exception in child. ignoring\n";
}
-
- // don't call atexit and stuff
- _exit(0);
+ } catch(...)
+ {
+ std::cerr << "exception in child. ignoring\n";
}
- default:
- // parent
- {
- // don't kill us on broken pipe
- signal(SIGPIPE, SIG_IGN);
+ // don't call atexit and stuff
+ _exit(0);
+ }
- // wait till server is up
- sleep(1);
- socket_client_connection sc("./socket");
+ default:
+ // parent
+ {
+ // don't kill us on broken pipe
+ signal(SIGPIPE, SIG_IGN);
- sc.write("x");
+ // wait till server is up
+ sleep(1);
+ socket_client_connection sc("./socket");
- // retrieve packets for 3 seconds
- time_t t0 = time(NULL);
+ sc.write("x");
- // max 3 sec
- while (time(NULL) < t0 + 3 )
- sc.fill_buffer(1000000);
+ // retrieve packets for 3 seconds
+ time_t t0 = time(NULL);
- // we now should have one complete packet and some stuff in the buffer
+ // max 3 sec
+ while (time(NULL) < t0 + 3 )
+ sc.fill_buffer(1000000);
- sc.reconnect();
+ // we now should have one complete packet and some stuff in the buffer
- string data;
+ sc.reconnect();
- CPPUNIT_ASSERT_EQUAL_MESSAGE("packet not there",true,sc.get_packet(data));
- CPPUNIT_ASSERT_EQUAL_MESSAGE("data incorrect",string().insert(0,100,'X'),data);
+ string data;
- CPPUNIT_ASSERT_EQUAL_MESSAGE("incomplete packet not removed",0,(int)sc.peek_packet(data));
- }
+ BOOST_CHECK_MESSAGE(sc.get_packet(data) == true, "packet not there");
+ BOOST_CHECK_MESSAGE(string().insert(0,100,'X') == data, "data incorrect");
+
+ BOOST_CHECK_MESSAGE((int)sc.peek_packet(data) == 0, "incomplete packet not removed");
}
}
+}
-};
-
-CPPUNIT_TEST_SUITE_REGISTRATION(test_reconnect);
+BOOST_AUTO_TEST_SUITE_END()