]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
tests: add new ASSERT_OK_OR macro
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Sat, 28 Jun 2025 17:11:13 +0000 (19:11 +0200)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Tue, 1 Jul 2025 15:51:49 +0000 (17:51 +0200)
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
src/test/test-tests.c

index d7c92504875a77e7739cb5bcb3d51205b8ac83b4..5d1dccba9b1ecc56b293d65a770f22a46b5bbb68 100644 (file)
@@ -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)
index 745fbeed3a2c3fba62aa96f86eafd442da102364..40112a6762496e302b2ef2a6fdf0b685c2804372 100644 (file)
@@ -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);