]>
Commit | Line | Data |
---|---|---|
db9ecf05 | 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
4f1a33dd | 2 | |
1b6a0f0b | 3 | #include <stdlib.h> |
4f1a33dd | 4 | |
4f1a33dd | 5 | #include "log.h" |
07630cea | 6 | #include "path-lookup.h" |
c6878637 | 7 | #include "rm-rf.h" |
07630cea LP |
8 | #include "string-util.h" |
9 | #include "strv.h" | |
6d7c4033 | 10 | #include "tests.h" |
84e8602d | 11 | #include "tmpfile-util.h" |
4f1a33dd | 12 | |
4870133b | 13 | static void test_paths_one(RuntimeScope scope) { |
84e8602d | 14 | _cleanup_(rm_rf_physical_and_freep) char *tmp = NULL; |
7dfc7139 MY |
15 | _cleanup_(lookup_paths_done) LookupPaths lp_without_env = {}; |
16 | _cleanup_(lookup_paths_done) LookupPaths lp_with_env = {}; | |
a3c4eb07 | 17 | char *systemd_unit_path; |
4f1a33dd | 18 | |
84e8602d | 19 | assert_se(mkdtemp_malloc("/tmp/test-path-lookup.XXXXXXX", &tmp) >= 0); |
4f1a33dd | 20 | |
1b6a0f0b | 21 | assert_se(unsetenv("SYSTEMD_UNIT_PATH") == 0); |
4943d143 | 22 | assert_se(lookup_paths_init(&lp_without_env, scope, 0, NULL) >= 0); |
a3c4eb07 | 23 | assert_se(!strv_isempty(lp_without_env.search_path)); |
581fef8d | 24 | lookup_paths_log(&lp_without_env); |
1b6a0f0b | 25 | |
84e8602d | 26 | systemd_unit_path = strjoina(tmp, "/systemd-unit-path"); |
1b6a0f0b | 27 | assert_se(setenv("SYSTEMD_UNIT_PATH", systemd_unit_path, 1) == 0); |
4943d143 | 28 | assert_se(lookup_paths_init(&lp_with_env, scope, 0, NULL) == 0); |
a3c4eb07 | 29 | assert_se(strv_length(lp_with_env.search_path) == 1); |
c79e88b3 | 30 | ASSERT_STREQ(lp_with_env.search_path[0], systemd_unit_path); |
581fef8d ZJS |
31 | lookup_paths_log(&lp_with_env); |
32 | assert_se(strv_equal(lp_with_env.search_path, STRV_MAKE(systemd_unit_path))); | |
4f1a33dd ZJS |
33 | } |
34 | ||
4f7452a8 | 35 | TEST(paths) { |
4870133b LP |
36 | test_paths_one(RUNTIME_SCOPE_SYSTEM); |
37 | test_paths_one(RUNTIME_SCOPE_USER); | |
38 | test_paths_one(RUNTIME_SCOPE_GLOBAL); | |
4f7452a8 JJ |
39 | } |
40 | ||
41 | TEST(user_and_global_paths) { | |
7dfc7139 | 42 | _cleanup_(lookup_paths_done) LookupPaths lp_global = {}, lp_user = {}; |
de010b0b | 43 | char **u, **g; |
7e684baf ZJS |
44 | unsigned k = 0; |
45 | ||
46 | assert_se(unsetenv("SYSTEMD_UNIT_PATH") == 0); | |
2b8b1056 YW |
47 | assert_se(unsetenv("XDG_DATA_DIRS") == 0); |
48 | assert_se(unsetenv("XDG_CONFIG_DIRS") == 0); | |
7e684baf | 49 | |
4870133b LP |
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); | |
7e684baf ZJS |
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. */ | |
7e684baf ZJS |
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); | |
f21b863e | 65 | assert_se(u[k]); /* If NULL, we didn't find a matching entry */ |
7e684baf ZJS |
66 | k++; |
67 | } | |
68 | STRV_FOREACH(p, u + k) | |
69 | log_info("+ %s", *p); | |
70 | } | |
71 | ||
4870133b | 72 | static void test_generator_binary_paths_one(RuntimeScope scope) { |
84e8602d | 73 | _cleanup_(rm_rf_physical_and_freep) char *tmp = NULL; |
96b10a13 BB |
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; | |
4f1a33dd | 80 | |
84e8602d | 81 | assert_se(mkdtemp_malloc("/tmp/test-path-lookup.XXXXXXX", &tmp) >= 0); |
96b10a13 BB |
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); | |
4870133b | 87 | env_gp_without_env = env_generator_binary_paths(scope); |
96b10a13 | 88 | |
4870133b | 89 | log_info("Generators dirs (%s):", runtime_scope_to_string(scope)); |
96b10a13 BB |
90 | STRV_FOREACH(dir, gp_without_env) |
91 | log_info(" %s", *dir); | |
92 | ||
4870133b | 93 | log_info("Environment generators dirs (%s):", runtime_scope_to_string(scope)); |
96b10a13 BB |
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 | ||
84e8602d YW |
100 | systemd_generator_path = strjoina(tmp, "/systemd-generator-path"); |
101 | systemd_env_generator_path = strjoina(tmp, "/systemd-environment-generator-path"); | |
96b10a13 BB |
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); | |
4870133b | 106 | env_gp_with_env = env_generator_binary_paths(scope); |
96b10a13 | 107 | |
4870133b | 108 | log_info("Generators dirs (%s):", runtime_scope_to_string(scope)); |
96b10a13 BB |
109 | STRV_FOREACH(dir, gp_with_env) |
110 | log_info(" %s", *dir); | |
4f1a33dd | 111 | |
4870133b | 112 | log_info("Environment generators dirs (%s):", runtime_scope_to_string(scope)); |
96b10a13 | 113 | STRV_FOREACH(dir, env_gp_with_env) |
4f1a33dd | 114 | log_info(" %s", *dir); |
96b10a13 BB |
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))); | |
4f1a33dd ZJS |
118 | } |
119 | ||
4f7452a8 | 120 | TEST(generator_binary_paths) { |
4870133b LP |
121 | test_generator_binary_paths_one(RUNTIME_SCOPE_SYSTEM); |
122 | test_generator_binary_paths_one(RUNTIME_SCOPE_USER); | |
4f1a33dd | 123 | } |
4f7452a8 JJ |
124 | |
125 | DEFINE_TEST_MAIN(LOG_DEBUG); |