]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-conf-files.c
Merge pull request #7411 from joergsteffens/tapechanger
[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 "rm-rf.h"
17 #include "string-util.h"
18 #include "strv.h"
19 #include "tests.h"
20 #include "user-util.h"
21 #include "util.h"
22
23 static void setup_test_dir(char *tmp_dir, const char *files, ...) {
24 va_list ap;
25
26 assert_se(mkdtemp(tmp_dir));
27
28 va_start(ap, files);
29 while (files) {
30 _cleanup_free_ char *path;
31
32 assert_se(path = strappend(tmp_dir, files));
33 (void) mkdir_parents(path, 0755);
34 assert_se(write_string_file(path, "foobar", WRITE_STRING_FILE_CREATE) >= 0);
35
36 files = va_arg(ap, const char *);
37 }
38 va_end(ap);
39 }
40
41 static void test_conf_files_list(bool use_root) {
42 char tmp_dir[] = "/tmp/test-conf-files-XXXXXX";
43 _cleanup_strv_free_ char **found_files = NULL, **found_files2 = NULL;
44 const char *root_dir, *search_1, *search_2, *expect_a, *expect_b, *expect_c, *mask;
45
46 log_debug("/* %s */", __func__);
47
48 setup_test_dir(tmp_dir,
49 "/dir1/a.conf",
50 "/dir2/a.conf",
51 "/dir2/b.conf",
52 "/dir2/c.foo",
53 "/dir2/d.conf",
54 NULL);
55
56 mask = strjoina(tmp_dir, "/dir1/d.conf");
57 assert_se(symlink("/dev/null", mask) >= 0);
58
59 if (use_root) {
60 root_dir = tmp_dir;
61 search_1 = "/dir1";
62 search_2 = "/dir2";
63 } else {
64 root_dir = NULL;
65 search_1 = strjoina(tmp_dir, "/dir1");
66 search_2 = strjoina(tmp_dir, "/dir2");
67 }
68
69 expect_a = strjoina(tmp_dir, "/dir1/a.conf");
70 expect_b = strjoina(tmp_dir, "/dir2/b.conf");
71 expect_c = strjoina(tmp_dir, "/dir2/c.foo");
72
73 log_debug("/* Check when filtered by suffix */");
74
75 assert_se(conf_files_list(&found_files, ".conf", root_dir, CONF_FILES_FILTER_MASKED, search_1, search_2, NULL) == 0);
76 strv_print(found_files);
77
78 assert_se(found_files);
79 assert_se(streq_ptr(found_files[0], expect_a));
80 assert_se(streq_ptr(found_files[1], expect_b));
81 assert_se(!found_files[2]);
82
83 log_debug("/* Check when unfiltered */");
84 assert_se(conf_files_list(&found_files2, NULL, root_dir, CONF_FILES_FILTER_MASKED, search_1, search_2, NULL) == 0);
85 strv_print(found_files2);
86
87 assert_se(found_files2);
88 assert_se(streq_ptr(found_files2[0], expect_a));
89 assert_se(streq_ptr(found_files2[1], expect_b));
90 assert_se(streq_ptr(found_files2[2], expect_c));
91 assert_se(!found_files2[3]);
92
93 assert_se(rm_rf(tmp_dir, REMOVE_ROOT|REMOVE_PHYSICAL) == 0);
94 }
95
96 int main(int argc, char **argv) {
97 test_setup_logging(LOG_DEBUG);
98
99 test_conf_files_list(false);
100 test_conf_files_list(true);
101 return 0;
102 }