2014-01-20 21:18:04 +01:00
|
|
|
/*
|
2017-05-01 13:11:11 +02:00
|
|
|
Copyright (c) 2007-2017 Contributors as noted in the AUTHORS file
|
2014-01-20 21:18:04 +01:00
|
|
|
|
2015-06-02 22:33:55 +02:00
|
|
|
This file is part of libzmq, the ZeroMQ core engine in C++.
|
2014-01-20 21:18:04 +01: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-01-20 21:18:04 +01: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-01-20 21:18:04 +01: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-16 18:09:38 +02:00
|
|
|
#include "testutil_unity.hpp"
|
2014-01-20 21:18:04 +01:00
|
|
|
|
2019-03-23 13:04:57 +01:00
|
|
|
#include <string.h>
|
|
|
|
|
2018-08-16 18:09:38 +02:00
|
|
|
void setUp ()
|
|
|
|
{
|
|
|
|
setup_test_context ();
|
|
|
|
}
|
|
|
|
|
|
|
|
void tearDown ()
|
|
|
|
{
|
|
|
|
teardown_test_context ();
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *rconn1routing_id = "conn1";
|
|
|
|
const char *x_routing_id = "X";
|
|
|
|
const char *y_routing_id = "Y";
|
|
|
|
const char *z_routing_id = "Z";
|
2014-01-20 21:18:04 +01:00
|
|
|
|
2018-02-01 11:46:09 +01:00
|
|
|
void test_stream_2_stream ()
|
|
|
|
{
|
2014-01-20 21:18:04 +01:00
|
|
|
char buff[256];
|
2018-08-16 18:09:38 +02:00
|
|
|
const char msg[] = "hi 1";
|
|
|
|
const int disabled = 0;
|
|
|
|
const int zero = 0;
|
2017-05-01 13:11:11 +02:00
|
|
|
char my_endpoint[MAX_SOCKET_STRING];
|
2014-01-20 21:18:04 +01:00
|
|
|
|
2014-01-21 20:24:39 +01:00
|
|
|
// Set up listener STREAM.
|
2018-08-16 18:09:38 +02:00
|
|
|
void *rbind = test_context_socket (ZMQ_STREAM);
|
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (
|
|
|
|
zmq_setsockopt (rbind, ZMQ_STREAM_NOTIFY, &disabled, sizeof (disabled)));
|
|
|
|
|
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (
|
|
|
|
zmq_setsockopt (rbind, ZMQ_LINGER, &zero, sizeof zero));
|
|
|
|
bind_loopback_ipv4 (rbind, my_endpoint, sizeof my_endpoint);
|
2014-01-20 21:18:04 +01:00
|
|
|
|
2014-01-21 20:24:39 +01:00
|
|
|
// Set up connection stream.
|
2018-08-16 18:09:38 +02:00
|
|
|
void *rconn1 = test_context_socket (ZMQ_STREAM);
|
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (
|
|
|
|
zmq_setsockopt (rconn1, ZMQ_LINGER, &zero, sizeof zero));
|
2015-08-20 16:46:34 +02:00
|
|
|
|
2014-01-21 20:24:39 +01:00
|
|
|
// Do the connection.
|
2018-08-16 18:09:38 +02:00
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_setsockopt (rconn1, ZMQ_CONNECT_ROUTING_ID,
|
|
|
|
rconn1routing_id,
|
|
|
|
strlen (rconn1routing_id)));
|
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (rconn1, my_endpoint));
|
2014-01-21 20:24:39 +01:00
|
|
|
|
2018-02-01 11:46:09 +01:00
|
|
|
/* Uncomment to test assert on duplicate routing id.
|
2014-01-21 20:24:39 +01:00
|
|
|
// Test duplicate connect attempt.
|
2018-08-16 18:09:38 +02:00
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_setsockopt (rconn1, ZMQ_CONNECT_ROUTING_ID, rconn1routing_id, strlen(rconn1routing_id)));
|
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (rconn1, bindip));
|
2015-08-20 16:46:34 +02:00
|
|
|
*/
|
2014-01-21 20:24:39 +01:00
|
|
|
// Send data to the bound stream.
|
2018-08-16 18:09:38 +02:00
|
|
|
send_string_expect_success (rconn1, rconn1routing_id, ZMQ_SNDMORE);
|
|
|
|
send_string_expect_success (rconn1, msg, 0);
|
2014-01-21 20:24:39 +01:00
|
|
|
|
|
|
|
// Accept data on the bound stream.
|
2018-08-16 18:09:38 +02:00
|
|
|
TEST_ASSERT_GREATER_THAN (
|
|
|
|
0, TEST_ASSERT_SUCCESS_ERRNO (zmq_recv (rbind, buff, 256, 0)));
|
|
|
|
TEST_ASSERT_EQUAL (0, buff[0]); // an auto-generated routing id
|
|
|
|
recv_string_expect_success (rbind, msg, 0);
|
2014-01-21 20:24:39 +01:00
|
|
|
|
2015-01-23 15:25:40 +01:00
|
|
|
// Handle close of the socket.
|
2018-08-16 18:09:38 +02:00
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_unbind (rbind, my_endpoint));
|
|
|
|
test_context_socket_close (rbind);
|
|
|
|
test_context_socket_close (rconn1);
|
2014-01-20 21:18:04 +01:00
|
|
|
}
|
2014-01-21 20:24:39 +01:00
|
|
|
|
2018-05-27 13:01:36 +02:00
|
|
|
void test_router_2_router (bool named_)
|
2018-02-01 11:46:09 +01:00
|
|
|
{
|
2014-01-20 21:18:04 +01:00
|
|
|
char buff[256];
|
2018-08-16 18:09:38 +02:00
|
|
|
const char msg[] = "hi 1";
|
|
|
|
const int zero = 0;
|
2017-05-01 13:11:11 +02:00
|
|
|
char my_endpoint[MAX_SOCKET_STRING];
|
2014-01-21 20:24:39 +01:00
|
|
|
|
|
|
|
// Create bind socket.
|
2018-08-16 18:09:38 +02:00
|
|
|
void *rbind = test_context_socket (ZMQ_ROUTER);
|
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (
|
|
|
|
zmq_setsockopt (rbind, ZMQ_LINGER, &zero, sizeof (zero)));
|
|
|
|
bind_loopback_ipv4 (rbind, my_endpoint, sizeof my_endpoint);
|
2014-01-21 20:24:39 +01:00
|
|
|
|
|
|
|
// Create connection socket.
|
2018-08-16 18:09:38 +02:00
|
|
|
void *rconn1 = test_context_socket (ZMQ_ROUTER);
|
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (
|
|
|
|
zmq_setsockopt (rconn1, ZMQ_LINGER, &zero, sizeof (zero)));
|
2014-01-21 20:24:39 +01:00
|
|
|
|
|
|
|
// If we're in named mode, set some identities.
|
2018-05-27 13:01:36 +02:00
|
|
|
if (named_) {
|
2018-08-16 18:09:38 +02:00
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (
|
|
|
|
zmq_setsockopt (rbind, ZMQ_ROUTING_ID, x_routing_id, 1));
|
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (
|
|
|
|
zmq_setsockopt (rconn1, ZMQ_ROUTING_ID, y_routing_id, 1));
|
2014-01-20 21:18:04 +01:00
|
|
|
}
|
2014-01-21 20:24:39 +01:00
|
|
|
|
2017-09-07 09:29:46 +02:00
|
|
|
// Make call to connect using a connect_routing_id.
|
2018-08-16 18:09:38 +02:00
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_setsockopt (rconn1, ZMQ_CONNECT_ROUTING_ID,
|
|
|
|
rconn1routing_id,
|
|
|
|
strlen (rconn1routing_id)));
|
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (rconn1, my_endpoint));
|
2018-02-01 11:46:09 +01:00
|
|
|
/* Uncomment to test assert on duplicate routing id
|
2014-01-21 20:24:39 +01:00
|
|
|
// Test duplicate connect attempt.
|
2018-08-16 18:09:38 +02:00
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_setsockopt (rconn1, ZMQ_CONNECT_ROUTING_ID, rconn1routing_id, strlen (rconn1routing_id)));
|
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (rconn1, bindip));
|
2014-01-21 20:43:34 +01:00
|
|
|
*/
|
2014-01-21 20:24:39 +01:00
|
|
|
// Send some data.
|
2018-08-16 18:09:38 +02:00
|
|
|
|
|
|
|
send_string_expect_success (rconn1, rconn1routing_id, ZMQ_SNDMORE);
|
|
|
|
send_string_expect_success (rconn1, msg, 0);
|
2014-01-21 20:24:39 +01:00
|
|
|
|
|
|
|
// Receive the name.
|
2018-08-16 18:09:38 +02:00
|
|
|
const int routing_id_len = zmq_recv (rbind, buff, 256, 0);
|
|
|
|
if (named_) {
|
|
|
|
TEST_ASSERT_EQUAL_INT (strlen (y_routing_id), routing_id_len);
|
|
|
|
TEST_ASSERT_EQUAL_STRING_LEN (y_routing_id, buff, routing_id_len);
|
|
|
|
} else {
|
|
|
|
TEST_ASSERT_TRUE (routing_id_len && 0 == buff[0]);
|
|
|
|
}
|
2014-01-21 20:24:39 +01:00
|
|
|
|
|
|
|
// Receive the data.
|
2018-08-16 18:09:38 +02:00
|
|
|
recv_string_expect_success (rbind, msg, 0);
|
2014-01-21 20:24:39 +01:00
|
|
|
|
|
|
|
// Send some data back.
|
2018-08-16 18:09:38 +02:00
|
|
|
const int ret = zmq_send (rbind, buff, routing_id_len, ZMQ_SNDMORE);
|
|
|
|
TEST_ASSERT_EQUAL_INT (routing_id_len, ret);
|
|
|
|
send_string_expect_success (rbind, "ok", 0);
|
2015-08-20 16:46:34 +02:00
|
|
|
|
2014-01-21 20:24:39 +01:00
|
|
|
// If bound socket identity naming a problem, we'll likely see something funky here.
|
2018-08-16 18:09:38 +02:00
|
|
|
recv_string_expect_success (rconn1, rconn1routing_id, 0);
|
|
|
|
recv_string_expect_success (rconn1, "ok", 0);
|
|
|
|
|
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_unbind (rbind, my_endpoint));
|
|
|
|
test_context_socket_close (rbind);
|
|
|
|
test_context_socket_close (rconn1);
|
2014-01-20 21:18:04 +01:00
|
|
|
}
|
|
|
|
|
2018-07-24 20:48:43 +02:00
|
|
|
void test_router_2_router_while_receiving ()
|
|
|
|
{
|
|
|
|
char buff[256];
|
2018-08-16 18:09:38 +02:00
|
|
|
const char msg[] = "hi 1";
|
|
|
|
const int zero = 0;
|
2018-07-24 20:48:43 +02:00
|
|
|
char x_endpoint[MAX_SOCKET_STRING];
|
|
|
|
char z_endpoint[MAX_SOCKET_STRING];
|
|
|
|
|
|
|
|
// Create xbind socket.
|
2018-08-16 18:09:38 +02:00
|
|
|
void *xbind = test_context_socket (ZMQ_ROUTER);
|
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (
|
|
|
|
zmq_setsockopt (xbind, ZMQ_LINGER, &zero, sizeof (zero)));
|
|
|
|
bind_loopback_ipv4 (xbind, x_endpoint, sizeof x_endpoint);
|
2018-07-24 20:48:43 +02:00
|
|
|
|
|
|
|
// Create zbind socket.
|
2018-08-16 18:09:38 +02:00
|
|
|
void *zbind = test_context_socket (ZMQ_ROUTER);
|
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (
|
|
|
|
zmq_setsockopt (zbind, ZMQ_LINGER, &zero, sizeof (zero)));
|
|
|
|
bind_loopback_ipv4 (zbind, z_endpoint, sizeof z_endpoint);
|
2018-07-24 20:48:43 +02:00
|
|
|
|
|
|
|
// Create connection socket.
|
2018-08-16 18:09:38 +02:00
|
|
|
void *yconn = test_context_socket (ZMQ_ROUTER);
|
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (
|
|
|
|
zmq_setsockopt (yconn, ZMQ_LINGER, &zero, sizeof (zero)));
|
|
|
|
|
|
|
|
// set identities for each socket
|
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_setsockopt (
|
|
|
|
xbind, ZMQ_ROUTING_ID, x_routing_id, strlen (x_routing_id)));
|
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (
|
|
|
|
zmq_setsockopt (yconn, ZMQ_ROUTING_ID, y_routing_id, 2));
|
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_setsockopt (
|
|
|
|
zbind, ZMQ_ROUTING_ID, z_routing_id, strlen (z_routing_id)));
|
2018-07-24 20:48:43 +02:00
|
|
|
|
|
|
|
// Connect Y to X using a routing id
|
2018-08-16 18:09:38 +02:00
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_setsockopt (
|
|
|
|
yconn, ZMQ_CONNECT_ROUTING_ID, x_routing_id, strlen (x_routing_id)));
|
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (yconn, x_endpoint));
|
2018-07-24 20:48:43 +02:00
|
|
|
|
|
|
|
// Send some data from Y to X.
|
2018-08-16 18:09:38 +02:00
|
|
|
send_string_expect_success (yconn, x_routing_id, ZMQ_SNDMORE);
|
|
|
|
send_string_expect_success (yconn, msg, 0);
|
2018-07-24 20:48:43 +02:00
|
|
|
|
|
|
|
// wait for the Y->X message to be received
|
|
|
|
msleep (SETTLE_TIME);
|
|
|
|
|
|
|
|
// Now X tries to connect to Z and send a message
|
2018-08-16 18:09:38 +02:00
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_setsockopt (
|
|
|
|
xbind, ZMQ_CONNECT_ROUTING_ID, z_routing_id, strlen (z_routing_id)));
|
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (xbind, z_endpoint));
|
2018-07-24 20:48:43 +02:00
|
|
|
|
|
|
|
// Try to send some data from X to Z.
|
2018-08-16 18:09:38 +02:00
|
|
|
send_string_expect_success (xbind, z_routing_id, ZMQ_SNDMORE);
|
|
|
|
send_string_expect_success (xbind, msg, 0);
|
2018-07-24 20:48:43 +02:00
|
|
|
|
|
|
|
// wait for the X->Z message to be received (so that our non-blocking check will actually
|
|
|
|
// fail if the message is routed to Y)
|
|
|
|
msleep (SETTLE_TIME);
|
|
|
|
|
|
|
|
// nothing should have been received on the Y socket
|
2018-08-16 18:09:38 +02:00
|
|
|
TEST_ASSERT_FAILURE_ERRNO (EAGAIN,
|
|
|
|
zmq_recv (yconn, buff, 256, ZMQ_DONTWAIT));
|
2018-07-24 20:48:43 +02:00
|
|
|
|
|
|
|
// the message should have been received on the Z socket
|
2018-08-16 18:09:38 +02:00
|
|
|
recv_string_expect_success (zbind, x_routing_id, 0);
|
|
|
|
recv_string_expect_success (zbind, msg, 0);
|
|
|
|
|
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_unbind (xbind, x_endpoint));
|
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_unbind (zbind, z_endpoint));
|
|
|
|
|
|
|
|
test_context_socket_close (yconn);
|
|
|
|
test_context_socket_close (xbind);
|
|
|
|
test_context_socket_close (zbind);
|
2018-07-24 20:48:43 +02:00
|
|
|
}
|
|
|
|
|
2018-08-16 18:09:38 +02:00
|
|
|
void test_router_2_router_unnamed ()
|
2014-01-20 21:18:04 +01:00
|
|
|
{
|
2014-03-17 14:41:02 +01:00
|
|
|
test_router_2_router (false);
|
2018-08-16 18:09:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void test_router_2_router_named ()
|
|
|
|
{
|
2014-03-17 14:41:02 +01:00
|
|
|
test_router_2_router (true);
|
2018-08-16 18:09:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int main ()
|
|
|
|
{
|
|
|
|
setup_test_environment ();
|
2014-03-17 14:41:02 +01:00
|
|
|
|
2018-08-16 18:09:38 +02:00
|
|
|
UNITY_BEGIN ();
|
|
|
|
RUN_TEST (test_stream_2_stream);
|
|
|
|
RUN_TEST (test_router_2_router_unnamed);
|
|
|
|
RUN_TEST (test_router_2_router_named);
|
|
|
|
RUN_TEST (test_router_2_router_while_receiving);
|
|
|
|
return UNITY_END ();
|
2014-01-20 21:18:04 +01:00
|
|
|
}
|