From 1e26f8a60bd0f3e5acf1931968af053eebb9a454 Mon Sep 17 00:00:00 2001 From: Luca Boccassi Date: Mon, 22 Feb 2021 18:20:37 +0000 Subject: core: add ConditionOSRelease= directive --- src/test/test-condition.c | 146 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 146 insertions(+) (limited to 'src/test/test-condition.c') diff --git a/src/test/test-condition.c b/src/test/test-condition.c index adba383fdd..d1dee22bd6 100644 --- a/src/test/test-condition.c +++ b/src/test/test-condition.c @@ -23,6 +23,7 @@ #include "log.h" #include "macro.h" #include "nulstr-util.h" +#include "os-util.h" #include "process-util.h" #include "selinux-util.h" #include "set.h" @@ -890,6 +891,150 @@ static void test_condition_test_environment(void) { test_condition_test_environment_one("EXISTINGENVVAR=", false); } +static void test_condition_test_os_release(void) { + _cleanup_strv_free_ char **os_release_pairs = NULL; + _cleanup_free_ char *version_id = NULL; + const char *key_value_pair; + Condition *condition; + + /* Should not happen, but it's a test so we don't know the environment. */ + if (load_os_release_pairs(NULL, &os_release_pairs) < 0) + return; + if (strv_length(os_release_pairs) < 2) + return; + + condition = condition_new(CONDITION_OS_RELEASE, "_THISHOPEFULLYWONTEXIST=01234 56789", false, false); + assert_se(condition); + assert_se(condition_test(condition, environ) == 0); + condition_free(condition); + + condition = condition_new(CONDITION_OS_RELEASE, "WRONG FORMAT", false, false); + assert_se(condition); + assert_se(condition_test(condition, environ) == -EINVAL); + condition_free(condition); + + condition = condition_new(CONDITION_OS_RELEASE, "WRONG!<>=FORMAT", false, false); + assert_se(condition); + assert_se(condition_test(condition, environ) == -EINVAL); + condition_free(condition); + + condition = condition_new(CONDITION_OS_RELEASE, "WRONG FORMAT=", false, false); + assert_se(condition); + assert_se(condition_test(condition, environ) == -EINVAL); + condition_free(condition); + + condition = condition_new(CONDITION_OS_RELEASE, "WRONG =FORMAT", false, false); + assert_se(condition); + assert_se(condition_test(condition, environ) == -EINVAL); + condition_free(condition); + + condition = condition_new(CONDITION_OS_RELEASE, "WRONG = FORMAT", false, false); + assert_se(condition); + assert_se(condition_test(condition, environ) == -EINVAL); + condition_free(condition); + + condition = condition_new(CONDITION_OS_RELEASE, "WRONGFORMAT= ", false, false); + assert_se(condition); + assert_se(condition_test(condition, environ) == -EINVAL); + condition_free(condition); + + condition = condition_new(CONDITION_OS_RELEASE, "WRO NG=FORMAT", false, false); + assert_se(condition); + assert_se(condition_test(condition, environ) == -EINVAL); + condition_free(condition); + + condition = condition_new(CONDITION_OS_RELEASE, "", false, false); + assert_se(condition); + assert_se(condition_test(condition, environ)); + condition_free(condition); + + /* load_os_release_pairs() removes quotes, we have to add them back, + * otherwise we get a string: "PRETTY_NAME=Debian GNU/Linux 10 (buster)" + * which is wrong, as the value is not quoted anymore. */ + const char *quote = strchr(os_release_pairs[1], ' ') ? "\"" : ""; + key_value_pair = strjoina(os_release_pairs[0], "=", quote, os_release_pairs[1], quote); + condition = condition_new(CONDITION_OS_RELEASE, key_value_pair, false, false); + assert_se(condition); + assert_se(condition_test(condition, environ)); + condition_free(condition); + + key_value_pair = strjoina(os_release_pairs[0], "!=", quote, os_release_pairs[1], quote); + condition = condition_new(CONDITION_OS_RELEASE, key_value_pair, false, false); + assert_se(condition); + assert_se(condition_test(condition, environ) == 0); + condition_free(condition); + + /* Some distros (eg: Arch) do not set VERSION_ID */ + if (parse_os_release(NULL, "VERSION_ID", &version_id) <= 0) + return; + + key_value_pair = strjoina("VERSION_ID", "=", version_id); + condition = condition_new(CONDITION_OS_RELEASE, key_value_pair, false, false); + assert_se(condition); + assert_se(condition_test(condition, environ)); + condition_free(condition); + + key_value_pair = strjoina("VERSION_ID", "!=", version_id); + condition = condition_new(CONDITION_OS_RELEASE, key_value_pair, false, false); + assert_se(condition); + assert_se(condition_test(condition, environ) == 0); + condition_free(condition); + + key_value_pair = strjoina("VERSION_ID", "<=", version_id); + condition = condition_new(CONDITION_OS_RELEASE, key_value_pair, false, false); + assert_se(condition); + assert_se(condition_test(condition, environ)); + condition_free(condition); + + key_value_pair = strjoina("VERSION_ID", ">=", version_id); + condition = condition_new(CONDITION_OS_RELEASE, key_value_pair, false, false); + assert_se(condition); + assert_se(condition_test(condition, environ)); + condition_free(condition); + + key_value_pair = strjoina("VERSION_ID", "<", version_id, ".1"); + condition = condition_new(CONDITION_OS_RELEASE, key_value_pair, false, false); + assert_se(condition); + assert_se(condition_test(condition, environ)); + condition_free(condition); + + key_value_pair = strjoina("VERSION_ID", ">", version_id, ".1"); + condition = condition_new(CONDITION_OS_RELEASE, key_value_pair, false, false); + assert_se(condition); + assert_se(condition_test(condition, environ) == 0); + condition_free(condition); + + key_value_pair = strjoina("VERSION_ID", "=", version_id, " ", os_release_pairs[0], "=", quote, os_release_pairs[1], quote); + condition = condition_new(CONDITION_OS_RELEASE, key_value_pair, false, false); + assert_se(condition); + assert_se(condition_test(condition, environ)); + condition_free(condition); + + key_value_pair = strjoina("VERSION_ID", "!=", version_id, " ", os_release_pairs[0], "=", quote, os_release_pairs[1], quote); + condition = condition_new(CONDITION_OS_RELEASE, key_value_pair, false, false); + assert_se(condition); + assert_se(condition_test(condition, environ) == 0); + condition_free(condition); + + key_value_pair = strjoina("VERSION_ID", "=", version_id, " ", os_release_pairs[0], "!=", quote, os_release_pairs[1], quote); + condition = condition_new(CONDITION_OS_RELEASE, key_value_pair, false, false); + assert_se(condition); + assert_se(condition_test(condition, environ) == 0); + condition_free(condition); + + key_value_pair = strjoina("VERSION_ID", "!=", version_id, " ", os_release_pairs[0], "!=", quote, os_release_pairs[1], quote); + condition = condition_new(CONDITION_OS_RELEASE, key_value_pair, false, false); + assert_se(condition); + assert_se(condition_test(condition, environ) == 0); + condition_free(condition); + + key_value_pair = strjoina("VERSION_ID", "<", version_id, ".1", " ", os_release_pairs[0], "=", quote, os_release_pairs[1], quote); + condition = condition_new(CONDITION_OS_RELEASE, key_value_pair, false, false); + assert_se(condition); + assert_se(condition_test(condition, environ)); + condition_free(condition); +} + int main(int argc, char *argv[]) { test_setup_logging(LOG_DEBUG); @@ -912,6 +1057,7 @@ int main(int argc, char *argv[]) { #if defined(__i386__) || defined(__x86_64__) test_condition_test_cpufeature(); #endif + test_condition_test_os_release(); return 0; } -- cgit v1.2.1