HALF ECHO LEARN / REFERENCE COLLECTION

v262.2

src/test/test-load-fragment.c

57,392 bytesGit mode 100644Download original fileLGPL-2.1-or-later

Git blob: 8c8600c0a72539035cea54eda51be415b801409c

Lines 1001–1252 of 1,252

        ASSERT_OK_ZERO(config_parse_device_allow(u->id, "fake", 6, "Service", 1,
                                                 "DeviceAllow", 0, "",
                                                 &c, u));
        ASSERT_NULL(c.device_allow);
}
 
TEST(contains_instance_specifier_superset) {
        ASSERT_TRUE(contains_instance_specifier_superset("foobar@a%i"));
        ASSERT_TRUE(contains_instance_specifier_superset("foobar@%ia"));
        ASSERT_TRUE(contains_instance_specifier_superset("foobar@%n"));
        ASSERT_TRUE(contains_instance_specifier_superset("foobar@%n.service"));
        ASSERT_TRUE(contains_instance_specifier_superset("foobar@%N"));
        ASSERT_TRUE(contains_instance_specifier_superset("foobar@%N.service"));
        ASSERT_TRUE(contains_instance_specifier_superset("foobar@baz.%N.service"));
        ASSERT_TRUE(contains_instance_specifier_superset("@%N.service"));
        ASSERT_TRUE(contains_instance_specifier_superset("@%N"));
        ASSERT_TRUE(contains_instance_specifier_superset("@%a%N"));
 
        ASSERT_FALSE(contains_instance_specifier_superset("foobar@%i.service"));
        ASSERT_FALSE(contains_instance_specifier_superset("foobar%ia.service"));
        ASSERT_FALSE(contains_instance_specifier_superset("foobar@%%n.service"));
        ASSERT_FALSE(contains_instance_specifier_superset("foobar@baz.service"));
        ASSERT_FALSE(contains_instance_specifier_superset("%N.service"));
        ASSERT_FALSE(contains_instance_specifier_superset("%N"));
        ASSERT_FALSE(contains_instance_specifier_superset("@%aN"));
        ASSERT_FALSE(contains_instance_specifier_superset("@%a%b"));
}
 
TEST(unit_is_recursive_template_dependency) {
        _cleanup_(manager_freep) Manager *m = NULL;
        Unit *u;
        int r;
 
        r = manager_new(RUNTIME_SCOPE_USER, MANAGER_TEST_RUN_MINIMAL, &m);
        if (manager_errno_skip_test(r)) {
                log_notice_errno(r, "Skipping test: manager_new: %m");
                return;
        }
 
        ASSERT_OK(r);
        ASSERT_OK(manager_startup(m, NULL, NULL, NULL, NULL));
 
        ASSERT_NOT_NULL(u = unit_new(m, sizeof(Service)));
        ASSERT_OK_ZERO(unit_add_name(u, "foobar@1.service"));
        u->fragment_path = strdup("/foobar@.service");
 
        ASSERT_OK(hashmap_put_strdup(&m->unit_id_map, "foobar@foobar@123.service", "/foobar@.service"));
        ASSERT_OK(hashmap_put_strdup(&m->unit_id_map, "foobar@foobar@456.service", "/custom.service"));
 
        /* Test that %n, %N and any extension of %i specifiers in the instance are detected as recursive. */
        ASSERT_EQ(unit_is_likely_recursive_template_dependency(u, "foobar@foobar@123.service", "foobar@%N.service"), 1);
        ASSERT_EQ(unit_is_likely_recursive_template_dependency(u, "foobar@foobar@123.service", "foobar@%n.service"), 1);
        ASSERT_EQ(unit_is_likely_recursive_template_dependency(u, "foobar@foobar@123.service", "foobar@a%i.service"), 1);
        ASSERT_EQ(unit_is_likely_recursive_template_dependency(u, "foobar@foobar@123.service", "foobar@%ia.service"), 1);
        ASSERT_EQ(unit_is_likely_recursive_template_dependency(u, "foobar@foobar@123.service", "foobar@%x%n.service"), 1);
        /* Test that %i on its own is not detected as recursive. */
        ASSERT_EQ(unit_is_likely_recursive_template_dependency(u, "foobar@foobar@123.service", "foobar@%i.service"), 0);
        /* Test that a specifier other than %i, %n and %N is not detected as recursive. */
        ASSERT_EQ(unit_is_likely_recursive_template_dependency(u, "foobar@foobar@123.service", "foobar@%xn.service"), 0);
        /* Test that an expanded specifier is not detected as recursive. */
        ASSERT_EQ(unit_is_likely_recursive_template_dependency(u, "foobar@foobar@123.service", "foobar@foobar@123.service"), 0);
        /* Test that a dependency with a custom fragment path is not detected as recursive. */
        ASSERT_EQ(unit_is_likely_recursive_template_dependency(u, "foobar@foobar@456.service", "foobar@%n.service"), 0);
        /* Test that a dependency without a fragment path is not detected as recursive. */
        ASSERT_EQ(unit_is_likely_recursive_template_dependency(u, "foobar@foobar@789.service", "foobar@%n.service"), 0);
        /* Test that a dependency with a different prefix is not detected as recursive. */
        ASSERT_EQ(unit_is_likely_recursive_template_dependency(u, "quux@foobar@123.service", "quux@%n.service"), 0);
        /* Test that a dependency of a different type is not detected as recursive. */
        ASSERT_EQ(unit_is_likely_recursive_template_dependency(u, "foobar@foobar@123.mount", "foobar@%n.mount"), 0);
}
 
