]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/test/test-path-lookup.c
calendarspec: rename free_chain() to chain_free()
[thirdparty/systemd.git] / src / test / test-path-lookup.c
CommitLineData
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 13static 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));
e46e4422 25 assert_se(lookup_paths_reduce(&lp_without_env) >= 0);
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));
a1453343 32 assert_se(lookup_paths_reduce(&lp_with_env) >= 0);
7b943bb7 33 assert_se(strv_isempty(lp_with_env.search_path));
4f1a33dd 34
c6878637 35 assert_se(rm_rf(template, REMOVE_ROOT|REMOVE_PHYSICAL) >= 0);
4f1a33dd
ZJS
36}
37
7e684baf 38static 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
9183df70 70static void print_generator_binary_paths(UnitFileScope scope) {
4f1a33dd
ZJS
71 _cleanup_strv_free_ char **paths;
72 char **dir;
73
463d0d15 74 log_info("Generators dirs (%s):", scope == UNIT_FILE_SYSTEM ? "system" : "user");
4f1a33dd 75
9183df70 76 paths = generator_binary_paths(scope);
4f1a33dd
ZJS
77 STRV_FOREACH(dir, paths)
78 log_info(" %s", *dir);
79}
80
81int main(int argc, char **argv) {
6d7c4033 82 test_setup_logging(LOG_DEBUG);
4f1a33dd 83
463d0d15
LP
84 test_paths(UNIT_FILE_SYSTEM);
85 test_paths(UNIT_FILE_USER);
86 test_paths(UNIT_FILE_GLOBAL);
4f1a33dd 87
7e684baf
ZJS
88 test_user_and_global_paths();
89
9183df70
LP
90 print_generator_binary_paths(UNIT_FILE_SYSTEM);
91 print_generator_binary_paths(UNIT_FILE_USER);
4f1a33dd
ZJS
92
93 return EXIT_SUCCESS;
94}