From 2c4bca35e0a4f5f84da1e89112b1a15eec1829f8 Mon Sep 17 00:00:00 2001 From: Jacob Barthelmeh Date: Mon, 2 Jul 2018 11:01:22 -0600 Subject: [PATCH 1/4] sftp test script added --- Makefile.am | 7 +++ examples/echoserver/echoserver.c | 21 ++++++++- scripts/include.am | 10 +++++ scripts/sftp.test | 77 ++++++++++++++++++++++++++++++++ 4 files changed, 114 insertions(+), 1 deletion(-) create mode 100644 scripts/include.am create mode 100755 scripts/sftp.test diff --git a/Makefile.am b/Makefile.am index c053041..12163ae 100644 --- a/Makefile.am +++ b/Makefile.am @@ -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 diff --git a/examples/echoserver/echoserver.c b/examples/echoserver/echoserver.c index 7518f81..0e683af 100644 --- a/examples/echoserver/echoserver.c +++ b/examples/echoserver/echoserver.c @@ -590,13 +590,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(); @@ -618,6 +619,10 @@ THREAD_RETURN WOLFSSH_THREAD echoserver_test(void* args) #endif break; + case 'R': + readyFile = myoptarg; + break; + default: ShowUsage(); exit(MY_EX_USAGE); @@ -697,7 +702,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; diff --git a/scripts/include.am b/scripts/include.am new file mode 100644 index 0000000..3165e6b --- /dev/null +++ b/scripts/include.am @@ -0,0 +1,10 @@ +# 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 + diff --git a/scripts/sftp.test b/scripts/sftp.test new file mode 100755 index 0000000..eca5f43 --- /dev/null +++ b/scripts/sftp.test @@ -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 + From a631cf5794c1857602db72ab10ed9293a5ff1386 Mon Sep 17 00:00:00 2001 From: Jacob Barthelmeh Date: Mon, 2 Jul 2018 12:02:19 -0600 Subject: [PATCH 2/4] add SCP functions to api tests --- tests/api.c | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) diff --git a/tests/api.c b/tests/api.c index 5cb08dd..919bedb 100644 --- a/tests/api.c +++ b/tests/api.c @@ -21,6 +21,9 @@ #include #include +#ifdef WOLFSSH_SCP + #include +#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; From 73bcd5c95a8c78087294001e7a9ac5c89213abe9 Mon Sep 17 00:00:00 2001 From: Jacob Barthelmeh Date: Mon, 2 Jul 2018 17:07:51 -0600 Subject: [PATCH 3/4] external test script --- examples/client/client.c | 30 +++++++++++++------- scripts/external.test | 59 ++++++++++++++++++++++++++++++++++++++++ scripts/include.am | 2 ++ 3 files changed, 81 insertions(+), 10 deletions(-) create mode 100755 scripts/external.test diff --git a/examples/client/client.c b/examples/client/client.c index 7793e7f..b6f1e69 100644 --- a/examples/client/client.c +++ b/examples/client/client.c @@ -100,6 +100,8 @@ static void ShowUsage(void) printf(" -p port to connect on, default %d\n", wolfSshPort); printf(" -u username to authenticate as (REQUIRED)\n"); printf(" -P 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) diff --git a/scripts/external.test b/scripts/external.test new file mode 100755 index 0000000..07a9ccd --- /dev/null +++ b/scripts/external.test @@ -0,0 +1,59 @@ +#!/bin/sh + +# external tests +host="test.rebex.net" +user="demo" +password="password" + +if test -n "$WOLFSSH_EXTERNAL_TEST"; then + echo "WOLFSSL_EXTERNAH_TEST set, running test..." +else + echo "WOLFSSL_EXTERNAH_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 +if [ -x ./wolfsftp/client/wolfsftp ]; 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 + diff --git a/scripts/include.am b/scripts/include.am index 3165e6b..8598f7f 100644 --- a/scripts/include.am +++ b/scripts/include.am @@ -8,3 +8,5 @@ if BUILD_SFTP dist_noinst_SCRIPTS+= scripts/sftp.test endif +dist_noinst_SCRIPTS+= scripts/external.test + From a1e2cde48599aef816b2c33ea22218794c5b6e89 Mon Sep 17 00:00:00 2001 From: Jacob Barthelmeh Date: Mon, 2 Jul 2018 17:19:43 -0600 Subject: [PATCH 4/4] fix spelling mistake and change check for sftp client --- scripts/external.test | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/scripts/external.test b/scripts/external.test index 07a9ccd..96a0c7a 100755 --- a/scripts/external.test +++ b/scripts/external.test @@ -6,9 +6,9 @@ user="demo" password="password" if test -n "$WOLFSSH_EXTERNAL_TEST"; then - echo "WOLFSSL_EXTERNAH_TEST set, running test..." + echo "WOLFSSH_EXTERNAL_TEST set, running test..." else - echo "WOLFSSL_EXTERNAH_TEST NOT set, won't run" + echo "WOLFSSH_EXTERNAL_TEST NOT set, won't run" exit 0 fi @@ -38,7 +38,8 @@ echo -e "Success\n" # not having the sftp client built in is not a failure case -if [ -x ./wolfsftp/client/wolfsftp ]; then +./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=$?