1 /*
2 * Copyright (C) 2019 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 #if defined(ART_TARGET_ANDROID)
18
19 #include "native_loader_test.h"
20
21 #include <dlfcn.h>
22
23 #include <android-base/strings.h>
24 #include <gtest/gtest.h>
25
26 #include "nativehelper/scoped_utf_chars.h"
27 #include "nativeloader/native_loader.h"
28 #include "public_libraries.h"
29
30 namespace android {
31 namespace nativeloader {
32
33 using ::testing::Eq;
34 using ::testing::NotNull;
35 using ::testing::StrEq;
36 using internal::ConfigEntry;
37 using internal::ParseApexLibrariesConfig;
38 using internal::ParseConfig;
39
40 #if defined(__LP64__)
41 #define LIB_DIR "lib64"
42 #else
43 #define LIB_DIR "lib"
44 #endif
45
46 static void* const any_nonnull = reinterpret_cast<void*>(0x12345678);
47
48 // Custom matcher for comparing namespace handles
49 MATCHER_P(NsEq, other, "") {
50 *result_listener << "comparing " << reinterpret_cast<const char*>(arg) << " and " << other;
51 return strcmp(reinterpret_cast<const char*>(arg), reinterpret_cast<const char*>(other)) == 0;
52 }
53
54 /////////////////////////////////////////////////////////////////
55
56 // Test fixture
57 class NativeLoaderTest : public ::testing::TestWithParam<bool> {
58 protected:
IsBridged()59 bool IsBridged() { return GetParam(); }
60
SetUp()61 void SetUp() override {
62 mock = std::make_unique<testing::NiceMock<MockPlatform>>(IsBridged());
63
64 env = std::make_unique<JNIEnv>();
65 env->functions = CreateJNINativeInterface();
66 }
67
SetExpectations()68 void SetExpectations() {
69 std::vector<std::string> default_public_libs =
70 android::base::Split(preloadable_public_libraries(), ":");
71 for (auto l : default_public_libs) {
72 EXPECT_CALL(*mock,
73 mock_dlopen_ext(false, StrEq(l.c_str()), RTLD_NOW | RTLD_NODELETE, NotNull()))
74 .WillOnce(Return(any_nonnull));
75 }
76 }
77
RunTest()78 void RunTest() { InitializeNativeLoader(); }
79
TearDown()80 void TearDown() override {
81 ResetNativeLoader();
82 delete env->functions;
83 mock.reset();
84 }
85
86 std::unique_ptr<JNIEnv> env;
87 };
88
89 /////////////////////////////////////////////////////////////////
90
TEST_P(NativeLoaderTest,InitializeLoadsDefaultPublicLibraries)91 TEST_P(NativeLoaderTest, InitializeLoadsDefaultPublicLibraries) {
92 SetExpectations();
93 RunTest();
94 }
95
TEST_P(NativeLoaderTest,OpenNativeLibraryWithoutClassloaderInApex)96 TEST_P(NativeLoaderTest, OpenNativeLibraryWithoutClassloaderInApex) {
97 const char* test_lib_path = "libfoo.so";
98 void* fake_handle = &fake_handle; // Arbitrary non-null value
99 EXPECT_CALL(*mock,
100 mock_dlopen_ext(false, StrEq(test_lib_path), RTLD_NOW, NsEq("com_android_art")))
101 .WillOnce(Return(fake_handle));
102
103 bool needs_native_bridge = false;
104 char* errmsg = nullptr;
105 EXPECT_EQ(fake_handle,
106 OpenNativeLibrary(env.get(),
107 /*target_sdk_version=*/17,
108 test_lib_path,
109 /*class_loader=*/nullptr,
110 /*caller_location=*/"/apex/com.android.art/javalib/myloadinglib.jar",
111 /*library_path=*/nullptr,
112 &needs_native_bridge,
113 &errmsg));
114 // OpenNativeLibrary never uses nativebridge when there's no classloader. That
115 // should maybe change.
116 EXPECT_EQ(needs_native_bridge, false);
117 EXPECT_EQ(errmsg, nullptr);
118 }
119
TEST_P(NativeLoaderTest,OpenNativeLibraryWithoutClassloaderInFramework)120 TEST_P(NativeLoaderTest, OpenNativeLibraryWithoutClassloaderInFramework) {
121 const char* test_lib_path = "libfoo.so";
122 void* fake_handle = &fake_handle; // Arbitrary non-null value
123 EXPECT_CALL(*mock, mock_dlopen_ext(false, StrEq(test_lib_path), RTLD_NOW, NsEq("system")))
124 .WillOnce(Return(fake_handle));
125
126 bool needs_native_bridge = false;
127 char* errmsg = nullptr;
128 EXPECT_EQ(fake_handle,
129 OpenNativeLibrary(env.get(),
130 /*target_sdk_version=*/17,
131 test_lib_path,
132 /*class_loader=*/nullptr,
133 /*caller_location=*/"/system/framework/framework.jar!classes1.dex",
134 /*library_path=*/nullptr,
135 &needs_native_bridge,
136 &errmsg));
137 // OpenNativeLibrary never uses nativebridge when there's no classloader. That
138 // should maybe change.
139 EXPECT_EQ(needs_native_bridge, false);
140 EXPECT_EQ(errmsg, nullptr);
141 }
142
TEST_P(NativeLoaderTest,OpenNativeLibraryWithoutClassloaderAndCallerLocation)143 TEST_P(NativeLoaderTest, OpenNativeLibraryWithoutClassloaderAndCallerLocation) {
144 const char* test_lib_path = "libfoo.so";
145 void* fake_handle = &fake_handle; // Arbitrary non-null value
146 EXPECT_CALL(*mock, mock_dlopen_ext(false, StrEq(test_lib_path), RTLD_NOW, NsEq("system")))
147 .WillOnce(Return(fake_handle));
148
149 bool needs_native_bridge = false;
150 char* errmsg = nullptr;
151 EXPECT_EQ(fake_handle,
152 OpenNativeLibrary(env.get(),
153 /*target_sdk_version=*/17,
154 test_lib_path,
155 /*class_loader=*/nullptr,
156 /*caller_location=*/nullptr,
157 /*library_path=*/nullptr,
158 &needs_native_bridge,
159 &errmsg));
160 // OpenNativeLibrary never uses nativebridge when there's no classloader. That
161 // should maybe change.
162 EXPECT_EQ(needs_native_bridge, false);
163 EXPECT_EQ(errmsg, nullptr);
164 }
165
166 INSTANTIATE_TEST_SUITE_P(NativeLoaderTests, NativeLoaderTest, testing::Bool());
167
168 /////////////////////////////////////////////////////////////////
169
170 class NativeLoaderTest_Create : public NativeLoaderTest {
171 protected:
172 // Test inputs (initialized to the default values). Overriding these
173 // must be done before calling SetExpectations() and RunTest().
174 uint32_t target_sdk_version = 29;
175 std::string class_loader = "my_classloader";
176 bool is_shared = false;
177 std::string dex_path = "/data/app/foo/classes.dex";
178 std::string library_path = "/data/app/foo/" LIB_DIR "/arm";
179 std::string permitted_path = "/data/app/foo/" LIB_DIR;
180
181 // expected output (.. for the default test inputs)
182 std::string expected_namespace_name = "classloader-namespace";
183 uint64_t expected_namespace_flags =
184 ANDROID_NAMESPACE_TYPE_ISOLATED | ANDROID_NAMESPACE_TYPE_ALSO_USED_AS_ANONYMOUS;
185 std::string expected_library_path = library_path;
186 std::string expected_permitted_path = std::string("/data:/mnt/expand:") + permitted_path;
187 std::string expected_parent_namespace = "system";
188 bool expected_link_with_platform_ns = true;
189 bool expected_link_with_art_ns = true;
190 bool expected_link_with_i18n_ns = true;
191 bool expected_link_with_conscrypt_ns = false;
192 bool expected_link_with_sphal_ns = !vendor_public_libraries().empty();
193 bool expected_link_with_vndk_ns = false;
194 bool expected_link_with_vndk_product_ns = false;
195 bool expected_link_with_default_ns = false;
196 bool expected_link_with_neuralnetworks_ns = true;
197 std::string expected_shared_libs_to_platform_ns = default_public_libraries();
198 std::string expected_shared_libs_to_art_ns = apex_public_libraries().at("com_android_art");
199 std::string expected_shared_libs_to_i18n_ns = apex_public_libraries().at("com_android_i18n");
200 std::string expected_shared_libs_to_conscrypt_ns = apex_jni_libraries("com_android_conscrypt");
201 std::string expected_shared_libs_to_sphal_ns = vendor_public_libraries();
202 std::string expected_shared_libs_to_vndk_ns = vndksp_libraries_vendor();
203 std::string expected_shared_libs_to_vndk_product_ns = vndksp_libraries_product();
204 std::string expected_shared_libs_to_default_ns = default_public_libraries();
205 std::string expected_shared_libs_to_neuralnetworks_ns = apex_public_libraries().at("com_android_neuralnetworks");
206
SetExpectations()207 void SetExpectations() {
208 NativeLoaderTest::SetExpectations();
209
210 ON_CALL(*mock, JniObject_getParent(StrEq(class_loader))).WillByDefault(Return(nullptr));
211
212 EXPECT_CALL(*mock, NativeBridgeIsPathSupported(_)).Times(testing::AnyNumber());
213 EXPECT_CALL(*mock, NativeBridgeInitialized()).Times(testing::AnyNumber());
214
215 EXPECT_CALL(*mock, mock_create_namespace(
216 Eq(IsBridged()), StrEq(expected_namespace_name), nullptr,
217 StrEq(expected_library_path), expected_namespace_flags,
218 StrEq(expected_permitted_path), NsEq(expected_parent_namespace.c_str())))
219 .WillOnce(Return(TO_MOCK_NAMESPACE(TO_ANDROID_NAMESPACE(dex_path.c_str()))));
220 if (expected_link_with_platform_ns) {
221 EXPECT_CALL(*mock, mock_link_namespaces(Eq(IsBridged()), _, NsEq("system"),
222 StrEq(expected_shared_libs_to_platform_ns)))
223 .WillOnce(Return(true));
224 }
225 if (expected_link_with_art_ns) {
226 EXPECT_CALL(*mock, mock_link_namespaces(Eq(IsBridged()), _, NsEq("com_android_art"),
227 StrEq(expected_shared_libs_to_art_ns)))
228 .WillOnce(Return(true));
229 }
230 if (expected_link_with_i18n_ns) {
231 EXPECT_CALL(*mock, mock_link_namespaces(Eq(IsBridged()), _, NsEq("com_android_i18n"),
232 StrEq(expected_shared_libs_to_i18n_ns)))
233 .WillOnce(Return(true));
234 }
235 if (expected_link_with_sphal_ns) {
236 EXPECT_CALL(*mock, mock_link_namespaces(Eq(IsBridged()), _, NsEq("sphal"),
237 StrEq(expected_shared_libs_to_sphal_ns)))
238 .WillOnce(Return(true));
239 }
240 if (expected_link_with_vndk_ns) {
241 EXPECT_CALL(*mock, mock_link_namespaces(Eq(IsBridged()), _, NsEq("vndk"),
242 StrEq(expected_shared_libs_to_vndk_ns)))
243 .WillOnce(Return(true));
244 }
245 if (expected_link_with_vndk_product_ns) {
246 EXPECT_CALL(*mock, mock_link_namespaces(Eq(IsBridged()), _, NsEq("vndk_product"),
247 StrEq(expected_shared_libs_to_vndk_product_ns)))
248 .WillOnce(Return(true));
249 }
250 if (expected_link_with_default_ns) {
251 EXPECT_CALL(*mock, mock_link_namespaces(Eq(IsBridged()), _, NsEq("default"),
252 StrEq(expected_shared_libs_to_default_ns)))
253 .WillOnce(Return(true));
254 }
255 if (expected_link_with_neuralnetworks_ns) {
256 EXPECT_CALL(*mock, mock_link_namespaces(Eq(IsBridged()), _, NsEq("com_android_neuralnetworks"),
257 StrEq(expected_shared_libs_to_neuralnetworks_ns)))
258 .WillOnce(Return(true));
259 }
260 if (expected_link_with_conscrypt_ns) {
261 EXPECT_CALL(*mock, mock_link_namespaces(Eq(IsBridged()), _, NsEq("com_android_conscrypt"),
262 StrEq(expected_shared_libs_to_conscrypt_ns)))
263 .WillOnce(Return(true));
264 }
265 }
266
RunTest()267 void RunTest() {
268 NativeLoaderTest::RunTest();
269
270 jstring err = CreateClassLoaderNamespace(
271 env(), target_sdk_version, env()->NewStringUTF(class_loader.c_str()), is_shared,
272 env()->NewStringUTF(dex_path.c_str()), env()->NewStringUTF(library_path.c_str()),
273 env()->NewStringUTF(permitted_path.c_str()), /*uses_library_list=*/ nullptr);
274
275 // no error
276 EXPECT_EQ(err, nullptr) << "Error is: " << std::string(ScopedUtfChars(env(), err).c_str());
277
278 if (!IsBridged()) {
279 struct android_namespace_t* ns =
280 FindNamespaceByClassLoader(env(), env()->NewStringUTF(class_loader.c_str()));
281
282 // The created namespace is for this apk
283 EXPECT_EQ(dex_path.c_str(), reinterpret_cast<const char*>(ns));
284 } else {
285 struct NativeLoaderNamespace* ns =
286 FindNativeLoaderNamespaceByClassLoader(env(), env()->NewStringUTF(class_loader.c_str()));
287
288 // The created namespace is for the this apk
289 EXPECT_STREQ(dex_path.c_str(),
290 reinterpret_cast<const char*>(ns->ToRawNativeBridgeNamespace()));
291 }
292 }
293
env()294 JNIEnv* env() { return NativeLoaderTest::env.get(); }
295 };
296
TEST_P(NativeLoaderTest_Create,DownloadedApp)297 TEST_P(NativeLoaderTest_Create, DownloadedApp) {
298 SetExpectations();
299 RunTest();
300 }
301
TEST_P(NativeLoaderTest_Create,BundledSystemApp)302 TEST_P(NativeLoaderTest_Create, BundledSystemApp) {
303 dex_path = "/system/app/foo/foo.apk";
304 is_shared = true;
305
306 expected_namespace_name = "classloader-namespace-shared";
307 expected_namespace_flags |= ANDROID_NAMESPACE_TYPE_SHARED;
308 SetExpectations();
309 RunTest();
310 }
311
TEST_P(NativeLoaderTest_Create,BundledVendorApp)312 TEST_P(NativeLoaderTest_Create, BundledVendorApp) {
313 dex_path = "/vendor/app/foo/foo.apk";
314 is_shared = true;
315
316 expected_namespace_name = "classloader-namespace-shared";
317 expected_namespace_flags |= ANDROID_NAMESPACE_TYPE_SHARED;
318 SetExpectations();
319 RunTest();
320 }
321
TEST_P(NativeLoaderTest_Create,UnbundledVendorApp)322 TEST_P(NativeLoaderTest_Create, UnbundledVendorApp) {
323 dex_path = "/vendor/app/foo/foo.apk";
324 is_shared = false;
325
326 expected_namespace_name = "vendor-classloader-namespace";
327 expected_library_path = expected_library_path + ":/vendor/" LIB_DIR;
328 expected_permitted_path = expected_permitted_path + ":/vendor/" LIB_DIR;
329 expected_shared_libs_to_platform_ns =
330 expected_shared_libs_to_platform_ns + ":" + llndk_libraries_vendor();
331 expected_link_with_vndk_ns = true;
332 SetExpectations();
333 RunTest();
334 }
335
TEST_P(NativeLoaderTest_Create,BundledProductApp)336 TEST_P(NativeLoaderTest_Create, BundledProductApp) {
337 dex_path = "/product/app/foo/foo.apk";
338 is_shared = true;
339
340 expected_namespace_name = "classloader-namespace-shared";
341 expected_namespace_flags |= ANDROID_NAMESPACE_TYPE_SHARED;
342 SetExpectations();
343 RunTest();
344 }
345
TEST_P(NativeLoaderTest_Create,SystemServerWithApexJars)346 TEST_P(NativeLoaderTest_Create, SystemServerWithApexJars) {
347 dex_path = "/system/framework/services.jar:/apex/com.android.conscrypt/javalib/service-foo.jar";
348 is_shared = true;
349
350 expected_namespace_name = "classloader-namespace-shared";
351 expected_namespace_flags |= ANDROID_NAMESPACE_TYPE_SHARED;
352 expected_link_with_conscrypt_ns = true;
353 SetExpectations();
354 RunTest();
355 }
356
TEST_P(NativeLoaderTest_Create,UnbundledProductApp)357 TEST_P(NativeLoaderTest_Create, UnbundledProductApp) {
358 dex_path = "/product/app/foo/foo.apk";
359 is_shared = false;
360
361 if (is_product_vndk_version_defined()) {
362 expected_namespace_name = "vendor-classloader-namespace";
363 expected_library_path = expected_library_path + ":/product/" LIB_DIR ":/system/product/" LIB_DIR;
364 expected_permitted_path =
365 expected_permitted_path + ":/product/" LIB_DIR ":/system/product/" LIB_DIR;
366 expected_shared_libs_to_platform_ns =
367 expected_shared_libs_to_platform_ns + ":" + llndk_libraries_product();
368 expected_link_with_vndk_product_ns = true;
369 }
370 SetExpectations();
371 RunTest();
372 }
373
TEST_P(NativeLoaderTest_Create,NamespaceForSharedLibIsNotUsedAsAnonymousNamespace)374 TEST_P(NativeLoaderTest_Create, NamespaceForSharedLibIsNotUsedAsAnonymousNamespace) {
375 if (IsBridged()) {
376 // There is no shared lib in translated arch
377 // TODO(jiyong): revisit this
378 return;
379 }
380 // compared to apks, for java shared libs, library_path is empty; java shared
381 // libs don't have their own native libs. They use platform's.
382 library_path = "";
383 expected_library_path = library_path;
384 // no ALSO_USED_AS_ANONYMOUS
385 expected_namespace_flags = ANDROID_NAMESPACE_TYPE_ISOLATED;
386 SetExpectations();
387 RunTest();
388 }
389
TEST_P(NativeLoaderTest_Create,TwoApks)390 TEST_P(NativeLoaderTest_Create, TwoApks) {
391 SetExpectations();
392 const uint32_t second_app_target_sdk_version = 29;
393 const std::string second_app_class_loader = "second_app_classloader";
394 const bool second_app_is_shared = false;
395 const std::string second_app_dex_path = "/data/app/bar/classes.dex";
396 const std::string second_app_library_path = "/data/app/bar/" LIB_DIR "/arm";
397 const std::string second_app_permitted_path = "/data/app/bar/" LIB_DIR;
398 const std::string expected_second_app_permitted_path =
399 std::string("/data:/mnt/expand:") + second_app_permitted_path;
400 const std::string expected_second_app_parent_namespace = "classloader-namespace";
401 // no ALSO_USED_AS_ANONYMOUS
402 const uint64_t expected_second_namespace_flags = ANDROID_NAMESPACE_TYPE_ISOLATED;
403
404 // The scenario is that second app is loaded by the first app.
405 // So the first app's classloader (`classloader`) is parent of the second
406 // app's classloader.
407 ON_CALL(*mock, JniObject_getParent(StrEq(second_app_class_loader)))
408 .WillByDefault(Return(class_loader.c_str()));
409
410 // namespace for the second app is created. Its parent is set to the namespace
411 // of the first app.
412 EXPECT_CALL(*mock, mock_create_namespace(
413 Eq(IsBridged()), StrEq(expected_namespace_name), nullptr,
414 StrEq(second_app_library_path), expected_second_namespace_flags,
415 StrEq(expected_second_app_permitted_path), NsEq(dex_path.c_str())))
416 .WillOnce(Return(TO_MOCK_NAMESPACE(TO_ANDROID_NAMESPACE(second_app_dex_path.c_str()))));
417 EXPECT_CALL(*mock, mock_link_namespaces(Eq(IsBridged()), NsEq(second_app_dex_path.c_str()), _, _))
418 .WillRepeatedly(Return(true));
419
420 RunTest();
421 jstring err = CreateClassLoaderNamespace(
422 env(), second_app_target_sdk_version, env()->NewStringUTF(second_app_class_loader.c_str()),
423 second_app_is_shared, env()->NewStringUTF(second_app_dex_path.c_str()),
424 env()->NewStringUTF(second_app_library_path.c_str()),
425 env()->NewStringUTF(second_app_permitted_path.c_str()), /*uses_library_list=*/ nullptr);
426
427 // success
428 EXPECT_EQ(err, nullptr) << "Error is: " << std::string(ScopedUtfChars(env(), err).c_str());
429
430 if (!IsBridged()) {
431 struct android_namespace_t* ns =
432 FindNamespaceByClassLoader(env(), env()->NewStringUTF(second_app_class_loader.c_str()));
433
434 // The created namespace is for the second apk
435 EXPECT_EQ(second_app_dex_path.c_str(), reinterpret_cast<const char*>(ns));
436 } else {
437 struct NativeLoaderNamespace* ns = FindNativeLoaderNamespaceByClassLoader(
438 env(), env()->NewStringUTF(second_app_class_loader.c_str()));
439
440 // The created namespace is for the second apk
441 EXPECT_STREQ(second_app_dex_path.c_str(),
442 reinterpret_cast<const char*>(ns->ToRawNativeBridgeNamespace()));
443 }
444 }
445
446 INSTANTIATE_TEST_SUITE_P(NativeLoaderTests_Create, NativeLoaderTest_Create, testing::Bool());
447
448 const std::function<Result<bool>(const struct ConfigEntry&)> always_true =
__anon877c9db20102(const struct ConfigEntry&) 449 [](const struct ConfigEntry&) -> Result<bool> { return true; };
450
TEST(NativeLoaderConfigParser,NamesAndComments)451 TEST(NativeLoaderConfigParser, NamesAndComments) {
452 const char file_content[] = R"(
453 ######
454
455 libA.so
456 #libB.so
457
458
459 libC.so
460 libD.so
461 #### libE.so
462 )";
463 const std::vector<std::string> expected_result = {"libA.so", "libC.so", "libD.so"};
464 Result<std::vector<std::string>> result = ParseConfig(file_content, always_true);
465 ASSERT_RESULT_OK(result);
466 ASSERT_EQ(expected_result, *result);
467 }
468
TEST(NativeLoaderConfigParser,WithBitness)469 TEST(NativeLoaderConfigParser, WithBitness) {
470 const char file_content[] = R"(
471 libA.so 32
472 libB.so 64
473 libC.so
474 )";
475 #if defined(__LP64__)
476 const std::vector<std::string> expected_result = {"libB.so", "libC.so"};
477 #else
478 const std::vector<std::string> expected_result = {"libA.so", "libC.so"};
479 #endif
480 Result<std::vector<std::string>> result = ParseConfig(file_content, always_true);
481 ASSERT_RESULT_OK(result);
482 ASSERT_EQ(expected_result, *result);
483 }
484
TEST(NativeLoaderConfigParser,WithNoPreload)485 TEST(NativeLoaderConfigParser, WithNoPreload) {
486 const char file_content[] = R"(
487 libA.so nopreload
488 libB.so nopreload
489 libC.so
490 )";
491
492 const std::vector<std::string> expected_result = {"libC.so"};
493 Result<std::vector<std::string>> result =
494 ParseConfig(file_content,
495 [](const struct ConfigEntry& entry) -> Result<bool> { return !entry.nopreload; });
496 ASSERT_RESULT_OK(result);
497 ASSERT_EQ(expected_result, *result);
498 }
499
TEST(NativeLoaderConfigParser,WithNoPreloadAndBitness)500 TEST(NativeLoaderConfigParser, WithNoPreloadAndBitness) {
501 const char file_content[] = R"(
502 libA.so nopreload 32
503 libB.so 64 nopreload
504 libC.so 32
505 libD.so 64
506 libE.so nopreload
507 )";
508
509 #if defined(__LP64__)
510 const std::vector<std::string> expected_result = {"libD.so"};
511 #else
512 const std::vector<std::string> expected_result = {"libC.so"};
513 #endif
514 Result<std::vector<std::string>> result =
515 ParseConfig(file_content,
516 [](const struct ConfigEntry& entry) -> Result<bool> { return !entry.nopreload; });
517 ASSERT_RESULT_OK(result);
518 ASSERT_EQ(expected_result, *result);
519 }
520
TEST(NativeLoaderConfigParser,RejectMalformed)521 TEST(NativeLoaderConfigParser, RejectMalformed) {
522 ASSERT_FALSE(ParseConfig("libA.so 32 64", always_true).ok());
523 ASSERT_FALSE(ParseConfig("libA.so 32 32", always_true).ok());
524 ASSERT_FALSE(ParseConfig("libA.so 32 nopreload 64", always_true).ok());
525 ASSERT_FALSE(ParseConfig("32 libA.so nopreload", always_true).ok());
526 ASSERT_FALSE(ParseConfig("nopreload libA.so 32", always_true).ok());
527 ASSERT_FALSE(ParseConfig("libA.so nopreload # comment", always_true).ok());
528 }
529
TEST(NativeLoaderApexLibrariesConfigParser,BasicLoading)530 TEST(NativeLoaderApexLibrariesConfigParser, BasicLoading) {
531 const char file_content[] = R"(
532 # comment
533 jni com_android_foo libfoo.so
534 # Empty line is ignored
535
536 jni com_android_bar libbar.so:libbar2.so
537
538 public com_android_bar libpublic.so
539 )";
540
541 auto jni_libs = ParseApexLibrariesConfig(file_content, "jni");
542 ASSERT_RESULT_OK(jni_libs);
543 std::map<std::string, std::string> expected_jni_libs {
544 {"com_android_foo", "libfoo.so"},
545 {"com_android_bar", "libbar.so:libbar2.so"},
546 };
547 ASSERT_EQ(expected_jni_libs, *jni_libs);
548
549 auto public_libs = ParseApexLibrariesConfig(file_content, "public");
550 ASSERT_RESULT_OK(public_libs);
551 std::map<std::string, std::string> expected_public_libs {
552 {"com_android_bar", "libpublic.so"},
553 };
554 ASSERT_EQ(expected_public_libs, *public_libs);
555 }
556
TEST(NativeLoaderApexLibrariesConfigParser,RejectMalformedLine)557 TEST(NativeLoaderApexLibrariesConfigParser, RejectMalformedLine) {
558 const char file_content[] = R"(
559 jni com_android_foo libfoo
560 # missing <library list>
561 jni com_android_bar
562 )";
563 auto result = ParseApexLibrariesConfig(file_content, "jni");
564 ASSERT_FALSE(result.ok());
565 ASSERT_EQ("Malformed line \"jni com_android_bar\"", result.error().message());
566 }
567
TEST(NativeLoaderApexLibrariesConfigParser,RejectInvalidTag)568 TEST(NativeLoaderApexLibrariesConfigParser, RejectInvalidTag) {
569 const char file_content[] = R"(
570 jni apex1 lib
571 public apex2 lib
572 # unknown tag
573 unknown com_android_foo libfoo
574 )";
575 auto result = ParseApexLibrariesConfig(file_content, "jni");
576 ASSERT_FALSE(result.ok());
577 ASSERT_EQ("Invalid tag \"unknown com_android_foo libfoo\"", result.error().message());
578 }
579
TEST(NativeLoaderApexLibrariesConfigParser,RejectInvalidApexNamespace)580 TEST(NativeLoaderApexLibrariesConfigParser, RejectInvalidApexNamespace) {
581 const char file_content[] = R"(
582 # apex linker namespace should be mangled ('.' -> '_')
583 jni com.android.foo lib
584 )";
585 auto result = ParseApexLibrariesConfig(file_content, "jni");
586 ASSERT_FALSE(result.ok());
587 ASSERT_EQ("Invalid apex_namespace \"jni com.android.foo lib\"", result.error().message());
588 }
589
TEST(NativeLoaderApexLibrariesConfigParser,RejectInvalidLibraryList)590 TEST(NativeLoaderApexLibrariesConfigParser, RejectInvalidLibraryList) {
591 const char file_content[] = R"(
592 # library list is ":" separated list of filenames
593 jni com_android_foo lib64/libfoo.so
594 )";
595 auto result = ParseApexLibrariesConfig(file_content, "jni");
596 ASSERT_FALSE(result.ok());
597 ASSERT_EQ("Invalid library_list \"jni com_android_foo lib64/libfoo.so\"", result.error().message());
598 }
599
600 } // namespace nativeloader
601 } // namespace android
602
603 #endif // defined(ART_TARGET_ANDROID)
604