]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - crypto/openssh/regress/multiplex.sh
Merge once more from ^/vendor/llvm-project/release-10.x, to get the
[FreeBSD/FreeBSD.git] / crypto / openssh / regress / multiplex.sh
1 #       $OpenBSD: multiplex.sh,v 1.28 2017/04/30 23:34:55 djm Exp $
2 #       Placed in the Public Domain.
3
4 make_tmpdir
5 CTL=${SSH_REGRESS_TMP}/ctl-sock
6
7 tid="connection multiplexing"
8
9 NC=$OBJ/netcat
10
11 trace "will use ProxyCommand $proxycmd"
12 if config_defined DISABLE_FD_PASSING ; then
13         echo "skipped (not supported on this platform)"
14         exit 0
15 fi
16
17 P=3301  # test port
18
19 wait_for_mux_master_ready()
20 {
21         for i in 1 2 3 4 5; do
22                 ${SSH} -F $OBJ/ssh_config -S $CTL -Ocheck otherhost \
23                     >/dev/null 2>&1 && return 0
24                 sleep $i
25         done
26         fatal "mux master never becomes ready"
27 }
28
29 start_sshd
30
31 start_mux_master()
32 {
33         trace "start master, fork to background"
34         ${SSH} -Nn2 -MS$CTL -F $OBJ/ssh_config -oSendEnv="_XXX_TEST" somehost \
35             -E $TEST_REGRESS_LOGFILE 2>&1 &
36         # NB. $SSH_PID will be killed by test-exec.sh:cleanup on fatal errors.
37         SSH_PID=$!
38         wait_for_mux_master_ready
39 }
40
41 start_mux_master
42
43 verbose "test $tid: envpass"
44 trace "env passing over multiplexed connection"
45 _XXX_TEST=blah ${SSH} -F $OBJ/ssh_config -oSendEnv="_XXX_TEST" -S$CTL otherhost sh << 'EOF'
46         test X"$_XXX_TEST" = X"blah"
47 EOF
48 if [ $? -ne 0 ]; then
49         fail "environment not found"
50 fi
51
52 verbose "test $tid: transfer"
53 rm -f ${COPY}
54 trace "ssh transfer over multiplexed connection and check result"
55 ${SSH} -F $OBJ/ssh_config -S$CTL otherhost cat ${DATA} > ${COPY}
56 test -f ${COPY}                 || fail "ssh -Sctl: failed copy ${DATA}" 
57 cmp ${DATA} ${COPY}             || fail "ssh -Sctl: corrupted copy of ${DATA}"
58
59 rm -f ${COPY}
60 trace "ssh transfer over multiplexed connection and check result"
61 ${SSH} -F $OBJ/ssh_config -S $CTL otherhost cat ${DATA} > ${COPY}
62 test -f ${COPY}                 || fail "ssh -S ctl: failed copy ${DATA}" 
63 cmp ${DATA} ${COPY}             || fail "ssh -S ctl: corrupted copy of ${DATA}"
64
65 rm -f ${COPY}
66 trace "sftp transfer over multiplexed connection and check result"
67 echo "get ${DATA} ${COPY}" | \
68         ${SFTP} -S ${SSH} -F $OBJ/ssh_config -oControlPath=$CTL otherhost >>$TEST_REGRESS_LOGFILE 2>&1
69 test -f ${COPY}                 || fail "sftp: failed copy ${DATA}" 
70 cmp ${DATA} ${COPY}             || fail "sftp: corrupted copy of ${DATA}"
71
72 rm -f ${COPY}
73 trace "scp transfer over multiplexed connection and check result"
74 ${SCP} -S ${SSH} -F $OBJ/ssh_config -oControlPath=$CTL otherhost:${DATA} ${COPY} >>$TEST_REGRESS_LOGFILE 2>&1
75 test -f ${COPY}                 || fail "scp: failed copy ${DATA}" 
76 cmp ${DATA} ${COPY}             || fail "scp: corrupted copy of ${DATA}"
77
78 rm -f ${COPY}
79 verbose "test $tid: forward"
80 trace "forward over TCP/IP and check result"
81 $NC -N -l 127.0.0.1 $((${PORT} + 1)) < ${DATA} > /dev/null &
82 netcat_pid=$!
83 ${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
84 $NC 127.0.0.1 $((${PORT} + 2)) < /dev/null > ${COPY}
85 cmp ${DATA} ${COPY}             || fail "ssh: corrupted copy of ${DATA}"
86 kill $netcat_pid 2>/dev/null
87 rm -f ${COPY} $OBJ/unix-[123].fwd
88
89 trace "forward over UNIX and check result"
90 $NC -N -Ul $OBJ/unix-1.fwd < ${DATA} > /dev/null &
91 netcat_pid=$!
92 ${SSH} -F $OBJ/ssh_config -S $CTL -Oforward -L$OBJ/unix-2.fwd:$OBJ/unix-1.fwd otherhost >>$TEST_SSH_LOGFILE 2>&1
93 ${SSH} -F $OBJ/ssh_config -S $CTL -Oforward -R$OBJ/unix-3.fwd:$OBJ/unix-2.fwd otherhost >>$TEST_SSH_LOGFILE 2>&1
94 $NC -U $OBJ/unix-3.fwd < /dev/null > ${COPY} 2>/dev/null
95 cmp ${DATA} ${COPY}             || fail "ssh: corrupted copy of ${DATA}"
96 kill $netcat_pid 2>/dev/null
97 rm -f ${COPY} $OBJ/unix-[123].fwd
98
99 for s in 0 1 4 5 44; do
100         trace "exit status $s over multiplexed connection"
101         verbose "test $tid: status $s"
102         ${SSH} -F $OBJ/ssh_config -S $CTL otherhost exit $s
103         r=$?
104         if [ $r -ne $s ]; then
105                 fail "exit code mismatch: $r != $s"
106         fi
107
108         # same with early close of stdout/err
109         trace "exit status $s with early close over multiplexed connection"
110         ${SSH} -F $OBJ/ssh_config -S $CTL -n otherhost \
111                 exec sh -c \'"sleep 2; exec > /dev/null 2>&1; sleep 3; exit $s"\'
112         r=$?
113         if [ $r -ne $s ]; then
114                 fail "exit code (with sleep) mismatch: $r != $s"
115         fi
116 done
117
118 verbose "test $tid: cmd check"
119 ${SSH} -F $OBJ/ssh_config -S $CTL -Ocheck otherhost >>$TEST_REGRESS_LOGFILE 2>&1 \
120     || fail "check command failed" 
121
122 verbose "test $tid: cmd forward local (TCP)"
123 ${SSH} -F $OBJ/ssh_config -S $CTL -Oforward -L $P:localhost:$PORT otherhost \
124      || fail "request local forward failed"
125 ${SSH} -F $OBJ/ssh_config -p$P otherhost true \
126      || fail "connect to local forward port failed"
127 ${SSH} -F $OBJ/ssh_config -S $CTL -Ocancel -L $P:localhost:$PORT otherhost \
128      || fail "cancel local forward failed"
129 ${SSH} -F $OBJ/ssh_config -p$P otherhost true \
130      && fail "local forward port still listening"
131
132 verbose "test $tid: cmd forward remote (TCP)"
133 ${SSH} -F $OBJ/ssh_config -S $CTL -Oforward -R $P:localhost:$PORT otherhost \
134      || fail "request remote forward failed"
135 ${SSH} -F $OBJ/ssh_config -p$P otherhost true \
136      || fail "connect to remote forwarded port failed"
137 ${SSH} -F $OBJ/ssh_config -S $CTL -Ocancel -R $P:localhost:$PORT otherhost \
138      || fail "cancel remote forward failed"
139 ${SSH} -F $OBJ/ssh_config -p$P otherhost true \
140      && fail "remote forward port still listening"
141
142 verbose "test $tid: cmd forward local (UNIX)"
143 ${SSH} -F $OBJ/ssh_config -S $CTL -Oforward -L $OBJ/unix-1.fwd:localhost:$PORT otherhost \
144      || fail "request local forward failed"
145 echo "" | $NC -U $OBJ/unix-1.fwd | grep "Protocol mismatch" >/dev/null 2>&1 \
146      || fail "connect to local forward path failed"
147 ${SSH} -F $OBJ/ssh_config -S $CTL -Ocancel -L $OBJ/unix-1.fwd:localhost:$PORT otherhost \
148      || fail "cancel local forward failed"
149 N=$(echo "xyzzy" | $NC -U $OBJ/unix-1.fwd 2>&1 | grep "xyzzy" | wc -l)
150 test ${N} -eq 0 || fail "local forward path still listening"
151 rm -f $OBJ/unix-1.fwd
152
153 verbose "test $tid: cmd forward remote (UNIX)"
154 ${SSH} -F $OBJ/ssh_config -S $CTL -Oforward -R $OBJ/unix-1.fwd:localhost:$PORT otherhost \
155      || fail "request remote forward failed"
156 echo "" | $NC -U $OBJ/unix-1.fwd | grep "Protocol mismatch" >/dev/null 2>&1 \
157      || fail "connect to remote forwarded path failed"
158 ${SSH} -F $OBJ/ssh_config -S $CTL -Ocancel -R $OBJ/unix-1.fwd:localhost:$PORT otherhost \
159      || fail "cancel remote forward failed"
160 N=$(echo "xyzzy" | $NC -U $OBJ/unix-1.fwd 2>&1 | grep "xyzzy" | wc -l)
161 test ${N} -eq 0 || fail "remote forward path still listening"
162 rm -f $OBJ/unix-1.fwd
163
164 verbose "test $tid: cmd exit"
165 ${SSH} -F $OBJ/ssh_config -S $CTL -Oexit otherhost >>$TEST_REGRESS_LOGFILE 2>&1 \
166     || fail "send exit command failed" 
167
168 # Wait for master to exit
169 wait $SSH_PID
170 kill -0 $SSH_PID >/dev/null 2>&1 && fail "exit command failed"
171
172 # Restart master and test -O stop command with master using -N
173 verbose "test $tid: cmd stop"
174 trace "restart master, fork to background"
175 start_mux_master
176
177 # start a long-running command then immediately request a stop
178 ${SSH} -F $OBJ/ssh_config -S $CTL otherhost "sleep 10; exit 0" \
179      >>$TEST_REGRESS_LOGFILE 2>&1 &
180 SLEEP_PID=$!
181 ${SSH} -F $OBJ/ssh_config -S $CTL -Ostop otherhost >>$TEST_REGRESS_LOGFILE 2>&1 \
182     || fail "send stop command failed"
183
184 # wait until both long-running command and master have exited.
185 wait $SLEEP_PID
186 [ $! != 0 ] || fail "waiting for concurrent command"
187 wait $SSH_PID
188 [ $! != 0 ] || fail "waiting for master stop"
189 kill -0 $SSH_PID >/dev/null 2>&1 && fatal "stop command failed"
190 SSH_PID="" # Already gone, so don't kill in cleanup
191