Unverified Commit f4b9cc99 authored by Luca Boccassi's avatar Luca Boccassi Committed by GitHub

Merge pull request #3457 from sigiesec/migrate-zap-test

Migration of further tests & test utilities to Unity
parents 99e53698 0266d4ac
...@@ -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}
......
...@@ -52,17 +52,6 @@ char error_message_buffer[256]; ...@@ -52,17 +52,6 @@ char error_message_buffer[256];
#define snprintf _snprintf #define snprintf _snprintf
#endif #endif
const char *zmq_errno_message ()
{
snprintf (error_message_buffer, sizeof (error_message_buffer),
"errno=%i (%s)", zmq_errno (), zmq_strerror (zmq_errno ()));
return error_message_buffer;
}
#define TEST_ASSERT_ZMQ_ERRNO(condition) \
TEST_ASSERT_MESSAGE ((condition), zmq_errno_message ())
void *ctx;
void *handler; void *handler;
void *zap_thread; void *zap_thread;
void *server; void *server;
...@@ -71,14 +60,15 @@ char my_endpoint[MAX_SOCKET_STRING]; ...@@ -71,14 +60,15 @@ char my_endpoint[MAX_SOCKET_STRING];
void setUp () void setUp ()
{ {
setup_context_and_server_side (&ctx, &handler, &zap_thread, &server, setup_test_context ();
&server_mon, my_endpoint); setup_context_and_server_side (&handler, &zap_thread, &server, &server_mon,
my_endpoint);
} }
void tearDown () void tearDown ()
{ {
shutdown_context_and_server_side (ctx, zap_thread, server, server_mon, shutdown_context_and_server_side (zap_thread, server, server_mon, handler);
handler); teardown_test_context ();
} }
const int timeout = 250; const int timeout = 250;
...@@ -91,13 +81,12 @@ const char large_routing_id[] = "0123456789012345678901234567890123456789" ...@@ -91,13 +81,12 @@ const char large_routing_id[] = "0123456789012345678901234567890123456789"
"0123456789012345678901234567890123456789" "0123456789012345678901234567890123456789"
"012345678901234"; "012345678901234";
static void zap_handler_large_routing_id (void *ctx_) static void zap_handler_large_routing_id (void * /*unused_*/)
{ {
zap_handler_generic (ctx_, zap_ok, large_routing_id); zap_handler_generic (zap_ok, large_routing_id);
} }
void expect_new_client_curve_bounce_fail (void *ctx_, void expect_new_client_curve_bounce_fail (char *server_public_,
char *server_public_,
char *client_public_, char *client_public_,
char *client_secret_, char *client_secret_,
char *my_endpoint_, char *my_endpoint_,
...@@ -109,20 +98,18 @@ void expect_new_client_curve_bounce_fail (void *ctx_, ...@@ -109,20 +98,18 @@ void expect_new_client_curve_bounce_fail (void *ctx_,
curve_client_data_t curve_client_data = {server_public_, client_public_, curve_client_data_t curve_client_data = {server_public_, client_public_,
client_secret_}; client_secret_};
expect_new_client_bounce_fail ( expect_new_client_bounce_fail (
ctx_, my_endpoint_, server_, socket_config_curve_client, my_endpoint_, server_, socket_config_curve_client, &curve_client_data,
&curve_client_data, client_mon_, expected_client_event_, client_mon_, expected_client_event_, expected_client_value_);
expected_client_value_);
} }
void test_null_key (void *ctx_, void test_null_key (void *server_,
void *server_,
void *server_mon_, void *server_mon_,
char *my_endpoint_, char *my_endpoint_,
char *server_public_, char *server_public_,
char *client_public_, char *client_public_,
char *client_secret_) char *client_secret_)
{ {
expect_new_client_curve_bounce_fail (ctx_, server_public_, client_public_, expect_new_client_curve_bounce_fail (server_public_, client_public_,
client_secret_, my_endpoint_, server_); client_secret_, my_endpoint_, server_);
int handshake_failed_encryption_event_count = int handshake_failed_encryption_event_count =
...@@ -148,12 +135,10 @@ void test_curve_security_with_valid_credentials () ...@@ -148,12 +135,10 @@ void test_curve_security_with_valid_credentials ()
curve_client_data_t curve_client_data = { curve_client_data_t curve_client_data = {
valid_server_public, valid_client_public, valid_client_secret}; valid_server_public, valid_client_public, valid_client_secret};
void *client_mon; void *client_mon;
void *client = void *client = create_and_connect_client (
create_and_connect_client (ctx, my_endpoint, socket_config_curve_client, my_endpoint, socket_config_curve_client, &curve_client_data, &client_mon);
&curve_client_data, &client_mon);
bounce (server, client); bounce (server, client);
int rc = zmq_close (client); test_context_socket_close (client);
TEST_ASSERT_ZMQ_ERRNO (rc == 0);
int event = get_monitor_event_with_timeout (server_mon, NULL, NULL, -1); int event = get_monitor_event_with_timeout (server_mon, NULL, NULL, -1);
assert (event == ZMQ_EVENT_HANDSHAKE_SUCCEEDED); assert (event == ZMQ_EVENT_HANDSHAKE_SUCCEEDED);
...@@ -165,8 +150,7 @@ void test_curve_security_with_valid_credentials () ...@@ -165,8 +150,7 @@ void test_curve_security_with_valid_credentials ()
assert_no_more_monitor_events_with_timeout (client_mon, timeout); assert_no_more_monitor_events_with_timeout (client_mon, timeout);
rc = zmq_close (client_mon); test_context_socket_close (client_mon);
TEST_ASSERT_ZMQ_ERRNO (rc == 0);
} }
void test_curve_security_with_bogus_client_credentials () void test_curve_security_with_bogus_client_credentials ()
...@@ -177,7 +161,7 @@ void test_curve_security_with_bogus_client_credentials () ...@@ -177,7 +161,7 @@ void test_curve_security_with_bogus_client_credentials ()
zmq_curve_keypair (bogus_public, bogus_secret); zmq_curve_keypair (bogus_public, bogus_secret);
expect_new_client_curve_bounce_fail ( expect_new_client_curve_bounce_fail (
ctx, valid_server_public, bogus_public, bogus_secret, my_endpoint, server, valid_server_public, bogus_public, bogus_secret, my_endpoint, server,
NULL, ZMQ_EVENT_HANDSHAKE_FAILED_AUTH, 400); NULL, ZMQ_EVENT_HANDSHAKE_FAILED_AUTH, 400);
int server_event_count = 0; int server_event_count = 0;
...@@ -196,10 +180,9 @@ void expect_zmtp_mechanism_mismatch (void *client_, ...@@ -196,10 +180,9 @@ void expect_zmtp_mechanism_mismatch (void *client_,
void *server_mon_) void *server_mon_)
{ {
// This must be caught by the curve_server class, not passed to ZAP // This must be caught by the curve_server class, not passed to ZAP
int rc = zmq_connect (client_, my_endpoint_); TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (client_, my_endpoint_));
TEST_ASSERT_ZMQ_ERRNO (rc == 0);
expect_bounce_fail (server_, client_); expect_bounce_fail (server_, client_);
close_zero_linger (client_); test_context_socket_close_zero_linger (client_);
expect_monitor_event_multiple (server_mon_, expect_monitor_event_multiple (server_mon_,
ZMQ_EVENT_HANDSHAKE_FAILED_PROTOCOL, ZMQ_EVENT_HANDSHAKE_FAILED_PROTOCOL,
...@@ -210,20 +193,18 @@ void expect_zmtp_mechanism_mismatch (void *client_, ...@@ -210,20 +193,18 @@ void expect_zmtp_mechanism_mismatch (void *client_,
void test_curve_security_with_null_client_credentials () void test_curve_security_with_null_client_credentials ()
{ {
void *client = zmq_socket (ctx, ZMQ_DEALER); void *client = test_context_socket (ZMQ_DEALER);
TEST_ASSERT_NOT_NULL (client);
expect_zmtp_mechanism_mismatch (client, my_endpoint, server, server_mon); expect_zmtp_mechanism_mismatch (client, my_endpoint, server, server_mon);
} }
void test_curve_security_with_plain_client_credentials () void test_curve_security_with_plain_client_credentials ()
{ {
void *client = zmq_socket (ctx, ZMQ_DEALER); void *client = test_context_socket (ZMQ_DEALER);
TEST_ASSERT_NOT_NULL (client); TEST_ASSERT_SUCCESS_ERRNO (
int rc = zmq_setsockopt (client, ZMQ_PLAIN_USERNAME, "admin", 5); zmq_setsockopt (client, ZMQ_PLAIN_USERNAME, "admin", 5));
TEST_ASSERT_ZMQ_ERRNO (rc == 0); TEST_ASSERT_SUCCESS_ERRNO (
rc = zmq_setsockopt (client, ZMQ_PLAIN_PASSWORD, "password", 8); zmq_setsockopt (client, ZMQ_PLAIN_PASSWORD, "password", 8));
TEST_ASSERT_ZMQ_ERRNO (rc == 0);
expect_zmtp_mechanism_mismatch (client, my_endpoint, server, server_mon); expect_zmtp_mechanism_mismatch (client, my_endpoint, server, server_mon);
} }
...@@ -368,8 +349,7 @@ void test_curve_security_invalid_hello_command_name () ...@@ -368,8 +349,7 @@ void test_curve_security_invalid_hello_command_name ()
// send CURVE HELLO with a misspelled command name (but otherwise correct) // send CURVE HELLO with a misspelled command name (but otherwise correct)
char hello[hello_length]; char hello[hello_length];
int rc = tools.produce_hello (hello, 0); TEST_ASSERT_SUCCESS_ERRNO (tools.produce_hello (hello, 0));
TEST_ASSERT_ZMQ_ERRNO (rc == 0);
hello[5] = 'X'; hello[5] = 'X';
send_command (s, hello); send_command (s, hello);
...@@ -391,8 +371,7 @@ void test_curve_security_invalid_hello_version () ...@@ -391,8 +371,7 @@ void test_curve_security_invalid_hello_version ()
// send CURVE HELLO with a wrong version number (but otherwise correct) // send CURVE HELLO with a wrong version number (but otherwise correct)
char hello[hello_length]; char hello[hello_length];
int rc = tools.produce_hello (hello, 0); TEST_ASSERT_SUCCESS_ERRNO (tools.produce_hello (hello, 0));
TEST_ASSERT_ZMQ_ERRNO (rc == 0);
hello[6] = 2; hello[6] = 2;
send_command (s, hello); send_command (s, hello);
...@@ -443,8 +422,7 @@ fd_t connect_exchange_greeting_and_send_hello ( ...@@ -443,8 +422,7 @@ fd_t connect_exchange_greeting_and_send_hello (
// send valid CURVE HELLO // send valid CURVE HELLO
char hello[hello_length]; char hello[hello_length];
int rc = tools_.produce_hello (hello, 0); TEST_ASSERT_SUCCESS_ERRNO (tools_.produce_hello (hello, 0));
TEST_ASSERT_ZMQ_ERRNO (rc == 0);
send_command (s, hello); send_command (s, hello);
return s; return s;
...@@ -484,10 +462,11 @@ fd_t connect_exchange_greeting_and_hello_welcome ( ...@@ -484,10 +462,11 @@ fd_t connect_exchange_greeting_and_hello_welcome (
recv_all (s, welcome, welcome_length + 2); recv_all (s, welcome, welcome_length + 2);
uint8_t cn_precom[crypto_box_BEFORENMBYTES]; uint8_t cn_precom[crypto_box_BEFORENMBYTES];
int res = tools_.process_welcome (welcome + 2, welcome_length, cn_precom); TEST_ASSERT_SUCCESS_ERRNO (
TEST_ASSERT_ZMQ_ERRNO (res == 0); tools_.process_welcome (welcome + 2, welcome_length, cn_precom));
res = get_monitor_event_with_timeout (server_mon_, NULL, NULL, timeout_); const int res =
get_monitor_event_with_timeout (server_mon_, NULL, NULL, timeout_);
TEST_ASSERT_EQUAL_INT (-1, res); TEST_ASSERT_EQUAL_INT (-1, res);
return s; return s;
...@@ -568,8 +547,7 @@ void test_curve_security_invalid_keysize (void *ctx_) ...@@ -568,8 +547,7 @@ void test_curve_security_invalid_keysize (void *ctx_)
errno = 0; errno = 0;
rc = zmq_setsockopt (client, ZMQ_CURVE_SECRETKEY, valid_client_secret, 123); rc = zmq_setsockopt (client, ZMQ_CURVE_SECRETKEY, valid_client_secret, 123);
assert (rc == -1 && errno == EINVAL); assert (rc == -1 && errno == EINVAL);
rc = zmq_close (client); TEST_ASSERT_SUCCESS_ERRNO (zmq_close (client));
TEST_ASSERT_ZMQ_ERRNO (rc == 0);
} }
// TODO why isn't this const? // TODO why isn't this const?
...@@ -579,7 +557,7 @@ void test_null_server_key () ...@@ -579,7 +557,7 @@ void test_null_server_key ()
{ {
// Check CURVE security with a null server key // Check CURVE security with a null server key
// This will be caught by the curve_server class, not passed to ZAP // This will be caught by the curve_server class, not passed to ZAP
test_null_key (ctx, server, server_mon, my_endpoint, null_key, test_null_key (server, server_mon, my_endpoint, null_key,
valid_client_public, valid_client_secret); valid_client_public, valid_client_secret);
} }
...@@ -587,7 +565,7 @@ void test_null_client_public_key () ...@@ -587,7 +565,7 @@ void test_null_client_public_key ()
{ {
// Check CURVE security with a null client public key // Check CURVE security with a null client public key
// This will be caught by the curve_server class, not passed to ZAP // This will be caught by the curve_server class, not passed to ZAP
test_null_key (ctx, server, server_mon, my_endpoint, valid_server_public, test_null_key (server, server_mon, my_endpoint, valid_server_public,
null_key, valid_client_secret); null_key, valid_client_secret);
} }
...@@ -595,7 +573,7 @@ void test_null_client_secret_key () ...@@ -595,7 +573,7 @@ void test_null_client_secret_key ()
{ {
// Check CURVE security with a null client public key // Check CURVE security with a null client public key
// This will be caught by the curve_server class, not passed to ZAP // This will be caught by the curve_server class, not passed to ZAP
test_null_key (ctx, server, server_mon, my_endpoint, valid_server_public, test_null_key (server, server_mon, my_endpoint, valid_server_public,
valid_client_public, null_key); valid_client_public, null_key);
} }
...@@ -637,18 +615,18 @@ int main (void) ...@@ -637,18 +615,18 @@ int main (void)
// test with a large routing id (resulting in large metadata) // test with a large routing id (resulting in large metadata)
fprintf (stderr, fprintf (stderr,
"test_curve_security_with_valid_credentials (large routing id)\n"); "test_curve_security_with_valid_credentials (large routing id)\n");
setup_context_and_server_side ( setup_test_context ();
&ctx, &handler, &zap_thread, &server, &server_mon, my_endpoint, setup_context_and_server_side (&handler, &zap_thread, &server, &server_mon,
&zap_handler_large_routing_id, &socket_config_curve_server, my_endpoint, &zap_handler_large_routing_id,
&socket_config_curve_server,
&valid_server_secret, large_routing_id); &valid_server_secret, large_routing_id);
test_curve_security_with_valid_credentials (); test_curve_security_with_valid_credentials ();
shutdown_context_and_server_side (ctx, zap_thread, server, server_mon, shutdown_context_and_server_side (zap_thread, server, server_mon, handler);
handler); teardown_test_context ();
ctx = zmq_ctx_new (); void *ctx = zmq_ctx_new ();
test_curve_security_invalid_keysize (ctx); test_curve_security_invalid_keysize (ctx);
int rc = zmq_ctx_term (ctx); TEST_ASSERT_SUCCESS_ERRNO (zmq_ctx_term (ctx));
TEST_ASSERT_ZMQ_ERRNO (rc == 0);
zmq::random_close (); zmq::random_close ();
......
...@@ -28,54 +28,64 @@ ...@@ -28,54 +28,64 @@
*/ */
#include "testutil_security.hpp" #include "testutil_security.hpp"
#include "testutil_unity.hpp"
static void zap_handler_wrong_version (void *ctx_) void setUp ()
{ {
zap_handler_generic (ctx_, zap_wrong_version); setup_test_context ();
} }
static void zap_handler_wrong_request_id (void *ctx_) void tearDown ()
{ {
zap_handler_generic (ctx_, zap_wrong_request_id); teardown_test_context ();
} }
static void zap_handler_wrong_status_invalid (void *ctx_) static void zap_handler_wrong_version (void * /*unused_*/)
{ {
zap_handler_generic (ctx_, zap_status_invalid); zap_handler_generic (zap_wrong_version);
} }
static void zap_handler_wrong_status_temporary_failure (void *ctx_) static void zap_handler_wrong_request_id (void * /*unused_*/)
{ {
zap_handler_generic (ctx_, zap_status_temporary_failure); zap_handler_generic (zap_wrong_request_id);
} }
static void zap_handler_wrong_status_internal_error (void *ctx_) static void zap_handler_wrong_status_invalid (void * /*unused_*/)
{ {
zap_handler_generic (ctx_, zap_status_internal_error); zap_handler_generic (zap_status_invalid);
} }
static void zap_handler_too_many_parts (void *ctx_) static void zap_handler_wrong_status_temporary_failure (void * /*unused_*/)
{ {
zap_handler_generic (ctx_, zap_too_many_parts); zap_handler_generic (zap_status_temporary_failure);
} }
static void zap_handler_disconnect (void *ctx_) static void zap_handler_wrong_status_internal_error (void * /*unused_*/)
{ {
zap_handler_generic (ctx_, zap_disconnect); zap_handler_generic (zap_status_internal_error);
} }
static void zap_handler_do_not_recv (void *ctx_) static void zap_handler_too_many_parts (void * /*unused_*/)
{ {
zap_handler_generic (ctx_, zap_do_not_recv); zap_handler_generic (zap_too_many_parts);
} }
static void zap_handler_do_not_send (void *ctx_) static void zap_handler_disconnect (void * /*unused_*/)
{ {
zap_handler_generic (ctx_, zap_do_not_send); zap_handler_generic (zap_disconnect);
}
static void zap_handler_do_not_recv (void * /*unused_*/)
{
zap_handler_generic (zap_do_not_recv);
}
static void zap_handler_do_not_send (void * /*unused_*/)
{
zap_handler_generic (zap_do_not_send);
} }
int expect_new_client_bounce_fail_and_count_monitor_events ( int expect_new_client_bounce_fail_and_count_monitor_events (
void *ctx_,
char *my_endpoint_, char *my_endpoint_,
void *server_, void *server_,
socket_config_fn socket_config_, socket_config_fn socket_config_,
...@@ -88,8 +98,8 @@ int expect_new_client_bounce_fail_and_count_monitor_events ( ...@@ -88,8 +98,8 @@ int expect_new_client_bounce_fail_and_count_monitor_events (
int expected_client_value_ = 0) int expected_client_value_ = 0)
{ {
expect_new_client_bounce_fail ( expect_new_client_bounce_fail (
ctx_, my_endpoint_, server_, socket_config_, socket_config_data_, my_endpoint_, server_, socket_config_, socket_config_data_, client_mon_,
client_mon_, expected_client_event_, expected_client_value_); expected_client_event_, expected_client_value_);
int events_received = 0; int events_received = 0;
events_received = expect_monitor_event_multiple ( events_received = expect_monitor_event_multiple (
...@@ -98,8 +108,7 @@ int expect_new_client_bounce_fail_and_count_monitor_events ( ...@@ -98,8 +108,7 @@ int expect_new_client_bounce_fail_and_count_monitor_events (
return events_received; return events_received;
} }
void test_zap_unsuccessful (void *ctx_, void test_zap_unsuccessful (char *my_endpoint_,
char *my_endpoint_,
void *server_, void *server_,
void *server_mon_, void *server_mon_,
int expected_server_event_, int expected_server_event_,
...@@ -112,18 +121,17 @@ void test_zap_unsuccessful (void *ctx_, ...@@ -112,18 +121,17 @@ void test_zap_unsuccessful (void *ctx_,
{ {
int server_events_received = int server_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_, my_endpoint_, server_, socket_config_, socket_config_data_, client_mon_,
client_mon_, server_mon_, expected_server_event_, server_mon_, expected_server_event_, expected_server_value_,
expected_server_value_, expected_client_event_, expected_client_value_); expected_client_event_, expected_client_value_);
// 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 (char *my_endpoint_,
char *my_endpoint_,
void *server_, void *server_,
void *server_mon_, void *server_mon_,
int expected_event_, int expected_event_,
...@@ -132,31 +140,29 @@ void test_zap_unsuccessful_no_handler (void *ctx_, ...@@ -132,31 +140,29 @@ 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_, my_endpoint_, server_, socket_config_, socket_config_data_, client_mon_,
client_mon_, server_mon_, expected_event_, expected_err_); 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 (char *my_endpoint_,
char *my_endpoint_,
void *server_, void *server_,
void *server_mon_, void *server_mon_,
socket_config_fn socket_config_, socket_config_fn socket_config_,
void *socket_config_data_, void *socket_config_data_,
int expected_error_) int expected_error_)
{ {
test_zap_unsuccessful (ctx_, my_endpoint_, server_, server_mon_, test_zap_unsuccessful (my_endpoint_, server_, server_mon_,
ZMQ_EVENT_HANDSHAKE_FAILED_PROTOCOL, expected_error_, ZMQ_EVENT_HANDSHAKE_FAILED_PROTOCOL, expected_error_,
socket_config_, socket_config_data_); socket_config_, socket_config_data_);
} }
void test_zap_unsuccessful_status_300 (void *ctx_, void test_zap_unsuccessful_status_300 (char *my_endpoint_,
char *my_endpoint_,
void *server_, void *server_,
void *server_mon_, void *server_mon_,
socket_config_fn client_socket_config_, socket_config_fn client_socket_config_,
...@@ -164,193 +170,311 @@ void test_zap_unsuccessful_status_300 (void *ctx_, ...@@ -164,193 +170,311 @@ void test_zap_unsuccessful_status_300 (void *ctx_,
{ {
void *client_mon; void *client_mon;
test_zap_unsuccessful ( test_zap_unsuccessful (
ctx_, my_endpoint_, server_, server_mon_, ZMQ_EVENT_HANDSHAKE_FAILED_AUTH, my_endpoint_, server_, server_mon_, ZMQ_EVENT_HANDSHAKE_FAILED_AUTH, 300,
300, client_socket_config_, client_socket_config_data_, &client_mon); client_socket_config_, client_socket_config_data_, &client_mon);
// 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_context_socket_close (client_mon);
assert (rc == 0);
} }
void test_zap_unsuccessful_status_500 (void *ctx_, void test_zap_unsuccessful_status_500 (char *my_endpoint_,
char *my_endpoint_,
void *server_, void *server_,
void *server_mon_, void *server_mon_,
socket_config_fn client_socket_config_, socket_config_fn client_socket_config_,
void *client_socket_config_data_) void *client_socket_config_data_)
{ {
test_zap_unsuccessful (ctx_, my_endpoint_, server_, server_mon_, test_zap_unsuccessful (my_endpoint_, server_, server_mon_,
ZMQ_EVENT_HANDSHAKE_FAILED_AUTH, 500, ZMQ_EVENT_HANDSHAKE_FAILED_AUTH, 500,
client_socket_config_, client_socket_config_data_, client_socket_config_, client_socket_config_data_,
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 *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, &handler, &zap_thread, &server, &server_mon, my_endpoint, zap_handler_,
&zap_handler_wrong_version, server_socket_config_, server_socket_config_, server_socket_config_data_);
server_socket_config_data_); test_zap_protocol_error (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_,
expected_failure_);
shutdown_context_and_server_side (zap_thread, server, server_mon, handler);
}
static void
test_zap_protocol_error_wrong_version (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_wrong_version,
ZMQ_PROTOCOL_ERROR_ZAP_BAD_VERSION); ZMQ_PROTOCOL_ERROR_ZAP_BAD_VERSION);
shutdown_context_and_server_side (ctx, zap_thread, server, server_mon, }
handler);
// wrong request id static void test_zap_protocol_error_wrong_request_id (
fprintf (stderr, "test_zap_protocol_error wrong_request_id\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_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_,
server_socket_config_data_, &zap_handler_wrong_request_id,
ZMQ_PROTOCOL_ERROR_ZAP_BAD_REQUEST_ID); ZMQ_PROTOCOL_ERROR_ZAP_BAD_REQUEST_ID);
shutdown_context_and_server_side (ctx, zap_thread, server, server_mon, }
handler);
// status invalid (not a 3-digit number) static void test_zap_protocol_error_wrong_status_invalid (
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_,
server_socket_config_data_, &zap_handler_wrong_status_invalid,
ZMQ_PROTOCOL_ERROR_ZAP_INVALID_STATUS_CODE); ZMQ_PROTOCOL_ERROR_ZAP_INVALID_STATUS_CODE);
shutdown_context_and_server_side (ctx, zap_thread, server, server_mon, }
handler);
// too many parts static void
fprintf (stderr, "test_zap_protocol_error too_many_parts\n"); test_zap_protocol_error_too_many_parts (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_,
server_socket_config_data_, &zap_handler_too_many_parts,
ZMQ_PROTOCOL_ERROR_ZAP_MALFORMED_REPLY); ZMQ_PROTOCOL_ERROR_ZAP_MALFORMED_REPLY);
shutdown_context_and_server_side (ctx, zap_thread, server, server_mon, }
handler);
// ZAP non-standard cases
// 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 *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, &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_,
server_socket_config_data_); server_socket_config_data_);
test_zap_unsuccessful_status_300 (ctx, my_endpoint, server, server_mon, test_zap_unsuccessful_status_300 (my_endpoint, server, server_mon,
client_socket_config_, 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 (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 *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, &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_);
test_zap_unsuccessful_status_500 (ctx, my_endpoint, server, server_mon, test_zap_unsuccessful_status_500 (my_endpoint, server, server_mon,
client_socket_config_, 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 (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 *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, &handler, &zap_thread, &server, &server_mon, my_endpoint, NULL,
server_socket_config_, server_socket_config_,
server_socket_config_data_ ? server_socket_config_data_ : &enforce); server_socket_config_data_ ? server_socket_config_data_ : &enforce);
test_zap_unsuccessful_no_handler (ctx, my_endpoint, server, server_mon, test_zap_unsuccessful_no_handler (
ZMQ_EVENT_HANDSHAKE_FAILED_NO_DETAIL, my_endpoint, server, server_mon, ZMQ_EVENT_HANDSHAKE_FAILED_NO_DETAIL,
EFAULT, client_socket_config_, EFAULT, client_socket_config_, client_socket_config_data_);
client_socket_config_data_); shutdown_context_and_server_side (zap_thread, server, server_mon, handler);
shutdown_context_and_server_side (ctx, zap_thread, server, server_mon,
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_,
test_zap_unsuccessful_no_handler (ctx, my_endpoint, server, server_mon, bool zap_handler_disconnected_ = false)
ZMQ_EVENT_HANDSHAKE_FAILED_NO_DETAIL, {
EPIPE, client_socket_config_, void *handler, *zap_thread, *server, *server_mon;
client_socket_config_data_); char my_endpoint[MAX_SOCKET_STRING];
shutdown_context_and_server_side (ctx, zap_thread, server, server_mon, setup_context_and_server_side (&handler, &zap_thread, &server, &server_mon,
handler, true); my_endpoint, zap_handler_func_,
server_socket_config_);
test_zap_unsuccessful_no_handler (
my_endpoint, server, server_mon, ZMQ_EVENT_HANDSHAKE_FAILED_NO_DETAIL,
EPIPE, client_socket_config_, client_socket_config_data_);
shutdown_context_and_server_side (zap_thread, server, server_mon, 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_); \
}
DEFINE_ZAP_ERROR_TESTS (
null, &socket_config_null_server, NULL, &socket_config_null_client, NULL)
DEFINE_ZAP_ERROR_TESTS (
plain, &socket_config_plain_server, NULL, &socket_config_plain_client, NULL)
fprintf (stderr, "NULL mechanism\n"); static curve_client_data_t curve_client_data = {
test_zap_errors (&socket_config_null_server, NULL, valid_server_public, valid_client_public, valid_client_secret};
&socket_config_null_client, NULL);
fprintf (stderr, "PLAIN mechanism\n"); DEFINE_ZAP_ERROR_TESTS (curve,
test_zap_errors (&socket_config_plain_server, NULL, &socket_config_curve_server,
&socket_config_plain_client, NULL); 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_); \
}
int main ()
{
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 ();
} }
...@@ -30,7 +30,7 @@ ...@@ -30,7 +30,7 @@
#ifndef __TESTUTIL_MONITORING_HPP_INCLUDED__ #ifndef __TESTUTIL_MONITORING_HPP_INCLUDED__
#define __TESTUTIL_MONITORING_HPP_INCLUDED__ #define __TESTUTIL_MONITORING_HPP_INCLUDED__
#include "testutil.hpp" #include "testutil_unity.hpp"
// General, i.e. non-security specific, monitor utilities // General, i.e. non-security specific, monitor utilities
...@@ -47,10 +47,10 @@ static int get_monitor_event_internal (void *monitor_, ...@@ -47,10 +47,10 @@ static int get_monitor_event_internal (void *monitor_,
zmq_msg_t msg; zmq_msg_t msg;
zmq_msg_init (&msg); zmq_msg_init (&msg);
if (zmq_msg_recv (&msg, monitor_, recv_flag_) == -1) { if (zmq_msg_recv (&msg, monitor_, recv_flag_) == -1) {
assert (errno == EAGAIN); TEST_ASSERT_FAILURE_ERRNO (EAGAIN, -1);
return -1; // timed out or no message available return -1; // timed out or no message available
} }
assert (zmq_msg_more (&msg)); TEST_ASSERT_TRUE (zmq_msg_more (&msg));
uint8_t *data = (uint8_t *) zmq_msg_data (&msg); uint8_t *data = (uint8_t *) zmq_msg_data (&msg);
uint16_t event = *(uint16_t *) (data); uint16_t event = *(uint16_t *) (data);
...@@ -59,9 +59,8 @@ static int get_monitor_event_internal (void *monitor_, ...@@ -59,9 +59,8 @@ static int get_monitor_event_internal (void *monitor_,
// Second frame in message contains event address // Second frame in message contains event address
zmq_msg_init (&msg); zmq_msg_init (&msg);
int res = zmq_msg_recv (&msg, monitor_, recv_flag_) == -1; TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_recv (&msg, monitor_, recv_flag_));
assert (res != -1); TEST_ASSERT_FALSE (zmq_msg_more (&msg));
assert (!zmq_msg_more (&msg));
if (address_) { if (address_) {
uint8_t *data = (uint8_t *) zmq_msg_data (&msg); uint8_t *data = (uint8_t *) zmq_msg_data (&msg);
...@@ -111,23 +110,33 @@ int get_monitor_event (void *monitor_, int *value_, char **address_) ...@@ -111,23 +110,33 @@ int get_monitor_event (void *monitor_, int *value_, char **address_)
void expect_monitor_event (void *monitor_, int expected_event_) void expect_monitor_event (void *monitor_, int expected_event_)
{ {
int event = get_monitor_event (monitor_, NULL, NULL); TEST_ASSERT_EQUAL_HEX (expected_event_,
if (event != expected_event_) { get_monitor_event (monitor_, NULL, NULL));
fprintf (stderr, "Expected monitor event %x but received %x\n",
expected_event_, event);
assert (event == expected_event_);
}
} }
void print_unexpected_event (int event_, void print_unexpected_event (char *buf_,
size_t buf_size_,
int event_,
int err_, int err_,
int expected_event_, int expected_event_,
int expected_err_) int expected_err_)
{ {
fprintf (stderr, snprintf (buf_, buf_size_,
"Unexpected event: 0x%x, value = %i/0x%x (expected: 0x%x, value " "Unexpected event: 0x%x, value = %i/0x%x (expected: 0x%x, value "
"= %i/0x%x)\n", "= %i/0x%x)\n",
event_, err_, err_, expected_event_, expected_err_, expected_err_); event_, err_, err_, expected_event_, expected_err_,
expected_err_);
}
void print_unexpected_event_stderr (int event_,
int err_,
int expected_event_,
int expected_err_)
{
char buf[256];
print_unexpected_event (buf, sizeof buf, event_, err_, expected_event_,
expected_err_);
fputs (buf, stderr);
} }
// expects that one or more occurrences of the expected event are received // expects that one or more occurrences of the expected event are received
...@@ -179,12 +188,14 @@ int expect_monitor_event_multiple (void *server_mon_, ...@@ -179,12 +188,14 @@ int expect_monitor_event_multiple (void *server_mon_,
} }
if (event != expected_event_ if (event != expected_event_
|| (-1 != expected_err_ && err != expected_err_)) { || (-1 != expected_err_ && err != expected_err_)) {
print_unexpected_event (event, err, expected_event_, expected_err_); char buf[256];
assert (false); print_unexpected_event (buf, sizeof buf, event, err,
expected_event_, expected_err_);
TEST_FAIL_MESSAGE (buf);
} }
++count_of_expected_events; ++count_of_expected_events;
} }
assert (optional_ || count_of_expected_events > 0 TEST_ASSERT_TRUE (optional_ || count_of_expected_events > 0
|| client_closed_connection); || client_closed_connection);
return count_of_expected_events; return count_of_expected_events;
...@@ -203,11 +214,11 @@ static int64_t get_monitor_event_internal_v2 (void *monitor_, ...@@ -203,11 +214,11 @@ static int64_t get_monitor_event_internal_v2 (void *monitor_,
zmq_msg_t msg; zmq_msg_t msg;
zmq_msg_init (&msg); zmq_msg_init (&msg);
if (zmq_msg_recv (&msg, monitor_, recv_flag_) == -1) { if (zmq_msg_recv (&msg, monitor_, recv_flag_) == -1) {
assert (errno == EAGAIN); TEST_ASSERT_FAILURE_ERRNO (EAGAIN, -1);
return -1; // timed out or no message available return -1; // timed out or no message available
} }
assert (zmq_msg_more (&msg)); TEST_ASSERT_TRUE (zmq_msg_more (&msg));
assert (sizeof (uint64_t) == zmq_msg_size (&msg)); TEST_ASSERT_EQUAL_UINT (sizeof (uint64_t), zmq_msg_size (&msg));
uint64_t event; uint64_t event;
memcpy (&event, zmq_msg_data (&msg), sizeof (event)); memcpy (&event, zmq_msg_data (&msg), sizeof (event));
...@@ -216,11 +227,11 @@ static int64_t get_monitor_event_internal_v2 (void *monitor_, ...@@ -216,11 +227,11 @@ static int64_t get_monitor_event_internal_v2 (void *monitor_,
// Second frame in message contains the number of values // Second frame in message contains the number of values
zmq_msg_init (&msg); zmq_msg_init (&msg);
if (zmq_msg_recv (&msg, monitor_, recv_flag_) == -1) { if (zmq_msg_recv (&msg, monitor_, recv_flag_) == -1) {
assert (errno == EAGAIN); TEST_ASSERT_FAILURE_ERRNO (EAGAIN, -1);
return -1; // timed out or no message available return -1; // timed out or no message available
} }
assert (zmq_msg_more (&msg)); TEST_ASSERT_TRUE (zmq_msg_more (&msg));
assert (sizeof (uint64_t) == zmq_msg_size (&msg)); TEST_ASSERT_EQUAL_UINT (sizeof (uint64_t), zmq_msg_size (&msg));
uint64_t value_count; uint64_t value_count;
memcpy (&value_count, zmq_msg_data (&msg), sizeof (value_count)); memcpy (&value_count, zmq_msg_data (&msg), sizeof (value_count));
...@@ -230,11 +241,11 @@ static int64_t get_monitor_event_internal_v2 (void *monitor_, ...@@ -230,11 +241,11 @@ static int64_t get_monitor_event_internal_v2 (void *monitor_,
// Subsequent frames in message contain event values // Subsequent frames in message contain event values
zmq_msg_init (&msg); zmq_msg_init (&msg);
if (zmq_msg_recv (&msg, monitor_, recv_flag_) == -1) { if (zmq_msg_recv (&msg, monitor_, recv_flag_) == -1) {
assert (errno == EAGAIN); TEST_ASSERT_FAILURE_ERRNO (EAGAIN, -1);
return -1; // timed out or no message available return -1; // timed out or no message available
} }
assert (zmq_msg_more (&msg)); TEST_ASSERT_TRUE (zmq_msg_more (&msg));
assert (sizeof (uint64_t) == zmq_msg_size (&msg)); TEST_ASSERT_EQUAL_UINT (sizeof (uint64_t), zmq_msg_size (&msg));
if (value_ && value_ + i) if (value_ && value_ + i)
memcpy (value_ + i, zmq_msg_data (&msg), sizeof (*value_)); memcpy (value_ + i, zmq_msg_data (&msg), sizeof (*value_));
...@@ -243,9 +254,8 @@ static int64_t get_monitor_event_internal_v2 (void *monitor_, ...@@ -243,9 +254,8 @@ static int64_t get_monitor_event_internal_v2 (void *monitor_,
// Second-to-last frame in message contains local address // Second-to-last frame in message contains local address
zmq_msg_init (&msg); zmq_msg_init (&msg);
int res = zmq_msg_recv (&msg, monitor_, recv_flag_) == -1; TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_recv (&msg, monitor_, recv_flag_));
assert (res != -1); TEST_ASSERT_TRUE (zmq_msg_more (&msg));
assert (zmq_msg_more (&msg));
if (local_address_) { if (local_address_) {
uint8_t *data = (uint8_t *) zmq_msg_data (&msg); uint8_t *data = (uint8_t *) zmq_msg_data (&msg);
...@@ -258,9 +268,8 @@ static int64_t get_monitor_event_internal_v2 (void *monitor_, ...@@ -258,9 +268,8 @@ static int64_t get_monitor_event_internal_v2 (void *monitor_,
// Last frame in message contains remote address // Last frame in message contains remote address
zmq_msg_init (&msg); zmq_msg_init (&msg);
res = zmq_msg_recv (&msg, monitor_, recv_flag_) == -1; TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_recv (&msg, monitor_, recv_flag_));
assert (res != -1); TEST_ASSERT_TRUE (!zmq_msg_more (&msg));
assert (!zmq_msg_more (&msg));
if (remote_address_) { if (remote_address_) {
uint8_t *data = (uint8_t *) zmq_msg_data (&msg); uint8_t *data = (uint8_t *) zmq_msg_data (&msg);
...@@ -326,24 +335,29 @@ void expect_monitor_event_v2 (void *monitor_, ...@@ -326,24 +335,29 @@ void expect_monitor_event_v2 (void *monitor_,
monitor_, NULL, expected_local_address_ ? &local_address : NULL, monitor_, NULL, expected_local_address_ ? &local_address : NULL,
expected_remote_address_ ? &remote_address : NULL); expected_remote_address_ ? &remote_address : NULL);
bool failed = false; bool failed = false;
char buf[256];
char *pos = buf;
if (event != expected_event_) { if (event != expected_event_) {
fprintf (stderr, "Expected monitor event %llx, but received %llx\n", pos += snprintf (pos, sizeof buf - (pos - buf),
"Expected monitor event %llx, but received %llx\n",
(long long) expected_event_, (long long) event); (long long) expected_event_, (long long) event);
failed = true; failed = true;
} }
if (expected_local_address_ if (expected_local_address_
&& 0 != strcmp (local_address, expected_local_address_)) { && 0 != strcmp (local_address, expected_local_address_)) {
fprintf (stderr, "Expected local address %s, but received %s\n", pos += snprintf (pos, sizeof buf - (pos - buf),
"Expected local address %s, but received %s\n",
expected_local_address_, local_address); expected_local_address_, local_address);
} }
if (expected_remote_address_ if (expected_remote_address_
&& 0 != strcmp (remote_address, expected_remote_address_)) { && 0 != strcmp (remote_address, expected_remote_address_)) {
fprintf (stderr, "Expected remote address %s, but received %s\n", pos += snprintf (pos, sizeof buf - (pos - buf),
"Expected remote address %s, but received %s\n",
expected_remote_address_, remote_address); expected_remote_address_, remote_address);
} }
free (local_address); free (local_address);
free (remote_address); free (remote_address);
assert (!failed); TEST_ASSERT_FALSE_MESSAGE (failed, buf);
} }
#endif #endif
......
...@@ -30,7 +30,7 @@ ...@@ -30,7 +30,7 @@
#ifndef __TESTUTIL_SECURITY_HPP_INCLUDED__ #ifndef __TESTUTIL_SECURITY_HPP_INCLUDED__
#define __TESTUTIL_SECURITY_HPP_INCLUDED__ #define __TESTUTIL_SECURITY_HPP_INCLUDED__
#include "testutil.hpp" #include "testutil_unity.hpp"
#include "testutil_monitoring.hpp" #include "testutil_monitoring.hpp"
// security test utils // security test utils
...@@ -48,14 +48,12 @@ void socket_config_null_client (void *server_, void *server_secret_) ...@@ -48,14 +48,12 @@ void socket_config_null_client (void *server_, void *server_secret_)
void socket_config_null_server (void *server_, void *server_secret_) void socket_config_null_server (void *server_, void *server_secret_)
{ {
int rc = zmq_setsockopt (server_, ZMQ_ZAP_DOMAIN, test_zap_domain, TEST_ASSERT_SUCCESS_ERRNO (zmq_setsockopt (
strlen (test_zap_domain)); server_, ZMQ_ZAP_DOMAIN, test_zap_domain, strlen (test_zap_domain)));
assert (rc == 0);
#ifdef ZMQ_ZAP_ENFORCE_DOMAIN #ifdef ZMQ_ZAP_ENFORCE_DOMAIN
int required = server_secret_ ? *(int *) server_secret_ : 0; int required = server_secret_ ? *(int *) server_secret_ : 0;
rc = TEST_ASSERT_SUCCESS_ERRNO (zmq_setsockopt (server_, ZMQ_ZAP_ENFORCE_DOMAIN,
zmq_setsockopt (server_, ZMQ_ZAP_ENFORCE_DOMAIN, &required, sizeof (int)); &required, sizeof (int)));
assert (rc == 0);
#else #else
LIBZMQ_UNUSED (server_secret_); LIBZMQ_UNUSED (server_secret_);
#endif #endif
...@@ -69,12 +67,10 @@ void socket_config_plain_client (void *server_, void *server_secret_) ...@@ -69,12 +67,10 @@ void socket_config_plain_client (void *server_, void *server_secret_)
{ {
LIBZMQ_UNUSED (server_secret_); LIBZMQ_UNUSED (server_secret_);
int rc = TEST_ASSERT_SUCCESS_ERRNO (
zmq_setsockopt (server_, ZMQ_PLAIN_PASSWORD, test_plain_password, 8); zmq_setsockopt (server_, ZMQ_PLAIN_PASSWORD, test_plain_password, 8));
assert (rc == 0); TEST_ASSERT_SUCCESS_ERRNO (
zmq_setsockopt (server_, ZMQ_PLAIN_USERNAME, test_plain_username, 8));
rc = zmq_setsockopt (server_, ZMQ_PLAIN_USERNAME, test_plain_username, 8);
assert (rc == 0);
} }
void socket_config_plain_server (void *server_, void *server_secret_) void socket_config_plain_server (void *server_, void *server_secret_)
...@@ -82,13 +78,10 @@ void socket_config_plain_server (void *server_, void *server_secret_) ...@@ -82,13 +78,10 @@ void socket_config_plain_server (void *server_, void *server_secret_)
LIBZMQ_UNUSED (server_secret_); LIBZMQ_UNUSED (server_secret_);
int as_server = 1; int as_server = 1;
int rc = TEST_ASSERT_SUCCESS_ERRNO (
zmq_setsockopt (server_, ZMQ_PLAIN_SERVER, &as_server, sizeof (int)); zmq_setsockopt (server_, ZMQ_PLAIN_SERVER, &as_server, sizeof (int)));
assert (rc == 0); TEST_ASSERT_SUCCESS_ERRNO (zmq_setsockopt (
server_, ZMQ_ZAP_DOMAIN, test_zap_domain, strlen (test_zap_domain)));
rc = zmq_setsockopt (server_, ZMQ_ZAP_DOMAIN, test_zap_domain,
strlen (test_zap_domain));
assert (rc == 0);
} }
// CURVE specific functions // CURVE specific functions
...@@ -102,31 +95,26 @@ char valid_server_secret[41]; ...@@ -102,31 +95,26 @@ char valid_server_secret[41];
void setup_testutil_security_curve () void setup_testutil_security_curve ()
{ {
// Generate new keypairs for these tests // Generate new keypairs for these tests
int rc = zmq_curve_keypair (valid_client_public, valid_client_secret); TEST_ASSERT_SUCCESS_ERRNO (
assert (rc == 0); zmq_curve_keypair (valid_client_public, valid_client_secret));
rc = zmq_curve_keypair (valid_server_public, valid_server_secret); TEST_ASSERT_SUCCESS_ERRNO (
assert (rc == 0); zmq_curve_keypair (valid_server_public, valid_server_secret));
} }
void socket_config_curve_server (void *server_, void *server_secret_) void socket_config_curve_server (void *server_, void *server_secret_)
{ {
int as_server = 1; int as_server = 1;
int rc = TEST_ASSERT_SUCCESS_ERRNO (
zmq_setsockopt (server_, ZMQ_CURVE_SERVER, &as_server, sizeof (int)); zmq_setsockopt (server_, ZMQ_CURVE_SERVER, &as_server, sizeof (int)));
assert (rc == 0); TEST_ASSERT_SUCCESS_ERRNO (
zmq_setsockopt (server_, ZMQ_CURVE_SECRETKEY, server_secret_, 41));
rc = zmq_setsockopt (server_, ZMQ_CURVE_SECRETKEY, server_secret_, 41); TEST_ASSERT_SUCCESS_ERRNO (zmq_setsockopt (
assert (rc == 0); server_, ZMQ_ZAP_DOMAIN, test_zap_domain, strlen (test_zap_domain)));
rc = zmq_setsockopt (server_, ZMQ_ZAP_DOMAIN, test_zap_domain,
strlen (test_zap_domain));
assert (rc == 0);
#ifdef ZMQ_ZAP_ENFORCE_DOMAIN #ifdef ZMQ_ZAP_ENFORCE_DOMAIN
int required = 1; int required = 1;
rc = TEST_ASSERT_SUCCESS_ERRNO (zmq_setsockopt (server_, ZMQ_ZAP_ENFORCE_DOMAIN,
zmq_setsockopt (server_, ZMQ_ZAP_ENFORCE_DOMAIN, &required, sizeof (int)); &required, sizeof (int)));
assert (rc == 0);
#endif #endif
} }
...@@ -142,15 +130,12 @@ void socket_config_curve_client (void *client_, void *data_) ...@@ -142,15 +130,12 @@ void socket_config_curve_client (void *client_, void *data_)
curve_client_data_t *curve_client_data = curve_client_data_t *curve_client_data =
static_cast<curve_client_data_t *> (data_); static_cast<curve_client_data_t *> (data_);
int rc = zmq_setsockopt (client_, ZMQ_CURVE_SERVERKEY, TEST_ASSERT_SUCCESS_ERRNO (zmq_setsockopt (
curve_client_data->server_public, 41); client_, ZMQ_CURVE_SERVERKEY, curve_client_data->server_public, 41));
assert (rc == 0); TEST_ASSERT_SUCCESS_ERRNO (zmq_setsockopt (
rc = zmq_setsockopt (client_, ZMQ_CURVE_PUBLICKEY, client_, ZMQ_CURVE_PUBLICKEY, curve_client_data->client_public, 41));
curve_client_data->client_public, 41); TEST_ASSERT_SUCCESS_ERRNO (zmq_setsockopt (
assert (rc == 0); client_, ZMQ_CURVE_SECRETKEY, curve_client_data->client_secret, 41));
rc = zmq_setsockopt (client_, ZMQ_CURVE_SECRETKEY,
curve_client_data->client_secret, 41);
assert (rc == 0);
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
...@@ -175,23 +160,20 @@ enum zap_protocol_t ...@@ -175,23 +160,20 @@ enum zap_protocol_t
void *zap_requests_handled; void *zap_requests_handled;
void zap_handler_generic (void *ctx_, void zap_handler_generic (zap_protocol_t zap_protocol_,
zap_protocol_t zap_protocol_,
const char *expected_routing_id_ = "IDENT") const char *expected_routing_id_ = "IDENT")
{ {
void *control = zmq_socket (ctx_, ZMQ_REQ); void *control = zmq_socket (get_test_context (), ZMQ_REQ);
assert (control); TEST_ASSERT_NOT_NULL (control);
int rc = zmq_connect (control, "inproc://handler-control"); TEST_ASSERT_SUCCESS_ERRNO (
assert (rc == 0); zmq_connect (control, "inproc://handler-control"));
void *handler = zmq_socket (ctx_, ZMQ_REP); void *handler = zmq_socket (get_test_context (), ZMQ_REP);
assert (handler); TEST_ASSERT_NOT_NULL (handler);
rc = zmq_bind (handler, "inproc://zeromq.zap.01"); TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (handler, "inproc://zeromq.zap.01"));
assert (rc == 0);
// Signal main thread that we are ready // Signal main thread that we are ready
rc = s_send (control, "GO"); send_string_expect_success (control, "GO", 0);
assert (rc == 2);
zmq_pollitem_t items[] = { zmq_pollitem_t items[] = {
{control, 0, ZMQ_POLLIN, 0}, {control, 0, ZMQ_POLLIN, 0},
...@@ -204,10 +186,7 @@ void zap_handler_generic (void *ctx_, ...@@ -204,10 +186,7 @@ void zap_handler_generic (void *ctx_,
// Process ZAP requests forever // Process ZAP requests forever
while (zmq_poll (items, numitems, -1) >= 0) { while (zmq_poll (items, numitems, -1) >= 0) {
if (items[0].revents & ZMQ_POLLIN) { if (items[0].revents & ZMQ_POLLIN) {
char *buf = s_recv (control); recv_string_expect_success (control, "STOP", 0);
assert (buf);
assert (streq (buf, "STOP"));
free (buf);
break; // Terminating - main thread signal break; // Terminating - main thread signal
} }
if (!(items[1].revents & ZMQ_POLLIN)) if (!(items[1].revents & ZMQ_POLLIN))
...@@ -229,8 +208,8 @@ void zap_handler_generic (void *ctx_, ...@@ -229,8 +208,8 @@ void zap_handler_generic (void *ctx_,
bool authentication_succeeded = false; bool authentication_succeeded = false;
if (streq (mechanism, "CURVE")) { if (streq (mechanism, "CURVE")) {
uint8_t client_key[32]; uint8_t client_key[32];
int size = zmq_recv (handler, client_key, 32, 0); TEST_ASSERT_EQUAL_INT (32, TEST_ASSERT_SUCCESS_ERRNO (zmq_recv (
assert (size == 32); handler, client_key, 32, 0)));
char client_key_text[41]; char client_key_text[41];
zmq_z85_encode (client_key_text, client_key, 32); zmq_z85_encode (client_key_text, client_key, 32);
...@@ -239,13 +218,13 @@ void zap_handler_generic (void *ctx_, ...@@ -239,13 +218,13 @@ void zap_handler_generic (void *ctx_,
streq (client_key_text, valid_client_public); streq (client_key_text, valid_client_public);
} else if (streq (mechanism, "PLAIN")) { } else if (streq (mechanism, "PLAIN")) {
char client_username[32]; char client_username[32];
int size = zmq_recv (handler, client_username, 32, 0); int size = TEST_ASSERT_SUCCESS_ERRNO (
assert (size > 0); zmq_recv (handler, client_username, 32, 0));
client_username[size] = 0; client_username[size] = 0;
char client_password[32]; char client_password[32];
size = zmq_recv (handler, client_password, 32, 0); size = TEST_ASSERT_SUCCESS_ERRNO (
assert (size > 0); zmq_recv (handler, client_password, 32, 0));
client_password[size] = 0; client_password[size] = 0;
authentication_succeeded = authentication_succeeded =
...@@ -254,12 +233,13 @@ void zap_handler_generic (void *ctx_, ...@@ -254,12 +233,13 @@ void zap_handler_generic (void *ctx_,
} else if (streq (mechanism, "NULL")) { } else if (streq (mechanism, "NULL")) {
authentication_succeeded = true; authentication_succeeded = true;
} else { } else {
fprintf (stderr, "Unsupported mechanism: %s\n", mechanism); char msg[128];
assert (false); printf ("Unsupported mechanism: %s\n", mechanism);
TEST_FAIL_MESSAGE (msg);
} }
assert (streq (version, "1.0")); TEST_ASSERT_EQUAL_STRING ("1.0", version);
assert (streq (routing_id, expected_routing_id_)); TEST_ASSERT_EQUAL_STRING (expected_routing_id_, routing_id);
s_sendmore (handler, zap_protocol_ == zap_wrong_version s_sendmore (handler, zap_protocol_ == zap_wrong_version
? "invalid_version" ? "invalid_version"
...@@ -307,20 +287,18 @@ void zap_handler_generic (void *ctx_, ...@@ -307,20 +287,18 @@ void zap_handler_generic (void *ctx_,
zmq_atomic_counter_inc (zap_requests_handled); zmq_atomic_counter_inc (zap_requests_handled);
} }
rc = zmq_unbind (handler, "inproc://zeromq.zap.01"); TEST_ASSERT_SUCCESS_ERRNO (zmq_unbind (handler, "inproc://zeromq.zap.01"));
assert (rc == 0);
close_zero_linger (handler); close_zero_linger (handler);
if (zap_protocol_ != zap_disconnect) { if (zap_protocol_ != zap_disconnect) {
rc = s_send (control, "STOPPED"); send_string_expect_success (control, "STOPPED", 0);
assert (rc == 7);
} }
close_zero_linger (control); close_zero_linger (control);
} }
void zap_handler (void *ctx_) void zap_handler (void * /*unused_*/)
{ {
zap_handler_generic (ctx_, zap_ok); zap_handler_generic (zap_ok);
} }
// Security-specific monitor event utilities // Security-specific monitor event utilities
...@@ -344,38 +322,34 @@ void zap_handler (void *ctx_) ...@@ -344,38 +322,34 @@ void zap_handler (void *ctx_)
continue; \ continue; \
} \ } \
++event_count; \ ++event_count; \
print_unexpected_event (event, err, 0, 0); \ /* TODO write this into a buffer and attach to the assertion msg below */ \
print_unexpected_event_stderr (event, err, 0, 0); \
} \ } \
assert (event_count == 0); \ TEST_ASSERT_EQUAL_INT (0, event_count); \
} }
void setup_handshake_socket_monitor (void *ctx_, void setup_handshake_socket_monitor (void *server_,
void *server_,
void **server_mon_, void **server_mon_,
const char *monitor_endpoint_) const char *monitor_endpoint_)
{ {
// Monitor handshake events on the server // Monitor handshake events on the server
int rc = zmq_socket_monitor (server_, monitor_endpoint_, TEST_ASSERT_SUCCESS_ERRNO (zmq_socket_monitor (
ZMQ_EVENT_HANDSHAKE_SUCCEEDED server_, monitor_endpoint_,
| ZMQ_EVENT_HANDSHAKE_FAILED_NO_DETAIL ZMQ_EVENT_HANDSHAKE_SUCCEEDED | ZMQ_EVENT_HANDSHAKE_FAILED_NO_DETAIL
| ZMQ_EVENT_HANDSHAKE_FAILED_AUTH | ZMQ_EVENT_HANDSHAKE_FAILED_AUTH
| ZMQ_EVENT_HANDSHAKE_FAILED_PROTOCOL); | ZMQ_EVENT_HANDSHAKE_FAILED_PROTOCOL));
assert (rc == 0);
// Create socket for collecting monitor events // Create socket for collecting monitor events
*server_mon_ = zmq_socket (ctx_, ZMQ_PAIR); *server_mon_ = test_context_socket (ZMQ_PAIR);
assert (*server_mon_);
int linger = 0; int linger = 0;
rc = zmq_setsockopt (*server_mon_, ZMQ_LINGER, &linger, sizeof (linger)); TEST_ASSERT_SUCCESS_ERRNO (
assert (rc == 0); zmq_setsockopt (*server_mon_, ZMQ_LINGER, &linger, sizeof (linger)));
// Connect it to the inproc endpoints so they'll get events // Connect it to the inproc endpoints so they'll get events
rc = zmq_connect (*server_mon_, monitor_endpoint_); TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (*server_mon_, monitor_endpoint_));
assert (rc == 0);
} }
void setup_context_and_server_side ( void setup_context_and_server_side (
void **ctx_,
void **zap_control_, void **zap_control_,
void **zap_thread_, void **zap_thread_,
void **server_, void **server_,
...@@ -386,114 +360,84 @@ void setup_context_and_server_side ( ...@@ -386,114 +360,84 @@ void setup_context_and_server_side (
void *socket_config_data_ = valid_server_secret, void *socket_config_data_ = valid_server_secret,
const char *routing_id_ = "IDENT") const char *routing_id_ = "IDENT")
{ {
*ctx_ = zmq_ctx_new ();
assert (*ctx_);
// Spawn ZAP handler // Spawn ZAP handler
zap_requests_handled = zmq_atomic_counter_new (); zap_requests_handled = zmq_atomic_counter_new ();
assert (zap_requests_handled != NULL); TEST_ASSERT_NOT_NULL (zap_requests_handled);
*zap_control_ = zmq_socket (*ctx_, ZMQ_REP); *zap_control_ = test_context_socket (ZMQ_REP);
assert (*zap_control_); TEST_ASSERT_SUCCESS_ERRNO (
int rc = zmq_bind (*zap_control_, "inproc://handler-control"); zmq_bind (*zap_control_, "inproc://handler-control"));
assert (rc == 0);
int linger = 0; int linger = 0;
rc = zmq_setsockopt (*zap_control_, ZMQ_LINGER, &linger, sizeof (linger)); TEST_ASSERT_SUCCESS_ERRNO (
assert (rc == 0); zmq_setsockopt (*zap_control_, ZMQ_LINGER, &linger, sizeof (linger)));
if (zap_handler_ != NULL) { if (zap_handler_ != NULL) {
*zap_thread_ = zmq_threadstart (zap_handler_, *ctx_); *zap_thread_ = zmq_threadstart (zap_handler_, NULL);
char *buf = s_recv (*zap_control_); recv_string_expect_success (*zap_control_, "GO", 0);
assert (buf);
assert (streq (buf, "GO"));
free (buf);
} else } else
*zap_thread_ = NULL; *zap_thread_ = NULL;
// Server socket will accept connections // Server socket will accept connections
*server_ = zmq_socket (*ctx_, ZMQ_DEALER); *server_ = test_context_socket (ZMQ_DEALER);
assert (*server_); TEST_ASSERT_SUCCESS_ERRNO (
rc = zmq_setsockopt (*server_, ZMQ_LINGER, &linger, sizeof (linger)); zmq_setsockopt (*server_, ZMQ_LINGER, &linger, sizeof (linger)));
assert (rc == 0);
socket_config_ (*server_, socket_config_data_); socket_config_ (*server_, socket_config_data_);
rc = zmq_setsockopt (*server_, ZMQ_ROUTING_ID, routing_id_, TEST_ASSERT_SUCCESS_ERRNO (zmq_setsockopt (
strlen (routing_id_)); *server_, ZMQ_ROUTING_ID, routing_id_, strlen (routing_id_)));
assert (rc == 0);
rc = zmq_bind (*server_, "tcp://127.0.0.1:*"); bind_loopback_ipv4 (*server_, my_endpoint_, MAX_SOCKET_STRING);
assert (rc == 0);
size_t len = MAX_SOCKET_STRING;
rc = zmq_getsockopt (*server_, ZMQ_LAST_ENDPOINT, my_endpoint_, &len);
assert (rc == 0);
const char server_monitor_endpoint[] = "inproc://monitor-server"; const char server_monitor_endpoint[] = "inproc://monitor-server";
setup_handshake_socket_monitor (*ctx_, *server_, server_mon_, setup_handshake_socket_monitor (*server_, server_mon_,
server_monitor_endpoint); server_monitor_endpoint);
} }
void shutdown_context_and_server_side (void *ctx_, void shutdown_context_and_server_side (void *zap_thread_,
void *zap_thread_,
void *server_, void *server_,
void *server_mon_, void *server_mon_,
void *zap_control_, void *zap_control_,
bool zap_handler_stopped_ = false) bool zap_handler_stopped_ = false)
{ {
if (zap_thread_ && !zap_handler_stopped_) { if (zap_thread_ && !zap_handler_stopped_) {
int rc = s_send (zap_control_, "STOP"); send_string_expect_success (zap_control_, "STOP", 0);
assert (rc == 4); recv_string_expect_success (zap_control_, "STOPPED", 0);
char *buf = s_recv (zap_control_); TEST_ASSERT_SUCCESS_ERRNO (
assert (buf); zmq_unbind (zap_control_, "inproc://handler-control"));
assert (streq (buf, "STOPPED"));
free (buf);
rc = zmq_unbind (zap_control_, "inproc://handler-control");
assert (rc == 0);
} }
int rc = zmq_close (zap_control_); test_context_socket_close (zap_control_);
assert (rc == 0); test_context_socket_close (server_mon_);
test_context_socket_close (server_);
rc = zmq_close (server_mon_);
assert (rc == 0);
rc = zmq_close (server_);
assert (rc == 0);
// Wait until ZAP handler terminates // Wait until ZAP handler terminates
if (zap_thread_) if (zap_thread_)
zmq_threadclose (zap_thread_); zmq_threadclose (zap_thread_);
rc = zmq_ctx_term (ctx_);
assert (rc == 0);
zmq_atomic_counter_destroy (&zap_requests_handled); zmq_atomic_counter_destroy (&zap_requests_handled);
} }
void *create_and_connect_client (void *ctx_, void *create_and_connect_client (char *my_endpoint_,
char *my_endpoint_,
socket_config_fn socket_config_, socket_config_fn socket_config_,
void *socket_config_data_, void *socket_config_data_,
void **client_mon_ = NULL) void **client_mon_ = NULL)
{ {
void *client = zmq_socket (ctx_, ZMQ_DEALER); void *client = test_context_socket (ZMQ_DEALER);
assert (client);
socket_config_ (client, socket_config_data_); socket_config_ (client, socket_config_data_);
int rc = zmq_connect (client, my_endpoint_); TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (client, my_endpoint_));
assert (rc == 0);
if (client_mon_) { if (client_mon_) {
setup_handshake_socket_monitor (ctx_, client, client_mon_, setup_handshake_socket_monitor (client, client_mon_,
"inproc://client-monitor"); "inproc://client-monitor");
} }
return client; return client;
} }
void expect_new_client_bounce_fail (void *ctx_, void expect_new_client_bounce_fail (char *my_endpoint_,
char *my_endpoint_,
void *server_, void *server_,
socket_config_fn socket_config_, socket_config_fn socket_config_,
void *socket_config_data_, void *socket_config_data_,
...@@ -502,11 +446,11 @@ void expect_new_client_bounce_fail (void *ctx_, ...@@ -502,11 +446,11 @@ void expect_new_client_bounce_fail (void *ctx_,
int expected_client_value_ = 0) int expected_client_value_ = 0)
{ {
void *my_client_mon; void *my_client_mon;
assert (client_mon_ == NULL || expected_client_event_ == 0); TEST_ASSERT_TRUE (client_mon_ == NULL || expected_client_event_ == 0);
if (expected_client_event_ != 0) if (expected_client_event_ != 0)
client_mon_ = &my_client_mon; client_mon_ = &my_client_mon;
void *client = create_and_connect_client ( void *client = create_and_connect_client (my_endpoint_, socket_config_,
ctx_, my_endpoint_, socket_config_, socket_config_data_, client_mon_); socket_config_data_, client_mon_);
expect_bounce_fail (server_, client); expect_bounce_fail (server_, client);
if (expected_client_event_ != 0) { if (expected_client_event_ != 0) {
...@@ -514,13 +458,12 @@ void expect_new_client_bounce_fail (void *ctx_, ...@@ -514,13 +458,12 @@ void expect_new_client_bounce_fail (void *ctx_,
events_received = expect_monitor_event_multiple ( events_received = expect_monitor_event_multiple (
my_client_mon, expected_client_event_, expected_client_value_, false); my_client_mon, expected_client_event_, expected_client_value_, false);
assert (events_received == 1); TEST_ASSERT_EQUAL_INT (1, events_received);
int rc = zmq_close (my_client_mon); test_context_socket_close (my_client_mon);
assert (rc == 0);
} }
close_zero_linger (client); test_context_socket_close_zero_linger (client);
} }
#endif #endif
...@@ -19,6 +19,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. ...@@ -19,6 +19,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#include <unity.h> #include <unity.h>
#include "../tests/testutil.hpp" #include "../tests/testutil.hpp"
#include "../tests/testutil_unity.hpp"
#include "../unittests/unittest_resolver_common.hpp" #include "../unittests/unittest_resolver_common.hpp"
#include <ip_resolver.hpp> #include <ip_resolver.hpp>
...@@ -150,10 +151,11 @@ static void test_resolve (zmq::ip_resolver_options_t opts_, ...@@ -150,10 +151,11 @@ static void test_resolve (zmq::ip_resolver_options_t opts_,
int rc = resolver.resolve (&addr, name_); int rc = resolver.resolve (&addr, name_);
if (expected_addr_ == NULL) { if (expected_addr_ == NULL) {
// TODO also check the expected errno
TEST_ASSERT_EQUAL (-1, rc); TEST_ASSERT_EQUAL (-1, rc);
return; return;
} else { } else {
TEST_ASSERT_EQUAL (0, rc); TEST_ASSERT_SUCCESS_ERRNO (rc);
} }
validate_address (family, &addr, expected_addr_, expected_port_, validate_address (family, &addr, expected_addr_, expected_port_,
......
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