]> git.proxmox.com Git - systemd.git/blame - src/test/test-path-lookup.c
Imported Upstream version 229
[systemd.git] / src / test / test-path-lookup.c
CommitLineData
e735f4d4
MP
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
db2df898 20#include <stdlib.h>
e735f4d4 21#include <sys/stat.h>
e735f4d4 22
e735f4d4 23#include "log.h"
db2df898 24#include "path-lookup.h"
e3bff60a 25#include "rm-rf.h"
db2df898
MP
26#include "string-util.h"
27#include "strv.h"
e735f4d4 28
e3bff60a 29static void test_paths(ManagerRunningAs running_as, bool personal) {
e735f4d4
MP
30 char template[] = "/tmp/test-path-lookup.XXXXXXX";
31
db2df898
MP
32 _cleanup_lookup_paths_free_ LookupPaths lp_without_env = {};
33 _cleanup_lookup_paths_free_ LookupPaths lp_with_env = {};
34 char *exists, *not, *systemd_unit_path;
e735f4d4
MP
35
36 assert_se(mkdtemp(template));
37 exists = strjoina(template, "/exists");
38 assert_se(mkdir(exists, 0755) == 0);
39 not = strjoina(template, "/not");
40
db2df898
MP
41 assert_se(unsetenv("SYSTEMD_UNIT_PATH") == 0);
42 assert_se(lookup_paths_init(&lp_without_env, running_as, personal, NULL, exists, not, not) == 0);
43
44 assert_se(!strv_isempty(lp_without_env.unit_path));
45 assert_se(strv_contains(lp_without_env.unit_path, exists));
46 assert_se(strv_contains(lp_without_env.unit_path, not));
e735f4d4 47
db2df898
MP
48 systemd_unit_path = strjoina(template, "/systemd-unit-path");
49 assert_se(setenv("SYSTEMD_UNIT_PATH", systemd_unit_path, 1) == 0);
50 assert_se(lookup_paths_init(&lp_with_env, running_as, personal, NULL, exists, not, not) == 0);
51 assert_se(strv_length(lp_with_env.unit_path) == 1);
52 assert_se(streq(lp_with_env.unit_path[0], systemd_unit_path));
e735f4d4 53
e3bff60a 54 assert_se(rm_rf(template, REMOVE_ROOT|REMOVE_PHYSICAL) >= 0);
e735f4d4
MP
55}
56
e3bff60a 57static void print_generator_paths(ManagerRunningAs running_as) {
e735f4d4
MP
58 _cleanup_strv_free_ char **paths;
59 char **dir;
60
e3bff60a 61 log_info("Generators dirs (%s):", running_as == MANAGER_SYSTEM ? "system" : "user");
e735f4d4
MP
62
63 paths = generator_paths(running_as);
64 STRV_FOREACH(dir, paths)
65 log_info(" %s", *dir);
66}
67
68int main(int argc, char **argv) {
69 log_set_max_level(LOG_DEBUG);
70 log_parse_environment();
71 log_open();
72
e3bff60a
MP
73 test_paths(MANAGER_SYSTEM, false);
74 test_paths(MANAGER_SYSTEM, true);
75 test_paths(MANAGER_USER, false);
76 test_paths(MANAGER_USER, true);
e735f4d4 77
e3bff60a
MP
78 print_generator_paths(MANAGER_SYSTEM);
79 print_generator_paths(MANAGER_USER);
e735f4d4
MP
80
81 return EXIT_SUCCESS;
82}