Problem: testutil_security not using unity

Solution: migrate to unity and adapt users
This commit is contained in:
Simon Giesecke 2019-03-20 16:03:06 +01:00
parent 6e0724609a
commit efaca82bff
3 changed files with 194 additions and 271 deletions

View File

@ -62,7 +62,6 @@ const char *zmq_errno_message ()
#define TEST_ASSERT_ZMQ_ERRNO(condition) \ #define TEST_ASSERT_ZMQ_ERRNO(condition) \
TEST_ASSERT_MESSAGE ((condition), zmq_errno_message ()) 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 +70,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 +91,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 +108,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 +145,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 +160,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 +171,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;
@ -199,7 +193,7 @@ void expect_zmtp_mechanism_mismatch (void *client_,
int rc = zmq_connect (client_, my_endpoint_); int rc = zmq_connect (client_, my_endpoint_);
TEST_ASSERT_ZMQ_ERRNO (rc == 0); 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,16 +204,14 @@ 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);
int rc = zmq_setsockopt (client, ZMQ_PLAIN_USERNAME, "admin", 5); int rc = zmq_setsockopt (client, ZMQ_PLAIN_USERNAME, "admin", 5);
TEST_ASSERT_ZMQ_ERRNO (rc == 0); TEST_ASSERT_ZMQ_ERRNO (rc == 0);
rc = zmq_setsockopt (client, ZMQ_PLAIN_PASSWORD, "password", 8); rc = zmq_setsockopt (client, ZMQ_PLAIN_PASSWORD, "password", 8);
@ -579,7 +571,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 +579,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 +587,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,15 +629,16 @@ 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,
&valid_server_secret, large_routing_id); &socket_config_curve_server,
&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); int rc = zmq_ctx_term (ctx);
TEST_ASSERT_ZMQ_ERRNO (rc == 0); TEST_ASSERT_ZMQ_ERRNO (rc == 0);

View File

