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