PackageManagerPresubmitTest.java revision d072d1415452abffd55a5742e3fef5abdaadf791
1/*
2 * Copyright (C) 2016 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.Context;
20import android.content.pm.PackageInfo;
21import android.content.pm.PackageManager;
22import android.content.pm.PermissionInfo;
23import android.platform.test.annotations.Presubmit;
24import android.support.test.InstrumentationRegistry;
25import android.support.test.filters.SmallTest;
26import android.support.test.runner.AndroidJUnit4;
27import android.util.ArraySet;
28
29import com.android.internal.os.RoSystemProperties;
30import com.android.server.SystemConfig;
31
32import org.junit.Before;
33import org.junit.Test;
34import org.junit.runner.RunWith;
35
36import static junit.framework.Assert.assertTrue;
37
38
39/**
40 * Presubmit tests for {@link PackageManager}.
41 */
42@RunWith(AndroidJUnit4.class)
43public class PackageManagerPresubmitTest {
44
45    private Context mContext;
46
47    private PackageManager mPackageManager;
48
49    @Before
50    public void setUp() {
51        mContext = InstrumentationRegistry.getContext();
52        mPackageManager = mContext.getPackageManager();
53    }
54
55    /**
56     * <p>This test ensures that all signature|privileged permissions are granted to core apps like
57     * systemui/settings. If CONTROL_PRIVAPP_PERMISSIONS is set, the test also verifies that
58     * granted permissions are whitelisted in {@link SystemConfig}
59     */
60    @Test
61    @SmallTest
62    @Presubmit
63    public void testPrivAppPermissions() throws PackageManager.NameNotFoundException {
64        String[] testPackages = {"com.android.settings", "com.android.shell",
65                "com.android.systemui"};
66        for (String testPackage : testPackages) {
67            testPackagePrivAppPermission(testPackage);
68        }
69    }
70
71    private void testPackagePrivAppPermission(String testPackage)
72            throws PackageManager.NameNotFoundException {
73        PackageInfo packageInfo = mPackageManager.getPackageInfo(testPackage,
74                PackageManager.GET_PERMISSIONS);
75        ArraySet<String> privAppPermissions = SystemConfig.getInstance()
76                .getPrivAppPermissions(testPackage);
77        for (int i = 0; i < packageInfo.requestedPermissions.length; i++) {
78            String pName = packageInfo.requestedPermissions[i];
79            int protectionLevel;
80            boolean platformPermission;
81            try {
82                PermissionInfo permissionInfo = mPackageManager.getPermissionInfo(pName, 0);
83                platformPermission = PackageManagerService.PLATFORM_PACKAGE_NAME.equals(
84                        permissionInfo.packageName);
85                protectionLevel = permissionInfo.protectionLevel;
86            } catch (PackageManager.NameNotFoundException e) {
87                continue;
88            }
89            if ((protectionLevel & PermissionInfo.PROTECTION_FLAG_PRIVILEGED) != 0) {
90                boolean granted = (packageInfo.requestedPermissionsFlags[i]
91                        & PackageInfo.REQUESTED_PERMISSION_GRANTED) != 0;
92                assertTrue("Permission " + pName + " should be granted to " + testPackage, granted);
93                // if privapp permissions are enforced, platform permissions must be whitelisted
94                // in SystemConfig
95                if (platformPermission && RoSystemProperties.CONTROL_PRIVAPP_PERMISSIONS_ENFORCE) {
96                    assertTrue("Permission " + pName
97                                    + " should be declared in the xml file for package "
98                                    + testPackage,
99                            privAppPermissions.contains(pName));
100                }
101            }
102        }
103    }
104}
105