1# $OpenBSD: multiplex.sh,v 1.31 2020/01/25 00:27:56 dtucker 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 6 7 8 9; 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 76sleep 1 # XXX remove once race fixed 77$NC 127.0.0.1 $((${PORT} + 2)) < /dev/null > ${COPY} 78cmp ${DATA} ${COPY} || fail "ssh: corrupted copy of ${DATA}" 79kill $netcat_pid 2>/dev/null 80rm -f ${COPY} $OBJ/unix-[123].fwd 81 82trace "forward over UNIX and check result" 83$NC -N -Ul $OBJ/unix-1.fwd < ${DATA} > /dev/null & 84netcat_pid=$! 85${SSH} -F $OBJ/ssh_config -S $CTL -Oforward -L$OBJ/unix-2.fwd:$OBJ/unix-1.fwd otherhost >>$TEST_SSH_LOGFILE 2>&1 86${SSH} -F $OBJ/ssh_config -S $CTL -Oforward -R$OBJ/unix-3.fwd:$OBJ/unix-2.fwd otherhost >>$TEST_SSH_LOGFILE 2>&1 87sleep 1 # XXX remove once race fixed 88$NC -U $OBJ/unix-3.fwd < /dev/null > ${COPY} 89cmp ${DATA} ${COPY} || fail "ssh: corrupted copy of ${DATA}" 90kill $netcat_pid 2>/dev/null 91rm -f ${COPY} $OBJ/unix-[123].fwd 92 93for s in 0 1 4 5 44; do 94 trace "exit status $s over multiplexed connection" 95 verbose "test $tid: status $s" 96 ${SSH} -F $OBJ/ssh_config -S $CTL 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" 104 ${SSH} -F $OBJ/ssh_config -S $CTL -n 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 110done 111 112verbose "test $tid: cmd check" 113${SSH} -F $OBJ/ssh_config -S $CTL -Ocheck otherhost >>$TEST_REGRESS_LOGFILE 2>&1 \ 114 || fail "check command failed" 115 116verbose "test $tid: cmd forward local (TCP)" 117${SSH} -F $OBJ/ssh_config -S $CTL -Oforward -L $P:localhost:$PORT otherhost \ 118 || fail "request local forward failed" 119sleep 1 # XXX remove once race fixed 120${SSH} -F $OBJ/ssh_config -p$P otherhost true \ 121 || fail "connect to local forward port failed" 122${SSH} -F $OBJ/ssh_config -S $CTL -Ocancel -L $P:localhost:$PORT otherhost \ 123 || fail "cancel local forward failed" 124${SSH} -F $OBJ/ssh_config -p$P otherhost true \ 125 && fail "local forward port still listening" 126 127verbose "test $tid: cmd forward remote (TCP)" 128${SSH} -F $OBJ/ssh_config -S $CTL -Oforward -R $P:localhost:$PORT otherhost \ 129 || fail "request remote forward failed" 130sleep 1 # XXX remove once race fixed 131${SSH} -F $OBJ/ssh_config -p$P otherhost true \ 132 || fail "connect to remote forwarded port failed" 133${SSH} -F $OBJ/ssh_config -S $CTL -Ocancel -R $P:localhost:$PORT otherhost \ 134 || fail "cancel remote forward failed" 135${SSH} -F $OBJ/ssh_config -p$P otherhost true \ 136 && fail "remote forward port still listening" 137 138verbose "test $tid: cmd forward local (UNIX)" 139${SSH} -F $OBJ/ssh_config -S $CTL -Oforward -L $OBJ/unix-1.fwd:localhost:$PORT otherhost \ 140 || fail "request local forward failed" 141sleep 1 # XXX remove once race fixed 142echo "" | $NC -U $OBJ/unix-1.fwd | \ 143 grep "Invalid SSH identification string" >/dev/null 2>&1 \ 144 || fail "connect to local forward path failed" 145${SSH} -F $OBJ/ssh_config -S $CTL -Ocancel -L $OBJ/unix-1.fwd:localhost:$PORT otherhost \ 146 || fail "cancel local forward failed" 147N=$(echo "xyzzy" | $NC -U $OBJ/unix-1.fwd 2>&1 | grep "xyzzy" | wc -l) 148test ${N} -eq 0 || fail "local forward path still listening" 149rm -f $OBJ/unix-1.fwd 150 151verbose "test $tid: cmd forward remote (UNIX)" 152${SSH} -F $OBJ/ssh_config -S $CTL -Oforward -R $OBJ/unix-1.fwd:localhost:$PORT otherhost \ 153 || fail "request remote forward failed" 154sleep 1 # XXX remove once race fixed 155echo "" | $NC -U $OBJ/unix-1.fwd | \ 156 grep "Invalid SSH identification string" >/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" 160N=$(echo "xyzzy" | $NC -U $OBJ/unix-1.fwd 2>&1 | grep "xyzzy" | wc -l) 161test ${N} -eq 0 || fail "remote forward path still listening" 162rm -f $OBJ/unix-1.fwd 163 164verbose "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" 167 168# Wait for master to exit 169wait $SSH_PID 170kill -0 $SSH_PID >/dev/null 2>&1 && fail "exit command failed" 171 172# Restart master and test -O stop command with master using -N 173verbose "test $tid: cmd stop" 174trace "restart master, fork to background" 175start_mux_master 176 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 & 180SLEEP_PID=$! 181${SSH} -F $OBJ/ssh_config -S $CTL -Ostop otherhost >>$TEST_REGRESS_LOGFILE 2>&1 \ 182 || fail "send stop command failed" 183 184# wait until both long-running command and master have exited. 185wait $SLEEP_PID 186[ $! != 0 ] || fail "waiting for concurrent command" 187wait $SSH_PID 188[ $! != 0 ] || fail "waiting for master stop" 189kill -0 $SSH_PID >/dev/null 2>&1 && fatal "stop command failed" 190SSH_PID="" # Already gone, so don't kill in cleanup 191 192