testutil.hpp 5.18 KB
Newer Older
Guido Goldstein's avatar
Guido Goldstein committed
1
/*
2
    Copyright (c) 2007-2017 Contributors as noted in the AUTHORS file
Guido Goldstein's avatar
Guido Goldstein committed
3

4
    This file is part of libzmq, the ZeroMQ core engine in C++.
Guido Goldstein's avatar
Guido Goldstein committed
5

6 7 8
    libzmq is free software; you can redistribute it and/or modify it under
    the terms of the GNU Lesser General Public License (LGPL) as published
    by the Free Software Foundation; either version 3 of the License, or
Guido Goldstein's avatar
Guido Goldstein committed
9 10
    (at your option) any later version.

11 12 13 14 15 16 17 18 19 20 21 22 23 24
    As a special exception, the Contributors give you permission to link
    this library with independent modules to produce an executable,
    regardless of the license terms of these independent modules, and to
    copy and distribute the resulting executable under terms of your choice,
    provided that you also meet, for each linked independent module, the
    terms and conditions of the license of that module. An independent
    module is a module which is not derived from or based on this library.
    If you modify this library, you must extend this exception to your
    version of the library.

    libzmq is distributed in the hope that it will be useful, but WITHOUT
    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
    License for more details.
Guido Goldstein's avatar
Guido Goldstein committed
25

26
    You should have received a copy of the GNU Lesser General Public License
Guido Goldstein's avatar
Guido Goldstein committed
27 28 29
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

30 31
#ifndef __TESTUTIL_HPP_INCLUDED__
#define __TESTUTIL_HPP_INCLUDED__
Guido Goldstein's avatar
Guido Goldstein committed
32

33
#if defined ZMQ_CUSTOM_PLATFORM_HPP
34
#include "platform.hpp"
35
#else
36
#include "../src/platform.hpp"
37
#endif
38 39
#include "../include/zmq.h"
#include "../src/stdint.hpp"
40

41 42 43
//  This defines the settle time used in tests; raise this if we
//  get test failures on slower systems due to binds/connects not
//  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 47 48
//  this used to be sizeof ("tcp://[::ffff:127.127.127.127]:65536"), but this
//  may be too short for ipc wildcard binds, e.g.
#define MAX_SOCKET_STRING 256
49 50 51 52 53 54 55 56 57

//  We need to test codepaths with non-random bind ports. List them here to
//  keep them unique, to allow parallel test runs.
#define ENDPOINT_0 "tcp://127.0.0.1:5555"
#define ENDPOINT_1 "tcp://127.0.0.1:5556"
#define ENDPOINT_2 "tcp://127.0.0.1:5557"
#define ENDPOINT_3 "tcp://127.0.0.1:5558"
#define ENDPOINT_4 "udp://127.0.0.1:5559"
#define ENDPOINT_5 "udp://127.0.0.1:5560"
58
#define PORT_6 5561
59

60
#undef NDEBUG
61

62 63
// duplicated from fd.hpp
#ifdef ZMQ_HAVE_WINDOWS
64 65 66
#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdexcept>
67 68
#define close closesocket
typedef int socket_size_t;
69
inline const char *as_setsockopt_opt_t (const void *opt)
70 71 72
{
    return static_cast<const char *> (opt);
}
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
#if defined _MSC_VER && _MSC_VER <= 1400
typedef UINT_PTR fd_t;
enum
{
    retired_fd = (fd_t) (~0)
};
#else
typedef SOCKET fd_t;
enum
{
    retired_fd = (fd_t) INVALID_SOCKET
};
#endif
#else
typedef size_t socket_size_t;
88
inline const void *as_setsockopt_opt_t (const void *opt_)
89
{
90
    return opt_;
91
}
92 93 94 95 96 97 98
typedef int fd_t;
enum
{
    retired_fd = -1
};
#endif

99
#define LIBZMQ_UNUSED(object) (void) object
100

101 102
//  Bounce a message from client to server and back
//  For REQ/REP or DEALER/DEALER pairs only
103
void bounce (void *server_, void *client_);
Guido Goldstein's avatar
Guido Goldstein committed
104

MinRK's avatar
MinRK committed
105 106
//  Same as bounce, but expect messages to never arrive
//  for security or subscriber reasons.
107
void expect_bounce_fail (void *server_, void *client_);
MinRK's avatar
MinRK committed
108

109 110 111
//  Receive 0MQ string from socket and convert into C string
//  Caller must free returned string. Returns NULL if the context
//  is being terminated.
112
char *s_recv (void *socket_);
113

114 115
bool streq (const char *lhs, const char *rhs);
bool strneq (const char *lhs, const char *rhs);
116

117
extern const char *SEQ_END;
118 119 120 121

//  Sends a message composed of frames that are C strings or null frames.
//  The list must be terminated by SEQ_END.
//  Example: s_send_seq (req, "ABC", 0, "DEF", SEQ_END);
122

123
void s_send_seq (void *socket_, ...);
124 125 126 127 128

//  Receives message a number of frames long and checks that the frames have
//  the given data which can be either C strings or 0 for a null frame.
//  The list must be terminated by SEQ_END.
//  Example: s_recv_seq (rep, "ABC", 0, "DEF", SEQ_END);
129

130
void s_recv_seq (void *socket_, ...);
131

132

133
//  Sets a zero linger period on a socket and closes it.
134
void close_zero_linger (void *socket_);
135

136
void setup_test_environment (void);
137

138
//  Provide portable millisecond sleep
139 140 141
//  http://www.cplusplus.com/forum/unices/60161/
//  http://en.cppreference.com/w/cpp/thread/sleep_for

142
void msleep (int milliseconds_);
143

144 145
// check if IPv6 is available (0/false if not, 1/true if it is)
// only way to reliably check is to actually open a socket and try to bind it
146
int is_ipv6_available (void);
147

148 149 150
// check if tipc is available (0/false if not, 1/true if it is)
// only way to reliably check is to actually open a socket and try to bind it
// as it depends on a non-default kernel module to be already loaded
151
int is_tipc_available (void);
152

153 154
//  Wrapper around 'inet_pton' for systems that don't support it (e.g. Windows
//  XP)
155
int test_inet_pton (int af_, const char *src_, void *dst_);
156

157
//  Binds an ipv4 BSD socket to an ephemeral port, returns the compiled sockaddr
158
struct sockaddr_in bind_bsd_socket (int socket);
159

Guido Goldstein's avatar
Guido Goldstein committed
160
#endif