]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/test/test-conf-files.c
util: rework rm_rf() logic
[thirdparty/systemd.git] / src / test / test-conf-files.c
CommitLineData
09e00c52
MM
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"
c6878637 29#include "rm-rf.h"
09e00c52
MM
30
31static 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);
46bcf492 39 assert_se(touch_file(path, true, USEC_INFINITY, UID_INVALID, GID_INVALID, 0) == 0);
09e00c52
MM
40 files = va_arg(ap, const char *);
41 }
42 va_end(ap);
43}
44
45static 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;
63c372cb
LP
62 search_1 = strjoina(tmp_dir, "/dir1");
63 search_2 = strjoina(tmp_dir, "/dir2");
09e00c52
MM
64 }
65
63c372cb
LP
66 expect_a = strjoina(tmp_dir, "/dir1/a.conf");
67 expect_b = strjoina(tmp_dir, "/dir2/b.conf");
09e00c52
MM
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
c6878637 77 assert_se(rm_rf(tmp_dir, REMOVE_ROOT|REMOVE_PHYSICAL) == 0);
09e00c52
MM
78}
79
80int main(int argc, char **argv) {
81 test_conf_files_list(false);
82 test_conf_files_list(true);
83 return 0;
84}