blob: 078a53a889e51508c5f678afd526fbc2dacf2860 [file] [log] [blame]
djm@openbsd.orgdd369322017-04-30 23:34:55 +00001# $OpenBSD: multiplex.sh,v 1.28 2017/04/30 23:34:55 djm Exp $
Darren Tuckere7d05832004-06-16 20:22:22 +10002# Placed in the Public Domain.
3
Darren Tuckeraf342552005-04-25 17:01:26 +10004CTL=/tmp/openssh.regress.ctl-sock.$$
Darren Tuckere7d05832004-06-16 20:22:22 +10005
6tid="connection multiplexing"
7
Damien Miller293cac52014-12-22 16:30:42 +11008NC=$OBJ/netcat
Damien Millerc8f610f2014-07-21 10:23:27 +10009
10trace "will use ProxyCommand $proxycmd"
Damien Miller58497782011-01-17 16:17:09 +110011if config_defined DISABLE_FD_PASSING ; then
Darren Tucker2a81adc2004-08-29 17:09:34 +100012 echo "skipped (not supported on this platform)"
13 exit 0
14fi
15
Darren Tuckera4df65b2013-05-17 09:37:31 +100016P=3301 # test port
Darren Tuckerffaa6a52004-06-17 16:32:45 +100017
Darren Tuckeree4ad772012-10-05 12:04:10 +100018wait_for_mux_master_ready()
19{
20 for i in 1 2 3 4 5; do
21 ${SSH} -F $OBJ/ssh_config -S $CTL -Ocheck otherhost \
22 >/dev/null 2>&1 && return 0
23 sleep $i
24 done
25 fatal "mux master never becomes ready"
26}
27
Darren Tuckere7d05832004-06-16 20:22:22 +100028start_sshd
29
Darren Tucker40aaff72013-05-17 09:36:20 +100030start_mux_master()
31{
32 trace "start master, fork to background"
Damien Miller5ea4fe02014-07-22 09:39:19 +100033 ${SSH} -Nn2 -MS$CTL -F $OBJ/ssh_config -oSendEnv="_XXX_TEST" somehost \
Darren Tucker40aaff72013-05-17 09:36:20 +100034 -E $TEST_REGRESS_LOGFILE 2>&1 &
Damien Miller612f9652014-07-09 13:22:03 +100035 # NB. $SSH_PID will be killed by test-exec.sh:cleanup on fatal errors.
36 SSH_PID=$!
Darren Tucker40aaff72013-05-17 09:36:20 +100037 wait_for_mux_master_ready
38}
39
40start_mux_master
Darren Tuckere7d05832004-06-16 20:22:22 +100041
Damien Millere826a8c2004-06-18 01:23:03 +100042verbose "test $tid: envpass"
43trace "env passing over multiplexed connection"
Darren Tucker79885532009-10-07 10:30:57 +110044_XXX_TEST=blah ${SSH} -F $OBJ/ssh_config -oSendEnv="_XXX_TEST" -S$CTL otherhost sh << 'EOF'
Darren Tucker430c6a12004-06-22 13:38:56 +100045 test X"$_XXX_TEST" = X"blah"
46EOF
Damien Millere826a8c2004-06-18 01:23:03 +100047if [ $? -ne 0 ]; then
48 fail "environment not found"
49fi
50
51verbose "test $tid: transfer"
Darren Tuckerffaa6a52004-06-17 16:32:45 +100052rm -f ${COPY}
Darren Tuckere7d05832004-06-16 20:22:22 +100053trace "ssh transfer over multiplexed connection and check result"
Darren Tucker79885532009-10-07 10:30:57 +110054${SSH} -F $OBJ/ssh_config -S$CTL otherhost cat ${DATA} > ${COPY}
Darren Tucker3e86fc42004-06-17 16:34:02 +100055test -f ${COPY} || fail "ssh -Sctl: failed copy ${DATA}"
56cmp ${DATA} ${COPY} || fail "ssh -Sctl: corrupted copy of ${DATA}"
Darren Tuckere7d05832004-06-16 20:22:22 +100057
Darren Tuckerffaa6a52004-06-17 16:32:45 +100058rm -f ${COPY}
Darren Tuckere7d05832004-06-16 20:22:22 +100059trace "ssh transfer over multiplexed connection and check result"
Darren Tucker79885532009-10-07 10:30:57 +110060${SSH} -F $OBJ/ssh_config -S $CTL otherhost cat ${DATA} > ${COPY}
Darren Tucker3e86fc42004-06-17 16:34:02 +100061test -f ${COPY} || fail "ssh -S ctl: failed copy ${DATA}"
62cmp ${DATA} ${COPY} || fail "ssh -S ctl: corrupted copy of ${DATA}"
Darren Tuckere7d05832004-06-16 20:22:22 +100063
Darren Tuckerffaa6a52004-06-17 16:32:45 +100064rm -f ${COPY}
Darren Tuckere7d05832004-06-16 20:22:22 +100065trace "sftp transfer over multiplexed connection and check result"
Darren Tuckerffaa6a52004-06-17 16:32:45 +100066echo "get ${DATA} ${COPY}" | \
Darren Tucker75129022013-05-17 09:19:10 +100067 ${SFTP} -S ${SSH} -F $OBJ/ssh_config -oControlPath=$CTL otherhost >>$TEST_REGRESS_LOGFILE 2>&1
Darren Tucker3e86fc42004-06-17 16:34:02 +100068test -f ${COPY} || fail "sftp: failed copy ${DATA}"
69cmp ${DATA} ${COPY} || fail "sftp: corrupted copy of ${DATA}"
Darren Tuckere7d05832004-06-16 20:22:22 +100070
Darren Tuckerffaa6a52004-06-17 16:32:45 +100071rm -f ${COPY}
Darren Tuckere7d05832004-06-16 20:22:22 +100072trace "scp transfer over multiplexed connection and check result"
Darren Tucker75129022013-05-17 09:19:10 +100073${SCP} -S ${SSH} -F $OBJ/ssh_config -oControlPath=$CTL otherhost:${DATA} ${COPY} >>$TEST_REGRESS_LOGFILE 2>&1
Darren Tucker3e86fc42004-06-17 16:34:02 +100074test -f ${COPY} || fail "scp: failed copy ${DATA}"
75cmp ${DATA} ${COPY} || fail "scp: corrupted copy of ${DATA}"
Darren Tuckere7d05832004-06-16 20:22:22 +100076
Darren Tuckerffaa6a52004-06-17 16:32:45 +100077rm -f ${COPY}
Damien Miller0e4e9552014-07-21 09:52:54 +100078verbose "test $tid: forward"
79trace "forward over TCP/IP and check result"
Damien Miller293cac52014-12-22 16:30:42 +110080$NC -N -l 127.0.0.1 $((${PORT} + 1)) < ${DATA} > /dev/null &
Damien Miller0e4e9552014-07-21 09:52:54 +100081netcat_pid=$!
82${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 Miller293cac52014-12-22 16:30:42 +110083$NC 127.0.0.1 $((${PORT} + 2)) < /dev/null > ${COPY}
Damien Miller0e4e9552014-07-21 09:52:54 +100084cmp ${DATA} ${COPY} || fail "ssh: corrupted copy of ${DATA}"
85kill $netcat_pid 2>/dev/null
86rm -f ${COPY} $OBJ/unix-[123].fwd
87
88trace "forward over UNIX and check result"
Damien Miller293cac52014-12-22 16:30:42 +110089$NC -N -Ul $OBJ/unix-1.fwd < ${DATA} > /dev/null &
Damien Miller0e4e9552014-07-21 09:52:54 +100090netcat_pid=$!
91${SSH} -F $OBJ/ssh_config -S $CTL -Oforward -L$OBJ/unix-2.fwd:$OBJ/unix-1.fwd otherhost >>$TEST_SSH_LOGFILE 2>&1
92${SSH} -F $OBJ/ssh_config -S $CTL -Oforward -R$OBJ/unix-3.fwd:$OBJ/unix-2.fwd otherhost >>$TEST_SSH_LOGFILE 2>&1
Damien Millerbd588532015-02-25 16:58:22 -080093$NC -U $OBJ/unix-3.fwd < /dev/null > ${COPY} 2>/dev/null
Damien Miller0e4e9552014-07-21 09:52:54 +100094cmp ${DATA} ${COPY} || fail "ssh: corrupted copy of ${DATA}"
95kill $netcat_pid 2>/dev/null
96rm -f ${COPY} $OBJ/unix-[123].fwd
Darren Tuckerddea13d2004-06-17 16:27:43 +100097
Darren Tuckere7d05832004-06-16 20:22:22 +100098for s in 0 1 4 5 44; do
99 trace "exit status $s over multiplexed connection"
100 verbose "test $tid: status $s"
Darren Tucker79885532009-10-07 10:30:57 +1100101 ${SSH} -F $OBJ/ssh_config -S $CTL otherhost exit $s
Darren Tuckere7d05832004-06-16 20:22:22 +1000102 r=$?
103 if [ $r -ne $s ]; then
djm@openbsd.orgdd369322017-04-30 23:34:55 +0000104 fail "exit code mismatch: $r != $s"
Darren Tuckere7d05832004-06-16 20:22:22 +1000105 fi
106
107 # same with early close of stdout/err
108 trace "exit status $s with early close over multiplexed connection"
Darren Tucker79885532009-10-07 10:30:57 +1100109 ${SSH} -F $OBJ/ssh_config -S $CTL -n otherhost \
Darren Tuckere7d05832004-06-16 20:22:22 +1000110 exec sh -c \'"sleep 2; exec > /dev/null 2>&1; sleep 3; exit $s"\'
111 r=$?
112 if [ $r -ne $s ]; then
djm@openbsd.orgdd369322017-04-30 23:34:55 +0000113 fail "exit code (with sleep) mismatch: $r != $s"
Darren Tuckere7d05832004-06-16 20:22:22 +1000114 fi
115done
116
Darren Tucker9b2c0362012-10-05 11:45:39 +1000117verbose "test $tid: cmd check"
Darren Tucker75129022013-05-17 09:19:10 +1000118${SSH} -F $OBJ/ssh_config -S $CTL -Ocheck otherhost >>$TEST_REGRESS_LOGFILE 2>&1 \
Darren Tucker9b2c0362012-10-05 11:45:39 +1000119 || fail "check command failed"
Darren Tucker79ec66e2004-12-06 23:12:15 +1100120
Damien Miller0e4e9552014-07-21 09:52:54 +1000121verbose "test $tid: cmd forward local (TCP)"
Darren Tuckera4df65b2013-05-17 09:37:31 +1000122${SSH} -F $OBJ/ssh_config -S $CTL -Oforward -L $P:localhost:$PORT otherhost \
123 || fail "request local forward failed"
124${SSH} -F $OBJ/ssh_config -p$P otherhost true \
125 || fail "connect to local forward port failed"
126${SSH} -F $OBJ/ssh_config -S $CTL -Ocancel -L $P:localhost:$PORT otherhost \
127 || fail "cancel local forward failed"
128${SSH} -F $OBJ/ssh_config -p$P otherhost true \
129 && fail "local forward port still listening"
130
Damien Miller0e4e9552014-07-21 09:52:54 +1000131verbose "test $tid: cmd forward remote (TCP)"
Darren Tuckera4df65b2013-05-17 09:37:31 +1000132${SSH} -F $OBJ/ssh_config -S $CTL -Oforward -R $P:localhost:$PORT otherhost \
133 || fail "request remote forward failed"
134${SSH} -F $OBJ/ssh_config -p$P otherhost true \
135 || fail "connect to remote forwarded port failed"
136${SSH} -F $OBJ/ssh_config -S $CTL -Ocancel -R $P:localhost:$PORT otherhost \
137 || fail "cancel remote forward failed"
138${SSH} -F $OBJ/ssh_config -p$P otherhost true \
139 && fail "remote forward port still listening"
140
Damien Miller0e4e9552014-07-21 09:52:54 +1000141verbose "test $tid: cmd forward local (UNIX)"
142${SSH} -F $OBJ/ssh_config -S $CTL -Oforward -L $OBJ/unix-1.fwd:localhost:$PORT otherhost \
143 || fail "request local forward failed"
Damien Millerc8f610f2014-07-21 10:23:27 +1000144echo "" | $NC -U $OBJ/unix-1.fwd | grep "Protocol mismatch" >/dev/null 2>&1 \
Damien Miller0e4e9552014-07-21 09:52:54 +1000145 || fail "connect to local forward path failed"
146${SSH} -F $OBJ/ssh_config -S $CTL -Ocancel -L $OBJ/unix-1.fwd:localhost:$PORT otherhost \
147 || fail "cancel local forward failed"
Damien Miller04f48242014-07-22 11:31:47 +1000148N=$(echo "xyzzy" | $NC -U $OBJ/unix-1.fwd 2>&1 | grep "xyzzy" | wc -l)
Damien Miller0e4e9552014-07-21 09:52:54 +1000149test ${N} -eq 0 || fail "local forward path still listening"
150rm -f $OBJ/unix-1.fwd
151
152verbose "test $tid: cmd forward remote (UNIX)"
153${SSH} -F $OBJ/ssh_config -S $CTL -Oforward -R $OBJ/unix-1.fwd:localhost:$PORT otherhost \
154 || fail "request remote forward failed"
Damien Millerc8f610f2014-07-21 10:23:27 +1000155echo "" | $NC -U $OBJ/unix-1.fwd | grep "Protocol mismatch" >/dev/null 2>&1 \
Damien Miller0e4e9552014-07-21 09:52:54 +1000156 || fail "connect to remote forwarded path failed"
157${SSH} -F $OBJ/ssh_config -S $CTL -Ocancel -R $OBJ/unix-1.fwd:localhost:$PORT otherhost \
158 || fail "cancel remote forward failed"
Damien Miller04f48242014-07-22 11:31:47 +1000159N=$(echo "xyzzy" | $NC -U $OBJ/unix-1.fwd 2>&1 | grep "xyzzy" | wc -l)
Damien Miller56b840f2014-07-25 08:11:30 +1000160test ${N} -eq 0 || fail "remote forward path still listening"
Damien Miller0e4e9552014-07-21 09:52:54 +1000161rm -f $OBJ/unix-1.fwd
162
Darren Tucker9b2c0362012-10-05 11:45:39 +1000163verbose "test $tid: cmd exit"
Darren Tucker75129022013-05-17 09:19:10 +1000164${SSH} -F $OBJ/ssh_config -S $CTL -Oexit otherhost >>$TEST_REGRESS_LOGFILE 2>&1 \
Darren Tucker9b2c0362012-10-05 11:45:39 +1000165 || fail "send exit command failed"
Darren Tucker79ec66e2004-12-06 23:12:15 +1100166
167# Wait for master to exit
Damien Miller612f9652014-07-09 13:22:03 +1000168wait $SSH_PID
169kill -0 $SSH_PID >/dev/null 2>&1 && fail "exit command failed"
Darren Tucker6fc5aa82012-10-05 11:43:57 +1000170
171# Restart master and test -O stop command with master using -N
Darren Tuckeree4ad772012-10-05 12:04:10 +1000172verbose "test $tid: cmd stop"
173trace "restart master, fork to background"
Darren Tucker40aaff72013-05-17 09:36:20 +1000174start_mux_master
Darren Tuckeree4ad772012-10-05 12:04:10 +1000175
176# start a long-running command then immediately request a stop
177${SSH} -F $OBJ/ssh_config -S $CTL otherhost "sleep 10; exit 0" \
Darren Tucker75129022013-05-17 09:19:10 +1000178 >>$TEST_REGRESS_LOGFILE 2>&1 &
Darren Tucker9b2c0362012-10-05 11:45:39 +1000179SLEEP_PID=$!
Darren Tucker75129022013-05-17 09:19:10 +1000180${SSH} -F $OBJ/ssh_config -S $CTL -Ostop otherhost >>$TEST_REGRESS_LOGFILE 2>&1 \
Darren Tucker9b2c0362012-10-05 11:45:39 +1000181 || fail "send stop command failed"
Darren Tuckeree4ad772012-10-05 12:04:10 +1000182
183# wait until both long-running command and master have exited.
Darren Tucker9b2c0362012-10-05 11:45:39 +1000184wait $SLEEP_PID
Darren Tuckeree4ad772012-10-05 12:04:10 +1000185[ $! != 0 ] || fail "waiting for concurrent command"
Damien Miller612f9652014-07-09 13:22:03 +1000186wait $SSH_PID
Darren Tuckeree4ad772012-10-05 12:04:10 +1000187[ $! != 0 ] || fail "waiting for master stop"
Damien Miller612f9652014-07-09 13:22:03 +1000188kill -0 $SSH_PID >/dev/null 2>&1 && fatal "stop command failed"
189SSH_PID="" # Already gone, so don't kill in cleanup
190