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 "build/build_config.h" | |
7 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
8 | 10 |
11 #if defined(OS_WIN) | |
12 #define EXPORT __declspec(dllexport) | |
13 #else | |
14 #define EXPORT __attribute__((visibility("default"))) | |
15 #endif | |
16 | |
17 // This symbol name must match the exported symbol in | |
18 // native_library_test_library.cc. | |
19 int EXPORT g_native_library_test_symbol = 0; | |
20 | |
9 namespace base { | 21 namespace base { |
10 | 22 |
11 const FilePath::CharType kDummyLibraryPath[] = | 23 const FilePath::CharType kDummyLibraryPath[] = |
12 FILE_PATH_LITERAL("dummy_library"); | 24 FILE_PATH_LITERAL("dummy_library"); |
13 | 25 |
14 TEST(NativeLibraryTest, LoadFailure) { | 26 TEST(NativeLibraryTest, LoadFailure) { |
15 NativeLibraryLoadError error; | 27 NativeLibraryLoadError error; |
16 EXPECT_FALSE(LoadNativeLibrary(FilePath(kDummyLibraryPath), &error)); | 28 EXPECT_FALSE(LoadNativeLibrary(FilePath(kDummyLibraryPath), &error)); |
17 EXPECT_FALSE(error.ToString().empty()); | 29 EXPECT_FALSE(error.ToString().empty()); |
18 } | 30 } |
(...skipping 10 matching lines...) Expand all Loading... | |
29 #elif defined(OS_MACOSX) | 41 #elif defined(OS_MACOSX) |
30 "libmylib.dylib"; | 42 "libmylib.dylib"; |
31 #elif defined(OS_POSIX) | 43 #elif defined(OS_POSIX) |
32 "libmylib.so"; | 44 "libmylib.so"; |
33 #elif defined(OS_WIN) | 45 #elif defined(OS_WIN) |
34 "mylib.dll"; | 46 "mylib.dll"; |
35 #endif | 47 #endif |
36 EXPECT_EQ(kExpectedName, GetNativeLibraryName("mylib")); | 48 EXPECT_EQ(kExpectedName, GetNativeLibraryName("mylib")); |
37 } | 49 } |
38 | 50 |
51 // We don't support dynamic loading on iOS, and ASAN will complain about our | |
52 // intentional ODR violation here. | |
53 #if !defined(OS_IOS) && !defined(ADDRESS_SANITIZER) | |
54 | |
55 const char kTestLibraryName[] = | |
56 #if defined(OS_MACOSX) | |
57 "libnative_library_test_library.dylib"; | |
58 #elif defined(OS_ANDROID) && defined(COMPONENT_BUILD) | |
59 "libnative_library_test_library.cr.so"; | |
60 #elif defined(OS_POSIX) | |
61 "libnative_library_test_library.so"; | |
62 #elif defined(OS_WIN) | |
63 "native_library_test_library.dll"; | |
64 #endif | |
65 | |
66 class TestLibrary { | |
67 public: | |
68 explicit TestLibrary( | |
69 const NativeLibraryOptions& options = NativeLibraryOptions()) | |
70 : library_(nullptr) { | |
71 base::FilePath exe_path; | |
72 CHECK(base::PathService::Get(base::DIR_EXE, &exe_path)); | |
73 | |
74 library_ = LoadNativeLibraryWithOptions( | |
75 exe_path.AppendASCII(kTestLibraryName), options, nullptr); | |
76 CHECK(library_); | |
77 } | |
78 | |
79 ~TestLibrary() { | |
80 UnloadNativeLibrary(library_); | |
81 } | |
82 | |
83 int GetTestValue() { | |
84 return GetFunction<int(*)()>("GetTestValue")(); | |
85 } | |
86 | |
87 bool IsSameTestSymbolAddress(int* address) { | |
88 return GetFunction<bool(*)(int*)>("IsSameTestSymbolAddress")(address); | |
89 } | |
90 | |
91 private: | |
92 template <typename FuncType> | |
93 FuncType GetFunction(const char* name) { | |
94 FuncType func = reinterpret_cast<FuncType>( | |
95 GetFunctionPointerFromNativeLibrary(library_, name)); | |
96 CHECK(func); | |
97 return func; | |
98 } | |
99 | |
100 NativeLibrary library_; | |
101 }; | |
102 | |
103 // Verifies that we can load a native library and resolve its exported symbols. | |
104 TEST(NativeLibraryTest, LoadLibrary) { | |
105 TestLibrary library; | |
106 EXPECT_EQ(42, library.GetTestValue()); | |
107 } | |
108 | |
109 // Verifies that the |prefer_own_symbols| option satisfies its guarantee that | |
110 // a loaded library will always prefer local symbol resolution before | |
111 // considering global symbols. | |
112 TEST(NativeLibraryTest, LoadLibraryPreferOwnSymbols) { | |
113 NativeLibraryOptions options; | |
114 options.prefer_own_symbols = true; | |
115 TestLibrary library(options); | |
116 EXPECT_FALSE(library.IsSameTestSymbolAddress(&g_native_library_test_symbol)); | |
Primiano Tucci (use gerrit)
2016/08/25 10:20:44
I'd probably rework the test to work as follows:
Ken Rockot(use gerrit already)
2016/08/25 15:52:12
Ah, yeah I considered that as well. I'll do this.
Primiano Tucci (use gerrit)
2016/08/25 16:14:33
Oh, you are right. I screwed up my example. That i
| |
117 } | |
118 | |
119 #endif // !defined(OS_IOS) && !defined(ADDRESS_SANITIZER) | |
120 | |
39 } // namespace base | 121 } // namespace base |
OLD | NEW |