1 /*
2  * Copyright (C) 2024 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.devicelockcontroller;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import android.content.pm.PackageInfo;
22 import android.content.pm.PackageManager;
23 import android.content.pm.PackageManager.NameNotFoundException;
24 import android.content.pm.PackageManager.PackageInfoFlags;
25 import android.content.pm.PermissionInfo;
26 import android.util.ArrayMap;
27 
28 import androidx.test.core.app.ApplicationProvider;
29 
30 import org.junit.Before;
31 import org.junit.Test;
32 import org.junit.runner.RunWith;
33 import org.robolectric.RobolectricTestRunner;
34 
35 import java.util.Arrays;
36 import java.util.List;
37 
38 @RunWith(RobolectricTestRunner.class)
39 public final class DeviceLockControllerPermissionsTest {
40     public static final String MANAGE_DEVICE_LOCK_SERVICE_FROM_CONTROLLER =
41             "com.android.devicelockcontroller.permission."
42                     + "MANAGE_DEVICE_LOCK_SERVICE_FROM_CONTROLLER";
43     public static final String START_KIOSK_SETUP_ACTIVITY =
44             "com.android.devicelock.permission.START_KIOSK_SETUP_ACTIVITY";
45     public static final String RECEIVE_CLEAR_BROADCAST =
46             "com.android.devicelock.permission.RECEIVE_CLEAR_BROADCAST";
47     private TestDeviceLockControllerApplication mTestApp;
48     private ArrayMap<String, PermissionInfo> mNameToDeclaredPermissionInfo;
49     private List<String> mRequestedPermissions;
50 
51     @Before
setUp()52     public void setUp() throws NameNotFoundException {
53         mTestApp = ApplicationProvider.getApplicationContext();
54         String packageName = mTestApp.getPackageName();
55         PackageManager packageManager = mTestApp.getPackageManager();
56         PackageInfo packageInfo =
57                 packageManager.getPackageInfo(packageName,
58                         PackageInfoFlags.of(PackageManager.GET_PERMISSIONS));
59         mNameToDeclaredPermissionInfo = new ArrayMap<>();
60         for (PermissionInfo declaredPermissionInfo: packageInfo.permissions) {
61             mNameToDeclaredPermissionInfo.put(declaredPermissionInfo.name, declaredPermissionInfo);
62         }
63         mRequestedPermissions = Arrays.asList(packageInfo.requestedPermissions);
64     }
65 
66     @Test
deviceLockControllerDeclaresManageDeviceLockServiceFromControllerPermission()67     public void deviceLockControllerDeclaresManageDeviceLockServiceFromControllerPermission() {
68         assertThat(mNameToDeclaredPermissionInfo)
69                 .containsKey(MANAGE_DEVICE_LOCK_SERVICE_FROM_CONTROLLER);
70 
71         assertThat(mNameToDeclaredPermissionInfo.get(MANAGE_DEVICE_LOCK_SERVICE_FROM_CONTROLLER)
72                 .getProtection()).isEqualTo(PermissionInfo.PROTECTION_SIGNATURE);
73     }
74 
75     @Test
deviceLockControllerDeclaresStartKioskSetupActivityPermission()76     public void deviceLockControllerDeclaresStartKioskSetupActivityPermission() {
77         assertThat(mNameToDeclaredPermissionInfo).containsKey(START_KIOSK_SETUP_ACTIVITY);
78 
79         assertThat(mNameToDeclaredPermissionInfo.get(START_KIOSK_SETUP_ACTIVITY)
80                 .getProtection()).isEqualTo(PermissionInfo.PROTECTION_SIGNATURE);
81     }
82 
83     @Test
deviceLockControllerDeclaresReceiveClearRestrictionsPermission()84     public void deviceLockControllerDeclaresReceiveClearRestrictionsPermission() {
85         assertThat(mNameToDeclaredPermissionInfo).containsKey(RECEIVE_CLEAR_BROADCAST);
86 
87         assertThat(mNameToDeclaredPermissionInfo.get(RECEIVE_CLEAR_BROADCAST)
88                 .getProtection()).isEqualTo(PermissionInfo.PROTECTION_SIGNATURE);
89     }
90 
91     @Test
deviceLockControllerRequestsManageDeviceLockServiceFromControllerPermission()92     public void deviceLockControllerRequestsManageDeviceLockServiceFromControllerPermission() {
93         assertThat(mRequestedPermissions).contains(MANAGE_DEVICE_LOCK_SERVICE_FROM_CONTROLLER);
94     }
95 
96     @Test
deviceLockControllerRequestsStartKioskSetupActivityPermission()97     public void deviceLockControllerRequestsStartKioskSetupActivityPermission() {
98         assertThat(mRequestedPermissions).contains(START_KIOSK_SETUP_ACTIVITY);
99     }
100 
101     // Controller declares but not requests this permission, since it's used between kiosk
102     // and system service component.
103     @Test
deviceLockControllerDoesNotRequestReceiveClearRestrictionsPermission()104     public void deviceLockControllerDoesNotRequestReceiveClearRestrictionsPermission() {
105         assertThat(mRequestedPermissions).doesNotContain(RECEIVE_CLEAR_BROADCAST);
106     }
107 }
108