test_srcfd.cpp
Go to the documentation of this file.
1 /* SPDX-License-Identifier: MPL-2.0 */
2 
3 #include "testutil.hpp"
4 #include "testutil_unity.hpp"
5 
6 #include <string.h>
7 
9 
10 #define MSG_SIZE 20
11 
12 #ifdef _WIN32
13 #include <winsock2.h>
14 #include <ws2tcpip.h>
15 #else
16 #include <sys/types.h>
17 #include <sys/socket.h>
18 #include <netdb.h>
19 #endif
20 
21 void test_srcfd ()
22 {
24 
25  // Create the infrastructure
26 
29 
31 
33 
34  char tmp[MSG_SIZE];
35  memset (tmp, 0, MSG_SIZE);
36  zmq_send (req, tmp, MSG_SIZE, 0);
37 
38  zmq_msg_t msg;
40 
41  zmq_recvmsg (rep, &msg, 0);
43 
44  // get the messages source file descriptor
45  int src_fd = zmq_msg_get (&msg, ZMQ_SRCFD);
46  TEST_ASSERT_GREATER_OR_EQUAL (0, src_fd);
47 
49 
50  // get the remote endpoint
51  struct sockaddr_storage ss;
52 #ifdef ZMQ_HAVE_HPUX
53  int addrlen = sizeof ss;
54 #else
55  socklen_t addrlen = sizeof ss;
56 #endif
58  getpeername (src_fd, (struct sockaddr *) &ss, &addrlen));
59 
60  char host[NI_MAXHOST];
61  TEST_ASSERT_SUCCESS_RAW_ERRNO (getnameinfo ((struct sockaddr *) &ss,
62  addrlen, host, sizeof host,
63  NULL, 0, NI_NUMERICHOST));
64 
65  // assert it is localhost which connected
66  TEST_ASSERT_EQUAL_STRING ("127.0.0.1", host);
67 
70 
71  // sleep a bit for the socket to be freed
73 
74  // getting name from closed socket will fail
75 #ifdef ZMQ_HAVE_WINDOWS
76  const int expected_errno = WSAENOTSOCK;
77 #else
78  const int expected_errno = EBADF;
79 #endif
81  expected_errno, getpeername (src_fd, (struct sockaddr *) &ss, &addrlen));
82 }
83 
84 int main ()
85 {
87 
88  UNITY_BEGIN ();
90  return UNITY_END ();
91 }
TEST_ASSERT_EQUAL_STRING
#define TEST_ASSERT_EQUAL_STRING(expected, actual)
Definition: unity.h:235
NULL
NULL
Definition: test_security_zap.cpp:405
UNITY_END
return UNITY_END()
msleep
void msleep(int milliseconds_)
Definition: testutil.cpp:227
RUN_TEST
#define RUN_TEST(func)
Definition: unity_internals.h:615
TEST_ASSERT_EQUAL_UINT
#define TEST_ASSERT_EQUAL_UINT(expected, actual)
Definition: unity.h:135
zmq_msg_get
ZMQ_EXPORT int zmq_msg_get(const zmq_msg_t *msg_, int property_)
Definition: zmq.cpp:657
SETUP_TEARDOWN_TESTCONTEXT
#define SETUP_TEARDOWN_TESTCONTEXT
Definition: testutil_unity.hpp:172
bind_loopback_ipv4
void bind_loopback_ipv4(void *socket_, char *my_endpoint_, size_t len_)
Definition: testutil_unity.cpp:246
rep
void * rep[services]
Definition: test_req_relaxed.cpp:11
ZMQ_REQ
#define ZMQ_REQ
Definition: zmq.h:261
zmq_connect
ZMQ_EXPORT int zmq_connect(void *s_, const char *addr_)
Definition: zmq.cpp:307
zmq_msg_size
ZMQ_EXPORT size_t zmq_msg_size(const zmq_msg_t *msg_)
Definition: zmq.cpp:647
testutil_unity.hpp
NI_MAXHOST
#define NI_MAXHOST
Definition: vxworks/platform.hpp:305
ZMQ_SRCFD
#define ZMQ_SRCFD
Definition: zmq.h:384
TEST_ASSERT_SUCCESS_RAW_ERRNO
#define TEST_ASSERT_SUCCESS_RAW_ERRNO(expr)
Definition: testutil_unity.hpp:69
testutil.hpp
zmq_msg_t
Definition: zmq.h:218
ZMQ_REP
#define ZMQ_REP
Definition: zmq.h:262
EBADF
#define EBADF
Definition: errno.hpp:12
my_endpoint
char my_endpoint[MAX_SOCKET_STRING]
Definition: test_security_curve.cpp:31
MAX_SOCKET_STRING
#define MAX_SOCKET_STRING
Definition: libzmq/tests/testutil.hpp:35
TEST_ASSERT_FAILURE_RAW_ERRNO
#define TEST_ASSERT_FAILURE_RAW_ERRNO(error_code, expr)
Definition: testutil_unity.hpp:87
test_context_socket
void * test_context_socket(int type_)
Definition: testutil_unity.cpp:200
test_srcfd
void test_srcfd()
Definition: test_srcfd.cpp:21
SETTLE_TIME
#define SETTLE_TIME
Definition: libzmq/tests/testutil.hpp:31
zmq_msg_init
ZMQ_EXPORT int zmq_msg_init(zmq_msg_t *msg_)
Definition: zmq.cpp:587
zmq_recvmsg
ZMQ_EXPORT int zmq_recvmsg(void *s_, zmq_msg_t *msg_, int flags_)
Definition: zmq.cpp:481
req
void * req
Definition: test_req_relaxed.cpp:10
setup_test_environment
void setup_test_environment(int timeout_seconds_)
Definition: testutil.cpp:201
UNITY_BEGIN
UNITY_BEGIN()
TEST_ASSERT_GREATER_OR_EQUAL
#define TEST_ASSERT_GREATER_OR_EQUAL(threshold, actual)
Definition: unity.h:184
zmq_send
ZMQ_EXPORT int zmq_send(void *s_, const void *buf_, size_t len_, int flags_)
Definition: zmq.cpp:377
main
int main()
Definition: test_srcfd.cpp:84
test_context_socket_close
void * test_context_socket_close(void *socket_)
Definition: testutil_unity.cpp:208
zmq_msg_close
ZMQ_EXPORT int zmq_msg_close(zmq_msg_t *msg_)
Definition: zmq.cpp:625
TEST_ASSERT_SUCCESS_ERRNO
#define TEST_ASSERT_SUCCESS_ERRNO(expr)
Definition: proxy_thr.cpp:47
MSG_SIZE
#define MSG_SIZE
Definition: test_srcfd.cpp:10


libaditof
Author(s):
autogenerated on Wed May 21 2025 02:06:59