]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-conf-files.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 Copyright © 2014 Michael Marineau
9 #include "alloc-util.h"
10 #include "conf-files.h"
15 #include "parse-util.h"
16 #include "path-util.h"
18 #include "string-util.h"
21 #include "user-util.h"
24 static void setup_test_dir(char *tmp_dir
, const char *files
, ...) {
27 assert_se(mkdtemp(tmp_dir
));
31 _cleanup_free_
char *path
;
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);
37 files
= va_arg(ap
, const char *);
42 static void test_conf_files_list(bool use_root
) {
43 char tmp_dir
[] = "/tmp/test-conf-files-XXXXXX";
44 _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
;
47 log_debug("/* %s(%s) */", __func__
, yes_no(use_root
));
49 setup_test_dir(tmp_dir
,
57 mask
= strjoina(tmp_dir
, "/dir1/d.conf");
58 assert_se(symlink("/dev/null", mask
) >= 0);
66 search_1
= strjoina(tmp_dir
, "/dir1");
67 search_2
= strjoina(tmp_dir
, "/dir2");
70 expect_a
= strjoina(tmp_dir
, "/dir1/a.conf");
71 expect_b
= strjoina(tmp_dir
, "/dir2/b.conf");
72 expect_c
= strjoina(tmp_dir
, "/dir2/c.foo");
74 log_debug("/* Check when filtered by suffix */");
76 assert_se(conf_files_list(&found_files
, ".conf", root_dir
, CONF_FILES_FILTER_MASKED
, search_1
, search_2
, NULL
) == 0);
77 strv_print(found_files
);
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
));
82 assert_se(!found_files
[2]);
84 log_debug("/* Check when unfiltered */");
85 assert_se(conf_files_list(&found_files2
, NULL
, root_dir
, CONF_FILES_FILTER_MASKED
, search_1
, search_2
, NULL
) == 0);
86 strv_print(found_files2
);
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
));
92 assert_se(!found_files2
[3]);
94 assert_se(rm_rf(tmp_dir
, REMOVE_ROOT
|REMOVE_PHYSICAL
) == 0);
97 static void test_conf_files_insert(const char *root
) {
98 _cleanup_strv_free_
char **s
= NULL
;
100 log_info("/* %s root=%s */", __func__
, strempty(root
));
102 char **dirs
= STRV_MAKE("/dir1", "/dir2", "/dir3");
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");
111 assert_se(conf_files_insert(&s
, root
, dirs
, "/dir2/foo.conf") == 0);
112 assert_se(strv_equal(s
, STRV_MAKE(foo2
)));
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
)));
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
)));
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
)));
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
)));
131 assert_se(conf_files_insert(&s
, root
, dirs
, "/dir3/zzz.conf") == 0);
132 assert_se(strv_equal(s
, STRV_MAKE(bar2
, foo1
, zzz3
)));
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
)));
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
)));
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
)));
151 int main(int argc
, char **argv
) {
152 test_setup_logging(LOG_DEBUG
);
154 test_conf_files_list(false);
155 test_conf_files_list(true);
156 test_conf_files_insert(NULL
);
157 test_conf_files_insert("/root");
158 test_conf_files_insert("/root/");