1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2014 Michael Marineau
11 #include "alloc-util.h"
12 #include "conf-files.h"
15 #include "parse-util.h"
17 #include "string-util.h"
19 #include "user-util.h"
22 static void setup_test_dir(char *tmp_dir, const char *files, ...) {
25 assert_se(mkdtemp(tmp_dir) != NULL);
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 *);
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;
41 log_debug("/* %s */", __func__);
43 setup_test_dir(tmp_dir,
56 search_1 = strjoina(tmp_dir, "/dir1");
57 search_2 = strjoina(tmp_dir, "/dir2");
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");
64 log_debug("/* Check when filtered by suffix */");
66 assert_se(conf_files_list(&found_files, ".conf", root_dir, 0, search_1, search_2, NULL) == 0);
67 strv_print(found_files);
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);
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);
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);
84 assert_se(rm_rf(tmp_dir, REMOVE_ROOT|REMOVE_PHYSICAL) == 0);
87 int main(int argc, char **argv) {
88 log_set_max_level(LOG_DEBUG);
89 log_parse_environment();
92 test_conf_files_list(false);
93 test_conf_files_list(true);