richardphibel / rpms / systemd

Forked from rpms/systemd 2 years ago
Clone
594167
From 40806a5f552c8d223d1d7ff1878dcf57e4d817c5 Mon Sep 17 00:00:00 2001
594167
From: Yu Watanabe <watanabe.yu+github@gmail.com>
594167
Date: Wed, 1 Jun 2022 08:56:08 +0900
594167
Subject: [PATCH] test: import logind test from debian/ubuntu test suite
594167
594167
(cherry picked from commit 9c94ab0f6ff22da4278a6e9a93ddc480607c55ac)
594167
594167
Related: #2087652
594167
---
594167
 test/TEST-35-LOGIN/Makefile     |   1 +
594167
 test/TEST-35-LOGIN/test.sh      |  10 +
594167
 test/test-functions             |   8 +
594167
 test/units/testsuite-35.service |  10 +
594167
 test/units/testsuite-35.sh      | 379 ++++++++++++++++++++++++++++++++
594167
 5 files changed, 408 insertions(+)
594167
 create mode 120000 test/TEST-35-LOGIN/Makefile
594167
 create mode 100755 test/TEST-35-LOGIN/test.sh
594167
 create mode 100644 test/units/testsuite-35.service
594167
 create mode 100755 test/units/testsuite-35.sh
