]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-conf-files.c
Merge pull request #6910 from ssahani/issue-6359
[thirdparty/systemd.git] / src / test / test-conf-files.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
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
21 #include <stdarg.h>
22 #include <stdio.h>
23
24 #include "alloc-util.h"
25 #include "conf-files.h"
26 #include "fs-util.h"
27 #include "macro.h"
28 #include "parse-util.h"
29 #include "rm-rf.h"
30 #include "string-util.h"
31 #include "strv.h"
32 #include "user-util.h"
33 #include "util.h"
34
35 static 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);
43 assert_se(touch_file(path, true, USEC_INFINITY, UID_INVALID, GID_INVALID, MODE_INVALID) == 0);
44 files = va_arg(ap, const char *);
45 }
46 va_end(ap);
47 }
48
49 static void test_conf_files_list(bool use_root) {
50 char tmp_dir[] = "/tmp/test-conf-files-XXXXXX";
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__);
55
56 setup_test_dir(tmp_dir,
57 "/dir1/a.conf",
58 "/dir2/a.conf",
59 "/dir2/b.conf",
60 "/dir2/c.foo",
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;
69 search_1 = strjoina(tmp_dir, "/dir1");
70 search_2 = strjoina(tmp_dir, "/dir2");
71 }
72
73 expect_a = strjoina(tmp_dir, "/dir1/a.conf");
74 expect_b = strjoina(tmp_dir, "/dir2/b.conf");
75 expect_c = strjoina(tmp_dir, "/dir2/c.foo");
76
77 log_debug("/* Check when filtered by suffix */");
78
79 assert_se(conf_files_list(&found_files, ".conf", root_dir, 0, search_1, search_2, NULL) == 0);
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
87 log_debug("/* Check when unfiltered */");
88 assert_se(conf_files_list(&found_files2, NULL, root_dir, 0, search_1, search_2, NULL) == 0);
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
97 assert_se(rm_rf(tmp_dir, REMOVE_ROOT|REMOVE_PHYSICAL) == 0);
98 }
99
100 int main(int argc, char **argv) {
101 log_set_max_level(LOG_DEBUG);
102 log_parse_environment();
103 log_open();
104
105 test_conf_files_list(false);
106 test_conf_files_list(true);
107 return 0;
108 }