]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/test/test-conf-files.c
Add SPDX license identifiers to source files under the LGPL
[thirdparty/systemd.git] / src / test / test-conf-files.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
09e00c52
MM
2/***
3 This file is part of systemd.
4
5 Copyright 2014 Michael Marineau
6
7 systemd is free software; you can redistribute it and/or modify it
8 under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 2.1 of the License, or
10 (at your option) any later version.
11
12 systemd is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public License
18 along with systemd; If not, see <http://www.gnu.org/licenses/>.
19***/
20
09e00c52 21#include <stdarg.h>
07630cea 22#include <stdio.h>
09e00c52 23
b5efdb8a 24#include "alloc-util.h"
09e00c52 25#include "conf-files.h"
f4f15635 26#include "fs-util.h"
09e00c52 27#include "macro.h"
ee735086 28#include "parse-util.h"
07630cea
LP
29#include "rm-rf.h"
30#include "string-util.h"
09e00c52 31#include "strv.h"
ee104e11 32#include "user-util.h"
09e00c52 33#include "util.h"
09e00c52
MM
34
35static void setup_test_dir(char *tmp_dir, const char *files, ...) {
36 va_list ap;
37
38 assert_se(mkdtemp(tmp_dir) != NULL);
39
40 va_start(ap, files);
41 while (files != NULL) {
42 _cleanup_free_ char *path = strappend(tmp_dir, files);
ee735086 43 assert_se(touch_file(path, true, USEC_INFINITY, UID_INVALID, GID_INVALID, MODE_INVALID) == 0);
09e00c52
MM
44 files = va_arg(ap, const char *);
45 }
46 va_end(ap);
47}
48
49static void test_conf_files_list(bool use_root) {
50 char tmp_dir[] = "/tmp/test-conf-files-XXXXXX";
4e281f68
ZJS
51 _cleanup_strv_free_ char **found_files = NULL, **found_files2 = NULL;
52 const char *root_dir, *search_1, *search_2, *expect_a, *expect_b, *expect_c;
53
54 log_debug("/* %s */", __func__);
09e00c52
MM
55
56 setup_test_dir(tmp_dir,
57 "/dir1/a.conf",
58 "/dir2/a.conf",
59 "/dir2/b.conf",
4e281f68 60 "/dir2/c.foo",
09e00c52
MM
61 NULL);
62
63 if (use_root) {
64 root_dir = tmp_dir;
65 search_1 = "/dir1";
66 search_2 = "/dir2";
67 } else {
68 root_dir = NULL;
63c372cb
LP
69 search_1 = strjoina(tmp_dir, "/dir1");
70 search_2 = strjoina(tmp_dir, "/dir2");
09e00c52
MM
71 }
72
63c372cb
LP
73 expect_a = strjoina(tmp_dir, "/dir1/a.conf");
74 expect_b = strjoina(tmp_dir, "/dir2/b.conf");
4e281f68
ZJS
75 expect_c = strjoina(tmp_dir, "/dir2/c.foo");
76
77 log_debug("/* Check when filtered by suffix */");
09e00c52 78
b5084605 79 assert_se(conf_files_list(&found_files, ".conf", root_dir, 0, search_1, search_2, NULL) == 0);
09e00c52
MM
80 strv_print(found_files);
81
82 assert_se(found_files);
83 assert_se(streq_ptr(found_files[0], expect_a));
84 assert_se(streq_ptr(found_files[1], expect_b));
85 assert_se(found_files[2] == NULL);
86
4e281f68 87 log_debug("/* Check when unfiltered */");
b5084605 88 assert_se(conf_files_list(&found_files2, NULL, root_dir, 0, search_1, search_2, NULL) == 0);
4e281f68
ZJS
89 strv_print(found_files2);
90
91 assert_se(found_files2);
92 assert_se(streq_ptr(found_files2[0], expect_a));
93 assert_se(streq_ptr(found_files2[1], expect_b));
94 assert_se(streq_ptr(found_files2[2], expect_c));
95 assert_se(found_files2[3] == NULL);
96
c6878637 97 assert_se(rm_rf(tmp_dir, REMOVE_ROOT|REMOVE_PHYSICAL) == 0);
09e00c52
MM
98}
99
100int main(int argc, char **argv) {
4e281f68
ZJS
101 log_set_max_level(LOG_DEBUG);
102 log_parse_environment();
103 log_open();
104
09e00c52
MM
105 test_conf_files_list(false);
106 test_conf_files_list(true);
107 return 0;
108}