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

Migrate more tests to Unity
This commit is contained in:
Luca Boccassi 2018-12-16 17:52:00 +00:00 committed by GitHub
commit 16af86d49c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 403 additions and 317 deletions

View File

@ -505,7 +505,8 @@ tests_test_sub_forward_LDADD = src/libzmq.la ${UNITY_LIBS}
tests_test_sub_forward_CPPFLAGS = ${UNITY_CPPFLAGS}
tests_test_invalid_rep_SOURCES = tests/test_invalid_rep.cpp
tests_test_invalid_rep_LDADD = src/libzmq.la
tests_test_invalid_rep_LDADD = src/libzmq.la ${UNITY_LIBS}
tests_test_invalid_rep_CPPFLAGS = ${UNITY_CPPFLAGS}
tests_test_msg_flags_SOURCES = tests/test_msg_flags.cpp
tests_test_msg_flags_LDADD = src/libzmq.la
@ -530,7 +531,8 @@ tests_test_term_endpoint_LDADD = src/libzmq.la ${UNITY_LIBS}
tests_test_term_endpoint_CPPFLAGS = ${UNITY_CPPFLAGS}
tests_test_srcfd_SOURCES = tests/test_srcfd.cpp
tests_test_srcfd_LDADD = src/libzmq.la
tests_test_srcfd_LDADD = src/libzmq.la ${UNITY_LIBS}
tests_test_srcfd_CPPFLAGS = ${UNITY_CPPFLAGS}
tests_test_monitor_SOURCES = tests/test_monitor.cpp
tests_test_monitor_LDADD = src/libzmq.la
@ -666,7 +668,8 @@ tests_test_bind_src_address_LDADD = src/libzmq.la ${UNITY_LIBS}
tests_test_bind_src_address_CPPFLAGS = ${UNITY_CPPFLAGS}
tests_test_metadata_SOURCES = tests/test_metadata.cpp
tests_test_metadata_LDADD = src/libzmq.la
tests_test_metadata_LDADD = src/libzmq.la ${UNITY_LIBS}
tests_test_metadata_CPPFLAGS = ${UNITY_CPPFLAGS}
tests_test_capabilities_SOURCES = tests/test_capabilities.cpp
tests_test_capabilities_LDADD = src/libzmq.la ${UNITY_LIBS}
@ -705,7 +708,8 @@ tests_test_heartbeats_LDADD = src/libzmq.la ${UNITY_LIBS}
tests_test_heartbeats_CPPFLAGS = ${UNITY_CPPFLAGS}
tests_test_stream_exceeds_buffer_SOURCES = tests/test_stream_exceeds_buffer.cpp
tests_test_stream_exceeds_buffer_LDADD = src/libzmq.la
tests_test_stream_exceeds_buffer_LDADD = src/libzmq.la ${UNITY_LIBS}
tests_test_stream_exceeds_buffer_CPPFLAGS = ${UNITY_CPPFLAGS}
tests_test_pub_invert_matching_SOURCES = tests/test_pub_invert_matching.cpp
tests_test_pub_invert_matching_LDADD = src/libzmq.la ${UNITY_LIBS}
@ -720,7 +724,8 @@ tests_test_base85_LDADD = src/libzmq.la ${UNITY_LIBS}
tests_test_base85_CPPFLAGS = ${UNITY_CPPFLAGS}
tests_test_sodium_SOURCES = tests/test_sodium.cpp
tests_test_sodium_LDADD = src/libzmq.la
tests_test_sodium_LDADD = src/libzmq.la ${UNITY_LIBS}
tests_test_sodium_CPPFLAGS = ${UNITY_CPPFLAGS}
tests_test_socket_null_SOURCES = tests/test_socket_null.cpp
tests_test_socket_null_LDADD = src/libzmq.la ${UNITY_LIBS}
@ -785,7 +790,8 @@ test_apps += \
tests/test_filter_ipc
tests_test_shutdown_stress_SOURCES = tests/test_shutdown_stress.cpp
tests_test_shutdown_stress_LDADD = src/libzmq.la
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
@ -867,7 +873,8 @@ tests_test_router_mandatory_tipc_SOURCES = tests/test_router_mandatory_tipc.cpp
tests_test_router_mandatory_tipc_LDADD = src/libzmq.la
tests_test_shutdown_stress_tipc_SOURCES = tests/test_shutdown_stress_tipc.cpp
tests_test_shutdown_stress_tipc_LDADD = src/libzmq.la
tests_test_shutdown_stress_tipc_LDADD = src/libzmq.la ${UNITY_LIBS}
tests_test_shutdown_stress_tipc_CPPFLAGS = ${UNITY_CPPFLAGS}
tests_test_sub_forward_tipc_SOURCES = tests/test_sub_forward_tipc.cpp
tests_test_sub_forward_tipc_LDADD = src/libzmq.la ${UNITY_LIBS}
@ -947,7 +954,8 @@ tests_test_radio_dish_LDADD = src/libzmq.la ${UNITY_LIBS}
tests_test_radio_dish_CPPFLAGS = ${UNITY_CPPFLAGS}
tests_test_scatter_gather_SOURCES = tests/test_scatter_gather.cpp
tests_test_scatter_gather_LDADD = src/libzmq.la
tests_test_scatter_gather_LDADD = src/libzmq.la ${UNITY_LIBS}
tests_test_scatter_gather_CPPFLAGS = ${UNITY_CPPFLAGS}
tests_test_dgram_SOURCES = tests/test_dgram.cpp
tests_test_dgram_LDADD = src/libzmq.la ${UNITY_LIBS}

