]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/test/test-conf-files.c
Merge pull request #2495 from heftig/master
[thirdparty/systemd.git] / src / test / test-conf-files.c
CommitLineData
09e00c52
MM
1/***
2 This file is part of systemd.
3
4 Copyright 2014 Michael Marineau
5
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.
10
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.
15
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/>.
18***/
19
09e00c52 20#include <stdarg.h>
07630cea 21#include <stdio.h>
09e00c52 22
b5efdb8a 23#include "alloc-util.h"
09e00c52 24#include "conf-files.h"
f4f15635 25#include "fs-util.h"
09e00c52 26#include "macro.h"
ee735086 27#include "parse-util.h"
07630cea
LP
28#include "rm-rf.h"
29#include "string-util.h"
09e00c52 30#include "strv.h"
ee104e11 31#include "user-util.h"
09e00c52 32#include "util.h"
09e00c52
MM
33
34static void setup_test_dir(char *tmp_dir, const char *files, ...) {
35 va_list ap;
36
37 assert_se(mkdtemp(tmp_dir) != NULL);
38
39 va_start(ap, files);
40 while (files != NULL) {
41 _cleanup_free_ char *path = strappend(tmp_dir, files);
ee735086 42 assert_se(touch_file(path, true, USEC_INFINITY, UID_INVALID, GID_INVALID, MODE_INVALID) == 0);
09e00c52
MM
43 files = va_arg(ap, const char *);
44 }
45 va_end(ap);
46}
47
48static 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;
51 const char *root_dir, *search_1, *search_2, *expect_a, *expect_b;
52
53 setup_test_dir(tmp_dir,
54 "/dir1/a.conf",
55 "/dir2/a.conf",
56 "/dir2/b.conf",
57 NULL);
58
59 if (use_root) {
60 root_dir = tmp_dir;
61 search_1 = "/dir1";
62 search_2 = "/dir2";
63 } else {
64 root_dir = NULL;
63c372cb
LP
65 search_1 = strjoina(tmp_dir, "/dir1");
66 search_2 = strjoina(tmp_dir, "/dir2");
09e00c52
MM
67 }
68
63c372cb
LP
69 expect_a = strjoina(tmp_dir, "/dir1/a.conf");
70 expect_b = strjoina(tmp_dir, "/dir2/b.conf");
09e00c52
MM
71
72 assert_se(conf_files_list(&found_files, ".conf", root_dir, search_1, search_2, NULL) == 0);
73 strv_print(found_files);
74
75 assert_se(found_files);
76 assert_se(streq_ptr(found_files[0], expect_a));
77 assert_se(streq_ptr(found_files[1], expect_b));
78 assert_se(found_files[2] == NULL);
79
c6878637 80 assert_se(rm_rf(tmp_dir, REMOVE_ROOT|REMOVE_PHYSICAL) == 0);
09e00c52
MM
81}
82
83int main(int argc, char **argv) {
84 test_conf_files_list(false);
85 test_conf_files_list(true);
86 return 0;
87}