#define TEST_PATTERN(_regex, _allowed_patterns_count, _denied_patterns_count)   \
        {                                                                       \
                .regex = _regex,                                                \
                .allowed_patterns_count = _allowed_patterns_count,              \
                .denied_patterns_count = _denied_patterns_count                 \
        }
 
TEST(config_parse_log_filter_patterns) {
        ExecContext c = {};
 
        static const struct {
                const char *regex;
                size_t allowed_patterns_count;
                size_t denied_patterns_count;
        } regex_tests[] = {
                TEST_PATTERN("", 0, 0),
                TEST_PATTERN(".*", 1, 0),
                TEST_PATTERN("~.*", 1, 1),
                TEST_PATTERN("", 0, 0),
                TEST_PATTERN("~.*", 0, 1),
                TEST_PATTERN("[.*", 0, 1),              /* Invalid pattern. */
                TEST_PATTERN(".*gg.*", 1, 1),
                TEST_PATTERN("~.*", 1, 1),              /* Already in the patterns list. */
                TEST_PATTERN("[.*", 1, 1),              /* Invalid pattern. */
                TEST_PATTERN("\\x7ehello", 2, 1),
                TEST_PATTERN("", 0, 0),
                TEST_PATTERN("~foobar", 0, 1),
        };
 
        if (ERRNO_IS_NOT_SUPPORTED(dlopen_pcre2(LOG_DEBUG)))
                return (void) log_tests_skipped("PCRE2 support is not available");
 
        FOREACH_ELEMENT(test, regex_tests) {
                ASSERT_OK(config_parse_log_filter_patterns(NULL, "fake", 1, "section", 1, "LogFilterPatterns", 1,
                                                           test->regex, &c, NULL));
 
                ASSERT_EQ(set_size(c.log_filter_allowed_patterns), test->allowed_patterns_count);
                ASSERT_EQ(set_size(c.log_filter_denied_patterns), test->denied_patterns_count);
 
                /* Ensure `~` is properly removed */
                const char *p;
                SET_FOREACH(p, c.log_filter_allowed_patterns)
                        ASSERT_TRUE(p && p[0] != '~');
                SET_FOREACH(p, c.log_filter_denied_patterns)
                        ASSERT_TRUE(p && p[0] != '~');
        }
 
        exec_context_done(&c);
}
 
TEST(config_parse_open_file) {
        _cleanup_(manager_freep) Manager *m = NULL;
        _cleanup_(unit_freep) Unit *u = NULL;
        _cleanup_(open_file_freep) OpenFile *of = NULL;
        int r;
 
        r = manager_new(RUNTIME_SCOPE_USER, MANAGER_TEST_RUN_MINIMAL, &m);
        if (manager_errno_skip_test(r)) {
                log_notice_errno(r, "Skipping test: manager_new: %m");
                return;
        }
 
        ASSERT_OK(r);
        ASSERT_OK(manager_startup(m, NULL, NULL, NULL, NULL));
 
        ASSERT_NOT_NULL(u = unit_new(m, sizeof(Service)));
        ASSERT_OK_ZERO(unit_add_name(u, "foobar.service"));
 
        ASSERT_OK(config_parse_open_file(NULL, "fake", 1, "section", 1,
                                         "OpenFile", 0, "/proc/1/ns/mnt:host-mount-namespace:read-only",
                                         &of, u));
        ASSERT_NOT_NULL(of);
        ASSERT_STREQ(of->path, "/proc/1/ns/mnt");
        ASSERT_STREQ(of->fdname, "host-mount-namespace");
        ASSERT_EQ(of->flags, OPENFILE_READ_ONLY);
 
        of = open_file_free(of);
        ASSERT_OK(config_parse_open_file(NULL, "fake", 1, "section", 1,
                                         "OpenFile", 0, "/proc/1/ns/mnt::read-only",
                                         &of, u));
        ASSERT_NOT_NULL(of);
        ASSERT_STREQ(of->path, "/proc/1/ns/mnt");
        ASSERT_STREQ(of->fdname, "mnt");
        ASSERT_EQ(of->flags, OPENFILE_READ_ONLY);
 
        ASSERT_OK(config_parse_open_file(NULL, "fake", 1, "section", 1,
                                         "OpenFile", 0, "",
                                         &of, u));
        ASSERT_NULL(of);
}
 
