1 # $OpenBSD: multiplex.sh,v 1.28 2017/04/30 23:34:55 djm Exp $
2 # Placed in the Public Domain.
5 CTL=${SSH_REGRESS_TMP}/ctl-sock
7 tid="connection multiplexing"
11 trace "will use ProxyCommand $proxycmd"
12 if config_defined DISABLE_FD_PASSING ; then
13 echo "skipped (not supported on this platform)"
19 wait_for_mux_master_ready()
21 for i in 1 2 3 4 5; do
22 ${SSH} -F $OBJ/ssh_config -S $CTL -Ocheck otherhost \
23 >/dev/null 2>&1 && return 0
26 fatal "mux master never becomes ready"
33 trace "start master, fork to background"
34 ${SSH} -Nn2 -MS$CTL -F $OBJ/ssh_config -oSendEnv="_XXX_TEST" somehost \
35 -E $TEST_REGRESS_LOGFILE 2>&1 &
36 # NB. $SSH_PID will be killed by test-exec.sh:cleanup on fatal errors.
38 wait_for_mux_master_ready
43 verbose "test $tid: envpass"
44 trace "env passing over multiplexed connection"
45 _XXX_TEST=blah ${SSH} -F $OBJ/ssh_config -oSendEnv="_XXX_TEST" -S$CTL otherhost sh << 'EOF'
46 test X"$_XXX_TEST" = X"blah"
49 fail "environment not found"
52 verbose "test $tid: transfer"
54 trace "ssh transfer over multiplexed connection and check result"
55 ${SSH} -F $OBJ/ssh_config -S$CTL otherhost cat ${DATA} > ${COPY}
56 test -f ${COPY} || fail "ssh -Sctl: failed copy ${DATA}"
57 cmp ${DATA} ${COPY} || fail "ssh -Sctl: corrupted copy of ${DATA}"
60 trace "ssh transfer over multiplexed connection and check result"
61 ${SSH} -F $OBJ/ssh_config -S $CTL otherhost cat ${DATA} > ${COPY}
62 test -f ${COPY} || fail "ssh -S ctl: failed copy ${DATA}"
63 cmp ${DATA} ${COPY} || fail "ssh -S ctl: corrupted copy of ${DATA}"
66 trace "sftp transfer over multiplexed connection and check result"
67 echo "get ${DATA} ${COPY}" | \
68 ${SFTP} -S ${SSH} -F $OBJ/ssh_config -oControlPath=$CTL otherhost >>$TEST_REGRESS_LOGFILE 2>&1
69 test -f ${COPY} || fail "sftp: failed copy ${DATA}"
70 cmp ${DATA} ${COPY} || fail "sftp: corrupted copy of ${DATA}"
73 trace "scp transfer over multiplexed connection and check result"
74 ${SCP} -S ${SSH} -F $OBJ/ssh_config -oControlPath=$CTL otherhost:${DATA} ${COPY} >>$TEST_REGRESS_LOGFILE 2>&1
75 test -f ${COPY} || fail "scp: failed copy ${DATA}"
76 cmp ${DATA} ${COPY} || fail "scp: corrupted copy of ${DATA}"
79 verbose "test $tid: forward"
80 trace "forward over TCP/IP and check result"
81 $NC -N -l 127.0.0.1 $((${PORT} + 1)) < ${DATA} > /dev/null &
83 ${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
84 $NC 127.0.0.1 $((${PORT} + 2)) < /dev/null > ${COPY}
85 cmp ${DATA} ${COPY} || fail "ssh: corrupted copy of ${DATA}"
86 kill $netcat_pid 2>/dev/null
87 rm -f ${COPY} $OBJ/unix-[123].fwd
89 trace "forward over UNIX and check result"
90 $NC -N -Ul $OBJ/unix-1.fwd < ${DATA} > /dev/null &
92 ${SSH} -F $OBJ/ssh_config -S $CTL -Oforward -L$OBJ/unix-2.fwd:$OBJ/unix-1.fwd otherhost >>$TEST_SSH_LOGFILE 2>&1
93 ${SSH} -F $OBJ/ssh_config -S $CTL -Oforward -R$OBJ/unix-3.fwd:$OBJ/unix-2.fwd otherhost >>$TEST_SSH_LOGFILE 2>&1
94 $NC -U $OBJ/unix-3.fwd < /dev/null > ${COPY} 2>/dev/null
95 cmp ${DATA} ${COPY} || fail "ssh: corrupted copy of ${DATA}"
96 kill $netcat_pid 2>/dev/null
97 rm -f ${COPY} $OBJ/unix-[123].fwd
99 for s in 0 1 4 5 44; do
100 trace "exit status $s over multiplexed connection"
101 verbose "test $tid: status $s"
102 ${SSH} -F $OBJ/ssh_config -S $CTL otherhost exit $s
104 if [ $r -ne $s ]; then
105 fail "exit code mismatch: $r != $s"
108 # same with early close of stdout/err
109 trace "exit status $s with early close over multiplexed connection"
110 ${SSH} -F $OBJ/ssh_config -S $CTL -n otherhost \
111 exec sh -c \'"sleep 2; exec > /dev/null 2>&1; sleep 3; exit $s"\'
113 if [ $r -ne $s ]; then
114 fail "exit code (with sleep) mismatch: $r != $s"
118 verbose "test $tid: cmd check"
119 ${SSH} -F $OBJ/ssh_config -S $CTL -Ocheck otherhost >>$TEST_REGRESS_LOGFILE 2>&1 \
120 || fail "check command failed"
122 verbose "test $tid: cmd forward local (TCP)"
123 ${SSH} -F $OBJ/ssh_config -S $CTL -Oforward -L $P:localhost:$PORT otherhost \
124 || fail "request local forward failed"
125 ${SSH} -F $OBJ/ssh_config -p$P otherhost true \
126 || fail "connect to local forward port failed"
127 ${SSH} -F $OBJ/ssh_config -S $CTL -Ocancel -L $P:localhost:$PORT otherhost \
128 || fail "cancel local forward failed"
129 ${SSH} -F $OBJ/ssh_config -p$P otherhost true \
130 && fail "local forward port still listening"
132 verbose "test $tid: cmd forward remote (TCP)"
133 ${SSH} -F $OBJ/ssh_config -S $CTL -Oforward -R $P:localhost:$PORT otherhost \
134 || fail "request remote forward failed"
135 ${SSH} -F $OBJ/ssh_config -p$P otherhost true \
136 || fail "connect to remote forwarded port failed"
137 ${SSH} -F $OBJ/ssh_config -S $CTL -Ocancel -R $P:localhost:$PORT otherhost \
138 || fail "cancel remote forward failed"
139 ${SSH} -F $OBJ/ssh_config -p$P otherhost true \
140 && fail "remote forward port still listening"
142 verbose "test $tid: cmd forward local (UNIX)"
143 ${SSH} -F $OBJ/ssh_config -S $CTL -Oforward -L $OBJ/unix-1.fwd:localhost:$PORT otherhost \
144 || fail "request local forward failed"
145 echo "" | $NC -U $OBJ/unix-1.fwd | grep "Protocol mismatch" >/dev/null 2>&1 \
146 || fail "connect to local forward path failed"
147 ${SSH} -F $OBJ/ssh_config -S $CTL -Ocancel -L $OBJ/unix-1.fwd:localhost:$PORT otherhost \
148 || fail "cancel local forward failed"
149 N=$(echo "xyzzy" | $NC -U $OBJ/unix-1.fwd 2>&1 | grep "xyzzy" | wc -l)
150 test ${N} -eq 0 || fail "local forward path still listening"
151 rm -f $OBJ/unix-1.fwd
153 verbose "test $tid: cmd forward remote (UNIX)"
154 ${SSH} -F $OBJ/ssh_config -S $CTL -Oforward -R $OBJ/unix-1.fwd:localhost:$PORT otherhost \
155 || fail "request remote forward failed"
156 echo "" | $NC -U $OBJ/unix-1.fwd | grep "Protocol mismatch" >/dev/null 2>&1 \
157 || fail "connect to remote forwarded path failed"
158 ${SSH} -F $OBJ/ssh_config -S $CTL -Ocancel -R $OBJ/unix-1.fwd:localhost:$PORT otherhost \
159 || fail "cancel remote forward failed"
160 N=$(echo "xyzzy" | $NC -U $OBJ/unix-1.fwd 2>&1 | grep "xyzzy" | wc -l)
161 test ${N} -eq 0 || fail "remote forward path still listening"
162 rm -f $OBJ/unix-1.fwd
164 verbose "test $tid: cmd exit"
165 ${SSH} -F $OBJ/ssh_config -S $CTL -Oexit otherhost >>$TEST_REGRESS_LOGFILE 2>&1 \
166 || fail "send exit command failed"
168 # Wait for master to exit
170 kill -0 $SSH_PID >/dev/null 2>&1 && fail "exit command failed"
172 # Restart master and test -O stop command with master using -N
173 verbose "test $tid: cmd stop"
174 trace "restart master, fork to background"
177 # start a long-running command then immediately request a stop
178 ${SSH} -F $OBJ/ssh_config -S $CTL otherhost "sleep 10; exit 0" \
179 >>$TEST_REGRESS_LOGFILE 2>&1 &
181 ${SSH} -F $OBJ/ssh_config -S $CTL -Ostop otherhost >>$TEST_REGRESS_LOGFILE 2>&1 \
182 || fail "send stop command failed"
184 # wait until both long-running command and master have exited.
186 [ $! != 0 ] || fail "waiting for concurrent command"
188 [ $! != 0 ] || fail "waiting for master stop"
189 kill -0 $SSH_PID >/dev/null 2>&1 && fatal "stop command failed"
190 SSH_PID="" # Already gone, so don't kill in cleanup