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