From 367b32555448c1558770f942505d7d53c19ccb47 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= Date: Sat, 28 Jun 2025 19:11:13 +0200 Subject: [PATCH] tests: add new ASSERT_OK_OR macro IN_SET() fails if __VA_ARGS__ is just one item. I inserted a bogus 0 item into the check to work around this. --- src/shared/tests.h | 16 ++++++++++++++++ src/test/test-tests.c | 10 ++++++++++ 2 files changed, 26 insertions(+) diff --git a/src/shared/tests.h b/src/shared/tests.h index d7c92504875..5d1dccba9b1 100644 --- a/src/shared/tests.h +++ b/src/shared/tests.h @@ -182,6 +182,22 @@ _noreturn_ void log_test_failed_internal(const char *file, int line, const char }) #endif +#ifdef __COVERITY__ +# define ASSERT_OK_OR(expr, ...) \ + ({ \ + typeof(expr) _result = (expr); \ + __coverity_check__(_result >= 0 || IN_SET(_result, 0, __VA_ARGS__) \ + }) +#else +# define ASSERT_OK_OR(expr, ...) \ + ({ \ + typeof(expr) _result = (expr); \ + if (_result < 0 && !IN_SET(_result, 0, __VA_ARGS__)) \ + log_test_failed("\"%s\" failed with unexpected error: %"PRIiMAX"/%s", \ + #expr, (intmax_t) _result, STRERROR(_result)); \ + }) +#endif + /* For functions that return a boolean on success and a negative errno on failure. */ #ifdef __COVERITY__ # define ASSERT_OK_POSITIVE(expr) __coverity_check__((expr) > 0) diff --git a/src/test/test-tests.c b/src/test/test-tests.c index 745fbeed3a2..40112a67624 100644 --- a/src/test/test-tests.c +++ b/src/test/test-tests.c @@ -127,4 +127,14 @@ TEST(ASSERT) { ASSERT_SIGNAL(ASSERT_NE_ID128(SD_ID128_NULL, SD_ID128_NULL), SIGABRT); } +TEST(ASSERT_OK_OR) { + ASSERT_OK_OR(0, -EINVAL, -EUCLEAN); + ASSERT_OK_OR(99, -EINVAL, -EUCLEAN); + ASSERT_OK_OR(-EINVAL, -EINVAL, -EUCLEAN); + ASSERT_OK_OR(-EUCLEAN, -EUCLEAN); + ASSERT_OK_OR(-1, -EPERM); + + ASSERT_SIGNAL(ASSERT_OK_OR(-1, -2), SIGABRT); +} + DEFINE_TEST_MAIN(LOG_INFO); -- 2.47.3