| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include "open-file.h" |
| 4 |
#include "string-util.h" |
| 5 |
#include "tests.h" |
| 6 |
|
| 7 |
TEST(open_file_parse) { |
| 8 |
_cleanup_(open_file_freep) OpenFile *of = NULL; |
| 9 |
int r; |
| 10 |
|
| 11 |
r = open_file_parse("/proc/1/ns/mnt:host-mount-namespace:read-only", &of); |
| 12 |
|
| 13 |
assert_se(r >= 0); |
| 14 |
ASSERT_STREQ(of->path, "/proc/1/ns/mnt"); |
| 15 |
ASSERT_STREQ(of->fdname, "host-mount-namespace"); |
| 16 |
assert_se(of->flags == OPENFILE_READ_ONLY); |
| 17 |
|
| 18 |
of = open_file_free(of); |
| 19 |
r = open_file_parse("/proc/1/ns/mnt", &of); |
| 20 |
|
| 21 |
assert_se(r >= 0); |
| 22 |
ASSERT_STREQ(of->path, "/proc/1/ns/mnt"); |
| 23 |
ASSERT_STREQ(of->fdname, "mnt"); |
| 24 |
assert_se(of->flags == 0); |
| 25 |
|
| 26 |
of = open_file_free(of); |
| 27 |
r = open_file_parse("/proc/1/ns/mnt:host-mount-namespace", &of); |
| 28 |
|
| 29 |
assert_se(r >= 0); |
| 30 |
ASSERT_STREQ(of->path, "/proc/1/ns/mnt"); |
| 31 |
ASSERT_STREQ(of->fdname, "host-mount-namespace"); |
| 32 |
assert_se(of->flags == 0); |
| 33 |
|
| 34 |
of = open_file_free(of); |
| 35 |
r = open_file_parse("/proc/1/ns/mnt::read-only", &of); |
| 36 |
|
| 37 |
assert_se(r >= 0); |
| 38 |
ASSERT_STREQ(of->path, "/proc/1/ns/mnt"); |
| 39 |
ASSERT_STREQ(of->fdname, "mnt"); |
| 40 |
assert_se(of->flags == OPENFILE_READ_ONLY); |
| 41 |
|
| 42 |
of = open_file_free(of); |
| 43 |
r = open_file_parse("../file.dat:file:read-only", &of); |
| 44 |
|
| 45 |
assert_se(r == -EINVAL); |
| 46 |
|
| 47 |
of = open_file_free(of); |
| 48 |
r = open_file_parse("/proc/1/ns/mnt:host-mount-namespace:rw", &of); |
| 49 |
|
| 50 |
assert_se(r == -EINVAL); |
| 51 |
|
| 52 |
of = open_file_free(of); |
| 53 |
r = open_file_parse("/proc/1/ns/mnt:host-mount-namespace:append", &of); |
| 54 |
|
| 55 |
assert_se(r >= 0); |
| 56 |
ASSERT_STREQ(of->path, "/proc/1/ns/mnt"); |
| 57 |
ASSERT_STREQ(of->fdname, "host-mount-namespace"); |
| 58 |
assert_se(of->flags == OPENFILE_APPEND); |
| 59 |
|
| 60 |
of = open_file_free(of); |
| 61 |
r = open_file_parse("/proc/1/ns/mnt:host-mount-namespace:truncate", &of); |
| 62 |
|
| 63 |
assert_se(r >= 0); |
| 64 |
ASSERT_STREQ(of->path, "/proc/1/ns/mnt"); |
| 65 |
ASSERT_STREQ(of->fdname, "host-mount-namespace"); |
| 66 |
assert_se(of->flags == OPENFILE_TRUNCATE); |
| 67 |
|
| 68 |
of = open_file_free(of); |
| 69 |
r = open_file_parse("/proc/1/ns/mnt:host-mount-namespace:read-only,append", &of); |
| 70 |
|
| 71 |
assert_se(r == -EINVAL); |
| 72 |
|
| 73 |
of = open_file_free(of); |
| 74 |
r = open_file_parse("/proc/1/ns/mnt:host-mount-namespace:read-only,truncate", &of); |
| 75 |
|
| 76 |
assert_se(r == -EINVAL); |
| 77 |
|
| 78 |
of = open_file_free(of); |
| 79 |
r = open_file_parse("/proc/1/ns/mnt:host-mount-namespace:append,truncate", &of); |
| 80 |
|
| 81 |
assert_se(r == -EINVAL); |
| 82 |
|
| 83 |
of = open_file_free(of); |
| 84 |
r = open_file_parse("/proc/1/ns/mnt:host-mount-namespace:read-only,read-only", &of); |
| 85 |
|
| 86 |
assert_se(r == -EINVAL); |
| 87 |
|
| 88 |
of = open_file_free(of); |
| 89 |
r = open_file_parse("/proc/1/ns/mnt:host-mount-namespace:graceful", &of); |
| 90 |
|
| 91 |
assert_se(r >= 0); |
| 92 |
ASSERT_STREQ(of->path, "/proc/1/ns/mnt"); |
| 93 |
ASSERT_STREQ(of->fdname, "host-mount-namespace"); |
| 94 |
assert_se(of->flags == OPENFILE_GRACEFUL); |
| 95 |
|
| 96 |
of = open_file_free(of); |
| 97 |
r = open_file_parse("/proc/1/ns/mnt:host-mount-namespace:read-only,graceful", &of); |
| 98 |
|
| 99 |
assert_se(r >= 0); |
| 100 |
ASSERT_STREQ(of->path, "/proc/1/ns/mnt"); |
| 101 |
ASSERT_STREQ(of->fdname, "host-mount-namespace"); |
| 102 |
assert_se(of->flags == (OPENFILE_READ_ONLY | OPENFILE_GRACEFUL)); |
| 103 |
|
| 104 |
of = open_file_free(of); |
| 105 |
r = open_file_parse("/proc/1/ns/mnt:host-mount-namespace:read-only:other", &of); |
| 106 |
|
| 107 |
assert_se(r == -EINVAL); |
| 108 |
} |
| 109 |
|
| 110 |
TEST(open_file_to_string) { |
| 111 |
_cleanup_free_ char *s = NULL; |
| 112 |
_cleanup_(open_file_freep) OpenFile *of = NULL; |
| 113 |
int r; |
| 114 |
|
| 115 |
assert_se(of = new (OpenFile, 1)); |
| 116 |
*of = (OpenFile){ .path = strdup("/proc/1/ns/mnt"), |
| 117 |
.fdname = strdup("host-mount-namespace"), |
| 118 |
.flags = OPENFILE_READ_ONLY }; |
| 119 |
|
| 120 |
r = open_file_to_string(of, &s); |
| 121 |
|
| 122 |
assert_se(r >= 0); |
| 123 |
ASSERT_STREQ(s, "/proc/1/ns/mnt:host-mount-namespace:read-only"); |
| 124 |
|
| 125 |
s = mfree(s); |
| 126 |
of->flags = OPENFILE_APPEND; |
| 127 |
|
| 128 |
r = open_file_to_string(of, &s); |
| 129 |
|
| 130 |
assert_se(r >= 0); |
| 131 |
ASSERT_STREQ(s, "/proc/1/ns/mnt:host-mount-namespace:append"); |
| 132 |
|
| 133 |
s = mfree(s); |
| 134 |
of->flags = OPENFILE_TRUNCATE; |
| 135 |
|
| 136 |
r = open_file_to_string(of, &s); |
| 137 |
|
| 138 |
assert_se(r >= 0); |
| 139 |
ASSERT_STREQ(s, "/proc/1/ns/mnt:host-mount-namespace:truncate"); |
| 140 |
|
| 141 |
s = mfree(s); |
| 142 |
of->flags = OPENFILE_GRACEFUL; |
| 143 |
|
| 144 |
r = open_file_to_string(of, &s); |
| 145 |
|
| 146 |
assert_se(r >= 0); |
| 147 |
ASSERT_STREQ(s, "/proc/1/ns/mnt:host-mount-namespace:graceful"); |
| 148 |
|
| 149 |
s = mfree(s); |
| 150 |
of->flags = OPENFILE_READ_ONLY | OPENFILE_GRACEFUL; |
| 151 |
|
| 152 |
r = open_file_to_string(of, &s); |
| 153 |
|
| 154 |
assert_se(r >= 0); |
| 155 |
ASSERT_STREQ(s, "/proc/1/ns/mnt:host-mount-namespace:read-only,graceful"); |
| 156 |
|
| 157 |
s = mfree(s); |
| 158 |
of->flags = 0; |
| 159 |
|
| 160 |
r = open_file_to_string(of, &s); |
| 161 |
|
| 162 |
assert_se(r >= 0); |
| 163 |
ASSERT_STREQ(s, "/proc/1/ns/mnt:host-mount-namespace"); |
| 164 |
|
| 165 |
s = mfree(s); |
| 166 |
assert_se(free_and_strdup(&of->fdname, "mnt")); |
| 167 |
of->flags = OPENFILE_READ_ONLY; |
| 168 |
|
| 169 |
r = open_file_to_string(of, &s); |
| 170 |
|
| 171 |
assert_se(r >= 0); |
| 172 |
ASSERT_STREQ(s, "/proc/1/ns/mnt::read-only"); |
| 173 |
|
| 174 |
s = mfree(s); |
| 175 |
ASSERT_OK(free_and_strdup(&of->path, "/path:with:colon")); |
| 176 |
ASSERT_OK(free_and_strdup(&of->fdname, "path:with:colon")); |
| 177 |
of->flags = 0; |
| 178 |
|
| 179 |
ASSERT_OK(open_file_to_string(of, &s)); |
| 180 |
ASSERT_STREQ(s, "/path\\x3awith\\x3acolon"); |
| 181 |
} |
| 182 |
|
| 183 |
DEFINE_TEST_MAIN(LOG_INFO); |