File: root-resolve.bats

package info (click to toggle)
rust-pathrs 0.2.1-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,912 kB
  • sloc: python: 1,138; sh: 371; ansic: 259; makefile: 151
file content (318 lines) | stat: -rw-r--r-- 10,044 bytes parent folder | download
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
#!/usr/bin/bats -t
# SPDX-License-Identifier: MPL-2.0
#
# libpathrs: safe path resolution on Linux
# Copyright (C) 2019-2025 Aleksa Sarai <cyphar@cyphar.com>
# Copyright (C) 2019-2025 SUSE LLC
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at https://mozilla.org/MPL/2.0/.

load helpers

function setup() {
	setup_tmpdirs
}

function teardown() {
	teardown_tmpdirs
}

# TODO: All of these tests (especially for --reopen) are very limited because
# we cannot verify anything useful about the opened files. Ideally we would
# instead output something simple like the fdinfo, stat, and/or contents to
# verify against.

@test "root resolve [file]" {
	ROOT="$(setup_tmpdir)"

	mkdir -p "$ROOT/etc"
	echo "dummy passwd" >"$ROOT/etc/passwd"
	ln -s /../../../../../../../../etc "$ROOT/bad-passwd"

	pathrs-cmd root --root "$ROOT" resolve /etc/passwd
	[ "$status" -eq 0 ]
	grep -Fx "HANDLE-PATH $ROOT/etc/passwd" <<<"$output"
	! grep '^FILE-PATH' <<<"$output"

	pathrs-cmd root --root "$ROOT" resolve bad-passwd/passwd
	[ "$status" -eq 0 ]
	grep -Fx "HANDLE-PATH $ROOT/etc/passwd" <<<"$output"
	! grep '^FILE-PATH' <<<"$output"
}

@test "root resolve --reopen O_RDONLY [file]" {
	ROOT="$(setup_tmpdir)"

	mkdir -p "$ROOT/etc"
	echo "dummy passwd" >"$ROOT/etc/passwd"
	ln -s /../../../../../../../../etc "$ROOT/bad-passwd"

	pathrs-cmd root --root "$ROOT" resolve --reopen O_RDONLY /etc/passwd
	[ "$status" -eq 0 ]
	grep -Fx "HANDLE-PATH $ROOT/etc/passwd" <<<"$output"
	grep -Fx "FILE-PATH $ROOT/etc/passwd" <<<"$output"

	pathrs-cmd root --root "$ROOT" resolve --reopen O_RDONLY bad-passwd/passwd
	[ "$status" -eq 0 ]
	grep -Fx "HANDLE-PATH $ROOT/etc/passwd" <<<"$output"
	grep -Fx "FILE-PATH $ROOT/etc/passwd" <<<"$output"
}

@test "root open --oflags O_RDONLY [file]" {
	ROOT="$(setup_tmpdir)"

	mkdir -p "$ROOT/etc"
	echo "dummy passwd" >"$ROOT/etc/passwd"
	ln -s /../../../../../../../../etc "$ROOT/bad-passwd"

	pathrs-cmd root --root "$ROOT" open --oflags O_RDONLY /etc/passwd
	[ "$status" -eq 0 ]
	! grep -Fx "HANDLE-PATH $ROOT/etc/passwd" <<<"$output"
	grep -Fx "FILE-PATH $ROOT/etc/passwd" <<<"$output"

	pathrs-cmd root --root "$ROOT" open --oflags O_RDONLY bad-passwd/passwd
	[ "$status" -eq 0 ]
	! grep -Fx "HANDLE-PATH $ROOT/etc/passwd" <<<"$output"
	grep -Fx "FILE-PATH $ROOT/etc/passwd" <<<"$output"
}

@test "root resolve --reopen O_DIRECTORY [file]" {
	ROOT="$(setup_tmpdir)"

	mkdir -p "$ROOT/etc"
	echo "dummy passwd" >"$ROOT/etc/passwd"

	pathrs-cmd root --root "$ROOT" resolve --reopen O_DIRECTORY /etc/passwd
	check-errno ENOTDIR
}

