1 /* 2 * Copyright (C) 2021 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.bedstead.harrier; 18 19 import com.android.bedstead.harrier.annotations.enterprise.EnterprisePolicy; 20 import com.android.bedstead.harrier.annotations.parameterized.IncludeNone; 21 import com.android.bedstead.harrier.annotations.parameterized.IncludeRunOnAffiliatedDeviceOwnerSecondaryUser; 22 import com.android.bedstead.harrier.annotations.parameterized.IncludeRunOnAffiliatedProfileOwnerSecondaryUser; 23 import com.android.bedstead.harrier.annotations.parameterized.IncludeRunOnDeviceOwnerUser; 24 import com.android.bedstead.harrier.annotations.parameterized.IncludeRunOnNonAffiliatedDeviceOwnerSecondaryUser; 25 import com.android.bedstead.harrier.annotations.parameterized.IncludeRunOnParentOfCorporateOwnedProfileOwner; 26 import com.android.bedstead.harrier.annotations.parameterized.IncludeRunOnParentOfProfileOwner; 27 import com.android.bedstead.harrier.annotations.parameterized.IncludeRunOnProfileOwnerPrimaryUser; 28 import com.android.bedstead.harrier.annotations.parameterized.IncludeRunOnProfileOwnerProfile; 29 import com.android.bedstead.harrier.annotations.parameterized.IncludeRunOnSecondaryUserInDifferentProfileGroupToProfileOwner; 30 31 import java.lang.annotation.Annotation; 32 import java.util.ArrayList; 33 import java.util.List; 34 35 /** 36 * Utility class for enterprise policy tests. 37 */ 38 @IncludeNone 39 @IncludeRunOnDeviceOwnerUser 40 @IncludeRunOnAffiliatedDeviceOwnerSecondaryUser 41 @IncludeRunOnAffiliatedProfileOwnerSecondaryUser 42 @IncludeRunOnNonAffiliatedDeviceOwnerSecondaryUser 43 @IncludeRunOnProfileOwnerProfile 44 @IncludeRunOnParentOfCorporateOwnedProfileOwner 45 @IncludeRunOnSecondaryUserInDifferentProfileGroupToProfileOwner 46 @IncludeRunOnParentOfProfileOwner 47 @IncludeRunOnProfileOwnerPrimaryUser 48 public final class Policy { 49 Policy()50 private Policy() { 51 52 } 53 54 private static final IncludeNone INCLUDE_NONE_ANNOTATION = 55 Policy.class.getAnnotation(IncludeNone.class); 56 private static final IncludeRunOnDeviceOwnerUser INCLUDE_RUN_ON_DEVICE_OWNER_USER = 57 Policy.class.getAnnotation(IncludeRunOnDeviceOwnerUser.class); 58 private static final IncludeRunOnNonAffiliatedDeviceOwnerSecondaryUser 59 INCLUDE_RUN_ON_NON_AFFILIATED_DEVICE_OWNER_SECONDARY_USER = 60 Policy.class.getAnnotation(IncludeRunOnNonAffiliatedDeviceOwnerSecondaryUser.class); 61 private static final IncludeRunOnAffiliatedDeviceOwnerSecondaryUser 62 INCLUDE_RUN_ON_AFFILIATED_DEVICE_OWNER_SECONDARY_USER = 63 Policy.class.getAnnotation(IncludeRunOnAffiliatedDeviceOwnerSecondaryUser.class); 64 private static final IncludeRunOnAffiliatedProfileOwnerSecondaryUser 65 INCLUDE_RUN_ON_AFFILIATED_PROFILE_OWNER_SECONDARY_USER = 66 Policy.class.getAnnotation(IncludeRunOnAffiliatedProfileOwnerSecondaryUser.class); 67 private static final IncludeRunOnProfileOwnerProfile 68 INCLUDE_RUN_ON_PROFILE_OWNER_PROFILE = 69 Policy.class.getAnnotation(IncludeRunOnProfileOwnerProfile.class); 70 private static final IncludeRunOnSecondaryUserInDifferentProfileGroupToProfileOwner 71 INCLUDE_RUN_ON_SECONDARY_USER_IN_DIFFERENT_PROFILE_GROUP_TO_PROFILE_OWNER = 72 Policy.class.getAnnotation( 73 IncludeRunOnSecondaryUserInDifferentProfileGroupToProfileOwner.class); 74 private static final IncludeRunOnParentOfProfileOwner INCLUDE_RUN_ON_PARENT_OF_PROFILE_OWNER = 75 Policy.class.getAnnotation(IncludeRunOnParentOfProfileOwner.class); 76 private static final IncludeRunOnParentOfCorporateOwnedProfileOwner 77 INCLUDE_RUN_ON_PARENT_OF_CORPORATE_OWNED_PROFILE_OWNER = 78 Policy.class.getAnnotation(IncludeRunOnParentOfCorporateOwnedProfileOwner.class); 79 private static final IncludeRunOnProfileOwnerPrimaryUser INCLUDE_RUN_ON_PROFILE_OWNER_PRIMARY_USER = 80 Policy.class.getAnnotation(IncludeRunOnProfileOwnerPrimaryUser.class); 81 82 /** 83 * Get positive state annotations for the given policy. 84 * 85 * <p>These are states which should be run where the policy is able to be applied. 86 */ positiveStates(EnterprisePolicy enterprisePolicy)87 public static List<Annotation> positiveStates(EnterprisePolicy enterprisePolicy) { 88 List<Annotation> annotations = new ArrayList<>(); 89 90 switch (enterprisePolicy.deviceOwner()) { 91 case NO: 92 break; 93 case GLOBAL: 94 annotations.add(INCLUDE_RUN_ON_DEVICE_OWNER_USER); 95 annotations.add(INCLUDE_RUN_ON_NON_AFFILIATED_DEVICE_OWNER_SECONDARY_USER); 96 break; 97 case AFFILIATED: 98 annotations.add(INCLUDE_RUN_ON_DEVICE_OWNER_USER); 99 annotations.add(INCLUDE_RUN_ON_AFFILIATED_DEVICE_OWNER_SECONDARY_USER); 100 break; 101 case USER: 102 annotations.add(INCLUDE_RUN_ON_DEVICE_OWNER_USER); 103 break; 104 default: 105 throw new IllegalStateException( 106 "Unknown policy control: " + enterprisePolicy.deviceOwner()); 107 } 108 109 switch (enterprisePolicy.profileOwner()) { 110 case NO: 111 break; 112 case AFFILIATED: 113 annotations.add(INCLUDE_RUN_ON_AFFILIATED_PROFILE_OWNER_SECONDARY_USER); 114 break; 115 case AFFILIATED_OR_NO_DO: 116 annotations.add(INCLUDE_RUN_ON_PROFILE_OWNER_PRIMARY_USER); 117 annotations.add(INCLUDE_RUN_ON_AFFILIATED_PROFILE_OWNER_SECONDARY_USER); 118 break; 119 case PARENT: 120 annotations.add(INCLUDE_RUN_ON_PROFILE_OWNER_PROFILE); 121 annotations.add(INCLUDE_RUN_ON_PARENT_OF_PROFILE_OWNER); 122 break; 123 case COPE_PARENT: 124 annotations.add(INCLUDE_RUN_ON_PROFILE_OWNER_PROFILE); 125 // TODO(scottjonathan): Re-add when we can setup this state 126 // annotations.add(INCLUDE_RUN_ON_PARENT_OF_CORPORATE_OWNED_PROFILE_OWNER); 127 break; 128 case PROFILE: 129 annotations.add(INCLUDE_RUN_ON_PROFILE_OWNER_PROFILE); 130 break; 131 default: 132 throw new IllegalStateException( 133 "Unknown policy control: " + enterprisePolicy.profileOwner()); 134 } 135 136 if (annotations.isEmpty()) { 137 // Don't run the original test unparameterized 138 annotations.add(INCLUDE_NONE_ANNOTATION); 139 } 140 141 return annotations; 142 } 143 144 /** 145 * Get negative state annotations for the given policy. 146 * 147 * <p>These are states which should be run where the policy is not able to be applied. 148 */ negativeStates(EnterprisePolicy enterprisePolicy)149 public static List<Annotation> negativeStates(EnterprisePolicy enterprisePolicy) { 150 List<Annotation> annotations = new ArrayList<>(); 151 152 switch (enterprisePolicy.deviceOwner()) { 153 case NO: 154 break; 155 case GLOBAL: 156 break; 157 case AFFILIATED: 158 annotations.add(INCLUDE_RUN_ON_NON_AFFILIATED_DEVICE_OWNER_SECONDARY_USER); 159 break; 160 case USER: 161 annotations.add(INCLUDE_RUN_ON_AFFILIATED_DEVICE_OWNER_SECONDARY_USER); 162 break; 163 default: 164 throw new IllegalStateException( 165 "Unknown policy control: " + enterprisePolicy.deviceOwner()); 166 } 167 168 switch (enterprisePolicy.profileOwner()) { 169 case NO: 170 break; 171 case AFFILIATED: 172 // TODO(scottjonathan): Define negative states 173 break; 174 case AFFILIATED_OR_NO_DO: 175 // TODO(scottjonathan): Define negative states 176 break; 177 case PARENT: 178 annotations.add( 179 INCLUDE_RUN_ON_SECONDARY_USER_IN_DIFFERENT_PROFILE_GROUP_TO_PROFILE_OWNER); 180 break; 181 case COPE_PARENT: 182 annotations.add( 183 INCLUDE_RUN_ON_SECONDARY_USER_IN_DIFFERENT_PROFILE_GROUP_TO_PROFILE_OWNER); 184 annotations.add( 185 INCLUDE_RUN_ON_PARENT_OF_PROFILE_OWNER); 186 break; 187 case PROFILE: 188 annotations.add( 189 INCLUDE_RUN_ON_SECONDARY_USER_IN_DIFFERENT_PROFILE_GROUP_TO_PROFILE_OWNER); 190 // TODO(scottjonathan): Re-add when we can setup this state 191 // annotations.add( 192 // INCLUDE_RUN_ON_PARENT_OF_CORPORATE_OWNED_PROFILE_OWNER); 193 break; 194 default: 195 throw new IllegalStateException( 196 "Unknown policy control: " + enterprisePolicy.profileOwner()); 197 } 198 199 if (annotations.isEmpty()) { 200 // Don't run the original test unparameterized 201 annotations.add(INCLUDE_NONE_ANNOTATION); 202 } 203 204 return annotations; 205 } 206 207 /** 208 * Get state annotations where the policy cannot be set for the given policy. 209 */ cannotSetPolicyStates(EnterprisePolicy enterprisePolicy)210 public static List<Annotation> cannotSetPolicyStates(EnterprisePolicy enterprisePolicy) { 211 List<Annotation> annotations = new ArrayList<>(); 212 213 // TODO(scottjonathan): Always include a state without a dpc 214 215 if (enterprisePolicy.deviceOwner() == EnterprisePolicy.DeviceOwnerControl.NO) { 216 annotations.add(INCLUDE_RUN_ON_DEVICE_OWNER_USER); 217 } 218 219 if (enterprisePolicy.profileOwner() == EnterprisePolicy.ProfileOwnerControl.NO) { 220 annotations.add(INCLUDE_RUN_ON_PROFILE_OWNER_PROFILE); 221 } else if (enterprisePolicy.profileOwner() == EnterprisePolicy.ProfileOwnerControl.AFFILIATED) { 222 annotations.add(INCLUDE_RUN_ON_PROFILE_OWNER_PROFILE); 223 } else if (enterprisePolicy.profileOwner() == EnterprisePolicy.ProfileOwnerControl.AFFILIATED_OR_NO_DO) { 224 annotations.add(INCLUDE_RUN_ON_PROFILE_OWNER_PROFILE); 225 } 226 227 if (annotations.isEmpty()) { 228 // Don't run the original test unparameterized 229 annotations.add(INCLUDE_NONE_ANNOTATION); 230 } 231 232 return annotations; 233 } 234 } 235