Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/files/file_path.h" | 5 #include "base/files/file_path.h" |
| 6 #include "base/native_library.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" | |
| 7 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
| 8 | 11 |
| 9 namespace base { | 12 namespace base { |
| 10 | 13 |
| 11 const FilePath::CharType kDummyLibraryPath[] = | 14 const FilePath::CharType kDummyLibraryPath[] = |
| 12 FILE_PATH_LITERAL("dummy_library"); | 15 FILE_PATH_LITERAL("dummy_library"); |
| 13 | 16 |
| 14 TEST(NativeLibraryTest, LoadFailure) { | 17 TEST(NativeLibraryTest, LoadFailure) { |
| 15 NativeLibraryLoadError error; | 18 NativeLibraryLoadError error; |
| 16 EXPECT_FALSE(LoadNativeLibrary(FilePath(kDummyLibraryPath), &error)); | 19 EXPECT_FALSE(LoadNativeLibrary(FilePath(kDummyLibraryPath), &error)); |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 29 #elif defined(OS_MACOSX) | 32 #elif defined(OS_MACOSX) |
| 30 "libmylib.dylib"; | 33 "libmylib.dylib"; |
| 31 #elif defined(OS_POSIX) | 34 #elif defined(OS_POSIX) |
| 32 "libmylib.so"; | 35 "libmylib.so"; |
| 33 #elif defined(OS_WIN) | 36 #elif defined(OS_WIN) |
| 34 "mylib.dll"; | 37 "mylib.dll"; |
| 35 #endif | 38 #endif |
| 36 EXPECT_EQ(kExpectedName, GetNativeLibraryName("mylib")); | 39 EXPECT_EQ(kExpectedName, GetNativeLibraryName("mylib")); |
| 37 } | 40 } |
| 38 | 41 |
| 42 // We don't support dynamic loading on iOS, and ASAN will complain about our | |
| 43 // intentional ODR violation because of |g_native_library_exported_value| being | |
| 44 // defined globally both here and in the shared library. | |
| 45 #if !defined(OS_IOS) && !defined(ADDRESS_SANITIZER) | |
| 46 | |
| 47 const char kTestLibraryName[] = | |
| 48 #if defined(OS_MACOSX) | |
| 49 "libtest_shared_library.dylib"; | |
| 50 #elif defined(OS_ANDROID) && defined(COMPONENT_BUILD) | |
| 51 "libtest_shared_library.cr.so"; | |
| 52 #elif defined(OS_POSIX) | |
| 53 "libtest_shared_library.so"; | |
| 54 #elif defined(OS_WIN) | |
| 55 "test_shared_library.dll"; | |
| 56 #endif | |
| 57 | |
| 58 class TestLibrary { | |
| 59 public: | |
| 60 explicit TestLibrary( | |
| 61 const NativeLibraryOptions& options = NativeLibraryOptions()) | |
|
Lei Zhang
2016/08/25 23:08:58
Can we instead have a separate TestLibrary() ctor
Ken Rockot(use gerrit already)
2016/08/25 23:20:42
Done
| |
| 62 : library_(nullptr) { | |
| 63 base::FilePath exe_path; | |
| 64 CHECK(base::PathService::Get(base::DIR_EXE, &exe_path)); | |
| 65 | |
| 66 library_ = LoadNativeLibraryWithOptions( | |
| 67 exe_path.AppendASCII(kTestLibraryName), options, nullptr); | |
| 68 CHECK(library_); | |
| 69 } | |
| 70 | |
| 71 ~TestLibrary() { | |
| 72 UnloadNativeLibrary(library_); | |
| 73 } | |
| 74 | |
| 75 template <typename ReturnType, typename... Args> | |
| 76 ReturnType Call(const char* function_name, Args... args) { | |
| 77 return reinterpret_cast<ReturnType(*)(Args...)>( | |
| 78 GetFunctionPointerFromNativeLibrary(library_, function_name))(args...); | |
| 79 } | |
| 80 | |
| 81 private: | |
| 82 NativeLibrary library_; | |
| 83 }; | |
|
Lei Zhang
2016/08/25 23:08:58
DISALLOW_COPY_AND_ASSIGN ?
Ken Rockot(use gerrit already)
2016/08/25 23:20:42
Done
| |
| 84 | |
| 85 // Verifies that we can load a native library and resolve its exported symbols. | |
| 86 TEST(NativeLibraryTest, LoadLibrary) { | |
| 87 TestLibrary library; | |
| 88 EXPECT_EQ(5, library.Call<int>("GetSimpleTestValue")); | |
| 89 } | |
| 90 | |
| 91 // Android dlopen() requires further investigation, as it might vary across | |
| 92 // versions with respect to symbol resolution scope. | |
| 93 #if !defined(OS_ANDROID) | |
| 94 | |
| 95 // Verifies that the |prefer_own_symbols| option satisfies its guarantee that | |
| 96 // a loaded library will always prefer local symbol resolution before | |
| 97 // considering global symbols. | |
| 98 TEST(NativeLibraryTest, LoadLibraryPreferOwnSymbols) { | |
| 99 NativeLibraryOptions options; | |
| 100 options.prefer_own_symbols = true; | |
| 101 TestLibrary library(options); | |
| 102 | |
| 103 // Verify that this binary and the DSO use different storage for | |
| 104 // |g_native_library_exported_value|. | |
| 105 g_native_library_exported_value = 1; | |
| 106 library.Call<void>("SetExportedValue", 2); | |
| 107 EXPECT_EQ(1, g_native_library_exported_value); | |
| 108 g_native_library_exported_value = 3; | |
| 109 EXPECT_EQ(2, library.Call<int>("GetExportedValue")); | |
| 110 | |
| 111 // Both this binary and the library link against the | |
| 112 // native_library_test_utils source library, which in turn exports the | |
| 113 // NativeLibraryTestIncrement() function whose return value depends on some | |
| 114 // static internal state. | |
| 115 // | |
| 116 // The DSO's GetIncrementValue() forwards to that function inside the DSO. | |
| 117 // | |
| 118 // Here we verify that direct calls to NativeLibraryTestIncrement() in this | |
| 119 // binary return a sequence of values independent from the sequence returned | |
| 120 // by GetIncrementValue(), ensuring that the DSO is calling its own local | |
| 121 // definition of NativeLibraryTestIncrement(). | |
| 122 EXPECT_EQ(1, library.Call<int>("GetIncrementValue")); | |
| 123 EXPECT_EQ(1, NativeLibraryTestIncrement()); | |
| 124 EXPECT_EQ(2, library.Call<int>("GetIncrementValue")); | |
| 125 EXPECT_EQ(3, library.Call<int>("GetIncrementValue")); | |
| 126 EXPECT_EQ(4, library.Call<int>("NativeLibraryTestIncrement")); | |
| 127 EXPECT_EQ(2, NativeLibraryTestIncrement()); | |
| 128 EXPECT_EQ(3, NativeLibraryTestIncrement()); | |
| 129 } | |
| 130 | |
| 131 #endif // !defined(OS_ANDROID) | |
| 132 | |
| 133 #endif // !defined(OS_IOS) && !defined(ADDRESS_SANITIZER) | |
| 134 | |
| 39 } // namespace base | 135 } // namespace base |
| OLD | NEW |