diff --git a/examples/echoserver/echoserver.c b/examples/echoserver/echoserver.c index 010c67aa..aecc8c7c 100644 --- a/examples/echoserver/echoserver.c +++ b/examples/echoserver/echoserver.c @@ -452,6 +452,9 @@ static int wolfSSH_FwdDefaultActions(WS_FwdCbAction action, void* vCtx, ctx->state = FWD_STATE_INIT; } else if (action == WOLFSSH_FWD_REMOTE_SETUP) { + struct sockaddr_in addr; + socklen_t addrSz = 0; + ctx->hostName = WSTRDUP(name, NULL, 0); ctx->hostPort = port; @@ -461,8 +464,6 @@ static int wolfSSH_FwdDefaultActions(WS_FwdCbAction action, void* vCtx, } if (ret == 0) { - struct sockaddr_in addr; - socklen_t addrSz = 0; WMEMSET(&addr, 0, sizeof addr); if (WSTRCMP(name, "") == 0 || @@ -477,9 +478,11 @@ static int wolfSSH_FwdDefaultActions(WS_FwdCbAction action, void* vCtx, } else { printf("Not using IPv6 yet.\n"); - WEXIT(EXIT_FAILURE); + ret = WS_FWD_SETUP_E; } + } + if (ret == 0) { ret = bind(ctx->listenFd, (const struct sockaddr*)&addr, addrSz); } @@ -1269,13 +1272,11 @@ static THREAD_RETURN WOLFSSH_THREAD server_worker(void* vArgs) ret = 0; break; - case WS_SFTP_COMPLETE: #ifdef WOLFSSH_SFTP + case WS_SFTP_COMPLETE: ret = sftp_worker(threadCtx); - #else - err_sys("SFTP not compiled in. Please use --enable-sftp"); - #endif break; + #endif case WS_SUCCESS: ret = ssh_worker(threadCtx); @@ -2091,15 +2092,18 @@ static void ShowUsage(void) } -static void SignalTcpReady(func_args* serverArgs, word16 port) +static INLINE void SignalTcpReady(func_args* serverArgs, word16 port) { -#if defined(_POSIX_THREADS) && defined(NO_MAIN_DRIVER) && !defined(__MINGW32__) +#if defined(_POSIX_THREADS) && defined(NO_MAIN_DRIVER) && \ + !defined(__MINGW32__) && !defined(SINGLE_THREADED) tcp_ready* ready = serverArgs->signal; - pthread_mutex_lock(&ready->mutex); - ready->ready = 1; - ready->port = port; - pthread_cond_signal(&ready->cond); - pthread_mutex_unlock(&ready->mutex); + if (ready != NULL) { + pthread_mutex_lock(&ready->mutex); + ready->ready = 1; + ready->port = port; + pthread_cond_signal(&ready->cond); + pthread_mutex_unlock(&ready->mutex); + } #else (void)serverArgs; (void)port; @@ -2135,95 +2139,106 @@ THREAD_RETURN WOLFSSH_THREAD echoserver_test(void* args) int argc = serverArgs->argc; char** argv = serverArgs->argv; - serverArgs->return_code = 0; + serverArgs->return_code = EXIT_SUCCESS; if (argc > 0) { - while ((ch = mygetopt(argc, argv, "?1a:d:efEp:R:Ni:j:I:J:K:P:")) != -1) { - switch (ch) { - case '?' : - ShowUsage(); - WEXIT(EXIT_SUCCESS); + const char* optlist = "?1a:d:efEp:R:Ni:j:I:J:K:P:"; + myoptind = 0; + while ((ch = mygetopt(argc, argv, optlist)) != -1) { + switch (ch) { + case '?' : + ShowUsage(); + serverArgs->return_code = MY_EX_USAGE; + return 0; - case '1': - multipleConnections = 0; - break; + case '1': + multipleConnections = 0; + break; - case 'a': - #ifdef WOLFSSH_CERTS - caCert = myoptarg; - #endif - break; - case 'e' : - userEcc = 1; - break; - - case 'E': - peerEcc = 1; - break; - - case 'f': - #ifdef WOLFSSH_SHELL - echo = 1; - #endif - break; - - case 'p': - if (myoptarg == NULL) { - err_sys("NULL port value"); - } - else { - port = (word16)atoi(myoptarg); - #if !defined(NO_MAIN_DRIVER) || defined(USE_WINDOWS_API) - if (port == 0) - err_sys("port number cannot be 0"); + case 'a': + #ifdef WOLFSSH_CERTS + caCert = myoptarg; #endif - } - break; + break; + case 'e' : + userEcc = 1; + break; - case 'R': - readyFile = myoptarg; - break; + case 'E': + peerEcc = 1; + break; - case 'N': - nonBlock = 1; - break; + case 'f': + #ifdef WOLFSSH_SHELL + echo = 1; + #endif + break; - case 'd': - defaultSftpPath = myoptarg; - break; + case 'p': + if (myoptarg == NULL) { + err_sys("NULL port value"); + serverArgs->return_code = EXIT_FAILURE; + return 0; + } + else { + port = (word16)atoi(myoptarg); + #if !defined(NO_MAIN_DRIVER) || defined(USE_WINDOWS_API) + if (port == 0) { + err_sys("port number cannot be 0"); + serverArgs->return_code = EXIT_FAILURE; + return 0; + } + #endif + } + break; - case 'j': - userPubKey = myoptarg; - break; + case 'R': + readyFile = myoptarg; + break; - case 'I': - sshPubKeyList = StrListAdd(sshPubKeyList, myoptarg); - break; + case 'N': + nonBlock = 1; + break; - case 'J': - pemPubKeyList = StrListAdd(pemPubKeyList, myoptarg); - break; + case 'd': + defaultSftpPath = myoptarg; + break; - case 'K': - derPubKeyList = StrListAdd(derPubKeyList, myoptarg); - break; + case 'j': + userPubKey = myoptarg; + break; - case 'P': - passwdList = StrListAdd(passwdList, myoptarg); - break; + case 'I': + sshPubKeyList = StrListAdd(sshPubKeyList, myoptarg); + break; - default: - ShowUsage(); - WEXIT(MY_EX_USAGE); + case 'J': + pemPubKeyList = StrListAdd(pemPubKeyList, myoptarg); + break; + + case 'K': + derPubKeyList = StrListAdd(derPubKeyList, myoptarg); + break; + + case 'P': + passwdList = StrListAdd(passwdList, myoptarg); + break; + + default: + ShowUsage(); + serverArgs->return_code = MY_EX_USAGE; + return 0; + } } } - } myoptind = 0; /* reset for test cases */ wc_InitMutex(&doneLock); #ifdef WOLFSSH_TEST_BLOCK if (!nonBlock) { err_sys("Use -N when testing forced non blocking"); + serverArgs->return_code = EXIT_FAILURE; + return 0; } #endif @@ -2241,13 +2256,15 @@ THREAD_RETURN WOLFSSH_THREAD echoserver_test(void* args) if (wolfSSH_Init() != WS_SUCCESS) { fprintf(stderr, "Couldn't initialize wolfSSH.\n"); - WEXIT(EXIT_FAILURE); + serverArgs->return_code = EXIT_FAILURE; + return 0; } ctx = wolfSSH_CTX_new(WOLFSSH_ENDPOINT_SERVER, NULL); if (ctx == NULL) { fprintf(stderr, "Couldn't allocate SSH CTX data.\n"); - WEXIT(EXIT_FAILURE); + serverArgs->return_code = EXIT_FAILURE; + return 0; } WMEMSET(&pwMapList, 0, sizeof(pwMapList)); @@ -2297,7 +2314,8 @@ THREAD_RETURN WOLFSSH_THREAD echoserver_test(void* args) keyLoadBuf = (byte*)WMALLOC(EXAMPLE_KEYLOAD_BUFFER_SZ, NULL, 0); if (keyLoadBuf == NULL) { - WEXIT(EXIT_FAILURE); + serverArgs->return_code = EXIT_FAILURE; + return 0; } #else keyLoadBuf = buf; @@ -2307,12 +2325,14 @@ THREAD_RETURN WOLFSSH_THREAD echoserver_test(void* args) bufSz = load_key(peerEcc, keyLoadBuf, bufSz); if (bufSz == 0) { fprintf(stderr, "Couldn't load first key file.\n"); - WEXIT(EXIT_FAILURE); + serverArgs->return_code = EXIT_FAILURE; + return 0; } if (wolfSSH_CTX_UsePrivateKey_buffer(ctx, keyLoadBuf, bufSz, WOLFSSH_FORMAT_ASN1) < 0) { fprintf(stderr, "Couldn't use first key buffer.\n"); - WEXIT(EXIT_FAILURE); + serverArgs->return_code = EXIT_FAILURE; + return 0; } peerEcc = !peerEcc; @@ -2321,12 +2341,14 @@ THREAD_RETURN WOLFSSH_THREAD echoserver_test(void* args) bufSz = load_key(peerEcc, keyLoadBuf, bufSz); if (bufSz == 0) { fprintf(stderr, "Couldn't load second key file.\n"); - WEXIT(EXIT_FAILURE); + serverArgs->return_code = EXIT_FAILURE; + return 0; } if (wolfSSH_CTX_UsePrivateKey_buffer(ctx, keyLoadBuf, bufSz, WOLFSSH_FORMAT_ASN1) < 0) { fprintf(stderr, "Couldn't use second key buffer.\n"); - WEXIT(EXIT_FAILURE); + serverArgs->return_code = EXIT_FAILURE; + return 0; } if (userPubKey) { @@ -2339,13 +2361,15 @@ THREAD_RETURN WOLFSSH_THREAD echoserver_test(void* args) /* create temp buffer and load in file */ if (userBufSz == 0) { fprintf(stderr, "Couldn't find size of file %s.\n", userPubKey); - WEXIT(EXIT_FAILURE); + serverArgs->return_code = EXIT_FAILURE; + return 0; } userBuf = (byte*)WMALLOC(userBufSz, NULL, 0); if (userBuf == NULL) { fprintf(stderr, "WMALLOC failed\n"); - WEXIT(EXIT_FAILURE); + serverArgs->return_code = EXIT_FAILURE; + return 0; } load_file(userPubKey, userBuf, &userBufSz); LoadPublicKeyBuffer(userBuf, userBufSz, &pwMapList); @@ -2363,20 +2387,23 @@ THREAD_RETURN WOLFSSH_THREAD echoserver_test(void* args) if (certBufSz == 0) { fprintf(stderr, "Couldn't find size of file %s.\n", caCert); - WEXIT(EXIT_FAILURE); + serverArgs->return_code = EXIT_FAILURE; + return 0; } certBuf = (byte*)WMALLOC(certBufSz, NULL, 0); if (certBuf == NULL) { fprintf(stderr, "WMALLOC failed\n"); - WEXIT(EXIT_FAILURE); + serverArgs->return_code = EXIT_FAILURE; + return 0; } load_file(caCert, certBuf, &certBufSz); ret = wolfSSH_CTX_AddRootCert_buffer(ctx, certBuf, certBufSz, WOLFSSH_FORMAT_PEM); if (ret != 0) { fprintf(stderr, "Couldn't add root cert\n"); - WEXIT(EXIT_FAILURE); + serverArgs->return_code = EXIT_FAILURE; + return 0; } WFREE(certBuf, NULL, 0); } @@ -2423,7 +2450,8 @@ THREAD_RETURN WOLFSSH_THREAD echoserver_test(void* args) /* wait for network and storage device */ if (NETBOOT_Wait_For_Network_Up(NU_SUSPEND) != NU_SUCCESS) { fprintf(stderr, "Couldn't find network.\r\n"); - WEXIT(EXIT_FAILURE); + serverArgs->return_code = EXIT_FAILURE; + return 0; } for(i = 0; i < 15 && ret != NU_SUCCESS; i++) @@ -2435,7 +2463,8 @@ THREAD_RETURN WOLFSSH_THREAD echoserver_test(void* args) if (ret != NU_SUCCESS) { fprintf(stderr, "Couldn't find storage device.\r\n"); - WEXIT(EXIT_FAILURE); + serverArgs->return_code = EXIT_FAILURE; + return 0; } } #endif @@ -2444,9 +2473,11 @@ THREAD_RETURN WOLFSSH_THREAD echoserver_test(void* args) if (readyFile != NULL) { #ifdef NO_FILESYSTEM fprintf(stderr, "cannot create readyFile with no file system.\r\n"); - WEXIT(EXIT_FAILURE); - #endif + serverArgs->return_code = EXIT_FAILURE; + return 0; + #else port = 0; + #endif } tcp_listen(&listenFd, &port, 1); /* write out port number listing to, to user set ready file */ @@ -2477,7 +2508,8 @@ THREAD_RETURN WOLFSSH_THREAD echoserver_test(void* args) NULL, 0); if (threadCtx == NULL) { fprintf(stderr, "Couldn't allocate thread context data.\n"); - WEXIT(EXIT_FAILURE); + serverArgs->return_code = EXIT_FAILURE; + return 0; } WMEMSET(threadCtx, 0, sizeof *threadCtx); @@ -2485,7 +2517,8 @@ THREAD_RETURN WOLFSSH_THREAD echoserver_test(void* args) if (ssh == NULL) { WFREE(threadCtx, NULL, 0); fprintf(stderr, "Couldn't allocate SSH data.\n"); - WEXIT(EXIT_FAILURE); + serverArgs->return_code = EXIT_FAILURE; + return 0; } wolfSSH_SetUserAuthCtx(ssh, &pwMapList); /* Use the session object for its own highwater callback ctx */ @@ -2497,7 +2530,8 @@ THREAD_RETURN WOLFSSH_THREAD echoserver_test(void* args) #ifdef WOLFSSH_SFTP if (SetDefaultSftpPath(ssh, defaultSftpPath) != 0) { fprintf(stderr, "Couldn't store default sftp path.\n"); - WEXIT(EXIT_FAILURE); + serverArgs->return_code = EXIT_FAILURE; + return 0; } #endif @@ -2514,7 +2548,8 @@ THREAD_RETURN WOLFSSH_THREAD echoserver_test(void* args) * 0.0.0.0 if ip adder any */ if (NU_Get_Sock_Name(listenFd, &sock, &addrLength) != NU_SUCCESS) { fprintf(stderr, "Couldn't find network.\r\n"); - WEXIT(EXIT_FAILURE); + serverArgs->return_code = EXIT_FAILURE; + return 0; } WMEMCPY(ipaddr, &sock.ip_num, MAX_ADDRESS_SIZE); @@ -2531,8 +2566,11 @@ THREAD_RETURN WOLFSSH_THREAD echoserver_test(void* args) clientFd = accept(listenFd, (struct sockaddr*)&clientAddr, &clientAddrSz); #endif - if (clientFd == -1) + if (clientFd == -1) { err_sys("tcp accept failed"); + serverArgs->return_code = EXIT_FAILURE; + return 0; + } if (nonBlock) tcp_set_nonblocking(&clientFd); @@ -2560,12 +2598,16 @@ THREAD_RETURN WOLFSSH_THREAD echoserver_test(void* args) } while (multipleConnections && !quit); + if (listenFd != 0) { + WCLOSESOCKET(listenFd); + } wc_FreeMutex(&doneLock); PwMapListDelete(&pwMapList); wolfSSH_CTX_free(ctx); if (wolfSSH_Cleanup() != WS_SUCCESS) { fprintf(stderr, "Couldn't clean up wolfSSH.\n"); - WEXIT(EXIT_FAILURE); + serverArgs->return_code = EXIT_FAILURE; + return 0; } #if !defined(WOLFSSH_NO_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS) wc_ecc_fp_free(); /* free per thread cache */ @@ -2578,40 +2620,45 @@ THREAD_RETURN WOLFSSH_THREAD echoserver_test(void* args) #endif /* NO_WOLFSSH_SERVER */ -#ifndef NO_MAIN_DRIVER void wolfSSL_Debugging_ON(void); +int wolfSSH_Echoserver(int argc, char** argv) +{ + func_args args; + + WMEMSET(&args, 0, sizeof(args)); + args.argc = argc; + args.argv = argv; + + WSTARTTCP(); + + #ifdef DEBUG_WOLFSSH + wolfSSL_Debugging_ON(); + wolfSSH_Debugging_ON(); + #endif + +#if !defined(WOLFSSL_NUCLEUS) && !defined(INTEGRITY) && !defined(__INTEGRITY) + ChangeToWolfSshRoot(); +#endif +#ifndef NO_WOLFSSH_SERVER + echoserver_test(&args); +#else + printf("wolfSSH compiled without server support\n"); +#endif + + wolfSSH_Cleanup(); + + return args.return_code; +} + + +#ifndef NO_MAIN_DRIVER + int main(int argc, char** argv) { - func_args args; - - args.argc = argc; - args.argv = argv; - args.return_code = 0; - args.user_auth = NULL; - - WSTARTTCP(); - - #ifdef DEBUG_WOLFSSH - wolfSSL_Debugging_ON(); - wolfSSH_Debugging_ON(); - #endif - -#ifndef WOLFSSL_NUCLEUS - ChangeToWolfSshRoot(); -#endif -#ifndef NO_WOLFSSH_SERVER - echoserver_test(&args); -#else - printf("wolfSSH compiled without server support\n"); -#endif - - wolfSSH_Cleanup(); - - return args.return_code; + return wolfSSH_Echoserver(argc, argv); } - int myoptind = 0; char* myoptarg = NULL; diff --git a/examples/echoserver/echoserver.h b/examples/echoserver/echoserver.h index 8bcdd2da..a3afe529 100644 --- a/examples/echoserver/echoserver.h +++ b/examples/echoserver/echoserver.h @@ -24,6 +24,7 @@ THREAD_RETURN WOLFSSH_THREAD echoserver_test(void* args); +int wolfSSH_Echoserver(int argc, char** argv); #endif /* _WOLFSSH_EXAMPLES_ECHOSERVER_H_ */ diff --git a/tests/api.c b/tests/api.c index 0517f91a..f0f9beaa 100644 --- a/tests/api.c +++ b/tests/api.c @@ -51,11 +51,18 @@ int myoptind = 0; char* myoptarg = NULL; +#ifndef WOLFSSH_NO_ABORT + #define WABORT() abort() +#else + #define WABORT() +#endif + #define Fail(description, result) do { \ printf("\nERROR - %s line %d failed with:", __FILE__, __LINE__); \ - printf("\n expected: "); printf description; \ - printf("\n result: "); printf result; printf("\n\n"); \ - abort(); \ + fputs("\n expected: ", stdout); printf description; \ + fputs("\n result: ", stdout); printf result; fputs("\n\n", stdout); \ + fflush(stdout); \ + WABORT(); \ } while(0) #define Assert(test, description, result) if (!(test)) Fail(description, result) @@ -65,15 +72,14 @@ char* myoptarg = NULL; #define AssertNotNull(x) Assert( (x), ("%s is not null", #x), (#x " => NULL")) #define AssertNull(x) do { \ - void* _x = (void *) (x); \ + PEDANTIC_EXTENSION void* _x = (void*)(x); \ \ Assert(!_x, ("%s is null", #x), (#x " => %p", _x)); \ } while(0) #define AssertInt(x, y, op, er) do { \ - int _x = x; \ - int _y = y; \ - \ + int _x = (int)(x); \ + int _y = (int)(y); \ Assert(_x op _y, ("%s " #op " %s", #x, #y), ("%d " #er " %d", _x, _y)); \ } while(0) @@ -85,10 +91,9 @@ char* myoptarg = NULL; #define AssertIntLE(x, y) AssertInt(x, y, <=, >) #define AssertStr(x, y, op, er) do { \ - const char* _x = x; \ - const char* _y = y; \ - int _z = strcmp(_x, _y); \ - \ + const char* _x = (const char*)(x); \ + const char* _y = (const char*)(y); \ + int _z = (_x && _y) ? strcmp(_x, _y) : -1; \ Assert(_z op 0, ("%s " #op " %s", #x, #y), \ ("\"%s\" " #er " \"%s\"", _x, _y));\ } while(0) @@ -100,98 +105,26 @@ char* myoptarg = NULL; #define AssertStrGE(x, y) AssertStr(x, y, >=, <) #define AssertStrLE(x, y) AssertStr(x, y, <=, >) +#define AssertPtr(x, y, op, er) do { \ + PRAGMA_GCC_DIAG_PUSH; \ + /* remarkably, without this inhibition, */ \ + /* the _Pragma()s make the declarations warn. */ \ + PRAGMA_GCC("GCC diagnostic ignored \"-Wdeclaration-after-statement\""); \ + /* inhibit "ISO C forbids conversion of function pointer */ \ + /* to object pointer type [-Werror=pedantic]" */ \ + PRAGMA_GCC("GCC diagnostic ignored \"-Wpedantic\""); \ + void* _x = (void*)(x); \ + void* _y = (void*)(y); \ + Assert(_x op _y, ("%s " #op " %s", #x, #y), ("%p " #er " %p", _x, _y)); \ + PRAGMA_GCC_DIAG_POP; \ +} while(0) -#if defined(WOLFSSH_SFTP) && !defined(NO_WOLFSSH_CLIENT) - -#include "examples/echoserver/echoserver.h" - -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; -} - -#ifndef NO_WOLFSSH_CLIENT -/* 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 /* NO_WOLFSSH_CLIENT */ -#endif /* WOLFSSH_SFTP */ +#define AssertPtrEq(x, y) AssertPtr(x, y, ==, !=) +#define AssertPtrNE(x, y) AssertPtr(x, y, !=, ==) +#define AssertPtrGT(x, y) AssertPtr(x, y, >, <=) +#define AssertPtrLT(x, y) AssertPtr(x, y, <, >=) +#define AssertPtrGE(x, y) AssertPtr(x, y, >=, <) +#define AssertPtrLE(x, y) AssertPtr(x, y, <=, >) enum WS_TestEndpointTypes { @@ -658,6 +591,7 @@ static void test_wolfSSH_CertMan(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, @@ -680,12 +614,10 @@ static int my_ScpRecv(WOLFSSH* ssh, int state, const char* basePath, 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 */ @@ -708,12 +640,107 @@ static void test_wolfSSH_SCP_CB(void) wolfSSH_free(ssh); wolfSSH_CTX_free(ctx); -#endif /* WOLFSSH_NO_CLIENT */ } +#else /* WOLFSSH_SCP */ +static void test_wolfSSH_SCP_CB(void) { ; } +#endif /* WOLFSSH_SCP */ + + +#if defined(WOLFSSH_SFTP) && !defined(NO_WOLFSSH_CLIENT) && \ + !defined(SINGLE_THREADED) + +#include "examples/echoserver/echoserver.h" + +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; + } +} + + static void test_wolfSSH_SFTP_SendReadPacket(void) { -#if defined(WOLFSSH_SFTP) && !defined(NO_WOLFSSH_CLIENT) func_args ser; tcp_ready ready; int argsCount; @@ -818,9 +845,11 @@ static void test_wolfSSH_SFTP_SendReadPacket(void) wolfSSH_free(ssh); wolfSSH_CTX_free(ctx); ThreadJoin(serThread); -#endif } +#else /* WOLFSSH_SFTP && !NO_WOLFSSH_CLIENT && !SINGLE_THREADED */ +static void test_wolfSSH_SFTP_SendReadPacket(void) { ; } +#endif /* WOLFSSH_SFTP && !NO_WOLFSSH_CLIENT && !SINGLE_THREADED */ #ifdef USE_WINDOWS_API diff --git a/tests/testsuite.c b/tests/testsuite.c index 762cb995..db01fd43 100644 --- a/tests/testsuite.c +++ b/tests/testsuite.c @@ -61,7 +61,8 @@ char* myoptarg = NULL; #endif /* !NO_TESTSUITE_MAIN_DRIVER */ -#if !defined(NO_WOLFSSH_SERVER) && !defined(NO_WOLFSSH_CLIENT) +#if !defined(NO_WOLFSSH_SERVER) && !defined(NO_WOLFSSH_CLIENT) && \ + !defined(SINGLE_THREADED) static int tsClientUserAuth(byte authType, WS_UserAuthData* authData, void* ctx) { @@ -166,15 +167,15 @@ int wolfSSH_TestsuiteTest(int argc, char** argv) return EXIT_SUCCESS; } -#else /* !NO_WOLFSSH_SERVER && !NO_WOLFSSH_CLIENT */ +#else /* !NO_WOLFSSH_SERVER && !NO_WOLFSSH_CLIENT && !SINGLE_THREADED */ -int TestsuiteTest(int argc, char** argv) +int wolfSSH_TestsuiteTest(int argc, char** argv) { (void)argc; (void)argv; return EXIT_SUCCESS; } -#endif /* !NO_WOLFSSH_SERVER && !NO_WOLFSSH_CLIENT */ +#endif /* !NO_WOLFSSH_SERVER && !NO_WOLFSSH_CLIENT && !SINGLE_THREADED */ diff --git a/wolfssh/port.h b/wolfssh/port.h index 55f99eff..56ed2b73 100644 --- a/wolfssh/port.h +++ b/wolfssh/port.h @@ -75,7 +75,11 @@ extern "C" { #endif -#define WEXIT(n) exit((n)) +#if defined(INTEGRITY) || defined(__INTEGRITY) + #define WEXIT(n) return (0) +#else + #define WEXIT(n) exit((n)) +#endif #ifndef WOLFSSH_HANDLE diff --git a/wolfssh/test.h b/wolfssh/test.h index d02141ba..cfbf94d1 100644 --- a/wolfssh/test.h +++ b/wolfssh/test.h @@ -22,7 +22,7 @@ * This file contains some utility code shared between the wolfSSH test * tools and examples. This is divided into a few sets of functions that * may be enabled with flags included before including this file: - * + * * WOLFSSH_TEST_CLIENT: Client utility functions * WOLFSSH_TEST_SERVER: Server utility functions * WOLFSSH_TEST_LOCKING: Mutex wrappers @@ -217,6 +217,11 @@ static INLINE void err_sys(const char* msg) printf("wolfSSH error: %s\n", msg); return; } +#elif defined(INTEGRITY) || defined(__INTEGRITY) +static INLINE void err_sys(const char* msg) +{ + printf("wolfSSH error: %s\n", msg); +} #else static INLINE WS_NORETURN void err_sys(const char* msg) { @@ -751,8 +756,8 @@ static INLINE void InitTcpReady(tcp_ready* ready) ready->ready = 0; ready->port = 0; ready->srfName = NULL; -#ifdef SINGLE_THREADED -#elif defined(_POSIX_THREADS) && !defined(__MINGW32__) +#if defined(_POSIX_THREADS) && defined(NO_MAIN_DRIVER) && \ + !defined(__MINGW32__) && !defined(SINGLE_THREADED) pthread_mutex_init(&ready->mutex, 0); pthread_cond_init(&ready->cond, 0); #endif @@ -761,9 +766,8 @@ static INLINE void InitTcpReady(tcp_ready* ready) static INLINE void FreeTcpReady(tcp_ready* ready) { -#ifdef SINGLE_THREADED - (void)ready; -#elif defined(_POSIX_THREADS) && !defined(__MINGW32__) +#if defined(_POSIX_THREADS) && defined(NO_MAIN_DRIVER) && \ + !defined(__MINGW32__) && !defined(SINGLE_THREADED) pthread_mutex_destroy(&ready->mutex); pthread_cond_destroy(&ready->cond); #else @@ -774,7 +778,8 @@ static INLINE void FreeTcpReady(tcp_ready* ready) static INLINE void WaitTcpReady(func_args* args) { -#if defined(_POSIX_THREADS) && !defined(__MINGW32__) +#if defined(_POSIX_THREADS) && defined(NO_MAIN_DRIVER) && \ + !defined(__MINGW32__) && !defined(SINGLE_THREADED) pthread_mutex_lock(&args->signal->mutex); if (!args->signal->ready) @@ -853,20 +858,25 @@ static INLINE void ThreadStart(THREAD_FUNC fun, void* args, THREAD_TYPE* thread) #else pthread_create(thread, 0, fun, args); #endif - return; #elif defined(WOLFSSL_TIRTOS) - /* Initialize the defaults and set the parameters. */ - Task_Params taskParams; - Task_Params_init(&taskParams); - taskParams.arg0 = (UArg)args; - taskParams.stackSize = 65535; - *thread = Task_create((Task_FuncPtr)fun, &taskParams, NULL); - if (*thread == NULL) { - printf("Failed to create new Task\n"); + { + /* Initialize the defaults and set the parameters. */ + Task_Params taskParams; + Task_Params_init(&taskParams); + taskParams.arg0 = (UArg)args; + taskParams.stackSize = 65535; + *thread = Task_create((Task_FuncPtr)fun, &taskParams, NULL); + if (*thread == NULL) { + printf("Failed to create new Task\n"); + } + Task_yield(); } - Task_yield(); -#else +#elif defined(USE_WINDOWS_API) *thread = (THREAD_TYPE)_beginthreadex(0, 0, fun, args, 0, 0); +#else + (void)fun; + (void)args; + (void)thread; #endif } @@ -885,12 +895,16 @@ static INLINE void ThreadJoin(THREAD_TYPE thread) } Task_yield(); } +#elif defined(USE_WINDOWS_API) + { + int res = WaitForSingleObject((HANDLE)thread, INFINITE); + assert(res == WAIT_OBJECT_0); + res = CloseHandle((HANDLE)thread); + assert(res); + (void)res; /* Suppress un-used variable warning */ + } #else - int res = WaitForSingleObject((HANDLE)thread, INFINITE); - assert(res == WAIT_OBJECT_0); - res = CloseHandle((HANDLE)thread); - assert(res); - (void)res; /* Suppress un-used variable warning */ + (void)thread; #endif } @@ -911,10 +925,14 @@ static INLINE void ThreadDetach(THREAD_TYPE thread) Task_yield(); } #endif +#elif defined(USE_WINDOWS_API) + { + int res = CloseHandle((HANDLE)thread); + assert(res); + (void)res; /* Suppress un-used variable warning */ + } #else - int res = CloseHandle((HANDLE)thread); - assert(res); - (void)res; /* Suppress un-used variable warning */ + (void)thread; #endif }