Commit 669fc4df authored by Simon Giesecke's avatar Simon Giesecke

Problem: test_security_zap not using a test framework

Solution: migrate to unity
parent 99e53698
...@@ -616,7 +616,8 @@ tests_test_security_zap_SOURCES = \ ...@@ -616,7 +616,8 @@ tests_test_security_zap_SOURCES = \
tests/testutil_monitoring.hpp \ tests/testutil_monitoring.hpp \
tests/testutil_security.hpp \ tests/testutil_security.hpp \
tests/testutil.hpp tests/testutil.hpp
tests_test_security_zap_LDADD = src/libzmq.la tests_test_security_zap_LDADD = src/libzmq.la ${UNITY_LIBS}
tests_test_security_zap_CPPFLAGS = ${UNITY_CPPFLAGS}
tests_test_spec_req_SOURCES = tests/test_spec_req.cpp tests_test_spec_req_SOURCES = tests/test_spec_req.cpp
tests_test_spec_req_LDADD = src/libzmq.la ${UNITY_LIBS} tests_test_spec_req_LDADD = src/libzmq.la ${UNITY_LIBS}
......
...@@ -28,6 +28,17 @@ ...@@ -28,6 +28,17 @@
*/ */
#include "testutil_security.hpp" #include "testutil_security.hpp"
#include "testutil_unity.hpp"
void setUp ()
{
setup_test_context ();
}
void tearDown ()
{
teardown_test_context ();
}
static void zap_handler_wrong_version (void *ctx_) static void zap_handler_wrong_version (void *ctx_)
{ {
...@@ -118,8 +129,8 @@ void test_zap_unsuccessful (void *ctx_, ...@@ -118,8 +129,8 @@ void test_zap_unsuccessful (void *ctx_,
// there may be more than one ZAP request due to repeated attempts by the // there may be more than one ZAP request due to repeated attempts by the
// client (actually only in case if ZAP status code 300) // client (actually only in case if ZAP status code 300)
assert (server_events_received == 0 TEST_ASSERT_TRUE (server_events_received == 0
|| 1 <= zmq_atomic_counter_value (zap_requests_handled)); || 1 <= zmq_atomic_counter_value (zap_requests_handled));
} }
void test_zap_unsuccessful_no_handler (void *ctx_, void test_zap_unsuccessful_no_handler (void *ctx_,
...@@ -132,14 +143,14 @@ void test_zap_unsuccessful_no_handler (void *ctx_, ...@@ -132,14 +143,14 @@ void test_zap_unsuccessful_no_handler (void *ctx_,
void *socket_config_data_, void *socket_config_data_,
void **client_mon_ = NULL) void **client_mon_ = NULL)
{ {
int events_received = const int events_received =
expect_new_client_bounce_fail_and_count_monitor_events ( expect_new_client_bounce_fail_and_count_monitor_events (
ctx_, my_endpoint_, server_, socket_config_, socket_config_data_, ctx_, my_endpoint_, server_, socket_config_, socket_config_data_,
client_mon_, server_mon_, expected_event_, expected_err_); client_mon_, server_mon_, expected_event_, expected_err_);
// there may be more than one ZAP request due to repeated attempts by the // there may be more than one ZAP request due to repeated attempts by the
// client // client
assert (events_received > 0); TEST_ASSERT_GREATER_THAN_INT (0, events_received);
} }
void test_zap_protocol_error (void *ctx_, void test_zap_protocol_error (void *ctx_,
...@@ -170,8 +181,7 @@ void test_zap_unsuccessful_status_300 (void *ctx_, ...@@ -170,8 +181,7 @@ void test_zap_unsuccessful_status_300 (void *ctx_,
// we can use a 0 timeout here, since the client socket is already closed // we can use a 0 timeout here, since the client socket is already closed
assert_no_more_monitor_events_with_timeout (client_mon, 0); assert_no_more_monitor_events_with_timeout (client_mon, 0);
int rc = zmq_close (client_mon); TEST_ASSERT_SUCCESS_ERRNO (zmq_close (client_mon));
assert (rc == 0);
} }
void test_zap_unsuccessful_status_500 (void *ctx_, void test_zap_unsuccessful_status_500 (void *ctx_,
...@@ -187,75 +197,86 @@ void test_zap_unsuccessful_status_500 (void *ctx_, ...@@ -187,75 +197,86 @@ void test_zap_unsuccessful_status_500 (void *ctx_,
NULL, ZMQ_EVENT_HANDSHAKE_FAILED_AUTH, 500); NULL, ZMQ_EVENT_HANDSHAKE_FAILED_AUTH, 500);
} }
void test_zap_errors (socket_config_fn server_socket_config_, static void
void *server_socket_config_data_, test_zap_protocol_error_closure (socket_config_fn server_socket_config_,
socket_config_fn client_socket_config_, socket_config_fn client_socket_config_,
void *client_socket_config_data_) void *client_socket_config_data_,
void *server_socket_config_data_,
zmq_thread_fn zap_handler_,
int expected_failure_)
{ {
void *ctx; void *ctx, *handler, *zap_thread, *server, *server_mon;
void *handler;
void *zap_thread;
void *server;
void *server_mon;
char my_endpoint[MAX_SOCKET_STRING]; char my_endpoint[MAX_SOCKET_STRING];
// Invalid ZAP protocol tests
// wrong version
fprintf (stderr, "test_zap_protocol_error wrong_version\n");
setup_context_and_server_side ( setup_context_and_server_side (
&ctx, &handler, &zap_thread, &server, &server_mon, my_endpoint, &ctx, &handler, &zap_thread, &server, &server_mon, my_endpoint,
&zap_handler_wrong_version, server_socket_config_, zap_handler_, server_socket_config_, server_socket_config_data_);
server_socket_config_data_);
test_zap_protocol_error (ctx, my_endpoint, server, server_mon, test_zap_protocol_error (ctx, my_endpoint, server, server_mon,
client_socket_config_, client_socket_config_data_, client_socket_config_, client_socket_config_data_,
ZMQ_PROTOCOL_ERROR_ZAP_BAD_VERSION); expected_failure_);
shutdown_context_and_server_side (ctx, zap_thread, server, server_mon, shutdown_context_and_server_side (ctx, zap_thread, server, server_mon,
handler); handler);
}
// wrong request id static void
fprintf (stderr, "test_zap_protocol_error wrong_request_id\n"); test_zap_protocol_error_wrong_version (socket_config_fn server_socket_config_,
setup_context_and_server_side ( socket_config_fn client_socket_config_,
&ctx, &handler, &zap_thread, &server, &server_mon, my_endpoint, void *client_socket_config_data_,
&zap_handler_wrong_request_id, server_socket_config_, void *server_socket_config_data_)
server_socket_config_data_); {
test_zap_protocol_error (ctx, my_endpoint, server, server_mon, test_zap_protocol_error_closure (
client_socket_config_, client_socket_config_data_, server_socket_config_, client_socket_config_, client_socket_config_data_,
ZMQ_PROTOCOL_ERROR_ZAP_BAD_REQUEST_ID); server_socket_config_data_, &zap_handler_wrong_version,
shutdown_context_and_server_side (ctx, zap_thread, server, server_mon, ZMQ_PROTOCOL_ERROR_ZAP_BAD_VERSION);
handler); }
// status invalid (not a 3-digit number) static void test_zap_protocol_error_wrong_request_id (
fprintf (stderr, "test_zap_protocol_error wrong_status_invalid\n"); socket_config_fn server_socket_config_,
setup_context_and_server_side ( socket_config_fn client_socket_config_,
&ctx, &handler, &zap_thread, &server, &server_mon, my_endpoint, void *client_socket_config_data_,
&zap_handler_wrong_status_invalid, server_socket_config_, void *server_socket_config_data_)
server_socket_config_data_); {
test_zap_protocol_error (ctx, my_endpoint, server, server_mon, test_zap_protocol_error_closure (
client_socket_config_, client_socket_config_data_, server_socket_config_, client_socket_config_, client_socket_config_data_,
ZMQ_PROTOCOL_ERROR_ZAP_INVALID_STATUS_CODE); server_socket_config_data_, &zap_handler_wrong_request_id,
shutdown_context_and_server_side (ctx, zap_thread, server, server_mon, ZMQ_PROTOCOL_ERROR_ZAP_BAD_REQUEST_ID);
handler); }
// too many parts static void test_zap_protocol_error_wrong_status_invalid (
fprintf (stderr, "test_zap_protocol_error too_many_parts\n"); socket_config_fn server_socket_config_,
setup_context_and_server_side ( socket_config_fn client_socket_config_,
&ctx, &handler, &zap_thread, &server, &server_mon, my_endpoint, void *client_socket_config_data_,
&zap_handler_too_many_parts, server_socket_config_, void *server_socket_config_data_)
server_socket_config_data_); {
test_zap_protocol_error (ctx, my_endpoint, server, server_mon, test_zap_protocol_error_closure (
client_socket_config_, client_socket_config_data_, server_socket_config_, client_socket_config_, client_socket_config_data_,
ZMQ_PROTOCOL_ERROR_ZAP_MALFORMED_REPLY); server_socket_config_data_, &zap_handler_wrong_status_invalid,
shutdown_context_and_server_side (ctx, zap_thread, server, server_mon, ZMQ_PROTOCOL_ERROR_ZAP_INVALID_STATUS_CODE);
handler); }
// ZAP non-standard cases static void
test_zap_protocol_error_too_many_parts (socket_config_fn server_socket_config_,
socket_config_fn client_socket_config_,
void *client_socket_config_data_,
void *server_socket_config_data_)
{
test_zap_protocol_error_closure (
server_socket_config_, client_socket_config_, client_socket_config_data_,
server_socket_config_data_, &zap_handler_too_many_parts,
ZMQ_PROTOCOL_ERROR_ZAP_MALFORMED_REPLY);
}
// TODO make these observable on the client side as well (they are // TODO the failed status (300/500) should be observable as monitoring events on the client side as well (they are
// transmitted as an ERROR message) // already transmitted as an ERROR message)
// status 300 temporary failure static void
fprintf (stderr, "test_zap_unsuccessful status 300\n"); test_zap_wrong_status_temporary_failure (socket_config_fn server_socket_config_,
socket_config_fn client_socket_config_,
void *client_socket_config_data_,
void *server_socket_config_data_)
{
void *ctx, *handler, *zap_thread, *server, *server_mon;
char my_endpoint[MAX_SOCKET_STRING];
setup_context_and_server_side ( setup_context_and_server_side (
&ctx, &handler, &zap_thread, &server, &server_mon, my_endpoint, &ctx, &handler, &zap_thread, &server, &server_mon, my_endpoint,
&zap_handler_wrong_status_temporary_failure, server_socket_config_, &zap_handler_wrong_status_temporary_failure, server_socket_config_,
...@@ -265,9 +286,15 @@ void test_zap_errors (socket_config_fn server_socket_config_, ...@@ -265,9 +286,15 @@ void test_zap_errors (socket_config_fn server_socket_config_,
client_socket_config_data_); client_socket_config_data_);
shutdown_context_and_server_side (ctx, zap_thread, server, server_mon, shutdown_context_and_server_side (ctx, zap_thread, server, server_mon,
handler); handler);
}
// status 500 internal error static void
fprintf (stderr, "test_zap_unsuccessful status 500\n"); test_zap_wrong_status_internal_error (socket_config_fn server_socket_config_,
socket_config_fn client_socket_config_,
void *client_socket_config_data_)
{
void *ctx, *handler, *zap_thread, *server, *server_mon;
char my_endpoint[MAX_SOCKET_STRING];
setup_context_and_server_side ( setup_context_and_server_side (
&ctx, &handler, &zap_thread, &server, &server_mon, my_endpoint, &ctx, &handler, &zap_thread, &server, &server_mon, my_endpoint,
&zap_handler_wrong_status_internal_error, server_socket_config_); &zap_handler_wrong_status_internal_error, server_socket_config_);
...@@ -276,11 +303,21 @@ void test_zap_errors (socket_config_fn server_socket_config_, ...@@ -276,11 +303,21 @@ void test_zap_errors (socket_config_fn server_socket_config_,
client_socket_config_data_); client_socket_config_data_);
shutdown_context_and_server_side (ctx, zap_thread, server, server_mon, shutdown_context_and_server_side (ctx, zap_thread, server, server_mon,
handler); handler);
}
static void
test_zap_unsuccesful_no_handler_started (socket_config_fn server_socket_config_,
socket_config_fn client_socket_config_,
void *client_socket_config_data_,
void *server_socket_config_data_)
{
#ifdef ZMQ_ZAP_ENFORCE_DOMAIN #ifdef ZMQ_ZAP_ENFORCE_DOMAIN
void *ctx, *handler, *zap_thread, *server, *server_mon;
char my_endpoint[MAX_SOCKET_STRING];
// TODO this looks wrong, where will the enforce value be used?
// no ZAP handler // no ZAP handler
int enforce = 1; int enforce = 1;
fprintf (stderr, "test_zap_unsuccessful no ZAP handler started\n");
setup_context_and_server_side ( setup_context_and_server_side (
&ctx, &handler, &zap_thread, &server, &server_mon, my_endpoint, NULL, &ctx, &handler, &zap_thread, &server, &server_mon, my_endpoint, NULL,
server_socket_config_, server_socket_config_,
...@@ -292,65 +329,164 @@ void test_zap_errors (socket_config_fn server_socket_config_, ...@@ -292,65 +329,164 @@ void test_zap_errors (socket_config_fn server_socket_config_,
shutdown_context_and_server_side (ctx, zap_thread, server, server_mon, shutdown_context_and_server_side (ctx, zap_thread, server, server_mon,
handler); handler);
#endif #endif
}
// ZAP handler disconnecting on first message static void
fprintf (stderr, "test_zap_unsuccessful ZAP handler disconnects\n"); test_zap_unsuccesful_no_handler_closure (socket_config_fn server_socket_config_,
setup_context_and_server_side ( socket_config_fn client_socket_config_,
&ctx, &handler, &zap_thread, &server, &server_mon, my_endpoint, void *client_socket_config_data_,
&zap_handler_disconnect, server_socket_config_); zmq_thread_fn zap_handler_func_,
bool zap_handler_disconnected_ = false)
{
void *ctx, *handler, *zap_thread, *server, *server_mon;
char my_endpoint[MAX_SOCKET_STRING];
setup_context_and_server_side (&ctx, &handler, &zap_thread, &server,
&server_mon, my_endpoint, zap_handler_func_,
server_socket_config_);
test_zap_unsuccessful_no_handler (ctx, my_endpoint, server, server_mon, test_zap_unsuccessful_no_handler (ctx, my_endpoint, server, server_mon,
ZMQ_EVENT_HANDSHAKE_FAILED_NO_DETAIL, ZMQ_EVENT_HANDSHAKE_FAILED_NO_DETAIL,
EPIPE, client_socket_config_, EPIPE, client_socket_config_,
client_socket_config_data_); client_socket_config_data_);
shutdown_context_and_server_side (ctx, zap_thread, server, server_mon, shutdown_context_and_server_side (ctx, zap_thread, server, server_mon,
handler, true); handler, zap_handler_disconnected_);
}
// ZAP handler does not read request static void
fprintf (stderr, test_zap_unsuccesful_disconnect (socket_config_fn server_socket_config_,
"test_zap_unsuccessful ZAP handler does not read request\n"); socket_config_fn client_socket_config_,
setup_context_and_server_side ( void *client_socket_config_data_)
&ctx, &handler, &zap_thread, &server, &server_mon, my_endpoint, {
&zap_handler_do_not_recv, server_socket_config_); test_zap_unsuccesful_no_handler_closure (
test_zap_unsuccessful_no_handler (ctx, my_endpoint, server, server_mon, server_socket_config_, client_socket_config_, client_socket_config_data_,
ZMQ_EVENT_HANDSHAKE_FAILED_NO_DETAIL, &zap_handler_disconnect, true);
EPIPE, client_socket_config_, }
client_socket_config_data_);
shutdown_context_and_server_side (ctx, zap_thread, server, server_mon,
handler);
// ZAP handler does not send reply static void
fprintf (stderr, test_zap_unsuccesful_do_not_recv (socket_config_fn server_socket_config_,
"test_zap_unsuccessful ZAP handler does not write reply\n"); socket_config_fn client_socket_config_,
setup_context_and_server_side ( void *client_socket_config_data_)
&ctx, &handler, &zap_thread, &server, &server_mon, my_endpoint, {
&zap_handler_do_not_send, server_socket_config_); test_zap_unsuccesful_no_handler_closure (
test_zap_unsuccessful_no_handler (ctx, my_endpoint, server, server_mon, server_socket_config_, client_socket_config_, client_socket_config_data_,
ZMQ_EVENT_HANDSHAKE_FAILED_NO_DETAIL, &zap_handler_do_not_recv);
EPIPE, client_socket_config_,
client_socket_config_data_);
shutdown_context_and_server_side (ctx, zap_thread, server, server_mon,
handler);
} }
int main (void) static void
test_zap_unsuccesful_do_not_send (socket_config_fn server_socket_config_,
socket_config_fn client_socket_config_,
void *client_socket_config_data_)
{ {
setup_test_environment (); test_zap_unsuccesful_no_handler_closure (
server_socket_config_, client_socket_config_, client_socket_config_data_,
&zap_handler_do_not_send);
}
#define DEFINE_ZAP_ERROR_TESTS( \
name_, server_socket_config_, server_socket_config_data_, \
client_socket_config_, client_socket_config_data_) \
void test_zap_protocol_error_wrong_version_##name_ () \
{ \
test_zap_protocol_error_wrong_version ( \
server_socket_config_, client_socket_config_, \
client_socket_config_data_, server_socket_config_data_); \
} \
void test_zap_protocol_error_wrong_request_id_##name_ () \
{ \
test_zap_protocol_error_wrong_request_id ( \
server_socket_config_, client_socket_config_, \
client_socket_config_data_, server_socket_config_data_); \
} \
void test_zap_protocol_error_wrong_status_invalid_##name_ () \
{ \
test_zap_protocol_error_wrong_status_invalid ( \
server_socket_config_, client_socket_config_, \
client_socket_config_data_, server_socket_config_data_); \
} \
void test_zap_protocol_error_too_many_parts_##name_ () \
{ \
test_zap_protocol_error_too_many_parts ( \
server_socket_config_, client_socket_config_, \
client_socket_config_data_, server_socket_config_data_); \
} \
void test_zap_wrong_status_temporary_failure_##name_ () \
{ \
test_zap_wrong_status_temporary_failure ( \
server_socket_config_, client_socket_config_, \
client_socket_config_data_, server_socket_config_data_); \
} \
void test_zap_wrong_status_internal_error_##name_ () \
{ \
test_zap_wrong_status_internal_error (server_socket_config_, \
client_socket_config_, \
client_socket_config_data_); \
} \
void test_zap_unsuccessful_no_handler_started_##name_ () \
{ \
test_zap_unsuccesful_no_handler_started ( \
server_socket_config_, client_socket_config_, \
client_socket_config_data_, server_socket_config_data_); \
} \
void test_zap_unsuccessful_disconnect_##name_ () \
{ \
test_zap_unsuccesful_disconnect (server_socket_config_, \
client_socket_config_, \
client_socket_config_data_); \
} \
void test_zap_unsuccessful_do_not_recv_##name_ () \
{ \
test_zap_unsuccesful_do_not_recv (server_socket_config_, \
client_socket_config_, \
client_socket_config_data_); \
} \
void test_zap_unsuccessful_do_not_send_##name_ () \
{ \
test_zap_unsuccesful_do_not_send (server_socket_config_, \
client_socket_config_, \
client_socket_config_data_); \
}
fprintf (stderr, "NULL mechanism\n"); DEFINE_ZAP_ERROR_TESTS (
test_zap_errors (&socket_config_null_server, NULL, null, &socket_config_null_server, NULL, &socket_config_null_client, NULL)
&socket_config_null_client, NULL);
DEFINE_ZAP_ERROR_TESTS (
plain, &socket_config_plain_server, NULL, &socket_config_plain_client, NULL)
static curve_client_data_t curve_client_data = {
valid_server_public, valid_client_public, valid_client_secret};
DEFINE_ZAP_ERROR_TESTS (curve,
&socket_config_curve_server,
valid_server_secret,
&socket_config_curve_client,
&curve_client_data)
#define RUN_ZAP_ERROR_TESTS(name_) \
{ \
RUN_TEST (test_zap_protocol_error_wrong_version_##name_); \
RUN_TEST (test_zap_protocol_error_wrong_request_id_##name_); \
RUN_TEST (test_zap_protocol_error_wrong_status_invalid_##name_); \
RUN_TEST (test_zap_protocol_error_too_many_parts_##name_); \
RUN_TEST (test_zap_wrong_status_temporary_failure_##name_); \
RUN_TEST (test_zap_wrong_status_internal_error_##name_); \
RUN_TEST (test_zap_unsuccessful_no_handler_started_##name_); \
RUN_TEST (test_zap_unsuccessful_disconnect_##name_); \
RUN_TEST (test_zap_unsuccessful_do_not_recv_##name_); \
RUN_TEST (test_zap_unsuccessful_do_not_send_##name_); \
}
fprintf (stderr, "PLAIN mechanism\n"); int main ()
test_zap_errors (&socket_config_plain_server, NULL, {
&socket_config_plain_client, NULL); setup_test_environment ();
if (zmq_has ("curve")) { if (zmq_has ("curve")) {
fprintf (stderr, "CURVE mechanism\n");
setup_testutil_security_curve (); setup_testutil_security_curve ();
}
curve_client_data_t curve_client_data = { UNITY_BEGIN ();
valid_server_public, valid_client_public, valid_client_secret}; RUN_ZAP_ERROR_TESTS (null);
test_zap_errors (&socket_config_curve_server, valid_server_secret, RUN_ZAP_ERROR_TESTS (plain);
&socket_config_curve_client, &curve_client_data); if (zmq_has ("curve")) {
RUN_ZAP_ERROR_TESTS (curve);
} }
return UNITY_END ();
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment