testutil_unity.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 /* SPDX-License-Identifier: MPL-2.0 */
4 
5 #include "../include/zmq.h"
6 
7 #include "testutil.hpp"
8 
9 #include <unity.h>
10 
11 // Internal helper functions that are not intended to be directly called from
12 // tests. They must be declared in the header since they are used by macros.
13 
15  const char *msg_,
16  const char *expr_,
17  int line);
18 
20  int rc_, const char *msg_, const char *expr_, int line, bool zero_ = false);
21 
23  const char *msg_,
24  const char *expr_,
25  int line);
26 
28  int rc_, int expected_errno_, const char *msg_, const char *expr_, int line);
29 
31 // Macros extending Unity's TEST_ASSERT_* macros in a similar fashion.
33 
34 // For TEST_ASSERT_SUCCESS_ERRNO, TEST_ASSERT_SUCCESS_MESSAGE_ERRNO and
35 // TEST_ASSERT_FAILURE_ERRNO, 'expr' must be an expression evaluating
36 // to a result in the style of a libzmq API function, i.e. an integer which
37 // is non-negative in case of success, and -1 in case of a failure, and sets
38 // the value returned by zmq_errno () to the error code.
39 // TEST_ASSERT_SUCCESS_RAW_ERRNO and TEST_ASSERT_FAILURE_RAW_ERRNO are similar,
40 // but used with the native socket API functions, and expect that the error
41 // code can be retrieved in the native way (i.e. WSAGetLastError on Windows,
42 // and errno otherwise).
43 
44 // Asserts that the libzmq API 'expr' is successful. In case of a failure, the
45 // assertion message includes the literal 'expr', the error number as
46 // determined by zmq_errno(), and the additional 'msg'.
47 // In case of success, the result of the macro is the result of 'expr'.
48 #define TEST_ASSERT_SUCCESS_MESSAGE_ERRNO(expr, msg) \
49  test_assert_success_message_errno_helper (expr, msg, #expr, __LINE__)
50 
51 // Asserts that the libzmq API 'expr' is successful. In case of a failure, the
52 // assertion message includes the literal 'expr' and the error code.
53 // A typical use would be:
54 // TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (socket, endpoint));
55 // In case of success, the result of the macro is the result of 'expr'.
56 //
57 // If an additional message should be displayed in case of a failure, use
58 // TEST_ASSERT_SUCCESS_MESSAGE_ERRNO.
59 #define TEST_ASSERT_SUCCESS_ERRNO(expr) \
60  test_assert_success_message_errno_helper (expr, NULL, #expr, __LINE__)
61 
62 // Asserts that the socket API 'expr' is successful. In case of a failure, the
63 // assertion message includes the literal 'expr' and the error code.
64 // A typical use would be:
65 // TEST_ASSERT_SUCCESS_RAW_ERRNO (send (fd, buffer, 64, 0));
66 // In case of success, the result of the macro is the result of 'expr'.
67 // Success is strictly defined by a return value different from -1, as opposed
68 // to checking that it is 0, like TEST_ASSERT_FAILURE_RAW_ZERO_ERRNO does.
69 #define TEST_ASSERT_SUCCESS_RAW_ERRNO(expr) \
70  test_assert_success_message_raw_errno_helper (expr, NULL, #expr, __LINE__)
71 
72 // Asserts that the socket API 'expr' is successful. In case of a failure, the
73 // assertion message includes the literal 'expr' and the error code.
74 // A typical use would be:
75 // TEST_ASSERT_SUCCESS_RAW_ZERO_ERRNO (send (fd, buffer, 64, 0));
76 // In case of success, the result of the macro is the result of 'expr'.
77 // Success is strictly defined by a return value of 0, as opposed to checking
78 // that it is not -1, like TEST_ASSERT_FAILURE_RAW_ERRNO does.
79 #define TEST_ASSERT_SUCCESS_RAW_ZERO_ERRNO(expr) \
80  test_assert_success_message_raw_zero_errno_helper (expr, NULL, #expr, \
81  __LINE__)
82 
83 // Asserts that the socket API 'expr' is not successful, and the error code is
84 // 'error_code'. In case of an unexpected succces, or a failure with an
85 // unexpected error code, the assertion message includes the literal 'expr'
86 // and, in case of a failure, the actual error code.
87 #define TEST_ASSERT_FAILURE_RAW_ERRNO(error_code, expr) \
88  test_assert_failure_message_raw_errno_helper (expr, error_code, NULL, \
89  #expr, __LINE__)
90 
91 // Asserts that the libzmq API 'expr' is not successful, and the error code is
92 // 'error_code'. In case of an unexpected succces, or a failure with an
93 // unexpected error code, the assertion message includes the literal 'expr'
94 // and, in case of a failure, the actual error code.
95 #define TEST_ASSERT_FAILURE_ERRNO(error_code, expr) \
96  { \
97  int _rc = (expr); \
98  TEST_ASSERT_EQUAL_INT (-1, _rc); \
99  TEST_ASSERT_EQUAL_INT (error_code, errno); \
100  }
101 
103 // Utility functions for testing sending and receiving.
105 
106 // Sends a string via a libzmq socket, and expects the operation to be
107 // successful (the meaning of which depends on the socket type and configured
108 // options, and might include dropping the message). Otherwise, a Unity test
109 // assertion is triggered.
110 // 'socket_' must be the libzmq socket to use for sending.
111 // 'str_' must be a 0-terminated string.
112 // 'flags_' are as documented by the zmq_send function.
113 void send_string_expect_success (void *socket_, const char *str_, int flags_);
114 
115 // Receives a message via a libzmq socket, and expects the operation to be
116 // successful, and the message to be a given string. Otherwise, a Unity test
117 // assertion is triggered.
118 // 'socket_' must be the libzmq socket to use for receiving.
119 // 'str_' must be a 0-terminated string.
120 // 'flags_' are as documented by the zmq_recv function.
121 void recv_string_expect_success (void *socket_, const char *str_, int flags_);
122 
123 // Sends a byte array via a libzmq socket, and expects the operation to be
124 // successful (the meaning of which depends on the socket type and configured
125 // options, and might include dropping the message). Otherwise, a Unity test
126 // assertion is triggered.
127 // 'socket_' must be the libzmq socket to use for sending.
128 // 'array_' must be a C uint8_t array. The array size is automatically
129 // determined via template argument deduction.
130 // 'flags_' are as documented by the zmq_send function.
131 template <size_t SIZE>
132 void send_array_expect_success (void *socket_,
133  const uint8_t (&array_)[SIZE],
134  int flags_)
135 {
136  const int rc = zmq_send (socket_, array_, SIZE, flags_);
137  TEST_ASSERT_EQUAL_INT (static_cast<int> (SIZE), rc);
138 }
139 
140 // Receives a message via a libzmq socket, and expects the operation to be
141 // successful, and the message to be a given byte array. Otherwise, a Unity
142 // test assertion is triggered.
143 // 'socket_' must be the libzmq socket to use for receiving.
144 // 'array_' must be a C uint8_t array. The array size is automatically
145 // determined via template argument deduction.
146 // 'flags_' are as documented by the zmq_recv function.
147 template <size_t SIZE>
148 void recv_array_expect_success (void *socket_,
149  const uint8_t (&array_)[SIZE],
150  int flags_)
151 {
152  char buffer[255];
154  "recv_string_expect_success cannot be "
155  "used for strings longer than 255 "
156  "characters");
157 
158  const int rc = TEST_ASSERT_SUCCESS_ERRNO (
159  zmq_recv (socket_, buffer, sizeof (buffer), flags_));
160  TEST_ASSERT_EQUAL_INT (static_cast<int> (SIZE), rc);
161  TEST_ASSERT_EQUAL_UINT8_ARRAY (array_, buffer, SIZE);
162 }
163 
165 // Utility function for handling a test libzmq context, that is set up and
166 // torn down for each Unity test case, such that a clean context is available
167 // for each test case, and some consistency checks can be performed.
169 
170 // Use this is an test executable to perform a default setup and teardown of
171 // the test context, which is appropriate for many libzmq test cases.
172 #define SETUP_TEARDOWN_TESTCONTEXT \
173  void setUp () \
174  { \
175  setup_test_context (); \
176  } \
177  void tearDown () \
178  { \
179  teardown_test_context (); \
180  }
181 
182 // The maximum number of sockets that can be managed by the test context.
183 #define MAX_TEST_SOCKETS 128
184 
185 // Expected to be called during Unity's setUp function.
186 void setup_test_context ();
187 
188 // Returns the test context, e.g. to create sockets in another thread using
189 // zmq_socket, or set context options.
190 void *get_test_context ();
191 
192 // Expected to be called during Unity's tearDown function. Checks that all
193 // sockets created via test_context_socket have been properly closed using
194 // test_context_socket_close or test_context_socket_close_zero_linger, and generates a warning otherwise.
195 void teardown_test_context ();
196 
197 // Creates a libzmq socket on the test context, and tracks its lifecycle.
198 // You MUST use test_context_socket_close or test_context_socket_close_zero_linger
199 // to close a socket created via this function, otherwise undefined behaviour
200 // will result.
201 // CAUTION: this function is not thread-safe, and may only be used from the
202 // main thread.
203 void *test_context_socket (int type_);
204 
205 // Closes a socket created via test_context_socket.
206 // CAUTION: this function is not thread-safe, and may only be used from the
207 // main thread.
208 void *test_context_socket_close (void *socket_);
209 
210 // Closes a socket created via test_context_socket after setting its linger
211 // timeout to 0.
212 // CAUTION: this function is not thread-safe, and may only be used from the
213 // main thread.
214 void *test_context_socket_close_zero_linger (void *socket_);
215 
217 // Utility function for handling wildcard binds.
219 
220 // All function binds a socket to some wildcard address, and retrieve the bound
221 // endpoint via the ZMQ_LAST_ENDPOINT socket option to a given buffer.
222 // Triggers a Unity test assertion in case of a failure (including the buffer
223 // being too small for the resulting endpoint string).
224 
225 // Binds to an explicitly given (wildcard) address.
226 // TODO redesign such that this function is not necessary to be exposed, but
227 // the protocol to use is rather specified via an enum value
228 void test_bind (void *socket_,
229  const char *bind_address_,
230  char *my_endpoint_,
231  size_t len_);
232 
233 // Binds to a tcp endpoint using the ipv4 or ipv6 loopback wildcard address.
234 void bind_loopback (void *socket_, int ipv6_, char *my_endpoint_, size_t len_);
235 
236 typedef void (*bind_function_t) (void *socket_,
237  char *my_endpoint_,
238  size_t len_);
239 
240 // Binds to a tcp endpoint using the ipv4 loopback wildcard address.
241 void bind_loopback_ipv4 (void *socket_, char *my_endpoint_, size_t len_);
242 
243 // Binds to a tcp endpoint using the ipv6 loopback wildcard address.
244 void bind_loopback_ipv6 (void *socket_, char *my_endpoint_, size_t len_);
245 
246 // Binds to an ipc endpoint using the ipc wildcard address.
247 // Note that the returned address cannot be reused to bind a second socket.
248 // If you need to do this, use make_random_ipc_endpoint instead.
249 void bind_loopback_ipc (void *socket_, char *my_endpoint_, size_t len_);
250 
251 // Binds to an ipc endpoint using the tipc wildcard address.
252 void bind_loopback_tipc (void *socket_, char *my_endpoint_, size_t len_);
253 
254 #if defined(ZMQ_HAVE_IPC)
255 // utility function to create a random IPC endpoint, similar to what a ipc://*
256 // wildcard binding does, but in a way it can be reused for multiple binds
257 // TODO also add a len parameter here
258 void make_random_ipc_endpoint (char *out_endpoint_);
259 #endif
recv_array_expect_success
void recv_array_expect_success(void *socket_, const uint8_t(&array_)[SIZE], int flags_)
Definition: testutil_unity.hpp:148
send_string_expect_success
void send_string_expect_success(void *socket_, const char *str_, int flags_)
Definition: testutil_unity.cpp:94
test_context_socket_close
void * test_context_socket_close(void *socket_)
Definition: testutil_unity.cpp:208
TEST_ASSERT_EQUAL_UINT8_ARRAY
#define TEST_ASSERT_EQUAL_UINT8_ARRAY(expected, actual, num_elements)
Definition: unity.h:246
bind_loopback
void bind_loopback(void *socket_, int ipv6_, char *my_endpoint_, size_t len_)
Definition: testutil_unity.cpp:233
test_bind
void test_bind(void *socket_, const char *bind_address_, char *my_endpoint_, size_t len_)
Definition: testutil_unity.cpp:223
recv_string_expect_success
void recv_string_expect_success(void *socket_, const char *str_, int flags_)
Definition: testutil_unity.cpp:101
TEST_ASSERT_LESS_OR_EQUAL_MESSAGE
#define TEST_ASSERT_LESS_OR_EQUAL_MESSAGE(threshold, actual, message)
Definition: unity.h:392
test_assert_failure_message_raw_errno_helper
int test_assert_failure_message_raw_errno_helper(int rc_, int expected_errno_, const char *msg_, const char *expr_, int line)
Definition: testutil_unity.cpp:62
setup_test_context
void setup_test_context()
Definition: testutil_unity.cpp:179
testutil.hpp
bind_function_t
void(* bind_function_t)(void *socket_, char *my_endpoint_, size_t len_)
Definition: testutil_unity.hpp:236
send_array_expect_success
void send_array_expect_success(void *socket_, const uint8_t(&array_)[SIZE], int flags_)
Definition: testutil_unity.hpp:132
teardown_test_context
void teardown_test_context()
Definition: testutil_unity.cpp:189
get_test_context
void * get_test_context()
Definition: testutil_unity.cpp:184
test_assert_success_message_raw_zero_errno_helper
int test_assert_success_message_raw_zero_errno_helper(int rc_, const char *msg_, const char *expr_, int line)
Definition: testutil_unity.cpp:53
test_assert_success_message_errno_helper
int test_assert_success_message_errno_helper(int rc_, const char *msg_, const char *expr_, int line)
Definition: testutil_unity.cpp:13
buffer
Definition: buffer_processor.h:43
TEST_ASSERT_EQUAL_INT
#define TEST_ASSERT_EQUAL_INT(expected, actual)
Definition: unity.h:128
bind_loopback_ipv4
void bind_loopback_ipv4(void *socket_, char *my_endpoint_, size_t len_)
Definition: testutil_unity.cpp:246
void
typedef void(APIENTRY *GLDEBUGPROCARB)(GLenum source
bind_loopback_ipc
void bind_loopback_ipc(void *socket_, char *my_endpoint_, size_t len_)
Definition: testutil_unity.cpp:256
test_context_socket_close_zero_linger
void * test_context_socket_close_zero_linger(void *socket_)
Definition: testutil_unity.cpp:215
unity.h
bind_loopback_tipc
void bind_loopback_tipc(void *socket_, char *my_endpoint_, size_t len_)
Definition: testutil_unity.cpp:265
zmq_recv
ZMQ_EXPORT int zmq_recv(void *s_, void *buf_, size_t len_, int flags_)
Definition: zmq.cpp:487
TEST_ASSERT_SUCCESS_ERRNO
#define TEST_ASSERT_SUCCESS_ERRNO(expr)
Definition: testutil_unity.hpp:59
test_assert_success_message_raw_errno_helper
int test_assert_success_message_raw_errno_helper(int rc_, const char *msg_, const char *expr_, int line, bool zero_=false)
Definition: testutil_unity.cpp:31
zmq_send
ZMQ_EXPORT int zmq_send(void *s_, const void *buf_, size_t len_, int flags_)
Definition: zmq.cpp:377
test_context_socket
void * test_context_socket(int type_)
Definition: testutil_unity.cpp:200
bind_loopback_ipv6
void bind_loopback_ipv6(void *socket_, char *my_endpoint_, size_t len_)
Definition: testutil_unity.cpp:251


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