1#	$OpenBSD: multiplex.sh,v 1.17 2012/10/05 02:05:30 dtucker Exp $
2#	Placed in the Public Domain.
3
4CTL=/tmp/openssh.regress.ctl-sock.$$
5
6tid="connection multiplexing"
7
8if config_defined DISABLE_FD_PASSING ; then
9	echo "skipped (not supported on this platform)"
10	exit 0
11fi
12
13DATA=/bin/ls${EXEEXT}
14COPY=$OBJ/ls.copy
15
16wait_for_mux_master_ready()
17{
18	for i in 1 2 3 4 5; do
19		${SSH} -F $OBJ/ssh_config -S $CTL -Ocheck otherhost \
20		    >/dev/null 2>&1 && return 0
21		sleep $i
22	done
23	fatal "mux master never becomes ready"
24}
25
26start_sshd
27
28trace "start master, fork to background"
29${SSH} -Nn2 -MS$CTL -F $OBJ/ssh_config -oSendEnv="_XXX_TEST" somehost &
30MASTER_PID=$!
31wait_for_mux_master_ready
32
33verbose "test $tid: envpass"
34trace "env passing over multiplexed connection"
35_XXX_TEST=blah ${SSH} -F $OBJ/ssh_config -oSendEnv="_XXX_TEST" -S$CTL otherhost sh << 'EOF'
36	test X"$_XXX_TEST" = X"blah"
37EOF
38if [ $? -ne 0 ]; then
39	fail "environment not found"
40fi
41
42verbose "test $tid: transfer"
43rm -f ${COPY}
44trace "ssh transfer over multiplexed connection and check result"
45${SSH} -F $OBJ/ssh_config -S$CTL otherhost cat ${DATA} > ${COPY}
46test -f ${COPY}			|| fail "ssh -Sctl: failed copy ${DATA}" 
47cmp ${DATA} ${COPY}		|| fail "ssh -Sctl: corrupted copy of ${DATA}"
48
49rm -f ${COPY}
50trace "ssh transfer over multiplexed connection and check result"
51${SSH} -F $OBJ/ssh_config -S $CTL otherhost cat ${DATA} > ${COPY}
52test -f ${COPY}			|| fail "ssh -S ctl: failed copy ${DATA}" 
53cmp ${DATA} ${COPY}		|| fail "ssh -S ctl: corrupted copy of ${DATA}"
54
55rm -f ${COPY}
56trace "sftp transfer over multiplexed connection and check result"
57echo "get ${DATA} ${COPY}" | \
58	${SFTP} -S ${SSH} -F $OBJ/ssh_config -oControlPath=$CTL otherhost >>$TEST_SSH_LOGFILE 2>&1
59test -f ${COPY}			|| fail "sftp: failed copy ${DATA}" 
60cmp ${DATA} ${COPY}		|| fail "sftp: corrupted copy of ${DATA}"
61
62rm -f ${COPY}
63trace "scp transfer over multiplexed connection and check result"
64${SCP} -S ${SSH} -F $OBJ/ssh_config -oControlPath=$CTL otherhost:${DATA} ${COPY} >>$TEST_SSH_LOGFILE 2>&1
65test -f ${COPY}			|| fail "scp: failed copy ${DATA}" 
66cmp ${DATA} ${COPY}		|| fail "scp: corrupted copy of ${DATA}"
67
68rm -f ${COPY}
69
70for s in 0 1 4 5 44; do
71	trace "exit status $s over multiplexed connection"
72	verbose "test $tid: status $s"
73	${SSH} -F $OBJ/ssh_config -S $CTL otherhost exit $s
74	r=$?
75	if [ $r -ne $s ]; then
76		fail "exit code mismatch for protocol $p: $r != $s"
77	fi
78
79	# same with early close of stdout/err
80	trace "exit status $s with early close over multiplexed connection"
81	${SSH} -F $OBJ/ssh_config -S $CTL -n otherhost \
82                exec sh -c \'"sleep 2; exec > /dev/null 2>&1; sleep 3; exit $s"\'
83	r=$?
84	if [ $r -ne $s ]; then
85		fail "exit code (with sleep) mismatch for protocol $p: $r != $s"
86	fi
87done
88
89verbose "test $tid: cmd check"
90${SSH} -F $OBJ/ssh_config -S $CTL -Ocheck otherhost >>$TEST_SSH_LOGFILE 2>&1 \
91    || fail "check command failed" 
92
93verbose "test $tid: cmd exit"
94${SSH} -F $OBJ/ssh_config -S $CTL -Oexit otherhost >>$TEST_SSH_LOGFILE 2>&1 \
95    || fail "send exit command failed" 
96
97# Wait for master to exit
98wait $MASTER_PID
99kill -0 $MASTER_PID >/dev/null 2>&1 && fail "exit command failed"
100
101# Restart master and test -O stop command with master using -N
102verbose "test $tid: cmd stop"
103trace "restart master, fork to background"
104${SSH} -Nn2 -MS$CTL -F $OBJ/ssh_config -oSendEnv="_XXX_TEST" somehost &
105MASTER_PID=$!
106wait_for_mux_master_ready
107
108# start a long-running command then immediately request a stop
109${SSH} -F $OBJ/ssh_config -S $CTL otherhost "sleep 10; exit 0" \
110     >>$TEST_SSH_LOGFILE 2>&1 &
111SLEEP_PID=$!
112${SSH} -F $OBJ/ssh_config -S $CTL -Ostop otherhost >>$TEST_SSH_LOGFILE 2>&1 \
113    || fail "send stop command failed"
114
115# wait until both long-running command and master have exited.
116wait $SLEEP_PID
117[ $! != 0 ] || fail "waiting for concurrent command"
118wait $MASTER_PID
119[ $! != 0 ] || fail "waiting for master stop"
120kill -0 $MASTER_PID >/dev/null 2>&1 && fail "stop command failed"
121