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