wolfssh/scripts/sftp.test

224 lines
5.8 KiB
Bash
Executable File

#!/bin/sh
# sftp local test
no_pid=-1
server_pid="$no_pid"
ready_file="$PWD/wolfssh_sftp_ready$$"
nonblockingOnly=0
blockBuild=0
[ ! -x ./examples/sftpclient/wolfsftp ] && echo && echo "wolfSFTP client doesn't exist" && exit 1
WOLFSSH_OPTIONS=$(./apps/wolfssh-options) || {
echo "fail: could not run ./apps/wolfssh-options"
exit 1
}
# test for if the SFTP client works
if ! echo "$WOLFSSH_OPTIONS" | grep -qx "CLIENT"
then
echo "macro NO_WOLFSSH_CLIENT was used"
echo "skipping test"
exit 77
fi
# test for nonblocking only
if echo "$WOLFSSH_OPTIONS" | grep -qx "TEST_BLOCK"
then
echo "macro WOLFSSH_TEST_BLOCK was used"
nonblockingOnly=1
blockBuild=1
fi
#echo "ready file $ready_file"
create_port() {
# counted per server start, so a slow first start cannot eat the wait
# the later ones need
counter=0
while [ ! -s "$ready_file" ] && [ "$counter" -lt 20 ]; do
echo "waiting for ready file..."
sleep 0.1
counter=$((counter+ 1))
done
# -s, not -e: the loop waits for the port to be written, and an empty
# file here means the wait ran out
if [ -s "$ready_file" ]; then
echo "found ready file, starting client..."
# get created port 0 ephemeral port
port=$(cat "$ready_file")
else
echo "NO ready file ending test..."
do_cleanup
exit 1
fi
}
remove_ready_file() {
if test -e "$ready_file"; then
echo "removing existing ready file"
rm "$ready_file"
fi
}
do_cleanup() {
echo "in cleanup"
if [ "$server_pid" != "$no_pid" ]
then
echo "killing server"
kill -9 "$server_pid"
fi
remove_ready_file
}
do_trap() {
echo "got trap"
do_cleanup
exit 1
}
trap do_trap INT TERM
if [ "$nonblockingOnly" = 0 ]; then
echo "Test basic connection"
./examples/echoserver/echoserver -1 -R "$ready_file" &
server_pid=$!
create_port
echo "exit" | ./examples/sftpclient/wolfsftp -u jill -P upthehill -p "$port"
RESULT=$?
remove_ready_file
if [ $RESULT -ne 0 ]; then
echo
echo "failed to connect"
do_cleanup
exit 1
fi
fi
# Test non blocking connection
echo "Test non blocking connection"
./examples/echoserver/echoserver -N -1 -R "$ready_file" &
server_pid=$!
create_port
echo "exit" | ./examples/sftpclient/wolfsftp -N -u jill -P upthehill -p "$port"
RESULT=$?
remove_ready_file
if [ $RESULT -ne 0 ]; then
echo
echo "failed to connect"
do_cleanup
exit 1
fi
# With -A the echoserver answers the subsystem request in its own callback
# and serves the session through wolfSSH_SFTP_accept(); the cases above take
# the accept() re-entry instead.
if [ "$nonblockingOnly" = 0 ]; then
echo "Test connection to an app-driven server"
./examples/echoserver/echoserver -A -1 -R "$ready_file" &
server_pid=$!
create_port
echo "exit" | ./examples/sftpclient/wolfsftp -u jill -P upthehill -p "$port"
RESULT=$?
remove_ready_file
if [ $RESULT -ne 0 ]; then
echo
echo "failed to connect to app-driven server"
do_cleanup
exit 1
fi
fi
# The same handoff on a non-blocking server, which reaches the accept call's
# want-read and want-write retries. Held out of a forced-blocking build: the
# two ends deadlock there, the server parked in DoReceive with output it owes
# still queued, which is a defect of its own rather than this case failing.
if [ "$blockBuild" = 0 ]; then
echo "Test non blocking connection to an app-driven server"
./examples/echoserver/echoserver -A -N -1 -R "$ready_file" &
server_pid=$!
create_port
echo "exit" | ./examples/sftpclient/wolfsftp -N -u jill -P upthehill \
-p "$port"
RESULT=$?
remove_ready_file
if [ $RESULT -ne 0 ]; then
echo
echo "failed to connect to non blocking app-driven server"
do_cleanup
exit 1
fi
fi
# Test want write return from highwater callback
if [ "$nonblockingOnly" = 0 ]; then
echo "Test want write return from highwater callback"
./examples/echoserver/echoserver -H -N -1 -R "$ready_file" &
server_pid=$!
create_port
./examples/sftpclient/wolfsftp -N -u jill -P upthehill -p "$port" -g -r "$PWD/README.md-2" -l "$PWD/README.md"
RESULT=$?
remove_ready_file
rm -f "$PWD/README.md-2"
if [ $RESULT -ne 0 ]; then
echo
echo "failed to connect"
do_cleanup
exit 1
fi
fi
# Test of setting directory
if [ "$nonblockingOnly" = 0 ]; then
echo "Test of setting directory"
./examples/echoserver/echoserver -d "$PWD/examples" -1 -R "$ready_file" &
server_pid=$!
create_port
echo "exit" | ./examples/sftpclient/wolfsftp -N -u jill -P upthehill -p "$port"
RESULT=$?
remove_ready_file
if [ $RESULT -ne 0 ]; then
echo
echo "failed to connect"
do_cleanup
exit 1
fi
fi
# Test SFTP status failure for non-existing file
if [ "$nonblockingOnly" = 0 ]; then
echo "Test SFTP status failure for non-existing file"
./examples/echoserver/echoserver -N -1 -R "$ready_file" &
server_pid=$!
create_port
./examples/sftpclient/wolfsftp -N -u jill -P upthehill -p "$port" -G -r "$PWD/this_file_does_not_exist_12345.txt" -l "$PWD/test_output.txt" > sftp_status_failure.log 2>&1
RESULT=$?
grep -q "Unable to copy remote file" sftp_status_failure.log
RESULT_MSG=$?
remove_ready_file
rm -f sftp_status_failure.log
rm -f "$PWD/test_output.txt"
if [ $RESULT -eq 0 ]; then
echo
echo "ERROR: Should have failed for non-existing file"
do_cleanup
exit 1
fi
if [ $RESULT_MSG -ne 0 ]; then
echo
echo "ERROR: Unexpected failure path while testing missing remote file"
do_cleanup
exit 1
fi
echo "Successfully received failure status packet"
fi
echo
echo "ALL Tests Passed"
exit 0