xref: /spdk/test/iscsi_tgt/sock/sock.sh (revision b3bec07939ebe2ea2e0c43931705d32aa9e06719)
1#!/usr/bin/env bash
2#  SPDX-License-Identifier: BSD-3-Clause
3#  Copyright (C) 2018 Intel Corporation
4#  All rights reserved.
5#
6testdir=$(readlink -f $(dirname $0))
7rootdir=$(readlink -f $testdir/../../..)
8source $rootdir/test/common/autotest_common.sh
9source $rootdir/test/iscsi_tgt/common.sh
10
11function waitfortcp() {
12	local addr="$2"
13
14	if hash ip &> /dev/null; then
15		local have_ip_cmd=true
16	else
17		local have_ip_cmd=false
18	fi
19
20	if hash ss &> /dev/null; then
21		local have_ss_cmd=true
22	else
23		local have_ss_cmd=false
24	fi
25
26	echo "Waiting for process to start up and listen on address $addr..."
27	# turn off trace for this loop
28	xtrace_disable
29	local ret=0
30	local i
31	for ((i = 40; i != 0; i--)); do
32		# if the process is no longer running, then exit the script
33		#  since it means the application crashed
34		if ! kill -s 0 $1; then
35			echo "ERROR: process (pid: $1) is no longer running"
36			ret=1
37			break
38		fi
39
40		if $have_ip_cmd; then
41			namespace=$(ip netns identify $1)
42			if [ -n "$namespace" ]; then
43				ns_cmd="ip netns exec $namespace"
44			fi
45		fi
46
47		if $have_ss_cmd; then
48			if $ns_cmd ss -ln | grep -E -q "\s+$addr\s+"; then
49				break
50			fi
51		elif [[ "$(uname -s)" == "Linux" ]]; then
52			# For Linux, if system doesn't have ss, just assume it has netstat
53			if $ns_cmd netstat -an | grep -iw LISTENING | grep -E -q "\s+$addr\$"; then
54				break
55			fi
56		fi
57		sleep 0.5
58	done
59
60	xtrace_restore
61	if ((i == 0)); then
62		echo "ERROR: timeout while waiting for process (pid: $1) to start listening on '$addr'"
63		ret=1
64	fi
65	return $ret
66}
67
68iscsitestinit
69
70HELLO_SOCK_APP="${TARGET_NS_CMD[*]} $SPDK_EXAMPLE_DIR/hello_sock"
71SOCAT_APP="socat"
72OPENSSL_APP="openssl"
73PSK="-N ssl -E 1234567890ABCDEF -I psk.spdk.io"
74
75# ----------------
76# Test client path
77# ----------------
78timing_enter sock_client
79echo "Testing client path"
80
81# start echo server using socat
82$SOCAT_APP tcp-l:$ISCSI_PORT,fork,bind=$INITIATOR_IP exec:'/bin/cat' &
83server_pid=$!
84trap 'killprocess $server_pid;iscsitestfini; exit 1' SIGINT SIGTERM EXIT
85
86waitfortcp $server_pid $INITIATOR_IP:$ISCSI_PORT
87
88# send message using hello_sock client
89message="**MESSAGE:This is a test message from the client**"
90response=$(echo $message | $HELLO_SOCK_APP -H $INITIATOR_IP -P $ISCSI_PORT -N "posix")
91
92if ! echo "$response" | grep -q "$message"; then
93	exit 1
94fi
95
96# send message using hello_sock client with zero copy disabled
97message="**MESSAGE:This is a test message from the client with zero copy disabled**"
98response=$(echo $message | $HELLO_SOCK_APP -H $INITIATOR_IP -P $ISCSI_PORT -N "posix" -z)
99
100if ! echo "$response" | grep -q "$message"; then
101	exit 1
102fi
103
104# send message using hello_sock client with zero copy enabled
105message="**MESSAGE:This is a test message from the client with zero copy enabled**"
106response=$(echo $message | $HELLO_SOCK_APP -H $INITIATOR_IP -P $ISCSI_PORT -N "posix" -Z)
107
108if ! echo "$response" | grep -q "$message"; then
109	exit 1
110fi
111
112trap '-' SIGINT SIGTERM EXIT
113# NOTE: socat returns code 143 on SIGINT
114killprocess $server_pid || true
115
116timing_exit sock_client
117
118# ----------------
119# Test SSL server path
120# ----------------
121timing_enter sock_ssl_server
122echo "Testing SSL server path"
123
124# start echo server using hello_sock echo server
125$HELLO_SOCK_APP -H $TARGET_IP -P $ISCSI_PORT -S $PSK -m 0x1 &
126server_pid=$!
127trap 'killprocess $server_pid; iscsitestfini; exit 1' SIGINT SIGTERM EXIT
128waitforlisten $server_pid
129
130# send message using hello_sock client
131message="**MESSAGE:This is a test message from the hello_sock client with ssl**"
132response=$(echo $message | $HELLO_SOCK_APP -H $TARGET_IP -P $ISCSI_PORT $PSK -m 0x2)
133if ! echo "$response" | grep -q "$message"; then
134	exit 1
135fi
136
137# send message using hello_sock client using TLS 1.3
138message="**MESSAGE:This is a test message from the hello_sock client with ssl using TLS 1.3**"
139response=$(echo $message | $HELLO_SOCK_APP -H $TARGET_IP -P $ISCSI_PORT $PSK -T 13 -m 0x2)
140if ! echo "$response" | grep -q "$message"; then
141	exit 1
142fi
143
144# send message using hello_sock client using incorrect TLS 7
145message="**MESSAGE:This is a test message from the hello_sock client with ssl using incorrect TLS 7**"
146echo $message | $HELLO_SOCK_APP -H $TARGET_IP -P $ISCSI_PORT $PSK -T 7 -m 0x2 && exit 1
147
148# send message using hello_sock client with KTLS disabled
149message="**MESSAGE:This is a test message from the hello_sock client with KTLS disabled**"
150response=$(echo $message | $HELLO_SOCK_APP -H $TARGET_IP -P $ISCSI_PORT $PSK -k -m 0x2)
151if ! echo "$response" | grep -q "$message"; then
152	exit 1
153fi
154
155# send message using hello_sock client with KTLS enabled
156
157# CI so far doesn't support new openssl-3 with this option.
158# This section is commented out and will be changed back after the CI has systems that run with openssl-3
159# See GH issue #2687
160
161# message="**MESSAGE:This is a test message from the hello_sock client with KTLS enabled**"
162# echo $message | $HELLO_SOCK_APP -H $TARGET_IP -P $ISCSI_PORT $PSK -K
163
164# send message using openssl client using TLS 1.3
165message="**MESSAGE:This is a test message from the openssl client using TLS 1.3**"
166response=$( (
167	echo -ne $message
168	sleep 2
169) | $OPENSSL_APP s_client -debug -state -tlsextdebug -tls1_3 -psk_identity psk.spdk.io -psk "1234567890ABCDEF" -connect $TARGET_IP:$ISCSI_PORT)
170if ! echo "$response" | grep -q "$message"; then
171	exit 1
172fi
173
174# send message using hello_sock client with unmatching PSK KEY, expect a failure
175message="**MESSAGE:This is a test message from the hello_sock client with unmatching psk_key**"
176echo $message | $HELLO_SOCK_APP -H $TARGET_IP -P $ISCSI_PORT $PSK -E 4321DEADBEEF1234 -m 0x2 && exit 1
177
178# send message using hello_sock client with unmatching PSK IDENTITY, expect a failure
179message="**MESSAGE:This is a test message from the hello_sock client with unmatching psk_key**"
180echo $message | $HELLO_SOCK_APP -H $TARGET_IP -P $ISCSI_PORT $PSK -I WRONG_PSK_ID -m 0x2 && exit 1
181
182trap '-' SIGINT SIGTERM EXIT
183# NOTE: socat returns code 143 on SIGINT
184killprocess $server_pid || true
185
186timing_exit sock_ssl_server
187
188# ----------------
189# Test server path
190# ----------------
191
192timing_enter sock_server
193
194# start echo server using hello_sock echo server
195$HELLO_SOCK_APP -H $TARGET_IP -P $ISCSI_PORT -S -N "posix" -m 0x1 &
196server_pid=$!
197trap 'killprocess $server_pid; iscsitestfini; exit 1' SIGINT SIGTERM EXIT
198waitforlisten $server_pid
199
200# send message to server using socat
201message="**MESSAGE:This is a test message to the server**"
202response=$(echo $message | $SOCAT_APP - tcp:$TARGET_IP:$ISCSI_PORT 2> /dev/null)
203
204if [ "$message" != "$response" ]; then
205	exit 1
206fi
207
208trap - SIGINT SIGTERM EXIT
209
210killprocess $server_pid
211
212iscsitestfini
213timing_exit sock_server
214