Skip to content

Commit

Permalink
Merge pull request #646 from ejohnstown/sftp-test-fix
Browse files Browse the repository at this point in the history
SFTP Test Maintenance
  • Loading branch information
JacobBarthelmeh authored Jan 3, 2024
2 parents e23970d + ec1248f commit f3752a9
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 27 deletions.
23 changes: 10 additions & 13 deletions examples/echoserver/echoserver.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tests/api.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
5 changes: 3 additions & 2 deletions tests/sftp.c
Original file line number Diff line number Diff line change
Expand Up @@ -208,18 +208,18 @@ 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++] = ".";
args[argsCount++] = "-u";
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
Expand All @@ -240,6 +240,7 @@ int wolfSSH_SftpTest(int flag)

ThreadJoin(serThread);
wolfSSH_Cleanup();
FreeTcpReady(&ready);

return ret;
}
Expand Down
2 changes: 1 addition & 1 deletion tests/testsuite.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
26 changes: 16 additions & 10 deletions wolfssh/test.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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
}

Expand Down

0 comments on commit f3752a9

Please sign in to comment.