• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2016 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.os.cts;
18 
19 import com.android.tradefed.util.RunUtil;
20 import static com.google.common.truth.Truth.assertThat;
21 import static com.google.common.truth.Truth.assertWithMessage;
22 
23 import android.platform.test.annotations.AppModeFull;
24 import android.platform.test.annotations.AppModeInstant;
25 import android.platform.test.annotations.LargeTest;
26 import android.platform.test.annotations.Presubmit;
27 
28 import com.android.compatibility.common.tradefed.build.CompatibilityBuildHelper;
29 import com.android.compatibility.common.util.PollingCheck;
30 import com.android.ddmlib.testrunner.RemoteAndroidTestRunner;
31 import com.android.ddmlib.testrunner.TestResult.TestStatus;
32 import com.android.tradefed.build.IBuildInfo;
33 import com.android.tradefed.device.DeviceNotAvailableException;
34 import com.android.tradefed.result.CollectingTestListener;
35 import com.android.tradefed.result.TestDescription;
36 import com.android.tradefed.result.TestResult;
37 import com.android.tradefed.result.TestRunResult;
38 import com.android.tradefed.testtype.DeviceTestCase;
39 import com.android.tradefed.testtype.IBuildReceiver;
40 import com.android.tradefed.util.CommandResult;
41 import com.android.tradefed.util.CommandStatus;
42 
43 import java.io.FileNotFoundException;
44 import java.util.Arrays;
45 import java.util.Map;
46 import java.util.concurrent.TimeUnit;
47 import java.util.stream.Collectors;
48 
49 @Presubmit
50 public class StaticSharedLibsHostTests extends DeviceTestCase implements IBuildReceiver {
51     private static final String ANDROID_JUNIT_RUNNER_CLASS =
52             "androidx.test.runner.AndroidJUnitRunner";
53 
54     private static final String STATIC_LIB_PROVIDER_RECURSIVE_APK =
55             "CtsStaticSharedLibProviderRecursive.apk";
56     private static final String STATIC_LIB_PROVIDER_RECURSIVE_PKG =
57             "android.os.lib.provider.recursive";
58 
59     private static final String STATIC_LIB_PROVIDER_RECURSIVE_NAME = "foo.bar.lib.recursive";
60     private static final String STATIC_LIB_PROVIDER_NAME = "foo.bar.lib";
61 
62     private static final String STATIC_LIB_PROVIDER1_APK = "CtsStaticSharedLibProviderApp1.apk";
63     private static final String STATIC_LIB_PROVIDER1_PKG = "android.os.lib.provider";
64 
65     private static final String STATIC_LIB_PROVIDER2_APK = "CtsStaticSharedLibProviderApp2.apk";
66     private static final String STATIC_LIB_PROVIDER2_PKG = "android.os.lib.provider";
67 
68     private static final String STATIC_LIB_PROVIDER3_APK = "CtsStaticSharedLibProviderApp3.apk";
69     private static final String STATIC_LIB_PROVIDER3_PKG = "android.os.lib.provider";
70 
71     private static final String STATIC_LIB_PROVIDER4_APK = "CtsStaticSharedLibProviderApp4.apk";
72     private static final String STATIC_LIB_PROVIDER4_PKG = "android.os.lib.provider";
73 
74     private static final String STATIC_LIB_PROVIDER5_APK = "CtsStaticSharedLibProviderApp5.apk";
75     private static final String STATIC_LIB_PROVIDER5_PKG = "android.os.lib.provider";
76 
77     private static final String STATIC_LIB_PROVIDER6_APK = "CtsStaticSharedLibProviderApp6.apk";
78     private static final String STATIC_LIB_PROVIDER6_PKG = "android.os.lib.provider";
79 
80     private static final String STATIC_LIB_PROVIDER7_APK = "CtsStaticSharedLibProviderApp7.apk";
81     private static final String STATIC_LIB_PROVIDER7_PKG = "android.os.lib.provider";
82 
83     private static final String STATIC_LIB_NATIVE_PROVIDER_APK =
84             "CtsStaticSharedNativeLibProvider.apk";
85     private static final String STATIC_LIB_NATIVE_PROVIDER_PKG =
86             "android.os.lib.provider";
87 
88     private static final String STATIC_LIB_NATIVE_PROVIDER_APK1 =
89             "CtsStaticSharedNativeLibProvider1.apk";
90     private static final String STATIC_LIB_NATIVE_PROVIDER_PKG1 =
91             "android.os.lib.provider";
92 
93     private static final String STATIC_LIB_CONSUMER1_APK = "CtsStaticSharedLibConsumerApp1.apk";
94     private static final String STATIC_LIB_CONSUMER1_BAD_CERT_DIGEST_APK =
95             "CtsStaticSharedLibConsumerApp1BadCertDigest.apk";
96     private static final String STATIC_LIB_CONSUMER1_PKG = "android.os.lib.consumer1";
97 
98     private static final String STATIC_LIB_CONSUMER2_APK = "CtsStaticSharedLibConsumerApp2.apk";
99     private static final String STATIC_LIB_CONSUMER2_PKG = "android.os.lib.consumer2";
100 
101     private static final String STATIC_LIB_CONSUMER3_APK = "CtsStaticSharedLibConsumerApp3.apk";
102     private static final String STATIC_LIB_CONSUMER3_PKG = "android.os.lib.consumer3";
103 
104     private static final String STATIC_LIB_NATIVE_CONSUMER_APK
105             = "CtsStaticSharedNativeLibConsumer.apk";
106     private static final String STATIC_LIB_NATIVE_CONSUMER_PKG
107             = "android.os.lib.consumer";
108 
109     private static final String STATIC_LIB_TEST_APP_PKG = "android.os.lib.app";
110     private static final String STATIC_LIB_TEST_APP_CLASS_NAME = STATIC_LIB_TEST_APP_PKG
111             + ".StaticSharedLibsTests";
112     private static final String STATIC_LIB_MULTI_USER_TEST_APP_CLASS_NAME = STATIC_LIB_TEST_APP_PKG
113             + ".StaticSharedLibsMultiUserTests";
114 
115     private static final String SETTING_UNUSED_STATIC_SHARED_LIB_MIN_CACHE_PERIOD =
116             "unused_static_shared_lib_min_cache_period";
117 
118     private static final long DEFAULT_TIMEOUT_MILLIS = TimeUnit.SECONDS.toMillis(15);
119 
120     private CompatibilityBuildHelper mBuildHelper;
121     private boolean mInstantMode = false;
122 
123     @Override
setBuild(IBuildInfo buildInfo)124     public void setBuild(IBuildInfo buildInfo) {
125         mBuildHelper = new CompatibilityBuildHelper(buildInfo);
126     }
127 
128     @AppModeInstant
testInstallSharedLibraryInstantMode()129     public void testInstallSharedLibraryInstantMode() throws Exception {
130         mInstantMode = true;
131         doTestInstallSharedLibrary();
132     }
133 
134     @AppModeFull
testInstallSharedLibraryFullMode()135     public void testInstallSharedLibraryFullMode() throws Exception {
136         doTestInstallSharedLibrary();
137     }
138 
doTestInstallSharedLibrary()139     private void doTestInstallSharedLibrary() throws Exception {
140         getDevice().uninstallPackage(STATIC_LIB_PROVIDER1_PKG);
141         getDevice().uninstallPackage(STATIC_LIB_PROVIDER2_PKG);
142         getDevice().uninstallPackage(STATIC_LIB_PROVIDER_RECURSIVE_PKG);
143         try {
144             // Install library dependency
145             assertNull(install(STATIC_LIB_PROVIDER_RECURSIVE_APK));
146             // Install version 1
147             assertNull(install(STATIC_LIB_PROVIDER1_APK));
148             // Install version 2
149             assertNull(install(STATIC_LIB_PROVIDER2_APK));
150             // Uninstall version 1
151             assertNull(getDevice().uninstallPackage(STATIC_LIB_PROVIDER1_PKG));
152             // Uninstall version 2
153             assertNull(getDevice().uninstallPackage(STATIC_LIB_PROVIDER2_PKG));
154             // Uninstall dependency
155             assertNull(getDevice().uninstallPackage(STATIC_LIB_PROVIDER_RECURSIVE_PKG));
156         } finally {
157             getDevice().uninstallPackage(STATIC_LIB_PROVIDER1_PKG);
158             getDevice().uninstallPackage(STATIC_LIB_PROVIDER2_PKG);
159             getDevice().uninstallPackage(STATIC_LIB_PROVIDER_RECURSIVE_PKG);
160         }
161     }
162 
163     @AppModeInstant
testCannotInstallSharedLibraryWithMissingDependencyInstantMode()164     public void testCannotInstallSharedLibraryWithMissingDependencyInstantMode() throws Exception {
165         mInstantMode = true;
166         doTestCannotInstallSharedLibraryWithMissingDependency();
167     }
168 
169     @AppModeFull
testCannotInstallSharedLibraryWithMissingDependencyFullMode()170     public void testCannotInstallSharedLibraryWithMissingDependencyFullMode() throws Exception {
171         doTestCannotInstallSharedLibraryWithMissingDependency();
172     }
173 
doTestCannotInstallSharedLibraryWithMissingDependency()174     private void doTestCannotInstallSharedLibraryWithMissingDependency() throws Exception {
175         getDevice().uninstallPackage(STATIC_LIB_PROVIDER1_PKG);
176         getDevice().uninstallPackage(STATIC_LIB_PROVIDER2_PKG);
177         try {
178             // Install version 1 - should fail - no dependency
179             assertNotNull(install(STATIC_LIB_PROVIDER1_APK));
180         } finally {
181             getDevice().uninstallPackage(STATIC_LIB_PROVIDER1_PKG);
182             getDevice().uninstallPackage(STATIC_LIB_PROVIDER2_PKG);
183         }
184     }
185 
testLoadCodeAndResourcesFromSharedLibraryRecursively()186     public void testLoadCodeAndResourcesFromSharedLibraryRecursively() throws Exception {
187         getDevice().uninstallPackage(STATIC_LIB_CONSUMER1_PKG);
188         getDevice().uninstallPackage(STATIC_LIB_PROVIDER1_PKG);
189         getDevice().uninstallPackage(STATIC_LIB_PROVIDER_RECURSIVE_PKG);
190         try {
191             // Install library dependency
192             assertNull(install(STATIC_LIB_PROVIDER_RECURSIVE_APK));
193             // Install the library
194             assertNull(install(STATIC_LIB_PROVIDER1_APK));
195             // Install the client
196             assertNull(install(STATIC_LIB_CONSUMER1_APK));
197             // Try to load code and resources
198             runDeviceTests(STATIC_LIB_CONSUMER1_PKG,
199                     "android.os.lib.consumer1.UseSharedLibraryTest",
200                     "testLoadCodeAndResources");
201         } finally {
202             getDevice().uninstallPackage(STATIC_LIB_CONSUMER1_PKG);
203             getDevice().uninstallPackage(STATIC_LIB_PROVIDER1_PKG);
204             getDevice().uninstallPackage(STATIC_LIB_PROVIDER_RECURSIVE_PKG);
205         }
206     }
207 
testLoadCodeAndResourcesFromSharedLibraryRecursivelyUpdate()208     public void testLoadCodeAndResourcesFromSharedLibraryRecursivelyUpdate() throws Exception {
209         getDevice().uninstallPackage(STATIC_LIB_CONSUMER1_PKG);
210         getDevice().uninstallPackage(STATIC_LIB_PROVIDER1_PKG);
211         getDevice().uninstallPackage(STATIC_LIB_PROVIDER_RECURSIVE_PKG);
212         try {
213             // Install library dependency
214             assertNull(install(STATIC_LIB_PROVIDER_RECURSIVE_APK));
215             // Install the library
216             assertNull(install(STATIC_LIB_PROVIDER1_APK));
217             // Install the client
218             assertNull(install(STATIC_LIB_CONSUMER1_APK));
219             // Try to load code and resources
220             runDeviceTests(STATIC_LIB_CONSUMER1_PKG,
221                     "android.os.lib.consumer1.UseSharedLibraryTest",
222                     "testLoadCodeAndResources");
223             // Install library dependency
224             assertNull(install(STATIC_LIB_PROVIDER_RECURSIVE_APK, true));
225             // Try to load code and resources
226             runDeviceTests(STATIC_LIB_CONSUMER1_PKG,
227                     "android.os.lib.consumer1.UseSharedLibraryTest",
228                     "testLoadCodeAndResources");
229             // Install the library
230             assertNull(install(STATIC_LIB_PROVIDER1_APK, true));
231             // Try to load code and resources
232             runDeviceTests(STATIC_LIB_CONSUMER1_PKG,
233                     "android.os.lib.consumer1.UseSharedLibraryTest",
234                     "testLoadCodeAndResources");
235         } finally {
236             getDevice().uninstallPackage(STATIC_LIB_CONSUMER1_PKG);
237             getDevice().uninstallPackage(STATIC_LIB_PROVIDER1_PKG);
238             getDevice().uninstallPackage(STATIC_LIB_PROVIDER_RECURSIVE_PKG);
239         }
240     }
241 
242     @AppModeInstant
testCannotUninstallUsedSharedLibrary1InstantMode()243     public void testCannotUninstallUsedSharedLibrary1InstantMode() throws Exception {
244         mInstantMode = true;
245         doTestCannotUninstallUsedSharedLibrary1();
246     }
247 
248     @AppModeFull
testCannotUninstallUsedSharedLibrary1FullMode()249     public void testCannotUninstallUsedSharedLibrary1FullMode() throws Exception {
250         doTestCannotUninstallUsedSharedLibrary1();
251     }
252 
doTestCannotUninstallUsedSharedLibrary1()253     private void doTestCannotUninstallUsedSharedLibrary1() throws Exception {
254         getDevice().uninstallPackage(STATIC_LIB_PROVIDER1_PKG);
255         getDevice().uninstallPackage(STATIC_LIB_PROVIDER_RECURSIVE_PKG);
256         try {
257             // Install library dependency
258             assertNull(install(STATIC_LIB_PROVIDER_RECURSIVE_APK));
259             // Install the library
260             assertNull(install(STATIC_LIB_PROVIDER1_APK));
261             // The library dependency cannot be uninstalled
262             assertNotNull(getDevice().uninstallPackage(STATIC_LIB_PROVIDER_RECURSIVE_PKG));
263             // Now the library dependency can be uninstalled
264             assertNull(getDevice().uninstallPackage(STATIC_LIB_PROVIDER1_PKG));
265             // Uninstall dependency
266             assertNull(getDevice().uninstallPackage(STATIC_LIB_PROVIDER_RECURSIVE_PKG));
267         } finally {
268             getDevice().uninstallPackage(STATIC_LIB_PROVIDER1_PKG);
269             getDevice().uninstallPackage(STATIC_LIB_PROVIDER_RECURSIVE_PKG);
270         }
271     }
272 
273     @AppModeInstant
testCannotUninstallUsedSharedLibrary2InstantMode()274     public void testCannotUninstallUsedSharedLibrary2InstantMode() throws Exception {
275         mInstantMode = true;
276         doTestCannotUninstallUsedSharedLibrary2();
277     }
278 
279     @AppModeFull
testCannotUninstallUsedSharedLibrary2FullMode()280     public void testCannotUninstallUsedSharedLibrary2FullMode() throws Exception {
281         doTestCannotUninstallUsedSharedLibrary2();
282     }
283 
doTestCannotUninstallUsedSharedLibrary2()284     private void doTestCannotUninstallUsedSharedLibrary2() throws Exception {
285         getDevice().uninstallPackage(STATIC_LIB_CONSUMER1_PKG);
286         getDevice().uninstallPackage(STATIC_LIB_PROVIDER1_PKG);
287         getDevice().uninstallPackage(STATIC_LIB_PROVIDER_RECURSIVE_PKG);
288         try {
289             // Install library dependency
290             assertNull(install(STATIC_LIB_PROVIDER_RECURSIVE_APK));
291             // Install the library
292             assertNull(install(STATIC_LIB_PROVIDER1_APK));
293             // Install the client
294             assertNull(install(STATIC_LIB_CONSUMER1_APK));
295             // The library cannot be uninstalled
296             assertNotNull(getDevice().uninstallPackage(STATIC_LIB_PROVIDER1_PKG));
297             // Uninstall the client
298             assertNull(getDevice().uninstallPackage(STATIC_LIB_CONSUMER1_PKG));
299             // Now the library can be uninstalled
300             assertNull(getDevice().uninstallPackage(STATIC_LIB_PROVIDER1_PKG));
301             // Uninstall dependency
302             assertNull(getDevice().uninstallPackage(STATIC_LIB_PROVIDER_RECURSIVE_PKG));
303         } finally {
304             getDevice().uninstallPackage(STATIC_LIB_CONSUMER1_PKG);
305             getDevice().uninstallPackage(STATIC_LIB_PROVIDER1_PKG);
306             getDevice().uninstallPackage(STATIC_LIB_PROVIDER_RECURSIVE_PKG);
307         }
308     }
309 
310     @AppModeInstant
testLibraryVersionsAndVersionCodesSameOrderInstantMode()311     public void testLibraryVersionsAndVersionCodesSameOrderInstantMode() throws Exception {
312         mInstantMode = true;
313         doTestLibraryVersionsAndVersionCodesSameOrder();
314     }
315 
316     @AppModeFull
testLibraryVersionsAndVersionCodesSameOrderFullMode()317     public void testLibraryVersionsAndVersionCodesSameOrderFullMode() throws Exception {
318         doTestLibraryVersionsAndVersionCodesSameOrder();
319     }
320 
doTestLibraryVersionsAndVersionCodesSameOrder()321     private void doTestLibraryVersionsAndVersionCodesSameOrder() throws Exception {
322         getDevice().uninstallPackage(STATIC_LIB_PROVIDER1_PKG);
323         getDevice().uninstallPackage(STATIC_LIB_PROVIDER2_PKG);
324         getDevice().uninstallPackage(STATIC_LIB_PROVIDER3_PKG);
325         getDevice().uninstallPackage(STATIC_LIB_PROVIDER_RECURSIVE_PKG);
326         try {
327             // Install library dependency
328             assertNull(install(STATIC_LIB_PROVIDER_RECURSIVE_APK));
329             // Install library version 1 with version code 1
330             assertNull(install(STATIC_LIB_PROVIDER1_APK));
331             // Install library version 2 with version code 4
332             assertNull(install(STATIC_LIB_PROVIDER2_APK));
333             // Shouldn't be able to install library version 3 with version code 3
334             assertNotNull(install(STATIC_LIB_PROVIDER3_APK));
335         } finally {
336             getDevice().uninstallPackage(STATIC_LIB_PROVIDER1_PKG);
337             getDevice().uninstallPackage(STATIC_LIB_PROVIDER2_PKG);
338             getDevice().uninstallPackage(STATIC_LIB_PROVIDER3_PKG);
339             getDevice().uninstallPackage(STATIC_LIB_PROVIDER_RECURSIVE_PKG);
340         }
341     }
342 
343     @AppModeInstant
testCannotInstallAppWithMissingLibraryInstantMode()344     public void testCannotInstallAppWithMissingLibraryInstantMode() throws Exception {
345         mInstantMode = true;
346         doTestCannotInstallAppWithMissingLibrary();
347     }
348 
349     @AppModeFull
testCannotInstallAppWithMissingLibraryFullMode()350     public void testCannotInstallAppWithMissingLibraryFullMode() throws Exception {
351         doTestCannotInstallAppWithMissingLibrary();
352     }
353 
doTestCannotInstallAppWithMissingLibrary()354     private void doTestCannotInstallAppWithMissingLibrary() throws Exception {
355         getDevice().uninstallPackage(STATIC_LIB_CONSUMER1_PKG);
356         try {
357             // Shouldn't be able to install an app if a dependency lib is missing
358             assertNotNull(install(STATIC_LIB_CONSUMER1_APK));
359         } finally {
360             getDevice().uninstallPackage(STATIC_LIB_CONSUMER1_PKG);
361         }
362     }
363 
364     @AppModeFull
testCanReplaceLibraryIfVersionAndVersionCodeSame()365     public void testCanReplaceLibraryIfVersionAndVersionCodeSame() throws Exception {
366         getDevice().uninstallPackage(STATIC_LIB_PROVIDER1_PKG);
367         getDevice().uninstallPackage(STATIC_LIB_PROVIDER_RECURSIVE_PKG);
368         try {
369             // Install library dependency
370             assertNull(install(STATIC_LIB_PROVIDER_RECURSIVE_APK));
371             // Install a library
372             assertNull(install(STATIC_LIB_PROVIDER1_APK));
373             // Can reinstall the library if version and version code same
374             assertNull(install(STATIC_LIB_PROVIDER1_APK));
375         } finally {
376             getDevice().uninstallPackage(STATIC_LIB_PROVIDER1_PKG);
377             getDevice().uninstallPackage(STATIC_LIB_PROVIDER_RECURSIVE_PKG);
378         }
379     }
380 
381     @AppModeInstant
testUninstallSpecificLibraryVersionInstantMode()382     public void testUninstallSpecificLibraryVersionInstantMode() throws Exception {
383         mInstantMode = true;
384         doTestUninstallSpecificLibraryVersion();
385     }
386 
387     @AppModeFull
testUninstallSpecificLibraryVersionFullMode()388     public void testUninstallSpecificLibraryVersionFullMode() throws Exception {
389         doTestUninstallSpecificLibraryVersion();
390     }
391 
doTestUninstallSpecificLibraryVersion()392     private void doTestUninstallSpecificLibraryVersion() throws Exception {
393         getDevice().uninstallPackage(STATIC_LIB_PROVIDER1_PKG);
394         getDevice().uninstallPackage(STATIC_LIB_PROVIDER_RECURSIVE_PKG);
395         try {
396             // Install library dependency
397             assertNull(install(STATIC_LIB_PROVIDER_RECURSIVE_APK));
398             // Install library version 1 with version code 1
399             assertNull(install(STATIC_LIB_PROVIDER1_APK));
400             // Install library version 2 with version code 4
401             assertNull(install(STATIC_LIB_PROVIDER2_APK));
402             // Uninstall the library package with version code 4 (version 2)
403             assertTrue(getDevice().executeShellCommand("pm uninstall --versionCode 4 "
404                     + STATIC_LIB_PROVIDER1_PKG).startsWith("Success"));
405             // Uninstall the library package with version code 1 (version 1)
406             assertTrue(getDevice().executeShellCommand("pm uninstall "
407                     + STATIC_LIB_PROVIDER1_PKG).startsWith("Success"));
408         } finally {
409             getDevice().uninstallPackage(STATIC_LIB_PROVIDER1_PKG);
410             getDevice().uninstallPackage(STATIC_LIB_PROVIDER_RECURSIVE_PKG);
411         }
412     }
413 
414     @AppModeInstant
testKeyRotationInstantMode()415     public void testKeyRotationInstantMode() throws Exception {
416         mInstantMode = true;
417         doTestKeyRotation();
418     }
419 
420     @AppModeFull
testKeyRotationFullMode()421     public void testKeyRotationFullMode() throws Exception {
422         doTestKeyRotation();
423     }
424 
doTestKeyRotation()425     private void doTestKeyRotation() throws Exception {
426         getDevice().uninstallPackage(STATIC_LIB_CONSUMER2_PKG);
427         getDevice().uninstallPackage(STATIC_LIB_PROVIDER2_PKG);
428         getDevice().uninstallPackage(STATIC_LIB_PROVIDER4_PKG);
429         try {
430             // Install a library version specifying an upgrade key set
431             assertNull(install(STATIC_LIB_PROVIDER2_APK));
432             // Install a newer library signed with the upgrade key set
433             assertNull(install(STATIC_LIB_PROVIDER4_APK));
434             // Install a client that depends on the upgraded key set
435             assertNull(install(STATIC_LIB_CONSUMER2_APK));
436             // Ensure code and resources can be loaded
437             runDeviceTests(STATIC_LIB_CONSUMER2_PKG,
438                     "android.os.lib.consumer2.UseSharedLibraryTest",
439                     "testLoadCodeAndResources");
440         } finally {
441             getDevice().uninstallPackage(STATIC_LIB_CONSUMER2_PKG);
442             getDevice().uninstallPackage(STATIC_LIB_PROVIDER2_PKG);
443             getDevice().uninstallPackage(STATIC_LIB_PROVIDER4_PKG);
444         }
445     }
446 
447     @AppModeInstant
testCannotInstallIncorrectlySignedLibraryInstantMode()448     public void testCannotInstallIncorrectlySignedLibraryInstantMode() throws Exception {
449         mInstantMode = true;
450         doTestCannotInstallIncorrectlySignedLibrary();
451     }
452 
453     @AppModeFull
testCannotInstallIncorrectlySignedLibraryFullMode()454     public void testCannotInstallIncorrectlySignedLibraryFullMode() throws Exception {
455         doTestCannotInstallIncorrectlySignedLibrary();
456     }
457 
doTestCannotInstallIncorrectlySignedLibrary()458     private void doTestCannotInstallIncorrectlySignedLibrary() throws Exception {
459         getDevice().uninstallPackage(STATIC_LIB_PROVIDER1_PKG);
460         getDevice().uninstallPackage(STATIC_LIB_PROVIDER4_PKG);
461         getDevice().uninstallPackage(STATIC_LIB_PROVIDER_RECURSIVE_PKG);
462         try {
463             // Install library dependency
464             assertNull(install(STATIC_LIB_PROVIDER_RECURSIVE_APK));
465             // Install a library version not specifying an upgrade key set
466             assertNull(install(STATIC_LIB_PROVIDER1_APK));
467             // Shouldn't be able to install a newer version signed differently
468             assertNotNull(install(STATIC_LIB_PROVIDER4_APK));
469         } finally {
470             getDevice().uninstallPackage(STATIC_LIB_PROVIDER1_PKG);
471             getDevice().uninstallPackage(STATIC_LIB_PROVIDER4_PKG);
472             getDevice().uninstallPackage(STATIC_LIB_PROVIDER_RECURSIVE_PKG);
473         }
474     }
475 
476     @AppModeInstant
testLibraryAndPackageNameCanMatchInstantMode()477     public void testLibraryAndPackageNameCanMatchInstantMode() throws Exception {
478         mInstantMode = true;
479         doTestLibraryAndPackageNameCanMatch();
480     }
481 
482     @AppModeFull
testLibraryAndPackageNameCanMatchFullMode()483     public void testLibraryAndPackageNameCanMatchFullMode() throws Exception {
484         doTestLibraryAndPackageNameCanMatch();
485     }
486 
doTestLibraryAndPackageNameCanMatch()487     private void doTestLibraryAndPackageNameCanMatch() throws Exception {
488         getDevice().uninstallPackage(STATIC_LIB_PROVIDER5_PKG);
489         getDevice().uninstallPackage(STATIC_LIB_PROVIDER6_PKG);
490         try {
491             // Install a library with same name as package should work.
492             assertNull(install(STATIC_LIB_PROVIDER5_APK));
493             // Install a library with same name as package should work.
494             assertNull(install(STATIC_LIB_PROVIDER6_APK));
495         } finally {
496             getDevice().uninstallPackage(STATIC_LIB_PROVIDER5_PKG);
497             getDevice().uninstallPackage(STATIC_LIB_PROVIDER6_PKG);
498         }
499     }
500 
501     @AppModeInstant
testGetSharedLibrariesInstantMode()502     public void testGetSharedLibrariesInstantMode() throws Exception {
503         mInstantMode = true;
504         doTestGetSharedLibraries();
505     }
506 
507     @AppModeFull
testGetSharedLibrariesFullMode()508     public void testGetSharedLibrariesFullMode() throws Exception {
509         doTestGetSharedLibraries();
510     }
511 
doTestGetSharedLibraries()512     private void doTestGetSharedLibraries() throws Exception {
513         getDevice().uninstallPackage(STATIC_LIB_CONSUMER1_PKG);
514         getDevice().uninstallPackage(STATIC_LIB_CONSUMER2_PKG);
515         getDevice().uninstallPackage(STATIC_LIB_PROVIDER1_PKG);
516         getDevice().uninstallPackage(STATIC_LIB_PROVIDER2_PKG);
517         getDevice().uninstallPackage(STATIC_LIB_PROVIDER4_PKG);
518         getDevice().uninstallPackage(STATIC_LIB_PROVIDER_RECURSIVE_PKG);
519         try {
520             // Install library dependency
521             assertNull(install(STATIC_LIB_PROVIDER_RECURSIVE_APK));
522             // Install the first library
523             assertNull(install(STATIC_LIB_PROVIDER1_APK));
524             // Install the second library
525             assertNull(install(STATIC_LIB_PROVIDER2_APK));
526             // Install the third library
527             assertNull(install(STATIC_LIB_PROVIDER4_APK));
528             // Install the first client
529             assertNull(install(STATIC_LIB_CONSUMER1_APK));
530             // Install the second client
531             assertNull(install(STATIC_LIB_CONSUMER2_APK));
532             // Ensure the first library has the REQUEST_INSTALL_PACKAGES app op
533             getDevice().executeShellV2Command("appops set "
534                     + STATIC_LIB_CONSUMER1_PKG
535                     + " REQUEST_INSTALL_PACKAGES allow");
536             // Ensure libraries are properly reported
537             runDeviceTests(STATIC_LIB_CONSUMER1_PKG,
538                     "android.os.lib.consumer1.UseSharedLibraryTest",
539                     "testSharedLibrariesProperlyReported");
540         } finally {
541             getDevice().uninstallPackage(STATIC_LIB_CONSUMER1_PKG);
542             getDevice().uninstallPackage(STATIC_LIB_CONSUMER2_PKG);
543             getDevice().uninstallPackage(STATIC_LIB_PROVIDER1_PKG);
544             getDevice().uninstallPackage(STATIC_LIB_PROVIDER2_PKG);
545             getDevice().uninstallPackage(STATIC_LIB_PROVIDER4_PKG);
546             getDevice().uninstallPackage(STATIC_LIB_PROVIDER_RECURSIVE_PKG);
547         }
548     }
549 
550     @AppModeFull(
551             reason = "getDeclaredSharedLibraries() requires ACCESS_SHARED_LIBRARIES permission")
testGetDeclaredSharedLibraries()552     public void testGetDeclaredSharedLibraries() throws Exception {
553         getDevice().uninstallPackage(STATIC_LIB_CONSUMER1_PKG);
554         getDevice().uninstallPackage(STATIC_LIB_CONSUMER2_PKG);
555         getDevice().uninstallPackage(STATIC_LIB_PROVIDER1_PKG);
556         getDevice().uninstallPackage(STATIC_LIB_PROVIDER2_PKG);
557         getDevice().uninstallPackage(STATIC_LIB_PROVIDER4_PKG);
558         getDevice().uninstallPackage(STATIC_LIB_PROVIDER_RECURSIVE_PKG);
559         try {
560             // Install library dependency
561             assertNull(install(STATIC_LIB_PROVIDER_RECURSIVE_APK));
562             // Install the first library
563             assertNull(install(STATIC_LIB_PROVIDER1_APK));
564             // Install the second library
565             assertNull(install(STATIC_LIB_PROVIDER2_APK));
566             // Install the third library
567             assertNull(install(STATIC_LIB_PROVIDER4_APK));
568             // Install the first client
569             assertNull(install(STATIC_LIB_CONSUMER1_APK));
570             // Install the second client
571             assertNull(install(STATIC_LIB_CONSUMER2_APK));
572             // Ensure declared libraries are properly reported
573             runDeviceTests(STATIC_LIB_CONSUMER1_PKG,
574                     "android.os.lib.consumer1.UseSharedLibraryTest",
575                     "testDeclaredSharedLibrariesProperlyReported");
576         } finally {
577             getDevice().uninstallPackage(STATIC_LIB_CONSUMER1_PKG);
578             getDevice().uninstallPackage(STATIC_LIB_CONSUMER2_PKG);
579             getDevice().uninstallPackage(STATIC_LIB_PROVIDER1_PKG);
580             getDevice().uninstallPackage(STATIC_LIB_PROVIDER2_PKG);
581             getDevice().uninstallPackage(STATIC_LIB_PROVIDER4_PKG);
582             getDevice().uninstallPackage(STATIC_LIB_PROVIDER_RECURSIVE_PKG);
583         }
584     }
585 
586     @AppModeInstant
testAppCanSeeOnlyLibrariesItDependOnInstantMode()587     public void testAppCanSeeOnlyLibrariesItDependOnInstantMode() throws Exception {
588         mInstantMode = true;
589         doTestAppCanSeeOnlyLibrariesItDependOn();
590     }
591 
592     @AppModeFull
testAppCanSeeOnlyLibrariesItDependOnFullMode()593     public void testAppCanSeeOnlyLibrariesItDependOnFullMode() throws Exception {
594         doTestAppCanSeeOnlyLibrariesItDependOn();
595     }
596 
doTestAppCanSeeOnlyLibrariesItDependOn()597     private void doTestAppCanSeeOnlyLibrariesItDependOn() throws Exception {
598         getDevice().uninstallPackage(STATIC_LIB_CONSUMER1_PKG);
599         getDevice().uninstallPackage(STATIC_LIB_PROVIDER1_PKG);
600         getDevice().uninstallPackage(STATIC_LIB_PROVIDER2_PKG);
601         getDevice().uninstallPackage(STATIC_LIB_PROVIDER_RECURSIVE_PKG);
602         try {
603             // Install library dependency
604             assertNull(install(STATIC_LIB_PROVIDER_RECURSIVE_APK));
605             // Install the first library
606             assertNull(install(STATIC_LIB_PROVIDER1_APK));
607             // Install the second library
608             assertNull(install(STATIC_LIB_PROVIDER2_APK));
609             // Install the client
610             assertNull(install(STATIC_LIB_CONSUMER1_APK));
611             // Ensure the client can see only the lib it depends on
612             runDeviceTests(STATIC_LIB_CONSUMER1_PKG,
613                     "android.os.lib.consumer1.UseSharedLibraryTest",
614                     "testAppCanSeeOnlyLibrariesItDependOn");
615         } finally {
616             getDevice().uninstallPackage(STATIC_LIB_CONSUMER1_PKG);
617             getDevice().uninstallPackage(STATIC_LIB_PROVIDER1_PKG);
618             getDevice().uninstallPackage(STATIC_LIB_PROVIDER2_PKG);
619             getDevice().uninstallPackage(STATIC_LIB_PROVIDER_RECURSIVE_PKG);
620         }
621     }
622 
623     @AppModeInstant
testLoadCodeFromNativeLibInstantMode()624     public void testLoadCodeFromNativeLibInstantMode() throws Exception {
625         mInstantMode = true;
626         doTestLoadCodeFromNativeLib();
627     }
628 
629     @AppModeFull
testLoadCodeFromNativeLibFullMode()630     public void testLoadCodeFromNativeLibFullMode() throws Exception {
631         doTestLoadCodeFromNativeLib();
632     }
633 
doTestLoadCodeFromNativeLib()634     private void doTestLoadCodeFromNativeLib() throws Exception {
635         getDevice().uninstallPackage(STATIC_LIB_NATIVE_CONSUMER_PKG);
636         getDevice().uninstallPackage(STATIC_LIB_NATIVE_PROVIDER_PKG);
637         try {
638             // Install library
639             assertNull(install(STATIC_LIB_NATIVE_PROVIDER_APK));
640             // Install the library client
641             assertNull(install(STATIC_LIB_NATIVE_CONSUMER_APK));
642             // Ensure the client can load native code from the library
643             runDeviceTests(STATIC_LIB_NATIVE_CONSUMER_PKG,
644                     "android.os.lib.consumer.UseSharedLibraryTest",
645                     "testLoadNativeCode");
646         } finally {
647             getDevice().uninstallPackage(STATIC_LIB_NATIVE_CONSUMER_PKG);
648             getDevice().uninstallPackage(STATIC_LIB_NATIVE_PROVIDER_PKG);
649         }
650     }
651 
652     @AppModeInstant
testLoadCodeFromNativeLibMultiArchViolationInstantMode()653     public void testLoadCodeFromNativeLibMultiArchViolationInstantMode() throws Exception {
654         mInstantMode = true;
655         doTestLoadCodeFromNativeLibMultiArchViolation();
656     }
657 
658     @AppModeFull
testLoadCodeFromNativeLibMultiArchViolationFullMode()659     public void testLoadCodeFromNativeLibMultiArchViolationFullMode() throws Exception {
660         doTestLoadCodeFromNativeLibMultiArchViolation();
661     }
662 
doTestLoadCodeFromNativeLibMultiArchViolation()663     private void doTestLoadCodeFromNativeLibMultiArchViolation() throws Exception {
664         getDevice().uninstallPackage(STATIC_LIB_NATIVE_PROVIDER_PKG1);
665         try {
666             // Cannot install the library with native code if not multi-arch
667             assertNotNull(install(STATIC_LIB_NATIVE_PROVIDER_APK1));
668         } finally {
669             getDevice().uninstallPackage(STATIC_LIB_NATIVE_PROVIDER_PKG1);
670         }
671     }
672 
673     @AppModeInstant
testLoadCodeAndResourcesFromSharedLibrarySignedWithTwoCertsInstantMode()674     public void testLoadCodeAndResourcesFromSharedLibrarySignedWithTwoCertsInstantMode() throws Exception {
675         mInstantMode = true;
676         doTestLoadCodeAndResourcesFromSharedLibrarySignedWithTwoCerts();
677     }
678 
679     @AppModeFull
testLoadCodeAndResourcesFromSharedLibrarySignedWithTwoCertsFullMode()680     public void testLoadCodeAndResourcesFromSharedLibrarySignedWithTwoCertsFullMode() throws Exception {
681         doTestLoadCodeAndResourcesFromSharedLibrarySignedWithTwoCerts();
682     }
683 
doTestLoadCodeAndResourcesFromSharedLibrarySignedWithTwoCerts()684     private void doTestLoadCodeAndResourcesFromSharedLibrarySignedWithTwoCerts()
685             throws Exception {
686         getDevice().uninstallPackage(STATIC_LIB_CONSUMER3_PKG);
687         getDevice().uninstallPackage(STATIC_LIB_PROVIDER7_PKG);
688         try {
689             // Install the library
690             assertNull(install(STATIC_LIB_PROVIDER7_APK));
691             // Install the client
692             assertNull(install(STATIC_LIB_CONSUMER3_APK));
693             // Try to load code and resources
694             runDeviceTests(STATIC_LIB_CONSUMER3_PKG,
695                     "android.os.lib.consumer3.UseSharedLibraryTest",
696                     "testLoadCodeAndResources");
697         } finally {
698             getDevice().uninstallPackage(STATIC_LIB_CONSUMER3_PKG);
699             getDevice().uninstallPackage(STATIC_LIB_PROVIDER7_PKG);
700         }
701     }
702 
testSamegradeStaticSharedLibByAdb()703     public void testSamegradeStaticSharedLibByAdb() throws Exception {
704         getDevice().uninstallPackage(STATIC_LIB_PROVIDER5_PKG);
705         try {
706             assertNull(install(STATIC_LIB_PROVIDER5_APK));
707             assertNull(install(STATIC_LIB_PROVIDER5_APK, true /*reinstall*/));
708         } finally {
709             getDevice().uninstallPackage(STATIC_LIB_PROVIDER5_PKG);
710         }
711     }
712 
713     @AppModeFull(reason = "Instant app cannot get package installer service")
testCannotSamegradeStaticSharedLibByInstaller()714     public void testCannotSamegradeStaticSharedLibByInstaller() throws Exception {
715         runDeviceTests(STATIC_LIB_TEST_APP_PKG, STATIC_LIB_TEST_APP_CLASS_NAME,
716                 "testSamegradeStaticSharedLibFail");
717     }
718 
runDeviceTests(String packageName, String testClassName, String testMethodName)719     private void runDeviceTests(String packageName, String testClassName,
720             String testMethodName) throws DeviceNotAvailableException {
721         RemoteAndroidTestRunner testRunner = new RemoteAndroidTestRunner(packageName,
722                 ANDROID_JUNIT_RUNNER_CLASS, getDevice().getIDevice());
723         testRunner.setMethodName(testClassName, testMethodName);
724         CollectingTestListener listener = new CollectingTestListener();
725 
726         getDevice().runInstrumentationTests(testRunner, listener);
727 
728         final TestRunResult result = listener.getCurrentRunResults();
729         if (result.isRunFailure()) {
730             throw new AssertionError("Failed to successfully run device tests for "
731                     + result.getName() + ": " + result.getRunFailureMessage());
732         }
733         if (result.getNumTests() == 0) {
734             throw new AssertionError("No tests were run on the device");
735         }
736         if (result.hasFailedTests()) {
737             // build a meaningful error message
738             StringBuilder errorBuilder = new StringBuilder("on-device tests failed:\n");
739             for (Map.Entry<TestDescription, TestResult> resultEntry :
740                     result.getTestResults().entrySet()) {
741                 if (!resultEntry.getValue().getStatus().equals(TestStatus.PASSED)) {
742                     errorBuilder.append(resultEntry.getKey().toString());
743                     errorBuilder.append(":\n");
744                     errorBuilder.append(resultEntry.getValue().getStackTrace());
745                 }
746             }
747             throw new AssertionError(errorBuilder.toString());
748         }
749     }
750 
751     @LargeTest
752     @AppModeFull
testPruneUnusedStaticSharedLibraries_reboot_fullMode()753     public void testPruneUnusedStaticSharedLibraries_reboot_fullMode()
754             throws Exception {
755         doTestPruneUnusedStaticSharedLibraries_reboot();
756     }
757 
758     @LargeTest
759     @AppModeInstant
testPruneUnusedStaticSharedLibraries_reboot_instantMode()760     public void testPruneUnusedStaticSharedLibraries_reboot_instantMode()
761             throws Exception {
762         mInstantMode = true;
763         doTestPruneUnusedStaticSharedLibraries_reboot();
764     }
765 
doTestPruneUnusedStaticSharedLibraries_reboot()766     private void doTestPruneUnusedStaticSharedLibraries_reboot()
767             throws Exception {
768         getDevice().uninstallPackage(STATIC_LIB_CONSUMER3_PKG);
769         getDevice().uninstallPackage(STATIC_LIB_PROVIDER7_PKG);
770         getDevice().uninstallPackage(STATIC_LIB_PROVIDER_RECURSIVE_PKG);
771         try {
772             // Install an unused library
773             assertThat(install(STATIC_LIB_PROVIDER_RECURSIVE_APK)).isNull();
774             assertThat(checkLibrary(STATIC_LIB_PROVIDER_RECURSIVE_NAME)).isTrue();
775 
776             // Install the client and the corresponding library
777             assertThat(install(STATIC_LIB_PROVIDER7_APK)).isNull();
778             assertThat(install(STATIC_LIB_CONSUMER3_APK)).isNull();
779             assertThat(checkLibrary(STATIC_LIB_PROVIDER_NAME)).isTrue();
780 
781             // Disallow to cache static shared library
782             setGlobalSetting(SETTING_UNUSED_STATIC_SHARED_LIB_MIN_CACHE_PERIOD,
783                     Integer.toString(0));
784 
785             // TODO(205779832): There's a maximum two-seconds-delay before SettingsProvider persists
786             //  the settings. Waits for 3 seconds before reboot the device to ensure the setting is
787             //  persisted.
788             RunUtil.getDefault().sleep(3_000);
789             getDevice().reboot();
790 
791             // Waits for the uninstallation of the unused library to ensure the job has be executed
792             // correctly.
793             PollingCheck.check("Library " + STATIC_LIB_PROVIDER_RECURSIVE_NAME
794                             + " should be uninstalled", DEFAULT_TIMEOUT_MILLIS,
795                     () -> !checkLibrary(STATIC_LIB_PROVIDER_RECURSIVE_NAME));
796             assertWithMessage(
797                     "Library " + STATIC_LIB_PROVIDER_NAME + " should not be uninstalled")
798                     .that(checkLibrary(STATIC_LIB_PROVIDER_NAME)).isTrue();
799         } finally {
800             setGlobalSetting(SETTING_UNUSED_STATIC_SHARED_LIB_MIN_CACHE_PERIOD, null);
801             getDevice().uninstallPackage(STATIC_LIB_CONSUMER3_PKG);
802             getDevice().uninstallPackage(STATIC_LIB_PROVIDER7_PKG);
803             getDevice().uninstallPackage(STATIC_LIB_PROVIDER_RECURSIVE_PKG);
804         }
805     }
806 
807     @LargeTest
808     @AppModeFull
testInstallStaticSharedLib_notKillDependentApp()809     public void testInstallStaticSharedLib_notKillDependentApp() throws Exception {
810         getDevice().uninstallPackage(STATIC_LIB_CONSUMER1_PKG);
811         getDevice().uninstallPackage(STATIC_LIB_PROVIDER1_PKG);
812         getDevice().uninstallPackage(STATIC_LIB_PROVIDER_RECURSIVE_PKG);
813         try {
814             // Install library dependency
815             assertNull(install(STATIC_LIB_PROVIDER_RECURSIVE_APK));
816             // Install the first library
817             assertNull(install(STATIC_LIB_PROVIDER1_APK));
818             // Install the client
819             assertNull(install(STATIC_LIB_CONSUMER1_APK));
820 
821             // Bind the service in consumer1 app to verify that the app should not be killed when
822             // a new version static shared library installed.
823             runDeviceTests(STATIC_LIB_TEST_APP_PKG, STATIC_LIB_TEST_APP_CLASS_NAME,
824                     "testInstallStaticSharedLib_notKillDependentApp");
825         } finally {
826             getDevice().uninstallPackage(STATIC_LIB_CONSUMER1_PKG);
827             getDevice().uninstallPackage(STATIC_LIB_PROVIDER1_PKG);
828             getDevice().uninstallPackage(STATIC_LIB_PROVIDER_RECURSIVE_PKG);
829         }
830     }
831 
832     @AppModeFull
testSamegradeStaticSharedLib_killDependentApp()833     public void testSamegradeStaticSharedLib_killDependentApp() throws Exception {
834         getDevice().uninstallPackage(STATIC_LIB_CONSUMER1_PKG);
835         getDevice().uninstallPackage(STATIC_LIB_PROVIDER1_PKG);
836         getDevice().uninstallPackage(STATIC_LIB_PROVIDER_RECURSIVE_PKG);
837         try {
838             // Install library dependency
839             assertNull(install(STATIC_LIB_PROVIDER_RECURSIVE_APK));
840             // Install the first library
841             assertNull(install(STATIC_LIB_PROVIDER1_APK));
842             // Install the client
843             assertNull(install(STATIC_LIB_CONSUMER1_APK));
844 
845             // Bind the service in consumer1 app to verify that the app should be killed when
846             // the static shared library is re-installed.
847             runDeviceTests(STATIC_LIB_TEST_APP_PKG, STATIC_LIB_TEST_APP_CLASS_NAME,
848                     "testSamegradeStaticSharedLib_killDependentApp");
849         } finally {
850             getDevice().uninstallPackage(STATIC_LIB_CONSUMER1_PKG);
851             getDevice().uninstallPackage(STATIC_LIB_PROVIDER1_PKG);
852             getDevice().uninstallPackage(STATIC_LIB_PROVIDER_RECURSIVE_PKG);
853         }
854     }
855 
856     @AppModeFull
testStaticSharedLibInstall_broadcastReceived()857     public void testStaticSharedLibInstall_broadcastReceived() throws Exception {
858         getDevice().uninstallPackage(STATIC_LIB_PROVIDER1_PKG);
859         getDevice().uninstallPackage(STATIC_LIB_PROVIDER_RECURSIVE_PKG);
860         // Install library dependency
861         assertNull(install(STATIC_LIB_PROVIDER_RECURSIVE_APK));
862         runDeviceTests(STATIC_LIB_TEST_APP_PKG, STATIC_LIB_TEST_APP_CLASS_NAME,
863                     "testStaticSharedLibInstall_broadcastReceived");
864     }
865 
866     @AppModeFull
testStaticSharedLibInstall_incorrectInstallerPkgName_broadcastNotReceived()867     public void testStaticSharedLibInstall_incorrectInstallerPkgName_broadcastNotReceived()
868             throws Exception {
869         getDevice().uninstallPackage(STATIC_LIB_PROVIDER1_PKG);
870         getDevice().uninstallPackage(STATIC_LIB_PROVIDER_RECURSIVE_PKG);
871         // Install library dependency
872         assertNull(install(STATIC_LIB_PROVIDER_RECURSIVE_APK));
873         runDeviceTests(STATIC_LIB_TEST_APP_PKG, STATIC_LIB_TEST_APP_CLASS_NAME,
874                 "testStaticSharedLibInstall_incorrectInstallerPkgName_broadcastNotReceived");
875     }
876 
877     @AppModeFull
testStaticSharedLibUninstall_broadcastReceived()878     public void testStaticSharedLibUninstall_broadcastReceived()
879             throws Exception {
880         getDevice().uninstallPackage(STATIC_LIB_PROVIDER1_PKG);
881         getDevice().uninstallPackage(STATIC_LIB_PROVIDER_RECURSIVE_PKG);
882         // Install library dependency
883         assertNull(install(STATIC_LIB_PROVIDER_RECURSIVE_APK));
884         runDeviceTests(STATIC_LIB_TEST_APP_PKG, STATIC_LIB_TEST_APP_CLASS_NAME,
885                 "testStaticSharedLibUninstall_broadcastReceived");
886     }
887 
888     @AppModeFull
testStaticSharedLibUninstall_incorrectInstallerPkgName_broadcastNotReceived()889     public void testStaticSharedLibUninstall_incorrectInstallerPkgName_broadcastNotReceived()
890             throws Exception {
891         getDevice().uninstallPackage(STATIC_LIB_PROVIDER1_PKG);
892         getDevice().uninstallPackage(STATIC_LIB_PROVIDER_RECURSIVE_PKG);
893         // Install library dependency
894         assertNull(install(STATIC_LIB_PROVIDER_RECURSIVE_APK));
895         runDeviceTests(STATIC_LIB_TEST_APP_PKG, STATIC_LIB_TEST_APP_CLASS_NAME,
896                 "testStaticSharedLibUninstall_incorrectInstallerPkgName_broadcastNotReceived");
897     }
898 
899     @AppModeFull
testStaticSharedLibInstallOnSecondaryUser_broadcastReceivedByAllUsers()900     public void testStaticSharedLibInstallOnSecondaryUser_broadcastReceivedByAllUsers()
901             throws Exception {
902         getDevice().uninstallPackage(STATIC_LIB_PROVIDER1_PKG);
903         getDevice().uninstallPackage(STATIC_LIB_PROVIDER_RECURSIVE_PKG);
904 
905         runDeviceTests(STATIC_LIB_TEST_APP_PKG, STATIC_LIB_MULTI_USER_TEST_APP_CLASS_NAME,
906                 "testStaticSharedLibInstallOnSecondaryUser_broadcastReceivedByAllUsers");
907     }
908 
909     @AppModeFull
testStaticSharedLibUninstallOnAllUsers_broadcastReceivedByAllUsers()910     public void testStaticSharedLibUninstallOnAllUsers_broadcastReceivedByAllUsers()
911             throws Exception {
912         getDevice().uninstallPackage(STATIC_LIB_PROVIDER1_PKG);
913         getDevice().uninstallPackage(STATIC_LIB_PROVIDER_RECURSIVE_PKG);
914 
915         runDeviceTests(STATIC_LIB_TEST_APP_PKG, STATIC_LIB_MULTI_USER_TEST_APP_CLASS_NAME,
916                 "testStaticSharedLibUninstallOnAllUsers_broadcastReceivedByAllUsers");
917     }
918 
919     @AppModeFull
testCannotInstallAppWithBadCertDigestDeclared()920     public void testCannotInstallAppWithBadCertDigestDeclared() throws Exception {
921         getDevice().uninstallPackage(STATIC_LIB_CONSUMER1_PKG);
922         getDevice().uninstallPackage(STATIC_LIB_PROVIDER1_PKG);
923         getDevice().uninstallPackage(STATIC_LIB_PROVIDER_RECURSIVE_PKG);
924         try {
925             // Install library dependency
926             assertNull(install(STATIC_LIB_PROVIDER_RECURSIVE_APK));
927             // Install the first library
928             assertNull(install(STATIC_LIB_PROVIDER1_APK));
929             // Failed to install app with bad certificate digest
930             assertThat(install(STATIC_LIB_CONSUMER1_BAD_CERT_DIGEST_APK))
931                     .contains("INSTALL_FAILED_SHARED_LIBRARY_BAD_CERTIFICATE_DIGEST");
932         } finally {
933             getDevice().uninstallPackage(STATIC_LIB_CONSUMER1_PKG);
934             getDevice().uninstallPackage(STATIC_LIB_PROVIDER1_PKG);
935             getDevice().uninstallPackage(STATIC_LIB_PROVIDER_RECURSIVE_PKG);
936         }
937     }
938 
install(String apk)939     private String install(String apk) throws DeviceNotAvailableException, FileNotFoundException {
940         return install(apk, false);
941     }
install(String apk, boolean reinstall)942     private String install(String apk, boolean reinstall)
943             throws DeviceNotAvailableException, FileNotFoundException {
944         return getDevice().installPackage(mBuildHelper.getTestFile(apk), reinstall, false,
945                 apk.contains("consumer") && mInstantMode ? "--instant" : "");
946     }
947 
checkLibrary(String libName)948     private boolean checkLibrary(String libName) throws DeviceNotAvailableException {
949         final CommandResult result = getDevice().executeShellV2Command("pm list libraries");
950         if (result.getStatus() != CommandStatus.SUCCESS) {
951             fail("Failed to execute shell command: pm list libraries");
952         }
953         return Arrays.stream(result.getStdout().split("\n"))
954                 .map(line -> line.split(":")[1])
955                 .collect(Collectors.toList()).contains(libName);
956     }
957 
setGlobalSetting(String key, String value)958     private void setGlobalSetting(String key, String value) throws DeviceNotAvailableException {
959         final boolean deleteKey = (value == null);
960         final StringBuilder cmd = new StringBuilder("settings ");
961         if (deleteKey) {
962             cmd.append("delete ");
963         } else {
964             cmd.append("put ");
965         }
966         cmd.append("global ").append(key);
967         if (!deleteKey) {
968             cmd.append(" ").append(value);
969         }
970         final CommandResult res = getDevice().executeShellV2Command(cmd.toString());
971         if (res.getStatus() != CommandStatus.SUCCESS) {
972             fail("Failed to execute shell command: " + cmd);
973         }
974     }
975 }
976