Commit dce77fda authored by Simon Giesecke's avatar Simon Giesecke

Problem: test_filter_ipc not yet using unity

Solution: migrate to unity
parent fe82c643
...@@ -775,7 +775,8 @@ tests_test_timeo_SOURCES = tests/test_timeo.cpp ...@@ -775,7 +775,8 @@ tests_test_timeo_SOURCES = tests/test_timeo.cpp
tests_test_timeo_LDADD = src/libzmq.la tests_test_timeo_LDADD = src/libzmq.la
tests_test_filter_ipc_SOURCES = tests/test_filter_ipc.cpp tests_test_filter_ipc_SOURCES = tests/test_filter_ipc.cpp
tests_test_filter_ipc_LDADD = src/libzmq.la tests_test_filter_ipc_LDADD = src/libzmq.la ${UNITY_LIBS}
tests_test_filter_ipc_CPPFLAGS = ${UNITY_CPPFLAGS}
tests_test_use_fd_ipc_SOURCES = \ tests_test_use_fd_ipc_SOURCES = \
tests/test_use_fd_ipc.cpp \ tests/test_use_fd_ipc.cpp \
......
...@@ -28,6 +28,17 @@ ...@@ -28,6 +28,17 @@
*/ */
#include "testutil.hpp" #include "testutil.hpp"
#include "testutil_unity.hpp"
void setUp ()
{
setup_test_context ();
}
void tearDown ()
{
teardown_test_context ();
}
static void bounce_fail (void *server_, void *client_) static void bounce_fail (void *server_, void *client_)
{ {
...@@ -35,131 +46,178 @@ static void bounce_fail (void *server_, void *client_) ...@@ -35,131 +46,178 @@ static void bounce_fail (void *server_, void *client_)
char buffer[32]; char buffer[32];
// Send message from client to server // Send message from client to server
int rc = zmq_send (client_, content, 32, ZMQ_SNDMORE); send_string_expect_success (client_, content, ZMQ_SNDMORE);
assert (rc == 32); send_string_expect_success (client_, content, 0);
rc = zmq_send (client_, content, 32, 0);
assert (rc == 32);
// Receive message at server side (should not succeed) // Receive message at server side (should not succeed)
int timeout = 250; int timeout = SETTLE_TIME;
rc = zmq_setsockopt (server_, ZMQ_RCVTIMEO, &timeout, sizeof (int)); TEST_ASSERT_SUCCESS_ERRNO (
assert (rc == 0); zmq_setsockopt (server_, ZMQ_RCVTIMEO, &timeout, sizeof (int)));
rc = zmq_recv (server_, buffer, 32, 0); TEST_ASSERT_FAILURE_ERRNO (EAGAIN, zmq_recv (server_, buffer, 32, 0));
assert (rc == -1);
assert (zmq_errno () == EAGAIN);
// Send message from server to client to test other direction // Send message from server to client to test other direction
rc = zmq_setsockopt (server_, ZMQ_SNDTIMEO, &timeout, sizeof (int)); TEST_ASSERT_SUCCESS_ERRNO (
assert (rc == 0); zmq_setsockopt (server_, ZMQ_SNDTIMEO, &timeout, sizeof (int)));
rc = zmq_send (server_, content, 32, ZMQ_SNDMORE); TEST_ASSERT_FAILURE_ERRNO (EAGAIN,
assert (rc == -1); zmq_send (server_, content, 32, ZMQ_SNDMORE));
assert (zmq_errno () == EAGAIN);
} }
template <class T> template <class T>
static void static void
run_test (int opt_, T optval_, int expected_error_, int bounce_test_) run_test (int opt_, T optval_, int expected_error_, int bounce_test_)
{ {
int rc; void *sb = test_context_socket (ZMQ_DEALER);
void *ctx = zmq_ctx_new ();
assert (ctx);
void *sb = zmq_socket (ctx, ZMQ_DEALER);
assert (sb);
if (opt_) { if (opt_) {
rc = zmq_setsockopt (sb, opt_, &optval_, sizeof (optval_)); const int rc = zmq_setsockopt (sb, opt_, &optval_, sizeof (optval_));
if (expected_error_) { if (expected_error_) {
assert (rc == -1); TEST_ASSERT_FAILURE_ERRNO (expected_error_, rc);
assert (zmq_errno () == expected_error_); } else {
} else TEST_ASSERT_SUCCESS_ERRNO (rc);
assert (rc == 0); }
} }
void *sc = zmq_socket (ctx, ZMQ_DEALER); void *sc = test_context_socket (ZMQ_DEALER);
assert (sc);
// If a test fails, don't hang for too long // If a test fails, don't hang for too long
int timeout = 2500; int timeout = 2500;
rc = zmq_setsockopt (sb, ZMQ_RCVTIMEO, &timeout, sizeof (int)); TEST_ASSERT_SUCCESS_ERRNO (
assert (rc == 0); zmq_setsockopt (sb, ZMQ_RCVTIMEO, &timeout, sizeof (int)));
rc = zmq_setsockopt (sb, ZMQ_SNDTIMEO, &timeout, sizeof (int)); TEST_ASSERT_SUCCESS_ERRNO (
assert (rc == 0); zmq_setsockopt (sb, ZMQ_SNDTIMEO, &timeout, sizeof (int)));
rc = zmq_setsockopt (sc, ZMQ_RCVTIMEO, &timeout, sizeof (int)); TEST_ASSERT_SUCCESS_ERRNO (
assert (rc == 0); zmq_setsockopt (sc, ZMQ_RCVTIMEO, &timeout, sizeof (int)));
rc = zmq_setsockopt (sc, ZMQ_SNDTIMEO, &timeout, sizeof (int)); TEST_ASSERT_SUCCESS_ERRNO (
assert (rc == 0); zmq_setsockopt (sc, ZMQ_SNDTIMEO, &timeout, sizeof (int)));
int interval = -1; int interval = -1;
rc = zmq_setsockopt (sc, ZMQ_RECONNECT_IVL, &interval, sizeof (int)); TEST_ASSERT_SUCCESS_ERRNO (
assert (rc == 0); zmq_setsockopt (sc, ZMQ_RECONNECT_IVL, &interval, sizeof (int)));
if (bounce_test_) { if (bounce_test_) {
const char *endpoint = "ipc://test_filter_ipc.sock"; const char *endpoint = "ipc://test_filter_ipc.sock";
int rc = zmq_bind (sb, endpoint); TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (sb, endpoint));
assert (rc == 0); TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (sc, endpoint));
rc = zmq_connect (sc, endpoint);
assert (rc == 0);
if (bounce_test_ > 0) if (bounce_test_ > 0)
bounce (sb, sc); bounce (sb, sc);
else else
bounce_fail (sb, sc); bounce_fail (sb, sc);
} }
close_zero_linger (sc);
close_zero_linger (sb);
rc = zmq_ctx_term (ctx); // TODO only use zero linger when bounce_test_ < 0?
assert (rc == 0); test_context_socket_close_zero_linger (sc);
test_context_socket_close_zero_linger (sb);
} }
int main (void)
{
#if !defined(ZMQ_HAVE_WINDOWS)
setup_test_environment ();
// No filters
run_test<int> (0, 0, 0, 1);
#if defined ZMQ_HAVE_SO_PEERCRED || defined ZMQ_HAVE_LOCAL_PEERCRED #if defined ZMQ_HAVE_SO_PEERCRED || defined ZMQ_HAVE_LOCAL_PEERCRED
// Get the group and supplimental groups of the process owner gid_t group, supgroup, notgroup;
void init_groups ()
{
// 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); assert (ngroups != -1);
gid_t group = getgid (), supgroup = group, notgroup = group + 1; group = getgid ();
supgroup = group;
notgroup = group + 1;
for (int i = 0; i < ngroups; i++) { for (int i = 0; i < ngroups; i++) {
if (supgroup == group && group != groups[i]) if (supgroup == group && group != groups[i])
supgroup = groups[i]; supgroup = groups[i];
if (notgroup <= groups[i]) if (notgroup <= groups[i])
notgroup = groups[i] + 1; notgroup = groups[i] + 1;
} }
}
#endif
void test_no_filters ()
{
run_test<int> (0, 0, 0, 1);
}
// Test filter with UID of process owner #if defined ZMQ_HAVE_SO_PEERCRED || defined ZMQ_HAVE_LOCAL_PEERCRED
void test_filter_with_process_owner_uid ()
{
run_test<uid_t> (ZMQ_IPC_FILTER_UID, getuid (), 0, 1); run_test<uid_t> (ZMQ_IPC_FILTER_UID, getuid (), 0, 1);
// Test filter with UID of another (possibly non-existent) user }
void test_filter_with_possibly_nonexistent_uid ()
{
run_test<uid_t> (ZMQ_IPC_FILTER_UID, getuid () + 1, 0, -1); run_test<uid_t> (ZMQ_IPC_FILTER_UID, getuid () + 1, 0, -1);
// Test filter with GID of process owner }
void test_filter_with_process_owner_gid ()
{
run_test<gid_t> (ZMQ_IPC_FILTER_GID, group, 0, 1); run_test<gid_t> (ZMQ_IPC_FILTER_GID, group, 0, 1);
// Test filter with supplimental group of process owner }
void test_filter_with_supplemental_process_owner_gid ()
{
run_test<gid_t> (ZMQ_IPC_FILTER_GID, supgroup, 0, 1); run_test<gid_t> (ZMQ_IPC_FILTER_GID, supgroup, 0, 1);
// Test filter with GID of another (possibly non-existent) group }
void test_filter_with_possibly_nonexistent_gid ()
{
run_test<gid_t> (ZMQ_IPC_FILTER_GID, notgroup, 0, -1); run_test<gid_t> (ZMQ_IPC_FILTER_GID, notgroup, 0, -1);
}
#if defined ZMQ_HAVE_SO_PEERCRED #if defined ZMQ_HAVE_SO_PEERCRED
// Test filter with PID of current process void test_filter_with_current_process_pid ()
{
run_test<pid_t> (ZMQ_IPC_FILTER_PID, getpid (), 0, 1); run_test<pid_t> (ZMQ_IPC_FILTER_PID, getpid (), 0, 1);
// Test filter with PID of another (possibly non-existent) process }
void test_filter_with_possibly_nonexistent_pid ()
{
run_test<pid_t> (ZMQ_IPC_FILTER_PID, getpid () + 1, 0, -1); run_test<pid_t> (ZMQ_IPC_FILTER_PID, getpid () + 1, 0, -1);
}
#else #else
void test_filter_with_pid_fails ()
{
// Setup of PID filter should fail with operation not supported error // Setup of PID filter should fail with operation not supported error
// TODO EINVAL is not ENOTSUP (!)
run_test<pid_t> (ZMQ_IPC_FILTER_PID, getpid (), EINVAL, 0); run_test<pid_t> (ZMQ_IPC_FILTER_PID, getpid (), EINVAL, 0);
}
#endif #endif
#else #else
void test_filter_with_zero_uid_fails ()
{
run_test<uid_t> (ZMQ_IPC_FILTER_UID, 0, EINVAL, 0); run_test<uid_t> (ZMQ_IPC_FILTER_UID, 0, EINVAL, 0);
}
void test_filter_with_zero_gid_fails ()
{
run_test<gid_t> (ZMQ_IPC_FILTER_GID, 0, EINVAL, 0); run_test<gid_t> (ZMQ_IPC_FILTER_GID, 0, EINVAL, 0);
}
void test_filter_with_zero_pid_fails ()
{
run_test<pid_t> (ZMQ_IPC_FILTER_PID, 0, EINVAL, 0); run_test<pid_t> (ZMQ_IPC_FILTER_PID, 0, EINVAL, 0);
}
#endif // defined ZMQ_HAVE_SO_PEERCRED || defined ZMQ_HAVE_LOCAL_PEERCRED #endif // defined ZMQ_HAVE_SO_PEERCRED || defined ZMQ_HAVE_LOCAL_PEERCRED
int main (void)
{
#if !defined(ZMQ_HAVE_WINDOWS)
setup_test_environment ();
#if defined ZMQ_HAVE_SO_PEERCRED || defined ZMQ_HAVE_LOCAL_PEERCRED
init_groups ();
#endif #endif
UNITY_BEGIN ();
RUN_TEST (test_no_filters);
#if defined ZMQ_HAVE_SO_PEERCRED || defined ZMQ_HAVE_LOCAL_PEERCRED
RUN_TEST (test_filter_with_process_owner_uid);
RUN_TEST (test_filter_with_possibly_nonexistent_uid);
RUN_TEST (test_filter_with_process_owner_gid);
RUN_TEST (test_filter_with_supplemental_process_owner_gid);
RUN_TEST (test_filter_with_possibly_nonexistent_gid);
#if defined ZMQ_HAVE_SO_PEERCRED
RUN_TEST (test_filter_with_current_process_pid);
RUN_TEST (test_filter_with_possibly_nonexistent_pid);
#else
RUN_TEST (test_filter_with_pid_fails ());
#endif
#else
RUN_TEST (test_filter_with_zero_uid_fails);
RUN_TEST (test_filter_with_zero_gid_fails);
RUN_TEST (test_filter_with_zero_pid_fails);
#endif // defined ZMQ_HAVE_SO_PEERCRED || defined ZMQ_HAVE_LOCAL_PEERCRED
return UNITY_END ();
#else
return 0; return 0;
#endif
} }
...@@ -90,8 +90,8 @@ int test_assert_success_message_raw_errno_helper (int rc_, ...@@ -90,8 +90,8 @@ int test_assert_success_message_raw_errno_helper (int rc_,
#define TEST_ASSERT_FAILURE_ERRNO(error_code, expr) \ #define TEST_ASSERT_FAILURE_ERRNO(error_code, expr) \
{ \ { \
int rc = (expr); \ int _rc = (expr); \
TEST_ASSERT_EQUAL_INT (-1, rc); \ TEST_ASSERT_EQUAL_INT (-1, _rc); \
TEST_ASSERT_EQUAL_INT (error_code, errno); \ TEST_ASSERT_EQUAL_INT (error_code, errno); \
} }
......
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