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 #include <gtest/gtest.h>
18 
19 #include "linkerconfig/apex.h"
20 #include "linkerconfig/baseconfig.h"
21 #include "linkerconfig/variables.h"
22 #include "testbase.h"
23 
24 using android::linkerconfig::contents::Context;
25 using android::linkerconfig::modules::ApexInfo;
26 using android::linkerconfig::modules::AsanPath;
27 
28 struct linkerconfig_vndklite_backward_compatibility : ::testing::Test {
29   void SetUp() override {
30     MockVariables();
31     MockVnkdLite();
32     ApexInfo vndk_apex;
33     vndk_apex.name = "com.android.vndk.vQ";
34     ctx.AddApexModule(vndk_apex);
35   }
36   Context ctx;
37 };
38 
39 TEST_F(linkerconfig_vndklite_backward_compatibility, system_section) {
40   auto config = android::linkerconfig::contents::CreateBaseConfiguration(ctx);
41 
42   auto system_section = config.GetSection("system");
43   ASSERT_TRUE(system_section);
44 
45   auto default_namespace = system_section->GetNamespace("default");
46   ASSERT_TRUE(default_namespace);
47 
48   EXPECT_TRUE(default_namespace->ContainsSearchPath("/vendor/${LIB}",
49                                                     AsanPath::WITH_DATA_ASAN));
50   EXPECT_TRUE(default_namespace->ContainsSearchPath("/odm/${LIB}",
51                                                     AsanPath::WITH_DATA_ASAN));
52 
53   auto sphal_namespace = system_section->GetNamespace("sphal");
54   ASSERT_TRUE(sphal_namespace);
55 
56   EXPECT_TRUE(sphal_namespace->ContainsSearchPath("/odm/${LIB}",
57                                                   AsanPath::WITH_DATA_ASAN));
58   EXPECT_TRUE(sphal_namespace->ContainsSearchPath("/vendor/${LIB}",
59                                                   AsanPath::WITH_DATA_ASAN));
60   EXPECT_TRUE(
61       sphal_namespace->ContainsSearchPath("/vendor/${LIB}/hw", AsanPath::NONE));
62 
63   EXPECT_TRUE(sphal_namespace->ContainsPermittedPath("/odm/${LIB}",
64                                                      AsanPath::WITH_DATA_ASAN));
65   EXPECT_TRUE(sphal_namespace->ContainsPermittedPath("/vendor/${LIB}",
66                                                      AsanPath::WITH_DATA_ASAN));
67   EXPECT_TRUE(sphal_namespace->ContainsPermittedPath("/system/vendor/${LIB}",
68                                                      AsanPath::NONE));
69 
70   auto rs_namespace = system_section->GetNamespace("rs");
71   ASSERT_TRUE(rs_namespace);
72 
73   EXPECT_TRUE(rs_namespace->ContainsSearchPath("/odm/${LIB}/vndk-sp",
74                                                AsanPath::WITH_DATA_ASAN));
75   EXPECT_TRUE(rs_namespace->ContainsSearchPath("/vendor/${LIB}/vndk-sp",
76                                                AsanPath::WITH_DATA_ASAN));
77   EXPECT_TRUE(rs_namespace->ContainsSearchPath("/odm/${LIB}",
78                                                AsanPath::WITH_DATA_ASAN));
79   EXPECT_TRUE(rs_namespace->ContainsSearchPath("/vendor/${LIB}",
80                                                AsanPath::WITH_DATA_ASAN));
81 
82   EXPECT_TRUE(rs_namespace->ContainsPermittedPath("/odm/${LIB}",
83                                                   AsanPath::WITH_DATA_ASAN));
84   EXPECT_TRUE(rs_namespace->ContainsPermittedPath("/vendor/${LIB}",
85                                                   AsanPath::WITH_DATA_ASAN));
86   EXPECT_TRUE(rs_namespace->ContainsPermittedPath("/system/vendor/${LIB}",
87                                                   AsanPath::NONE));
88 
89   auto vndk_namespace = system_section->GetNamespace("vndk");
90   ASSERT_TRUE(vndk_namespace);
91 
92   EXPECT_TRUE(vndk_namespace->ContainsSearchPath("/odm/${LIB}/vndk-sp",
93                                                  AsanPath::WITH_DATA_ASAN));
94   EXPECT_TRUE(vndk_namespace->ContainsSearchPath("/vendor/${LIB}/vndk-sp",
95                                                  AsanPath::WITH_DATA_ASAN));
96 
97   EXPECT_TRUE(vndk_namespace->ContainsPermittedPath("/odm/${LIB}/hw",
98                                                     AsanPath::WITH_DATA_ASAN));
99   EXPECT_TRUE(vndk_namespace->ContainsPermittedPath("/odm/${LIB}/egl",
100                                                     AsanPath::WITH_DATA_ASAN));
101   EXPECT_TRUE(vndk_namespace->ContainsPermittedPath("/vendor/${LIB}/hw",
102                                                     AsanPath::WITH_DATA_ASAN));
103   EXPECT_TRUE(vndk_namespace->ContainsPermittedPath("/vendor/${LIB}/egl",
104                                                     AsanPath::WITH_DATA_ASAN));
105 }
106 
107 TEST_F(linkerconfig_vndklite_backward_compatibility, vendor_section) {
108   auto config = android::linkerconfig::contents::CreateBaseConfiguration(ctx);
109 
110   auto vendor_section = config.GetSection("vendor");
111   ASSERT_TRUE(vendor_section);
112 
113   auto default_namespace = vendor_section->GetNamespace("default");
114   ASSERT_TRUE(default_namespace);
115 
116   EXPECT_TRUE(default_namespace->ContainsSearchPath("/odm/${LIB}",
117                                                     AsanPath::WITH_DATA_ASAN));
118   EXPECT_TRUE(default_namespace->ContainsSearchPath("/odm/${LIB}/vndk",
119                                                     AsanPath::WITH_DATA_ASAN));
120   EXPECT_TRUE(default_namespace->ContainsSearchPath("/odm/${LIB}/vndk-sp",
121                                                     AsanPath::WITH_DATA_ASAN));
122   EXPECT_TRUE(default_namespace->ContainsSearchPath("/vendor/${LIB}",
123                                                     AsanPath::WITH_DATA_ASAN));
124   EXPECT_TRUE(default_namespace->ContainsSearchPath("/vendor/${LIB}/vndk",
125                                                     AsanPath::WITH_DATA_ASAN));
126   EXPECT_TRUE(default_namespace->ContainsSearchPath("/vendor/${LIB}/vndk-sp",
127                                                     AsanPath::WITH_DATA_ASAN));
128 }
129 
130 TEST_F(linkerconfig_vndklite_backward_compatibility, unrestricted_section) {
131   auto config = android::linkerconfig::contents::CreateBaseConfiguration(ctx);
132 
133   auto unrestricted_section = config.GetSection("unrestricted");
134   ASSERT_TRUE(unrestricted_section);
135 
136   auto default_namespace = unrestricted_section->GetNamespace("default");
137   ASSERT_TRUE(default_namespace);
138 
139   EXPECT_TRUE(default_namespace->ContainsSearchPath("/odm/${LIB}",
140                                                     AsanPath::WITH_DATA_ASAN));
141   EXPECT_TRUE(default_namespace->ContainsSearchPath("/vendor/${LIB}",
142                                                     AsanPath::WITH_DATA_ASAN));
143 }