xref: /openbsd-src/regress/usr.bin/ssh/multiplex.sh (revision 306ee9243269370a88cd4e75d9ee935a2e916a30)
1#	$OpenBSD: multiplex.sh,v 1.19 2013/04/22 07:23:08 dtucker Exp $
2#	Placed in the Public Domain.
3
4CTL=$OBJ/ctl-sock
5
6tid="connection multiplexing"
7
8DATA=/bin/ls
9COPY=$OBJ/ls.copy
10
11wait_for_mux_master_ready()
12{
13	for i in 1 2 3 4 5; do
14		${SSH} -F $OBJ/ssh_config -S $CTL -Ocheck otherhost \
15		    >/dev/null 2>&1 && return 0
16		sleep $i
17	done
18	fatal "mux master never becomes ready"
19}
20
21start_sshd
22
23start_mux_master()
24{
25	trace "start master, fork to background"
26	${SSH} -Nn2 -MS$CTL -F $OBJ/ssh_config -oSendEnv="_XXX_TEST" somehost \
27	    -E $TEST_REGRESS_LOGFILE 2>&1 &
28	MASTER_PID=$!
29	wait_for_mux_master_ready
30}
31
32start_mux_master
33
34verbose "test $tid: envpass"
35trace "env passing over multiplexed connection"
36_XXX_TEST=blah ${SSH} -F $OBJ/ssh_config -oSendEnv="_XXX_TEST" -S$CTL otherhost sh << 'EOF'
37	test X"$_XXX_TEST" = X"blah"
38EOF
39if [ $? -ne 0 ]; then
40	fail "environment not found"
41fi
42
43verbose "test $tid: transfer"
44rm -f ${COPY}
45trace "ssh transfer over multiplexed connection and check result"
46${SSH} -F $OBJ/ssh_config -S$CTL otherhost cat ${DATA} > ${COPY}
47test -f ${COPY}			|| fail "ssh -Sctl: failed copy ${DATA}"
48cmp ${DATA} ${COPY}		|| fail "ssh -Sctl: corrupted copy of ${DATA}"
49
50rm -f ${COPY}
51trace "ssh transfer over multiplexed connection and check result"
52${SSH} -F $OBJ/ssh_config -S $CTL otherhost cat ${DATA} > ${COPY}
53test -f ${COPY}			|| fail "ssh -S ctl: failed copy ${DATA}"
54cmp ${DATA} ${COPY}		|| fail "ssh -S ctl: corrupted copy of ${DATA}"
55
56rm -f ${COPY}
57trace "sftp transfer over multiplexed connection and check result"
58echo "get ${DATA} ${COPY}" | \
59	${SFTP} -S ${SSH} -F $OBJ/ssh_config -oControlPath=$CTL otherhost >>$TEST_REGRESS_LOGFILE 2>&1
60test -f ${COPY}			|| fail "sftp: failed copy ${DATA}"
61cmp ${DATA} ${COPY}		|| fail "sftp: corrupted copy of ${DATA}"
62
63rm -f ${COPY}
64trace "scp transfer over multiplexed connection and check result"
65${SCP} -S ${SSH} -F $OBJ/ssh_config -oControlPath=$CTL otherhost:${DATA} ${COPY} >>$TEST_REGRESS_LOGFILE 2>&1
66test -f ${COPY}			|| fail "scp: failed copy ${DATA}"
67cmp ${DATA} ${COPY}		|| fail "scp: corrupted copy of ${DATA}"
68
69rm -f ${COPY}
70
71for s in 0 1 4 5 44; do
72	trace "exit status $s over multiplexed connection"
73	verbose "test $tid: status $s"
74	${SSH} -F $OBJ/ssh_config -S $CTL otherhost exit $s
75	r=$?
76	if [ $r -ne $s ]; then
77		fail "exit code mismatch for protocol $p: $r != $s"
78	fi
79
80	# same with early close of stdout/err
81	trace "exit status $s with early close over multiplexed connection"
82	${SSH} -F $OBJ/ssh_config -S $CTL -n otherhost \
83                exec sh -c \'"sleep 2; exec > /dev/null 2>&1; sleep 3; exit $s"\'
84	r=$?
85	if [ $r -ne $s ]; then
86		fail "exit code (with sleep) mismatch for protocol $p: $r != $s"
87	fi
88done
89
90verbose "test $tid: cmd check"
91${SSH} -F $OBJ/ssh_config -S $CTL -Ocheck otherhost >>$TEST_REGRESS_LOGFILE 2>&1 \
92    || fail "check command failed"
93
94verbose "test $tid: cmd exit"
95${SSH} -F $OBJ/ssh_config -S $CTL -Oexit otherhost >>$TEST_REGRESS_LOGFILE 2>&1 \
96    || fail "send exit command failed"
97
98# Wait for master to exit
99wait $MASTER_PID
100kill -0 $MASTER_PID >/dev/null 2>&1 && fail "exit command failed"
101
102# Restart master and test -O stop command with master using -N
103verbose "test $tid: cmd stop"
104trace "restart master, fork to background"
105start_mux_master
106
107# start a long-running command then immediately request a stop
108${SSH} -F $OBJ/ssh_config -S $CTL otherhost "sleep 10; exit 0" \
109     >>$TEST_REGRESS_LOGFILE 2>&1 &
110SLEEP_PID=$!
111${SSH} -F $OBJ/ssh_config -S $CTL -Ostop otherhost >>$TEST_REGRESS_LOGFILE 2>&1 \
112    || fail "send stop command failed"
113
114# wait until both long-running command and master have exited.
115wait $SLEEP_PID
116[ $! != 0 ] || fail "waiting for concurrent command"
117wait $MASTER_PID
118[ $! != 0 ] || fail "waiting for master stop"
119kill -0 $MASTER_PID >/dev/null 2>&1 && fail "stop command failed"
120