2014-01-12 22:36:47 -05:00
|
|
|
/*
|
2017-05-01 12:11:11 +01:00
|
|
|
Copyright (c) 2007-2017 Contributors as noted in the AUTHORS file
|
2014-01-12 22:36:47 -05:00
|
|
|
|
2015-06-02 22:33:55 +02:00
|
|
|
This file is part of libzmq, the ZeroMQ core engine in C++.
|
2014-01-12 22:36:47 -05: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-12 22:36:47 -05: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-12 22:36:47 -05: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:13:23 +02:00
|
|
|
#include "testutil_unity.hpp"
|
|
|
|
|
2019-03-23 08:04:57 -04:00
|
|
|
#include <string.h>
|
|
|
|
|
2019-03-24 12:51:28 -04:00
|
|
|
SETUP_TEARDOWN_TESTCONTEXT
|
2014-01-12 22:36:47 -05:00
|
|
|
|
|
|
|
static const int SERVER = 0;
|
|
|
|
static const int CLIENT = 1;
|
|
|
|
|
|
|
|
struct test_message_t
|
|
|
|
{
|
|
|
|
int turn;
|
|
|
|
const char *text;
|
|
|
|
};
|
|
|
|
|
|
|
|
// NOTE: messages are sent without null terminator.
|
|
|
|
const test_message_t dialog[] = {
|
|
|
|
{CLIENT, "i can haz cheez burger?"},
|
|
|
|
{SERVER, "y u no disonnect?"},
|
|
|
|
{CLIENT, ""},
|
|
|
|
};
|
|
|
|
const int steps = sizeof (dialog) / sizeof (dialog[0]);
|
|
|
|
|
2018-05-27 07:01:36 -04:00
|
|
|
bool has_more (void *socket_)
|
2014-01-12 22:36:47 -05:00
|
|
|
{
|
|
|
|
int more = 0;
|
|
|
|
size_t more_size = sizeof (more);
|
2018-05-27 07:01:36 -04:00
|
|
|
int rc = zmq_getsockopt (socket_, ZMQ_RCVMORE, &more, &more_size);
|
2015-08-20 07:46:34 -07:00
|
|
|
if (rc != 0)
|
2014-01-12 22:36:47 -05:00
|
|
|
return false;
|
|
|
|
return more != 0;
|
|
|
|
}
|
|
|
|
|
2018-08-23 12:13:23 +02:00
|
|
|
void test_stream_disconnect ()
|
2014-01-12 22:36:47 -05:00
|
|
|
{
|
2017-05-01 12:11:11 +01:00
|
|
|
size_t len = MAX_SOCKET_STRING;
|
|
|
|
char bind_endpoint[MAX_SOCKET_STRING];
|
|
|
|
char connect_endpoint[MAX_SOCKET_STRING];
|
2015-01-23 15:25:40 +01:00
|
|
|
void *sockets[2];
|
2014-01-12 22:36:47 -05:00
|
|
|
|
2018-08-23 12:13:23 +02:00
|
|
|
sockets[SERVER] = test_context_socket (ZMQ_STREAM);
|
2015-01-23 15:25:40 +01:00
|
|
|
int enabled = 1;
|
2018-08-23 12:13:23 +02:00
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_setsockopt (
|
|
|
|
sockets[SERVER], ZMQ_STREAM_NOTIFY, &enabled, sizeof (enabled)));
|
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (sockets[SERVER], "tcp://0.0.0.0:*"));
|
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (
|
|
|
|
zmq_getsockopt (sockets[SERVER], ZMQ_LAST_ENDPOINT, bind_endpoint, &len));
|
2017-05-01 12:11:11 +01:00
|
|
|
|
|
|
|
// Apparently Windows can't connect to 0.0.0.0. A better fix would be welcome.
|
|
|
|
#ifdef ZMQ_HAVE_WINDOWS
|
2023-01-24 12:54:48 +01:00
|
|
|
snprintf (connect_endpoint, MAX_SOCKET_STRING * sizeof (char),
|
|
|
|
"tcp://127.0.0.1:%s", strrchr (bind_endpoint, ':') + 1);
|
2017-05-01 12:11:11 +01:00
|
|
|
#else
|
|
|
|
strcpy (connect_endpoint, bind_endpoint);
|
|
|
|
#endif
|
2014-01-12 22:36:47 -05:00
|
|
|
|
2018-08-23 12:13:23 +02:00
|
|
|
sockets[CLIENT] = test_context_socket (ZMQ_STREAM);
|
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_setsockopt (
|
|
|
|
sockets[CLIENT], ZMQ_STREAM_NOTIFY, &enabled, sizeof (enabled)));
|
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (sockets[CLIENT], connect_endpoint));
|
2014-01-12 22:36:47 -05:00
|
|
|
|
2014-01-17 23:21:42 +01:00
|
|
|
// wait for connect notification
|
2017-09-07 11:21:13 +02:00
|
|
|
// Server: Grab the 1st frame (peer routing id).
|
2014-01-17 23:21:42 +01:00
|
|
|
zmq_msg_t peer_frame;
|
2018-08-23 12:13:23 +02:00
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_init (&peer_frame));
|
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_recv (&peer_frame, sockets[SERVER], 0));
|
|
|
|
TEST_ASSERT_GREATER_THAN_INT (0, zmq_msg_size (&peer_frame));
|
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_close (&peer_frame));
|
|
|
|
TEST_ASSERT_TRUE (has_more (sockets[SERVER]));
|
2014-01-17 23:21:42 +01:00
|
|
|
|
|
|
|
// Server: Grab the 2nd frame (actual payload).
|
|
|
|
zmq_msg_t data_frame;
|
2018-08-23 12:13:23 +02:00
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_init (&data_frame));
|
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_recv (&data_frame, sockets[SERVER], 0));
|
|
|
|
TEST_ASSERT_EQUAL_INT (0, zmq_msg_size (&data_frame));
|
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_close (&data_frame));
|
2014-01-17 23:21:42 +01:00
|
|
|
|
2017-09-07 11:21:13 +02:00
|
|
|
// Client: Grab the 1st frame (peer routing id).
|
2018-08-23 12:13:23 +02:00
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_init (&peer_frame));
|
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_recv (&peer_frame, sockets[CLIENT], 0));
|
|
|
|
TEST_ASSERT_GREATER_THAN_INT (0, zmq_msg_size (&peer_frame));
|
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_close (&peer_frame));
|
|
|
|
TEST_ASSERT_TRUE (has_more (sockets[CLIENT]));
|
2014-01-17 23:21:42 +01:00
|
|
|
|
|
|
|
// Client: Grab the 2nd frame (actual payload).
|
2018-08-23 12:13:23 +02:00
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_init (&data_frame));
|
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_recv (&data_frame, sockets[CLIENT], 0));
|
|
|
|
TEST_ASSERT_EQUAL_INT (0, zmq_msg_size (&data_frame));
|
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_close (&data_frame));
|
2014-01-12 22:36:47 -05:00
|
|
|
|
|
|
|
// Send initial message.
|
|
|
|
char blob_data[256];
|
|
|
|
size_t blob_size = sizeof (blob_data);
|
2018-08-23 12:13:23 +02:00
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (
|
|
|
|
zmq_getsockopt (sockets[CLIENT], ZMQ_ROUTING_ID, blob_data, &blob_size));
|
|
|
|
TEST_ASSERT_GREATER_THAN (0, blob_size);
|
2014-01-12 22:36:47 -05:00
|
|
|
zmq_msg_t msg;
|
2018-08-23 12:13:23 +02:00
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_init_size (&msg, blob_size));
|
2014-01-12 22:36:47 -05:00
|
|
|
memcpy (zmq_msg_data (&msg), blob_data, blob_size);
|
2018-08-23 12:13:23 +02:00
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (
|
|
|
|
zmq_msg_send (&msg, sockets[dialog[0].turn], ZMQ_SNDMORE));
|
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_close (&msg));
|
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (
|
|
|
|
zmq_msg_init_size (&msg, strlen (dialog[0].text)));
|
2014-01-17 23:21:42 +01:00
|
|
|
memcpy (zmq_msg_data (&msg), dialog[0].text, strlen (dialog[0].text));
|
2018-08-23 12:13:23 +02:00
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (
|
|
|
|
zmq_msg_send (&msg, sockets[dialog[0].turn], ZMQ_SNDMORE));
|
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_close (&msg));
|
2014-01-12 22:36:47 -05:00
|
|
|
|
|
|
|
// TODO: make sure this loop doesn't loop forever if something is wrong
|
|
|
|
// with the test (or the implementation).
|
|
|
|
|
|
|
|
int step = 0;
|
|
|
|
while (step < steps) {
|
|
|
|
// Wait until something happens.
|
|
|
|
zmq_pollitem_t items[] = {
|
|
|
|
{sockets[SERVER], 0, ZMQ_POLLIN, 0},
|
|
|
|
{sockets[CLIENT], 0, ZMQ_POLLIN, 0},
|
|
|
|
};
|
2018-08-23 12:13:23 +02:00
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_poll (items, 2, 100));
|
2014-01-12 22:36:47 -05:00
|
|
|
|
|
|
|
// Check for data received by the server.
|
|
|
|
if (items[SERVER].revents & ZMQ_POLLIN) {
|
2018-08-23 12:13:23 +02:00
|
|
|
TEST_ASSERT_EQUAL_INT (CLIENT, dialog[step].turn);
|
2014-01-12 22:36:47 -05:00
|
|
|
|
2017-09-07 11:21:13 +02:00
|
|
|
// Grab the 1st frame (peer routing id).
|
2014-01-12 22:36:47 -05:00
|
|
|
zmq_msg_t peer_frame;
|
2018-08-23 12:13:23 +02:00
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_init (&peer_frame));
|
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (
|
|
|
|
zmq_msg_recv (&peer_frame, sockets[SERVER], 0));
|
|
|
|
TEST_ASSERT_GREATER_THAN_INT (0, zmq_msg_size (&peer_frame));
|
|
|
|
TEST_ASSERT_TRUE (has_more (sockets[SERVER]));
|
2014-01-12 22:36:47 -05:00
|
|
|
|
|
|
|
// Grab the 2nd frame (actual payload).
|
|
|
|
zmq_msg_t data_frame;
|
2018-08-23 12:13:23 +02:00
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_init (&data_frame));
|
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (
|
|
|
|
zmq_msg_recv (&data_frame, sockets[SERVER], 0));
|
2014-01-12 22:36:47 -05:00
|
|
|
|
|
|
|
// Make sure payload matches what we expect.
|
2019-12-08 14:26:57 +01:00
|
|
|
const char *const data =
|
|
|
|
static_cast<const char *> (zmq_msg_data (&data_frame));
|
2018-05-14 20:55:57 +02:00
|
|
|
const size_t size = zmq_msg_size (&data_frame);
|
2014-01-12 22:36:47 -05:00
|
|
|
// 0-length frame is a disconnection notification. The server
|
|
|
|
// should receive it as the last step in the dialogue.
|
|
|
|
if (size == 0) {
|
2015-08-20 07:46:34 -07:00
|
|
|
++step;
|
2018-08-23 12:13:23 +02:00
|
|
|
TEST_ASSERT_EQUAL_INT (steps, step);
|
2014-01-12 22:36:47 -05:00
|
|
|
} else {
|
2018-08-23 12:13:23 +02:00
|
|
|
TEST_ASSERT_EQUAL_INT (strlen (dialog[step].text), size);
|
|
|
|
TEST_ASSERT_EQUAL_STRING_LEN (dialog[step].text, data, size);
|
2014-01-17 23:21:42 +01:00
|
|
|
|
2015-08-20 07:46:34 -07:00
|
|
|
++step;
|
2014-01-17 23:21:42 +01:00
|
|
|
|
2018-08-23 12:13:23 +02:00
|
|
|
TEST_ASSERT_LESS_THAN_INT (steps, step);
|
2014-01-12 22:36:47 -05:00
|
|
|
|
|
|
|
// Prepare the response.
|
2018-08-23 12:13:23 +02:00
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_close (&data_frame));
|
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (
|
|
|
|
zmq_msg_init_size (&data_frame, strlen (dialog[step].text)));
|
2014-01-17 23:21:42 +01:00
|
|
|
memcpy (zmq_msg_data (&data_frame), dialog[step].text,
|
2015-08-20 07:46:34 -07:00
|
|
|
zmq_msg_size (&data_frame));
|
2014-01-12 22:36:47 -05:00
|
|
|
|
|
|
|
// Send the response.
|
2018-08-23 12:13:23 +02:00
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (
|
|
|
|
zmq_msg_send (&peer_frame, sockets[SERVER], ZMQ_SNDMORE));
|
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (
|
|
|
|
zmq_msg_send (&data_frame, sockets[SERVER], ZMQ_SNDMORE));
|
2014-01-12 22:36:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Release resources.
|
2018-08-23 12:13:23 +02:00
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_close (&peer_frame));
|
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_close (&data_frame));
|
2014-01-12 22:36:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check for data received by the client.
|
|
|
|
if (items[CLIENT].revents & ZMQ_POLLIN) {
|
2018-08-23 12:13:23 +02:00
|
|
|
TEST_ASSERT_EQUAL_INT (SERVER, dialog[step].turn);
|
2014-01-12 22:36:47 -05:00
|
|
|
|
2017-09-07 11:21:13 +02:00
|
|
|
// Grab the 1st frame (peer routing id).
|
2014-01-12 22:36:47 -05:00
|
|
|
zmq_msg_t peer_frame;
|
2018-08-23 12:13:23 +02:00
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_init (&peer_frame));
|
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (
|
|
|
|
zmq_msg_recv (&peer_frame, sockets[CLIENT], 0));
|
|
|
|
TEST_ASSERT_GREATER_THAN_INT (0, zmq_msg_size (&peer_frame));
|
|
|
|
TEST_ASSERT_TRUE (has_more (sockets[CLIENT]));
|
2014-01-12 22:36:47 -05:00
|
|
|
|
|
|
|
// Grab the 2nd frame (actual payload).
|
|
|
|
zmq_msg_t data_frame;
|
2018-08-23 12:13:23 +02:00
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_init (&data_frame));
|
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (
|
|
|
|
zmq_msg_recv (&data_frame, sockets[CLIENT], 0));
|
|
|
|
TEST_ASSERT_GREATER_THAN_INT (0, zmq_msg_size (&data_frame));
|
2014-01-12 22:36:47 -05:00
|
|
|
|
|
|
|
// Make sure payload matches what we expect.
|
2019-12-08 14:26:57 +01:00
|
|
|
const char *const data =
|
|
|
|
static_cast<const char *> (zmq_msg_data (&data_frame));
|
2018-05-14 20:55:57 +02:00
|
|
|
const size_t size = zmq_msg_size (&data_frame);
|
2018-08-23 12:13:23 +02:00
|
|
|
TEST_ASSERT_EQUAL_INT (strlen (dialog[step].text), size);
|
|
|
|
TEST_ASSERT_EQUAL_STRING_LEN (dialog[step].text, data, size);
|
2014-01-12 22:36:47 -05:00
|
|
|
|
|
|
|
++step;
|
|
|
|
|
|
|
|
// Prepare the response (next line in the dialog).
|
2018-08-23 12:13:23 +02:00
|
|
|
TEST_ASSERT_LESS_THAN_INT (steps, step);
|
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_close (&data_frame));
|
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (
|
|
|
|
zmq_msg_init_size (&data_frame, strlen (dialog[step].text)));
|
2014-01-12 22:36:47 -05:00
|
|
|
memcpy (zmq_msg_data (&data_frame), dialog[step].text,
|
|
|
|
zmq_msg_size (&data_frame));
|
|
|
|
|
|
|
|
// Send the response.
|
2018-08-23 12:13:23 +02:00
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (
|
|
|
|
zmq_msg_send (&peer_frame, sockets[CLIENT], ZMQ_SNDMORE));
|
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (
|
|
|
|
zmq_msg_send (&data_frame, sockets[CLIENT], ZMQ_SNDMORE));
|
2014-01-12 22:36:47 -05:00
|
|
|
|
|
|
|
// Release resources.
|
2018-08-23 12:13:23 +02:00
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_close (&peer_frame));
|
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_close (&data_frame));
|
2014-01-12 22:36:47 -05:00
|
|
|
}
|
|
|
|
}
|
2018-08-23 12:13:23 +02:00
|
|
|
TEST_ASSERT_EQUAL_INT (steps, step);
|
|
|
|
test_context_socket_close (sockets[CLIENT]);
|
|
|
|
test_context_socket_close (sockets[SERVER]);
|
|
|
|
}
|
|
|
|
|
|
|
|
int main (int, char **)
|
|
|
|
{
|
|
|
|
setup_test_environment ();
|
|
|
|
|
|
|
|
UNITY_BEGIN ();
|
|
|
|
RUN_TEST (test_stream_disconnect);
|
|
|
|
return UNITY_END ();
|
2014-01-12 22:36:47 -05:00
|
|
|
}
|