View File

@ -28,70 +28,74 @@
*/
#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_invalid_rep ()
{
setup_test_environment ();
// Create REQ/ROUTER wiring.
void *ctx = zmq_ctx_new ();
assert (ctx);
void *router_socket = zmq_socket (ctx, ZMQ_ROUTER);
assert (router_socket);
void *req_socket = zmq_socket (ctx, ZMQ_REQ);
assert (req_socket);
void *router_socket = test_context_socket (ZMQ_ROUTER);
void *req_socket = test_context_socket (ZMQ_REQ);
int linger = 0;
int rc = zmq_setsockopt (router_socket, ZMQ_LINGER, &linger, sizeof (int));
assert (rc == 0);
rc = zmq_setsockopt (req_socket, ZMQ_LINGER, &linger, sizeof (int));
assert (rc == 0);
rc = zmq_bind (router_socket, "inproc://hi");
assert (rc == 0);
rc = zmq_connect (req_socket, "inproc://hi");
assert (rc == 0);
TEST_ASSERT_SUCCESS_ERRNO (
zmq_setsockopt (router_socket, ZMQ_LINGER, &linger, sizeof (int)));
TEST_ASSERT_SUCCESS_ERRNO (
zmq_setsockopt (req_socket, ZMQ_LINGER, &linger, sizeof (int)));
TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (router_socket, "inproc://hi"));
TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (req_socket, "inproc://hi"));
// Initial request.
rc = zmq_send (req_socket, "r", 1, 0);
assert (rc == 1);
send_string_expect_success (req_socket, "r", 0);
// Receive the request.
char addr[32];
int addr_size;
char bottom[1];
char body[1];
addr_size = zmq_recv (router_socket, addr, sizeof (addr), 0);
assert (addr_size >= 0);
rc = zmq_recv (router_socket, bottom, sizeof (bottom), 0);
assert (rc == 0);
rc = zmq_recv (router_socket, body, sizeof (body), 0);
assert (rc == 1);
TEST_ASSERT_SUCCESS_ERRNO (
addr_size = zmq_recv (router_socket, addr, sizeof (addr), 0));
TEST_ASSERT_EQUAL_INT (0, TEST_ASSERT_SUCCESS_ERRNO (zmq_recv (
router_socket, bottom, sizeof (bottom), 0)));
TEST_ASSERT_EQUAL_INT (1, TEST_ASSERT_SUCCESS_ERRNO (zmq_recv (
router_socket, body, sizeof (body), 0)));
// Send invalid reply.
rc = zmq_send (router_socket, addr, addr_size, 0);
assert (rc == addr_size);
TEST_ASSERT_EQUAL_INT (addr_size, TEST_ASSERT_SUCCESS_ERRNO (zmq_send (
router_socket, addr, addr_size, 0)));
// Send valid reply.
rc = zmq_send (router_socket, addr, addr_size, ZMQ_SNDMORE);
assert (rc == addr_size);
rc = zmq_send (router_socket, bottom, 0, ZMQ_SNDMORE);
assert (rc == 0);
rc = zmq_send (router_socket, "b", 1, 0);
assert (rc == 1);
TEST_ASSERT_EQUAL_INT (
addr_size, TEST_ASSERT_SUCCESS_ERRNO (
zmq_send (router_socket, addr, addr_size, ZMQ_SNDMORE)));
TEST_ASSERT_EQUAL_INT (0, TEST_ASSERT_SUCCESS_ERRNO (zmq_send (
router_socket, bottom, 0, ZMQ_SNDMORE)));
send_string_expect_success (router_socket, "b", 0);
// Check whether we've got the valid reply.
rc = zmq_recv (req_socket, body, sizeof (body), 0);
assert (rc == 1);
assert (body[0] == 'b');
recv_string_expect_success (req_socket, "b", 0);
// Tear down the wiring.
rc = zmq_close (router_socket);
assert (rc == 0);
rc = zmq_close (req_socket);
assert (rc == 0);
rc = zmq_ctx_term (ctx);
assert (rc == 0);
return 0;
test_context_socket_close (router_socket);
test_context_socket_close (req_socket);
}
int main ()
{
setup_test_environment ();
UNITY_BEGIN ();
RUN_TEST (test_invalid_rep);
return UNITY_END ();
}

