0
0
mirror of https://github.com/zeromq/libzmq.git synced 2024-12-27 15:41:05 +08:00

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().
This commit is contained in:
Luca Boccassi 2020-04-26 14:37:08 +01:00
parent 727637082f
commit c81a973cd8
2 changed files with 36 additions and 12 deletions

View File

@ -55,12 +55,10 @@ int test_assert_success_message_errno_helper (int rc_,
return rc_; return rc_;
} }
int test_assert_success_message_raw_errno_helper (int rc_, int test_assert_success_message_raw_errno_helper (
const char *msg_, int rc_, const char *msg_, const char *expr_, int line_, bool zero)
const char *expr_,
int line_)
{ {
if (rc_ == -1) { if (rc_ == -1 || (zero && rc_ != 0)) {
#if defined ZMQ_HAVE_WINDOWS #if defined ZMQ_HAVE_WINDOWS
int current_errno = WSAGetLastError (); int current_errno = WSAGetLastError ();
#else #else
@ -70,14 +68,24 @@ int test_assert_success_message_raw_errno_helper (int rc_,
char buffer[512]; char buffer[512];
buffer[sizeof (buffer) - 1] = buffer[sizeof (buffer) - 1] =
0; // to ensure defined behavior with VC++ <= 2013 0; // to ensure defined behavior with VC++ <= 2013
snprintf (buffer, sizeof (buffer) - 1, "%s failed%s%s%s, errno = %i", snprintf (
expr_, msg_ ? " (additional info: " : "", msg_ ? msg_ : "", buffer, sizeof (buffer) - 1, "%s failed%s%s%s with %d, errno = %i/%s",
msg_ ? ")" : "", current_errno); expr_, msg_ ? " (additional info: " : "", msg_ ? msg_ : "",
msg_ ? ")" : "", rc_, current_errno, strerror (current_errno));
UNITY_TEST_FAIL (line_, buffer); UNITY_TEST_FAIL (line_, buffer);
} }
return rc_; 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 test_assert_failure_message_raw_errno_helper (
int rc_, int expected_errno_, const char *msg_, const char *expr_, int line_) int rc_, int expected_errno_, const char *msg_, const char *expr_, int line_)
{ {

View File

@ -43,10 +43,13 @@ int test_assert_success_message_errno_helper (int rc_,
const char *expr_, const char *expr_,
int line); int line);
int test_assert_success_message_raw_errno_helper (int rc_, int test_assert_success_message_raw_errno_helper (
const char *msg_, int rc_, const char *msg_, const char *expr_, int line, bool zero_ = false);
const char *expr_,
int line); 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 test_assert_failure_message_raw_errno_helper (
int rc_, int expected_errno_, const char *msg_, const char *expr_, int line); 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: // A typical use would be:
// TEST_ASSERT_SUCCESS_RAW_ERRNO (send (fd, buffer, 64, 0)); // TEST_ASSERT_SUCCESS_RAW_ERRNO (send (fd, buffer, 64, 0));
// In case of success, the result of the macro is the result of 'expr'. // 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) \ #define TEST_ASSERT_SUCCESS_RAW_ERRNO(expr) \
test_assert_success_message_raw_errno_helper (expr, NULL, #expr, __LINE__) 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 // 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 // 'error_code'. In case of an unexpected succces, or a failure with an
// unexpected error code, the assertion message includes the literal 'expr' // unexpected error code, the assertion message includes the literal 'expr'