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 | // This defines the settle time used in tests; raise this if we |
42 | // get test failures on slower systems due to binds/connects not |
43 | // settled. Tested to work reliably at 1 msec on a fast PC. |
44 | #define SETTLE_TIME 300 // In msec |
45 | // Commonly used buffer size for ZMQ_LAST_ENDPOINT |
46 | // this used to be sizeof ("tcp://[::ffff:127.127.127.127]:65536"), but this |
47 | // may be too short for ipc wildcard binds, e.g. |
48 | #define MAX_SOCKET_STRING 256 |
49 | |
50 | // We need to test codepaths with non-random bind ports. List them here to |
51 | // keep them unique, to allow parallel test runs. |
52 | #define ENDPOINT_0 "tcp://127.0.0.1:5555" |
53 | #define ENDPOINT_1 "tcp://127.0.0.1:5556" |
54 | #define ENDPOINT_2 "tcp://127.0.0.1:5557" |
55 | #define ENDPOINT_3 "tcp://127.0.0.1:5558" |
56 | #define ENDPOINT_4 "udp://127.0.0.1:5559" |
57 | #define ENDPOINT_5 "udp://127.0.0.1:5560" |
58 | #define PORT_6 5561 |
59 | |
60 | #undef NDEBUG |
61 | |
62 | // duplicated from fd.hpp |
63 | #ifdef ZMQ_HAVE_WINDOWS |
64 | #include <winsock2.h> |
65 | #include <ws2tcpip.h> |
66 | #include <stdexcept> |
67 | #define close closesocket |
68 | typedef int socket_size_t; |
69 | inline const char *as_setsockopt_opt_t (const void *opt) |
70 | { |
71 | return static_cast<const char *> (opt); |
72 | } |
73 | #if defined _MSC_VER && _MSC_VER <= 1400 |
74 | typedef UINT_PTR fd_t; |
75 | enum |
76 | { |
77 | retired_fd = (fd_t) (~0) |
78 | }; |
79 | #else |
80 | typedef SOCKET fd_t; |
81 | enum |
82 | { |
83 | retired_fd = (fd_t) INVALID_SOCKET |
84 | }; |
85 | #endif |
86 | #else |
87 | typedef size_t socket_size_t; |
88 | inline const void *as_setsockopt_opt_t (const void *opt_) |
89 | { |
90 | return opt_; |
91 | } |
92 | typedef int fd_t; |
93 | enum |
94 | { |
95 | retired_fd = -1 |
96 | }; |
97 | #endif |
98 | |
99 | // In MSVC prior to v14, snprintf is not available |
100 | // The closest implementation is the _snprintf_s function |
101 | #if defined _MSC_VER && _MSC_VER < 1900 |
102 | #define snprintf(buffer_, count_, format_, ...) \ |
103 | _snprintf_s (buffer_, count_, _TRUNCATE, format_, __VA_ARGS__) |
104 | #endif |
105 | |
106 | #define LIBZMQ_UNUSED(object) (void) object |
107 | |
108 | // Bounce a message from client to server and back |
109 | // For REQ/REP or DEALER/DEALER pairs only |
110 | void bounce (void *server_, void *client_); |
111 | |
112 | // Same as bounce, but expect messages to never arrive |
113 | // for security or subscriber reasons. |
114 | void expect_bounce_fail (void *server_, void *client_); |
115 | |
116 | // Receive 0MQ string from socket and convert into C string |
117 | // Caller must free returned string. Returns NULL if the context |
118 | // is being terminated. |
119 | char *s_recv (void *socket_); |
120 | |
121 | bool streq (const char *lhs, const char *rhs); |
122 | bool strneq (const char *lhs, const char *rhs); |
123 | |
124 | extern const char *SEQ_END; |
125 | |
126 | // Sends a message composed of frames that are C strings or null frames. |
127 | // The list must be terminated by SEQ_END. |
128 | // Example: s_send_seq (req, "ABC", 0, "DEF", SEQ_END); |
129 | |
130 | void s_send_seq (void *socket_, ...); |
131 | |
132 | // Receives message a number of frames long and checks that the frames have |
133 | // the given data which can be either C strings or 0 for a null frame. |
134 | // The list must be terminated by SEQ_END. |
135 | // Example: s_recv_seq (rep, "ABC", 0, "DEF", SEQ_END); |
136 | |
137 | void s_recv_seq (void *socket_, ...); |
138 | |
139 | |
140 | // Sets a zero linger period on a socket and closes it. |
141 | void close_zero_linger (void *socket_); |
142 | |
143 | void setup_test_environment (void); |
144 | |
145 | // Provide portable millisecond sleep |
146 | // http://www.cplusplus.com/forum/unices/60161/ |
147 | // http://en.cppreference.com/w/cpp/thread/sleep_for |
148 | |
149 | void msleep (int milliseconds_); |
150 | |
151 | // check if IPv6 is available (0/false if not, 1/true if it is) |
152 | // only way to reliably check is to actually open a socket and try to bind it |
153 | int is_ipv6_available (void); |
154 | |
155 | // check if tipc is available (0/false if not, 1/true if it is) |
156 | // only way to reliably check is to actually open a socket and try to bind it |
157 | // as it depends on a non-default kernel module to be already loaded |
158 | int is_tipc_available (void); |
159 | |
160 | // Wrapper around 'inet_pton' for systems that don't support it (e.g. Windows |
161 | // XP) |
162 | int test_inet_pton (int af_, const char *src_, void *dst_); |
163 | |
164 | // Binds an ipv4 BSD socket to an ephemeral port, returns the compiled sockaddr |
165 | struct sockaddr_in bind_bsd_socket (int socket); |
166 | |
167 | #endif |
168 | |