From 864908b65ab82143ca5fbe97fb9b8c33e1020029 Mon Sep 17 00:00:00 2001 From: Jacob Barthelmeh Date: Tue, 20 Aug 2019 13:24:19 -0600 Subject: [PATCH] add test case for wolfSSH_SFTP_SendReadPacket --- tests/api.c | 186 ++++++++++++++++++++++++++++++++++++++++++++++ tests/include.am | 3 +- tests/testsuite.c | 15 ---- wolfssh/test.h | 16 +++- 4 files changed, 203 insertions(+), 17 deletions(-) diff --git a/tests/api.c b/tests/api.c index ddd0bf06..3b9bc7e9 100644 --- a/tests/api.c +++ b/tests/api.c @@ -25,6 +25,21 @@ #ifdef WOLFSSH_SCP #include #endif +#ifdef WOLFSSH_SFTP + #define WOLFSSH_TEST_LOCKING + #define WOLFSSH_TEST_THREADING + + #define WOLFSSH_TEST_SERVER + #define WOLFSSH_TEST_ECHOSERVER + #include + + #include "examples/echoserver/echoserver.h" + +#endif + +/* for echoserver test cases */ +int myoptind = 0; +char* myoptarg = NULL; #define Fail(description, result) do { \ @@ -231,6 +246,93 @@ static int ConvertHexToBin(const char* h1, byte** b1, word32* b1Sz, return 0; } +#ifdef WOLFSSH_SFTP +byte userPassword[256]; +static int sftpUserAuth(byte authType, WS_UserAuthData* authData, void* ctx) +{ + int ret = WOLFSSH_USERAUTH_INVALID_AUTHTYPE; + + if (authType == WOLFSSH_USERAUTH_PASSWORD) { + const char* defaultPassword = (const char*)ctx; + word32 passwordSz; + + ret = WOLFSSH_USERAUTH_SUCCESS; + if (defaultPassword != NULL) { + passwordSz = (word32)strlen(defaultPassword); + memcpy(userPassword, defaultPassword, passwordSz); + } + else { + printf("Expecting password set for test cases\n"); + return ret; + } + + if (ret == WOLFSSH_USERAUTH_SUCCESS) { + authData->sf.password.password = userPassword; + authData->sf.password.passwordSz = passwordSz; + } + } + return ret; +} + +/* preforms connection to port, sets WOLFSSH_CTX and WOLFSSH on success + * caller needs to free ctx and ssh when done + */ +static void sftp_client_connect(WOLFSSH_CTX** ctx, WOLFSSH** ssh, int port) +{ + SOCKET_T sockFd = WOLFSSH_SOCKET_INVALID; + SOCKADDR_IN_T clientAddr; + socklen_t clientAddrSz = sizeof(clientAddr); + int ret; + char* host = (char*)wolfSshIp; + const char* username = "jill"; + const char* password = "upthehill"; + + if (ctx == NULL || ssh == NULL) { + return; + } + + *ctx = wolfSSH_CTX_new(WOLFSSH_ENDPOINT_CLIENT, NULL); + if (*ctx == NULL) { + return; + } + + wolfSSH_SetUserAuth(*ctx, sftpUserAuth); + *ssh = wolfSSH_new(*ctx); + if (*ssh == NULL) { + wolfSSH_CTX_free(*ctx); + *ctx = NULL; + return; + } + + build_addr(&clientAddr, host, port); + tcp_socket(&sockFd); + ret = connect(sockFd, (const struct sockaddr *)&clientAddr, clientAddrSz); + if (ret != 0){ + wolfSSH_free(*ssh); + wolfSSH_CTX_free(*ctx); + *ctx = NULL; + *ssh = NULL; + return; + } + + wolfSSH_SetUserAuthCtx(*ssh, (void*)password); + ret = wolfSSH_SetUsername(*ssh, username); + if (ret == WS_SUCCESS) + ret = wolfSSH_set_fd(*ssh, (int)sockFd); + + if (ret == WS_SUCCESS) + ret = wolfSSH_SFTP_connect(*ssh); + + if (ret != WS_SUCCESS){ + wolfSSH_free(*ssh); + wolfSSH_CTX_free(*ctx); + *ctx = NULL; + *ssh = NULL; + return; + } +} +#endif /* WOLFSSH_SFTP */ + enum WS_TestEndpointTypes { TEST_GOOD_ENDPOINT_SERVER = WOLFSSH_ENDPOINT_SERVER, @@ -529,6 +631,86 @@ static void test_wolfSSH_SCP_CB(void) #endif /* WOLFSSH_NO_CLIENT */ } +static void test_wolfSSH_SFTP_SendReadPacket(void) +{ +#ifdef WOLFSSH_SFTP + func_args ser; + tcp_ready ready; + int argsCount; + + const char* args[10]; + WOLFSSH_CTX* ctx = NULL; + WOLFSSH* ssh = NULL; + + THREAD_TYPE serThread; + + WMEMSET(&ser, 0, sizeof(func_args)); + + argsCount = 0; + args[argsCount++] = "."; + args[argsCount++] = "-1"; +#ifndef USE_WINDOWS_API + args[argsCount++] = "-p"; + args[argsCount++] = "0"; +#endif + ser.argv = (char**)args; + ser.argc = argsCount; + ser.signal = &ready; + InitTcpReady(ser.signal); + ThreadStart(echoserver_test, (void*)&ser, &serThread); + WaitTcpReady(&ser); + + sftp_client_connect(&ctx, &ssh, ready.port); + AssertNotNull(ctx); + AssertNotNull(ssh); + + { + WS_SFTPNAME* tmp; + WS_SFTPNAME* current; + byte handle[WOLFSSH_MAX_HANDLE]; + word32 handleSz = WOLFSSH_MAX_HANDLE; + const char* currentDir = "."; + byte* out = NULL; + int outSz = 18; + const word32 ofst[2] = {0}; + + current = wolfSSH_SFTP_LS(ssh, (char*)currentDir); + tmp = current; + while (tmp != NULL) { + if (tmp->atrb.sz[0] > 0) { + if (WMEMCMP(tmp->fName , ".", sizeof(".")) != 0 && + WMEMCMP(tmp->fName, "..", sizeof("..")) != 0) + break; + } + tmp = tmp->next; + } + + out = (byte*)malloc(tmp->atrb.sz[0]); + AssertIntEQ(wolfSSH_SFTP_Open(ssh, tmp->fName, WOLFSSH_FXF_READ, NULL, + handle, &handleSz), WS_SUCCESS); + + outSz = 18; + AssertIntEQ(wolfSSH_SFTP_SendReadPacket(ssh, handle, handleSz, ofst, + out, outSz), outSz); + + outSz = tmp->atrb.sz[0]; + AssertIntEQ(wolfSSH_SFTP_SendReadPacket(ssh, handle, handleSz, ofst, + out, outSz), outSz); + + free(out); + wolfSSH_SFTP_Close(ssh, handle, handleSz); + wolfSSH_SFTPNAME_list_free(current); + } + + AssertIntEQ(wolfSSH_shutdown(ssh), WS_SUCCESS); + wolfSSH_free(ssh); + wolfSSH_CTX_free(ctx); + ThreadJoin(serThread); +#endif +} + + + #ifdef USE_WINDOWS_API static byte color_test[] = { 0x1B, 0x5B, 0x34, 0x6D, 0x75, 0x6E, 0x64, 0x65, @@ -636,6 +818,10 @@ int main(void) /* SCP tests */ test_wolfSSH_SCP_CB(); + /* SFTP tests */ + test_wolfSSH_SFTP_SendReadPacket(); + + AssertIntEQ(wolfSSH_Cleanup(), WS_SUCCESS); return 0; diff --git a/tests/include.am b/tests/include.am index cdef8b96..986f31f3 100644 --- a/tests/include.am +++ b/tests/include.am @@ -18,7 +18,8 @@ endif tests_unit_test_LDADD = src/libwolfssh.la tests_unit_test_DEPENDENCIES = src/libwolfssh.la -tests_api_test_SOURCES = tests/api.c +tests_api_test_SOURCES = tests/api.c \ + examples/echoserver/echoserver.c tests_api_test_CPPFLAGS = -DNO_MAIN_DRIVER if BUILD_SCP tests_api_test_CPPFLAGS += -DWOLFSSH_SCP diff --git a/tests/testsuite.c b/tests/testsuite.c index eaca68a6..25c2fc32 100644 --- a/tests/testsuite.c +++ b/tests/testsuite.c @@ -164,18 +164,3 @@ int TestsuiteTest(int argc, char** argv) #endif /* !NO_WOLFSSH_SERVER && !NO_WOLFSSH_CLIENT */ -void WaitTcpReady(func_args* args) -{ -#if defined(_POSIX_THREADS) && !defined(__MINGW32__) - pthread_mutex_lock(&args->signal->mutex); - - if (!args->signal->ready) - pthread_cond_wait(&args->signal->cond, &args->signal->mutex); - args->signal->ready = 0; /* reset */ - - pthread_mutex_unlock(&args->signal->mutex); -#else - (void)args; -#endif -} - diff --git a/wolfssh/test.h b/wolfssh/test.h index 97853d00..b7c94b85 100644 --- a/wolfssh/test.h +++ b/wolfssh/test.h @@ -741,7 +741,21 @@ static INLINE void FreeTcpReady(tcp_ready* ready) } -void WaitTcpReady(func_args*); +static INLINE void WaitTcpReady(func_args* args) +{ +#if defined(_POSIX_THREADS) && !defined(__MINGW32__) + pthread_mutex_lock(&args->signal->mutex); + + if (!args->signal->ready) + pthread_cond_wait(&args->signal->cond, &args->signal->mutex); + args->signal->ready = 0; /* reset */ + + pthread_mutex_unlock(&args->signal->mutex); +#else + (void)args; +#endif +} + #endif /* WOLFSSH_TEST_LOCKING */