]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/test/test-path-lookup.c
doc: mention that units can be masked via credentials
[thirdparty/systemd.git] / src / test / test-path-lookup.c
CommitLineData
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
4870133b 14static void test_paths_one(RuntimeScope scope) {
84e8602d 15 _cleanup_(rm_rf_physical_and_freep) char *tmp = NULL;
7dfc7139
MY
16 _cleanup_(lookup_paths_done) LookupPaths lp_without_env = {};
17 _cleanup_(lookup_paths_done) 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 30 assert_se(strv_length(lp_with_env.search_path) == 1);
c79e88b3 31 ASSERT_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 36TEST(paths) {
4870133b
LP
37 test_paths_one(RUNTIME_SCOPE_SYSTEM);
38 test_paths_one(RUNTIME_SCOPE_USER);
39 test_paths_one(RUNTIME_SCOPE_GLOBAL);
4f7452a8
JJ
40}
41
42TEST(user_and_global_paths) {
7dfc7139 43 _cleanup_(lookup_paths_done) 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 50
4870133b
LP
51 assert_se(lookup_paths_init(&lp_global, RUNTIME_SCOPE_GLOBAL, 0, NULL) == 0);
52 assert_se(lookup_paths_init(&lp_user, RUNTIME_SCOPE_USER, 0, NULL) == 0);
7e684baf
ZJS
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
4870133b 73static void test_generator_binary_paths_one(RuntimeScope 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);
4870133b 88 env_gp_without_env = env_generator_binary_paths(scope);
96b10a13 89
4870133b 90 log_info("Generators dirs (%s):", runtime_scope_to_string(scope));
96b10a13
BB
91 STRV_FOREACH(dir, gp_without_env)
92 log_info(" %s", *dir);
93
4870133b 94 log_info("Environment generators dirs (%s):", runtime_scope_to_string(scope));
96b10a13
BB
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);
4870133b 107 env_gp_with_env = env_generator_binary_paths(scope);
96b10a13 108
4870133b 109 log_info("Generators dirs (%s):", runtime_scope_to_string(scope));
96b10a13
BB
110 STRV_FOREACH(dir, gp_with_env)
111 log_info(" %s", *dir);
4f1a33dd 112
4870133b 113 log_info("Environment generators dirs (%s):", runtime_scope_to_string(scope));
96b10a13 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 121TEST(generator_binary_paths) {
4870133b
LP
122 test_generator_binary_paths_one(RUNTIME_SCOPE_SYSTEM);
123 test_generator_binary_paths_one(RUNTIME_SCOPE_USER);
4f1a33dd 124}
4f7452a8
JJ
125
126DEFINE_TEST_MAIN(LOG_DEBUG);