View File

@ -28,6 +28,17 @@
*/
#include "testutil.hpp"
#include "testutil_unity.hpp"
#include <unity.h>
void setUp ()
{
}
void tearDown ()
{
}
static void zap_handler (void *handler_)
{
@ -72,59 +83,53 @@ static void zap_handler (void *handler_)
close_zero_linger (handler_);
}
int main (void)
void test_metadata ()
{
setup_test_environment ();
size_t len = MAX_SOCKET_STRING;
char my_endpoint[MAX_SOCKET_STRING];
void *ctx = zmq_ctx_new ();
assert (ctx);
setup_test_context ();
// Spawn ZAP handler
// We create and bind ZAP socket in main thread to avoid case
// where child thread does not start up fast enough.
void *handler = zmq_socket (ctx, ZMQ_REP);
assert (handler);
int rc = zmq_bind (handler, "inproc://zeromq.zap.01");
assert (rc == 0);
void *handler = zmq_socket (get_test_context (), ZMQ_REP);
TEST_ASSERT_NOT_NULL (handler);
TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (handler, "inproc://zeromq.zap.01"));
void *zap_thread = zmq_threadstart (&zap_handler, handler);
void *server = zmq_socket (ctx, ZMQ_DEALER);
assert (server);
void *client = zmq_socket (ctx, ZMQ_DEALER);
assert (client);
rc = zmq_setsockopt (server, ZMQ_ZAP_DOMAIN, "DOMAIN", 6);
assert (rc == 0);
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);
rc = zmq_connect (client, my_endpoint);
assert (rc == 0);
void *server = test_context_socket (ZMQ_DEALER);
void *client = test_context_socket (ZMQ_DEALER);
TEST_ASSERT_SUCCESS_ERRNO (
zmq_setsockopt (server, ZMQ_ZAP_DOMAIN, "DOMAIN", 6));
bind_loopback_ipv4 (server, my_endpoint, sizeof (my_endpoint));
TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (client, my_endpoint));
s_send (client, "This is a message");
zmq_msg_t msg;
zmq_msg_init (&msg);
rc = zmq_msg_recv (&msg, server, 0);
assert (rc != -1);
assert (streq (zmq_msg_gets (&msg, "Hello"), "World"));
assert (streq (zmq_msg_gets (&msg, "Socket-Type"), "DEALER"));
assert (streq (zmq_msg_gets (&msg, "User-Id"), "anonymous"));
assert (streq (zmq_msg_gets (&msg, "Peer-Address"), "127.0.0.1"));
TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_recv (&msg, server, 0));
TEST_ASSERT_EQUAL_STRING ("World", zmq_msg_gets (&msg, "Hello"));
TEST_ASSERT_EQUAL_STRING ("DEALER", zmq_msg_gets (&msg, "Socket-Type"));
TEST_ASSERT_EQUAL_STRING ("anonymous", zmq_msg_gets (&msg, "User-Id"));
TEST_ASSERT_EQUAL_STRING ("127.0.0.1", zmq_msg_gets (&msg, "Peer-Address"));
assert (zmq_msg_gets (&msg, "No Such") == NULL);
assert (zmq_errno () == EINVAL);
TEST_ASSERT_NULL (zmq_msg_gets (&msg, "No Such"));
TEST_ASSERT_EQUAL_INT (EINVAL, zmq_errno ());
zmq_msg_close (&msg);
close_zero_linger (client);
close_zero_linger (server);
test_context_socket_close_zero_linger (client);
test_context_socket_close_zero_linger (server);
// Shutdown
rc = zmq_ctx_term (ctx);
assert (rc == 0);
teardown_test_context ();
// Wait until ZAP handler terminates
zmq_threadclose (zap_thread);
return 0;
}
int main ()
{
setup_test_environment ();
UNITY_BEGIN ();
RUN_TEST (test_metadata);
return UNITY_END ();
}

View File

