From 638c2418e705410344e07e77f944530df0f4608f Mon Sep 17 00:00:00 2001
From: Michal Sekletar <msekleta@redhat.com>
Date: Fri, 1 Jul 2022 12:59:57 +0200
Subject: [PATCH] tests: verify that Lock D-Bus signal is sent when
 IdleAction=lock

(cherry picked from commit 181656fc0faa885d69bc34822b8e9b5de3fdf6bf)

Related: #1866955
---
 test/TEST-35-LOGIN/test.sh      | 11 +++-
 test/TEST-35-LOGIN/testsuite.sh | 93 +++++++++++++++++++++++++++++++++
 2 files changed, 103 insertions(+), 1 deletion(-)

diff --git a/test/TEST-35-LOGIN/test.sh b/test/TEST-35-LOGIN/test.sh
index 32410c8149..f83afcff49 100755
--- a/test/TEST-35-LOGIN/test.sh
+++ b/test/TEST-35-LOGIN/test.sh
@@ -14,14 +14,23 @@ test_setup() {
 
     (
         LOG_LEVEL=5
-        eval $(udevadm info --export --query=env --name="${LOOPDEV}p2")
+        eval "$(udevadm info --export --query=env --name="${LOOPDEV}p2")"
 
         setup_basic_environment
 
+        inst_binary awk
         inst_binary pkill
         inst_binary useradd
         inst_binary userdel
 
+        if command -v expect >/dev/null && command -v tclsh >/dev/null ; then
+                # shellcheck disable=SC2016
+                version="$(tclsh <<< 'puts $tcl_version')"
+
+                dracut_install expect
+                inst_recursive /usr/lib64/tcl"$version" /usr/share/tcl"$version"
+        fi
+
         # mask some services that we do not want to run in these tests
         ln -fs /dev/null "$initdir"/etc/systemd/system/systemd-hwdb-update.service
         ln -fs /dev/null "$initdir"/etc/systemd/system/systemd-journal-catalog-update.service
diff --git a/test/TEST-35-LOGIN/testsuite.sh b/test/TEST-35-LOGIN/testsuite.sh
index 9855b4bc80..e4d72beb74 100755
--- a/test/TEST-35-LOGIN/testsuite.sh
+++ b/test/TEST-35-LOGIN/testsuite.sh
@@ -3,7 +3,100 @@
 set -eux
 set -o pipefail
 
+setup_idle_action_lock() {
+    useradd testuser ||:
+
+    mkdir -p /run/systemd/logind.conf.d/
+    cat >/run/systemd/logind.conf.d/idle-action-lock.conf <<EOF
+[Login]
+IdleAction=lock
+IdleActionSec=1s
+EOF
+
+    mkdir -p /run/systemd/systemd-logind.service.d/
+    cat >/run/systemd/systemd-logind.service.d/debug.conf <<EOF
+[Service]
+Environment=SYSTEMD_LOG_LEVEL=debug
+EOF
+
+    systemctl restart systemd-logind.service
+}
+
+teardown_idle_action_lock() {(
+    set +ex
+    rm -f /run/systemd/logind.conf.d/idle-action-lock.conf
+    rm -f /run/systemd/systemd-logind.service.d/debug.conf
+    pkill -9 -u "$(id -u testuser)"
+    userdel -r testuser
+    systemctl restart systemd-logind.service
+)}
+
+test_lock_idle_action() {
+    if ! command -v expect >/dev/null ; then
+        echo >&2 "expect not installed, skiping test ${FUNCNAME[0]}"
+        return 0
+    fi
+
+    setup_idle_action_lock
+    trap teardown_idle_action_lock RETURN
+
+    if loginctl --no-legend | awk '{ print $3; }' | sort -u | grep -q testuser ; then
+        echo >&2 "Session of the \'testuser\' is already present."
+        return 1
+    fi
+
+    # IdleActionSec is set 1s but the accuracy of associated timer is 30s so we
+    # need to sleep in worst case for 31s to make sure timer elapsed. We sleep
+    # here for 35s to accomodate for any possible scheudling delays.
+    cat > /tmp/test.exp <<EOF
+spawn systemd-run -G -t -p PAMName=login -p User=testuser bash
+send "sleep 35\r"
+send "echo foobar\r"
+send "sleep 35\r"
+send "exit\r"
+interact
+wait
+EOF
+
+    ts="$(date '+%H:%M:%S')"
+    busctl --match "type='signal',sender='org.freedesktop.login1',interface='org.freedesktop.login1.Session',member='Lock'" monitor  > dbus.log &
+
+    expect /tmp/test.exp &
+
+    # Sleep a bit to give expect time to spawn systemd-run before we check for
+    # the presence of resulting session.
+    sleep 2
+    if [ "$(loginctl --no-legend | awk '{ print $3; }' | sort -u | grep -c testuser)" != 1 ] ; then
+        echo >&2 "\'testuser\' is expected to have exactly one session running."
+        return 1
+    fi
+
+    wait %2
+    sleep 20
+    kill %1
+
+    # We slept for 35s , in that interval all sessions should have become idle
+    # and "Lock" signal should have been sent out. Then we wrote to tty to make
+    # session active again and next we slept for another 35s so sessions have
+    # become idle again. 'Lock' signal is sent out for each session, we have at
+    # least one session, so minimum of 2 "Lock" signals must have been sent.
+    if [ "$(grep -c Member=Lock dbus.log)" -lt 2 ]; then
+        echo >&2 "Too few 'Lock' D-Bus signal sent, expected at least 2."
+        return 1
+    fi
+
+    journalctl -b -u systemd-logind.service --since="$ts" > logind.log
+    if [ "$(grep -c 'System idle. Doing lock operation.' logind.log)" -lt 2 ]; then
+        echo >&2 "System haven't entered idle state at least 2 times."
+        return 1
+    fi
+
+    rm -f dbus.log logind.log
+}
+
 : >/failed
 
+test_lock_idle_action
+
 touch /testok
 rm /failed
