Problem: ZMTP mocks duplicated across tests

Solution: define buffers in common header
This commit is contained in:
Luca Boccassi 2020-04-26 13:33:32 +01:00
parent 4f35d1af1a
commit c6d3bc68c8
4 changed files with 47 additions and 42 deletions

View File

@ -91,29 +91,24 @@ static void recv_with_retry (raw_socket fd_, char *buffer_, int bytes_)
static void mock_handshake (raw_socket fd_, int mock_ping_)
{
const uint8_t zmtp_greeting[33] = {0xff, 0, 0, 0, 0, 0, 0, 0, 0,
0x7f, 3, 0, 'N', 'U', 'L', 'L', 0};
char buffer[128];
memset (buffer, 0, sizeof (buffer));
memcpy (buffer, zmtp_greeting, sizeof (zmtp_greeting));
memcpy (buffer, zmtp_greeting_null, sizeof (zmtp_greeting_null));
int rc = TEST_ASSERT_SUCCESS_RAW_ERRNO (send (fd_, buffer, 64, 0));
TEST_ASSERT_EQUAL_INT (64, rc);
int rc = TEST_ASSERT_SUCCESS_RAW_ERRNO (
send (fd_, buffer, sizeof (zmtp_greeting_null), 0));
TEST_ASSERT_EQUAL_INT (sizeof (zmtp_greeting_null), rc);
recv_with_retry (fd_, buffer, 64);
const uint8_t zmtp_ready[43] = {
4, 41, 5, 'R', 'E', 'A', 'D', 'Y', 11, 'S', 'o', 'c', 'k', 'e', 't',
'-', 'T', 'y', 'p', 'e', 0, 0, 0, 6, 'D', 'E', 'A', 'L', 'E', 'R',
8, 'I', 'd', 'e', 'n', 't', 'i', 't', 'y', 0, 0, 0, 0};
recv_with_retry (fd_, buffer, sizeof (zmtp_greeting_null));
memset (buffer, 0, sizeof (buffer));
memcpy (buffer, zmtp_ready, 43);
rc = TEST_ASSERT_SUCCESS_RAW_ERRNO (send (fd_, buffer, 43, 0));
TEST_ASSERT_EQUAL_INT (43, rc);
memcpy (buffer, zmtp_ready_dealer, sizeof (zmtp_ready_dealer));
rc = TEST_ASSERT_SUCCESS_RAW_ERRNO (
send (fd_, buffer, sizeof (zmtp_ready_dealer), 0));
TEST_ASSERT_EQUAL_INT (sizeof (zmtp_ready_dealer), rc);
// greeting
recv_with_retry (fd_, buffer, 43);
recv_with_retry (fd_, buffer, sizeof (zmtp_ready_dealer));
if (mock_ping_) {
// test PING context - should be replicated in the PONG

View File

@ -72,11 +72,9 @@ static void recv_with_retry (fd_t fd_, char *buffer_, int bytes_)
static void mock_handshake (fd_t fd_, bool sub_command, bool mock_pub)
{
const uint8_t zmtp_greeting[33] = {0xff, 0, 0, 0, 0, 0, 0, 0, 0,
0x7f, 3, 0, 'N', 'U', 'L', 'L', 0};
char buffer[128];
memset (buffer, 0, sizeof (buffer));
memcpy (buffer, zmtp_greeting, sizeof (zmtp_greeting));
memcpy (buffer, zmtp_greeting_null, sizeof (zmtp_greeting_null));
// Mock ZMTP 3.1 which uses commands
if (sub_command) {
@ -88,24 +86,20 @@ static void mock_handshake (fd_t fd_, bool sub_command, bool mock_pub)
recv_with_retry (fd_, buffer, 64);
if (!mock_pub) {
const uint8_t zmtp_ready[27] = {
4, 25, 5, 'R', 'E', 'A', 'D', 'Y', 11, 'S', 'o', 'c', 'k', 'e',
't', '-', 'T', 'y', 'p', 'e', 0, 0, 0, 3, 'S', 'U', 'B'};
rc = TEST_ASSERT_SUCCESS_RAW_ERRNO (
send (fd_, (const char *) zmtp_ready, 27, 0));
TEST_ASSERT_EQUAL_INT (27, rc);
rc = TEST_ASSERT_SUCCESS_RAW_ERRNO (send (
fd_, (const char *) zmtp_ready_sub, sizeof (zmtp_ready_sub), 0));
TEST_ASSERT_EQUAL_INT (sizeof (zmtp_ready_sub), rc);
} else {
const uint8_t zmtp_ready[28] = {
4, 26, 5, 'R', 'E', 'A', 'D', 'Y', 11, 'S', 'o', 'c', 'k', 'e',
't', '-', 'T', 'y', 'p', 'e', 0, 0, 0, 4, 'X', 'P', 'U', 'B'};
rc = TEST_ASSERT_SUCCESS_RAW_ERRNO (
send (fd_, (const char *) zmtp_ready, 28, 0));
TEST_ASSERT_EQUAL_INT (28, rc);
rc = TEST_ASSERT_SUCCESS_RAW_ERRNO (send (
fd_, (const char *) zmtp_ready_xpub, sizeof (zmtp_ready_xpub), 0));
TEST_ASSERT_EQUAL_INT (sizeof (zmtp_ready_xpub), rc);
}
// greeting - XPUB has one extra byte
memset (buffer, 0, sizeof (buffer));
recv_with_retry (fd_, buffer, mock_pub ? 27 : 28);
recv_with_retry (fd_, buffer,
mock_pub ? sizeof (zmtp_ready_sub)
: sizeof (zmtp_ready_xpub));
}
static void prep_server_socket (void **server_out_,

View File

@ -242,21 +242,16 @@ template <size_t N> void send (fd_t fd_, const char (&data_)[N])
send_all (fd_, data_, N - 1);
}
void send_greeting (fd_t s_)
template <size_t N> void send (fd_t fd_, const uint8_t (&data_)[N])
{
send (s_, "\xff\0\0\0\0\0\0\0\0\x7f"); // signature
send (s_, "\x03\x00"); // version 3.0
send (s_, "CURVE\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"); // mechanism CURVE
send (s_, "\0"); // as-server == false
send (s_, "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0");
send_all (fd_, reinterpret_cast<const char *> (&data_), N);
}
void test_curve_security_invalid_hello_wrong_length ()
{
fd_t s = connect_socket (my_endpoint);
// send GREETING
send_greeting (s);
send (s, zmtp_greeting_curve);
// send CURVE HELLO of wrong size
send (s, "\x04\x06\x05HELLO");
@ -322,7 +317,7 @@ void test_curve_security_invalid_hello_command_name ()
{
fd_t s = connect_socket (my_endpoint);
send_greeting (s);
send (s, zmtp_greeting_curve);
zmq::curve_client_tools_t tools = make_curve_client_tools ();
@ -344,7 +339,7 @@ void test_curve_security_invalid_hello_version ()
{
fd_t s = connect_socket (my_endpoint);
send_greeting (s);
send (s, zmtp_greeting_curve);
zmq::curve_client_tools_t tools = make_curve_client_tools ();
@ -396,7 +391,7 @@ fd_t connect_exchange_greeting_and_send_hello (
{
fd_t s = connect_socket (my_endpoint_);
send_greeting (s);
send (s, zmtp_greeting_curve);
recv_greeting (s);
// send valid CURVE HELLO

View File

@ -65,6 +65,27 @@
#define ENDPOINT_5 "udp://127.0.0.1:5560"
#define PORT_6 5561
// For tests that mock ZMTP
const uint8_t zmtp_greeting_null[64] = {
0xff, 0, 0, 0, 0, 0, 0, 0, 0, 0x7f, 3, 0, 'N', 'U', 'L', 'L',
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
const uint8_t zmtp_greeting_curve[64] = {
0xff, 0, 0, 0, 0, 0, 0, 0, 0, 0x7f, 3, 0, 'C', 'U', 'R', 'V',
'E', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
const uint8_t zmtp_ready_dealer[43] = {
4, 41, 5, 'R', 'E', 'A', 'D', 'Y', 11, 'S', 'o', 'c', 'k', 'e', 't',
'-', 'T', 'y', 'p', 'e', 0, 0, 0, 6, 'D', 'E', 'A', 'L', 'E', 'R',
8, 'I', 'd', 'e', 'n', 't', 'i', 't', 'y', 0, 0, 0, 0};
const uint8_t zmtp_ready_xpub[28] = {
4, 26, 5, 'R', 'E', 'A', 'D', 'Y', 11, 'S', 'o', 'c', 'k', 'e',
't', '-', 'T', 'y', 'p', 'e', 0, 0, 0, 4, 'X', 'P', 'U', 'B'};
const uint8_t zmtp_ready_sub[27] = {
4, 25, 5, 'R', 'E', 'A', 'D', 'Y', 11, 'S', 'o', 'c', 'k', 'e',
't', '-', 'T', 'y', 'p', 'e', 0, 0, 0, 3, 'S', 'U', 'B'};
#undef NDEBUG
// duplicated from fd.hpp