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 
22 import androidx.test.runner.AndroidJUnit4;
23 
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;
29 
30 import java.io.File;
31 
32 // runtest -c com.android.server.pm.PackageManagerServiceTest frameworks-services
33 // bit FrameworksServicesTests:com.android.server.pm.PackageManagerServiceTest
34 @RunWith(AndroidJUnit4.class)
35 public class PackageManagerServiceTest {
36     @Before
setUp()37     public void setUp() throws Exception {
38     }
39 
40     @After
tearDown()41     public void tearDown() throws Exception {
42     }
43 
44     @Test
testPackageRemoval()45     public void testPackageRemoval() throws Exception {
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
59             public void notifyPackageAdded(String packageName, int uid) {
60             }
61 
62             @Override
63             public void notifyPackageChanged(String packageName, int uid) {
64 
65             }
66 
67             @Override
68             public void notifyPackageRemoved(String packageName, int uid) {
69             }
70         }
71 
72         PackageSenderImpl sender = new PackageSenderImpl();
73         PackageSetting setting = null;
74         PackageManagerService.PackageRemovedInfo pri =
75                 new PackageManagerService.PackageRemovedInfo(sender);
76 
77         // Initial conditions: nothing there
78         Assert.assertNull(pri.removedUsers);
79         Assert.assertNull(pri.broadcastUsers);
80 
81         // populateUsers with nothing leaves nothing
82         pri.populateUsers(null, setting);
83         Assert.assertNull(pri.broadcastUsers);
84 
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);
99 
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);
111 
112         // TODO: test that sendApplicationHiddenForUser() actually fills in
113         // broadcastUsers
114     }
115 
116     @Test
testPartitions()117     public void testPartitions() throws Exception {
118         String[] partitions = { "system", "vendor", "odm", "oem", "product", "system_ext" };
119         String[] appdir = { "app", "priv-app" };
120         for (int i = 0; i < partitions.length; i++) {
121             final PackageManagerService.SystemPartition systemPartition =
122                     PackageManagerService.SYSTEM_PARTITIONS.get(i);
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 
127                 Assert.assertEquals(j == 1 && i != 3, systemPartition.containsPrivPath(path));
128 
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);
135             }
136         }
137     }
138 }
139