]>
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 MM |
24 | |
25 | #include "conf-files.h" | |
26 | #include "macro.h" | |
07630cea LP |
27 | #include "rm-rf.h" |
28 | #include "string-util.h" | |
09e00c52 MM |
29 | #include "strv.h" |
30 | #include "util.h" | |
09e00c52 MM |
31 | |
32 | static void setup_test_dir(char *tmp_dir, const char *files, ...) { | |
33 | va_list ap; | |
34 | ||
35 | assert_se(mkdtemp(tmp_dir) != NULL); | |
36 | ||
37 | va_start(ap, files); | |
38 | while (files != NULL) { | |
39 | _cleanup_free_ char *path = strappend(tmp_dir, files); | |
46bcf492 | 40 | assert_se(touch_file(path, true, USEC_INFINITY, UID_INVALID, GID_INVALID, 0) == 0); |
09e00c52 MM |
41 | files = va_arg(ap, const char *); |
42 | } | |
43 | va_end(ap); | |
44 | } | |
45 | ||
46 | static void test_conf_files_list(bool use_root) { | |
47 | char tmp_dir[] = "/tmp/test-conf-files-XXXXXX"; | |
48 | _cleanup_strv_free_ char **found_files = NULL; | |
49 | const char *root_dir, *search_1, *search_2, *expect_a, *expect_b; | |
50 | ||
51 | setup_test_dir(tmp_dir, | |
52 | "/dir1/a.conf", | |
53 | "/dir2/a.conf", | |
54 | "/dir2/b.conf", | |
55 | NULL); | |
56 | ||
57 | if (use_root) { | |
58 | root_dir = tmp_dir; | |
59 | search_1 = "/dir1"; | |
60 | search_2 = "/dir2"; | |
61 | } else { | |
62 | root_dir = NULL; | |
63c372cb LP |
63 | search_1 = strjoina(tmp_dir, "/dir1"); |
64 | search_2 = strjoina(tmp_dir, "/dir2"); | |
09e00c52 MM |
65 | } |
66 | ||
63c372cb LP |
67 | expect_a = strjoina(tmp_dir, "/dir1/a.conf"); |
68 | expect_b = strjoina(tmp_dir, "/dir2/b.conf"); | |
09e00c52 MM |
69 | |
70 | assert_se(conf_files_list(&found_files, ".conf", root_dir, search_1, search_2, NULL) == 0); | |
71 | strv_print(found_files); | |
72 | ||
73 | assert_se(found_files); | |
74 | assert_se(streq_ptr(found_files[0], expect_a)); | |
75 | assert_se(streq_ptr(found_files[1], expect_b)); | |
76 | assert_se(found_files[2] == NULL); | |
77 | ||
c6878637 | 78 | assert_se(rm_rf(tmp_dir, REMOVE_ROOT|REMOVE_PHYSICAL) == 0); |
09e00c52 MM |
79 | } |
80 | ||
81 | int main(int argc, char **argv) { | |
82 | test_conf_files_list(false); | |
83 | test_conf_files_list(true); | |
84 | return 0; | |
85 | } |