blob: c75dee900dbb05fc3d2a1f78cf7215c07ee18c1e [file] [log] [blame]
djm@openbsd.org0d1b2412014-12-22 06:14:29 +00001# $OpenBSD: multiplex.sh,v 1.27 2014/12/22 06:14:29 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 Millerc8f610f2014-07-21 10:23:27 +10008if have_prog nc ; then
9 if nc -h 2>&1 | grep -- -N >/dev/null; then
10 NC="nc -N";
Damien Millera9c46742014-08-01 12:26:49 +100011 elif nc -h 2>&1 | grep -- "-U.*Use UNIX" >/dev/null ; then
Damien Millerc8f610f2014-07-21 10:23:27 +100012 NC="nc"
Damien Millera9c46742014-08-01 12:26:49 +100013 else
14 echo "nc is incompatible"
15 fi
16fi
17
18if test -z "$NC" ; then
19 echo "skipped (no compatible nc found)"
Damien Millerc8f610f2014-07-21 10:23:27 +100020 exit 0
21fi
22
23trace "will use ProxyCommand $proxycmd"
Damien Miller58497782011-01-17 16:17:09 +110024if config_defined DISABLE_FD_PASSING ; then
Darren Tucker2a81adc2004-08-29 17:09:34 +100025 echo "skipped (not supported on this platform)"
26 exit 0
27fi
28
Darren Tuckera4df65b2013-05-17 09:37:31 +100029P=3301 # test port
Darren Tuckerffaa6a52004-06-17 16:32:45 +100030
Darren Tuckeree4ad772012-10-05 12:04:10 +100031wait_for_mux_master_ready()
32{
33 for i in 1 2 3 4 5; do
34 ${SSH} -F $OBJ/ssh_config -S $CTL -Ocheck otherhost \
35 >/dev/null 2>&1 && return 0
36 sleep $i
37 done
38 fatal "mux master never becomes ready"
39}
40
Darren Tuckere7d05832004-06-16 20:22:22 +100041start_sshd
42
Darren Tucker40aaff72013-05-17 09:36:20 +100043start_mux_master()
44{
45 trace "start master, fork to background"
Damien Miller5ea4fe02014-07-22 09:39:19 +100046 ${SSH} -Nn2 -MS$CTL -F $OBJ/ssh_config -oSendEnv="_XXX_TEST" somehost \
Darren Tucker40aaff72013-05-17 09:36:20 +100047 -E $TEST_REGRESS_LOGFILE 2>&1 &
Damien Miller612f9652014-07-09 13:22:03 +100048 # NB. $SSH_PID will be killed by test-exec.sh:cleanup on fatal errors.
49 SSH_PID=$!
Darren Tucker40aaff72013-05-17 09:36:20 +100050 wait_for_mux_master_ready
51}
52
53start_mux_master
Darren Tuckere7d05832004-06-16 20:22:22 +100054
Damien Millere826a8c2004-06-18 01:23:03 +100055verbose "test $tid: envpass"
56trace "env passing over multiplexed connection"
Darren Tucker79885532009-10-07 10:30:57 +110057_XXX_TEST=blah ${SSH} -F $OBJ/ssh_config -oSendEnv="_XXX_TEST" -S$CTL otherhost sh << 'EOF'
Darren Tucker430c6a12004-06-22 13:38:56 +100058 test X"$_XXX_TEST" = X"blah"
59EOF
Damien Millere826a8c2004-06-18 01:23:03 +100060if [ $? -ne 0 ]; then
61 fail "environment not found"
62fi
63
64verbose "test $tid: transfer"
Darren Tuckerffaa6a52004-06-17 16:32:45 +100065rm -f ${COPY}
Darren Tuckere7d05832004-06-16 20:22:22 +100066trace "ssh transfer over multiplexed connection and check result"
Darren Tucker79885532009-10-07 10:30:57 +110067${SSH} -F $OBJ/ssh_config -S$CTL otherhost cat ${DATA} > ${COPY}
Darren Tucker3e86fc42004-06-17 16:34:02 +100068test -f ${COPY} || fail "ssh -Sctl: failed copy ${DATA}"
69cmp ${DATA} ${COPY} || fail "ssh -Sctl: 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 "ssh transfer over multiplexed connection and check result"
Darren Tucker79885532009-10-07 10:30:57 +110073${SSH} -F $OBJ/ssh_config -S $CTL otherhost cat ${DATA} > ${COPY}
Darren Tucker3e86fc42004-06-17 16:34:02 +100074test -f ${COPY} || fail "ssh -S ctl: failed copy ${DATA}"
75cmp ${DATA} ${COPY} || fail "ssh -S ctl: corrupted copy of ${DATA}"
Darren Tuckere7d05832004-06-16 20:22:22 +100076
Darren Tuckerffaa6a52004-06-17 16:32:45 +100077rm -f ${COPY}
Darren Tuckere7d05832004-06-16 20:22:22 +100078trace "sftp transfer over multiplexed connection and check result"
Darren Tuckerffaa6a52004-06-17 16:32:45 +100079echo "get ${DATA} ${COPY}" | \
Darren Tucker75129022013-05-17 09:19:10 +100080 ${SFTP} -S ${SSH} -F $OBJ/ssh_config -oControlPath=$CTL otherhost >>$TEST_REGRESS_LOGFILE 2>&1
Darren Tucker3e86fc42004-06-17 16:34:02 +100081test -f ${COPY} || fail "sftp: failed copy ${DATA}"
82cmp ${DATA} ${COPY} || fail "sftp: corrupted copy of ${DATA}"
Darren Tuckere7d05832004-06-16 20:22:22 +100083
Darren Tuckerffaa6a52004-06-17 16:32:45 +100084rm -f ${COPY}
Darren Tuckere7d05832004-06-16 20:22:22 +100085trace "scp transfer over multiplexed connection and check result"
Darren Tucker75129022013-05-17 09:19:10 +100086${SCP} -S ${SSH} -F $OBJ/ssh_config -oControlPath=$CTL otherhost:${DATA} ${COPY} >>$TEST_REGRESS_LOGFILE 2>&1
Darren Tucker3e86fc42004-06-17 16:34:02 +100087test -f ${COPY} || fail "scp: failed copy ${DATA}"
88cmp ${DATA} ${COPY} || fail "scp: corrupted copy of ${DATA}"
Darren Tuckere7d05832004-06-16 20:22:22 +100089
Darren Tuckerffaa6a52004-06-17 16:32:45 +100090rm -f ${COPY}
Damien Miller0e4e9552014-07-21 09:52:54 +100091verbose "test $tid: forward"
92trace "forward over TCP/IP and check result"
djm@openbsd.org6b405672014-12-22 01:14:49 +000093$NC -l 127.0.0.1 $((${PORT} + 1)) < ${DATA} > /dev/null &
Damien Miller0e4e9552014-07-21 09:52:54 +100094netcat_pid=$!
95${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 Millerf8988fb2014-08-01 13:31:52 +100096$NC -d 127.0.0.1 $((${PORT} + 2)) > ${COPY} < /dev/null
Damien Miller0e4e9552014-07-21 09:52:54 +100097cmp ${DATA} ${COPY} || fail "ssh: corrupted copy of ${DATA}"
98kill $netcat_pid 2>/dev/null
99rm -f ${COPY} $OBJ/unix-[123].fwd
100
101trace "forward over UNIX and check result"
djm@openbsd.org6b405672014-12-22 01:14:49 +0000102$NC -Ul $OBJ/unix-1.fwd < ${DATA} > /dev/null &
Damien Miller0e4e9552014-07-21 09:52:54 +1000103netcat_pid=$!
104${SSH} -F $OBJ/ssh_config -S $CTL -Oforward -L$OBJ/unix-2.fwd:$OBJ/unix-1.fwd otherhost >>$TEST_SSH_LOGFILE 2>&1
105${SSH} -F $OBJ/ssh_config -S $CTL -Oforward -R$OBJ/unix-3.fwd:$OBJ/unix-2.fwd otherhost >>$TEST_SSH_LOGFILE 2>&1
djm@openbsd.org6b405672014-12-22 01:14:49 +0000106$NC -d -U $OBJ/unix-3.fwd > ${COPY} < /dev/null
Damien Miller0e4e9552014-07-21 09:52:54 +1000107cmp ${DATA} ${COPY} || fail "ssh: corrupted copy of ${DATA}"
108kill $netcat_pid 2>/dev/null
109rm -f ${COPY} $OBJ/unix-[123].fwd
Darren Tuckerddea13d2004-06-17 16:27:43 +1000110
Darren Tuckere7d05832004-06-16 20:22:22 +1000111for s in 0 1 4 5 44; do
112 trace "exit status $s over multiplexed connection"
113 verbose "test $tid: status $s"
Darren Tucker79885532009-10-07 10:30:57 +1100114 ${SSH} -F $OBJ/ssh_config -S $CTL otherhost exit $s
Darren Tuckere7d05832004-06-16 20:22:22 +1000115 r=$?
116 if [ $r -ne $s ]; then
117 fail "exit code mismatch for protocol $p: $r != $s"
118 fi
119
120 # same with early close of stdout/err
121 trace "exit status $s with early close over multiplexed connection"
Darren Tucker79885532009-10-07 10:30:57 +1100122 ${SSH} -F $OBJ/ssh_config -S $CTL -n otherhost \
Darren Tuckere7d05832004-06-16 20:22:22 +1000123 exec sh -c \'"sleep 2; exec > /dev/null 2>&1; sleep 3; exit $s"\'
124 r=$?
125 if [ $r -ne $s ]; then
126 fail "exit code (with sleep) mismatch for protocol $p: $r != $s"
127 fi
128done
129
Darren Tucker9b2c0362012-10-05 11:45:39 +1000130verbose "test $tid: cmd check"
Darren Tucker75129022013-05-17 09:19:10 +1000131${SSH} -F $OBJ/ssh_config -S $CTL -Ocheck otherhost >>$TEST_REGRESS_LOGFILE 2>&1 \
Darren Tucker9b2c0362012-10-05 11:45:39 +1000132 || fail "check command failed"
Darren Tucker79ec66e2004-12-06 23:12:15 +1100133
Damien Miller0e4e9552014-07-21 09:52:54 +1000134verbose "test $tid: cmd forward local (TCP)"
Darren Tuckera4df65b2013-05-17 09:37:31 +1000135${SSH} -F $OBJ/ssh_config -S $CTL -Oforward -L $P:localhost:$PORT otherhost \
136 || fail "request local forward failed"
137${SSH} -F $OBJ/ssh_config -p$P otherhost true \
138 || fail "connect to local forward port failed"
139${SSH} -F $OBJ/ssh_config -S $CTL -Ocancel -L $P:localhost:$PORT otherhost \
140 || fail "cancel local forward failed"
141${SSH} -F $OBJ/ssh_config -p$P otherhost true \
142 && fail "local forward port still listening"
143
Damien Miller0e4e9552014-07-21 09:52:54 +1000144verbose "test $tid: cmd forward remote (TCP)"
Darren Tuckera4df65b2013-05-17 09:37:31 +1000145${SSH} -F $OBJ/ssh_config -S $CTL -Oforward -R $P:localhost:$PORT otherhost \
146 || fail "request remote forward failed"
147${SSH} -F $OBJ/ssh_config -p$P otherhost true \
148 || fail "connect to remote forwarded port failed"
149${SSH} -F $OBJ/ssh_config -S $CTL -Ocancel -R $P:localhost:$PORT otherhost \
150 || fail "cancel remote forward failed"
151${SSH} -F $OBJ/ssh_config -p$P otherhost true \
152 && fail "remote forward port still listening"
153
Damien Miller0e4e9552014-07-21 09:52:54 +1000154verbose "test $tid: cmd forward local (UNIX)"
155${SSH} -F $OBJ/ssh_config -S $CTL -Oforward -L $OBJ/unix-1.fwd:localhost:$PORT otherhost \
156 || fail "request local forward failed"
Damien Millerc8f610f2014-07-21 10:23:27 +1000157echo "" | $NC -U $OBJ/unix-1.fwd | grep "Protocol mismatch" >/dev/null 2>&1 \
Damien Miller0e4e9552014-07-21 09:52:54 +1000158 || fail "connect to local forward path failed"
159${SSH} -F $OBJ/ssh_config -S $CTL -Ocancel -L $OBJ/unix-1.fwd:localhost:$PORT otherhost \
160 || fail "cancel local forward failed"
Damien Miller04f48242014-07-22 11:31:47 +1000161N=$(echo "xyzzy" | $NC -U $OBJ/unix-1.fwd 2>&1 | grep "xyzzy" | wc -l)
Damien Miller0e4e9552014-07-21 09:52:54 +1000162test ${N} -eq 0 || fail "local forward path still listening"
163rm -f $OBJ/unix-1.fwd
164
165verbose "test $tid: cmd forward remote (UNIX)"
166${SSH} -F $OBJ/ssh_config -S $CTL -Oforward -R $OBJ/unix-1.fwd:localhost:$PORT otherhost \
167 || fail "request remote forward failed"
Damien Millerc8f610f2014-07-21 10:23:27 +1000168echo "" | $NC -U $OBJ/unix-1.fwd | grep "Protocol mismatch" >/dev/null 2>&1 \
Damien Miller0e4e9552014-07-21 09:52:54 +1000169 || 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"
Damien Miller04f48242014-07-22 11:31:47 +1000172N=$(echo "xyzzy" | $NC -U $OBJ/unix-1.fwd 2>&1 | grep "xyzzy" | wc -l)
Damien Miller56b840f2014-07-25 08:11:30 +1000173test ${N} -eq 0 || fail "remote forward path still listening"
Damien Miller0e4e9552014-07-21 09:52:54 +1000174rm -f $OBJ/unix-1.fwd
175
Darren Tucker9b2c0362012-10-05 11:45:39 +1000176verbose "test $tid: cmd exit"
Darren Tucker75129022013-05-17 09:19:10 +1000177${SSH} -F $OBJ/ssh_config -S $CTL -Oexit otherhost >>$TEST_REGRESS_LOGFILE 2>&1 \
Darren Tucker9b2c0362012-10-05 11:45:39 +1000178 || fail "send exit command failed"
Darren Tucker79ec66e2004-12-06 23:12:15 +1100179
180# Wait for master to exit
Damien Miller612f9652014-07-09 13:22:03 +1000181wait $SSH_PID
182kill -0 $SSH_PID >/dev/null 2>&1 && fail "exit command failed"
Darren Tucker6fc5aa82012-10-05 11:43:57 +1000183
184# Restart master and test -O stop command with master using -N
Darren Tuckeree4ad772012-10-05 12:04:10 +1000185verbose "test $tid: cmd stop"
186trace "restart master, fork to background"
Darren Tucker40aaff72013-05-17 09:36:20 +1000187start_mux_master
Darren Tuckeree4ad772012-10-05 12:04:10 +1000188
189# start a long-running command then immediately request a stop
190${SSH} -F $OBJ/ssh_config -S $CTL otherhost "sleep 10; exit 0" \
Darren Tucker75129022013-05-17 09:19:10 +1000191 >>$TEST_REGRESS_LOGFILE 2>&1 &
Darren Tucker9b2c0362012-10-05 11:45:39 +1000192SLEEP_PID=$!
Darren Tucker75129022013-05-17 09:19:10 +1000193${SSH} -F $OBJ/ssh_config -S $CTL -Ostop otherhost >>$TEST_REGRESS_LOGFILE 2>&1 \
Darren Tucker9b2c0362012-10-05 11:45:39 +1000194 || fail "send stop command failed"
Darren Tuckeree4ad772012-10-05 12:04:10 +1000195
196# wait until both long-running command and master have exited.
Darren Tucker9b2c0362012-10-05 11:45:39 +1000197wait $SLEEP_PID
Darren Tuckeree4ad772012-10-05 12:04:10 +1000198[ $! != 0 ] || fail "waiting for concurrent command"
Damien Miller612f9652014-07-09 13:22:03 +1000199wait $SSH_PID
Darren Tuckeree4ad772012-10-05 12:04:10 +1000200[ $! != 0 ] || fail "waiting for master stop"
Damien Miller612f9652014-07-09 13:22:03 +1000201kill -0 $SSH_PID >/dev/null 2>&1 && fatal "stop command failed"
202SSH_PID="" # Already gone, so don't kill in cleanup
203