1295367Sdes# $OpenBSD: multiplex.sh,v 1.27 2014/12/22 06:14:29 djm Exp $ 2137015Sdes# Placed in the Public Domain. 3137015Sdes 4147001SdesCTL=/tmp/openssh.regress.ctl-sock.$$ 5137015Sdes 6137015Sdestid="connection multiplexing" 7137015Sdes 8295367SdesNC=$OBJ/netcat 9295367Sdes 10295367Sdestrace "will use ProxyCommand $proxycmd" 11218767Sdesif config_defined DISABLE_FD_PASSING ; then 12146998Sdes echo "skipped (not supported on this platform)" 13146998Sdes exit 0 14146998Sdesfi 15146998Sdes 16255670SdesP=3301 # test port 17137015Sdes 18248613Sdeswait_for_mux_master_ready() 19248613Sdes{ 20248613Sdes for i in 1 2 3 4 5; do 21248613Sdes ${SSH} -F $OBJ/ssh_config -S $CTL -Ocheck otherhost \ 22248613Sdes >/dev/null 2>&1 && return 0 23248613Sdes sleep $i 24248613Sdes done 25248613Sdes fatal "mux master never becomes ready" 26248613Sdes} 27248613Sdes 28137015Sdesstart_sshd 29137015Sdes 30255670Sdesstart_mux_master() 31255670Sdes{ 32255670Sdes trace "start master, fork to background" 33255670Sdes ${SSH} -Nn2 -MS$CTL -F $OBJ/ssh_config -oSendEnv="_XXX_TEST" somehost \ 34255670Sdes -E $TEST_REGRESS_LOGFILE 2>&1 & 35295367Sdes # NB. $SSH_PID will be killed by test-exec.sh:cleanup on fatal errors. 36295367Sdes SSH_PID=$! 37255670Sdes wait_for_mux_master_ready 38255670Sdes} 39137015Sdes 40255670Sdesstart_mux_master 41255670Sdes 42137015Sdesverbose "test $tid: envpass" 43137015Sdestrace "env passing over multiplexed connection" 44204861Sdes_XXX_TEST=blah ${SSH} -F $OBJ/ssh_config -oSendEnv="_XXX_TEST" -S$CTL otherhost sh << 'EOF' 45137015Sdes test X"$_XXX_TEST" = X"blah" 46137015SdesEOF 47137015Sdesif [ $? -ne 0 ]; then 48137015Sdes fail "environment not found" 49137015Sdesfi 50137015Sdes 51137015Sdesverbose "test $tid: transfer" 52137015Sdesrm -f ${COPY} 53137015Sdestrace "ssh transfer over multiplexed connection and check result" 54204861Sdes${SSH} -F $OBJ/ssh_config -S$CTL otherhost cat ${DATA} > ${COPY} 55137015Sdestest -f ${COPY} || fail "ssh -Sctl: failed copy ${DATA}" 56137015Sdescmp ${DATA} ${COPY} || fail "ssh -Sctl: corrupted copy of ${DATA}" 57137015Sdes 58137015Sdesrm -f ${COPY} 59137015Sdestrace "ssh transfer over multiplexed connection and check result" 60204861Sdes${SSH} -F $OBJ/ssh_config -S $CTL otherhost cat ${DATA} > ${COPY} 61137015Sdestest -f ${COPY} || fail "ssh -S ctl: failed copy ${DATA}" 62137015Sdescmp ${DATA} ${COPY} || fail "ssh -S ctl: corrupted copy of ${DATA}" 63137015Sdes 64137015Sdesrm -f ${COPY} 65137015Sdestrace "sftp transfer over multiplexed connection and check result" 66137015Sdesecho "get ${DATA} ${COPY}" | \ 67255670Sdes ${SFTP} -S ${SSH} -F $OBJ/ssh_config -oControlPath=$CTL otherhost >>$TEST_REGRESS_LOGFILE 2>&1 68137015Sdestest -f ${COPY} || fail "sftp: failed copy ${DATA}" 69137015Sdescmp ${DATA} ${COPY} || fail "sftp: corrupted copy of ${DATA}" 70137015Sdes 71137015Sdesrm -f ${COPY} 72137015Sdestrace "scp transfer over multiplexed connection and check result" 73255670Sdes${SCP} -S ${SSH} -F $OBJ/ssh_config -oControlPath=$CTL otherhost:${DATA} ${COPY} >>$TEST_REGRESS_LOGFILE 2>&1 74137015Sdestest -f ${COPY} || fail "scp: failed copy ${DATA}" 75137015Sdescmp ${DATA} ${COPY} || fail "scp: corrupted copy of ${DATA}" 76137015Sdes 77137015Sdesrm -f ${COPY} 78295367Sdesverbose "test $tid: forward" 79295367Sdestrace "forward over TCP/IP and check result" 80295367Sdes$NC -N -l 127.0.0.1 $((${PORT} + 1)) < ${DATA} > /dev/null & 81295367Sdesnetcat_pid=$! 82295367Sdes${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 83295367Sdes$NC 127.0.0.1 $((${PORT} + 2)) < /dev/null > ${COPY} 84295367Sdescmp ${DATA} ${COPY} || fail "ssh: corrupted copy of ${DATA}" 85295367Sdeskill $netcat_pid 2>/dev/null 86295367Sdesrm -f ${COPY} $OBJ/unix-[123].fwd 87137015Sdes 88295367Sdestrace "forward over UNIX and check result" 89295367Sdes$NC -N -Ul $OBJ/unix-1.fwd < ${DATA} > /dev/null & 90295367Sdesnetcat_pid=$! 91295367Sdes${SSH} -F $OBJ/ssh_config -S $CTL -Oforward -L$OBJ/unix-2.fwd:$OBJ/unix-1.fwd otherhost >>$TEST_SSH_LOGFILE 2>&1 92295367Sdes${SSH} -F $OBJ/ssh_config -S $CTL -Oforward -R$OBJ/unix-3.fwd:$OBJ/unix-2.fwd otherhost >>$TEST_SSH_LOGFILE 2>&1 93295367Sdes$NC -U $OBJ/unix-3.fwd < /dev/null > ${COPY} 2>/dev/null 94295367Sdescmp ${DATA} ${COPY} || fail "ssh: corrupted copy of ${DATA}" 95295367Sdeskill $netcat_pid 2>/dev/null 96295367Sdesrm -f ${COPY} $OBJ/unix-[123].fwd 97295367Sdes 98137015Sdesfor s in 0 1 4 5 44; do 99137015Sdes trace "exit status $s over multiplexed connection" 100137015Sdes verbose "test $tid: status $s" 101204861Sdes ${SSH} -F $OBJ/ssh_config -S $CTL otherhost exit $s 102137015Sdes r=$? 103137015Sdes if [ $r -ne $s ]; then 104137015Sdes fail "exit code mismatch for protocol $p: $r != $s" 105137015Sdes fi 106137015Sdes 107137015Sdes # same with early close of stdout/err 108137015Sdes trace "exit status $s with early close over multiplexed connection" 109204861Sdes ${SSH} -F $OBJ/ssh_config -S $CTL -n otherhost \ 110137015Sdes exec sh -c \'"sleep 2; exec > /dev/null 2>&1; sleep 3; exit $s"\' 111137015Sdes r=$? 112137015Sdes if [ $r -ne $s ]; then 113137015Sdes fail "exit code (with sleep) mismatch for protocol $p: $r != $s" 114137015Sdes fi 115137015Sdesdone 116137015Sdes 117248613Sdesverbose "test $tid: cmd check" 118255670Sdes${SSH} -F $OBJ/ssh_config -S $CTL -Ocheck otherhost >>$TEST_REGRESS_LOGFILE 2>&1 \ 119248613Sdes || fail "check command failed" 120146998Sdes 121295367Sdesverbose "test $tid: cmd forward local (TCP)" 122255670Sdes${SSH} -F $OBJ/ssh_config -S $CTL -Oforward -L $P:localhost:$PORT otherhost \ 123255670Sdes || fail "request local forward failed" 124255670Sdes${SSH} -F $OBJ/ssh_config -p$P otherhost true \ 125255670Sdes || fail "connect to local forward port failed" 126255670Sdes${SSH} -F $OBJ/ssh_config -S $CTL -Ocancel -L $P:localhost:$PORT otherhost \ 127255670Sdes || fail "cancel local forward failed" 128255670Sdes${SSH} -F $OBJ/ssh_config -p$P otherhost true \ 129255670Sdes && fail "local forward port still listening" 130255670Sdes 131295367Sdesverbose "test $tid: cmd forward remote (TCP)" 132255670Sdes${SSH} -F $OBJ/ssh_config -S $CTL -Oforward -R $P:localhost:$PORT otherhost \ 133255670Sdes || fail "request remote forward failed" 134255670Sdes${SSH} -F $OBJ/ssh_config -p$P otherhost true \ 135255670Sdes || fail "connect to remote forwarded port failed" 136255670Sdes${SSH} -F $OBJ/ssh_config -S $CTL -Ocancel -R $P:localhost:$PORT otherhost \ 137255670Sdes || fail "cancel remote forward failed" 138255670Sdes${SSH} -F $OBJ/ssh_config -p$P otherhost true \ 139255670Sdes && fail "remote forward port still listening" 140255670Sdes 141295367Sdesverbose "test $tid: cmd forward local (UNIX)" 142295367Sdes${SSH} -F $OBJ/ssh_config -S $CTL -Oforward -L $OBJ/unix-1.fwd:localhost:$PORT otherhost \ 143295367Sdes || fail "request local forward failed" 144295367Sdesecho "" | $NC -U $OBJ/unix-1.fwd | grep "Protocol mismatch" >/dev/null 2>&1 \ 145295367Sdes || fail "connect to local forward path failed" 146295367Sdes${SSH} -F $OBJ/ssh_config -S $CTL -Ocancel -L $OBJ/unix-1.fwd:localhost:$PORT otherhost \ 147295367Sdes || fail "cancel local forward failed" 148295367SdesN=$(echo "xyzzy" | $NC -U $OBJ/unix-1.fwd 2>&1 | grep "xyzzy" | wc -l) 149295367Sdestest ${N} -eq 0 || fail "local forward path still listening" 150295367Sdesrm -f $OBJ/unix-1.fwd 151295367Sdes 152295367Sdesverbose "test $tid: cmd forward remote (UNIX)" 153295367Sdes${SSH} -F $OBJ/ssh_config -S $CTL -Oforward -R $OBJ/unix-1.fwd:localhost:$PORT otherhost \ 154295367Sdes || fail "request remote forward failed" 155295367Sdesecho "" | $NC -U $OBJ/unix-1.fwd | grep "Protocol mismatch" >/dev/null 2>&1 \ 156295367Sdes || fail "connect to remote forwarded path failed" 157295367Sdes${SSH} -F $OBJ/ssh_config -S $CTL -Ocancel -R $OBJ/unix-1.fwd:localhost:$PORT otherhost \ 158295367Sdes || fail "cancel remote forward failed" 159295367SdesN=$(echo "xyzzy" | $NC -U $OBJ/unix-1.fwd 2>&1 | grep "xyzzy" | wc -l) 160295367Sdestest ${N} -eq 0 || fail "remote forward path still listening" 161295367Sdesrm -f $OBJ/unix-1.fwd 162295367Sdes 163248613Sdesverbose "test $tid: cmd exit" 164255670Sdes${SSH} -F $OBJ/ssh_config -S $CTL -Oexit otherhost >>$TEST_REGRESS_LOGFILE 2>&1 \ 165248613Sdes || fail "send exit command failed" 166146998Sdes 167146998Sdes# Wait for master to exit 168295367Sdeswait $SSH_PID 169295367Sdeskill -0 $SSH_PID >/dev/null 2>&1 && fail "exit command failed" 170146998Sdes 171248613Sdes# Restart master and test -O stop command with master using -N 172248613Sdesverbose "test $tid: cmd stop" 173248613Sdestrace "restart master, fork to background" 174255670Sdesstart_mux_master 175248613Sdes 176248613Sdes# start a long-running command then immediately request a stop 177248613Sdes${SSH} -F $OBJ/ssh_config -S $CTL otherhost "sleep 10; exit 0" \ 178255670Sdes >>$TEST_REGRESS_LOGFILE 2>&1 & 179248613SdesSLEEP_PID=$! 180255670Sdes${SSH} -F $OBJ/ssh_config -S $CTL -Ostop otherhost >>$TEST_REGRESS_LOGFILE 2>&1 \ 181248613Sdes || fail "send stop command failed" 182248613Sdes 183248613Sdes# wait until both long-running command and master have exited. 184248613Sdeswait $SLEEP_PID 185248613Sdes[ $! != 0 ] || fail "waiting for concurrent command" 186295367Sdeswait $SSH_PID 187248613Sdes[ $! != 0 ] || fail "waiting for master stop" 188295367Sdeskill -0 $SSH_PID >/dev/null 2>&1 && fatal "stop command failed" 189295367SdesSSH_PID="" # Already gone, so don't kill in cleanup 190295367Sdes 191