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