Unverified Commit b65b523a authored by Luca Boccassi's avatar Luca Boccassi Committed by GitHub

Merge pull request #3341 from sigiesec/migrate-more-tests

Problem: tests without test framework
parents cb2b9a8c 88bea4fd
......@@ -580,7 +580,8 @@ tests_test_ctx_options_SOURCES = tests/test_ctx_options.cpp
tests_test_ctx_options_LDADD = src/libzmq.la
tests_test_iov_SOURCES = tests/test_iov.cpp
tests_test_iov_LDADD = src/libzmq.la
tests_test_iov_LDADD = src/libzmq.la ${UNITY_LIBS}
tests_test_iov_CPPFLAGS = ${UNITY_CPPFLAGS}
tests_test_ctx_destroy_SOURCES = tests/test_ctx_destroy.cpp
tests_test_ctx_destroy_LDADD = src/libzmq.la ${UNITY_LIBS}
......@@ -635,7 +636,8 @@ tests_test_inproc_connect_LDADD = src/libzmq.la ${UNITY_LIBS}
tests_test_inproc_connect_CPPFLAGS = ${UNITY_CPPFLAGS}
tests_test_issue_566_SOURCES = tests/test_issue_566.cpp
tests_test_issue_566_LDADD = src/libzmq.la
tests_test_issue_566_LDADD = src/libzmq.la ${UNITY_LIBS}
tests_test_issue_566_CPPFLAGS = ${UNITY_CPPFLAGS}
tests_test_proxy_SOURCES = tests/test_proxy.cpp
tests_test_proxy_LDADD = src/libzmq.la
......@@ -657,7 +659,8 @@ tests_test_many_sockets_SOURCES = tests/test_many_sockets.cpp
tests_test_many_sockets_LDADD = src/libzmq.la
tests_test_diffserv_SOURCES = tests/test_diffserv.cpp
tests_test_diffserv_LDADD = src/libzmq.la
tests_test_diffserv_LDADD = src/libzmq.la ${UNITY_LIBS}
tests_test_diffserv_CPPFLAGS = ${UNITY_CPPFLAGS}
tests_test_connect_rid_SOURCES = tests/test_connect_rid.cpp
tests_test_connect_rid_LDADD = src/libzmq.la ${UNITY_LIBS}
......@@ -794,7 +797,8 @@ tests_test_shutdown_stress_LDADD = src/libzmq.la ${UNITY_LIBS}
tests_test_shutdown_stress_CPPFLAGS = ${UNITY_CPPFLAGS}
tests_test_ipc_wildcard_SOURCES = tests/test_ipc_wildcard.cpp
tests_test_ipc_wildcard_LDADD = src/libzmq.la
tests_test_ipc_wildcard_LDADD = src/libzmq.la ${UNITY_LIBS}
tests_test_ipc_wildcard_CPPFLAGS = ${UNITY_CPPFLAGS}
tests_test_pair_ipc_SOURCES = \
tests/test_pair_ipc.cpp \
......@@ -813,7 +817,8 @@ tests_test_reqrep_ipc_LDADD = src/libzmq.la ${UNITY_LIBS}
tests_test_reqrep_ipc_CPPFLAGS = ${UNITY_CPPFLAGS}
tests_test_timeo_SOURCES = tests/test_timeo.cpp
tests_test_timeo_LDADD = src/libzmq.la
tests_test_timeo_LDADD = src/libzmq.la ${UNITY_LIBS}
tests_test_timeo_CPPFLAGS = ${UNITY_CPPFLAGS}
tests_test_filter_ipc_SOURCES = tests/test_filter_ipc.cpp
tests_test_filter_ipc_LDADD = src/libzmq.la ${UNITY_LIBS}
......
......@@ -28,57 +28,55 @@
*/
#include "testutil.hpp"
#include "testutil_unity.hpp"
int main (void)
#include <unity.h>
void setUp ()
{
setup_test_context ();
}
void tearDown ()
{
teardown_test_context ();
}
void test_diffserv ()
{
int rc;
int tos = 0x28;
int o_tos;
size_t tos_size = sizeof (tos);
size_t len = MAX_SOCKET_STRING;
char my_endpoint[MAX_SOCKET_STRING];
setup_test_environment ();
void *ctx = zmq_ctx_new ();
assert (ctx);
void *sb = zmq_socket (ctx, ZMQ_PAIR);
assert (sb);
rc = zmq_setsockopt (sb, ZMQ_TOS, &tos, tos_size);
assert (rc == 0);
rc = zmq_bind (sb, "tcp://127.0.0.1:*");
assert (rc == 0);
rc = zmq_getsockopt (sb, ZMQ_LAST_ENDPOINT, my_endpoint, &len);
assert (rc == 0);
rc = zmq_getsockopt (sb, ZMQ_TOS, &o_tos, &tos_size);
assert (rc == 0);
assert (o_tos == tos);
void *sc = zmq_socket (ctx, ZMQ_PAIR);
assert (sc);
void *sb = test_context_socket (ZMQ_PAIR);
TEST_ASSERT_SUCCESS_ERRNO (zmq_setsockopt (sb, ZMQ_TOS, &tos, tos_size));
bind_loopback_ipv4 (sb, my_endpoint, sizeof (my_endpoint));
TEST_ASSERT_SUCCESS_ERRNO (zmq_getsockopt (sb, ZMQ_TOS, &o_tos, &tos_size));
TEST_ASSERT_EQUAL (tos, o_tos);
void *sc = test_context_socket (ZMQ_PAIR);
tos = 0x58;
rc = zmq_setsockopt (sc, ZMQ_TOS, &tos, tos_size);
assert (rc == 0);
rc = zmq_connect (sc, my_endpoint);
assert (rc == 0);
rc = zmq_getsockopt (sc, ZMQ_TOS, &o_tos, &tos_size);
assert (rc == 0);
assert (o_tos == tos);
TEST_ASSERT_SUCCESS_ERRNO (zmq_setsockopt (sc, ZMQ_TOS, &tos, tos_size));
TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (sc, my_endpoint));
TEST_ASSERT_SUCCESS_ERRNO (zmq_getsockopt (sc, ZMQ_TOS, &o_tos, &tos_size));
TEST_ASSERT_EQUAL (tos, o_tos);
// Wireshark can be used to verify that the server socket is
// using DSCP 0x28 in packets to the client while the client
// is using 0x58 in packets to the server.
bounce (sb, sc);
rc = zmq_close (sc);
assert (rc == 0);
rc = zmq_close (sb);
assert (rc == 0);
test_context_socket_close (sc);
test_context_socket_close (sb);
}
rc = zmq_ctx_term (ctx);
assert (rc == 0);
int main ()
{
setup_test_environment ();
return 0;
UNITY_BEGIN ();
RUN_TEST (test_diffserv);
return UNITY_END ();
}
......@@ -28,6 +28,19 @@
*/
#include "testutil.hpp"
#include "testutil_unity.hpp"
#include <unity.h>
void setUp ()
{
setup_test_context ();
}
void tearDown ()
{
teardown_test_context ();
}
// XSI vector I/O
#if defined ZMQ_HAVE_UIO
......@@ -40,102 +53,91 @@ struct iovec
};
#endif
void do_check (void *sb_, void *sc_, size_t msg_size_)
static void do_check (void *sb_, void *sc_, size_t msg_size_)
{
assert (sb_ && sc_ && msg_size_ > 0);
int rc = 0;
const char msg_val = '1';
const int num_messages = 10;
size_t send_count, recv_count;
send_count = recv_count = num_messages;
char *ref_msg = (char *) malloc (msg_size_);
assert (ref_msg);
char *ref_msg = static_cast<char *> (malloc (msg_size_));
TEST_ASSERT_NOT_NULL (ref_msg);
memset (ref_msg, msg_val, msg_size_);
// zmq_sendiov(3) as a single multi-part send
struct iovec send_iov[num_messages];
char *buf = (char *) malloc (msg_size_ * num_messages);
char *buf = static_cast<char *> (malloc (msg_size_ * num_messages));
for (int i = 0; i < num_messages; i++) {
send_iov[i].iov_base = &buf[i * msg_size_];
send_iov[i].iov_len = msg_size_;
memcpy (send_iov[i].iov_base, ref_msg, msg_size_);
// TODO: this assertion only checks if memcpy behaves as expected... remove this?
assert (memcmp (ref_msg, send_iov[i].iov_base, msg_size_) == 0);
}
// Test errors - zmq_recviov - null socket
rc = zmq_sendiov (NULL, send_iov, send_count, ZMQ_SNDMORE);
assert (rc == -1 && errno == ENOTSOCK);
TEST_ASSERT_FAILURE_ERRNO (
ENOTSOCK, zmq_sendiov (NULL, send_iov, send_count, ZMQ_SNDMORE));
// Test errors - zmq_recviov - invalid send count
rc = zmq_sendiov (sc_, send_iov, 0, 0);
assert (rc == -1 && errno == EINVAL);
TEST_ASSERT_FAILURE_ERRNO (EINVAL, zmq_sendiov (sc_, send_iov, 0, 0));
// Test errors - zmq_recviov - null iovec
rc = zmq_sendiov (sc_, NULL, send_count, 0);
assert (rc == -1 && errno == EINVAL);
TEST_ASSERT_FAILURE_ERRNO (EINVAL, zmq_sendiov (sc_, NULL, send_count, 0));
// Test success
rc = zmq_sendiov (sc_, send_iov, send_count, ZMQ_SNDMORE);
// The zmq_sendiov(3) API method does not follow the same semantics as
// zmq_recviov(3); the latter returns the count of messages sent, rightly
// so, whilst the former sends the number of bytes successfully sent from
// the last message, which does not hold much sense from a batch send
// perspective; hence the assert checks if rc is same as msg_size.
assert ((size_t) rc == msg_size_);
// perspective; hence the assert checks if the result is same as msg_size.
TEST_ASSERT_EQUAL_INT (
(int) msg_size_, TEST_ASSERT_SUCCESS_ERRNO (
zmq_sendiov (sc_, send_iov, send_count, ZMQ_SNDMORE)));
// zmq_recviov(3) single-shot
struct iovec recv_iov[num_messages];
// Test errors - zmq_recviov - null socket
rc = zmq_recviov (NULL, recv_iov, &recv_count, 0);
assert (rc == -1 && errno == ENOTSOCK);
TEST_ASSERT_FAILURE_ERRNO (ENOTSOCK,
zmq_recviov (NULL, recv_iov, &recv_count, 0));
// Test error - zmq_recviov - invalid receive count
rc = zmq_recviov (sb_, recv_iov, NULL, 0);
assert (rc == -1 && errno == EINVAL);
TEST_ASSERT_FAILURE_ERRNO (EINVAL, zmq_recviov (sb_, recv_iov, NULL, 0));
size_t invalid_recv_count = 0;
rc = zmq_recviov (sb_, recv_iov, &invalid_recv_count, 0);
assert (rc == -1 && errno == EINVAL);
TEST_ASSERT_FAILURE_ERRNO (
EINVAL, zmq_recviov (sb_, recv_iov, &invalid_recv_count, 0));
// Test error - zmq_recviov - null iovec
rc = zmq_recviov (sb_, NULL, &recv_count, 0);
assert (rc == -1 && errno == EINVAL);
TEST_ASSERT_FAILURE_ERRNO (EINVAL, zmq_recviov (sb_, NULL, &recv_count, 0));
// Test success
rc = zmq_recviov (sb_, recv_iov, &recv_count, 0);
assert (rc == num_messages);
TEST_ASSERT_EQUAL_INT (
num_messages,
TEST_ASSERT_SUCCESS_ERRNO (zmq_recviov (sb_, recv_iov, &recv_count, 0)));
for (int i = 0; i < num_messages; i++) {
assert (recv_iov[i].iov_base);
assert (memcmp (ref_msg, recv_iov[i].iov_base, msg_size_) == 0);
TEST_ASSERT_NOT_NULL (recv_iov[i].iov_base);
TEST_ASSERT_EQUAL_STRING_LEN (ref_msg, recv_iov[i].iov_base, msg_size_);
free (recv_iov[i].iov_base);
}
assert (send_count == recv_count);
TEST_ASSERT_EQUAL_INT (send_count, recv_count);
free (ref_msg);
free (buf);
}
int main (void)
void test_iov ()
{
setup_test_environment ();
void *ctx = zmq_ctx_new ();
assert (ctx);
int rc;
void *sb = zmq_socket (ctx, ZMQ_PULL);
assert (sb);
rc = zmq_bind (sb, "inproc://a");
assert (rc == 0);
void *sb = test_context_socket (ZMQ_PULL);
TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (sb, "inproc://a"));
msleep (SETTLE_TIME);
void *sc = zmq_socket (ctx, ZMQ_PUSH);
rc = zmq_connect (sc, "inproc://a");
assert (rc == 0);
void *sc = test_context_socket (ZMQ_PUSH);
TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (sc, "inproc://a"));
// message bigger than VSM max
do_check (sb, sc, 100);
......@@ -143,14 +145,15 @@ int main (void)
// message smaller than VSM max
do_check (sb, sc, 10);
rc = zmq_close (sc);
assert (rc == 0);
rc = zmq_close (sb);
assert (rc == 0);
test_context_socket_close (sc);
test_context_socket_close (sb);
}
rc = zmq_ctx_term (ctx);
assert (rc == 0);
int main ()
{
setup_test_environment ();
return 0;
UNITY_BEGIN ();
RUN_TEST (test_iov);
return UNITY_END ();
}
......@@ -28,38 +28,44 @@
*/
#include "testutil.hpp"
#include "testutil_unity.hpp"
int main (void)
#include <unity.h>
void setUp ()
{
setup_test_environment ();
void *ctx = zmq_ctx_new ();
assert (ctx);
setup_test_context ();
}
void *sb = zmq_socket (ctx, ZMQ_PAIR);
assert (sb);
int rc = zmq_bind (sb, "ipc://*");
assert (rc == 0);
void tearDown ()
{
teardown_test_context ();
}
void test_ipc_wildcard ()
{
void *sb = test_context_socket (ZMQ_PAIR);
TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (sb, "ipc://*"));
char endpoint[200];
size_t size = sizeof (endpoint);
rc = zmq_getsockopt (sb, ZMQ_LAST_ENDPOINT, endpoint, &size);
assert (rc == 0);
TEST_ASSERT_SUCCESS_ERRNO (
zmq_getsockopt (sb, ZMQ_LAST_ENDPOINT, endpoint, &size));
void *sc = zmq_socket (ctx, ZMQ_PAIR);
assert (sc);
rc = zmq_connect (sc, endpoint);
assert (rc == 0);
void *sc = test_context_socket (ZMQ_PAIR);
TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (sc, endpoint));
bounce (sb, sc);
rc = zmq_close (sc);
assert (rc == 0);
rc = zmq_close (sb);
assert (rc == 0);
test_context_socket_close (sc);
test_context_socket_close (sb);
}
rc = zmq_ctx_term (ctx);
assert (rc == 0);
int main ()
{
setup_test_environment ();
return 0;
UNITY_BEGIN ();
RUN_TEST (test_ipc_wildcard);
return UNITY_END ();
}
......@@ -28,32 +28,38 @@
*/
#include "testutil.hpp"
#include "testutil_unity.hpp"
#include <unity.h>
void setUp ()
{
setup_test_context ();
}
void tearDown ()
{
teardown_test_context ();
}
// Issue 566 describes a problem in libzmq v4.0.0 where a dealer to router
// connection would fail randomly. The test works when the two sockets are
// on the same context, and failed when they were on separate contexts.
// Fixed by https://github.com/zeromq/libzmq/commit/be25cf.
int main (void)
void test_issue_566 ()
{
setup_test_environment ();
size_t len = MAX_SOCKET_STRING;
char my_endpoint[MAX_SOCKET_STRING];
void *ctx1 = zmq_ctx_new ();
assert (ctx1);
TEST_ASSERT_NOT_NULL (ctx1);
void *ctx2 = zmq_ctx_new ();
assert (ctx2);
TEST_ASSERT_NOT_NULL (ctx2);
void *router = zmq_socket (ctx1, ZMQ_ROUTER);
int on = 1;
int rc = zmq_setsockopt (router, ZMQ_ROUTER_MANDATORY, &on, sizeof (on));
assert (rc == 0);
rc = zmq_bind (router, "tcp://127.0.0.1:*");
assert (rc != -1);
rc = zmq_getsockopt (router, ZMQ_LAST_ENDPOINT, my_endpoint, &len);
assert (rc == 0);
TEST_ASSERT_SUCCESS_ERRNO (
zmq_setsockopt (router, ZMQ_ROUTER_MANDATORY, &on, sizeof (on)));
bind_loopback_ipv4 (router, my_endpoint, sizeof (my_endpoint));
// Repeat often enough to be sure this works as it should
for (int cycle = 0; cycle < 100; cycle++) {
......@@ -62,13 +68,12 @@ int main (void)
void *dealer = zmq_socket (ctx2, ZMQ_DEALER);
char routing_id[10];
sprintf (routing_id, "%09d", cycle);
rc = zmq_setsockopt (dealer, ZMQ_ROUTING_ID, routing_id, 10);
assert (rc == 0);
TEST_ASSERT_SUCCESS_ERRNO (
zmq_setsockopt (dealer, ZMQ_ROUTING_ID, routing_id, 10));
int rcvtimeo = 1000;
rc = zmq_setsockopt (dealer, ZMQ_RCVTIMEO, &rcvtimeo, sizeof (int));
assert (rc == 0);
rc = zmq_connect (dealer, my_endpoint);
assert (rc == 0);
TEST_ASSERT_SUCCESS_ERRNO (
zmq_setsockopt (dealer, ZMQ_RCVTIMEO, &rcvtimeo, sizeof (int)));
TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (dealer, my_endpoint));
// Router will try to send to dealer, at short intervals.
// It typically takes 2-5 msec for the connection to establish
......@@ -76,24 +81,27 @@ int main (void)
// before failing the test (e.g. for running on a debugger or
// a very slow system).
for (int attempt = 0; attempt < 500; attempt++) {
zmq_poll (0, 0, 2);
rc = zmq_send (router, routing_id, 10, ZMQ_SNDMORE);
zmq_poll (NULL, 0, 2);
int rc = zmq_send (router, routing_id, 10, ZMQ_SNDMORE);
if (rc == -1 && errno == EHOSTUNREACH)
continue;
assert (rc == 10);
rc = zmq_send (router, "HELLO", 5, 0);
assert (rc == 5);
TEST_ASSERT_EQUAL (10, rc);
send_string_expect_success (router, "HELLO", 0);
break;
}
uint8_t buffer[5];
rc = zmq_recv (dealer, buffer, 5, 0);
assert (rc == 5);
assert (memcmp (buffer, "HELLO", 5) == 0);
recv_string_expect_success (dealer, "HELLO", 0);
close_zero_linger (dealer);
}
zmq_close (router);
zmq_ctx_destroy (ctx1);
zmq_ctx_destroy (ctx2);
}
int main ()
{
setup_test_environment ();
return 0;
UNITY_BEGIN ();
RUN_TEST (test_issue_566);
return UNITY_END ();
}
......@@ -28,36 +28,41 @@
*/
#include "testutil.hpp"
#include "testutil_unity.hpp"
int main (void)
#include <unity.h>
void setUp ()
{
setup_test_environment ();
void *ctx = zmq_ctx_new ();
assert (ctx);
setup_test_context ();
}
void *frontend = zmq_socket (ctx, ZMQ_DEALER);
assert (frontend);
int rc = zmq_bind (frontend, "inproc://timeout_test");
assert (rc == 0);
void tearDown ()
{
teardown_test_context ();
}
void test_timeo ()
{
void *frontend = test_context_socket (ZMQ_DEALER);
TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (frontend, "inproc://timeout_test"));
// Receive on disconnected socket returns immediately
char buffer[32];
rc = zmq_recv (frontend, buffer, 32, ZMQ_DONTWAIT);
assert (rc == -1);
assert (zmq_errno () == EAGAIN);
TEST_ASSERT_FAILURE_ERRNO (EAGAIN,
zmq_recv (frontend, buffer, 32, ZMQ_DONTWAIT));
// Check whether receive timeout is honored
const int timeout = 250;
const int jitter = 50;
rc = zmq_setsockopt (frontend, ZMQ_RCVTIMEO, &timeout, sizeof (int));
assert (rc == 0);
TEST_ASSERT_SUCCESS_ERRNO (
zmq_setsockopt (frontend, ZMQ_RCVTIMEO, &timeout, sizeof (int)));
void *stopwatch = zmq_stopwatch_start ();
rc = zmq_recv (frontend, buffer, 32, 0);
assert (rc == -1);
assert (zmq_errno () == EAGAIN);
TEST_ASSERT_FAILURE_ERRNO (EAGAIN, zmq_recv (frontend, buffer, 32, 0));
unsigned int elapsed = zmq_stopwatch_stop (stopwatch) / 1000;
assert (elapsed > timeout - jitter);
TEST_ASSERT_GREATER_THAN_INT (timeout - jitter, elapsed);
if (elapsed >= timeout + jitter) {
// we cannot assert this on a non-RT system
fprintf (stderr,
......@@ -67,27 +72,24 @@ int main (void)
}
// Check that normal message flow works as expected
void *backend = zmq_socket (ctx, ZMQ_DEALER);
assert (backend);
rc = zmq_connect (backend, "inproc://timeout_test");
assert (rc == 0);
rc = zmq_setsockopt (backend, ZMQ_SNDTIMEO, &timeout, sizeof (int));
assert (rc == 0);
rc = zmq_send (backend, "Hello", 5, 0);
assert (rc == 5);
rc = zmq_recv (frontend, buffer, 32, 0);
assert (rc == 5);
void *backend = test_context_socket (ZMQ_DEALER);
TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (backend, "inproc://timeout_test"));
TEST_ASSERT_SUCCESS_ERRNO (
zmq_setsockopt (backend, ZMQ_SNDTIMEO, &timeout, sizeof (int)));
// Clean-up
rc = zmq_close (backend);
assert (rc == 0);
send_string_expect_success (backend, "Hello", 0);
recv_string_expect_success (frontend, "Hello", 0);
rc = zmq_close (frontend);
assert (rc == 0);
// Clean-up
test_context_socket_close (backend);
test_context_socket_close (frontend);
}
rc = zmq_ctx_term (ctx);
assert (rc == 0);
int main ()
{
setup_test_environment ();
return 0;
UNITY_BEGIN ();
RUN_TEST (test_timeo);
return UNITY_END ();
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment