blob: 0e2fa5de58cffc7e4df5ed2ae0f4ee83b2581567 [file] [log] [blame]
Martin Stjernholm8701bf52024-02-13 16:38:49 +00001/*
2 * Copyright (C) 2024 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
Martin Stjernholm8701bf52024-02-13 16:38:49 +000017#include "library_namespaces.h"
18
19#include "android-base/result-gmock.h"
20#include "gtest/gtest.h"
Martin Stjernholm62fc7ea2024-02-21 18:56:37 +000021#include "public_libraries.h"
Martin Stjernholm8701bf52024-02-13 16:38:49 +000022
23namespace android {
24namespace nativeloader {
25namespace {
26
27using ::android::base::testing::HasError;
28using ::android::base::testing::HasValue;
29using ::android::base::testing::WithMessage;
30using ::testing::StartsWith;
31
Martin Stjernholm899c4292024-01-29 19:57:04 +000032static ApiDomain GetProductApiDomain(ApiDomain fallback_domain) {
Martin Stjernholm62fc7ea2024-02-21 18:56:37 +000033 // GetApiDomainFromPath returns API_DOMAIN_PRODUCT only if the device is
34 // trebleized and has an unbundled product partition.
Martin Stjernholm899c4292024-01-29 19:57:04 +000035 return is_product_treblelized() ? API_DOMAIN_PRODUCT : fallback_domain;
36}
Martin Stjernholm62fc7ea2024-02-21 18:56:37 +000037
Martin Stjernholm899c4292024-01-29 19:57:04 +000038TEST(LibraryNamespacesTest, TestGetApiDomainFromPath) {
Martin Stjernholm8701bf52024-02-13 16:38:49 +000039 EXPECT_EQ(GetApiDomainFromPath("/data/somewhere"), API_DOMAIN_DEFAULT);
Martin Stjernholm899c4292024-01-29 19:57:04 +000040 EXPECT_EQ(GetApiDomainFromPath("/system/somewhere"), API_DOMAIN_SYSTEM);
41 EXPECT_EQ(GetApiDomainFromPath("/system_ext/somewhere"), API_DOMAIN_SYSTEM);
42 EXPECT_EQ(GetApiDomainFromPath("/systemext/somewhere"), API_DOMAIN_DEFAULT);
43 EXPECT_EQ(GetApiDomainFromPath("/product/somewhere"), GetProductApiDomain(API_DOMAIN_DEFAULT));
Martin Stjernholm8701bf52024-02-13 16:38:49 +000044 EXPECT_EQ(GetApiDomainFromPath("/vendor/somewhere"), API_DOMAIN_VENDOR);
Martin Stjernholm899c4292024-01-29 19:57:04 +000045 EXPECT_EQ(GetApiDomainFromPath("/system/product/somewhere"),
46 GetProductApiDomain(API_DOMAIN_SYSTEM));
Martin Stjernholm8701bf52024-02-13 16:38:49 +000047 EXPECT_EQ(GetApiDomainFromPath("/system/vendor/somewhere"), API_DOMAIN_VENDOR);
48
49 EXPECT_EQ(GetApiDomainFromPath(""), API_DOMAIN_DEFAULT);
50 EXPECT_EQ(GetApiDomainFromPath("/"), API_DOMAIN_DEFAULT);
51 EXPECT_EQ(GetApiDomainFromPath("product/somewhere"), API_DOMAIN_DEFAULT);
52 EXPECT_EQ(GetApiDomainFromPath("/product"), API_DOMAIN_DEFAULT);
Martin Stjernholm899c4292024-01-29 19:57:04 +000053 EXPECT_EQ(GetApiDomainFromPath("/product/"), GetProductApiDomain(API_DOMAIN_DEFAULT));
Martin Stjernholm8701bf52024-02-13 16:38:49 +000054 EXPECT_EQ(GetApiDomainFromPath(":/product/"), API_DOMAIN_DEFAULT);
55
56 EXPECT_EQ(GetApiDomainFromPath("/data/somewhere:/product/somewhere"), API_DOMAIN_DEFAULT);
57 EXPECT_EQ(GetApiDomainFromPath("/vendor/somewhere:/product/somewhere"), API_DOMAIN_VENDOR);
Martin Stjernholm899c4292024-01-29 19:57:04 +000058 EXPECT_EQ(GetApiDomainFromPath("/product/somewhere:/vendor/somewhere"),
59 GetProductApiDomain(API_DOMAIN_DEFAULT));
Martin Stjernholm8701bf52024-02-13 16:38:49 +000060}
61
62TEST(LibraryNamespacesTest, TestGetApiDomainFromPathList) {
63 EXPECT_THAT(GetApiDomainFromPathList("/data/somewhere"), HasValue(API_DOMAIN_DEFAULT));
Becky Siegelc35eef72024-02-23 20:04:52 +000064 EXPECT_THAT(GetApiDomainFromPathList("/system/somewhere"), HasValue(API_DOMAIN_DEFAULT));
Martin Stjernholm899c4292024-01-29 19:57:04 +000065 EXPECT_THAT(GetApiDomainFromPathList("/system_ext/somewhere"), HasValue(API_DOMAIN_DEFAULT));
66 EXPECT_THAT(GetApiDomainFromPathList("/product/somewhere"),
67 HasValue(GetProductApiDomain(API_DOMAIN_DEFAULT)));
Martin Stjernholm8701bf52024-02-13 16:38:49 +000068 EXPECT_THAT(GetApiDomainFromPathList("/vendor/somewhere"), HasValue(API_DOMAIN_VENDOR));
Martin Stjernholm899c4292024-01-29 19:57:04 +000069 EXPECT_THAT(GetApiDomainFromPathList("/system/product/somewhere"),
70 HasValue(GetProductApiDomain(API_DOMAIN_DEFAULT)));
Martin Stjernholm8701bf52024-02-13 16:38:49 +000071 EXPECT_THAT(GetApiDomainFromPathList("/system/vendor/somewhere"), HasValue(API_DOMAIN_VENDOR));
72
73 EXPECT_THAT(GetApiDomainFromPathList(""), HasValue(API_DOMAIN_DEFAULT));
74 EXPECT_THAT(GetApiDomainFromPathList(":"), HasValue(API_DOMAIN_DEFAULT));
75 EXPECT_THAT(GetApiDomainFromPathList(":/vendor/somewhere"), HasValue(API_DOMAIN_VENDOR));
76 EXPECT_THAT(GetApiDomainFromPathList("/vendor/somewhere:"), HasValue(API_DOMAIN_VENDOR));
77
78 EXPECT_THAT(GetApiDomainFromPathList("/data/somewhere:/product/somewhere"),
Martin Stjernholm899c4292024-01-29 19:57:04 +000079 HasValue(GetProductApiDomain(API_DOMAIN_DEFAULT)));
80 EXPECT_THAT(GetApiDomainFromPathList("/system/somewhere:/product/somewhere"),
81 HasValue(GetProductApiDomain(API_DOMAIN_DEFAULT)));
82 EXPECT_THAT(GetApiDomainFromPathList("/product/somewhere:/system/somewhere"),
83 HasValue(GetProductApiDomain(API_DOMAIN_DEFAULT)));
84 EXPECT_THAT(GetApiDomainFromPathList("/data/somewhere:/vendor/somewhere"),
85 HasValue(API_DOMAIN_VENDOR));
86 EXPECT_THAT(GetApiDomainFromPathList("/system/somewhere:/vendor/somewhere"),
87 HasValue(API_DOMAIN_VENDOR));
88 EXPECT_THAT(GetApiDomainFromPathList("/vendor/somewhere:/system/somewhere"),
89 HasValue(API_DOMAIN_VENDOR));
90
91 if (GetProductApiDomain(API_DOMAIN_DEFAULT) == API_DOMAIN_PRODUCT) {
92 EXPECT_THAT(
93 GetApiDomainFromPathList("/vendor/somewhere:/product/somewhere"),
94 HasError(WithMessage(StartsWith("Path list crosses vendor/product partition boundaries"))));
95 EXPECT_THAT(
96 GetApiDomainFromPathList("/system/somewhere:/product/somewhere:/vendor/somewhere"),
97 HasError(WithMessage(StartsWith("Path list crosses vendor/product partition boundaries"))));
Martin Stjernholm62fc7ea2024-02-21 18:56:37 +000098 }
Martin Stjernholm8701bf52024-02-13 16:38:49 +000099}
100
Martin Stjernholm7d4ebce2024-06-18 12:32:03 +0100101TEST(LibraryNamespacesTest, TestIsPartitionNativeLibPath) {
102 EXPECT_TRUE(IsPartitionNativeLibPath("/system/lib/libfoo.so"));
103 EXPECT_TRUE(IsPartitionNativeLibPath("/system/lib64/libfoo.so"));
104 EXPECT_TRUE(IsPartitionNativeLibPath("/system_ext/lib64/libfoo.so"));
105 EXPECT_TRUE(IsPartitionNativeLibPath("/product/lib64/libfoo.so"));
106 EXPECT_TRUE(IsPartitionNativeLibPath("/vendor/lib64/libfoo.so"));
107 EXPECT_TRUE(IsPartitionNativeLibPath("/vendor/lib64/hw/libhw.so"));
108 EXPECT_TRUE(IsPartitionNativeLibPath("/system/system_ext/lib64/libfoo.so"));
109 EXPECT_TRUE(IsPartitionNativeLibPath("/system/product/lib64/libfoo.so"));
110 EXPECT_TRUE(IsPartitionNativeLibPath("/system/vendor/lib64/libfoo.so"));
111
112 EXPECT_FALSE(IsPartitionNativeLibPath("/data/app/~~hash==/myapp-hash==/lib/arm64/libapp.so"));
113 EXPECT_FALSE(
114 IsPartitionNativeLibPath("/system/app/PrintSpooler/lib/arm64/libprintspooler_jni.so"));
115 EXPECT_FALSE(IsPartitionNativeLibPath("/product/app/Camera2/lib/arm64/libjni_jpegutil.so"));
116}
117
Martin Stjernholm8701bf52024-02-13 16:38:49 +0000118} // namespace
119} // namespace nativeloader
120} // namespace android