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/native_library.h"
7 #include "base/path_service.h"
8 #include "base/test/native_library_test_utils.h"
9 #include "build/build_config.h"
10 #include "testing/gtest/include/gtest/gtest.h"
14 const FilePath::CharType kDummyLibraryPath[] =
15 FILE_PATH_LITERAL("dummy_library");
17 TEST(NativeLibraryTest, LoadFailure) {
18 NativeLibraryLoadError error;
19 EXPECT_FALSE(LoadNativeLibrary(FilePath(kDummyLibraryPath), &error));
20 EXPECT_FALSE(error.ToString().empty());
23 // |error| is optional and can be null.
24 TEST(NativeLibraryTest, LoadFailureWithNullError) {
25 EXPECT_FALSE(LoadNativeLibrary(FilePath(kDummyLibraryPath), nullptr));
28 TEST(NativeLibraryTest, GetNativeLibraryName) {
29 const char kExpectedName[] =
36 #elif defined(OS_POSIX) || defined(OS_FUCHSIA)
39 EXPECT_EQ(kExpectedName, GetNativeLibraryName("mylib"));
42 TEST(NativeLibraryTest, GetLoadableModuleName) {
43 const char kExpectedName[] =
50 #elif defined(OS_POSIX) || defined(OS_FUCHSIA)
53 EXPECT_EQ(kExpectedName, GetLoadableModuleName("mylib"));
56 // We don't support dynamic loading on iOS, and ASAN will complain about our
57 // intentional ODR violation because of |g_native_library_exported_value| being
58 // defined globally both here and in the shared library.
59 #if !defined(OS_IOS) && !defined(ADDRESS_SANITIZER)
61 const char kTestLibraryName[] =
63 "test_shared_library.dll";
65 "libtest_shared_library.dylib";
66 #elif defined(OS_ANDROID) && defined(COMPONENT_BUILD)
67 "libtest_shared_library.cr.so";
68 #elif defined(OS_POSIX) || defined(OS_FUCHSIA)
69 "libtest_shared_library.so";
74 TestLibrary() : TestLibrary(NativeLibraryOptions()) {}
76 explicit TestLibrary(const NativeLibraryOptions& options)
78 base::FilePath exe_path;
80 #if !defined(OS_FUCHSIA)
81 // Libraries do not sit alongside the executable in Fuchsia. NativeLibrary
82 // is aware of this and is able to resolve library paths correctly.
83 CHECK(base::PathService::Get(base::DIR_EXE, &exe_path));
86 library_ = LoadNativeLibraryWithOptions(
87 exe_path.AppendASCII(kTestLibraryName), options, nullptr);
90 TestLibrary(const TestLibrary&) = delete;
91 TestLibrary& operator=(const TestLibrary&) = delete;
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_;
106 // NativeLibraaryTest.LoadLibrary is failing on M tablets only.
108 #if !defined(OS_ANDROID)
110 // Verifies that we can load a native library and resolve its exported symbols.
111 TEST(NativeLibraryTest, LoadLibrary) {
113 EXPECT_EQ(5, library.Call<int>("GetSimpleTestValue"));
116 #endif // !defined(OS_ANDROID)
118 // Android dlopen() requires further investigation, as it might vary across
119 // versions with respect to symbol resolution scope.
120 // TSan and MSan error out on RTLD_DEEPBIND, https://crbug.com/705255
121 #if !defined(OS_ANDROID) && !defined(THREAD_SANITIZER) && \
122 !defined(MEMORY_SANITIZER)
124 // Verifies that the |prefer_own_symbols| option satisfies its guarantee that
125 // a loaded library will always prefer local symbol resolution before
126 // considering global symbols.
127 TEST(NativeLibraryTest, LoadLibraryPreferOwnSymbols) {
128 NativeLibraryOptions options;
129 options.prefer_own_symbols = true;
130 TestLibrary library(options);
132 // Verify that this binary and the DSO use different storage for
133 // |g_native_library_exported_value|.
134 g_native_library_exported_value = 1;
135 library.Call<void>("SetExportedValue", 2);
136 EXPECT_EQ(1, g_native_library_exported_value);
137 g_native_library_exported_value = 3;
138 EXPECT_EQ(2, library.Call<int>("GetExportedValue"));
140 // Both this binary and the library link against the
141 // native_library_test_utils source library, which in turn exports the
142 // NativeLibraryTestIncrement() function whose return value depends on some
143 // static internal state.
145 // The DSO's GetIncrementValue() forwards to that function inside the DSO.
147 // Here we verify that direct calls to NativeLibraryTestIncrement() in this
148 // binary return a sequence of values independent from the sequence returned
149 // by GetIncrementValue(), ensuring that the DSO is calling its own local
150 // definition of NativeLibraryTestIncrement().
151 EXPECT_EQ(1, library.Call<int>("GetIncrementValue"));
152 EXPECT_EQ(1, NativeLibraryTestIncrement());
153 EXPECT_EQ(2, library.Call<int>("GetIncrementValue"));
154 EXPECT_EQ(3, library.Call<int>("GetIncrementValue"));
155 EXPECT_EQ(4, library.Call<int>("NativeLibraryTestIncrement"));
156 EXPECT_EQ(2, NativeLibraryTestIncrement());
157 EXPECT_EQ(3, NativeLibraryTestIncrement());
160 #endif // !defined(OS_ANDROID)
162 #endif // !defined(OS_IOS) && !defined(ADDRESS_SANITIZER)