TEST(config_parse_service_refresh_on_reload) {
        ServiceRefreshOnReload flags;
        _cleanup_strv_free_ char **l = NULL;
        int r;
 
        r = service_refresh_on_reload_from_string_many("extensions", &flags);
        ASSERT_OK(r);
        ASSERT_EQ(flags, SERVICE_RELOAD_EXTENSIONS);
        ASSERT_OK(service_refresh_on_reload_to_strv(flags, &l));
        ASSERT_TRUE(strv_equal(l, STRV_MAKE("extensions")));
 
        l = strv_free(l);
 
        r = service_refresh_on_reload_from_string_many("credentials extensions", &flags);
        ASSERT_OK(r);
        ASSERT_EQ(flags, SERVICE_RELOAD_EXTENSIONS|SERVICE_RELOAD_CREDENTIALS);
        ASSERT_OK(service_refresh_on_reload_to_strv(flags, &l));
        ASSERT_TRUE(strv_equal(l, STRV_MAKE("extensions", "credentials")));
 
        ASSERT_ERROR(service_refresh_on_reload_from_string_many("hoge", &flags), EINVAL);
 
        _cleanup_(manager_freep) Manager *m = NULL;
        _cleanup_(unit_freep) Unit *u = NULL;
 
        r = manager_new(RUNTIME_SCOPE_USER, MANAGER_TEST_RUN_MINIMAL, &m);
        if (manager_errno_skip_test(r)) {
                log_notice_errno(r, "Skipping test: manager_new: %m");
                return;
        }
 
        ASSERT_OK(r);
        ASSERT_OK(manager_startup(m, NULL, NULL, NULL, NULL));
 
        ASSERT_NOT_NULL(u = unit_new(m, sizeof(Service)));
        ASSERT_OK_ZERO(unit_add_name(u, "foobar.service"));
 
        ASSERT_FALSE(SERVICE(u)->refresh_on_reload_set);
 
        ASSERT_OK(config_parse_service_refresh_on_reload(
                                NULL, "fake", 1, "section", 1,
                                "RefreshOnReload", 0, "no",
                                NULL, u));
        ASSERT_TRUE(SERVICE(u)->refresh_on_reload_set);
        ASSERT_EQ(SERVICE(u)->refresh_on_reload_flags, 0);
 
        ASSERT_OK(config_parse_service_refresh_on_reload(
                                NULL, "fake", 1, "section", 1,
                                "RefreshOnReload", 0, "yes",
                                NULL, u));
        ASSERT_TRUE(SERVICE(u)->refresh_on_reload_set);
        ASSERT_EQ(SERVICE(u)->refresh_on_reload_flags, _SERVICE_REFRESH_ON_RELOAD_ALL);
 
        ASSERT_OK(config_parse_service_refresh_on_reload(
                                NULL, "fake", 1, "section", 1,
                                "RefreshOnReload", 0, "~extensions",
                                NULL, u));
        ASSERT_TRUE(SERVICE(u)->refresh_on_reload_set);
        ASSERT_EQ(SERVICE(u)->refresh_on_reload_flags, _SERVICE_REFRESH_ON_RELOAD_ALL & ~SERVICE_RELOAD_EXTENSIONS);
 
        ASSERT_OK(config_parse_service_refresh_on_reload(
                                NULL, "fake", 1, "section", 1,
                                "RefreshOnReload", 0, "~extensions credentials",
                                NULL, u));
        ASSERT_TRUE(SERVICE(u)->refresh_on_reload_set);
        ASSERT_EQ(SERVICE(u)->refresh_on_reload_flags, 0);
 
        ASSERT_OK(config_parse_service_refresh_on_reload(
                                NULL, "fake", 1, "section", 1,
                                "RefreshOnReload", 0, "",
                                NULL, u));
        ASSERT_FALSE(SERVICE(u)->refresh_on_reload_set);
 
        ASSERT_OK(config_parse_service_refresh_on_reload(
                                NULL, "fake", 1, "section", 1,
                                "RefreshOnReload", 0, "~extensions",
                                NULL, u));
        ASSERT_TRUE(SERVICE(u)->refresh_on_reload_set);
        ASSERT_EQ(SERVICE(u)->refresh_on_reload_flags, SERVICE_REFRESH_ON_RELOAD_DEFAULT & ~SERVICE_RELOAD_EXTENSIONS);
}
 
static int intro(void) {
        if (enter_cgroup_subroot(NULL) == -ENOMEDIUM)
                return log_tests_skipped("cgroupfs not available");
 
        ASSERT_NOT_NULL(runtime_dir = setup_fake_runtime_dir());
        return EXIT_SUCCESS;
}
 
DEFINE_TEST_MAIN_WITH_INTRO(LOG_INFO, intro);