From c81a973cd895eef8e69328fdc2e96be0c20562ce Mon Sep 17 00:00:00 2001 From: Luca Boccassi Date: Sun, 26 Apr 2020 14:37:08 +0100 Subject: [PATCH] Problem: assert macros not detecting errors from syscall that do not return -1 on failure Solution: add a new TEST_ASSERT_SUCCESS_RAW_ZERO_ERRNO macro so that it can check explicitly for non-zero values. This will be used for getaddrinfo(). --- tests/testutil_unity.cpp | 24 ++++++++++++++++-------- tests/testutil_unity.hpp | 24 ++++++++++++++++++++---- 2 files changed, 36 insertions(+), 12 deletions(-) diff --git a/tests/testutil_unity.cpp b/tests/testutil_unity.cpp index e35272de..1c273058 100644 --- a/tests/testutil_unity.cpp +++ b/tests/testutil_unity.cpp @@ -55,12 +55,10 @@ int test_assert_success_message_errno_helper (int rc_, return rc_; } -int test_assert_success_message_raw_errno_helper (int rc_, - const char *msg_, - const char *expr_, - int line_) +int test_assert_success_message_raw_errno_helper ( + int rc_, const char *msg_, const char *expr_, int line_, bool zero) { - if (rc_ == -1) { + if (rc_ == -1 || (zero && rc_ != 0)) { #if defined ZMQ_HAVE_WINDOWS int current_errno = WSAGetLastError (); #else @@ -70,14 +68,24 @@ int test_assert_success_message_raw_errno_helper (int rc_, char buffer[512]; buffer[sizeof (buffer) - 1] = 0; // to ensure defined behavior with VC++ <= 2013 - snprintf (buffer, sizeof (buffer) - 1, "%s failed%s%s%s, errno = %i", - expr_, msg_ ? " (additional info: " : "", msg_ ? msg_ : "", - msg_ ? ")" : "", current_errno); + snprintf ( + buffer, sizeof (buffer) - 1, "%s failed%s%s%s with %d, errno = %i/%s", + expr_, msg_ ? " (additional info: " : "", msg_ ? msg_ : "", + msg_ ? ")" : "", rc_, current_errno, strerror (current_errno)); UNITY_TEST_FAIL (line_, buffer); } return rc_; } +int test_assert_success_message_raw_zero_errno_helper (int rc_, + const char *msg_, + const char *expr_, + int line_) +{ + return test_assert_success_message_raw_errno_helper (rc_, msg_, expr_, + line_, true); +} + int test_assert_failure_message_raw_errno_helper ( int rc_, int expected_errno_, const char *msg_, const char *expr_, int line_) { diff --git a/tests/testutil_unity.hpp b/tests/testutil_unity.hpp index a682a49e..0be46db2 100644 --- a/tests/testutil_unity.hpp +++ b/tests/testutil_unity.hpp @@ -43,10 +43,13 @@ int test_assert_success_message_errno_helper (int rc_, const char *expr_, int line); -int test_assert_success_message_raw_errno_helper (int rc_, - const char *msg_, - const char *expr_, - int line); +int test_assert_success_message_raw_errno_helper ( + int rc_, const char *msg_, const char *expr_, int line, bool zero_ = false); + +int test_assert_success_message_raw_zero_errno_helper (int rc_, + const char *msg_, + const char *expr_, + int line); int test_assert_failure_message_raw_errno_helper ( int rc_, int expected_errno_, const char *msg_, const char *expr_, int line); @@ -88,9 +91,22 @@ int test_assert_failure_message_raw_errno_helper ( // A typical use would be: // TEST_ASSERT_SUCCESS_RAW_ERRNO (send (fd, buffer, 64, 0)); // In case of success, the result of the macro is the result of 'expr'. +// Success is strictly defined by a return value different from -1, as opposed +// to checking that it is 0, like TEST_ASSERT_FAILURE_RAW_ZERO_ERRNO does. #define TEST_ASSERT_SUCCESS_RAW_ERRNO(expr) \ test_assert_success_message_raw_errno_helper (expr, NULL, #expr, __LINE__) +// Asserts that the socket API 'expr' is successful. In case of a failure, the +// assertion message includes the literal 'expr' and the error code. +// A typical use would be: +// TEST_ASSERT_SUCCESS_RAW_ZERO_ERRNO (send (fd, buffer, 64, 0)); +// In case of success, the result of the macro is the result of 'expr'. +// Success is strictly defined by a return value of 0, as opposed to checking +// that it is not -1, like TEST_ASSERT_FAILURE_RAW_ERRNO does. +#define TEST_ASSERT_SUCCESS_RAW_ZERO_ERRNO(expr) \ + test_assert_success_message_raw_zero_errno_helper (expr, NULL, #expr, \ + __LINE__) + // Asserts that the socket API 'expr' is not successful, and the error code is // 'error_code'. In case of an unexpected succces, or a failure with an // unexpected error code, the assertion message includes the literal 'expr'