@ -28,57 +28,70 @@
*/
#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 *scatter = zmq_socket (ctx, ZMQ_SCATTER);
void *gather = zmq_socket (ctx, ZMQ_GATHER);
void *gather2 = zmq_socket (ctx, ZMQ_GATHER);
void tearDown ()
{
teardown_test_context ();
}
int rc = zmq_bind (scatter, "inproc://test-scatter-gather");
assert (rc == 0);
void test_scatter_gather_multipart_fails ()
{
void *scatter = test_context_socket (ZMQ_SCATTER);
void *gather = test_context_socket (ZMQ_GATHER);
rc = zmq_connect (gather, "inproc://test-scatter-gather");
assert (rc == 0);
TEST_ASSERT_SUCCESS_ERRNO (
zmq_bind (scatter, "inproc://test-scatter-gather"));
rc = zmq_connect (gather2, "inproc://test-scatter-gather");
assert (rc == 0);
TEST_ASSERT_SUCCESS_ERRNO (
zmq_connect (gather, "inproc://test-scatter-gather"));
// Should fail, multipart is not supported
rc = s_sendmore (scatter, "1");
assert (rc == -1);
TEST_ASSERT_FAILURE_ERRNO (EINVAL, s_sendmore (scatter, "1"));
rc = s_send (scatter, "1");
assert (rc == 1);
rc = s_send (scatter, "2");
assert (rc == 1);
char *message = s_recv (gather);
assert (message);
assert (streq (message, "1"));
free (message);
message = s_recv (gather2);
assert (message);
assert (streq (message, "2"));
free (message);
rc = zmq_close (scatter);
assert (rc == 0);
rc = zmq_close (gather);
assert (rc == 0);
rc = zmq_close (gather2);
assert (rc == 0);
rc = zmq_ctx_term (ctx);
assert (rc == 0);
return 0;
test_context_socket_close (scatter);
test_context_socket_close (gather);
}
void test_scatter_gather ()
{
void *scatter = test_context_socket (ZMQ_SCATTER);
void *gather = test_context_socket (ZMQ_GATHER);
void *gather2 = test_context_socket (ZMQ_GATHER);
TEST_ASSERT_SUCCESS_ERRNO (
zmq_bind (scatter, "inproc://test-scatter-gather"));
TEST_ASSERT_SUCCESS_ERRNO (
zmq_connect (gather, "inproc://test-scatter-gather"));
TEST_ASSERT_SUCCESS_ERRNO (
zmq_connect (gather2, "inproc://test-scatter-gather"));
send_string_expect_success (scatter, "1", 0);
send_string_expect_success (scatter, "2", 0);
recv_string_expect_success (gather, "1", 0);
recv_string_expect_success (gather2, "2", 0);
test_context_socket_close (scatter);
test_context_socket_close (gather);
test_context_socket_close (gather2);
}
int main ()
{
setup_test_environment ();
UNITY_BEGIN ();
RUN_TEST (test_scatter_gather);
RUN_TEST (test_scatter_gather_multipart_fails);
return UNITY_END ();
}

View File

