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