Commit 5d74eba6 authored by Simon Giesecke's avatar Simon Giesecke

Problem: remaining basic assertions

Solution: use unity assertions instead
parent 5b40bdb1
......@@ -57,7 +57,7 @@ void test_version ()
void test_strerrror ()
{
assert (zmq_strerror (EINVAL));
TEST_ASSERT_NOT_NULL (zmq_strerror (EINVAL));
}
int main ()
......
......@@ -44,7 +44,8 @@ static void receiver (void *socket_)
{
char buffer[16];
int rc = zmq_recv (socket_, &buffer, sizeof (buffer), 0);
assert (rc == -1);
// TODO which error is expected here? use TEST_ASSERT_FAILURE_ERRNO instead
TEST_ASSERT_EQUAL_INT (-1, rc);
}
void test_ctx_destroy ()
......
......@@ -117,7 +117,7 @@ void init_groups ()
// Get the group and supplemental groups of the process owner
gid_t groups[100];
int ngroups = getgroups (100, groups);
assert (ngroups != -1);
TEST_ASSERT_NOT_EQUAL (-1, ngroups);
group = getgid ();
supgroup = group;
notgroup = group + 1;
......
......@@ -30,6 +30,8 @@
#include "testutil.hpp"
#include "testutil_unity.hpp"
#include <assert.h>
void setUp ()
{
setup_test_context ();
......@@ -53,6 +55,8 @@ void test_fork ()
int pid = fork ();
if (pid == 0) {
// use regular assertions in the child process
// Child process
// Immediately close parent sockets and context
zmq_close (pull);
......
......@@ -55,7 +55,9 @@ struct iovec
static void do_check (void *sb_, void *sc_, size_t msg_size_)
{
assert (sb_ && sc_ && msg_size_ > 0);
TEST_ASSERT_NOT_NULL (sb_);
TEST_ASSERT_NOT_NULL (sc_);
TEST_ASSERT_GREATER_THAN (0, msg_size_);
const char msg_val = '1';
const int num_messages = 10;
......@@ -76,8 +78,8 @@ static void do_check (void *sb_, void *sc_, size_t msg_size_)
send_iov[i].iov_len = msg_size_;
memcpy (send_iov[i].iov_base, ref_msg, msg_size_);
// TODO: this assertion only checks if memcpy behaves as expected... remove this?
assert (memcmp (ref_msg, send_iov[i].iov_base, msg_size_) == 0);
// TODO: this assertion only checks if memcpy behaves as expected... remove this or assert something else?
TEST_ASSERT_EQUAL_HEX8_ARRAY (ref_msg, send_iov[i].iov_base, msg_size_);
}
// Test errors - zmq_recviov - null socket
......
......@@ -44,12 +44,12 @@ void tearDown ()
static void do_bind_and_verify (void *s_, const char *endpoint_)
{
int rc = zmq_bind (s_, endpoint_);
assert (rc == 0);
TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (s_, endpoint_));
char reported[255];
size_t size = 255;
rc = zmq_getsockopt (s_, ZMQ_LAST_ENDPOINT, reported, &size);
assert (rc == 0 && strcmp (reported, endpoint_) == 0);
TEST_ASSERT_SUCCESS_ERRNO (
zmq_getsockopt (s_, ZMQ_LAST_ENDPOINT, reported, &size));
TEST_ASSERT_EQUAL_STRING (endpoint_, reported);
}
void test_last_endpoint ()
......
......@@ -57,8 +57,8 @@ static void zap_handler (void *handler_)
char *routing_id = s_recv (handler_);
char *mechanism = s_recv (handler_);
assert (streq (version, "1.0"));
assert (streq (mechanism, "NULL"));
TEST_ASSERT_EQUAL_STRING ("1.0", version);
TEST_ASSERT_EQUAL_STRING ("NULL", mechanism);
s_sendmore (handler_, version);
s_sendmore (handler_, sequence);
......
......@@ -99,7 +99,7 @@ void test_monitor_basic ()
int event = get_monitor_event (client_mon, NULL, NULL);
if (event == ZMQ_EVENT_CONNECT_DELAYED)
event = get_monitor_event (client_mon, NULL, NULL);
assert (event == ZMQ_EVENT_CONNECTED);
TEST_ASSERT_EQUAL_INT (ZMQ_EVENT_CONNECTED, event);
expect_monitor_event (client_mon, ZMQ_EVENT_HANDSHAKE_SUCCEEDED);
event = get_monitor_event (client_mon, NULL, NULL);
if (event == ZMQ_EVENT_DISCONNECTED) {
......
......@@ -45,9 +45,8 @@ typedef void (*extra_func_t) (void *socket_);
void set_sockopt_bind_to_device (void *socket)
{
const char device[] = "lo";
int rc =
zmq_setsockopt (socket, ZMQ_BINDTODEVICE, &device, sizeof (device) - 1);
assert (rc == 0);
TEST_ASSERT_SUCCESS_ERRNO (
zmq_setsockopt (socket, ZMQ_BINDTODEVICE, &device, sizeof (device) - 1));
}
// TODO this is duplicated from test_pair_tcp
......
......@@ -30,6 +30,7 @@
#include "testutil.hpp"
#include "testutil_unity.hpp"
#include <unity.h>
#include <assert.h>
//
// Asynchronous proxy test using ZMQ_XPUB_NODROP and HWM:
......
......@@ -79,7 +79,7 @@ void test_req_correlate ()
// Receive request id 1
TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_recv (&msg, router, 0));
assert (zmq_msg_size (&msg) == sizeof (uint32_t));
TEST_ASSERT_EQUAL_UINT (sizeof (uint32_t), zmq_msg_size (&msg));
const uint32_t req_id = *static_cast<uint32_t *> (zmq_msg_data (&msg));
zmq_msg_t req_id_msg;
zmq_msg_init (&req_id_msg);
......
......@@ -56,8 +56,8 @@ static void zap_handler (void *handler_)
char *routing_id = s_recv (handler_);
char *mechanism = s_recv (handler_);
assert (streq (version, "1.0"));
assert (streq (mechanism, "NULL"));
TEST_ASSERT_EQUAL_STRING ("1.0", version);
TEST_ASSERT_EQUAL_STRING ("NULL", mechanism);
s_sendmore (handler_, version);
s_sendmore (handler_, sequence);
......
......@@ -57,9 +57,9 @@ static void zap_handler (void *zap_)
char *username = s_recv (zap_);
char *password = s_recv (zap_);
assert (streq (version, "1.0"));
assert (streq (mechanism, "PLAIN"));
assert (streq (routing_id, "IDENT"));
TEST_ASSERT_EQUAL_STRING ("1.0", version);
TEST_ASSERT_EQUAL_STRING ("PLAIN", mechanism);
TEST_ASSERT_EQUAL_STRING ("IDENT", routing_id);
s_sendmore (zap_, version);
s_sendmore (zap_, sequence);
......@@ -83,8 +83,7 @@ static void zap_handler (void *zap_)
free (username);
free (password);
}
int rc = zmq_close (zap_);
assert (rc == 0);
TEST_ASSERT_SUCCESS_ERRNO (zmq_close (zap_));
}
void *zap_thread;
......
......@@ -27,97 +27,82 @@
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "testutil.hpp"
#include "testutil_unity.hpp"
const char *SEQ_END = (const char *) 1;
void bounce (void *server_, void *client_)
{
const char content[] = "12345678ABCDEFGH12345678abcdefgh";
const char bounce_content[] = "12345678ABCDEFGH12345678abcdefgh";
// Send message from client to server
int rc = zmq_send (client_, content, 32, ZMQ_SNDMORE);
assert (rc == 32);
rc = zmq_send (client_, content, 32, 0);
assert (rc == 32);
static void send_bounce_msg (void *socket_)
{
send_string_expect_success (socket_, bounce_content, ZMQ_SNDMORE);
send_string_expect_success (socket_, bounce_content, 0);
}
// Receive message at server side
char buffer[32];
rc = zmq_recv (server_, buffer, 32, 0);
assert (rc == 32);
// Check that message is still the same
assert (memcmp (buffer, content, 32) == 0);
static void recv_bounce_msg (void *socket_)
{
recv_string_expect_success (socket_, bounce_content, 0);
int rcvmore;
size_t sz = sizeof (rcvmore);
rc = zmq_getsockopt (server_, ZMQ_RCVMORE, &rcvmore, &sz);
assert (rc == 0);
assert (rcvmore);
rc = zmq_recv (server_, buffer, 32, 0);
assert (rc == 32);
// Check that message is still the same
assert (memcmp (buffer, content, 32) == 0);
rc = zmq_getsockopt (server_, ZMQ_RCVMORE, &rcvmore, &sz);
assert (rc == 0);
assert (!rcvmore);
TEST_ASSERT_SUCCESS_ERRNO (
zmq_getsockopt (socket_, ZMQ_RCVMORE, &rcvmore, &sz));
TEST_ASSERT_TRUE (rcvmore);
recv_string_expect_success (socket_, bounce_content, 0);
TEST_ASSERT_SUCCESS_ERRNO (
zmq_getsockopt (socket_, ZMQ_RCVMORE, &rcvmore, &sz));
TEST_ASSERT_FALSE (rcvmore);
}
void bounce (void *server_, void *client_)
{
// Send message from client to server
send_bounce_msg (client_);
// Receive message at server side and
// check that message is still the same
recv_bounce_msg (server_);
// Send two parts back to client
rc = zmq_send (server_, buffer, 32, ZMQ_SNDMORE);
assert (rc == 32);
rc = zmq_send (server_, buffer, 32, 0);
assert (rc == 32);
send_bounce_msg (server_);
// Receive the two parts at the client side
rc = zmq_recv (client_, buffer, 32, 0);
assert (rc == 32);
// Check that message is still the same
assert (memcmp (buffer, content, 32) == 0);
rc = zmq_getsockopt (client_, ZMQ_RCVMORE, &rcvmore, &sz);
assert (rc == 0);
assert (rcvmore);
rc = zmq_recv (client_, buffer, 32, 0);
assert (rc == 32);
// Check that message is still the same
assert (memcmp (buffer, content, 32) == 0);
rc = zmq_getsockopt (client_, ZMQ_RCVMORE, &rcvmore, &sz);
assert (rc == 0);
assert (!rcvmore);
recv_bounce_msg (client_);
}
void expect_bounce_fail (void *server_, void *client_)
static void send_bounce_msg_may_fail (void *socket_)
{
const char *content = "12345678ABCDEFGH12345678abcdefgh";
char buffer[32];
int timeout = 250;
TEST_ASSERT_SUCCESS_ERRNO (
zmq_setsockopt (socket_, ZMQ_SNDTIMEO, &timeout, sizeof (int)));
int rc = zmq_send (socket_, bounce_content, 32, ZMQ_SNDMORE);
TEST_ASSERT_TRUE ((rc == 32) || ((rc == -1) && (errno == EAGAIN)));
rc = zmq_send (socket_, bounce_content, 32, 0);
TEST_ASSERT_TRUE ((rc == 32) || ((rc == -1) && (errno == EAGAIN)));
}
static void recv_bounce_msg_fail (void *socket_)
{
int timeout = 250;
char buffer[32];
TEST_ASSERT_SUCCESS_ERRNO (
zmq_setsockopt (socket_, ZMQ_RCVTIMEO, &timeout, sizeof (int)));
TEST_ASSERT_FAILURE_ERRNO (EAGAIN, zmq_recv (socket_, buffer, 32, 0));
}
void expect_bounce_fail (void *server_, void *client_)
{
// Send message from client to server
int rc = zmq_setsockopt (client_, ZMQ_SNDTIMEO, &timeout, sizeof (int));
assert (rc == 0);
rc = zmq_send (client_, content, 32, ZMQ_SNDMORE);
assert ((rc == 32) || ((rc == -1) && (errno == EAGAIN)));
rc = zmq_send (client_, content, 32, 0);
assert ((rc == 32) || ((rc == -1) && (errno == EAGAIN)));
send_bounce_msg_may_fail (client_);
// Receive message at server side (should not succeed)
rc = zmq_setsockopt (server_, ZMQ_RCVTIMEO, &timeout, sizeof (int));
assert (rc == 0);
rc = zmq_recv (server_, buffer, 32, 0);
assert (rc == -1);
assert (zmq_errno () == EAGAIN);
recv_bounce_msg_fail (server_);
// Send message from server to client to test other direction
// If connection failed, send may block, without a timeout
rc = zmq_setsockopt (server_, ZMQ_SNDTIMEO, &timeout, sizeof (int));
assert (rc == 0);
rc = zmq_send (server_, content, 32, ZMQ_SNDMORE);
assert (rc == 32 || (rc == -1 && zmq_errno () == EAGAIN));
rc = zmq_send (server_, content, 32, 0);
assert (rc == 32 || (rc == -1 && zmq_errno () == EAGAIN));
send_bounce_msg_may_fail (server_);
// Receive message at client side (should not succeed)
rc = zmq_setsockopt (client_, ZMQ_RCVTIMEO, &timeout, sizeof (int));
assert (rc == 0);
rc = zmq_recv (client_, buffer, 32, 0);
assert (rc == -1);
assert (zmq_errno () == EAGAIN);
recv_bounce_msg_fail (client_);
}
char *s_recv (void *socket_)
......@@ -155,12 +140,11 @@ void s_send_seq (void *socket_, ...)
bool end = data == SEQ_END;
if (!prev) {
int rc = zmq_send (socket_, 0, 0, end ? 0 : ZMQ_SNDMORE);
assert (rc != -1);
TEST_ASSERT_SUCCESS_ERRNO (
zmq_send (socket_, 0, 0, end ? 0 : ZMQ_SNDMORE));
} else {
int rc = zmq_send (socket_, prev, strlen (prev) + 1,
end ? 0 : ZMQ_SNDMORE);
assert (rc != -1);
TEST_ASSERT_SUCCESS_ERRNO (zmq_send (
socket_, prev, strlen (prev) + 1, end ? 0 : ZMQ_SNDMORE));
}
if (end)
break;
......@@ -181,21 +165,20 @@ void s_recv_seq (void *socket_, ...)
const char *data = va_arg (ap, const char *);
while (true) {
int rc = zmq_msg_recv (&msg, socket_, 0);
assert (rc != -1);
TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_recv (&msg, socket_, 0));
if (!data)
assert (zmq_msg_size (&msg) == 0);
TEST_ASSERT_EQUAL_INT (0, zmq_msg_size (&msg));
else
assert (strcmp (data, (const char *) zmq_msg_data (&msg)) == 0);
TEST_ASSERT_EQUAL_STRING (data, (const char *) zmq_msg_data (&msg));
data = va_arg (ap, const char *);
bool end = data == SEQ_END;
rc = zmq_getsockopt (socket_, ZMQ_RCVMORE, &more, &more_size);
assert (rc == 0);
TEST_ASSERT_SUCCESS_ERRNO (
zmq_getsockopt (socket_, ZMQ_RCVMORE, &more, &more_size));
assert (!more == end);
TEST_ASSERT_TRUE (!more == end);
if (end)
break;
}
......@@ -208,9 +191,8 @@ void close_zero_linger (void *socket_)
{
int linger = 0;
int rc = zmq_setsockopt (socket_, ZMQ_LINGER, &linger, sizeof (linger));
assert (rc == 0 || errno == ETERM);
rc = zmq_close (socket_);
assert (rc == 0);
TEST_ASSERT_TRUE (rc == 0 || errno == ETERM);
TEST_ASSERT_SUCCESS_ERRNO (zmq_close (socket_));
}
void setup_test_environment ()
......@@ -302,9 +284,9 @@ int is_tipc_available ()
int tipc = 0;
void *ctx = zmq_init (1);
assert (ctx);
TEST_ASSERT_NOT_NULL (ctx);
void *rep = zmq_socket (ctx, ZMQ_REP);
assert (rep);
TEST_ASSERT_NOT_NULL (rep);
tipc = zmq_bind (rep, "tipc://{5560,0,0}");
zmq_close (rep);
......@@ -352,13 +334,13 @@ sockaddr_in bind_bsd_socket (int socket)
saddr.sin_port = htons (PORT_6);
#endif
int rc = bind (socket, (struct sockaddr *) &saddr, sizeof (saddr));
assert (rc == 0);
TEST_ASSERT_SUCCESS_RAW_ERRNO (
bind (socket, (struct sockaddr *) &saddr, sizeof (saddr)));
#if !defined(_WIN32_WINNT) || (_WIN32_WINNT >= 0x0600)
socklen_t saddr_len = sizeof (saddr);
rc = getsockname (socket, (struct sockaddr *) &saddr, &saddr_len);
assert (rc == 0);
TEST_ASSERT_SUCCESS_RAW_ERRNO (
getsockname (socket, (struct sockaddr *) &saddr, &saddr_len));
#endif
return saddr;
......
......@@ -59,7 +59,6 @@
#undef NDEBUG
#include <time.h>
#include <assert.h>
#include <stdarg.h>
#include <string>
#include <string.h>
......
......@@ -146,7 +146,7 @@ void internal_manage_test_sockets (void *socket_, bool add_)
static void *test_sockets[MAX_TEST_SOCKETS];
static size_t test_socket_count = 0;
if (!socket_) {
assert (!add_);
TEST_ASSERT_FALSE (add_);
// force-close all sockets
if (test_socket_count) {
......
......@@ -62,7 +62,7 @@ class test_ip_resolver_t : public zmq::ip_resolver_t
unsigned lut_len = sizeof (dns_lut) / sizeof (dns_lut[0]);
struct addrinfo ai;
assert (service_ == NULL);
TEST_ASSERT_NULL (service_);
bool ipv6 = (hints_->ai_family == AF_INET6);
bool no_dns = (hints_->ai_flags & AI_NUMERICHOST) != 0;
......@@ -805,9 +805,7 @@ static void test_addr (int family_, const char *addr_, bool multicast_)
test_ip_resolver_t resolver (resolver_opts);
zmq::ip_addr_t addr;
int rc = resolver.resolve (&addr, addr_);
assert (rc == 0);
TEST_ASSERT_SUCCESS_ERRNO (resolver.resolve (&addr, addr_));
TEST_ASSERT_EQUAL (family_, addr.family ());
TEST_ASSERT_EQUAL (multicast_, addr.is_multicast ());
......
......@@ -27,22 +27,23 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
// On windows we can receive an IPv4 address even when an IPv6 is requested, if
// we're in this situation then we compare to 'expected_addr_v4_failover_'
// instead.
void validate_address(int family, const zmq::ip_addr_t *addr_,
const char *expected_addr_,
uint16_t expected_port_ = 0,
uint16_t expected_zone_ = 0,
const char *expected_addr_v4_failover_ = NULL)
void validate_address (int family,
const zmq::ip_addr_t *addr_,
const char *expected_addr_,
uint16_t expected_port_ = 0,
uint16_t expected_zone_ = 0,
const char *expected_addr_v4_failover_ = NULL)
{
#if defined ZMQ_HAVE_WINDOWS
if (family == AF_INET6 && expected_addr_v4_failover_ != NULL &&
addr_->family () == AF_INET) {
#if defined ZMQ_HAVE_WINDOWS
if (family == AF_INET6 && expected_addr_v4_failover_ != NULL
&& addr_->family () == AF_INET) {
// We've requested an IPv6 but the system gave us an IPv4, use the
// failover address
family = AF_INET;
expected_addr_ = expected_addr_v4_failover_;
}
#else
(void)expected_addr_v4_failover_;
(void) expected_addr_v4_failover_;
#endif
TEST_ASSERT_EQUAL (family, addr_->family ());
......@@ -51,7 +52,8 @@ void validate_address(int family, const zmq::ip_addr_t *addr_,
struct in6_addr expected_addr;
const sockaddr_in6 *ip6_addr = &addr_->ipv6;
assert (test_inet_pton (AF_INET6, expected_addr_, &expected_addr) == 1);
TEST_ASSERT_EQUAL (
1, test_inet_pton (AF_INET6, expected_addr_, &expected_addr));
int neq = memcmp (&ip6_addr->sin6_addr, &expected_addr,
sizeof (expected_addr_));
......@@ -63,7 +65,8 @@ void validate_address(int family, const zmq::ip_addr_t *addr_,
struct in_addr expected_addr;
const sockaddr_in *ip4_addr = &addr_->ipv4;
assert (test_inet_pton (AF_INET, expected_addr_, &expected_addr) == 1);
TEST_ASSERT_EQUAL (
1, test_inet_pton (AF_INET, expected_addr_, &expected_addr));
TEST_ASSERT_EQUAL (expected_addr.s_addr, ip4_addr->sin_addr.s_addr);
TEST_ASSERT_EQUAL (htons (expected_port_), ip4_addr->sin_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