Coverage Report

Created: 2026-03-21 07:01

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libzmq/tests/testutil.hpp
Line
Count
Source
1
/* SPDX-License-Identifier: MPL-2.0 */
2
3
#ifdef _AIX
4
#define MSG_DONTWAIT MSG_NONBLOCK
5
#endif
6
7
#ifndef __TESTUTIL_HPP_INCLUDED__
8
#define __TESTUTIL_HPP_INCLUDED__
9
10
#if defined ZMQ_CUSTOM_PLATFORM_HPP
11
#include "platform.hpp"
12
#else
13
#include "../src/platform.hpp"
14
#endif
15
#include "../include/zmq.h"
16
#include "../src/stdint.hpp"
17
18
//  For AF_INET and IPPROTO_TCP
19
#if defined _WIN32
20
#include "../src/windows.hpp"
21
#if defined(__MINGW32__)
22
#include <unistd.h>
23
#endif
24
#else
25
#include <sys/socket.h>
26
#include <netinet/in.h>
27
#include <arpa/inet.h>
28
#include <unistd.h>
29
#include <stdlib.h>
30
#endif
31
32
//  This defines the settle time used in tests; raise this if we
33
//  get test failures on slower systems due to binds/connects not
34
//  settled. Tested to work reliably at 1 msec on a fast PC.
35
#define SETTLE_TIME 300 //  In msec
36
//  Commonly used buffer size for ZMQ_LAST_ENDPOINT
37
//  this used to be sizeof ("tcp://[::ffff:127.127.127.127]:65536"), but this
38
//  may be too short for ipc wildcard binds, e.g.
39
#define MAX_SOCKET_STRING 256
40
41
//  We need to test codepaths with non-random bind ports. List them here to
42
//  keep them unique, to allow parallel test runs.
43
#define ENDPOINT_0 "tcp://127.0.0.1:5555"
44
#define ENDPOINT_1 "tcp://127.0.0.1:5556"
45
#define ENDPOINT_2 "tcp://127.0.0.1:5557"
46
#define ENDPOINT_3 "tcp://127.0.0.1:5558"
47
#define ENDPOINT_4 "udp://127.0.0.1:5559"
48
#define ENDPOINT_5 "udp://127.0.0.1:5560"
49
#define PORT_6 5561
50
51
//  For tests that mock ZMTP
52
const uint8_t zmtp_greeting_null[64] = {
53
  0xff, 0, 0, 0, 0, 0, 0, 0, 0, 0x7f, 3, 0, 'N', 'U', 'L', 'L',
54
  0,    0, 0, 0, 0, 0, 0, 0, 0, 0,    0, 0, 0,   0,   0,   0,
55
  0,    0, 0, 0, 0, 0, 0, 0, 0, 0,    0, 0, 0,   0,   0,   0};
56
57
const uint8_t zmtp_greeting_curve[64] = {
58
  0xff, 0, 0, 0, 0, 0, 0, 0, 0, 0x7f, 3, 0, 'C', 'U', 'R', 'V',
59
  'E',  0, 0, 0, 0, 0, 0, 0, 0, 0,    0, 0, 0,   0,   0,   0,
60
  0,    0, 0, 0, 0, 0, 0, 0, 0, 0,    0, 0, 0,   0,   0,   0};
61
const uint8_t zmtp_ready_dealer[43] = {
62
  4,   41,  5,   'R', 'E', 'A', 'D', 'Y', 11,  'S', 'o', 'c', 'k', 'e', 't',
63
  '-', 'T', 'y', 'p', 'e', 0,   0,   0,   6,   'D', 'E', 'A', 'L', 'E', 'R',
64
  8,   'I', 'd', 'e', 'n', 't', 'i', 't', 'y', 0,   0,   0,   0};
65
const uint8_t zmtp_ready_xpub[28] = {
66
  4,   26,  5,   'R', 'E', 'A', 'D', 'Y', 11, 'S', 'o', 'c', 'k', 'e',
67
  't', '-', 'T', 'y', 'p', 'e', 0,   0,   0,  4,   'X', 'P', 'U', 'B'};
68
const uint8_t zmtp_ready_sub[27] = {
69
  4,   25,  5,   'R', 'E', 'A', 'D', 'Y', 11, 'S', 'o', 'c', 'k', 'e',
70
  't', '-', 'T', 'y', 'p', 'e', 0,   0,   0,  3,   'S', 'U', 'B'};
71
72
#undef NDEBUG
73
74
#ifndef MSG_NOSIGNAL
75
#define MSG_NOSIGNAL 0
76
#endif
77
78
// duplicated from fd.hpp
79
#ifdef ZMQ_HAVE_WINDOWS
80
#ifndef NOMINMAX
81
#define NOMINMAX // Macros min(a,b) and max(a,b)
82
#endif
83
84
#include <winsock2.h>
85
#include <ws2tcpip.h>
86
#include <stdexcept>
87
#define close closesocket
88
typedef int socket_size_t;
89
inline const char *as_setsockopt_opt_t (const void *opt)
90
{
91
    return static_cast<const char *> (opt);
92
}
93
#else
94
typedef size_t socket_size_t;
95
inline const void *as_setsockopt_opt_t (const void *opt_)
96
0
{
97
0
    return opt_;
98
0
}
99
#endif
100
101
// duplicated from fd.hpp
102
typedef zmq_fd_t fd_t;
103
#ifdef ZMQ_HAVE_WINDOWS
104
#if defined _MSC_VER && _MSC_VER <= 1400
105
enum
106
{
107
    retired_fd = (zmq_fd_t) (~0)
108
};
109
#else
110
enum
111
#if _MSC_VER >= 1800
112
  : zmq_fd_t
