blob: fc7cfec9dc8751d01a8485f99941d1db2e138017 [file] [log] [blame]
Mike Tsaocfe4ffe2017-04-11 13:57:31 -07001/*
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
17package com.android.server.pm;
18
19import android.content.IIntentReceiver;
Mike Tsaocfe4ffe2017-04-11 13:57:31 -070020import android.os.Bundle;
Brett Chabota26eda92018-07-23 13:08:30 -070021
22import androidx.test.runner.AndroidJUnit4;
Mike Tsaocfe4ffe2017-04-11 13:57:31 -070023
Todd Kennedy42d61602017-12-12 14:44:19 -080024import org.junit.After;
25import org.junit.Assert;
26import org.junit.Before;
27import org.junit.Test;
28import org.junit.runner.RunWith;
Mike Tsaocfe4ffe2017-04-11 13:57:31 -070029
30import java.io.File;
31
32// runtest -c com.android.server.pm.PackageManagerServiceTest frameworks-services
Todd Kennedy42d61602017-12-12 14:44:19 -080033// bit FrameworksServicesTests:com.android.server.pm.PackageManagerServiceTest
34@RunWith(AndroidJUnit4.class)
35public class PackageManagerServiceTest {
36 @Before
37 public void setUp() throws Exception {
Mike Tsaocfe4ffe2017-04-11 13:57:31 -070038 }
39
Todd Kennedy42d61602017-12-12 14:44:19 -080040 @After
41 public void tearDown() throws Exception {
Mike Tsaocfe4ffe2017-04-11 13:57:31 -070042 }
43
Todd Kennedy42d61602017-12-12 14:44:19 -080044 @Test
Mike Tsaocfe4ffe2017-04-11 13:57:31 -070045 public void testPackageRemoval() throws Exception {
Todd Kennedy42d61602017-12-12 14:44:19 -080046 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 Fengde8e3b72019-02-21 14:24:24 -080059 public void notifyPackageAdded(String packageName, int uid) {
Todd Kennedy42d61602017-12-12 14:44:19 -080060 }
61
62 @Override
Svet Ganovd8eb8b22019-04-05 18:52:08 -070063 public void notifyPackageChanged(String packageName, int uid) {
64
65 }
66
67 @Override
Chenbo Fengde8e3b72019-02-21 14:24:24 -080068 public void notifyPackageRemoved(String packageName, int uid) {
Todd Kennedy42d61602017-12-12 14:44:19 -080069 }
Mike Tsaocfe4ffe2017-04-11 13:57:31 -070070 }
71
Todd Kennedy42d61602017-12-12 14:44:19 -080072 PackageSenderImpl sender = new PackageSenderImpl();
73 PackageSetting setting = null;
74 PackageManagerService.PackageRemovedInfo pri =
75 new PackageManagerService.PackageRemovedInfo(sender);
Mike Tsaocfe4ffe2017-04-11 13:57:31 -070076
Todd Kennedy42d61602017-12-12 14:44:19 -080077 // Initial conditions: nothing there
78 Assert.assertNull(pri.removedUsers);
79 Assert.assertNull(pri.broadcastUsers);
Mike Tsaocfe4ffe2017-04-11 13:57:31 -070080
Todd Kennedy42d61602017-12-12 14:44:19 -080081 // populateUsers with nothing leaves nothing
82 pri.populateUsers(null, setting);
83 Assert.assertNull(pri.broadcastUsers);
Mike Tsaocfe4ffe2017-04-11 13:57:31 -070084
Todd Kennedy42d61602017-12-12 14:44:19 -080085 // 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 Tsaocfe4ffe2017-04-11 13:57:31 -070099
Todd Kennedy42d61602017-12-12 14:44:19 -0800100 // 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 Tsaocfe4ffe2017-04-11 13:57:31 -0700111
Todd Kennedy42d61602017-12-12 14:44:19 -0800112 // TODO: test that sendApplicationHiddenForUser() actually fills in
113 // broadcastUsers
Mike Tsaocfe4ffe2017-04-11 13:57:31 -0700114 }
Anton Hansson980590a2018-08-22 18:24:47 +0100115
116 @Test
117 public void testPartitions() throws Exception {
Jeongik Chaf6629832019-07-04 21:12:06 +0900118 String[] partitions = { "system", "vendor", "odm", "oem", "product", "system_ext" };
Anton Hansson980590a2018-08-22 18:24:47 +0100119 String[] appdir = { "app", "priv-app" };
120 for (int i = 0; i < partitions.length; i++) {
121 for (int j = 0; j < appdir.length; j++) {
122 String canonical = new File("/" + partitions[i]).getCanonicalPath();
123 String path = String.format("%s/%s/A.apk", canonical, appdir[j]);
124
125 Assert.assertEquals(j == 1 && i != 3,
126 PackageManagerService.locationIsPrivileged(path));
127
128 Assert.assertEquals(i == 1 || i == 2, PackageManagerService.locationIsVendor(path));
129 Assert.assertEquals(i == 3, PackageManagerService.locationIsOem(path));
130 Assert.assertEquals(i == 4, PackageManagerService.locationIsProduct(path));
Jeongik Chaf6629832019-07-04 21:12:06 +0900131 Assert.assertEquals(i == 5, PackageManagerService.locationIsSystemExt(path));
Anton Hansson980590a2018-08-22 18:24:47 +0100132 }
133 }
134 }
Mike Tsaocfe4ffe2017-04-11 13:57:31 -0700135}