]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
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" |
07630cea LP |
16 | #include "rm-rf.h" |
17 | #include "string-util.h" | |
09e00c52 | 18 | #include "strv.h" |
6d7c4033 | 19 | #include "tests.h" |
ee104e11 | 20 | #include "user-util.h" |
09e00c52 | 21 | #include "util.h" |
09e00c52 MM |
22 | |
23 | static void setup_test_dir(char *tmp_dir, const char *files, ...) { | |
24 | va_list ap; | |
25 | ||
3e36211b | 26 | assert_se(mkdtemp(tmp_dir)); |
09e00c52 MM |
27 | |
28 | va_start(ap, files); | |
3e36211b LP |
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 | ||
09e00c52 MM |
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"; | |
4e281f68 | 43 | _cleanup_strv_free_ char **found_files = NULL, **found_files2 = NULL; |
3e36211b | 44 | const char *root_dir, *search_1, *search_2, *expect_a, *expect_b, *expect_c, *mask; |
4e281f68 ZJS |
45 | |
46 | log_debug("/* %s */", __func__); | |
09e00c52 MM |
47 | |
48 | setup_test_dir(tmp_dir, | |
49 | "/dir1/a.conf", | |
50 | "/dir2/a.conf", | |
51 | "/dir2/b.conf", | |
4e281f68 | 52 | "/dir2/c.foo", |
3e36211b | 53 | "/dir2/d.conf", |
09e00c52 MM |
54 | NULL); |
55 | ||
3e36211b LP |
56 | mask = strjoina(tmp_dir, "/dir1/d.conf"); |
57 | assert_se(symlink("/dev/null", mask) >= 0); | |
58 | ||
09e00c52 MM |
59 | if (use_root) { |
60 | root_dir = tmp_dir; | |
61 | search_1 = "/dir1"; | |
62 | search_2 = "/dir2"; | |
63 | } else { | |
64 | root_dir = NULL; | |
63c372cb LP |
65 | search_1 = strjoina(tmp_dir, "/dir1"); |
66 | search_2 = strjoina(tmp_dir, "/dir2"); | |
09e00c52 MM |
67 | } |
68 | ||
63c372cb LP |
69 | expect_a = strjoina(tmp_dir, "/dir1/a.conf"); |
70 | expect_b = strjoina(tmp_dir, "/dir2/b.conf"); | |
4e281f68 ZJS |
71 | expect_c = strjoina(tmp_dir, "/dir2/c.foo"); |
72 | ||
73 | log_debug("/* Check when filtered by suffix */"); | |
09e00c52 | 74 | |
3e36211b | 75 | assert_se(conf_files_list(&found_files, ".conf", root_dir, CONF_FILES_FILTER_MASKED, search_1, search_2, NULL) == 0); |
09e00c52 MM |
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)); | |
3e36211b | 81 | assert_se(!found_files[2]); |
09e00c52 | 82 | |
4e281f68 | 83 | log_debug("/* Check when unfiltered */"); |
3e36211b | 84 | assert_se(conf_files_list(&found_files2, NULL, root_dir, CONF_FILES_FILTER_MASKED, search_1, search_2, NULL) == 0); |
4e281f68 ZJS |
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)); | |
3e36211b | 91 | assert_se(!found_files2[3]); |
4e281f68 | 92 | |
c6878637 | 93 | assert_se(rm_rf(tmp_dir, REMOVE_ROOT|REMOVE_PHYSICAL) == 0); |
09e00c52 MM |
94 | } |
95 | ||
96 | int main(int argc, char **argv) { | |
6d7c4033 | 97 | test_setup_logging(LOG_DEBUG); |
4e281f68 | 98 | |
09e00c52 MM |
99 | test_conf_files_list(false); |
100 | test_conf_files_list(true); | |
101 | return 0; | |
102 | } |