1#!/usr/bin/sh
2
3set -eux
4
5SOCAT="$1"
6SERVER="$2"
7
8# Meet DBus bus and path name constraints, append own PID for parallel runs
9TEST_NAME="$(basename "$0" | tr '-' '_')"_${$}
10TEST_DIR="$(mktemp --tmpdir --directory "${TEST_NAME}.XXXXXX")"
11PTYS_PID=""
12SERVER_PID=""
13SUN_PID=""
14
15cd "$TEST_DIR"
16
17cleanup()
18{
19  [ -z "$SUN_PID" ] || kill "$SUN_PID"
20  [ -z "$SERVER_PID" ] || kill "$SERVER_PID"
21  [ -z "$PTYS_PID" ] || kill "$PTYS_PID"
22  wait
23  cd -
24  rm -rf "$TEST_DIR"
25}
26
27trap cleanup EXIT
28
29TEST_CONF="${TEST_NAME}.conf"
30
31TEST_A_NAME="${TEST_NAME}_a"
32TEST_A_LOG="${TEST_A_NAME}.log"
33
34TEST_B_NAME="${TEST_NAME}_b"
35TEST_B_LOG="${TEST_B_NAME}.log"
36
37cat <<EOF > "$TEST_CONF"
38active-console = $TEST_A_NAME
39[$TEST_A_NAME]
40logfile = $TEST_A_LOG
41console-id = $TEST_A_NAME
42[$TEST_B_NAME]
43logfile = $TEST_B_LOG
44console-id = $TEST_B_NAME
45EOF
46
47"$SOCAT" -u PTY,raw,echo=0,link=remote PTY,raw,echo=0,wait-slave,link=local &
48PTYS_PID="$!"
49while ! [ -e remote ] || ! [ -e local ]; do sleep 1; done
50
51"$SERVER" --config "$TEST_CONF" "$(realpath local)" &
52SERVER_PID="$!"
53while ! busctl status --user xyz.openbmc_project.Console."${TEST_A_NAME}"; do sleep 1; done
54
55echo log-for-console-a > remote
56
57sleep 1
58
59grep -LF log-for-console-a "$TEST_A_LOG"
60! grep -F log-for-console-a "$TEST_B_LOG" || exit 1
61
62# change the active console
63socat -u "ABSTRACT:obmc-console.${TEST_B_NAME}" SYSTEM:'cat > /dev/null' &
64SUN_PID="$!"
65
66sleep 1
67
68echo log-for-console-b > remote
69
70sleep 1
71
72grep -LF log-for-console-b "$TEST_B_LOG"
73! grep -F log-for-console-b "$TEST_A_LOG" || exit 1
74