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