From 09ba6d1a14ba027f2bc4e3426c7dd85db19e720e Mon Sep 17 00:00:00 2001
From: Lennart Poettering <lennart@poettering.net>
Date: Tue, 28 Mar 2023 16:35:35 +0200
Subject: [PATCH] TEST-80: synchronize explicitly instead of by time

This removes "sleep" invocations, and makes the notify access testcase a
lot more robust to runtime jitter. We use a pair of fifos in the fs to
sync instead.

Also various other improvoements, including comments.

(Also removes the unnecessary "no-qemu" restriction)

Conflict:NA
Reference:https://github.com/systemd/systemd/commit/09ba6d1a14ba027f2bc4e3426c7dd85db19e720e

---
 test/TEST-80-NOTIFYACCESS/test.sh |  1 -
 test/testsuite-80.units/test.sh   | 59 +++++++++++++++++++++++++------
 test/units/testsuite-80.sh        | 30 +++++++++++++---
 3 files changed, 73 insertions(+), 17 deletions(-)

diff --git a/test/TEST-80-NOTIFYACCESS/test.sh b/test/TEST-80-NOTIFYACCESS/test.sh
index b4d2452b75..8ec5b1bc5f 100755
--- a/test/TEST-80-NOTIFYACCESS/test.sh
+++ b/test/TEST-80-NOTIFYACCESS/test.sh
@@ -3,7 +3,6 @@
 set -e
 
 TEST_DESCRIPTION="test NotifyAccess through sd-notify"
-TEST_NO_QEMU=1
 
 # shellcheck source=test/test-functions
 . "${TEST_BASE_DIR:?}/test-functions"
diff --git a/test/testsuite-80.units/test.sh b/test/testsuite-80.units/test.sh
index 3ca71d5648..565ed8d35a 100755
--- a/test/testsuite-80.units/test.sh
+++ b/test/testsuite-80.units/test.sh
@@ -4,23 +4,60 @@
 set -eux
 set -o pipefail
 
-systemd-notify --status="Test starts, waiting for 5 seconds"
-sleep 5
+sync_in() {
+    read -r x < /tmp/syncfifo2
+    test "$x" = "$1"
+}
 
+sync_out() {
+    echo "$1" > /tmp/syncfifo1
+}
+
+export SYSTEMD_LOG_LEVEL=debug
+
+echo "toplevel PID: $BASHPID"
+
+systemd-notify --status="Test starts"
+sync_out a
+sync_in b
 (
-    systemd-notify --pid=auto
+    echo "subshell PID: $BASHPID"
+
+    # Make us main process
+    systemd-notify --pid="$BASHPID"
+
+    # Lock down access to just us
     systemd-notify "NOTIFYACCESS=main"
 
-    systemd-notify --status="Sending READY=1 in an unpriviledged process"
-    (
-        sleep 0.1
-        systemd-notify --ready
-    )
-    sleep 10
+    # This should still work
+    systemd-notify --status="Sending READY=1 in an unprivileged process"
+
+    # Send as subprocess of the subshell, this should not work
+    systemd-notify --ready --pid=self --status "BOGUS1"
 
-    systemd-notify "MAINPID=$$"
+    sync_out c
+    sync_in d
+
+    # Move main process back to toplevel
+    systemd-notify --pid=parent "MAINPID=$$"
+
+    # Should be dropped again
+    systemd-notify --status="BOGUS2" --pid=parent
+
+    # Apparently, bash will automatically invoke the last command in a subshell
+    # via a simple execve() rather than fork()ing first. But we want that the
+    # previous command uses the subshell's PID, hence let's insert a final,
+    # bogus redundant command as last command to run in the subshell, so that
+    # bash can't optimize things like that.
+    echo "bye"
 )
 
+echo "toplevel again: $BASHPID"
+
 systemd-notify --ready --status="OK"
 systemd-notify "NOTIFYACCESS=none"
-sleep infinity
+systemd-notify --status="BOGUS3"
+
+sync_out e
+
+exec sleep infinity
diff --git a/test/units/testsuite-80.sh b/test/units/testsuite-80.sh
index 5f57569b07..43647a707f 100755
--- a/test/units/testsuite-80.sh
+++ b/test/units/testsuite-80.sh
@@ -9,17 +9,35 @@ set -o pipefail
 
 : >/failed
 
+mkfifo /tmp/syncfifo1 /tmp/syncfifo2
+
+sync_in() {
+    read -r x < /tmp/syncfifo1
+    test "$x" = "$1"
+}
+
+sync_out() {
+    echo "$1" > /tmp/syncfifo2
+}
+
+export SYSTEMD_LOG_LEVEL=debug
+
 systemctl --no-block start notify.service
-sleep 2
 
-assert_eq "$(systemctl show notify.service -p StatusText --value)" "Test starts, waiting for 5 seconds"
+sync_in a
+
 assert_eq "$(systemctl show notify.service -p NotifyAccess --value)" "all"
-sleep 5
+assert_eq "$(systemctl show notify.service -p StatusText --value)" "Test starts"
+
+sync_out b
+sync_in c
 
 assert_eq "$(systemctl show notify.service -p NotifyAccess --value)" "main"
-assert_eq "$(systemctl show notify.service -p StatusText --value)" "Sending READY=1 in an unpriviledged process"
+assert_eq "$(systemctl show notify.service -p StatusText --value)" "Sending READY=1 in an unprivileged process"
 assert_rc 3 systemctl --quiet is-active notify.service
-sleep 10
+
+sync_out d
+sync_in e
 
 systemctl --quiet is-active notify.service
 assert_eq "$(systemctl show notify.service -p StatusText --value)" "OK"
@@ -28,5 +46,7 @@ assert_eq "$(systemctl show notify.service -p NotifyAccess --value)" "none"
 systemctl stop notify.service
 assert_eq "$(systemctl show notify.service -p NotifyAccess --value)" "all"
 
+rm /tmp/syncfifo1 /tmp/syncfifo2
+
 touch /testok
 rm /failed
-- 
2.33.0

