1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
|
/* SPDX-License-Identifier: LGPL-2.1-or-later */
/***
Copyright © 2014 Michael Marineau
***/
#include <stdarg.h>
#include <stdio.h>
#include "alloc-util.h"
#include "conf-files.h"
#include "fd-util.h"
#include "fileio.h"
#include "fs-util.h"
#include "macro.h"
#include "mkdir.h"
#include "path-util.h"
#include "rm-rf.h"
#include "string-util.h"
#include "strv.h"
#include "tests.h"
#include "tmpfile-util.h"
TEST(conf_files_list) {
_cleanup_(rm_rf_physical_and_freep) char *t = NULL;
_cleanup_close_ int tfd = -EBADF;
_cleanup_strv_free_ char **result = NULL;
const char *search1, *search2, *search1_a, *search1_b, *search1_c, *search2_aa;
tfd = mkdtemp_open("/tmp/test-conf-files-XXXXXX", O_PATH, &t);
assert(tfd >= 0);
assert_se(mkdirat(tfd, "dir1", 0755) >= 0);
assert_se(mkdirat(tfd, "dir2", 0755) >= 0);
search1 = strjoina(t, "/dir1/");
search2 = strjoina(t, "/dir2/");
FOREACH_STRING(p, "a.conf", "b.conf", "c.foo") {
_cleanup_free_ char *path = NULL;
assert_se(path = path_join(search1, p));
assert_se(write_string_file(path, "foobar", WRITE_STRING_FILE_CREATE) >= 0);
}
assert_se(symlinkat("/dev/null", tfd, "dir1/m.conf") >= 0);
FOREACH_STRING(p, "a.conf", "aa.conf", "m.conf") {
_cleanup_free_ char *path = NULL;
assert_se(path = path_join(search2, p));
assert_se(write_string_file(path, "hogehoge", WRITE_STRING_FILE_CREATE) >= 0);
}
search1_a = strjoina(search1, "a.conf");
search1_b = strjoina(search1, "b.conf");
search1_c = strjoina(search1, "c.foo");
search2_aa = strjoina(search2, "aa.conf");
/* search dir1 without suffix */
assert_se(conf_files_list(&result, NULL, NULL, CONF_FILES_FILTER_MASKED, search1) >= 0);
strv_print(result);
assert_se(strv_equal(result, STRV_MAKE(search1_a, search1_b, search1_c)));
result = strv_free(result);
assert_se(conf_files_list(&result, NULL, t, CONF_FILES_FILTER_MASKED, "/dir1/") >= 0);
strv_print(result);
assert_se(strv_equal(result, STRV_MAKE(search1_a, search1_b, search1_c)));
result = strv_free(result);
assert_se(conf_files_list_at(&result, NULL, AT_FDCWD, CONF_FILES_FILTER_MASKED, search1) >= 0);
strv_print(result);
assert_se(strv_equal(result, STRV_MAKE(search1_a, search1_b, search1_c)));
result = strv_free(result);
assert_se(conf_files_list_at(&result, NULL, tfd, CONF_FILES_FILTER_MASKED, "/dir1/") >= 0);
strv_print(result);
assert_se(strv_equal(result, STRV_MAKE("dir1/a.conf", "dir1/b.conf", "dir1/c.foo")));
result = strv_free(result);
/* search dir1 with suffix */
assert_se(conf_files_list(&result, ".conf", NULL, CONF_FILES_FILTER_MASKED, search1) >= 0);
strv_print(result);
assert_se(strv_equal(result, STRV_MAKE(search1_a, search1_b)));
result = strv_free(result);
assert_se(conf_files_list(&result, ".conf", t, CONF_FILES_FILTER_MASKED, "/dir1/") >= 0);
strv_print(result);
assert_se(strv_equal(result, STRV_MAKE(search1_a, search1_b)));
result = strv_free(result);
assert_se(conf_files_list_at(&result, ".conf", AT_FDCWD, CONF_FILES_FILTER_MASKED, search1) >= 0);
strv_print(result);
assert_se(strv_equal(result, STRV_MAKE(search1_a, search1_b)));
result = strv_free(result);
assert_se(conf_files_list_at(&result, ".conf", tfd, CONF_FILES_FILTER_MASKED, "/dir1/") >= 0);
strv_print(result);
assert_se(strv_equal(result, STRV_MAKE("dir1/a.conf", "dir1/b.conf")));
result = strv_free(result);
/* search two dirs */
assert_se(conf_files_list_strv(&result, ".conf", NULL, CONF_FILES_FILTER_MASKED, STRV_MAKE_CONST(search1, search2)) >= 0);
strv_print(result);
assert_se(strv_equal(result, STRV_MAKE(search1_a, search2_aa, search1_b)));
result = strv_free(result);
assert_se(conf_files_list_strv(&result, ".conf", t, CONF_FILES_FILTER_MASKED, STRV_MAKE_CONST("/dir1/", "/dir2/")) >= 0);
strv_print(result);
assert_se(strv_equal(result, STRV_MAKE(search1_a, search2_aa, search1_b)));
result = strv_free(result);
assert_se(conf_files_list_strv_at(&result, ".conf", AT_FDCWD, CONF_FILES_FILTER_MASKED, STRV_MAKE_CONST(search1, search2)) >= 0);
strv_print(result);
assert_se(strv_equal(result, STRV_MAKE(search1_a, search2_aa, search1_b)));
result = strv_free(result);
assert_se(conf_files_list_strv_at(&result, ".conf", tfd, CONF_FILES_FILTER_MASKED, STRV_MAKE_CONST("/dir1/", "/dir2/")) >= 0);
strv_print(result);
assert_se(strv_equal(result, STRV_MAKE("dir1/a.conf", "dir2/aa.conf", "dir1/b.conf")));
result = strv_free(result);
/* filename only */
assert_se(conf_files_list_strv(&result, ".conf", NULL, CONF_FILES_FILTER_MASKED | CONF_FILES_BASENAME, STRV_MAKE_CONST(search1, search2)) >= 0);
strv_print(result);
assert_se(strv_equal(result, STRV_MAKE("a.conf", "aa.conf", "b.conf")));
result = strv_free(result);
assert_se(conf_files_list_strv(&result, ".conf", t, CONF_FILES_FILTER_MASKED | CONF_FILES_BASENAME, STRV_MAKE_CONST("/dir1/", "/dir2/")) >= 0);
strv_print(result);
assert_se(strv_equal(result, STRV_MAKE("a.conf", "aa.conf", "b.conf")));
result = strv_free(result);
assert_se(conf_files_list_strv_at(&result, ".conf", AT_FDCWD, CONF_FILES_FILTER_MASKED | CONF_FILES_BASENAME, STRV_MAKE_CONST(search1, search2)) >= 0);
strv_print(result);
assert_se(strv_equal(result, STRV_MAKE("a.conf", "aa.conf", "b.conf")));
result = strv_free(result);
assert_se(conf_files_list_strv_at(&result, ".conf", tfd, CONF_FILES_FILTER_MASKED | CONF_FILES_BASENAME, STRV_MAKE_CONST("/dir1/", "/dir2/")) >= 0);
strv_print(result);
assert_se(strv_equal(result, STRV_MAKE("a.conf", "aa.conf", "b.conf")));
}
static void test_conf_files_insert_one(const char *root) {
_cleanup_strv_free_ char **s = NULL;
log_info("/* %s root=%s */", __func__, strempty(root));
char **dirs = STRV_MAKE("/dir1", "/dir2", "/dir3");
_cleanup_free_ const char
*foo1 = path_join(root, "/dir1/foo.conf"),
*foo2 = path_join(root, "/dir2/foo.conf"),
*bar2 = path_join(root, "/dir2/bar.conf"),
*zzz3 = path_join(root, "/dir3/zzz.conf"),
*whatever = path_join(root, "/whatever.conf");
assert_se(conf_files_insert(&s, root, dirs, "/dir2/foo.conf") == 0);
assert_se(strv_equal(s, STRV_MAKE(foo2)));
/* The same file again, https://github.com/systemd/systemd/issues/11124 */
assert_se(conf_files_insert(&s, root, dirs, "/dir2/foo.conf") == 0);
assert_se(strv_equal(s, STRV_MAKE(foo2)));
/* Lower priority → new entry is ignored */
assert_se(conf_files_insert(&s, root, dirs, "/dir3/foo.conf") == 0);
assert_se(strv_equal(s, STRV_MAKE(foo2)));
/* Higher priority → new entry replaces */
assert_se(conf_files_insert(&s, root, dirs, "/dir1/foo.conf") == 0);
assert_se(strv_equal(s, STRV_MAKE(foo1)));
/* Earlier basename */
assert_se(conf_files_insert(&s, root, dirs, "/dir2/bar.conf") == 0);
assert_se(strv_equal(s, STRV_MAKE(bar2, foo1)));
/* Later basename */
assert_se(conf_files_insert(&s, root, dirs, "/dir3/zzz.conf") == 0);
assert_se(strv_equal(s, STRV_MAKE(bar2, foo1, zzz3)));
/* All lower priority → all ignored */
assert_se(conf_files_insert(&s, root, dirs, "/dir3/zzz.conf") == 0);
assert_se(conf_files_insert(&s, root, dirs, "/dir2/bar.conf") == 0);
assert_se(conf_files_insert(&s, root, dirs, "/dir3/bar.conf") == 0);
assert_se(conf_files_insert(&s, root, dirs, "/dir2/foo.conf") == 0);
assert_se(strv_equal(s, STRV_MAKE(bar2, foo1, zzz3)));
/* Two entries that don't match any of the directories, but match basename */
assert_se(conf_files_insert(&s, root, dirs, "/dir4/zzz.conf") == 0);
assert_se(conf_files_insert(&s, root, dirs, "/zzz.conf") == 0);
assert_se(strv_equal(s, STRV_MAKE(bar2, foo1, zzz3)));
/* An entry that doesn't match any of the directories, no match at all */
assert_se(conf_files_insert(&s, root, dirs, "/whatever.conf") == 0);
assert_se(strv_equal(s, STRV_MAKE(bar2, foo1, whatever, zzz3)));
}
TEST(conf_files_insert) {
test_conf_files_insert_one(NULL);
test_conf_files_insert_one("/root");
test_conf_files_insert_one("/root/");
}
DEFINE_TEST_MAIN(LOG_DEBUG);
|