]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-conf-files.c
5654bc0dc8dee2867ed660dfb0fa7d3bba94708b
[thirdparty/systemd.git] / src / test / test-conf-files.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3 Copyright 2014 Michael Marineau
4 ***/
5
6 #include <stdarg.h>
7 #include <stdio.h>
8
9 #include "alloc-util.h"
10 #include "conf-files.h"
11 #include "fileio.h"
12 #include "fs-util.h"
13 #include "macro.h"
14 #include "mkdir.h"
15 #include "parse-util.h"
16 #include "rm-rf.h"
17 #include "string-util.h"
18 #include "strv.h"
19 #include "user-util.h"
20 #include "util.h"
21
22 static void setup_test_dir(char *tmp_dir, const char *files, ...) {
23 va_list ap;
24
25 assert_se(mkdtemp(tmp_dir));
26
27 va_start(ap, files);
28 while (files) {
29 _cleanup_free_ char *path;
30
31 assert_se(path = strappend(tmp_dir, files));
32 (void) mkdir_parents(path, 0755);
33 assert_se(write_string_file(path, "foobar", WRITE_STRING_FILE_CREATE) >= 0);
34
35 files = va_arg(ap, const char *);
36 }
37 va_end(ap);
38 }
39
40 static void test_conf_files_list(bool use_root) {
41 char tmp_dir[] = "/tmp/test-conf-files-XXXXXX";
42 _cleanup_strv_free_ char **found_files = NULL, **found_files2 = NULL;
43 const char *root_dir, *search_1, *search_2, *expect_a, *expect_b, *expect_c, *mask;
44
45 log_debug("/* %s */", __func__);
46
47 setup_test_dir(tmp_dir,
48 "/dir1/a.conf",
49 "/dir2/a.conf",
50 "/dir2/b.conf",
51 "/dir2/c.foo",
52 "/dir2/d.conf",
53 NULL);
54
55 mask = strjoina(tmp_dir, "/dir1/d.conf");
56 assert_se(symlink("/dev/null", mask) >= 0);
57
58 if (use_root) {
59 root_dir = tmp_dir;
60 search_1 = "/dir1";
61 search_2 = "/dir2";
62 } else {
63 root_dir = NULL;
64 search_1 = strjoina(tmp_dir, "/dir1");
65 search_2 = strjoina(tmp_dir, "/dir2");
66 }
67
68 expect_a = strjoina(tmp_dir, "/dir1/a.conf");
69 expect_b = strjoina(tmp_dir, "/dir2/b.conf");
70 expect_c = strjoina(tmp_dir, "/dir2/c.foo");
71
72 log_debug("/* Check when filtered by suffix */");
73
74 assert_se(conf_files_list(&found_files, ".conf", root_dir, CONF_FILES_FILTER_MASKED, search_1, search_2, NULL) == 0);
75 strv_print(found_files);
76
77 assert_se(found_files);
78 assert_se(streq_ptr(found_files[0], expect_a));
79 assert_se(streq_ptr(found_files[1], expect_b));
80 assert_se(!found_files[2]);
81
82 log_debug("/* Check when unfiltered */");
83 assert_se(conf_files_list(&found_files2, NULL, root_dir, CONF_FILES_FILTER_MASKED, search_1, search_2, NULL) == 0);
84 strv_print(found_files2);
85
86 assert_se(found_files2);
87 assert_se(streq_ptr(found_files2[0], expect_a));
88 assert_se(streq_ptr(found_files2[1], expect_b));
89 assert_se(streq_ptr(found_files2[2], expect_c));
90 assert_se(!found_files2[3]);
91
92 assert_se(rm_rf(tmp_dir, REMOVE_ROOT|REMOVE_PHYSICAL) == 0);
93 }
94
95 int main(int argc, char **argv) {
96 log_set_max_level(LOG_DEBUG);
97 log_parse_environment();
98 log_open();
99
100 test_conf_files_list(false);
101 test_conf_files_list(true);
102 return 0;
103 }