2018-03-13 12:00:41 +01:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
/*
|
|
|
|
Copyright (c) 2018 Contributors as noted in the AUTHORS file
|
|
|
|
|
|
|
|
This file is part of libzmq, the ZeroMQ core engine in C++.
|
|
|
|
|
|
|
|
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
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "../include/zmq.h"
|
|
|
|
|
2019-02-03 11:29:14 +01:00
|
|
|
#include "testutil.hpp"
|
|
|
|
|
2018-03-13 12:00:41 +01:00
|
|
|
#include <unity.h>
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#if defined(_MSC_VER) && _MSC_VER <= 1800
|
|
|
|
#define snprintf _snprintf
|
|
|
|
#endif
|
|
|
|
|
2018-05-27 07:01:36 -04:00
|
|
|
int test_assert_success_message_errno_helper (int rc_,
|
|
|
|
const char *msg_,
|
|
|
|
const char *expr_)
|
2018-03-13 12:00:41 +01:00
|
|
|
{
|
2018-05-27 07:01:36 -04:00
|
|
|
if (rc_ == -1) {
|
2018-03-13 12:00:41 +01:00
|
|
|
char buffer[512];
|
|
|
|
buffer[sizeof (buffer) - 1] =
|
|
|
|
0; // to ensure defined behavior with VC++ <= 2013
|
|
|
|
snprintf (buffer, sizeof (buffer) - 1,
|
2018-05-27 07:01:36 -04:00
|
|
|
"%s failed%s%s%s, errno = %i (%s)", expr_,
|
|
|
|
msg_ ? " (additional info: " : "", msg_ ? msg_ : "",
|
|
|
|
msg_ ? ")" : "", zmq_errno (), zmq_strerror (zmq_errno ()));
|
2018-03-13 12:00:41 +01:00
|
|
|
TEST_FAIL_MESSAGE (buffer);
|
|
|
|
}
|
2018-05-27 07:01:36 -04:00
|
|
|
return rc_;
|
2018-03-13 12:00:41 +01:00
|
|
|
}
|
|
|
|
|
2018-05-27 07:01:36 -04:00
|
|
|
int test_assert_success_message_raw_errno_helper (int rc_,
|
|
|
|
const char *msg_,
|
|
|
|
const char *expr_)
|
2018-05-13 12:09:59 +02:00
|
|
|
{
|
2018-05-27 07:01:36 -04:00
|
|
|
if (rc_ == -1) {
|
2018-05-13 12:09:59 +02:00
|
|
|
#if defined ZMQ_HAVE_WINDOWS
|
|
|
|
int current_errno = WSAGetLastError ();
|
|
|
|
#else
|
|
|
|
int current_errno = errno;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
char buffer[512];
|
|
|
|
buffer[sizeof (buffer) - 1] =
|
|
|
|
0; // to ensure defined behavior with VC++ <= 2013
|
|
|
|
snprintf (buffer, sizeof (buffer) - 1, "%s failed%s%s%s, errno = %i",
|
2018-05-27 07:01:36 -04:00
|
|
|
expr_, msg_ ? " (additional info: " : "", msg_ ? msg_ : "",
|
|
|
|
msg_ ? ")" : "", current_errno);
|
2018-05-13 12:09:59 +02:00
|
|
|
TEST_FAIL_MESSAGE (buffer);
|
|
|
|
}
|
2018-05-27 07:01:36 -04:00
|
|
|
return rc_;
|
2018-05-13 12:09:59 +02:00
|
|
|
}
|
|
|
|
|
2018-12-12 05:27:50 -05:00
|
|
|
int test_assert_failure_message_raw_errno_helper (int rc_,
|
|
|
|
int expected_errno_,
|
|
|
|
const char *msg_,
|
|
|
|
const char *expr_)
|
|
|
|
{
|
|
|
|
char buffer[512];
|
|
|
|
buffer[sizeof (buffer) - 1] =
|
|
|
|
0; // to ensure defined behavior with VC++ <= 2013
|
|
|
|
if (rc_ != -1) {
|
|
|
|
snprintf (buffer, sizeof (buffer) - 1,
|
|
|
|
"%s was unexpectedly successful%s%s%s, expected "
|
|
|
|
"errno = %i, actual return value = %i",
|
|
|
|
expr_, msg_ ? " (additional info: " : "", msg_ ? msg_ : "",
|
|
|
|
msg_ ? ")" : "", expected_errno_, rc_);
|
|
|
|
TEST_FAIL_MESSAGE (buffer);
|
|
|
|
} else {
|
|
|
|
#if defined ZMQ_HAVE_WINDOWS
|
|
|
|
int current_errno = WSAGetLastError ();
|
|
|
|
#else
|
|
|
|
int current_errno = errno;
|
|
|
|
#endif
|
|
|
|
if (current_errno != expected_errno_) {
|
|
|
|
snprintf (buffer, sizeof (buffer) - 1,
|
|
|
|
"%s failed with an unexpected error%s%s%s, expected "
|
|
|
|
"errno = %i, actual errno = %i",
|
|
|
|
expr_, msg_ ? " (additional info: " : "",
|
|
|
|
msg_ ? msg_ : "", msg_ ? ")" : "", expected_errno_,
|
|
|
|
current_errno);
|
|
|
|
TEST_FAIL_MESSAGE (buffer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return rc_;
|
|
|
|
}
|
|
|
|
|
2018-03-13 12:00:41 +01:00
|
|
|
#define TEST_ASSERT_SUCCESS_MESSAGE_ERRNO(expr, msg) \
|
|
|
|
test_assert_success_message_errno_helper (expr, msg, #expr)
|
|
|
|
|
|
|
|
#define TEST_ASSERT_SUCCESS_ERRNO(expr) \
|
|
|
|
test_assert_success_message_errno_helper (expr, NULL, #expr)
|
|
|
|
|
2018-05-13 12:09:59 +02:00
|
|
|
#define TEST_ASSERT_SUCCESS_RAW_ERRNO(expr) \
|
|
|
|
test_assert_success_message_raw_errno_helper (expr, NULL, #expr)
|
|
|
|
|
2018-12-12 05:27:50 -05:00
|
|
|
#define TEST_ASSERT_FAILURE_RAW_ERRNO(error_code, expr) \
|
|
|
|
test_assert_failure_message_raw_errno_helper (expr, error_code, NULL, #expr)
|
|
|
|
|
2018-03-15 14:46:17 +01:00
|
|
|
#define TEST_ASSERT_FAILURE_ERRNO(error_code, expr) \
|
|
|
|
{ \
|
2018-08-16 15:43:41 +02:00
|
|
|
int _rc = (expr); \
|
|
|
|
TEST_ASSERT_EQUAL_INT (-1, _rc); \
|
2018-03-15 14:46:17 +01:00
|
|
|
TEST_ASSERT_EQUAL_INT (error_code, errno); \
|
|
|
|
}
|
|
|
|
|
2018-05-27 07:01:36 -04:00
|
|
|
void send_string_expect_success (void *socket_, const char *str_, int flags_)
|
2018-03-13 12:00:41 +01:00
|
|
|
{
|
2018-05-27 07:01:36 -04:00
|
|
|
const size_t len = str_ ? strlen (str_) : 0;
|
|
|
|
const int rc = zmq_send (socket_, str_, len, flags_);
|
2018-03-13 12:00:41 +01:00
|
|
|
TEST_ASSERT_EQUAL_INT ((int) len, rc);
|
|
|
|
}
|
|
|
|
|
2018-05-27 07:01:36 -04:00
|
|
|
void recv_string_expect_success (void *socket_, const char *str_, int flags_)
|
2018-03-13 12:00:41 +01:00
|
|
|
{
|
2018-05-27 07:01:36 -04:00
|
|
|
const size_t len = str_ ? strlen (str_) : 0;
|
2018-03-13 12:00:41 +01:00
|
|
|
char buffer[255];
|
|
|
|
TEST_ASSERT_LESS_OR_EQUAL_MESSAGE (sizeof (buffer), len,
|
|
|
|
"recv_string_expect_success cannot be "
|
|
|
|
"used for strings longer than 255 "
|
|
|
|
"characters");
|
|
|
|
|
2018-05-10 11:40:22 +02:00
|
|
|
const int rc = TEST_ASSERT_SUCCESS_ERRNO (
|
2018-05-27 07:01:36 -04:00
|
|
|
zmq_recv (socket_, buffer, sizeof (buffer), flags_));
|
2018-03-13 12:00:41 +01:00
|
|
|
TEST_ASSERT_EQUAL_INT ((int) len, rc);
|
2018-05-27 07:01:36 -04:00
|
|
|
if (str_)
|
|
|
|
TEST_ASSERT_EQUAL_STRING_LEN (str_, buffer, len);
|
2018-03-13 12:00:41 +01:00
|
|
|
}
|
2018-03-14 09:59:16 +01:00
|
|
|
|
2018-08-20 13:45:58 +02:00
|
|
|
template <size_t SIZE>
|
|
|
|
void send_array_expect_success (void *socket_,
|
|
|
|
const uint8_t (&array_)[SIZE],
|
|
|
|
int flags_)
|
|
|
|
{
|
|
|
|
const int rc = zmq_send (socket_, array_, SIZE, flags_);
|
|
|
|
TEST_ASSERT_EQUAL_INT (static_cast<int> (SIZE), rc);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <size_t SIZE>
|
|
|
|
void recv_array_expect_success (void *socket_,
|
|
|
|
const uint8_t (&array_)[SIZE],
|
|
|
|
int flags_)
|
|
|
|
{
|
|
|
|
char buffer[255];
|
|
|
|
TEST_ASSERT_LESS_OR_EQUAL_MESSAGE (sizeof (buffer), SIZE,
|
|
|
|
"recv_string_expect_success cannot be "
|
|
|
|
"used for strings longer than 255 "
|
|
|
|
"characters");
|
|
|
|
|
|
|
|
const int rc = TEST_ASSERT_SUCCESS_ERRNO (
|
|
|
|
zmq_recv (socket_, buffer, sizeof (buffer), flags_));
|
|
|
|
TEST_ASSERT_EQUAL_INT (static_cast<int> (SIZE), rc);
|
|
|
|
TEST_ASSERT_EQUAL_UINT8_ARRAY (array_, buffer, SIZE);
|
|
|
|
}
|
|
|
|
|
2018-03-14 09:59:16 +01:00
|
|
|
// do not call from tests directly, use setup_test_context, get_test_context and teardown_test_context only
|
2018-05-27 07:01:36 -04:00
|
|
|
void *internal_manage_test_context (bool init_, bool clear_)
|
2018-03-14 09:59:16 +01:00
|
|
|
{
|
|
|
|
static void *test_context = NULL;
|
2018-05-27 07:01:36 -04:00
|
|
|
if (clear_) {
|
2018-03-14 09:59:16 +01:00
|
|
|
TEST_ASSERT_NOT_NULL (test_context);
|
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_ctx_term (test_context));
|
|
|
|
test_context = NULL;
|
|
|
|
} else {
|
2018-05-27 07:01:36 -04:00
|
|
|
if (init_) {
|
2018-03-14 09:59:16 +01:00
|
|
|
TEST_ASSERT_NULL (test_context);
|
|
|
|
test_context = zmq_ctx_new ();
|
|
|
|
TEST_ASSERT_NOT_NULL (test_context);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return test_context;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define MAX_TEST_SOCKETS 128
|
|
|
|
|
2018-05-27 07:01:36 -04:00
|
|
|
void internal_manage_test_sockets (void *socket_, bool add_)
|
2018-03-14 09:59:16 +01:00
|
|
|
{
|
|
|
|
static void *test_sockets[MAX_TEST_SOCKETS];
|
|
|
|
static size_t test_socket_count = 0;
|
2018-05-27 07:01:36 -04:00
|
|
|
if (!socket_) {
|
|
|
|
assert (!add_);
|
2018-03-14 09:59:16 +01:00
|
|
|
|
|
|
|
// force-close all sockets
|
|
|
|
if (test_socket_count) {
|
|
|
|
for (size_t i = 0; i < test_socket_count; ++i) {
|
|
|
|
close_zero_linger (test_sockets[i]);
|
|
|
|
}
|
|
|
|
fprintf (stderr,
|
|
|
|
"WARNING: Forced closure of %i sockets, this is an "
|
|
|
|
"implementation error unless the test case failed\n",
|
|
|
|
(int) test_socket_count);
|
|
|
|
test_socket_count = 0;
|
|
|
|
}
|
|
|
|
} else {
|
2018-05-27 07:01:36 -04:00
|
|
|
if (add_) {
|
2018-03-14 09:59:16 +01:00
|
|
|
++test_socket_count;
|
|
|
|
TEST_ASSERT_LESS_THAN_MESSAGE (MAX_TEST_SOCKETS, test_socket_count,
|
|
|
|
"MAX_TEST_SOCKETS must be "
|
|
|
|
"increased, or you cannot use the "
|
|
|
|
"test context");
|
2018-05-27 07:01:36 -04:00
|
|
|
test_sockets[test_socket_count - 1] = socket_;
|
2018-03-14 09:59:16 +01:00
|
|
|
} else {
|
|
|
|
bool found = false;
|
|
|
|
for (size_t i = 0; i < test_socket_count; ++i) {
|
2018-05-27 07:01:36 -04:00
|
|
|
if (test_sockets[i] == socket_) {
|
2018-03-14 09:59:16 +01:00
|
|
|
found = true;
|
|
|
|
}
|
|
|
|
if (found) {
|
|
|
|
if (i < test_socket_count)
|
|
|
|
test_sockets[i] = test_sockets[i + 1];
|
|
|
|
}
|
|
|
|
}
|
2018-12-16 17:51:55 +01:00
|
|
|
TEST_ASSERT_TRUE_MESSAGE (found,
|
|
|
|
"Attempted to close a socket that was "
|
|
|
|
"not created by test_context_socket");
|
2018-03-14 09:59:16 +01:00
|
|
|
--test_socket_count;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void setup_test_context ()
|
|
|
|
{
|
|
|
|
internal_manage_test_context (true, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void *get_test_context ()
|
|
|
|
{
|
|
|
|
return internal_manage_test_context (false, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void teardown_test_context ()
|
|
|
|
{
|
|
|
|
// this condition allows an explicit call to teardown_test_context from a
|
|
|
|
// test. if this is never used, it should probably be removed, to detect
|
|
|
|
// misuses
|
|
|
|
if (get_test_context ()) {
|
|
|
|
internal_manage_test_sockets (NULL, false);
|
|
|
|
internal_manage_test_context (false, true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-27 07:01:36 -04:00
|
|
|
void *test_context_socket (int type_)
|
2018-03-14 09:59:16 +01:00
|
|
|
{
|
2018-05-27 07:01:36 -04:00
|
|
|
void *const socket = zmq_socket (get_test_context (), type_);
|
2018-03-14 09:59:16 +01:00
|
|
|
TEST_ASSERT_NOT_NULL (socket);
|
|
|
|
internal_manage_test_sockets (socket, true);
|
|
|
|
return socket;
|
|
|
|
}
|
|
|
|
|
2018-05-27 07:01:36 -04:00
|
|
|
void *test_context_socket_close (void *socket_)
|
2018-03-14 09:59:16 +01:00
|
|
|
{
|
2018-05-27 07:01:36 -04:00
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_close (socket_));
|
|
|
|
internal_manage_test_sockets (socket_, false);
|
|
|
|
return socket_;
|
2018-03-14 09:59:16 +01:00
|
|
|
}
|
2018-03-16 15:21:39 +01:00
|
|
|
|
2018-05-27 07:01:36 -04:00
|
|
|
void *test_context_socket_close_zero_linger (void *socket_)
|
2018-05-15 12:20:19 +02:00
|
|
|
{
|
|
|
|
const int linger = 0;
|
2018-05-27 07:01:36 -04:00
|
|
|
int rc = zmq_setsockopt (socket_, ZMQ_LINGER, &linger, sizeof (linger));
|
2018-05-15 12:20:19 +02:00
|
|
|
TEST_ASSERT_TRUE (rc == 0 || zmq_errno () == ETERM);
|
2018-05-27 07:01:36 -04:00
|
|
|
return test_context_socket_close (socket_);
|
2018-05-15 12:20:19 +02:00
|
|
|
}
|
|
|
|
|
2018-05-27 07:01:36 -04:00
|
|
|
void test_bind (void *socket_,
|
|
|
|
const char *bind_address_,
|
|
|
|
char *my_endpoint_,
|
|
|
|
size_t len_)
|
2018-05-15 12:20:19 +02:00
|
|
|
{
|
2018-05-27 07:01:36 -04:00
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (socket_, bind_address_));
|
2018-05-15 12:20:19 +02:00
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (
|
2018-05-27 07:01:36 -04:00
|
|
|
zmq_getsockopt (socket_, ZMQ_LAST_ENDPOINT, my_endpoint_, &len_));
|
2018-05-15 12:20:19 +02:00
|
|
|
}
|
|
|
|
|
2018-05-27 07:01:36 -04:00
|
|
|
void bind_loopback (void *socket_, int ipv6_, char *my_endpoint_, size_t len_)
|
2018-03-16 15:21:39 +01:00
|
|
|
{
|
2018-05-27 07:01:36 -04:00
|
|
|
if (ipv6_ && !is_ipv6_available ()) {
|
2018-03-21 15:01:25 +01:00
|
|
|
TEST_IGNORE_MESSAGE ("ipv6 is not available");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (
|
2018-05-27 07:01:36 -04:00
|
|
|
zmq_setsockopt (socket_, ZMQ_IPV6, &ipv6_, sizeof (int)));
|
2018-05-15 12:20:19 +02:00
|
|
|
|
2018-05-27 07:01:36 -04:00
|
|
|
test_bind (socket_, ipv6_ ? "tcp://[::1]:*" : "tcp://127.0.0.1:*",
|
|
|
|
my_endpoint_, len_);
|
2018-03-16 15:21:39 +01:00
|
|
|
}
|
2018-03-21 15:01:25 +01:00
|
|
|
|
2019-02-02 19:00:33 +01:00
|
|
|
typedef void (*bind_function_t) (void *socket_,
|
|
|
|
char *my_endpoint_,
|
|
|
|
size_t len_);
|
|
|
|
|
2018-05-27 07:01:36 -04:00
|
|
|
void bind_loopback_ipv4 (void *socket_, char *my_endpoint_, size_t len_)
|
2018-03-21 15:01:25 +01:00
|
|
|
{
|
2018-05-27 07:01:36 -04:00
|
|
|
bind_loopback (socket_, false, my_endpoint_, len_);
|
2018-03-21 15:01:25 +01:00
|
|
|
}
|
2019-02-02 19:00:33 +01:00
|
|
|
|
|
|
|
void bind_loopback_ipv6 (void *socket_, char *my_endpoint_, size_t len_)
|
|
|
|
{
|
|
|
|
bind_loopback (socket_, true, my_endpoint_, len_);
|
|
|
|
}
|
2019-02-04 05:33:28 -05:00
|
|
|
|
2019-02-03 11:29:14 +01:00
|
|
|
void bind_loopback_ipc (void *socket_, char *my_endpoint_, size_t len_)
|
|
|
|
{
|
|
|
|
if (!zmq_has ("ipc")) {
|
|
|
|
TEST_IGNORE_MESSAGE ("ipc is not available");
|
|
|
|
}
|
|
|
|
|
|
|
|
test_bind (socket_, "ipc://*", my_endpoint_, len_);
|
|
|
|
}
|
|
|
|
|
|
|
|
void bind_loopback_tipc (void *socket_, char *my_endpoint_, size_t len_)
|
|
|
|
{
|
|
|
|
if (!is_tipc_available ()) {
|
|
|
|
TEST_IGNORE_MESSAGE ("tipc is not available");
|
|
|
|
}
|
|
|
|
|
|
|
|
test_bind (socket_, "tipc://<*>", my_endpoint_, len_);
|
|
|
|
}
|
|
|
|
|
2019-02-07 11:01:14 +01:00
|
|
|
#if !defined(ZMQ_HAVE_WINDOWS) && !defined(ZMQ_HAVE_GNU)
|
2019-02-04 05:33:28 -05:00
|
|
|
// utility function to create a random IPC endpoint, similar to what a ipc://*
|
|
|
|
// wildcard binding does, but in a way it can be reused for multiple binds
|
|
|
|
void make_random_ipc_endpoint (char *out_endpoint_)
|
|
|
|
{
|
|
|
|
char random_file[16];
|
|
|
|
strcpy (random_file, "tmpXXXXXX");
|
|
|
|
|
|
|
|
#ifdef HAVE_MKDTEMP
|
|
|
|
TEST_ASSERT_TRUE (mkdtemp (random_file));
|
|
|
|
strcat (random_file, "/ipc");
|
|
|
|
#else
|
|
|
|
int fd = mkstemp (random_file);
|
|
|
|
TEST_ASSERT_TRUE (fd != -1);
|
|
|
|
close (fd);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
strcpy (out_endpoint_, "ipc://");
|
|
|
|
strcat (out_endpoint_, random_file);
|
|
|
|
}
|
2019-02-07 11:01:14 +01:00
|
|
|
#endif
|