594167
594167
diff --git a/test/TEST-35-LOGIN/Makefile b/test/TEST-35-LOGIN/Makefile
594167
new file mode 120000
594167
index 0000000000..e9f93b1104
594167
--- /dev/null
594167
+++ b/test/TEST-35-LOGIN/Makefile
594167
@@ -0,0 +1 @@
594167
+../TEST-01-BASIC/Makefile
594167
\ No newline at end of file
594167
diff --git a/test/TEST-35-LOGIN/test.sh b/test/TEST-35-LOGIN/test.sh
594167
new file mode 100755
594167
index 0000000000..9762410fa3
594167
--- /dev/null
594167
+++ b/test/TEST-35-LOGIN/test.sh
594167
@@ -0,0 +1,10 @@
594167
+#!/usr/bin/env bash
594167
+# SPDX-License-Identifier: LGPL-2.1-or-later
594167
+set -e
594167
+
594167
+TEST_DESCRIPTION="LOGIN"
594167
+
594167
+# shellcheck source=test/test-functions
594167
+. "${TEST_BASE_DIR:?}/test-functions"
594167
+
594167
+do_test "$@"
594167
diff --git a/test/test-functions b/test/test-functions
594167
index 98efd047d7..d71e2a3328 100644
594167
--- a/test/test-functions
594167
+++ b/test/test-functions
594167
@@ -165,10 +165,12 @@ BASICTOOLS=(
594167
     mv
594167
     nc
594167
     nproc
594167
+    pkill
594167
     readlink
594167
     rev
594167
     rm
594167
     rmdir
594167
+    rmmod
594167
     sed
594167
     seq
594167
     setfattr
594167
@@ -193,6 +195,8 @@ BASICTOOLS=(
594167
     umount
594167
     uname
594167
     unshare
594167
+    useradd
594167
+    userdel
594167
     wc
594167
     xargs
594167
     xzcat
594167
@@ -921,6 +925,8 @@ install_modules() {
594167
     instmods vfat
594167
     instmods nls_ascii =nls
594167
     instmods dummy
594167
+    # for TEST-35-LOGIN
594167
+    instmods scsi_debug uinput
594167
 
594167
     if get_bool "$LOOKS_LIKE_SUSE"; then
594167
         instmods ext4
594167
@@ -1702,6 +1708,8 @@ install_basic_tools() {
594167
     image_install -o sushell
594167
     # in Debian ldconfig is just a shell script wrapper around ldconfig.real
594167
     image_install -o ldconfig.real
594167
+    # for TEST-35-LOGIN
594167
+    image_install -o evemu-device evemu-event
594167
 }
594167
 
594167
 install_debug_tools() {
594167
diff --git a/test/units/testsuite-35.service b/test/units/testsuite-35.service
594167
new file mode 100644
594167
index 0000000000..556a57a384
594167
--- /dev/null
594167
+++ b/test/units/testsuite-35.service
594167
@@ -0,0 +1,10 @@
594167
+# SPDX-License-Identifier: LGPL-2.1-or-later
594167
+[Unit]
594167
+Description=TEST-35-LOGIN
594167
+
594167
+[Service]
594167
+ExecStartPre=rm -f /failed /testok
594167
+ExecStart=/usr/lib/systemd/tests/testdata/units/%N.sh
594167
+StandardOutput=journal+console
594167
+StandardError=journal+console
594167
+Type=oneshot
594167
diff --git a/test/units/testsuite-35.sh b/test/units/testsuite-35.sh
594167
new file mode 100755
594167
index 0000000000..0a7198c3fe
594167
--- /dev/null
594167
+++ b/test/units/testsuite-35.sh
594167
@@ -0,0 +1,379 @@
594167
+#!/usr/bin/env bash
594167
+# SPDX-License-Identifier: LGPL-2.1-or-later
594167
+set -eux
594167
+set -o pipefail
594167
+
594167
+test_enable_debug() {
594167
+    mkdir -p /run/systemd/system/systemd-logind.service.d
594167
+    cat >/run/systemd/system/systemd-logind.service.d/debug.conf <
594167
+[Service]
594167
+Environment=SYSTEMD_LOG_LEVEL=debug
594167
+EOF
594167
+    systemctl daemon-reload
594167
+}
594167
+
594167
+test_properties() {
594167
+    mkdir -p /run/systemd/logind.conf.d
594167
+
594167
+    cat >/run/systemd/logind.conf.d/kill-user-processes.conf <
594167
+[Login]
594167
+KillUserProcesses=no
594167
+EOF
594167
+
594167
+    systemctl restart systemd-logind.service
594167
+    r=$(busctl get-property org.freedesktop.login1 /org/freedesktop/login1 org.freedesktop.login1.Manager KillUserProcesses)
594167
+    if [[ "$r" != "b false" ]]; then
594167
+        echo "Unexpected KillUserProcesses property '$r', expected='b false'" >&2
594167
+        exit 1
594167
+    fi
594167
+
594167
+    cat >/run/systemd/logind.conf.d/kill-user-processes.conf <
594167
+[Login]
594167
+KillUserProcesses=yes
594167
+EOF
594167
+
594167
+    systemctl restart systemd-logind.service
594167
+    r=$(busctl get-property org.freedesktop.login1 /org/freedesktop/login1 org.freedesktop.login1.Manager KillUserProcesses)
594167
+    if [[ "$r" != "b true" ]]; then
594167
+        echo "Unexpected KillUserProcesses property '$r', expected='b true'" >&2
594167
+        exit 1
594167
+    fi
594167
+
594167
+    rm -rf /run/systemd/logind.conf.d
594167
+}
594167
+
594167
+test_started() {
594167
+    systemctl restart systemd-logind.service
594167
+
594167
+    # should start at boot, not with D-BUS activation
594167
+    LOGINDPID=$(systemctl show systemd-logind.service -p ExecMainPID --value)
594167
+
594167
+    # loginctl should succeed
594167
+    loginctl --no-pager
594167
+}
594167
+
594167
+# args: <timeout>
594167
+wait_suspend() {
594167
+    timeout="$1"
594167
+    while [[ $timeout -gt 0 && ! -e /run/suspend.flag ]]; do
594167
+        sleep 1
594167
+        timeout=$((timeout - 1))
594167
+    done
594167
+    if [[ ! -e /run/suspend.flag ]]; then
594167
+        echo "closing lid did not cause suspend" >&2
594167
+        exit 1
594167
+    fi
594167
+    rm /run/suspend.flag
594167
+}
594167
+
594167
+test_suspend_tear_down() {
594167
+    set +e
594167
+
594167
+    kill "$KILL_PID"
594167
+}
594167
+
594167
+test_suspend_on_lid() {
594167
+    if systemd-detect-virt --quiet --container; then
594167
+        echo "Skipping suspend test in container"
594167
+        return
594167
+    fi
594167
+    if ! grep -s -q mem /sys/power/state; then
594167
+        echo "suspend not supported on this testbed, skipping"
594167
+        return
594167
+    fi
594167
+    if ! command -v evemu-device &>/dev/null; then
594167
+        echo "command evemu-device not found, skipping"
594167
+        return
594167
+    fi
594167
+    if ! command -v evemu-event &>/dev/null; then
594167
+        echo "command evemu-event not found, skipping"
594167
+        return
594167
+    fi
594167
+
594167
+    KILL_PID=
594167
+    trap test_suspend_tear_down EXIT
594167
+
594167
+    # create fake suspend
594167
+    mkdir -p /run/systemd/system/systemd-suspend.service.d
594167
+    cat >/run/systemd/system/systemd-suspend.service.d/override.conf <
594167
+[Service]
594167
+ExecStart=
594167
+ExecStart=touch /run/suspend.flag
594167
+EOF
594167
+    systemctl daemon-reload
594167
+
594167
+    # create fake lid switch
594167
+    mkdir -p /run/udev/rules.d
594167
+    cat >/run/udev/rules.d/70-logindtest-lid.rules <
594167
+SUBSYSTEM=="input", KERNEL=="event*", ATTRS{name}=="Fake Lid Switch", TAG+="power-switch"
594167
+EOF
594167
+    udevadm control --reload
594167
+
594167
+    cat >/run/lidswitch.evemu <
594167
+# EVEMU 1.2
594167
+# Input device name: "Lid Switch"
594167
+# Input device ID: bus 0x19 vendor 0000 product 0x05 version 0000
594167
+# Supported events:
594167
+#   Event type 0 (EV_SYN)
594167
+#     Event code 0 (SYN_REPORT)
594167
+#     Event code 5 (FF_STATUS_MAX)
594167
+#   Event type 5 (EV_SW)
594167
+#     Event code 0 (SW_LID)
594167
+# Properties:
594167
+N: Fake Lid Switch
594167
+I: 0019 0000 0005 0000
594167
+P: 00 00 00 00 00 00 00 00
594167
+B: 00 21 00 00 00 00 00 00 00
594167
+B: 01 00 00 00 00 00 00 00 00
594167
+B: 01 00 00 00 00 00 00 00 00
594167
+B: 01 00 00 00 00 00 00 00 00
594167
+B: 01 00 00 00 00 00 00 00 00
594167
+B: 01 00 00 00 00 00 00 00 00
594167
+B: 01 00 00 00 00 00 00 00 00
594167
+B: 01 00 00 00 00 00 00 00 00
594167
+B: 01 00 00 00 00 00 00 00 00
594167
+B: 01 00 00 00 00 00 00 00 00
594167
+B: 01 00 00 00 00 00 00 00 00
594167
+B: 01 00 00 00 00 00 00 00 00
594167
+B: 01 00 00 00 00 00 00 00 00
594167
+B: 02 00 00 00 00 00 00 00 00
594167
+B: 03 00 00 00 00 00 00 00 00
594167
+B: 04 00 00 00 00 00 00 00 00
594167
+B: 05 01 00 00 00 00 00 00 00
594167
+B: 11 00 00 00 00 00 00 00 00
594167
+B: 12 00 00 00 00 00 00 00 00
594167
+B: 15 00 00 00 00 00 00 00 00
594167
+B: 15 00 00 00 00 00 00 00 00
594167
+EOF
594167
+
594167
+    evemu-device /run/lidswitch.evemu &
594167
+    KILL_PID="$!"
594167
+
594167
+    for ((i=0;i<20;i++)); do
594167
+        if (( i != 0 )); then sleep .5; fi
594167
+
594167
+        INPUT_NAME=$(grep -l '^Fake Lid Switch' /sys/class/input/*/device/name || :)
594167
+        if [[ -n "$INPUT_NAME" ]]; then break; fi
594167
+    done
594167
+    if [[ -z "$INPUT_NAME" ]]; then
594167
+        echo "cannot find fake lid switch." >&2
594167
+        exit 1
594167
+    fi
594167
+    INPUT_NAME=${INPUT_NAME%/device/name}
594167
+    LID_DEV=/dev/${INPUT_NAME#/sys/class/}
594167
+    udevadm info --wait-for-initialization=10s "$LID_DEV"
594167
+    udevadm settle
594167
+
594167
+    # close lid
594167
+    evemu-event "$LID_DEV" --sync --type 5 --code 0 --value 1
594167
+    # need to wait for 30s suspend inhibition after boot
594167
+    wait_suspend 31
594167
+    # open lid again
594167
+    evemu-event "$LID_DEV" --sync --type 5 --code 0 --value 0
594167
+
594167
+    # waiting for 30s inhibition time between suspends
594167
+    sleep 30
594167
+
594167
+    # now closing lid should cause instant suspend
594167
+    evemu-event "$LID_DEV" --sync --type 5 --code 0 --value 1
594167
+    wait_suspend 2
594167
+    evemu-event "$LID_DEV" --sync --type 5 --code 0 --value 0
594167
+
594167
+    P=$(systemctl show systemd-logind.service -p ExecMainPID --value)
594167
+    if [[ "$P" != "$LOGINDPID" ]]; then
594167
+        echo "logind crashed" >&2
594167
+        exit 1
594167
+    fi
594167
+
594167
+    test_suspend_tear_down
594167
+    trap - EXIT
594167
+}
594167
+
594167
+test_shutdown() {
594167
+    # scheduled shutdown with wall message
594167
+    shutdown 2>&1
594167
+    sleep 5
594167
+    shutdown -c || :
594167
+    # logind should still be running
594167
+    P=$(systemctl show systemd-logind.service -p ExecMainPID --value)
594167
+    if [[ "$P" != "$LOGINDPID" ]]; then
594167
+        echo "logind crashed" >&2
594167
+        exit 1
594167
+    fi
594167
+
594167
+    # scheduled shutdown without wall message
594167
+    shutdown --no-wall 2>&1
594167
+    sleep 5
594167
+    shutdown -c --no-wall || true
594167
+    P=$(systemctl show systemd-logind.service -p ExecMainPID --value)
594167
+    if [[ "$P" != "$LOGINDPID" ]]; then
594167
+        echo "logind crashed" >&2
594167
+        exit 1
594167
+    fi
594167
+}
594167
+
594167
+test_session_tear_down() {
594167
+    set +e
594167
+
594167
+    rm -f /run/udev/rules.d/70-logindtest-scsi_debug-user.rules
594167
+    udevadm control --reload
594167
+
594167
+    systemctl stop getty@tty2.service
594167
+    rm -rf /run/systemd/system/getty@tty2.service.d
594167
+    systemctl daemon-reload
594167
+
594167
+    pkill -u logind-test-user
594167
+    userdel logind-test-user
594167
+
594167
+    rmmod scsi_debug
594167
+}
594167
+
594167
+check_session() {
594167
+    loginctl
594167
+    if [[ $(loginctl --no-legend | grep -c "logind-test-user") != 1 ]]; then
594167
+        echo "no session or multile sessions for logind-test-user." >&2
594167
+        return 1
594167
+    fi
594167
+
594167
+    SEAT=$(loginctl --no-legend | grep 'logind-test-user *seat' | awk '{ print $4 }')
594167
+    if [[ -z "$SEAT" ]]; then
594167
+        echo "no seat found for user logind-test-user" >&2
594167
+        return 1
594167
+    fi
594167
+
594167
+    SESSION=$(loginctl --no-legend | grep "logind-test-user" | awk '{ print $1 }')
594167
+    if [[ -z "$SESSION" ]]; then
594167
+        echo "no session found for user logind-test-user" >&2
594167
+        return 1
594167
+    fi
594167
+
594167
+    loginctl session-status "$SESSION"
594167
+    loginctl session-status "$SESSION" | grep -q "Unit: session-${SESSION}\.scope"
594167
+    LEADER_PID=$(loginctl session-status "$SESSION" | grep "Leader:" | awk '{ print $2 }')
594167
+    if [[ -z "$LEADER_PID" ]]; then
594167
+        echo "cannot found leader process for session $SESSION" >&2
594167
+        return 1
594167
+    fi
594167
+
594167
+    # cgroup v1: "1:name=systemd:/user.slice/..."; unified hierarchy: "0::/user.slice"
594167
+    if ! grep -q -E '(name=systemd|^0:):.*session.*scope' /proc/"$LEADER_PID"/cgroup; then
594167
+        echo "FAIL: process $LEADER_PID is not in the session cgroup" >&2
594167
+        cat /proc/self/cgroup
594167
+        return 1
594167
+    fi
594167
+}
594167
+
594167
+test_session() {
594167
+    if systemd-detect-virt --quiet --container; then
594167
+        echo " * Skipping ACL tests in container"
594167
+        return
594167
+    fi
594167
+
594167
+    trap test_session_tear_down EXIT
594167
+
594167
+    # add user
594167
+    useradd -s /bin/bash logind-test-user
594167
+
594167
+    # login with the test user to start a session
594167
+    mkdir -p /run/systemd/system/getty@tty2.service.d
594167
+    cat >/run/systemd/system/getty@tty2.service.d/override.conf <
594167
+[Service]
594167
+Type=simple
594167
+ExecStart=
594167
+ExecStart=-/sbin/agetty --autologin logind-test-user --noclear %I $TERM
594167
+EOF
594167
+    systemctl daemon-reload
594167
+    systemctl start getty@tty2.service
594167
+
594167
+    # check session
594167
+    ret=1
594167
+    for ((i=0;i<30;i++)); do
594167
+        if (( i != 0)); then sleep 1; fi
594167
+        if check_session; then
594167
+            ret=0
594167
+            break
594167
+        fi
594167
+    done
594167
+    if [[ "$ret" == "1" ]]; then
594167
+        exit 1
594167
+    fi
594167
+
594167
+    # scsi_debug should not be loaded yet
594167
+    if [[ -d /sys/bus/pseudo/drivers/scsi_debug ]]; then
594167
+        echo "scsi_debug module is already loaded." >&2
594167
+        exit 1
594167
+    fi
594167
+
594167
+    # we use scsi_debug to create new devices which we can put ACLs on
594167
+    # tell udev about the tagging, so that logind can pick it up
594167
+    mkdir -p /run/udev/rules.d
594167
+    cat >/run/udev/rules.d/70-logindtest-scsi_debug-user.rules <
594167
+SUBSYSTEM=="block", ATTRS{model}=="scsi_debug*", TAG+="uaccess"
594167
+EOF
594167
+    udevadm control --reload
594167
+
594167
+    # coldplug: logind started with existing device
594167
+    systemctl stop systemd-logind.service
594167
+    modprobe scsi_debug
594167
+    for ((i=0;i<30;i++)); do
594167
+        if (( i != 0)); then sleep 1; fi
594167
+        if dev=/dev/$(ls /sys/bus/pseudo/drivers/scsi_debug/adapter*/host*/target*/*:*/block 2>/dev/null); then
594167
+            break
594167
+        fi
594167
+    done
594167
+    if [[ ! -b "$dev" ]]; then
594167
+        echo "cannot find suitable scsi block device" >&2
594167
+        exit 1
594167
+    fi
594167
+    udevadm settle
594167
+    udevadm info "$dev"
594167
+
594167
+    # trigger logind and activate session
594167
+    loginctl activate "$SESSION"
594167
+
594167
+    # check ACL
594167
+    sleep 1
594167
+    if ! getfacl -p "$dev" | grep -q "user:logind-test-user:rw-"; then
594167
+        echo "$dev has no ACL for user logind-test-user" >&2
594167
+        getfacl -p "$dev" >&2
594167
+        exit 1
594167
+    fi
594167
+
594167
+    # hotplug: new device appears while logind is running
594167
+    rmmod scsi_debug
594167
+    modprobe scsi_debug
594167
+    for ((i=0;i<30;i++)); do
594167
+        if (( i != 0)); then sleep 1; fi
594167
+        if dev=/dev/$(ls /sys/bus/pseudo/drivers/scsi_debug/adapter*/host*/target*/*:*/block 2>/dev/null); then
594167
+            break
594167
+        fi
594167
+    done
594167
+    if [[ ! -b "$dev" ]]; then
594167
+        echo "cannot find suitable scsi block device" >&2
594167
+        exit 1
594167
+    fi
594167
+    udevadm settle
594167
+
594167
+    # check ACL
594167
+    sleep 1
594167
+    if ! getfacl -p "$dev" | grep -q "user:logind-test-user:rw-"; then
594167
+        echo "$dev has no ACL for user logind-test-user" >&2
594167
+        getfacl -p "$dev" >&2
594167
+        exit 1
594167
+    fi
594167
+
594167
+    test_session_tear_down
594167
+    trap - EXIT
594167
+}
594167
+
594167
+: >/failed
594167
+
594167
+test_enable_debug
594167
+test_properties
594167
+test_started
594167
+test_suspend_on_lid
594167
+test_shutdown
594167
+test_session
594167
+
594167
+touch /testok
594167
+rm /failed