]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
09e00c52 MM |
2 | /*** |
3 | This file is part of systemd. | |
4 | ||
5 | Copyright 2014 Michael Marineau | |
09e00c52 MM |
6 | ***/ |
7 | ||
09e00c52 | 8 | #include <stdarg.h> |
07630cea | 9 | #include <stdio.h> |
09e00c52 | 10 | |
b5efdb8a | 11 | #include "alloc-util.h" |
09e00c52 | 12 | #include "conf-files.h" |
f4f15635 | 13 | #include "fs-util.h" |
09e00c52 | 14 | #include "macro.h" |
ee735086 | 15 | #include "parse-util.h" |
07630cea LP |
16 | #include "rm-rf.h" |
17 | #include "string-util.h" | |
09e00c52 | 18 | #include "strv.h" |
ee104e11 | 19 | #include "user-util.h" |
09e00c52 | 20 | #include "util.h" |
09e00c52 MM |
21 | |
22 | static void setup_test_dir(char *tmp_dir, const char *files, ...) { | |
23 | va_list ap; | |
24 | ||
25 | assert_se(mkdtemp(tmp_dir) != NULL); | |
26 | ||
27 | va_start(ap, files); | |
28 | while (files != NULL) { | |
29 | _cleanup_free_ char *path = strappend(tmp_dir, files); | |
ee735086 | 30 | assert_se(touch_file(path, true, USEC_INFINITY, UID_INVALID, GID_INVALID, MODE_INVALID) == 0); |
09e00c52 MM |
31 | files = va_arg(ap, const char *); |
32 | } | |
33 | va_end(ap); | |
34 | } | |
35 | ||
36 | static void test_conf_files_list(bool use_root) { | |
37 | char tmp_dir[] = "/tmp/test-conf-files-XXXXXX"; | |
4e281f68 ZJS |
38 | _cleanup_strv_free_ char **found_files = NULL, **found_files2 = NULL; |
39 | const char *root_dir, *search_1, *search_2, *expect_a, *expect_b, *expect_c; | |
40 | ||
41 | log_debug("/* %s */", __func__); | |
09e00c52 MM |
42 | |
43 | setup_test_dir(tmp_dir, | |
44 | "/dir1/a.conf", | |
45 | "/dir2/a.conf", | |
46 | "/dir2/b.conf", | |
4e281f68 | 47 | "/dir2/c.foo", |
09e00c52 MM |
48 | NULL); |
49 | ||
50 | if (use_root) { | |
51 | root_dir = tmp_dir; | |
52 | search_1 = "/dir1"; | |
53 | search_2 = "/dir2"; | |
54 | } else { | |
55 | root_dir = NULL; | |
63c372cb LP |
56 | search_1 = strjoina(tmp_dir, "/dir1"); |
57 | search_2 = strjoina(tmp_dir, "/dir2"); | |
09e00c52 MM |
58 | } |
59 | ||
63c372cb LP |
60 | expect_a = strjoina(tmp_dir, "/dir1/a.conf"); |
61 | expect_b = strjoina(tmp_dir, "/dir2/b.conf"); | |
4e281f68 ZJS |
62 | expect_c = strjoina(tmp_dir, "/dir2/c.foo"); |
63 | ||
64 | log_debug("/* Check when filtered by suffix */"); | |
09e00c52 | 65 | |
b5084605 | 66 | assert_se(conf_files_list(&found_files, ".conf", root_dir, 0, search_1, search_2, NULL) == 0); |
09e00c52 MM |
67 | strv_print(found_files); |
68 | ||
69 | assert_se(found_files); | |
70 | assert_se(streq_ptr(found_files[0], expect_a)); | |
71 | assert_se(streq_ptr(found_files[1], expect_b)); | |
72 | assert_se(found_files[2] == NULL); | |
73 | ||
4e281f68 | 74 | log_debug("/* Check when unfiltered */"); |
b5084605 | 75 | assert_se(conf_files_list(&found_files2, NULL, root_dir, 0, search_1, search_2, NULL) == 0); |
4e281f68 ZJS |
76 | strv_print(found_files2); |
77 | ||
78 | assert_se(found_files2); | |
79 | assert_se(streq_ptr(found_files2[0], expect_a)); | |
80 | assert_se(streq_ptr(found_files2[1], expect_b)); | |
81 | assert_se(streq_ptr(found_files2[2], expect_c)); | |
82 | assert_se(found_files2[3] == NULL); | |
83 | ||
c6878637 | 84 | assert_se(rm_rf(tmp_dir, REMOVE_ROOT|REMOVE_PHYSICAL) == 0); |
09e00c52 MM |
85 | } |
86 | ||
87 | int main(int argc, char **argv) { | |
4e281f68 ZJS |
88 | log_set_max_level(LOG_DEBUG); |
89 | log_parse_environment(); | |
90 | log_open(); | |
91 | ||
09e00c52 MM |
92 | test_conf_files_list(false); |
93 | test_conf_files_list(true); | |
94 | return 0; | |
95 | } |