mirror of
https://github.com/systemd/systemd.git
synced 2026-08-11 17:45:59 +00:00
test-condition: Migrate to new assertion macros
(cherry picked from commit 54b745d7bb)
This commit is contained in:
committed by
Luca Boccassi
parent
ba2391fed7
commit
fadf8cc06b
@@ -274,6 +274,29 @@ _noreturn_ void log_test_failed_internal(const char *file, int line, const char
|
||||
})
|
||||
#endif
|
||||
|
||||
#ifdef __COVERITY__
|
||||
# define ASSERT_OK_NE(expr1, expr2) \
|
||||
({ \
|
||||
typeof(expr1) _expr1 = (expr1); \
|
||||
typeof(expr2) _expr2 = (expr2); \
|
||||
__coverity_check__(_expr1 != _expr2); \
|
||||
_expr1; \
|
||||
})
|
||||
#else
|
||||
# define ASSERT_OK_NE(expr1, expr2) \
|
||||
({ \
|
||||
typeof(expr1) _expr1 = (expr1); \
|
||||
typeof(expr2) _expr2 = (expr2); \
|
||||
if (_expr1 < 0) \
|
||||
log_test_failed("Expected \"%s\" to succeed, but got error: %"PRIiMAX"/%s", \
|
||||
#expr1, (intmax_t) _expr1, ERRNO_NAME(_expr1)); \
|
||||
if (_expr1 == _expr2) \
|
||||
log_test_failed("Expected \"%s != %s\", got %"PRIiMAX" != %"PRIiMAX, \
|
||||
#expr1, #expr2, (intmax_t) _expr1, (intmax_t) _expr2); \
|
||||
_expr1; \
|
||||
})
|
||||
#endif
|
||||
|
||||
/* For functions that return a boolean on success and set errno on failure. */
|
||||
#ifdef __COVERITY__
|
||||
# define ASSERT_OK_ERRNO(expr) \
|
||||
|
||||
@@ -187,15 +187,15 @@ TEST(condition_test_ac_power) {
|
||||
Condition *condition;
|
||||
|
||||
ASSERT_NOT_NULL((condition = condition_new(CONDITION_AC_POWER, "true", false, false)));
|
||||
assert_se(condition_test(condition, environ) == on_ac_power());
|
||||
ASSERT_OK_EQ(condition_test(condition, environ), on_ac_power());
|
||||
condition_free(condition);
|
||||
|
||||
ASSERT_NOT_NULL((condition = condition_new(CONDITION_AC_POWER, "false", false, false)));
|
||||
assert_se(condition_test(condition, environ) != on_ac_power());
|
||||
ASSERT_OK_NE(condition_test(condition, environ), on_ac_power());
|
||||
condition_free(condition);
|
||||
|
||||
ASSERT_NOT_NULL((condition = condition_new(CONDITION_AC_POWER, "false", false, true)));
|
||||
assert_se(condition_test(condition, environ) == on_ac_power());
|
||||
ASSERT_OK_EQ(condition_test(condition, environ), on_ac_power());
|
||||
condition_free(condition);
|
||||
}
|
||||
|
||||
@@ -714,8 +714,8 @@ TEST(condition_test_credential) {
|
||||
_cleanup_free_ char *d1 = NULL, *d2 = NULL, *j = NULL;
|
||||
Condition *condition;
|
||||
|
||||
assert_se(free_and_strdup(&d1, getenv("CREDENTIALS_DIRECTORY")) >= 0);
|
||||
assert_se(free_and_strdup(&d2, getenv("ENCRYPTED_CREDENTIALS_DIRECTORY")) >= 0);
|
||||
ASSERT_OK(free_and_strdup(&d1, getenv("CREDENTIALS_DIRECTORY")));
|
||||
ASSERT_OK(free_and_strdup(&d2, getenv("ENCRYPTED_CREDENTIALS_DIRECTORY")));
|
||||
|
||||
ASSERT_OK_ERRNO(unsetenv("CREDENTIALS_DIRECTORY"));
|
||||
ASSERT_OK_ERRNO(unsetenv("ENCRYPTED_CREDENTIALS_DIRECTORY"));
|
||||
@@ -729,8 +729,8 @@ TEST(condition_test_credential) {
|
||||
ASSERT_OK_ZERO(condition_test(condition, environ));
|
||||
condition_free(condition);
|
||||
|
||||
assert_se(mkdtemp_malloc(NULL, &n1) >= 0);
|
||||
assert_se(mkdtemp_malloc(NULL, &n2) >= 0);
|
||||
ASSERT_OK(mkdtemp_malloc(NULL, &n1));
|
||||
ASSERT_OK(mkdtemp_malloc(NULL, &n2));
|
||||
|
||||
ASSERT_OK_ERRNO(setenv("CREDENTIALS_DIRECTORY", n1, /* overwrite= */ true));
|
||||
ASSERT_OK_ERRNO(setenv("ENCRYPTED_CREDENTIALS_DIRECTORY", n2, /* overwrite= */ true));
|
||||
@@ -740,20 +740,20 @@ TEST(condition_test_credential) {
|
||||
condition_free(condition);
|
||||
|
||||
ASSERT_NOT_NULL((j = path_join(n1, "existing")));
|
||||
assert_se(touch(j) >= 0);
|
||||
ASSERT_OK(touch(j));
|
||||
ASSERT_NOT_NULL((condition = condition_new(CONDITION_CREDENTIAL, "existing", /* trigger= */ false, /* negate= */ false)));
|
||||
ASSERT_OK_POSITIVE(condition_test(condition, environ));
|
||||
condition_free(condition);
|
||||
free(j);
|
||||
|
||||
ASSERT_NOT_NULL((j = path_join(n2, "existing-encrypted")));
|
||||
assert_se(touch(j) >= 0);
|
||||
ASSERT_OK(touch(j));
|
||||
ASSERT_NOT_NULL((condition = condition_new(CONDITION_CREDENTIAL, "existing-encrypted", /* trigger= */ false, /* negate= */ false)));
|
||||
ASSERT_OK_POSITIVE(condition_test(condition, environ));
|
||||
condition_free(condition);
|
||||
|
||||
assert_se(set_unset_env("CREDENTIALS_DIRECTORY", d1, /* overwrite= */ true) >= 0);
|
||||
assert_se(set_unset_env("ENCRYPTED_CREDENTIALS_DIRECTORY", d2, /* overwrite= */ true) >= 0);
|
||||
ASSERT_OK(set_unset_env("CREDENTIALS_DIRECTORY", d1, /* overwrite= */ true));
|
||||
ASSERT_OK(set_unset_env("ENCRYPTED_CREDENTIALS_DIRECTORY", d2, /* overwrite= */ true));
|
||||
}
|
||||
|
||||
#if defined(__i386__) || defined(__x86_64__)
|
||||
@@ -782,36 +782,36 @@ TEST(condition_test_security) {
|
||||
condition_free(condition);
|
||||
|
||||
ASSERT_NOT_NULL((condition = condition_new(CONDITION_SECURITY, "selinux", false, true)));
|
||||
assert_se(condition_test(condition, environ) != mac_selinux_use());
|
||||
ASSERT_OK_NE(condition_test(condition, environ), mac_selinux_use());
|
||||
condition_free(condition);
|
||||
|
||||
ASSERT_NOT_NULL((condition = condition_new(CONDITION_SECURITY, "apparmor", false, false)));
|
||||
assert_se(condition_test(condition, environ) == mac_apparmor_use());
|
||||
ASSERT_OK_EQ(condition_test(condition, environ), mac_apparmor_use());
|
||||
condition_free(condition);
|
||||
|
||||
ASSERT_NOT_NULL((condition = condition_new(CONDITION_SECURITY, "tomoyo", false, false)));
|
||||
assert_se(condition_test(condition, environ) == mac_tomoyo_use());
|
||||
ASSERT_OK_EQ(condition_test(condition, environ), mac_tomoyo_use());
|
||||
condition_free(condition);
|
||||
|
||||
ASSERT_NOT_NULL((condition = condition_new(CONDITION_SECURITY, "ima", false, false)));
|
||||
assert_se(condition_test(condition, environ) == use_ima());
|
||||
ASSERT_OK_EQ(condition_test(condition, environ), use_ima());
|
||||
condition_free(condition);
|
||||
|
||||
ASSERT_NOT_NULL((condition = condition_new(CONDITION_SECURITY, "smack", false, false)));
|
||||
assert_se(condition_test(condition, environ) == mac_smack_use());
|
||||
ASSERT_OK_EQ(condition_test(condition, environ), mac_smack_use());
|
||||
condition_free(condition);
|
||||
|
||||
ASSERT_NOT_NULL((condition = condition_new(CONDITION_SECURITY, "audit", false, false)));
|
||||
assert_se(condition_test(condition, environ) == use_audit());
|
||||
ASSERT_OK_EQ(condition_test(condition, environ), use_audit());
|
||||
condition_free(condition);
|
||||
|
||||
ASSERT_NOT_NULL((condition = condition_new(CONDITION_SECURITY, "uefi-secureboot", false, false)));
|
||||
assert_se(condition_test(condition, environ) == is_efi_secure_boot());
|
||||
ASSERT_OK_EQ(condition_test(condition, environ), is_efi_secure_boot());
|
||||
condition_free(condition);
|
||||
|
||||
ASSERT_NOT_NULL((condition = condition_new(CONDITION_SECURITY, "cvm", false, false)));
|
||||
assert_se(condition_test(condition, environ) ==
|
||||
(detect_confidential_virtualization() != CONFIDENTIAL_VIRTUALIZATION_NONE));
|
||||
ASSERT_OK_EQ(condition_test(condition, environ),
|
||||
(detect_confidential_virtualization() != CONFIDENTIAL_VIRTUALIZATION_NONE));
|
||||
condition_free(condition);
|
||||
}
|
||||
|
||||
@@ -844,19 +844,19 @@ TEST(condition_test_virtualization) {
|
||||
ASSERT_NOT_NULL((condition = condition_new(CONDITION_VIRTUALIZATION, "container", false, false)));
|
||||
r = condition_test(condition, environ);
|
||||
log_info("ConditionVirtualization=container → %i", r);
|
||||
assert_se(r == !!detect_container());
|
||||
ASSERT_OK_EQ(r, !!detect_container());
|
||||
condition_free(condition);
|
||||
|
||||
ASSERT_NOT_NULL((condition = condition_new(CONDITION_VIRTUALIZATION, "vm", false, false)));
|
||||
r = condition_test(condition, environ);
|
||||
log_info("ConditionVirtualization=vm → %i", r);
|
||||
assert_se(r == (detect_vm() && !detect_container()));
|
||||
ASSERT_OK_EQ(r, (detect_vm() && !detect_container()));
|
||||
condition_free(condition);
|
||||
|
||||
ASSERT_NOT_NULL((condition = condition_new(CONDITION_VIRTUALIZATION, "private-users", false, false)));
|
||||
r = condition_test(condition, environ);
|
||||
log_info("ConditionVirtualization=private-users → %i", r);
|
||||
assert_se(r == !!running_in_userns());
|
||||
ASSERT_OK_EQ(r, !!running_in_userns());
|
||||
condition_free(condition);
|
||||
|
||||
NULSTR_FOREACH(virt,
|
||||
@@ -963,13 +963,12 @@ TEST(condition_test_group) {
|
||||
ASSERT_OK_POSITIVE(r);
|
||||
condition_free(condition);
|
||||
|
||||
ngroups_max = sysconf(_SC_NGROUPS_MAX);
|
||||
assert_se(ngroups_max > 0);
|
||||
ngroups_max = ASSERT_OK_ERRNO(sysconf(_SC_NGROUPS_MAX));
|
||||
ASSERT_GT(ngroups_max, 0);
|
||||
|
||||
gids = newa(gid_t, ngroups_max);
|
||||
|
||||
ngroups = getgroups(ngroups_max, gids);
|
||||
assert_se(ngroups >= 0);
|
||||
ngroups = ASSERT_OK_ERRNO(getgroups(ngroups_max, gids));
|
||||
|
||||
max_gid = getgid();
|
||||
for (i = 0; i < ngroups; i++) {
|
||||
@@ -1019,15 +1018,12 @@ TEST(condition_test_group) {
|
||||
|
||||
static void test_condition_test_cpus_one(const char *s, bool result) {
|
||||
Condition *condition;
|
||||
int r;
|
||||
|
||||
log_debug("%s=%s", condition_type_to_string(CONDITION_CPUS), s);
|
||||
|
||||
ASSERT_NOT_NULL((condition = condition_new(CONDITION_CPUS, s, false, false)));
|
||||
|
||||
r = condition_test(condition, environ);
|
||||
assert_se(r >= 0);
|
||||
assert_se(r == result);
|
||||
ASSERT_OK_EQ(condition_test(condition, environ), result);
|
||||
condition_free(condition);
|
||||
}
|
||||
|
||||
@@ -1035,8 +1031,7 @@ TEST(condition_test_cpus) {
|
||||
_cleanup_free_ char *t = NULL;
|
||||
int cpus;
|
||||
|
||||
cpus = cpus_in_affinity_mask();
|
||||
assert_se(cpus >= 0);
|
||||
cpus = ASSERT_OK(cpus_in_affinity_mask());
|
||||
|
||||
test_condition_test_cpus_one("> 0", true);
|
||||
test_condition_test_cpus_one(">= 0", true);
|
||||
@@ -1052,42 +1047,39 @@ TEST(condition_test_cpus) {
|
||||
test_condition_test_cpus_one("!= 100000", true);
|
||||
test_condition_test_cpus_one("<= 100000", true);
|
||||
|
||||
assert_se(asprintf(&t, "= %i", cpus) >= 0);
|
||||
ASSERT_OK(asprintf(&t, "= %i", cpus));
|
||||
test_condition_test_cpus_one(t, true);
|
||||
t = mfree(t);
|
||||
|
||||
assert_se(asprintf(&t, "<= %i", cpus) >= 0);
|
||||
ASSERT_OK(asprintf(&t, "<= %i", cpus));
|
||||
test_condition_test_cpus_one(t, true);
|
||||
t = mfree(t);
|
||||
|
||||
assert_se(asprintf(&t, ">= %i", cpus) >= 0);
|
||||
ASSERT_OK(asprintf(&t, ">= %i", cpus));
|
||||
test_condition_test_cpus_one(t, true);
|
||||
t = mfree(t);
|
||||
|
||||
assert_se(asprintf(&t, "!= %i", cpus) >= 0);
|
||||
ASSERT_OK(asprintf(&t, "!= %i", cpus));
|
||||
test_condition_test_cpus_one(t, false);
|
||||
t = mfree(t);
|
||||
|
||||
assert_se(asprintf(&t, "< %i", cpus) >= 0);
|
||||
ASSERT_OK(asprintf(&t, "< %i", cpus));
|
||||
test_condition_test_cpus_one(t, false);
|
||||
t = mfree(t);
|
||||
|
||||
assert_se(asprintf(&t, "> %i", cpus) >= 0);
|
||||
ASSERT_OK(asprintf(&t, "> %i", cpus));
|
||||
test_condition_test_cpus_one(t, false);
|
||||
t = mfree(t);
|
||||
}
|
||||
|
||||
static void test_condition_test_memory_one(const char *s, bool result) {
|
||||
Condition *condition;
|
||||
int r;
|
||||
|
||||
log_debug("%s=%s", condition_type_to_string(CONDITION_MEMORY), s);
|
||||
|
||||
ASSERT_NOT_NULL((condition = condition_new(CONDITION_MEMORY, s, false, false)));
|
||||
|
||||
r = condition_test(condition, environ);
|
||||
assert_se(r >= 0);
|
||||
assert_se(r == result);
|
||||
ASSERT_OK_EQ(condition_test(condition, environ), result);
|
||||
condition_free(condition);
|
||||
}
|
||||
|
||||
@@ -1132,42 +1124,39 @@ TEST(condition_test_memory) {
|
||||
test_condition_test_memory_one("!= 100 T 1 G", true);
|
||||
test_condition_test_memory_one("<= 100 T 1 G", true);
|
||||
|
||||
assert_se(asprintf(&t, "= %" PRIu64, memory) >= 0);
|
||||
ASSERT_OK(asprintf(&t, "= %" PRIu64, memory));
|
||||
test_condition_test_memory_one(t, true);
|
||||
t = mfree(t);
|
||||
|
||||
assert_se(asprintf(&t, "<= %" PRIu64, memory) >= 0);
|
||||
ASSERT_OK(asprintf(&t, "<= %" PRIu64, memory));
|
||||
test_condition_test_memory_one(t, true);
|
||||
t = mfree(t);
|
||||
|
||||
assert_se(asprintf(&t, ">= %" PRIu64, memory) >= 0);
|
||||
ASSERT_OK(asprintf(&t, ">= %" PRIu64, memory));
|
||||
test_condition_test_memory_one(t, true);
|
||||
t = mfree(t);
|
||||
|
||||
assert_se(asprintf(&t, "!= %" PRIu64, memory) >= 0);
|
||||
ASSERT_OK(asprintf(&t, "!= %" PRIu64, memory));
|
||||
test_condition_test_memory_one(t, false);
|
||||
t = mfree(t);
|
||||
|
||||
assert_se(asprintf(&t, "< %" PRIu64, memory) >= 0);
|
||||
ASSERT_OK(asprintf(&t, "< %" PRIu64, memory));
|
||||
test_condition_test_memory_one(t, false);
|
||||
t = mfree(t);
|
||||
|
||||
assert_se(asprintf(&t, "> %" PRIu64, memory) >= 0);
|
||||
ASSERT_OK(asprintf(&t, "> %" PRIu64, memory));
|
||||
test_condition_test_memory_one(t, false);
|
||||
t = mfree(t);
|
||||
}
|
||||
|
||||
static void test_condition_test_environment_one(const char *s, bool result) {
|
||||
Condition *condition;
|
||||
int r;
|
||||
|
||||
log_debug("%s=%s", condition_type_to_string(CONDITION_ENVIRONMENT), s);
|
||||
|
||||
ASSERT_NOT_NULL((condition = condition_new(CONDITION_ENVIRONMENT, s, false, false)));
|
||||
|
||||
r = condition_test(condition, environ);
|
||||
assert_se(r >= 0);
|
||||
assert_se(r == result);
|
||||
ASSERT_OK_EQ(condition_test(condition, environ), result);
|
||||
condition_free(condition);
|
||||
}
|
||||
|
||||
@@ -1463,13 +1452,11 @@ TEST(condition_test_kernel_module_loaded) {
|
||||
Condition *condition;
|
||||
int r;
|
||||
|
||||
condition = condition_new(CONDITION_KERNEL_MODULE_LOADED, "", /* trigger= */ false, /* negate= */ false);
|
||||
assert_se(condition);
|
||||
condition = ASSERT_NOT_NULL(condition_new(CONDITION_KERNEL_MODULE_LOADED, "", /* trigger= */ false, /* negate= */ false));
|
||||
ASSERT_OK_ZERO(condition_test(condition, environ));
|
||||
condition_free(condition);
|
||||
|
||||
condition = condition_new(CONDITION_KERNEL_MODULE_LOADED, "..", /* trigger= */ false, /* negate= */ false);
|
||||
assert_se(condition);
|
||||
condition = ASSERT_NOT_NULL(condition_new(CONDITION_KERNEL_MODULE_LOADED, "..", /* trigger= */ false, /* negate= */ false));
|
||||
ASSERT_OK_ZERO(condition_test(condition, environ));
|
||||
condition_free(condition);
|
||||
|
||||
@@ -1477,8 +1464,7 @@ TEST(condition_test_kernel_module_loaded) {
|
||||
return (void) log_tests_skipped("/sys/module not available, skipping.");
|
||||
|
||||
FOREACH_STRING(m, "random", "vfat", "fat", "cec", "binfmt_misc", "binfmt-misc") {
|
||||
condition = condition_new(CONDITION_KERNEL_MODULE_LOADED, m, /* trigger= */ false, /* negate= */ false);
|
||||
assert_se(condition);
|
||||
condition = ASSERT_NOT_NULL(condition_new(CONDITION_KERNEL_MODULE_LOADED, m, /* trigger= */ false, /* negate= */ false));
|
||||
r = condition_test(condition, environ);
|
||||
ASSERT_OK(r);
|
||||
condition_free(condition);
|
||||
@@ -1486,8 +1472,7 @@ TEST(condition_test_kernel_module_loaded) {
|
||||
log_notice("kmod %s is loaded: %s", m, yes_no(r));
|
||||
}
|
||||
|
||||
condition = condition_new(CONDITION_KERNEL_MODULE_LOADED, "idefinitelydontexist", /* trigger= */ false, /* negate= */ false);
|
||||
assert_se(condition);
|
||||
condition = ASSERT_NOT_NULL(condition_new(CONDITION_KERNEL_MODULE_LOADED, "idefinitelydontexist", /* trigger= */ false, /* negate= */ false));
|
||||
ASSERT_OK_ZERO(condition_test(condition, environ));
|
||||
condition_free(condition);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user