2014-05-09 13:54:24 +00:00
|
|
|
/*
|
2017-05-01 12:11:11 +01:00
|
|
|
Copyright (c) 2007-2017 Contributors as noted in the AUTHORS file
|
2014-05-09 13:54:24 +00:00
|
|
|
|
2015-06-02 22:33:55 +02:00
|
|
|
This file is part of libzmq, the ZeroMQ core engine in C++.
|
2014-05-09 13:54:24 +00: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
|
2014-05-09 13:54:24 +00: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.
|
2014-05-09 13:54:24 +00:00
|
|
|
|
|
|
|
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 "testutil.hpp"
|
2018-08-23 12:24:36 +02:00
|
|
|
#include "testutil_unity.hpp"
|
|
|
|
|
2019-03-23 08:04:57 -04:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2018-08-23 12:24:36 +02:00
|
|
|
void setUp ()
|
|
|
|
{
|
|
|
|
setup_test_context ();
|
|
|
|
}
|
|
|
|
|
|
|
|
void tearDown ()
|
|
|
|
{
|
|
|
|
teardown_test_context ();
|
|
|
|
}
|
2014-05-09 13:54:24 +00:00
|
|
|
|
|
|
|
// Read one event off the monitor socket; return value and address
|
|
|
|
// by reference, if not null, and event number by value. Returns -1
|
|
|
|
// in case of error.
|
|
|
|
|
2018-05-27 07:01:36 -04:00
|
|
|
static int get_monitor_event (void *monitor_, int *value_, char **address_)
|
2014-05-09 13:54:24 +00:00
|
|
|
{
|
|
|
|
// First frame in message contains event number and value
|
2018-02-01 11:46:09 +01:00
|
|
|
zmq_msg_t msg;
|
2014-05-09 13:54:24 +00:00
|
|
|
zmq_msg_init (&msg);
|
2018-05-27 07:01:36 -04:00
|
|
|
if (zmq_msg_recv (&msg, monitor_, 0) == -1)
|
2018-02-01 11:46:09 +01:00
|
|
|
return -1; // Interruped, presumably
|
2018-08-23 12:24:36 +02:00
|
|
|
TEST_ASSERT_TRUE (zmq_msg_more (&msg));
|
2018-02-01 11:46:09 +01:00
|
|
|
|
2014-05-09 13:54:24 +00:00
|
|
|
uint8_t *data = (uint8_t *) zmq_msg_data (&msg);
|
|
|
|
uint16_t event = *(uint16_t *) (data);
|
2018-05-27 07:01:36 -04:00
|
|
|
if (value_)
|
|
|
|
*value_ = *(uint32_t *) (data + 2);
|
2014-05-09 13:54:24 +00:00
|
|
|
|
|
|
|
// Second frame in message contains event address
|
|
|
|
zmq_msg_init (&msg);
|
2018-05-27 07:01:36 -04:00
|
|
|
if (zmq_msg_recv (&msg, monitor_, 0) == -1)
|
2018-02-01 11:46:09 +01:00
|
|
|
return -1; // Interruped, presumably
|
2018-08-23 12:24:36 +02:00
|
|
|
TEST_ASSERT_TRUE (!zmq_msg_more (&msg));
|
2018-02-01 11:46:09 +01:00
|
|
|
|
2018-05-27 07:01:36 -04:00
|
|
|
if (address_) {
|
2014-05-09 13:54:24 +00:00
|
|
|
uint8_t *data = (uint8_t *) zmq_msg_data (&msg);
|
|
|
|
size_t size = zmq_msg_size (&msg);
|
2018-05-27 07:01:36 -04:00
|
|
|
*address_ = (char *) malloc (size + 1);
|
|
|
|
memcpy (*address_, data, size);
|
|
|
|
*address_[size] = 0;
|
2014-05-09 13:54:24 +00:00
|
|
|
}
|
|
|
|
return event;
|
|
|
|
}
|
|
|
|
|
2018-08-23 12:24:36 +02:00
|
|
|
static void test_stream_handshake_timeout_accept ()
|
2014-05-09 13:54:24 +00:00
|
|
|
{
|
2017-05-01 12:11:11 +01:00
|
|
|
char my_endpoint[MAX_SOCKET_STRING];
|
2014-05-09 13:54:24 +00:00
|
|
|
|
|
|
|
// We use this socket in raw mode, to make a connection and send nothing
|
2018-08-23 12:24:36 +02:00
|
|
|
void *stream = test_context_socket (ZMQ_STREAM);
|
2014-05-09 13:54:24 +00:00
|
|
|
|
|
|
|
int zero = 0;
|
2018-08-23 12:24:36 +02:00
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (
|
|
|
|
zmq_setsockopt (stream, ZMQ_LINGER, &zero, sizeof (zero)));
|
2014-05-09 13:54:24 +00:00
|
|
|
|
|
|
|
// We'll be using this socket to test TCP stream handshake timeout
|
2018-08-23 12:24:36 +02:00
|
|
|
void *dealer = test_context_socket (ZMQ_DEALER);
|
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (
|
|
|
|
zmq_setsockopt (dealer, ZMQ_LINGER, &zero, sizeof (zero)));
|
2014-05-09 13:54:24 +00:00
|
|
|
int val, tenth = 100;
|
2018-02-01 11:46:09 +01:00
|
|
|
size_t vsize = sizeof (val);
|
2014-05-09 13:54:24 +00:00
|
|
|
|
|
|
|
// check for the expected default handshake timeout value - 30 sec
|
2018-08-23 12:24:36 +02:00
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (
|
|
|
|
zmq_getsockopt (dealer, ZMQ_HANDSHAKE_IVL, &val, &vsize));
|
|
|
|
TEST_ASSERT_EQUAL (sizeof (val), vsize);
|
|
|
|
TEST_ASSERT_EQUAL_INT (30000, val);
|
2014-05-09 13:54:24 +00:00
|
|
|
// make handshake timeout faster - 1/10 sec
|
2018-08-23 12:24:36 +02:00
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (
|
|
|
|
zmq_setsockopt (dealer, ZMQ_HANDSHAKE_IVL, &tenth, sizeof (tenth)));
|
2018-02-01 11:46:09 +01:00
|
|
|
vsize = sizeof (val);
|
2014-05-09 13:54:24 +00:00
|
|
|
// make sure zmq_setsockopt changed the value
|
2018-08-23 12:24:36 +02:00
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (
|
|
|
|
zmq_getsockopt (dealer, ZMQ_HANDSHAKE_IVL, &val, &vsize));
|
|
|
|
TEST_ASSERT_EQUAL (sizeof (val), vsize);
|
|
|
|
TEST_ASSERT_EQUAL_INT (tenth, val);
|
2014-05-09 13:54:24 +00:00
|
|
|
|
|
|
|
// Create and connect a socket for collecting monitor events on dealer
|
2018-08-23 12:24:36 +02:00
|
|
|
void *dealer_mon = test_context_socket (ZMQ_PAIR);
|
2014-05-09 13:54:24 +00:00
|
|
|
|
2018-08-23 12:24:36 +02:00
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_socket_monitor (
|
|
|
|
dealer, "inproc://monitor-dealer",
|
|
|
|
ZMQ_EVENT_CONNECTED | ZMQ_EVENT_DISCONNECTED | ZMQ_EVENT_ACCEPTED));
|
2014-05-09 13:54:24 +00:00
|
|
|
|
|
|
|
// Connect to the inproc endpoint so we'll get events
|
2018-08-23 12:24:36 +02:00
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (
|
|
|
|
zmq_connect (dealer_mon, "inproc://monitor-dealer"));
|
2014-05-09 13:54:24 +00:00
|
|
|
|
|
|
|
// bind dealer socket to accept connection from non-sending stream socket
|
2018-08-23 12:24:36 +02:00
|
|
|
bind_loopback_ipv4 (dealer, my_endpoint, sizeof my_endpoint);
|
2017-05-01 12:11:11 +01:00
|
|
|
|
2018-08-23 12:24:36 +02:00
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (stream, my_endpoint));
|
2014-05-09 13:54:24 +00:00
|
|
|
|
|
|
|
// we should get ZMQ_EVENT_ACCEPTED and then ZMQ_EVENT_DISCONNECTED
|
|
|
|
int event = get_monitor_event (dealer_mon, NULL, NULL);
|
2018-08-23 12:24:36 +02:00
|
|
|
TEST_ASSERT_EQUAL_INT (ZMQ_EVENT_ACCEPTED, event);
|
2014-05-09 13:54:24 +00:00
|
|
|
event = get_monitor_event (dealer_mon, NULL, NULL);
|
2018-08-23 12:24:36 +02:00
|
|
|
TEST_ASSERT_EQUAL_INT (ZMQ_EVENT_DISCONNECTED, event);
|
2014-05-09 13:54:24 +00:00
|
|
|
|
2018-08-23 12:24:36 +02:00
|
|
|
test_context_socket_close (dealer);
|
|
|
|
test_context_socket_close (dealer_mon);
|
|
|
|
test_context_socket_close (stream);
|
2014-05-09 13:54:24 +00:00
|
|
|
}
|
|
|
|
|
2018-08-23 12:24:36 +02:00
|
|
|
static void test_stream_handshake_timeout_connect ()
|
2014-05-09 13:54:24 +00:00
|
|
|
{
|
2017-05-01 12:11:11 +01:00
|
|
|
char my_endpoint[MAX_SOCKET_STRING];
|
2014-05-09 13:54:24 +00:00
|
|
|
|
|
|
|
// We use this socket in raw mode, to accept a connection and send nothing
|
2018-08-23 12:24:36 +02:00
|
|
|
void *stream = test_context_socket (ZMQ_STREAM);
|
2014-05-09 13:54:24 +00:00
|
|
|
|
|
|
|
int zero = 0;
|
2018-08-23 12:24:36 +02:00
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (
|
|
|
|
zmq_setsockopt (stream, ZMQ_LINGER, &zero, sizeof (zero)));
|
|
|
|
|
|
|
|
bind_loopback_ipv4 (stream, my_endpoint, sizeof my_endpoint);
|
2014-05-09 13:54:24 +00:00
|
|
|
|
|
|
|
// We'll be using this socket to test TCP stream handshake timeout
|
2018-08-23 12:24:36 +02:00
|
|
|
void *dealer = test_context_socket (ZMQ_DEALER);
|
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (
|
|
|
|
zmq_setsockopt (dealer, ZMQ_LINGER, &zero, sizeof (zero)));
|
2014-05-09 13:54:24 +00:00
|
|
|
int val, tenth = 100;
|
2018-02-01 11:46:09 +01:00
|
|
|
size_t vsize = sizeof (val);
|
2014-05-09 13:54:24 +00:00
|
|
|
|
|
|
|
// check for the expected default handshake timeout value - 30 sec
|
2018-08-23 12:24:36 +02:00
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (
|
|
|
|
zmq_getsockopt (dealer, ZMQ_HANDSHAKE_IVL, &val, &vsize));
|
|
|
|
TEST_ASSERT_EQUAL (sizeof (val), vsize);
|
|
|
|
TEST_ASSERT_EQUAL_INT (30000, val);
|
2014-05-09 13:54:24 +00:00
|
|
|
// make handshake timeout faster - 1/10 sec
|
2018-08-23 12:24:36 +02:00
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (
|
|
|
|
zmq_setsockopt (dealer, ZMQ_HANDSHAKE_IVL, &tenth, sizeof (tenth)));
|
2018-02-01 11:46:09 +01:00
|
|
|
vsize = sizeof (val);
|
2014-05-09 13:54:24 +00:00
|
|
|
// make sure zmq_setsockopt changed the value
|
2018-08-23 12:24:36 +02:00
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (
|
|
|
|
zmq_getsockopt (dealer, ZMQ_HANDSHAKE_IVL, &val, &vsize));
|
|
|
|
TEST_ASSERT_EQUAL (sizeof (val), vsize);
|
|
|
|
TEST_ASSERT_EQUAL_INT (tenth, val);
|
2014-05-09 13:54:24 +00:00
|
|
|
|
|
|
|
// Create and connect a socket for collecting monitor events on dealer
|
2018-08-23 12:24:36 +02:00
|
|
|
void *dealer_mon = test_context_socket (ZMQ_PAIR);
|
2014-05-09 13:54:24 +00:00
|
|
|
|
2018-08-23 12:24:36 +02:00
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_socket_monitor (
|
|
|
|
dealer, "inproc://monitor-dealer",
|
|
|
|
ZMQ_EVENT_CONNECTED | ZMQ_EVENT_DISCONNECTED | ZMQ_EVENT_ACCEPTED));
|
2014-05-09 13:54:24 +00:00
|
|
|
|
|
|
|
// Connect to the inproc endpoint so we'll get events
|
2018-08-23 12:24:36 +02:00
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (
|
|
|
|
zmq_connect (dealer_mon, "inproc://monitor-dealer"));
|
2014-05-09 13:54:24 +00:00
|
|
|
|
|
|
|
// connect dealer socket to non-sending stream socket
|
2018-08-23 12:24:36 +02:00
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (dealer, my_endpoint));
|
2014-05-09 13:54:24 +00:00
|
|
|
|
|
|
|
// we should get ZMQ_EVENT_CONNECTED and then ZMQ_EVENT_DISCONNECTED
|
|
|
|
int event = get_monitor_event (dealer_mon, NULL, NULL);
|
2018-08-23 12:24:36 +02:00
|
|
|
TEST_ASSERT_EQUAL_INT (ZMQ_EVENT_CONNECTED, event);
|
2014-05-09 13:54:24 +00:00
|
|
|
event = get_monitor_event (dealer_mon, NULL, NULL);
|
2018-08-23 12:24:36 +02:00
|
|
|
TEST_ASSERT_EQUAL_INT (ZMQ_EVENT_DISCONNECTED, event);
|
2014-05-09 13:54:24 +00:00
|
|
|
|
2018-08-23 12:24:36 +02:00
|
|
|
test_context_socket_close (dealer);
|
|
|
|
test_context_socket_close (dealer_mon);
|
|
|
|
test_context_socket_close (stream);
|
2014-05-09 13:54:24 +00:00
|
|
|
}
|
|
|
|
|
2018-08-23 12:24:36 +02:00
|
|
|
int main ()
|
2014-05-09 13:54:24 +00:00
|
|
|
{
|
2018-02-01 11:46:09 +01:00
|
|
|
setup_test_environment ();
|
2018-08-23 12:24:36 +02:00
|
|
|
|
|
|
|
UNITY_BEGIN ();
|
|
|
|
RUN_TEST (test_stream_handshake_timeout_accept);
|
|
|
|
RUN_TEST (test_stream_handshake_timeout_connect);
|
|
|
|
return UNITY_END ();
|
2014-05-09 13:54:24 +00:00
|
|
|
}
|