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