1 /*
2  * Copyright (C) 2022 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 package android.test.app;
18 
19 import android.test.lib.AppTestCommon;
20 import android.test.lib.TestUtils;
21 import android.test.productsharedlib.ProductSharedLib;
22 import android.test.systemextsharedlib.SystemExtSharedLib;
23 import android.test.systemsharedlib.SystemSharedLib;
24 import android.test.vendorsharedlib.VendorSharedLib;
25 
26 import androidx.test.filters.MediumTest;
27 
28 import org.junit.Test;
29 
30 @MediumTest
31 public class ProductAppTest extends AppTestCommon {
32     @Override
getAppLocation()33     public AppLocation getAppLocation() {
34         return AppLocation.PRODUCT;
35     }
36 
37     @Test
testPrivateLibsExist()38     public void testPrivateLibsExist() {
39         TestUtils.testPrivateLibsExist("/product", "product_private");
40     }
41 
42     @Test
testLoadExtendedPublicLibraries()43     public void testLoadExtendedPublicLibraries() {
44         System.loadLibrary("system_extpub.oem1");
45         System.loadLibrary("system_extpub.oem2");
46         System.loadLibrary("system_extpub1.oem1");
47         if (!TestUtils.productAppsAreShared()) {
48             TestUtils.assertLibraryInaccessible( // Missing <uses-native-library>.
49                     () -> System.loadLibrary("system_extpub_nouses.oem2"));
50         }
51         System.loadLibrary("product_extpub.product1");
52         System.loadLibrary("product_extpub1.product1");
53     }
54 
55     @Test
testLoadPrivateLibraries()56     public void testLoadPrivateLibraries() {
57         if (!TestUtils.productAppsAreShared()) {
58             TestUtils.assertLibraryInaccessible(() -> System.loadLibrary("system_private1"));
59             TestUtils.assertLibraryInaccessible(() -> System.loadLibrary("systemext_private1"));
60         }
61         System.loadLibrary("product_private1");
62         TestUtils.assertLibraryInaccessible(() -> System.loadLibrary("vendor_private1"));
63     }
64 
65     @Test
testLoadExtendedPublicLibrariesViaSystemSharedLib()66     public void testLoadExtendedPublicLibrariesViaSystemSharedLib() {
67         SystemSharedLib.loadLibrary("system_extpub2.oem1");
68         if (!TestUtils.skipPublicProductLibTests()) {
69             SystemSharedLib.loadLibrary("product_extpub2.product1");
70         }
71     }
72 
73     @Test
testLoadPrivateLibrariesViaSystemSharedLib()74     public void testLoadPrivateLibrariesViaSystemSharedLib() {
75         if (TestUtils.productAppsAreShared() || TestUtils.canLoadPrivateLibsFromSamePartition()) {
76             // TODO(b/186729817): These loads work in the
77             // canLoadPrivateLibsFromSamePartition case because the findLibrary
78             // call in loadLibrary0 in Runtime.java searches the system libs and
79             // converts them to absolute paths.
80             SystemSharedLib.loadLibrary("system_private2");
81             SystemSharedLib.loadLibrary("systemext_private2");
82         } else {
83             TestUtils.assertLibraryInaccessible(
84                     () -> SystemSharedLib.loadLibrary("system_private2"));
85             TestUtils.assertLibraryInaccessible(
86                     () -> SystemSharedLib.loadLibrary("systemext_private2"));
87         }
88 
89         if (!TestUtils.productAppsAreShared()) {
90             TestUtils.assertLibraryInaccessible(
91                     () -> SystemSharedLib.loadLibrary("product_private2"));
92         }
93 
94         TestUtils.assertLibraryInaccessible(() -> SystemSharedLib.loadLibrary("vendor_private2"));
95     }
96 
97     @Test
testLoadPrivateLibrariesViaSystemExtSharedLib()98     public void testLoadPrivateLibrariesViaSystemExtSharedLib() {
99         if (TestUtils.productAppsAreShared() || TestUtils.canLoadPrivateLibsFromSamePartition()) {
100             // TODO(b/186729817): These loads work in the
101             // canLoadPrivateLibsFromSamePartition case because the findLibrary
102             // call in loadLibrary0 in Runtime.java searches the system libs and
103             // converts them to absolute paths.
104             SystemExtSharedLib.loadLibrary("system_private3");
105             SystemExtSharedLib.loadLibrary("systemext_private3");
106         } else {
107             TestUtils.assertLibraryInaccessible(
108                     () -> SystemExtSharedLib.loadLibrary("system_private3"));
109             TestUtils.assertLibraryInaccessible(
110                     () -> SystemExtSharedLib.loadLibrary("systemext_private3"));
111         }
112 
113         if (!TestUtils.productAppsAreShared()) {
114             TestUtils.assertLibraryInaccessible(
115                     () -> SystemExtSharedLib.loadLibrary("product_private3"));
116         }
117 
118         TestUtils.assertLibraryInaccessible(
119                 () -> SystemExtSharedLib.loadLibrary("vendor_private3"));
120     }
121 
122     @Test
testLoadPrivateLibrariesViaProductSharedLib()123     public void testLoadPrivateLibrariesViaProductSharedLib() {
124         if (!TestUtils.productAppsAreShared()) {
125             TestUtils.assertLibraryInaccessible(
126                     () -> ProductSharedLib.loadLibrary("system_private4"));
127             TestUtils.assertLibraryInaccessible(
128                     () -> ProductSharedLib.loadLibrary("systemext_private4"));
129         }
130 
131         // Can load product_private4 by name only through the app classloader namespace.
132         ProductSharedLib.loadLibrary("product_private4");
133 
134         TestUtils.assertLibraryInaccessible(() -> ProductSharedLib.loadLibrary("vendor_private4"));
135     }
136 
137     @Test
testLoadPrivateLibrariesViaVendorSharedLib()138     public void testLoadPrivateLibrariesViaVendorSharedLib() {
139         if (!TestUtils.productAppsAreShared()) {
140             TestUtils.assertLibraryInaccessible(
141                     () -> VendorSharedLib.loadLibrary("system_private5"));
142             TestUtils.assertLibraryInaccessible(
143                     () -> VendorSharedLib.loadLibrary("systemext_private5"));
144 
145             TestUtils.assertLibraryInaccessible(
146                     () -> VendorSharedLib.loadLibrary("product_private5"));
147 
148             // When the app has a shared namespace, its libraries get loaded
149             // with shared namespaces as well, inheriting the same paths. So
150             // since the app wouldn't have access to /vendor/${LIB},
151             // VendorSharedLib here wouldn't either, and this would fail.
152             VendorSharedLib.loadLibrary("vendor_private5");
153         }
154     }
155 
156     @Test
testLoadExtendedPublicLibrariesWithAbsolutePaths()157     public void testLoadExtendedPublicLibrariesWithAbsolutePaths() {
158         System.load(TestUtils.libPath("/system", "system_extpub3.oem1"));
159         System.load(TestUtils.libPath("/product", "product_extpub3.product1"));
160     }
161 
162     @Test
testLoadPrivateLibrariesWithAbsolutePaths()163     public void testLoadPrivateLibrariesWithAbsolutePaths() {
164         if (!TestUtils.productAppsAreShared()) {
165             TestUtils.assertLibraryInaccessible(
166                     () -> System.load(TestUtils.libPath("/system", "system_private6")));
167             TestUtils.assertLibraryInaccessible(
168                     () -> System.load(TestUtils.libPath("/system_ext", "systemext_private6")));
169         }
170         System.load(TestUtils.libPath("/product", "product_private6"));
171         TestUtils.assertLibraryInaccessible(
172                 () -> System.load(TestUtils.libPath("/vendor", "vendor_private6")));
173     }
174 }
175