1 /* 2 * Copyright (C) 2023 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 android.devicepolicy.cts.utils; 18 19 import static com.android.bedstead.permissions.CommonPermissions.MANAGE_PROFILE_AND_DEVICE_OWNERS; 20 21 import static org.junit.Assert.fail; 22 23 import android.app.admin.DevicePolicyManager; 24 import android.app.admin.DevicePolicyState; 25 import android.app.admin.LockTaskPolicy; 26 import android.app.admin.MostRecent; 27 import android.app.admin.MostRestrictive; 28 import android.app.admin.PolicyKey; 29 import android.app.admin.PolicyState; 30 import android.app.admin.TopPriority; 31 import android.content.ComponentName; 32 import android.os.UserHandle; 33 34 import com.android.bedstead.nene.TestApis; 35 import com.android.bedstead.permissions.PermissionContext; 36 37 import java.util.List; 38 import java.util.Set; 39 40 public final class PolicyEngineUtils { 41 PolicyEngineUtils()42 private PolicyEngineUtils() {} 43 44 public static final List<Boolean> TRUE_MORE_RESTRICTIVE = List.of(true, false); 45 46 public static final List<Boolean> FALSE_MORE_RESTRICTIVE = List.of(false, true); 47 48 public static String FINANCED_DEVICE_CONTROLLER_ROLE = 49 "android.app.role.SYSTEM_FINANCED_DEVICE_CONTROLLER"; 50 getBooleanPolicyState(PolicyKey policyKey, UserHandle user)51 public static PolicyState<Boolean> getBooleanPolicyState(PolicyKey policyKey, UserHandle user) { 52 try (PermissionContext p = TestApis.permissions().withPermission( 53 MANAGE_PROFILE_AND_DEVICE_OWNERS)) { 54 DevicePolicyManager dpm = TestApis.context().instrumentedContext().getSystemService( 55 DevicePolicyManager.class); 56 DevicePolicyState state = dpm.getDevicePolicyState(); 57 if (state.getPoliciesForUser(user).get(policyKey) != null) { 58 return (PolicyState<Boolean>) state.getPoliciesForUser(user).get(policyKey); 59 } else { 60 return (PolicyState<Boolean>) state.getPoliciesForUser(UserHandle.ALL) 61 .get(policyKey); 62 } 63 } catch (ClassCastException e) { 64 fail("Returned policy is not of type Boolean: " + e); 65 return null; 66 } 67 } 68 getStringSetPolicyState( PolicyKey policyKey, UserHandle user)69 public static PolicyState<Set<String>> getStringSetPolicyState( 70 PolicyKey policyKey, UserHandle user) { 71 try (PermissionContext p = TestApis.permissions().withPermission( 72 MANAGE_PROFILE_AND_DEVICE_OWNERS)) { 73 DevicePolicyManager dpm = TestApis.context().instrumentedContext().getSystemService( 74 DevicePolicyManager.class); 75 DevicePolicyState state = dpm.getDevicePolicyState(); 76 if (state.getPoliciesForUser(user).get(policyKey) != null) { 77 return (PolicyState<Set<String>>) state.getPoliciesForUser(user).get(policyKey); 78 } else { 79 return (PolicyState<Set<String>>) state.getPoliciesForUser(UserHandle.ALL) 80 .get(policyKey); 81 } 82 } catch (ClassCastException e) { 83 fail("Returned policy is not of type Set<String>: " + e); 84 return null; 85 } 86 } 87 getLockTaskPolicyState( PolicyKey policyKey, UserHandle user)88 public static PolicyState<LockTaskPolicy> getLockTaskPolicyState( 89 PolicyKey policyKey, UserHandle user) { 90 try (PermissionContext p = TestApis.permissions().withPermission( 91 MANAGE_PROFILE_AND_DEVICE_OWNERS)) { 92 DevicePolicyManager dpm = TestApis.context().instrumentedContext().getSystemService( 93 DevicePolicyManager.class); 94 DevicePolicyState state = dpm.getDevicePolicyState(); 95 return (PolicyState<LockTaskPolicy>) state.getPoliciesForUser(user).get(policyKey); 96 } catch (ClassCastException e) { 97 fail("Returned policy is not of type LockTaskPolicy: " + e); 98 return null; 99 } 100 } 101 getComponentNamePolicyState( PolicyKey policyKey, UserHandle user)102 public static PolicyState<ComponentName> getComponentNamePolicyState( 103 PolicyKey policyKey, UserHandle user) { 104 try (PermissionContext p = TestApis.permissions().withPermission( 105 MANAGE_PROFILE_AND_DEVICE_OWNERS)) { 106 DevicePolicyManager dpm = TestApis.context().instrumentedContext().getSystemService( 107 DevicePolicyManager.class); 108 DevicePolicyState state = dpm.getDevicePolicyState(); 109 if (state.getPoliciesForUser(user).get(policyKey) != null) { 110 return (PolicyState<ComponentName>) state.getPoliciesForUser(user).get(policyKey); 111 } else { 112 return (PolicyState<ComponentName>) state.getPoliciesForUser(UserHandle.ALL) 113 .get(policyKey); 114 } 115 } catch (ClassCastException e) { 116 fail("Returned policy is not of type ComponentName: " + e); 117 return null; 118 } 119 } 120 getMostRestrictiveBooleanMechanism( PolicyState<Boolean> policyState)121 public static MostRestrictive<Boolean> getMostRestrictiveBooleanMechanism( 122 PolicyState<Boolean> policyState) { 123 try { 124 return (MostRestrictive<Boolean>) policyState.getResolutionMechanism(); 125 } catch (ClassCastException e) { 126 fail("Returned resolution mechanism is not of type MostRestrictive<Boolean>: " + e); 127 return null; 128 } 129 } 130 getTopPriorityMechanism(PolicyState<?> policyState)131 public static TopPriority<?> getTopPriorityMechanism(PolicyState<?> policyState) { 132 try { 133 return (TopPriority<?>) policyState.getResolutionMechanism(); 134 } catch (ClassCastException e) { 135 fail("Returned resolution mechanism is not of type TopPriority<>: " + e); 136 return null; 137 } 138 } 139 getMostRecentStringSetMechanism( PolicyState<Set<String>> policyState)140 public static MostRecent<Set<String>> getMostRecentStringSetMechanism( 141 PolicyState<Set<String>> policyState) { 142 try { 143 return (MostRecent<Set<String>>) policyState.getResolutionMechanism(); 144 } catch (ClassCastException e) { 145 fail("Returned resolution mechanism is not of type MostRecent<Set<String>>: " + e); 146 return null; 147 } 148 } 149 } 150