1# $OpenBSD: multiplex.sh,v 1.36 2023/03/01 09:29:32 dtucker 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 20maybe_add_scp_path_to_sshd 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: setenv" 36trace "setenv 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: envpass" 45trace "env passing over multiplexed connection" 46${SSH} -F $OBJ/ssh_config -oSetEnv="_XXX_TEST=foo" -S$CTL otherhost sh << 'EOF' 47 test X"$_XXX_TEST" = X"foo" 48EOF 49if [ $? -ne 0 ]; then 50 fail "environment not found" 51fi 52 53 54verbose "test $tid: transfer" 55rm -f ${COPY} 56trace "ssh transfer over multiplexed connection and check result" 57${SSH} -F $OBJ/ssh_config -S$CTL otherhost cat ${DATA} > ${COPY} 58test -f ${COPY} || fail "ssh -Sctl: failed copy ${DATA}" 59cmp ${DATA} ${COPY} || fail "ssh -Sctl: corrupted copy of ${DATA}" 60 61rm -f ${COPY} 62trace "ssh transfer over multiplexed connection and check result" 63${SSH} -F $OBJ/ssh_config -S $CTL otherhost cat ${DATA} > ${COPY} 64test -f ${COPY} || fail "ssh -S ctl: failed copy ${DATA}" 65cmp ${DATA} ${COPY} || fail "ssh -S ctl: corrupted copy of ${DATA}" 66 67rm -f ${COPY} 68trace "sftp transfer over multiplexed connection and check result" 69echo "get ${DATA} ${COPY}" | \ 70 ${SFTP} -S ${SSH} -F $OBJ/ssh_config -oControlPath=$CTL otherhost >>$TEST_REGRESS_LOGFILE 2>&1 71test -f ${COPY} || fail "sftp: failed copy ${DATA}" 72cmp ${DATA} ${COPY} || fail "sftp: corrupted copy of ${DATA}" 73 74rm -f ${COPY} 75trace "scp transfer over multiplexed connection and check result" 76${SCP} -S ${SSH} -F $OBJ/ssh_config -oControlPath=$CTL otherhost:${DATA} ${COPY} >>$TEST_REGRESS_LOGFILE 2>&1 77test -f ${COPY} || fail "scp: failed copy ${DATA}" 78cmp ${DATA} ${COPY} || fail "scp: corrupted copy of ${DATA}" 79 80rm -f ${COPY} 81verbose "test $tid: forward" 82trace "forward over TCP/IP and check result" 83$NC -N -l 127.0.0.1 $((${PORT} + 1)) < ${DATA} >`ssh_logfile nc` & 84netcat_pid=$! 85${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 86sleep 1 # XXX remove once race fixed 87$NC 127.0.0.1 $((${PORT} + 2)) < /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 92trace "forward over UNIX and check result" 93$NC -N -Ul $OBJ/unix-1.fwd < ${DATA} > /dev/null & 94netcat_pid=$! 95${SSH} -F $OBJ/ssh_config -S $CTL -Oforward -L$OBJ/unix-2.fwd:$OBJ/unix-1.fwd otherhost >>$TEST_SSH_LOGFILE 2>&1 96${SSH} -F $OBJ/ssh_config -S $CTL -Oforward -R$OBJ/unix-3.fwd:$OBJ/unix-2.fwd otherhost >>$TEST_SSH_LOGFILE 2>&1 97sleep 1 # XXX remove once race fixed 98$NC -U $OBJ/unix-3.fwd < /dev/null > ${COPY} 99cmp ${DATA} ${COPY} || fail "ssh: corrupted copy of ${DATA}" 100kill $netcat_pid 2>/dev/null 101rm -f ${COPY} $OBJ/unix-[123].fwd 102 103for s in 0 1 4 5 44; do 104 for mode in "" "-Oproxy"; do 105 trace "exit status $s over multiplexed connection ($mode)" 106 verbose "test $tid: status $s ($mode)" 107 ${SSH} -F $OBJ/ssh_config -S $CTL $mode otherhost exit $s 108 r=$? 109 if [ $r -ne $s ]; then 110 fail "exit code mismatch: $r != $s" 111 fi 112 113 # same with early close of stdout/err 114 trace "exit status $s with early close over multiplexed connection ($mode)" 115 ${SSH} -F $OBJ/ssh_config -S $CTL -n $mode otherhost \ 116 exec sh -c \'"sleep 2; exec > /dev/null 2>&1; sleep 3; exit $s"\' 117 r=$? 118 if [ $r -ne $s ]; then 119 fail "exit code (with sleep) mismatch: $r != $s" 120 fi 121 done 122done 123 124verbose "test $tid: cmd check" 125${SSH} -F $OBJ/ssh_config -S $CTL -Ocheck otherhost >>$TEST_REGRESS_LOGFILE 2>&1 \ 126 || fail "check command failed" 127 128verbose "test $tid: cmd forward local (TCP)" 129${SSH} -F $OBJ/ssh_config -S $CTL -Oforward -L $P:localhost:$PORT otherhost \ 130 || fail "request local forward failed" 131sleep 1 # XXX remove once race fixed 132${SSH} -F $OBJ/ssh_config -p$P otherhost true \ 133 || fail "connect to local forward port failed" 134${SSH} -F $OBJ/ssh_config -S $CTL -Ocancel -L $P:localhost:$PORT otherhost \ 135 || fail "cancel local forward failed" 136${SSH} -F $OBJ/ssh_config -p$P otherhost true \ 137 && fail "local forward port still listening" 138 139verbose "test $tid: cmd forward remote (TCP)" 140${SSH} -F $OBJ/ssh_config -S $CTL -Oforward -R $P:localhost:$PORT otherhost \ 141 || fail "request remote forward failed" 142sleep 1 # XXX remove once race fixed 143${SSH} -F $OBJ/ssh_config -p$P otherhost true \ 144 || fail "connect to remote forwarded port failed" 145${SSH} -F $OBJ/ssh_config -S $CTL -Ocancel -R $P:localhost:$PORT otherhost \ 146 || fail "cancel remote forward failed" 147${SSH} -F $OBJ/ssh_config -p$P otherhost true \ 148 && fail "remote forward port still listening" 149 150verbose "test $tid: cmd forward local (UNIX)" 151${SSH} -F $OBJ/ssh_config -S $CTL -Oforward -L $OBJ/unix-1.fwd:localhost:$PORT otherhost \ 152 || fail "request local forward failed" 153sleep 1 # XXX remove once race fixed 154echo "" | $NC -U $OBJ/unix-1.fwd | \ 155 grep "Invalid SSH identification string" >/dev/null 2>&1 \ 156 || fail "connect to local forward path failed" 157${SSH} -F $OBJ/ssh_config -S $CTL -Ocancel -L $OBJ/unix-1.fwd:localhost:$PORT otherhost \ 158 || fail "cancel local forward failed" 159N=$(echo "xyzzy" | $NC -U $OBJ/unix-1.fwd 2>&1 | grep "xyzzy" | wc -l) 160test ${N} -eq 0 || fail "local forward path still listening" 161rm -f $OBJ/unix-1.fwd 162 163verbose "test $tid: cmd forward remote (UNIX)" 164${SSH} -F $OBJ/ssh_config -S $CTL -Oforward -R $OBJ/unix-1.fwd:localhost:$PORT otherhost \ 165 || fail "request remote forward failed" 166sleep 1 # XXX remove once race fixed 167echo "" | $NC -U $OBJ/unix-1.fwd | \ 168 grep "Invalid SSH identification string" >/dev/null 2>&1 \ 169 || fail "connect to remote forwarded path failed" 170${SSH} -F $OBJ/ssh_config -S $CTL -Ocancel -R $OBJ/unix-1.fwd:localhost:$PORT otherhost \ 171 || fail "cancel remote forward failed" 172N=$(echo "xyzzy" | $NC -U $OBJ/unix-1.fwd 2>&1 | grep "xyzzy" | wc -l) 173test ${N} -eq 0 || fail "remote forward path still listening" 174rm -f $OBJ/unix-1.fwd 175 176verbose "test $tid: cmd exit" 177${SSH} -F $OBJ/ssh_config -S $CTL -Oexit otherhost >>$TEST_REGRESS_LOGFILE 2>&1 \ 178 || fail "send exit command failed" 179 180# Wait for master to exit 181wait $SSH_PID 182kill -0 $SSH_PID >/dev/null 2>&1 && fail "exit command failed" 183 184# Restart master and test -O stop command with master using -N 185verbose "test $tid: cmd stop" 186trace "restart master, fork to background" 187start_mux_master 188 189# start a long-running command then immediately request a stop 190${SSH} -F $OBJ/ssh_config -S $CTL otherhost "sleep 10; exit 0" \ 191 >>$TEST_REGRESS_LOGFILE 2>&1 & 192SLEEP_PID=$! 193${SSH} -F $OBJ/ssh_config -S $CTL -Ostop otherhost >>$TEST_REGRESS_LOGFILE 2>&1 \ 194 || fail "send stop command failed" 195 196# wait until both long-running command and master have exited. 197wait $SLEEP_PID 198[ $! != 0 ] || fail "waiting for concurrent command" 199wait $SSH_PID 200[ $! != 0 ] || fail "waiting for master stop" 201kill -0 $SSH_PID >/dev/null 2>&1 && fatal "stop command failed" 202SSH_PID="" # Already gone, so don't kill in cleanup 203 204