]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-conf-files.c
Merge pull request #6428 from boucman/device_reload
[thirdparty/systemd.git] / src / test / test-conf-files.c
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
20 #include <stdarg.h>
21 #include <stdio.h>
22
23 #include "alloc-util.h"
24 #include "conf-files.h"
25 #include "fs-util.h"
26 #include "macro.h"
27 #include "parse-util.h"
28 #include "rm-rf.h"
29 #include "string-util.h"
30 #include "strv.h"
31 #include "user-util.h"
32 #include "util.h"
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);
42 assert_se(touch_file(path, true, USEC_INFINITY, UID_INVALID, GID_INVALID, MODE_INVALID) == 0);
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, **found_files2 = NULL;
51 const char *root_dir, *search_1, *search_2, *expect_a, *expect_b, *expect_c;
52
53 log_debug("/* %s */", __func__);
54
55 setup_test_dir(tmp_dir,
56 "/dir1/a.conf",
57 "/dir2/a.conf",
58 "/dir2/b.conf",
59 "/dir2/c.foo",
60 NULL);
61
62 if (use_root) {
63 root_dir = tmp_dir;
64 search_1 = "/dir1";
65 search_2 = "/dir2";
66 } else {
67 root_dir = NULL;
68 search_1 = strjoina(tmp_dir, "/dir1");
69 search_2 = strjoina(tmp_dir, "/dir2");
70 }
71
72 expect_a = strjoina(tmp_dir, "/dir1/a.conf");
73 expect_b = strjoina(tmp_dir, "/dir2/b.conf");
74 expect_c = strjoina(tmp_dir, "/dir2/c.foo");
75
76 log_debug("/* Check when filtered by suffix */");
77
78 assert_se(conf_files_list(&found_files, ".conf", root_dir, 0, search_1, search_2, NULL) == 0);
79 strv_print(found_files);
80
81 assert_se(found_files);
82 assert_se(streq_ptr(found_files[0], expect_a));
83 assert_se(streq_ptr(found_files[1], expect_b));
84 assert_se(found_files[2] == NULL);
85
86 log_debug("/* Check when unfiltered */");
87 assert_se(conf_files_list(&found_files2, NULL, root_dir, 0, search_1, search_2, NULL) == 0);
88 strv_print(found_files2);
89
90 assert_se(found_files2);
91 assert_se(streq_ptr(found_files2[0], expect_a));
92 assert_se(streq_ptr(found_files2[1], expect_b));
93 assert_se(streq_ptr(found_files2[2], expect_c));
94 assert_se(found_files2[3] == NULL);
95
96 assert_se(rm_rf(tmp_dir, REMOVE_ROOT|REMOVE_PHYSICAL) == 0);
97 }
98
99 int main(int argc, char **argv) {
100 log_set_max_level(LOG_DEBUG);
101 log_parse_environment();
102 log_open();
103
104 test_conf_files_list(false);
105 test_conf_files_list(true);
106 return 0;
107 }