test: console-client can read

Change-Id: I08c7523af548968c1545189c0007f44144589aec
Signed-off-by: Alexander Hansen <alexander.hansen@9elements.com>
Signed-off-by: Andrew Jeffery <andrew@codeconstruct.com.au>
diff --git a/test/meson.build b/test/meson.build
index 4aa1829..fb987f0 100644
--- a/test/meson.build
+++ b/test/meson.build
@@ -42,3 +42,11 @@
   depends: [ server ],
   suite: 'itests'
 )
+
+meson.override_find_program('test-console-client-can-read', files('test-console-client-can-read'))
+test('test-console-client-can-read',
+  find_program('test-console-client-can-read'),
+  args: [ socat.full_path(), server.full_path(), client.full_path() ],
+  depends: [ server, client ],
+  suite: 'itests'
+)
diff --git a/test/test-console-client-can-read b/test/test-console-client-can-read
new file mode 100755
index 0000000..c8b6551
--- /dev/null
+++ b/test/test-console-client-can-read
@@ -0,0 +1,60 @@
+#!/usr/bin/sh
+
+set -eux
+
+SOCAT="$1"
+SERVER="$2"
+CLIENT="$3"
+
+# Meet DBus bus and path name constraints, append own PID for parallel runs
+TEST_NAME="$(basename "$0" | tr '-' '_')"_${$}
+TEST_DIR="$(mktemp --tmpdir --directory "${TEST_NAME}.XXXXXX")"
+PTYS_PID=""
+SERVER_PID=""
+CLIENT_PID=""
+
+cd "$TEST_DIR"
+
+cleanup()
+{
+  [ -z "$CLIENT_PID" ] || kill "$CLIENT_PID"
+  [ -z "$SERVER_PID" ] || kill "$SERVER_PID"
+  [ -z "$PTYS_PID" ] || kill "$PTYS_PID"
+  wait
+  cd -
+  rm -rf "$TEST_DIR"
+}
+
+trap cleanup EXIT
+
+TEST_CONF="${TEST_NAME}.conf"
+TEST_LOG="${TEST_NAME}.log"
+
+cat <<EOF > "$TEST_CONF"
+active-console = $TEST_NAME
+[$TEST_NAME]
+console-id = $TEST_NAME
+logfile = $TEST_LOG
+EOF
+
+"$SOCAT" -u PTY,raw,echo=0,link=remote PTY,raw,echo=0,wait-slave,link=local &
+PTYS_PID="$!"
+while ! [ -e remote ] || ! [ -e local ]; do sleep 1; done
+
+"$SERVER" --config "$TEST_CONF" "$(realpath local)" &
+SERVER_PID="$!"
+while ! busctl status --user xyz.openbmc_project.Console."${TEST_NAME}"; do sleep 1; done
+
+$SOCAT EXEC:"$CLIENT -i $TEST_NAME" EXEC:'grep -m1 -qF client-reads-this' &
+CLIENT_PID="$!"
+
+sleep 1
+
+echo client-reads-this > remote
+
+sleep 1
+
+# If we can signal the process, the test has failed.
+# The 'grep -m1' should have ended the process when the message was read.
+kill -0 "$CLIENT_PID" && exit 1
+CLIENT_PID=""