1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
7 #include "path-lookup.h"
9 #include "string-util.h"
12 #include "tmpfile-util.h"
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
;
20 assert_se(mkdtemp_malloc("/tmp/test-path-lookup.XXXXXXX", &tmp
) >= 0);
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
);
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
)));
37 test_paths_one(UNIT_FILE_SYSTEM
);
38 test_paths_one(UNIT_FILE_USER
);
39 test_paths_one(UNIT_FILE_GLOBAL
);
42 TEST(user_and_global_paths
) {
43 _cleanup_(lookup_paths_free
) LookupPaths lp_global
= {}, lp_user
= {};
47 assert_se(unsetenv("SYSTEMD_UNIT_PATH") == 0);
48 assert_se(unsetenv("XDG_DATA_DIRS") == 0);
49 assert_se(unsetenv("XDG_CONFIG_DIRS") == 0);
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
;
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. */
61 while (u
[k
] && !streq(*p
, u
[k
])) {
62 log_info("+ %s", u
[k
]);
66 assert_se(u
[k
]); /* If NULL, we didn't find a matching entry */
69 STRV_FOREACH(p
, u
+ k
)
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
;
82 assert_se(mkdtemp_malloc("/tmp/test-path-lookup.XXXXXXX", &tmp
) >= 0);
84 assert_se(unsetenv("SYSTEMD_GENERATOR_PATH") == 0);
85 assert_se(unsetenv("SYSTEMD_ENVIRONMENT_GENERATOR_PATH") == 0);
87 gp_without_env
= generator_binary_paths(scope
);
88 env_gp_without_env
= env_generator_binary_paths(scope
== UNIT_FILE_SYSTEM
? true : false);
90 log_info("Generators dirs (%s):", scope
== UNIT_FILE_SYSTEM
? "system" : "user");
91 STRV_FOREACH(dir
, gp_without_env
)
92 log_info(" %s", *dir
);
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
);
98 assert_se(!strv_isempty(gp_without_env
));
99 assert_se(!strv_isempty(env_gp_without_env
));
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);
106 gp_with_env
= generator_binary_paths(scope
);
107 env_gp_with_env
= env_generator_binary_paths(scope
== UNIT_FILE_SYSTEM
? true : false);
109 log_info("Generators dirs (%s):", scope
== UNIT_FILE_SYSTEM
? "system" : "user");
110 STRV_FOREACH(dir
, gp_with_env
)
111 log_info(" %s", *dir
);
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
);
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
)));
121 TEST(generator_binary_paths
) {
122 test_generator_binary_paths_one(UNIT_FILE_SYSTEM
);
123 test_generator_binary_paths_one(UNIT_FILE_USER
);
126 DEFINE_TEST_MAIN(LOG_DEBUG
);