1#!/bin/sh
2
3#
4# This test checks for operation of the MOSH_SERVER_NETWORK_TMOUT variable.
5# It does this by
6# * setting the variable
7# * killing the client (and its network traffic)
8# * waiting server-side, for the server to die
9# If it is killed, the test is successful.
10# If it survives that long and the server is still around, the test fails.
11# The client waits a bit longer than the server so that status can be collected
12# properly.
13#
14
15TIMEOUT=10
16
17# shellcheck source=e2e-test-subrs
18. "$(dirname "$0")/e2e-test-subrs"
19PATH=$PATH:.:$srcdir
20# Top-level wrapper.
21if [ $# -eq 0 ]; then
22    e2e-test "$0" client baseline
23    exit
24fi
25
26# OK, we have arguments, we're one of the test hooks.
27
28client()
29{
30    case "$myname" in
31	server-network-timeout)
32	    export MOSH_SERVER_NETWORK_TMOUT=$TIMEOUT;;
33	server-signal-timeout)
34	    export MOSH_SERVER_SIGNAL_TMOUT=$TIMEOUT;;
35	*)
36	    fail "unexpected test name %s\n" "$myname"
37    esac
38    shift
39    # Print this early, because the server is expected to die before
40    # the normal time this is sent by e2e-test-server.
41    printf "@@@ server complete @@@\n" >&2
42    # Run mosh SUT.
43    eval "$@"
44    # The client may be murdered.  We need to expect that...
45    retval=$?
46    case $retval in
47	0|1)
48	    fail "mosh-client had a normal exit\n";; # test condition failed
49	9|137|265)
50	    # Aha, signal 9.  Wait.
51	    sleep $(( TIMEOUT + 12 ))
52	    exit 0
53	    ;;
54	*)
55	    fail "unknown client wrapper failure, retval=%d\n" $retval
56	    ;;
57    esac
58    fail "client wrapper shouldnt get here\n"
59}
60baseline()
61{
62    # check for our wonderful variable
63    if [ -z "$MOSH_SERVER_NETWORK_TMOUT" ] && [ -z "$MOSH_SERVER_SIGNAL_TMOUT" ]; then
64	env
65	fail "Variable unset\n"
66    fi
67    # check for our client
68    if [ -z "$MOSH_CLIENT_PID" ]; then
69	env
70	fail "Client pid unavailable\n"
71    fi
72    if ! kill -0 "$MOSH_CLIENT_PID"; then
73	fail "mosh client is unexpectedly missing\n"
74    fi
75
76    # Set up for good return and cleanup on being killed
77    trap "echo got killed >&2; sleep 1; exit 0" HUP TERM
78    sleep 1
79
80    # Kill the client, to stop network traffic.
81    kill -KILL "$MOSH_CLIENT_PID"
82    case "$myname" in
83	server-network-timeout)
84	    # Just wait.  This is the hardest part.
85	    sleep $(( TIMEOUT + 7 ))
86	    ;;
87	server-signal-timeout)
88	    # Wait for the timeout to expire.
89	    sleep $(( TIMEOUT + 2 ))
90	    # Tell the server to go away.
91	    kill -USR1 "$MOSH_SERVER_PID"
92	    sleep 5
93	    ;;
94	*)
95	    fail "unexpected test name %s\n" "$myname"
96    esac
97    # If we're still alive and the server is too, the test failed.
98    # XXX the server is getting killed and we're getting here anyway.
99    # Exit with error only if server is still around.
100    sleep 1
101    ! kill -0 "$MOSH_SERVER_PID"
102    exit
103}
104
105myname="$(basename "$0" .test)"
106
107case $1 in
108    baseline|variant)
109	baseline;;
110    client)
111	client "$@";;
112    *)
113	fail "unknown test argument %s\n" "$1";;
114esac
115