]>
Commit | Line | Data |
---|---|---|
1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ | |
2 | ||
3 | #include <stdlib.h> | |
4 | ||
5 | #include "log.h" | |
6 | #include "path-lookup.h" | |
7 | #include "rm-rf.h" | |
8 | #include "string-util.h" | |
9 | #include "strv.h" | |
10 | #include "tests.h" | |
11 | #include "tmpfile-util.h" | |
12 | ||
13 | static void test_paths_one(RuntimeScope scope) { | |
14 | _cleanup_(rm_rf_physical_and_freep) char *tmp = NULL; | |
15 | _cleanup_(lookup_paths_done) LookupPaths lp_without_env = {}; | |
16 | _cleanup_(lookup_paths_done) LookupPaths lp_with_env = {}; | |
17 | char *systemd_unit_path; | |
18 | ||
19 | assert_se(mkdtemp_malloc("/tmp/test-path-lookup.XXXXXXX", &tmp) >= 0); | |
20 | ||
21 | assert_se(unsetenv("SYSTEMD_UNIT_PATH") == 0); | |
22 | assert_se(lookup_paths_init(&lp_without_env, scope, 0, NULL) >= 0); | |
23 | assert_se(!strv_isempty(lp_without_env.search_path)); | |
24 | lookup_paths_log(&lp_without_env); | |
25 | ||
26 | systemd_unit_path = strjoina(tmp, "/systemd-unit-path"); | |
27 | assert_se(setenv("SYSTEMD_UNIT_PATH", systemd_unit_path, 1) == 0); | |
28 | assert_se(lookup_paths_init(&lp_with_env, scope, 0, NULL) == 0); | |
29 | assert_se(strv_length(lp_with_env.search_path) == 1); | |
30 | ASSERT_STREQ(lp_with_env.search_path[0], systemd_unit_path); | |
31 | lookup_paths_log(&lp_with_env); | |
32 | assert_se(strv_equal(lp_with_env.search_path, STRV_MAKE(systemd_unit_path))); | |
33 | } | |
34 | ||
35 | TEST(paths) { | |
36 | test_paths_one(RUNTIME_SCOPE_SYSTEM); | |
37 | test_paths_one(RUNTIME_SCOPE_USER); | |
38 | test_paths_one(RUNTIME_SCOPE_GLOBAL); | |
39 | } | |
40 | ||
41 | TEST(user_and_global_paths) { | |
42 | _cleanup_(lookup_paths_done) LookupPaths lp_global = {}, lp_user = {}; | |
43 | char **u, **g; | |
44 | unsigned k = 0; | |
45 | ||
46 | assert_se(unsetenv("SYSTEMD_UNIT_PATH") == 0); | |
47 | assert_se(unsetenv("XDG_DATA_DIRS") == 0); | |
48 | assert_se(unsetenv("XDG_CONFIG_DIRS") == 0); | |
49 | ||
50 | assert_se(lookup_paths_init(&lp_global, RUNTIME_SCOPE_GLOBAL, 0, NULL) == 0); | |
51 | assert_se(lookup_paths_init(&lp_user, RUNTIME_SCOPE_USER, 0, NULL) == 0); | |
52 | g = lp_global.search_path; | |
53 | u = lp_user.search_path; | |
54 | ||
55 | /* Go over all entries in global search path, and verify | |
56 | * that they also exist in the user search path. Skip any | |
57 | * entries in user search path which don't exist in the global | |
58 | * one, but not vice versa. */ | |
59 | STRV_FOREACH(p, g) { | |
60 | while (u[k] && !streq(*p, u[k])) { | |
61 | log_info("+ %s", u[k]); | |
62 | k++; | |
63 | } | |
64 | log_info(" %s", *p); | |
65 | assert_se(u[k]); /* If NULL, we didn't find a matching entry */ | |
66 | k++; | |
67 | } | |
68 | STRV_FOREACH(p, u + k) | |
69 | log_info("+ %s", *p); | |
70 | } | |
71 | ||
72 | static void test_generator_binary_paths_one(RuntimeScope scope) { | |
73 | _cleanup_(rm_rf_physical_and_freep) char *tmp = NULL; | |
74 | _cleanup_strv_free_ char **gp_without_env = NULL; | |
75 | _cleanup_strv_free_ char **env_gp_without_env = NULL; | |
76 | _cleanup_strv_free_ char **gp_with_env = NULL; | |
77 | _cleanup_strv_free_ char **env_gp_with_env = NULL; | |
78 | char *systemd_generator_path = NULL; | |
79 | char *systemd_env_generator_path = NULL; | |
80 | ||
81 | assert_se(mkdtemp_malloc("/tmp/test-path-lookup.XXXXXXX", &tmp) >= 0); | |
82 | ||
83 | assert_se(unsetenv("SYSTEMD_GENERATOR_PATH") == 0); | |
84 | assert_se(unsetenv("SYSTEMD_ENVIRONMENT_GENERATOR_PATH") == 0); | |
85 | ||
86 | gp_without_env = generator_binary_paths(scope); | |
87 | env_gp_without_env = env_generator_binary_paths(scope); | |
88 | ||
89 | log_info("Generators dirs (%s):", runtime_scope_to_string(scope)); | |
90 | STRV_FOREACH(dir, gp_without_env) | |
91 | log_info(" %s", *dir); | |
92 | ||
93 | log_info("Environment generators dirs (%s):", runtime_scope_to_string(scope)); | |
94 | STRV_FOREACH(dir, env_gp_without_env) | |
95 | log_info(" %s", *dir); | |
96 | ||
97 | assert_se(!strv_isempty(gp_without_env)); | |
98 | assert_se(!strv_isempty(env_gp_without_env)); | |
99 | ||
100 | systemd_generator_path = strjoina(tmp, "/systemd-generator-path"); | |
101 | systemd_env_generator_path = strjoina(tmp, "/systemd-environment-generator-path"); | |
102 | assert_se(setenv("SYSTEMD_GENERATOR_PATH", systemd_generator_path, 1) == 0); | |
103 | assert_se(setenv("SYSTEMD_ENVIRONMENT_GENERATOR_PATH", systemd_env_generator_path, 1) == 0); | |
104 | ||
105 | gp_with_env = generator_binary_paths(scope); | |
106 | env_gp_with_env = env_generator_binary_paths(scope); | |
107 | ||
108 | log_info("Generators dirs (%s):", runtime_scope_to_string(scope)); | |
109 | STRV_FOREACH(dir, gp_with_env) | |
110 | log_info(" %s", *dir); | |
111 | ||
112 | log_info("Environment generators dirs (%s):", runtime_scope_to_string(scope)); | |
113 | STRV_FOREACH(dir, env_gp_with_env) | |
114 | log_info(" %s", *dir); | |
115 | ||
116 | assert_se(strv_equal(gp_with_env, STRV_MAKE(systemd_generator_path))); | |
117 | assert_se(strv_equal(env_gp_with_env, STRV_MAKE(systemd_env_generator_path))); | |
118 | } | |
119 | ||
120 | TEST(generator_binary_paths) { | |
121 | test_generator_binary_paths_one(RUNTIME_SCOPE_SYSTEM); | |
122 | test_generator_binary_paths_one(RUNTIME_SCOPE_USER); | |
123 | } | |
124 | ||
125 | DEFINE_TEST_MAIN(LOG_DEBUG); |