/* Copyright (c) 2007-2017 Contributors as noted in the AUTHORS file This file is part of 0MQ. 0MQ is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version. 0MQ is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this program. If not, see . */ #include "testutil.hpp" #if defined(ZMQ_HAVE_WINDOWS) #include #include #include #define close closesocket typedef SOCKET raw_socket; #else #include #include typedef int raw_socket; #endif #include #include #include // TODO remove this here, either ensure that UINT16_MAX is always properly // defined or handle this at a more central location #ifndef UINT16_MAX #define UINT16_MAX 65535 #endif #include "testutil_unity.hpp" SETUP_TEARDOWN_TESTCONTEXT // Read one event off the monitor socket; return value and address // by reference, if not null, and event number by value. Returns -1 // in case of error. static int get_monitor_event (void *monitor_) { for (int i = 0; i < 2; i++) { // First frame in message contains event number and value zmq_msg_t msg; TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_init (&msg)); if (zmq_msg_recv (&msg, monitor_, ZMQ_DONTWAIT) == -1) { msleep (SETTLE_TIME); continue; // Interrupted, presumably } TEST_ASSERT_TRUE (zmq_msg_more (&msg)); uint8_t *data = (uint8_t *) zmq_msg_data (&msg); uint16_t event = *(uint16_t *) (data); // Second frame in message contains event address TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_init (&msg)); if (zmq_msg_recv (&msg, monitor_, 0) == -1) { return -1; // Interrupted, presumably } TEST_ASSERT_FALSE (zmq_msg_more (&msg)); return event; } return -1; } static void recv_with_retry (raw_socket fd_, char *buffer_, int bytes_) { int received = 0; while (true) { int rc = TEST_ASSERT_SUCCESS_RAW_ERRNO ( recv (fd_, buffer_ + received, bytes_ - received, 0)); TEST_ASSERT_GREATER_THAN_INT (0, rc); received += rc; TEST_ASSERT_LESS_OR_EQUAL_INT (bytes_, received); if (received == bytes_) break; } } 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)); int rc = TEST_ASSERT_SUCCESS_RAW_ERRNO (send (fd_, buffer, 64, 0)); TEST_ASSERT_EQUAL_INT (64, 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}; 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); // greeting recv_with_retry (fd_, buffer, 43); if (mock_ping_) { // test PING context - should be replicated in the PONG // to avoid timeouts, do a bulk send const uint8_t zmtp_ping[12] = {4, 10, 4, 'P', 'I', 'N', 'G', 0, 0, 'L', 'O', 'L'}; uint8_t zmtp_pong[10] = {4, 8, 4, 'P', 'O', 'N', 'G', 'L', 'O', 'L'}; memset (buffer, 0, sizeof (buffer)); memcpy (buffer, zmtp_ping, 12); rc = TEST_ASSERT_SUCCESS_RAW_ERRNO (send (fd_, buffer, 12, 0)); TEST_ASSERT_EQUAL_INT (12, rc); // test a larger body that won't fit in a small message and should get // truncated memset (buffer, 'z', sizeof (buffer)); memcpy (buffer, zmtp_ping, 12); buffer[1] = 65; rc = TEST_ASSERT_SUCCESS_RAW_ERRNO (send (fd_, buffer, 67, 0)); TEST_ASSERT_EQUAL_INT (67, rc); // small pong recv_with_retry (fd_, buffer, 10); TEST_ASSERT_EQUAL_INT (0, memcmp (zmtp_pong, buffer, 10)); // large pong recv_with_retry (fd_, buffer, 23); uint8_t zmtp_pooong[65] = {4, 21, 4, 'P', 'O', 'N', 'G', 'L', 'O', 'L'}; memset (zmtp_pooong + 10, 'z', 55); TEST_ASSERT_EQUAL_INT (0, memcmp (zmtp_pooong, buffer, 23)); } } static void setup_curve (void *socket_, int is_server_) { const char *secret_key; const char *public_key; const char *server_key; if (is_server_) { secret_key = "JTKVSB%%)wK0E.X)V>+}o?pNmC{O&4W4b!Ni{Lh6"; public_key = "rq:rM>}U?@Lns47E1%kR.o@n%FcmmsL/@{H8]yf7"; server_key = NULL; } else { secret_key = "D:)Q[IlAW!ahhC2ac:9*A}h:p?([4%wOTJ%JR%cs"; public_key = "Yne@$w-vo