testutil.hpp 8.3 KB

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