1 /* 2 * Copyright (C) 2020 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.managedprovisioning.common; 18 19 import static android.app.admin.DevicePolicyManager.ACTION_GET_PROVISIONING_MODE; 20 import static android.app.admin.DevicePolicyManager.ACTION_PROVISION_FINANCED_DEVICE; 21 import static android.app.admin.DevicePolicyManager.ACTION_PROVISION_MANAGED_DEVICE_FROM_TRUSTED_SOURCE; 22 import static android.app.admin.DevicePolicyManager.ACTION_PROVISION_MANAGED_PROFILE; 23 import static android.app.admin.DevicePolicyManager.FLAG_SUPPORTED_MODES_DEVICE_OWNER; 24 import static android.app.admin.DevicePolicyManager.FLAG_SUPPORTED_MODES_ORGANIZATION_OWNED; 25 import static android.app.admin.DevicePolicyManager.FLAG_SUPPORTED_MODES_PERSONALLY_OWNED; 26 27 import static com.google.common.truth.Truth.assertThat; 28 29 import static org.robolectric.Shadows.shadowOf; 30 31 import android.app.admin.DevicePolicyManager; 32 import android.content.ComponentName; 33 import android.content.Context; 34 import android.content.Intent; 35 import android.content.pm.ActivityInfo; 36 import android.content.pm.ApplicationInfo; 37 import android.content.pm.PackageInfo; 38 import android.content.pm.ResolveInfo; 39 40 import androidx.test.core.app.ApplicationProvider; 41 42 import com.android.managedprovisioning.model.ProvisioningParams; 43 44 import org.junit.Test; 45 import org.junit.runner.RunWith; 46 import org.robolectric.RobolectricTestRunner; 47 48 @RunWith(RobolectricTestRunner.class) 49 public class UtilsRoboTest { 50 private static final String TEST_MDM_PACKAGE_NAME = "mdm.package.name"; 51 private static final String TEST_MDM_ADMIN_RECEIVER = TEST_MDM_PACKAGE_NAME + ".AdminReceiver"; 52 private static final ComponentName TEST_MDM_ADMIN = new ComponentName(TEST_MDM_PACKAGE_NAME, 53 TEST_MDM_ADMIN_RECEIVER); 54 private static final ProvisioningParams PARAMS_ORG_OWNED = createTrustedSourceParamsBuilder() 55 .setIsOrganizationOwnedProvisioning(true) 56 .build(); 57 private static final ProvisioningParams PARAMS_NFC = createTrustedSourceParamsBuilder() 58 .setIsOrganizationOwnedProvisioning(true) 59 .setIsNfc(true) 60 .build(); 61 private static final ProvisioningParams PARAMS_PROVISION_MANAGED_PROFILE = 62 ProvisioningParams.Builder.builder() 63 .setProvisioningAction(ACTION_PROVISION_MANAGED_PROFILE) 64 .setDeviceAdminComponentName(TEST_MDM_ADMIN) 65 .setStartedByTrustedSource(false) 66 .build(); 67 private static final ProvisioningParams PARAMS_PROVISION_FINANCED_DEVICE = 68 ProvisioningParams.Builder.builder() 69 .setProvisioningAction(ACTION_PROVISION_FINANCED_DEVICE) 70 .setDeviceAdminComponentName(TEST_MDM_ADMIN) 71 .setStartedByTrustedSource(false) 72 .build(); 73 private static final ProvisioningParams PARAMS_NON_TRUSTED_SOURCE = 74 PARAMS_PROVISION_MANAGED_PROFILE; 75 76 private final Context mContext = ApplicationProvider.getApplicationContext(); 77 private Utils mUtils = new Utils(); 78 private PolicyComplianceUtils mPolicyComplianceUtils = new PolicyComplianceUtils(); 79 private GetProvisioningModeUtils mGetProvisioningModeUtils = new GetProvisioningModeUtils(); 80 81 @Test shouldPerformAdminIntegratedFlow_allConditionsMet_returnsTrue()82 public void shouldPerformAdminIntegratedFlow_allConditionsMet_returnsTrue() { 83 Intent policyComplianceIntent = getPolicyComplianceIntent(); 84 Intent getProvisioningModeIntent = getGetProvisioningModeIntent(); 85 ResolveInfo info = createFakeResolveInfo(); 86 shadowOf(mContext.getPackageManager()) 87 .addResolveInfoForIntent(policyComplianceIntent, info); 88 shadowOf(mContext.getPackageManager()) 89 .addResolveInfoForIntent(getProvisioningModeIntent, info); 90 91 assertThat(mUtils.canPerformAdminIntegratedFlow(mContext, PARAMS_ORG_OWNED, 92 mPolicyComplianceUtils, mGetProvisioningModeUtils)).isTrue(); 93 } 94 95 @Test shouldPerformAdminIntegratedFlow_noPolicyComplianceScreen_returnsFalse()96 public void shouldPerformAdminIntegratedFlow_noPolicyComplianceScreen_returnsFalse() { 97 Intent getProvisioningModeIntent = getGetProvisioningModeIntent(); 98 ResolveInfo info = createFakeResolveInfo(); 99 shadowOf(mContext.getPackageManager()) 100 .addResolveInfoForIntent(getProvisioningModeIntent, info); 101 102 assertThat(mUtils.canPerformAdminIntegratedFlow(mContext, PARAMS_ORG_OWNED, 103 mPolicyComplianceUtils, mGetProvisioningModeUtils)).isFalse(); 104 } 105 106 @Test shouldPerformAdminIntegratedFlow_noGetProvisioningModeScreen_returnsFalse()107 public void shouldPerformAdminIntegratedFlow_noGetProvisioningModeScreen_returnsFalse() { 108 Intent policyComplianceIntent = getPolicyComplianceIntent(); 109 ResolveInfo info = createFakeResolveInfo(); 110 shadowOf(mContext.getPackageManager()) 111 .addResolveInfoForIntent(policyComplianceIntent, info); 112 113 assertThat(mUtils.canPerformAdminIntegratedFlow(mContext, PARAMS_ORG_OWNED, 114 mPolicyComplianceUtils, mGetProvisioningModeUtils)).isFalse(); 115 } 116 117 @Test shouldPerformAdminIntegratedFlow_nfcProvisioning_returnsFalse()118 public void shouldPerformAdminIntegratedFlow_nfcProvisioning_returnsFalse() { 119 Intent policyComplianceIntent = getPolicyComplianceIntent(); 120 Intent getProvisioningModeIntent = getGetProvisioningModeIntent(); 121 ResolveInfo info = createFakeResolveInfo(); 122 shadowOf(mContext.getPackageManager()) 123 .addResolveInfoForIntent(policyComplianceIntent, info); 124 shadowOf(mContext.getPackageManager()) 125 .addResolveInfoForIntent(getProvisioningModeIntent, info); 126 127 assertThat(mUtils.canPerformAdminIntegratedFlow(mContext, PARAMS_NFC, 128 mPolicyComplianceUtils, mGetProvisioningModeUtils)).isFalse(); 129 } 130 131 @Test checkAdminIntegratedFlowPreconditions_nfcProvisioning_returnsFalse()132 public void checkAdminIntegratedFlowPreconditions_nfcProvisioning_returnsFalse() { 133 assertThat(mUtils.checkAdminIntegratedFlowPreconditions(PARAMS_NFC)).isFalse(); 134 } 135 136 @Test checkAdminIntegratedFlowPreconditions_notStartedByTrustedSource_returnsFalse()137 public void checkAdminIntegratedFlowPreconditions_notStartedByTrustedSource_returnsFalse() { 138 assertThat(mUtils.checkAdminIntegratedFlowPreconditions( 139 PARAMS_NON_TRUSTED_SOURCE)).isFalse(); 140 } 141 142 @Test checkAdminIntegratedFlowPreconditions_financedDevice_returnsFalse()143 public void checkAdminIntegratedFlowPreconditions_financedDevice_returnsFalse() { 144 assertThat(mUtils.checkAdminIntegratedFlowPreconditions( 145 PARAMS_PROVISION_FINANCED_DEVICE)).isFalse(); 146 } 147 148 @Test isOrganizationOwnedAllowed_personallyOwnedProvisioning_returnsFalse()149 public void isOrganizationOwnedAllowed_personallyOwnedProvisioning_returnsFalse() { 150 ProvisioningParams params = createTrustedSourceParamsBuilder() 151 .setInitiatorRequestedProvisioningModes( 152 FLAG_SUPPORTED_MODES_PERSONALLY_OWNED) 153 .build(); 154 155 assertThat(mUtils.isOrganizationOwnedAllowed(params)).isFalse(); 156 } 157 158 @Test isOrganizationOwnedAllowed_organizationOwnedProvisioning_returnsTrue()159 public void isOrganizationOwnedAllowed_organizationOwnedProvisioning_returnsTrue() { 160 ProvisioningParams params = createTrustedSourceParamsBuilder() 161 .setInitiatorRequestedProvisioningModes( 162 FLAG_SUPPORTED_MODES_ORGANIZATION_OWNED) 163 .build(); 164 165 assertThat(mUtils.isOrganizationOwnedAllowed(params)).isTrue(); 166 } 167 168 @Test 169 public void isOrganizationOwnedAllowed_organizationAndPersonallyOwnedProvisioning_returnsTrue()170 isOrganizationOwnedAllowed_organizationAndPersonallyOwnedProvisioning_returnsTrue() { 171 ProvisioningParams params = createTrustedSourceParamsBuilder() 172 .setInitiatorRequestedProvisioningModes( 173 FLAG_SUPPORTED_MODES_ORGANIZATION_OWNED 174 | FLAG_SUPPORTED_MODES_PERSONALLY_OWNED) 175 .build(); 176 177 assertThat(mUtils.isOrganizationOwnedAllowed(params)).isTrue(); 178 } 179 180 @Test isOrganizationOwnedAllowed_deviceOwnerProvisioning_returnsTrue()181 public void isOrganizationOwnedAllowed_deviceOwnerProvisioning_returnsTrue() { 182 ProvisioningParams params = createTrustedSourceParamsBuilder() 183 .setInitiatorRequestedProvisioningModes( 184 FLAG_SUPPORTED_MODES_DEVICE_OWNER) 185 .build(); 186 187 assertThat(mUtils.isOrganizationOwnedAllowed(params)).isTrue(); 188 } 189 190 @Test shouldShowOwnershipDisclaimerScreen_skipOwnershipDisclaimerFalse_returnsTrue()191 public void shouldShowOwnershipDisclaimerScreen_skipOwnershipDisclaimerFalse_returnsTrue() { 192 ProvisioningParams params = createTrustedSourceParamsBuilder() 193 .setSkipOwnershipDisclaimer(false) 194 .build(); 195 196 assertThat(mUtils.shouldShowOwnershipDisclaimerScreen(params)).isTrue(); 197 } 198 199 @Test shouldShowOwnershipDisclaimerScreen_showOwnershipDisclaimerTrue_returnsFalse()200 public void shouldShowOwnershipDisclaimerScreen_showOwnershipDisclaimerTrue_returnsFalse() { 201 ProvisioningParams params = createTrustedSourceParamsBuilder() 202 .setSkipOwnershipDisclaimer(true) 203 .build(); 204 205 assertThat(mUtils.shouldShowOwnershipDisclaimerScreen(params)).isFalse(); 206 } 207 208 @Test isPackageInstalled_packageExists_returnsTrue()209 public void isPackageInstalled_packageExists_returnsTrue() { 210 PackageInfo packageInfo = new PackageInfo(); 211 packageInfo.applicationInfo = new ApplicationInfo(); 212 packageInfo.packageName = "com.example.package"; 213 shadowOf(mContext.getPackageManager()).installPackage(packageInfo); 214 215 assertThat(mUtils 216 .isPackageInstalled("com.example.package", mContext.getPackageManager())).isTrue(); 217 } 218 219 @Test isPackageInstalled_packageDoesNotExist_returnsFalse()220 public void isPackageInstalled_packageDoesNotExist_returnsFalse() { 221 assertThat(mUtils 222 .isPackageInstalled("com.example.package", mContext.getPackageManager())).isFalse(); 223 } 224 createTrustedSourceParamsBuilder()225 private static ProvisioningParams.Builder createTrustedSourceParamsBuilder() { 226 return ProvisioningParams.Builder.builder() 227 .setProvisioningAction(ACTION_PROVISION_MANAGED_DEVICE_FROM_TRUSTED_SOURCE) 228 .setDeviceAdminComponentName(TEST_MDM_ADMIN) 229 .setStartedByTrustedSource(true); 230 } 231 getGetProvisioningModeIntent()232 private Intent getGetProvisioningModeIntent() { 233 final Intent intentGetMode = new Intent(ACTION_GET_PROVISIONING_MODE); 234 intentGetMode.setPackage(TEST_MDM_PACKAGE_NAME); 235 return intentGetMode; 236 } 237 getPolicyComplianceIntent()238 private Intent getPolicyComplianceIntent() { 239 Intent policyComplianceIntent = 240 new Intent(DevicePolicyManager.ACTION_ADMIN_POLICY_COMPLIANCE); 241 policyComplianceIntent.setPackage(TEST_MDM_PACKAGE_NAME); 242 return policyComplianceIntent; 243 } 244 createFakeResolveInfo()245 private ResolveInfo createFakeResolveInfo() { 246 ResolveInfo info = new ResolveInfo(); 247 ApplicationInfo applicationInfo = new ApplicationInfo(); 248 applicationInfo.packageName = TEST_MDM_PACKAGE_NAME; 249 ActivityInfo activityInfo = new ActivityInfo(); 250 activityInfo.applicationInfo = applicationInfo; 251 activityInfo.name = "SomeClassName"; 252 info.activityInfo = activityInfo; 253 return info; 254 } 255 } 256