]> git.proxmox.com Git - systemd.git/blame - src/test/test-conf-files.c
New upstream version 236
[systemd.git] / src / test / test-conf-files.c
CommitLineData
52ad194e 1/* SPDX-License-Identifier: LGPL-2.1+ */
e842803a
MB
2/***
3 This file is part of systemd.
4
5 Copyright 2014 Michael Marineau
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
e842803a 21#include <stdarg.h>
db2df898 22#include <stdio.h>
e842803a 23
db2df898 24#include "alloc-util.h"
e842803a 25#include "conf-files.h"
db2df898 26#include "fs-util.h"
e842803a 27#include "macro.h"
db2df898
MP
28#include "parse-util.h"
29#include "rm-rf.h"
30#include "string-util.h"
e842803a 31#include "strv.h"
db2df898 32#include "user-util.h"
e842803a 33#include "util.h"
e842803a
MB
34
35static void setup_test_dir(char *tmp_dir, const char *files, ...) {
36 va_list ap;
37
38 assert_se(mkdtemp(tmp_dir) != NULL);
39
40 va_start(ap, files);
41 while (files != NULL) {
42 _cleanup_free_ char *path = strappend(tmp_dir, files);
db2df898 43 assert_se(touch_file(path, true, USEC_INFINITY, UID_INVALID, GID_INVALID, MODE_INVALID) == 0);
e842803a
MB
44 files = va_arg(ap, const char *);
45 }
46 va_end(ap);
47}
48
49static void test_conf_files_list(bool use_root) {
50 char tmp_dir[] = "/tmp/test-conf-files-XXXXXX";
2897b343
MP
51 _cleanup_strv_free_ char **found_files = NULL, **found_files2 = NULL;
52 const char *root_dir, *search_1, *search_2, *expect_a, *expect_b, *expect_c;
53
54 log_debug("/* %s */", __func__);
e842803a
MB
55
56 setup_test_dir(tmp_dir,
57 "/dir1/a.conf",
58 "/dir2/a.conf",
59 "/dir2/b.conf",
2897b343 60 "/dir2/c.foo",
e842803a
MB
61 NULL);
62
63 if (use_root) {
64 root_dir = tmp_dir;
65 search_1 = "/dir1";
66 search_2 = "/dir2";
67 } else {
68 root_dir = NULL;
e735f4d4
MP
69 search_1 = strjoina(tmp_dir, "/dir1");
70 search_2 = strjoina(tmp_dir, "/dir2");
e842803a
MB
71 }
72
e735f4d4
MP
73 expect_a = strjoina(tmp_dir, "/dir1/a.conf");
74 expect_b = strjoina(tmp_dir, "/dir2/b.conf");
2897b343
MP
75 expect_c = strjoina(tmp_dir, "/dir2/c.foo");
76
77 log_debug("/* Check when filtered by suffix */");
e842803a 78
f5e65279 79 assert_se(conf_files_list(&found_files, ".conf", root_dir, 0, search_1, search_2, NULL) == 0);
e842803a
MB
80 strv_print(found_files);
81
82 assert_se(found_files);
83 assert_se(streq_ptr(found_files[0], expect_a));
84 assert_se(streq_ptr(found_files[1], expect_b));
85 assert_se(found_files[2] == NULL);
86
2897b343 87 log_debug("/* Check when unfiltered */");
f5e65279 88 assert_se(conf_files_list(&found_files2, NULL, root_dir, 0, search_1, search_2, NULL) == 0);
2897b343
MP
89 strv_print(found_files2);
90
91 assert_se(found_files2);
92 assert_se(streq_ptr(found_files2[0], expect_a));
93 assert_se(streq_ptr(found_files2[1], expect_b));
94 assert_se(streq_ptr(found_files2[2], expect_c));
95 assert_se(found_files2[3] == NULL);
96
e3bff60a 97 assert_se(rm_rf(tmp_dir, REMOVE_ROOT|REMOVE_PHYSICAL) == 0);
e842803a
MB
98}
99
100int main(int argc, char **argv) {
2897b343
MP
101 log_set_max_level(LOG_DEBUG);
102 log_parse_environment();
103 log_open();
104
e842803a
MB
105 test_conf_files_list(false);
106 test_conf_files_list(true);
107 return 0;
108}