mirror of
https://github.com/morgan9e/systemd
synced 2026-04-14 08:25:20 +09:00
Prepare for reducing transitive includes in tests.h (#37414)
This commit is contained in:
@@ -367,3 +367,89 @@ const char* ci_environment(void) {
|
||||
|
||||
return (ans = NULL);
|
||||
}
|
||||
|
||||
int run_test_table(const TestFunc *start, const TestFunc *end) {
|
||||
_cleanup_strv_free_ char **tests = NULL;
|
||||
int r = EXIT_SUCCESS;
|
||||
bool ran = false;
|
||||
const char *e;
|
||||
|
||||
if (!start)
|
||||
return r;
|
||||
|
||||
e = getenv("TESTFUNCS");
|
||||
if (e) {
|
||||
r = strv_split_full(&tests, e, ":", EXTRACT_DONT_COALESCE_SEPARATORS);
|
||||
if (r < 0)
|
||||
return log_error_errno(r, "Failed to parse $TESTFUNCS: %m");
|
||||
}
|
||||
|
||||
for (const TestFunc *t = ALIGN_PTR(start); t + 1 <= end; t = ALIGN_PTR(t + 1)) {
|
||||
|
||||
if (tests && !strv_contains(tests, t->name))
|
||||
continue;
|
||||
|
||||
if (t->sd_booted && sd_booted() <= 0) {
|
||||
log_info("/* systemd not booted, skipping %s */", t->name);
|
||||
if (t->has_ret && r == EXIT_SUCCESS)
|
||||
r = EXIT_TEST_SKIP;
|
||||
} else {
|
||||
log_info("/* %s */", t->name);
|
||||
|
||||
if (t->has_ret) {
|
||||
int r2 = t->f.int_func();
|
||||
if (r == EXIT_SUCCESS)
|
||||
r = r2;
|
||||
} else
|
||||
t->f.void_func();
|
||||
}
|
||||
|
||||
ran = true;
|
||||
}
|
||||
|
||||
if (!ran)
|
||||
return log_error_errno(SYNTHETIC_ERRNO(ENXIO), "No matching tests found.");
|
||||
|
||||
return r;
|
||||
}
|
||||
|
||||
void test_prepare(int argc, char *argv[], int log_level) {
|
||||
save_argc_argv(argc, argv);
|
||||
test_setup_logging(log_level);
|
||||
}
|
||||
|
||||
int assert_signal_internal(void) {
|
||||
siginfo_t siginfo = {};
|
||||
int r;
|
||||
|
||||
r = fork();
|
||||
if (r < 0)
|
||||
return -errno;
|
||||
|
||||
if (r == 0) {
|
||||
/* Speed things up by never even attempting to generate a coredump */
|
||||
(void) prctl(PR_SET_DUMPABLE, 0);
|
||||
/* But still set an rlimit just in case */
|
||||
(void) setrlimit(RLIMIT_CORE, &RLIMIT_MAKE_CONST(0));
|
||||
return 0;
|
||||
}
|
||||
|
||||
r = wait_for_terminate(r, &siginfo);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
return siginfo.si_status;
|
||||
}
|
||||
|
||||
|
||||
void log_test_failed_internal(const char *file, int line, const char *func, const char *format, ...) {
|
||||
va_list ap;
|
||||
|
||||
va_start(ap, format);
|
||||
DISABLE_WARNING_FORMAT_NONLITERAL;
|
||||
log_internalv(LOG_ERR, 0, file, line, func, format, ap);
|
||||
REENABLE_WARNING;
|
||||
va_end(ap);
|
||||
|
||||
abort();
|
||||
}
|
||||
|
||||
@@ -91,13 +91,6 @@ bool can_memlock(void);
|
||||
size_t name##_len = 0; \
|
||||
assert_se(unhexmem_full(hex, strlen_ptr(hex), false, &name, &name##_len) >= 0);
|
||||
|
||||
#define TEST_REQ_RUNNING_SYSTEMD(x) \
|
||||
if (sd_booted() > 0) { \
|
||||
x; \
|
||||
} else { \
|
||||
printf("systemd not booted, skipping '%s'\n", #x); \
|
||||
}
|
||||
|
||||
/* Provide a convenient way to check if we're running in CI. */
|
||||
const char* ci_environment(void);
|
||||
|
||||
@@ -138,63 +131,19 @@ extern const TestFunc _weak_ __stop_SYSTEMD_TEST_TABLE[];
|
||||
#define TEST_LOG_FUNC() \
|
||||
log_info("/* %s() */", __func__)
|
||||
|
||||
static inline int run_test_table(void) {
|
||||
_cleanup_strv_free_ char **tests = NULL;
|
||||
int r = EXIT_SUCCESS;
|
||||
bool ran = false;
|
||||
const char *e;
|
||||
int run_test_table(const TestFunc *start, const TestFunc *end);
|
||||
|
||||
if (!__start_SYSTEMD_TEST_TABLE)
|
||||
return r;
|
||||
|
||||
e = getenv("TESTFUNCS");
|
||||
if (e) {
|
||||
r = strv_split_full(&tests, e, ":", EXTRACT_DONT_COALESCE_SEPARATORS);
|
||||
if (r < 0)
|
||||
return log_error_errno(r, "Failed to parse $TESTFUNCS: %m");
|
||||
}
|
||||
|
||||
for (const TestFunc *t = ALIGN_PTR(__start_SYSTEMD_TEST_TABLE);
|
||||
t + 1 <= __stop_SYSTEMD_TEST_TABLE;
|
||||
t = ALIGN_PTR(t + 1)) {
|
||||
|
||||
if (tests && !strv_contains(tests, t->name))
|
||||
continue;
|
||||
|
||||
if (t->sd_booted && sd_booted() <= 0) {
|
||||
log_info("/* systemd not booted, skipping %s */", t->name);
|
||||
if (t->has_ret && r == EXIT_SUCCESS)
|
||||
r = EXIT_TEST_SKIP;
|
||||
} else {
|
||||
log_info("/* %s */", t->name);
|
||||
|
||||
if (t->has_ret) {
|
||||
int r2 = t->f.int_func();
|
||||
if (r == EXIT_SUCCESS)
|
||||
r = r2;
|
||||
} else
|
||||
t->f.void_func();
|
||||
}
|
||||
|
||||
ran = true;
|
||||
}
|
||||
|
||||
if (!ran)
|
||||
return log_error_errno(SYNTHETIC_ERRNO(ENXIO), "No matching tests found.");
|
||||
|
||||
return r;
|
||||
}
|
||||
void test_prepare(int argc, char *argv[], int log_level);
|
||||
|
||||
#define DEFINE_TEST_MAIN_FULL(log_level, intro, outro) \
|
||||
int main(int argc, char *argv[]) { \
|
||||
int (*_intro)(void) = intro; \
|
||||
int (*_outro)(void) = outro; \
|
||||
int _r, _q; \
|
||||
test_setup_logging(log_level); \
|
||||
save_argc_argv(argc, argv); \
|
||||
test_prepare(argc, argv, log_level); \
|
||||
_r = _intro ? _intro() : EXIT_SUCCESS; \
|
||||
if (_r == EXIT_SUCCESS) \
|
||||
_r = run_test_table(); \
|
||||
_r = run_test_table(__start_SYSTEMD_TEST_TABLE, __stop_SYSTEMD_TEST_TABLE); \
|
||||
_q = _outro ? _outro() : EXIT_SUCCESS; \
|
||||
static_destruct(); \
|
||||
if (_r < 0) \
|
||||
@@ -211,6 +160,11 @@ static inline int run_test_table(void) {
|
||||
#define DEFINE_TEST_MAIN(log_level) \
|
||||
DEFINE_TEST_MAIN_FULL(log_level, NULL, NULL)
|
||||
|
||||
_noreturn_ void log_test_failed_internal(const char *file, int line, const char *func, const char *format, ...) _printf_(4,5);
|
||||
|
||||
#define log_test_failed(format, ...) \
|
||||
log_test_failed_internal(PROJECT_FILE, __LINE__, __func__, "%s:%i: Assertion failed: " format, PROJECT_FILE, __LINE__, ##__VA_ARGS__)
|
||||
|
||||
#define DECIMAL_STR_FMT(x) _Generic((x), \
|
||||
char: "%c", \
|
||||
bool: "%d", \
|
||||
@@ -227,14 +181,11 @@ static inline int run_test_table(void) {
|
||||
#ifdef __COVERITY__
|
||||
#define ASSERT_OK(expr) __coverity_check__((expr) >= 0)
|
||||
#else
|
||||
#define ASSERT_OK(expr) \
|
||||
({ \
|
||||
typeof(expr) _result = (expr); \
|
||||
if (_result < 0) { \
|
||||
log_error_errno(_result, "%s:%i: Assertion failed: expected \"%s\" to succeed, but got error: %m", \
|
||||
PROJECT_FILE, __LINE__, #expr); \
|
||||
abort(); \
|
||||
} \
|
||||
#define ASSERT_OK(expr) \
|
||||
({ \
|
||||
typeof(expr) _result = (expr); \
|
||||
if (_result < 0) \
|
||||
log_test_failed("Expected \"%s\" to succeed, but got error: %s", #expr, STRERROR(_result)); \
|
||||
})
|
||||
#endif
|
||||
|
||||
@@ -242,259 +193,204 @@ static inline int run_test_table(void) {
|
||||
#ifdef __COVERITY__
|
||||
#define ASSERT_OK_POSITIVE(expr) __coverity_check__((expr) > 0)
|
||||
#else
|
||||
#define ASSERT_OK_POSITIVE(expr) \
|
||||
({ \
|
||||
typeof(expr) _result = (expr); \
|
||||
if (_result < 0) { \
|
||||
log_error_errno(_result, "%s:%i: Assertion failed: expected \"%s\" to succeed, but got error: %m", \
|
||||
PROJECT_FILE, __LINE__, #expr); \
|
||||
abort(); \
|
||||
} \
|
||||
if (_result == 0) { \
|
||||
log_error("%s:%i: Assertion failed: expected \"%s\" to be positive, but it is zero.", \
|
||||
PROJECT_FILE, __LINE__, #expr); \
|
||||
abort(); \
|
||||
} \
|
||||
#define ASSERT_OK_POSITIVE(expr) \
|
||||
({ \
|
||||
typeof(expr) _result = (expr); \
|
||||
if (_result < 0) \
|
||||
log_test_failed("Expected \"%s\" to succeed, but got error: %s", #expr, STRERROR(_result)); \
|
||||
if (_result == 0) \
|
||||
log_test_failed("Expected \"%s\" to be positive, but it is zero.", #expr); \
|
||||
})
|
||||
#endif
|
||||
|
||||
#ifdef __COVERITY__
|
||||
#define ASSERT_OK_ZERO(expr) __coverity_check__((expr) == 0)
|
||||
#else
|
||||
#define ASSERT_OK_ZERO(expr) \
|
||||
({ \
|
||||
typeof(expr) _result = (expr); \
|
||||
if (_result < 0) { \
|
||||
log_error_errno(_result, "%s:%i: Assertion failed: expected \"%s\" to succeed, but got error: %m", \
|
||||
PROJECT_FILE, __LINE__, #expr); \
|
||||
abort(); \
|
||||
} \
|
||||
if (_result != 0) { \
|
||||
char _sexpr[DECIMAL_STR_MAX(typeof(expr))]; \
|
||||
xsprintf(_sexpr, DECIMAL_STR_FMT(_result), _result); \
|
||||
log_error("%s:%i: Assertion failed: expected \"%s\" to be zero, but it is %s.", \
|
||||
PROJECT_FILE, __LINE__, #expr, _sexpr); \
|
||||
abort(); \
|
||||
} \
|
||||
#define ASSERT_OK_ZERO(expr) \
|
||||
({ \
|
||||
typeof(expr) _result = (expr); \
|
||||
if (_result < 0) \
|
||||
log_test_failed("Expected \"%s\" to succeed, but got error: %s", #expr, STRERROR(_result)); \
|
||||
if (_result != 0) { \
|
||||
char _sexpr[DECIMAL_STR_MAX(typeof(expr))]; \
|
||||
xsprintf(_sexpr, DECIMAL_STR_FMT(_result), _result); \
|
||||
log_test_failed("Expected \"%s\" to be zero, but it is %s.", #expr, _sexpr); \
|
||||
} \
|
||||
})
|
||||
#endif
|
||||
|
||||
#ifdef __COVERITY__
|
||||
#define ASSERT_OK_EQ(expr1, expr2) __coverity_check__((expr1) == (expr2))
|
||||
#else
|
||||
#define ASSERT_OK_EQ(expr1, expr2) \
|
||||
({ \
|
||||
typeof(expr1) _expr1 = (expr1); \
|
||||
typeof(expr2) _expr2 = (expr2); \
|
||||
if (_expr1 < 0) { \
|
||||
log_error_errno(_expr1, "%s:%i: Assertion failed: expected \"%s\" to succeed, but got error: %m", \
|
||||
PROJECT_FILE, __LINE__, #expr1); \
|
||||
abort(); \
|
||||
} \
|
||||
if (_expr1 != _expr2) { \
|
||||
char _sexpr1[DECIMAL_STR_MAX(typeof(expr1))]; \
|
||||
char _sexpr2[DECIMAL_STR_MAX(typeof(expr2))]; \
|
||||
xsprintf(_sexpr1, DECIMAL_STR_FMT(_expr1), _expr1); \
|
||||
xsprintf(_sexpr2, DECIMAL_STR_FMT(_expr2), _expr2); \
|
||||
log_error("%s:%i: Assertion failed: expected \"%s == %s\", got %s != %s", \
|
||||
PROJECT_FILE, __LINE__, #expr1, #expr2, _sexpr1, _sexpr2); \
|
||||
abort(); \
|
||||
} \
|
||||
#define ASSERT_OK_EQ(expr1, expr2) \
|
||||
({ \
|
||||
typeof(expr1) _expr1 = (expr1); \
|
||||
typeof(expr2) _expr2 = (expr2); \
|
||||
if (_expr1 < 0) \
|
||||
log_test_failed("Expected \"%s\" to succeed, but got error: %s", #expr1, STRERROR(_expr1)); \
|
||||
if (_expr1 != _expr2) { \
|
||||
char _sexpr1[DECIMAL_STR_MAX(typeof(expr1))]; \
|
||||
char _sexpr2[DECIMAL_STR_MAX(typeof(expr2))]; \
|
||||
xsprintf(_sexpr1, DECIMAL_STR_FMT(_expr1), _expr1); \
|
||||
xsprintf(_sexpr2, DECIMAL_STR_FMT(_expr2), _expr2); \
|
||||
log_test_failed("Expected \"%s == %s\", got %s != %s", #expr1, #expr2, _sexpr1, _sexpr2); \
|
||||
} \
|
||||
})
|
||||
#endif
|
||||
|
||||
#ifdef __COVERITY__
|
||||
#define ASSERT_OK_ERRNO(expr) __coverity_check__((expr) >= 0)
|
||||
#else
|
||||
#define ASSERT_OK_ERRNO(expr) \
|
||||
({ \
|
||||
typeof(expr) _result = (expr); \
|
||||
if (_result < 0) { \
|
||||
log_error_errno(errno, "%s:%i: Assertion failed: expected \"%s\" to succeed, but got error: %m", \
|
||||
PROJECT_FILE, __LINE__, #expr); \
|
||||
abort(); \
|
||||
} \
|
||||
#define ASSERT_OK_ERRNO(expr) \
|
||||
({ \
|
||||
typeof(expr) _result = (expr); \
|
||||
if (_result < 0) \
|
||||
log_test_failed("Expected \"%s\" to succeed, but got error: %s", #expr, STRERROR(errno)); \
|
||||
})
|
||||
#endif
|
||||
|
||||
#ifdef __COVERITY__
|
||||
#define ASSERT_OK_ZERO_ERRNO(expr) __coverity_check__((expr) == 0)
|
||||
#else
|
||||
#define ASSERT_OK_ZERO_ERRNO(expr) \
|
||||
({ \
|
||||
typeof(expr) _result = (expr); \
|
||||
if (_result < 0) { \
|
||||
log_error_errno(errno, "%s:%i: Assertion failed: expected \"%s\" to succeed, but got error: %m", \
|
||||
PROJECT_FILE, __LINE__, #expr); \
|
||||
abort(); \
|
||||
} \
|
||||
if (_result != 0) { \
|
||||
char _sexpr[DECIMAL_STR_MAX(typeof(expr))]; \
|
||||
xsprintf(_sexpr, DECIMAL_STR_FMT(_result), _result); \
|
||||
log_error("%s:%i: Assertion failed: expected \"%s\" to be zero, but it is %s.", \
|
||||
PROJECT_FILE, __LINE__, #expr, _sexpr); \
|
||||
abort(); \
|
||||
} \
|
||||
#define ASSERT_OK_ZERO_ERRNO(expr) \
|
||||
({ \
|
||||
typeof(expr) _result = (expr); \
|
||||
if (_result < 0) \
|
||||
log_test_failed("Expected \"%s\" to succeed, but got error: %s", #expr, STRERROR(errno)); \
|
||||
if (_result != 0) { \
|
||||
char _sexpr[DECIMAL_STR_MAX(typeof(expr))]; \
|
||||
xsprintf(_sexpr, DECIMAL_STR_FMT(_result), _result); \
|
||||
log_test_failed("Expected \"%s\" to be zero, but it is %s.", #expr, _sexpr); \
|
||||
} \
|
||||
})
|
||||
#endif
|
||||
|
||||
#ifdef __COVERITY__
|
||||
#define ASSERT_OK_EQ_ERRNO(expr1, expr2) __coverity_check__((expr1) == (expr2))
|
||||
#else
|
||||
#define ASSERT_OK_EQ_ERRNO(expr1, expr2) \
|
||||
({ \
|
||||
typeof(expr1) _expr1 = (expr1); \
|
||||
typeof(expr2) _expr2 = (expr2); \
|
||||
if (_expr1 < 0) { \
|
||||
log_error_errno(errno, "%s:%i: Assertion failed: expected \"%s\" to succeed, but got error: %m", \
|
||||
PROJECT_FILE, __LINE__, #expr1); \
|
||||
abort(); \
|
||||
} \
|
||||
if (_expr1 != _expr2) { \
|
||||
char _sexpr1[DECIMAL_STR_MAX(typeof(expr1))]; \
|
||||
char _sexpr2[DECIMAL_STR_MAX(typeof(expr2))]; \
|
||||
xsprintf(_sexpr1, DECIMAL_STR_FMT(_expr1), _expr1); \
|
||||
xsprintf(_sexpr2, DECIMAL_STR_FMT(_expr2), _expr2); \
|
||||
log_error("%s:%i: Assertion failed: expected \"%s == %s\", but %s != %s", \
|
||||
PROJECT_FILE, __LINE__, #expr1, #expr2, _sexpr1, _sexpr2); \
|
||||
abort(); \
|
||||
} \
|
||||
#define ASSERT_OK_EQ_ERRNO(expr1, expr2) \
|
||||
({ \
|
||||
typeof(expr1) _expr1 = (expr1); \
|
||||
typeof(expr2) _expr2 = (expr2); \
|
||||
if (_expr1 < 0) \
|
||||
log_test_failed("Expected \"%s\" to succeed, but got error: %s", #expr1, STRERROR(errno)); \
|
||||
if (_expr1 != _expr2) { \
|
||||
char _sexpr1[DECIMAL_STR_MAX(typeof(expr1))]; \
|
||||
char _sexpr2[DECIMAL_STR_MAX(typeof(expr2))]; \
|
||||
xsprintf(_sexpr1, DECIMAL_STR_FMT(_expr1), _expr1); \
|
||||
xsprintf(_sexpr2, DECIMAL_STR_FMT(_expr2), _expr2); \
|
||||
log_test_failed("Expected \"%s == %s\", but %s != %s", #expr1, #expr2, _sexpr1, _sexpr2); \
|
||||
} \
|
||||
})
|
||||
#endif
|
||||
|
||||
#ifdef __COVERITY__
|
||||
#define ASSERT_FAIL(expr) __coverity_check__((expr) < 0)
|
||||
#else
|
||||
#define ASSERT_FAIL(expr) \
|
||||
({ \
|
||||
typeof(expr) _result = (expr); \
|
||||
if (_result >= 0) { \
|
||||
log_error("%s:%i: Assertion failed: expected \"%s\" to fail, but it succeeded.", \
|
||||
PROJECT_FILE, __LINE__, #expr); \
|
||||
abort(); \
|
||||
} \
|
||||
#define ASSERT_FAIL(expr) \
|
||||
({ \
|
||||
typeof(expr) _result = (expr); \
|
||||
if (_result >= 0) \
|
||||
log_test_failed("Expected \"%s\" to fail, but it succeeded.", #expr); \
|
||||
})
|
||||
#endif
|
||||
|
||||
#ifdef __COVERITY__
|
||||
#define ASSERT_ERROR(expr1, expr2) __coverity_check__((expr1) == -(expr2))
|
||||
#else
|
||||
#define ASSERT_ERROR(expr1, expr2) \
|
||||
({ \
|
||||
int _expr1 = (expr1); \
|
||||
int _expr2 = (expr2); \
|
||||
if (_expr1 >= 0) { \
|
||||
log_error("%s:%i: Assertion failed: expected \"%s\" to fail with error \"%s\", but it succeeded", \
|
||||
PROJECT_FILE, __LINE__, #expr1, STRERROR(_expr2)); \
|
||||
abort(); \
|
||||
} else if (-_expr1 != _expr2) { \
|
||||
log_error_errno(_expr1, "%s:%i: Assertion failed: expected \"%s\" to fail with error \"%s\", but got the following error: %m", \
|
||||
PROJECT_FILE, __LINE__, #expr1, STRERROR(_expr2)); \
|
||||
abort(); \
|
||||
} \
|
||||
#define ASSERT_ERROR(expr1, expr2) \
|
||||
({ \
|
||||
int _expr1 = (expr1); \
|
||||
int _expr2 = (expr2); \
|
||||
if (_expr1 >= 0) \
|
||||
log_test_failed("Expected \"%s\" to fail with error \"%s\", but it succeeded", \
|
||||
#expr1, STRERROR(_expr2)); \
|
||||
else if (-_expr1 != _expr2) \
|
||||
log_test_failed("Expected \"%s\" to fail with error \"%s\", but got the following error: %s", \
|
||||
#expr1, STRERROR(_expr2), STRERROR(_expr1)); \
|
||||
})
|
||||
#endif
|
||||
|
||||
#ifdef __COVERITY__
|
||||
#define ASSERT_ERROR_ERRNO(expr1, expr2) __coverity_check__((expr1) < 0 && errno == (expr2))
|
||||
#else
|
||||
#define ASSERT_ERROR_ERRNO(expr1, expr2) \
|
||||
({ \
|
||||
int _expr1 = (expr1); \
|
||||
int _expr2 = (expr2); \
|
||||
if (_expr1 >= 0) { \
|
||||
log_error("%s:%i: Assertion failed: expected \"%s\" to fail with error \"%s\", but it succeeded", \
|
||||
PROJECT_FILE, __LINE__, #expr1, STRERROR(_expr2)); \
|
||||
abort(); \
|
||||
} else if (errno != _expr2) { \
|
||||
log_error_errno(errno, "%s:%i: Assertion failed: expected \"%s\" to fail with error \"%s\", but got the following error: %m", \
|
||||
PROJECT_FILE, __LINE__, #expr1, STRERROR(errno)); \
|
||||
abort(); \
|
||||
} \
|
||||
#define ASSERT_ERROR_ERRNO(expr1, expr2) \
|
||||
({ \
|
||||
int _expr1 = (expr1); \
|
||||
int _expr2 = (expr2); \
|
||||
if (_expr1 >= 0) \
|
||||
log_test_failed("Expected \"%s\" to fail with error \"%s\", but it succeeded", \
|
||||
#expr1, STRERROR(_expr2)); \
|
||||
else if (errno != _expr2) \
|
||||
log_test_failed("Expected \"%s\" to fail with error \"%s\", but got the following error: %s", \
|
||||
#expr1, STRERROR(_expr2), STRERROR(errno)); \
|
||||
})
|
||||
#endif
|
||||
|
||||
#ifdef __COVERITY__
|
||||
#define ASSERT_TRUE(expr) __coverity_check__(!!(expr))
|
||||
#else
|
||||
#define ASSERT_TRUE(expr) \
|
||||
({ \
|
||||
if (!(expr)) { \
|
||||
log_error("%s:%i: Assertion failed: expected \"%s\" to be true", \
|
||||
PROJECT_FILE, __LINE__, #expr); \
|
||||
abort(); \
|
||||
} \
|
||||
#define ASSERT_TRUE(expr) \
|
||||
({ \
|
||||
if (!(expr)) \
|
||||
log_test_failed("Expected \"%s\" to be true", #expr); \
|
||||
})
|
||||
#endif
|
||||
|
||||
#ifdef __COVERITY__
|
||||
#define ASSERT_FALSE(expr) __coverity_check__(!(expr))
|
||||
#else
|
||||
#define ASSERT_FALSE(expr) \
|
||||
({ \
|
||||
if ((expr)) { \
|
||||
log_error("%s:%i: Assertion failed: expected \"%s\" to be false", \
|
||||
PROJECT_FILE, __LINE__, #expr); \
|
||||
abort(); \
|
||||
} \
|
||||
#define ASSERT_FALSE(expr) \
|
||||
({ \
|
||||
if ((expr)) \
|
||||
log_test_failed("Expected \"%s\" to be false", #expr); \
|
||||
})
|
||||
#endif
|
||||
|
||||
#ifdef __COVERITY__
|
||||
#define ASSERT_NULL(expr) __coverity_check__((expr) == NULL)
|
||||
#else
|
||||
#define ASSERT_NULL(expr) \
|
||||
({ \
|
||||
typeof(expr) _result = (expr); \
|
||||
if (_result != NULL) { \
|
||||
log_error("%s:%i: Assertion failed: expected \"%s\" to be NULL, got \"%p\" != NULL", \
|
||||
PROJECT_FILE, __LINE__, #expr, _result); \
|
||||
abort(); \
|
||||
} \
|
||||
#define ASSERT_NULL(expr) \
|
||||
({ \
|
||||
typeof(expr) _result = (expr); \
|
||||
if (_result != NULL) \
|
||||
log_test_failed("Expected \"%s\" to be NULL, got \"%p\" != NULL", #expr, _result); \
|
||||
})
|
||||
#endif
|
||||
|
||||
#ifdef __COVERITY__
|
||||
#define ASSERT_NOT_NULL(expr) __coverity_check__((expr) != NULL)
|
||||
#else
|
||||
#define ASSERT_NOT_NULL(expr) \
|
||||
({ \
|
||||
typeof(expr) _result = (expr); \
|
||||
if (_result == NULL) { \
|
||||
log_error("%s:%i: Assertion failed: expected \"%s\" to be not NULL", \
|
||||
PROJECT_FILE, __LINE__, #expr); \
|
||||
abort(); \
|
||||
} \
|
||||
\
|
||||
_result; \
|
||||
#define ASSERT_NOT_NULL(expr) \
|
||||
({ \
|
||||
typeof(expr) _result = (expr); \
|
||||
if (_result == NULL) \
|
||||
log_test_failed("Expected \"%s\" to be not NULL", #expr); \
|
||||
_result; \
|
||||
})
|
||||
#endif
|
||||
|
||||
#ifdef __COVERITY__
|
||||
#define ASSERT_STREQ(expr1, expr2) __coverity_check__(streq_ptr((expr1), (expr2)))
|
||||
#else
|
||||
#define ASSERT_STREQ(expr1, expr2) \
|
||||
({ \
|
||||
const char *_expr1 = (expr1), *_expr2 = (expr2); \
|
||||
if (!streq_ptr(_expr1, _expr2)) { \
|
||||
log_error("%s:%i: Assertion failed: expected \"%s == %s\", got \"%s != %s\"", \
|
||||
PROJECT_FILE, __LINE__, #expr1, #expr2, strnull(_expr1), strnull(_expr2)); \
|
||||
abort(); \
|
||||
} \
|
||||
#define ASSERT_STREQ(expr1, expr2) \
|
||||
({ \
|
||||
const char *_expr1 = (expr1), *_expr2 = (expr2); \
|
||||
if (!streq_ptr(_expr1, _expr2)) \
|
||||
log_test_failed("Expected \"%s == %s\", got \"%s != %s\"", \
|
||||
#expr1, #expr2, strnull(_expr1), strnull(_expr2)); \
|
||||
})
|
||||
#endif
|
||||
|
||||
#ifdef __COVERITY__
|
||||
#define ASSERT_PTR_EQ(expr1, expr2) __coverity_check__((expr1) == (expr2))
|
||||
#else
|
||||
#define ASSERT_PTR_EQ(expr1, expr2) \
|
||||
({ \
|
||||
const void *_expr1 = (expr1), *_expr2 = (expr2); \
|
||||
if (_expr1 != _expr2) { \
|
||||
log_error("%s:%i: Assertion failed: expected \"%s == %s\", got \"0x%p != 0x%p\"", \
|
||||
PROJECT_FILE, __LINE__, #expr1, #expr2, _expr1, _expr2); \
|
||||
abort(); \
|
||||
} \
|
||||
#define ASSERT_PTR_EQ(expr1, expr2) \
|
||||
({ \
|
||||
const void *_expr1 = (expr1), *_expr2 = (expr2); \
|
||||
if (_expr1 != _expr2) \
|
||||
log_test_failed("Expected \"%s == %s\", got \"0x%p != 0x%p\"", \
|
||||
#expr1, #expr2, _expr1, _expr2); \
|
||||
})
|
||||
#endif
|
||||
|
||||
@@ -513,9 +409,8 @@ static inline int run_test_table(void) {
|
||||
char _sexpr2[DECIMAL_STR_MAX(typeof(expr2))]; \
|
||||
xsprintf(_sexpr1, DECIMAL_STR_FMT(_expr1), _expr1); \
|
||||
xsprintf(_sexpr2, DECIMAL_STR_FMT(_expr2), _expr2); \
|
||||
log_error("%s:%i: Assertion failed: expected \"%s == %s\", but %s != %s", \
|
||||
PROJECT_FILE, __LINE__, #expr1, #expr2, _sexpr1, _sexpr2); \
|
||||
abort(); \
|
||||
log_test_failed("Expected \"%s == %s\", but %s != %s", \
|
||||
#expr1, #expr2, _sexpr1, _sexpr2); \
|
||||
} \
|
||||
})
|
||||
#endif
|
||||
@@ -532,9 +427,8 @@ static inline int run_test_table(void) {
|
||||
char _sexpr2[DECIMAL_STR_MAX(typeof(expr2))]; \
|
||||
xsprintf(_sexpr1, DECIMAL_STR_FMT(_expr1), _expr1); \
|
||||
xsprintf(_sexpr2, DECIMAL_STR_FMT(_expr2), _expr2); \
|
||||
log_error("%s:%i: Assertion failed: expected \"%s >= %s\", but %s < %s", \
|
||||
PROJECT_FILE, __LINE__, #expr1, #expr2, _sexpr1, _sexpr2); \
|
||||
abort(); \
|
||||
log_test_failed("Expected \"%s >= %s\", but %s < %s", \
|
||||
#expr1, #expr2, _sexpr1, _sexpr2); \
|
||||
} \
|
||||
})
|
||||
#endif
|
||||
@@ -551,9 +445,8 @@ static inline int run_test_table(void) {
|
||||
char _sexpr2[DECIMAL_STR_MAX(typeof(expr2))]; \
|
||||
xsprintf(_sexpr1, DECIMAL_STR_FMT(_expr1), _expr1); \
|
||||
xsprintf(_sexpr2, DECIMAL_STR_FMT(_expr2), _expr2); \
|
||||
log_error("%s:%i: Assertion failed: expected \"%s <= %s\", but %s > %s", \
|
||||
PROJECT_FILE, __LINE__, #expr1, #expr2, _sexpr1, _sexpr2); \
|
||||
abort(); \
|
||||
log_test_failed("Expected \"%s <= %s\", but %s > %s", \
|
||||
#expr1, #expr2, _sexpr1, _sexpr2); \
|
||||
} \
|
||||
})
|
||||
#endif
|
||||
@@ -570,9 +463,8 @@ static inline int run_test_table(void) {
|
||||
char _sexpr2[DECIMAL_STR_MAX(typeof(expr2))]; \
|
||||
xsprintf(_sexpr1, DECIMAL_STR_FMT(_expr1), _expr1); \
|
||||
xsprintf(_sexpr2, DECIMAL_STR_FMT(_expr2), _expr2); \
|
||||
log_error("%s:%i: Assertion failed: expected \"%s != %s\", but %s == %s", \
|
||||
PROJECT_FILE, __LINE__, #expr1, #expr2, _sexpr1, _sexpr2); \
|
||||
abort(); \
|
||||
log_test_failed("Expected \"%s != %s\", but %s == %s", \
|
||||
#expr1, #expr2, _sexpr1, _sexpr2); \
|
||||
} \
|
||||
})
|
||||
#endif
|
||||
@@ -589,9 +481,8 @@ static inline int run_test_table(void) {
|
||||
char _sexpr2[DECIMAL_STR_MAX(typeof(expr2))]; \
|
||||
xsprintf(_sexpr1, DECIMAL_STR_FMT(_expr1), _expr1); \
|
||||
xsprintf(_sexpr2, DECIMAL_STR_FMT(_expr2), _expr2); \
|
||||
log_error("%s:%i: Assertion failed: expected \"%s > %s\", but %s <= %s", \
|
||||
PROJECT_FILE, __LINE__, #expr1, #expr2, _sexpr1, _sexpr2); \
|
||||
abort(); \
|
||||
log_test_failed("Expected \"%s > %s\", but %s <= %s", \
|
||||
#expr1, #expr2, _sexpr1, _sexpr2); \
|
||||
} \
|
||||
})
|
||||
#endif
|
||||
@@ -608,37 +499,29 @@ static inline int run_test_table(void) {
|
||||
char _sexpr2[DECIMAL_STR_MAX(typeof(expr2))]; \
|
||||
xsprintf(_sexpr1, DECIMAL_STR_FMT(_expr1), _expr1); \
|
||||
xsprintf(_sexpr2, DECIMAL_STR_FMT(_expr2), _expr2); \
|
||||
log_error("%s:%i: Assertion failed: expected \"%s < %s\", but %s >= %s", \
|
||||
PROJECT_FILE, __LINE__, #expr1, #expr2, _sexpr1, _sexpr2); \
|
||||
abort(); \
|
||||
log_test_failed("Expected \"%s < %s\", but %s >= %s", \
|
||||
#expr1, #expr2, _sexpr1, _sexpr2); \
|
||||
} \
|
||||
})
|
||||
#endif
|
||||
|
||||
int assert_signal_internal(void);
|
||||
|
||||
#ifdef __COVERITY__
|
||||
#define ASSERT_SIGNAL(expr, signal) __coverity_check__(((expr), false))
|
||||
#else
|
||||
#define ASSERT_SIGNAL(expr, signal) \
|
||||
({ \
|
||||
ASSERT_TRUE(SIGNAL_VALID(signal)); \
|
||||
siginfo_t _siginfo = {}; \
|
||||
int _pid = fork(); \
|
||||
ASSERT_OK(_pid); \
|
||||
if (_pid == 0) { \
|
||||
/* Speed things up by never even attempting to generate a coredump */ \
|
||||
(void) prctl(PR_SET_DUMPABLE, 0); \
|
||||
/* But still set an rlimit just in case */ \
|
||||
(void) setrlimit(RLIMIT_CORE, &RLIMIT_MAKE_CONST(0)); \
|
||||
int _r = assert_signal_internal(); \
|
||||
ASSERT_OK_ERRNO(_r); \
|
||||
if (_r == 0) { \
|
||||
expr; \
|
||||
_exit(EXIT_SUCCESS); \
|
||||
} \
|
||||
(void) wait_for_terminate(_pid, &_siginfo); \
|
||||
if (_siginfo.si_status != signal) { \
|
||||
log_error("%s:%i: Assertion failed: \"%s\" died with signal %s, but %s was expected", \
|
||||
PROJECT_FILE, __LINE__, #expr, signal_to_string(_siginfo.si_status), \
|
||||
signal_to_string(signal)); \
|
||||
abort(); \
|
||||
} \
|
||||
if (_r != signal) \
|
||||
log_test_failed("\"%s\" died with signal %s, but %s was expected", \
|
||||
#expr, signal_to_string(_r), signal_to_string(signal)); \
|
||||
})
|
||||
#endif
|
||||
|
||||
@@ -649,13 +532,10 @@ static inline int run_test_table(void) {
|
||||
({ \
|
||||
typeof(expr1) _expr1 = (expr1); \
|
||||
typeof(expr2) _expr2 = (expr2); \
|
||||
if (!sd_id128_equal(_expr1, _expr2)) { \
|
||||
log_error("%s:%i: Assertion failed: \"%s == %s\", but %s != %s", \
|
||||
PROJECT_FILE, __LINE__, \
|
||||
#expr1, #expr2, \
|
||||
SD_ID128_TO_STRING(_expr1), SD_ID128_TO_STRING(_expr2)); \
|
||||
abort(); \
|
||||
} \
|
||||
if (!sd_id128_equal(_expr1, _expr2)) \
|
||||
log_test_failed("\"%s == %s\", but %s != %s", \
|
||||
#expr1, #expr2, \
|
||||
SD_ID128_TO_STRING(_expr1), SD_ID128_TO_STRING(_expr2)); \
|
||||
})
|
||||
#endif
|
||||
|
||||
@@ -666,13 +546,10 @@ static inline int run_test_table(void) {
|
||||
({ \
|
||||
typeof(expr1) _expr1 = (expr1); \
|
||||
typeof(expr2) _expr2 = (expr2); \
|
||||
if (sd_id128_equal(_expr1, _expr2)) { \
|
||||
log_error("%s:%i: Assertion failed: \"%s != %s\", but %s == %s", \
|
||||
PROJECT_FILE, __LINE__, \
|
||||
#expr1, #expr2, \
|
||||
SD_ID128_TO_STRING(_expr1), SD_ID128_TO_STRING(_expr2)); \
|
||||
abort(); \
|
||||
} \
|
||||
if (sd_id128_equal(_expr1, _expr2)) \
|
||||
log_test_failed("\"%s != %s\", but %s == %s", \
|
||||
#expr1, #expr2, \
|
||||
SD_ID128_TO_STRING(_expr1), SD_ID128_TO_STRING(_expr2)); \
|
||||
})
|
||||
#endif
|
||||
|
||||
@@ -688,13 +565,10 @@ static inline int run_test_table(void) {
|
||||
({ \
|
||||
typeof(expr1) _expr1 = (expr1); \
|
||||
typeof(expr2) _expr2 = (expr2); \
|
||||
if (!efi_guid_equal(_expr1, _expr2)) { \
|
||||
log_error("%s:%i: Assertion failed: expected \"%s == %s\", but " EFI_GUID_Fmt \
|
||||
" != " EFI_GUID_Fmt, \
|
||||
PROJECT_FILE, __LINE__, \
|
||||
#expr1, #expr2, \
|
||||
EFI_GUID_Arg(*_expr1), EFI_GUID_Arg(*_expr2)); \
|
||||
abort(); \
|
||||
} \
|
||||
if (!efi_guid_equal(_expr1, _expr2)) \
|
||||
log_test_failed("Expected \"%s == %s\", but " EFI_GUID_Fmt \
|
||||
" != " EFI_GUID_Fmt, \
|
||||
#expr1, #expr2, \
|
||||
EFI_GUID_Arg(*_expr1), EFI_GUID_Arg(*_expr2)); \
|
||||
})
|
||||
#endif
|
||||
|
||||
@@ -113,7 +113,8 @@ TEST(pid_get_comm) {
|
||||
(void) parse_pid(saved_argv[1], &pid);
|
||||
test_pid_get_comm_one(pid);
|
||||
} else {
|
||||
TEST_REQ_RUNNING_SYSTEMD(test_pid_get_comm_one(1));
|
||||
if (sd_booted() > 0)
|
||||
test_pid_get_comm_one(1);
|
||||
test_pid_get_comm_one(getpid());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user