summaryrefslogtreecommitdiffstats
path: root/src/test/test-dev-setup.c
blob: bdf562584c27fa0a69d707e9b6a59241578a4f03 (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
/* SPDX-License-Identifier: LGPL-2.1-or-later */

#include "capability-util.h"
#include "dev-setup.h"
#include "fs-util.h"
#include "mkdir.h"
#include "path-util.h"
#include "rm-rf.h"
#include "tests.h"
#include "tmpfile-util.h"

int main(int argc, char *argv[]) {
        _cleanup_(rm_rf_physical_and_freep) char *p = NULL;
        const char *f;
        struct stat st;

        test_setup_logging(LOG_DEBUG);

        if (have_effective_cap(CAP_DAC_OVERRIDE) <= 0)
                return log_tests_skipped("missing capability (CAP_DAC_OVERRIDE)");

        ASSERT_OK(mkdtemp_malloc("/tmp/test-dev-setupXXXXXX", &p));

        f = prefix_roota(p, "/run/systemd");
        ASSERT_OK(mkdir_p(f, 0755));

        ASSERT_OK(make_inaccessible_nodes(f, 1, 1));
        ASSERT_OK(make_inaccessible_nodes(f, 1, 1)); /* 2nd call should be a clean NOP */

        f = prefix_roota(p, "/run/systemd/inaccessible/reg");
        ASSERT_OK_ERRNO(stat(f, &st));
        ASSERT_TRUE(S_ISREG(st.st_mode));
        ASSERT_EQ((st.st_mode & 07777), 0000U);

        f = prefix_roota(p, "/run/systemd/inaccessible/dir");
        ASSERT_OK_ERRNO(stat(f, &st));
        ASSERT_TRUE(S_ISDIR(st.st_mode));
        ASSERT_EQ((st.st_mode & 07777), 0000U);

        f = prefix_roota(p, "/run/systemd/inaccessible/fifo");
        ASSERT_OK_ERRNO(stat(f, &st));
        ASSERT_TRUE(S_ISFIFO(st.st_mode));
        ASSERT_EQ((st.st_mode & 07777), 0000U);

        f = prefix_roota(p, "/run/systemd/inaccessible/sock");
        ASSERT_OK_ERRNO(stat(f, &st));
        ASSERT_TRUE(S_ISSOCK(st.st_mode));
        ASSERT_EQ((st.st_mode & 07777), 0000U);

        f = prefix_roota(p, "/run/systemd/inaccessible/chr");
        if (stat(f, &st) < 0)
                ASSERT_EQ(errno, ENOENT);
        else {
                ASSERT_TRUE(S_ISCHR(st.st_mode));
                ASSERT_EQ((st.st_mode & 07777), 0000U);
        }

        f = prefix_roota(p, "/run/systemd/inaccessible/blk");
        if (stat(f, &st) < 0)
                ASSERT_EQ(errno, ENOENT);
        else {
                ASSERT_TRUE(S_ISBLK(st.st_mode));
                ASSERT_EQ((st.st_mode & 07777), 0000U);
        }

        return EXIT_SUCCESS;
}