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