]> git.proxmox.com Git - systemd.git/blame - test/units/testsuite-22.03.sh
bump version to 252.11-pve1
[systemd.git] / test / units / testsuite-22.03.sh
CommitLineData
8b3d4ff0 1#!/bin/bash
ea0999c9 2# SPDX-License-Identifier: LGPL-2.1-or-later
6e866b33
MB
3#
4# Basic tests for types creating/writing files
8b3d4ff0
MB
5set -eux
6set -o pipefail
6e866b33
MB
7
8rm -fr /tmp/{f,F,w}
9mkdir /tmp/{f,F,w}
10touch /tmp/file-owned-by-root
11
12#
13# 'f'
14#
15systemd-tmpfiles --create - <<EOF
16f /tmp/f/1 0644 - - - -
17f /tmp/f/2 0644 - - - This string should be written
18EOF
19
20### '1' should exist and be empty
8b3d4ff0
MB
21test -f /tmp/f/1; test ! -s /tmp/f/1
22test "$(stat -c %U:%G:%a /tmp/f/1)" = "root:root:644"
6e866b33 23
8b3d4ff0 24test "$(stat -c %U:%G:%a /tmp/f/2)" = "root:root:644"
6e866b33
MB
25test "$(< /tmp/f/2)" = "This string should be written"
26
27### The perms are supposed to be updated even if the file already exists.
28systemd-tmpfiles --create - <<EOF
29f /tmp/f/1 0666 daemon daemon - This string should not be written
30EOF
31
32# file should be empty
8b3d4ff0
MB
33test ! -s /tmp/f/1
34test "$(stat -c %U:%G:%a /tmp/f/1)" = "daemon:daemon:666"
6e866b33
MB
35
36### But we shouldn't try to set perms on an existing file which is not a
37### regular one.
38mkfifo /tmp/f/fifo
39chmod 644 /tmp/f/fifo
40
ecfb185f 41(! systemd-tmpfiles --create -) <<EOF
6e866b33
MB
42f /tmp/f/fifo 0666 daemon daemon - This string should not be written
43EOF
44
45test -p /tmp/f/fifo
8b3d4ff0 46test "$(stat -c %U:%G:%a /tmp/f/fifo)" = "root:root:644"
6e866b33
MB
47
48### 'f' should not follow symlinks.
49ln -s missing /tmp/f/dangling
50ln -s /tmp/file-owned-by-root /tmp/f/symlink
51
ecfb185f 52(! systemd-tmpfiles --create -) <<EOF
6e866b33
MB
53f /tmp/f/dangling 0644 daemon daemon - -
54f /tmp/f/symlink 0644 daemon daemon - -
55EOF
8b3d4ff0
MB
56test ! -e /tmp/f/missing
57test "$(stat -c %U:%G:%a /tmp/file-owned-by-root)" = "root:root:644"
6e866b33
MB
58
59### Handle read-only filesystem gracefully: we shouldn't fail if the target
60### already exists and have the correct perms.
61mkdir /tmp/f/rw-fs
62mkdir /tmp/f/ro-fs
63
64touch /tmp/f/rw-fs/foo
65chmod 644 /tmp/f/rw-fs/foo
66
67mount -o bind,ro /tmp/f/rw-fs /tmp/f/ro-fs
68
69systemd-tmpfiles --create - <<EOF
70f /tmp/f/ro-fs/foo 0644 - - - - This string should not be written
71EOF
8b3d4ff0 72test -f /tmp/f/ro-fs/foo; test ! -s /tmp/f/ro-fs/foo
6e866b33 73
ecfb185f 74(! systemd-tmpfiles --create -) <<EOF
6e866b33
MB
75f /tmp/f/ro-fs/foo 0666 - - - -
76EOF
8b3d4ff0 77test "$(stat -c %U:%G:%a /tmp/f/fifo)" = "root:root:644"
6e866b33 78
ecfb185f 79(! systemd-tmpfiles --create -) <<EOF
6e866b33
MB
80f /tmp/f/ro-fs/bar 0644 - - - -
81EOF
8b3d4ff0 82test ! -e /tmp/f/ro-fs/bar
6e866b33
MB
83
84### 'f' shouldn't follow unsafe paths.
85mkdir /tmp/f/daemon
86ln -s /root /tmp/f/daemon/unsafe-symlink
87chown -R --no-dereference daemon:daemon /tmp/f/daemon
88
ecfb185f 89(! systemd-tmpfiles --create -) <<EOF
6e866b33
MB
90f /tmp/f/daemon/unsafe-symlink/exploit 0644 daemon daemon - -
91EOF
8b3d4ff0 92test ! -e /tmp/f/daemon/unsafe-symlink/exploit
6e866b33
MB
93
94#
95# 'F'
96#
97echo "This should be truncated" >/tmp/F/truncated
98echo "This should be truncated" >/tmp/F/truncated-with-content
99
100systemd-tmpfiles --create - <<EOF
101F /tmp/F/created 0644 - - - -
102F /tmp/F/created-with-content 0644 - - - new content
103F /tmp/F/truncated 0666 daemon daemon - -
104F /tmp/F/truncated-with-content 0666 daemon daemon - new content
105EOF
106
8b3d4ff0 107test -f /tmp/F/created; test ! -s /tmp/F/created
6e866b33
MB
108test -f /tmp/F/created-with-content
109test "$(< /tmp/F/created-with-content)" = "new content"
8b3d4ff0
MB
110test -f /tmp/F/truncated; test ! -s /tmp/F/truncated
111test "$(stat -c %U:%G:%a /tmp/F/truncated)" = "daemon:daemon:666"
6e866b33 112test -s /tmp/F/truncated-with-content
8b3d4ff0 113test "$(stat -c %U:%G:%a /tmp/F/truncated-with-content)" = "daemon:daemon:666"
6e866b33
MB
114
115### We shouldn't try to truncate anything but regular files since the behavior is
116### unspecified in the other cases.
117mkfifo /tmp/F/fifo
118
ecfb185f 119(! systemd-tmpfiles --create -) <<EOF
6e866b33
MB
120F /tmp/F/fifo 0644 - - - -
121EOF
122
123test -p /tmp/F/fifo
124
125### 'F' should not follow symlinks.
126ln -s missing /tmp/F/dangling
127ln -s /tmp/file-owned-by-root /tmp/F/symlink
128
ecfb185f 129(! systemd-tmpfiles --create -) <<EOF
6e866b33
MB
130f /tmp/F/dangling 0644 daemon daemon - -
131f /tmp/F/symlink 0644 daemon daemon - -
132EOF
8b3d4ff0
MB
133test ! -e /tmp/F/missing
134test "$(stat -c %U:%G:%a /tmp/file-owned-by-root)" = "root:root:644"
6e866b33
MB
135
136### Handle read-only filesystem gracefully: we shouldn't fail if the target
137### already exists and is empty.
138mkdir /tmp/F/rw-fs
139mkdir /tmp/F/ro-fs
140
141touch /tmp/F/rw-fs/foo
142chmod 644 /tmp/F/rw-fs/foo
143
144mount -o bind,ro /tmp/F/rw-fs /tmp/F/ro-fs
145
146systemd-tmpfiles --create - <<EOF
147F /tmp/F/ro-fs/foo 0644 - - - -
148EOF
8b3d4ff0 149test -f /tmp/F/ro-fs/foo; test ! -s /tmp/F/ro-fs/foo
6e866b33
MB
150
151echo "truncating is not allowed anymore" >/tmp/F/rw-fs/foo
ecfb185f 152(! systemd-tmpfiles --create -) <<EOF
6e866b33
MB
153F /tmp/F/ro-fs/foo 0644 - - - -
154EOF
155
ecfb185f 156(! systemd-tmpfiles --create -) <<EOF
6e866b33
MB
157F /tmp/F/ro-fs/foo 0644 - - - - This string should not be written
158EOF
8b3d4ff0
MB
159test -f /tmp/F/ro-fs/foo
160grep -q 'truncating is not allowed' /tmp/F/ro-fs/foo
6e866b33
MB
161
162# Trying to change the perms should fail.
8b3d4ff0 163: >/tmp/F/rw-fs/foo
ecfb185f 164(! systemd-tmpfiles --create -) <<EOF
6e866b33
MB
165F /tmp/F/ro-fs/foo 0666 - - - -
166EOF
8b3d4ff0 167test "$(stat -c %U:%G:%a /tmp/F/ro-fs/foo)" = "root:root:644"
6e866b33
MB
168
169### Try to create a new file.
ecfb185f 170(! systemd-tmpfiles --create -) <<EOF
6e866b33
MB
171F /tmp/F/ro-fs/bar 0644 - - - -
172EOF
8b3d4ff0 173test ! -e /tmp/F/ro-fs/bar
6e866b33
MB
174
175### 'F' shouldn't follow unsafe paths.
176mkdir /tmp/F/daemon
177ln -s /root /tmp/F/daemon/unsafe-symlink
178chown -R --no-dereference daemon:daemon /tmp/F/daemon
179
ecfb185f 180(! systemd-tmpfiles --create -) <<EOF
6e866b33
MB
181F /tmp/F/daemon/unsafe-symlink/exploit 0644 daemon daemon - -
182EOF
8b3d4ff0 183test ! -e /tmp/F/daemon/unsafe-symlink/exploit
6e866b33
MB
184
185#
186# 'w'
187#
188touch /tmp/w/overwritten
8f232108 189touch /tmp/w/appended
6e866b33
MB
190
191### nop if the target does not exist.
192systemd-tmpfiles --create - <<EOF
193w /tmp/w/unexistent 0644 - - - new content
194EOF
8b3d4ff0 195test ! -e /tmp/w/unexistent
6e866b33
MB
196
197### no argument given -> fails.
ecfb185f 198(! systemd-tmpfiles --create -) <<EOF
6e866b33
MB
199w /tmp/w/unexistent 0644 - - - -
200EOF
201
202### write into an empty file.
203systemd-tmpfiles --create - <<EOF
204w /tmp/w/overwritten 0644 - - - old content
205EOF
206test -f /tmp/w/overwritten
207test "$(< /tmp/w/overwritten)" = "old content"
208
8f232108 209### old content is overwritten
6e866b33
MB
210systemd-tmpfiles --create - <<EOF
211w /tmp/w/overwritten 0644 - - - new content
212EOF
213test -f /tmp/w/overwritten
214test "$(< /tmp/w/overwritten)" = "new content"
215
8f232108
MB
216### append lines
217systemd-tmpfiles --create - <<EOF
218w+ /tmp/w/appended 0644 - - - 1
219w+ /tmp/w/appended 0644 - - - 2\n
220w+ /tmp/w/appended 0644 - - - 3
221EOF
222test -f /tmp/w/appended
223test "$(< /tmp/w/appended)" = "$(echo -ne '12\n3')"
224
f2dec872 225### writing into an 'exotic' file should be allowed.
6e866b33
MB
226systemd-tmpfiles --create - <<EOF
227w /dev/null - - - - new content
228EOF
229
230### 'w' follows symlinks
231ln -s ./overwritten /tmp/w/symlink
232systemd-tmpfiles --create - <<EOF
233w /tmp/w/symlink - - - - $(readlink -e /tmp/w/symlink)
234EOF
235readlink -e /tmp/w/symlink
236test "$(< /tmp/w/overwritten)" = "/tmp/w/overwritten"
237
238### 'w' shouldn't follow unsafe paths.
239mkdir /tmp/w/daemon
240ln -s /root /tmp/w/daemon/unsafe-symlink
241chown -R --no-dereference daemon:daemon /tmp/w/daemon
242
ecfb185f 243(! systemd-tmpfiles --create -) <<EOF
6e866b33
MB
244f /tmp/w/daemon/unsafe-symlink/exploit 0644 daemon daemon - -
245EOF
8b3d4ff0 246test ! -e /tmp/w/daemon/unsafe-symlink/exploit