|
| 1 | +/* |
| 2 | + ============================================================================== |
| 3 | +
|
| 4 | + This file is part of the YUP library. |
| 5 | + Copyright (c) 2025 - kunitoki@gmail.com |
| 6 | +
|
| 7 | + YUP is an open source library subject to open-source licensing. |
| 8 | +
|
| 9 | + The code included in this file is provided under the terms of the ISC license |
| 10 | + http://www.isc.org/downloads/software-support-policy/isc-license. Permission |
| 11 | + to use, copy, modify, and/or distribute this software for any purpose with or |
| 12 | + without fee is hereby granted provided that the above copyright notice and |
| 13 | + this permission notice appear in all copies. |
| 14 | +
|
| 15 | + YUP IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER |
| 16 | + EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE |
| 17 | + DISCLAIMED. |
| 18 | +
|
| 19 | + ============================================================================== |
| 20 | +*/ |
| 21 | + |
| 22 | +#include <gtest/gtest.h> |
| 23 | + |
| 24 | +#include <yup_core/yup_core.h> |
| 25 | + |
| 26 | +using namespace yup; |
| 27 | + |
| 28 | +#if ! YUP_WASM |
| 29 | + |
| 30 | +TEST (DynamicLibraryTests, OpenSystemLibrary) |
| 31 | +{ |
| 32 | + DynamicLibrary lib; |
| 33 | + |
| 34 | + // Try to open a system library |
| 35 | +#if YUP_MAC || YUP_IOS |
| 36 | + EXPECT_TRUE (lib.open ("/usr/lib/libSystem.dylib")); |
| 37 | +#elif YUP_LINUX || YUP_ANDROID |
| 38 | + EXPECT_TRUE (lib.open ("libc.so.6") || lib.open ("libc.so")); |
| 39 | +#elif YUP_WINDOWS |
| 40 | + EXPECT_TRUE (lib.open ("kernel32.dll")); |
| 41 | +#endif |
| 42 | +} |
| 43 | + |
| 44 | +TEST (DynamicLibraryTests, OpenNonExistent) |
| 45 | +{ |
| 46 | + DynamicLibrary lib; |
| 47 | + |
| 48 | + // Should fail to open non-existent library |
| 49 | + EXPECT_FALSE (lib.open ("/nonexistent/library.so")); |
| 50 | +} |
| 51 | + |
| 52 | +TEST (DynamicLibraryTests, Close) |
| 53 | +{ |
| 54 | + DynamicLibrary lib; |
| 55 | + |
| 56 | + // Close without opening should not crash |
| 57 | + EXPECT_NO_THROW (lib.close()); |
| 58 | + |
| 59 | +#if YUP_MAC || YUP_IOS |
| 60 | + lib.open ("/usr/lib/libSystem.dylib"); |
| 61 | +#elif YUP_LINUX || YUP_ANDROID |
| 62 | + lib.open ("libc.so.6"); |
| 63 | +#endif |
| 64 | + |
| 65 | + // Close after opening |
| 66 | + EXPECT_NO_THROW (lib.close()); |
| 67 | + |
| 68 | + // Close again should not crash |
| 69 | + EXPECT_NO_THROW (lib.close()); |
| 70 | +} |
| 71 | + |
| 72 | +TEST (DynamicLibraryTests, GetFunction) |
| 73 | +{ |
| 74 | + DynamicLibrary lib; |
| 75 | + |
| 76 | + // Getting function from unopened library should return nullptr |
| 77 | + EXPECT_EQ (lib.getFunction ("some_function"), nullptr); |
| 78 | + |
| 79 | +#if YUP_MAC || YUP_IOS |
| 80 | + if (lib.open ("/usr/lib/libSystem.dylib")) |
| 81 | + { |
| 82 | + // Try to get a known function |
| 83 | + auto func = lib.getFunction ("malloc"); |
| 84 | + EXPECT_NE (func, nullptr); |
| 85 | + |
| 86 | + // Try to get non-existent function |
| 87 | + func = lib.getFunction ("nonexistent_function_12345"); |
| 88 | + EXPECT_EQ (func, nullptr); |
| 89 | + } |
| 90 | +#elif YUP_LINUX || YUP_ANDROID |
| 91 | + if (lib.open ("libc.so.6") || lib.open ("libc.so")) |
| 92 | + { |
| 93 | + // Try to get a known function |
| 94 | + auto func = lib.getFunction ("malloc"); |
| 95 | + EXPECT_NE (func, nullptr); |
| 96 | + |
| 97 | + // Try to get non-existent function |
| 98 | + func = lib.getFunction ("nonexistent_function_12345"); |
| 99 | + EXPECT_EQ (func, nullptr); |
| 100 | + } |
| 101 | +#endif |
| 102 | +} |
| 103 | + |
| 104 | +TEST (DynamicLibraryTests, ReopenAfterClose) |
| 105 | +{ |
| 106 | + DynamicLibrary lib; |
| 107 | + |
| 108 | +#if YUP_MAC || YUP_IOS |
| 109 | + EXPECT_TRUE (lib.open ("/usr/lib/libSystem.dylib")); |
| 110 | + lib.close(); |
| 111 | + EXPECT_TRUE (lib.open ("/usr/lib/libSystem.dylib")); |
| 112 | +#elif YUP_LINUX || YUP_ANDROID |
| 113 | + if (lib.open ("libc.so.6") || lib.open ("libc.so")) |
| 114 | + { |
| 115 | + lib.close(); |
| 116 | + EXPECT_TRUE (lib.open ("libc.so.6") || lib.open ("libc.so")); |
| 117 | + } |
| 118 | +#endif |
| 119 | +} |
| 120 | + |
| 121 | +TEST (DynamicLibraryTests, OpenEmptyString) |
| 122 | +{ |
| 123 | + DynamicLibrary lib; |
| 124 | + |
| 125 | + // Opening with empty string loads current process symbols |
| 126 | + EXPECT_TRUE (lib.open ("")); |
| 127 | + |
| 128 | + // Should be able to get functions from current process |
| 129 | + auto func = lib.getFunction ("malloc"); |
| 130 | + EXPECT_NE (func, nullptr); |
| 131 | +} |
| 132 | + |
| 133 | +#endif // ! YUP_WASM |
0 commit comments