Home | History | Annotate | Download | only in consumer1
      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.lib.consumer1;
     18 
     19 import static org.junit.Assert.assertEquals;
     20 import static org.junit.Assert.assertNotNull;
     21 import static org.junit.Assert.assertSame;
     22 import static org.junit.Assert.assertTrue;
     23 import static org.junit.Assert.fail;
     24 
     25 import android.content.Context;
     26 import android.content.pm.ApplicationInfo;
     27 import android.content.pm.PackageInfo;
     28 import android.content.pm.SharedLibraryInfo;
     29 import android.content.pm.VersionedPackage;
     30 import android.os.lib.provider.R;
     31 import android.os.lib.provider.StaticSharedLib;
     32 
     33 import androidx.test.InstrumentationRegistry;
     34 import androidx.test.runner.AndroidJUnit4;
     35 
     36 import com.android.compatibility.common.util.SystemUtil;
     37 
     38 import org.junit.Test;
     39 import org.junit.runner.RunWith;
     40 
     41 import java.util.List;
     42 
     43 @RunWith(AndroidJUnit4.class)
     44 public class UseSharedLibraryTest {
     45     private static final String LIB_NAME = "foo.bar.lib";
     46     private static final String RECURSIVE_LIB_NAME = "foo.bar.lib.recursive";
     47     private static final String RECURSIVE_LIB_PROVIDER_NAME = "android.os.lib.provider.recursive";
     48     private static final String PLATFORM_PACKAGE = "android";
     49 
     50     private static final String STATIC_LIB_PROVIDER_PKG = "android.os.lib.provider";
     51     private static final String STATIC_LIB_CONSUMER1_PKG = "android.os.lib.consumer1";
     52     private static final String STATIC_LIB_CONSUMER2_PKG = "android.os.lib.consumer2";
     53 
     54     @Test
     55     public void testLoadCodeAndResources() {
     56         final Context context = InstrumentationRegistry.getContext();
     57         assertSame(1, StaticSharedLib.getVersion(context));
     58         assertSame(1, context.getResources().getInteger(android.os.lib.provider.R.integer.version));
     59         assertSame(2, StaticSharedLib.getRecursiveVersion(context));
     60     }
     61 
     62     @Test
     63     public void testSharedLibrariesProperlyReported() throws Exception {
     64         SystemUtil.runShellCommand(InstrumentationRegistry.getInstrumentation(), "appops set "
     65                 + InstrumentationRegistry.getInstrumentation().getContext().getPackageName()
     66                 + " REQUEST_INSTALL_PACKAGES allow");
     67 
     68         try {
     69             List<SharedLibraryInfo> sharedLibs = InstrumentationRegistry.getContext()
     70                     .getPackageManager().getSharedLibraries(0);
     71 
     72             assertNotNull(sharedLibs);
     73 
     74             boolean firstLibFound = false;
     75             boolean secondLibFound = false;
     76             boolean thirdLibFound = false;
     77             boolean fourthLibFound = false;
     78 
     79             for (SharedLibraryInfo sharedLib : sharedLibs) {
     80                 assertNotNull(sharedLib.getName());
     81 
     82                 final int type = sharedLib.getType();
     83                 int typeCount = 0;
     84                 typeCount += type == SharedLibraryInfo.TYPE_BUILTIN ? 1 : 0;
     85                 typeCount += type == SharedLibraryInfo.TYPE_DYNAMIC ? 1 : 0;
     86                 typeCount += type == SharedLibraryInfo.TYPE_STATIC ? 1 : 0;
     87 
     88                 if (typeCount != 1) {
     89                     fail("Library " + sharedLib.getName()
     90                             + " must be either builtin or dynamic or static");
     91                 }
     92 
     93                 if (type == SharedLibraryInfo.TYPE_BUILTIN) {
     94                     assertSame((long) SharedLibraryInfo.VERSION_UNDEFINED, sharedLib.getLongVersion());
     95                     VersionedPackage declaringPackage = sharedLib.getDeclaringPackage();
     96                     assertEquals(PLATFORM_PACKAGE, declaringPackage.getPackageName());
     97                     assertSame(0L, declaringPackage.getLongVersionCode());
     98                 }
     99 
    100                 if (type == SharedLibraryInfo.TYPE_DYNAMIC) {
    101                     assertSame((long) SharedLibraryInfo.VERSION_UNDEFINED, sharedLib.getLongVersion());
    102                     VersionedPackage declaringPackage = sharedLib.getDeclaringPackage();
    103                     assertNotNull(declaringPackage.getPackageName());
    104                     assertTrue(declaringPackage.getLongVersionCode() >= 0);
    105                 }
    106 
    107                 if (type == SharedLibraryInfo.TYPE_STATIC) {
    108                     assertTrue(sharedLib.getLongVersion() >= 0);
    109                     VersionedPackage declaringPackage = sharedLib.getDeclaringPackage();
    110                     assertNotNull(declaringPackage.getPackageName());
    111                     assertTrue(declaringPackage.getLongVersionCode() >= 0);
    112                 }
    113 
    114                 boolean validLibName = false;
    115                 if (LIB_NAME.equals(sharedLib.getName())) {
    116                     VersionedPackage declaringPackage = sharedLib.getDeclaringPackage();
    117                     assertEquals(STATIC_LIB_PROVIDER_PKG, declaringPackage.getPackageName());
    118                     validLibName = true;
    119                 }
    120                 if (RECURSIVE_LIB_NAME.equals(sharedLib.getName())) {
    121                     VersionedPackage declaringPackage = sharedLib.getDeclaringPackage();
    122                     assertEquals(RECURSIVE_LIB_PROVIDER_NAME, declaringPackage.getPackageName());
    123                     validLibName = true;
    124                 }
    125 
    126                 if (validLibName) {
    127                     assertTrue(type == SharedLibraryInfo.TYPE_STATIC);
    128 
    129                     VersionedPackage declaringPackage = sharedLib.getDeclaringPackage();
    130                     List<VersionedPackage> dependentPackages = sharedLib.getDependentPackages();
    131 
    132                     final long versionCode = sharedLib.getLongVersion();
    133                     if (versionCode == 1) {
    134                         firstLibFound = true;
    135                         assertSame(1L, declaringPackage.getLongVersionCode());
    136                         assertSame(1, dependentPackages.size());
    137                         VersionedPackage dependentPackage = dependentPackages.get(0);
    138                         assertEquals(STATIC_LIB_CONSUMER1_PKG, dependentPackage.getPackageName());
    139                         assertSame(1L, dependentPackage.getLongVersionCode());
    140                     } else if (versionCode == 2) {
    141                         secondLibFound = true;
    142                         assertSame(4L, declaringPackage.getLongVersionCode());
    143                         assertTrue(dependentPackages.isEmpty());
    144                     } else if (versionCode == 5) {
    145                         thirdLibFound = true;
    146                         assertSame(5L, declaringPackage.getLongVersionCode());
    147                         assertSame(1, dependentPackages.size());
    148                         VersionedPackage dependentPackage = dependentPackages.get(0);
    149                         assertEquals(STATIC_LIB_CONSUMER2_PKG, dependentPackage.getPackageName());
    150                         assertSame(2L, dependentPackage.getLongVersionCode());
    151                     } else if (versionCode == 6) {
    152                         fourthLibFound = true;
    153                         assertSame(1L, declaringPackage.getLongVersionCode());
    154                         assertSame(1, dependentPackages.size());
    155                         VersionedPackage dependentPackage = dependentPackages.get(0);
    156                         assertEquals(STATIC_LIB_PROVIDER_PKG, dependentPackage.getPackageName());
    157                         assertSame(1L, dependentPackage.getLongVersionCode());
    158                     }
    159                 }
    160             }
    161 
    162             assertTrue("Did not find lib " + LIB_NAME + " version 1", firstLibFound);
    163             assertTrue("Did not find lib " + LIB_NAME + " version 4", secondLibFound);
    164             assertTrue("Did not find lib " + LIB_NAME + " version 5", thirdLibFound);
    165             assertTrue("Did not find lib " + RECURSIVE_LIB_NAME + " version 6", fourthLibFound);
    166         } finally {
    167             SystemUtil.runShellCommand(InstrumentationRegistry.getInstrumentation(), "appops set "
    168                     + InstrumentationRegistry.getInstrumentation().getContext().getPackageName()
    169                     + " REQUEST_INSTALL_PACKAGES default");
    170         }
    171     }
    172 
    173     @Test
    174     public void testAppCanSeeOnlyLibrariesItDependOn() throws Exception {
    175         // Make sure we see only the lib we depend on via getting its package info
    176         PackageInfo libPackageInfo = InstrumentationRegistry.getInstrumentation()
    177                 .getContext().getPackageManager().getPackageInfo(STATIC_LIB_PROVIDER_PKG, 0);
    178         assertEquals(STATIC_LIB_PROVIDER_PKG, libPackageInfo.packageName);
    179         assertEquals(1L, libPackageInfo.getLongVersionCode());
    180 
    181         // Make sure we see the lib we depend on via getting installed packages
    182         List<PackageInfo> installedPackages = InstrumentationRegistry.getInstrumentation()
    183                 .getContext().getPackageManager().getInstalledPackages(0);
    184         long usedLibraryVersionCode = -1;
    185         for (PackageInfo installedPackage : installedPackages) {
    186             if (STATIC_LIB_PROVIDER_PKG.equals(installedPackage.packageName)) {
    187                 if (usedLibraryVersionCode != -1) {
    188                     fail("Should see only the lib it depends on");
    189                 }
    190                 usedLibraryVersionCode = installedPackage.getLongVersionCode();
    191             }
    192         }
    193         assertEquals(1L, usedLibraryVersionCode);
    194 
    195         // Make sure we see only the lib we depend on via getting its app info
    196         ApplicationInfo appInfo = InstrumentationRegistry.getInstrumentation()
    197                 .getContext().getPackageManager().getApplicationInfo(STATIC_LIB_PROVIDER_PKG, 0);
    198         assertEquals(STATIC_LIB_PROVIDER_PKG, appInfo.packageName);
    199         assertEquals(1L, libPackageInfo.getLongVersionCode());
    200 
    201         // Make sure we see the lib we depend on via getting installed apps
    202         List<PackageInfo> installedApps = InstrumentationRegistry.getInstrumentation().getContext()
    203                 .getPackageManager().getInstalledPackages(0);
    204         usedLibraryVersionCode = -1;
    205         for (PackageInfo installedApp : installedApps) {
    206             if (STATIC_LIB_PROVIDER_PKG.equals(installedApp.packageName)) {
    207                 if (usedLibraryVersionCode != -1) {
    208                     fail("Should see only the lib it depends on");
    209                 }
    210                 usedLibraryVersionCode = installedApp.getLongVersionCode();
    211             }
    212         }
    213         assertEquals(1L, usedLibraryVersionCode);
    214     }
    215 }
    216