]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-conf-files.c
util-lib: split our string related calls from util.[ch] into its own file string...
[thirdparty/systemd.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 <stdarg.h>
23 #include <stdio.h>
24
25 #include "conf-files.h"
26 #include "macro.h"
27 #include "rm-rf.h"
28 #include "string-util.h"
29 #include "strv.h"
30 #include "util.h"
31
32 static void setup_test_dir(char *tmp_dir, const char *files, ...) {
33 va_list ap;
34
35 assert_se(mkdtemp(tmp_dir) != NULL);
36
37 va_start(ap, files);
38 while (files != NULL) {
39 _cleanup_free_ char *path = strappend(tmp_dir, files);
40 assert_se(touch_file(path, true, USEC_INFINITY, UID_INVALID, GID_INVALID, 0) == 0);
41 files = va_arg(ap, const char *);
42 }
43 va_end(ap);
44 }
45
46 static void test_conf_files_list(bool use_root) {
47 char tmp_dir[] = "/tmp/test-conf-files-XXXXXX";
48 _cleanup_strv_free_ char **found_files = NULL;
49 const char *root_dir, *search_1, *search_2, *expect_a, *expect_b;
50
51 setup_test_dir(tmp_dir,
52 "/dir1/a.conf",
53 "/dir2/a.conf",
54 "/dir2/b.conf",
55 NULL);
56
57 if (use_root) {
58 root_dir = tmp_dir;
59 search_1 = "/dir1";
60 search_2 = "/dir2";
61 } else {
62 root_dir = NULL;
63 search_1 = strjoina(tmp_dir, "/dir1");
64 search_2 = strjoina(tmp_dir, "/dir2");
65 }
66
67 expect_a = strjoina(tmp_dir, "/dir1/a.conf");
68 expect_b = strjoina(tmp_dir, "/dir2/b.conf");
69
70 assert_se(conf_files_list(&found_files, ".conf", root_dir, search_1, search_2, NULL) == 0);
71 strv_print(found_files);
72
73 assert_se(found_files);
74 assert_se(streq_ptr(found_files[0], expect_a));
75 assert_se(streq_ptr(found_files[1], expect_b));
76 assert_se(found_files[2] == NULL);
77
78 assert_se(rm_rf(tmp_dir, REMOVE_ROOT|REMOVE_PHYSICAL) == 0);
79 }
80
81 int main(int argc, char **argv) {
82 test_conf_files_list(false);
83 test_conf_files_list(true);
84 return 0;
85 }