1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "base/files/file_path.h"
6 #include "base/macros.h"
7 #include "base/native_library.h"
8 #include "base/path_service.h"
9 #include "base/test/native_library_test_utils.h"
10 #include "build/build_config.h"
11 #include "testing/gtest/include/gtest/gtest.h"
15 const FilePath::CharType kDummyLibraryPath[] =
16 FILE_PATH_LITERAL("dummy_library");
18 TEST(NativeLibraryTest, LoadFailure) {
19 NativeLibraryLoadError error;
20 EXPECT_FALSE(LoadNativeLibrary(FilePath(kDummyLibraryPath), &error));
21 EXPECT_FALSE(error.ToString().empty());
24 // |error| is optional and can be null.
25 TEST(NativeLibraryTest, LoadFailureWithNullError) {
26 EXPECT_FALSE(LoadNativeLibrary(FilePath(kDummyLibraryPath), nullptr));
29 TEST(NativeLibraryTest, GetNativeLibraryName) {
30 const char kExpectedName[] =
35 #elif defined(OS_MACOSX)
37 #elif defined(OS_POSIX) || defined(OS_FUCHSIA)
40 EXPECT_EQ(kExpectedName, GetNativeLibraryName("mylib"));
43 TEST(NativeLibraryTest, GetLoadableModuleName) {
44 const char kExpectedName[] =
49 #elif defined(OS_MACOSX)
51 #elif defined(OS_POSIX) || defined(OS_FUCHSIA)
54 EXPECT_EQ(kExpectedName, GetLoadableModuleName("mylib"));
57 // We don't support dynamic loading on iOS, and ASAN will complain about our
58 // intentional ODR violation because of |g_native_library_exported_value| being
59 // defined globally both here and in the shared library.
60 #if !defined(OS_IOS) && !defined(ADDRESS_SANITIZER)
62 const char kTestLibraryName[] =
64 "test_shared_library.dll";
65 #elif defined(OS_MACOSX)
66 "libtest_shared_library.dylib";
67 #elif defined(OS_ANDROID) && defined(COMPONENT_BUILD)
68 "libtest_shared_library.cr.so";
69 #elif defined(OS_POSIX) || defined(OS_FUCHSIA)
70 "libtest_shared_library.so";
75 TestLibrary() : TestLibrary(NativeLibraryOptions()) {}
77 explicit TestLibrary(const NativeLibraryOptions& options)
79 base::FilePath exe_path;
81 #if !defined(OS_FUCHSIA)
82 // Libraries do not sit alongside the executable in Fuchsia. NativeLibrary
83 // is aware of this and is able to resolve library paths correctly.
84 CHECK(base::PathService::Get(base::DIR_EXE, &exe_path));
87 library_ = LoadNativeLibraryWithOptions(
88 exe_path.AppendASCII(kTestLibraryName), options, nullptr);
93 UnloadNativeLibrary(library_);
96 template <typename ReturnType, typename... Args>
97 ReturnType Call(const char* function_name, Args... args) {
98 return reinterpret_cast<ReturnType(*)(Args...)>(
99 GetFunctionPointerFromNativeLibrary(library_, function_name))(args...);
103 NativeLibrary library_;
105 DISALLOW_COPY_AND_ASSIGN(TestLibrary);
108 // NativeLibraaryTest.LoadLibrary is failing on M tablets only.
110 #if !defined(OS_ANDROID)
112 // Verifies that we can load a native library and resolve its exported symbols.
113 TEST(NativeLibraryTest, LoadLibrary) {
115 EXPECT_EQ(5, library.Call<int>("GetSimpleTestValue"));
118 #endif // !defined(OS_ANDROID)
120 // Android dlopen() requires further investigation, as it might vary across
121 // versions with respect to symbol resolution scope.
122 // TSan and MSan error out on RTLD_DEEPBIND, https://crbug.com/705255
123 #if !defined(OS_ANDROID) && !defined(THREAD_SANITIZER) && \
124 !defined(MEMORY_SANITIZER)
126 // Verifies that the |prefer_own_symbols| option satisfies its guarantee that
127 // a loaded library will always prefer local symbol resolution before
128 // considering global symbols.
129 TEST(NativeLibraryTest, LoadLibraryPreferOwnSymbols) {
130 NativeLibraryOptions options;
131 options.prefer_own_symbols = true;
132 TestLibrary library(options);
134 // Verify that this binary and the DSO use different storage for
135 // |g_native_library_exported_value|.
136 g_native_library_exported_value = 1;
137 library.Call<void>("SetExportedValue", 2);
138 EXPECT_EQ(1, g_native_library_exported_value);
139 g_native_library_exported_value = 3;
140 EXPECT_EQ(2, library.Call<int>("GetExportedValue"));
142 // Both this binary and the library link against the
143 // native_library_test_utils source library, which in turn exports the
144 // NativeLibraryTestIncrement() function whose return value depends on some
145 // static internal state.
147 // The DSO's GetIncrementValue() forwards to that function inside the DSO.
149 // Here we verify that direct calls to NativeLibraryTestIncrement() in this
150 // binary return a sequence of values independent from the sequence returned
151 // by GetIncrementValue(), ensuring that the DSO is calling its own local
152 // definition of NativeLibraryTestIncrement().
153 EXPECT_EQ(1, library.Call<int>("GetIncrementValue"));
154 EXPECT_EQ(1, NativeLibraryTestIncrement());
155 EXPECT_EQ(2, library.Call<int>("GetIncrementValue"));
156 EXPECT_EQ(3, library.Call<int>("GetIncrementValue"));
157 EXPECT_EQ(4, library.Call<int>("NativeLibraryTestIncrement"));
158 EXPECT_EQ(2, NativeLibraryTestIncrement());
159 EXPECT_EQ(3, NativeLibraryTestIncrement());
162 #endif // !defined(OS_ANDROID)
164 #endif // !defined(OS_IOS) && !defined(ADDRESS_SANITIZER)