]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
test: mount unit with credential 34732/head
authordavjav <davjav@users.noreply.github.com>
Sun, 27 Oct 2024 20:42:57 +0000 (13:42 -0700)
committerMike Yuan <me@yhndnzj.com>
Tue, 10 Dec 2024 19:57:20 +0000 (20:57 +0100)
Verify mount unit credential file is present.

test/units/TEST-54-CREDS.sh

index 3a4fa654e9494e222f7a351058bd9d72fdcf3b87..82dd37aa4d6c9e9de3c5d80529bd25ff01ef2550 100755 (executable)
@@ -23,6 +23,49 @@ run_with_cred_compare() (
     diff "$log_file" <(echo -ne "$exp")
 )
 
+test_mount_with_credential() {
+    local credfile tmpdir unit
+    credfile="/tmp/mount-cred"
+    tmpdir="/tmp/test-54-mount"
+    unit=$(systemd-escape --suffix mount --path "$tmpdir")
+
+    echo foo >"$credfile"
+    mkdir -p "$tmpdir"
+
+    # Set up test mount unit
+    cat >/run/systemd/system/"$unit" <<EOF
+[Mount]
+What=tmpfs
+Where=$tmpdir
+Type=thisisatest
+LoadCredential=loadcred:$credfile
+EOF
+
+    # Set up test mount type
+    cat >/usr/sbin/mount.thisisatest <<EOF
+#!/usr/bin/env bash
+# Mount after verifying credential file content
+if [ \$(cat \${CREDENTIALS_DIRECTORY}/loadcred) = "foo" ]; then
+    mount -t tmpfs \$1 \$2
+fi
+EOF
+    chmod +x /usr/sbin/mount.thisisatest
+
+    # Verify mount succeeds
+    systemctl daemon-reload
+    systemctl start "$unit"
+    systemctl --no-pager show -p SubState --value "$unit" | grep -q mounted
+
+    # Verify mount fails with different credential file content
+    echo bar >"$credfile"
+    (! systemctl restart "$unit")
+
+    # Stop unit and delete files
+    systemctl stop "$unit"
+    rm -f "$credfile" /run/systemd/system/"$unit" /usr/sbin/mount.thisisatest
+    rm -rf "$tmpdir"
+}
+
 # Sanity checks
 #
 # Create a dummy "full" disk (similar to /dev/full) to check out-of-space
@@ -474,6 +517,9 @@ systemd-creds encrypt --user /tmp/usertest.data /tmp/usertest.creds --name=mytes
 systemctl start user@0.service
 XDG_RUNTIME_DIR=/run/user/0 systemd-run --pipe --user --unit=waldi.service -p LoadCredentialEncrypted=mytest:/tmp/usertest.creds cat /run/user/0/credentials/waldi.service/mytest | cmp /tmp/usertest.data
 
+# Test mount unit with credential
+test_mount_with_credential
+
 systemd-analyze log-level info
 
 touch /testok