@ -28,73 +28,70 @@
*/
#include "testutil.hpp"
#include "testutil_unity.hpp"
void setUp ()
{
}
void tearDown ()
{
}
#define THREAD_COUNT 100
struct thread_data
{
void *ctx;
char endpoint[MAX_SOCKET_STRING];
};
extern "C" {
static void worker (void *data_)
{
int rc;
void *socket;
struct thread_data *tdata = (struct thread_data *) data_;
socket = zmq_socket (tdata->ctx, ZMQ_SUB);
assert (socket);
void *socket = zmq_socket (get_test_context (), ZMQ_SUB);
rc = zmq_connect (socket, tdata->endpoint);
assert (rc == 0);
TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (socket, tdata->endpoint));
// Start closing the socket while the connecting process is underway.
rc = zmq_close (socket);
assert (rc == 0);
TEST_ASSERT_SUCCESS_ERRNO (zmq_close (socket));
}
}
int main (void)
void test_shutdown_stress ()
{
setup_test_environment ();
void *socket;
int i;
int j;
int rc;
void *threads[THREAD_COUNT];
for (j = 0; j != 10; j++) {
for (int j = 0; j != 10; j++) {
// Check the shutdown with many parallel I/O threads.
struct thread_data tdata;
tdata.ctx = zmq_ctx_new ();
assert (tdata.ctx);
zmq_ctx_set (tdata.ctx, ZMQ_IO_THREADS, 7);
setup_test_context ();
zmq_ctx_set (get_test_context (), ZMQ_IO_THREADS, 7);
socket = zmq_socket (tdata.ctx, ZMQ_PUB);
assert (socket);
void *socket = test_context_socket (ZMQ_PUB);
rc = zmq_bind (socket, "tcp://127.0.0.1:*");
assert (rc == 0);
size_t len = MAX_SOCKET_STRING;
rc = zmq_getsockopt (socket, ZMQ_LAST_ENDPOINT, tdata.endpoint, &len);
assert (rc == 0);
bind_loopback_ipv4 (socket, tdata.endpoint, sizeof (tdata.endpoint));
for (i = 0; i != THREAD_COUNT; i++) {
for (int i = 0; i != THREAD_COUNT; i++) {
threads[i] = zmq_threadstart (&worker, &tdata);
}
for (i = 0; i != THREAD_COUNT; i++) {
for (int i = 0; i != THREAD_COUNT; i++) {
zmq_threadclose (threads[i]);
}
rc = zmq_close (socket);
assert (rc == 0);
test_context_socket_close (socket);
rc = zmq_ctx_term (tdata.ctx);
assert (rc == 0);
teardown_test_context ();
}
return 0;
}
int main ()
{
setup_test_environment ();
UNITY_BEGIN ();
RUN_TEST (test_shutdown_stress);
return UNITY_END ();
}

View File

@ -28,71 +28,71 @@
*/
#include "testutil.hpp"
#include "testutil_unity.hpp"
void setUp ()
{
}
void tearDown ()
{
}
#define THREAD_COUNT 100
extern "C" {
static void *worker (void *s_)
{
int rc;
rc = zmq_connect (s_, "tipc://{5560,0}@0.0.0");
assert (rc == 0);
TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (s_, "tipc://{5560,0}@0.0.0"));
// Start closing the socket while the connecting process is underway.
rc = zmq_close (s_);
assert (rc == 0);
TEST_ASSERT_SUCCESS_ERRNO (zmq_close (s_));
return NULL;
}
}
int main (void)
void test_shutdown_stress_tipc ()
{
void *s1;
void *s2;
int i;
int j;
pthread_t threads[THREAD_COUNT];
for (j = 0; j != 10; j++) {
// Check the shutdown with many parallel I/O threads.
setup_test_context ();
zmq_ctx_set (get_test_context (), ZMQ_IO_THREADS, 7);
s1 = test_context_socket (ZMQ_PUB);
TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (s1, "tipc://{5560,0,0}"));
for (i = 0; i != THREAD_COUNT; i++) {
s2 = zmq_socket (get_test_context (), ZMQ_SUB);
TEST_ASSERT_SUCCESS_RAW_ERRNO (
pthread_create (&threads[i], NULL, worker, s2));
}
for (i = 0; i != THREAD_COUNT; i++) {
TEST_ASSERT_SUCCESS_RAW_ERRNO (pthread_join (threads[i], NULL));
}
test_context_socket_close (s1);
teardown_test_context ();
}
}
int main ()
{
if (!is_tipc_available ()) {
printf ("TIPC environment unavailable, skipping test\n");
return 77;
}
void *ctx;
void *s1;
void *s2;
int i;
int j;
int rc;
pthread_t threads[THREAD_COUNT];
fprintf (stderr, "test_shutdown_stress_tipc running...\n");
for (j = 0; j != 10; j++) {
// Check the shutdown with many parallel I/O threads.
ctx = zmq_init (7);
assert (ctx);
s1 = zmq_socket (ctx, ZMQ_PUB);
assert (s1);
rc = zmq_bind (s1, "tipc://{5560,0,0}");
assert (rc == 0);
for (i = 0; i != THREAD_COUNT; i++) {
s2 = zmq_socket (ctx, ZMQ_SUB);
assert (s2);
rc = pthread_create (&threads[i], NULL, worker, s2);
assert (rc == 0);
}
for (i = 0; i != THREAD_COUNT; i++) {
rc = pthread_join (threads[i], NULL);
assert (rc == 0);
}
rc = zmq_close (s1);
assert (rc == 0);
rc = zmq_ctx_term (ctx);
assert (rc == 0);
}
return 0;
UNITY_BEGIN ();
RUN_TEST (test_shutdown_stress_tipc);
return UNITY_END ();
}

View File

@ -28,6 +28,15 @@
*/
#include "testutil.hpp"
#include "testutil_unity.hpp"
void setUp ()
{
}
void tearDown ()
{
}
// There is no way to test for correctness because of the embedded RNG.
void test__zmq_curve_keypair__always__success (void)
@ -39,10 +48,9 @@ void test__zmq_curve_keypair__always__success (void)
const int rc = zmq_curve_keypair (public_key, secret_key);
#if defined(ZMQ_HAVE_CURVE)
assert (rc == 0);
TEST_ASSERT_SUCCESS_ERRNO (rc);
#else
assert (rc == -1);
assert (zmq_errno () == ENOTSUP);
TEST_ASSERT_FAILURE_ERRNO (ENOTSUP, rc);
#endif
}
@ -58,12 +66,10 @@ void test__zmq_curve_public__valid__success ()
const int rc = zmq_curve_public (out_public, secret_key);
#if defined(ZMQ_HAVE_CURVE)
assert (rc == 0);
assert (zmq_errno () == 0);
assert (streq (out_public, public_key));
TEST_ASSERT_SUCCESS_ERRNO (rc);
TEST_ASSERT_EQUAL_STRING (public_key, out_public);
#else
assert (rc == -1);
assert (zmq_errno () == ENOTSUP);
TEST_ASSERT_FAILURE_ERRNO (ENOTSUP, rc);
(void) public_key;
#endif
}
@ -77,23 +83,32 @@ void test__zmq_curve_public__invalid__failure (const char *secret_)
const int rc = zmq_curve_public (out_public, secret_);
#if defined(ZMQ_HAVE_CURVE)
assert (rc == -1);
assert (zmq_errno () == EINVAL);
assert (streq (out_public, ""));
TEST_ASSERT_FAILURE_ERRNO (EINVAL, rc);
TEST_ASSERT_EQUAL_STRING ("", out_public);
#else
assert (rc == -1);
assert (zmq_errno () == ENOTSUP);
TEST_ASSERT_FAILURE_ERRNO (ENOTSUP, rc);
#endif
}
int main (void)
void test__zmq_curve_public__invalid__failure_short ()
{
test__zmq_curve_keypair__always__success ();
test__zmq_curve_public__valid__success ();
test__zmq_curve_public__invalid__failure ("42");
}
void test__zmq_curve_public__invalid__failure_long ()
{
test__zmq_curve_public__invalid__failure (
"0123456789012345678901234567890123456789.");
return 0;
}
int main ()
{
UNITY_BEGIN ();
RUN_TEST (test__zmq_curve_keypair__always__success);
RUN_TEST (test__zmq_curve_public__valid__success);
RUN_TEST (test__zmq_curve_public__invalid__failure_short);
RUN_TEST (test__zmq_curve_public__invalid__failure_long);
return UNITY_END ();
}