113
#endif
114
{
115
    retired_fd = INVALID_SOCKET
116
};
117
#endif
118
#else
119
enum
120
{
121
    retired_fd = -1
122
};
123
#endif
124
125
//  In MSVC prior to v14, snprintf is not available
126
//  The closest implementation is the _snprintf_s function
127
#if defined _MSC_VER && _MSC_VER < 1900
128
#define snprintf(buffer_, count_, format_, ...)                                \
129
    _snprintf_s (buffer_, count_, _TRUNCATE, format_, __VA_ARGS__)
130
#endif
131
132
#define LIBZMQ_UNUSED(object) (void) object
133
134
//  Bounce a message from client to server and back
135
//  For REQ/REP or DEALER/DEALER pairs only
136
void bounce (void *server_, void *client_);
137
138
//  Same as bounce, but expect messages to never arrive
139
//  for security or subscriber reasons.
140
void expect_bounce_fail (void *server_, void *client_);
141
142
//  Receive 0MQ string from socket and convert into C string
143
//  Caller must free returned string. Returns NULL if the context
144
//  is being terminated.
145
char *s_recv (void *socket_);
146
147
bool streq (const char *lhs, const char *rhs);
148
bool strneq (const char *lhs, const char *rhs);
149
150
extern const char *SEQ_END;
151
152
//  Sends a message composed of frames that are C strings or null frames.
153
//  The list must be terminated by SEQ_END.
154
//  Example: s_send_seq (req, "ABC", 0, "DEF", SEQ_END);
155
156
void s_send_seq (void *socket_, ...);
157
158
//  Receives message a number of frames long and checks that the frames have
159
//  the given data which can be either C strings or 0 for a null frame.
160
//  The list must be terminated by SEQ_END.
161
//  Example: s_recv_seq (rep, "ABC", 0, "DEF", SEQ_END);
162
163
void s_recv_seq (void *socket_, ...);
164
165
166
//  Sets a zero linger period on a socket and closes it.
167
void close_zero_linger (void *socket_);
168
169
//  Setups the test environment. Must be called at the beginning of each test
170
//  executable. On POSIX systems, it sets an alarm to the specified number of
171
//  seconds, after which the test will be killed. Set to 0 to disable this
172
//  timeout.
173
void setup_test_environment (int timeout_seconds_ = 60);
174
175
//  Provide portable millisecond sleep
176
//  http://www.cplusplus.com/forum/unices/60161/
177
//  http://en.cppreference.com/w/cpp/thread/sleep_for
178
179
void msleep (int milliseconds_);
180
181
// check if IPv6 is available (0/false if not, 1/true if it is)
182
// only way to reliably check is to actually open a socket and try to bind it
183
int is_ipv6_available (void);
184
185
// check if tipc is available (0/false if not, 1/true if it is)
186
// only way to reliably check is to actually open a socket and try to bind it
187
// as it depends on a non-default kernel module to be already loaded
188
int is_tipc_available (void);
189
190
//  Wrapper around 'inet_pton' for systems that don't support it (e.g. Windows
191
//  XP)
192
int test_inet_pton (int af_, const char *src_, void *dst_);
193
194
//  Binds an ipv4 BSD socket to an ephemeral port, returns the compiled sockaddr
195
struct sockaddr_in bind_bsd_socket (int socket);
196
197
//  Some custom definitions in addition to IPPROTO_TCP and IPPROTO_UDP
198
0
#define IPPROTO_WS 10000
199
0
#define IPPROTO_WSS 10001
200
201
//  Connects a BSD socket to the ZMQ endpoint. Works with ipv4/ipv6/unix.
202
fd_t connect_socket (const char *endpoint_,
203
                     const int af_ = AF_INET,
204
                     const int protocol_ = IPPROTO_TCP);
205
206
//  Binds a BSD socket to an ephemeral port, returns the file descriptor.
207
//  The resulting ZMQ endpoint will be stored in my_endpoint, including the protocol
208
//  prefix, so ensure it is writable and of appropriate size.
209
//  Works with ipv4/ipv6/unix. With unix sockets address_/port_ can be empty and
210
//  my_endpoint_ will contain a random path.
211
fd_t bind_socket_resolve_port (const char *address_,
212
                               const char *port_,
213
                               char *my_endpoint_,
214
                               const int af_ = AF_INET,
215
                               const int protocol_ = IPPROTO_TCP);
216
217
int fuzzer_corpus_encode (const char *filename,
218
                          uint8_t ***data,
219
                          size_t **len,
220
                          size_t *num_cases);
221
222
#endif