diff --git a/.github/workflows/compile-shared-libraries.yml b/.github/workflows/compile-shared-libraries.yml index 416799e..6ea2c99 100644 --- a/.github/workflows/compile-shared-libraries.yml +++ b/.github/workflows/compile-shared-libraries.yml @@ -7,6 +7,9 @@ jobs: build: runs-on: ubuntu-latest + outputs: + artifact: shared-libraries + steps: - name: Checkout code uses: actions/checkout@v3 @@ -29,3 +32,58 @@ jobs: with: name: shared-libraries path: dist/* + + test: + needs: build + runs-on: ${{ matrix.os }} + strategy: + fail-fast: false + matrix: + include: + - os: ubuntu-latest + lib_ext: amd64.so + - os: windows-latest + lib_ext: amd64.dll + - os: macos-latest + lib_ext: arm64.dylib + + steps: + - uses: actions/checkout@v3 + + - name: Download shared libraries + uses: actions/download-artifact@v4 + with: + name: shared-libraries + path: libs + + - name: Install MinGW (GCC) on Windows + if: runner.os == 'Windows' + run: choco install mingw --no-progress + + - name: Build test loader (Windows) + if: runner.os == 'Windows' + run: | + mkdir build + gcc -Wall -o build/test_loader.exe sharedlibrary/tests/*.c + + - name: Build test loader (Unix) + if: runner.os != 'Windows' + run: | + mkdir build + gcc -Wall -ldl -o build/test_loader sharedlibrary/tests/*.c + + - name: Run test (Unix) + if: runner.os != 'Windows' + run: | + LIB=$(ls libs/*${{ matrix.lib_ext }} | head -n 1) + echo "Testing library: $LIB" + chmod +x build/test_loader + ./build/test_loader "$LIB" + + - name: Run test (Windows) + if: runner.os == 'Windows' + shell: pwsh + run: | + $lib = Get-ChildItem "libs/*${{ matrix.lib_ext }}" | Select-Object -First 1 + Write-Host "Testing library: $($lib.FullName)" + .\build\test_loader.exe $lib.FullName diff --git a/sharedlibrary/tests/main.c b/sharedlibrary/tests/main.c index 4315215..4df4ee0 100644 --- a/sharedlibrary/tests/main.c +++ b/sharedlibrary/tests/main.c @@ -7,6 +7,9 @@ int test_Databases(); int main(int argc, char *argv[]) { + /* Disable stdout buffering for CI environments without a real terminal */ + setvbuf(stdout, NULL, _IONBF, 0); + if (argc < 2) { fprintf(stderr, "Usage: %s \n", argv[0]); @@ -14,13 +17,20 @@ int main(int argc, char *argv[]) } const char *libPath = argv[1]; - handle = dlopen(libPath, RTLD_LAZY); + handle = load_library(libPath); if (!handle) { - fprintf(stderr, "Failed to load shared library: %s\n", dlerror()); + fprintf(stderr, "Failed to load shared library: %s\n", get_load_error()); return EXIT_FAILURE; } + /* give the loaded library a short time to initialize */ +#ifdef _WIN32 + Sleep(1000); +#else + sleep(1); +#endif + printf("Running tests for library: %s\n", libPath); int results[] = { test_CreateServerInstance(), @@ -41,6 +51,15 @@ int main(int argc, char *argv[]) printf("Tests passed: %d/%d\n", numPassed, numTests); - dlclose(handle); - return EXIT_SUCCESS; + /* Exit explicitly before unloading the library. + Go runtime cleanup during FreeLibrary can set a non-zero exit code on Windows. */ + int exitCode = (numPassed == numTests) ? EXIT_SUCCESS : EXIT_FAILURE; + +#ifdef _WIN32 + ExitProcess(exitCode); +#else + close_library(handle); +#endif + + return exitCode; } diff --git a/sharedlibrary/tests/shared.c b/sharedlibrary/tests/shared.c index 36ac210..f2c479f 100644 --- a/sharedlibrary/tests/shared.c +++ b/sharedlibrary/tests/shared.c @@ -1,13 +1,50 @@ #include "shared.h" -void *handle = NULL; +lib_handle_t handle = NULL; + +char *get_load_error(void) +{ +#ifdef _WIN32 + DWORD error = GetLastError(); + static char buf[256]; + FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, + NULL, error, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), + buf, sizeof(buf), NULL); + return buf; +#else + return dlerror(); +#endif +} + +lib_handle_t load_library(const char *path) +{ +#ifdef _WIN32 + return LoadLibraryA(path); +#else + return dlopen(path, RTLD_LAZY); +#endif +} + +void close_library(lib_handle_t handle) +{ +#ifdef _WIN32 + FreeLibrary(handle); +#else + dlclose(handle); +#endif +} void *load_function(const char *func_name) { +#ifdef _WIN32 + void *func = (void *)GetProcAddress(handle, func_name); +#else void *func = dlsym(handle, func_name); +#endif + if (!func) { - fprintf(stderr, "Failed to load function %s: %s\n", func_name, dlerror()); + fprintf(stderr, "Failed to load function %s: %s\n", func_name, get_load_error()); } return func; } diff --git a/sharedlibrary/tests/shared.h b/sharedlibrary/tests/shared.h index 76b7508..f88bdac 100644 --- a/sharedlibrary/tests/shared.h +++ b/sharedlibrary/tests/shared.h @@ -3,13 +3,24 @@ #include #include -#include #include #include -extern void *handle; +#ifdef _WIN32 + #include + #include + typedef HMODULE lib_handle_t; +#else + #include + typedef void* lib_handle_t; +#endif + +extern lib_handle_t handle; void *load_function(const char *func_name); char *compact_json(const char *json); +char *get_load_error(void); +lib_handle_t load_library(const char *path); +void close_library(lib_handle_t handle); #endif diff --git a/sharedlibrary/tests/test_databases.c b/sharedlibrary/tests/test_databases.c index 07ed868..f6467de 100644 --- a/sharedlibrary/tests/test_databases.c +++ b/sharedlibrary/tests/test_databases.c @@ -2,6 +2,16 @@ int test_Databases() { + /* Load FreeMemory function - must use this to free memory allocated by the DLL + because the DLL may use a different C runtime heap than the test loader */ + typedef void (*FreeMemoryFn)(char *); + FreeMemoryFn FreeMemory = (FreeMemoryFn)load_function("FreeMemory"); + if (!FreeMemory) + { + fprintf(stderr, "Failed to find FreeMemory function\n"); + return 0; + } + typedef int (*CreateDatabaseFn)(char *, char *); CreateDatabaseFn CreateDatabase = (CreateDatabaseFn)load_function("CreateDatabase"); if (!CreateDatabase) @@ -36,6 +46,7 @@ int test_Databases() if (database) { printf("GetDatabase: SUCCESS (database = %s)\n", database); + FreeMemory(database); } else { diff --git a/sharedlibrary/tests/test_instance_state.c b/sharedlibrary/tests/test_instance_state.c index 9506142..66d2e04 100644 --- a/sharedlibrary/tests/test_instance_state.c +++ b/sharedlibrary/tests/test_instance_state.c @@ -2,6 +2,16 @@ int test_ServerInstanceStateMethods() { + /* Load FreeMemory function - must use this to free memory allocated by the DLL + because the DLL may use a different C runtime heap than the test loader */ + typedef void (*FreeMemoryFn)(char *); + FreeMemoryFn FreeMemory = (FreeMemoryFn)load_function("FreeMemory"); + if (!FreeMemory) + { + fprintf(stderr, "Failed to find FreeMemory function\n"); + return 0; + } + typedef int (*LoadServerInstanceStateFn)(char *, char *); LoadServerInstanceStateFn LoadServerInstanceState = (LoadServerInstanceStateFn)load_function("LoadServerInstanceState"); if (!LoadServerInstanceState) @@ -46,7 +56,7 @@ int test_ServerInstanceStateMethods() char *compact_state = compact_json(state); if (!compact_state) { - free(state); + FreeMemory(state); return 0; } @@ -59,10 +69,12 @@ int test_ServerInstanceStateMethods() printf("GetServerInstanceState: State does not match expected value.\n"); printf("Expected: %s\n", expected_state); printf("Actual: %s\n", compact_state); + FreeMemory(state); + free(compact_state); return 0; } - free(state); + FreeMemory(state); free(compact_state); return 1; }