wolfssh/scripts/fwd.test.expect

162 lines
5.2 KiB
Plaintext
Executable File

#!/usr/bin/env -S expect -f
#
# SSH Tunnel Test Script
#
# Tests an SSH tunnel using wolfSSH and netcat (nc).
#
# Architecture:
#
# [nc client] --plain--> :12345 [wolfssh client]
# |
# SSH
# |
# [wolfssh server] :ephem --plain--> :11111 [nc server]
#
# The nc client sends each line of a Lorem Ipsum paragraph through the tunnel
# to the nc server one at a time. The server echoes each line back. Both sides
# verify receipt of every line.
#
# Ports used:
# 11111 - nc server (plain text backend)
# 12345 - wolfssh client listener (plain text, nc connects here)
# 22222 - wolfSSH rendezvous (SSH, internal use only)
#
# Requirements: nc (netcat), expect
set timeout 30
# Logs / ready file. Under --enable-debug builds wolfSSH's WLOG output is
# voluminous; if we let it write to expect's spawned pty, the pty buffer
# fills and the process blocks in write(), which can stall portfwd inside
# its select() loop before it ever sends the channel-open. Redirect to
# regular files instead, and use portfwd's -R ready file for sync.
set srv_log "/tmp/fwd.test.echoserver.[pid].log"
set clt_log "/tmp/fwd.test.portfwd.[pid].log"
set clt_ready "/tmp/fwd.test.portfwd.[pid].ready"
file delete -force $srv_log $clt_log $clt_ready
set lorem_lines {
{Lorem ipsum dolor sit amet, consectetur adipiscing elit,}
{sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.}
{Ut enim ad minim veniam, quis nostrud exercitation ullamco}
{laboris nisi ut aliquip ex ea commodo consequat.}
{Duis aute irure dolor in reprehenderit in voluptate velit esse}
{cillum dolore eu fugiat nulla pariatur.}
{Excepteur sint occaecat cupidatat non proident, sunt in culpa qui}
{officia deserunt mollit anim id est laborum.}
}
# PIDs for cleanup
set nc_server_pid ""
set wolfssh_srv_pid ""
set wolfssh_clt_pid ""
set nc_client_pid ""
# --- Cleanup -----------------------------------------------------------------
proc cleanup {} {
global nc_client_pid wolfssh_clt_pid wolfssh_srv_pid nc_server_pid
global srv_log clt_log clt_ready
puts "\n--- Cleaning up ---"
foreach pid [list $nc_client_pid $wolfssh_clt_pid $wolfssh_srv_pid $nc_server_pid] {
if {$pid ne ""} {
catch {exec kill $pid}
}
}
file delete -force $srv_log $clt_log $clt_ready
puts "Done."
}
# --- Fail helper -------------------------------------------------------------
proc fail {msg} {
puts "\n\[FAIL\] $msg"
cleanup
exit 1
}
# --- Check prerequisites -----------------------------------------------------
foreach tool {nc} {
if {[auto_execok $tool] eq ""} {
puts "ERROR: '$tool' not found in PATH"
exit 1
}
}
# --- [1] Start nc server -----------------------------------------------------
puts "\n\[1\] Starting nc server: nc -l 11111"
spawn nc -l 11111
set nc_server_id $spawn_id
set nc_server_pid [exp_pid]
puts " PID $nc_server_pid - waiting for a connection..."
# --- [2] Start wolfssh server ------------------------------------------------
puts "\n\[2\] Starting wolfssh server..."
spawn sh -c "exec ./examples/echoserver/echoserver -1 -f >$srv_log 2>&1"
set wolfssh_srv_id $spawn_id
set wolfssh_srv_pid [exp_pid]
puts " PID $wolfssh_srv_pid - waiting for a connection..."
# --- [3] Start wolfssh client ------------------------------------------------
puts "\n\[3\] Starting wolfssh client (plain:12345 -> 11111)..."
spawn sh -c "exec ./examples/portfwd/portfwd -u jill -P upthehill -f 12345 -t 11111 -R $clt_ready >$clt_log 2>&1"
set wolfssh_clt_id $spawn_id
set wolfssh_clt_pid [exp_pid]
# portfwd writes the listening port to $clt_ready once SSH is up.
set elapsed 0
while {![file exists $clt_ready] && $elapsed < $timeout} {
sleep 1
incr elapsed
}
if {![file exists $clt_ready]} {
fail "Timed out waiting for wolfssh client to start (no $clt_ready)"
}
puts " wolfssh client ready (PID $wolfssh_clt_pid)."
# --- [4] Start nc client -----------------------------------------------------
puts "\n\[4\] Starting nc client: nc localhost 12345"
spawn nc localhost 12345
set nc_client_id $spawn_id
set nc_client_pid [exp_pid]
puts " PID $nc_client_pid"
# Allow the TCP handshake and SSH negotiation to complete
sleep 1
# --- [5] Send each line, verify receipt, echo back, verify echo --------------
set n [llength $lorem_lines]
set i 0
foreach line $lorem_lines {
incr i
puts "\n\[5.$i/$n\] Client sending: \"$line\""
send -i $nc_client_id "$line\n"
expect {
-i $nc_server_id
-ex $line {
puts " \[PASS\] Server received line $i."
}
timeout {
fail "Server did not receive line $i within ${timeout}s"
}
}
send -i $nc_server_id "$line\n"
expect {
-i $nc_client_id
-ex $line {
puts " \[PASS\] Client received echo of line $i."
}
timeout {
fail "Client did not receive echo of line $i within ${timeout}s"
}
}
}
# --- Done --------------------------------------------------------------------
puts "\n=== TEST PASSED ===\n"
cleanup
exit 0