View File

@ -28,6 +28,19 @@
*/
#include "testutil.hpp"
#include "testutil_unity.hpp"
#include <unity.h>
void setUp ()
{
setup_test_context ();
}
void tearDown ()
{
teardown_test_context ();
}
#define MSG_SIZE 20
@ -40,48 +53,34 @@
#include <netdb.h>
#endif
int main (void)
void test_srcfd ()
{
int rc;
size_t len = MAX_SOCKET_STRING;
char my_endpoint[MAX_SOCKET_STRING];
setup_test_environment ();
// Create the infrastructure
void *ctx = zmq_ctx_new ();
assert (ctx);
void *rep = zmq_socket (ctx, ZMQ_REP);
assert (rep);
void *req = zmq_socket (ctx, ZMQ_REQ);
assert (req);
void *rep = test_context_socket (ZMQ_REP);
void *req = test_context_socket (ZMQ_REQ);
rc = zmq_bind (rep, "tcp://127.0.0.1:*");
assert (rc == 0);
bind_loopback_ipv4 (rep, my_endpoint, sizeof (my_endpoint));
rc = zmq_getsockopt (rep, ZMQ_LAST_ENDPOINT, my_endpoint, &len);
assert (rc == 0);
rc = zmq_connect (req, my_endpoint);
assert (rc == 0);
TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (req, my_endpoint));
char tmp[MSG_SIZE];
memset (tmp, 0, MSG_SIZE);
zmq_send (req, tmp, MSG_SIZE, 0);
zmq_msg_t msg;
rc = zmq_msg_init (&msg);
assert (rc == 0);
TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_init (&msg));
zmq_recvmsg (rep, &msg, 0);
assert (zmq_msg_size (&msg) == MSG_SIZE);
TEST_ASSERT_EQUAL_UINT (MSG_SIZE, zmq_msg_size (&msg));
// get the messages source file descriptor
int src_fd = zmq_msg_get (&msg, ZMQ_SRCFD);
assert (src_fd >= 0);
TEST_ASSERT_GREATER_OR_EQUAL (0, src_fd);
rc = zmq_msg_close (&msg);
assert (rc == 0);
TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_close (&msg));
// get the remote endpoint
struct sockaddr_storage ss;
@ -90,37 +89,38 @@ int main (void)
#else
socklen_t addrlen = sizeof ss;
#endif
rc = getpeername (src_fd, (struct sockaddr *) &ss, &addrlen);
assert (rc == 0);
TEST_ASSERT_SUCCESS_RAW_ERRNO (
getpeername (src_fd, (struct sockaddr *) &ss, &addrlen));
char host[NI_MAXHOST];
rc = getnameinfo ((struct sockaddr *) &ss, addrlen, host, sizeof host, NULL,
0, NI_NUMERICHOST);
assert (rc == 0);
TEST_ASSERT_SUCCESS_RAW_ERRNO (getnameinfo ((struct sockaddr *) &ss,
addrlen, host, sizeof host,
NULL, 0, NI_NUMERICHOST));
// assert it is localhost which connected
assert (strcmp (host, "127.0.0.1") == 0);
TEST_ASSERT_EQUAL_STRING ("127.0.0.1", host);
rc = zmq_close (rep);
assert (rc == 0);
rc = zmq_close (req);
assert (rc == 0);
test_context_socket_close (rep);
test_context_socket_close (req);
// sleep a bit for the socket to be freed
msleep (SETTLE_TIME);
// getting name from closed socket will fail
rc = getpeername (src_fd, (struct sockaddr *) &ss, &addrlen);
#ifdef ZMQ_HAVE_WINDOWS
assert (rc == SOCKET_ERROR);
assert (WSAGetLastError () == WSAENOTSOCK);
const int expected_errno = WSAENOTSOCK;
#else
assert (rc == -1);
assert (errno == EBADF);
const int expected_errno = EBADF;
#endif
rc = zmq_ctx_term (ctx);
assert (rc == 0);
return 0;
TEST_ASSERT_FAILURE_RAW_ERRNO (
expected_errno, getpeername (src_fd, (struct sockaddr *) &ss, &addrlen));
}
int main ()
{
setup_test_environment ();
UNITY_BEGIN ();
RUN_TEST (test_srcfd);
return UNITY_END ();
}

