]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-path-lookup.c
core: rework logic to drop duplicate and non-existing items from search path
[thirdparty/systemd.git] / src / test / test-path-lookup.c
1 /***
2 This file is part of systemd.
3
4 Copyright 2014 Zbigniew Jędrzejewski-Szmek
5
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
10
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
18 ***/
19
20 #include <stdlib.h>
21 #include <sys/stat.h>
22
23 #include "log.h"
24 #include "path-lookup.h"
25 #include "rm-rf.h"
26 #include "string-util.h"
27 #include "strv.h"
28
29 static void test_paths(UnitFileScope scope) {
30 char template[] = "/tmp/test-path-lookup.XXXXXXX";
31
32 _cleanup_lookup_paths_free_ LookupPaths lp_without_env = {};
33 _cleanup_lookup_paths_free_ LookupPaths lp_with_env = {};
34 char *systemd_unit_path;
35
36 assert_se(mkdtemp(template));
37
38 assert_se(unsetenv("SYSTEMD_UNIT_PATH") == 0);
39 assert_se(lookup_paths_init(&lp_without_env, scope, NULL) >= 0);
40 assert_se(lookup_paths_reduce(&lp_without_env) >= 0);
41 assert_se(!strv_isempty(lp_without_env.search_path));
42
43 systemd_unit_path = strjoina(template, "/systemd-unit-path");
44 assert_se(setenv("SYSTEMD_UNIT_PATH", systemd_unit_path, 1) == 0);
45 assert_se(lookup_paths_init(&lp_with_env, scope, NULL) == 0);
46 assert_se(strv_length(lp_with_env.search_path) == 1);
47 assert_se(streq(lp_with_env.search_path[0], systemd_unit_path));
48 assert_se(lookup_paths_reduce(&lp_with_env) >= 0);
49 assert_se(strv_length(lp_with_env.search_path) == 0);
50
51 assert_se(rm_rf(template, REMOVE_ROOT|REMOVE_PHYSICAL) >= 0);
52 }
53
54 static void print_generator_paths(UnitFileScope scope) {
55 _cleanup_strv_free_ char **paths;
56 char **dir;
57
58 log_info("Generators dirs (%s):", scope == UNIT_FILE_SYSTEM ? "system" : "user");
59
60 paths = generator_paths(scope);
61 STRV_FOREACH(dir, paths)
62 log_info(" %s", *dir);
63 }
64
65 int main(int argc, char **argv) {
66 log_set_max_level(LOG_DEBUG);
67 log_parse_environment();
68 log_open();
69
70 test_paths(UNIT_FILE_SYSTEM);
71 test_paths(UNIT_FILE_USER);
72 test_paths(UNIT_FILE_GLOBAL);
73
74 print_generator_paths(UNIT_FILE_SYSTEM);
75 print_generator_paths(UNIT_FILE_USER);
76
77 return EXIT_SUCCESS;
78 }