Unverified Commit 6d77558c authored by Luca Boccassi's avatar Luca Boccassi Committed by GitHub

Merge pull request #3459 from sigiesec/migrate-testutil

Migrate testutil* to Unity, and build testutil as separate library
parents f4b9cc99 c2cd2966
...@@ -7,6 +7,7 @@ doc/Makefile ...@@ -7,6 +7,7 @@ doc/Makefile
Makefile.in Makefile.in
configure configure
libtool libtool
libtestutil.a
libunity.a libunity.a
config config
config.status config.status
......
This diff is collapsed.
...@@ -33,6 +33,7 @@ ...@@ -33,6 +33,7 @@
#if !defined ZMQ_HAVE_WINDOWS #if !defined ZMQ_HAVE_WINDOWS
#include <sys/socket.h> #include <sys/socket.h>
#include <netinet/in.h> #include <netinet/in.h>
#include <netdb.h>
#endif #endif
#include "address.hpp" #include "address.hpp"
......
...@@ -136,7 +136,7 @@ zmq::socks_request_t::socks_request_t (uint8_t command_, ...@@ -136,7 +136,7 @@ zmq::socks_request_t::socks_request_t (uint8_t command_,
hostname (ZMQ_MOVE (hostname_)), hostname (ZMQ_MOVE (hostname_)),
port (port_) port (port_)
{ {
zmq_assert (hostname_.size () <= UINT8_MAX); zmq_assert (hostname.size () <= UINT8_MAX);
} }
zmq::socks_request_encoder_t::socks_request_encoder_t () : zmq::socks_request_encoder_t::socks_request_encoder_t () :
......
...@@ -297,13 +297,20 @@ void zmq::stream_engine_t::terminate () ...@@ -297,13 +297,20 @@ void zmq::stream_engine_t::terminate ()
} }
void zmq::stream_engine_t::in_event () void zmq::stream_engine_t::in_event ()
{
// ignore errors
const bool res = in_event_internal ();
LIBZMQ_UNUSED (res);
}
bool zmq::stream_engine_t::in_event_internal ()
{ {
zmq_assert (!_io_error); zmq_assert (!_io_error);
// If still handshaking, receive and process the greeting message. // If still handshaking, receive and process the greeting message.
if (unlikely (_handshaking)) if (unlikely (_handshaking))
if (!handshake ()) if (!handshake ())
return; return false;
zmq_assert (_decoder); zmq_assert (_decoder);
...@@ -311,7 +318,7 @@ void zmq::stream_engine_t::in_event () ...@@ -311,7 +318,7 @@ void zmq::stream_engine_t::in_event ()
if (_input_stopped) { if (_input_stopped) {
rm_fd (_handle); rm_fd (_handle);
_io_error = true; _io_error = true;
return; return true; // TODO or return false in this case too?
} }
// If there's no data to process in the buffer... // If there's no data to process in the buffer...
...@@ -329,12 +336,14 @@ void zmq::stream_engine_t::in_event () ...@@ -329,12 +336,14 @@ void zmq::stream_engine_t::in_event ()
// connection closed by peer // connection closed by peer
errno = EPIPE; errno = EPIPE;
error (connection_error); error (connection_error);
return; return false;
} }
if (rc == -1) { if (rc == -1) {
if (errno != EAGAIN) if (errno != EAGAIN) {
error (connection_error); error (connection_error);
return; return false;
}
return true;
} }
// Adjust input size // Adjust input size
...@@ -363,13 +372,14 @@ void zmq::stream_engine_t::in_event () ...@@ -363,13 +372,14 @@ void zmq::stream_engine_t::in_event ()
if (rc == -1) { if (rc == -1) {
if (errno != EAGAIN) { if (errno != EAGAIN) {
error (protocol_error); error (protocol_error);
return; return false;
} }
_input_stopped = true; _input_stopped = true;
reset_pollin (_handle); reset_pollin (_handle);
} }
_session->flush (); _session->flush ();
return true;
} }
void zmq::stream_engine_t::out_event () void zmq::stream_engine_t::out_event ()
...@@ -497,7 +507,8 @@ bool zmq::stream_engine_t::restart_input () ...@@ -497,7 +507,8 @@ bool zmq::stream_engine_t::restart_input ()
_session->flush (); _session->flush ();
// Speculative read. // Speculative read.
in_event (); if (!in_event_internal ())
return false;
} }
return true; return true;
......
...@@ -87,6 +87,8 @@ class stream_engine_t : public io_object_t, public i_engine ...@@ -87,6 +87,8 @@ class stream_engine_t : public io_object_t, public i_engine
void timer_event (int id_); void timer_event (int id_);
private: private:
bool in_event_internal ();
// Unplug the engine from the session. // Unplug the engine from the session.
void unplug (); void unplug ();
......
...@@ -75,6 +75,11 @@ set(tests ...@@ -75,6 +75,11 @@ set(tests
test_mock_pub_sub test_mock_pub_sub
) )
if(NOT WIN32)
list(APPEND tests
test_security_gssapi)
endif()
if(ZMQ_HAVE_CURVE) if(ZMQ_HAVE_CURVE)
list(APPEND tests list(APPEND tests
test_security_curve) test_security_curve)
...@@ -168,6 +173,32 @@ set_target_properties(unity PROPERTIES ...@@ -168,6 +173,32 @@ set_target_properties(unity PROPERTIES
target_compile_definitions(unity PUBLIC "UNITY_USE_COMMAND_LINE_ARGS" "UNITY_EXCLUDE_FLOAT") target_compile_definitions(unity PUBLIC "UNITY_USE_COMMAND_LINE_ARGS" "UNITY_EXCLUDE_FLOAT")
target_include_directories(unity PUBLIC "${CMAKE_CURRENT_LIST_DIR}/../external/unity") target_include_directories(unity PUBLIC "${CMAKE_CURRENT_LIST_DIR}/../external/unity")
set(TESTUTIL_SOURCES
testutil.cpp
testutil.hpp
testutil_monitoring.cpp
testutil_monitoring.hpp
testutil_security.cpp
testutil_security.hpp
testutil_unity.cpp
testutil_unity.hpp
)
if(BUILD_STATIC)
add_library(testutil-static STATIC ${TESTUTIL_SOURCES})
target_link_libraries(testutil-static libzmq-static ${OPTIONAL_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT} unity)
endif()
if(BUILD_SHARED)
add_library(testutil STATIC ${TESTUTIL_SOURCES})
target_link_libraries(testutil libzmq ${OPTIONAL_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT} unity)
endif()
if(BUILD_STATIC AND NOT BUILD_SHARED)
# use testutil-static for both tests and unit tests
set(TESTUTIL_LIB testutil-static)
else()
# use testutil for tests and testutil-static for unit tests
set(TESTUTIL_LIB testutil)
endif()
if(MSVC_VERSION LESS 1700) if(MSVC_VERSION LESS 1700)
set_source_files_properties("${CMAKE_CURRENT_LIST_DIR}/../external/unity/unity.c" PROPERTIES LANGUAGE CXX) set_source_files_properties("${CMAKE_CURRENT_LIST_DIR}/../external/unity/unity.c" PROPERTIES LANGUAGE CXX)
endif() endif()
...@@ -176,13 +207,11 @@ if(MSVC_VERSION LESS 1600) ...@@ -176,13 +207,11 @@ if(MSVC_VERSION LESS 1600)
target_compile_definitions(unity PUBLIC "UNITY_EXCLUDE_STDINT_H") target_compile_definitions(unity PUBLIC "UNITY_EXCLUDE_STDINT_H")
endif() endif()
# add library and include dirs for all targets # add include dirs for all targets
if(BUILD_SHARED)
link_libraries(libzmq ${OPTIONAL_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT} unity)
else()
link_libraries(libzmq-static ${OPTIONAL_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT} unity)
endif()
include_directories("${ZeroMQ_SOURCE_DIR}/../include" "${ZeroMQ_BINARY_DIR}") include_directories("${ZeroMQ_SOURCE_DIR}/../include" "${ZeroMQ_BINARY_DIR}")
if(WIN32)
add_definitions(-D_CRT_NONSTDC_NO_DEPRECATE)
endif()
foreach(test ${tests}) foreach(test ${tests})
# target_sources not supported before CMake 3.1 # target_sources not supported before CMake 3.1
...@@ -191,14 +220,11 @@ foreach(test ${tests}) ...@@ -191,14 +220,11 @@ foreach(test ${tests})
"../src/tweetnacl.c" "../src/tweetnacl.c"
"../src/err.cpp" "../src/err.cpp"
"../src/random.cpp" "../src/random.cpp"
"../src/clock.cpp" "../src/clock.cpp")
"testutil_security.hpp")
elseif(${test} MATCHES test_security_zap)
add_executable(${test} ${test}.cpp
"testutil_security.hpp")
else() else()
add_executable(${test} ${test}.cpp "testutil.hpp" "testutil_unity.hpp" "testutil_monitoring.hpp") add_executable(${test} ${test}.cpp)
endif() endif()
target_link_libraries(${test} ${TESTUTIL_LIB})
if(WIN32) if(WIN32)
# This is the output for Debug dynamic builds on Visual Studio 6.0 # This is the output for Debug dynamic builds on Visual Studio 6.0
# You should provide the correct directory, don't know how to do it automatically # You should provide the correct directory, don't know how to do it automatically
......
...@@ -30,6 +30,7 @@ ...@@ -30,6 +30,7 @@
#include "testutil.hpp" #include "testutil.hpp"
#include "testutil_unity.hpp" #include "testutil_unity.hpp"
#include <string.h>
#include <unity.h> #include <unity.h>
void setUp () void setUp ()
......
...@@ -57,7 +57,7 @@ void test_version () ...@@ -57,7 +57,7 @@ void test_version ()
void test_strerrror () void test_strerrror ()
{ {
assert (zmq_strerror (EINVAL)); TEST_ASSERT_NOT_NULL (zmq_strerror (EINVAL));
} }
int main () int main ()
......
...@@ -31,6 +31,7 @@ ...@@ -31,6 +31,7 @@
#include "testutil_unity.hpp" #include "testutil_unity.hpp"
#include <unity.h> #include <unity.h>
#include <string.h>
void setUp () void setUp ()
{ {
......
...@@ -30,6 +30,8 @@ ...@@ -30,6 +30,8 @@
#include "testutil.hpp" #include "testutil.hpp"
#include "testutil_unity.hpp" #include "testutil_unity.hpp"
#include <string.h>
void setUp () void setUp ()
{ {
setup_test_context (); setup_test_context ();
......
...@@ -44,7 +44,8 @@ static void receiver (void *socket_) ...@@ -44,7 +44,8 @@ static void receiver (void *socket_)
{ {
char buffer[16]; char buffer[16];
int rc = zmq_recv (socket_, &buffer, sizeof (buffer), 0); int rc = zmq_recv (socket_, &buffer, sizeof (buffer), 0);
assert (rc == -1); // TODO which error is expected here? use TEST_ASSERT_FAILURE_ERRNO instead
TEST_ASSERT_EQUAL_INT (-1, rc);
} }
void test_ctx_destroy () void test_ctx_destroy ()
......
This diff is collapsed.
...@@ -30,6 +30,8 @@ ...@@ -30,6 +30,8 @@
#include "testutil.hpp" #include "testutil.hpp"
#include "testutil_unity.hpp" #include "testutil_unity.hpp"
#include <stdlib.h>
#include <string.h>
#include <unity.h> #include <unity.h>
void setUp () void setUp ()
......
...@@ -28,46 +28,43 @@ ...@@ -28,46 +28,43 @@
*/ */
#include "testutil.hpp" #include "testutil.hpp"
#include "testutil_unity.hpp"
#include <string.h>
void setUp ()
{
setup_test_context ();
}
void tearDown ()
{
teardown_test_context ();
}
/// Initialize a zeromq message with a given null-terminated string /// Initialize a zeromq message with a given null-terminated string
#define ZMQ_PREPARE_STRING(msg, data, size) \ #define ZMQ_PREPARE_STRING(msg, data, size) \
\ TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_init (&msg)); \
zmq_msg_init (&msg) \ TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_init_size (&msg, size + 1)); \
&& printf ("zmq_msg_init: %s\n", zmq_strerror (errno)); \
\
zmq_msg_init_size (&msg, size + 1) \
&& printf ("zmq_msg_init_size: %s\n", zmq_strerror (errno)); \
\
memcpy (zmq_msg_data (&msg), data, size + 1); memcpy (zmq_msg_data (&msg), data, size + 1);
// TODO: this code fails to meet our style guidelines, and needs rewriting
static int publicationsReceived = 0; static int publicationsReceived = 0;
static bool isSubscribed = false; static bool isSubscribed = false;
int main (int, char **) void test_disconnect_inproc ()
{ {
setup_test_environment (); void *pub_socket = test_context_socket (ZMQ_XPUB);
void *context = zmq_ctx_new (); void *sub_socket = test_context_socket (ZMQ_SUB);
void *pub_socket; TEST_ASSERT_SUCCESS_ERRNO (
void *sub_socket; zmq_setsockopt (sub_socket, ZMQ_SUBSCRIBE, "foo", 3));
(pub_socket = zmq_socket (context, ZMQ_XPUB))
|| printf ("zmq_socket: %s\n", zmq_strerror (errno));
(sub_socket = zmq_socket (context, ZMQ_SUB))
|| printf ("zmq_socket: %s\n", zmq_strerror (errno));
zmq_setsockopt (sub_socket, ZMQ_SUBSCRIBE, "foo", 3)
&& printf ("zmq_setsockopt: %s\n", zmq_strerror (errno));
zmq_bind (pub_socket, "inproc://someInProcDescriptor") TEST_ASSERT_SUCCESS_ERRNO (
&& printf ("zmq_bind: %s\n", zmq_strerror (errno)); zmq_bind (pub_socket, "inproc://someInProcDescriptor"));
//zmq_bind(pubSocket, "tcp://127.0.0.1:30010") && printf("zmq_bind: %s\n", zmq_strerror(errno));
int more; int more;
size_t more_size = sizeof (more); size_t more_size = sizeof (more);
int iteration = 0;
while (1) { for (int iteration = 0;; ++iteration) {
zmq_pollitem_t items[] = { zmq_pollitem_t items[] = {
{sub_socket, 0, ZMQ_POLLIN, 0}, // read publications {sub_socket, 0, ZMQ_POLLIN, 0}, // read publications
{pub_socket, 0, ZMQ_POLLIN, 0}, // read subscriptions {pub_socket, 0, ZMQ_POLLIN, 0}, // read subscriptions
...@@ -75,77 +72,73 @@ int main (int, char **) ...@@ -75,77 +72,73 @@ int main (int, char **)
int rc = zmq_poll (items, 2, 100); int rc = zmq_poll (items, 2, 100);
if (items[1].revents & ZMQ_POLLIN) { if (items[1].revents & ZMQ_POLLIN) {
while (1) { for (more = 1; more;) {
zmq_msg_t msg; zmq_msg_t msg;
zmq_msg_init (&msg); zmq_msg_init (&msg);
zmq_msg_recv (&msg, pub_socket, 0); TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_recv (&msg, pub_socket, 0));
char *buffer = (char *) zmq_msg_data (&msg); char *buffer = (char *) zmq_msg_data (&msg);
if (buffer[0] == 0) { if (buffer[0] == 0) {
assert (isSubscribed); TEST_ASSERT_TRUE (isSubscribed);
isSubscribed = false; isSubscribed = false;
} else { } else {
assert (!isSubscribed); TEST_ASSERT_FALSE (isSubscribed);
isSubscribed = true; isSubscribed = true;
} }
zmq_getsockopt (pub_socket, ZMQ_RCVMORE, &more, &more_size); TEST_ASSERT_SUCCESS_ERRNO (
zmq_msg_close (&msg); zmq_getsockopt (pub_socket, ZMQ_RCVMORE, &more, &more_size));
TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_close (&msg));
if (!more)
break; // Last message part
} }
} }
if (items[0].revents & ZMQ_POLLIN) { if (items[0].revents & ZMQ_POLLIN) {
while (1) { more = 1;
for (more = 1; more;) {
zmq_msg_t msg; zmq_msg_t msg;
zmq_msg_init (&msg); TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_init (&msg));
zmq_msg_recv (&msg, sub_socket, 0); TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_recv (&msg, sub_socket, 0));
zmq_getsockopt (sub_socket, ZMQ_RCVMORE, &more, &more_size); TEST_ASSERT_SUCCESS_ERRNO (
zmq_msg_close (&msg); zmq_getsockopt (sub_socket, ZMQ_RCVMORE, &more, &more_size));
TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_close (&msg));
if (!more) {
publicationsReceived++;
break; // Last message part
}
} }
publicationsReceived++;
} }
if (iteration == 1) { if (iteration == 1) {
zmq_connect (sub_socket, "inproc://someInProcDescriptor") TEST_ASSERT_SUCCESS_ERRNO (
&& printf ("zmq_connect: %s\n", zmq_strerror (errno)); zmq_connect (sub_socket, "inproc://someInProcDescriptor"));
msleep (SETTLE_TIME); msleep (SETTLE_TIME);
} }
if (iteration == 4) { if (iteration == 4) {
zmq_disconnect (sub_socket, "inproc://someInProcDescriptor") TEST_ASSERT_SUCCESS_ERRNO (
&& printf ("zmq_disconnect(%d): %s\n", errno, zmq_disconnect (sub_socket, "inproc://someInProcDescriptor"));
zmq_strerror (errno));
} }
if (iteration > 4 && rc == 0) if (iteration > 4 && rc == 0)
break; break;
zmq_msg_t channel_envlp; zmq_msg_t channel_envlp;
ZMQ_PREPARE_STRING (channel_envlp, "foo", 3); ZMQ_PREPARE_STRING (channel_envlp, "foo", 3);
zmq_msg_send (&channel_envlp, pub_socket, ZMQ_SNDMORE) >= 0 TEST_ASSERT_SUCCESS_ERRNO (
|| printf ("zmq_msg_send: %s\n", zmq_strerror (errno)); zmq_msg_send (&channel_envlp, pub_socket, ZMQ_SNDMORE));
zmq_msg_close (&channel_envlp) TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_close (&channel_envlp));
&& printf ("zmq_msg_close: %s\n", zmq_strerror (errno));
zmq_msg_t message; zmq_msg_t message;
ZMQ_PREPARE_STRING (message, "this is foo!", 12); ZMQ_PREPARE_STRING (message, "this is foo!", 12);
zmq_msg_send (&message, pub_socket, 0) >= 0 TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_send (&message, pub_socket, 0));
|| printf ("zmq_msg_send: %s\n", zmq_strerror (errno)); TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_close (&message));
zmq_msg_close (&message)
&& printf ("zmq_msg_close: %s\n", zmq_strerror (errno));
iteration++;
} }
assert (publicationsReceived == 3); TEST_ASSERT_EQUAL_INT (3, publicationsReceived);
assert (!isSubscribed); TEST_ASSERT_FALSE (isSubscribed);
zmq_close (pub_socket) && printf ("zmq_close: %s", zmq_strerror (errno)); test_context_socket_close (pub_socket);
zmq_close (sub_socket) && printf ("zmq_close: %s", zmq_strerror (errno)); test_context_socket_close (sub_socket);
}
int main (int, char **)
{
setup_test_environment ();
zmq_ctx_term (context); UNITY_BEGIN ();
return 0; RUN_TEST (test_disconnect_inproc);
return UNITY_END ();
} }
...@@ -30,6 +30,9 @@ ...@@ -30,6 +30,9 @@
#include "testutil.hpp" #include "testutil.hpp"
#include "testutil_unity.hpp" #include "testutil_unity.hpp"
#include <unistd.h>
#include <grp.h>
void setUp () void setUp ()
{ {
setup_test_context (); setup_test_context ();
...@@ -117,7 +120,7 @@ void init_groups () ...@@ -117,7 +120,7 @@ void init_groups ()
// Get the group and supplemental groups of the process owner // Get the group and supplemental groups of the process owner
gid_t groups[100]; gid_t groups[100];
int ngroups = getgroups (100, groups); int ngroups = getgroups (100, groups);
assert (ngroups != -1); TEST_ASSERT_NOT_EQUAL (-1, ngroups);
group = getgid (); group = getgid ();
supgroup = group; supgroup = group;
notgroup = group + 1; notgroup = group + 1;
......
...@@ -28,41 +28,50 @@ ...@@ -28,41 +28,50 @@
*/ */
#include "testutil.hpp" #include "testutil.hpp"
#include "testutil_unity.hpp"
#include <assert.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
void setUp ()
{
setup_test_context ();
}
void tearDown ()
{
teardown_test_context ();
}
const char *address = "tcp://127.0.0.1:*";
char connect_address[MAX_SOCKET_STRING]; char connect_address[MAX_SOCKET_STRING];
#define NUM_MESSAGES 5 #define NUM_MESSAGES 5
int main (void) void test_fork ()
{ {
#if !defined(ZMQ_HAVE_WINDOWS) #if !defined(ZMQ_HAVE_WINDOWS)
setup_test_environment ();
void *ctx = zmq_ctx_new ();
assert (ctx);
// Create and bind pull socket to receive messages // Create and bind pull socket to receive messages
void *pull = zmq_socket (ctx, ZMQ_PULL); void *pull = test_context_socket (ZMQ_PULL);
assert (pull); bind_loopback_ipv4 (pull, connect_address, sizeof connect_address);
int rc = zmq_bind (pull, address);
assert (rc == 0);
size_t len = MAX_SOCKET_STRING;
rc = zmq_getsockopt (pull, ZMQ_LAST_ENDPOINT, connect_address, &len);
assert (rc == 0);
int pid = fork (); int pid = fork ();
if (pid == 0) { if (pid == 0) {
// use regular assertions in the child process
// Child process // Child process
// Immediately close parent sockets and context // Immediately close parent sockets and context
zmq_close (pull); zmq_close (pull);
zmq_ctx_term (ctx); zmq_ctx_term (get_test_context ());
// Create new context, socket, connect and send some messages // Create new context, socket, connect and send some messages
void *child_ctx = zmq_ctx_new (); void *child_ctx = zmq_ctx_new ();
assert (child_ctx); assert (child_ctx);
void *push = zmq_socket (child_ctx, ZMQ_PUSH); void *push = zmq_socket (child_ctx, ZMQ_PUSH);
assert (push); assert (push);
rc = zmq_connect (push, connect_address); int rc = zmq_connect (push, connect_address);
assert (rc == 0); assert (rc == 0);
int count; int count;
for (count = 0; count < NUM_MESSAGES; count++) for (count = 0; count < NUM_MESSAGES; count++)
...@@ -75,24 +84,28 @@ int main (void) ...@@ -75,24 +84,28 @@ int main (void)
// Parent process // Parent process
int count; int count;
for (count = 0; count < NUM_MESSAGES; count++) { for (count = 0; count < NUM_MESSAGES; count++) {
char buffer[5]; recv_string_expect_success (pull, "Hello", 0);
int num_bytes = zmq_recv (pull, buffer, 5, 0);
assert (num_bytes == 5);
} }
int child_status; int child_status;
while (true) { while (true) {
rc = waitpid (pid, &child_status, 0); int rc = waitpid (pid, &child_status, 0);
if (rc == -1 && errno == EINTR) if (rc == -1 && errno == EINTR)
continue; continue;
assert (rc > 0); TEST_ASSERT_GREATER_THAN (0, rc);
// Verify the status code of the child was zero // Verify the status code of the child was zero
assert (WEXITSTATUS (child_status) == 0); TEST_ASSERT_EQUAL (0, WEXITSTATUS (child_status));
break; break;
} }
zmq_close (pull); test_context_socket_close (pull);
zmq_ctx_term (ctx);
exit (0);
} }
#endif #endif
return 0; }
int main (void)
{
setup_test_environment ();
UNITY_BEGIN ();
RUN_TEST (test_fork);
return UNITY_END ();
} }
...@@ -23,42 +23,46 @@ ...@@ -23,42 +23,46 @@
*/ */
#include "testutil.hpp" #include "testutil.hpp"
#include "testutil_unity.hpp"
int main (void) #include <string.h>
void setUp ()
{
setup_test_context ();
}
void tearDown ()
{
teardown_test_context ();
}
void test_getsockopt_memset ()
{ {
int64_t more; int64_t more;
size_t more_size = sizeof (more); size_t more_size = sizeof (more);
setup_test_environment (); void *sb = test_context_socket (ZMQ_PUB);
void *ctx = zmq_ctx_new (); TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (sb, "inproc://a"));
assert (ctx);
void *sb = zmq_socket (ctx, ZMQ_PUB);
assert (sb);
int rc = zmq_bind (sb, "inproc://a");
assert (rc == 0);
void *sc = zmq_socket (ctx, ZMQ_SUB); void *sc = test_context_socket (ZMQ_SUB);
assert (sc); TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (sc, "inproc://a"));
rc = zmq_connect (sc, "inproc://a");
assert (rc == 0);
memset (&more, 0xFF, sizeof (int64_t)); memset (&more, 0xFF, sizeof (int64_t));
zmq_getsockopt (sc, ZMQ_RCVMORE, &more, &more_size); TEST_ASSERT_SUCCESS_ERRNO (
assert (more_size == sizeof (int)); zmq_getsockopt (sc, ZMQ_RCVMORE, &more, &more_size));
assert (more == 0); TEST_ASSERT_EQUAL_INT (sizeof (int), more_size);
TEST_ASSERT_EQUAL_INT (0, more);
// Cleanup // Cleanup
test_context_socket_close (sc);
test_context_socket_close (sb);
}
rc = zmq_close (sc); int main (void)
assert (rc == 0); {
setup_test_environment ();
rc = zmq_close (sb);
assert (rc == 0);
rc = zmq_ctx_term (ctx);
assert (rc == 0);
return 0; UNITY_BEGIN ();
RUN_TEST (test_getsockopt_memset);
return UNITY_END ();
} }
...@@ -26,10 +26,13 @@ ...@@ -26,10 +26,13 @@
typedef SOCKET raw_socket; typedef SOCKET raw_socket;
#else #else
#include <arpa/inet.h> #include <arpa/inet.h>
#include <unistd.h>
typedef int raw_socket; typedef int raw_socket;
#endif #endif
#include <limits.h> #include <limits.h>
#include <stdlib.h>
#include <string.h>
// TODO remove this here, either ensure that UINT16_MAX is always properly // TODO remove this here, either ensure that UINT16_MAX is always properly
// defined or handle this at a more central location // defined or handle this at a more central location
......
...@@ -191,12 +191,8 @@ int test_inproc_connect_and_close_first (int send_hwm_, int recv_hwm_) ...@@ -191,12 +191,8 @@ int test_inproc_connect_and_close_first (int send_hwm_, int recv_hwm_)
int test_inproc_bind_and_close_first (int send_hwm_, int /* recv_hwm */) int test_inproc_bind_and_close_first (int send_hwm_, int /* recv_hwm */)
{ {
void *ctx = zmq_ctx_new ();
TEST_ASSERT_NOT_NULL (ctx);
// Set up bind socket // Set up bind socket
void *bind_socket = zmq_socket (ctx, ZMQ_PUSH); void *bind_socket = test_context_socket (ZMQ_PUSH);
TEST_ASSERT_NOT_NULL (bind_socket);
TEST_ASSERT_SUCCESS_ERRNO ( TEST_ASSERT_SUCCESS_ERRNO (
zmq_setsockopt (bind_socket, ZMQ_SNDHWM, &send_hwm_, sizeof (send_hwm_))); zmq_setsockopt (bind_socket, ZMQ_SNDHWM, &send_hwm_, sizeof (send_hwm_)));
TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (bind_socket, "inproc://a")); TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (bind_socket, "inproc://a"));
...@@ -208,12 +204,11 @@ int test_inproc_bind_and_close_first (int send_hwm_, int /* recv_hwm */) ...@@ -208,12 +204,11 @@ int test_inproc_bind_and_close_first (int send_hwm_, int /* recv_hwm */)
++send_count; ++send_count;
// Close bind // Close bind
TEST_ASSERT_SUCCESS_ERRNO (zmq_close (bind_socket)); test_context_socket_close (bind_socket);
/* TODO Can't currently do connect without then wiring up a bind as things hang, this needs top be fixed. /* TODO Can't currently do connect without then wiring up a bind as things hang, this needs top be fixed.
// Set up connect socket // Set up connect socket
void *connect_socket = zmq_socket (ctx, ZMQ_PULL); void *connect_socket = test_context_socket (ZMQ_PULL);
TEST_ASSERT_NOT_NULL(connect_socket);
TEST_ASSERT_SUCCESS_ERRNO (zmq_setsockopt (connect_socket, ZMQ_RCVHWM, &recv_hwm, sizeof (recv_hwm))); TEST_ASSERT_SUCCESS_ERRNO (zmq_setsockopt (connect_socket, ZMQ_RCVHWM, &recv_hwm, sizeof (recv_hwm)));
TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (connect_socket, "inproc://a")); TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (connect_socket, "inproc://a"));
...@@ -226,9 +221,7 @@ int test_inproc_bind_and_close_first (int send_hwm_, int /* recv_hwm */) ...@@ -226,9 +221,7 @@ int test_inproc_bind_and_close_first (int send_hwm_, int /* recv_hwm */)
*/ */
// Clean up // Clean up
//TEST_ASSERT_SUCCESS_ERRNO (zmq_close (connect_socket)); //test_context_socket_close (connect_socket);
TEST_ASSERT_SUCCESS_ERRNO (zmq_ctx_term (ctx));
return send_count; return send_count;
} }
......
...@@ -30,6 +30,8 @@ ...@@ -30,6 +30,8 @@
#include "testutil.hpp" #include "testutil.hpp"
#include "testutil_unity.hpp" #include "testutil_unity.hpp"
#include <string.h>
// NOTE: on OSX the endpoint returned by ZMQ_LAST_ENDPOINT may be quite long, // NOTE: on OSX the endpoint returned by ZMQ_LAST_ENDPOINT may be quite long,
// ensure we have extra space for that: // ensure we have extra space for that:
#define SOCKET_STRING_LEN (MAX_SOCKET_STRING * 4) #define SOCKET_STRING_LEN (MAX_SOCKET_STRING * 4)
......
...@@ -30,6 +30,8 @@ ...@@ -30,6 +30,8 @@
#include "testutil.hpp" #include "testutil.hpp"
#include "testutil_unity.hpp" #include "testutil_unity.hpp"
#include <stdlib.h>
#include <string.h>
#include <unity.h> #include <unity.h>
void setUp () void setUp ()
...@@ -55,7 +57,9 @@ struct iovec ...@@ -55,7 +57,9 @@ struct iovec
static 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); TEST_ASSERT_NOT_NULL (sb_);
TEST_ASSERT_NOT_NULL (sc_);
TEST_ASSERT_GREATER_THAN (0, msg_size_);
const char msg_val = '1'; const char msg_val = '1';
const int num_messages = 10; const int num_messages = 10;
...@@ -76,8 +80,8 @@ static void do_check (void *sb_, void *sc_, size_t msg_size_) ...@@ -76,8 +80,8 @@ static void do_check (void *sb_, void *sc_, size_t msg_size_)
send_iov[i].iov_len = msg_size_; send_iov[i].iov_len = msg_size_;
memcpy (send_iov[i].iov_base, ref_msg, msg_size_); memcpy (send_iov[i].iov_base, ref_msg, msg_size_);
// TODO: this assertion only checks if memcpy behaves as expected... remove this? // TODO: this assertion only checks if memcpy behaves as expected... remove this or assert something else?
assert (memcmp (ref_msg, send_iov[i].iov_base, msg_size_) == 0); TEST_ASSERT_EQUAL_HEX8_ARRAY (ref_msg, send_iov[i].iov_base, msg_size_);
} }
// Test errors - zmq_recviov - null socket // Test errors - zmq_recviov - null socket
......
...@@ -44,12 +44,12 @@ void tearDown () ...@@ -44,12 +44,12 @@ void tearDown ()
static void do_bind_and_verify (void *s_, const char *endpoint_) static void do_bind_and_verify (void *s_, const char *endpoint_)
{ {
int rc = zmq_bind (s_, endpoint_); TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (s_, endpoint_));
assert (rc == 0);
char reported[255]; char reported[255];
size_t size = 255; size_t size = 255;
rc = zmq_getsockopt (s_, ZMQ_LAST_ENDPOINT, reported, &size); TEST_ASSERT_SUCCESS_ERRNO (
assert (rc == 0 && strcmp (reported, endpoint_) == 0); zmq_getsockopt (s_, ZMQ_LAST_ENDPOINT, reported, &size));
TEST_ASSERT_EQUAL_STRING (endpoint_, reported);
} }
void test_last_endpoint () void test_last_endpoint ()
......
...@@ -28,75 +28,81 @@ ...@@ -28,75 +28,81 @@
*/ */
#include "testutil.hpp" #include "testutil.hpp"
#include <zmq.h> #include "testutil_unity.hpp"
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <vector> #include <vector>
void setUp ()
{
setup_test_context ();
}
void tearDown ()
{
teardown_test_context ();
}
void test_system_max () void test_system_max ()
{ {
// Keep allocating sockets until we run out of system resources // Keep allocating sockets until we run out of system resources
const int no_of_sockets = 2 * 65536; const int no_of_sockets = 2 * 65536;
void *ctx = zmq_ctx_new (); zmq_ctx_set (get_test_context (), ZMQ_MAX_SOCKETS, no_of_sockets);
zmq_ctx_set (ctx, ZMQ_MAX_SOCKETS, no_of_sockets);
std::vector<void *> sockets; std::vector<void *> sockets;
while (true) { while (true) {
void *socket = zmq_socket (ctx, ZMQ_PAIR); void *socket = zmq_socket (get_test_context (), ZMQ_PAIR);
if (!socket) if (!socket)
break; break;
sockets.push_back (socket); sockets.push_back (socket);
} }
assert (static_cast<int> (sockets.size ()) <= no_of_sockets); TEST_ASSERT_LESS_OR_EQUAL (no_of_sockets,
static_cast<int> (sockets.size ()));
printf ("Socket creation failed after %i sockets\n", printf ("Socket creation failed after %i sockets\n",
static_cast<int> (sockets.size ())); static_cast<int> (sockets.size ()));
// System is out of resources, further calls to zmq_socket should return NULL // System is out of resources, further calls to zmq_socket should return NULL
for (unsigned int i = 0; i < 10; ++i) { for (unsigned int i = 0; i < 10; ++i) {
void *socket = zmq_socket (ctx, ZMQ_PAIR); void *socket = zmq_socket (get_test_context (), ZMQ_PAIR);
assert (socket == NULL); TEST_ASSERT_NULL (socket);
} }
// Clean up. // Clean up.
for (unsigned int i = 0; i < sockets.size (); ++i) for (unsigned int i = 0; i < sockets.size (); ++i)
zmq_close (sockets[i]); TEST_ASSERT_SUCCESS_ERRNO (zmq_close (sockets[i]));
zmq_ctx_destroy (ctx);
} }
void test_zmq_default_max () void test_zmq_default_max ()
{ {
// Keep allocating sockets until we hit the default limit // Keep allocating sockets until we hit the default limit
void *ctx = zmq_ctx_new ();
std::vector<void *> sockets; std::vector<void *> sockets;
while (true) { while (true) {
void *socket = zmq_socket (ctx, ZMQ_PAIR); void *socket = zmq_socket (get_test_context (), ZMQ_PAIR);
if (!socket) if (!socket)
break; break;
sockets.push_back (socket); sockets.push_back (socket);
} }
// We may stop sooner if system has fewer available sockets // We may stop sooner if system has fewer available sockets
assert (sockets.size () <= ZMQ_MAX_SOCKETS_DFLT); TEST_ASSERT_LESS_OR_EQUAL (ZMQ_MAX_SOCKETS_DFLT, sockets.size ());
// Further calls to zmq_socket should return NULL // Further calls to zmq_socket should return NULL
for (unsigned int i = 0; i < 10; ++i) { for (unsigned int i = 0; i < 10; ++i) {
void *socket = zmq_socket (ctx, ZMQ_PAIR); void *socket = zmq_socket (get_test_context (), ZMQ_PAIR);
assert (socket == NULL); TEST_ASSERT_NULL (socket);
} }
// Clean up // Clean up
for (unsigned int i = 0; i < sockets.size (); ++i) for (unsigned int i = 0; i < sockets.size (); ++i)
zmq_close (sockets[i]); TEST_ASSERT_SUCCESS_ERRNO (zmq_close (sockets[i]));
zmq_ctx_destroy (ctx);
} }
int main (void) int main (void)
{ {
setup_test_environment (); setup_test_environment ();
test_system_max (); UNITY_BEGIN ();
test_zmq_default_max (); RUN_TEST (test_system_max);
RUN_TEST (test_zmq_default_max);
return 0; return UNITY_END ();
} }
...@@ -30,6 +30,7 @@ ...@@ -30,6 +30,7 @@
#include "testutil.hpp" #include "testutil.hpp"
#include "testutil_unity.hpp" #include "testutil_unity.hpp"
#include <stdlib.h>
#include <unity.h> #include <unity.h>
void setUp () void setUp ()
...@@ -57,8 +58,8 @@ static void zap_handler (void *handler_) ...@@ -57,8 +58,8 @@ static void zap_handler (void *handler_)
char *routing_id = s_recv (handler_); char *routing_id = s_recv (handler_);
char *mechanism = s_recv (handler_); char *mechanism = s_recv (handler_);
assert (streq (version, "1.0")); TEST_ASSERT_EQUAL_STRING ("1.0", version);
assert (streq (mechanism, "NULL")); TEST_ASSERT_EQUAL_STRING ("NULL", mechanism);
s_sendmore (handler_, version); s_sendmore (handler_, version);
s_sendmore (handler_, sequence); s_sendmore (handler_, sequence);
......
...@@ -27,9 +27,12 @@ ...@@ -27,9 +27,12 @@
typedef SOCKET raw_socket; typedef SOCKET raw_socket;
#else #else
#include <arpa/inet.h> #include <arpa/inet.h>
#include <unistd.h>
typedef int raw_socket; typedef int raw_socket;
#endif #endif
#include <stdlib.h>
#include <string.h>
void setUp () void setUp ()
{ {
......
...@@ -32,6 +32,9 @@ ...@@ -32,6 +32,9 @@
#include "testutil_unity.hpp" #include "testutil_unity.hpp"
#include <stdlib.h>
#include <string.h>
void setUp () void setUp ()
{ {
setup_test_context (); setup_test_context ();
...@@ -99,7 +102,7 @@ void test_monitor_basic () ...@@ -99,7 +102,7 @@ void test_monitor_basic ()
int event = get_monitor_event (client_mon, NULL, NULL); int event = get_monitor_event (client_mon, NULL, NULL);
if (event == ZMQ_EVENT_CONNECT_DELAYED) if (event == ZMQ_EVENT_CONNECT_DELAYED)
event = get_monitor_event (client_mon, NULL, NULL); event = get_monitor_event (client_mon, NULL, NULL);
assert (event == ZMQ_EVENT_CONNECTED); TEST_ASSERT_EQUAL_INT (ZMQ_EVENT_CONNECTED, event);
expect_monitor_event (client_mon, ZMQ_EVENT_HANDSHAKE_SUCCEEDED); expect_monitor_event (client_mon, ZMQ_EVENT_HANDSHAKE_SUCCEEDED);
event = get_monitor_event (client_mon, NULL, NULL); event = get_monitor_event (client_mon, NULL, NULL);
if (event == ZMQ_EVENT_DISCONNECTED) { if (event == ZMQ_EVENT_DISCONNECTED) {
......
...@@ -28,6 +28,19 @@ ...@@ -28,6 +28,19 @@
*/ */
#include "testutil.hpp" #include "testutil.hpp"
#include "testutil_unity.hpp"
#include <string.h>
void setUp ()
{
setup_test_context ();
}
void tearDown ()
{
teardown_test_context ();
}
void ffn (void *data_, void *hint_) void ffn (void *data_, void *hint_)
{ {
...@@ -36,29 +49,16 @@ void ffn (void *data_, void *hint_) ...@@ -36,29 +49,16 @@ void ffn (void *data_, void *hint_)
memcpy (hint_, (void *) "freed", 5); memcpy (hint_, (void *) "freed", 5);
} }
int main (void) void test_msg_ffn ()
{ {
setup_test_environment ();
// Create the infrastructure // Create the infrastructure
void *ctx = zmq_ctx_new ();
assert (ctx);
size_t len = MAX_SOCKET_STRING;
char my_endpoint[MAX_SOCKET_STRING]; char my_endpoint[MAX_SOCKET_STRING];
void *router = zmq_socket (ctx, ZMQ_ROUTER); void *router = test_context_socket (ZMQ_ROUTER);
assert (router); bind_loopback_ipv4 (router, my_endpoint, sizeof my_endpoint);
int rc = zmq_bind (router, "tcp://127.0.0.1:*");
assert (rc == 0);
rc = zmq_getsockopt (router, ZMQ_LAST_ENDPOINT, my_endpoint, &len);
assert (rc == 0);
void *dealer = zmq_socket (ctx, ZMQ_DEALER); void *dealer = test_context_socket (ZMQ_DEALER);
assert (dealer); TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (dealer, my_endpoint));
rc = zmq_connect (dealer, my_endpoint);
assert (rc == 0);
// Test that creating and closing a message triggers ffn // Test that creating and closing a message triggers ffn
zmq_msg_t msg; zmq_msg_t msg;
...@@ -67,80 +67,68 @@ int main (void) ...@@ -67,80 +67,68 @@ int main (void)
memset (data, 0, 255); memset (data, 0, 255);
memcpy (data, (void *) "data", 4); memcpy (data, (void *) "data", 4);
memcpy (hint, (void *) "hint", 4); memcpy (hint, (void *) "hint", 4);
rc = zmq_msg_init_data (&msg, (void *) data, 255, ffn, (void *) hint); TEST_ASSERT_SUCCESS_ERRNO (
assert (rc == 0); zmq_msg_init_data (&msg, (void *) data, 255, ffn, (void *) hint));
rc = zmq_msg_close (&msg); TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_close (&msg));
assert (rc == 0);
msleep (SETTLE_TIME); msleep (SETTLE_TIME);
assert (memcmp (hint, "freed", 5) == 0); TEST_ASSERT_EQUAL_STRING_LEN ("freed", hint, 5);
memcpy (hint, (void *) "hint", 4); memcpy (hint, (void *) "hint", 4);
// Making and closing a copy triggers ffn // Making and closing a copy triggers ffn
zmq_msg_t msg2; zmq_msg_t msg2;
zmq_msg_init (&msg2); zmq_msg_init (&msg2);
rc = zmq_msg_init_data (&msg, (void *) data, 255, ffn, (void *) hint); TEST_ASSERT_SUCCESS_ERRNO (
assert (rc == 0); zmq_msg_init_data (&msg, (void *) data, 255, ffn, (void *) hint));
rc = zmq_msg_copy (&msg2, &msg); TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_copy (&msg2, &msg));
assert (rc == 0); TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_close (&msg2));
rc = zmq_msg_close (&msg2); TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_close (&msg));
assert (rc == 0);
rc = zmq_msg_close (&msg);
assert (rc == 0);
msleep (SETTLE_TIME); msleep (SETTLE_TIME);
assert (memcmp (hint, "freed", 5) == 0); TEST_ASSERT_EQUAL_STRING_LEN ("freed", hint, 5);
memcpy (hint, (void *) "hint", 4); memcpy (hint, (void *) "hint", 4);
// Test that sending a message triggers ffn // Test that sending a message triggers ffn
rc = zmq_msg_init_data (&msg, (void *) data, 255, ffn, (void *) hint); TEST_ASSERT_SUCCESS_ERRNO (
assert (rc == 0); zmq_msg_init_data (&msg, (void *) data, 255, ffn, (void *) hint));
zmq_msg_send (&msg, dealer, 0); zmq_msg_send (&msg, dealer, 0);
char buf[255]; char buf[255];
rc = zmq_recv (router, buf, 255, 0); TEST_ASSERT_SUCCESS_ERRNO (zmq_recv (router, buf, 255, 0));
assert (rc > -1); TEST_ASSERT_EQUAL_INT (255, zmq_recv (router, buf, 255, 0));
rc = zmq_recv (router, buf, 255, 0); TEST_ASSERT_EQUAL_STRING_LEN (data, buf, 4);
assert (rc == 255);
assert (memcmp (data, buf, 4) == 0);
msleep (SETTLE_TIME); msleep (SETTLE_TIME);
assert (memcmp (hint, "freed", 5) == 0); TEST_ASSERT_EQUAL_STRING_LEN ("freed", hint, 5);
memcpy (hint, (void *) "hint", 4); memcpy (hint, (void *) "hint", 4);
rc = zmq_msg_close (&msg); TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_close (&msg));
assert (rc == 0);
// Sending a copy of a message triggers ffn // Sending a copy of a message triggers ffn
rc = zmq_msg_init (&msg2); TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_init (&msg2));
assert (rc == 0); TEST_ASSERT_SUCCESS_ERRNO (
rc = zmq_msg_init_data (&msg, (void *) data, 255, ffn, (void *) hint); zmq_msg_init_data (&msg, (void *) data, 255, ffn, (void *) hint));
assert (rc == 0); TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_copy (&msg2, &msg));
rc = zmq_msg_copy (&msg2, &msg);
assert (rc == 0);
zmq_msg_send (&msg, dealer, 0); zmq_msg_send (&msg, dealer, 0);
rc = zmq_recv (router, buf, 255, 0); TEST_ASSERT_SUCCESS_ERRNO (zmq_recv (router, buf, 255, 0));
assert (rc > -1); TEST_ASSERT_EQUAL_INT (255, zmq_recv (router, buf, 255, 0));
rc = zmq_recv (router, buf, 255, 0); TEST_ASSERT_EQUAL_STRING_LEN (data, buf, 4);
assert (rc == 255); TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_close (&msg2));
assert (memcmp (data, buf, 4) == 0); TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_close (&msg));
rc = zmq_msg_close (&msg2);
assert (rc == 0);
rc = zmq_msg_close (&msg);
assert (rc == 0);
msleep (SETTLE_TIME); msleep (SETTLE_TIME);
assert (memcmp (hint, "freed", 5) == 0); TEST_ASSERT_EQUAL_STRING_LEN ("freed", hint, 5);
memcpy (hint, (void *) "hint", 4);
// Deallocate the infrastructure. // Deallocate the infrastructure.
rc = zmq_close (router); test_context_socket_close (router);
assert (rc == 0); test_context_socket_close (dealer);
}
rc = zmq_close (dealer); int main (void)
assert (rc == 0); {
setup_test_environment ();
rc = zmq_ctx_term (ctx); UNITY_BEGIN ();
assert (rc == 0); RUN_TEST (test_msg_ffn);
return 0; return UNITY_END ();
} }
...@@ -28,99 +28,98 @@ ...@@ -28,99 +28,98 @@
*/ */
#include "testutil.hpp" #include "testutil.hpp"
#include "testutil_unity.hpp"
int main (void) void setUp ()
{ {
setup_test_environment (); setup_test_context ();
// Create the infrastructure }
void *ctx = zmq_ctx_new ();
assert (ctx);
void *sb = zmq_socket (ctx, ZMQ_ROUTER);
assert (sb);
int rc = zmq_bind (sb, "inproc://a"); void tearDown ()
assert (rc == 0); {
teardown_test_context ();
}
void *sc = zmq_socket (ctx, ZMQ_DEALER); void test_more ()
assert (sc); {
// Create the infrastructure
void *sb = test_context_socket (ZMQ_ROUTER);
TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (sb, "inproc://a"));
rc = zmq_connect (sc, "inproc://a"); void *sc = test_context_socket (ZMQ_DEALER);
assert (rc == 0); TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (sc, "inproc://a"));
// Send 2-part message. // Send 2-part message.
rc = zmq_send (sc, "A", 1, ZMQ_SNDMORE); send_string_expect_success (sc, "A", ZMQ_SNDMORE);
assert (rc == 1); send_string_expect_success (sc, "B", 0);
rc = zmq_send (sc, "B", 1, 0);
assert (rc == 1);
// Routing id comes first. // Routing id comes first.
zmq_msg_t msg; zmq_msg_t msg;
rc = zmq_msg_init (&msg); TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_init (&msg));
assert (rc == 0); TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_recv (&msg, sb, 0));
rc = zmq_msg_recv (&msg, sb, 0); TEST_ASSERT_EQUAL_INT (1, TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_more (&msg)));
assert (rc >= 0);
int more = zmq_msg_more (&msg);
assert (more == 1);
// Then the first part of the message body. // Then the first part of the message body.
rc = zmq_msg_recv (&msg, sb, 0); TEST_ASSERT_EQUAL_INT (
assert (rc == 1); 1, TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_recv (&msg, sb, 0)));
more = zmq_msg_more (&msg); TEST_ASSERT_EQUAL_INT (1, TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_more (&msg)));
assert (more == 1);
// And finally, the second part of the message body. // And finally, the second part of the message body.
rc = zmq_msg_recv (&msg, sb, 0); TEST_ASSERT_EQUAL_INT (
assert (rc == 1); 1, TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_recv (&msg, sb, 0)));
more = zmq_msg_more (&msg); TEST_ASSERT_EQUAL_INT (0, TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_more (&msg)));
assert (more == 0);
// Deallocate the infrastructure.
test_context_socket_close (sc);
test_context_socket_close (sb);
}
void test_shared_refcounted ()
{
// Test ZMQ_SHARED property (case 1, refcounted messages) // Test ZMQ_SHARED property (case 1, refcounted messages)
zmq_msg_t msg_a; zmq_msg_t msg_a;
rc = zmq_msg_init_size (&msg_a, 1024); // large enough to be a type_lmsg TEST_ASSERT_SUCCESS_ERRNO (
assert (rc == 0); zmq_msg_init_size (&msg_a, 1024)); // large enough to be a type_lmsg
// Message is not shared // Message is not shared
rc = zmq_msg_get (&msg_a, ZMQ_SHARED); TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_get (&msg_a, ZMQ_SHARED));
assert (rc == 0);
zmq_msg_t msg_b; zmq_msg_t msg_b;
rc = zmq_msg_init (&msg_b); TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_init (&msg_b));
assert (rc == 0);
rc = zmq_msg_copy (&msg_b, &msg_a); TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_copy (&msg_b, &msg_a));
assert (rc == 0);
// Message is now shared // Message is now shared
rc = zmq_msg_get (&msg_b, ZMQ_SHARED); TEST_ASSERT_EQUAL_INT (
assert (rc == 1); 1, TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_get (&msg_b, ZMQ_SHARED)));
// cleanup // cleanup
rc = zmq_msg_close (&msg_a); TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_close (&msg_a));
assert (rc == 0); TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_close (&msg_b));
rc = zmq_msg_close (&msg_b); }
assert (rc == 0);
void test_shared_const ()
{
zmq_msg_t msg_a;
// Test ZMQ_SHARED property (case 2, constant data messages) // Test ZMQ_SHARED property (case 2, constant data messages)
rc = zmq_msg_init_data (&msg_a, (void *) "TEST", 5, 0, 0); TEST_ASSERT_SUCCESS_ERRNO (
assert (rc == 0); zmq_msg_init_data (&msg_a, (void *) "TEST", 5, 0, 0));
// Message reports as shared // Message reports as shared
rc = zmq_msg_get (&msg_a, ZMQ_SHARED); TEST_ASSERT_EQUAL_INT (
assert (rc == 1); 1, TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_get (&msg_a, ZMQ_SHARED)));
// cleanup // cleanup
rc = zmq_msg_close (&msg_a); TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_close (&msg_a));
assert (rc == 0); }
// Deallocate the infrastructure.
rc = zmq_close (sc);
assert (rc == 0);
rc = zmq_close (sb); int main ()
assert (rc == 0); {
setup_test_environment ();
rc = zmq_ctx_term (ctx); UNITY_BEGIN ();
assert (rc == 0); RUN_TEST (test_more);
return 0; RUN_TEST (test_shared_refcounted);
RUN_TEST (test_shared_const);
return UNITY_END ();
} }
...@@ -30,6 +30,8 @@ ...@@ -30,6 +30,8 @@
#include "testutil.hpp" #include "testutil.hpp"
#include "testutil_unity.hpp" #include "testutil_unity.hpp"
#include <string.h>
void setUp () void setUp ()
{ {
setup_test_context (); setup_test_context ();
......
...@@ -45,9 +45,8 @@ typedef void (*extra_func_t) (void *socket_); ...@@ -45,9 +45,8 @@ typedef void (*extra_func_t) (void *socket_);
void set_sockopt_bind_to_device (void *socket) void set_sockopt_bind_to_device (void *socket)
{ {
const char device[] = "lo"; const char device[] = "lo";
int rc = TEST_ASSERT_SUCCESS_ERRNO (
zmq_setsockopt (socket, ZMQ_BINDTODEVICE, &device, sizeof (device) - 1); zmq_setsockopt (socket, ZMQ_BINDTODEVICE, &device, sizeof (device) - 1));
assert (rc == 0);
} }
// TODO this is duplicated from test_pair_tcp // TODO this is duplicated from test_pair_tcp
......
...@@ -32,37 +32,41 @@ ...@@ -32,37 +32,41 @@
#include <vmci_sockets.h> #include <vmci_sockets.h>
#include "testutil.hpp" #include "testutil.hpp"
#include "testutil_unity.hpp"
int main (void) void setUp ()
{ {
setup_test_environment (); setup_test_context ();
void *ctx = zmq_ctx_new (); }
assert (ctx);
void tearDown ()
{
teardown_test_context ();
}
void test_pair_vmci ()
{
std::stringstream s; std::stringstream s;
s << "vmci://" << VMCISock_GetLocalCID () << ":" << 5560; s << "vmci://" << VMCISock_GetLocalCID () << ":" << 5560;
std::string endpoint = s.str (); std::string endpoint = s.str ();
void *sb = zmq_socket (ctx, ZMQ_PAIR); void *sb = test_context_socket (ZMQ_PAIR);
assert (sb); TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (sb, endpoint.c_str ()));
int rc = zmq_bind (sb, endpoint.c_str ());
assert (rc == 0);
void *sc = zmq_socket (ctx, ZMQ_PAIR); void *sc = test_context_socket (ZMQ_PAIR);
assert (sc); TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (sc, endpoint.c_str ()));
rc = zmq_connect (sc, endpoint.c_str ());
assert (rc == 0);
bounce (sb, sc); bounce (sb, sc);
rc = zmq_close (sc); test_context_socket_close (sc);
assert (rc == 0); test_context_socket_close (sb);
}
rc = zmq_close (sb);
assert (rc == 0);
rc = zmq_ctx_term (ctx); int main (void)
assert (rc == 0); {
setup_test_environment ();
return 0; UNITY_BEGIN ();
RUN_TEST (test_pair_vmci);
return UNITY_END ();
} }
...@@ -33,6 +33,13 @@ ...@@ -33,6 +33,13 @@
#include <unity.h> #include <unity.h>
#include <limits.h> #include <limits.h>
#ifndef _WIN32
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#endif
void setUp () void setUp ()
{ {
setup_test_context (); setup_test_context ();
......
This diff is collapsed.
...@@ -29,7 +29,10 @@ ...@@ -29,7 +29,10 @@
#include "testutil.hpp" #include "testutil.hpp"
#include "testutil_unity.hpp" #include "testutil_unity.hpp"
#include <string.h>
#include <unity.h> #include <unity.h>
#include <assert.h>
#include <unistd.h>
// //
// Asynchronous proxy test using ZMQ_XPUB_NODROP and HWM: // Asynchronous proxy test using ZMQ_XPUB_NODROP and HWM:
......
...@@ -28,92 +28,83 @@ ...@@ -28,92 +28,83 @@
*/ */
#include "testutil.hpp" #include "testutil.hpp"
#include "testutil_unity.hpp"
#include <stdlib.h>
void setUp ()
{
setup_test_context ();
}
void tearDown ()
{
teardown_test_context ();
}
// This is our server task. // This is our server task.
// It runs a proxy with a single REP socket as both frontend and backend. // It runs a proxy with a single REP socket as both frontend and backend.
void server_task (void *ctx_) void server_task (void * /*unused_*/)
{ {
size_t len = MAX_SOCKET_STRING;
char my_endpoint[MAX_SOCKET_STRING]; char my_endpoint[MAX_SOCKET_STRING];
void *rep = zmq_socket (ctx_, ZMQ_REP); void *rep = zmq_socket (get_test_context (), ZMQ_REP);
assert (rep); TEST_ASSERT_NOT_NULL (rep);
int rc = zmq_bind (rep, "tcp://127.0.0.1:*"); bind_loopback_ipv4 (rep, my_endpoint, sizeof my_endpoint);
assert (rc == 0);
rc = zmq_getsockopt (rep, ZMQ_LAST_ENDPOINT, my_endpoint, &len);
assert (rc == 0);
// Control socket receives terminate command from main over inproc // Control socket receives terminate command from main over inproc
void *control = zmq_socket (ctx_, ZMQ_REQ); void *control = zmq_socket (get_test_context (), ZMQ_REQ);
assert (control); TEST_ASSERT_NOT_NULL (control);
rc = zmq_connect (control, "inproc://control"); TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (control, "inproc://control"));
assert (rc == 0); TEST_ASSERT_GREATER_THAN_INT (
rc = s_send (control, my_endpoint); 0, TEST_ASSERT_SUCCESS_ERRNO (s_send (control, my_endpoint)));
assert (rc > 0);
// Use rep as both frontend and backend // Use rep as both frontend and backend
rc = zmq_proxy_steerable (rep, rep, NULL, control); TEST_ASSERT_SUCCESS_ERRNO (zmq_proxy_steerable (rep, rep, NULL, control));
assert (rc == 0);
rc = zmq_close (rep); TEST_ASSERT_SUCCESS_ERRNO (zmq_close (rep));
assert (rc == 0); TEST_ASSERT_SUCCESS_ERRNO (zmq_close (control));
rc = zmq_close (control);
assert (rc == 0);
} }
// The main thread simply starts several clients and a server, and then // The main thread simply starts several clients and a server, and then
// waits for the server to finish. // waits for the server to finish.
void test_proxy_single_socket ()
int main (void)
{ {
setup_test_environment (); void *server_thread = zmq_threadstart (&server_task, NULL);
void *ctx = zmq_ctx_new ();
assert (ctx);
void *server_thread = zmq_threadstart (&server_task, ctx);
// Control socket receives terminate command from main over inproc // Control socket receives terminate command from main over inproc
void *control = zmq_socket (ctx, ZMQ_REP); void *control = test_context_socket (ZMQ_REP);
assert (control); TEST_ASSERT_NOT_NULL (control);
int rc = zmq_bind (control, "inproc://control"); TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (control, "inproc://control"));
assert (rc == 0);
char *my_endpoint = s_recv (control); char *my_endpoint = s_recv (control);
assert (my_endpoint); TEST_ASSERT_NOT_NULL (my_endpoint);
// client socket pings proxy over tcp // client socket pings proxy over tcp
void *req = zmq_socket (ctx, ZMQ_REQ); void *req = test_context_socket (ZMQ_REQ);
assert (req); TEST_ASSERT_NOT_NULL (req);
rc = zmq_connect (req, my_endpoint); TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (req, my_endpoint));
assert (rc == 0);
send_string_expect_success (req, "msg1", 0);
char buf[255]; recv_string_expect_success (req, "msg1", 0);
rc = zmq_send (req, "msg1", 4, 0);
assert (rc == 4); send_string_expect_success (req, "msg22", 0);
rc = zmq_recv (req, buf, 255, 0); recv_string_expect_success (req, "msg22", 0);
assert (rc == 4);
assert (memcmp (buf, "msg1", 4) == 0); send_string_expect_success (control, "TERMINATE", 0);
rc = zmq_send (req, "msg22", 5, 0); test_context_socket_close (control);
assert (rc == 5); test_context_socket_close (req);
rc = zmq_recv (req, buf, 255, 0);
assert (rc == 5);
assert (memcmp (buf, "msg22", 5) == 0);
rc = zmq_send (control, "TERMINATE", 9, 0);
assert (rc == 9);
rc = zmq_close (control);
assert (rc == 0);
rc = zmq_close (req);
assert (rc == 0);
free (my_endpoint); free (my_endpoint);
zmq_threadclose (server_thread); zmq_threadclose (server_thread);
}
int main (void)
{
setup_test_environment ();
rc = zmq_ctx_term (ctx); UNITY_BEGIN ();
assert (rc == 0); RUN_TEST (test_proxy_single_socket);
return 0; return UNITY_END ();
} }
...@@ -28,104 +28,98 @@ ...@@ -28,104 +28,98 @@
*/ */
#include "testutil.hpp" #include "testutil.hpp"
#include "testutil_unity.hpp"
#include <stdlib.h>
void setUp ()
{
setup_test_context ();
}
void tearDown ()
{
teardown_test_context ();
}
// This is a test for issue #1382. The server thread creates a SUB-PUSH // This is a test for issue #1382. The server thread creates a SUB-PUSH
// steerable proxy. The main process then sends messages to the SUB // steerable proxy. The main process then sends messages to the SUB
// but there is no pull on the other side, previously the proxy blocks // but there is no pull on the other side, previously the proxy blocks
// in writing to the backend, preventing the proxy from terminating // in writing to the backend, preventing the proxy from terminating
void server_task (void *ctx_) void server_task (void * /*unused_*/)
{ {
size_t len = MAX_SOCKET_STRING;
char my_endpoint[MAX_SOCKET_STRING]; char my_endpoint[MAX_SOCKET_STRING];
// Frontend socket talks to main process // Frontend socket talks to main process
void *frontend = zmq_socket (ctx_, ZMQ_SUB); void *frontend = zmq_socket (get_test_context (), ZMQ_SUB);
assert (frontend); TEST_ASSERT_NOT_NULL (frontend);
int rc = zmq_setsockopt (frontend, ZMQ_SUBSCRIBE, "", 0); TEST_ASSERT_SUCCESS_ERRNO (zmq_setsockopt (frontend, ZMQ_SUBSCRIBE, "", 0));
assert (rc == 0); bind_loopback_ipv4 (frontend, my_endpoint, sizeof my_endpoint);
rc = zmq_bind (frontend, "tcp://127.0.0.1:*");
assert (rc == 0);
rc = zmq_getsockopt (frontend, ZMQ_LAST_ENDPOINT, my_endpoint, &len);
assert (rc == 0);
// Nice socket which is never read // Nice socket which is never read
void *backend = zmq_socket (ctx_, ZMQ_PUSH); void *backend = zmq_socket (get_test_context (), ZMQ_PUSH);
assert (backend); TEST_ASSERT_NOT_NULL (backend);
rc = zmq_bind (backend, "tcp://127.0.0.1:*"); TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (backend, "tcp://127.0.0.1:*"));
assert (rc == 0);
// Control socket receives terminate command from main over inproc // Control socket receives terminate command from main over inproc
void *control = zmq_socket (ctx_, ZMQ_REQ); void *control = zmq_socket (get_test_context (), ZMQ_REQ);
assert (control); TEST_ASSERT_NOT_NULL (control);
rc = zmq_connect (control, "inproc://control"); TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (control, "inproc://control"));
assert (rc == 0); TEST_ASSERT_GREATER_THAN_INT (
rc = s_send (control, my_endpoint); 0, TEST_ASSERT_SUCCESS_ERRNO (s_send (control, my_endpoint)));
assert (rc > 0);
// Connect backend to frontend via a proxy // Connect backend to frontend via a proxy
rc = zmq_proxy_steerable (frontend, backend, NULL, control); TEST_ASSERT_SUCCESS_ERRNO (
assert (rc == 0); zmq_proxy_steerable (frontend, backend, NULL, control));
rc = zmq_close (frontend); TEST_ASSERT_SUCCESS_ERRNO (zmq_close (frontend));
assert (rc == 0); TEST_ASSERT_SUCCESS_ERRNO (zmq_close (backend));
rc = zmq_close (backend); TEST_ASSERT_SUCCESS_ERRNO (zmq_close (control));
assert (rc == 0);
rc = zmq_close (control);
assert (rc == 0);
} }
// The main thread simply starts a basic steerable proxy server, publishes some messages, and then // The main thread simply starts a basic steerable proxy server, publishes some messages, and then
// waits for the server to terminate. // waits for the server to terminate.
void test_proxy_terminate ()
int main (void)
{ {
setup_test_environment (); void *thread = zmq_threadstart (&server_task, NULL);
void *ctx = zmq_ctx_new ();
assert (ctx);
void *thread = zmq_threadstart (&server_task, ctx);
// Control socket receives terminate command from main over inproc // Control socket receives terminate command from main over inproc
void *control = zmq_socket (ctx, ZMQ_REP); void *control = test_context_socket (ZMQ_REP);
assert (control); TEST_ASSERT_NOT_NULL (control);
int rc = zmq_bind (control, "inproc://control"); TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (control, "inproc://control"));
assert (rc == 0);
char *my_endpoint = s_recv (control); char *my_endpoint = s_recv (control);
assert (my_endpoint); TEST_ASSERT_NOT_NULL (my_endpoint);
msleep (500); // Run for 500 ms msleep (500); // Run for 500 ms
// Start a secondary publisher which writes data to the SUB-PUSH server socket // Start a secondary publisher which writes data to the SUB-PUSH server socket
void *publisher = zmq_socket (ctx, ZMQ_PUB); void *publisher = test_context_socket (ZMQ_PUB);
assert (publisher); TEST_ASSERT_NOT_NULL (publisher);
rc = zmq_connect (publisher, my_endpoint); TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (publisher, my_endpoint));
assert (rc == 0);
msleep (SETTLE_TIME); msleep (SETTLE_TIME);
rc = zmq_send (publisher, "This is a test", 14, 0); send_string_expect_success (publisher, "This is a test", 0);
assert (rc == 14);
msleep (50); msleep (50);
rc = zmq_send (publisher, "This is a test", 14, 0); send_string_expect_success (publisher, "This is a test", 0);
assert (rc == 14);
msleep (50); msleep (50);
rc = zmq_send (publisher, "This is a test", 14, 0); send_string_expect_success (publisher, "This is a test", 0);
assert (rc == 14); send_string_expect_success (control, "TERMINATE", 0);
rc = zmq_send (control, "TERMINATE", 9, 0);
assert (rc == 9); test_context_socket_close (publisher);
test_context_socket_close (control);
rc = zmq_close (publisher);
assert (rc == 0);
rc = zmq_close (control);
assert (rc == 0);
free (my_endpoint); free (my_endpoint);
zmq_threadclose (thread); zmq_threadclose (thread);
}
int main (void)
{
setup_test_environment ();
rc = zmq_ctx_term (ctx); UNITY_BEGIN ();
assert (rc == 0); RUN_TEST (test_proxy_terminate);
return 0; return UNITY_END ();
} }
...@@ -30,6 +30,8 @@ ...@@ -30,6 +30,8 @@
#include "testutil.hpp" #include "testutil.hpp"
#include "testutil_unity.hpp" #include "testutil_unity.hpp"
#include <string.h>
void setUp () void setUp ()
{ {
setup_test_context (); setup_test_context ();
......
...@@ -32,6 +32,15 @@ ...@@ -32,6 +32,15 @@
#include <unity.h> #include <unity.h>
#include <string.h>
#ifndef _WIN32
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#endif
// Helper macro to define the v4/v6 function pairs // Helper macro to define the v4/v6 function pairs
#define MAKE_TEST_V4V6(_test) \ #define MAKE_TEST_V4V6(_test) \
static void _test##_ipv4 () { _test (false); } \ static void _test##_ipv4 () { _test (false); } \
......
...@@ -79,7 +79,7 @@ void test_req_correlate () ...@@ -79,7 +79,7 @@ void test_req_correlate ()
// Receive request id 1 // Receive request id 1
TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_recv (&msg, router, 0)); TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_recv (&msg, router, 0));
assert (zmq_msg_size (&msg) == sizeof (uint32_t)); TEST_ASSERT_EQUAL_UINT (sizeof (uint32_t), zmq_msg_size (&msg));
const uint32_t req_id = *static_cast<uint32_t *> (zmq_msg_data (&msg)); const uint32_t req_id = *static_cast<uint32_t *> (zmq_msg_data (&msg));
zmq_msg_t req_id_msg; zmq_msg_t req_id_msg;
zmq_msg_init (&req_id_msg); zmq_msg_init (&req_id_msg);
......
...@@ -30,6 +30,7 @@ ...@@ -30,6 +30,7 @@
#include "testutil.hpp" #include "testutil.hpp"
#include "testutil_unity.hpp" #include "testutil_unity.hpp"
#include <stdlib.h>
#include <unity.h> #include <unity.h>
void setUp () void setUp ()
......
...@@ -30,6 +30,8 @@ ...@@ -30,6 +30,8 @@
#include "testutil.hpp" #include "testutil.hpp"
#include "testutil_unity.hpp" #include "testutil_unity.hpp"
#include <string.h>
void setUp () void setUp ()
{ {
setup_test_context (); setup_test_context ();
......
...@@ -32,37 +32,41 @@ ...@@ -32,37 +32,41 @@
#include <vmci_sockets.h> #include <vmci_sockets.h>
#include "testutil.hpp" #include "testutil.hpp"
#include "testutil_unity.hpp"
int main (void) void setUp ()
{ {
setup_test_environment (); setup_test_context ();
void *ctx = zmq_ctx_new (); }
assert (ctx);
void tearDown ()
{
teardown_test_context ();
}
void test_reqrep_vmci ()
{
std::stringstream s; std::stringstream s;
s << "vmci://" << VMCISock_GetLocalCID () << ":" << 5560; s << "vmci://" << VMCISock_GetLocalCID () << ":" << 5560;
std::string endpoint = s.str (); std::string endpoint = s.str ();
void *sb = zmq_socket (ctx, ZMQ_REP); void *sb = test_context_socket (ZMQ_REP);
assert (sb); TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (sb, endpoint.c_str ()));
int rc = zmq_bind (sb, endpoint.c_str ());
assert (rc == 0);
void *sc = zmq_socket (ctx, ZMQ_REQ); void *sc = test_context_socket (ZMQ_REQ);
assert (sc); TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (sc, endpoint.c_str ()));
rc = zmq_connect (sc, endpoint.c_str ());
assert (rc == 0);
bounce (sb, sc); bounce (sb, sc);
rc = zmq_close (sc); test_context_socket_close (sc);
assert (rc == 0); test_context_socket_close (sb);
}
rc = zmq_close (sb);
assert (rc == 0);
rc = zmq_ctx_term (ctx); int main (void)
assert (rc == 0); {
setup_test_environment ();
return 0; UNITY_BEGIN ();
RUN_TEST (test_reqrep_vmci);
return UNITY_END ();
} }
...@@ -30,6 +30,7 @@ ...@@ -30,6 +30,7 @@
#include "testutil.hpp" #include "testutil.hpp"
#include "testutil_unity.hpp" #include "testutil_unity.hpp"
#include <string.h>
#include <unity.h> #include <unity.h>
void setUp () void setUp ()
......
...@@ -30,47 +30,49 @@ ...@@ -30,47 +30,49 @@
#include <stdio.h> #include <stdio.h>
#include "testutil.hpp" #include "testutil.hpp"
int main (void) #include "testutil_unity.hpp"
void setUp ()
{ {
if (!is_tipc_available ()) { setup_test_context ();
printf ("TIPC environment unavailable, skipping test\n"); }
return 77;
}
fprintf (stderr, "test_router_mandatory_tipc running...\n"); void tearDown ()
{
teardown_test_context ();
}
void *ctx = zmq_init (1); void test_router_mandatory_tipc ()
assert (ctx); {
if (!is_tipc_available ()) {
TEST_IGNORE_MESSAGE ("TIPC environment unavailable, skipping test");
}
// Creating the first socket. // Creating the first socket.
void *sa = zmq_socket (ctx, ZMQ_ROUTER); void *sa = test_context_socket (ZMQ_ROUTER);
assert (sa);
int rc = zmq_bind (sa, "tipc://{15560,0,0}"); TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (sa, "tipc://{15560,0,0}"));
assert (rc == 0);
// Sending a message to an unknown peer with the default setting // Sending a message to an unknown peer with the default setting
rc = zmq_send (sa, "UNKNOWN", 7, ZMQ_SNDMORE); send_string_expect_success (sa, "UNKNOWN", ZMQ_SNDMORE);
assert (rc == 7); send_string_expect_success (sa, "DATA", 0);
rc = zmq_send (sa, "DATA", 4, 0);
assert (rc == 4);
int mandatory = 1; int mandatory = 1;
// Set mandatory routing on socket // Set mandatory routing on socket
rc = TEST_ASSERT_SUCCESS_ERRNO (zmq_setsockopt (sa, ZMQ_ROUTER_MANDATORY,
zmq_setsockopt (sa, ZMQ_ROUTER_MANDATORY, &mandatory, sizeof (mandatory)); &mandatory, sizeof (mandatory)));
assert (rc == 0);
// Send a message and check that it fails // Send a message and check that it fails
rc = zmq_send (sa, "UNKNOWN", 7, ZMQ_SNDMORE | ZMQ_DONTWAIT); TEST_ASSERT_FAILURE_ERRNO (
assert (rc == -1 && errno == EHOSTUNREACH); EHOSTUNREACH, zmq_send (sa, "UNKNOWN", 7, ZMQ_SNDMORE | ZMQ_DONTWAIT));
rc = zmq_close (sa);
assert (rc == 0);
rc = zmq_ctx_term (ctx); test_context_socket_close (sa);
assert (rc == 0); }
return 0; int main (void)
{
UNITY_BEGIN ();
RUN_TEST (test_router_mandatory_tipc);
return UNITY_END ();
} }
...@@ -30,6 +30,7 @@ ...@@ -30,6 +30,7 @@
#include "testutil.hpp" #include "testutil.hpp"
#include "testutil_unity.hpp" #include "testutil_unity.hpp"
#include <string.h>
#include <unity.h> #include <unity.h>
void setUp () void setUp ()
......
This diff is collapsed.
...@@ -42,6 +42,8 @@ ...@@ -42,6 +42,8 @@
#include <unistd.h> #include <unistd.h>
#endif #endif
#include <stdlib.h>
static void zap_handler (void *handler_) static void zap_handler (void *handler_)
{ {
// Process ZAP requests forever // Process ZAP requests forever
...@@ -56,8 +58,8 @@ static void zap_handler (void *handler_) ...@@ -56,8 +58,8 @@ static void zap_handler (void *handler_)
char *routing_id = s_recv (handler_); char *routing_id = s_recv (handler_);
char *mechanism = s_recv (handler_); char *mechanism = s_recv (handler_);
assert (streq (version, "1.0")); TEST_ASSERT_EQUAL_STRING ("1.0", version);
assert (streq (mechanism, "NULL")); TEST_ASSERT_EQUAL_STRING ("NULL", mechanism);
s_sendmore (handler_, version); s_sendmore (handler_, version);
s_sendmore (handler_, sequence); s_sendmore (handler_, sequence);
......
...@@ -42,6 +42,9 @@ ...@@ -42,6 +42,9 @@
#include <unistd.h> #include <unistd.h>
#endif #endif
#include <stdlib.h>
#include <string.h>
static void zap_handler (void *zap_) static void zap_handler (void *zap_)
{ {
// Process ZAP requests forever // Process ZAP requests forever
...@@ -57,9 +60,9 @@ static void zap_handler (void *zap_) ...@@ -57,9 +60,9 @@ static void zap_handler (void *zap_)
char *username = s_recv (zap_); char *username = s_recv (zap_);
char *password = s_recv (zap_); char *password = s_recv (zap_);
assert (streq (version, "1.0")); TEST_ASSERT_EQUAL_STRING ("1.0", version);
assert (streq (mechanism, "PLAIN")); TEST_ASSERT_EQUAL_STRING ("PLAIN", mechanism);
assert (streq (routing_id, "IDENT")); TEST_ASSERT_EQUAL_STRING ("IDENT", routing_id);
s_sendmore (zap_, version); s_sendmore (zap_, version);
s_sendmore (zap_, sequence); s_sendmore (zap_, sequence);
...@@ -83,8 +86,7 @@ static void zap_handler (void *zap_) ...@@ -83,8 +86,7 @@ static void zap_handler (void *zap_)
free (username); free (username);
free (password); free (password);
} }
int rc = zmq_close (zap_); TEST_ASSERT_SUCCESS_ERRNO (zmq_close (zap_));
assert (rc == 0);
} }
void *zap_thread; void *zap_thread;
......
...@@ -30,6 +30,8 @@ ...@@ -30,6 +30,8 @@
#include "testutil.hpp" #include "testutil.hpp"
#include "testutil_unity.hpp" #include "testutil_unity.hpp"
#include <string.h>
void setUp () void setUp ()
{ {
setup_test_context (); setup_test_context ();
......
...@@ -30,6 +30,8 @@ ...@@ -30,6 +30,8 @@
#include "testutil.hpp" #include "testutil.hpp"
#include "testutil_unity.hpp" #include "testutil_unity.hpp"
#include <pthread.h>
void setUp () void setUp ()
{ {
} }
......
...@@ -30,6 +30,9 @@ ...@@ -30,6 +30,9 @@
#include "testutil.hpp" #include "testutil.hpp"
#include "testutil_unity.hpp" #include "testutil_unity.hpp"
#include <stdlib.h>
#include <string.h>
void setUp () void setUp ()
{ {
setup_test_context (); setup_test_context ();
......
...@@ -30,6 +30,7 @@ ...@@ -30,6 +30,7 @@
#include "testutil.hpp" #include "testutil.hpp"
#include "testutil_unity.hpp" #include "testutil_unity.hpp"
#include <stdlib.h>
#include <unity.h> #include <unity.h>
void setUp () void setUp ()
......
...@@ -30,6 +30,8 @@ ...@@ -30,6 +30,8 @@
#include "testutil.hpp" #include "testutil.hpp"
#include "testutil_unity.hpp" #include "testutil_unity.hpp"
#include <stdlib.h>
#include <string.h>
#include <unity.h> #include <unity.h>
void setUp () void setUp ()
......
...@@ -30,6 +30,7 @@ ...@@ -30,6 +30,7 @@
#include "testutil.hpp" #include "testutil.hpp"
#include "testutil_unity.hpp" #include "testutil_unity.hpp"
#include <string.h>
#include <unity.h> #include <unity.h>
void setUp () void setUp ()
......
...@@ -30,6 +30,8 @@ ...@@ -30,6 +30,8 @@
#include "testutil.hpp" #include "testutil.hpp"
#include "testutil_unity.hpp" #include "testutil_unity.hpp"
#include <string.h>
void setUp () void setUp ()
{ {
setup_test_context (); setup_test_context ();
......
...@@ -30,6 +30,8 @@ ...@@ -30,6 +30,8 @@
#include "testutil.hpp" #include "testutil.hpp"
#include "testutil_unity.hpp" #include "testutil_unity.hpp"
#include <string.h>
void setUp () void setUp ()
{ {
setup_test_context (); setup_test_context ();
......
...@@ -30,6 +30,14 @@ ...@@ -30,6 +30,14 @@
#include "testutil.hpp" #include "testutil.hpp"
#include "testutil_unity.hpp" #include "testutil_unity.hpp"
#include <string.h>
#ifndef _WIN32
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#endif
void setUp () void setUp ()
{ {
setup_test_context (); setup_test_context ();
......
...@@ -30,6 +30,9 @@ ...@@ -30,6 +30,9 @@
#include "testutil.hpp" #include "testutil.hpp"
#include "testutil_unity.hpp" #include "testutil_unity.hpp"
#include <stdlib.h>
#include <string.h>
void setUp () void setUp ()
{ {
setup_test_context (); setup_test_context ();
......
...@@ -28,6 +28,7 @@ ...@@ -28,6 +28,7 @@
*/ */
#include "testutil.hpp" #include "testutil.hpp"
#include "testutil_unity.hpp"
#if defined(ZMQ_HAVE_WINDOWS) #if defined(ZMQ_HAVE_WINDOWS)
#include <winsock2.h> #include <winsock2.h>
...@@ -38,6 +39,16 @@ ...@@ -38,6 +39,16 @@
#include <unistd.h> #include <unistd.h>
#endif #endif
void setUp ()
{
setup_test_context ();
}
void tearDown ()
{
teardown_test_context ();
}
// Solaris has a default of 256 max files per process // Solaris has a default of 256 max files per process
#ifdef ZMQ_HAVE_SOLARIS #ifdef ZMQ_HAVE_SOLARIS
#define MAX_SOCKETS 200 #define MAX_SOCKETS 200
...@@ -62,25 +73,21 @@ void initialise_network (void) ...@@ -62,25 +73,21 @@ void initialise_network (void)
#endif #endif
// This test case stresses the system to shake out known configuration void test_localhost ()
// problems. We're direct system calls when necessary. Some code may
// need wrapping to be properly portable.
int main (void)
{ {
initialise_network ();
// Check that we have local networking via ZeroMQ // Check that we have local networking via ZeroMQ
void *ctx = zmq_ctx_new (); void *dealer = test_context_socket (ZMQ_DEALER);
assert (ctx);
void *dealer = zmq_socket (ctx, ZMQ_DEALER);
if (zmq_bind (dealer, "tcp://127.0.0.1:*") == -1) { if (zmq_bind (dealer, "tcp://127.0.0.1:*") == -1) {
printf ( TEST_FAIL_MESSAGE (
"E: Cannot find 127.0.0.1 -- your system does not have local\n"); "E: Cannot find 127.0.0.1 -- your system does not have local\n"
printf (
"E: networking. Please fix this before running libzmq checks.\n"); "E: networking. Please fix this before running libzmq checks.\n");
return -1;
} }
test_context_socket_close (dealer);
}
void test_max_sockets ()
{
// Check that we can create 1,000 sockets // Check that we can create 1,000 sockets
fd_t handle[MAX_SOCKETS]; fd_t handle[MAX_SOCKETS];
int count; int count;
...@@ -88,19 +95,31 @@ int main (void) ...@@ -88,19 +95,31 @@ int main (void)
handle[count] = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP); handle[count] = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (handle[count] == -1) { if (handle[count] == -1) {
printf ("W: Only able to create %d sockets on this box\n", count); printf ("W: Only able to create %d sockets on this box\n", count);
printf ( const char msg[] =
"I: Tune your system to increase maximum allowed file handles\n"); "I: Tune your system to increase maximum allowed file handles\n"
#if !defined(ZMQ_HAVE_WINDOWS) #if !defined(ZMQ_HAVE_WINDOWS)
printf ("I: Run 'ulimit -n 1200' in bash\n"); "I: Run 'ulimit -n 1200' in bash\n"
#endif #endif
return -1; ;
TEST_FAIL_MESSAGE (msg);
} }
} }
// Release the socket handles // Release the socket handles
for (count = 0; count < MAX_SOCKETS; count++) { for (count = 0; count < MAX_SOCKETS; count++) {
close (handle[count]); close (handle[count]);
} }
}
// This test case stresses the system to shake out known configuration
// problems. We're direct system calls when necessary. Some code may
// need wrapping to be properly portable.
int main (void)
{
initialise_network ();
zmq_close (dealer); UNITY_BEGIN ();
zmq_ctx_term (ctx); RUN_TEST (test_localhost);
RUN_TEST (test_max_sockets);
return UNITY_END ();
} }
...@@ -28,98 +28,84 @@ ...@@ -28,98 +28,84 @@
*/ */
#include "testutil.hpp" #include "testutil.hpp"
#include "testutil_unity.hpp"
int main (void) void setUp ()
{ {
if (!is_tipc_available ()) { setup_test_context ();
printf ("TIPC environment unavailable, skipping test\n"); }
return 77;
} void tearDown ()
{
teardown_test_context ();
}
int rc; const char ep[] = "tipc://{5560,0,0}";
char buf[32]; const char name[] = "tipc://{5560,0}@0.0.0";
const char *ep = "tipc://{5560,0,0}";
const char *name = "tipc://{5560,0}@0.0.0";
fprintf (stderr, "unbind endpoint test running...\n"); void test_term_endpoint_unbind_tipc ()
{
if (!is_tipc_available ()) {
TEST_IGNORE_MESSAGE ("TIPC environment unavailable, skipping test\n");
}
// Create infrastructure. // Create infrastructure.
void *ctx = zmq_init (1); void *push = test_context_socket (ZMQ_PUSH);
assert (ctx); TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (push, ep));
void *push = zmq_socket (ctx, ZMQ_PUSH); void *pull = test_context_socket (ZMQ_PULL);
assert (push); TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (pull, name));
rc = zmq_bind (push, ep);
assert (rc == 0);
void *pull = zmq_socket (ctx, ZMQ_PULL);
assert (pull);
rc = zmq_connect (pull, name);
assert (rc == 0);
// Pass one message through to ensure the connection is established. // Pass one message through to ensure the connection is established.
rc = zmq_send (push, "ABC", 3, 0); send_string_expect_success (push, "ABC", 0);
assert (rc == 3); recv_string_expect_success (pull, "ABC", 0);
rc = zmq_recv (pull, buf, sizeof (buf), 0);
assert (rc == 3);
// Unbind the lisnening endpoint // Unbind the lisnening endpoint
rc = zmq_unbind (push, ep); TEST_ASSERT_SUCCESS_ERRNO (zmq_unbind (push, ep));
assert (rc == 0);
// Let events some time // Let events some time
msleep (SETTLE_TIME); msleep (SETTLE_TIME);
// Check that sending would block (there's no outbound connection). // Check that sending would block (there's no outbound connection).
rc = zmq_send (push, "ABC", 3, ZMQ_DONTWAIT); TEST_ASSERT_FAILURE_ERRNO (EAGAIN, zmq_send (push, "ABC", 3, ZMQ_DONTWAIT));
assert (rc == -1 && zmq_errno () == EAGAIN);
// Clean up. // Clean up.
rc = zmq_close (pull); test_context_socket_close (pull);
assert (rc == 0); test_context_socket_close (push);
rc = zmq_close (push); }
assert (rc == 0);
rc = zmq_ctx_term (ctx);
assert (rc == 0);
// Now the other way round.
fprintf (stderr, "disconnect endpoint test running...\n");
void test_term_endpoint_disconnect_tipc ()
{
if (!is_tipc_available ()) {
TEST_IGNORE_MESSAGE ("TIPC environment unavailable, skipping test\n");
}
// Create infrastructure. // Create infrastructure.
ctx = zmq_init (1); void *push = test_context_socket (ZMQ_PUSH);
assert (ctx); TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (push, name));
push = zmq_socket (ctx, ZMQ_PUSH); void *pull = test_context_socket (ZMQ_PULL);
assert (push); TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (pull, ep));
rc = zmq_connect (push, name);
assert (rc == 0);
pull = zmq_socket (ctx, ZMQ_PULL);
assert (pull);
rc = zmq_bind (pull, ep);
assert (rc == 0);
// Pass one message through to ensure the connection is established. // Pass one message through to ensure the connection is established.
rc = zmq_send (push, "ABC", 3, 0); send_string_expect_success (push, "ABC", 0);
assert (rc == 3); recv_string_expect_success (pull, "ABC", 0);
rc = zmq_recv (pull, buf, sizeof (buf), 0);
assert (rc == 3);
// Disconnect the bound endpoint // Disconnect the bound endpoint
rc = zmq_disconnect (push, name); TEST_ASSERT_SUCCESS_ERRNO (zmq_disconnect (push, name));
assert (rc == 0);
msleep (SETTLE_TIME); msleep (SETTLE_TIME);
// Check that sending would block (there's no inbound connections). // Check that sending would block (there's no inbound connections).
rc = zmq_send (push, "ABC", 3, ZMQ_DONTWAIT); TEST_ASSERT_FAILURE_ERRNO (EAGAIN, zmq_send (push, "ABC", 3, ZMQ_DONTWAIT));
assert (rc == -1 && zmq_errno () == EAGAIN);
// Clean up. // Clean up.
rc = zmq_close (pull); test_context_socket_close (pull);
assert (rc == 0); test_context_socket_close (push);
rc = zmq_close (push); }
assert (rc == 0);
rc = zmq_ctx_term (ctx); int main (void)
assert (rc == 0); {
UNITY_BEGIN ();
return 0; RUN_TEST (test_term_endpoint_unbind_tipc);
RUN_TEST (test_term_endpoint_disconnect_tipc);
return UNITY_END ();
} }
...@@ -20,6 +20,8 @@ ...@@ -20,6 +20,8 @@
#include "testutil.hpp" #include "testutil.hpp"
#include "testutil_unity.hpp" #include "testutil_unity.hpp"
#include <string.h>
void setUp () void setUp ()
{ {
setup_test_context (); setup_test_context ();
......
...@@ -30,7 +30,10 @@ ...@@ -30,7 +30,10 @@
#include "testutil.hpp" #include "testutil.hpp"
#include "testutil_unity.hpp" #include "testutil_unity.hpp"
#include <stdlib.h>
#include <string.h>
#include <unity.h> #include <unity.h>
#include <unistd.h>
void setUp () void setUp ()
{ {
...@@ -47,6 +50,7 @@ void tearDown () ...@@ -47,6 +50,7 @@ void tearDown ()
#include <sys/socket.h> #include <sys/socket.h>
#include <sys/un.h> #include <sys/un.h>
#include <netdb.h> #include <netdb.h>
#include <unistd.h>
int setup_socket_and_set_fd (void *zmq_socket_, int setup_socket_and_set_fd (void *zmq_socket_,
int af_, int af_,
......
...@@ -31,8 +31,13 @@ ...@@ -31,8 +31,13 @@
#include "testutil_unity.hpp" #include "testutil_unity.hpp"
#include <netdb.h> #include <netdb.h>
#include <string.h>
#include <unity.h> #include <unity.h>
#ifndef _WIN32
#include <unistd.h>
#endif
void setUp () void setUp ()
{ {
setup_test_context (); setup_test_context ();
......
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
...@@ -35,7 +35,7 @@ foreach(test ${unittests}) ...@@ -35,7 +35,7 @@ foreach(test ${unittests})
link_directories(${test} PRIVATE "${ZeroMQ_SOURCE_DIR}/../lib") link_directories(${test} PRIVATE "${ZeroMQ_SOURCE_DIR}/../lib")
endif() endif()
target_link_libraries(${test} libzmq-static ${OPTIONAL_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT} unity) target_link_libraries(${test} testutil-static)
if(RT_LIBRARY) if(RT_LIBRARY)
target_link_libraries(${test} ${RT_LIBRARY}) target_link_libraries(${test} ${RT_LIBRARY})
......
This diff is collapsed.
This diff is collapsed.
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