2023-06-05 00:16:05 +01:00
|
|
|
/* SPDX-License-Identifier: MPL-2.0 */
|
2012-04-21 18:39:19 +04:00
|
|
|
|
2013-08-17 13:43:45 +01:00
|
|
|
#include "testutil.hpp"
|
2018-08-16 18:04:29 +02:00
|
|
|
#include "testutil_unity.hpp"
|
2012-04-21 18:39:19 +04:00
|
|
|
|
2019-03-24 12:51:28 -04:00
|
|
|
SETUP_TEARDOWN_TESTCONTEXT
|
2018-08-16 18:04:29 +02:00
|
|
|
|
|
|
|
/* Use the worst case filename size for the buffer (+1 for trailing NUL), this
|
|
|
|
* is larger than MAX_SOCKET_STRING, which is not large enough for IPC */
|
|
|
|
#define BUF_SIZE (FILENAME_MAX + 1)
|
|
|
|
|
|
|
|
const char *ep_wc_tcp = "tcp://127.0.0.1:*";
|
2014-10-29 10:10:14 +01:00
|
|
|
#if !defined ZMQ_HAVE_WINDOWS && !defined ZMQ_HAVE_OPENVMS
|
2018-08-16 18:04:29 +02:00
|
|
|
const char *ep_wc_ipc = "ipc://*";
|
2014-10-29 10:10:14 +01:00
|
|
|
#endif
|
2015-12-07 18:19:45 +06:00
|
|
|
#if defined ZMQ_HAVE_VMCI
|
2018-08-16 18:04:29 +02:00
|
|
|
const char *ep_wc_vmci = "vmci://*:*";
|
2015-12-07 18:19:45 +06:00
|
|
|
#endif
|
2012-04-21 18:39:19 +04:00
|
|
|
|
2018-08-16 18:04:29 +02:00
|
|
|
void test_send_after_unbind_fails ()
|
|
|
|
{
|
|
|
|
char my_endpoint[BUF_SIZE];
|
|
|
|
|
2012-04-21 18:39:19 +04:00
|
|
|
// Create infrastructure.
|
2018-08-16 18:04:29 +02:00
|
|
|
void *push = test_context_socket (ZMQ_PUSH);
|
|
|
|
bind_loopback_ipv4 (push, my_endpoint, BUF_SIZE);
|
|
|
|
|
|
|
|
void *pull = test_context_socket (ZMQ_PULL);
|
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (pull, my_endpoint));
|
2012-04-21 18:39:19 +04:00
|
|
|
|
2013-01-31 19:46:22 +01:00
|
|
|
// Pass one message through to ensure the connection is established
|
2018-08-16 18:04:29 +02:00
|
|
|
send_string_expect_success (push, "ABC", 0);
|
|
|
|
recv_string_expect_success (pull, "ABC", 0);
|
2012-04-21 18:39:19 +04:00
|
|
|
|
2013-01-31 19:46:22 +01:00
|
|
|
// Unbind the listening endpoint
|
2018-08-16 18:04:29 +02:00
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_unbind (push, my_endpoint));
|
2012-04-21 18:39:19 +04:00
|
|
|
|
2013-01-31 19:46:22 +01:00
|
|
|
// Allow unbind to settle
|
2013-11-06 13:30:41 +01:00
|
|
|
msleep (SETTLE_TIME);
|
2012-04-21 18:39:19 +04:00
|
|
|
|
2013-01-31 19:46:22 +01:00
|
|
|
// Check that sending would block (there's no outbound connection)
|
2018-08-16 18:04:29 +02:00
|
|
|
TEST_ASSERT_FAILURE_ERRNO (EAGAIN, zmq_send (push, "ABC", 3, ZMQ_DONTWAIT));
|
2012-04-21 18:39:19 +04:00
|
|
|
|
2013-01-31 19:46:22 +01:00
|
|
|
// Clean up
|
2018-08-16 18:04:29 +02:00
|
|
|
test_context_socket_close (pull);
|
|
|
|
test_context_socket_close (push);
|
|
|
|
}
|
2012-04-21 18:39:19 +04:00
|
|
|
|
2018-08-16 18:04:29 +02:00
|
|
|
void test_send_after_disconnect_fails ()
|
|
|
|
{
|
2013-01-31 19:46:22 +01:00
|
|
|
// Create infrastructure
|
2018-08-16 18:04:29 +02:00
|
|
|
void *pull = test_context_socket (ZMQ_PULL);
|
|
|
|
char my_endpoint[BUF_SIZE];
|
|
|
|
bind_loopback_ipv4 (pull, my_endpoint, BUF_SIZE);
|
|
|
|
|
|
|
|
void *push = test_context_socket (ZMQ_PUSH);
|
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (push, my_endpoint));
|
2012-04-21 18:39:19 +04:00
|
|
|
|
|
|
|
// Pass one message through to ensure the connection is established.
|
2018-08-16 18:04:29 +02:00
|
|
|
send_string_expect_success (push, "ABC", 0);
|
|
|
|
recv_string_expect_success (pull, "ABC", 0);
|
2012-04-21 18:39:19 +04:00
|
|
|
|
2013-01-31 19:46:22 +01:00
|
|
|
// Disconnect the bound endpoint
|
2018-08-16 18:04:29 +02:00
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_disconnect (push, my_endpoint));
|
2012-04-21 18:39:19 +04:00
|
|
|
|
2013-01-31 19:46:22 +01:00
|
|
|
// Allow disconnect to settle
|
2013-11-06 13:30:41 +01:00
|
|
|
msleep (SETTLE_TIME);
|
2012-04-21 18:39:19 +04:00
|
|
|
|
|
|
|
// Check that sending would block (there's no inbound connections).
|
2018-08-16 18:04:29 +02:00
|
|
|
TEST_ASSERT_FAILURE_ERRNO (EAGAIN, zmq_send (push, "ABC", 3, ZMQ_DONTWAIT));
|
2012-04-21 18:39:19 +04:00
|
|
|
|
2018-08-16 18:04:29 +02:00
|
|
|
// Clean up
|
|
|
|
test_context_socket_close (pull);
|
|
|
|
test_context_socket_close (push);
|
|
|
|
}
|
2012-04-21 18:39:19 +04:00
|
|
|
|
2018-08-16 18:04:29 +02:00
|
|
|
void test_unbind_via_last_endpoint ()
|
|
|
|
{
|
2014-10-29 09:43:47 +01:00
|
|
|
// Create infrastructure (wild-card binding)
|
2018-08-16 18:04:29 +02:00
|
|
|
void *push = test_context_socket (ZMQ_PUSH);
|
|
|
|
char my_endpoint[BUF_SIZE];
|
|
|
|
bind_loopback_ipv4 (push, my_endpoint, BUF_SIZE);
|
|
|
|
|
|
|
|
void *pull = test_context_socket (ZMQ_PULL);
|
|
|
|
|
2014-10-29 10:10:14 +01:00
|
|
|
#if !defined ZMQ_HAVE_WINDOWS && !defined ZMQ_HAVE_OPENVMS
|
2018-08-16 18:04:29 +02:00
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (pull, ep_wc_ipc));
|
2014-10-29 10:10:14 +01:00
|
|
|
#endif
|
2015-12-07 18:19:45 +06:00
|
|
|
#if defined ZMQ_HAVE_VMCI
|
2018-08-16 18:04:29 +02:00
|
|
|
void *req = test_context_socket (ZMQ_REQ);
|
2021-09-21 19:06:56 +01:00
|
|
|
int rc = zmq_bind (req, ep_wc_vmci);
|
|
|
|
if (rc < 0 && errno == EAFNOSUPPORT)
|
|
|
|
TEST_IGNORE_MESSAGE ("VMCI not supported");
|
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (rc);
|
2015-12-07 18:19:45 +06:00
|
|
|
#endif
|
2014-10-29 09:43:47 +01:00
|
|
|
|
|
|
|
// Unbind sockets binded by wild-card address
|
2018-08-16 18:04:29 +02:00
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_unbind (push, my_endpoint));
|
|
|
|
|
|
|
|
size_t buf_size = 0;
|
|
|
|
(void) buf_size;
|
|
|
|
|
2014-10-29 10:10:14 +01:00
|
|
|
#if !defined ZMQ_HAVE_WINDOWS && !defined ZMQ_HAVE_OPENVMS
|
2018-08-16 18:04:29 +02:00
|
|
|
buf_size = sizeof (my_endpoint);
|
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (
|
|
|
|
zmq_getsockopt (pull, ZMQ_LAST_ENDPOINT, my_endpoint, &buf_size));
|
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_unbind (pull, my_endpoint));
|
2014-10-29 10:10:14 +01:00
|
|
|
#endif
|
2015-12-07 18:19:45 +06:00
|
|
|
#if defined ZMQ_HAVE_VMCI
|
2018-08-16 18:04:29 +02:00
|
|
|
buf_size = sizeof (my_endpoint);
|
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (
|
|
|
|
zmq_getsockopt (req, ZMQ_LAST_ENDPOINT, my_endpoint, &buf_size));
|
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_unbind (req, my_endpoint));
|
2015-12-07 18:19:45 +06:00
|
|
|
#endif
|
2014-10-29 09:43:47 +01:00
|
|
|
|
2018-08-16 18:04:29 +02:00
|
|
|
// Clean up
|
|
|
|
test_context_socket_close (pull);
|
|
|
|
test_context_socket_close (push);
|
|
|
|
}
|
2016-02-05 13:40:59 +00:00
|
|
|
|
2018-08-16 18:04:29 +02:00
|
|
|
void test_wildcard_unbind_fails ()
|
|
|
|
{
|
2014-10-29 09:43:47 +01:00
|
|
|
// Create infrastructure (wild-card binding)
|
2018-08-16 18:04:29 +02:00
|
|
|
void *push = test_context_socket (ZMQ_PUSH);
|
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (push, ep_wc_tcp));
|
|
|
|
void *pull = test_context_socket (ZMQ_PULL);
|
2016-04-23 22:01:34 +03:00
|
|
|
#if !defined ZMQ_HAVE_WINDOWS && !defined ZMQ_HAVE_OPENVMS
|
2018-08-16 18:04:29 +02:00
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (pull, ep_wc_ipc));
|
2014-10-29 10:10:14 +01:00
|
|
|
#endif
|
2015-12-07 18:19:45 +06:00
|
|
|
#if defined ZMQ_HAVE_VMCI
|
2018-08-16 18:04:29 +02:00
|
|
|
void *req = test_context_socket (ZMQ_REQ);
|
2021-09-21 19:06:56 +01:00
|
|
|
int rc = zmq_bind (req, ep_wc_vmci);
|
|
|
|
if (rc < 0 && errno == EAFNOSUPPORT)
|
|
|
|
TEST_IGNORE_MESSAGE ("VMCI not supported");
|
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (rc);
|
2015-12-07 18:19:45 +06:00
|
|
|
#endif
|
2014-10-29 09:43:47 +01:00
|
|
|
|
|
|
|
// Sockets binded by wild-card address can't be unbinded by wild-card address
|
2018-08-16 18:04:29 +02:00
|
|
|
TEST_ASSERT_FAILURE_ERRNO (ENOENT, zmq_unbind (push, ep_wc_tcp));
|
2014-10-29 10:10:14 +01:00
|
|
|
#if !defined ZMQ_HAVE_WINDOWS && !defined ZMQ_HAVE_OPENVMS
|
2018-08-16 18:04:29 +02:00
|
|
|
TEST_ASSERT_FAILURE_ERRNO (ENOENT, zmq_unbind (pull, ep_wc_ipc));
|
2014-10-29 10:10:14 +01:00
|
|
|
#endif
|
2015-12-07 18:19:45 +06:00
|
|
|
#if defined ZMQ_HAVE_VMCI
|
2018-08-16 18:04:29 +02:00
|
|
|
TEST_ASSERT_FAILURE_ERRNO (ENOENT, zmq_unbind (req, ep_wc_vmci));
|
2015-12-07 18:19:45 +06:00
|
|
|
#endif
|
2014-10-29 09:43:47 +01:00
|
|
|
|
2018-08-16 18:04:29 +02:00
|
|
|
// Clean up
|
|
|
|
test_context_socket_close (pull);
|
|
|
|
test_context_socket_close (push);
|
|
|
|
}
|
|
|
|
|
|
|
|
int main ()
|
|
|
|
{
|
|
|
|
setup_test_environment ();
|
2016-02-05 13:40:59 +00:00
|
|
|
|
2018-08-16 18:04:29 +02:00
|
|
|
UNITY_BEGIN ();
|
|
|
|
RUN_TEST (test_send_after_unbind_fails);
|
|
|
|
RUN_TEST (test_send_after_disconnect_fails);
|
|
|
|
RUN_TEST (test_unbind_via_last_endpoint);
|
|
|
|
RUN_TEST (test_wildcard_unbind_fails);
|
|
|
|
return UNITY_END ();
|
2012-04-21 18:39:19 +04:00
|
|
|
}
|