blob: cab18daed1d719cefe530b52aed625011a128b5f [file] [log] [blame]
Martin Stjernholm19d1feb2021-03-30 22:35:24 +01001/*
2 * Copyright (C) 2021 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ART_LIBNATIVELOADER_NATIVE_LOADER_TEST_H_
18#define ART_LIBNATIVELOADER_NATIVE_LOADER_TEST_H_
19
20#include <string>
21#include <unordered_map>
22
23#include <android-base/stringprintf.h>
24#include <gmock/gmock.h>
25#include <jni.h>
26
27#include "native_loader_namespace.h"
28#include "nativeloader/dlext_namespaces.h"
29
30namespace android {
31namespace nativeloader {
32
33using ::testing::Return;
34using ::testing::_;
35
36// gmock interface that represents interested platform APIs on libdl_android and libnativebridge
37class Platform {
38 public:
39 virtual ~Platform() {}
40
41 // These mock_* are the APIs semantically the same across libdl_android and libnativebridge.
42 // Instead of having two set of mock APIs for the two, define only one set with an additional
43 // argument 'bool bridged' to identify the context (i.e., called for libdl_android or
44 // libnativebridge).
45 typedef char* mock_namespace_handle;
46 virtual bool mock_init_anonymous_namespace(bool bridged, const char* sonames,
47 const char* search_paths) = 0;
48 virtual mock_namespace_handle mock_create_namespace(
49 bool bridged, const char* name, const char* ld_library_path, const char* default_library_path,
50 uint64_t type, const char* permitted_when_isolated_path, mock_namespace_handle parent) = 0;
51 virtual bool mock_link_namespaces(bool bridged, mock_namespace_handle from,
52 mock_namespace_handle to, const char* sonames) = 0;
53 virtual mock_namespace_handle mock_get_exported_namespace(bool bridged, const char* name) = 0;
54 virtual void* mock_dlopen_ext(bool bridged, const char* filename, int flags,
55 mock_namespace_handle ns) = 0;
56
57 // libnativebridge APIs for which libdl_android has no corresponding APIs
58 virtual bool NativeBridgeInitialized() = 0;
59 virtual const char* NativeBridgeGetError() = 0;
60 virtual bool NativeBridgeIsPathSupported(const char*) = 0;
61 virtual bool NativeBridgeIsSupported(const char*) = 0;
62
63 // To mock "ClassLoader Object.getParent()"
64 virtual const char* JniObject_getParent(const char*) = 0;
65};
66
67// The mock does not actually create a namespace object. But simply casts the pointer to the
68// string for the namespace name as the handle to the namespace object.
69#define TO_ANDROID_NAMESPACE(str) \
70 reinterpret_cast<struct android_namespace_t*>(const_cast<char*>(str))
71
72#define TO_BRIDGED_NAMESPACE(str) \
73 reinterpret_cast<struct native_bridge_namespace_t*>(const_cast<char*>(str))
74
75#define TO_MOCK_NAMESPACE(ns) reinterpret_cast<Platform::mock_namespace_handle>(ns)
76
77// These represents built-in namespaces created by the linker according to ld.config.txt
78static std::unordered_map<std::string, Platform::mock_namespace_handle> namespaces = {
79#define NAMESPACE_ENTRY(ns) {ns, TO_MOCK_NAMESPACE(TO_ANDROID_NAMESPACE(ns))}
80 NAMESPACE_ENTRY("com_android_i18n"),
81 NAMESPACE_ENTRY("com_android_neuralnetworks"),
82 NAMESPACE_ENTRY("com_android_art"),
83 NAMESPACE_ENTRY("default"),
84 NAMESPACE_ENTRY("sphal"),
85 NAMESPACE_ENTRY("system"),
86 NAMESPACE_ENTRY("vndk"),
87 NAMESPACE_ENTRY("vndk_product"),
88#undef NAMESPACE_ENTRY
89};
90
91// The actual gmock object
92class MockPlatform : public Platform {
93 public:
94 explicit MockPlatform(bool is_bridged) : is_bridged_(is_bridged) {
95 ON_CALL(*this, NativeBridgeIsSupported(_)).WillByDefault(Return(is_bridged_));
96 ON_CALL(*this, NativeBridgeIsPathSupported(_)).WillByDefault(Return(is_bridged_));
97 ON_CALL(*this, mock_get_exported_namespace(_, _))
98 .WillByDefault(testing::Invoke([](bool, const char* name) -> mock_namespace_handle {
99 if (namespaces.find(name) != namespaces.end()) {
100 return namespaces[name];
101 }
102 std::string msg = android::base::StringPrintf("(namespace %s not found)", name);
103 // The strdup'ed string will leak, but the test is already failing if we get here.
104 return TO_MOCK_NAMESPACE(TO_ANDROID_NAMESPACE(strdup(msg.c_str())));
105 }));
106 }
107
108 // Mocking the common APIs
109 MOCK_METHOD3(mock_init_anonymous_namespace, bool(bool, const char*, const char*));
110 MOCK_METHOD7(mock_create_namespace,
111 mock_namespace_handle(bool, const char*, const char*, const char*, uint64_t,
112 const char*, mock_namespace_handle));
113 MOCK_METHOD4(mock_link_namespaces,
114 bool(bool, mock_namespace_handle, mock_namespace_handle, const char*));
115 MOCK_METHOD2(mock_get_exported_namespace, mock_namespace_handle(bool, const char*));
116 MOCK_METHOD4(mock_dlopen_ext, void*(bool, const char*, int, mock_namespace_handle));
117
118 // Mocking libnativebridge APIs
119 MOCK_METHOD0(NativeBridgeInitialized, bool());
120 MOCK_METHOD0(NativeBridgeGetError, const char*());
121 MOCK_METHOD1(NativeBridgeIsPathSupported, bool(const char*));
122 MOCK_METHOD1(NativeBridgeIsSupported, bool(const char*));
123
124 // Mocking "ClassLoader Object.getParent()"
125 MOCK_METHOD1(JniObject_getParent, const char*(const char*));
126
127 private:
128 bool is_bridged_;
129};
130
131static std::unique_ptr<MockPlatform> mock;
132
133// Provide C wrappers for the mock object. These symbols must be exported by ld
134// to be able to override the real symbols in the shared libs.
135extern "C" {
136
137// libdl_android APIs
138
139bool android_init_anonymous_namespace(const char* sonames, const char* search_path) {
140 return mock->mock_init_anonymous_namespace(false, sonames, search_path);
141}
142
143struct android_namespace_t* android_create_namespace(const char* name, const char* ld_library_path,
144 const char* default_library_path,
145 uint64_t type,
146 const char* permitted_when_isolated_path,
147 struct android_namespace_t* parent) {
148 return TO_ANDROID_NAMESPACE(
149 mock->mock_create_namespace(false, name, ld_library_path, default_library_path, type,
150 permitted_when_isolated_path, TO_MOCK_NAMESPACE(parent)));
151}
152
153bool android_link_namespaces(struct android_namespace_t* from, struct android_namespace_t* to,
154 const char* sonames) {
155 return mock->mock_link_namespaces(false, TO_MOCK_NAMESPACE(from), TO_MOCK_NAMESPACE(to), sonames);
156}
157
158struct android_namespace_t* android_get_exported_namespace(const char* name) {
159 return TO_ANDROID_NAMESPACE(mock->mock_get_exported_namespace(false, name));
160}
161
162void* android_dlopen_ext(const char* filename, int flags, const android_dlextinfo* info) {
163 return mock->mock_dlopen_ext(false, filename, flags, TO_MOCK_NAMESPACE(info->library_namespace));
164}
165
166// libnativebridge APIs
167
168bool NativeBridgeIsSupported(const char* libpath) {
169 return mock->NativeBridgeIsSupported(libpath);
170}
171
172struct native_bridge_namespace_t* NativeBridgeGetExportedNamespace(const char* name) {
173 return TO_BRIDGED_NAMESPACE(mock->mock_get_exported_namespace(true, name));
174}
175
176struct native_bridge_namespace_t* NativeBridgeCreateNamespace(
177 const char* name, const char* ld_library_path, const char* default_library_path, uint64_t type,
178 const char* permitted_when_isolated_path, struct native_bridge_namespace_t* parent) {
179 return TO_BRIDGED_NAMESPACE(
180 mock->mock_create_namespace(true, name, ld_library_path, default_library_path, type,
181 permitted_when_isolated_path, TO_MOCK_NAMESPACE(parent)));
182}
183
184bool NativeBridgeLinkNamespaces(struct native_bridge_namespace_t* from,
185 struct native_bridge_namespace_t* to, const char* sonames) {
186 return mock->mock_link_namespaces(true, TO_MOCK_NAMESPACE(from), TO_MOCK_NAMESPACE(to), sonames);
187}
188
189void* NativeBridgeLoadLibraryExt(const char* libpath, int flag,
190 struct native_bridge_namespace_t* ns) {
191 return mock->mock_dlopen_ext(true, libpath, flag, TO_MOCK_NAMESPACE(ns));
192}
193
194bool NativeBridgeInitialized() {
195 return mock->NativeBridgeInitialized();
196}
197
198bool NativeBridgeInitAnonymousNamespace(const char* public_ns_sonames,
199 const char* anon_ns_library_path) {
200 return mock->mock_init_anonymous_namespace(true, public_ns_sonames, anon_ns_library_path);
201}
202
203const char* NativeBridgeGetError() {
204 return mock->NativeBridgeGetError();
205}
206
207bool NativeBridgeIsPathSupported(const char* path) {
208 return mock->NativeBridgeIsPathSupported(path);
209}
210
211} // extern "C"
212
213// A very simple JNI mock.
214// jstring is a pointer to utf8 char array. We don't need utf16 char here.
215// jobject, jclass, and jmethodID are also a pointer to utf8 char array
216// Only a few JNI methods that are actually used in libnativeloader are mocked.
217JNINativeInterface* CreateJNINativeInterface() {
218 JNINativeInterface* inf = new JNINativeInterface();
219 memset(inf, 0, sizeof(JNINativeInterface));
220
221 inf->GetStringUTFChars = [](JNIEnv*, jstring s, jboolean*) -> const char* {
222 return reinterpret_cast<const char*>(s);
223 };
224
225 inf->ReleaseStringUTFChars = [](JNIEnv*, jstring, const char*) -> void { return; };
226
227 inf->NewStringUTF = [](JNIEnv*, const char* bytes) -> jstring {
228 return reinterpret_cast<jstring>(const_cast<char*>(bytes));
229 };
230
231 inf->FindClass = [](JNIEnv*, const char* name) -> jclass {
232 return reinterpret_cast<jclass>(const_cast<char*>(name));
233 };
234
235 inf->CallObjectMethodV = [](JNIEnv*, jobject obj, jmethodID mid, va_list) -> jobject {
236 if (strcmp("getParent", reinterpret_cast<const char*>(mid)) == 0) {
237 // JniObject_getParent can be a valid jobject or nullptr if there is
238 // no parent classloader.
239 const char* ret = mock->JniObject_getParent(reinterpret_cast<const char*>(obj));
240 return reinterpret_cast<jobject>(const_cast<char*>(ret));
241 }
242 return nullptr;
243 };
244
245 inf->GetMethodID = [](JNIEnv*, jclass, const char* name, const char*) -> jmethodID {
246 return reinterpret_cast<jmethodID>(const_cast<char*>(name));
247 };
248
249 inf->NewWeakGlobalRef = [](JNIEnv*, jobject obj) -> jobject { return obj; };
250
251 inf->IsSameObject = [](JNIEnv*, jobject a, jobject b) -> jboolean {
252 return strcmp(reinterpret_cast<const char*>(a), reinterpret_cast<const char*>(b)) == 0;
253 };
254
255 return inf;
256}
257
258} // namespace nativeloader
259} // namespace android
260
261#endif // ART_LIBNATIVELOADER_NATIVE_LOADER_TEST_H_