From b64f5ddacad725d5e4e021691fad52aa2eb32c46 Mon Sep 17 00:00:00 2001
From: Mike Yuan <me@yhndnzj.com>
Date: Wed, 22 Mar 2023 03:40:52 +0800
Subject: [PATCH] test: add tests for NOTIFYACCESS override through sd_notify

Conflict:Adaptation Context.
Reference:https://github.com/systemd/systemd/commit/b64f5ddacad725d5e4e021691fad52aa2eb32c46

---
 test/TEST-80-NOTIFYACCESS/Makefile     |  1 +
 test/TEST-80-NOTIFYACCESS/test.sh      | 11 +++++++++
 test/meson.build                       |  3 +++
 test/testsuite-80.units/notify.service |  4 ++++
 test/testsuite-80.units/test.sh        | 26 +++++++++++++++++++++
 test/units/testsuite-80.service        |  8 +++++++
 test/units/testsuite-80.sh             | 32 ++++++++++++++++++++++++++
 7 files changed, 85 insertions(+)
 create mode 120000 test/TEST-80-NOTIFYACCESS/Makefile
 create mode 100755 test/TEST-80-NOTIFYACCESS/test.sh
 create mode 100644 test/testsuite-80.units/notify.service
 create mode 100755 test/testsuite-80.units/test.sh
 create mode 100644 test/units/testsuite-80.service
 create mode 100755 test/units/testsuite-80.sh

diff --git a/test/TEST-80-NOTIFYACCESS/Makefile b/test/TEST-80-NOTIFYACCESS/Makefile
new file mode 120000
index 0000000..e9f93b1
--- /dev/null
+++ b/test/TEST-80-NOTIFYACCESS/Makefile
@@ -0,0 +1 @@
+../TEST-01-BASIC/Makefile
\ No newline at end of file
diff --git a/test/TEST-80-NOTIFYACCESS/test.sh b/test/TEST-80-NOTIFYACCESS/test.sh
new file mode 100755
index 0000000..b4d2452
--- /dev/null
+++ b/test/TEST-80-NOTIFYACCESS/test.sh
@@ -0,0 +1,11 @@
+#!/usr/bin/env bash
+# SPDX-License-Identifier: LGPL-2.1-or-later
+set -e
+
+TEST_DESCRIPTION="test NotifyAccess through sd-notify"
+TEST_NO_QEMU=1
+
+# shellcheck source=test/test-functions
+. "${TEST_BASE_DIR:?}/test-functions"
+
+do_test "$@"
diff --git a/test/meson.build b/test/meson.build
index 8727e66..8eb9bc4 100644
--- a/test/meson.build
+++ b/test/meson.build
@@ -51,6 +51,9 @@ if install_tests
         install_subdir('testsuite-63.units',
                        exclude_files : '.gitattributes',
                        install_dir : testdata_dir)
+        install_subdir('testsuite-80.units',
+                       exclude_files : '.gitattributes',
+                       install_dir : testdata_dir)
 
         testsuite08_dir = testdata_dir + '/testsuite-08.units'
         install_data('testsuite-08.units/-.mount',
diff --git a/test/testsuite-80.units/notify.service b/test/testsuite-80.units/notify.service
new file mode 100644
index 0000000..196b076
--- /dev/null
+++ b/test/testsuite-80.units/notify.service
@@ -0,0 +1,4 @@
+[Service]
+Type=notify
+NotifyAccess=all
+ExecStart=/usr/lib/systemd/tests/testdata/testsuite-80.units/test.sh
diff --git a/test/testsuite-80.units/test.sh b/test/testsuite-80.units/test.sh
new file mode 100755
index 0000000..3ca71d5
--- /dev/null
+++ b/test/testsuite-80.units/test.sh
@@ -0,0 +1,26 @@
+#!/usr/bin/env bash
+# SPDX-License-Identifier: LGPL-2.1-or-later
+# shellcheck disable=SC2016
+set -eux
+set -o pipefail
+
+systemd-notify --status="Test starts, waiting for 5 seconds"
+sleep 5
+
+(
+    systemd-notify --pid=auto
+    systemd-notify "NOTIFYACCESS=main"
+
+    systemd-notify --status="Sending READY=1 in an unpriviledged process"
+    (
+        sleep 0.1
+        systemd-notify --ready
+    )
+    sleep 10
+
+    systemd-notify "MAINPID=$$"
+)
+
+systemd-notify --ready --status="OK"
+systemd-notify "NOTIFYACCESS=none"
+sleep infinity
diff --git a/test/units/testsuite-80.service b/test/units/testsuite-80.service
new file mode 100644
index 0000000..4c7f5d5
--- /dev/null
+++ b/test/units/testsuite-80.service
@@ -0,0 +1,8 @@
+# SPDX-License-Identifier: LGPL-2.1-or-later
+[Unit]
+Description=TEST-80-NOTIFYACCESS
+
+[Service]
+ExecStartPre=rm -f /failed /testok
+ExecStart=/usr/lib/systemd/tests/testdata/units/%N.sh
+Type=oneshot
diff --git a/test/units/testsuite-80.sh b/test/units/testsuite-80.sh
new file mode 100755
index 0000000..5f57569
--- /dev/null
+++ b/test/units/testsuite-80.sh
@@ -0,0 +1,32 @@
+#!/usr/bin/env bash
+# SPDX-License-Identifier: LGPL-2.1-or-later
+# shellcheck disable=SC2016
+set -eux
+set -o pipefail
+
+# shellcheck source=test/units/assert.sh
+. "$(dirname "$0")"/assert.sh
+
+: >/failed
+
+systemctl --no-block start notify.service
+sleep 2
+
+assert_eq "$(systemctl show notify.service -p StatusText --value)" "Test starts, waiting for 5 seconds"
+assert_eq "$(systemctl show notify.service -p NotifyAccess --value)" "all"
+sleep 5
+
+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_rc 3 systemctl --quiet is-active notify.service
+sleep 10
+
+systemctl --quiet is-active notify.service
+assert_eq "$(systemctl show notify.service -p StatusText --value)" "OK"
+assert_eq "$(systemctl show notify.service -p NotifyAccess --value)" "none"
+
+systemctl stop notify.service
+assert_eq "$(systemctl show notify.service -p NotifyAccess --value)" "all"
+
+touch /testok
+rm /failed
-- 
2.33.0

