summaryrefslogtreecommitdiffstats
path: root/src/test/test-open-file.c
blob: 1b938ec5f73df2398cec9b9dd8cdcaa9a56f724c (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
/* SPDX-License-Identifier: LGPL-2.1-or-later */

#include "open-file.h"
#include "string-util.h"
#include "tests.h"

TEST(open_file_parse) {
        _cleanup_(open_file_freep) OpenFile *of = NULL;
        int r;

        r = open_file_parse("/proc/1/ns/mnt:host-mount-namespace:read-only", &of);

        assert_se(r >= 0);
        assert_se(streq(of->path, "/proc/1/ns/mnt"));
        assert_se(streq(of->fdname, "host-mount-namespace"));
        assert_se(of->flags == OPENFILE_READ_ONLY);

        of = open_file_free(of);
        r = open_file_parse("/proc/1/ns/mnt", &of);

        assert_se(r >= 0);
        assert_se(streq(of->path, "/proc/1/ns/mnt"));
        assert_se(streq(of->fdname, "mnt"));
        assert_se(of->flags == 0);

        of = open_file_free(of);
        r = open_file_parse("/proc/1/ns/mnt:host-mount-namespace", &of);

        assert_se(r >= 0);
        assert_se(streq(of->path, "/proc/1/ns/mnt"));
        assert_se(streq(of->fdname, "host-mount-namespace"));
        assert_se(of->flags == 0);

        of = open_file_free(of);
        r = open_file_parse("/proc/1/ns/mnt::read-only", &of);

        assert_se(r >= 0);
        assert_se(streq(of->path, "/proc/1/ns/mnt"));
        assert_se(streq(of->fdname, "mnt"));
        assert_se(of->flags == OPENFILE_READ_ONLY);

        of = open_file_free(of);
        r = open_file_parse("../file.dat:file:read-only", &of);

        assert_se(r == -EINVAL);

        of = open_file_free(of);
        r = open_file_parse("/proc/1/ns/mnt:host-mount-namespace:rw", &of);

        assert_se(r == -EINVAL);

        of = open_file_free(of);
        r = open_file_parse("/proc/1/ns/mnt:host-mount-namespace:append", &of);

        assert_se(r >= 0);
        assert_se(streq(of->path, "/proc/1/ns/mnt"));
        assert_se(streq(of->fdname, "host-mount-namespace"));
        assert_se(of->flags == OPENFILE_APPEND);

        of = open_file_free(of);
        r = open_file_parse("/proc/1/ns/mnt:host-mount-namespace:truncate", &of);

        assert_se(r >= 0);
        assert_se(streq(of->path, "/proc/1/ns/mnt"));
        assert_se(streq(of->fdname, "host-mount-namespace"));
        assert_se(of->flags == OPENFILE_TRUNCATE);

        of = open_file_free(of);
        r = open_file_parse("/proc/1/ns/mnt:host-mount-namespace:read-only,append", &of);

        assert_se(r == -EINVAL);

        of = open_file_free(of);
        r = open_file_parse("/proc/1/ns/mnt:host-mount-namespace:read-only,truncate", &of);

        assert_se(r == -EINVAL);

        of = open_file_free(of);
        r = open_file_parse("/proc/1/ns/mnt:host-mount-namespace:append,truncate", &of);

        assert_se(r == -EINVAL);

        of = open_file_free(of);
        r = open_file_parse("/proc/1/ns/mnt:host-mount-namespace:read-only,read-only", &of);

        assert_se(r == -EINVAL);

        of = open_file_free(of);
        r = open_file_parse("/proc/1/ns/mnt:host-mount-namespace:graceful", &of);

        assert_se(r >= 0);
        assert_se(streq(of->path, "/proc/1/ns/mnt"));
        assert_se(streq(of->fdname, "host-mount-namespace"));
        assert_se(of->flags == OPENFILE_GRACEFUL);

        of = open_file_free(of);
        r = open_file_parse("/proc/1/ns/mnt:host-mount-namespace:read-only,graceful", &of);

        assert_se(r >= 0);
        assert_se(streq(of->path, "/proc/1/ns/mnt"));
        assert_se(streq(of->fdname, "host-mount-namespace"));
        assert_se(of->flags == (OPENFILE_READ_ONLY | OPENFILE_GRACEFUL));

        of = open_file_free(of);
        r = open_file_parse("/proc/1/ns/mnt:host-mount-namespace:read-only:other", &of);

        assert_se(r == -EINVAL);
}

TEST(open_file_to_string) {
        _cleanup_free_ char *s = NULL;
        _cleanup_(open_file_freep) OpenFile *of = NULL;
        int r;

        assert_se(of = new (OpenFile, 1));
        *of = (OpenFile){ .path = strdup("/proc/1/ns/mnt"),
                          .fdname = strdup("host-mount-namespace"),
                          .flags = OPENFILE_READ_ONLY };

        r = open_file_to_string(of, &s);

        assert_se(r >= 0);
        assert_se(streq(s, "/proc/1/ns/mnt:host-mount-namespace:read-only"));

        s = mfree(s);
        of->flags = OPENFILE_APPEND;

        r = open_file_to_string(of, &s);

        assert_se(r >= 0);
        assert_se(streq(s, "/proc/1/ns/mnt:host-mount-namespace:append"));

        s = mfree(s);
        of->flags = OPENFILE_TRUNCATE;

        r = open_file_to_string(of, &s);

        assert_se(r >= 0);
        assert_se(streq(s, "/proc/1/ns/mnt:host-mount-namespace:truncate"));

        s = mfree(s);
        of->flags = OPENFILE_GRACEFUL;

        r = open_file_to_string(of, &s);

        assert_se(r >= 0);
        assert_se(streq(s, "/proc/1/ns/mnt:host-mount-namespace:graceful"));

        s = mfree(s);
        of->flags = OPENFILE_READ_ONLY | OPENFILE_GRACEFUL;

        r = open_file_to_string(of, &s);

        assert_se(r >= 0);
        assert_se(streq(s, "/proc/1/ns/mnt:host-mount-namespace:read-only,graceful"));

        s = mfree(s);
        of->flags = 0;

        r = open_file_to_string(of, &s);

        assert_se(r >= 0);
        assert_se(streq(s, "/proc/1/ns/mnt:host-mount-namespace"));

        s = mfree(s);
        assert_se(free_and_strdup(&of->fdname, "mnt"));
        of->flags = OPENFILE_READ_ONLY;

        r = open_file_to_string(of, &s);

        assert_se(r >= 0);
        assert_se(streq(s, "/proc/1/ns/mnt::read-only"));

        s = mfree(s);
        assert_se(free_and_strdup(&of->path, "/path:with:colon"));
        assert_se(free_and_strdup(&of->fdname, "path:with:colon"));
        of->flags = 0;

        r = open_file_to_string(of, &s);

        assert_se(r >= 0);
        assert_se(streq(s, "/path\\:with\\:colon"));
}

DEFINE_TEST_MAIN(LOG_INFO);