summaryrefslogtreecommitdiffstats
path: root/src/test/test-mkdir.c
blob: ef00729c08ba75470b6515f6740a42d266dbc916 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/* SPDX-License-Identifier: LGPL-2.1-or-later */

#include <unistd.h>

#include "capability-util.h"
#include "fs-util.h"
#include "mkdir.h"
#include "path-util.h"
#include "process-util.h"
#include "rm-rf.h"
#include "stat-util.h"
#include "tests.h"
#include "tmpfile-util.h"
#include "user-util.h"

TEST(mkdir_p_safe) {
        _cleanup_(rm_rf_physical_and_freep) char *tmp = NULL;
        _cleanup_free_ char *p = NULL, *q = NULL;
        int r;

        assert_se(mkdtemp_malloc("/tmp/test-mkdir-XXXXXX", &tmp) >= 0);

        assert_se(p = path_join(tmp, "run/aaa/bbb"));
        assert_se(mkdir_p(p, 0755) >= 0);
        assert_se(is_dir(p, false) > 0);
        assert_se(is_dir(p, true) > 0);

        p = mfree(p);
        assert_se(p = path_join(tmp, "run/ccc/ddd"));
        assert_se(mkdir_p_safe(tmp, p, 0755, UID_INVALID, GID_INVALID, 0) >= 0);
        assert_se(is_dir(p, false) > 0);
        assert_se(is_dir(p, true) > 0);

        p = mfree(p);
        assert_se(p = path_join(tmp, "var/run"));
        assert_se(mkdir_parents_safe(tmp, p, 0755, UID_INVALID, GID_INVALID, 0) >= 0);
        assert_se(symlink("../run", p) >= 0);
        assert_se(is_dir(p, false) == 0);
        assert_se(is_dir(p, true) > 0);

        assert_se(mkdir_safe(p, 0755, UID_INVALID, GID_INVALID, 0) == -ENOTDIR);
        assert_se(mkdir_safe(p, 0755, UID_INVALID, GID_INVALID, MKDIR_IGNORE_EXISTING) >= 0);
        assert_se(mkdir_safe(p, 0755, UID_INVALID, GID_INVALID, MKDIR_FOLLOW_SYMLINK) >= 0);
        assert_se(is_dir(p, false) == 0);
        assert_se(is_dir(p, true) > 0);

        p = mfree(p);
        assert_se(p = path_join(tmp, "var/run/hoge/foo/baz"));
        assert_se(mkdir_p_safe(tmp, p, 0755, UID_INVALID, GID_INVALID, 0) >= 0);
        assert_se(is_dir(p, false) > 0);
        assert_se(is_dir(p, true) > 0);

        p = mfree(p);
        assert_se(p = path_join(tmp, "not-exists"));
        assert_se(q = path_join(p, "aaa"));
        assert_se(mkdir_p_safe(p, q, 0755, UID_INVALID, GID_INVALID, 0) == -ENOENT);

        p = mfree(p);
        q = mfree(q);
        assert_se(p = path_join(tmp, "regular-file"));
        assert_se(q = path_join(p, "aaa"));
        assert_se(touch(p) >= 0);
        assert_se(mkdir_p_safe(p, q, 0755, UID_INVALID, GID_INVALID, 0) == -ENOTDIR);

        p = mfree(p);
        q = mfree(q);
        assert_se(p = path_join(tmp, "symlink"));
        assert_se(q = path_join(p, "hoge/foo"));
        assert_se(symlink("aaa", p) >= 0);
        assert_se(mkdir_p_safe(tmp, q, 0755, UID_INVALID, GID_INVALID, 0) >= 0);
        assert_se(is_dir(q, false) > 0);
        assert_se(is_dir(q, true) > 0);
        q = mfree(q);
        assert_se(q = path_join(tmp, "aaa/hoge/foo"));
        assert_se(is_dir(q, false) > 0);
        assert_se(is_dir(q, true) > 0);

        assert_se(mkdir_p_safe(tmp, "/tmp/test-mkdir-outside", 0755, UID_INVALID, GID_INVALID, 0) == -ENOTDIR);

        p = mfree(p);
        assert_se(p = path_join(tmp, "zero-mode/should-fail-to-create-child"));
        assert_se(mkdir_parents_safe(tmp, p, 0000, UID_INVALID, GID_INVALID, 0) >= 0);
        r = safe_fork("(test-mkdir-no-cap)", FORK_DEATHSIG_SIGTERM | FORK_WAIT | FORK_LOG, NULL);
        if (r == 0) {
                (void) capability_bounding_set_drop(0, /* right_now = */ true);
                assert_se(mkdir_p_safe(tmp, p, 0000, UID_INVALID, GID_INVALID, 0) == -EACCES);
                _exit(EXIT_SUCCESS);
        }
        assert_se(r >= 0);
}

