]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-path-lookup.c
Merge pull request #22791 from keszybz/bootctl-invert-order
[thirdparty/systemd.git] / src / test / test-path-lookup.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <stdlib.h>
4 #include <sys/stat.h>
5
6 #include "log.h"
7 #include "path-lookup.h"
8 #include "rm-rf.h"
9 #include "string-util.h"
10 #include "strv.h"
11 #include "tests.h"
12 #include "tmpfile-util.h"
13
14 static void test_paths_one(UnitFileScope scope) {
15 _cleanup_(rm_rf_physical_and_freep) char *tmp = NULL;
16 _cleanup_(lookup_paths_free) LookupPaths lp_without_env = {};
17 _cleanup_(lookup_paths_free) LookupPaths lp_with_env = {};
18 char *systemd_unit_path;
19
20 assert_se(mkdtemp_malloc("/tmp/test-path-lookup.XXXXXXX", &tmp) >= 0);
21
22 assert_se(unsetenv("SYSTEMD_UNIT_PATH") == 0);
23 assert_se(lookup_paths_init(&lp_without_env, scope, 0, NULL) >= 0);
24 assert_se(!strv_isempty(lp_without_env.search_path));
25 lookup_paths_log(&lp_without_env);
26
27 systemd_unit_path = strjoina(tmp, "/systemd-unit-path");
28 assert_se(setenv("SYSTEMD_UNIT_PATH", systemd_unit_path, 1) == 0);
29 assert_se(lookup_paths_init(&lp_with_env, scope, 0, NULL) == 0);
30 assert_se(strv_length(lp_with_env.search_path) == 1);
31 assert_se(streq(lp_with_env.search_path[0], systemd_unit_path));
32 lookup_paths_log(&lp_with_env);
33 assert_se(strv_equal(lp_with_env.search_path, STRV_MAKE(systemd_unit_path)));
34 }
35
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) {
43 _cleanup_(lookup_paths_free) LookupPaths lp_global = {}, lp_user = {};
44 char **u, **g;
45 unsigned k = 0;
46
47 assert_se(unsetenv("SYSTEMD_UNIT_PATH") == 0);
48 assert_se(unsetenv("XDG_DATA_DIRS") == 0);
49 assert_se(unsetenv("XDG_CONFIG_DIRS") == 0);
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. */
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);
66 assert_se(u[k]); /* If NULL, we didn't find a matching entry */
67 k++;
68 }
69 STRV_FOREACH(p, u + k)
70 log_info("+ %s", *p);
71 }
72
73 static void test_generator_binary_paths_one(UnitFileScope scope) {
74 _cleanup_(rm_rf_physical_and_freep) char *tmp = NULL;
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;
81
82 assert_se(mkdtemp_malloc("/tmp/test-path-lookup.XXXXXXX", &tmp) >= 0);
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
101 systemd_generator_path = strjoina(tmp, "/systemd-generator-path");
102 systemd_env_generator_path = strjoina(tmp, "/systemd-environment-generator-path");
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
109 log_info("Generators dirs (%s):", scope == UNIT_FILE_SYSTEM ? "system" : "user");
110 STRV_FOREACH(dir, gp_with_env)
111 log_info(" %s", *dir);
112
113 log_info("Environment generators dirs (%s):", scope == UNIT_FILE_SYSTEM ? "system" : "user");
114 STRV_FOREACH(dir, env_gp_with_env)
115 log_info(" %s", *dir);
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)));
119 }
120
121 TEST(generator_binary_paths) {
122 test_generator_binary_paths_one(UNIT_FILE_SYSTEM);
123 test_generator_binary_paths_one(UNIT_FILE_USER);
124 }
125
126 DEFINE_TEST_MAIN(LOG_DEBUG);