chiark / gitweb /
tree-wide: drop license boilerplate
[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 "fs-util.h"
14 #include "macro.h"
15 #include "parse-util.h"
16 #include "rm-rf.h"
17 #include "string-util.h"
18 #include "strv.h"
19 #include "user-util.h"
20 #include "util.h"
21
22 static void setup_test_dir(char *tmp_dir, const char *files, ...) {
23         va_list ap;
24
25         assert_se(mkdtemp(tmp_dir) != NULL);
26
27         va_start(ap, files);
28         while (files != NULL) {
29                 _cleanup_free_ char *path = strappend(tmp_dir, files);
30                 assert_se(touch_file(path, true, USEC_INFINITY, UID_INVALID, GID_INVALID, MODE_INVALID) == 0);
31                 files = va_arg(ap, const char *);
32         }
33         va_end(ap);
34 }
35
36 static void test_conf_files_list(bool use_root) {
37         char tmp_dir[] = "/tmp/test-conf-files-XXXXXX";
38         _cleanup_strv_free_ char **found_files = NULL, **found_files2 = NULL;
39         const char *root_dir, *search_1, *search_2, *expect_a, *expect_b, *expect_c;
40
41         log_debug("/* %s */", __func__);
42
43         setup_test_dir(tmp_dir,
44                        "/dir1/a.conf",
45                        "/dir2/a.conf",
46                        "/dir2/b.conf",
47                        "/dir2/c.foo",
48                        NULL);
49
50         if (use_root) {
51                 root_dir = tmp_dir;
52                 search_1 = "/dir1";
53                 search_2 = "/dir2";
54         } else {
55                 root_dir = NULL;
56                 search_1 = strjoina(tmp_dir, "/dir1");
57                 search_2 = strjoina(tmp_dir, "/dir2");
58         }
59
60         expect_a = strjoina(tmp_dir, "/dir1/a.conf");
61         expect_b = strjoina(tmp_dir, "/dir2/b.conf");
62         expect_c = strjoina(tmp_dir, "/dir2/c.foo");
63
64         log_debug("/* Check when filtered by suffix */");
65
66         assert_se(conf_files_list(&found_files, ".conf", root_dir, 0, search_1, search_2, NULL) == 0);
67         strv_print(found_files);
68
69         assert_se(found_files);
70         assert_se(streq_ptr(found_files[0], expect_a));
71         assert_se(streq_ptr(found_files[1], expect_b));
72         assert_se(found_files[2] == NULL);
73
74         log_debug("/* Check when unfiltered */");
75         assert_se(conf_files_list(&found_files2, NULL, root_dir, 0, search_1, search_2, NULL) == 0);
76         strv_print(found_files2);
77
78         assert_se(found_files2);
79         assert_se(streq_ptr(found_files2[0], expect_a));
80         assert_se(streq_ptr(found_files2[1], expect_b));
81         assert_se(streq_ptr(found_files2[2], expect_c));
82         assert_se(found_files2[3] == NULL);
83
84         assert_se(rm_rf(tmp_dir, REMOVE_ROOT|REMOVE_PHYSICAL) == 0);
85 }
86
87 int main(int argc, char **argv) {
88         log_set_max_level(LOG_DEBUG);
89         log_parse_environment();
90         log_open();
91
92         test_conf_files_list(false);
93         test_conf_files_list(true);
94         return 0;
95 }