ff6046
From afb82e108fe45d8481f2be50c74c4c6f307f8d60 Mon Sep 17 00:00:00 2001
ff6046
From: Lennart Poettering <lennart@poettering.net>
ff6046
Date: Tue, 17 Jul 2018 12:35:12 +0200
ff6046
Subject: [PATCH] test: add test for Type=exec
ff6046
ff6046
(cherry picked from commit 0e1f17561f5f6061ec5503de044298372ed7ca37)
ff6046
ff6046
Resolves: #1683334
ff6046
---
ff6046
 test/TEST-23-TYPE-EXEC/Makefile     |  4 +++
ff6046
 test/TEST-23-TYPE-EXEC/test.sh      | 42 +++++++++++++++++++++++++++++
ff6046
 test/TEST-23-TYPE-EXEC/testsuite.sh | 28 +++++++++++++++++++
ff6046
 3 files changed, 74 insertions(+)
ff6046
 create mode 100644 test/TEST-23-TYPE-EXEC/Makefile
ff6046
 create mode 100755 test/TEST-23-TYPE-EXEC/test.sh
ff6046
 create mode 100755 test/TEST-23-TYPE-EXEC/testsuite.sh
ff6046
ff6046
diff --git a/test/TEST-23-TYPE-EXEC/Makefile b/test/TEST-23-TYPE-EXEC/Makefile
ff6046
new file mode 100644
ff6046
index 0000000000..34d7cc6cdf
ff6046
--- /dev/null
ff6046
+++ b/test/TEST-23-TYPE-EXEC/Makefile
ff6046
@@ -0,0 +1,4 @@
ff6046
+BUILD_DIR=$(shell ../../tools/find-build-dir.sh)
ff6046
+
ff6046
+all setup clean run:
ff6046
+	@basedir=../.. TEST_BASE_DIR=../ BUILD_DIR=$(BUILD_DIR) ./test.sh --$@
ff6046
diff --git a/test/TEST-23-TYPE-EXEC/test.sh b/test/TEST-23-TYPE-EXEC/test.sh
ff6046
new file mode 100755
ff6046
index 0000000000..bdcea239a7
ff6046
--- /dev/null
ff6046
+++ b/test/TEST-23-TYPE-EXEC/test.sh
ff6046
@@ -0,0 +1,42 @@
ff6046
+#!/bin/bash
ff6046
+# -*- mode: shell-script; indent-tabs-mode: nil; sh-basic-offset: 4; -*-
ff6046
+# ex: ts=8 sw=4 sts=4 et filetype=sh
ff6046
+set -e
ff6046
+TEST_DESCRIPTION="test Type=exec"
ff6046
+
ff6046
+. $TEST_BASE_DIR/test-functions
ff6046
+
ff6046
+test_setup() {
ff6046
+    create_empty_image
ff6046
+    mkdir -p $TESTDIR/root
ff6046
+    mount ${LOOPDEV}p1 $TESTDIR/root
ff6046
+
ff6046
+    (
ff6046
+        LOG_LEVEL=5
ff6046
+        eval $(udevadm info --export --query=env --name=${LOOPDEV}p2)
ff6046
+
ff6046
+        setup_basic_environment
ff6046
+
ff6046
+        # setup the testsuite service
ff6046
+        cat >$initdir/etc/systemd/system/testsuite.service <
ff6046
+[Unit]
ff6046
+Description=Testsuite service
ff6046
+
ff6046
+[Service]
ff6046
+ExecStart=/testsuite.sh
ff6046
+Type=oneshot
ff6046
+StandardOutput=tty
ff6046
+StandardError=tty
ff6046
+NotifyAccess=all
ff6046
+EOF
ff6046
+        cp testsuite.sh $initdir/
ff6046
+
ff6046
+        setup_testsuite
ff6046
+    ) || return 1
ff6046
+    setup_nspawn_root
ff6046
+
ff6046
+    ddebug "umount $TESTDIR/root"
ff6046
+    umount $TESTDIR/root
ff6046
+}
ff6046
+
ff6046
+do_test "$@"
ff6046
diff --git a/test/TEST-23-TYPE-EXEC/testsuite.sh b/test/TEST-23-TYPE-EXEC/testsuite.sh
ff6046
new file mode 100755
ff6046
index 0000000000..80734bbbdc
ff6046
--- /dev/null
ff6046
+++ b/test/TEST-23-TYPE-EXEC/testsuite.sh
ff6046
@@ -0,0 +1,28 @@
ff6046
+#!/bin/bash
ff6046
+# -*- mode: shell-script; indent-tabs-mode: nil; sh-basic-offset: 4; -*-
ff6046
+# ex: ts=8 sw=4 sts=4 et filetype=sh
ff6046
+set -ex
ff6046
+set -o pipefail
ff6046
+
ff6046
+systemd-analyze set-log-level debug
ff6046
+systemd-analyze set-log-target console
ff6046
+
ff6046
+# Create a binary for which execve() will fail
ff6046
+touch /tmp/brokenbinary
ff6046
+chmod +x /tmp/brokenbinary
ff6046
+
ff6046
+# These three commands should succeed.
ff6046
+systemd-run --unit=one -p Type=simple /bin/sleep infinity
ff6046
+systemd-run --unit=two -p Type=simple -p User=idontexist /bin/sleep infinity
ff6046
+systemd-run --unit=three -p Type=simple /tmp/brokenbinary
ff6046
+
ff6046
+# And now, do the same with Type=exec, where the latter two should fail
ff6046
+systemd-run --unit=four -p Type=exec /bin/sleep infinity
ff6046
+! systemd-run --unit=five -p Type=exec -p User=idontexist /bin/sleep infinity
ff6046
+! systemd-run --unit=six -p Type=exec /tmp/brokenbinary
ff6046
+
ff6046
+systemd-analyze set-log-level info
ff6046
+
ff6046
+echo OK > /testok
ff6046
+
ff6046
+exit 0