diff --git a/examples/client/client.c b/examples/client/client.c index dc4a80f0a..796e19e6b 100644 --- a/examples/client/client.c +++ b/examples/client/client.c @@ -1261,12 +1261,7 @@ THREAD_RETURN WOLFSSL_THREAD client_test(void* args) #if defined(DEBUG_WOLFSSL) && !defined(WOLFSSL_MDK_SHELL) && !defined(STACK_TRAP) wolfSSL_Debugging_ON(); #endif - if (CurrentDir("_build")) - ChangeDirBack(1); - else if (CurrentDir("client")) - ChangeDirBack(2); - else if (CurrentDir("Debug") || CurrentDir("Release")) - ChangeDirBack(3); + ChangeToWolfRoot(); #ifdef HAVE_STACK_SIZE StackSizeCheck(&args, client_test); diff --git a/examples/echoclient/echoclient.c b/examples/echoclient/echoclient.c index 8cf05c26c..6f06dd82c 100644 --- a/examples/echoclient/echoclient.c +++ b/examples/echoclient/echoclient.c @@ -261,10 +261,7 @@ void echoclient_test(void* args) CyaSSL_Debugging_ON(); #endif #ifndef CYASSL_TIRTOS - if (CurrentDir("echoclient")) - ChangeDirBack(2); - else if (CurrentDir("Debug") || CurrentDir("Release")) - ChangeDirBack(3); + ChangeToWolfRoot(); #endif echoclient_test(&args); diff --git a/examples/echoserver/echoserver.c b/examples/echoserver/echoserver.c index e510e1387..a01377a7f 100644 --- a/examples/echoserver/echoserver.c +++ b/examples/echoserver/echoserver.c @@ -393,10 +393,7 @@ THREAD_RETURN CYASSL_THREAD echoserver_test(void* args) #if defined(DEBUG_CYASSL) && !defined(CYASSL_MDK_SHELL) CyaSSL_Debugging_ON(); #endif - if (CurrentDir("echoserver")) - ChangeDirBack(2); - else if (CurrentDir("Debug") || CurrentDir("Release")) - ChangeDirBack(3); + ChangeToWolfRoot(); echoserver_test(&args); CyaSSL_Cleanup(); diff --git a/examples/server/server.c b/examples/server/server.c index 455d9b2fa..ed263e67f 100644 --- a/examples/server/server.c +++ b/examples/server/server.c @@ -906,12 +906,7 @@ THREAD_RETURN CYASSL_THREAD server_test(void* args) #if defined(DEBUG_CYASSL) && !defined(WOLFSSL_MDK_SHELL) CyaSSL_Debugging_ON(); #endif - if (CurrentDir("_build")) - ChangeDirBack(1); - else if (CurrentDir("server")) - ChangeDirBack(2); - else if (CurrentDir("Debug") || CurrentDir("Release")) - ChangeDirBack(3); + ChangeToWolfRoot(); #ifdef HAVE_STACK_SIZE StackSizeCheck(&args, server_test); diff --git a/tests/unit.c b/tests/unit.c index a05ae3ccd..e25c6776e 100644 --- a/tests/unit.c +++ b/tests/unit.c @@ -60,10 +60,7 @@ int unit_test(int argc, char** argv) #endif /* HAVE_CAVIUM */ #ifndef WOLFSSL_TIRTOS - if (CurrentDir("tests") || CurrentDir("_build")) - ChangeDirBack(1); - else if (CurrentDir("Debug") || CurrentDir("Release")) - ChangeDirBack(3); + ChangeToWolfRoot(); #endif ApiTest(); diff --git a/testsuite/testsuite.c b/testsuite/testsuite.c index c0304e324..1d228d12e 100644 --- a/testsuite/testsuite.c +++ b/testsuite/testsuite.c @@ -29,24 +29,6 @@ #include #include "wolfcrypt/test/test.h" -/* This function changes the current directory to the wolfssl root */ -static void ChangeDirToRoot(void) -{ - /* Normal Command Line=_build, Visual Studio=testsuite */ - if (CurrentDir("testsuite") || CurrentDir("_build")) { - ChangeDirBack(1); - } - - /* Xcode: To output application to correct location: */ - /* 1. Xcode->Preferences->Locations->Locations */ - /* 2. Derived Data Advanced -> Custom */ - /* 3. Relative to Workspace, Build/Products */ - /* Build/Products/Debug or Build/Products/Release */ - else if (CurrentDir("Debug") || CurrentDir("Release")) { - ChangeDirBack(5); - } -} - #ifndef SINGLE_THREADED @@ -118,7 +100,7 @@ int testsuite_test(int argc, char** argv) #endif #if !defined(WOLFSSL_TIRTOS) - ChangeDirToRoot(); + ChangeToWolfRoot(); #endif #ifdef WOLFSSL_TIRTOS @@ -431,7 +413,7 @@ int main(int argc, char** argv) server_args.argc = argc; server_args.argv = argv; - ChangeDirToRoot(); + ChangeToWolfRoot(); wolfcrypt_test(&server_args); if (server_args.return_code != 0) return server_args.return_code; diff --git a/wolfssl/test.h b/wolfssl/test.h index 8549f0f7a..526b98171 100644 --- a/wolfssl/test.h +++ b/wolfssl/test.h @@ -1160,87 +1160,43 @@ static INLINE int OpenNitroxDevice(int dma_mode,int dev_id) #endif /* HAVE_CAVIUM */ -#ifdef USE_WINDOWS_API +/* Wolf Root Directory Helper */ +/* KEIL-RL File System does not support relative directry */ +#if !defined(WOLFSSL_MDK_ARM) && !defined(WOLFSSL_KEIL_FS) && !defined(WOLFSSL_TIRTOS) + #ifndef MAX_PATH + #define MAX_PATH 256 + #endif -/* do back x number of directories */ -static INLINE void ChangeDirBack(int x) -{ - char path[MAX_PATH]; - XMEMSET(path, 0, MAX_PATH); - XSTRNCAT(path, ".\\", MAX_PATH); - while (x-- > 0) { - XSTRNCAT(path, "..\\", MAX_PATH); + /* Maximum depth to search for WolfSSL root */ + #define MAX_WOLF_ROOT_DEPTH 5 + + static INLINE int ChangeToWolfRoot(void) + { + int depth; + XFILE file; + char path[MAX_PATH]; + XMEMSET(path, 0, MAX_PATH); + + for(depth = 0; depth < MAX_WOLF_ROOT_DEPTH; depth++) { + file = XFOPEN(ntruCert, "rb"); + if (file != XBADFILE) { + XFCLOSE(file); + break; + } + #ifdef USE_WINDOWS_API + XSTRNCAT(path, "..\\", MAX_PATH); + SetCurrentDirectoryA(path); + #else + XSTRNCAT(path, "../", MAX_PATH); + if (chdir(path) < 0) { + printf("chdir to %s failed\n", path); + break; + } + #endif + } + return depth; } - SetCurrentDirectoryA(path); -} - -/* does current dir contain str */ -static INLINE int CurrentDir(const char* str) -{ - char path[MAX_PATH]; - char* baseName; - - GetCurrentDirectoryA(sizeof(path), path); - - baseName = strrchr(path, '\\'); - if (baseName) - baseName++; - else - baseName = path; - - if (strstr(baseName, str)) - return 1; - - return 0; -} - -#elif defined(WOLFSSL_MDK_ARM) || defined(WOLFSSL_KEIL_FS) - /* KEIL-RL File System does not support relative directry */ -#elif defined(WOLFSSL_TIRTOS) -#else - -#ifndef MAX_PATH - #define MAX_PATH 256 -#endif - -/* do back x number of directories */ -static INLINE void ChangeDirBack(int x) -{ - char path[MAX_PATH]; - XMEMSET(path, 0, MAX_PATH); - XSTRNCAT(path, "./", MAX_PATH); - while (x-- > 0) { - XSTRNCAT(path, "../", MAX_PATH); - } - if (chdir(path) < 0) { - printf("chdir to %s failed\n", path); - } -} - -/* does current dir contain str */ -static INLINE int CurrentDir(const char* str) -{ - char path[MAX_PATH]; - char* baseName; - - if (getcwd(path, sizeof(path)) == NULL) { - printf("no current dir?\n"); - return 0; - } - - baseName = strrchr(path, '/'); - if (baseName) - baseName++; - else - baseName = path; - - if (strstr(baseName, str)) - return 1; - - return 0; -} - -#endif /* USE_WINDOWS_API */ +#endif /* !defined(WOLFSSL_MDK_ARM) && !defined(WOLFSSL_KEIL_FS) && !defined(WOLFSSL_TIRTOS) */ #ifdef USE_WOLFSSL_MEMORY