Line data Source code
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 "path-util.h"
17 : #include "rm-rf.h"
18 : #include "string-util.h"
19 : #include "strv.h"
20 : #include "tests.h"
21 : #include "user-util.h"
22 : #include "util.h"
23 :
24 2 : static void setup_test_dir(char *tmp_dir, const char *files, ...) {
25 : va_list ap;
26 :
27 2 : assert_se(mkdtemp(tmp_dir));
28 :
29 2 : va_start(ap, files);
30 12 : while (files) {
31 10 : _cleanup_free_ char *path;
32 :
33 10 : assert_se(path = path_join(tmp_dir, files));
34 10 : (void) mkdir_parents(path, 0755);
35 10 : assert_se(write_string_file(path, "foobar", WRITE_STRING_FILE_CREATE) >= 0);
36 :
37 10 : files = va_arg(ap, const char *);
38 : }
39 2 : va_end(ap);
40 2 : }
41 :
42 2 : static void test_conf_files_list(bool use_root) {
43 2 : char tmp_dir[] = "/tmp/test-conf-files-XXXXXX";
44 2 : _cleanup_strv_free_ char **found_files = NULL, **found_files2 = NULL;
45 : const char *root_dir, *search_1, *search_2, *expect_a, *expect_b, *expect_c, *mask;
46 :
47 2 : log_debug("/* %s(%s) */", __func__, yes_no(use_root));
48 :
49 2 : setup_test_dir(tmp_dir,
50 : "/dir1/a.conf",
51 : "/dir2/a.conf",
52 : "/dir2/b.conf",
53 : "/dir2/c.foo",
54 : "/dir2/d.conf",
55 : NULL);
56 :
57 10 : mask = strjoina(tmp_dir, "/dir1/d.conf");
58 2 : assert_se(symlink("/dev/null", mask) >= 0);
59 :
60 2 : if (use_root) {
61 1 : root_dir = tmp_dir;
62 1 : search_1 = "/dir1";
63 1 : search_2 = "/dir2";
64 : } else {
65 1 : root_dir = NULL;
66 5 : search_1 = strjoina(tmp_dir, "/dir1");
67 5 : search_2 = strjoina(tmp_dir, "/dir2");
68 : }
69 :
70 10 : expect_a = strjoina(tmp_dir, "/dir1/a.conf");
71 10 : expect_b = strjoina(tmp_dir, "/dir2/b.conf");
72 10 : expect_c = strjoina(tmp_dir, "/dir2/c.foo");
73 :
74 2 : log_debug("/* Check when filtered by suffix */");
75 :
76 2 : assert_se(conf_files_list(&found_files, ".conf", root_dir, CONF_FILES_FILTER_MASKED, search_1, search_2, NULL) == 0);
77 2 : strv_print(found_files);
78 :
79 2 : assert_se(found_files);
80 2 : assert_se(streq_ptr(found_files[0], expect_a));
81 2 : assert_se(streq_ptr(found_files[1], expect_b));
82 2 : assert_se(!found_files[2]);
83 :
84 2 : log_debug("/* Check when unfiltered */");
85 2 : assert_se(conf_files_list(&found_files2, NULL, root_dir, CONF_FILES_FILTER_MASKED, search_1, search_2, NULL) == 0);
86 2 : strv_print(found_files2);
87 :
88 2 : assert_se(found_files2);
89 2 : assert_se(streq_ptr(found_files2[0], expect_a));
90 2 : assert_se(streq_ptr(found_files2[1], expect_b));
91 2 : assert_se(streq_ptr(found_files2[2], expect_c));
92 2 : assert_se(!found_files2[3]);
93 :
94 2 : assert_se(rm_rf(tmp_dir, REMOVE_ROOT|REMOVE_PHYSICAL) == 0);
95 2 : }
96 :
97 3 : static void test_conf_files_insert(const char *root) {
98 3 : _cleanup_strv_free_ char **s = NULL;
99 :
100 3 : log_info("/* %s root=%s */", __func__, strempty(root));
101 :
102 3 : char **dirs = STRV_MAKE("/dir1", "/dir2", "/dir3");
103 :
104 : _cleanup_free_ const char
105 6 : *foo1 = path_join(root, "/dir1/foo.conf"),
106 6 : *foo2 = path_join(root, "/dir2/foo.conf"),
107 6 : *bar2 = path_join(root, "/dir2/bar.conf"),
108 6 : *zzz3 = path_join(root, "/dir3/zzz.conf"),
109 6 : *whatever = path_join(root, "/whatever.conf");
110 :
111 3 : assert_se(conf_files_insert(&s, root, dirs, "/dir2/foo.conf") == 0);
112 3 : assert_se(strv_equal(s, STRV_MAKE(foo2)));
113 :
114 : /* The same file again, https://github.com/systemd/systemd/issues/11124 */
115 3 : assert_se(conf_files_insert(&s, root, dirs, "/dir2/foo.conf") == 0);
116 3 : assert_se(strv_equal(s, STRV_MAKE(foo2)));
117 :
118 : /* Lower priority → new entry is ignored */
119 3 : assert_se(conf_files_insert(&s, root, dirs, "/dir3/foo.conf") == 0);
120 3 : assert_se(strv_equal(s, STRV_MAKE(foo2)));
121 :
122 : /* Higher priority → new entry replaces */
123 3 : assert_se(conf_files_insert(&s, root, dirs, "/dir1/foo.conf") == 0);
124 3 : assert_se(strv_equal(s, STRV_MAKE(foo1)));
125 :
126 : /* Earlier basename */
127 3 : assert_se(conf_files_insert(&s, root, dirs, "/dir2/bar.conf") == 0);
128 3 : assert_se(strv_equal(s, STRV_MAKE(bar2, foo1)));
129 :
130 : /* Later basename */
131 3 : assert_se(conf_files_insert(&s, root, dirs, "/dir3/zzz.conf") == 0);
132 3 : assert_se(strv_equal(s, STRV_MAKE(bar2, foo1, zzz3)));
133 :
134 : /* All lower priority → all ignored */
135 3 : assert_se(conf_files_insert(&s, root, dirs, "/dir3/zzz.conf") == 0);
136 3 : assert_se(conf_files_insert(&s, root, dirs, "/dir2/bar.conf") == 0);
137 3 : assert_se(conf_files_insert(&s, root, dirs, "/dir3/bar.conf") == 0);
138 3 : assert_se(conf_files_insert(&s, root, dirs, "/dir2/foo.conf") == 0);
139 3 : assert_se(strv_equal(s, STRV_MAKE(bar2, foo1, zzz3)));
140 :
141 : /* Two entries that don't match any of the directories, but match basename */
142 3 : assert_se(conf_files_insert(&s, root, dirs, "/dir4/zzz.conf") == 0);
143 3 : assert_se(conf_files_insert(&s, root, dirs, "/zzz.conf") == 0);
144 3 : assert_se(strv_equal(s, STRV_MAKE(bar2, foo1, zzz3)));
145 :
146 : /* An entry that doesn't match any of the directories, no match at all */
147 3 : assert_se(conf_files_insert(&s, root, dirs, "/whatever.conf") == 0);
148 3 : assert_se(strv_equal(s, STRV_MAKE(bar2, foo1, whatever, zzz3)));
149 3 : }
150 :
151 1 : int main(int argc, char **argv) {
152 1 : test_setup_logging(LOG_DEBUG);
153 :
154 1 : test_conf_files_list(false);
155 1 : test_conf_files_list(true);
156 1 : test_conf_files_insert(NULL);
157 1 : test_conf_files_insert("/root");
158 1 : test_conf_files_insert("/root/");
159 :
160 1 : return 0;
161 : }
|