multiplex.sh 7.4 KB

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