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