]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
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" |
4f1a33dd | 12 | |
463d0d15 | 13 | static void test_paths(UnitFileScope scope) { |
4f1a33dd ZJS |
14 | char template[] = "/tmp/test-path-lookup.XXXXXXX"; |
15 | ||
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 ZJS |
19 | |
20 | assert_se(mkdtemp(template)); | |
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 EV |
26 | |
27 | systemd_unit_path = strjoina(template, "/systemd-unit-path"); | |
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 | 34 | |
c6878637 | 35 | assert_se(rm_rf(template, REMOVE_ROOT|REMOVE_PHYSICAL) >= 0); |
4f1a33dd ZJS |
36 | } |
37 | ||
7e684baf | 38 | static void test_user_and_global_paths(void) { |
8e766630 | 39 | _cleanup_(lookup_paths_free) LookupPaths lp_global = {}, lp_user = {}; |
7e684baf ZJS |
40 | char **u, **g, **p; |
41 | unsigned k = 0; | |
42 | ||
43 | assert_se(unsetenv("SYSTEMD_UNIT_PATH") == 0); | |
2b8b1056 YW |
44 | assert_se(unsetenv("XDG_DATA_DIRS") == 0); |
45 | assert_se(unsetenv("XDG_CONFIG_DIRS") == 0); | |
7e684baf ZJS |
46 | |
47 | assert_se(lookup_paths_init(&lp_global, UNIT_FILE_GLOBAL, 0, NULL) == 0); | |
48 | assert_se(lookup_paths_init(&lp_user, UNIT_FILE_USER, 0, NULL) == 0); | |
49 | g = lp_global.search_path; | |
50 | u = lp_user.search_path; | |
51 | ||
52 | /* Go over all entries in global search path, and verify | |
53 | * that they also exist in the user search path. Skip any | |
54 | * entries in user search path which don't exist in the global | |
55 | * one, but not vice versa. */ | |
56 | log_info("/* %s */", __func__); | |
57 | STRV_FOREACH(p, g) { | |
58 | while (u[k] && !streq(*p, u[k])) { | |
59 | log_info("+ %s", u[k]); | |
60 | k++; | |
61 | } | |
62 | log_info(" %s", *p); | |
63 | assert(u[k]); /* If NULL, we didn't find a matching entry */ | |
64 | k++; | |
65 | } | |
66 | STRV_FOREACH(p, u + k) | |
67 | log_info("+ %s", *p); | |
68 | } | |
69 | ||
96b10a13 BB |
70 | static void test_generator_binary_paths(UnitFileScope scope) { |
71 | char template[] = "/tmp/test-path-lookup.XXXXXXX"; | |
72 | ||
73 | _cleanup_strv_free_ char **gp_without_env = NULL; | |
74 | _cleanup_strv_free_ char **env_gp_without_env = NULL; | |
75 | _cleanup_strv_free_ char **gp_with_env = NULL; | |
76 | _cleanup_strv_free_ char **env_gp_with_env = NULL; | |
77 | char *systemd_generator_path = NULL; | |
78 | char *systemd_env_generator_path = NULL; | |
4f1a33dd ZJS |
79 | char **dir; |
80 | ||
96b10a13 BB |
81 | assert_se(mkdtemp(template)); |
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 == UNIT_FILE_SYSTEM ? true : false); | |
88 | ||
89 | log_info("Generators dirs (%s):", scope == UNIT_FILE_SYSTEM ? "system" : "user"); | |
90 | STRV_FOREACH(dir, gp_without_env) | |
91 | log_info(" %s", *dir); | |
92 | ||
93 | log_info("Environment generators dirs (%s):", scope == UNIT_FILE_SYSTEM ? "system" : "user"); | |
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(template, "/systemd-generator-path"); | |
101 | systemd_env_generator_path = strjoina(template, "/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 == UNIT_FILE_SYSTEM ? true : false); | |
107 | ||
463d0d15 | 108 | log_info("Generators dirs (%s):", scope == UNIT_FILE_SYSTEM ? "system" : "user"); |
96b10a13 BB |
109 | STRV_FOREACH(dir, gp_with_env) |
110 | log_info(" %s", *dir); | |
4f1a33dd | 111 | |
96b10a13 BB |
112 | log_info("Environment generators dirs (%s):", scope == UNIT_FILE_SYSTEM ? "system" : "user"); |
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 | ||
120 | int main(int argc, char **argv) { | |
6d7c4033 | 121 | test_setup_logging(LOG_DEBUG); |
4f1a33dd | 122 | |
463d0d15 LP |
123 | test_paths(UNIT_FILE_SYSTEM); |
124 | test_paths(UNIT_FILE_USER); | |
125 | test_paths(UNIT_FILE_GLOBAL); | |
4f1a33dd | 126 | |
7e684baf ZJS |
127 | test_user_and_global_paths(); |
128 | ||
96b10a13 BB |
129 | test_generator_binary_paths(UNIT_FILE_SYSTEM); |
130 | test_generator_binary_paths(UNIT_FILE_USER); | |
4f1a33dd ZJS |
131 | |
132 | return EXIT_SUCCESS; | |
133 | } |