Debian Codegraph @

src/test/test-tmpfile-util.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <unistd.h>
6
7 #include "alloc-util.h"
8 #include "argv-util.h"
9 #include "errno-util.h"
10 #include "fd-util.h"
11 #include "fileio.h"
12 #include "format-util.h"
13 #include "fs-util.h"
14 #include "iovec-util.h"
15 #include "log.h"
16 #include "path-util.h"
17 #include "process-util.h"
18 #include "string-util.h"
19 #include "tests.h"
20 #include "tmpfile-util.h"
21
22 TEST(writing_tmpfile) {
23 _cleanup_(unlink_tempfilep) char name[] = "/tmp/test-systemd_writing_tmpfile.XXXXXX";
24 _cleanup_free_ char *contents = NULL;
25 size_t size;
26 _cleanup_close_ int fd = -EBADF;
27
28 struct iovec iov[] = {
29 IOVEC_MAKE_STRING("abc\n"),
30 IOVEC_MAKE_STRING(ALPHANUMERICAL "\n"),
31 IOVEC_MAKE_STRING(""),
32 };
33
34 ASSERT_OK(fd = mkostemp_safe(name));
35
36 ASSERT_OK_ERRNO(writev(fd, iov, 3));
37
38 ASSERT_OK(read_full_file(name, &contents, &size));
39 ASSERT_STREQ(contents, "abc\n" ALPHANUMERICAL "\n");
40 }
41
42 TEST(tempfn) {
43 char *ret = NULL, *p;
44
45 ASSERT_OK(tempfn_xxxxxx("/foo/bar/waldo", NULL, &ret));
46 ASSERT_STREQ(ret, "/foo/bar/.#waldoXXXXXX");
47 free(ret);
48
49 ASSERT_OK(tempfn_xxxxxx("/foo/bar/waldo", "[miau]", &ret));
50 ASSERT_STREQ(ret, "/foo/bar/.#[miau]waldoXXXXXX");
51 free(ret);
52
53 ASSERT_OK(tempfn_random("/foo/bar/waldo", NULL, &ret));
54 ASSERT_NOT_NULL(p = startswith(ret, "/foo/bar/.#waldo"));
55 ASSERT_EQ(strlen(p), 16U);
56 ASSERT_TRUE(in_charset(p, "0123456789abcdef"));
57 free(ret);
58
59 ASSERT_OK(tempfn_random("/foo/bar/waldo", "[wuff]", &ret));
60 ASSERT_NOT_NULL(p = startswith(ret, "/foo/bar/.#[wuff]waldo"));
61 ASSERT_EQ(strlen(p), 16U);
62 ASSERT_TRUE(in_charset(p, "0123456789abcdef"));
63 free(ret);
64
65 ASSERT_OK(tempfn_random_child("/foo/bar/waldo", NULL, &ret));
66 ASSERT_NOT_NULL(p = startswith(ret, "/foo/bar/waldo/.#"));
67 ASSERT_EQ(strlen(p), 16U);
68 ASSERT_TRUE(in_charset(p, "0123456789abcdef"));
69 free(ret);
70
71 ASSERT_OK(tempfn_random_child("/foo/bar/waldo", "[kikiriki]", &ret));
72 ASSERT_NOT_NULL(p = startswith(ret, "/foo/bar/waldo/.#[kikiriki]"));
73 ASSERT_EQ(strlen(p), 16U);
74 ASSERT_TRUE(in_charset(p, "0123456789abcdef"));
75 free(ret);
76 }
77
78 static void test_tempfn_random_one(const char *p, const char *extra, const char *expect, int ret) {
79 _cleanup_free_ char *s = NULL;
80 int r;
81
82 r = tempfn_random(p, extra, &s);
83 log_info("%s+%s → %s vs. %s (%i/%s vs. %i/%s)",
84 p, strna(extra), strna(s), strna(expect),
85 r, STRERROR(r), ret, STRERROR(ret));
86
87 assert_se(!s == !expect);
88 if (s) {
89 const char *suffix;
90
91 assert_se(suffix = startswith(s, expect));
92 assert_se(in_charset(suffix, HEXDIGITS));
93 assert_se(strlen(suffix) == 16);
94 }
95 assert_se(ret == r);
96 }
97
98 TEST(tempfn_random) {
99 _cleanup_free_ char *dir = NULL, *p = NULL, *q = NULL;
100
101 test_tempfn_random_one("", NULL, NULL, -EINVAL);
102 test_tempfn_random_one(".", NULL, NULL, -EADDRNOTAVAIL);
103 test_tempfn_random_one("..", NULL, NULL, -EINVAL);
104 test_tempfn_random_one("/", NULL, NULL, -EADDRNOTAVAIL);
105 test_tempfn_random_one("foo", "hoge/aaa", NULL, -EINVAL);
106
107 test_tempfn_random_one("foo", NULL, ".#foo", 0);
108 test_tempfn_random_one("foo", "bar", ".#barfoo", 0);
109 test_tempfn_random_one("/tmp/foo", NULL, "/tmp/.#foo", 0);
110 test_tempfn_random_one("/tmp/foo", "bar", "/tmp/.#barfoo", 0);
111 test_tempfn_random_one("./foo", NULL, ".#foo", 0);
112 test_tempfn_random_one("./foo", "bar", ".#barfoo", 0);
113 test_tempfn_random_one("../foo", NULL, "../.#foo", 0);
114 test_tempfn_random_one("../foo", "bar", "../.#barfoo", 0);
115
116 test_tempfn_random_one("foo/", NULL, ".#foo", 0);
117 test_tempfn_random_one("foo/", "bar", ".#barfoo", 0);
118 test_tempfn_random_one("/tmp/foo/", NULL, "/tmp/.#foo", 0);
119 test_tempfn_random_one("/tmp/foo/", "bar", "/tmp/.#barfoo", 0);
120 test_tempfn_random_one("./foo/", NULL, ".#foo", 0);
121 test_tempfn_random_one("./foo/", "bar", ".#barfoo", 0);
122 test_tempfn_random_one("../foo/", NULL, "../.#foo", 0);
123 test_tempfn_random_one("../foo/", "bar", "../.#barfoo", 0);
124
125 assert_se(dir = new(char, PATH_MAX - 20));
126 memset(dir, 'x', PATH_MAX - 21);
127 dir[PATH_MAX - 21] = '\0';
128 for (size_t i = 0; i < PATH_MAX - 21; i += NAME_MAX + 1)
129 dir[i] = '/';
130
131 assert_se(p = path_join(dir, "a"));
132 assert_se(q = path_join(dir, ".#a"));
133
134 test_tempfn_random_one(p, NULL, q, 0);
135 test_tempfn_random_one(p, "b", NULL, -EINVAL);
136
137 p = mfree(p);
138 q = mfree(q);
139
140 assert_se(p = new(char, NAME_MAX + 1));
141 memset(p, 'x', NAME_MAX);
142 p[NAME_MAX] = '\0';
143
144 assert_se(q = new(char, NAME_MAX + 1));
145 memset(stpcpy(q, ".#"), 'x', NAME_MAX - STRLEN(".#") - 16);
146 q[NAME_MAX - 16] = '\0';
147
148 test_tempfn_random_one(p, NULL, q, 0);
149
150 memset(stpcpy(q, ".#hoge"), 'x', NAME_MAX - STRLEN(".#hoge") - 16);
151 q[NAME_MAX - 16] = '\0';
152
153 test_tempfn_random_one(p, "hoge", q, 0);
154 }
155
156 static void test_tempfn_xxxxxx_one(const char *p, const char *extra, const char *expect, int ret) {
157 _cleanup_free_ char *s = NULL;
158 int r;
159
160 r = tempfn_xxxxxx(p, extra, &s);
161 log_info("%s+%s → %s vs. %s (%i/%s vs. %i/%s)",
162 p, strna(extra), strna(s), strna(expect),
163 r, STRERROR(r), ret, STRERROR(ret));
164
165 assert_se(!s == !expect);
166 if (s) {
167 const char *suffix;
168
169 assert_se(suffix = startswith(s, expect));
170 ASSERT_STREQ(suffix, "XXXXXX");
171 }
172 assert_se(ret == r);
173 }
174
175 TEST(tempfn_xxxxxx) {
176 _cleanup_free_ char *dir = NULL, *p = NULL, *q = NULL;
177
178 test_tempfn_xxxxxx_one("", NULL, NULL, -EINVAL);
179 test_tempfn_xxxxxx_one(".", NULL, NULL, -EADDRNOTAVAIL);
180 test_tempfn_xxxxxx_one("..", NULL, NULL, -EINVAL);
181 test_tempfn_xxxxxx_one("/", NULL, NULL, -EADDRNOTAVAIL);
182 test_tempfn_xxxxxx_one("foo", "hoge/aaa", NULL, -EINVAL);
183
184 test_tempfn_xxxxxx_one("foo", NULL, ".#foo", 0);
185 test_tempfn_xxxxxx_one("foo", "bar", ".#barfoo", 0);
186 test_tempfn_xxxxxx_one("/tmp/foo", NULL, "/tmp/.#foo", 0);
187 test_tempfn_xxxxxx_one("/tmp/foo", "bar", "/tmp/.#barfoo", 0);
188 test_tempfn_xxxxxx_one("./foo", NULL, ".#foo", 0);
189 test_tempfn_xxxxxx_one("./foo", "bar", ".#barfoo", 0);
190 test_tempfn_xxxxxx_one("../foo", NULL, "../.#foo", 0);
191 test_tempfn_xxxxxx_one("../foo", "bar", "../.#barfoo", 0);
192
193 test_tempfn_xxxxxx_one("foo/", NULL, ".#foo", 0);
194 test_tempfn_xxxxxx_one("foo/", "bar", ".#barfoo", 0);
195 test_tempfn_xxxxxx_one("/tmp/foo/", NULL, "/tmp/.#foo", 0);
196 test_tempfn_xxxxxx_one("/tmp/foo/", "bar", "/tmp/.#barfoo", 0);
197 test_tempfn_xxxxxx_one("./foo/", NULL, ".#foo", 0);
198 test_tempfn_xxxxxx_one("./foo/", "bar", ".#barfoo", 0);
199 test_tempfn_xxxxxx_one("../foo/", NULL, "../.#foo", 0);
200 test_tempfn_xxxxxx_one("../foo/", "bar", "../.#barfoo", 0);
201
202 assert_se(dir = new(char, PATH_MAX - 10));
203 memset(dir, 'x', PATH_MAX - 11);
204 dir[PATH_MAX - 11] = '\0';
205 for (size_t i = 0; i < PATH_MAX - 11; i += NAME_MAX + 1)
206 dir[i] = '/';
207
208 assert_se(p = path_join(dir, "a"));
209 assert_se(q = path_join(dir, ".#a"));
210
211 test_tempfn_xxxxxx_one(p, NULL, q, 0);
212 test_tempfn_xxxxxx_one(p, "b", NULL, -EINVAL);
213
214 p = mfree(p);
215 q = mfree(q);
216
217 assert_se(p = new(char, NAME_MAX + 1));
218 memset(p, 'x', NAME_MAX);
219 p[NAME_MAX] = '\0';
220
221 assert_se(q = new(char, NAME_MAX + 1));
222 memset(stpcpy(q, ".#"), 'x', NAME_MAX - STRLEN(".#") - 6);
223 q[NAME_MAX - 6] = '\0';
224
225 test_tempfn_xxxxxx_one(p, NULL, q, 0);
226
227 memset(stpcpy(q, ".#hoge"), 'x', NAME_MAX - STRLEN(".#hoge") - 6);
228 q[NAME_MAX - 6] = '\0';
229
230 test_tempfn_xxxxxx_one(p, "hoge", q, 0);
231 }
232
233 static void test_tempfn_random_child_one(const char *p, const char *extra, const char *expect, int ret) {
234 _cleanup_free_ char *s = NULL;
235 int r;
236
237 r = tempfn_random_child(p, extra, &s);
238 log_info_errno(r, "%s+%s → %s vs. %s (%i/%s vs. %i/%s)",
239 p, strna(extra), strna(s), strna(expect),
240 r, STRERROR(r), ret, STRERROR(ret));
241
242 assert_se(!s == !expect);
243 if (s) {
244 const char *suffix;
245
246 assert_se(suffix = startswith(s, expect));
247 assert_se(in_charset(suffix, HEXDIGITS));
248 assert_se(strlen(suffix) == 16);
249 }
250 assert_se(ret == r);
251 }
252
253 TEST(tempfn_random_child) {
254 _cleanup_free_ char *dir = NULL, *p = NULL, *q = NULL;
255
256 test_tempfn_random_child_one("", NULL, ".#", 0);
257 test_tempfn_random_child_one(".", NULL, ".#", 0);
258 test_tempfn_random_child_one("..", NULL, "../.#", 0);
259 test_tempfn_random_child_one("/", NULL, "/.#", 0);
260 test_tempfn_random_child_one("foo", "hoge/aaa", NULL, -EINVAL);
261
262 test_tempfn_random_child_one("foo", NULL, "foo/.#", 0);
263 test_tempfn_random_child_one("foo", "bar", "foo/.#bar", 0);
264 test_tempfn_random_child_one("/tmp/foo", NULL, "/tmp/foo/.#", 0);
265 test_tempfn_random_child_one("/tmp/foo", "bar", "/tmp/foo/.#bar", 0);
266 test_tempfn_random_child_one("./foo", NULL, "foo/.#", 0);
267 test_tempfn_random_child_one("./foo", "bar", "foo/.#bar", 0);
268 test_tempfn_random_child_one("../foo", NULL, "../foo/.#", 0);
269 test_tempfn_random_child_one("../foo", "bar", "../foo/.#bar", 0);
270
271 test_tempfn_random_child_one("foo/", NULL, "foo/.#", 0);
272 test_tempfn_random_child_one("foo/", "bar", "foo/.#bar", 0);
273 test_tempfn_random_child_one("/tmp/foo/", NULL, "/tmp/foo/.#", 0);
274 test_tempfn_random_child_one("/tmp/foo/", "bar", "/tmp/foo/.#bar", 0);
275 test_tempfn_random_child_one("./foo/", NULL, "foo/.#", 0);
276 test_tempfn_random_child_one("./foo/", "bar", "foo/.#bar", 0);
277 test_tempfn_random_child_one("../foo/", NULL, "../foo/.#", 0);
278 test_tempfn_random_child_one("../foo/", "bar", "../foo/.#bar", 0);
279
280 assert_se(dir = new(char, PATH_MAX - 21));
281 memset(dir, 'x', PATH_MAX - 22);
282 dir[PATH_MAX - 22] = '\0';
283 for (size_t i = 0; i < PATH_MAX - 22; i += NAME_MAX + 1)
284 dir[i] = '/';
285
286 assert_se(p = path_join(dir, "a"));
287 assert_se(q = path_join(p, ".#"));
288
289 test_tempfn_random_child_one(p, NULL, q, 0);
290 test_tempfn_random_child_one(p, "b", NULL, -EINVAL);
291
292 p = mfree(p);
293 q = mfree(q);
294
295 assert_se(p = new(char, NAME_MAX + 1));
296 memset(p, 'x', NAME_MAX);
297 p[NAME_MAX] = '\0';
298
299 assert_se(q = path_join(p, ".#"));
300
301 test_tempfn_random_child_one(p, NULL, q, 0);
302
303 assert_se(strextend(&q, "hoge"));
304 test_tempfn_random_child_one(p, "hoge", q, 0);
305 }
306
307 TEST(link_tmpfile) {
308 _cleanup_free_ char *cmd = NULL, *cmd2 = NULL, *ans = NULL, *ans2 = NULL, *d = NULL, *tmp = NULL, *line = NULL;
309 _cleanup_close_ int fd = -EBADF, fd2 = -EBADF;
310 const char *p = saved_argv[1] ?: "/tmp";
311 char *pattern;
312
313 pattern = strjoina(p, "/systemd-test-XXXXXX");
314
315 fd = open_tmpfile_unlinkable(p, O_RDWR|O_CLOEXEC);
316 assert_se(fd >= 0);
317
318 assert_se(asprintf(&cmd, "ls -l /proc/"PID_FMT"/fd/%d", getpid_cached(), fd) > 0);
319 (void) system(cmd);
320 assert_se(readlink_malloc(cmd + 6, &ans) >= 0);
321 log_debug("link1: %s", ans);
322 assert_se(endswith(ans, " (deleted)"));
323
324 fd2 = mkostemp_safe(pattern);
325 assert_se(fd2 >= 0);
326 assert_se(unlink(pattern) == 0);
327
328 assert_se(asprintf(&cmd2, "ls -l /proc/"PID_FMT"/fd/%d", getpid_cached(), fd2) > 0);
329 (void) system(cmd2);
330 assert_se(readlink_malloc(cmd2 + 6, &ans2) >= 0);
331 log_debug("link2: %s", ans2);
332 assert_se(endswith(ans2, " (deleted)"));
333
334 pattern = strjoina(p, "/tmpfiles-test");
335 assert_se(tempfn_random(pattern, NULL, &d) >= 0);
336
337 fd = safe_close(fd);
338 fd = open_tmpfile_linkable(d, O_RDWR|O_CLOEXEC, &tmp);
339 assert_se(fd >= 0);
340 assert_se(write(fd, "foobar\n", 7) == 7);
341
342 assert_se(touch(d) >= 0);
343 assert_se(link_tmpfile(fd, tmp, d, /* flags= */ 0) == -EEXIST);
344 assert_se(unlink(d) >= 0);
345 assert_se(link_tmpfile(fd, tmp, d, /* flags= */ 0) >= 0);
346
347 assert_se(read_one_line_file(d, &line) >= 0);
348 ASSERT_STREQ(line, "foobar");
349
350 fd = safe_close(fd);
351 tmp = mfree(tmp);
352
353 fd = open_tmpfile_linkable(d, O_RDWR|O_CLOEXEC, &tmp);
354 assert_se(fd >= 0);
355
356 assert_se(write(fd, "waumiau\n", 8) == 8);
357
358 assert_se(link_tmpfile(fd, tmp, d, /* flags= */ 0) == -EEXIST);
359 assert_se(link_tmpfile(fd, tmp, d, LINK_TMPFILE_REPLACE) >= 0);
360
361 line = mfree(line);
362 assert_se(read_one_line_file(d, &line) >= 0);
363 ASSERT_STREQ(line, "waumiau");
364
365 assert_se(unlink(d) >= 0);
366 }
367
368 DEFINE_TEST_MAIN(LOG_DEBUG);