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