@ -40,53 +40,52 @@ void tearDown ()
teardown_test_context (); teardown_test_context ();
} }
static void zap_handler_wrong_version (void *ctx_) static void zap_handler_wrong_version (void * /*unused_*/)
{ {
zap_handler_generic (ctx_, zap_wrong_version); zap_handler_generic (zap_wrong_version);
} }
static void zap_handler_wrong_request_id (void *ctx_) static void zap_handler_wrong_request_id (void * /*unused_*/)
{ {
zap_handler_generic (ctx_, zap_wrong_request_id); zap_handler_generic (zap_wrong_request_id);
} }
static void zap_handler_wrong_status_invalid (void *ctx_) static void zap_handler_wrong_status_invalid (void * /*unused_*/)
{ {
zap_handler_generic (ctx_, zap_status_invalid); zap_handler_generic (zap_status_invalid);
} }
static void zap_handler_wrong_status_temporary_failure (void *ctx_) static void zap_handler_wrong_status_temporary_failure (void * /*unused_*/)
{ {
zap_handler_generic (ctx_, zap_status_temporary_failure); zap_handler_generic (zap_status_temporary_failure);
} }
static void zap_handler_wrong_status_internal_error (void *ctx_) static void zap_handler_wrong_status_internal_error (void * /*unused_*/)
{ {
zap_handler_generic (ctx_, zap_status_internal_error); zap_handler_generic (zap_status_internal_error);
} }
static void zap_handler_too_many_parts (void *ctx_) static void zap_handler_too_many_parts (void * /*unused_*/)
{ {
zap_handler_generic (ctx_, zap_too_many_parts); zap_handler_generic (zap_too_many_parts);
} }
static void zap_handler_disconnect (void *ctx_) static void zap_handler_disconnect (void * /*unused_*/)
{ {
zap_handler_generic (ctx_, zap_disconnect); zap_handler_generic (zap_disconnect);
} }
static void zap_handler_do_not_recv (void *ctx_) static void zap_handler_do_not_recv (void * /*unused_*/)
{ {
zap_handler_generic (ctx_, zap_do_not_recv); zap_handler_generic (zap_do_not_recv);
} }
static void zap_handler_do_not_send (void *ctx_) static void zap_handler_do_not_send (void * /*unused_*/)
{ {
zap_handler_generic (ctx_, zap_do_not_send); 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_,
@ -99,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 (
@ -109,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_,
@ -123,9 +121,9 @@ 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)
@ -133,8 +131,7 @@ void test_zap_unsuccessful (void *ctx_,
|| 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_,
@ -145,29 +142,27 @@ void test_zap_unsuccessful_no_handler (void *ctx_,
{ {
const 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
TEST_ASSERT_GREATER_THAN_INT (0, events_received); 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_,
@ -175,23 +170,22 @@ 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);
TEST_ASSERT_SUCCESS_ERRNO (zmq_close (client_mon)); test_context_socket_close (client_mon);
} }
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);
@ -205,17 +199,16 @@ test_zap_protocol_error_closure (socket_config_fn server_socket_config_,
zmq_thread_fn zap_handler_, zmq_thread_fn zap_handler_,
int expected_failure_) int expected_failure_)
{ {
void *ctx, *handler, *zap_thread, *server, *server_mon; void *handler, *zap_thread, *server, *server_mon;
char my_endpoint[MAX_SOCKET_STRING]; 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_,
zap_handler_, server_socket_config_, server_socket_config_data_); server_socket_config_, server_socket_config_data_);
test_zap_protocol_error (ctx, my_endpoint, server, server_mon, test_zap_protocol_error (my_endpoint, server, server_mon,
client_socket_config_, client_socket_config_data_, client_socket_config_, client_socket_config_data_,
expected_failure_); expected_failure_);
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 static void
@ -275,17 +268,16 @@ test_zap_wrong_status_temporary_failure (socket_config_fn server_socket_config_,
void *client_socket_config_data_, void *client_socket_config_data_,
void *server_socket_config_data_) void *server_socket_config_data_)
{ {
void *ctx, *handler, *zap_thread, *server, *server_mon; void *handler, *zap_thread, *server, *server_mon;
char my_endpoint[MAX_SOCKET_STRING]; 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);
} }
static void static void
@ -293,16 +285,15 @@ test_zap_wrong_status_internal_error (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 *ctx, *handler, *zap_thread, *server, *server_mon; void *handler, *zap_thread, *server, *server_mon;
char my_endpoint[MAX_SOCKET_STRING]; 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 static void
@ -312,22 +303,20 @@ test_zap_unsuccesful_no_handler_started (socket_config_fn server_socket_config_,
void *server_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; void *handler, *zap_thread, *server, *server_mon;
char my_endpoint[MAX_SOCKET_STRING]; char my_endpoint[MAX_SOCKET_STRING];
// TODO this looks wrong, where will the enforce value be used? // TODO this looks wrong, where will the enforce value be used?
// no ZAP handler // no ZAP handler
int enforce = 1; int enforce = 1;
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
} }
@ -338,17 +327,16 @@ test_zap_unsuccesful_no_handler_closure (socket_config_fn server_socket_config_,
zmq_thread_fn zap_handler_func_, zmq_thread_fn zap_handler_func_,
bool zap_handler_disconnected_ = false) bool zap_handler_disconnected_ = false)
{ {
void *ctx, *handler, *zap_thread, *server, *server_mon; void *handler, *zap_thread, *server, *server_mon;
char my_endpoint[MAX_SOCKET_STRING]; char my_endpoint[MAX_SOCKET_STRING];
setup_context_and_server_side (&ctx, &handler, &zap_thread, &server, setup_context_and_server_side (&handler, &zap_thread, &server, &server_mon,
&server_mon, my_endpoint, zap_handler_func_, my_endpoint, zap_handler_func_,
server_socket_config_); server_socket_config_);
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,
EPIPE, client_socket_config_, EPIPE, 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, zap_handler_disconnected_);
handler, zap_handler_disconnected_);
} }
static void static void

View File

@ -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_)
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_)
{ {
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_)
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];
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_)
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
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_,
// 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_,
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_,
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_,
} 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_,
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
@ -346,36 +324,31 @@ void zap_handler (void *ctx_)
++event_count; \ ++event_count; \
print_unexpected_event (event, err, 0, 0); \ print_unexpected_event (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 +359,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 +445,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 +457,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