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 android.platform.test.annotations.AppModeFull;
20 import android.platform.test.annotations.AppModeInstant;
21 
22 import com.android.compatibility.common.tradefed.build.CompatibilityBuildHelper;
23 import com.android.ddmlib.testrunner.RemoteAndroidTestRunner;
24 import com.android.ddmlib.testrunner.TestResult.TestStatus;
25 import com.android.tradefed.build.IBuildInfo;
26 import com.android.tradefed.device.DeviceNotAvailableException;
27 import com.android.tradefed.result.CollectingTestListener;
28 import com.android.tradefed.result.TestDescription;
29 import com.android.tradefed.result.TestResult;
30 import com.android.tradefed.result.TestRunResult;
31 import com.android.tradefed.testtype.DeviceTestCase;
32 import com.android.tradefed.testtype.IBuildReceiver;
33 
34 import java.io.FileNotFoundException;
35 import java.util.Map;
36 
37 public class StaticSharedLibsHostTests extends DeviceTestCase implements IBuildReceiver {
38     private static final String ANDROID_JUNIT_RUNNER_CLASS =
39             "androidx.test.runner.AndroidJUnitRunner";
40 
41     private static final String STATIC_LIB_PROVIDER_RECURSIVE_APK =
42             "CtsStaticSharedLibProviderRecursive.apk";
43     private static final String STATIC_LIB_PROVIDER_RECURSIVE_PKG =
44             "android.os.lib.provider.recursive";
45 
46     private static final String STATIC_LIB_PROVIDER1_APK = "CtsStaticSharedLibProviderApp1.apk";
47     private static final String STATIC_LIB_PROVIDER1_PKG = "android.os.lib.provider";
48 
49     private static final String STATIC_LIB_PROVIDER2_APK = "CtsStaticSharedLibProviderApp2.apk";
50     private static final String STATIC_LIB_PROVIDER2_PKG = "android.os.lib.provider";
51 
52     private static final String STATIC_LIB_PROVIDER3_APK = "CtsStaticSharedLibProviderApp3.apk";
53     private static final String STATIC_LIB_PROVIDER3_PKG = "android.os.lib.provider";
54 
55     private static final String STATIC_LIB_PROVIDER4_APK = "CtsStaticSharedLibProviderApp4.apk";
56     private static final String STATIC_LIB_PROVIDER4_PKG = "android.os.lib.provider";
57 
58     private static final String STATIC_LIB_PROVIDER5_APK = "CtsStaticSharedLibProviderApp5.apk";
59     private static final String STATIC_LIB_PROVIDER5_PKG = "android.os.lib.provider";
60 
61     private static final String STATIC_LIB_PROVIDER6_APK = "CtsStaticSharedLibProviderApp6.apk";
62     private static final String STATIC_LIB_PROVIDER6_PKG = "android.os.lib.provider";
63 
64     private static final String STATIC_LIB_PROVIDER7_APK = "CtsStaticSharedLibProviderApp7.apk";
65     private static final String STATIC_LIB_PROVIDER7_PKG = "android.os.lib.provider";
66 
67     private static final String STATIC_LIB_NATIVE_PROVIDER_APK =
68             "CtsStaticSharedNativeLibProvider.apk";
69     private static final String STATIC_LIB_NATIVE_PROVIDER_PKG =
70             "android.os.lib.provider";
71 
72     private static final String STATIC_LIB_NATIVE_PROVIDER_APK1 =
73             "CtsStaticSharedNativeLibProvider1.apk";
74     private static final String STATIC_LIB_NATIVE_PROVIDER_PKG1 =
75             "android.os.lib.provider";
76 
77     private static final String STATIC_LIB_CONSUMER1_APK = "CtsStaticSharedLibConsumerApp1.apk";
78     private static final String STATIC_LIB_CONSUMER1_PKG = "android.os.lib.consumer1";
79 
80     private static final String STATIC_LIB_CONSUMER2_APK = "CtsStaticSharedLibConsumerApp2.apk";
81     private static final String STATIC_LIB_CONSUMER2_PKG = "android.os.lib.consumer2";
82 
83     private static final String STATIC_LIB_CONSUMER3_APK = "CtsStaticSharedLibConsumerApp3.apk";
84     private static final String STATIC_LIB_CONSUMER3_PKG = "android.os.lib.consumer3";
85 
86     private static final String STATIC_LIB_NATIVE_CONSUMER_APK
87             = "CtsStaticSharedNativeLibConsumer.apk";
88     private static final String STATIC_LIB_NATIVE_CONSUMER_PKG
89             = "android.os.lib.consumer";
90 
91     private CompatibilityBuildHelper mBuildHelper;
92     private boolean mInstantMode = false;
93 
94     @Override
setBuild(IBuildInfo buildInfo)95     public void setBuild(IBuildInfo buildInfo) {
96         mBuildHelper = new CompatibilityBuildHelper(buildInfo);
97     }
98 
99     @AppModeInstant
testInstallSharedLibraryInstantMode()100     public void testInstallSharedLibraryInstantMode() throws Exception {
101         mInstantMode = true;
102         doTestInstallSharedLibrary();
103     }
104 
105     @AppModeFull
testInstallSharedLibraryFullMode()106     public void testInstallSharedLibraryFullMode() throws Exception {
107         doTestInstallSharedLibrary();
108     }
109 
doTestInstallSharedLibrary()110     private void doTestInstallSharedLibrary() throws Exception {
111         getDevice().uninstallPackage(STATIC_LIB_PROVIDER1_PKG);
112         getDevice().uninstallPackage(STATIC_LIB_PROVIDER2_PKG);
113         getDevice().uninstallPackage(STATIC_LIB_PROVIDER_RECURSIVE_PKG);
114         try {
115             // Install library dependency
116             assertNull(install(STATIC_LIB_PROVIDER_RECURSIVE_APK));
117             // Install version 1
118             assertNull(install(STATIC_LIB_PROVIDER1_APK));
119             // Install version 2
120             assertNull(install(STATIC_LIB_PROVIDER2_APK));
121             // Uninstall version 1
122             assertNull(getDevice().uninstallPackage(STATIC_LIB_PROVIDER1_PKG));
123             // Uninstall version 2
124             assertNull(getDevice().uninstallPackage(STATIC_LIB_PROVIDER2_PKG));
125             // Uninstall dependency
126             assertNull(getDevice().uninstallPackage(STATIC_LIB_PROVIDER_RECURSIVE_PKG));
127         } finally {
128             getDevice().uninstallPackage(STATIC_LIB_PROVIDER1_PKG);
129             getDevice().uninstallPackage(STATIC_LIB_PROVIDER2_PKG);
130             getDevice().uninstallPackage(STATIC_LIB_PROVIDER_RECURSIVE_PKG);
131         }
132     }
133 
134     @AppModeInstant
testCannotInstallSharedLibraryWithMissingDependencyInstantMode()135     public void testCannotInstallSharedLibraryWithMissingDependencyInstantMode() throws Exception {
136         mInstantMode = true;
137         doTestCannotInstallSharedLibraryWithMissingDependency();
138     }
139 
140     @AppModeFull
testCannotInstallSharedLibraryWithMissingDependencyFullMode()141     public void testCannotInstallSharedLibraryWithMissingDependencyFullMode() throws Exception {
142         doTestCannotInstallSharedLibraryWithMissingDependency();
143     }
144 
doTestCannotInstallSharedLibraryWithMissingDependency()145     private void doTestCannotInstallSharedLibraryWithMissingDependency() throws Exception {
146         getDevice().uninstallPackage(STATIC_LIB_PROVIDER1_PKG);
147         getDevice().uninstallPackage(STATIC_LIB_PROVIDER2_PKG);
148         try {
149             // Install version 1 - should fail - no dependency
150             assertNotNull(install(STATIC_LIB_PROVIDER1_APK));
151         } finally {
152             getDevice().uninstallPackage(STATIC_LIB_PROVIDER1_PKG);
153             getDevice().uninstallPackage(STATIC_LIB_PROVIDER2_PKG);
154         }
155     }
156 
testLoadCodeAndResourcesFromSharedLibraryRecursively()157     public void testLoadCodeAndResourcesFromSharedLibraryRecursively() throws Exception {
158         getDevice().uninstallPackage(STATIC_LIB_CONSUMER1_PKG);
159         getDevice().uninstallPackage(STATIC_LIB_PROVIDER1_PKG);
160         getDevice().uninstallPackage(STATIC_LIB_PROVIDER_RECURSIVE_PKG);
161         try {
162             // Install library dependency
163             assertNull(install(STATIC_LIB_PROVIDER_RECURSIVE_APK));
164             // Install the library
165             assertNull(install(STATIC_LIB_PROVIDER1_APK));
166             // Install the client
167             assertNull(install(STATIC_LIB_CONSUMER1_APK));
168             // Try to load code and resources
169             runDeviceTests(STATIC_LIB_CONSUMER1_PKG,
170                     "android.os.lib.consumer1.UseSharedLibraryTest",
171                     "testLoadCodeAndResources");
172         } finally {
173             getDevice().uninstallPackage(STATIC_LIB_CONSUMER1_PKG);
174             getDevice().uninstallPackage(STATIC_LIB_PROVIDER1_PKG);
175             getDevice().uninstallPackage(STATIC_LIB_PROVIDER_RECURSIVE_PKG);
176         }
177     }
178 
testLoadCodeAndResourcesFromSharedLibraryRecursivelyUpdate()179     public void testLoadCodeAndResourcesFromSharedLibraryRecursivelyUpdate() throws Exception {
180         getDevice().uninstallPackage(STATIC_LIB_CONSUMER1_PKG);
181         getDevice().uninstallPackage(STATIC_LIB_PROVIDER1_PKG);
182         getDevice().uninstallPackage(STATIC_LIB_PROVIDER_RECURSIVE_PKG);
183         try {
184             // Install library dependency
185             assertNull(install(STATIC_LIB_PROVIDER_RECURSIVE_APK));
186             // Install the library
187             assertNull(install(STATIC_LIB_PROVIDER1_APK));
188             // Install the client
189             assertNull(install(STATIC_LIB_CONSUMER1_APK));
190             // Try to load code and resources
191             runDeviceTests(STATIC_LIB_CONSUMER1_PKG,
192                     "android.os.lib.consumer1.UseSharedLibraryTest",
193                     "testLoadCodeAndResources");
194             // Install library dependency
195             assertNull(install(STATIC_LIB_PROVIDER_RECURSIVE_APK, true));
196             // Try to load code and resources
197             runDeviceTests(STATIC_LIB_CONSUMER1_PKG,
198                     "android.os.lib.consumer1.UseSharedLibraryTest",
199                     "testLoadCodeAndResources");
200             // Install the library
201             assertNull(install(STATIC_LIB_PROVIDER1_APK, true));
202             // Try to load code and resources
203             runDeviceTests(STATIC_LIB_CONSUMER1_PKG,
204                     "android.os.lib.consumer1.UseSharedLibraryTest",
205                     "testLoadCodeAndResources");
206         } finally {
207             getDevice().uninstallPackage(STATIC_LIB_CONSUMER1_PKG);
208             getDevice().uninstallPackage(STATIC_LIB_PROVIDER1_PKG);
209             getDevice().uninstallPackage(STATIC_LIB_PROVIDER_RECURSIVE_PKG);
210         }
211     }
212 
213     @AppModeInstant
testCannotUninstallUsedSharedLibrary1InstantMode()214     public void testCannotUninstallUsedSharedLibrary1InstantMode() throws Exception {
215         mInstantMode = true;
216         doTestCannotUninstallUsedSharedLibrary1();
217     }
218 
219     @AppModeFull
testCannotUninstallUsedSharedLibrary1FullMode()220     public void testCannotUninstallUsedSharedLibrary1FullMode() throws Exception {
221         doTestCannotUninstallUsedSharedLibrary1();
222     }
223 
doTestCannotUninstallUsedSharedLibrary1()224     private void doTestCannotUninstallUsedSharedLibrary1() throws Exception {
225         getDevice().uninstallPackage(STATIC_LIB_PROVIDER1_PKG);
226         getDevice().uninstallPackage(STATIC_LIB_PROVIDER_RECURSIVE_PKG);
227         try {
228             // Install library dependency
229             assertNull(install(STATIC_LIB_PROVIDER_RECURSIVE_APK));
230             // Install the library
231             assertNull(install(STATIC_LIB_PROVIDER1_APK));
232             // The library dependency cannot be uninstalled
233             assertNotNull(getDevice().uninstallPackage(STATIC_LIB_PROVIDER_RECURSIVE_PKG));
234             // Now the library dependency can be uninstalled
235             assertNull(getDevice().uninstallPackage(STATIC_LIB_PROVIDER1_PKG));
236             // Uninstall dependency
237             assertNull(getDevice().uninstallPackage(STATIC_LIB_PROVIDER_RECURSIVE_PKG));
238         } finally {
239             getDevice().uninstallPackage(STATIC_LIB_PROVIDER1_PKG);
240             getDevice().uninstallPackage(STATIC_LIB_PROVIDER_RECURSIVE_PKG);
241         }
242     }
243 
244     @AppModeInstant
testCannotUninstallUsedSharedLibrary2InstantMode()245     public void testCannotUninstallUsedSharedLibrary2InstantMode() throws Exception {
246         mInstantMode = true;
247         doTestCannotUninstallUsedSharedLibrary2();
248     }
249 
250     @AppModeFull
testCannotUninstallUsedSharedLibrary2FullMode()251     public void testCannotUninstallUsedSharedLibrary2FullMode() throws Exception {
252         doTestCannotUninstallUsedSharedLibrary2();
253     }
254 
doTestCannotUninstallUsedSharedLibrary2()255     private void doTestCannotUninstallUsedSharedLibrary2() throws Exception {
256         getDevice().uninstallPackage(STATIC_LIB_CONSUMER1_PKG);
257         getDevice().uninstallPackage(STATIC_LIB_PROVIDER1_PKG);
258         getDevice().uninstallPackage(STATIC_LIB_PROVIDER_RECURSIVE_PKG);
259         try {
260             // Install library dependency
261             assertNull(install(STATIC_LIB_PROVIDER_RECURSIVE_APK));
262             // Install the library
263             assertNull(install(STATIC_LIB_PROVIDER1_APK));
264             // Install the client
265             assertNull(install(STATIC_LIB_CONSUMER1_APK));
266             // The library cannot be uninstalled
267             assertNotNull(getDevice().uninstallPackage(STATIC_LIB_PROVIDER1_PKG));
268             // Uninstall the client
269             assertNull(getDevice().uninstallPackage(STATIC_LIB_CONSUMER1_PKG));
270             // Now the library can be uninstalled
271             assertNull(getDevice().uninstallPackage(STATIC_LIB_PROVIDER1_PKG));
272             // Uninstall dependency
273             assertNull(getDevice().uninstallPackage(STATIC_LIB_PROVIDER_RECURSIVE_PKG));
274         } finally {
275             getDevice().uninstallPackage(STATIC_LIB_CONSUMER1_PKG);
276             getDevice().uninstallPackage(STATIC_LIB_PROVIDER1_PKG);
277             getDevice().uninstallPackage(STATIC_LIB_PROVIDER_RECURSIVE_PKG);
278         }
279     }
280 
281     @AppModeInstant
testLibraryVersionsAndVersionCodesSameOrderInstantMode()282     public void testLibraryVersionsAndVersionCodesSameOrderInstantMode() throws Exception {
283         mInstantMode = true;
284         doTestLibraryVersionsAndVersionCodesSameOrder();
285     }
286 
287     @AppModeFull
testLibraryVersionsAndVersionCodesSameOrderFullMode()288     public void testLibraryVersionsAndVersionCodesSameOrderFullMode() throws Exception {
289         doTestLibraryVersionsAndVersionCodesSameOrder();
290     }
291 
doTestLibraryVersionsAndVersionCodesSameOrder()292     private void doTestLibraryVersionsAndVersionCodesSameOrder() throws Exception {
293         getDevice().uninstallPackage(STATIC_LIB_PROVIDER1_PKG);
294         getDevice().uninstallPackage(STATIC_LIB_PROVIDER2_PKG);
295         getDevice().uninstallPackage(STATIC_LIB_PROVIDER3_PKG);
296         getDevice().uninstallPackage(STATIC_LIB_PROVIDER_RECURSIVE_PKG);
297         try {
298             // Install library dependency
299             assertNull(install(STATIC_LIB_PROVIDER_RECURSIVE_APK));
300             // Install library version 1 with version code 1
301             assertNull(install(STATIC_LIB_PROVIDER1_APK));
302             // Install library version 2 with version code 4
303             assertNull(install(STATIC_LIB_PROVIDER2_APK));
304             // Shouldn't be able to install library version 3 with version code 3
305             assertNotNull(install(STATIC_LIB_PROVIDER3_APK));
306         } finally {
307             getDevice().uninstallPackage(STATIC_LIB_PROVIDER1_PKG);
308             getDevice().uninstallPackage(STATIC_LIB_PROVIDER2_PKG);
309             getDevice().uninstallPackage(STATIC_LIB_PROVIDER3_PKG);
310             getDevice().uninstallPackage(STATIC_LIB_PROVIDER_RECURSIVE_PKG);
311         }
312     }
313 
314     @AppModeInstant
testCannotInstallAppWithMissingLibraryInstantMode()315     public void testCannotInstallAppWithMissingLibraryInstantMode() throws Exception {
316         mInstantMode = true;
317         doTestCannotInstallAppWithMissingLibrary();
318     }
319 
320     @AppModeFull
testCannotInstallAppWithMissingLibraryFullMode()321     public void testCannotInstallAppWithMissingLibraryFullMode() throws Exception {
322         doTestCannotInstallAppWithMissingLibrary();
323     }
324 
doTestCannotInstallAppWithMissingLibrary()325     private void doTestCannotInstallAppWithMissingLibrary() throws Exception {
326         getDevice().uninstallPackage(STATIC_LIB_CONSUMER1_PKG);
327         try {
328             // Shouldn't be able to install an app if a dependency lib is missing
329             assertNotNull(install(STATIC_LIB_CONSUMER1_APK));
330         } finally {
331             getDevice().uninstallPackage(STATIC_LIB_CONSUMER1_PKG);
332         }
333     }
334 
335     @AppModeFull
testCanReplaceLibraryIfVersionAndVersionCodeSame()336     public void testCanReplaceLibraryIfVersionAndVersionCodeSame() throws Exception {
337         getDevice().uninstallPackage(STATIC_LIB_PROVIDER1_PKG);
338         getDevice().uninstallPackage(STATIC_LIB_PROVIDER_RECURSIVE_PKG);
339         try {
340             // Install library dependency
341             assertNull(install(STATIC_LIB_PROVIDER_RECURSIVE_APK));
342             // Install a library
343             assertNull(install(STATIC_LIB_PROVIDER1_APK));
344             // Can reinstall the library if version and version code same
345             assertNull(install(STATIC_LIB_PROVIDER1_APK));
346         } finally {
347             getDevice().uninstallPackage(STATIC_LIB_PROVIDER1_PKG);
348             getDevice().uninstallPackage(STATIC_LIB_PROVIDER_RECURSIVE_PKG);
349         }
350     }
351 
352     @AppModeInstant
testUninstallSpecificLibraryVersionInstantMode()353     public void testUninstallSpecificLibraryVersionInstantMode() throws Exception {
354         mInstantMode = true;
355         doTestUninstallSpecificLibraryVersion();
356     }
357 
358     @AppModeFull
testUninstallSpecificLibraryVersionFullMode()359     public void testUninstallSpecificLibraryVersionFullMode() throws Exception {
360         doTestUninstallSpecificLibraryVersion();
361     }
362 
doTestUninstallSpecificLibraryVersion()363     private void doTestUninstallSpecificLibraryVersion() throws Exception {
364         getDevice().uninstallPackage(STATIC_LIB_PROVIDER1_PKG);
365         getDevice().uninstallPackage(STATIC_LIB_PROVIDER_RECURSIVE_PKG);
366         try {
367             // Install library dependency
368             assertNull(install(STATIC_LIB_PROVIDER_RECURSIVE_APK));
369             // Install library version 1 with version code 1
370             assertNull(install(STATIC_LIB_PROVIDER1_APK));
371             // Install library version 2 with version code 4
372             assertNull(install(STATIC_LIB_PROVIDER2_APK));
373             // Uninstall the library package with version code 4 (version 2)
374             assertTrue(getDevice().executeShellCommand("pm uninstall --versionCode 4 "
375                     + STATIC_LIB_PROVIDER1_PKG).startsWith("Success"));
376             // Uninstall the library package with version code 1 (version 1)
377             assertTrue(getDevice().executeShellCommand("pm uninstall "
378                     + STATIC_LIB_PROVIDER1_PKG).startsWith("Success"));
379         } finally {
380             getDevice().uninstallPackage(STATIC_LIB_PROVIDER1_PKG);
381             getDevice().uninstallPackage(STATIC_LIB_PROVIDER_RECURSIVE_PKG);
382         }
383     }
384 
385     @AppModeInstant
testKeyRotationInstantMode()386     public void testKeyRotationInstantMode() throws Exception {
387         mInstantMode = true;
388         doTestKeyRotation();
389     }
390 
391     @AppModeFull
testKeyRotationFullMode()392     public void testKeyRotationFullMode() throws Exception {
393         doTestKeyRotation();
394     }
395 
doTestKeyRotation()396     private void doTestKeyRotation() throws Exception {
397         getDevice().uninstallPackage(STATIC_LIB_CONSUMER2_PKG);
398         getDevice().uninstallPackage(STATIC_LIB_PROVIDER2_PKG);
399         getDevice().uninstallPackage(STATIC_LIB_PROVIDER4_PKG);
400         try {
401             // Install a library version specifying an upgrade key set
402             assertNull(install(STATIC_LIB_PROVIDER2_APK));
403             // Install a newer library signed with the upgrade key set
404             assertNull(install(STATIC_LIB_PROVIDER4_APK));
405             // Install a client that depends on the upgraded key set
406             assertNull(install(STATIC_LIB_CONSUMER2_APK));
407             // Ensure code and resources can be loaded
408             runDeviceTests(STATIC_LIB_CONSUMER2_PKG,
409                     "android.os.lib.consumer2.UseSharedLibraryTest",
410                     "testLoadCodeAndResources");
411         } finally {
412             getDevice().uninstallPackage(STATIC_LIB_CONSUMER2_PKG);
413             getDevice().uninstallPackage(STATIC_LIB_PROVIDER2_PKG);
414             getDevice().uninstallPackage(STATIC_LIB_PROVIDER4_PKG);
415         }
416     }
417 
418     @AppModeInstant
testCannotInstallIncorrectlySignedLibraryInstantMode()419     public void testCannotInstallIncorrectlySignedLibraryInstantMode() throws Exception {
420         mInstantMode = true;
421         doTestCannotInstallIncorrectlySignedLibrary();
422     }
423 
424     @AppModeFull
testCannotInstallIncorrectlySignedLibraryFullMode()425     public void testCannotInstallIncorrectlySignedLibraryFullMode() throws Exception {
426         doTestCannotInstallIncorrectlySignedLibrary();
427     }
428 
doTestCannotInstallIncorrectlySignedLibrary()429     private void doTestCannotInstallIncorrectlySignedLibrary() throws Exception {
430         getDevice().uninstallPackage(STATIC_LIB_PROVIDER1_PKG);
431         getDevice().uninstallPackage(STATIC_LIB_PROVIDER4_PKG);
432         getDevice().uninstallPackage(STATIC_LIB_PROVIDER_RECURSIVE_PKG);
433         try {
434             // Install library dependency
435             assertNull(install(STATIC_LIB_PROVIDER_RECURSIVE_APK));
436             // Install a library version not specifying an upgrade key set
437             assertNull(install(STATIC_LIB_PROVIDER1_APK));
438             // Shouldn't be able to install a newer version signed differently
439             assertNotNull(install(STATIC_LIB_PROVIDER4_APK));
440         } finally {
441             getDevice().uninstallPackage(STATIC_LIB_PROVIDER1_PKG);
442             getDevice().uninstallPackage(STATIC_LIB_PROVIDER4_PKG);
443             getDevice().uninstallPackage(STATIC_LIB_PROVIDER_RECURSIVE_PKG);
444         }
445     }
446 
447     @AppModeInstant
testLibraryAndPackageNameCanMatchInstantMode()448     public void testLibraryAndPackageNameCanMatchInstantMode() throws Exception {
449         mInstantMode = true;
450         doTestLibraryAndPackageNameCanMatch();
451     }
452 
453     @AppModeFull
testLibraryAndPackageNameCanMatchFullMode()454     public void testLibraryAndPackageNameCanMatchFullMode() throws Exception {
455         doTestLibraryAndPackageNameCanMatch();
456     }
457 
doTestLibraryAndPackageNameCanMatch()458     private void doTestLibraryAndPackageNameCanMatch() throws Exception {
459         getDevice().uninstallPackage(STATIC_LIB_PROVIDER5_PKG);
460         getDevice().uninstallPackage(STATIC_LIB_PROVIDER6_PKG);
461         try {
462             // Install a library with same name as package should work.
463             assertNull(install(STATIC_LIB_PROVIDER5_APK));
464             // Install a library with same name as package should work.
465             assertNull(install(STATIC_LIB_PROVIDER6_APK));
466         } finally {
467             getDevice().uninstallPackage(STATIC_LIB_PROVIDER5_PKG);
468             getDevice().uninstallPackage(STATIC_LIB_PROVIDER6_PKG);
469         }
470     }
471 
472     @AppModeInstant
testGetSharedLibrariesInstantMode()473     public void testGetSharedLibrariesInstantMode() throws Exception {
474         mInstantMode = true;
475         doTestGetSharedLibraries();
476     }
477 
478     @AppModeFull
testGetSharedLibrariesFullMode()479     public void testGetSharedLibrariesFullMode() throws Exception {
480         doTestGetSharedLibraries();
481     }
482 
doTestGetSharedLibraries()483     private void doTestGetSharedLibraries() throws Exception {
484         getDevice().uninstallPackage(STATIC_LIB_CONSUMER1_PKG);
485         getDevice().uninstallPackage(STATIC_LIB_CONSUMER2_PKG);
486         getDevice().uninstallPackage(STATIC_LIB_PROVIDER1_PKG);
487         getDevice().uninstallPackage(STATIC_LIB_PROVIDER2_PKG);
488         getDevice().uninstallPackage(STATIC_LIB_PROVIDER4_PKG);
489         getDevice().uninstallPackage(STATIC_LIB_PROVIDER_RECURSIVE_PKG);
490         try {
491             // Install library dependency
492             assertNull(install(STATIC_LIB_PROVIDER_RECURSIVE_APK));
493             // Install the first library
494             assertNull(install(STATIC_LIB_PROVIDER1_APK));
495             // Install the second library
496             assertNull(install(STATIC_LIB_PROVIDER2_APK));
497             // Install the third library
498             assertNull(install(STATIC_LIB_PROVIDER4_APK));
499             // Install the first client
500             assertNull(install(STATIC_LIB_CONSUMER1_APK));
501             // Install the second client
502             assertNull(install(STATIC_LIB_CONSUMER2_APK));
503             // Ensure the first library has the REQUEST_INSTALL_PACKAGES app op
504             getDevice().executeShellV2Command("appops set "
505                     + STATIC_LIB_CONSUMER1_PKG
506                     + " REQUEST_INSTALL_PACKAGES allow");
507             // Ensure libraries are properly reported
508             runDeviceTests(STATIC_LIB_CONSUMER1_PKG,
509                     "android.os.lib.consumer1.UseSharedLibraryTest",
510                     "testSharedLibrariesProperlyReported");
511         } finally {
512             getDevice().uninstallPackage(STATIC_LIB_CONSUMER1_PKG);
513             getDevice().uninstallPackage(STATIC_LIB_CONSUMER2_PKG);
514             getDevice().uninstallPackage(STATIC_LIB_PROVIDER1_PKG);
515             getDevice().uninstallPackage(STATIC_LIB_PROVIDER2_PKG);
516             getDevice().uninstallPackage(STATIC_LIB_PROVIDER4_PKG);
517             getDevice().uninstallPackage(STATIC_LIB_PROVIDER_RECURSIVE_PKG);
518         }
519     }
520 
521     @AppModeFull(
522             reason = "getDeclaredSharedLibraries() requires ACCESS_SHARED_LIBRARIES permission")
testGetDeclaredSharedLibraries()523     public void testGetDeclaredSharedLibraries() throws Exception {
524         getDevice().uninstallPackage(STATIC_LIB_CONSUMER1_PKG);
525         getDevice().uninstallPackage(STATIC_LIB_CONSUMER2_PKG);
526         getDevice().uninstallPackage(STATIC_LIB_PROVIDER1_PKG);
527         getDevice().uninstallPackage(STATIC_LIB_PROVIDER2_PKG);
528         getDevice().uninstallPackage(STATIC_LIB_PROVIDER4_PKG);
529         getDevice().uninstallPackage(STATIC_LIB_PROVIDER_RECURSIVE_PKG);
530         try {
531             // Install library dependency
532             assertNull(install(STATIC_LIB_PROVIDER_RECURSIVE_APK));
533             // Install the first library
534             assertNull(install(STATIC_LIB_PROVIDER1_APK));
535             // Install the second library
536             assertNull(install(STATIC_LIB_PROVIDER2_APK));
537             // Install the third library
538             assertNull(install(STATIC_LIB_PROVIDER4_APK));
539             // Install the first client
540             assertNull(install(STATIC_LIB_CONSUMER1_APK));
541             // Install the second client
542             assertNull(install(STATIC_LIB_CONSUMER2_APK));
543             // Ensure declared libraries are properly reported
544             runDeviceTests(STATIC_LIB_CONSUMER1_PKG,
545                     "android.os.lib.consumer1.UseSharedLibraryTest",
546                     "testDeclaredSharedLibrariesProperlyReported");
547         } finally {
548             getDevice().uninstallPackage(STATIC_LIB_CONSUMER1_PKG);
549             getDevice().uninstallPackage(STATIC_LIB_CONSUMER2_PKG);
550             getDevice().uninstallPackage(STATIC_LIB_PROVIDER1_PKG);
551             getDevice().uninstallPackage(STATIC_LIB_PROVIDER2_PKG);
552             getDevice().uninstallPackage(STATIC_LIB_PROVIDER4_PKG);
553             getDevice().uninstallPackage(STATIC_LIB_PROVIDER_RECURSIVE_PKG);
554         }
555     }
556 
557     @AppModeInstant
testAppCanSeeOnlyLibrariesItDependOnInstantMode()558     public void testAppCanSeeOnlyLibrariesItDependOnInstantMode() throws Exception {
559         mInstantMode = true;
560         doTestAppCanSeeOnlyLibrariesItDependOn();
561     }
562 
563     @AppModeFull
testAppCanSeeOnlyLibrariesItDependOnFullMode()564     public void testAppCanSeeOnlyLibrariesItDependOnFullMode() throws Exception {
565         doTestAppCanSeeOnlyLibrariesItDependOn();
566     }
567 
doTestAppCanSeeOnlyLibrariesItDependOn()568     private void doTestAppCanSeeOnlyLibrariesItDependOn() throws Exception {
569         getDevice().uninstallPackage(STATIC_LIB_CONSUMER1_PKG);
570         getDevice().uninstallPackage(STATIC_LIB_PROVIDER1_PKG);
571         getDevice().uninstallPackage(STATIC_LIB_PROVIDER2_PKG);
572         getDevice().uninstallPackage(STATIC_LIB_PROVIDER_RECURSIVE_PKG);
573         try {
574             // Install library dependency
575             assertNull(install(STATIC_LIB_PROVIDER_RECURSIVE_APK));
576             // Install the first library
577             assertNull(install(STATIC_LIB_PROVIDER1_APK));
578             // Install the second library
579             assertNull(install(STATIC_LIB_PROVIDER2_APK));
580             // Install the client
581             assertNull(install(STATIC_LIB_CONSUMER1_APK));
582             // Ensure the client can see only the lib it depends on
583             runDeviceTests(STATIC_LIB_CONSUMER1_PKG,
584                     "android.os.lib.consumer1.UseSharedLibraryTest",
585                     "testAppCanSeeOnlyLibrariesItDependOn");
586         } finally {
587             getDevice().uninstallPackage(STATIC_LIB_CONSUMER1_PKG);
588             getDevice().uninstallPackage(STATIC_LIB_PROVIDER1_PKG);
589             getDevice().uninstallPackage(STATIC_LIB_PROVIDER2_PKG);
590             getDevice().uninstallPackage(STATIC_LIB_PROVIDER_RECURSIVE_PKG);
591         }
592     }
593 
594     @AppModeInstant
testLoadCodeFromNativeLibInstantMode()595     public void testLoadCodeFromNativeLibInstantMode() throws Exception {
596         mInstantMode = true;
597         doTestLoadCodeFromNativeLib();
598     }
599 
600     @AppModeFull
testLoadCodeFromNativeLibFullMode()601     public void testLoadCodeFromNativeLibFullMode() throws Exception {
602         doTestLoadCodeFromNativeLib();
603     }
604 
doTestLoadCodeFromNativeLib()605     private void doTestLoadCodeFromNativeLib() throws Exception {
606         getDevice().uninstallPackage(STATIC_LIB_NATIVE_CONSUMER_PKG);
607         getDevice().uninstallPackage(STATIC_LIB_NATIVE_PROVIDER_PKG);
608         try {
609             // Install library
610             assertNull(install(STATIC_LIB_NATIVE_PROVIDER_APK));
611             // Install the library client
612             assertNull(install(STATIC_LIB_NATIVE_CONSUMER_APK));
613             // Ensure the client can load native code from the library
614             runDeviceTests(STATIC_LIB_NATIVE_CONSUMER_PKG,
615                     "android.os.lib.consumer.UseSharedLibraryTest",
616                     "testLoadNativeCode");
617         } finally {
618             getDevice().uninstallPackage(STATIC_LIB_NATIVE_CONSUMER_PKG);
619             getDevice().uninstallPackage(STATIC_LIB_NATIVE_PROVIDER_PKG);
620         }
621     }
622 
623     @AppModeInstant
testLoadCodeFromNativeLibMultiArchViolationInstantMode()624     public void testLoadCodeFromNativeLibMultiArchViolationInstantMode() throws Exception {
625         mInstantMode = true;
626         doTestLoadCodeFromNativeLibMultiArchViolation();
627     }
628 
629     @AppModeFull
testLoadCodeFromNativeLibMultiArchViolationFullMode()630     public void testLoadCodeFromNativeLibMultiArchViolationFullMode() throws Exception {
631         doTestLoadCodeFromNativeLibMultiArchViolation();
632     }
633 
doTestLoadCodeFromNativeLibMultiArchViolation()634     private void doTestLoadCodeFromNativeLibMultiArchViolation() throws Exception {
635         getDevice().uninstallPackage(STATIC_LIB_NATIVE_PROVIDER_PKG1);
636         try {
637             // Cannot install the library with native code if not multi-arch
638             assertNotNull(install(STATIC_LIB_NATIVE_PROVIDER_APK1));
639         } finally {
640             getDevice().uninstallPackage(STATIC_LIB_NATIVE_PROVIDER_PKG1);
641         }
642     }
643 
644     @AppModeInstant
testLoadCodeAndResourcesFromSharedLibrarySignedWithTwoCertsInstantMode()645     public void testLoadCodeAndResourcesFromSharedLibrarySignedWithTwoCertsInstantMode() throws Exception {
646         mInstantMode = true;
647         doTestLoadCodeAndResourcesFromSharedLibrarySignedWithTwoCerts();
648     }
649 
650     @AppModeFull
testLoadCodeAndResourcesFromSharedLibrarySignedWithTwoCertsFullMode()651     public void testLoadCodeAndResourcesFromSharedLibrarySignedWithTwoCertsFullMode() throws Exception {
652         doTestLoadCodeAndResourcesFromSharedLibrarySignedWithTwoCerts();
653     }
654 
doTestLoadCodeAndResourcesFromSharedLibrarySignedWithTwoCerts()655     private void doTestLoadCodeAndResourcesFromSharedLibrarySignedWithTwoCerts()
656             throws Exception {
657         getDevice().uninstallPackage(STATIC_LIB_CONSUMER3_PKG);
658         getDevice().uninstallPackage(STATIC_LIB_PROVIDER7_PKG);
659         try {
660             // Install the library
661             assertNull(install(STATIC_LIB_PROVIDER7_APK));
662             // Install the client
663             assertNull(install(STATIC_LIB_CONSUMER3_APK));
664             // Try to load code and resources
665             runDeviceTests(STATIC_LIB_CONSUMER3_PKG,
666                     "android.os.lib.consumer3.UseSharedLibraryTest",
667                     "testLoadCodeAndResources");
668         } finally {
669             getDevice().uninstallPackage(STATIC_LIB_CONSUMER3_PKG);
670             getDevice().uninstallPackage(STATIC_LIB_PROVIDER7_PKG);
671         }
672     }
673 
testSamegradeStaticSharedLibByAdb()674     public void testSamegradeStaticSharedLibByAdb() throws Exception {
675         getDevice().uninstallPackage(STATIC_LIB_PROVIDER5_PKG);
676         try {
677             assertNull(install(STATIC_LIB_PROVIDER5_APK));
678             assertNull(install(STATIC_LIB_PROVIDER5_APK, true /*reinstall*/));
679         } finally {
680             getDevice().uninstallPackage(STATIC_LIB_PROVIDER5_PKG);
681         }
682     }
683 
684     @AppModeFull(reason = "Instant app cannot get package installer service")
testCannotSamegradeStaticSharedLibByInstaller()685     public void testCannotSamegradeStaticSharedLibByInstaller() throws Exception {
686         runDeviceTests("android.os.lib.app",
687                 "android.os.lib.app.StaticSharedLibsTests",
688                 "testSamegradeStaticSharedLibFail");
689     }
690 
runDeviceTests(String packageName, String testClassName, String testMethodName)691     private void runDeviceTests(String packageName, String testClassName,
692             String testMethodName) throws DeviceNotAvailableException {
693         RemoteAndroidTestRunner testRunner = new RemoteAndroidTestRunner(packageName,
694                 ANDROID_JUNIT_RUNNER_CLASS, getDevice().getIDevice());
695         testRunner.setMethodName(testClassName, testMethodName);
696         CollectingTestListener listener = new CollectingTestListener();
697 
698         getDevice().runInstrumentationTests(testRunner, listener);
699 
700         final TestRunResult result = listener.getCurrentRunResults();
701         if (result.isRunFailure()) {
702             throw new AssertionError("Failed to successfully run device tests for "
703                     + result.getName() + ": " + result.getRunFailureMessage());
704         }
705         if (result.getNumTests() == 0) {
706             throw new AssertionError("No tests were run on the device");
707         }
708         if (result.hasFailedTests()) {
709             // build a meaningful error message
710             StringBuilder errorBuilder = new StringBuilder("on-device tests failed:\n");
711             for (Map.Entry<TestDescription, TestResult> resultEntry :
712                     result.getTestResults().entrySet()) {
713                 if (!resultEntry.getValue().getStatus().equals(TestStatus.PASSED)) {
714                     errorBuilder.append(resultEntry.getKey().toString());
715                     errorBuilder.append(":\n");
716                     errorBuilder.append(resultEntry.getValue().getStackTrace());
717                 }
718             }
719             throw new AssertionError(errorBuilder.toString());
720         }
721     }
722 
install(String apk)723     private String install(String apk) throws DeviceNotAvailableException, FileNotFoundException {
724         return install(apk, false);
725     }
install(String apk, boolean reinstall)726     private String install(String apk, boolean reinstall)
727             throws DeviceNotAvailableException, FileNotFoundException {
728         return getDevice().installPackage(mBuildHelper.getTestFile(apk), reinstall, false,
729                 apk.contains("consumer") && mInstantMode ? "--instant" : "");
730     }
731 }
732