2023-06-05 01:16:05 +02:00
|
|
|
/* SPDX-License-Identifier: MPL-2.0 */
|
2014-05-09 15:54:24 +02:00
|
|
|
|
|
|
|
#include "testutil.hpp"
|
2020-02-04 13:19:52 +01:00
|
|
|
#include "testutil_monitoring.hpp"
|
2018-08-23 12:24:36 +02:00
|
|
|
#include "testutil_unity.hpp"
|
|
|
|
|
2019-03-23 13:04:57 +01:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2019-03-24 17:51:28 +01:00
|
|
|
SETUP_TEARDOWN_TESTCONTEXT
|
2014-05-09 15:54:24 +02:00
|
|
|
|
2018-08-23 12:24:36 +02:00
|
|
|
static void test_stream_handshake_timeout_accept ()
|
2014-05-09 15:54:24 +02:00
|
|
|
{
|
2017-05-01 13:11:11 +02:00
|
|
|
char my_endpoint[MAX_SOCKET_STRING];
|
2014-05-09 15:54:24 +02: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 15:54:24 +02: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 15:54:24 +02: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 15:54:24 +02:00
|
|
|
int val, tenth = 100;
|
|
|
|
size_t vsize = sizeof (val);
|
|
|
|
|
|
|
|
// 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 15:54:24 +02: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)));
|
2014-05-09 15:54:24 +02:00
|
|
|
vsize = sizeof (val);
|
|
|
|
// 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 15:54:24 +02: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 15:54:24 +02: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 15:54:24 +02: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 15:54:24 +02: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 13:11:11 +02:00
|
|
|
|
2018-08-23 12:24:36 +02:00
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (stream, my_endpoint));
|
2014-05-09 15:54:24 +02: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 15:54:24 +02: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 15:54:24 +02: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 15:54:24 +02:00
|
|
|
}
|
|
|
|
|
2018-08-23 12:24:36 +02:00
|
|
|
static void test_stream_handshake_timeout_connect ()
|
2014-05-09 15:54:24 +02:00
|
|
|
{
|
2017-05-01 13:11:11 +02:00
|
|
|
char my_endpoint[MAX_SOCKET_STRING];
|
2014-05-09 15:54:24 +02: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 15:54:24 +02: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 15:54:24 +02: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 15:54:24 +02:00
|
|
|
int val, tenth = 100;
|
|
|
|
size_t vsize = sizeof (val);
|
|
|
|
|
|
|
|
// 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 15:54:24 +02: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)));
|
2014-05-09 15:54:24 +02:00
|
|
|
vsize = sizeof (val);
|
|
|
|
// 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 15:54:24 +02: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 15:54:24 +02: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 15:54:24 +02: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 15:54:24 +02: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 15:54:24 +02: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 15:54:24 +02: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 15:54:24 +02: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 15:54:24 +02:00
|
|
|
}
|
|
|
|
|
2018-08-23 12:24:36 +02:00
|
|
|
int main ()
|
2014-05-09 15:54:24 +02: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 15:54:24 +02:00
|
|
|
}
|