1#!/usr/bin/env bash
2# SPDX-License-Identifier: LGPL-2.1-or-later
3set -eux
4set -o pipefail
5
6# Make sure that the "stat" output is not locale dependent.
7export LANG=C LC_ALL=C
8
9# first, create file without suid/sgid
10systemd-tmpfiles --create - <<EOF
11f     /tmp/xxx    0755 1 1 - -
12f     /tmp/yyy    0755 1 1 - -
13EOF
14
15test "$(stat -c %F:%u:%g:%a /tmp/xxx)" = "regular empty file:1:1:755"
16test "$(stat -c %F:%u:%g:%a /tmp/yyy)" = "regular empty file:1:1:755"
17
18# then, add suid/sgid
19systemd-tmpfiles --create - <<EOF
20f     /tmp/xxx    04755
21f     /tmp/yyy    02755
22EOF
23
24test "$(stat -c %F:%u:%g:%a /tmp/xxx)" = "regular empty file:1:1:4755"
25test "$(stat -c %F:%u:%g:%a /tmp/yyy)" = "regular empty file:1:1:2755"
26
27# then, chown the files to somebody else
28systemd-tmpfiles --create - <<EOF
29f     /tmp/xxx    - 2 2
30f     /tmp/yyy    - 2 2
31EOF
32
33test "$(stat -c %F:%u:%g:%a /tmp/xxx)" = "regular empty file:2:2:4755"
34test "$(stat -c %F:%u:%g:%a /tmp/yyy)" = "regular empty file:2:2:2755"
35
36# then, chown the files to a third user/group but also drop to a mask that has
37# both more and fewer bits set
38systemd-tmpfiles --create - <<EOF
39f     /tmp/xxx    0770 3 3
40f     /tmp/yyy    0770 3 3
41EOF
42
43test "$(stat -c %F:%u:%g:%a /tmp/xxx)" = "regular empty file:3:3:770"
44test "$(stat -c %F:%u:%g:%a /tmp/yyy)" = "regular empty file:3:3:770"
45
46# return to the beginning
47systemd-tmpfiles --create - <<EOF
48f     /tmp/xxx    0755 1 1 - -
49f     /tmp/yyy    0755 1 1 - -
50EOF
51
52test "$(stat -c %F:%u:%g:%a /tmp/xxx)" = "regular empty file:1:1:755"
53test "$(stat -c %F:%u:%g:%a /tmp/yyy)" = "regular empty file:1:1:755"
54
55# remove everything
56systemd-tmpfiles --remove - <<EOF
57r /tmp/xxx
58r /tmp/yyy
59EOF
60