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