Merge pull request #81 from JacobBarthelmeh/testing

additional sftp and scp tests
pull/83/head
John Safranek 2018-07-16 10:49:42 -07:00 committed by GitHub
commit 8a010c168e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 257 additions and 11 deletions

View File

@ -16,6 +16,8 @@ EXTRA_HEADERS =
BUILT_SOURCES=
EXTRA_DIST=
dist_doc_DATA=
dist_noinst_SCRIPTS=
check_SCRIPTS=
#includes additional rules from aminclude.am
@ -37,10 +39,15 @@ include tests/include.am
include keys/include.am
include ide/include.am
include wolfsftp/include.am
include scripts/include.am
TEST_EXTENSIONS = .test
TESTS += $(check_PROGRAMS)
check_SCRIPTS+= $(dist_noinst_SCRIPTS)
TESTS += $(check_SCRIPTS)
test: check
DISTCLEANFILES+= wolfssh-config

View File

@ -100,6 +100,8 @@ static void ShowUsage(void)
printf(" -p <num> port to connect on, default %d\n", wolfSshPort);
printf(" -u <username> username to authenticate as (REQUIRED)\n");
printf(" -P <password> password for username, prompted if omitted\n");
printf(" -x exit after successful connection without doing\n"
" read/write\n");
}
@ -160,12 +162,13 @@ THREAD_RETURN WOLFSSH_THREAD client_test(void* args)
char* host = (char*)wolfSshIp;
const char* username = NULL;
const char* password = NULL;
byte imExit = 0;
int argc = ((func_args*)args)->argc;
char** argv = ((func_args*)args)->argv;
((func_args*)args)->return_code = 0;
while ((ch = mygetopt(argc, argv, "?h:p:u:P:")) != -1) {
while ((ch = mygetopt(argc, argv, "?h:p:u:P:x")) != -1) {
switch (ch) {
case 'h':
host = myoptarg;
@ -187,6 +190,11 @@ THREAD_RETURN WOLFSSH_THREAD client_test(void* args)
password = myoptarg;
break;
case 'x':
/* exit after successful connection without read/write */
imExit = 1;
break;
case '?':
ShowUsage();
exit(EXIT_SUCCESS);
@ -235,16 +243,18 @@ THREAD_RETURN WOLFSSH_THREAD client_test(void* args)
if (ret != WS_SUCCESS)
err_sys("Couldn't connect SSH stream.");
ret = wolfSSH_stream_send(ssh, (byte*)testString,
(word32)strlen(testString));
if (ret <= 0)
err_sys("Couldn't send test string.");
if (!imExit) {
ret = wolfSSH_stream_send(ssh, (byte*)testString,
(word32)strlen(testString));
if (ret <= 0)
err_sys("Couldn't send test string.");
ret = wolfSSH_stream_read(ssh, (byte*)rxBuf, sizeof(rxBuf) - 1);
if (ret <= 0)
err_sys("Stream read failed.");
rxBuf[ret] = '\0';
printf("Server said: %s\n", rxBuf);
ret = wolfSSH_stream_read(ssh, (byte*)rxBuf, sizeof(rxBuf) - 1);
if (ret <= 0)
err_sys("Stream read failed.");
rxBuf[ret] = '\0';
printf("Server said: %s\n", rxBuf);
}
ret = wolfSSH_shutdown(ssh);
if (ret != WS_SUCCESS)

View File

@ -599,13 +599,14 @@ THREAD_RETURN WOLFSSH_THREAD echoserver_test(void* args)
int useEcc = 0;
char ch;
word16 port = wolfSshPort;
char* readyFile = NULL;
int argc = serverArgs->argc;
char** argv = serverArgs->argv;
serverArgs->return_code = 0;
if (argc > 0) {
while ((ch = mygetopt(argc, argv, "?1ep:")) != -1) {
while ((ch = mygetopt(argc, argv, "?1ep:R:")) != -1) {
switch (ch) {
case '?' :
ShowUsage();
@ -627,6 +628,10 @@ THREAD_RETURN WOLFSSH_THREAD echoserver_test(void* args)
#endif
break;
case 'R':
readyFile = myoptarg;
break;
default:
ShowUsage();
exit(MY_EX_USAGE);
@ -706,7 +711,21 @@ THREAD_RETURN WOLFSSH_THREAD echoserver_test(void* args)
}
#endif
/* if creating a ready file with port then override port to be 0 */
if (readyFile != NULL) {
port = 0;
}
tcp_listen(&listenFd, &port, 1);
/* write out port number listing to, to user set ready file */
if (readyFile != NULL) {
WFILE* f = NULL;
int ret;
ret = WFOPEN(&f, readyFile, "w");
if (f != NULL && ret == 0) {
fprintf(f, "%d\n", (int)port);
WFCLOSE(f);
}
}
do {
SOCKET_T clientFd = 0;

View File

@ -0,0 +1,60 @@
#!/bin/sh
# external tests
host="test.rebex.net"
user="demo"
password="password"
if test -n "$WOLFSSH_EXTERNAL_TEST"; then
echo "WOLFSSH_EXTERNAL_TEST set, running test..."
else
echo "WOLFSSH_EXTERNAL_TEST NOT set, won't run"
exit 0
fi
do_cleanup() {
echo "in cleanup"
}
do_trap() {
echo "got trap"
do_cleanup
exit -1
}
trap do_trap INT TERM
[ ! -x ./examples/client/client ] && echo -e "\n\nClient doesn't exist" && exit 1
echo "Testing client connection to $host : "
./examples/client/client -u $user -P $password -h $host -p 22 -x
RESULT=$?
if [ $RESULT -ne 0 ]; then
echo -e "failed to connect\n"
do_cleanup
exit 1
fi
echo -e "Success\n"
# not having the sftp client built in is not a failure case
./wolfsftp/client/wolfsftp -h
if [ $? == 0 ]; then
echo "Testing wolfsftp connection to $host : "
echo "exit" | ./wolfsftp/client/wolfsftp -u $user -P $password -h $host -p 22
RESULT=$?
if [ $RESULT -ne 0 ]; then
echo -e "failed to connect\n"
do_cleanup
exit 1
else
echo -e "Success\n"
fi
else
echo -e "\n\nwolfSFTP client doesn't exist"
fi
echo -e "\nALL Tests Passed"
exit 0

12
scripts/include.am 100644
View File

@ -0,0 +1,12 @@
# vim:ft=automake
# included from Top Level Makefile.am
# All paths should be given relative to the root
if BUILD_SFTP
dist_noinst_SCRIPTS+= scripts/sftp.test
endif
dist_noinst_SCRIPTS+= scripts/external.test

77
scripts/sftp.test 100755
View File

@ -0,0 +1,77 @@
#!/bin/sh
# sftp local test
no_pid=-1
server_pid=$no_pid
ready_file=`pwd`/wolfssh_sftp_ready$$
counter=0
[ ! -x ./wolfsftp/client/wolfsftp ] && echo -e "\n\nwolfSFTP client doesn't exist" && exit 1
#echo "ready file $ready_file"
create_port() {
while [ ! -s "$ready_file" ] && [ "$counter" -lt 20 ]; do
echo -e "waiting for ready file..."
sleep 0.1
counter=$((counter+ 1))
done
if test -e $ready_file; then
echo -e "found ready file, starting client..."
# get created port 0 ephemeral port
port=`cat $ready_file`
else
echo -e "NO ready file ending test..."
do_cleanup
exit 1
fi
}
remove_ready_file() {
if test -e $ready_file; then
echo -e "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
[ ! -x ./wolfsftp/client/wolfsftp ] && echo -e "\n\nClient doesn't exist" && exit 1
./examples/echoserver/echoserver -1 -R $ready_file &
server_pid=$!
create_port
echo "ls\nexit" | ./wolfsftp/client/wolfsftp -u jill -P upthehill -p $port
RESULT=$?
remove_ready_file
# if fail here then is a settings issue so return 0
if [ $RESULT -ne 0 ]; then
echo -e "\n\nfailed to connect"
do_cleanup
exit 1
fi
echo -e "\nALL Tests Passed"
exit 0

View File

@ -21,6 +21,9 @@
#include <stdio.h>
#include <wolfssh/ssh.h>
#ifdef WOLFSSH_SCP
#include <wolfssh/wolfscp.h>
#endif
#define Fail(description, result) do { \
@ -154,6 +157,61 @@ static void test_wolfSSH_SetUsername(void)
}
#ifdef WOLFSSH_SCP
static int my_ScpRecv(WOLFSSH* ssh, int state, const char* basePath,
const char* fileName, int fileMode, word64 mTime, word64 aTime,
word32 totalFileSz, byte* buf, word32 bufSz, word32 fileOffset,
void* ctx)
{
(void)ssh;
printf("calling scp recv cb with state %d\n", state);
printf("\tbase path = %s\n", basePath);
printf("\tfile name = %s\n", fileName);
printf("\tfile mode = %d\n", fileMode);
printf("\tfile size = %d\n", totalFileSz);
printf("\tfile offset = %d\n", fileOffset);
(void)mTime;
(void)aTime;
(void)buf;
(void)bufSz;
(void)ctx;
return WS_SCP_ABORT; /* error out for test function */
}
#endif
static void test_wolfSSH_SCP_CB(void)
{
#ifdef WOLFSSH_SCP
WOLFSSH_CTX* ctx;
WOLFSSH* ssh;
int i = 3, j = 4; /* arbitrary value */
const char err[] = "test setting error msg";
AssertIntNE(WS_SUCCESS, wolfSSH_SetUsername(NULL, NULL));
AssertNotNull(ctx = wolfSSH_CTX_new(WOLFSSH_ENDPOINT_SERVER, NULL));
wolfSSH_SetScpRecv(ctx, my_ScpRecv);
AssertNotNull(ssh = wolfSSH_new(ctx));
wolfSSH_SetScpRecvCtx(ssh, (void*)&i);
AssertIntEQ(i, *(int*)wolfSSH_GetScpRecvCtx(ssh));
wolfSSH_SetScpSendCtx(ssh, (void*)&j);
AssertIntEQ(j, *(int*)wolfSSH_GetScpSendCtx(ssh));
AssertIntNE(j, *(int*)wolfSSH_GetScpRecvCtx(ssh));
AssertIntEQ(wolfSSH_SetScpErrorMsg(ssh, err), WS_SUCCESS);
wolfSSH_free(ssh);
wolfSSH_CTX_free(ctx);
#endif /* WOLFSSH_NO_CLIENT */
}
int main(void)
{
AssertIntEQ(wolfSSH_Init(), WS_SUCCESS);
@ -163,6 +221,9 @@ int main(void)
test_client_wolfSSH_new();
test_wolfSSH_SetUsername();
/* SCP tests */
test_wolfSSH_SCP_CB();
AssertIntEQ(wolfSSH_Cleanup(), WS_SUCCESS);
return 0;