Darren Tucker | cea099a | 2014-07-23 10:04:02 +1000 | [diff] [blame] | 1 | # $OpenBSD: multiplex.sh,v 1.25 2014/07/22 01:32:12 djm Exp $ |
Darren Tucker | e7d0583 | 2004-06-16 20:22:22 +1000 | [diff] [blame] | 2 | # Placed in the Public Domain. |
| 3 | |
Darren Tucker | af34255 | 2005-04-25 17:01:26 +1000 | [diff] [blame] | 4 | CTL=/tmp/openssh.regress.ctl-sock.$$ |
Darren Tucker | e7d0583 | 2004-06-16 20:22:22 +1000 | [diff] [blame] | 5 | |
| 6 | tid="connection multiplexing" |
| 7 | |
Damien Miller | c8f610f | 2014-07-21 10:23:27 +1000 | [diff] [blame] | 8 | if have_prog nc ; then |
| 9 | if nc -h 2>&1 | grep -- -N >/dev/null; then |
| 10 | NC="nc -N"; |
| 11 | else |
| 12 | NC="nc" |
| 13 | fi |
| 14 | else |
| 15 | echo "skipped (no nc found)" |
| 16 | exit 0 |
| 17 | fi |
| 18 | |
| 19 | trace "will use ProxyCommand $proxycmd" |
Damien Miller | 5849778 | 2011-01-17 16:17:09 +1100 | [diff] [blame] | 20 | if config_defined DISABLE_FD_PASSING ; then |
Darren Tucker | 2a81adc | 2004-08-29 17:09:34 +1000 | [diff] [blame] | 21 | echo "skipped (not supported on this platform)" |
| 22 | exit 0 |
| 23 | fi |
| 24 | |
Darren Tucker | a4df65b | 2013-05-17 09:37:31 +1000 | [diff] [blame] | 25 | P=3301 # test port |
Darren Tucker | ffaa6a5 | 2004-06-17 16:32:45 +1000 | [diff] [blame] | 26 | |
Darren Tucker | ee4ad77 | 2012-10-05 12:04:10 +1000 | [diff] [blame] | 27 | wait_for_mux_master_ready() |
| 28 | { |
| 29 | for i in 1 2 3 4 5; do |
| 30 | ${SSH} -F $OBJ/ssh_config -S $CTL -Ocheck otherhost \ |
| 31 | >/dev/null 2>&1 && return 0 |
| 32 | sleep $i |
| 33 | done |
| 34 | fatal "mux master never becomes ready" |
| 35 | } |
| 36 | |
Darren Tucker | e7d0583 | 2004-06-16 20:22:22 +1000 | [diff] [blame] | 37 | start_sshd |
| 38 | |
Darren Tucker | 40aaff7 | 2013-05-17 09:36:20 +1000 | [diff] [blame] | 39 | start_mux_master() |
| 40 | { |
| 41 | trace "start master, fork to background" |
Damien Miller | 5ea4fe0 | 2014-07-22 09:39:19 +1000 | [diff] [blame] | 42 | ${SSH} -Nn2 -MS$CTL -F $OBJ/ssh_config -oSendEnv="_XXX_TEST" somehost \ |
Darren Tucker | 40aaff7 | 2013-05-17 09:36:20 +1000 | [diff] [blame] | 43 | -E $TEST_REGRESS_LOGFILE 2>&1 & |
Damien Miller | 612f965 | 2014-07-09 13:22:03 +1000 | [diff] [blame] | 44 | # NB. $SSH_PID will be killed by test-exec.sh:cleanup on fatal errors. |
| 45 | SSH_PID=$! |
Darren Tucker | 40aaff7 | 2013-05-17 09:36:20 +1000 | [diff] [blame] | 46 | wait_for_mux_master_ready |
| 47 | } |
| 48 | |
| 49 | start_mux_master |
Darren Tucker | e7d0583 | 2004-06-16 20:22:22 +1000 | [diff] [blame] | 50 | |
Damien Miller | e826a8c | 2004-06-18 01:23:03 +1000 | [diff] [blame] | 51 | verbose "test $tid: envpass" |
| 52 | trace "env passing over multiplexed connection" |
Darren Tucker | 7988553 | 2009-10-07 10:30:57 +1100 | [diff] [blame] | 53 | _XXX_TEST=blah ${SSH} -F $OBJ/ssh_config -oSendEnv="_XXX_TEST" -S$CTL otherhost sh << 'EOF' |
Darren Tucker | 430c6a1 | 2004-06-22 13:38:56 +1000 | [diff] [blame] | 54 | test X"$_XXX_TEST" = X"blah" |
| 55 | EOF |
Damien Miller | e826a8c | 2004-06-18 01:23:03 +1000 | [diff] [blame] | 56 | if [ $? -ne 0 ]; then |
| 57 | fail "environment not found" |
| 58 | fi |
| 59 | |
| 60 | verbose "test $tid: transfer" |
Darren Tucker | ffaa6a5 | 2004-06-17 16:32:45 +1000 | [diff] [blame] | 61 | rm -f ${COPY} |
Darren Tucker | e7d0583 | 2004-06-16 20:22:22 +1000 | [diff] [blame] | 62 | trace "ssh transfer over multiplexed connection and check result" |
Darren Tucker | 7988553 | 2009-10-07 10:30:57 +1100 | [diff] [blame] | 63 | ${SSH} -F $OBJ/ssh_config -S$CTL otherhost cat ${DATA} > ${COPY} |
Darren Tucker | 3e86fc4 | 2004-06-17 16:34:02 +1000 | [diff] [blame] | 64 | test -f ${COPY} || fail "ssh -Sctl: failed copy ${DATA}" |
| 65 | cmp ${DATA} ${COPY} || fail "ssh -Sctl: corrupted copy of ${DATA}" |
Darren Tucker | e7d0583 | 2004-06-16 20:22:22 +1000 | [diff] [blame] | 66 | |
Darren Tucker | ffaa6a5 | 2004-06-17 16:32:45 +1000 | [diff] [blame] | 67 | rm -f ${COPY} |
Darren Tucker | e7d0583 | 2004-06-16 20:22:22 +1000 | [diff] [blame] | 68 | trace "ssh transfer over multiplexed connection and check result" |
Darren Tucker | 7988553 | 2009-10-07 10:30:57 +1100 | [diff] [blame] | 69 | ${SSH} -F $OBJ/ssh_config -S $CTL otherhost cat ${DATA} > ${COPY} |
Darren Tucker | 3e86fc4 | 2004-06-17 16:34:02 +1000 | [diff] [blame] | 70 | test -f ${COPY} || fail "ssh -S ctl: failed copy ${DATA}" |
| 71 | cmp ${DATA} ${COPY} || fail "ssh -S ctl: corrupted copy of ${DATA}" |
Darren Tucker | e7d0583 | 2004-06-16 20:22:22 +1000 | [diff] [blame] | 72 | |
Darren Tucker | ffaa6a5 | 2004-06-17 16:32:45 +1000 | [diff] [blame] | 73 | rm -f ${COPY} |
Darren Tucker | e7d0583 | 2004-06-16 20:22:22 +1000 | [diff] [blame] | 74 | trace "sftp transfer over multiplexed connection and check result" |
Darren Tucker | ffaa6a5 | 2004-06-17 16:32:45 +1000 | [diff] [blame] | 75 | echo "get ${DATA} ${COPY}" | \ |
Darren Tucker | 7512902 | 2013-05-17 09:19:10 +1000 | [diff] [blame] | 76 | ${SFTP} -S ${SSH} -F $OBJ/ssh_config -oControlPath=$CTL otherhost >>$TEST_REGRESS_LOGFILE 2>&1 |
Darren Tucker | 3e86fc4 | 2004-06-17 16:34:02 +1000 | [diff] [blame] | 77 | test -f ${COPY} || fail "sftp: failed copy ${DATA}" |
| 78 | cmp ${DATA} ${COPY} || fail "sftp: corrupted copy of ${DATA}" |
Darren Tucker | e7d0583 | 2004-06-16 20:22:22 +1000 | [diff] [blame] | 79 | |
Darren Tucker | ffaa6a5 | 2004-06-17 16:32:45 +1000 | [diff] [blame] | 80 | rm -f ${COPY} |
Darren Tucker | e7d0583 | 2004-06-16 20:22:22 +1000 | [diff] [blame] | 81 | trace "scp transfer over multiplexed connection and check result" |
Darren Tucker | 7512902 | 2013-05-17 09:19:10 +1000 | [diff] [blame] | 82 | ${SCP} -S ${SSH} -F $OBJ/ssh_config -oControlPath=$CTL otherhost:${DATA} ${COPY} >>$TEST_REGRESS_LOGFILE 2>&1 |
Darren Tucker | 3e86fc4 | 2004-06-17 16:34:02 +1000 | [diff] [blame] | 83 | test -f ${COPY} || fail "scp: failed copy ${DATA}" |
| 84 | cmp ${DATA} ${COPY} || fail "scp: corrupted copy of ${DATA}" |
Darren Tucker | e7d0583 | 2004-06-16 20:22:22 +1000 | [diff] [blame] | 85 | |
Darren Tucker | ffaa6a5 | 2004-06-17 16:32:45 +1000 | [diff] [blame] | 86 | rm -f ${COPY} |
Damien Miller | 0e4e955 | 2014-07-21 09:52:54 +1000 | [diff] [blame] | 87 | verbose "test $tid: forward" |
| 88 | trace "forward over TCP/IP and check result" |
Damien Miller | c8f610f | 2014-07-21 10:23:27 +1000 | [diff] [blame] | 89 | $NC -l 127.0.0.1 $((${PORT} + 1)) < ${DATA} & |
Damien Miller | 0e4e955 | 2014-07-21 09:52:54 +1000 | [diff] [blame] | 90 | netcat_pid=$! |
| 91 | ${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 |
Damien Miller | c8f610f | 2014-07-21 10:23:27 +1000 | [diff] [blame] | 92 | $NC 127.0.0.1 $((${PORT} + 2)) > ${COPY} |
Damien Miller | 0e4e955 | 2014-07-21 09:52:54 +1000 | [diff] [blame] | 93 | cmp ${DATA} ${COPY} || fail "ssh: corrupted copy of ${DATA}" |
| 94 | kill $netcat_pid 2>/dev/null |
| 95 | rm -f ${COPY} $OBJ/unix-[123].fwd |
| 96 | |
| 97 | trace "forward over UNIX and check result" |
Damien Miller | c8f610f | 2014-07-21 10:23:27 +1000 | [diff] [blame] | 98 | $NC -Ul $OBJ/unix-1.fwd < ${DATA} & |
Damien Miller | 0e4e955 | 2014-07-21 09:52:54 +1000 | [diff] [blame] | 99 | netcat_pid=$! |
| 100 | ${SSH} -F $OBJ/ssh_config -S $CTL -Oforward -L$OBJ/unix-2.fwd:$OBJ/unix-1.fwd otherhost >>$TEST_SSH_LOGFILE 2>&1 |
| 101 | ${SSH} -F $OBJ/ssh_config -S $CTL -Oforward -R$OBJ/unix-3.fwd:$OBJ/unix-2.fwd otherhost >>$TEST_SSH_LOGFILE 2>&1 |
Damien Miller | c8f610f | 2014-07-21 10:23:27 +1000 | [diff] [blame] | 102 | $NC -U $OBJ/unix-3.fwd > ${COPY} |
Damien Miller | 0e4e955 | 2014-07-21 09:52:54 +1000 | [diff] [blame] | 103 | cmp ${DATA} ${COPY} || fail "ssh: corrupted copy of ${DATA}" |
| 104 | kill $netcat_pid 2>/dev/null |
| 105 | rm -f ${COPY} $OBJ/unix-[123].fwd |
Darren Tucker | ddea13d | 2004-06-17 16:27:43 +1000 | [diff] [blame] | 106 | |
Darren Tucker | e7d0583 | 2004-06-16 20:22:22 +1000 | [diff] [blame] | 107 | for s in 0 1 4 5 44; do |
| 108 | trace "exit status $s over multiplexed connection" |
| 109 | verbose "test $tid: status $s" |
Darren Tucker | 7988553 | 2009-10-07 10:30:57 +1100 | [diff] [blame] | 110 | ${SSH} -F $OBJ/ssh_config -S $CTL otherhost exit $s |
Darren Tucker | e7d0583 | 2004-06-16 20:22:22 +1000 | [diff] [blame] | 111 | r=$? |
| 112 | if [ $r -ne $s ]; then |
| 113 | fail "exit code mismatch for protocol $p: $r != $s" |
| 114 | fi |
| 115 | |
| 116 | # same with early close of stdout/err |
| 117 | trace "exit status $s with early close over multiplexed connection" |
Darren Tucker | 7988553 | 2009-10-07 10:30:57 +1100 | [diff] [blame] | 118 | ${SSH} -F $OBJ/ssh_config -S $CTL -n otherhost \ |
Darren Tucker | e7d0583 | 2004-06-16 20:22:22 +1000 | [diff] [blame] | 119 | exec sh -c \'"sleep 2; exec > /dev/null 2>&1; sleep 3; exit $s"\' |
| 120 | r=$? |
| 121 | if [ $r -ne $s ]; then |
| 122 | fail "exit code (with sleep) mismatch for protocol $p: $r != $s" |
| 123 | fi |
| 124 | done |
| 125 | |
Darren Tucker | 9b2c036 | 2012-10-05 11:45:39 +1000 | [diff] [blame] | 126 | verbose "test $tid: cmd check" |
Darren Tucker | 7512902 | 2013-05-17 09:19:10 +1000 | [diff] [blame] | 127 | ${SSH} -F $OBJ/ssh_config -S $CTL -Ocheck otherhost >>$TEST_REGRESS_LOGFILE 2>&1 \ |
Darren Tucker | 9b2c036 | 2012-10-05 11:45:39 +1000 | [diff] [blame] | 128 | || fail "check command failed" |
Darren Tucker | 79ec66e | 2004-12-06 23:12:15 +1100 | [diff] [blame] | 129 | |
Damien Miller | 0e4e955 | 2014-07-21 09:52:54 +1000 | [diff] [blame] | 130 | verbose "test $tid: cmd forward local (TCP)" |
Darren Tucker | a4df65b | 2013-05-17 09:37:31 +1000 | [diff] [blame] | 131 | ${SSH} -F $OBJ/ssh_config -S $CTL -Oforward -L $P:localhost:$PORT otherhost \ |
| 132 | || fail "request local forward failed" |
| 133 | ${SSH} -F $OBJ/ssh_config -p$P otherhost true \ |
| 134 | || fail "connect to local forward port failed" |
| 135 | ${SSH} -F $OBJ/ssh_config -S $CTL -Ocancel -L $P:localhost:$PORT otherhost \ |
| 136 | || fail "cancel local forward failed" |
| 137 | ${SSH} -F $OBJ/ssh_config -p$P otherhost true \ |
| 138 | && fail "local forward port still listening" |
| 139 | |
Damien Miller | 0e4e955 | 2014-07-21 09:52:54 +1000 | [diff] [blame] | 140 | verbose "test $tid: cmd forward remote (TCP)" |
Darren Tucker | a4df65b | 2013-05-17 09:37:31 +1000 | [diff] [blame] | 141 | ${SSH} -F $OBJ/ssh_config -S $CTL -Oforward -R $P:localhost:$PORT otherhost \ |
| 142 | || fail "request remote forward failed" |
| 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 | |
Damien Miller | 0e4e955 | 2014-07-21 09:52:54 +1000 | [diff] [blame] | 150 | verbose "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" |
Damien Miller | c8f610f | 2014-07-21 10:23:27 +1000 | [diff] [blame] | 153 | echo "" | $NC -U $OBJ/unix-1.fwd | grep "Protocol mismatch" >/dev/null 2>&1 \ |
Damien Miller | 0e4e955 | 2014-07-21 09:52:54 +1000 | [diff] [blame] | 154 | || fail "connect to local forward path failed" |
| 155 | ${SSH} -F $OBJ/ssh_config -S $CTL -Ocancel -L $OBJ/unix-1.fwd:localhost:$PORT otherhost \ |
| 156 | || fail "cancel local forward failed" |
Damien Miller | 04f4824 | 2014-07-22 11:31:47 +1000 | [diff] [blame] | 157 | N=$(echo "xyzzy" | $NC -U $OBJ/unix-1.fwd 2>&1 | grep "xyzzy" | wc -l) |
Damien Miller | 0e4e955 | 2014-07-21 09:52:54 +1000 | [diff] [blame] | 158 | test ${N} -eq 0 || fail "local forward path still listening" |
| 159 | rm -f $OBJ/unix-1.fwd |
| 160 | |
| 161 | verbose "test $tid: cmd forward remote (UNIX)" |
| 162 | ${SSH} -F $OBJ/ssh_config -S $CTL -Oforward -R $OBJ/unix-1.fwd:localhost:$PORT otherhost \ |
| 163 | || fail "request remote forward failed" |
Damien Miller | c8f610f | 2014-07-21 10:23:27 +1000 | [diff] [blame] | 164 | echo "" | $NC -U $OBJ/unix-1.fwd | grep "Protocol mismatch" >/dev/null 2>&1 \ |
Damien Miller | 0e4e955 | 2014-07-21 09:52:54 +1000 | [diff] [blame] | 165 | || fail "connect to remote forwarded path failed" |
| 166 | ${SSH} -F $OBJ/ssh_config -S $CTL -Ocancel -R $OBJ/unix-1.fwd:localhost:$PORT otherhost \ |
| 167 | || fail "cancel remote forward failed" |
Damien Miller | 04f4824 | 2014-07-22 11:31:47 +1000 | [diff] [blame] | 168 | N=$(echo "xyzzy" | $NC -U $OBJ/unix-1.fwd 2>&1 | grep "xyzzy" | wc -l) |
Damien Miller | 56b840f | 2014-07-25 08:11:30 +1000 | [diff] [blame^] | 169 | test ${N} -eq 0 || fail "remote forward path still listening" |
Damien Miller | 0e4e955 | 2014-07-21 09:52:54 +1000 | [diff] [blame] | 170 | rm -f $OBJ/unix-1.fwd |
| 171 | |
Darren Tucker | 9b2c036 | 2012-10-05 11:45:39 +1000 | [diff] [blame] | 172 | verbose "test $tid: cmd exit" |
Darren Tucker | 7512902 | 2013-05-17 09:19:10 +1000 | [diff] [blame] | 173 | ${SSH} -F $OBJ/ssh_config -S $CTL -Oexit otherhost >>$TEST_REGRESS_LOGFILE 2>&1 \ |
Darren Tucker | 9b2c036 | 2012-10-05 11:45:39 +1000 | [diff] [blame] | 174 | || fail "send exit command failed" |
Darren Tucker | 79ec66e | 2004-12-06 23:12:15 +1100 | [diff] [blame] | 175 | |
| 176 | # Wait for master to exit |
Damien Miller | 612f965 | 2014-07-09 13:22:03 +1000 | [diff] [blame] | 177 | wait $SSH_PID |
| 178 | kill -0 $SSH_PID >/dev/null 2>&1 && fail "exit command failed" |
Darren Tucker | 6fc5aa8 | 2012-10-05 11:43:57 +1000 | [diff] [blame] | 179 | |
| 180 | # Restart master and test -O stop command with master using -N |
Darren Tucker | ee4ad77 | 2012-10-05 12:04:10 +1000 | [diff] [blame] | 181 | verbose "test $tid: cmd stop" |
| 182 | trace "restart master, fork to background" |
Darren Tucker | 40aaff7 | 2013-05-17 09:36:20 +1000 | [diff] [blame] | 183 | start_mux_master |
Darren Tucker | ee4ad77 | 2012-10-05 12:04:10 +1000 | [diff] [blame] | 184 | |
| 185 | # start a long-running command then immediately request a stop |
| 186 | ${SSH} -F $OBJ/ssh_config -S $CTL otherhost "sleep 10; exit 0" \ |
Darren Tucker | 7512902 | 2013-05-17 09:19:10 +1000 | [diff] [blame] | 187 | >>$TEST_REGRESS_LOGFILE 2>&1 & |
Darren Tucker | 9b2c036 | 2012-10-05 11:45:39 +1000 | [diff] [blame] | 188 | SLEEP_PID=$! |
Darren Tucker | 7512902 | 2013-05-17 09:19:10 +1000 | [diff] [blame] | 189 | ${SSH} -F $OBJ/ssh_config -S $CTL -Ostop otherhost >>$TEST_REGRESS_LOGFILE 2>&1 \ |
Darren Tucker | 9b2c036 | 2012-10-05 11:45:39 +1000 | [diff] [blame] | 190 | || fail "send stop command failed" |
Darren Tucker | ee4ad77 | 2012-10-05 12:04:10 +1000 | [diff] [blame] | 191 | |
| 192 | # wait until both long-running command and master have exited. |
Darren Tucker | 9b2c036 | 2012-10-05 11:45:39 +1000 | [diff] [blame] | 193 | wait $SLEEP_PID |
Darren Tucker | ee4ad77 | 2012-10-05 12:04:10 +1000 | [diff] [blame] | 194 | [ $! != 0 ] || fail "waiting for concurrent command" |
Damien Miller | 612f965 | 2014-07-09 13:22:03 +1000 | [diff] [blame] | 195 | wait $SSH_PID |
Darren Tucker | ee4ad77 | 2012-10-05 12:04:10 +1000 | [diff] [blame] | 196 | [ $! != 0 ] || fail "waiting for master stop" |
Damien Miller | 612f965 | 2014-07-09 13:22:03 +1000 | [diff] [blame] | 197 | kill -0 $SSH_PID >/dev/null 2>&1 && fatal "stop command failed" |
| 198 | SSH_PID="" # Already gone, so don't kill in cleanup |
| 199 | |