2 This file is part of systemd.
4 Copyright 2014 Michael Marineau
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
23 #include "alloc-util.h"
24 #include "conf-files.h"
27 #include "parse-util.h"
29 #include "string-util.h"
31 #include "user-util.h"
34 static void setup_test_dir(char *tmp_dir, const char *files, ...) {
37 assert_se(mkdtemp(tmp_dir) != NULL);
40 while (files != NULL) {
41 _cleanup_free_ char *path = strappend(tmp_dir, files);
42 assert_se(touch_file(path, true, USEC_INFINITY, UID_INVALID, GID_INVALID, MODE_INVALID) == 0);
43 files = va_arg(ap, const char *);
48 static void test_conf_files_list(bool use_root) {
49 char tmp_dir[] = "/tmp/test-conf-files-XXXXXX";
50 _cleanup_strv_free_ char **found_files = NULL, **found_files2 = NULL;
51 const char *root_dir, *search_1, *search_2, *expect_a, *expect_b, *expect_c;
53 log_debug("/* %s */", __func__);
55 setup_test_dir(tmp_dir,
68 search_1 = strjoina(tmp_dir, "/dir1");
69 search_2 = strjoina(tmp_dir, "/dir2");
72 expect_a = strjoina(tmp_dir, "/dir1/a.conf");
73 expect_b = strjoina(tmp_dir, "/dir2/b.conf");
74 expect_c = strjoina(tmp_dir, "/dir2/c.foo");
76 log_debug("/* Check when filtered by suffix */");
78 assert_se(conf_files_list(&found_files, ".conf", root_dir, search_1, search_2, NULL) == 0);
79 strv_print(found_files);
81 assert_se(found_files);
82 assert_se(streq_ptr(found_files[0], expect_a));
83 assert_se(streq_ptr(found_files[1], expect_b));
84 assert_se(found_files[2] == NULL);
86 log_debug("/* Check when unfiltered */");
87 assert_se(conf_files_list(&found_files2, NULL, root_dir, search_1, search_2, NULL) == 0);
88 strv_print(found_files2);
90 assert_se(found_files2);
91 assert_se(streq_ptr(found_files2[0], expect_a));
92 assert_se(streq_ptr(found_files2[1], expect_b));
93 assert_se(streq_ptr(found_files2[2], expect_c));
94 assert_se(found_files2[3] == NULL);
96 assert_se(rm_rf(tmp_dir, REMOVE_ROOT|REMOVE_PHYSICAL) == 0);
99 int main(int argc, char **argv) {
100 log_set_max_level(LOG_DEBUG);
101 log_parse_environment();
104 test_conf_files_list(false);
105 test_conf_files_list(true);