simple-socket library 1.1.5

UnixDatagramSocket_TEST.cpp

Go to the documentation of this file.
00001 #include <cppunit/extensions/HelperMacros.h>
00002 #include <UnixDatagramSocket.h>
00003 
00004 #include <cstring>
00005 
00006 static const char sock_file[] = "/tmp/simple-socket_test.sock";
00007 static const char send_msg[] = "The quick brown fox jumps over the lazy dog";
00008 static char recv_msg[sizeof(send_msg)];
00009 static const int len = sizeof(send_msg);
00010 
00011 class UnixDatagramSocket_TEST : public CppUnit::TestFixture
00012 {
00013     CPPUNIT_TEST_SUITE( UnixDatagramSocket_TEST );
00014     CPPUNIT_TEST( testSendTo );
00015     CPPUNIT_TEST_SUITE_END();
00016 
00017 private:
00018     NET::UnixDatagramSocket* send_socket;
00019     NET::UnixDatagramSocket* recv_socket;
00020 
00021 public:
00022     void setUp()
00023     {
00024         send_socket = new NET::UnixDatagramSocket();
00025         recv_socket = new NET::UnixDatagramSocket();
00026     }
00027 
00028     void tearDown()
00029     {
00030         delete send_socket;
00031         delete recv_socket;
00032 
00033         // remove this if socket can handle it
00034         ::unlink(sock_file);
00035     }
00036 
00037     void testSendTo()
00038     {
00039         int ret;
00040         std::string source;
00041         send_socket->bind(sock_file);
00042         recv_socket->bind(sock_file);
00043         send_socket->sendTo( send_msg, len, sock_file);
00044 
00045         ret = recv_socket->receiveFrom( recv_msg, len, source);
00046         CPPUNIT_ASSERT_EQUAL( len, ret );
00047         CPPUNIT_ASSERT_EQUAL( std::string(sock_file), source );
00048 
00049         ret = recv_socket->timedReceiveFrom( recv_msg, len, source, 10);
00050         CPPUNIT_ASSERT_EQUAL( 0, ret );
00051         CPPUNIT_ASSERT_EQUAL( std::string(sock_file), source );
00052         CPPUNIT_ASSERT( std::memcmp(send_msg, recv_msg, len) == 0 );
00053     }
00054 };
00055 
00056 CPPUNIT_TEST_SUITE_REGISTRATION( UnixDatagramSocket_TEST );