blob: 693211bff1aade8a6b723429b1b18dea5fc1568b [file] [log] [blame]
Darren Tuckercea099a2014-07-23 10:04:02 +10001# $OpenBSD: multiplex.sh,v 1.25 2014/07/22 01:32:12 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";
11 else
12 NC="nc"
13 fi
14else
15 echo "skipped (no nc found)"
16 exit 0
17fi
18
19trace "will use ProxyCommand $proxycmd"
Damien Miller58497782011-01-17 16:17:09 +110020if config_defined DISABLE_FD_PASSING ; then
Darren Tucker2a81adc2004-08-29 17:09:34 +100021 echo "skipped (not supported on this platform)"
22 exit 0
23fi
24
Darren Tuckera4df65b2013-05-17 09:37:31 +100025P=3301 # test port
Darren Tuckerffaa6a52004-06-17 16:32:45 +100026
Darren Tuckeree4ad772012-10-05 12:04:10 +100027wait_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 Tuckere7d05832004-06-16 20:22:22 +100037start_sshd
38
Darren Tucker40aaff72013-05-17 09:36:20 +100039start_mux_master()
40{
41 trace "start master, fork to background"
Damien Miller5ea4fe02014-07-22 09:39:19 +100042 ${SSH} -Nn2 -MS$CTL -F $OBJ/ssh_config -oSendEnv="_XXX_TEST" somehost \
Darren Tucker40aaff72013-05-17 09:36:20 +100043 -E $TEST_REGRESS_LOGFILE 2>&1 &
Damien Miller612f9652014-07-09 13:22:03 +100044 # NB. $SSH_PID will be killed by test-exec.sh:cleanup on fatal errors.
45 SSH_PID=$!
Darren Tucker40aaff72013-05-17 09:36:20 +100046 wait_for_mux_master_ready
47}
48
49start_mux_master
Darren Tuckere7d05832004-06-16 20:22:22 +100050
Damien Millere826a8c2004-06-18 01:23:03 +100051verbose "test $tid: envpass"
52trace "env passing over multiplexed connection"
Darren Tucker79885532009-10-07 10:30:57 +110053_XXX_TEST=blah ${SSH} -F $OBJ/ssh_config -oSendEnv="_XXX_TEST" -S$CTL otherhost sh << 'EOF'
Darren Tucker430c6a12004-06-22 13:38:56 +100054 test X"$_XXX_TEST" = X"blah"
55EOF
Damien Millere826a8c2004-06-18 01:23:03 +100056if [ $? -ne 0 ]; then
57 fail "environment not found"
58fi
59
60verbose "test $tid: transfer"
Darren Tuckerffaa6a52004-06-17 16:32:45 +100061rm -f ${COPY}
Darren Tuckere7d05832004-06-16 20:22:22 +100062trace "ssh transfer over multiplexed connection and check result"
Darren Tucker79885532009-10-07 10:30:57 +110063${SSH} -F $OBJ/ssh_config -S$CTL otherhost cat ${DATA} > ${COPY}
Darren Tucker3e86fc42004-06-17 16:34:02 +100064test -f ${COPY} || fail "ssh -Sctl: failed copy ${DATA}"
65cmp ${DATA} ${COPY} || fail "ssh -Sctl: corrupted copy of ${DATA}"
Darren Tuckere7d05832004-06-16 20:22:22 +100066
Darren Tuckerffaa6a52004-06-17 16:32:45 +100067rm -f ${COPY}
Darren Tuckere7d05832004-06-16 20:22:22 +100068trace "ssh transfer over multiplexed connection and check result"
Darren Tucker79885532009-10-07 10:30:57 +110069${SSH} -F $OBJ/ssh_config -S $CTL otherhost cat ${DATA} > ${COPY}
Darren Tucker3e86fc42004-06-17 16:34:02 +100070test -f ${COPY} || fail "ssh -S ctl: failed copy ${DATA}"
71cmp ${DATA} ${COPY} || fail "ssh -S ctl: corrupted copy of ${DATA}"
Darren Tuckere7d05832004-06-16 20:22:22 +100072
Darren Tuckerffaa6a52004-06-17 16:32:45 +100073rm -f ${COPY}
Darren Tuckere7d05832004-06-16 20:22:22 +100074trace "sftp transfer over multiplexed connection and check result"
Darren Tuckerffaa6a52004-06-17 16:32:45 +100075echo "get ${DATA} ${COPY}" | \
Darren Tucker75129022013-05-17 09:19:10 +100076 ${SFTP} -S ${SSH} -F $OBJ/ssh_config -oControlPath=$CTL otherhost >>$TEST_REGRESS_LOGFILE 2>&1
Darren Tucker3e86fc42004-06-17 16:34:02 +100077test -f ${COPY} || fail "sftp: failed copy ${DATA}"
78cmp ${DATA} ${COPY} || fail "sftp: corrupted copy of ${DATA}"
Darren Tuckere7d05832004-06-16 20:22:22 +100079
Darren Tuckerffaa6a52004-06-17 16:32:45 +100080rm -f ${COPY}
Darren Tuckere7d05832004-06-16 20:22:22 +100081trace "scp transfer over multiplexed connection and check result"
Darren Tucker75129022013-05-17 09:19:10 +100082${SCP} -S ${SSH} -F $OBJ/ssh_config -oControlPath=$CTL otherhost:${DATA} ${COPY} >>$TEST_REGRESS_LOGFILE 2>&1
Darren Tucker3e86fc42004-06-17 16:34:02 +100083test -f ${COPY} || fail "scp: failed copy ${DATA}"
84cmp ${DATA} ${COPY} || fail "scp: corrupted copy of ${DATA}"
Darren Tuckere7d05832004-06-16 20:22:22 +100085
Darren Tuckerffaa6a52004-06-17 16:32:45 +100086rm -f ${COPY}
Damien Miller0e4e9552014-07-21 09:52:54 +100087verbose "test $tid: forward"
88trace "forward over TCP/IP and check result"
Damien Millerc8f610f2014-07-21 10:23:27 +100089$NC -l 127.0.0.1 $((${PORT} + 1)) < ${DATA} &
Damien Miller0e4e9552014-07-21 09:52:54 +100090netcat_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 Millerc8f610f2014-07-21 10:23:27 +100092$NC 127.0.0.1 $((${PORT} + 2)) > ${COPY}
Damien Miller0e4e9552014-07-21 09:52:54 +100093cmp ${DATA} ${COPY} || fail "ssh: corrupted copy of ${DATA}"
94kill $netcat_pid 2>/dev/null
95rm -f ${COPY} $OBJ/unix-[123].fwd
96
97trace "forward over UNIX and check result"
Damien Millerc8f610f2014-07-21 10:23:27 +100098$NC -Ul $OBJ/unix-1.fwd < ${DATA} &
Damien Miller0e4e9552014-07-21 09:52:54 +100099netcat_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 Millerc8f610f2014-07-21 10:23:27 +1000102$NC -U $OBJ/unix-3.fwd > ${COPY}
Damien Miller0e4e9552014-07-21 09:52:54 +1000103cmp ${DATA} ${COPY} || fail "ssh: corrupted copy of ${DATA}"
104kill $netcat_pid 2>/dev/null
105rm -f ${COPY} $OBJ/unix-[123].fwd
Darren Tuckerddea13d2004-06-17 16:27:43 +1000106
Darren Tuckere7d05832004-06-16 20:22:22 +1000107for s in 0 1 4 5 44; do
108 trace "exit status $s over multiplexed connection"
109 verbose "test $tid: status $s"
Darren Tucker79885532009-10-07 10:30:57 +1100110 ${SSH} -F $OBJ/ssh_config -S $CTL otherhost exit $s
Darren Tuckere7d05832004-06-16 20:22:22 +1000111 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 Tucker79885532009-10-07 10:30:57 +1100118 ${SSH} -F $OBJ/ssh_config -S $CTL -n otherhost \
Darren Tuckere7d05832004-06-16 20:22:22 +1000119 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
124done
125
Darren Tucker9b2c0362012-10-05 11:45:39 +1000126verbose "test $tid: cmd check"
Darren Tucker75129022013-05-17 09:19:10 +1000127${SSH} -F $OBJ/ssh_config -S $CTL -Ocheck otherhost >>$TEST_REGRESS_LOGFILE 2>&1 \
Darren Tucker9b2c0362012-10-05 11:45:39 +1000128 || fail "check command failed"
Darren Tucker79ec66e2004-12-06 23:12:15 +1100129
Damien Miller0e4e9552014-07-21 09:52:54 +1000130verbose "test $tid: cmd forward local (TCP)"
Darren Tuckera4df65b2013-05-17 09:37:31 +1000131${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 Miller0e4e9552014-07-21 09:52:54 +1000140verbose "test $tid: cmd forward remote (TCP)"
Darren Tuckera4df65b2013-05-17 09:37:31 +1000141${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 Miller0e4e9552014-07-21 09:52:54 +1000150verbose "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 Millerc8f610f2014-07-21 10:23:27 +1000153echo "" | $NC -U $OBJ/unix-1.fwd | grep "Protocol mismatch" >/dev/null 2>&1 \
Damien Miller0e4e9552014-07-21 09:52:54 +1000154 || 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 Miller04f48242014-07-22 11:31:47 +1000157N=$(echo "xyzzy" | $NC -U $OBJ/unix-1.fwd 2>&1 | grep "xyzzy" | wc -l)
Damien Miller0e4e9552014-07-21 09:52:54 +1000158test ${N} -eq 0 || fail "local forward path still listening"
159rm -f $OBJ/unix-1.fwd
160
161verbose "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 Millerc8f610f2014-07-21 10:23:27 +1000164echo "" | $NC -U $OBJ/unix-1.fwd | grep "Protocol mismatch" >/dev/null 2>&1 \
Damien Miller0e4e9552014-07-21 09:52:54 +1000165 || 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 Miller04f48242014-07-22 11:31:47 +1000168N=$(echo "xyzzy" | $NC -U $OBJ/unix-1.fwd 2>&1 | grep "xyzzy" | wc -l)
Damien Miller56b840f2014-07-25 08:11:30 +1000169test ${N} -eq 0 || fail "remote forward path still listening"
Damien Miller0e4e9552014-07-21 09:52:54 +1000170rm -f $OBJ/unix-1.fwd
171
Darren Tucker9b2c0362012-10-05 11:45:39 +1000172verbose "test $tid: cmd exit"
Darren Tucker75129022013-05-17 09:19:10 +1000173${SSH} -F $OBJ/ssh_config -S $CTL -Oexit otherhost >>$TEST_REGRESS_LOGFILE 2>&1 \
Darren Tucker9b2c0362012-10-05 11:45:39 +1000174 || fail "send exit command failed"
Darren Tucker79ec66e2004-12-06 23:12:15 +1100175
176# Wait for master to exit
Damien Miller612f9652014-07-09 13:22:03 +1000177wait $SSH_PID
178kill -0 $SSH_PID >/dev/null 2>&1 && fail "exit command failed"
Darren Tucker6fc5aa82012-10-05 11:43:57 +1000179
180# Restart master and test -O stop command with master using -N
Darren Tuckeree4ad772012-10-05 12:04:10 +1000181verbose "test $tid: cmd stop"
182trace "restart master, fork to background"
Darren Tucker40aaff72013-05-17 09:36:20 +1000183start_mux_master
Darren Tuckeree4ad772012-10-05 12:04:10 +1000184
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 Tucker75129022013-05-17 09:19:10 +1000187 >>$TEST_REGRESS_LOGFILE 2>&1 &
Darren Tucker9b2c0362012-10-05 11:45:39 +1000188SLEEP_PID=$!
Darren Tucker75129022013-05-17 09:19:10 +1000189${SSH} -F $OBJ/ssh_config -S $CTL -Ostop otherhost >>$TEST_REGRESS_LOGFILE 2>&1 \
Darren Tucker9b2c0362012-10-05 11:45:39 +1000190 || fail "send stop command failed"
Darren Tuckeree4ad772012-10-05 12:04:10 +1000191
192# wait until both long-running command and master have exited.
Darren Tucker9b2c0362012-10-05 11:45:39 +1000193wait $SLEEP_PID
Darren Tuckeree4ad772012-10-05 12:04:10 +1000194[ $! != 0 ] || fail "waiting for concurrent command"
Damien Miller612f9652014-07-09 13:22:03 +1000195wait $SSH_PID
Darren Tuckeree4ad772012-10-05 12:04:10 +1000196[ $! != 0 ] || fail "waiting for master stop"
Damien Miller612f9652014-07-09 13:22:03 +1000197kill -0 $SSH_PID >/dev/null 2>&1 && fatal "stop command failed"
198SSH_PID="" # Already gone, so don't kill in cleanup
199