]>
Commit | Line | Data |
---|---|---|
e842803a MB |
1 | /*** |
2 | This file is part of systemd. | |
3 | ||
4 | Copyright 2014 Michael Marineau | |
5 | ||
6 | systemd is free software; you can redistribute it and/or modify it | |
7 | under the terms of the GNU Lesser General Public License as published by | |
8 | the Free Software Foundation; either version 2.1 of the License, or | |
9 | (at your option) any later version. | |
10 | ||
11 | systemd is distributed in the hope that it will be useful, but | |
12 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 | Lesser General Public License for more details. | |
15 | ||
16 | You should have received a copy of the GNU Lesser General Public License | |
17 | along with systemd; If not, see <http://www.gnu.org/licenses/>. | |
18 | ***/ | |
19 | ||
e842803a | 20 | #include <stdarg.h> |
db2df898 | 21 | #include <stdio.h> |
e842803a | 22 | |
db2df898 | 23 | #include "alloc-util.h" |
e842803a | 24 | #include "conf-files.h" |
db2df898 | 25 | #include "fs-util.h" |
e842803a | 26 | #include "macro.h" |
db2df898 MP |
27 | #include "parse-util.h" |
28 | #include "rm-rf.h" | |
29 | #include "string-util.h" | |
e842803a | 30 | #include "strv.h" |
db2df898 | 31 | #include "user-util.h" |
e842803a | 32 | #include "util.h" |
e842803a MB |
33 | |
34 | static void setup_test_dir(char *tmp_dir, const char *files, ...) { | |
35 | va_list ap; | |
36 | ||
37 | assert_se(mkdtemp(tmp_dir) != NULL); | |
38 | ||
39 | va_start(ap, files); | |
40 | while (files != NULL) { | |
41 | _cleanup_free_ char *path = strappend(tmp_dir, files); | |
db2df898 | 42 | assert_se(touch_file(path, true, USEC_INFINITY, UID_INVALID, GID_INVALID, MODE_INVALID) == 0); |
e842803a MB |
43 | files = va_arg(ap, const char *); |
44 | } | |
45 | va_end(ap); | |
46 | } | |
47 | ||
48 | static void test_conf_files_list(bool use_root) { | |
49 | char tmp_dir[] = "/tmp/test-conf-files-XXXXXX"; | |
50 | _cleanup_strv_free_ char **found_files = NULL; | |
51 | const char *root_dir, *search_1, *search_2, *expect_a, *expect_b; | |
52 | ||
53 | setup_test_dir(tmp_dir, | |
54 | "/dir1/a.conf", | |
55 | "/dir2/a.conf", | |
56 | "/dir2/b.conf", | |
57 | NULL); | |
58 | ||
59 | if (use_root) { | |
60 | root_dir = tmp_dir; | |
61 | search_1 = "/dir1"; | |
62 | search_2 = "/dir2"; | |
63 | } else { | |
64 | root_dir = NULL; | |
e735f4d4 MP |
65 | search_1 = strjoina(tmp_dir, "/dir1"); |
66 | search_2 = strjoina(tmp_dir, "/dir2"); | |
e842803a MB |
67 | } |
68 | ||
e735f4d4 MP |
69 | expect_a = strjoina(tmp_dir, "/dir1/a.conf"); |
70 | expect_b = strjoina(tmp_dir, "/dir2/b.conf"); | |
e842803a MB |
71 | |
72 | assert_se(conf_files_list(&found_files, ".conf", root_dir, search_1, search_2, NULL) == 0); | |
73 | strv_print(found_files); | |
74 | ||
75 | assert_se(found_files); | |
76 | assert_se(streq_ptr(found_files[0], expect_a)); | |
77 | assert_se(streq_ptr(found_files[1], expect_b)); | |
78 | assert_se(found_files[2] == NULL); | |
79 | ||
e3bff60a | 80 | assert_se(rm_rf(tmp_dir, REMOVE_ROOT|REMOVE_PHYSICAL) == 0); |
e842803a MB |
81 | } |
82 | ||
83 | int main(int argc, char **argv) { | |
84 | test_conf_files_list(false); | |
85 | test_conf_files_list(true); | |
86 | return 0; | |
87 | } |