1 // Copyright 2015 The Chromium Authors
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/native_library.h"
7 #include "base/files/file_path.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 #if BUILDFLAG(IS_FUCHSIA)
30 TEST(NativeLibraryTest, LoadAbsolutePath) {
31 EXPECT_TRUE(LoadNativeLibrary(FilePath("/pkg/lib/libtest_shared_library.so"),
35 TEST(NativeLibraryTest, LoadAbsolutePath_OutsideLibraryRoot) {
36 NativeLibraryLoadError error;
37 EXPECT_FALSE(LoadNativeLibrary(FilePath("/pkg/tmp/libtest_shared_library.so"),
39 std::string expected_error =
40 "Absolute library paths must begin with /pkg/lib";
41 EXPECT_EQ(error.ToString(), expected_error);
45 TEST(NativeLibraryTest, GetNativeLibraryName) {
46 const char kExpectedName[] =
49 #elif BUILDFLAG(IS_IOS)
51 #elif BUILDFLAG(IS_MAC)
53 #elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
56 EXPECT_EQ(kExpectedName, GetNativeLibraryName("mylib"));
59 TEST(NativeLibraryTest, GetLoadableModuleName) {
60 const char kExpectedName[] =
63 #elif BUILDFLAG(IS_IOS)
65 #elif BUILDFLAG(IS_MAC)
67 #elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
70 EXPECT_EQ(kExpectedName, GetLoadableModuleName("mylib"));
73 // We don't support dynamic loading on iOS, and ASAN will complain about our
74 // intentional ODR violation because of |g_native_library_exported_value| being
75 // defined globally both here and in the shared library.
76 #if !BUILDFLAG(IS_IOS) && !defined(ADDRESS_SANITIZER)
78 const char kTestLibraryName[] =
80 "test_shared_library.dll";
81 #elif BUILDFLAG(IS_MAC)
82 "libtest_shared_library.dylib";
83 #elif BUILDFLAG(IS_ANDROID) && defined(COMPONENT_BUILD)
84 "libtest_shared_library.cr.so";
85 #elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
86 "libtest_shared_library.so";
91 TestLibrary() : TestLibrary(NativeLibraryOptions()) {}
93 explicit TestLibrary(const NativeLibraryOptions& options)
95 base::FilePath exe_path;
97 #if !BUILDFLAG(IS_FUCHSIA)
98 // Libraries do not sit alongside the executable in Fuchsia. NativeLibrary
99 // is aware of this and is able to resolve library paths correctly.
100 CHECK(base::PathService::Get(base::DIR_EXE, &exe_path));
103 library_ = LoadNativeLibraryWithOptions(
104 exe_path.AppendASCII(kTestLibraryName), options, nullptr);
107 TestLibrary(const TestLibrary&) = delete;
108 TestLibrary& operator=(const TestLibrary&) = delete;
109 ~TestLibrary() { UnloadNativeLibrary(library_); }
111 template <typename ReturnType, typename... Args>
112 ReturnType Call(const char* function_name, Args... args) {
113 return reinterpret_cast<ReturnType (*)(Args...)>(
114 GetFunctionPointerFromNativeLibrary(library_, function_name))(args...);
118 NativeLibrary library_;
121 // NativeLibraaryTest.LoadLibrary is failing on M tablets only.
123 #if !BUILDFLAG(IS_ANDROID)
125 // Verifies that we can load a native library and resolve its exported symbols.
126 TEST(NativeLibraryTest, LoadLibrary) {
128 EXPECT_EQ(5, library.Call<int>("GetSimpleTestValue"));
131 #endif // !BUILDFLAG(IS_ANDROID)
133 // Android dlopen() requires further investigation, as it might vary across
134 // versions with respect to symbol resolution scope.
135 // TSan and MSan error out on RTLD_DEEPBIND, https://crbug.com/705255
136 #if !BUILDFLAG(IS_ANDROID) && !defined(THREAD_SANITIZER) && \
137 !defined(MEMORY_SANITIZER)
139 // Verifies that the |prefer_own_symbols| option satisfies its guarantee that
140 // a loaded library will always prefer local symbol resolution before
141 // considering global symbols.
142 TEST(NativeLibraryTest, LoadLibraryPreferOwnSymbols) {
143 NativeLibraryOptions options;
144 options.prefer_own_symbols = true;
145 TestLibrary library(options);
147 // Verify that this binary and the DSO use different storage for
148 // |g_native_library_exported_value|.
149 g_native_library_exported_value = 1;
150 library.Call<void>("SetExportedValue", 2);
151 EXPECT_EQ(1, g_native_library_exported_value);
152 g_native_library_exported_value = 3;
153 EXPECT_EQ(2, library.Call<int>("GetExportedValue"));
155 // Both this binary and the library link against the
156 // native_library_test_utils source library, which in turn exports the
157 // NativeLibraryTestIncrement() function whose return value depends on some
158 // static internal state.
160 // The DSO's GetIncrementValue() forwards to that function inside the DSO.
162 // Here we verify that direct calls to NativeLibraryTestIncrement() in this
163 // binary return a sequence of values independent from the sequence returned
164 // by GetIncrementValue(), ensuring that the DSO is calling its own local
165 // definition of NativeLibraryTestIncrement().
166 EXPECT_EQ(1, library.Call<int>("GetIncrementValue"));
167 EXPECT_EQ(1, NativeLibraryTestIncrement());
168 EXPECT_EQ(2, library.Call<int>("GetIncrementValue"));
169 EXPECT_EQ(3, library.Call<int>("GetIncrementValue"));
170 EXPECT_EQ(4, library.Call<int>("NativeLibraryTestIncrement"));
171 EXPECT_EQ(2, NativeLibraryTestIncrement());
172 EXPECT_EQ(3, NativeLibraryTestIncrement());
175 #endif // !BUILDFLAG(IS_ANDROID) && !defined(THREAD_SANITIZER) && \
176 // !defined(MEMORY_SANITIZER)
178 #endif // !BUILDFLAG(IS_IOS) && !defined(ADDRESS_SANITIZER)