1 /* 2 * Copyright (C) 2016 Google Inc. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package android.os.cts; 18 19 import static org.junit.Assert.assertTrue; 20 import static org.junit.Assert.fail; 21 22 import android.content.Intent; 23 import android.content.pm.ApplicationInfo; 24 import android.content.pm.PackageInfo; 25 import android.content.pm.PackageManager; 26 import android.content.pm.ResolveInfo; 27 import android.net.Uri; 28 import android.support.test.InstrumentationRegistry; 29 import android.support.test.runner.AndroidJUnit4; 30 import org.junit.Test; 31 import org.junit.runner.RunWith; 32 33 import java.io.File; 34 import java.util.List; 35 36 import static android.content.pm.PackageManager.MATCH_DIRECT_BOOT_AWARE; 37 import static android.content.pm.PackageManager.MATCH_DIRECT_BOOT_UNAWARE; 38 import static android.content.pm.PackageManager.MATCH_SYSTEM_ONLY; 39 40 /** 41 * Tests whether all platform components that are implemented 42 * as APKs for various reasons are present. 43 */ 44 @RunWith(AndroidJUnit4.class) 45 public class RequiredComponentsTest { 46 private static final String PACKAGE_MIME_TYPE = "application/vnd.android.package-archive"; 47 48 @Test 49 public void testPackageInstallerPresent() throws Exception { 50 Intent installerIntent = new Intent(Intent.ACTION_INSTALL_PACKAGE); 51 installerIntent.addCategory(Intent.CATEGORY_DEFAULT); 52 installerIntent.setDataAndType(Uri.fromFile(new File("foo.apk")), PACKAGE_MIME_TYPE); 53 List<ResolveInfo> installers = InstrumentationRegistry.getContext() 54 .getPackageManager().queryIntentActivities(installerIntent, MATCH_SYSTEM_ONLY 55 | MATCH_DIRECT_BOOT_AWARE | MATCH_DIRECT_BOOT_UNAWARE); 56 if (installers.size() == 1) { 57 ResolveInfo resolveInfo = installers.get(0); 58 if (!resolveInfo.activityInfo.applicationInfo.isPrivilegedApp()) { 59 fail("The installer must be a privileged app"); 60 } 61 } else { 62 fail("There must be exactly one installer; found " + installers); 63 } 64 } 65 66 @Test 67 public void testExtServicesPresent() throws Exception { 68 enforceSharedLibPresentAndProperlyHosted( 69 PackageManager.SYSTEM_SHARED_LIBRARY_SERVICES, 70 ApplicationInfo.FLAG_SYSTEM, 71 ApplicationInfo.PRIVATE_FLAG_PRIVILEGED); 72 } 73 74 @Test 75 public void testSharedServicesPresent() throws Exception { 76 enforceSharedLibPresentAndProperlyHosted( 77 PackageManager.SYSTEM_SHARED_LIBRARY_SHARED, 78 ApplicationInfo.FLAG_SYSTEM, 0); 79 } 80 81 private void enforceSharedLibPresentAndProperlyHosted(String libName, 82 int requiredHostAppFlags, int requiredHostAppPrivateFlags) throws Exception { 83 PackageManager packageManager = InstrumentationRegistry.getContext() 84 .getPackageManager(); 85 86 // Is the lib present? 87 String[] libs = packageManager.getSystemSharedLibraryNames(); 88 boolean libPresent = false; 89 for (String lib : libs) { 90 if (libName.equals(lib)) { 91 libPresent = true; 92 break; 93 } 94 } 95 if (!libPresent) { 96 fail("Missing required shared library:" + libName); 97 } 98 99 // Is it properly hosted? 100 String packageName = packageManager.getServicesSystemSharedLibraryPackageName(); 101 PackageInfo packageInfo = packageManager.getPackageInfo(packageName, 0); 102 103 assertTrue(libName + " must be hosted by a system app with flags:" 104 + requiredHostAppFlags, (packageInfo.applicationInfo.flags 105 & requiredHostAppFlags) == requiredHostAppFlags); 106 107 assertTrue(libName + " must be hosted by a system app with private flags:" 108 + requiredHostAppPrivateFlags, (packageInfo.applicationInfo.privateFlags 109 & requiredHostAppPrivateFlags) == requiredHostAppPrivateFlags); 110 } 111 } 112