@test "root open --oflags O_DIRECTORY [file]" {
	ROOT="$(setup_tmpdir)"

	mkdir -p "$ROOT/etc"
	echo "dummy passwd" >"$ROOT/etc/passwd"

	pathrs-cmd root --root "$ROOT" open --oflags O_DIRECTORY /etc/passwd
	check-errno ENOTDIR
}

@test "root resolve --reopen O_RDWR|O_TRUNC [file]" {
	ROOT="$(setup_tmpdir)"

	echo "THIS SHOULD BE TRUNCATED" >"$ROOT/to-trunc"
	[ "$(stat -c '%s' "$ROOT/to-trunc")" -ne 0 ]

	pathrs-cmd root --root "$ROOT" resolve --reopen O_RDWR,O_TRUNC to-trunc
	[ "$status" -eq 0 ]
	grep -Fx "HANDLE-PATH $ROOT/to-trunc" <<<"$output"
	grep -Fx "FILE-PATH $ROOT/to-trunc" <<<"$output"
	# The file should've been truncated by O_TRUNC.
	[ "$(stat -c '%s' "$ROOT/to-trunc")" -eq 0 ]
}

@test "root open --oflags O_RDWR|O_TRUNC [file]" {
	ROOT="$(setup_tmpdir)"

	echo "THIS SHOULD BE TRUNCATED" >"$ROOT/to-trunc"
	[ "$(stat -c '%s' "$ROOT/to-trunc")" -ne 0 ]

	pathrs-cmd root --root "$ROOT" open --oflags O_RDWR,O_TRUNC to-trunc
	[ "$status" -eq 0 ]
	! grep -Fx "HANDLE-PATH $ROOT/to-trunc" <<<"$output"
	grep -Fx "FILE-PATH $ROOT/to-trunc" <<<"$output"
	# The file should've been truncated by O_TRUNC.
	[ "$(stat -c '%s' "$ROOT/to-trunc")" -eq 0 ]
}

@test "root resolve [directory]" {
	ROOT="$(setup_tmpdir)"

	mkdir -p "$ROOT/some/random/dir"

	pathrs-cmd root --root "$ROOT" resolve some/../some/./random/dir/..
	[ "$status" -eq 0 ]
	grep -Fx "HANDLE-PATH $ROOT/some/random" <<<"$output"
	! grep '^FILE-PATH' <<<"$output"
}

@test "root resolve --reopen [directory]" {
	ROOT="$(setup_tmpdir)"

	mkdir -p "$ROOT/some/random/dir"

	pathrs-cmd root --root "$ROOT" resolve --reopen O_DIRECTORY some/random/dir
	[ "$status" -eq 0 ]
	grep -Fx "HANDLE-PATH $ROOT/some/random/dir" <<<"$output"
	grep -Fx "FILE-PATH $ROOT/some/random/dir" <<<"$output"

	mkdir -p "$ROOT/some/random/dir"
	pathrs-cmd root --root "$ROOT" resolve --reopen O_WRONLY some
	check-errno EISDIR
}

@test "root open [directory]" {
	ROOT="$(setup_tmpdir)"

	mkdir -p "$ROOT/some/random/dir"

	pathrs-cmd root --root "$ROOT" open --oflags O_DIRECTORY some/random/dir
	[ "$status" -eq 0 ]
	! grep -Fx "HANDLE-PATH $ROOT/some/random/dir" <<<"$output"
	grep -Fx "FILE-PATH $ROOT/some/random/dir" <<<"$output"

	mkdir -p "$ROOT/some/random/dir"
	pathrs-cmd root --root "$ROOT" open --oflags O_WRONLY some
	check-errno EISDIR
}

