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