1 # $OpenBSD: multiplex.sh,v 1.36 2023/03/01 09:29:32 dtucker Exp $
2 # Placed in the Public Domain.
5 CTL=${SSH_REGRESS_TMP}/ctl-sock
7 tid="connection multiplexing"
9 trace "will use ProxyCommand $proxycmd"
10 if config_defined DISABLE_FD_PASSING ; then
11 echo "skipped (not supported on this platform)"
17 wait_for_mux_master_ready()
19 for i in 1 2 3 4 5 6 7 8 9; do
20 ${SSH} -F $OBJ/ssh_config -S $CTL -Ocheck otherhost \
21 >/dev/null 2>&1 && return 0
24 fatal "mux master never becomes ready"
27 maybe_add_scp_path_to_sshd
32 trace "start master, fork to background"
33 ${SSH} -Nn2 -MS$CTL -F $OBJ/ssh_config -oSendEnv="_XXX_TEST" somehost \
34 -E $TEST_REGRESS_LOGFILE 2>&1 &
35 # NB. $SSH_PID will be killed by test-exec.sh:cleanup on fatal errors.
37 wait_for_mux_master_ready
42 verbose "test $tid: setenv"
43 trace "setenv over multiplexed connection"
44 _XXX_TEST=blah ${SSH} -F $OBJ/ssh_config -oSendEnv="_XXX_TEST" -S$CTL otherhost sh << 'EOF'
45 test X"$_XXX_TEST" = X"blah"
48 fail "environment not found"
51 verbose "test $tid: envpass"
52 trace "env passing over multiplexed connection"
53 ${SSH} -F $OBJ/ssh_config -oSetEnv="_XXX_TEST=foo" -S$CTL otherhost sh << 'EOF'
54 test X"$_XXX_TEST" = X"foo"
57 fail "environment not found"
61 verbose "test $tid: transfer"
63 trace "ssh transfer over multiplexed connection and check result"
64 ${SSH} -F $OBJ/ssh_config -S$CTL otherhost cat ${DATA} > ${COPY}
65 test -f ${COPY} || fail "ssh -Sctl: failed copy ${DATA}"
66 cmp ${DATA} ${COPY} || fail "ssh -Sctl: corrupted copy of ${DATA}"
69 trace "ssh transfer over multiplexed connection and check result"
70 ${SSH} -F $OBJ/ssh_config -S $CTL otherhost cat ${DATA} > ${COPY}
71 test -f ${COPY} || fail "ssh -S ctl: failed copy ${DATA}"
72 cmp ${DATA} ${COPY} || fail "ssh -S ctl: corrupted copy of ${DATA}"
75 trace "sftp transfer over multiplexed connection and check result"
76 echo "get ${DATA} ${COPY}" | \
77 ${SFTP} -S ${SSH} -F $OBJ/ssh_config -oControlPath=$CTL otherhost >>$TEST_REGRESS_LOGFILE 2>&1
78 test -f ${COPY} || fail "sftp: failed copy ${DATA}"
79 cmp ${DATA} ${COPY} || fail "sftp: corrupted copy of ${DATA}"
82 trace "scp transfer over multiplexed connection and check result"
83 ${SCP} -S ${SSH} -F $OBJ/ssh_config -oControlPath=$CTL otherhost:${DATA} ${COPY} >>$TEST_REGRESS_LOGFILE 2>&1
84 test -f ${COPY} || fail "scp: failed copy ${DATA}"
85 cmp ${DATA} ${COPY} || fail "scp: corrupted copy of ${DATA}"
88 verbose "test $tid: forward"
89 trace "forward over TCP/IP and check result"
90 $NC -N -l 127.0.0.1 $((${PORT} + 1)) < ${DATA} >`ssh_logfile nc` &
92 ${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
93 sleep 1 # XXX remove once race fixed
94 $NC 127.0.0.1 $((${PORT} + 2)) < /dev/null > ${COPY}
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 trace "forward over UNIX and check result"
100 $NC -N -Ul $OBJ/unix-1.fwd < ${DATA} > /dev/null &
102 ${SSH} -F $OBJ/ssh_config -S $CTL -Oforward -L$OBJ/unix-2.fwd:$OBJ/unix-1.fwd otherhost >>$TEST_SSH_LOGFILE 2>&1
103 ${SSH} -F $OBJ/ssh_config -S $CTL -Oforward -R$OBJ/unix-3.fwd:$OBJ/unix-2.fwd otherhost >>$TEST_SSH_LOGFILE 2>&1
104 sleep 1 # XXX remove once race fixed
105 $NC -U $OBJ/unix-3.fwd < /dev/null > ${COPY}
106 cmp ${DATA} ${COPY} || fail "ssh: corrupted copy of ${DATA}"
107 kill $netcat_pid 2>/dev/null
108 rm -f ${COPY} $OBJ/unix-[123].fwd
110 for s in 0 1 4 5 44; do
111 for mode in "" "-Oproxy"; do
112 trace "exit status $s over multiplexed connection ($mode)"
113 verbose "test $tid: status $s ($mode)"
114 ${SSH} -F $OBJ/ssh_config -S $CTL $mode otherhost exit $s
116 if [ $r -ne $s ]; then
117 fail "exit code mismatch: $r != $s"
120 # same with early close of stdout/err
121 trace "exit status $s with early close over multiplexed connection ($mode)"
122 ${SSH} -F $OBJ/ssh_config -S $CTL -n $mode otherhost \
123 exec sh -c \'"sleep 2; exec > /dev/null 2>&1; sleep 3; exit $s"\'
125 if [ $r -ne $s ]; then
126 fail "exit code (with sleep) mismatch: $r != $s"
131 verbose "test $tid: cmd check"
132 ${SSH} -F $OBJ/ssh_config -S $CTL -Ocheck otherhost >>$TEST_REGRESS_LOGFILE 2>&1 \
133 || fail "check command failed"
135 verbose "test $tid: cmd forward local (TCP)"
136 ${SSH} -F $OBJ/ssh_config -S $CTL -Oforward -L $P:localhost:$PORT otherhost \
137 || fail "request local forward failed"
138 sleep 1 # XXX remove once race fixed
139 ${SSH} -F $OBJ/ssh_config -p$P otherhost true \
140 || fail "connect to local forward port failed"
141 ${SSH} -F $OBJ/ssh_config -S $CTL -Ocancel -L $P:localhost:$PORT otherhost \
142 || fail "cancel local forward failed"
143 ${SSH} -F $OBJ/ssh_config -p$P otherhost true \
144 && fail "local forward port still listening"
146 verbose "test $tid: cmd forward remote (TCP)"
147 ${SSH} -F $OBJ/ssh_config -S $CTL -Oforward -R $P:localhost:$PORT otherhost \
148 || fail "request remote forward failed"
149 sleep 1 # XXX remove once race fixed
150 ${SSH} -F $OBJ/ssh_config -p$P otherhost true \
151 || fail "connect to remote forwarded port failed"
152 ${SSH} -F $OBJ/ssh_config -S $CTL -Ocancel -R $P:localhost:$PORT otherhost \
153 || fail "cancel remote forward failed"
154 ${SSH} -F $OBJ/ssh_config -p$P otherhost true \
155 && fail "remote forward port still listening"
157 verbose "test $tid: cmd forward local (UNIX)"
158 ${SSH} -F $OBJ/ssh_config -S $CTL -Oforward -L $OBJ/unix-1.fwd:localhost:$PORT otherhost \
159 || fail "request local forward failed"
160 sleep 1 # XXX remove once race fixed
161 echo "" | $NC -U $OBJ/unix-1.fwd | \
162 grep "Invalid SSH identification string" >/dev/null 2>&1 \
163 || fail "connect to local forward path failed"
164 ${SSH} -F $OBJ/ssh_config -S $CTL -Ocancel -L $OBJ/unix-1.fwd:localhost:$PORT otherhost \
165 || fail "cancel local forward failed"
166 N=$(echo "xyzzy" | $NC -U $OBJ/unix-1.fwd 2>&1 | grep "xyzzy" | wc -l)
167 test ${N} -eq 0 || fail "local forward path still listening"
168 rm -f $OBJ/unix-1.fwd
170 verbose "test $tid: cmd forward remote (UNIX)"
171 ${SSH} -F $OBJ/ssh_config -S $CTL -Oforward -R $OBJ/unix-1.fwd:localhost:$PORT otherhost \
172 || fail "request remote forward failed"
173 sleep 1 # XXX remove once race fixed
174 echo "" | $NC -U $OBJ/unix-1.fwd | \
175 grep "Invalid SSH identification string" >/dev/null 2>&1 \
176 || fail "connect to remote forwarded path failed"
177 ${SSH} -F $OBJ/ssh_config -S $CTL -Ocancel -R $OBJ/unix-1.fwd:localhost:$PORT otherhost \
178 || fail "cancel remote forward failed"
179 N=$(echo "xyzzy" | $NC -U $OBJ/unix-1.fwd 2>&1 | grep "xyzzy" | wc -l)
180 test ${N} -eq 0 || fail "remote forward path still listening"
181 rm -f $OBJ/unix-1.fwd
183 verbose "test $tid: cmd exit"
184 ${SSH} -F $OBJ/ssh_config -S $CTL -Oexit otherhost >>$TEST_REGRESS_LOGFILE 2>&1 \
185 || fail "send exit command failed"
187 # Wait for master to exit
189 kill -0 $SSH_PID >/dev/null 2>&1 && fail "exit command failed"
191 # Restart master and test -O stop command with master using -N
192 verbose "test $tid: cmd stop"
193 trace "restart master, fork to background"
196 # start a long-running command then immediately request a stop
197 ${SSH} -F $OBJ/ssh_config -S $CTL otherhost "sleep 10; exit 0" \
198 >>$TEST_REGRESS_LOGFILE 2>&1 &
200 ${SSH} -F $OBJ/ssh_config -S $CTL -Ostop otherhost >>$TEST_REGRESS_LOGFILE 2>&1 \
201 || fail "send stop command failed"
203 # wait until both long-running command and master have exited.
205 [ $! != 0 ] || fail "waiting for concurrent command"
207 [ $! != 0 ] || fail "waiting for master stop"
208 kill -0 $SSH_PID >/dev/null 2>&1 && fatal "stop command failed"
209 SSH_PID="" # Already gone, so don't kill in cleanup