]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-conf-files.c
Merge pull request #11827 from keszybz/pkgconfig-variables
[thirdparty/systemd.git] / src / test / test-conf-files.c
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 static void setup_test_dir(char *tmp_dir, const char *files, ...) {
25 va_list ap;
26
27 assert_se(mkdtemp(tmp_dir));
28
29 va_start(ap, files);
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
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";
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;
46
47 log_debug("/* %s(%s) */", __func__, yes_no(use_root));
48
49 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 mask = strjoina(tmp_dir, "/dir1/d.conf");
58 assert_se(symlink("/dev/null", mask) >= 0);
59
60 if (use_root) {
61 root_dir = tmp_dir;
62 search_1 = "/dir1";
63 search_2 = "/dir2";
64 } else {
65 root_dir = NULL;
66 search_1 = strjoina(tmp_dir, "/dir1");
67 search_2 = strjoina(tmp_dir, "/dir2");
68 }
69
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");
73
74 log_debug("/* Check when filtered by suffix */");
75
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);
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));
82 assert_se(!found_files[2]);
83
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);
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));
92 assert_se(!found_files2[3]);
93
94 assert_se(rm_rf(tmp_dir, REMOVE_ROOT|REMOVE_PHYSICAL) == 0);
95 }
96
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
151 int main(int argc, char **argv) {
152 test_setup_logging(LOG_DEBUG);
153
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/");
159
160 return 0;
161 }