Chromium Code Reviews| Index: base/native_library_unittest.cc |
| diff --git a/base/native_library_unittest.cc b/base/native_library_unittest.cc |
| index c189f56e995c04598df5bf2d85c1a882dcafd629..3463fd45d1926f7fe184e2e91efde69e0e5efb1b 100644 |
| --- a/base/native_library_unittest.cc |
| +++ b/base/native_library_unittest.cc |
| @@ -4,8 +4,20 @@ |
| #include "base/files/file_path.h" |
| #include "base/native_library.h" |
| +#include "base/path_service.h" |
| +#include "build/build_config.h" |
| #include "testing/gtest/include/gtest/gtest.h" |
| +#if defined(OS_WIN) |
| +#define EXPORT __declspec(dllexport) |
| +#else |
| +#define EXPORT __attribute__((visibility("default"))) |
| +#endif |
| + |
| +// This symbol name must match the exported symbol in |
| +// native_library_test_library.cc. |
| +int EXPORT g_native_library_test_symbol = 0; |
| + |
| namespace base { |
| const FilePath::CharType kDummyLibraryPath[] = |
| @@ -36,4 +48,82 @@ TEST(NativeLibraryTest, GetNativeLibraryName) { |
| EXPECT_EQ(kExpectedName, GetNativeLibraryName("mylib")); |
| } |
| +#if !defined(OS_IOS) |
| + |
| +const char kTestLibraryName[] = |
| +#if defined(OS_MACOSX) |
| + "libnative_library_test_library.dylib"; |
| +#elif defined(OS_ANDROID) |
| + "libnative_library_test_library.cr.so"; |
|
Primiano Tucci (use gerrit)
2016/08/24 22:14:09
.cr.so should be only for component builds.
Also l
Ken Rockot(use gerrit already)
2016/08/24 23:23:57
I suppose we don't need to support it now, but I w
|
| +#elif defined(OS_POSIX) |
| + "libnative_library_test_library.so"; |
| +#elif defined(OS_WIN) |
| + "native_library_test_library.dll"; |
| +#endif |
| + |
| +class TestLibrary { |
| + public: |
| + explicit TestLibrary( |
| + const NativeLibraryOptions& options = NativeLibraryOptions()) |
| + : library_(nullptr) { |
| + base::FilePath exe_path; |
| + CHECK(base::PathService::Get(base::DIR_EXE, &exe_path)); |
| + |
| + library_ = LoadNativeLibraryWithOptions( |
| + exe_path.AppendASCII(kTestLibraryName), options, nullptr); |
| + CHECK(library_); |
| + } |
| + |
| + ~TestLibrary() { |
| + UnloadNativeLibrary(library_); |
| + } |
| + |
| + int GetTestValue() { |
| + return GetFunction<int(*)()>("GetTestValue")(); |
| + } |
| + |
| + bool IsSameTestSymbolAddress(int* address) { |
| + return GetFunction<bool(*)(int*)>("IsSameTestSymbolAddress")(address); |
| + } |
| + |
| + private: |
| + template <typename FuncType> |
| + FuncType GetFunction(const char* name) { |
| + FuncType func = reinterpret_cast<FuncType>( |
| + GetFunctionPointerFromNativeLibrary(library_, name)); |
| + CHECK(func); |
| + return func; |
| + } |
| + |
| + NativeLibrary library_; |
| +}; |
| + |
| +// Verifies that we can load a native library and resolve its exported symbols. |
| +TEST(NativeLibraryTest, LoadLibrary) { |
| + TestLibrary library; |
| + EXPECT_EQ(42, library.GetTestValue()); |
| +} |
| + |
| +// Verifies that if we load a native library which exports some symbol also |
| +// exported by the main binary, the native library resolves the symbol to its |
| +// own local definition. Note that on some POSIX builds this may not be the |
| +// default behavior, so the |prefer_own_symbols| option must be used to opt in. |
| +TEST(NativeLibraryTest, LoadLibraryPreferOwnSymbols) { |
|
Primiano Tucci (use gerrit)
2016/08/24 22:14:09
I think it's just clear if this is #ifdef OS_LINUX
Ken Rockot(use gerrit already)
2016/08/24 23:23:57
see comment in BUILD.gn
|
| +#if defined(EXPECT_GLOBAL_RTLD_BEHAVIOR) |
| + // NOTE: This is only defined on non-Android POSIX builds which use gold for |
| + // linking. Otherwise we don't expect this behavior. |
| + { |
| + TestLibrary library; |
| + EXPECT_TRUE(library.IsSameTestSymbolAddress(&g_native_library_test_symbol)); |
| + } |
| +#endif // defined(EXPECT_GLOBAL_RTLD_BEHAVIOR) |
| + |
| + NativeLibraryOptions options; |
| + options.prefer_own_symbols = true; |
| + TestLibrary library(options); |
| + EXPECT_FALSE(library.IsSameTestSymbolAddress(&g_native_library_test_symbol)); |
| +} |
| + |
| +#endif // !defined(OS_IOS) |
| + |
| } // namespace base |