xref: /openbsd/regress/usr.bin/ssh/multiplex.sh (revision 09467b48)
1#	$OpenBSD: multiplex.sh,v 1.33 2020/06/24 15:16:23 markus Exp $
2#	Placed in the Public Domain.
3
4CTL=$OBJ/ctl-sock
5
6tid="connection multiplexing"
7
8P=3301  # test port
9
10wait_for_mux_master_ready()
11{
12	for i in 1 2 3 4 5 6 7 8 9; do
13		${SSH} -F $OBJ/ssh_config -S $CTL -Ocheck otherhost \
14		    >/dev/null 2>&1 && return 0
15		sleep $i
16	done
17	fatal "mux master never becomes ready"
18}
19
20start_sshd
21
22start_mux_master()
23{
24	trace "start master, fork to background"
25	${SSH} -Nn2 -MS$CTL -F $OBJ/ssh_config -oSendEnv="_XXX_TEST" somehost \
26	    -E $TEST_REGRESS_LOGFILE 2>&1 &
27	# NB. $SSH_PID will be killed by test-exec.sh:cleanup on fatal errors.
28	SSH_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}
70verbose "test $tid: forward"
71trace "forward over TCP/IP and check result"
72$NC -N -l 127.0.0.1 $((${PORT} + 1)) < ${DATA} > /dev/null &
73netcat_pid=$!
74${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
75sleep 1  # XXX remove once race fixed
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
86sleep 1  # XXX remove once race fixed
87$NC -U $OBJ/unix-3.fwd < /dev/null > ${COPY}
88cmp ${DATA} ${COPY}		|| fail "ssh: corrupted copy of ${DATA}"
89kill $netcat_pid 2>/dev/null
90rm -f ${COPY} $OBJ/unix-[123].fwd
91
92for s in 0 1 4 5 44; do
93   for mode in "" "-Oproxy"; do
94	trace "exit status $s over multiplexed connection ($mode)"
95	verbose "test $tid: status $s ($mode)"
96	${SSH} -F $OBJ/ssh_config -S $CTL $mode otherhost exit $s
97	r=$?
98	if [ $r -ne $s ]; then
99		fail "exit code mismatch: $r != $s"
100	fi
101
102	# same with early close of stdout/err
103	trace "exit status $s with early close over multiplexed connection ($mode)"
104	${SSH} -F $OBJ/ssh_config -S $CTL -n $mode otherhost \
105                exec sh -c \'"sleep 2; exec > /dev/null 2>&1; sleep 3; exit $s"\'
106	r=$?
107	if [ $r -ne $s ]; then
108		fail "exit code (with sleep) mismatch: $r != $s"
109	fi
110   done
111done
112
113verbose "test $tid: cmd check"
114${SSH} -F $OBJ/ssh_config -S $CTL -Ocheck otherhost >>$TEST_REGRESS_LOGFILE 2>&1 \
115    || fail "check command failed"
116
117verbose "test $tid: cmd forward local (TCP)"
118${SSH} -F $OBJ/ssh_config -S $CTL -Oforward -L $P:localhost:$PORT otherhost \
119     || fail "request local forward failed"
120sleep 1  # XXX remove once race fixed
121${SSH} -F $OBJ/ssh_config -p$P otherhost true \
122     || fail "connect to local forward port failed"
123${SSH} -F $OBJ/ssh_config -S $CTL -Ocancel -L $P:localhost:$PORT otherhost \
124     || fail "cancel local forward failed"
125${SSH} -F $OBJ/ssh_config -p$P otherhost true \
126     && fail "local forward port still listening"
127
128verbose "test $tid: cmd forward remote (TCP)"
129${SSH} -F $OBJ/ssh_config -S $CTL -Oforward -R $P:localhost:$PORT otherhost \
130     || fail "request remote forward failed"
131sleep 1  # XXX remove once race fixed
132${SSH} -F $OBJ/ssh_config -p$P otherhost true \
133     || fail "connect to remote forwarded port failed"
134${SSH} -F $OBJ/ssh_config -S $CTL -Ocancel -R $P:localhost:$PORT otherhost \
135     || fail "cancel remote forward failed"
136${SSH} -F $OBJ/ssh_config -p$P otherhost true \
137     && fail "remote forward port still listening"
138
139verbose "test $tid: cmd forward local (UNIX)"
140${SSH} -F $OBJ/ssh_config -S $CTL -Oforward -L $OBJ/unix-1.fwd:localhost:$PORT otherhost \
141     || fail "request local forward failed"
142sleep 1  # XXX remove once race fixed
143echo "" | $NC -U $OBJ/unix-1.fwd | \
144    grep "Invalid SSH identification string" >/dev/null 2>&1 \
145     || fail "connect to local forward path failed"
146${SSH} -F $OBJ/ssh_config -S $CTL -Ocancel -L $OBJ/unix-1.fwd:localhost:$PORT otherhost \
147     || fail "cancel local forward failed"
148N=$(echo "xyzzy" | $NC -U $OBJ/unix-1.fwd 2>&1 | grep "xyzzy" | wc -l)
149test ${N} -eq 0 || fail "local forward path still listening"
150rm -f $OBJ/unix-1.fwd
151
152verbose "test $tid: cmd forward remote (UNIX)"
153${SSH} -F $OBJ/ssh_config -S $CTL -Oforward -R $OBJ/unix-1.fwd:localhost:$PORT otherhost \
154     || fail "request remote forward failed"
155sleep 1  # XXX remove once race fixed
156echo "" | $NC -U $OBJ/unix-1.fwd | \
157    grep "Invalid SSH identification string" >/dev/null 2>&1 \
158     || fail "connect to remote forwarded path failed"
159${SSH} -F $OBJ/ssh_config -S $CTL -Ocancel -R $OBJ/unix-1.fwd:localhost:$PORT otherhost \
160     || fail "cancel remote forward failed"
161N=$(echo "xyzzy" | $NC -U $OBJ/unix-1.fwd 2>&1 | grep "xyzzy" | wc -l)
162test ${N} -eq 0 || fail "remote forward path still listening"
163rm -f $OBJ/unix-1.fwd
164
165verbose "test $tid: cmd exit"
166${SSH} -F $OBJ/ssh_config -S $CTL -Oexit otherhost >>$TEST_REGRESS_LOGFILE 2>&1 \
167    || fail "send exit command failed"
168
169# Wait for master to exit
170wait $SSH_PID
171kill -0 $SSH_PID >/dev/null 2>&1 && fail "exit command failed"
172
173# Restart master and test -O stop command with master using -N
174verbose "test $tid: cmd stop"
175trace "restart master, fork to background"
176start_mux_master
177
178# start a long-running command then immediately request a stop
179${SSH} -F $OBJ/ssh_config -S $CTL otherhost "sleep 10; exit 0" \
180     >>$TEST_REGRESS_LOGFILE 2>&1 &
181SLEEP_PID=$!
182${SSH} -F $OBJ/ssh_config -S $CTL -Ostop otherhost >>$TEST_REGRESS_LOGFILE 2>&1 \
183    || fail "send stop command failed"
184
185# wait until both long-running command and master have exited.
186wait $SLEEP_PID
187[ $! != 0 ] || fail "waiting for concurrent command"
188wait $SSH_PID
189[ $! != 0 ] || fail "waiting for master stop"
190kill -0 $SSH_PID >/dev/null 2>&1 && fatal "stop command failed"
191SSH_PID="" # Already gone, so don't kill in cleanup
192
193