-
Notifications
You must be signed in to change notification settings - Fork 104
Expand file tree
/
Copy pathfwd.test.expect
More file actions
executable file
·154 lines (134 loc) · 4.5 KB
/
fwd.test.expect
File metadata and controls
executable file
·154 lines (134 loc) · 4.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#!/usr/bin/env 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
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 fugat 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
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}
}
}
puts "Done."
}
# --- Fail helper -------------------------------------------------------------
proc fail {msg} {
puts "\n\[FAIL\] $msg"
cleanup
exit 1
}
# --- Check prerequisites -----------------------------------------------------
foreach tool {nc} {
if {[catch {exec which $tool}]} {
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 ./examples/echoserver/echoserver -1 -f
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 ./examples/portfwd/portfwd -u jill -P upthehill -f 12345 -t 11111
set wolfssh_clt_id $spawn_id
set wolfssh_clt_pid [exp_pid]
expect {
-i $wolfssh_clt_id
-re {sampled} {
puts " wolfssh client ready (PID $wolfssh_clt_pid)."
}
-re {(?i)(error|fatal)} {
fail "wolfssh client failed to start"
}
timeout {
fail "Timed out waiting for wolfssh client to start"
}
}
# Brief pause to let the wolfssh tunnels fully bind their listening ports
sleep 1
# --- [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