Branch data Line data Source code
1 : : /* SPDX-License-Identifier: LGPL-2.1+ */
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 : :
13 : 12 : static void test_paths(UnitFileScope scope) {
14 : 12 : char template[] = "/tmp/test-path-lookup.XXXXXXX";
15 : :
16 : 12 : _cleanup_(lookup_paths_free) LookupPaths lp_without_env = {};
17 : 12 : _cleanup_(lookup_paths_free) LookupPaths lp_with_env = {};
18 : : char *systemd_unit_path;
19 : :
20 [ - + ]: 12 : assert_se(mkdtemp(template));
21 : :
22 [ - + ]: 12 : assert_se(unsetenv("SYSTEMD_UNIT_PATH") == 0);
23 [ - + ]: 12 : assert_se(lookup_paths_init(&lp_without_env, scope, 0, NULL) >= 0);
24 [ - + ]: 12 : assert_se(!strv_isempty(lp_without_env.search_path));
25 [ - + ]: 12 : assert_se(lookup_paths_reduce(&lp_without_env) >= 0);
26 : :
27 [ + + + - : 60 : systemd_unit_path = strjoina(template, "/systemd-unit-path");
- + - + +
+ + - ]
28 [ - + ]: 12 : assert_se(setenv("SYSTEMD_UNIT_PATH", systemd_unit_path, 1) == 0);
29 [ - + ]: 12 : assert_se(lookup_paths_init(&lp_with_env, scope, 0, NULL) == 0);
30 [ - + ]: 12 : assert_se(strv_length(lp_with_env.search_path) == 1);
31 [ - + ]: 12 : assert_se(streq(lp_with_env.search_path[0], systemd_unit_path));
32 [ - + ]: 12 : assert_se(lookup_paths_reduce(&lp_with_env) >= 0);
33 [ - + ]: 12 : assert_se(strv_isempty(lp_with_env.search_path));
34 : :
35 [ - + ]: 12 : assert_se(rm_rf(template, REMOVE_ROOT|REMOVE_PHYSICAL) >= 0);
36 : 12 : }
37 : :
38 : 4 : static void test_user_and_global_paths(void) {
39 : 4 : _cleanup_(lookup_paths_free) LookupPaths lp_global = {}, lp_user = {};
40 : : char **u, **g, **p;
41 : 4 : unsigned k = 0;
42 : :
43 [ - + ]: 4 : assert_se(unsetenv("SYSTEMD_UNIT_PATH") == 0);
44 [ - + ]: 4 : assert_se(unsetenv("XDG_DATA_DIRS") == 0);
45 [ - + ]: 4 : assert_se(unsetenv("XDG_CONFIG_DIRS") == 0);
46 : :
47 [ - + ]: 4 : assert_se(lookup_paths_init(&lp_global, UNIT_FILE_GLOBAL, 0, NULL) == 0);
48 [ - + ]: 4 : assert_se(lookup_paths_init(&lp_user, UNIT_FILE_USER, 0, NULL) == 0);
49 : 4 : g = lp_global.search_path;
50 : 4 : 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 [ + - ]: 4 : log_info("/* %s */", __func__);
57 [ + - + + ]: 28 : STRV_FOREACH(p, g) {
58 [ + - + + ]: 56 : while (u[k] && !streq(*p, u[k])) {
59 [ + - ]: 32 : log_info("+ %s", u[k]);
60 : 32 : k++;
61 : : }
62 [ + - ]: 24 : log_info(" %s", *p);
63 [ - + ]: 24 : assert(u[k]); /* If NULL, we didn't find a matching entry */
64 : 24 : k++;
65 : : }
66 [ + - + + ]: 8 : STRV_FOREACH(p, u + k)
67 [ + - ]: 4 : log_info("+ %s", *p);
68 : 4 : }
69 : :
70 : 8 : static void print_generator_binary_paths(UnitFileScope scope) {
71 : 8 : _cleanup_strv_free_ char **paths;
72 : : char **dir;
73 : :
74 [ + - + + ]: 8 : log_info("Generators dirs (%s):", scope == UNIT_FILE_SYSTEM ? "system" : "user");
75 : :
76 : 8 : paths = generator_binary_paths(scope);
77 [ + - + + ]: 40 : STRV_FOREACH(dir, paths)
78 [ + - ]: 32 : log_info(" %s", *dir);
79 : 8 : }
80 : :
81 : 4 : int main(int argc, char **argv) {
82 : 4 : test_setup_logging(LOG_DEBUG);
83 : :
84 : 4 : test_paths(UNIT_FILE_SYSTEM);
85 : 4 : test_paths(UNIT_FILE_USER);
86 : 4 : test_paths(UNIT_FILE_GLOBAL);
87 : :
88 : 4 : test_user_and_global_paths();
89 : :
90 : 4 : print_generator_binary_paths(UNIT_FILE_SYSTEM);
91 : 4 : print_generator_binary_paths(UNIT_FILE_USER);
92 : :
93 : 4 : return EXIT_SUCCESS;
94 : : }
|