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_PROVISION_MANAGED_PROFILE =
58             ProvisioningParams.Builder.builder()
59             .setProvisioningAction(ACTION_PROVISION_MANAGED_PROFILE)
60             .setDeviceAdminComponentName(TEST_MDM_ADMIN)
61             .setStartedByTrustedSource(false)
62             .build();
63     private static final ProvisioningParams PARAMS_PROVISION_FINANCED_DEVICE =
64             ProvisioningParams.Builder.builder()
65                     .setProvisioningAction(ACTION_PROVISION_FINANCED_DEVICE)
66                     .setDeviceAdminComponentName(TEST_MDM_ADMIN)
67                     .setStartedByTrustedSource(false)
68                     .build();
69     private static final ProvisioningParams PARAMS_NON_TRUSTED_SOURCE =
70             PARAMS_PROVISION_MANAGED_PROFILE;
71 
72     private final Context mContext = ApplicationProvider.getApplicationContext();
73     private Utils mUtils = new Utils();
74     private PolicyComplianceUtils mPolicyComplianceUtils = new PolicyComplianceUtils();
75     private GetProvisioningModeUtils mGetProvisioningModeUtils = new GetProvisioningModeUtils();
76 
77     @Test
shouldPerformAdminIntegratedFlow_allConditionsMet_returnsTrue()78     public void shouldPerformAdminIntegratedFlow_allConditionsMet_returnsTrue() {
79         Intent policyComplianceIntent = getPolicyComplianceIntent();
80         Intent getProvisioningModeIntent = getGetProvisioningModeIntent();
81         ResolveInfo info = createFakeResolveInfo();
82         shadowOf(mContext.getPackageManager())
83                 .addResolveInfoForIntent(policyComplianceIntent, info);
84         shadowOf(mContext.getPackageManager())
85                 .addResolveInfoForIntent(getProvisioningModeIntent, info);
86 
87         assertThat(mUtils.canPerformAdminIntegratedFlow(mContext, PARAMS_ORG_OWNED,
88                 mPolicyComplianceUtils, mGetProvisioningModeUtils)).isTrue();
89     }
90 
91     @Test
shouldPerformAdminIntegratedFlow_noPolicyComplianceScreen_returnsFalse()92     public void shouldPerformAdminIntegratedFlow_noPolicyComplianceScreen_returnsFalse() {
93         Intent getProvisioningModeIntent = getGetProvisioningModeIntent();
94         ResolveInfo info = createFakeResolveInfo();
95         shadowOf(mContext.getPackageManager())
96                 .addResolveInfoForIntent(getProvisioningModeIntent, info);
97 
98         assertThat(mUtils.canPerformAdminIntegratedFlow(mContext, PARAMS_ORG_OWNED,
99                 mPolicyComplianceUtils, mGetProvisioningModeUtils)).isFalse();
100     }
101 
102     @Test
shouldPerformAdminIntegratedFlow_noGetProvisioningModeScreen_returnsFalse()103     public void shouldPerformAdminIntegratedFlow_noGetProvisioningModeScreen_returnsFalse() {
104         Intent policyComplianceIntent = getPolicyComplianceIntent();
105         ResolveInfo info = createFakeResolveInfo();
106         shadowOf(mContext.getPackageManager())
107                 .addResolveInfoForIntent(policyComplianceIntent, info);
108 
109         assertThat(mUtils.canPerformAdminIntegratedFlow(mContext, PARAMS_ORG_OWNED,
110                 mPolicyComplianceUtils, mGetProvisioningModeUtils)).isFalse();
111     }
112 
113     @Test
checkAdminIntegratedFlowPreconditions_notStartedByTrustedSource_returnsFalse()114     public void checkAdminIntegratedFlowPreconditions_notStartedByTrustedSource_returnsFalse() {
115         assertThat(mUtils.checkAdminIntegratedFlowPreconditions(
116                 PARAMS_NON_TRUSTED_SOURCE)).isFalse();
117     }
118 
119     @Test
checkAdminIntegratedFlowPreconditions_financedDevice_returnsFalse()120     public void checkAdminIntegratedFlowPreconditions_financedDevice_returnsFalse() {
121         assertThat(mUtils.checkAdminIntegratedFlowPreconditions(
122                 PARAMS_PROVISION_FINANCED_DEVICE)).isFalse();
123     }
124 
125     @Test
isOrganizationOwnedAllowed_personallyOwnedProvisioning_returnsFalse()126     public void isOrganizationOwnedAllowed_personallyOwnedProvisioning_returnsFalse() {
127         ProvisioningParams params = createTrustedSourceParamsBuilder()
128                 .setInitiatorRequestedProvisioningModes(
129                         FLAG_SUPPORTED_MODES_PERSONALLY_OWNED)
130                 .build();
131 
132         assertThat(mUtils.isOrganizationOwnedAllowed(params)).isFalse();
133     }
134 
135     @Test
isOrganizationOwnedAllowed_organizationOwnedProvisioning_returnsTrue()136     public void isOrganizationOwnedAllowed_organizationOwnedProvisioning_returnsTrue() {
137         ProvisioningParams params = createTrustedSourceParamsBuilder()
138                 .setInitiatorRequestedProvisioningModes(
139                         FLAG_SUPPORTED_MODES_ORGANIZATION_OWNED)
140                 .build();
141 
142         assertThat(mUtils.isOrganizationOwnedAllowed(params)).isTrue();
143     }
144 
145     @Test
146     public void
isOrganizationOwnedAllowed_organizationAndPersonallyOwnedProvisioning_returnsTrue()147             isOrganizationOwnedAllowed_organizationAndPersonallyOwnedProvisioning_returnsTrue() {
148         ProvisioningParams params = createTrustedSourceParamsBuilder()
149                 .setInitiatorRequestedProvisioningModes(
150                         FLAG_SUPPORTED_MODES_ORGANIZATION_OWNED
151                                 | FLAG_SUPPORTED_MODES_PERSONALLY_OWNED)
152                 .build();
153 
154         assertThat(mUtils.isOrganizationOwnedAllowed(params)).isTrue();
155     }
156 
157     @Test
isOrganizationOwnedAllowed_deviceOwnerProvisioning_returnsTrue()158     public void isOrganizationOwnedAllowed_deviceOwnerProvisioning_returnsTrue() {
159         ProvisioningParams params = createTrustedSourceParamsBuilder()
160                 .setInitiatorRequestedProvisioningModes(
161                         FLAG_SUPPORTED_MODES_DEVICE_OWNER)
162                 .build();
163 
164         assertThat(mUtils.isOrganizationOwnedAllowed(params)).isTrue();
165     }
166 
167     @Test
shouldShowOwnershipDisclaimerScreen_skipOwnershipDisclaimerFalse_returnsTrue()168     public void shouldShowOwnershipDisclaimerScreen_skipOwnershipDisclaimerFalse_returnsTrue() {
169         ProvisioningParams params = createTrustedSourceParamsBuilder()
170                 .setSkipOwnershipDisclaimer(false)
171                 .build();
172 
173         assertThat(mUtils.shouldShowOwnershipDisclaimerScreen(params)).isTrue();
174     }
175 
176     @Test
shouldShowOwnershipDisclaimerScreen_showOwnershipDisclaimerTrue_returnsFalse()177     public void shouldShowOwnershipDisclaimerScreen_showOwnershipDisclaimerTrue_returnsFalse() {
178         ProvisioningParams params = createTrustedSourceParamsBuilder()
179                 .setSkipOwnershipDisclaimer(true)
180                 .build();
181 
182         assertThat(mUtils.shouldShowOwnershipDisclaimerScreen(params)).isFalse();
183     }
184 
185     @Test
isPackageInstalled_packageExists_returnsTrue()186     public void isPackageInstalled_packageExists_returnsTrue() {
187         PackageInfo packageInfo = new PackageInfo();
188         packageInfo.applicationInfo = new ApplicationInfo();
189         packageInfo.packageName = "com.example.package";
190         shadowOf(mContext.getPackageManager()).installPackage(packageInfo);
191 
192         assertThat(mUtils
193                 .isPackageInstalled("com.example.package", mContext.getPackageManager())).isTrue();
194     }
195 
196     @Test
isPackageInstalled_packageDoesNotExist_returnsFalse()197     public void isPackageInstalled_packageDoesNotExist_returnsFalse() {
198         assertThat(mUtils
199                 .isPackageInstalled("com.example.package", mContext.getPackageManager())).isFalse();
200     }
201 
createTrustedSourceParamsBuilder()202     private static ProvisioningParams.Builder createTrustedSourceParamsBuilder() {
203         return ProvisioningParams.Builder.builder()
204                 .setProvisioningAction(ACTION_PROVISION_MANAGED_DEVICE_FROM_TRUSTED_SOURCE)
205                 .setDeviceAdminComponentName(TEST_MDM_ADMIN)
206                 .setStartedByTrustedSource(true);
207     }
208 
getGetProvisioningModeIntent()209     private Intent getGetProvisioningModeIntent() {
210         final Intent intentGetMode = new Intent(ACTION_GET_PROVISIONING_MODE);
211         intentGetMode.setPackage(TEST_MDM_PACKAGE_NAME);
212         return intentGetMode;
213     }
214 
getPolicyComplianceIntent()215     private Intent getPolicyComplianceIntent() {
216         Intent policyComplianceIntent =
217                 new Intent(DevicePolicyManager.ACTION_ADMIN_POLICY_COMPLIANCE);
218         policyComplianceIntent.setPackage(TEST_MDM_PACKAGE_NAME);
219         return policyComplianceIntent;
220     }
221 
createFakeResolveInfo()222     private ResolveInfo createFakeResolveInfo() {
223         ResolveInfo info = new ResolveInfo();
224         ApplicationInfo applicationInfo = new ApplicationInfo();
225         applicationInfo.packageName = TEST_MDM_PACKAGE_NAME;
226         ActivityInfo activityInfo = new ActivityInfo();
227         activityInfo.applicationInfo = applicationInfo;
228         activityInfo.name = "SomeClassName";
229         info.activityInfo = activityInfo;
230         return info;
231     }
232 }
233