View File

@ -28,6 +28,17 @@
*/
#include "testutil.hpp"
#include "testutil_unity.hpp"
void setUp ()
{
setup_test_context ();
}
void tearDown ()
{
teardown_test_context ();
}
#if defined(ZMQ_HAVE_WINDOWS)
#include <winsock2.h>
@ -36,19 +47,19 @@
#define close closesocket
#endif
int main ()
void test_stream_exceeds_buffer ()
{
const int msgsize = 8193;
char sndbuf[msgsize] = "\xde\xad\xbe\xef";
unsigned char rcvbuf[msgsize];
char my_endpoint[MAX_SOCKET_STRING];
int server_sock = socket (AF_INET, SOCK_STREAM, 0);
assert (server_sock != -1);
int server_sock =
TEST_ASSERT_SUCCESS_RAW_ERRNO (socket (AF_INET, SOCK_STREAM, 0));
int enable = 1;
int rc = setsockopt (server_sock, SOL_SOCKET, SO_REUSEADDR,
(char *) &enable, sizeof (enable));
assert (rc != -1);
TEST_ASSERT_SUCCESS_RAW_ERRNO (setsockopt (server_sock, SOL_SOCKET,
SO_REUSEADDR, (char *) &enable,
sizeof (enable)));
struct sockaddr_in saddr;
memset (&saddr, 0, sizeof (saddr));
@ -60,33 +71,26 @@ int main ()
saddr.sin_port = htons (12345);
#endif
rc = bind (server_sock, (struct sockaddr *) &saddr, sizeof (saddr));
assert (rc != -1);
rc = listen (server_sock, 1);
assert (rc != -1);
TEST_ASSERT_SUCCESS_RAW_ERRNO (
bind (server_sock, (struct sockaddr *) &saddr, sizeof (saddr)));
TEST_ASSERT_SUCCESS_RAW_ERRNO (listen (server_sock, 1));
#if !defined(_WIN32_WINNT) || (_WIN32_WINNT >= 0x0600)
socklen_t saddr_len = sizeof (saddr);
rc = getsockname (server_sock, (struct sockaddr *) &saddr, &saddr_len);
assert (rc != -1);
TEST_ASSERT_SUCCESS_RAW_ERRNO (
getsockname (server_sock, (struct sockaddr *) &saddr, &saddr_len));
#endif
sprintf (my_endpoint, "tcp://127.0.0.1:%d", ntohs (saddr.sin_port));
void *zctx = zmq_ctx_new ();
assert (zctx);
void *zsock = zmq_socket (zctx, ZMQ_STREAM);
assert (zsock);
rc = zmq_connect (zsock, my_endpoint);
assert (rc != -1);
void *zsock = test_context_socket (ZMQ_STREAM);
TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (zsock, my_endpoint));
int client_sock = accept (server_sock, NULL, NULL);
assert (client_sock != -1);
int client_sock =
TEST_ASSERT_SUCCESS_RAW_ERRNO (accept (server_sock, NULL, NULL));
rc = close (server_sock);
assert (rc != -1);
TEST_ASSERT_SUCCESS_RAW_ERRNO (close (server_sock));
rc = send (client_sock, sndbuf, msgsize, 0);
assert (rc == msgsize);
TEST_ASSERT_EQUAL_INT (msgsize, send (client_sock, sndbuf, msgsize, 0));
zmq_msg_t msg;
zmq_msg_init (&msg);
@ -94,12 +98,10 @@ int main ()
int rcvbytes = 0;
while (rcvbytes == 0) // skip connection notification, if any
{
rc = zmq_msg_recv (&msg, zsock, 0); // peerid
assert (rc != -1);
assert (zmq_msg_more (&msg));
rcvbytes = zmq_msg_recv (&msg, zsock, 0);
assert (rcvbytes != -1);
assert (!zmq_msg_more (&msg));
TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_recv (&msg, zsock, 0)); // peerid
TEST_ASSERT_TRUE (zmq_msg_more (&msg));
rcvbytes = TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_recv (&msg, zsock, 0));
TEST_ASSERT_FALSE (zmq_msg_more (&msg));
}
// for this test, we only collect the first chunk
@ -107,18 +109,21 @@ int main ()
memcpy (rcvbuf, zmq_msg_data (&msg), zmq_msg_size (&msg));
zmq_msg_close (&msg);
zmq_close (zsock);
test_context_socket_close (zsock);
close (client_sock);
zmq_ctx_destroy (zctx);
assert (rcvbytes >= 4);
TEST_ASSERT_GREATER_OR_EQUAL (4, rcvbytes);
// notice that only the 1st byte gets corrupted
assert (rcvbuf[3] == 0xef);
assert (rcvbuf[2] == 0xbe);
assert (rcvbuf[1] == 0xad);
assert (rcvbuf[0] == 0xde);
(void) (rc); // avoid -Wunused-but-set-variable warning in release build
TEST_ASSERT_EQUAL_UINT (0xef, rcvbuf[3]);
TEST_ASSERT_EQUAL_UINT (0xbe, rcvbuf[2]);
TEST_ASSERT_EQUAL_UINT (0xad, rcvbuf[1]);
TEST_ASSERT_EQUAL_UINT (0xde, rcvbuf[0]);
}
int main ()
{
UNITY_BEGIN ();
RUN_TEST (test_stream_exceeds_buffer);
return UNITY_END ();
}