@test "root resolve [device inode]" {
	requires can-mkwhiteout

	ROOT="$(setup_tmpdir)"
	mknod "$ROOT/char-0-0" c 0 0

	pathrs-cmd root --root "$ROOT" resolve char-0-0
	[ "$status" -eq 0 ]
	grep -Fx "HANDLE-PATH $ROOT/char-0-0" <<<"$output"
	! grep '^FILE-PATH' <<<"$output"
}

@test "root resolve [/dev/full]" {
	pathrs-cmd root --root /dev resolve full
	[ "$status" -eq 0 ]
	grep -Fx 'HANDLE-PATH /dev/full' <<<"$output"
	! grep '^FILE-PATH' <<<"$output"
}

@test "root resolve [fifo]" {
	ROOT="$(setup_tmpdir)"
	mkfifo "$ROOT/fifo"

	pathrs-cmd root --root "$ROOT" resolve fifo
	[ "$status" -eq 0 ]
	grep -Fx "HANDLE-PATH $ROOT/fifo" <<<"$output"
	! grep '^FILE-PATH' <<<"$output"
}

@test "root resolve --follow [symlinks]" {
	ROOT="$(setup_tmpdir)"

	mkdir -p "$ROOT/target/dir"
	echo "TARGET" >"$ROOT/target/dir/file"
	[ "$(stat -c '%s' "$ROOT/target/dir/file")" -ne 0 ]

	ln -s /target/dir "$ROOT/a"
	ln -s /a/../dir "$ROOT/b"
	ln -s ../b/. "$ROOT/c"
	ln -s /../../../../c "$ROOT/d"
	ln -s d "$ROOT/e"
	ln -s e/file "$ROOT/file-link"

	pathrs-cmd root --root "$ROOT" resolve --follow "target/dir"
	[ "$status" -eq 0 ]
	grep -Fx "HANDLE-PATH $ROOT/target/dir" <<<"$output"
	! grep '^FILE-PATH' <<<"$output"
	pathrs-cmd root --root "$ROOT" resolve --follow "a"
	[ "$status" -eq 0 ]
	grep -Fx "HANDLE-PATH $ROOT/target/dir" <<<"$output"
	! grep '^FILE-PATH' <<<"$output"
	pathrs-cmd root --root "$ROOT" resolve --follow "b"
	[ "$status" -eq 0 ]
	grep -Fx "HANDLE-PATH $ROOT/target/dir" <<<"$output"
	! grep '^FILE-PATH' <<<"$output"
	pathrs-cmd root --root "$ROOT" resolve --follow "c"
	[ "$status" -eq 0 ]
	grep -Fx "HANDLE-PATH $ROOT/target/dir" <<<"$output"
	! grep '^FILE-PATH' <<<"$output"
	pathrs-cmd root --root "$ROOT" resolve --follow "d"
	[ "$status" -eq 0 ]
	grep -Fx "HANDLE-PATH $ROOT/target/dir" <<<"$output"
	! grep '^FILE-PATH' <<<"$output"
	pathrs-cmd root --root "$ROOT" resolve --follow "e"
	[ "$status" -eq 0 ]
	grep -Fx "HANDLE-PATH $ROOT/target/dir" <<<"$output"
	! grep '^FILE-PATH' <<<"$output"

	pathrs-cmd root --root "$ROOT" resolve --follow --reopen O_DIRECTORY "e"
	[ "$status" -eq 0 ]
	grep -Fx "HANDLE-PATH $ROOT/target/dir" <<<"$output"
	grep -Fx "FILE-PATH $ROOT/target/dir" <<<"$output"

	pathrs-cmd root --root "$ROOT" resolve --follow "file-link"
	[ "$status" -eq 0 ]
	grep -Fx "HANDLE-PATH $ROOT/target/dir/file" <<<"$output"
	! grep '^FILE-PATH' <<<"$output"

	pathrs-cmd root --root "$ROOT" resolve --follow --reopen O_WRONLY,O_TRUNC "file-link"
	[ "$status" -eq 0 ]
	grep -Fx "HANDLE-PATH $ROOT/target/dir/file" <<<"$output"
	grep -Fx "FILE-PATH $ROOT/target/dir/file" <<<"$output"
	# The file should've been truncated by O_TRUNC.
	[ "$(stat -c '%s' "$ROOT/target/dir/file")" -eq 0 ]
}

