Skip to content

Commit 676df52

Browse files
committed
Forwarding Test
1. Add a test script for testing forwarding. 2. Update portfwd to have a ready file option.
1 parent f18c692 commit 676df52

File tree

3 files changed

+123
-2
lines changed

3 files changed

+123
-2
lines changed

examples/portfwd/portfwd.c

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,7 @@ THREAD_RETURN WOLFSSH_THREAD portfwd_worker(void* args)
235235
const char* fwdToHost = NULL;
236236
const char* username = NULL;
237237
const char* password = NULL;
238+
const char* readyFile = NULL;
238239
SOCKADDR_IN_T hostAddr;
239240
socklen_t hostAddrSz = sizeof(hostAddr);
240241
SOCKET_T sshFd;
@@ -266,7 +267,7 @@ THREAD_RETURN WOLFSSH_THREAD portfwd_worker(void* args)
266267

267268
((func_args*)args)->return_code = 0;
268269

269-
while ((ch = mygetopt(argc, argv, "?f:h:p:t:u:F:P:T:")) != -1) {
270+
while ((ch = mygetopt(argc, argv, "?f:h:p:t:u:F:P:R:T:")) != -1) {
270271
switch (ch) {
271272
case 'h':
272273
host = myoptarg;
@@ -306,6 +307,10 @@ THREAD_RETURN WOLFSSH_THREAD portfwd_worker(void* args)
306307
password = myoptarg;
307308
break;
308309

310+
case 'R':
311+
readyFile = myoptarg;
312+
break;
313+
309314
case 'T':
310315
fwdToHost = myoptarg;
311316
break;
@@ -404,6 +409,21 @@ THREAD_RETURN WOLFSSH_THREAD portfwd_worker(void* args)
404409
if (ret != WS_SUCCESS)
405410
err_sys("Couldn't connect SFTP");
406411

412+
if (readyFile != NULL) {
413+
#ifndef NO_FILESYSTEM
414+
WFILE* f = NULL;
415+
ret = WFOPEN(NULL, &f, readyFile, "w");
416+
if (f != NULL && ret == 0) {
417+
char portStr[10];
418+
int l = WSNPRINTF(portStr, sizeof(portStr), "%d\n", (int)port);
419+
WFWRITE(NULL, portStr, MIN((size_t)l, sizeof(portStr)), 1, f);
420+
WFCLOSE(NULL, f);
421+
}
422+
#else
423+
err_sys("cannot create readyFile with no file system.\r\n");
424+
#endif
425+
}
426+
407427
FD_ZERO(&templateFds);
408428
FD_SET(sshFd, &templateFds);
409429
FD_SET(listenFd, &templateFds);

scripts/fwd.test

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
#!/usr/bin/env bash
2+
3+
NCSVRPORT=11111
4+
NCCLIPORT=12345
5+
ECHO_READY=ready.es.$$
6+
FWD_READY=ready.fwd.$$
7+
ESPID=0
8+
NCSVRPID=0
9+
FWDPID=0
10+
11+
do_cleanup() {
12+
rm -f "$ECHO_READY" "$FWD_READY"
13+
if [ "$ESPID" -ne 0 ]
14+
then
15+
kill "$ESPID" >/dev/null 2>&1 || true
16+
fi
17+
if [ "$NCSVRPID" -ne 0 ]
18+
then
19+
kill "$NCSVRPID" >/dev/null 2>&1 || true
20+
fi
21+
if [ "$FWDPID" -ne 0 ]
22+
then
23+
kill "$FWDPID" >/dev/null 2>&1 || true
24+
fi
25+
}
26+
27+
wait_for_ready_file() {
28+
READY_COUNTER=0
29+
while [ ! -s "$1" ] && [ "$READY_COUNTER" -lt 20 ]
30+
do
31+
sleep 0.1
32+
READY_COUNTER=$((READY_COUNTER + 1))
33+
done
34+
35+
if [ ! -e "$1" ]
36+
then
37+
echo "$2 never started."
38+
do_cleanup
39+
exit 1
40+
fi
41+
}
42+
43+
# Check for prerequisites: build for forwarding, nc present
44+
45+
# libtool can leave behind a script that runs the actual executable.
46+
if [ ! -x ./examples/echoserver/echoserver ] || \
47+
./examples/echoserver/echoserver "-?" 2>&1 | grep -q "does not exist"
48+
then
49+
echo "This test requires the echoserver."
50+
exit 1
51+
fi
52+
53+
# libtool can leave behind a script that runs the actual executable.
54+
if [ ! -x ./examples/portfwd/portfwd ] || \
55+
./examples/portfwd/portfwd "-?" 2>&1 | grep -q "does not exist"
56+
then
57+
echo "Port forwarding not enabled. Skipping."
58+
exit 77
59+
fi
60+
61+
if [ ! -x "$(which nc)" ]
62+
then
63+
echo "Tool nc not installed. Skipping."
64+
exit 77
65+
fi
66+
67+
nc -l $NCSVRPORT &
68+
NCSVRPID=$!
69+
./examples/echoserver/echoserver -1 -f -R "$ECHO_READY" &
70+
ESPID=$!
71+
wait_for_ready_file "$ECHO_READY" "Echoserver"
72+
./examples/portfwd/portfwd -p "$(cat $ECHO_READY)" -R "$FWD_READY" \
73+
-u jill -P upthehill -f "$NCCLIPORT" -t "$NCSVRPORT" &
74+
FWDPID=$!
75+
wait_for_ready_file "$FWD_READY" "Port forwarding"
76+
77+
nc -w 2 -4 localhost $NCCLIPORT <<END &
78+
This is some test text.
79+
And some more test text.
80+
Last of the test text.
81+
Later.
82+
END
83+
NCCLIPID=$!
84+
85+
{
86+
sleep 10
87+
# If the nc client is there, kill it.
88+
if kill -0 "$NCCLIPID" 2>/dev/null
89+
then
90+
kill "$NCCLIPID"
91+
fi
92+
} &
93+
MONPID=$!
94+
95+
wait "$NCCLIPID"
96+
CLIENTRESULT=$?
97+
98+
kill "$MONPID" 2>/dev/null || true
99+
100+
do_cleanup
101+
exit "$CLIENTRESULT"

scripts/include.am

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@ if BUILD_SCP
1111
dist_noinst_SCRIPTS+= scripts/scp.test
1212
endif
1313

14-
dist_noinst_SCRIPTS+= scripts/external.test
14+
dist_noinst_SCRIPTS+= scripts/external.test scripts/fwd.test

0 commit comments

Comments
 (0)