View File

@ -79,6 +79,40 @@ int test_assert_success_message_raw_errno_helper (int rc_,
return rc_;
}
int test_assert_failure_message_raw_errno_helper (int rc_,
int expected_errno_,
const char *msg_,
const char *expr_)
{
char buffer[512];
buffer[sizeof (buffer) - 1] =
0; // to ensure defined behavior with VC++ <= 2013
if (rc_ != -1) {
snprintf (buffer, sizeof (buffer) - 1,
"%s was unexpectedly successful%s%s%s, expected "
"errno = %i, actual return value = %i",
expr_, msg_ ? " (additional info: " : "", msg_ ? msg_ : "",
msg_ ? ")" : "", expected_errno_, rc_);
TEST_FAIL_MESSAGE (buffer);
} else {
#if defined ZMQ_HAVE_WINDOWS
int current_errno = WSAGetLastError ();
#else
int current_errno = errno;
#endif
if (current_errno != expected_errno_) {
snprintf (buffer, sizeof (buffer) - 1,
"%s failed with an unexpected error%s%s%s, expected "
"errno = %i, actual errno = %i",
expr_, msg_ ? " (additional info: " : "",
msg_ ? msg_ : "", msg_ ? ")" : "", expected_errno_,
current_errno);
TEST_FAIL_MESSAGE (buffer);
}
}
return rc_;
}
#define TEST_ASSERT_SUCCESS_MESSAGE_ERRNO(expr, msg) \
test_assert_success_message_errno_helper (expr, msg, #expr)
@ -88,6 +122,9 @@ int test_assert_success_message_raw_errno_helper (int rc_,
#define TEST_ASSERT_SUCCESS_RAW_ERRNO(expr) \
test_assert_success_message_raw_errno_helper (expr, NULL, #expr)
#define TEST_ASSERT_FAILURE_RAW_ERRNO(error_code, expr) \
test_assert_failure_message_raw_errno_helper (expr, error_code, NULL, #expr)
#define TEST_ASSERT_FAILURE_ERRNO(error_code, expr) \
{ \
int _rc = (expr); \
@ -201,7 +238,9 @@ void internal_manage_test_sockets (void *socket_, bool add_)
test_sockets[i] = test_sockets[i + 1];
}
}
TEST_ASSERT_TRUE (found);
TEST_ASSERT_TRUE_MESSAGE (found,
"Attempted to close a socket that was "
"not created by test_context_socket");
--test_socket_count;
}
}