@test "root resolve --no-follow [symlinks]" {
	ROOT="$(setup_tmpdir)"

	mkdir -p "$ROOT/target/dir"
	echo "TARGET" >"$ROOT/target/dir/file"
	[ "$(stat -c '%s' "$ROOT/target/dir/file")" -ne 0 ]

	ln -s /target/dir "$ROOT/a"
	ln -s /a/../dir "$ROOT/b"
	ln -s ../b/. "$ROOT/c"
	ln -s /../../../../c "$ROOT/d"
	ln -s d "$ROOT/e"
	ln -s e/file "$ROOT/file-link"

	pathrs-cmd root --root "$ROOT" resolve --follow "target/dir"
	[ "$status" -eq 0 ]
	grep -Fx "HANDLE-PATH $ROOT/target/dir" <<<"$output"
	! grep '^FILE-PATH' <<<"$output"
	pathrs-cmd root --root "$ROOT" resolve --no-follow "a"
	[ "$status" -eq 0 ]
	grep -Fx "HANDLE-PATH $ROOT/a" <<<"$output"
	! grep '^FILE-PATH' <<<"$output"
	pathrs-cmd root --root "$ROOT" resolve --no-follow "b"
	[ "$status" -eq 0 ]
	grep -Fx "HANDLE-PATH $ROOT/b" <<<"$output"
	! grep '^FILE-PATH' <<<"$output"
	pathrs-cmd root --root "$ROOT" resolve --no-follow "c"
	[ "$status" -eq 0 ]
	grep -Fx "HANDLE-PATH $ROOT/c" <<<"$output"
	! grep '^FILE-PATH' <<<"$output"
	pathrs-cmd root --root "$ROOT" resolve --no-follow "d"
	[ "$status" -eq 0 ]
	grep -Fx "HANDLE-PATH $ROOT/d" <<<"$output"
	! grep '^FILE-PATH' <<<"$output"
	pathrs-cmd root --root "$ROOT" resolve --no-follow "e"
	[ "$status" -eq 0 ]
	grep -Fx "HANDLE-PATH $ROOT/e" <<<"$output"
	! grep '^FILE-PATH' <<<"$output"

	# You cannot reopen an O_PATH|O_NOFOLLOW handle to a symlink.
	pathrs-cmd root --root "$ROOT" resolve --no-follow --reopen O_RDONLY "e"
	check-errno ELOOP
	pathrs-cmd root --root "$ROOT" resolve --no-follow --reopen O_DIRECTORY "e"
	check-errno ELOOP

	pathrs-cmd root --root "$ROOT" resolve --no-follow "file-link"
	[ "$status" -eq 0 ]
	grep -Fx "HANDLE-PATH $ROOT/file-link" <<<"$output"
	! grep '^FILE-PATH' <<<"$output"

	pathrs-cmd root --root "$ROOT" resolve --no-follow --reopen O_WRONLY,O_TRUNC "file-link"
	check-errno ELOOP
	# The file should NOT have been truncated by O_TRUNC.
	[ "$(stat -c '%s' "$ROOT/target/dir/file")" -ne 0 ]

	# --no-follow has no impact on non-final components.
	pathrs-cmd root --root "$ROOT" resolve --no-follow --reopen O_WRONLY,O_TRUNC "e/file"
	[ "$status" -eq 0 ]
	grep -Fx "HANDLE-PATH $ROOT/target/dir/file" <<<"$output"
	grep -Fx "FILE-PATH $ROOT/target/dir/file" <<<"$output"
	# The file should've been truncated by O_TRUNC.
	[ "$(stat -c '%s' "$ROOT/target/dir/file")" -eq 0 ]
}