xref: /spdk/test/iscsi_tgt/sock/sock.sh (revision c6c1234de9e0015e670dd0b51bf6ce39ee0e07bd)
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
11tcp_sock() {
12	local ap=$1 pid=$2 addr port proc
13	addr=${ap%:*} port=${ap#*:}
14
15	IFS="." read -ra addr <<< "$addr"
16	printf -v addr '%02X%02X%02X%02X:%04X' \
17		"${addr[3]}" "${addr[2]}" "${addr[1]}" "${addr[0]}" \
18		"$port"
19
20	[[ -e /proc/$pid/net/tcp ]] && proc=/proc/$pid/net/tcp || proc=/proc/net/tcp
21	[[ $(< "$proc") == *"$addr 00000000:0000 0A"* ]] # TCP_LISTEN == 0x0A
22}
23
24waitfortcp() {
25	local addr="$2"
26
27	echo "Waiting for process to start up and listen on address $addr..."
28	# turn off trace for this loop
29	xtrace_disable
30	local i=40
31	while ((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			return 1
37		fi
38
39		tcp_sock "$addr" "$1" && return 0
40		sleep 0.5
41	done
42	xtrace_restore
43
44	echo "ERROR: timeout while waiting for process (pid: $1) to start listening on '$addr'"
45	return 1
46}
47
48iscsitestinit
49
50HELLO_SOCK_APP="${TARGET_NS_CMD[*]} $SPDK_EXAMPLE_DIR/hello_sock"
51SOCAT_APP="socat"
52OPENSSL_APP="openssl"
53PSK="-N ssl -E 1234567890ABCDEF -I psk.spdk.io"
54
55# ----------------
56# Test client path
57# ----------------
58timing_enter sock_client
59echo "Testing client path"
60
61# start echo server using socat
62$SOCAT_APP tcp-l:$ISCSI_PORT,fork,bind=$INITIATOR_IP exec:'/bin/cat' &
63server_pid=$!
64trap 'killprocess $server_pid;iscsitestfini; exit 1' SIGINT SIGTERM EXIT
65
66waitfortcp $server_pid $INITIATOR_IP:$ISCSI_PORT
67
68# send message using hello_sock client
69message="**MESSAGE:This is a test message from the client**"
70response=$(echo "$message" | $HELLO_SOCK_APP -H $INITIATOR_IP -P $ISCSI_PORT -N "posix")
71
72if ! echo "$response" | grep -q "$message"; then
73	exit 1
74fi
75
76# send message using hello_sock client with zero copy disabled
77message="**MESSAGE:This is a test message from the client with zero copy disabled**"
78response=$(echo "$message" | $HELLO_SOCK_APP -H $INITIATOR_IP -P $ISCSI_PORT -N "posix" -z)
79
80if ! echo "$response" | grep -q "$message"; then
81	exit 1
82fi
83
84# send message using hello_sock client with zero copy enabled
85message="**MESSAGE:This is a test message from the client with zero copy enabled**"
86response=$(echo "$message" | $HELLO_SOCK_APP -H $INITIATOR_IP -P $ISCSI_PORT -N "posix" -Z)
87
88if ! echo "$response" | grep -q "$message"; then
89	exit 1
90fi
91
92trap '-' SIGINT SIGTERM EXIT
93# NOTE: socat returns code 143 on SIGINT
94killprocess $server_pid || true
95
96timing_exit sock_client
97
98# ----------------
99# Test SSL server path
100# ----------------
101timing_enter sock_ssl_server
102echo "Testing SSL server path"
103
104# start echo server using hello_sock echo server
105$HELLO_SOCK_APP -H $TARGET_IP -P $ISCSI_PORT -S $PSK -m 0x1 &
106server_pid=$!
107trap 'killprocess $server_pid; iscsitestfini; exit 1' SIGINT SIGTERM EXIT
108waitfortcp $server_pid $TARGET_IP:$ISCSI_PORT
109
110# send message using hello_sock client
111message="**MESSAGE:This is a test message from the hello_sock client with ssl**"
112response=$(echo "$message" | $HELLO_SOCK_APP -H $TARGET_IP -P $ISCSI_PORT $PSK -m 0x2)
113if ! echo "$response" | grep -q "$message"; then
114	exit 1
115fi
116
117# send message using hello_sock client using TLS 1.3
118message="**MESSAGE:This is a test message from the hello_sock client with ssl using TLS 1.3**"
119response=$(echo "$message" | $HELLO_SOCK_APP -H $TARGET_IP -P $ISCSI_PORT $PSK -T 13 -m 0x2)
120if ! echo "$response" | grep -q "$message"; then
121	exit 1
122fi
123
124# send message using hello_sock client using incorrect TLS 7
125message="**MESSAGE:This is a test message from the hello_sock client with ssl using incorrect TLS 7**"
126echo "$message" | $HELLO_SOCK_APP -H $TARGET_IP -P $ISCSI_PORT $PSK -T 7 -m 0x2 && exit 1
127
128# send message using hello_sock client with KTLS disabled
129message="**MESSAGE:This is a test message from the hello_sock client with KTLS disabled**"
130response=$(echo "$message" | $HELLO_SOCK_APP -H $TARGET_IP -P $ISCSI_PORT $PSK -k -m 0x2)
131if ! echo "$response" | grep -q "$message"; then
132	exit 1
133fi
134
135# send message using hello_sock client with KTLS enabled
136
137# CI so far doesn't support new openssl-3 with this option.
138# This section is commented out and will be changed back after the CI has systems that run with openssl-3
139# See GH issue #2687
140
141# message="**MESSAGE:This is a test message from the hello_sock client with KTLS enabled**"
142# echo "$message" | $HELLO_SOCK_APP -H $TARGET_IP -P $ISCSI_PORT $PSK -K
143
144# send message using openssl client using TLS 1.3
145message="**MESSAGE:This is a test message from the openssl client using TLS 1.3**"
146response=$( (
147	echo -ne "$message"
148	sleep 2
149) | $OPENSSL_APP s_client -debug -state -tlsextdebug -tls1_3 -psk_identity psk.spdk.io -psk "1234567890ABCDEF" -connect $TARGET_IP:$ISCSI_PORT)
150if ! echo "$response" | grep -q "$message"; then
151	exit 1
152fi
153
154# send message using hello_sock client with unmatching PSK KEY, expect a failure
155message="**MESSAGE:This is a test message from the hello_sock client with unmatching psk_key**"
156echo "$message" | $HELLO_SOCK_APP -H $TARGET_IP -P $ISCSI_PORT $PSK -E 4321DEADBEEF1234 -m 0x2 && exit 1
157
158# send message using hello_sock client with unmatching PSK IDENTITY, expect a failure
159message="**MESSAGE:This is a test message from the hello_sock client with unmatching psk_key**"
160echo "$message" | $HELLO_SOCK_APP -H $TARGET_IP -P $ISCSI_PORT $PSK -I WRONG_PSK_ID -m 0x2 && exit 1
161
162trap '-' SIGINT SIGTERM EXIT
163# NOTE: socat returns code 143 on SIGINT
164killprocess $server_pid || true
165
166timing_exit sock_ssl_server
167
168# ----------------
169# Test server path
170# ----------------
171
172timing_enter sock_server
173
174# start echo server using hello_sock echo server
175$HELLO_SOCK_APP -H $TARGET_IP -P $ISCSI_PORT -S -N "posix" -m 0x1 &
176server_pid=$!
177trap 'killprocess $server_pid; iscsitestfini; exit 1' SIGINT SIGTERM EXIT
178waitfortcp $server_pid $TARGET_IP:$ISCSI_PORT
179
180# send message to server using socat
181message="**MESSAGE:This is a test message to the server**"
182response=$(echo "$message" | $SOCAT_APP - tcp:$TARGET_IP:$ISCSI_PORT 2> /dev/null)
183
184if [ "$message" != "$response" ]; then
185	exit 1
186fi
187
188trap - SIGINT SIGTERM EXIT
189
190killprocess $server_pid
191
192iscsitestfini
193timing_exit sock_server
194