simple-socket library 1.1.5
|
00001 #include <cppunit/extensions/HelperMacros.h> 00002 #include <TCPSocket.h> 00003 00004 #include <cstring> 00005 00006 static const char send_msg[] = "The quick brown fox jumps over the lazy dog"; 00007 static char recv_msg[sizeof(send_msg)]; 00008 static const int len = sizeof(send_msg); 00009 00010 class TCPSocket_TEST : public CppUnit::TestFixture 00011 { 00012 CPPUNIT_TEST_SUITE( TCPSocket_TEST ); 00013 CPPUNIT_TEST( testSocketHandle ); 00014 CPPUNIT_TEST( testPeerStatus ); 00015 CPPUNIT_TEST_SUITE_END(); 00016 00017 private: 00018 NET::TCPSocket* server_socket; 00019 NET::TCPSocket* client_socket; 00020 00021 public: 00022 void setUp() 00023 { 00024 server_socket = new NET::TCPSocket(); 00025 client_socket = new NET::TCPSocket(); 00026 } 00027 00028 void tearDown() 00029 { 00030 delete server_socket; 00031 delete client_socket; 00032 } 00033 00034 void testSocketHandle() 00035 { 00036 server_socket->bind( "127.0.0.1", 47777); 00037 server_socket->listen(); 00038 client_socket->connect( "127.0.0.1", 47777); 00039 00040 NET::TCPSocket::Handle tmp_handle; 00041 tmp_handle = server_socket->accept(); 00042 NET::TCPSocket::Handle handle; 00043 NET::TCPSocket::Handle& ref_handle = handle; 00044 handle = ref_handle; 00045 CPPUNIT_ASSERT( !handle ); 00046 CPPUNIT_ASSERT( tmp_handle ); 00047 handle = tmp_handle; 00048 CPPUNIT_ASSERT( handle ); 00049 CPPUNIT_ASSERT( !tmp_handle ); 00050 NET::TCPSocket session_socket(handle); 00051 CPPUNIT_ASSERT( !handle ); 00052 CPPUNIT_ASSERT_THROW( NET::TCPSocket bad_socket(handle), NET::SocketException ); 00053 client_socket->disconnect(); 00054 } 00055 00056 void testPeerStatus() 00057 { 00058 int ret; 00059 server_socket->bind( "127.0.0.1", 47777); 00060 server_socket->listen(); 00061 CPPUNIT_ASSERT( !server_socket->peerDisconnected() ); 00062 00063 client_socket->connect( "127.0.0.1", 47777); 00064 NET::TCPSocket::Handle handle = server_socket->accept(); 00065 CPPUNIT_ASSERT( handle ); 00066 NET::TCPSocket session_socket(handle); 00067 CPPUNIT_ASSERT( !handle ); 00068 00069 session_socket.send( send_msg, len); 00070 ret = client_socket->receive( recv_msg, len); 00071 CPPUNIT_ASSERT_EQUAL( len, ret ); 00072 CPPUNIT_ASSERT( !session_socket.peerDisconnected() ); 00073 CPPUNIT_ASSERT( std::memcmp( send_msg, recv_msg, len) == 0 ); 00074 00075 client_socket->disconnect(); 00076 ret = session_socket.send( send_msg, len); 00077 CPPUNIT_ASSERT_EQUAL( -1, ret ); 00078 CPPUNIT_ASSERT( session_socket.peerDisconnected() ); 00079 } 00080 }; 00081 00082 CPPUNIT_TEST_SUITE_REGISTRATION( TCPSocket_TEST );