1 /* 2 * Copyright (C) 2017 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 com.android.server.pm; 18 19 import android.content.IIntentReceiver; 20 import android.os.Bundle; 21 import android.util.SparseArray; 22 23 import androidx.test.runner.AndroidJUnit4; 24 25 import org.junit.After; 26 import org.junit.Assert; 27 import org.junit.Before; 28 import org.junit.Test; 29 import org.junit.runner.RunWith; 30 31 import java.io.File; 32 33 // runtest -c com.android.server.pm.PackageManagerServiceTest frameworks-services 34 // bit FrameworksServicesTests:com.android.server.pm.PackageManagerServiceTest 35 @RunWith(AndroidJUnit4.class) 36 public class PackageManagerServiceTest { 37 @Before setUp()38 public void setUp() throws Exception { 39 } 40 41 @After tearDown()42 public void tearDown() throws Exception { 43 } 44 45 @Test testPackageRemoval()46 public void testPackageRemoval() throws Exception { 47 class PackageSenderImpl implements PackageSender { 48 public void sendPackageBroadcast(final String action, final String pkg, 49 final Bundle extras, final int flags, final String targetPkg, 50 final IIntentReceiver finishedReceiver, final int[] userIds, 51 int[] instantUserIds, SparseArray<int[]> broadcastWhitelist) { 52 } 53 54 public void sendPackageAddedForNewUsers(String packageName, 55 boolean sendBootComplete, boolean includeStopped, int appId, 56 int[] userIds, int[] instantUserIds, int dataLoaderType) { 57 } 58 59 @Override 60 public void notifyPackageAdded(String packageName, int uid) { 61 } 62 63 @Override 64 public void notifyPackageChanged(String packageName, int uid) { 65 66 } 67 68 @Override 69 public void notifyPackageRemoved(String packageName, int uid) { 70 } 71 } 72 73 PackageSenderImpl sender = new PackageSenderImpl(); 74 PackageSetting setting = null; 75 PackageManagerService.PackageRemovedInfo pri = 76 new PackageManagerService.PackageRemovedInfo(sender); 77 78 // Initial conditions: nothing there 79 Assert.assertNull(pri.removedUsers); 80 Assert.assertNull(pri.broadcastUsers); 81 82 // populateUsers with nothing leaves nothing 83 pri.populateUsers(null, setting); 84 Assert.assertNull(pri.broadcastUsers); 85 86 // Create a real (non-null) PackageSetting and confirm that the removed 87 // users are copied properly 88 setting = new PackageSetting("name", "realName", new File("codePath"), 89 new File("resourcePath"), "legacyNativeLibraryPathString", 90 "primaryCpuAbiString", "secondaryCpuAbiString", 91 "cpuAbiOverrideString", 0, 0, 0, 0, 92 null, null, null); 93 pri.populateUsers(new int[] { 94 1, 2, 3, 4, 5 95 }, setting); 96 Assert.assertNotNull(pri.broadcastUsers); 97 Assert.assertEquals(5, pri.broadcastUsers.length); 98 Assert.assertNotNull(pri.instantUserIds); 99 Assert.assertEquals(0, pri.instantUserIds.length); 100 101 // Exclude a user 102 pri.broadcastUsers = null; 103 final int EXCLUDED_USER_ID = 4; 104 setting.setInstantApp(true, EXCLUDED_USER_ID); 105 pri.populateUsers(new int[] { 106 1, 2, 3, EXCLUDED_USER_ID, 5 107 }, setting); 108 Assert.assertNotNull(pri.broadcastUsers); 109 Assert.assertEquals(4, pri.broadcastUsers.length); 110 Assert.assertNotNull(pri.instantUserIds); 111 Assert.assertEquals(1, pri.instantUserIds.length); 112 113 // TODO: test that sendApplicationHiddenForUser() actually fills in 114 // broadcastUsers 115 } 116 117 @Test testPartitions()118 public void testPartitions() throws Exception { 119 String[] partitions = { "system", "vendor", "odm", "oem", "product", "system_ext" }; 120 String[] appdir = { "app", "priv-app" }; 121 for (int i = 0; i < partitions.length; i++) { 122 final PackageManagerService.ScanPartition scanPartition = 123 PackageManagerService.SYSTEM_PARTITIONS.get(i); 124 for (int j = 0; j < appdir.length; j++) { 125 File path = new File(String.format("%s/%s/A.apk", partitions[i], appdir[j])); 126 Assert.assertEquals(j == 1 && i != 3, scanPartition.containsPrivApp(path)); 127 128 final int scanFlag = scanPartition.scanFlag; 129 Assert.assertEquals(i == 1, scanFlag == PackageManagerService.SCAN_AS_VENDOR); 130 Assert.assertEquals(i == 2, scanFlag == PackageManagerService.SCAN_AS_ODM); 131 Assert.assertEquals(i == 3, scanFlag == PackageManagerService.SCAN_AS_OEM); 132 Assert.assertEquals(i == 4, scanFlag == PackageManagerService.SCAN_AS_PRODUCT); 133 Assert.assertEquals(i == 5, scanFlag == PackageManagerService.SCAN_AS_SYSTEM_EXT); 134 } 135 } 136 } 137 } 138