TEST(mkdir_p_root) {
        _cleanup_(rm_rf_physical_and_freep) char *tmp = NULL;
        _cleanup_free_ char *p = NULL;

        assert_se(mkdtemp_malloc("/tmp/test-mkdir-XXXXXX", &tmp) >= 0);

        assert_se(p = path_join(tmp, "run/aaa/bbb"));
        assert_se(mkdir_p_root(tmp, "/run/aaa/bbb", UID_INVALID, GID_INVALID, 0755) >= 0);
        assert_se(is_dir(p, false) > 0);
        assert_se(is_dir(p, true) > 0);

        p = mfree(p);
        assert_se(p = path_join(tmp, "var/run"));
        assert_se(mkdir_parents_safe(tmp, p, 0755, UID_INVALID, GID_INVALID, 0) >= 0);
        assert_se(symlink("../run", p) >= 0);
        assert_se(is_dir(p, false) == 0);
        assert_se(is_dir(p, true) > 0);

        p = mfree(p);
        assert_se(p = path_join(tmp, "var/run/hoge/foo/baz"));
        assert_se(mkdir_p_root(tmp, "/var/run/hoge/foo/baz", UID_INVALID, GID_INVALID, 0755) >= 0);
        assert_se(is_dir(p, false) > 0);
        assert_se(is_dir(p, true) > 0);

        p = mfree(p);
        assert_se(p = path_join(tmp, "not-exists"));
        assert_se(mkdir_p_root(p, "/aaa", UID_INVALID, GID_INVALID, 0755) == -ENOENT);

        p = mfree(p);
        assert_se(p = path_join(tmp, "regular-file"));
        assert_se(touch(p) >= 0);
        assert_se(mkdir_p_root(p, "/aaa", UID_INVALID, GID_INVALID, 0755) == -ENOTDIR);

        /* FIXME: The tests below do not work.
        p = mfree(p);
        assert_se(p = path_join(tmp, "symlink"));
        assert_se(symlink("aaa", p) >= 0);
        assert_se(mkdir_p_root(tmp, "/symlink/hoge/foo", UID_INVALID, GID_INVALID, 0755) >= 0);
        p = mfree(p);
        assert_se(p = path_join(tmp, "symlink/hoge/foo"));
        assert_se(is_dir(p, false) > 0);
        assert_se(is_dir(p, true) > 0);
        p = mfree(p);
        assert_se(p = path_join(tmp, "aaa/hoge/foo"));
        assert_se(is_dir(p, false) > 0);
        assert_se(is_dir(p, true) > 0);
        */
}

TEST(mkdir_p_root_full) {
        _cleanup_(rm_rf_physical_and_freep) char *tmp = NULL;
        _cleanup_free_ char *p = NULL;
        struct stat st;

        ASSERT_OK(mkdtemp_malloc("/tmp/test-mkdir-XXXXXX", &tmp));

        ASSERT_NOT_NULL(p = path_join(tmp, "foo"));
        ASSERT_OK(mkdir_p_root_full(tmp, "/foo", UID_INVALID, GID_INVALID, 0755, 2 * USEC_PER_SEC, NULL));
        ASSERT_GT(is_dir(p, false), 0);
        ASSERT_GT(is_dir(p, true), 0);
        ASSERT_OK_ERRNO(stat(p, &st));
        ASSERT_EQ(st.st_mtim.tv_sec, 2);
        ASSERT_EQ(st.st_atim.tv_sec, 2);

        p = mfree(p);
        ASSERT_NOT_NULL(p = path_join(tmp, "dir-not-exists/foo"));
        ASSERT_OK(mkdir_p_root_full(NULL, p, UID_INVALID, GID_INVALID, 0755, 90 * USEC_PER_HOUR, NULL));
        ASSERT_GT(is_dir(p, false), 0);
        ASSERT_GT(is_dir(p, true), 0);
        p = mfree(p);
        ASSERT_NOT_NULL(p = path_join(tmp, "dir-not-exists"));
        ASSERT_OK_ERRNO(stat(p, &st));
        ASSERT_EQ(st.st_mtim.tv_sec, 90 * 60 * 60);
        ASSERT_EQ(st.st_atim.tv_sec, 90 * 60 * 60);
}

DEFINE_TEST_MAIN(LOG_DEBUG);