1 /*
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
17 #include "library_namespaces.h"
18
19 #include "android-base/result-gmock.h"
20 #include "gtest/gtest.h"
21 #include "public_libraries.h"
22
23 namespace android {
24 namespace nativeloader {
25 namespace {
26
27 using ::android::base::testing::HasError;
28 using ::android::base::testing::HasValue;
29 using ::android::base::testing::WithMessage;
30 using ::testing::StartsWith;
31
GetProductApiDomain(ApiDomain fallback_domain)32 static ApiDomain GetProductApiDomain(ApiDomain fallback_domain) {
33 // GetApiDomainFromPath returns API_DOMAIN_PRODUCT only if the device is
34 // trebleized and has an unbundled product partition.
35 return is_product_treblelized() ? API_DOMAIN_PRODUCT : fallback_domain;
36 }
37
TEST(LibraryNamespacesTest,TestGetApiDomainFromPath)38 TEST(LibraryNamespacesTest, TestGetApiDomainFromPath) {
39 EXPECT_EQ(GetApiDomainFromPath("/data/somewhere"), API_DOMAIN_DEFAULT);
40 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));
44 EXPECT_EQ(GetApiDomainFromPath("/vendor/somewhere"), API_DOMAIN_VENDOR);
45 EXPECT_EQ(GetApiDomainFromPath("/system/product/somewhere"),
46 GetProductApiDomain(API_DOMAIN_SYSTEM));
47 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);
53 EXPECT_EQ(GetApiDomainFromPath("/product/"), GetProductApiDomain(API_DOMAIN_DEFAULT));
54 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);
58 EXPECT_EQ(GetApiDomainFromPath("/product/somewhere:/vendor/somewhere"),
59 GetProductApiDomain(API_DOMAIN_DEFAULT));
60 }
61
TEST(LibraryNamespacesTest,TestGetApiDomainFromPathList)62 TEST(LibraryNamespacesTest, TestGetApiDomainFromPathList) {
63 EXPECT_THAT(GetApiDomainFromPathList("/data/somewhere"), HasValue(API_DOMAIN_DEFAULT));
64 EXPECT_THAT(GetApiDomainFromPathList("/system/somewhere"), HasValue(API_DOMAIN_DEFAULT));
65 EXPECT_THAT(GetApiDomainFromPathList("/system_ext/somewhere"), HasValue(API_DOMAIN_DEFAULT));
66 EXPECT_THAT(GetApiDomainFromPathList("/product/somewhere"),
67 HasValue(GetProductApiDomain(API_DOMAIN_DEFAULT)));
68 EXPECT_THAT(GetApiDomainFromPathList("/vendor/somewhere"), HasValue(API_DOMAIN_VENDOR));
69 EXPECT_THAT(GetApiDomainFromPathList("/system/product/somewhere"),
70 HasValue(GetProductApiDomain(API_DOMAIN_DEFAULT)));
71 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"),
79 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"))));
98 }
99 }
100
101 } // namespace
102 } // namespace nativeloader
103 } // namespace android
104