Unverified Commit 4cb15ec6 authored by Luca Boccassi's avatar Luca Boccassi Committed by GitHub

Merge pull request #2994 from sigiesec/migrate-to-unity

Migrate further tests to unity, and split up into individual test cases
parents e3887747 7bd57ba8
......@@ -429,7 +429,7 @@ test_apps = \
tests/test_reconnect_ivl \
tests/test_socket_null
UNITY_CPPFLAGS = -I$(top_srcdir)/external/unity
UNITY_CPPFLAGS = -I$(top_srcdir)/external/unity -DUNITY_USE_COMMAND_LINE_ARGS -DUNITY_EXCLUDE_FLOAT
UNITY_LIBS = $(top_builddir)/external/unity/libunity.a
noinst_LIBRARIES = external/unity/libunity.a
external_unity_libunity_a_SOURCES = external/unity/unity.c \
......@@ -545,7 +545,8 @@ tests_test_iov_SOURCES = tests/test_iov.cpp
tests_test_iov_LDADD = src/libzmq.la
tests_test_ctx_destroy_SOURCES = tests/test_ctx_destroy.cpp
tests_test_ctx_destroy_LDADD = src/libzmq.la
tests_test_ctx_destroy_LDADD = src/libzmq.la ${UNITY_LIBS}
tests_test_ctx_destroy_CPPFLAGS = ${UNITY_CPPFLAGS}
tests_test_security_null_SOURCES = tests/test_security_null.cpp
tests_test_security_null_LDADD = src/libzmq.la
......@@ -668,7 +669,8 @@ tests_test_socket_null_LDADD = src/libzmq.la ${UNITY_LIBS}
tests_test_socket_null_CPPFLAGS = ${UNITY_CPPFLAGS}
tests_test_reconnect_ivl_SOURCES = tests/test_reconnect_ivl.cpp
tests_test_reconnect_ivl_LDADD = src/libzmq.la
tests_test_reconnect_ivl_LDADD = src/libzmq.la ${UNITY_LIBS}
tests_test_reconnect_ivl_CPPFLAGS = ${UNITY_CPPFLAGS}
if HAVE_CURVE
......@@ -857,14 +859,16 @@ test_apps += tests/test_poller \
tests/test_app_meta
tests_test_poller_SOURCES = tests/test_poller.cpp
tests_test_poller_LDADD = src/libzmq.la
tests_test_poller_LDADD = src/libzmq.la ${UNITY_LIBS}
tests_test_poller_CPPFLAGS = ${UNITY_CPPFLAGS}
tests_test_client_server_SOURCES = tests/test_client_server.cpp
tests_test_client_server_LDADD = src/libzmq.la ${UNITY_LIBS}
tests_test_client_server_CPPFLAGS = ${UNITY_CPPFLAGS}
tests_test_thread_safe_SOURCES = tests/test_thread_safe.cpp
tests_test_thread_safe_LDADD = src/libzmq.la
tests_test_thread_safe_LDADD = src/libzmq.la ${UNITY_LIBS}
tests_test_thread_safe_CPPFLAGS = ${UNITY_CPPFLAGS}
tests_test_timers_SOURCES = tests/test_timers.cpp
tests_test_timers_LDADD = src/libzmq.la
......
......@@ -154,6 +154,7 @@ add_library (unity
"${CMAKE_CURRENT_LIST_DIR}/../external/unity/unity_internals.h")
set_target_properties (unity PROPERTIES
PUBLIC_HEADER "${CMAKE_CURRENT_LIST_DIR}/../external/unity/unity.h")
target_compile_definitions (unity PUBLIC "UNITY_USE_COMMAND_LINE_ARGS" "UNITY_EXCLUDE_FLOAT")
target_include_directories (unity
PUBLIC "${CMAKE_CURRENT_LIST_DIR}/../external/unity")
......
......@@ -28,6 +28,17 @@
*/
#include "testutil.hpp"
#include "testutil_unity.hpp"
#include <unity.h>
void setUp ()
{
}
void tearDown ()
{
}
static void receiver (void *socket)
{
......@@ -38,40 +49,28 @@ static void receiver (void *socket)
void test_ctx_destroy ()
{
int rc;
// Set up our context and sockets
void *ctx = zmq_ctx_new ();
assert (ctx);
TEST_ASSERT_NOT_NULL (ctx);
void *socket = zmq_socket (ctx, ZMQ_PULL);
assert (socket);
TEST_ASSERT_NOT_NULL (socket);
// Close the socket
rc = zmq_close (socket);
assert (rc == 0);
// Test error - API has multiple ways to kill Contexts
rc = zmq_ctx_term (NULL);
assert (rc == -1 && errno == EFAULT);
rc = zmq_term (NULL);
assert (rc == -1 && errno == EFAULT);
TEST_ASSERT_SUCCESS_ERRNO (zmq_close (socket));
// Destroy the context
rc = zmq_ctx_destroy (ctx);
assert (rc == 0);
TEST_ASSERT_SUCCESS_ERRNO (zmq_ctx_destroy (ctx));
}
void test_ctx_shutdown ()
{
int rc;
// Set up our context and sockets
void *ctx = zmq_ctx_new ();
assert (ctx);
TEST_ASSERT_NOT_NULL (ctx);
void *socket = zmq_socket (ctx, ZMQ_PULL);
assert (socket);
TEST_ASSERT_NOT_NULL (socket);
// Spawn a thread to receive on socket
void *receiver_thread = zmq_threadstart (&receiver, socket);
......@@ -79,32 +78,49 @@ void test_ctx_shutdown ()
// Wait for thread to start up and block
msleep (SETTLE_TIME);
// Test error - Shutdown context
rc = zmq_ctx_shutdown (NULL);
assert (rc == -1 && errno == EFAULT);
// Shutdown context, if we used destroy here we would deadlock.
rc = zmq_ctx_shutdown (ctx);
assert (rc == 0);
TEST_ASSERT_SUCCESS_ERRNO (zmq_ctx_shutdown (ctx));
// Wait for thread to finish
zmq_threadclose (receiver_thread);
// Close the socket.
rc = zmq_close (socket);
assert (rc == 0);
TEST_ASSERT_SUCCESS_ERRNO (zmq_close (socket));
// Destory the context, will now not hang as we have closed the socket.
rc = zmq_ctx_destroy (ctx);
assert (rc == 0);
TEST_ASSERT_SUCCESS_ERRNO (zmq_ctx_destroy (ctx));
}
void test_zmq_ctx_term_null_fails ()
{
int rc = zmq_ctx_term (NULL);
TEST_ASSERT_EQUAL_INT (-1, rc);
TEST_ASSERT_EQUAL_INT (EFAULT, errno);
}
void test_zmq_term_null_fails ()
{
int rc = zmq_term (NULL);
TEST_ASSERT_EQUAL_INT (-1, rc);
TEST_ASSERT_EQUAL_INT (EFAULT, errno);
}
void test_zmq_ctx_shutdown_null_fails ()
{
int rc = zmq_ctx_shutdown (NULL);
TEST_ASSERT_EQUAL_INT (-1, rc);
TEST_ASSERT_EQUAL_INT (EFAULT, errno);
}
int main (void)
{
setup_test_environment ();
test_ctx_destroy ();
test_ctx_shutdown ();
return 0;
UNITY_BEGIN ();
RUN_TEST (test_ctx_destroy);
RUN_TEST (test_ctx_shutdown);
RUN_TEST (test_zmq_ctx_term_null_fails);
RUN_TEST (test_zmq_term_null_fails);
RUN_TEST (test_zmq_ctx_shutdown_null_fails);
return UNITY_END ();
}
This diff is collapsed.
......@@ -28,52 +28,54 @@
*/
#include "testutil.hpp"
#include "testutil_unity.hpp"
#include <unity.h>
#ifndef ZMQ_HAVE_WINDOWS
void test_reconnect_ivl_ipc (void)
void setUp ()
{
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:///tmp/test_reconnect_ivl");
assert (rc == 0);
void tearDown ()
{
teardown_test_context ();
}
void *sc = zmq_socket (ctx, ZMQ_PAIR);
assert (sc);
void test_reconnect_ivl_against_pair_socket (const char *my_endpoint, void *sb)
{
void *sc = test_context_socket (ZMQ_PAIR);
int interval = -1;
rc = zmq_setsockopt (sc, ZMQ_RECONNECT_IVL, &interval, sizeof (int));
assert (rc == 0);
rc = zmq_connect (sc, "ipc:///tmp/test_reconnect_ivl");
assert (rc == 0);
TEST_ASSERT_SUCCESS_ERRNO (
zmq_setsockopt (sc, ZMQ_RECONNECT_IVL, &interval, sizeof (int)));
TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (sc, my_endpoint));
bounce (sb, sc);
rc = zmq_unbind (sb, "ipc:///tmp/test_reconnect_ivl");
assert (rc == 0);
TEST_ASSERT_SUCCESS_ERRNO (zmq_unbind (sb, my_endpoint));
expect_bounce_fail (sb, sc);
rc = zmq_bind (sb, "ipc:///tmp/test_reconnect_ivl");
assert (rc == 0);
TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (sb, my_endpoint));
expect_bounce_fail (sb, sc);
rc = zmq_connect (sc, "ipc:///tmp/test_reconnect_ivl");
assert (rc == 0);
TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (sc, my_endpoint));
bounce (sb, sc);
rc = zmq_close (sc);
assert (rc == 0);
test_context_socket_close (sc);
}
rc = zmq_close (sb);
assert (rc == 0);
#ifndef ZMQ_HAVE_WINDOWS
void test_reconnect_ivl_ipc (void)
{
const char *ipc_endpoint = "ipc:///tmp/test_reconnect_ivl";
void *sb = test_context_socket (ZMQ_PAIR);
TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (sb, ipc_endpoint));
rc = zmq_ctx_term (ctx);
assert (rc == 0);
test_reconnect_ivl_against_pair_socket (ipc_endpoint, sb);
test_context_socket_close (sb);
}
#endif
......@@ -81,69 +83,39 @@ void test_reconnect_ivl_tcp (const char *address)
{
size_t len = MAX_SOCKET_STRING;
char my_endpoint[MAX_SOCKET_STRING];
void *ctx = zmq_ctx_new ();
assert (ctx);
if (streq (address, "tcp://[::1]:*")) {
if (is_ipv6_available ()) {
zmq_ctx_set (ctx, ZMQ_IPV6, 1);
} else {
zmq_ctx_term (ctx);
return;
}
}
void *sb = zmq_socket (ctx, ZMQ_PAIR);
assert (sb);
int rc = zmq_bind (sb, address);
assert (rc == 0);
rc = zmq_getsockopt (sb, ZMQ_LAST_ENDPOINT, my_endpoint, &len);
assert (rc == 0);
void *sc = zmq_socket (ctx, ZMQ_PAIR);
assert (sc);
int interval = -1;
rc = zmq_setsockopt (sc, ZMQ_RECONNECT_IVL, &interval, sizeof (int));
assert (rc == 0);
rc = zmq_connect (sc, my_endpoint);
assert (rc == 0);
bounce (sb, sc);
rc = zmq_unbind (sb, my_endpoint);
assert (rc == 0);
void *sb = test_context_socket (ZMQ_PAIR);
TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (sb, address));
TEST_ASSERT_SUCCESS_ERRNO (
zmq_getsockopt (sb, ZMQ_LAST_ENDPOINT, my_endpoint, &len));
expect_bounce_fail (sb, sc);
rc = zmq_bind (sb, my_endpoint);
assert (rc == 0);
expect_bounce_fail (sb, sc);
rc = zmq_connect (sc, my_endpoint);
assert (rc == 0);
bounce (sb, sc);
rc = zmq_close (sc);
assert (rc == 0);
test_reconnect_ivl_against_pair_socket (my_endpoint, sb);
test_context_socket_close (sb);
}
rc = zmq_close (sb);
assert (rc == 0);
void test_reconnect_ivl_tcp_ipv4 ()
{
test_reconnect_ivl_tcp ("tcp://127.0.0.1:*");
}
rc = zmq_ctx_term (ctx);
assert (rc == 0);
void test_reconnect_ivl_tcp_ipv6 ()
{
if (is_ipv6_available ()) {
zmq_ctx_set (get_test_context (), ZMQ_IPV6, 1);
test_reconnect_ivl_tcp ("tcp://[::1]:*");
}
}
int main (void)
{
setup_test_environment ();
UNITY_BEGIN ();
#ifndef ZMQ_HAVE_WINDOWS
test_reconnect_ivl_ipc ();
RUN_TEST (test_reconnect_ivl_ipc);
#endif
test_reconnect_ivl_tcp ("tcp://127.0.0.1:*");
test_reconnect_ivl_tcp ("tcp://[::1]:*");
RUN_TEST (test_reconnect_ivl_tcp_ipv4);
RUN_TEST (test_reconnect_ivl_tcp_ipv6);
return 0;
return UNITY_END ();
}
......@@ -28,41 +28,42 @@
*/
#include "testutil.hpp"
#include "testutil_unity.hpp"
#include <unity.h>
void setUp ()
{
setup_test_context ();
}
void tearDown ()
{
teardown_test_context ();
}
// Client threads loop on send/recv until told to exit
void client_thread (void *client)
{
char data = 0;
for (int count = 0; count < 15000; count++) {
int rc = zmq_send (client, &data, 1, 0);
assert (rc == 1);
send_string_expect_success (client, "0", 0);
}
data = 1;
int rc = zmq_send (client, &data, 1, 0);
assert (rc == 1);
send_string_expect_success (client, "1", 0);
}
int main (void)
void test_thread_safe ()
{
setup_test_environment ();
size_t len = MAX_SOCKET_STRING;
char my_endpoint[MAX_SOCKET_STRING];
void *ctx = zmq_ctx_new ();
assert (ctx);
void *server = zmq_socket (ctx, ZMQ_SERVER);
int rc = zmq_bind (server, "tcp://127.0.0.1:*");
assert (rc == 0);
rc = zmq_getsockopt (server, ZMQ_LAST_ENDPOINT, my_endpoint, &len);
assert (rc == 0);
void *server = test_context_socket (ZMQ_SERVER);
TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (server, "tcp://127.0.0.1:*"));
TEST_ASSERT_SUCCESS_ERRNO (
zmq_getsockopt (server, ZMQ_LAST_ENDPOINT, my_endpoint, &len));
void *client = zmq_socket (ctx, ZMQ_CLIENT);
int thread_safe;
size_t size = sizeof (int);
zmq_getsockopt (client, ZMQ_THREAD_SAFE, &thread_safe, &size);
assert (thread_safe == 1);
rc = zmq_connect (client, my_endpoint);
assert (rc == 0);
void *client = test_context_socket (ZMQ_CLIENT);
TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (client, my_endpoint));
void *t1 = zmq_threadstart (client_thread, client);
void *t2 = zmq_threadstart (client_thread, client);
......@@ -70,21 +71,49 @@ int main (void)
char data;
int threads_completed = 0;
while (threads_completed < 2) {
zmq_recv (server, &data, 1, 0);
if (data == 1)
TEST_ASSERT_SUCCESS_ERRNO (zmq_recv (server, &data, 1, 0));
if (data == '1')
threads_completed++; // Thread ended
}
zmq_threadclose (t1);
zmq_threadclose (t2);
rc = zmq_close (server);
assert (rc == 0);
test_context_socket_close (server);
test_context_socket_close (client);
}
rc = zmq_close (client);
assert (rc == 0);
void test_getsockopt_thread_safe (void *const socket)
{
int thread_safe;
size_t size = sizeof (int);
TEST_ASSERT_SUCCESS_ERRNO (
zmq_getsockopt (socket, ZMQ_THREAD_SAFE, &thread_safe, &size));
TEST_ASSERT_EQUAL_INT (1, thread_safe);
}
void test_client_getsockopt_thread_safe ()
{
void *client = test_context_socket (ZMQ_CLIENT);
test_getsockopt_thread_safe (client);
test_context_socket_close (client);
}
void test_server_getsockopt_thread_safe ()
{
void *server = test_context_socket (ZMQ_SERVER);
test_getsockopt_thread_safe (server);
test_context_socket_close (server);
}
int main (void)
{
setup_test_environment ();
rc = zmq_ctx_term (ctx);
assert (rc == 0);
// TODO this file could be merged with test_client_server
UNITY_BEGIN ();
RUN_TEST (test_client_getsockopt_thread_safe);
RUN_TEST (test_server_getsockopt_thread_safe);
RUN_TEST (test_thread_safe);
return 0;
return UNITY_END ();
}
......@@ -63,6 +63,13 @@ int test_assert_success_message_errno_helper (int rc,
#define TEST_ASSERT_SUCCESS_ERRNO(expr) \
test_assert_success_message_errno_helper (expr, NULL, #expr)
#define TEST_ASSERT_FAILURE_ERRNO(error_code, expr) \
{ \
int rc = (expr); \
TEST_ASSERT_EQUAL_INT (-1, rc); \
TEST_ASSERT_EQUAL_INT (error_code, errno); \
}
void send_string_expect_success (void *socket, const char *str, int flags)
{
const size_t len = str ? strlen (str) : 0;
......
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