xref: /openbsd-src/regress/usr.bin/ssh/multiplex.sh (revision f2da64fbbbf1b03f09f390ab01267c93dfd77c4c)
1#	$OpenBSD: multiplex.sh,v 1.27 2014/12/22 06:14:29 djm Exp $
2#	Placed in the Public Domain.
3
4CTL=$OBJ/ctl-sock
5
6tid="connection multiplexing"
7
8NC=nc
9P=3301  # test port
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	# NB. $SSH_PID will be killed by test-exec.sh:cleanup on fatal errors.
29	SSH_PID=$!
30	wait_for_mux_master_ready
31}
32
33start_mux_master
34
35verbose "test $tid: envpass"
36trace "env passing over multiplexed connection"
37_XXX_TEST=blah ${SSH} -F $OBJ/ssh_config -oSendEnv="_XXX_TEST" -S$CTL otherhost sh << 'EOF'
38	test X"$_XXX_TEST" = X"blah"
39EOF
40if [ $? -ne 0 ]; then
41	fail "environment not found"
42fi
43
44verbose "test $tid: transfer"
45rm -f ${COPY}
46trace "ssh transfer over multiplexed connection and check result"
47${SSH} -F $OBJ/ssh_config -S$CTL otherhost cat ${DATA} > ${COPY}
48test -f ${COPY}			|| fail "ssh -Sctl: failed copy ${DATA}"
49cmp ${DATA} ${COPY}		|| fail "ssh -Sctl: corrupted copy of ${DATA}"
50
51rm -f ${COPY}
52trace "ssh transfer over multiplexed connection and check result"
53${SSH} -F $OBJ/ssh_config -S $CTL otherhost cat ${DATA} > ${COPY}
54test -f ${COPY}			|| fail "ssh -S ctl: failed copy ${DATA}"
55cmp ${DATA} ${COPY}		|| fail "ssh -S ctl: corrupted copy of ${DATA}"
56
57rm -f ${COPY}
58trace "sftp transfer over multiplexed connection and check result"
59echo "get ${DATA} ${COPY}" | \
60	${SFTP} -S ${SSH} -F $OBJ/ssh_config -oControlPath=$CTL otherhost >>$TEST_REGRESS_LOGFILE 2>&1
61test -f ${COPY}			|| fail "sftp: failed copy ${DATA}"
62cmp ${DATA} ${COPY}		|| fail "sftp: corrupted copy of ${DATA}"
63
64rm -f ${COPY}
65trace "scp transfer over multiplexed connection and check result"
66${SCP} -S ${SSH} -F $OBJ/ssh_config -oControlPath=$CTL otherhost:${DATA} ${COPY} >>$TEST_REGRESS_LOGFILE 2>&1
67test -f ${COPY}			|| fail "scp: failed copy ${DATA}"
68cmp ${DATA} ${COPY}		|| fail "scp: corrupted copy of ${DATA}"
69
70rm -f ${COPY}
71verbose "test $tid: forward"
72trace "forward over TCP/IP and check result"
73$NC -N -l 127.0.0.1 $((${PORT} + 1)) < ${DATA} > /dev/null &
74netcat_pid=$!
75${SSH} -F $OBJ/ssh_config -S $CTL -Oforward -L127.0.0.1:$((${PORT} + 2)):127.0.0.1:$((${PORT} + 1)) otherhost >>$TEST_SSH_LOGFILE 2>&1
76$NC 127.0.0.1 $((${PORT} + 2)) < /dev/null > ${COPY}
77cmp ${DATA} ${COPY}		|| fail "ssh: corrupted copy of ${DATA}"
78kill $netcat_pid 2>/dev/null
79rm -f ${COPY} $OBJ/unix-[123].fwd
80
81trace "forward over UNIX and check result"
82$NC -N -Ul $OBJ/unix-1.fwd < ${DATA} > /dev/null &
83netcat_pid=$!
84${SSH} -F $OBJ/ssh_config -S $CTL -Oforward -L$OBJ/unix-2.fwd:$OBJ/unix-1.fwd otherhost >>$TEST_SSH_LOGFILE 2>&1
85${SSH} -F $OBJ/ssh_config -S $CTL -Oforward -R$OBJ/unix-3.fwd:$OBJ/unix-2.fwd otherhost >>$TEST_SSH_LOGFILE 2>&1
86$NC -U $OBJ/unix-3.fwd < /dev/null > ${COPY}
87cmp ${DATA} ${COPY}		|| fail "ssh: corrupted copy of ${DATA}"
88kill $netcat_pid 2>/dev/null
89rm -f ${COPY} $OBJ/unix-[123].fwd
90
91for s in 0 1 4 5 44; do
92	trace "exit status $s over multiplexed connection"
93	verbose "test $tid: status $s"
94	${SSH} -F $OBJ/ssh_config -S $CTL otherhost exit $s
95	r=$?
96	if [ $r -ne $s ]; then
97		fail "exit code mismatch for protocol $p: $r != $s"
98	fi
99
100	# same with early close of stdout/err
101	trace "exit status $s with early close over multiplexed connection"
102	${SSH} -F $OBJ/ssh_config -S $CTL -n otherhost \
103                exec sh -c \'"sleep 2; exec > /dev/null 2>&1; sleep 3; exit $s"\'
104	r=$?
105	if [ $r -ne $s ]; then
106		fail "exit code (with sleep) mismatch for protocol $p: $r != $s"
107	fi
108done
109
110verbose "test $tid: cmd check"
111${SSH} -F $OBJ/ssh_config -S $CTL -Ocheck otherhost >>$TEST_REGRESS_LOGFILE 2>&1 \
112    || fail "check command failed"
113
114verbose "test $tid: cmd forward local (TCP)"
115${SSH} -F $OBJ/ssh_config -S $CTL -Oforward -L $P:localhost:$PORT otherhost \
116     || fail "request local forward failed"
117${SSH} -F $OBJ/ssh_config -p$P otherhost true \
118     || fail "connect to local forward port failed"
119${SSH} -F $OBJ/ssh_config -S $CTL -Ocancel -L $P:localhost:$PORT otherhost \
120     || fail "cancel local forward failed"
121${SSH} -F $OBJ/ssh_config -p$P otherhost true \
122     && fail "local forward port still listening"
123
124verbose "test $tid: cmd forward remote (TCP)"
125${SSH} -F $OBJ/ssh_config -S $CTL -Oforward -R $P:localhost:$PORT otherhost \
126     || fail "request remote forward failed"
127${SSH} -F $OBJ/ssh_config -p$P otherhost true \
128     || fail "connect to remote forwarded port failed"
129${SSH} -F $OBJ/ssh_config -S $CTL -Ocancel -R $P:localhost:$PORT otherhost \
130     || fail "cancel remote forward failed"
131${SSH} -F $OBJ/ssh_config -p$P otherhost true \
132     && fail "remote forward port still listening"
133
134verbose "test $tid: cmd forward local (UNIX)"
135${SSH} -F $OBJ/ssh_config -S $CTL -Oforward -L $OBJ/unix-1.fwd:localhost:$PORT otherhost \
136     || fail "request local forward failed"
137echo "" | $NC -U $OBJ/unix-1.fwd | grep "Protocol mismatch" >/dev/null 2>&1 \
138     || fail "connect to local forward path failed"
139${SSH} -F $OBJ/ssh_config -S $CTL -Ocancel -L $OBJ/unix-1.fwd:localhost:$PORT otherhost \
140     || fail "cancel local forward failed"
141N=$(echo "xyzzy" | $NC -U $OBJ/unix-1.fwd 2>&1 | grep "xyzzy" | wc -l)
142test ${N} -eq 0 || fail "local forward path still listening"
143rm -f $OBJ/unix-1.fwd
144
145verbose "test $tid: cmd forward remote (UNIX)"
146${SSH} -F $OBJ/ssh_config -S $CTL -Oforward -R $OBJ/unix-1.fwd:localhost:$PORT otherhost \
147     || fail "request remote forward failed"
148echo "" | $NC -U $OBJ/unix-1.fwd | grep "Protocol mismatch" >/dev/null 2>&1 \
149     || fail "connect to remote forwarded path failed"
150${SSH} -F $OBJ/ssh_config -S $CTL -Ocancel -R $OBJ/unix-1.fwd:localhost:$PORT otherhost \
151     || fail "cancel remote forward failed"
152N=$(echo "xyzzy" | $NC -U $OBJ/unix-1.fwd 2>&1 | grep "xyzzy" | wc -l)
153test ${N} -eq 0 || fail "remote forward path still listening"
154rm -f $OBJ/unix-1.fwd
155
156verbose "test $tid: cmd exit"
157${SSH} -F $OBJ/ssh_config -S $CTL -Oexit otherhost >>$TEST_REGRESS_LOGFILE 2>&1 \
158    || fail "send exit command failed"
159
160# Wait for master to exit
161wait $SSH_PID
162kill -0 $SSH_PID >/dev/null 2>&1 && fail "exit command failed"
163
164# Restart master and test -O stop command with master using -N
165verbose "test $tid: cmd stop"
166trace "restart master, fork to background"
167start_mux_master
168
169# start a long-running command then immediately request a stop
170${SSH} -F $OBJ/ssh_config -S $CTL otherhost "sleep 10; exit 0" \
171     >>$TEST_REGRESS_LOGFILE 2>&1 &
172SLEEP_PID=$!
173${SSH} -F $OBJ/ssh_config -S $CTL -Ostop otherhost >>$TEST_REGRESS_LOGFILE 2>&1 \
174    || fail "send stop command failed"
175
176# wait until both long-running command and master have exited.
177wait $SLEEP_PID
178[ $! != 0 ] || fail "waiting for concurrent command"
179wait $SSH_PID
180[ $! != 0 ] || fail "waiting for master stop"
181kill -0 $SSH_PID >/dev/null 2>&1 && fatal "stop command failed"
182SSH_PID="" # Already gone, so don't kill in cleanup
183
184