Commit c81a973c authored by Luca Boccassi's avatar Luca Boccassi

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().
parent 72763708
......@@ -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_)
{
......
......@@ -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'
......
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