summaryrefslogtreecommitdiff
path: root/src/test/test-tmpfile-util.c
blob: 5e5f60bdc850e15a85f55fe929076619b2b51f22 (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
/* SPDX-License-Identifier: LGPL-2.1-or-later */

#include "alloc-util.h"
#include "errno-util.h"
#include "log.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_errno(r, "%s+%s → %s vs. %s (%i/%m vs. %i/%s)", p, strna(extra), strna(s), strna(expect), r, ret, strerror_safe(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) {
        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", 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);
}

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_errno(r, "%s+%s → %s vs. %s (%i/%m vs. %i/%s)", p, strna(extra), strna(s), strna(expect), r, ret, strerror_safe(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) {
        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", 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);
}

DEFINE_TEST_MAIN(LOG_DEBUG);