simple-socket library 1.2.0
UDPSocket_TEST.cpp
Go to the documentation of this file.
1#include <cppunit/extensions/HelperMacros.h>
2#include "../UDPSocket.h"
3
4#include <cstring>
5
6static const char send_msg[] = "The quick brown fox jumps over the lazy dog";
7static char recv_msg[sizeof(send_msg)];
8static const int len = sizeof(send_msg);
9
10class UDPSocket_TEST : public CppUnit::TestFixture
11{
12 CPPUNIT_TEST_SUITE( UDPSocket_TEST );
13 CPPUNIT_TEST( testPeerStatus );
14 CPPUNIT_TEST( testMulticast );
15 CPPUNIT_TEST( testSendTo );
16 CPPUNIT_TEST_SUITE_END();
17
18private:
19 NET::UDPSocket* send_socket;
20 NET::UDPSocket* recv_socket;
21
22public:
23 void setUp()
24 {
25 send_socket = new NET::UDPSocket();
26 recv_socket = new NET::UDPSocket();
27 }
28
29 void tearDown()
30 {
31 delete send_socket;
32 delete recv_socket;
33 }
34
35 void testPeerStatus()
36 {
37 send_socket->connect( "127.0.0.1", 65001);
38 CPPUNIT_ASSERT( !send_socket->peerDisconnected() );
39 send_socket->send( "", 1); // ok, no icmp received yet
40 send_socket->send( "", 1); // icmp received, send fails
41 CPPUNIT_ASSERT( send_socket->peerDisconnected() );
42 }
43
44 void testMulticast()
45 {
46 int ret;
47 send_socket->setMulticastTTL(0);
48 send_socket->connect( "224.40.0.1", 47777);
49 recv_socket->bind(47777);
50 recv_socket->joinGroup("224.40.0.1");
51
52 ret = send_socket->send( send_msg, len);
53 CPPUNIT_ASSERT_EQUAL( len, ret );
54 ret = recv_socket->receive( recv_msg, len);
55 CPPUNIT_ASSERT_EQUAL( len, ret );
56 CPPUNIT_ASSERT( std::memcmp( send_msg, recv_msg, len) == 0 );
57
58 recv_socket->leaveGroup("224.40.0.1");
59 ret = send_socket->send( send_msg, len);
60 CPPUNIT_ASSERT_EQUAL( len, ret );
61
62 ret = recv_socket->timedReceive( recv_msg, len, 1);
63 CPPUNIT_ASSERT_EQUAL( 0, ret );
64 }
65
66 void testSendTo()
67 {
68 int ret;
69 std::string source;
70 unsigned short port = 0;
71 send_socket->bind(47776);
72 recv_socket->bind(47777);
73 send_socket->sendTo( send_msg, len, "127.0.0.1", 47777);
74
75 ret = recv_socket->receiveFrom( recv_msg, len, source, port);
76 CPPUNIT_ASSERT_EQUAL( len, ret );
77 CPPUNIT_ASSERT_EQUAL( (unsigned short)47776, port );
78 CPPUNIT_ASSERT_EQUAL( std::string("127.0.0.1"), source );
79
80 ret = recv_socket->timedReceiveFrom( recv_msg, len, source, port, 10);
81 CPPUNIT_ASSERT_EQUAL( 0, ret );
82 CPPUNIT_ASSERT_EQUAL( (unsigned short)47776, port );
83 CPPUNIT_ASSERT_EQUAL( std::string("127.0.0.1"), source );
84 CPPUNIT_ASSERT( std::memcmp( send_msg, recv_msg, len) == 0 );
85 }
86};
87
CPPUNIT_TEST_SUITE_REGISTRATION(UDPSocket_TEST)
static NET::can_frame recv_msg
static NET::can_frame send_msg
static const int len
void bind(unsigned short localPort=0)
void connect(std::string_view foreignAddress, unsigned short foreignPort)
establish a connection with the given foreign address and port
int timedReceive(void *buffer, size_t len, int timeout)
receive data from a bound socket, return after the given timespan
int send(const void *buffer, size_t len)
send data through a connected socket
bool peerDisconnected() const
returns whether a peer disconnected
int receive(void *buffer, size_t len)
receive data from a bound socket
UDP socket class.
Definition UDPSocket.h:10
int timedReceiveFrom(void *buffer, size_t len, std::string &sourceAddress, unsigned short &sourcePort, int timeout)
Definition UDPSocket.cpp:72
int receiveFrom(void *buffer, size_t len, std::string &sourceAddress, unsigned short &sourcePort)
Definition UDPSocket.cpp:57
void setMulticastTTL(unsigned char multicastTTL)
Definition UDPSocket.cpp:92
void joinGroup(const std::string &multicastGroup)
void sendTo(const void *buffer, size_t len, std::string_view foreignAddress, unsigned short foreignPort)
Definition UDPSocket.cpp:45
void leaveGroup(const std::string &multicastGroup)