| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
#pragma once |
| 3 |
|
| 4 |
#include <unistd.h> |
| 5 |
|
| 6 |
#include "errno-list.h" |
| 7 |
#include "shared-forward.h" |
| 8 |
#include "log.h" |
| 9 |
#include "log-assert-critical.h" |
| 10 |
#include "static-destruct.h" /* IWYU pragma: keep */ |
| 11 |
#include "signal-util.h" /* IWYU pragma: keep */ |
| 12 |
#include "stdio-util.h" |
| 13 |
#include "string-util.h" /* IWYU pragma: keep */ |
| 14 |
|
| 15 |
static inline void log_set_assert_return_is_criticalp(bool *p) { |
| 16 |
log_set_assert_return_is_critical(*p); |
| 17 |
} |
| 18 |
|
| 19 |
#define _SAVE_ASSERT_RETURN_IS_CRITICAL(b) \ |
| 20 |
_unused_ _cleanup_(log_set_assert_return_is_criticalp) bool b = \ |
| 21 |
log_get_assert_return_is_critical() |
| 22 |
|
| 23 |
#define SAVE_ASSERT_RETURN_IS_CRITICAL \ |
| 24 |
_SAVE_ASSERT_RETURN_IS_CRITICAL(UNIQ_T(saved, UNIQ)) |
| 25 |
|
| 26 |
#define ASSERT_RETURN_IS_CRITICAL(b, expr) \ |
| 27 |
({ \ |
| 28 |
SAVE_ASSERT_RETURN_IS_CRITICAL; \ |
| 29 |
log_set_assert_return_is_critical(b); \ |
| 30 |
expr; \ |
| 31 |
}) |
| 32 |
|
| 33 |
#define ASSERT_RETURN_EXPECTED(expr) ASSERT_RETURN_IS_CRITICAL(false, expr) |
| 34 |
#define ASSERT_RETURN_EXPECTED_SE(expr) ASSERT_RETURN_EXPECTED(assert_se(expr)); |
| 35 |
|
| 36 |
static inline bool manager_errno_skip_test(int r) { |
| 37 |
return IN_SET(ABS(r), |
| 38 |
EPERM, |
| 39 |
EACCES, |
| 40 |
EADDRINUSE, |
| 41 |
EHOSTDOWN, |
| 42 |
ENOENT, |
| 43 |
ENOMEDIUM /* cannot determine cgroup */ |
| 44 |
); |
| 45 |
} |
| 46 |
|
| 47 |
char* setup_fake_runtime_dir(void); |
| 48 |
int enter_cgroup_subroot(char **ret_cgroup); |
| 49 |
int enter_cgroup_root(char **ret_cgroup); |
| 50 |
int get_testdata_dir(const char *suffix, char **ret); |
| 51 |
const char* get_catalog_dir(void); |
| 52 |
bool slow_tests_enabled(void); |
| 53 |
void test_setup_logging(int level); |
| 54 |
|
| 55 |
#define log_tests_skipped(fmt, ...) \ |
| 56 |
({ \ |
| 57 |
log_notice("%s: " fmt ", skipping tests.", \ |
| 58 |
program_invocation_short_name, \ |
| 59 |
##__VA_ARGS__); \ |
| 60 |
EXIT_TEST_SKIP; \ |
| 61 |
}) |
| 62 |
|
| 63 |
#define log_tests_skipped_errno(error, fmt, ...) \ |
| 64 |
({ \ |
| 65 |
log_notice_errno(error, \ |
| 66 |
"%s: " fmt ", skipping tests: %m", \ |
| 67 |
program_invocation_short_name, \ |
| 68 |
##__VA_ARGS__); \ |
| 69 |
EXIT_TEST_SKIP; \ |
| 70 |
}) |
| 71 |
|
| 72 |
int write_tmpfile(char *pattern, const char *contents); |
| 73 |
|
| 74 |
bool have_namespaces(void); |
| 75 |
bool userns_has_single_user(void); |
| 76 |
|
| 77 |
/* We use the small but non-trivial limit here */ |
| 78 |
#define CAN_MEMLOCK_SIZE (512 * 1024U) |
| 79 |
bool can_memlock(void); |
| 80 |
|
| 81 |
int define_hex_ptr_internal(const char *hex, void **name, size_t *name_len); |
| 82 |
|
| 83 |
/* Define void* buffer and size_t length variables from a hex string. */ |
| 84 |
#define DEFINE_HEX_PTR(name, hex) \ |
| 85 |
_cleanup_free_ void *name = NULL; \ |
| 86 |
size_t name##_len = 0; \ |
| 87 |
ASSERT_OK(define_hex_ptr_internal(hex, &name, &name##_len)) |
| 88 |
|
| 89 |
/* Provide a convenient way to check if we're running in CI. */ |
| 90 |
const char* ci_environment(void); |
| 91 |
|
| 92 |
/* Note: see the comment on struct Option in options.h for why _alignptr_ is required here. */ |
| 93 |
typedef struct _alignptr_ TestFunc { |
| 94 |
union f { |
| 95 |
void (*void_func)(void); |
| 96 |
int (*int_func)(void); |
| 97 |
} f; |
| 98 |
const char * const name; |
| 99 |
bool has_ret:1; |
| 100 |
bool sd_booted:1; |
| 101 |
} TestFunc; |
| 102 |
assert_cc(sizeof(TestFunc) % sizeof(void*) == 0); |
| 103 |
|
| 104 |
/* See static-destruct.h for an explanation of how this works. */ |
| 105 |
#define REGISTER_TEST(func, ...) \ |
| 106 |
_section_("SYSTEMD_TEST_TABLE") _alignptr_ _used_ _retain_ _variable_no_sanitize_address_ \ |
| 107 |
static const TestFunc UNIQ_T(static_test_table_entry, UNIQ) = { \ |
| 108 |
.f = (union f) &(func), \ |
| 109 |
.name = STRINGIFY(func), \ |
| 110 |
.has_ret = __builtin_types_compatible_p(typeof((union f){}.int_func), typeof(&(func))), \ |
| 111 |
##__VA_ARGS__ \ |
| 112 |
} |
| 113 |
|
| 114 |
extern const TestFunc __start_SYSTEMD_TEST_TABLE[]; |
| 115 |
extern const TestFunc __stop_SYSTEMD_TEST_TABLE[]; |
| 116 |
|
| 117 |
#define TEST(name, ...) \ |
| 118 |
static void test_##name(void); \ |
| 119 |
REGISTER_TEST(test_##name, ##__VA_ARGS__); \ |
| 120 |
static void test_##name(void) |
| 121 |
|
| 122 |
#define TEST_RET(name, ...) \ |
| 123 |
static int test_##name(void); \ |
| 124 |
REGISTER_TEST(test_##name, ##__VA_ARGS__); \ |
| 125 |
static int test_##name(void) |
| 126 |
|
| 127 |
#define TEST_LOG_FUNC() \ |
| 128 |
log_info("/* %s() */", __func__) |
| 129 |
|
| 130 |
int run_test_table(const TestFunc *start, const TestFunc *end); |
| 131 |
|
| 132 |
void test_prepare(int argc, char *argv[], int log_level); |
| 133 |
|
| 134 |
#define DEFINE_TEST_MAIN_FULL(log_level, intro, outro) \ |
| 135 |
int main(int argc, char *argv[]) { \ |
| 136 |
int (*_intro)(void) = intro; \ |
| 137 |
int (*_outro)(void) = outro; \ |
| 138 |
int _r, _q; \ |
| 139 |
test_prepare(argc, argv, log_level); \ |
| 140 |
_r = _intro ? _intro() : EXIT_SUCCESS; \ |
| 141 |
if (_r == EXIT_SUCCESS) \ |
| 142 |
_r = run_test_table(__start_SYSTEMD_TEST_TABLE, __stop_SYSTEMD_TEST_TABLE); \ |
| 143 |
_q = _outro ? _outro() : EXIT_SUCCESS; \ |
| 144 |
static_destruct(); \ |
| 145 |
if (_r < 0) \ |
| 146 |
return EXIT_FAILURE; \ |
| 147 |
if (_r != EXIT_SUCCESS) \ |
| 148 |
return _r; \ |
| 149 |
if (_q < 0) \ |
| 150 |
return EXIT_FAILURE; \ |
| 151 |
return _q; \ |
| 152 |
} |
| 153 |
|
| 154 |
#define DEFINE_TEST_MAIN_WITH_INTRO(log_level, intro) \ |
| 155 |
DEFINE_TEST_MAIN_FULL(log_level, intro, NULL) |
| 156 |
#define DEFINE_TEST_MAIN(log_level) \ |
| 157 |
DEFINE_TEST_MAIN_FULL(log_level, NULL, NULL) |
| 158 |
|
| 159 |
_noreturn_ void log_test_failed_internal(const char *file, int line, const char *func, const char *format, ...) _printf_(4,5); |
| 160 |
|
| 161 |
#define log_test_failed(format, ...) \ |
| 162 |
log_test_failed_internal(PROJECT_FILE, __LINE__, __func__, "%s:%i: Assertion failed: " format, PROJECT_FILE, __LINE__, ##__VA_ARGS__) |
| 163 |
|
| 164 |
#define DECIMAL_STR_FMT(x) _Generic((x), \ |
| 165 |
char: "%c", \ |
| 166 |
bool: "%d", \ |
| 167 |
unsigned char: "%d", \ |
| 168 |
short: "%hd", \ |
| 169 |
unsigned short: "%hu", \ |
| 170 |
int: "%d", \ |
| 171 |
unsigned: "%u", \ |
| 172 |
long: "%ld", \ |
| 173 |
unsigned long: "%lu", \ |
| 174 |
long long: "%lld", \ |
| 175 |
unsigned long long: "%llu") |
| 176 |
|
| 177 |
#ifdef __COVERITY__ |
| 178 |
# define ASSERT_OK(expr) \ |
| 179 |
({ \ |
| 180 |
typeof(expr) _result = (expr); \ |
| 181 |
__coverity_check__(_result >= 0); \ |
| 182 |
_result; \ |
| 183 |
}) |
| 184 |
#else |
| 185 |
# define ASSERT_OK(expr) \ |
| 186 |
({ \ |
| 187 |
typeof(expr) _result = (expr); \ |
| 188 |
if (_result < 0) \ |
| 189 |
log_test_failed("Expected \"%s\" to succeed, but got error: %"PRIiMAX"/%s", \ |
| 190 |
#expr, (intmax_t) _result, ERRNO_NAME(_result)); \ |
| 191 |
_result; \ |
| 192 |
}) |
| 193 |
#endif |
| 194 |
|
| 195 |
#ifdef __COVERITY__ |
| 196 |
# define ASSERT_OK_OR(expr, ...) \ |
| 197 |
({ \ |
| 198 |
typeof(expr) _result = (expr); \ |
| 199 |
__coverity_check__(_result >= 0 || IN_SET(_result, 0, __VA_ARGS__); \ |
| 200 |
_result; \ |
| 201 |
}) |
| 202 |
#else |
| 203 |
# define ASSERT_OK_OR(expr, ...) \ |
| 204 |
({ \ |
| 205 |
typeof(expr) _result = (expr); \ |
| 206 |
if (_result < 0 && !IN_SET(_result, 0, __VA_ARGS__)) \ |
| 207 |
log_test_failed("\"%s\" failed with unexpected error: %"PRIiMAX"/%s", \ |
| 208 |
#expr, (intmax_t) _result, ERRNO_NAME(_result)); \ |
| 209 |
_result; \ |
| 210 |
}) |
| 211 |
#endif |
| 212 |
|
| 213 |
/* For functions that return a boolean on success and a negative errno on failure. */ |
| 214 |
#ifdef __COVERITY__ |
| 215 |
# define ASSERT_OK_POSITIVE(expr) \ |
| 216 |
({ \ |
| 217 |
typeof(expr) _result = (expr); \ |
| 218 |
__coverity_check__(_result > 0); \ |
| 219 |
_result; \ |
| 220 |
}) |
| 221 |
#else |
| 222 |
# define ASSERT_OK_POSITIVE(expr) \ |
| 223 |
({ \ |
| 224 |
typeof(expr) _result = (expr); \ |
| 225 |
if (_result < 0) \ |
| 226 |
log_test_failed("Expected \"%s\" to succeed, but got error: %"PRIiMAX"/%s", \ |
| 227 |
#expr, (intmax_t) _result, ERRNO_NAME(_result)); \ |
| 228 |
if (_result == 0) \ |
| 229 |
log_test_failed("Expected \"%s\" to be positive, but it is zero.", #expr); \ |
| 230 |
_result; \ |
| 231 |
}) |
| 232 |
#endif |
| 233 |
|
| 234 |
#ifdef __COVERITY__ |
| 235 |
# define ASSERT_OK_ZERO(expr) \ |
| 236 |
({ \ |
| 237 |
typeof(expr) _result = (expr); \ |
| 238 |
__coverity_check__(_result == 0); \ |
| 239 |
_result; \ |
| 240 |
}) |
| 241 |
#else |
| 242 |
# define ASSERT_OK_ZERO(expr) \ |
| 243 |
({ \ |
| 244 |
typeof(expr) _result = (expr); \ |
| 245 |
if (_result < 0) \ |
| 246 |
log_test_failed("Expected \"%s\" to succeed, but got error: %"PRIiMAX"/%s", \ |
| 247 |
#expr, (intmax_t) _result, ERRNO_NAME(_result)); \ |
| 248 |
if (_result != 0) \ |
| 249 |
log_test_failed("Expected \"%s\" to be zero, but it is %"PRIiMAX".", \ |
| 250 |
#expr, (intmax_t) _result); \ |
| 251 |
_result; \ |
| 252 |
}) |
| 253 |
#endif |
| 254 |
|
| 255 |
#ifdef __COVERITY__ |
| 256 |
# define ASSERT_OK_EQ(expr1, expr2) \ |
| 257 |
({ \ |
| 258 |
typeof(expr1) _expr1 = (expr1); \ |
| 259 |
typeof(expr2) _expr2 = (expr2); \ |
| 260 |
__coverity_check__(_expr1 == _expr2); \ |
| 261 |
_expr1; \ |
| 262 |
}) |
| 263 |
#else |
| 264 |
# define ASSERT_OK_EQ(expr1, expr2) \ |
| 265 |
({ \ |
| 266 |
typeof(expr1) _expr1 = (expr1); \ |
| 267 |
typeof(expr2) _expr2 = (expr2); \ |
| 268 |
if (_expr1 < 0) \ |
| 269 |
log_test_failed("Expected \"%s\" to succeed, but got error: %"PRIiMAX"/%s", \ |
| 270 |
#expr1, (intmax_t) _expr1, ERRNO_NAME(_expr1)); \ |
| 271 |
if (_expr1 != _expr2) \ |
| 272 |
log_test_failed("Expected \"%s == %s\", got %"PRIiMAX" != %"PRIiMAX, \ |
| 273 |
#expr1, #expr2, (intmax_t) _expr1, (intmax_t) _expr2); \ |
| 274 |
_expr1; \ |
| 275 |
}) |
| 276 |
#endif |
| 277 |
|
| 278 |
#ifdef __COVERITY__ |
| 279 |
# define ASSERT_OK_NE(expr1, expr2) \ |
| 280 |
({ \ |
| 281 |
typeof(expr1) _expr1 = (expr1); \ |
| 282 |
typeof(expr2) _expr2 = (expr2); \ |
| 283 |
__coverity_check__(_expr1 != _expr2); \ |
| 284 |
_expr1; \ |
| 285 |
}) |
| 286 |
#else |
| 287 |
# define ASSERT_OK_NE(expr1, expr2) \ |
| 288 |
({ \ |
| 289 |
typeof(expr1) _expr1 = (expr1); \ |
| 290 |
typeof(expr2) _expr2 = (expr2); \ |
| 291 |
if (_expr1 < 0) \ |
| 292 |
log_test_failed("Expected \"%s\" to succeed, but got error: %"PRIiMAX"/%s", \ |
| 293 |
#expr1, (intmax_t) _expr1, ERRNO_NAME(_expr1)); \ |
| 294 |
if (_expr1 == _expr2) \ |
| 295 |
log_test_failed("Expected \"%s != %s\", got %"PRIiMAX" != %"PRIiMAX, \ |
| 296 |
#expr1, #expr2, (intmax_t) _expr1, (intmax_t) _expr2); \ |
| 297 |
_expr1; \ |
| 298 |
}) |
| 299 |
#endif |
| 300 |
|
| 301 |
/* For functions that return a boolean on success and set errno on failure. */ |
| 302 |
#ifdef __COVERITY__ |
| 303 |
# define ASSERT_OK_ERRNO(expr) \ |
| 304 |
({ \ |
| 305 |
typeof(expr) _result = (expr); \ |
| 306 |
__coverity_check__(_result >= 0); \ |
| 307 |
_result; \ |
| 308 |
}) |
| 309 |
#else |
| 310 |
# define ASSERT_OK_ERRNO(expr) \ |
| 311 |
({ \ |
| 312 |
typeof(expr) _result = (expr); \ |
| 313 |
if (_result < 0) \ |
| 314 |
log_test_failed("Expected \"%s\" to succeed, but got errno: %d/%s", \ |
| 315 |
#expr, errno, ERRNO_NAME(errno)); \ |
| 316 |
_result; \ |
| 317 |
}) |
| 318 |
#endif |
| 319 |
|
| 320 |
#ifdef __COVERITY__ |
| 321 |
# define ASSERT_OK_ZERO_ERRNO(expr) \ |
| 322 |
({ \ |
| 323 |
typeof(expr) _result = (expr); \ |
| 324 |
__coverity_check__(_result == 0); \ |
| 325 |
_result; \ |
| 326 |
}) |
| 327 |
#else |
| 328 |
# define ASSERT_OK_ZERO_ERRNO(expr) \ |
| 329 |
({ \ |
| 330 |
typeof(expr) _result = (expr); \ |
| 331 |
if (_result < 0) \ |
| 332 |
log_test_failed("Expected \"%s\" to succeed, but got errno: %d/%s", \ |
| 333 |
#expr, errno, ERRNO_NAME(errno)); \ |
| 334 |
if (_result != 0) \ |
| 335 |
log_test_failed("Expected \"%s\" to be zero, but it is %"PRIiMAX".", \ |
| 336 |
#expr, (intmax_t) _result); \ |
| 337 |
_result; \ |
| 338 |
}) |
| 339 |
#endif |
| 340 |
|
| 341 |
#ifdef __COVERITY__ |
| 342 |
# define ASSERT_OK_EQ_ERRNO(expr1, expr2) \ |
| 343 |
({ \ |
| 344 |
typeof(expr1) _expr1 = (expr1); \ |
| 345 |
typeof(expr2) _expr2 = (expr2); \ |
| 346 |
__coverity_check__(_expr1 == _expr2); \ |
| 347 |
_expr1; \ |
| 348 |
}) |
| 349 |
#else |
| 350 |
# define ASSERT_OK_EQ_ERRNO(expr1, expr2) \ |
| 351 |
({ \ |
| 352 |
typeof(expr1) _expr1 = (expr1); \ |
| 353 |
typeof(expr2) _expr2 = (expr2); \ |
| 354 |
if (_expr1 < 0) \ |
| 355 |
log_test_failed("Expected \"%s\" to succeed, but got errno: %d/%s", \ |
| 356 |
#expr1, errno, ERRNO_NAME(errno)); \ |
| 357 |
if (_expr1 != _expr2) \ |
| 358 |
log_test_failed("Expected \"%s == %s\", but %"PRIiMAX" != %"PRIiMAX, \ |
| 359 |
#expr1, #expr2, (intmax_t) _expr1, (intmax_t) _expr2); \ |
| 360 |
_expr1; \ |
| 361 |
}) |
| 362 |
#endif |
| 363 |
|
| 364 |
#ifdef __COVERITY__ |
| 365 |
# define ASSERT_FAIL(expr) \ |
| 366 |
({ \ |
| 367 |
typeof(expr) _result = (expr); \ |
| 368 |
__coverity_check__(_result < 0); \ |
| 369 |
_result; \ |
| 370 |
}) |
| 371 |
#else |
| 372 |
# define ASSERT_FAIL(expr) \ |
| 373 |
({ \ |
| 374 |
typeof(expr) _result = (expr); \ |
| 375 |
if (_result >= 0) \ |
| 376 |
log_test_failed("Expected \"%s\" to fail, but it succeeded.", #expr); \ |
| 377 |
_result; \ |
| 378 |
}) |
| 379 |
#endif |
| 380 |
|
| 381 |
#ifdef __COVERITY__ |
| 382 |
# define ASSERT_ERROR(expr1, expr2) \ |
| 383 |
({ \ |
| 384 |
int _expr1 = (expr1); \ |
| 385 |
int _expr2 = (expr2); \ |
| 386 |
__coverity_check__((_expr1) == -(_expr2)); \ |
| 387 |
_expr1; \ |
| 388 |
}) |
| 389 |
#else |
| 390 |
# define ASSERT_ERROR(expr1, expr2) \ |
| 391 |
({ \ |
| 392 |
int _expr1 = (expr1); \ |
| 393 |
int _expr2 = (expr2); \ |
| 394 |
if (_expr1 >= 0) \ |
| 395 |
log_test_failed("Expected \"%s\" to fail with error %d/%s, but it succeeded", \ |
| 396 |
#expr1, -_expr2, ERRNO_NAME(_expr2)); \ |
| 397 |
else if (-_expr1 != _expr2) \ |
| 398 |
log_test_failed("Expected \"%s\" to fail with error %d/%s, but got %d/%s", \ |
| 399 |
#expr1, -_expr2, ERRNO_NAME(_expr2), _expr1, ERRNO_NAME(_expr1)); \ |
| 400 |
_expr1; \ |
| 401 |
}) |
| 402 |
#endif |
| 403 |
|
| 404 |
#ifdef __COVERITY__ |
| 405 |
# define ASSERT_ERROR_ERRNO(expr1, expr2) \ |
| 406 |
({ \ |
| 407 |
int _expr1 = (expr1); \ |
| 408 |
int _expr2 = (expr2); \ |
| 409 |
__coverity_check__(_expr1 < 0 && errno == _expr2); \ |
| 410 |
_expr1; \ |
| 411 |
}) |
| 412 |
#else |
| 413 |
# define ASSERT_ERROR_ERRNO(expr1, expr2) \ |
| 414 |
({ \ |
| 415 |
int _expr1 = (expr1); \ |
| 416 |
int _expr2 = (expr2); \ |
| 417 |
if (_expr1 >= 0) \ |
| 418 |
log_test_failed("Expected \"%s\" to fail with errno %d/%s, but it succeeded", \ |
| 419 |
#expr1, _expr2, ERRNO_NAME(_expr2)); \ |
| 420 |
else if (errno != _expr2) \ |
| 421 |
log_test_failed("Expected \"%s\" to fail with errno %d/%s, but got %d/%s", \ |
| 422 |
#expr1, _expr2, ERRNO_NAME(_expr2), errno, ERRNO_NAME(errno)); \ |
| 423 |
_expr1; \ |
| 424 |
}) |
| 425 |
#endif |
| 426 |
|
| 427 |
#ifdef __COVERITY__ |
| 428 |
# define ASSERT_TRUE(expr) __coverity_check__(!!(expr)) |
| 429 |
#else |
| 430 |
# define ASSERT_TRUE(expr) \ |
| 431 |
({ \ |
| 432 |
if (!(expr)) \ |
| 433 |
log_test_failed("Expected \"%s\" to be true", #expr); \ |
| 434 |
}) |
| 435 |
#endif |
| 436 |
|
| 437 |
#ifdef __COVERITY__ |
| 438 |
# define ASSERT_FALSE(expr) __coverity_check__(!(expr)) |
| 439 |
#else |
| 440 |
# define ASSERT_FALSE(expr) \ |
| 441 |
({ \ |
| 442 |
if ((expr)) \ |
| 443 |
log_test_failed("Expected \"%s\" to be false", #expr); \ |
| 444 |
}) |
| 445 |
#endif |
| 446 |
|
| 447 |
#ifdef __COVERITY__ |
| 448 |
# define ASSERT_NULL(expr) __coverity_check__((expr) == NULL) |
| 449 |
#else |
| 450 |
# define ASSERT_NULL(expr) \ |
| 451 |
({ \ |
| 452 |
typeof(expr) _result = (expr); \ |
| 453 |
if (_result != NULL) \ |
| 454 |
log_test_failed("Expected \"%s\" to be NULL, got \"%p\" != NULL", #expr, _result); \ |
| 455 |
}) |
| 456 |
#endif |
| 457 |
|
| 458 |
#ifdef __COVERITY__ |
| 459 |
# define ASSERT_NOT_NULL(expr) __coverity_check__((expr) != NULL) |
| 460 |
#else |
| 461 |
# define ASSERT_NOT_NULL(expr) \ |
| 462 |
({ \ |
| 463 |
typeof(expr) _result = (expr); \ |
| 464 |
if (_result == NULL) \ |
| 465 |
log_test_failed("Expected \"%s\" to be not NULL", #expr); \ |
| 466 |
_result; \ |
| 467 |
}) |
| 468 |
#endif |
| 469 |
|
| 470 |
#ifdef __COVERITY__ |
| 471 |
# define ASSERT_STREQ(expr1, expr2) __coverity_check__(streq_ptr((expr1), (expr2))) |
| 472 |
#else |
| 473 |
# define ASSERT_STREQ(expr1, expr2) \ |
| 474 |
({ \ |
| 475 |
const char *_expr1 = (expr1), *_expr2 = (expr2); \ |
| 476 |
if (!streq_ptr(_expr1, _expr2)) \ |
| 477 |
log_test_failed("Expected \"%s == %s\", got \"%s != %s\"", \ |
| 478 |
#expr1, #expr2, strnull(_expr1), strnull(_expr2)); \ |
| 479 |
}) |
| 480 |
#endif |
| 481 |
|
| 482 |
#ifdef __COVERITY__ |
| 483 |
# define ASSERT_PATH_EQ(expr1, expr2) __coverity_check__(path_equal((expr1), (expr2))) |
| 484 |
#else |
| 485 |
# define ASSERT_PATH_EQ(expr1, expr2) \ |
| 486 |
({ \ |
| 487 |
const char *_expr1 = (expr1), *_expr2 = (expr2); \ |
| 488 |
if (!path_equal(_expr1, _expr2)) \ |
| 489 |
log_test_failed("Expected \"%s == %s\", got \"%s != %s\"", \ |
| 490 |
#expr1, #expr2, strnull(_expr1), strnull(_expr2)); \ |
| 491 |
}) |
| 492 |
#endif |
| 493 |
|
| 494 |
#ifdef __COVERITY__ |
| 495 |
# define ASSERT_NOT_STREQ(expr1, expr2) __coverity_check__(!streq_ptr((expr1), (expr2))) |
| 496 |
#else |
| 497 |
# define ASSERT_NOT_STREQ(expr1, expr2) \ |
| 498 |
({ \ |
| 499 |
const char *_expr1 = (expr1), *_expr2 = (expr2); \ |
| 500 |
if (streq_ptr(_expr1, _expr2)) \ |
| 501 |
log_test_failed("Expected \"%s != %s\", got \"%s == %s\"", \ |
| 502 |
#expr1, #expr2, strnull(_expr1), strnull(_expr2)); \ |
| 503 |
}) |
| 504 |
#endif |
| 505 |
|
| 506 |
#ifdef __COVERITY__ |
| 507 |
# define ASSERT_STRNEQ(expr1, expr2, n) __coverity_check__(strneq_ptr((expr1), (expr2), (n))) |
| 508 |
#else |
| 509 |
# define ASSERT_STRNEQ(expr1, expr2, n) \ |
| 510 |
({ \ |
| 511 |
const char *_expr1 = (expr1), *_expr2 = (expr2); \ |
| 512 |
size_t _n = (n); \ |
| 513 |
if (!strneq_ptr(_expr1, _expr2, _n)) \ |
| 514 |
log_test_failed("Expected \"%s == %s\", got \"%s != %s\" (first %zu characters)", \ |
| 515 |
#expr1, #expr2, strnull(_expr1), strnull(_expr2), _n); \ |
| 516 |
}) |
| 517 |
#endif |
| 518 |
|
| 519 |
#ifdef __COVERITY__ |
| 520 |
# define ASSERT_PTR_EQ(expr1, expr2) __coverity_check__((expr1) == (expr2)) |
| 521 |
#else |
| 522 |
# define ASSERT_PTR_EQ(expr1, expr2) \ |
| 523 |
({ \ |
| 524 |
const void *_expr1 = (expr1), *_expr2 = (expr2); \ |
| 525 |
if (_expr1 != _expr2) \ |
| 526 |
log_test_failed("Expected \"%s == %s\", got \"0x%p != 0x%p\"", \ |
| 527 |
#expr1, #expr2, _expr1, _expr2); \ |
| 528 |
}) |
| 529 |
#endif |
| 530 |
|
| 531 |
/* DECIMAL_STR_FMT() uses _Generic which cannot be used in string concatenation so we have to format the |
| 532 |
* input into strings first and then format those into the final assertion message. */ |
| 533 |
|
| 534 |
#ifdef __COVERITY__ |
| 535 |
# define ASSERT_EQ(expr1, expr2) __coverity_check__((expr1) == (expr2)) |
| 536 |
#else |
| 537 |
# define ASSERT_EQ(expr1, expr2) \ |
| 538 |
({ \ |
| 539 |
typeof(expr1) _expr1 = (expr1); \ |
| 540 |
typeof(expr2) _expr2 = (expr2); \ |
| 541 |
if (_expr1 != _expr2) { \ |
| 542 |
char _sexpr1[DECIMAL_STR_MAX(typeof(expr1))]; \ |
| 543 |
char _sexpr2[DECIMAL_STR_MAX(typeof(expr2))]; \ |
| 544 |
xsprintf(_sexpr1, DECIMAL_STR_FMT(_expr1), _expr1); \ |
| 545 |
xsprintf(_sexpr2, DECIMAL_STR_FMT(_expr2), _expr2); \ |
| 546 |
log_test_failed("Expected \"%s == %s\", but %s != %s", \ |
| 547 |
#expr1, #expr2, _sexpr1, _sexpr2); \ |
| 548 |
} \ |
| 549 |
}) |
| 550 |
#endif |
| 551 |
|
| 552 |
#ifdef __COVERITY__ |
| 553 |
# define ASSERT_GE(expr1, expr2) __coverity_check__((expr1) >= (expr2)) |
| 554 |
#else |
| 555 |
# define ASSERT_GE(expr1, expr2) \ |
| 556 |
({ \ |
| 557 |
typeof(expr1) _expr1 = (expr1); \ |
| 558 |
typeof(expr2) _expr2 = (expr2); \ |
| 559 |
if (_expr1 < _expr2) { \ |
| 560 |
char _sexpr1[DECIMAL_STR_MAX(typeof(expr1))]; \ |
| 561 |
char _sexpr2[DECIMAL_STR_MAX(typeof(expr2))]; \ |
| 562 |
xsprintf(_sexpr1, DECIMAL_STR_FMT(_expr1), _expr1); \ |
| 563 |
xsprintf(_sexpr2, DECIMAL_STR_FMT(_expr2), _expr2); \ |
| 564 |
log_test_failed("Expected \"%s >= %s\", but %s < %s", \ |
| 565 |
#expr1, #expr2, _sexpr1, _sexpr2); \ |
| 566 |
} \ |
| 567 |
}) |
| 568 |
#endif |
| 569 |
|
| 570 |
#ifdef __COVERITY__ |
| 571 |
# define ASSERT_LE(expr1, expr2) __coverity_check__((expr1) <= (expr2)) |
| 572 |
#else |
| 573 |
# define ASSERT_LE(expr1, expr2) \ |
| 574 |
({ \ |
| 575 |
typeof(expr1) _expr1 = (expr1); \ |
| 576 |
typeof(expr2) _expr2 = (expr2); \ |
| 577 |
if (_expr1 > _expr2) { \ |
| 578 |
char _sexpr1[DECIMAL_STR_MAX(typeof(expr1))]; \ |
| 579 |
char _sexpr2[DECIMAL_STR_MAX(typeof(expr2))]; \ |
| 580 |
xsprintf(_sexpr1, DECIMAL_STR_FMT(_expr1), _expr1); \ |
| 581 |
xsprintf(_sexpr2, DECIMAL_STR_FMT(_expr2), _expr2); \ |
| 582 |
log_test_failed("Expected \"%s <= %s\", but %s > %s", \ |
| 583 |
#expr1, #expr2, _sexpr1, _sexpr2); \ |
| 584 |
} \ |
| 585 |
}) |
| 586 |
#endif |
| 587 |
|
| 588 |
#ifdef __COVERITY__ |
| 589 |
# define ASSERT_NE(expr1, expr2) __coverity_check__((expr1) != (expr2)) |
| 590 |
#else |
| 591 |
# define ASSERT_NE(expr1, expr2) \ |
| 592 |
({ \ |
| 593 |
typeof(expr1) _expr1 = (expr1); \ |
| 594 |
typeof(expr2) _expr2 = (expr2); \ |
| 595 |
if (_expr1 == _expr2) { \ |
| 596 |
char _sexpr1[DECIMAL_STR_MAX(typeof(expr1))]; \ |
| 597 |
char _sexpr2[DECIMAL_STR_MAX(typeof(expr2))]; \ |
| 598 |
xsprintf(_sexpr1, DECIMAL_STR_FMT(_expr1), _expr1); \ |
| 599 |
xsprintf(_sexpr2, DECIMAL_STR_FMT(_expr2), _expr2); \ |
| 600 |
log_test_failed("Expected \"%s != %s\", but %s == %s", \ |
| 601 |
#expr1, #expr2, _sexpr1, _sexpr2); \ |
| 602 |
} \ |
| 603 |
}) |
| 604 |
#endif |
| 605 |
|
| 606 |
#ifdef __COVERITY__ |
| 607 |
# define ASSERT_GT(expr1, expr2) __coverity_check__((expr1) > (expr2)) |
| 608 |
#else |
| 609 |
# define ASSERT_GT(expr1, expr2) \ |
| 610 |
({ \ |
| 611 |
typeof(expr1) _expr1 = (expr1); \ |
| 612 |
typeof(expr2) _expr2 = (expr2); \ |
| 613 |
if (!(_expr1 > _expr2)) { \ |
| 614 |
char _sexpr1[DECIMAL_STR_MAX(typeof(expr1))]; \ |
| 615 |
char _sexpr2[DECIMAL_STR_MAX(typeof(expr2))]; \ |
| 616 |
xsprintf(_sexpr1, DECIMAL_STR_FMT(_expr1), _expr1); \ |
| 617 |
xsprintf(_sexpr2, DECIMAL_STR_FMT(_expr2), _expr2); \ |
| 618 |
log_test_failed("Expected \"%s > %s\", but %s <= %s", \ |
| 619 |
#expr1, #expr2, _sexpr1, _sexpr2); \ |
| 620 |
} \ |
| 621 |
}) |
| 622 |
#endif |
| 623 |
|
| 624 |
#ifdef __COVERITY__ |
| 625 |
# define ASSERT_LT(expr1, expr2) __coverity_check__((expr1) < (expr2)) |
| 626 |
#else |
| 627 |
# define ASSERT_LT(expr1, expr2) \ |
| 628 |
({ \ |
| 629 |
typeof(expr1) _expr1 = (expr1); \ |
| 630 |
typeof(expr2) _expr2 = (expr2); \ |
| 631 |
if (!(_expr1 < _expr2)) { \ |
| 632 |
char _sexpr1[DECIMAL_STR_MAX(typeof(expr1))]; \ |
| 633 |
char _sexpr2[DECIMAL_STR_MAX(typeof(expr2))]; \ |
| 634 |
xsprintf(_sexpr1, DECIMAL_STR_FMT(_expr1), _expr1); \ |
| 635 |
xsprintf(_sexpr2, DECIMAL_STR_FMT(_expr2), _expr2); \ |
| 636 |
log_test_failed("Expected \"%s < %s\", but %s >= %s", \ |
| 637 |
#expr1, #expr2, _sexpr1, _sexpr2); \ |
| 638 |
} \ |
| 639 |
}) |
| 640 |
#endif |
| 641 |
|
| 642 |
enum { |
| 643 |
ASSERT_SIGNAL_FORK_CHILD = 0, /* We are in the child process */ |
| 644 |
ASSERT_SIGNAL_FORK_PARENT = 1, /* We are in the parent process */ |
| 645 |
}; |
| 646 |
|
| 647 |
int assert_signal_internal(int *ret_signal); |
| 648 |
|
| 649 |
#ifdef __COVERITY__ |
| 650 |
# define ASSERT_SIGNAL(expr, signal) __coverity_check__(((expr), false)) |
| 651 |
#else |
| 652 |
# define ASSERT_SIGNAL(expr, signal) __ASSERT_SIGNAL(UNIQ, expr, signal) |
| 653 |
# define __ASSERT_SIGNAL(uniq, expr, sgnl) \ |
| 654 |
({ \ |
| 655 |
ASSERT_TRUE(SIGNAL_VALID(sgnl)); \ |
| 656 |
int UNIQ_T(_status, uniq); \ |
| 657 |
int UNIQ_T(_path, uniq) = assert_signal_internal(&UNIQ_T(_status, uniq)); \ |
| 658 |
ASSERT_OK_ERRNO(UNIQ_T(_path, uniq)); \ |
| 659 |
if (UNIQ_T(_path, uniq) == ASSERT_SIGNAL_FORK_CHILD) { \ |
| 660 |
(void) signal(sgnl, SIG_DFL); \ |
| 661 |
expr; \ |
| 662 |
_exit(EXIT_SUCCESS); \ |
| 663 |
} \ |
| 664 |
ASSERT_EQ(UNIQ_T(_path, uniq), ASSERT_SIGNAL_FORK_PARENT); \ |
| 665 |
if (UNIQ_T(_status, uniq) != sgnl) \ |
| 666 |
log_test_failed("\"%s\" died with signal %s, but %s was expected", \ |
| 667 |
#expr, signal_to_string(UNIQ_T(_status, uniq)), \ |
| 668 |
signal_to_string(sgnl)); \ |
| 669 |
}) |
| 670 |
#endif |
| 671 |
|
| 672 |
#ifdef __COVERITY__ |
| 673 |
# define ASSERT_EQ_ID128(expr1, expr2) __coverity_check__(sd_id128_equal((expr1), (expr2))) |
| 674 |
#else |
| 675 |
# define ASSERT_EQ_ID128(expr1, expr2) \ |
| 676 |
({ \ |
| 677 |
typeof(expr1) _expr1 = (expr1); \ |
| 678 |
typeof(expr2) _expr2 = (expr2); \ |
| 679 |
if (!sd_id128_equal(_expr1, _expr2)) \ |
| 680 |
log_test_failed("\"%s == %s\", but %s != %s", \ |
| 681 |
#expr1, #expr2, \ |
| 682 |
SD_ID128_TO_STRING(_expr1), SD_ID128_TO_STRING(_expr2)); \ |
| 683 |
}) |
| 684 |
#endif |
| 685 |
|
| 686 |
#ifdef __COVERITY__ |
| 687 |
# define ASSERT_NE_ID128(expr1, expr2) __coverity_check__(!sd_id128_equal((expr1), (expr2))) |
| 688 |
#else |
| 689 |
# define ASSERT_NE_ID128(expr1, expr2) \ |
| 690 |
({ \ |
| 691 |
typeof(expr1) _expr1 = (expr1); \ |
| 692 |
typeof(expr2) _expr2 = (expr2); \ |
| 693 |
if (sd_id128_equal(_expr1, _expr2)) \ |
| 694 |
log_test_failed("\"%s != %s\", but %s == %s", \ |
| 695 |
#expr1, #expr2, \ |
| 696 |
SD_ID128_TO_STRING(_expr1), SD_ID128_TO_STRING(_expr2)); \ |
| 697 |
}) |
| 698 |
#endif |
| 699 |
|
| 700 |
#define EFI_GUID_Fmt "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x" |
| 701 |
#define EFI_GUID_Arg(guid) (guid).Data1, (guid).Data2, (guid).Data3, \ |
| 702 |
(guid).Data4[0], (guid).Data4[1], (guid).Data4[2], (guid).Data4[3], \ |
| 703 |
(guid).Data4[4], (guid).Data4[5], (guid).Data4[6], (guid).Data4[7] \ |
| 704 |
|
| 705 |
#ifdef __COVERITY__ |
| 706 |
# define ASSERT_EQ_EFI_GUID(expr1, expr2) __coverity_check__(efi_guid_equal((expr1), (expr2))) |
| 707 |
#else |
| 708 |
# define ASSERT_EQ_EFI_GUID(expr1, expr2) \ |
| 709 |
({ \ |
| 710 |
typeof(expr1) _expr1 = (expr1); \ |
| 711 |
typeof(expr2) _expr2 = (expr2); \ |
| 712 |
if (!efi_guid_equal(_expr1, _expr2)) \ |
| 713 |
log_test_failed("Expected \"%s == %s\", but " EFI_GUID_Fmt \ |
| 714 |
" != " EFI_GUID_Fmt, \ |
| 715 |
#expr1, #expr2, \ |
| 716 |
EFI_GUID_Arg(*_expr1), EFI_GUID_Arg(*_expr2)); \ |
| 717 |
}) |
| 718 |
#endif |