2010-08-27 21:13:45 +02:00
|
|
|
/*
|
2017-05-01 13:11:11 +02:00
|
|
|
Copyright (c) 2007-2017 Contributors as noted in the AUTHORS file
|
2010-08-27 21:13:45 +02:00
|
|
|
|
2015-06-02 22:33:55 +02:00
|
|
|
This file is part of libzmq, the ZeroMQ core engine in C++.
|
2010-08-27 21:13:45 +02:00
|
|
|
|
2015-06-02 22:33:55 +02:00
|
|
|
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
|
2010-08-27 21:13:45 +02:00
|
|
|
(at your option) any later version.
|
|
|
|
|
2015-06-02 22:33:55 +02:00
|
|
|
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.
|
2010-08-27 21:13:45 +02:00
|
|
|
|
2010-10-30 15:08:28 +02:00
|
|
|
You should have received a copy of the GNU Lesser General Public License
|
2010-08-27 21:13:45 +02:00
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2013-05-16 20:29:55 +02:00
|
|
|
#ifndef __TESTUTIL_HPP_INCLUDED__
|
|
|
|
#define __TESTUTIL_HPP_INCLUDED__
|
2010-08-27 21:13:45 +02:00
|
|
|
|
2016-02-12 11:31:38 +01:00
|
|
|
#if defined ZMQ_CUSTOM_PLATFORM_HPP
|
2018-02-01 11:46:09 +01:00
|
|
|
#include "platform.hpp"
|
2015-09-11 22:42:26 +02:00
|
|
|
#else
|
2018-02-01 11:46:09 +01:00
|
|
|
#include "../src/platform.hpp"
|
2015-09-11 22:42:26 +02:00
|
|
|
#endif
|
2016-05-02 20:39:13 +02:00
|
|
|
#include "../include/zmq.h"
|
|
|
|
#include "../src/stdint.hpp"
|
2013-08-17 14:43:45 +02:00
|
|
|
|
2013-11-06 13:30:41 +01:00
|
|
|
// 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.
|
2018-02-01 11:46:09 +01:00
|
|
|
#define SETTLE_TIME 300 // In msec
|
2017-05-01 13:11:11 +02:00
|
|
|
// Commonly used buffer size for ZMQ_LAST_ENDPOINT
|
2019-02-06 15:09:14 +01:00
|
|
|
// 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
|
2017-05-01 13:11:11 +02:00
|
|
|
|
|
|
|
// 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"
|
2019-03-19 00:27:01 +01:00
|
|
|
#define PORT_6 5561
|
2013-11-06 13:30:41 +01:00
|
|
|
|
2012-08-23 22:40:30 +02:00
|
|
|
#undef NDEBUG
|
2013-09-15 20:07:33 +02:00
|
|
|
#include <time.h>
|
2012-08-23 22:40:30 +02:00
|
|
|
#include <assert.h>
|
2013-07-02 15:04:31 +02:00
|
|
|
#include <stdarg.h>
|
2013-09-15 20:07:33 +02:00
|
|
|
#include <string>
|
2014-06-27 15:56:29 +02:00
|
|
|
#include <string.h>
|
2010-08-28 13:58:23 +02:00
|
|
|
|
2013-08-17 14:43:45 +02:00
|
|
|
#if defined _WIN32
|
2018-02-01 11:46:09 +01:00
|
|
|
#include "../src/windows.hpp"
|
|
|
|
#if defined _MSC_VER
|
|
|
|
#include <crtdbg.h>
|
|
|
|
#pragma warning(disable : 4996)
|
2016-07-12 15:58:17 +02:00
|
|
|
// iphlpapi is needed for if_nametoindex (not on Windows XP)
|
2018-02-01 11:46:09 +01:00
|
|
|
#if !defined ZMQ_HAVE_WINDOWS_TARGET_XP
|
|
|
|
#pragma comment(lib, "iphlpapi")
|
|
|
|
#endif
|
|
|
|
#endif
|
2013-09-15 20:07:33 +02:00
|
|
|
#else
|
2018-02-01 11:46:09 +01:00
|
|
|
#include <pthread.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <stdlib.h>
|
2019-03-16 01:48:18 +01:00
|
|
|
#include <grp.h>
|
2018-02-01 11:46:09 +01:00
|
|
|
#include <sys/wait.h>
|
|
|
|
#include <sys/socket.h>
|
2019-03-16 01:48:18 +01:00
|
|
|
#include <sys/types.h>
|
2018-02-01 11:46:09 +01:00
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <arpa/inet.h>
|
2018-04-30 16:56:17 +02:00
|
|
|
#include <net/if.h>
|
|
|
|
#include <netdb.h>
|
2018-02-01 11:46:09 +01:00
|
|
|
#if defined(ZMQ_HAVE_AIX)
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socketvar.h>
|
|
|
|
#endif
|
2013-08-17 14:43:45 +02:00
|
|
|
#endif
|
|
|
|
|
2018-05-14 20:49:58 +02:00
|
|
|
// duplicated from fd.hpp
|
|
|
|
#ifdef ZMQ_HAVE_WINDOWS
|
2019-03-19 00:27:01 +01:00
|
|
|
#include <winsock2.h>
|
|
|
|
#include <ws2tcpip.h>
|
|
|
|
#include <stdexcept>
|
2018-05-14 20:49:58 +02:00
|
|
|
#define close closesocket
|
|
|
|
typedef int socket_size_t;
|
2018-05-15 13:49:50 +02:00
|
|
|
const char *as_setsockopt_opt_t (const void *opt)
|
|
|
|
{
|
|
|
|
return static_cast<const char *> (opt);
|
|
|
|
}
|
2018-05-14 20:49:58 +02:00
|
|
|
#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;
|
2018-05-27 13:01:36 +02:00
|
|
|
const void *as_setsockopt_opt_t (const void *opt_)
|
2018-05-15 13:49:50 +02:00
|
|
|
{
|
2018-05-27 13:01:36 +02:00
|
|
|
return opt_;
|
2018-05-15 13:49:50 +02:00
|
|
|
}
|
2018-05-14 20:49:58 +02:00
|
|
|
typedef int fd_t;
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
retired_fd = -1
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
2018-02-01 11:46:09 +01:00
|
|
|
#define LIBZMQ_UNUSED(object) (void) object
|
2017-08-19 13:18:51 +02:00
|
|
|
|
2013-05-16 20:29:55 +02:00
|
|
|
// Bounce a message from client to server and back
|
|
|
|
// For REQ/REP or DEALER/DEALER pairs only
|
2018-05-27 13:01:36 +02:00
|
|
|
void bounce (void *server_, void *client_)
|
2010-08-27 21:13:45 +02:00
|
|
|
{
|
2011-03-24 10:03:49 +01:00
|
|
|
const char *content = "12345678ABCDEFGH12345678abcdefgh";
|
|
|
|
|
2013-05-16 20:29:55 +02:00
|
|
|
// Send message from client to server
|
2018-05-27 13:01:36 +02:00
|
|
|
int rc = zmq_send (client_, content, 32, ZMQ_SNDMORE);
|
2011-06-20 11:16:10 +02:00
|
|
|
assert (rc == 32);
|
2018-05-27 13:01:36 +02:00
|
|
|
rc = zmq_send (client_, content, 32, 0);
|
2011-03-24 11:53:55 +01:00
|
|
|
assert (rc == 32);
|
2011-03-24 10:03:49 +01:00
|
|
|
|
2013-05-16 20:29:55 +02:00
|
|
|
// Receive message at server side
|
2018-02-01 11:46:09 +01:00
|
|
|
char buffer[32];
|
2018-05-27 13:01:36 +02:00
|
|
|
rc = zmq_recv (server_, buffer, 32, 0);
|
2011-03-24 11:53:55 +01:00
|
|
|
assert (rc == 32);
|
2013-09-12 18:17:31 +02:00
|
|
|
// Check that message is still the same
|
|
|
|
assert (memcmp (buffer, content, 32) == 0);
|
2011-06-20 11:16:10 +02:00
|
|
|
int rcvmore;
|
|
|
|
size_t sz = sizeof (rcvmore);
|
2018-05-27 13:01:36 +02:00
|
|
|
rc = zmq_getsockopt (server_, ZMQ_RCVMORE, &rcvmore, &sz);
|
2011-06-20 11:16:10 +02:00
|
|
|
assert (rc == 0);
|
|
|
|
assert (rcvmore);
|
2018-05-27 13:01:36 +02:00
|
|
|
rc = zmq_recv (server_, buffer, 32, 0);
|
2011-06-20 11:16:10 +02:00
|
|
|
assert (rc == 32);
|
2013-09-12 18:17:31 +02:00
|
|
|
// Check that message is still the same
|
|
|
|
assert (memcmp (buffer, content, 32) == 0);
|
2018-05-27 13:01:36 +02:00
|
|
|
rc = zmq_getsockopt (server_, ZMQ_RCVMORE, &rcvmore, &sz);
|
2011-06-20 11:16:10 +02:00
|
|
|
assert (rc == 0);
|
|
|
|
assert (!rcvmore);
|
2015-07-19 16:11:22 +02:00
|
|
|
|
2013-05-16 20:29:55 +02:00
|
|
|
// Send two parts back to client
|
2018-05-27 13:01:36 +02:00
|
|
|
rc = zmq_send (server_, buffer, 32, ZMQ_SNDMORE);
|
2011-06-20 11:16:10 +02:00
|
|
|
assert (rc == 32);
|
2018-05-27 13:01:36 +02:00
|
|
|
rc = zmq_send (server_, buffer, 32, 0);
|
2011-03-24 11:53:55 +01:00
|
|
|
assert (rc == 32);
|
2011-03-24 10:03:49 +01:00
|
|
|
|
2013-05-16 20:29:55 +02:00
|
|
|
// Receive the two parts at the client side
|
2018-05-27 13:01:36 +02:00
|
|
|
rc = zmq_recv (client_, buffer, 32, 0);
|
2011-03-24 11:53:55 +01:00
|
|
|
assert (rc == 32);
|
2013-09-12 18:17:31 +02:00
|
|
|
// Check that message is still the same
|
|
|
|
assert (memcmp (buffer, content, 32) == 0);
|
2018-05-27 13:01:36 +02:00
|
|
|
rc = zmq_getsockopt (client_, ZMQ_RCVMORE, &rcvmore, &sz);
|
2011-06-20 11:16:10 +02:00
|
|
|
assert (rc == 0);
|
|
|
|
assert (rcvmore);
|
2018-05-27 13:01:36 +02:00
|
|
|
rc = zmq_recv (client_, buffer, 32, 0);
|
2011-06-20 11:16:10 +02:00
|
|
|
assert (rc == 32);
|
2013-09-12 18:17:31 +02:00
|
|
|
// Check that message is still the same
|
|
|
|
assert (memcmp (buffer, content, 32) == 0);
|
2018-05-27 13:01:36 +02:00
|
|
|
rc = zmq_getsockopt (client_, ZMQ_RCVMORE, &rcvmore, &sz);
|
2011-06-20 11:16:10 +02:00
|
|
|
assert (rc == 0);
|
|
|
|
assert (!rcvmore);
|
2010-08-27 21:13:45 +02:00
|
|
|
}
|
|
|
|
|
2013-08-31 02:56:59 +02:00
|
|
|
// Same as bounce, but expect messages to never arrive
|
|
|
|
// for security or subscriber reasons.
|
2018-05-27 13:01:36 +02:00
|
|
|
void expect_bounce_fail (void *server_, void *client_)
|
2013-08-31 02:56:59 +02:00
|
|
|
{
|
|
|
|
const char *content = "12345678ABCDEFGH12345678abcdefgh";
|
2018-02-01 11:46:09 +01:00
|
|
|
char buffer[32];
|
2015-01-30 11:57:31 +01:00
|
|
|
int timeout = 250;
|
2013-08-31 02:56:59 +02:00
|
|
|
|
|
|
|
// Send message from client to server
|
2018-05-27 13:01:36 +02:00
|
|
|
int rc = zmq_setsockopt (client_, ZMQ_SNDTIMEO, &timeout, sizeof (int));
|
2014-05-19 17:01:44 +02:00
|
|
|
assert (rc == 0);
|
2018-05-27 13:01:36 +02:00
|
|
|
rc = zmq_send (client_, content, 32, ZMQ_SNDMORE);
|
2014-05-19 17:01:44 +02:00
|
|
|
assert ((rc == 32) || ((rc == -1) && (errno == EAGAIN)));
|
2018-05-27 13:01:36 +02:00
|
|
|
rc = zmq_send (client_, content, 32, 0);
|
2014-05-19 17:01:44 +02:00
|
|
|
assert ((rc == 32) || ((rc == -1) && (errno == EAGAIN)));
|
2013-08-31 02:56:59 +02:00
|
|
|
|
|
|
|
// Receive message at server side (should not succeed)
|
2018-05-27 13:01:36 +02:00
|
|
|
rc = zmq_setsockopt (server_, ZMQ_RCVTIMEO, &timeout, sizeof (int));
|
2013-08-31 02:56:59 +02:00
|
|
|
assert (rc == 0);
|
2018-05-27 13:01:36 +02:00
|
|
|
rc = zmq_recv (server_, buffer, 32, 0);
|
2013-08-31 02:56:59 +02:00
|
|
|
assert (rc == -1);
|
2013-09-02 18:21:36 +02:00
|
|
|
assert (zmq_errno () == EAGAIN);
|
2013-08-31 02:56:59 +02:00
|
|
|
|
2013-09-02 18:21:36 +02:00
|
|
|
// Send message from server to client to test other direction
|
2014-05-18 11:11:57 +02:00
|
|
|
// If connection failed, send may block, without a timeout
|
2018-05-27 13:01:36 +02:00
|
|
|
rc = zmq_setsockopt (server_, ZMQ_SNDTIMEO, &timeout, sizeof (int));
|
2014-05-19 17:01:44 +02:00
|
|
|
assert (rc == 0);
|
2018-05-27 13:01:36 +02:00
|
|
|
rc = zmq_send (server_, content, 32, ZMQ_SNDMORE);
|
2014-05-18 11:11:57 +02:00
|
|
|
assert (rc == 32 || (rc == -1 && zmq_errno () == EAGAIN));
|
2018-05-27 13:01:36 +02:00
|
|
|
rc = zmq_send (server_, content, 32, 0);
|
2014-05-18 11:11:57 +02:00
|
|
|
assert (rc == 32 || (rc == -1 && zmq_errno () == EAGAIN));
|
2013-08-31 02:56:59 +02:00
|
|
|
|
2013-09-02 18:21:36 +02:00
|
|
|
// Receive message at client side (should not succeed)
|
2018-05-27 13:01:36 +02:00
|
|
|
rc = zmq_setsockopt (client_, ZMQ_RCVTIMEO, &timeout, sizeof (int));
|
2013-09-02 18:21:36 +02:00
|
|
|
assert (rc == 0);
|
2018-05-27 13:01:36 +02:00
|
|
|
rc = zmq_recv (client_, buffer, 32, 0);
|
2013-08-31 02:56:59 +02:00
|
|
|
assert (rc == -1);
|
2013-09-02 18:21:36 +02:00
|
|
|
assert (zmq_errno () == EAGAIN);
|
2013-08-31 02:56:59 +02:00
|
|
|
}
|
|
|
|
|
2013-06-28 11:42:54 +02:00
|
|
|
// Receive 0MQ string from socket and convert into C string
|
|
|
|
// Caller must free returned string. Returns NULL if the context
|
|
|
|
// is being terminated.
|
2018-05-27 13:01:36 +02:00
|
|
|
char *s_recv (void *socket_)
|
2018-02-01 11:46:09 +01:00
|
|
|
{
|
|
|
|
char buffer[256];
|
2018-05-27 13:01:36 +02:00
|
|
|
int size = zmq_recv (socket_, buffer, 255, 0);
|
2013-06-28 11:42:54 +02:00
|
|
|
if (size == -1)
|
|
|
|
return NULL;
|
|
|
|
if (size > 255)
|
|
|
|
size = 255;
|
2018-02-01 11:46:09 +01:00
|
|
|
buffer[size] = 0;
|
2013-06-28 11:42:54 +02:00
|
|
|
return strdup (buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Convert C string to 0MQ string and send to socket
|
2018-05-27 13:01:36 +02:00
|
|
|
int s_send (void *socket_, const char *string_)
|
2018-02-01 11:46:09 +01:00
|
|
|
{
|
2018-05-27 13:01:36 +02:00
|
|
|
int size = zmq_send (socket_, string_, strlen (string_), 0);
|
2013-06-28 11:42:54 +02:00
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sends string as 0MQ string, as multipart non-terminal
|
2018-05-27 13:01:36 +02:00
|
|
|
int s_sendmore (void *socket_, const char *string_)
|
2018-02-01 11:46:09 +01:00
|
|
|
{
|
2018-05-27 13:01:36 +02:00
|
|
|
int size = zmq_send (socket_, string_, strlen (string_), ZMQ_SNDMORE);
|
2013-06-28 11:42:54 +02:00
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
2018-02-01 11:46:09 +01:00
|
|
|
#define streq(s1, s2) (!strcmp ((s1), (s2)))
|
|
|
|
#define strneq(s1, s2) (strcmp ((s1), (s2)))
|
2013-06-28 11:42:54 +02:00
|
|
|
|
2013-07-07 12:49:24 +02:00
|
|
|
const char *SEQ_END = (const char *) 1;
|
2013-07-02 15:04:31 +02:00
|
|
|
|
|
|
|
// 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);
|
2015-09-11 22:42:26 +02:00
|
|
|
|
2018-05-27 13:01:36 +02:00
|
|
|
void s_send_seq (void *socket_, ...)
|
2013-07-02 15:04:31 +02:00
|
|
|
{
|
|
|
|
va_list ap;
|
2018-05-27 13:01:36 +02:00
|
|
|
va_start (ap, socket_);
|
2018-02-01 11:46:09 +01:00
|
|
|
const char *data = va_arg (ap, const char *);
|
|
|
|
while (true) {
|
|
|
|
const char *prev = data;
|
2013-07-02 15:04:31 +02:00
|
|
|
data = va_arg (ap, const char *);
|
|
|
|
bool end = data == SEQ_END;
|
|
|
|
|
2013-07-07 12:49:24 +02:00
|
|
|
if (!prev) {
|
2018-05-27 13:01:36 +02:00
|
|
|
int rc = zmq_send (socket_, 0, 0, end ? 0 : ZMQ_SNDMORE);
|
2013-07-02 15:04:31 +02:00
|
|
|
assert (rc != -1);
|
2018-02-01 11:46:09 +01:00
|
|
|
} else {
|
2018-05-27 13:01:36 +02:00
|
|
|
int rc = zmq_send (socket_, prev, strlen (prev) + 1,
|
|
|
|
end ? 0 : ZMQ_SNDMORE);
|
2013-07-02 15:04:31 +02:00
|
|
|
assert (rc != -1);
|
|
|
|
}
|
|
|
|
if (end)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
va_end (ap);
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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);
|
2015-09-11 22:42:26 +02:00
|
|
|
|
2018-05-27 13:01:36 +02:00
|
|
|
void s_recv_seq (void *socket_, ...)
|
2013-07-02 15:04:31 +02:00
|
|
|
{
|
|
|
|
zmq_msg_t msg;
|
|
|
|
zmq_msg_init (&msg);
|
|
|
|
|
|
|
|
int more;
|
2018-02-01 11:46:09 +01:00
|
|
|
size_t more_size = sizeof (more);
|
2013-07-02 15:04:31 +02:00
|
|
|
|
|
|
|
va_list ap;
|
2018-05-27 13:01:36 +02:00
|
|
|
va_start (ap, socket_);
|
2018-02-01 11:46:09 +01:00
|
|
|
const char *data = va_arg (ap, const char *);
|
2015-07-19 16:11:22 +02:00
|
|
|
|
2013-07-07 12:49:24 +02:00
|
|
|
while (true) {
|
2018-05-27 13:01:36 +02:00
|
|
|
int rc = zmq_msg_recv (&msg, socket_, 0);
|
2013-07-02 15:04:31 +02:00
|
|
|
assert (rc != -1);
|
|
|
|
|
|
|
|
if (!data)
|
|
|
|
assert (zmq_msg_size (&msg) == 0);
|
|
|
|
else
|
2018-02-01 11:46:09 +01:00
|
|
|
assert (strcmp (data, (const char *) zmq_msg_data (&msg)) == 0);
|
2013-07-02 15:04:31 +02:00
|
|
|
|
|
|
|
data = va_arg (ap, const char *);
|
|
|
|
bool end = data == SEQ_END;
|
|
|
|
|
2018-05-27 13:01:36 +02:00
|
|
|
rc = zmq_getsockopt (socket_, ZMQ_RCVMORE, &more, &more_size);
|
2013-07-02 15:04:31 +02:00
|
|
|
assert (rc == 0);
|
|
|
|
|
|
|
|
assert (!more == end);
|
|
|
|
if (end)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
va_end (ap);
|
|
|
|
|
|
|
|
zmq_msg_close (&msg);
|
|
|
|
}
|
|
|
|
|
2013-07-05 17:58:01 +02:00
|
|
|
|
2014-04-28 11:30:04 +02:00
|
|
|
// Sets a zero linger period on a socket and closes it.
|
2018-05-27 13:01:36 +02:00
|
|
|
void close_zero_linger (void *socket_)
|
2013-07-05 17:58:01 +02:00
|
|
|
{
|
|
|
|
int linger = 0;
|
2018-05-27 13:01:36 +02:00
|
|
|
int rc = zmq_setsockopt (socket_, ZMQ_LINGER, &linger, sizeof (linger));
|
2013-09-02 17:22:24 +02:00
|
|
|
assert (rc == 0 || errno == ETERM);
|
2018-05-27 13:01:36 +02:00
|
|
|
rc = zmq_close (socket_);
|
2013-07-05 17:58:01 +02:00
|
|
|
assert (rc == 0);
|
|
|
|
}
|
|
|
|
|
2018-02-01 11:46:09 +01:00
|
|
|
void setup_test_environment (void)
|
2013-08-17 14:43:45 +02:00
|
|
|
{
|
|
|
|
#if defined _WIN32
|
2018-02-01 11:46:09 +01:00
|
|
|
#if defined _MSC_VER
|
|
|
|
_set_abort_behavior (0, _WRITE_ABORT_MSG);
|
|
|
|
_CrtSetReportMode (_CRT_ASSERT, _CRTDBG_MODE_FILE);
|
|
|
|
_CrtSetReportFile (_CRT_ASSERT, _CRTDBG_FILE_STDERR);
|
|
|
|
#endif
|
2014-12-29 23:39:19 +01:00
|
|
|
#else
|
|
|
|
#if defined ZMQ_HAVE_CYGWIN
|
|
|
|
// abort test after 121 seconds
|
2018-02-01 11:46:09 +01:00
|
|
|
alarm (121);
|
2014-05-19 17:00:09 +02:00
|
|
|
#else
|
2018-02-01 11:46:09 +01:00
|
|
|
#if !defined ZMQ_DISABLE_TEST_TIMEOUT
|
2014-05-19 17:00:09 +02:00
|
|
|
// abort test after 60 seconds
|
2018-02-01 11:46:09 +01:00
|
|
|
alarm (60);
|
|
|
|
#endif
|
2013-08-17 14:43:45 +02:00
|
|
|
#endif
|
2014-12-29 23:39:19 +01:00
|
|
|
#endif
|
2014-07-23 03:39:45 +02:00
|
|
|
#if defined __MVS__
|
|
|
|
// z/OS UNIX System Services: Ignore SIGPIPE during test runs, as a
|
|
|
|
// workaround for no SO_NOGSIGPIPE socket option.
|
2018-02-01 11:46:09 +01:00
|
|
|
signal (SIGPIPE, SIG_IGN);
|
2014-07-23 03:39:45 +02:00
|
|
|
#endif
|
2013-08-17 14:43:45 +02:00
|
|
|
}
|
|
|
|
|
2013-11-06 13:30:41 +01:00
|
|
|
// Provide portable millisecond sleep
|
2015-09-11 22:42:26 +02:00
|
|
|
// http://www.cplusplus.com/forum/unices/60161/
|
|
|
|
// http://en.cppreference.com/w/cpp/thread/sleep_for
|
|
|
|
|
2018-05-27 13:01:36 +02:00
|
|
|
void msleep (int milliseconds_)
|
2013-11-06 13:30:41 +01:00
|
|
|
{
|
2013-10-18 11:12:48 +02:00
|
|
|
#ifdef ZMQ_HAVE_WINDOWS
|
2018-05-27 13:01:36 +02:00
|
|
|
Sleep (milliseconds_);
|
2013-10-18 11:12:48 +02:00
|
|
|
#else
|
2018-05-27 13:01:36 +02:00
|
|
|
usleep (static_cast<useconds_t> (milliseconds_) * 1000);
|
2013-10-18 11:12:48 +02:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2016-04-18 17:20:58 +02:00
|
|
|
// 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
|
2018-02-01 11:46:09 +01:00
|
|
|
int is_ipv6_available (void)
|
2016-04-18 17:20:58 +02:00
|
|
|
{
|
2018-02-01 11:46:09 +01:00
|
|
|
#if defined(ZMQ_HAVE_WINDOWS) && (_WIN32_WINNT < 0x0600)
|
2016-08-27 17:40:43 +02:00
|
|
|
return 0;
|
|
|
|
#else
|
2016-04-18 17:20:58 +02:00
|
|
|
int rc, ipv6 = 1;
|
|
|
|
struct sockaddr_in6 test_addr;
|
|
|
|
|
|
|
|
memset (&test_addr, 0, sizeof (test_addr));
|
|
|
|
test_addr.sin6_family = AF_INET6;
|
|
|
|
inet_pton (AF_INET6, "::1", &(test_addr.sin6_addr));
|
|
|
|
|
2018-05-14 20:49:58 +02:00
|
|
|
fd_t fd = socket (AF_INET6, SOCK_STREAM, IPPROTO_IP);
|
|
|
|
if (fd == retired_fd)
|
2016-04-18 17:20:58 +02:00
|
|
|
ipv6 = 0;
|
|
|
|
else {
|
2018-05-14 20:49:58 +02:00
|
|
|
#ifdef ZMQ_HAVE_WINDOWS
|
2018-02-01 11:46:09 +01:00
|
|
|
setsockopt (fd, SOL_SOCKET, SO_REUSEADDR, (const char *) &ipv6,
|
|
|
|
sizeof (int));
|
|
|
|
rc = setsockopt (fd, IPPROTO_IPV6, IPV6_V6ONLY, (const char *) &ipv6,
|
|
|
|
sizeof (int));
|
2016-04-18 17:20:58 +02:00
|
|
|
if (rc == SOCKET_ERROR)
|
|
|
|
ipv6 = 0;
|
|
|
|
else {
|
2018-02-01 11:46:09 +01:00
|
|
|
rc = bind (fd, (struct sockaddr *) &test_addr, sizeof (test_addr));
|
2016-04-18 17:20:58 +02:00
|
|
|
if (rc == SOCKET_ERROR)
|
|
|
|
ipv6 = 0;
|
|
|
|
}
|
|
|
|
#else
|
2018-02-01 11:46:09 +01:00
|
|
|
setsockopt (fd, SOL_SOCKET, SO_REUSEADDR, &ipv6, sizeof (int));
|
|
|
|
rc = setsockopt (fd, IPPROTO_IPV6, IPV6_V6ONLY, &ipv6, sizeof (int));
|
2016-04-18 17:20:58 +02:00
|
|
|
if (rc != 0)
|
|
|
|
ipv6 = 0;
|
|
|
|
else {
|
2018-02-01 11:46:09 +01:00
|
|
|
rc = bind (fd, (struct sockaddr *) &test_addr, sizeof (test_addr));
|
2016-04-18 17:20:58 +02:00
|
|
|
if (rc != 0)
|
|
|
|
ipv6 = 0;
|
|
|
|
}
|
2018-05-14 20:49:58 +02:00
|
|
|
#endif
|
2016-04-18 17:20:58 +02:00
|
|
|
close (fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ipv6;
|
2016-08-27 17:40:43 +02:00
|
|
|
#endif // _WIN32_WINNT < 0x0600
|
2016-04-18 17:20:58 +02:00
|
|
|
}
|
2013-10-18 11:12:48 +02:00
|
|
|
|
2018-03-09 17:47:42 +01:00
|
|
|
// 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
|
|
|
|
int is_tipc_available (void)
|
|
|
|
{
|
|
|
|
#ifndef ZMQ_HAVE_TIPC
|
|
|
|
return 0;
|
|
|
|
#else
|
|
|
|
int tipc = 0;
|
|
|
|
|
|
|
|
void *ctx = zmq_init (1);
|
|
|
|
assert (ctx);
|
|
|
|
void *rep = zmq_socket (ctx, ZMQ_REP);
|
|
|
|
assert (rep);
|
|
|
|
tipc = zmq_bind (rep, "tipc://{5560,0,0}");
|
|
|
|
|
|
|
|
zmq_close (rep);
|
|
|
|
zmq_ctx_term (ctx);
|
|
|
|
|
|
|
|
return tipc == 0;
|
|
|
|
#endif // ZMQ_HAVE_TIPC
|
|
|
|
}
|
|
|
|
|
2018-04-30 16:56:17 +02:00
|
|
|
// Wrapper around 'inet_pton' for systems that don't support it (e.g. Windows
|
|
|
|
// XP)
|
|
|
|
int test_inet_pton (int af_, const char *src_, void *dst_)
|
|
|
|
{
|
|
|
|
#if defined(ZMQ_HAVE_WINDOWS) && (_WIN32_WINNT < 0x0600)
|
|
|
|
if (af_ == AF_INET) {
|
|
|
|
struct in_addr *ip4addr = (struct in_addr *) dst_;
|
|
|
|
|
|
|
|
ip4addr->s_addr = inet_addr (src_);
|
|
|
|
|
|
|
|
// INADDR_NONE is -1 which is also a valid representation for IP
|
|
|
|
// 255.255.255.255
|
|
|
|
if (ip4addr->s_addr == INADDR_NONE
|
|
|
|
&& strcmp (src_, "255.255.255.255") != 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Success
|
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
// Not supported.
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
return inet_pton (af_, src_, dst_);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2019-03-19 00:27:01 +01:00
|
|
|
// Binds an ipv4 BSD socket to an ephemeral port, returns the compiled sockaddr
|
|
|
|
struct sockaddr_in bind_bsd_socket (int socket)
|
|
|
|
{
|
|
|
|
struct sockaddr_in saddr;
|
|
|
|
memset (&saddr, 0, sizeof (saddr));
|
|
|
|
saddr.sin_family = AF_INET;
|
|
|
|
saddr.sin_addr.s_addr = INADDR_ANY;
|
|
|
|
#if !defined(_WIN32_WINNT) || (_WIN32_WINNT >= 0x0600)
|
|
|
|
saddr.sin_port = 0;
|
|
|
|
#else
|
|
|
|
saddr.sin_port = htons (PORT_6);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
int rc = bind (socket, (struct sockaddr *) &saddr, sizeof (saddr));
|
|
|
|
assert (rc == 0);
|
|
|
|
|
|
|
|
#if !defined(_WIN32_WINNT) || (_WIN32_WINNT >= 0x0600)
|
|
|
|
socklen_t saddr_len = sizeof (saddr);
|
|
|
|
rc = getsockname (socket, (struct sockaddr *) &saddr, &saddr_len);
|
|
|
|
assert (rc == 0);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return saddr;
|
|
|
|
}
|
|
|
|
|
2010-08-27 21:13:45 +02:00
|
|
|
#endif
|