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