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