[M94 Dev][Tizen] Fix for errors for generating ninja files
[platform/framework/web/chromium-efl.git] / base / native_library_unittest.cc
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.
4
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"
11
12 namespace base {
13
14 const FilePath::CharType kDummyLibraryPath[] =
15     FILE_PATH_LITERAL("dummy_library");
16
17 TEST(NativeLibraryTest, LoadFailure) {
18   NativeLibraryLoadError error;
19   EXPECT_FALSE(LoadNativeLibrary(FilePath(kDummyLibraryPath), &error));
20   EXPECT_FALSE(error.ToString().empty());
21 }
22
23 // |error| is optional and can be null.
24 TEST(NativeLibraryTest, LoadFailureWithNullError) {
25   EXPECT_FALSE(LoadNativeLibrary(FilePath(kDummyLibraryPath), nullptr));
26 }
27
28 TEST(NativeLibraryTest, GetNativeLibraryName) {
29   const char kExpectedName[] =
30 #if defined(OS_WIN)
31       "mylib.dll";
32 #elif defined(OS_IOS)
33       "mylib";
34 #elif defined(OS_MAC)
35       "libmylib.dylib";
36 #elif defined(OS_POSIX) || defined(OS_FUCHSIA)
37       "libmylib.so";
38 #endif
39   EXPECT_EQ(kExpectedName, GetNativeLibraryName("mylib"));
40 }
41
42 TEST(NativeLibraryTest, GetLoadableModuleName) {
43   const char kExpectedName[] =
44 #if defined(OS_WIN)
45       "mylib.dll";
46 #elif defined(OS_IOS)
47       "mylib";
48 #elif defined(OS_MAC)
49       "mylib.so";
50 #elif defined(OS_POSIX) || defined(OS_FUCHSIA)
51       "libmylib.so";
52 #endif
53   EXPECT_EQ(kExpectedName, GetLoadableModuleName("mylib"));
54 }
55
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)
60
61 const char kTestLibraryName[] =
62 #if defined(OS_WIN)
63     "test_shared_library.dll";
64 #elif defined(OS_MAC)
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";
70 #endif
71
72 class TestLibrary {
73  public:
74   TestLibrary() : TestLibrary(NativeLibraryOptions()) {}
75
76   explicit TestLibrary(const NativeLibraryOptions& options)
77     : library_(nullptr) {
78     base::FilePath exe_path;
79
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));
84 #endif
85
86     library_ = LoadNativeLibraryWithOptions(
87         exe_path.AppendASCII(kTestLibraryName), options, nullptr);
88     CHECK(library_);
89   }
90   TestLibrary(const TestLibrary&) = delete;
91   TestLibrary& operator=(const TestLibrary&) = delete;
92   ~TestLibrary() {
93     UnloadNativeLibrary(library_);
94   }
95
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...);
100   }
101
102  private:
103   NativeLibrary library_;
104 };
105
106 // NativeLibraaryTest.LoadLibrary is failing on M tablets only.
107 // crbug/641309
108 #if !defined(OS_ANDROID)
109
110 // Verifies that we can load a native library and resolve its exported symbols.
111 TEST(NativeLibraryTest, LoadLibrary) {
112   TestLibrary library;
113   EXPECT_EQ(5, library.Call<int>("GetSimpleTestValue"));
114 }
115
116 #endif  // !defined(OS_ANDROID)
117
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)
123
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);
131
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"));
139
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.
144   //
145   // The DSO's GetIncrementValue() forwards to that function inside the DSO.
146   //
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());
158 }
159
160 #endif  // !defined(OS_ANDROID)
161
162 #endif  // !defined(OS_IOS) && !defined(ADDRESS_SANITIZER)
163
164 }  // namespace base