simple-socket library 1.2.0
UnixDatagramSocket_TEST.cpp
Go to the documentation of this file.
1#include <cppunit/extensions/HelperMacros.h>
3
4#include <unistd.h>
5#include <cstring>
6
7static const char sock_file[] = "/tmp/simple-socket_test.sock";
8static const char send_msg[] = "The quick brown fox jumps over the lazy dog";
9static char recv_msg[sizeof(send_msg)];
10static const int len = sizeof(send_msg);
11
12class UnixDatagramSocket_TEST : public CppUnit::TestFixture
13{
14 CPPUNIT_TEST_SUITE( UnixDatagramSocket_TEST );
15 CPPUNIT_TEST( testSendTo );
16 CPPUNIT_TEST_SUITE_END();
17
18private:
19 NET::UnixDatagramSocket* send_socket;
20 NET::UnixDatagramSocket* recv_socket;
21
22public:
23 void setUp()
24 {
25 send_socket = new NET::UnixDatagramSocket();
26 recv_socket = new NET::UnixDatagramSocket();
27 }
28
29 void tearDown()
30 {
31 delete send_socket;
32 delete recv_socket;
33
34 // remove this if socket can handle it
35 ::unlink(sock_file);
36 }
37
38 void testSendTo()
39 {
40 int ret;
41 std::string source;
42 send_socket->bind(sock_file);
43 recv_socket->bind(sock_file);
44 send_socket->sendTo( send_msg, len, sock_file);
45
46 ret = recv_socket->receiveFrom( recv_msg, len, source);
47 CPPUNIT_ASSERT_EQUAL( len, ret );
48 CPPUNIT_ASSERT_EQUAL( std::string(sock_file), source );
49
50 ret = recv_socket->timedReceiveFrom( recv_msg, len, source, 10);
51 CPPUNIT_ASSERT_EQUAL( 0, ret );
52 CPPUNIT_ASSERT_EQUAL( std::string(sock_file), source );
53 CPPUNIT_ASSERT( std::memcmp(send_msg, recv_msg, len) == 0 );
54 }
55};
56
57CPPUNIT_TEST_SUITE_REGISTRATION( UnixDatagramSocket_TEST );
static NET::can_frame recv_msg
static NET::can_frame send_msg
static const int len
static const char sock_file[]
CPPUNIT_TEST_SUITE_REGISTRATION(UnixDatagramSocket_TEST)
Unix datagram socket class.
void sendTo(const void *buffer, size_t len, std::string_view foreignPath)
int timedReceiveFrom(void *buffer, size_t len, std::string &sourcePath, int timeout)
int receiveFrom(void *buffer, size_t len, std::string &sourcePath)
void bind(std::string_view localPath)