blob: 9f5fc38eaaf90307963d65032c92b4373c5719d9 [file] [log] [blame]
Damien Miller0e4e9552014-07-21 09:52:54 +10001# $OpenBSD: multiplex.sh,v 1.24 2014/07/15 15:54:15 millert 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 Miller58497782011-01-17 16:17:09 +11008if config_defined DISABLE_FD_PASSING ; then
Darren Tucker2a81adc2004-08-29 17:09:34 +10009 echo "skipped (not supported on this platform)"
10 exit 0
11fi
12
Darren Tuckera4df65b2013-05-17 09:37:31 +100013P=3301 # test port
Darren Tuckerffaa6a52004-06-17 16:32:45 +100014
Darren Tuckeree4ad772012-10-05 12:04:10 +100015wait_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 Tuckere7d05832004-06-16 20:22:22 +100025start_sshd
26
Darren Tucker40aaff72013-05-17 09:36:20 +100027start_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 Miller612f9652014-07-09 13:22:03 +100032 # NB. $SSH_PID will be killed by test-exec.sh:cleanup on fatal errors.
33 SSH_PID=$!
Darren Tucker40aaff72013-05-17 09:36:20 +100034 wait_for_mux_master_ready
35}
36
37start_mux_master
Darren Tuckere7d05832004-06-16 20:22:22 +100038
Damien Millere826a8c2004-06-18 01:23:03 +100039verbose "test $tid: envpass"
40trace "env passing over multiplexed connection"
Darren Tucker79885532009-10-07 10:30:57 +110041_XXX_TEST=blah ${SSH} -F $OBJ/ssh_config -oSendEnv="_XXX_TEST" -S$CTL otherhost sh << 'EOF'
Darren Tucker430c6a12004-06-22 13:38:56 +100042 test X"$_XXX_TEST" = X"blah"
43EOF
Damien Millere826a8c2004-06-18 01:23:03 +100044if [ $? -ne 0 ]; then
45 fail "environment not found"
46fi
47
48verbose "test $tid: transfer"
Darren Tuckerffaa6a52004-06-17 16:32:45 +100049rm -f ${COPY}
Darren Tuckere7d05832004-06-16 20:22:22 +100050trace "ssh transfer over multiplexed connection and check result"
Darren Tucker79885532009-10-07 10:30:57 +110051${SSH} -F $OBJ/ssh_config -S$CTL otherhost cat ${DATA} > ${COPY}
Darren Tucker3e86fc42004-06-17 16:34:02 +100052test -f ${COPY} || fail "ssh -Sctl: failed copy ${DATA}"
53cmp ${DATA} ${COPY} || fail "ssh -Sctl: corrupted copy of ${DATA}"
Darren Tuckere7d05832004-06-16 20:22:22 +100054
Darren Tuckerffaa6a52004-06-17 16:32:45 +100055rm -f ${COPY}
Darren Tuckere7d05832004-06-16 20:22:22 +100056trace "ssh transfer over multiplexed connection and check result"
Darren Tucker79885532009-10-07 10:30:57 +110057${SSH} -F $OBJ/ssh_config -S $CTL otherhost cat ${DATA} > ${COPY}
Darren Tucker3e86fc42004-06-17 16:34:02 +100058test -f ${COPY} || fail "ssh -S ctl: failed copy ${DATA}"
59cmp ${DATA} ${COPY} || fail "ssh -S ctl: corrupted copy of ${DATA}"
Darren Tuckere7d05832004-06-16 20:22:22 +100060
Darren Tuckerffaa6a52004-06-17 16:32:45 +100061rm -f ${COPY}
Darren Tuckere7d05832004-06-16 20:22:22 +100062trace "sftp transfer over multiplexed connection and check result"
Darren Tuckerffaa6a52004-06-17 16:32:45 +100063echo "get ${DATA} ${COPY}" | \
Darren Tucker75129022013-05-17 09:19:10 +100064 ${SFTP} -S ${SSH} -F $OBJ/ssh_config -oControlPath=$CTL otherhost >>$TEST_REGRESS_LOGFILE 2>&1
Darren Tucker3e86fc42004-06-17 16:34:02 +100065test -f ${COPY} || fail "sftp: failed copy ${DATA}"
66cmp ${DATA} ${COPY} || fail "sftp: corrupted copy of ${DATA}"
Darren Tuckere7d05832004-06-16 20:22:22 +100067
Darren Tuckerffaa6a52004-06-17 16:32:45 +100068rm -f ${COPY}
Darren Tuckere7d05832004-06-16 20:22:22 +100069trace "scp transfer over multiplexed connection and check result"
Darren Tucker75129022013-05-17 09:19:10 +100070${SCP} -S ${SSH} -F $OBJ/ssh_config -oControlPath=$CTL otherhost:${DATA} ${COPY} >>$TEST_REGRESS_LOGFILE 2>&1
Darren Tucker3e86fc42004-06-17 16:34:02 +100071test -f ${COPY} || fail "scp: failed copy ${DATA}"
72cmp ${DATA} ${COPY} || fail "scp: corrupted copy of ${DATA}"
Darren Tuckere7d05832004-06-16 20:22:22 +100073
Darren Tuckerffaa6a52004-06-17 16:32:45 +100074rm -f ${COPY}
Damien Miller0e4e9552014-07-21 09:52:54 +100075verbose "test $tid: forward"
76trace "forward over TCP/IP and check result"
77nc -N -l 127.0.0.1 $((${PORT} + 1)) < ${DATA} &
78netcat_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
80nc 127.0.0.1 $((${PORT} + 2)) > ${COPY}
81cmp ${DATA} ${COPY} || fail "ssh: corrupted copy of ${DATA}"
82kill $netcat_pid 2>/dev/null
83rm -f ${COPY} $OBJ/unix-[123].fwd
84
85trace "forward over UNIX and check result"
86nc -N -Ul $OBJ/unix-1.fwd < ${DATA} &
87netcat_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
90nc -U $OBJ/unix-3.fwd > ${COPY}
91cmp ${DATA} ${COPY} || fail "ssh: corrupted copy of ${DATA}"
92kill $netcat_pid 2>/dev/null
93rm -f ${COPY} $OBJ/unix-[123].fwd
Darren Tuckerddea13d2004-06-17 16:27:43 +100094
Darren Tuckere7d05832004-06-16 20:22:22 +100095for s in 0 1 4 5 44; do
96 trace "exit status $s over multiplexed connection"
97 verbose "test $tid: status $s"
Darren Tucker79885532009-10-07 10:30:57 +110098 ${SSH} -F $OBJ/ssh_config -S $CTL otherhost exit $s
Darren Tuckere7d05832004-06-16 20:22:22 +100099 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 Tucker79885532009-10-07 10:30:57 +1100106 ${SSH} -F $OBJ/ssh_config -S $CTL -n otherhost \
Darren Tuckere7d05832004-06-16 20:22:22 +1000107 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
112done
113
Darren Tucker9b2c0362012-10-05 11:45:39 +1000114verbose "test $tid: cmd check"
Darren Tucker75129022013-05-17 09:19:10 +1000115${SSH} -F $OBJ/ssh_config -S $CTL -Ocheck otherhost >>$TEST_REGRESS_LOGFILE 2>&1 \
Darren Tucker9b2c0362012-10-05 11:45:39 +1000116 || fail "check command failed"
Darren Tucker79ec66e2004-12-06 23:12:15 +1100117
Damien Miller0e4e9552014-07-21 09:52:54 +1000118verbose "test $tid: cmd forward local (TCP)"
Darren Tuckera4df65b2013-05-17 09:37:31 +1000119${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 Miller0e4e9552014-07-21 09:52:54 +1000128verbose "test $tid: cmd forward remote (TCP)"
Darren Tuckera4df65b2013-05-17 09:37:31 +1000129${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 Miller0e4e9552014-07-21 09:52:54 +1000138verbose "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"
141echo "" | 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"
145N=$(echo "" | nc -U $OBJ/unix-1.fwd 2>&1 | wc -l)
146test ${N} -eq 0 || fail "local forward path still listening"
147rm -f $OBJ/unix-1.fwd
148
149verbose "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"
152echo "" | 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"
156N=$(echo "" | nc -U $OBJ/unix-1.fwd 2>&1 | wc -l)
157test ${N} -eq 0 || fail "remote forward path still listening"
158rm -f $OBJ/unix-1.fwd
159
Darren Tucker9b2c0362012-10-05 11:45:39 +1000160verbose "test $tid: cmd exit"
Darren Tucker75129022013-05-17 09:19:10 +1000161${SSH} -F $OBJ/ssh_config -S $CTL -Oexit otherhost >>$TEST_REGRESS_LOGFILE 2>&1 \
Darren Tucker9b2c0362012-10-05 11:45:39 +1000162 || fail "send exit command failed"
Darren Tucker79ec66e2004-12-06 23:12:15 +1100163
164# Wait for master to exit
Damien Miller612f9652014-07-09 13:22:03 +1000165wait $SSH_PID
166kill -0 $SSH_PID >/dev/null 2>&1 && fail "exit command failed"
Darren Tucker6fc5aa82012-10-05 11:43:57 +1000167
168# Restart master and test -O stop command with master using -N
Darren Tuckeree4ad772012-10-05 12:04:10 +1000169verbose "test $tid: cmd stop"
170trace "restart master, fork to background"
Darren Tucker40aaff72013-05-17 09:36:20 +1000171start_mux_master
Darren Tuckeree4ad772012-10-05 12:04:10 +1000172
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 Tucker75129022013-05-17 09:19:10 +1000175 >>$TEST_REGRESS_LOGFILE 2>&1 &
Darren Tucker9b2c0362012-10-05 11:45:39 +1000176SLEEP_PID=$!
Darren Tucker75129022013-05-17 09:19:10 +1000177${SSH} -F $OBJ/ssh_config -S $CTL -Ostop otherhost >>$TEST_REGRESS_LOGFILE 2>&1 \
Darren Tucker9b2c0362012-10-05 11:45:39 +1000178 || fail "send stop command failed"
Darren Tuckeree4ad772012-10-05 12:04:10 +1000179
180# wait until both long-running command and master have exited.
Darren Tucker9b2c0362012-10-05 11:45:39 +1000181wait $SLEEP_PID
Darren Tuckeree4ad772012-10-05 12:04:10 +1000182[ $! != 0 ] || fail "waiting for concurrent command"
Damien Miller612f9652014-07-09 13:22:03 +1000183wait $SSH_PID
Darren Tuckeree4ad772012-10-05 12:04:10 +1000184[ $! != 0 ] || fail "waiting for master stop"
Damien Miller612f9652014-07-09 13:22:03 +1000185kill -0 $SSH_PID >/dev/null 2>&1 && fatal "stop command failed"
186SSH_PID="" # Already gone, so don't kill in cleanup
187