1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2014 Michael Marineau
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.
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.
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/>.
24 #include "alloc-util.h"
25 #include "conf-files.h"
28 #include "parse-util.h"
30 #include "string-util.h"
32 #include "user-util.h"
35 static void setup_test_dir(char *tmp_dir, const char *files, ...) {
38 assert_se(mkdtemp(tmp_dir) != NULL);
41 while (files != NULL) {
42 _cleanup_free_ char *path = strappend(tmp_dir, files);
43 assert_se(touch_file(path, true, USEC_INFINITY, UID_INVALID, GID_INVALID, MODE_INVALID) == 0);
44 files = va_arg(ap, const char *);
49 static void test_conf_files_list(bool use_root) {
50 char tmp_dir[] = "/tmp/test-conf-files-XXXXXX";
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;
54 log_debug("/* %s */", __func__);
56 setup_test_dir(tmp_dir,
69 search_1 = strjoina(tmp_dir, "/dir1");
70 search_2 = strjoina(tmp_dir, "/dir2");
73 expect_a = strjoina(tmp_dir, "/dir1/a.conf");
74 expect_b = strjoina(tmp_dir, "/dir2/b.conf");
75 expect_c = strjoina(tmp_dir, "/dir2/c.foo");
77 log_debug("/* Check when filtered by suffix */");
79 assert_se(conf_files_list(&found_files, ".conf", root_dir, 0, search_1, search_2, NULL) == 0);
80 strv_print(found_files);
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);
87 log_debug("/* Check when unfiltered */");
88 assert_se(conf_files_list(&found_files2, NULL, root_dir, 0, search_1, search_2, NULL) == 0);
89 strv_print(found_files2);
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);
97 assert_se(rm_rf(tmp_dir, REMOVE_ROOT|REMOVE_PHYSICAL) == 0);
100 int main(int argc, char **argv) {
101 log_set_max_level(LOG_DEBUG);
102 log_parse_environment();
105 test_conf_files_list(false);
106 test_conf_files_list(true);