summaryrefslogtreecommitdiffstats
path: root/src/test/test-tmpfile-util.c
blob: 415f185a817b5dde5f8ed242d59d344cd3440ebd (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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/* SPDX-License-Identifier: LGPL-2.1-or-later */

#include "alloc-util.h"
#include "errno-util.h"
#include "log.h"
#include "path-util.h"
#include "string-util.h"
#include "tests.h"
#include "tmpfile-util.h"

static void test_tempfn_random_one(const char *p, const char *extra, const char *expect, int ret) {
        _cleanup_free_ char *s = NULL;
        int r;

        r = tempfn_random(p, extra, &s);
        log_info("%s+%s → %s vs. %s (%i/%s vs. %i/%s)",
                 p, strna(extra), strna(s), strna(expect),
                 r, STRERROR(r), ret, STRERROR(ret));

        assert_se(!s == !expect);
        if (s) {
                const char *suffix;

                assert_se(suffix = startswith(s, expect));
                assert_se(in_charset(suffix, HEXDIGITS));
                assert_se(strlen(suffix) == 16);
        }
        assert_se(ret == r);
}

TEST(tempfn_random) {
        _cleanup_free_ char *dir = NULL, *p = NULL, *q = NULL;

        test_tempfn_random_one("", NULL, NULL, -EINVAL);
        test_tempfn_random_one(".", NULL, NULL, -EADDRNOTAVAIL);
        test_tempfn_random_one("..", NULL, NULL, -EINVAL);
        test_tempfn_random_one("/", NULL, NULL, -EADDRNOTAVAIL);
        test_tempfn_random_one("foo", "hoge/aaa", NULL, -EINVAL);

        test_tempfn_random_one("foo", NULL, ".#foo", 0);
        test_tempfn_random_one("foo", "bar", ".#barfoo", 0);
        test_tempfn_random_one("/tmp/foo", NULL, "/tmp/.#foo", 0);
        test_tempfn_random_one("/tmp/foo", "bar", "/tmp/.#barfoo", 0);
        test_tempfn_random_one("./foo", NULL, ".#foo", 0);
        test_tempfn_random_one("./foo", "bar", ".#barfoo", 0);
        test_tempfn_random_one("../foo", NULL, "../.#foo", 0);
        test_tempfn_random_one("../foo", "bar", "../.#barfoo", 0);

        test_tempfn_random_one("foo/", NULL, ".#foo", 0);
        test_tempfn_random_one("foo/", "bar", ".#barfoo", 0);
        test_tempfn_random_one("/tmp/foo/", NULL, "/tmp/.#foo", 0);
        test_tempfn_random_one("/tmp/foo/", "bar", "/tmp/.#barfoo", 0);
        test_tempfn_random_one("./foo/", NULL, ".#foo", 0);
        test_tempfn_random_one("./foo/", "bar", ".#barfoo", 0);
        test_tempfn_random_one("../foo/", NULL, "../.#foo", 0);
        test_tempfn_random_one("../foo/", "bar", "../.#barfoo", 0);

        assert_se(dir = new(char, PATH_MAX - 20));
        memset(dir, 'x', PATH_MAX - 21);
        dir[PATH_MAX - 21] = '\0';
        for (size_t i = 0; i < PATH_MAX - 21; i += NAME_MAX + 1)
                dir[i] = '/';

        assert_se(p = path_join(dir, "a"));
        assert_se(q = path_join(dir, ".#a"));

        test_tempfn_random_one(p, NULL, q, 0);
        test_tempfn_random_one(p, "b", NULL, -EINVAL);

        p = mfree(p);
        q = mfree(q);

        assert_se(p = new(char, NAME_MAX + 1));
        memset(p, 'x', NAME_MAX);
        p[NAME_MAX] = '\0';

        assert_se(q = new(char, NAME_MAX + 1));
        memset(stpcpy(q, ".#"), 'x', NAME_MAX - STRLEN(".#") - 16);
        q[NAME_MAX - 16] = '\0';

        test_tempfn_random_one(p, NULL, q, 0);

        memset(stpcpy(q, ".#hoge"), 'x', NAME_MAX - STRLEN(".#hoge") - 16);
        q[NAME_MAX - 16] = '\0';

        test_tempfn_random_one(p, "hoge", q, 0);
}

static void test_tempfn_xxxxxx_one(const char *p, const char *extra, const char *expect, int ret) {
        _cleanup_free_ char *s = NULL;
        int r;

        r = tempfn_xxxxxx(p, extra, &s);
        log_info("%s+%s → %s vs. %s (%i/%s vs. %i/%s)",
                 p, strna(extra), strna(s), strna(expect),
                 r, STRERROR(r), ret, STRERROR(ret));

        assert_se(!s == !expect);
        if (s) {
                const char *suffix;

                assert_se(suffix = startswith(s, expect));
                assert_se(streq(suffix, "XXXXXX"));
        }
        assert_se(ret == r);
}

TEST(tempfn_xxxxxx) {
        _cleanup_free_ char *dir = NULL, *p = NULL, *q = NULL;

        test_tempfn_xxxxxx_one("", NULL, NULL, -EINVAL);
        test_tempfn_xxxxxx_one(".", NULL, NULL, -EADDRNOTAVAIL);
        test_tempfn_xxxxxx_one("..", NULL, NULL, -EINVAL);
        test_tempfn_xxxxxx_one("/", NULL, NULL, -EADDRNOTAVAIL);
        test_tempfn_xxxxxx_one("foo", "hoge/aaa", NULL, -EINVAL);

        test_tempfn_xxxxxx_one("foo", NULL, ".#foo", 0);
        test_tempfn_xxxxxx_one("foo", "bar", ".#barfoo", 0);
        test_tempfn_xxxxxx_one("/tmp/foo", NULL, "/tmp/.#foo", 0);
        test_tempfn_xxxxxx_one("/tmp/foo", "bar", "/tmp/.#barfoo", 0);
        test_tempfn_xxxxxx_one("./foo", NULL, ".#foo", 0);
        test_tempfn_xxxxxx_one("./foo", "bar", ".#barfoo", 0);
        test_tempfn_xxxxxx_one("../foo", NULL, "../.#foo", 0);
        test_tempfn_xxxxxx_one("../foo", "bar", "../.#barfoo", 0);

        test_tempfn_xxxxxx_one("foo/", NULL, ".#foo", 0);
        test_tempfn_xxxxxx_one("foo/", "bar", ".#barfoo", 0);
        test_tempfn_xxxxxx_one("/tmp/foo/", NULL, "/tmp/.#foo", 0);
        test_tempfn_xxxxxx_one("/tmp/foo/", "bar", "/tmp/.#barfoo", 0);
        test_tempfn_xxxxxx_one("./foo/", NULL, ".#foo", 0);
        test_tempfn_xxxxxx_one("./foo/", "bar", ".#barfoo", 0);
        test_tempfn_xxxxxx_one("../foo/", NULL, "../.#foo", 0);
        test_tempfn_xxxxxx_one("../foo/", "bar", "../.#barfoo", 0);

        assert_se(dir = new(char, PATH_MAX - 10));
        memset(dir, 'x', PATH_MAX - 11);
        dir[PATH_MAX - 11] = '\0';
        for (size_t i = 0; i < PATH_MAX - 11; i += NAME_MAX + 1)
                dir[i] = '/';

        assert_se(p = path_join(dir, "a"));
        assert_se(q = path_join(dir, ".#a"));

        test_tempfn_xxxxxx_one(p, NULL, q, 0);
        test_tempfn_xxxxxx_one(p, "b", NULL, -EINVAL);

        p = mfree(p);
        q = mfree(q);

        assert_se(p = new(char, NAME_MAX + 1));
        memset(p, 'x', NAME_MAX);
        p[NAME_MAX] = '\0';

        assert_se(q = new(char, NAME_MAX + 1));
        memset(stpcpy(q, ".#"), 'x', NAME_MAX - STRLEN(".#") - 6);
        q[NAME_MAX - 6] = '\0';

        test_tempfn_xxxxxx_one(p, NULL, q, 0);

        memset(stpcpy(q, ".#hoge"), 'x', NAME_MAX - STRLEN(".#hoge") - 6);
        q[NAME_MAX - 6] = '\0';

        test_tempfn_xxxxxx_one(p, "hoge", q, 0);
}

static void test_tempfn_random_child_one(const char *p, const char *extra, const char *expect, int ret) {
        _cleanup_free_ char *s = NULL;
        int r;

        r = tempfn_random_child(p, extra, &s);
        log_info_errno(r, "%s+%s → %s vs. %s (%i/%s vs. %i/%s)",
                       p, strna(extra), strna(s), strna(expect),
                       r, STRERROR(r), ret, STRERROR(ret));

        assert_se(!s == !expect);
        if (s) {
                const char *suffix;

                assert_se(suffix = startswith(s, expect));
                assert_se(in_charset(suffix, HEXDIGITS));
                assert_se(strlen(suffix) == 16);
        }
        assert_se(ret == r);
}

TEST(tempfn_random_child) {
        _cleanup_free_ char *dir = NULL, *p = NULL, *q = NULL;

        test_tempfn_random_child_one("", NULL, ".#", 0);
        test_tempfn_random_child_one(".", NULL, ".#", 0);
        test_tempfn_random_child_one("..", NULL, "../.#", 0);
        test_tempfn_random_child_one("/", NULL, "/.#", 0);
        test_tempfn_random_child_one("foo", "hoge/aaa", NULL, -EINVAL);

        test_tempfn_random_child_one("foo", NULL, "foo/.#", 0);
        test_tempfn_random_child_one("foo", "bar", "foo/.#bar", 0);
        test_tempfn_random_child_one("/tmp/foo", NULL, "/tmp/foo/.#", 0);
        test_tempfn_random_child_one("/tmp/foo", "bar", "/tmp/foo/.#bar", 0);
        test_tempfn_random_child_one("./foo", NULL, "foo/.#", 0);
        test_tempfn_random_child_one("./foo", "bar", "foo/.#bar", 0);
        test_tempfn_random_child_one("../foo", NULL, "../foo/.#", 0);
        test_tempfn_random_child_one("../foo", "bar", "../foo/.#bar", 0);

        test_tempfn_random_child_one("foo/", NULL, "foo/.#", 0);
        test_tempfn_random_child_one("foo/", "bar", "foo/.#bar", 0);
        test_tempfn_random_child_one("/tmp/foo/", NULL, "/tmp/foo/.#", 0);
        test_tempfn_random_child_one("/tmp/foo/", "bar", "/tmp/foo/.#bar", 0);
        test_tempfn_random_child_one("./foo/", NULL, "foo/.#", 0);
        test_tempfn_random_child_one("./foo/", "bar", "foo/.#bar", 0);
        test_tempfn_random_child_one("../foo/", NULL, "../foo/.#", 0);
        test_tempfn_random_child_one("../foo/", "bar", "../foo/.#bar", 0);

        assert_se(dir = new(char, PATH_MAX - 21));
        memset(dir, 'x', PATH_MAX - 22);
        dir[PATH_MAX - 22] = '\0';
        for (size_t i = 0; i < PATH_MAX - 22; i += NAME_MAX + 1)
                dir[i] = '/';

        assert_se(p = path_join(dir, "a"));
        assert_se(q = path_join(p, ".#"));

        test_tempfn_random_child_one(p, NULL, q, 0);
        test_tempfn_random_child_one(p, "b", NULL, -EINVAL);

        p = mfree(p);
        q = mfree(q);

        assert_se(p = new(char, NAME_MAX + 1));
        memset(p, 'x', NAME_MAX);
        p[NAME_MAX] = '\0';

        assert_se(q = path_join(p, ".#"));

        test_tempfn_random_child_one(p, NULL, q, 0);

        assert_se(strextend(&q, "hoge"));
        test_tempfn_random_child_one(p, "hoge", q, 0);
}

DEFINE_TEST_MAIN(LOG_DEBUG);