test_pair_tcp.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 
8 #if defined _WIN32
9 #include "../src/windows.hpp"
10 #endif
11 
13 
14 typedef void (*extra_func_t) (void *socket_);
15 
16 #ifdef ZMQ_BUILD_DRAFT
17 void set_sockopt_fastpath (void *socket)
18 {
19  int value = 1;
20  int rc =
21  zmq_setsockopt (socket, ZMQ_LOOPBACK_FASTPATH, &value, sizeof value);
22  assert (rc == 0);
23 }
24 #endif
25 
26 void test_pair_tcp (extra_func_t extra_func_ = NULL)
27 {
29 
30  if (extra_func_)
31  extra_func_ (sb);
32 
35 
37  if (extra_func_)
38  extra_func_ (sc);
39 
41 
42  bounce (sb, sc);
43 
46 }
47 
49 {
50  test_pair_tcp ();
51 }
52 
54 {
55  // all other tcp test cases bind to a loopback wildcard address, then
56  // retrieve the bound endpoint, which is numerical, and use that to
57  // connect. this test cases specifically uses "localhost" to connect
58  // to ensure that names are correctly resolved
60 
61  char bound_endpoint[MAX_SOCKET_STRING];
62  bind_loopback_ipv4 (sb, bound_endpoint, sizeof bound_endpoint);
63 
64  // extract the bound port number
65  const char *pos = strrchr (bound_endpoint, ':');
67  const char connect_endpoint_prefix[] = "tcp://localhost";
68  char connect_endpoint[MAX_SOCKET_STRING];
69  strcpy (connect_endpoint, connect_endpoint_prefix);
70  strcat (connect_endpoint, pos);
71 
73 
74  TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (sc, connect_endpoint));
75 
76  bounce (sb, sc);
77 
80 }
81 
82 
83 #ifdef ZMQ_BUILD_DRAFT
84 void test_pair_tcp_fastpath ()
85 {
86  test_pair_tcp (set_sockopt_fastpath);
87 }
88 #endif
89 
90 #ifdef _WIN32
91 void test_io_completion_port ()
92 {
93  void *const s = test_context_socket (ZMQ_PAIR);
94  SOCKET fd;
95  size_t fd_size = sizeof fd;
96  TEST_ASSERT_SUCCESS_ERRNO (zmq_getsockopt (s, ZMQ_FD, &fd, &fd_size));
97 
98  ::WSAPROTOCOL_INFO pi;
100  ::WSADuplicateSocket (fd, ::GetCurrentProcessId (), &pi));
101  const SOCKET socket = ::WSASocket (pi.iAddressFamily /*AF_INET*/,
102  pi.iSocketType /*SOCK_STREAM*/,
103  pi.iProtocol /*IPPROTO_TCP*/, &pi, 0, 0);
104 
105  const HANDLE iocp =
106  ::CreateIoCompletionPort (INVALID_HANDLE_VALUE, NULL, 0, 0);
107  TEST_ASSERT_NOT_EQUAL (NULL, iocp);
108  const HANDLE res =
109  ::CreateIoCompletionPort (reinterpret_cast<HANDLE> (socket), iocp, 0, 0);
111 
113  TEST_ASSERT_TRUE (CloseHandle (iocp));
114 
116 }
117 #endif
118 
119 int main ()
120 {
122 
123  UNITY_BEGIN ();
126 #ifdef ZMQ_BUILD_DRAFT
127  RUN_TEST (test_pair_tcp_fastpath);
128 #endif
129 #ifdef _WIN32
130  RUN_TEST (test_io_completion_port);
131 #endif
132 
133  return UNITY_END ();
134 }
bounce
static void bounce(void *socket_)
Definition: test_req_relaxed.cpp:50
closesocket
#define closesocket
Definition: unittest_poller.cpp:13
NULL
NULL
Definition: test_security_zap.cpp:405
UNITY_END
return UNITY_END()
TEST_ASSERT_TRUE
#define TEST_ASSERT_TRUE(condition)
Definition: unity.h:121
s
XmlRpcServer s
RUN_TEST
#define RUN_TEST(func)
Definition: unity_internals.h:615
test_pair_tcp_connect_by_name
void test_pair_tcp_connect_by_name()
Definition: test_pair_tcp.cpp:53
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
ZMQ_LOOPBACK_FASTPATH
#define ZMQ_LOOPBACK_FASTPATH
Definition: zmq_draft.h:26
zmq_connect
ZMQ_EXPORT int zmq_connect(void *s_, const char *addr_)
Definition: zmq.cpp:307
TEST_ASSERT_NOT_EQUAL
#define TEST_ASSERT_NOT_EQUAL(expected, actual)
Definition: unity.h:134
test_pair_tcp
void test_pair_tcp(extra_func_t extra_func_=NULL)
Definition: test_pair_tcp.cpp:26
testutil_unity.hpp
zmq_setsockopt
ZMQ_EXPORT int zmq_setsockopt(void *s_, int option_, const void *optval_, size_t optvallen_)
Definition: zmq.cpp:250
main
int main()
Definition: test_pair_tcp.cpp:119
sb
void * sb
Definition: test_channel.cpp:8
TEST_ASSERT_SUCCESS_RAW_ERRNO
#define TEST_ASSERT_SUCCESS_RAW_ERRNO(expr)
Definition: testutil_unity.hpp:69
testutil.hpp
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_pair_tcp_regular
void test_pair_tcp_regular()
Definition: test_pair_tcp.cpp:48
sc
void * sc
Definition: test_channel.cpp:9
test_context_socket
void * test_context_socket(int type_)
Definition: testutil_unity.cpp:200
void
typedef void(APIENTRY *GLDEBUGPROCARB)(GLenum source
ZMQ_PAIR
#define ZMQ_PAIR
Definition: zmq.h:258
setup_test_environment
void setup_test_environment(int timeout_seconds_)
Definition: testutil.cpp:201
UNITY_BEGIN
UNITY_BEGIN()
HANDLE
void * HANDLE
Definition: wepoll.c:70
extra_func_t
SETUP_TEARDOWN_TESTCONTEXT typedef void(* extra_func_t)(void *socket_)
Definition: test_pair_tcp.cpp:14
ZMQ_FD
#define ZMQ_FD
Definition: zmq.h:285
value
GLsizei const GLfloat * value
Definition: glcorearb.h:3093
test_context_socket_close
void * test_context_socket_close(void *socket_)
Definition: testutil_unity.cpp:208
SOCKET
uintptr_t SOCKET
Definition: wepoll.c:71
TEST_ASSERT_NOT_NULL
#define TEST_ASSERT_NOT_NULL(pointer)
Definition: unity.h:125
TEST_ASSERT_SUCCESS_ERRNO
#define TEST_ASSERT_SUCCESS_ERRNO(expr)
Definition: proxy_thr.cpp:47
zmq_getsockopt
ZMQ_EXPORT int zmq_getsockopt(void *s_, int option_, void *optval_, size_t *optvallen_)
Definition: zmq.cpp:261


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