-/***************************************************************************
- * 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 <signal.h>
#include <stdio.h>
+#include <time.h>
#include <iostream>
#include <string>
#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();
-
- public:
-
- void setUp()
- { }
-
- void tearDown()
- { }
-
- 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()
- {
- pid_t pid;
-
- switch(pid=fork())
+ case -1:
{
- case -1:
- {
- CPPUNIT_FAIL("fork error");
- break;
- }
- case 0:
- // child
+ BOOST_FAIL("fork error");
+ break;
+ }
+ case 0:
+ // child
+ {
+ try
{
socket_server ss("./socket");
con->write(string().insert(0,100,'Y'));
}
}
-
- // 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())
{
- pid_t pid;
-
- switch(pid=fork())
+ case -1:
{
- case -1:
- {
- CPPUNIT_FAIL("fork error");
- break;
- }
- case 0:
- // child
+ BOOST_FAIL("fork error");
+ break;
+ }
+ case 0:
+ // child
+ {
+ try
{
socket_server ss("./socket");
con->write(string().insert(0,100,'Y'));
}
}
-
- // 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())
{
- pid_t pid;
-
- switch(pid=fork())
+ case -1:
{
- case -1:
- {
- CPPUNIT_FAIL("fork error");
- break;
- }
- case 0:
- // child
+ BOOST_FAIL("fork error");
+ break;
+ }
+ case 0:
+ // child
+ {
+ try
{
socket_server ss("./socket");
con->write(string().insert(0,100,'X'));
}
}
-
- // 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);
+ }
+
+ default:
+ // parent
+ {
+ // don't kill us on broken pipe
+ signal(SIGPIPE, SIG_IGN);
- // wait till server is up
- sleep(1);
- socket_client_connection sc("./socket");
+ // wait till server is up
+ sleep(1);
+ socket_client_connection sc("./socket");
- sc.write("x");
+ sc.write("x");
- string data;
+ string data;
- while (!sc.packet_available())
- sc.fill_buffer(1000000);
+ while (!sc.packet_available())
+ sc.fill_buffer(1000000);
- sc.reconnect();
+ sc.reconnect();
- CPPUNIT_ASSERT_EQUAL_MESSAGE("packet not there",true,sc.get_packet(data));
+ BOOST_CHECK_MESSAGE(sc.get_packet(data) == true, "packet not there");
- CPPUNIT_ASSERT_EQUAL_MESSAGE("data incorrect",string().insert(0,100,'X'),data);
- }
+ BOOST_CHECK_MESSAGE(data == string().insert(0,100,'X'), "data incorrect");
}
}
+}
- void reconnect_buffer_several_complete()
- {
- pid_t pid;
-
- const int packets=3;
+BOOST_AUTO_TEST_CASE(reconnect_buffer_several_complete)
+{
+ const int packets=3;
- switch(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
{
socket_server ss("./socket");
}
}
}
-
- // 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())
{
- pid_t pid;
-
- switch(pid=fork())
+ case -1:
{
- case -1:
- {
- CPPUNIT_FAIL("fork error");
- break;
- }
- case 0:
- // child
+ BOOST_FAIL("fork error");
+ break;
+ }
+ case 0:
+ // child
+ {
+ try
{
socket_server ss("./socket");
}
}
}
-
- // 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);
+ }
+
+ default:
+ // parent
+ {
+ // don't kill us on broken pipe
+ signal(SIGPIPE, SIG_IGN);
- // wait till server is up
- sleep(1);
- socket_client_connection sc("./socket");
+ // wait till server is up
+ sleep(1);
+ socket_client_connection sc("./socket");
- sc.write("x");
+ sc.write("x");
- // retrieve packets for 3 seconds
- time_t t0 = time(NULL);
+ // retrieve packets for 3 seconds
+ time_t t0 = time(NULL);
- // max 3 sec
- while (time(NULL) < t0 + 3 )
- sc.fill_buffer(1000000);
+ // max 3 sec
+ while (time(NULL) < t0 + 3 )
+ sc.fill_buffer(1000000);
- // we now should have one complete packet and some stuff in the buffer
+ // we now should have one complete packet and some stuff in the buffer
- string data;
- sc.get_packet(data);
+ string data;
+ sc.get_packet(data);
- CPPUNIT_ASSERT_EQUAL_MESSAGE("no incomplete packet",true,(sc.peek_packet(data))>0);
+ BOOST_CHECK_MESSAGE((sc.peek_packet(data))>0, "no incomplete packet");
- sc.reconnect();
+ sc.reconnect();
- CPPUNIT_ASSERT_EQUAL_MESSAGE("incomplete packet not removed",0,(int)sc.peek_packet(data));
- }
+ 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())
{
- pid_t pid;
-
- switch(pid=fork())
+ case -1:
{
- case -1:
- {
- CPPUNIT_FAIL("fork error");
- break;
- }
- case 0:
- // child
+ BOOST_FAIL("fork error");
+ break;
+ }
+ case 0:
+ // child
+ {
+ try
{
socket_server ss("./socket");
int size=blob.size()+1;
char sizetransfer[sizeof(int)+1];
memcpy(sizetransfer,(void*)&size,sizeof(int));
- sizetransfer[sizeof(int)+1]=0;
+ sizetransfer[sizeof(int)]=0;
string packet=string(sizetransfer)+blob;
}
}
}
-
- // 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()