]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/test/test-path-lookup.c
Merge pull request #3162 from keszybz/alias-refusal
[thirdparty/systemd.git] / src / test / test-path-lookup.c
CommitLineData
4f1a33dd
ZJS
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
1b6a0f0b 20#include <stdlib.h>
4f1a33dd 21#include <sys/stat.h>
4f1a33dd 22
4f1a33dd 23#include "log.h"
07630cea 24#include "path-lookup.h"
c6878637 25#include "rm-rf.h"
07630cea
LP
26#include "string-util.h"
27#include "strv.h"
4f1a33dd 28
463d0d15 29static void test_paths(UnitFileScope scope) {
4f1a33dd
ZJS
30 char template[] = "/tmp/test-path-lookup.XXXXXXX";
31
1b6a0f0b
EV
32 _cleanup_lookup_paths_free_ LookupPaths lp_without_env = {};
33 _cleanup_lookup_paths_free_ LookupPaths lp_with_env = {};
a3c4eb07 34 char *systemd_unit_path;
4f1a33dd
ZJS
35
36 assert_se(mkdtemp(template));
4f1a33dd 37
1b6a0f0b 38 assert_se(unsetenv("SYSTEMD_UNIT_PATH") == 0);
4943d143 39 assert_se(lookup_paths_init(&lp_without_env, scope, 0, NULL) >= 0);
a3c4eb07 40 assert_se(!strv_isempty(lp_without_env.search_path));
e46e4422 41 assert_se(lookup_paths_reduce(&lp_without_env) >= 0);
1b6a0f0b
EV
42
43 systemd_unit_path = strjoina(template, "/systemd-unit-path");
44 assert_se(setenv("SYSTEMD_UNIT_PATH", systemd_unit_path, 1) == 0);
4943d143 45 assert_se(lookup_paths_init(&lp_with_env, scope, 0, NULL) == 0);
a3c4eb07
LP
46 assert_se(strv_length(lp_with_env.search_path) == 1);
47 assert_se(streq(lp_with_env.search_path[0], systemd_unit_path));
a1453343
LP
48 assert_se(lookup_paths_reduce(&lp_with_env) >= 0);
49 assert_se(strv_length(lp_with_env.search_path) == 0);
4f1a33dd 50
c6878637 51 assert_se(rm_rf(template, REMOVE_ROOT|REMOVE_PHYSICAL) >= 0);
4f1a33dd
ZJS
52}
53
9183df70 54static void print_generator_binary_paths(UnitFileScope scope) {
4f1a33dd
ZJS
55 _cleanup_strv_free_ char **paths;
56 char **dir;
57
463d0d15 58 log_info("Generators dirs (%s):", scope == UNIT_FILE_SYSTEM ? "system" : "user");
4f1a33dd 59
9183df70 60 paths = generator_binary_paths(scope);
4f1a33dd
ZJS
61 STRV_FOREACH(dir, paths)
62 log_info(" %s", *dir);
63}
64
65int main(int argc, char **argv) {
66 log_set_max_level(LOG_DEBUG);
67 log_parse_environment();
68 log_open();
69
463d0d15
LP
70 test_paths(UNIT_FILE_SYSTEM);
71 test_paths(UNIT_FILE_USER);
72 test_paths(UNIT_FILE_GLOBAL);
4f1a33dd 73
9183df70
LP
74 print_generator_binary_paths(UNIT_FILE_SYSTEM);
75 print_generator_binary_paths(UNIT_FILE_USER);
4f1a33dd
ZJS
76
77 return EXIT_SUCCESS;
78}