From 16708d2bb04b26196497aa208ae5cd5b65aac109 Mon Sep 17 00:00:00 2001 From: John Safranek Date: Tue, 2 Jan 2024 14:01:59 -0800 Subject: [PATCH 1/2] SFTP Test Maintenance 1. In wolfSSH_SftpTest(), move the -p parameter inside the guard with the port number it belongs to. 2. In wolfSSH_SftpTest(), free the conditional variable and mutex. --- tests/sftp.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/sftp.c b/tests/sftp.c index ef19ca0b7..ca7dd2b72 100644 --- a/tests/sftp.c +++ b/tests/sftp.c @@ -216,10 +216,10 @@ int wolfSSH_SftpTest(int flag) args[argsCount++] = "jill"; args[argsCount++] = "-P"; args[argsCount++] = "upthehill"; - args[argsCount++] = "-p"; #ifndef USE_WINDOWS_API /* use port that server has found */ + args[argsCount++] = "-p"; snprintf(portNumber, sizeof(portNumber), "%d", ready.port); args[argsCount++] = portNumber; #endif @@ -240,6 +240,7 @@ int wolfSSH_SftpTest(int flag) ThreadJoin(serThread); wolfSSH_Cleanup(); + FreeTcpReady(&ready); return ret; } From ec1248f14d3bbef49c86de5b299cce860eb4f9ad Mon Sep 17 00:00:00 2001 From: John Safranek Date: Wed, 3 Jan 2024 13:24:10 -0800 Subject: [PATCH 2/2] SFTP Test Maintenance 1. Modified SignalTcpReady() to test.h. Matched its prototype to the other functions for TcpReady. 2. Add a timeout in WaitTcpReady() specifically for Zephyr builds. 3. Misc few cleanups. --- examples/echoserver/echoserver.c | 23 ++++++++++------------- tests/api.c | 2 +- tests/sftp.c | 2 +- tests/testsuite.c | 2 +- wolfssh/test.h | 26 ++++++++++++++++---------- 5 files changed, 29 insertions(+), 26 deletions(-) diff --git a/examples/echoserver/echoserver.c b/examples/echoserver/echoserver.c index 63409c2f6..3d9b542e0 100644 --- a/examples/echoserver/echoserver.c +++ b/examples/echoserver/echoserver.c @@ -2159,21 +2159,18 @@ static void ShowUsage(void) } -static INLINE void SignalTcpReady(func_args* serverArgs, word16 port) +static INLINE void SignalTcpReady(tcp_ready* ready, word16 port) { #if defined(_POSIX_THREADS) && defined(NO_MAIN_DRIVER) && \ !defined(__MINGW32__) && !defined(SINGLE_THREADED) - tcp_ready* ready = serverArgs->signal; - if (ready != NULL) { - pthread_mutex_lock(&ready->mutex); - ready->ready = 1; - ready->port = port; - pthread_cond_signal(&ready->cond); - pthread_mutex_unlock(&ready->mutex); - } + 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; + WOLFSSH_UNUSED(ready); + WOLFSSH_UNUSED(port); #endif } @@ -2543,6 +2540,8 @@ THREAD_RETURN WOLFSSH_THREAD echoserver_test(void* args) #endif } + SignalTcpReady(serverArgs->signal, port); + do { WS_SOCKET_T clientFd = WOLFSSH_SOCKET_INVALID; #ifdef WOLFSSL_NUCLEUS @@ -2600,8 +2599,6 @@ THREAD_RETURN WOLFSSH_THREAD echoserver_test(void* args) } #endif - SignalTcpReady(serverArgs, port); - #ifdef WOLFSSL_NUCLEUS clientFd = NU_Accept(listenFd, &clientAddr, 0); #else diff --git a/tests/api.c b/tests/api.c index 9353c4d2b..c417a1c56 100644 --- a/tests/api.c +++ b/tests/api.c @@ -974,7 +974,7 @@ static void test_wolfSSH_SFTP_SendReadPacket(void) ser.signal = &ready; InitTcpReady(ser.signal); ThreadStart(echoserver_test, (void*)&ser, &serThread); - WaitTcpReady(&ser); + WaitTcpReady(&ready); sftp_client_connect(&ctx, &ssh, ready.port); AssertNotNull(ctx); diff --git a/tests/sftp.c b/tests/sftp.c index ca7dd2b72..36b2cb081 100644 --- a/tests/sftp.c +++ b/tests/sftp.c @@ -208,7 +208,7 @@ int wolfSSH_SftpTest(int flag) ser.signal = &ready; InitTcpReady(ser.signal); ThreadStart(echoserver_test, (void*)&ser, &serThread); - WaitTcpReady(&ser); + WaitTcpReady(&ready); argsCount = 0; args[argsCount++] = "."; diff --git a/tests/testsuite.c b/tests/testsuite.c index 8ee4a7c38..c9441b24d 100644 --- a/tests/testsuite.c +++ b/tests/testsuite.c @@ -145,7 +145,7 @@ int wolfSSH_TestsuiteTest(int argc, char** argv) serverArgs.signal = &ready; serverArgs.user_auth = NULL; ThreadStart(echoserver_test, &serverArgs, &serverThread); - WaitTcpReady(&serverArgs); + WaitTcpReady(&ready); WSTRNCPY(cA[clientArgc++], "client", ARGLEN); WSTRNCPY(cA[clientArgc++], "-u", ARGLEN); diff --git a/wolfssh/test.h b/wolfssh/test.h index 9b767f08c..cf276b102 100644 --- a/wolfssh/test.h +++ b/wolfssh/test.h @@ -850,8 +850,8 @@ static INLINE void InitTcpReady(tcp_ready* ready) ready->srfName = NULL; #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); + pthread_mutex_init(&ready->mutex, NULL); + pthread_cond_init(&ready->cond, NULL); #endif } @@ -863,24 +863,30 @@ static INLINE void FreeTcpReady(tcp_ready* ready) pthread_mutex_destroy(&ready->mutex); pthread_cond_destroy(&ready->cond); #else - (void)ready; + WOLFSSH_UNUSED(ready); #endif } -static INLINE void WaitTcpReady(func_args* args) +static INLINE void WaitTcpReady(tcp_ready* ready) { #if defined(_POSIX_THREADS) && defined(NO_MAIN_DRIVER) && \ !defined(__MINGW32__) && !defined(SINGLE_THREADED) - pthread_mutex_lock(&args->signal->mutex); + pthread_mutex_lock(&ready->mutex); - if (!args->signal->ready) - pthread_cond_wait(&args->signal->cond, &args->signal->mutex); - args->signal->ready = 0; /* reset */ + while (!ready->ready) { + pthread_cond_wait(&ready->cond, &ready->mutex); + } - pthread_mutex_unlock(&args->signal->mutex); + pthread_mutex_unlock(&ready->mutex); +#ifdef WOLFSSH_ZEPHYR + /* It's like the server isn't ready to accept connections it is + * listening for despite this conditional variable. A 300ms wait + * seems to help. This is not ideal. (XXX) */ + k_sleep(Z_TIMEOUT_TICKS(300)); +#endif /* WOLFSSH_ZEPHYR */ #else - (void)args; + WOLFSSH_UNUSED(ready); #endif }