Home | History | Annotate | Download | only in cts
      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             "android.support.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
     95     public void setBuild(IBuildInfo buildInfo) {
     96         mBuildHelper = new CompatibilityBuildHelper(buildInfo);
     97     }
     98 
     99     @AppModeInstant
    100     public void testInstallSharedLibraryInstantMode() throws Exception {
    101         mInstantMode = true;
    102         doTestInstallSharedLibrary();
    103     }
    104 
    105     @AppModeFull
    106     public void testInstallSharedLibraryFullMode() throws Exception {
    107         doTestInstallSharedLibrary();
    108     }
    109 
    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
    135     public void testCannotInstallSharedLibraryWithMissingDependencyInstantMode() throws Exception {
    136         mInstantMode = true;
    137         doTestCannotInstallSharedLibraryWithMissingDependency();
    138     }
    139 
    140     @AppModeFull
    141     public void testCannotInstallSharedLibraryWithMissingDependencyFullMode() throws Exception {
    142         doTestCannotInstallSharedLibraryWithMissingDependency();
    143     }
    144 
    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 
    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 
    179     @AppModeInstant
    180     public void testCannotUninstallUsedSharedLibrary1InstantMode() throws Exception {
    181         mInstantMode = true;
    182         doTestCannotUninstallUsedSharedLibrary1();
    183     }
    184 
    185     @AppModeFull
    186     public void testCannotUninstallUsedSharedLibrary1FullMode() throws Exception {
    187         doTestCannotUninstallUsedSharedLibrary1();
    188     }
    189 
    190     private void doTestCannotUninstallUsedSharedLibrary1() throws Exception {
    191         getDevice().uninstallPackage(STATIC_LIB_PROVIDER1_PKG);
    192         getDevice().uninstallPackage(STATIC_LIB_PROVIDER_RECURSIVE_PKG);
    193         try {
    194             // Install library dependency
    195             assertNull(install(STATIC_LIB_PROVIDER_RECURSIVE_APK));
    196             // Install the library
    197             assertNull(install(STATIC_LIB_PROVIDER1_APK));
    198             // The library dependency cannot be uninstalled
    199             assertNotNull(getDevice().uninstallPackage(STATIC_LIB_PROVIDER_RECURSIVE_PKG));
    200             // Now the library dependency can be uninstalled
    201             assertNull(getDevice().uninstallPackage(STATIC_LIB_PROVIDER1_PKG));
    202             // Uninstall dependency
    203             assertNull(getDevice().uninstallPackage(STATIC_LIB_PROVIDER_RECURSIVE_PKG));
    204         } finally {
    205             getDevice().uninstallPackage(STATIC_LIB_PROVIDER1_PKG);
    206             getDevice().uninstallPackage(STATIC_LIB_PROVIDER_RECURSIVE_PKG);
    207         }
    208     }
    209 
    210     @AppModeInstant
    211     public void testCannotUninstallUsedSharedLibrary2InstantMode() throws Exception {
    212         mInstantMode = true;
    213         doTestCannotUninstallUsedSharedLibrary2();
    214     }
    215 
    216     @AppModeFull
    217     public void testCannotUninstallUsedSharedLibrary2FullMode() throws Exception {
    218         doTestCannotUninstallUsedSharedLibrary2();
    219     }
    220 
    221     private void doTestCannotUninstallUsedSharedLibrary2() throws Exception {
    222         getDevice().uninstallPackage(STATIC_LIB_CONSUMER1_PKG);
    223         getDevice().uninstallPackage(STATIC_LIB_PROVIDER1_PKG);
    224         getDevice().uninstallPackage(STATIC_LIB_PROVIDER_RECURSIVE_PKG);
    225         try {
    226             // Install library dependency
    227             assertNull(install(STATIC_LIB_PROVIDER_RECURSIVE_APK));
    228             // Install the library
    229             assertNull(install(STATIC_LIB_PROVIDER1_APK));
    230             // Install the client
    231             assertNull(install(STATIC_LIB_CONSUMER1_APK));
    232             // The library cannot be uninstalled
    233             assertNotNull(getDevice().uninstallPackage(STATIC_LIB_PROVIDER1_PKG));
    234             // Uninstall the client
    235             assertNull(getDevice().uninstallPackage(STATIC_LIB_CONSUMER1_PKG));
    236             // Now the library can be uninstalled
    237             assertNull(getDevice().uninstallPackage(STATIC_LIB_PROVIDER1_PKG));
    238             // Uninstall dependency
    239             assertNull(getDevice().uninstallPackage(STATIC_LIB_PROVIDER_RECURSIVE_PKG));
    240         } finally {
    241             getDevice().uninstallPackage(STATIC_LIB_CONSUMER1_PKG);
    242             getDevice().uninstallPackage(STATIC_LIB_PROVIDER1_PKG);
    243             getDevice().uninstallPackage(STATIC_LIB_PROVIDER_RECURSIVE_PKG);
    244         }
    245     }
    246 
    247     @AppModeInstant
    248     public void testLibraryVersionsAndVersionCodesSameOrderInstantMode() throws Exception {
    249         mInstantMode = true;
    250         doTestLibraryVersionsAndVersionCodesSameOrder();
    251     }
    252 
    253     @AppModeFull
    254     public void testLibraryVersionsAndVersionCodesSameOrderFullMode() throws Exception {
    255         doTestLibraryVersionsAndVersionCodesSameOrder();
    256     }
    257 
    258     private void doTestLibraryVersionsAndVersionCodesSameOrder() throws Exception {
    259         getDevice().uninstallPackage(STATIC_LIB_PROVIDER1_PKG);
    260         getDevice().uninstallPackage(STATIC_LIB_PROVIDER2_PKG);
    261         getDevice().uninstallPackage(STATIC_LIB_PROVIDER3_PKG);
    262         getDevice().uninstallPackage(STATIC_LIB_PROVIDER_RECURSIVE_PKG);
    263         try {
    264             // Install library dependency
    265             assertNull(install(STATIC_LIB_PROVIDER_RECURSIVE_APK));
    266             // Install library version 1 with version code 1
    267             assertNull(install(STATIC_LIB_PROVIDER1_APK));
    268             // Install library version 2 with version code 4
    269             assertNull(install(STATIC_LIB_PROVIDER2_APK));
    270             // Shouldn't be able to install library version 3 with version code 3
    271             assertNotNull(install(STATIC_LIB_PROVIDER3_APK));
    272         } finally {
    273             getDevice().uninstallPackage(STATIC_LIB_PROVIDER1_PKG);
    274             getDevice().uninstallPackage(STATIC_LIB_PROVIDER2_PKG);
    275             getDevice().uninstallPackage(STATIC_LIB_PROVIDER3_PKG);
    276             getDevice().uninstallPackage(STATIC_LIB_PROVIDER_RECURSIVE_PKG);
    277         }
    278     }
    279 
    280     @AppModeInstant
    281     public void testCannotInstallAppWithMissingLibraryInstantMode() throws Exception {
    282         mInstantMode = true;
    283         doTestCannotInstallAppWithMissingLibrary();
    284     }
    285 
    286     @AppModeFull
    287     public void testCannotInstallAppWithMissingLibraryFullMode() throws Exception {
    288         doTestCannotInstallAppWithMissingLibrary();
    289     }
    290 
    291     private void doTestCannotInstallAppWithMissingLibrary() throws Exception {
    292         getDevice().uninstallPackage(STATIC_LIB_CONSUMER1_PKG);
    293         try {
    294             // Shouldn't be able to install an app if a dependency lib is missing
    295             assertNotNull(install(STATIC_LIB_CONSUMER1_APK));
    296         } finally {
    297             getDevice().uninstallPackage(STATIC_LIB_CONSUMER1_PKG);
    298         }
    299     }
    300 
    301     @AppModeInstant
    302     public void testCanReplaceLibraryIfVersionAndVersionCodeSameInstantMode() throws Exception {
    303         mInstantMode = true;
    304         doTestCanReplaceLibraryIfVersionAndVersionCodeSame();
    305     }
    306 
    307     @AppModeFull
    308     public void testCanReplaceLibraryIfVersionAndVersionCodeSameFullMode() throws Exception {
    309         doTestCanReplaceLibraryIfVersionAndVersionCodeSame();
    310     }
    311 
    312     private void doTestCanReplaceLibraryIfVersionAndVersionCodeSame() throws Exception {
    313         getDevice().uninstallPackage(STATIC_LIB_PROVIDER1_PKG);
    314         getDevice().uninstallPackage(STATIC_LIB_PROVIDER_RECURSIVE_PKG);
    315         try {
    316             // Install library dependency
    317             assertNull(install(STATIC_LIB_PROVIDER_RECURSIVE_APK));
    318             // Install a library
    319             assertNull(install(STATIC_LIB_PROVIDER1_APK));
    320             // Cannot install the library (need to reinstall)
    321             assertNotNull(install(STATIC_LIB_PROVIDER1_APK));
    322             // Can reinstall the library if version and version code same
    323             assertNull(getDevice().installPackage(mBuildHelper.getTestFile(
    324                     STATIC_LIB_PROVIDER1_APK), true, false));
    325         } finally {
    326             getDevice().uninstallPackage(STATIC_LIB_PROVIDER1_PKG);
    327             getDevice().uninstallPackage(STATIC_LIB_PROVIDER_RECURSIVE_PKG);
    328         }
    329     }
    330 
    331     @AppModeInstant
    332     public void testUninstallSpecificLibraryVersionInstantMode() throws Exception {
    333         mInstantMode = true;
    334         doTestUninstallSpecificLibraryVersion();
    335     }
    336 
    337     @AppModeFull
    338     public void testUninstallSpecificLibraryVersionFullMode() throws Exception {
    339         doTestUninstallSpecificLibraryVersion();
    340     }
    341 
    342     private void doTestUninstallSpecificLibraryVersion() throws Exception {
    343         getDevice().uninstallPackage(STATIC_LIB_PROVIDER1_PKG);
    344         getDevice().uninstallPackage(STATIC_LIB_PROVIDER_RECURSIVE_PKG);
    345         try {
    346             // Install library dependency
    347             assertNull(install(STATIC_LIB_PROVIDER_RECURSIVE_APK));
    348             // Install library version 1 with version code 1
    349             assertNull(install(STATIC_LIB_PROVIDER1_APK));
    350             // Install library version 2 with version code 4
    351             assertNull(install(STATIC_LIB_PROVIDER2_APK));
    352             // Uninstall the library package with version code 4 (version 2)
    353             assertTrue(getDevice().executeShellCommand("pm uninstall --versionCode 4 "
    354                     + STATIC_LIB_PROVIDER1_PKG).startsWith("Success"));
    355             // Uninstall the library package with version code 1 (version 1)
    356             assertTrue(getDevice().executeShellCommand("pm uninstall "
    357                     + STATIC_LIB_PROVIDER1_PKG).startsWith("Success"));
    358         } finally {
    359             getDevice().uninstallPackage(STATIC_LIB_PROVIDER1_PKG);
    360             getDevice().uninstallPackage(STATIC_LIB_PROVIDER_RECURSIVE_PKG);
    361         }
    362     }
    363 
    364     @AppModeInstant
    365     public void testKeyRotationInstantMode() throws Exception {
    366         mInstantMode = true;
    367         doTestKeyRotation();
    368     }
    369 
    370     @AppModeFull
    371     public void testKeyRotationFullMode() throws Exception {
    372         doTestKeyRotation();
    373     }
    374 
    375     private void doTestKeyRotation() throws Exception {
    376         getDevice().uninstallPackage(STATIC_LIB_CONSUMER2_PKG);
    377         getDevice().uninstallPackage(STATIC_LIB_PROVIDER2_PKG);
    378         getDevice().uninstallPackage(STATIC_LIB_PROVIDER4_PKG);
    379         try {
    380             // Install a library version specifying an upgrade key set
    381             assertNull(install(STATIC_LIB_PROVIDER2_APK));
    382             // Install a newer library signed with the upgrade key set
    383             assertNull(install(STATIC_LIB_PROVIDER4_APK));
    384             // Install a client that depends on the upgraded key set
    385             assertNull(install(STATIC_LIB_CONSUMER2_APK));
    386             // Ensure code and resources can be loaded
    387             runDeviceTests(STATIC_LIB_CONSUMER2_PKG,
    388                     "android.os.lib.consumer2.UseSharedLibraryTest",
    389                     "testLoadCodeAndResources");
    390         } finally {
    391             getDevice().uninstallPackage(STATIC_LIB_CONSUMER2_PKG);
    392             getDevice().uninstallPackage(STATIC_LIB_PROVIDER2_PKG);
    393             getDevice().uninstallPackage(STATIC_LIB_PROVIDER4_PKG);
    394         }
    395     }
    396 
    397     @AppModeInstant
    398     public void testCannotInstallIncorrectlySignedLibraryInstantMode() throws Exception {
    399         mInstantMode = true;
    400         doTestCannotInstallIncorrectlySignedLibrary();
    401     }
    402 
    403     @AppModeFull
    404     public void testCannotInstallIncorrectlySignedLibraryFullMode() throws Exception {
    405         doTestCannotInstallIncorrectlySignedLibrary();
    406     }
    407 
    408     private void doTestCannotInstallIncorrectlySignedLibrary() throws Exception {
    409         getDevice().uninstallPackage(STATIC_LIB_PROVIDER1_PKG);
    410         getDevice().uninstallPackage(STATIC_LIB_PROVIDER4_PKG);
    411         getDevice().uninstallPackage(STATIC_LIB_PROVIDER_RECURSIVE_PKG);
    412         try {
    413             // Install library dependency
    414             assertNull(install(STATIC_LIB_PROVIDER_RECURSIVE_APK));
    415             // Install a library version not specifying an upgrade key set
    416             assertNull(install(STATIC_LIB_PROVIDER1_APK));
    417             // Shouldn't be able to install a newer version signed differently
    418             assertNotNull(install(STATIC_LIB_PROVIDER4_APK));
    419         } finally {
    420             getDevice().uninstallPackage(STATIC_LIB_PROVIDER1_PKG);
    421             getDevice().uninstallPackage(STATIC_LIB_PROVIDER4_PKG);
    422             getDevice().uninstallPackage(STATIC_LIB_PROVIDER_RECURSIVE_PKG);
    423         }
    424     }
    425 
    426     @AppModeInstant
    427     public void testLibraryAndPackageNameCanMatchInstantMode() throws Exception {
    428         mInstantMode = true;
    429         doTestLibraryAndPackageNameCanMatch();
    430     }
    431 
    432     @AppModeFull
    433     public void testLibraryAndPackageNameCanMatchFullMode() throws Exception {
    434         doTestLibraryAndPackageNameCanMatch();
    435     }
    436 
    437     private void doTestLibraryAndPackageNameCanMatch() throws Exception {
    438         getDevice().uninstallPackage(STATIC_LIB_PROVIDER5_PKG);
    439         getDevice().uninstallPackage(STATIC_LIB_PROVIDER6_PKG);
    440         try {
    441             // Install a library with same name as package should work.
    442             assertNull(install(STATIC_LIB_PROVIDER5_APK));
    443             // Install a library with same name as package should work.
    444             assertNull(getDevice().installPackage(mBuildHelper.getTestFile(
    445                     STATIC_LIB_PROVIDER6_APK), true, false));
    446         } finally {
    447             getDevice().uninstallPackage(STATIC_LIB_PROVIDER5_PKG);
    448             getDevice().uninstallPackage(STATIC_LIB_PROVIDER6_PKG);
    449         }
    450     }
    451 
    452     @AppModeInstant
    453     public void testGetSharedLibrariesInstantMode() throws Exception {
    454         mInstantMode = true;
    455         doTestGetSharedLibraries();
    456     }
    457 
    458     @AppModeFull
    459     public void testGetSharedLibrariesFullMode() throws Exception {
    460         doTestGetSharedLibraries();
    461     }
    462 
    463     private void doTestGetSharedLibraries() throws Exception {
    464         getDevice().uninstallPackage(STATIC_LIB_CONSUMER1_PKG);
    465         getDevice().uninstallPackage(STATIC_LIB_CONSUMER2_PKG);
    466         getDevice().uninstallPackage(STATIC_LIB_PROVIDER1_PKG);
    467         getDevice().uninstallPackage(STATIC_LIB_PROVIDER2_PKG);
    468         getDevice().uninstallPackage(STATIC_LIB_PROVIDER4_PKG);
    469         getDevice().uninstallPackage(STATIC_LIB_PROVIDER_RECURSIVE_PKG);
    470         try {
    471             // Install library dependency
    472             assertNull(install(STATIC_LIB_PROVIDER_RECURSIVE_APK));
    473             // Install the first library
    474             assertNull(install(STATIC_LIB_PROVIDER1_APK));
    475             // Install the second library
    476             assertNull(install(STATIC_LIB_PROVIDER2_APK));
    477             // Install the third library
    478             assertNull(install(STATIC_LIB_PROVIDER4_APK));
    479             // Install the first client
    480             assertNull(install(STATIC_LIB_CONSUMER1_APK));
    481             // Install the second client
    482             assertNull(install(STATIC_LIB_CONSUMER2_APK));
    483             // Ensure libraries are properly reported
    484             runDeviceTests(STATIC_LIB_CONSUMER1_PKG,
    485                     "android.os.lib.consumer1.UseSharedLibraryTest",
    486                     "testSharedLibrariesProperlyReported");
    487         } finally {
    488             getDevice().uninstallPackage(STATIC_LIB_CONSUMER1_PKG);
    489             getDevice().uninstallPackage(STATIC_LIB_CONSUMER2_PKG);
    490             getDevice().uninstallPackage(STATIC_LIB_PROVIDER1_PKG);
    491             getDevice().uninstallPackage(STATIC_LIB_PROVIDER2_PKG);
    492             getDevice().uninstallPackage(STATIC_LIB_PROVIDER4_PKG);
    493             getDevice().uninstallPackage(STATIC_LIB_PROVIDER_RECURSIVE_PKG);
    494         }
    495     }
    496 
    497     @AppModeInstant
    498     public void testAppCanSeeOnlyLibrariesItDependOnInstantMode() throws Exception {
    499         mInstantMode = true;
    500         doTestAppCanSeeOnlyLibrariesItDependOn();
    501     }
    502 
    503     @AppModeFull
    504     public void testAppCanSeeOnlyLibrariesItDependOnFullMode() throws Exception {
    505         doTestAppCanSeeOnlyLibrariesItDependOn();
    506     }
    507 
    508     private void doTestAppCanSeeOnlyLibrariesItDependOn() throws Exception {
    509         getDevice().uninstallPackage(STATIC_LIB_CONSUMER1_PKG);
    510         getDevice().uninstallPackage(STATIC_LIB_PROVIDER1_PKG);
    511         getDevice().uninstallPackage(STATIC_LIB_PROVIDER2_PKG);
    512         getDevice().uninstallPackage(STATIC_LIB_PROVIDER_RECURSIVE_PKG);
    513         try {
    514             // Install library dependency
    515             assertNull(install(STATIC_LIB_PROVIDER_RECURSIVE_APK));
    516             // Install the first library
    517             assertNull(install(STATIC_LIB_PROVIDER1_APK));
    518             // Install the second library
    519             assertNull(install(STATIC_LIB_PROVIDER2_APK));
    520             // Install the client
    521             assertNull(install(STATIC_LIB_CONSUMER1_APK));
    522             // Ensure the client can see only the lib it depends on
    523             runDeviceTests(STATIC_LIB_CONSUMER1_PKG,
    524                     "android.os.lib.consumer1.UseSharedLibraryTest",
    525                     "testAppCanSeeOnlyLibrariesItDependOn");
    526         } finally {
    527             getDevice().uninstallPackage(STATIC_LIB_CONSUMER1_PKG);
    528             getDevice().uninstallPackage(STATIC_LIB_PROVIDER1_PKG);
    529             getDevice().uninstallPackage(STATIC_LIB_PROVIDER2_PKG);
    530             getDevice().uninstallPackage(STATIC_LIB_PROVIDER_RECURSIVE_PKG);
    531         }
    532     }
    533 
    534     @AppModeInstant
    535     public void testLoadCodeFromNativeLibInstantMode() throws Exception {
    536         mInstantMode = true;
    537         doTestLoadCodeFromNativeLib();
    538     }
    539 
    540     @AppModeFull
    541     public void testLoadCodeFromNativeLibFullMode() throws Exception {
    542         doTestLoadCodeFromNativeLib();
    543     }
    544 
    545     private void doTestLoadCodeFromNativeLib() throws Exception {
    546         getDevice().uninstallPackage(STATIC_LIB_NATIVE_CONSUMER_PKG);
    547         getDevice().uninstallPackage(STATIC_LIB_NATIVE_PROVIDER_PKG);
    548         try {
    549             // Install library
    550             assertNull(install(STATIC_LIB_NATIVE_PROVIDER_APK));
    551             // Install the library client
    552             assertNull(install(STATIC_LIB_NATIVE_CONSUMER_APK));
    553             // Ensure the client can load native code from the library
    554             runDeviceTests(STATIC_LIB_NATIVE_CONSUMER_PKG,
    555                     "android.os.lib.consumer.UseSharedLibraryTest",
    556                     "testLoadNativeCode");
    557         } finally {
    558             getDevice().uninstallPackage(STATIC_LIB_NATIVE_CONSUMER_PKG);
    559             getDevice().uninstallPackage(STATIC_LIB_NATIVE_PROVIDER_PKG);
    560         }
    561     }
    562 
    563     @AppModeInstant
    564     public void testLoadCodeFromNativeLibMultiArchViolationInstantMode() throws Exception {
    565         mInstantMode = true;
    566         doTestLoadCodeFromNativeLibMultiArchViolation();
    567     }
    568 
    569     @AppModeFull
    570     public void testLoadCodeFromNativeLibMultiArchViolationFullMode() throws Exception {
    571         doTestLoadCodeFromNativeLibMultiArchViolation();
    572     }
    573 
    574     private void doTestLoadCodeFromNativeLibMultiArchViolation() throws Exception {
    575         getDevice().uninstallPackage(STATIC_LIB_NATIVE_PROVIDER_PKG1);
    576         try {
    577             // Cannot install the library with native code if not multi-arch
    578             assertNotNull(install(STATIC_LIB_NATIVE_PROVIDER_APK1));
    579         } finally {
    580             getDevice().uninstallPackage(STATIC_LIB_NATIVE_PROVIDER_PKG1);
    581         }
    582     }
    583 
    584     @AppModeInstant
    585     public void testLoadCodeAndResourcesFromSharedLibrarySignedWithTwoCertsInstantMode() throws Exception {
    586         mInstantMode = true;
    587         doTestLoadCodeAndResourcesFromSharedLibrarySignedWithTwoCerts();
    588     }
    589 
    590     @AppModeFull
    591     public void testLoadCodeAndResourcesFromSharedLibrarySignedWithTwoCertsFullMode() throws Exception {
    592         doTestLoadCodeAndResourcesFromSharedLibrarySignedWithTwoCerts();
    593     }
    594 
    595     private void doTestLoadCodeAndResourcesFromSharedLibrarySignedWithTwoCerts()
    596             throws Exception {
    597         getDevice().uninstallPackage(STATIC_LIB_CONSUMER3_PKG);
    598         getDevice().uninstallPackage(STATIC_LIB_PROVIDER7_PKG);
    599         try {
    600             // Install the library
    601             assertNull(install(STATIC_LIB_PROVIDER7_APK));
    602             // Install the client
    603             assertNull(install(STATIC_LIB_CONSUMER3_APK));
    604             // Try to load code and resources
    605             runDeviceTests(STATIC_LIB_CONSUMER3_PKG,
    606                     "android.os.lib.consumer3.UseSharedLibraryTest",
    607                     "testLoadCodeAndResources");
    608         } finally {
    609             getDevice().uninstallPackage(STATIC_LIB_CONSUMER3_PKG);
    610             getDevice().uninstallPackage(STATIC_LIB_PROVIDER7_PKG);
    611         }
    612     }
    613 
    614     private void runDeviceTests(String packageName, String testClassName,
    615             String testMethodName) throws DeviceNotAvailableException {
    616         RemoteAndroidTestRunner testRunner = new RemoteAndroidTestRunner(packageName,
    617                 ANDROID_JUNIT_RUNNER_CLASS, getDevice().getIDevice());
    618         testRunner.setMethodName(testClassName, testMethodName);
    619         CollectingTestListener listener = new CollectingTestListener();
    620 
    621         getDevice().runInstrumentationTests(testRunner, listener);
    622 
    623         final TestRunResult result = listener.getCurrentRunResults();
    624         if (result.isRunFailure()) {
    625             throw new AssertionError("Failed to successfully run device tests for "
    626                     + result.getName() + ": " + result.getRunFailureMessage());
    627         }
    628         if (result.getNumTests() == 0) {
    629             throw new AssertionError("No tests were run on the device");
    630         }
    631         if (result.hasFailedTests()) {
    632             // build a meaningful error message
    633             StringBuilder errorBuilder = new StringBuilder("on-device tests failed:\n");
    634             for (Map.Entry<TestDescription, TestResult> resultEntry :
    635                     result.getTestResults().entrySet()) {
    636                 if (!resultEntry.getValue().getStatus().equals(TestStatus.PASSED)) {
    637                     errorBuilder.append(resultEntry.getKey().toString());
    638                     errorBuilder.append(":\n");
    639                     errorBuilder.append(resultEntry.getValue().getStackTrace());
    640                 }
    641             }
    642             throw new AssertionError(errorBuilder.toString());
    643         }
    644     }
    645 
    646     private String install(String apk) throws DeviceNotAvailableException, FileNotFoundException {
    647         return getDevice().installPackage(mBuildHelper.getTestFile(apk), false, false,
    648                 apk.contains("consumer") && mInstantMode ? "--instant" : "");
    649     }
    650 }
    651