chiark / gitweb /
resolved: when there's already somebody listening on the LLMNR ports, simple disable...
[elogind.git] / src / test / test-conf-files.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4   This file is part of systemd.
5
6   Copyright 2014 Michael Marineau
7
8   systemd is free software; you can redistribute it and/or modify it
9   under the terms of the GNU Lesser General Public License as published by
10   the Free Software Foundation; either version 2.1 of the License, or
11   (at your option) any later version.
12
13   systemd is distributed in the hope that it will be useful, but
14   WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16   Lesser General Public License for more details.
17
18   You should have received a copy of the GNU Lesser General Public License
19   along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 ***/
21
22 #include <stdio.h>
23 #include <stdarg.h>
24
25 #include "conf-files.h"
26 #include "macro.h"
27 #include "strv.h"
28 #include "util.h"
29
30
31 static void setup_test_dir(char *tmp_dir, const char *files, ...) {
32         va_list ap;
33
34         assert_se(mkdtemp(tmp_dir) != NULL);
35
36         va_start(ap, files);
37         while (files != NULL) {
38                 _cleanup_free_ char *path = strappend(tmp_dir, files);
39                 assert_se(touch_file(path, true, (usec_t) -1, (uid_t) -1, (gid_t) -1, 0) == 0);
40                 files = va_arg(ap, const char *);
41         }
42         va_end(ap);
43 }
44
45 static void test_conf_files_list(bool use_root) {
46         char tmp_dir[] = "/tmp/test-conf-files-XXXXXX";
47         _cleanup_strv_free_ char **found_files = NULL;
48         const char *root_dir, *search_1, *search_2, *expect_a, *expect_b;
49
50         setup_test_dir(tmp_dir,
51                        "/dir1/a.conf",
52                        "/dir2/a.conf",
53                        "/dir2/b.conf",
54                        NULL);
55
56         if (use_root) {
57                 root_dir = tmp_dir;
58                 search_1 = "/dir1";
59                 search_2 = "/dir2";
60         } else {
61                 root_dir = NULL;
62                 search_1 = strappenda(tmp_dir, "/dir1");
63                 search_2 = strappenda(tmp_dir, "/dir2");
64         }
65
66         expect_a = strappenda(tmp_dir, "/dir1/a.conf");
67         expect_b = strappenda(tmp_dir, "/dir2/b.conf");
68
69         assert_se(conf_files_list(&found_files, ".conf", root_dir, search_1, search_2, NULL) == 0);
70         strv_print(found_files);
71
72         assert_se(found_files);
73         assert_se(streq_ptr(found_files[0], expect_a));
74         assert_se(streq_ptr(found_files[1], expect_b));
75         assert_se(found_files[2] == NULL);
76
77         assert_se(rm_rf_dangerous(tmp_dir, false, true, false) == 0);
78 }
79
80 int main(int argc, char **argv) {
81         test_conf_files_list(false);
82         test_conf_files_list(true);
83         return 0;
84 }