1 /*
2  * Copyright (C) 2015 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.task;
18 
19 import static android.app.admin.DevicePolicyManager.ACTION_PROVISION_FINANCED_DEVICE;
20 import static android.app.admin.DevicePolicyManager.ACTION_PROVISION_MANAGED_DEVICE;
21 import static android.app.admin.DevicePolicyManager.DEVICE_OWNER_TYPE_FINANCED;
22 
23 import static org.mockito.ArgumentMatchers.any;
24 import static org.mockito.ArgumentMatchers.anyInt;
25 import static org.mockito.ArgumentMatchers.eq;
26 import static org.mockito.Mockito.mock;
27 import static org.mockito.Mockito.never;
28 import static org.mockito.Mockito.verify;
29 import static org.mockito.Mockito.verifyNoMoreInteractions;
30 import static org.mockito.Mockito.when;
31 
32 import android.app.admin.DevicePolicyManager;
33 import android.content.ComponentName;
34 import android.content.Context;
35 import android.content.pm.PackageManager;
36 import android.test.AndroidTestCase;
37 
38 import androidx.test.filters.SmallTest;
39 
40 import com.android.managedprovisioning.analytics.ProvisioningAnalyticsTracker;
41 import com.android.managedprovisioning.common.Utils;
42 import com.android.managedprovisioning.model.ProvisioningParams;
43 
44 import org.mockito.Mock;
45 import org.mockito.MockitoAnnotations;
46 
47 public class SetDeviceOwnerPolicyTaskTest extends AndroidTestCase {
48     private static final String ADMIN_PACKAGE_NAME = "com.admin.test";
49     private static final String ADMIN_RECEIVER_NAME = ADMIN_PACKAGE_NAME + ".AdminReceiver";
50     private static final ComponentName ADMIN_COMPONENT_NAME = new ComponentName(ADMIN_PACKAGE_NAME,
51             ADMIN_RECEIVER_NAME);
52     private static final int TEST_USER_ID = 123;
53 
54     @Mock private Context mContext;
55     @Mock private PackageManager mPackageManager;
56     @Mock private DevicePolicyManager mDevicePolicyManager;
57     @Mock private AbstractProvisioningTask.Callback mCallback;
58     @Mock private Utils mUtils;
59 
60     private SetDeviceOwnerPolicyTask mTask;
61 
62     @Override
setUp()63     protected void setUp() throws Exception {
64         super.setUp();
65         // This is necessary for mockito to work
66         System.setProperty("dexmaker.dexcache", getContext().getCacheDir().toString());
67         MockitoAnnotations.initMocks(this);
68 
69         when(mContext.getPackageManager()).thenReturn(mPackageManager);
70         when(mContext.getSystemService(Context.DEVICE_POLICY_SERVICE))
71                 .thenReturn(mDevicePolicyManager);
72         when(mContext.getResources()).thenReturn(getContext().getResources());
73 
74         when(mPackageManager.getApplicationEnabledSetting(ADMIN_PACKAGE_NAME))
75                 .thenReturn(PackageManager.COMPONENT_ENABLED_STATE_DEFAULT);
76         when(mUtils.getCurrentDeviceOwnerComponentName(mDevicePolicyManager)).thenReturn(null);
77         when(mDevicePolicyManager.setDeviceOwner(ADMIN_COMPONENT_NAME, TEST_USER_ID))
78                 .thenReturn(true);
79         when(mUtils.findDeviceAdmin(null, ADMIN_COMPONENT_NAME, mContext, TEST_USER_ID))
80                 .thenReturn(ADMIN_COMPONENT_NAME);
81     }
82 
83     @SmallTest
testEnableDevicePolicyApp_DefaultToDefault()84     public void testEnableDevicePolicyApp_DefaultToDefault() {
85         // GIVEN that we are provisioning device owner
86         createTask(ACTION_PROVISION_MANAGED_DEVICE);
87         // GIVEN that the management app is currently the manifest default
88         when(mPackageManager.getApplicationEnabledSetting(ADMIN_PACKAGE_NAME))
89                 .thenReturn(PackageManager.COMPONENT_ENABLED_STATE_DEFAULT);
90 
91         // WHEN running the task
92         mTask.run(TEST_USER_ID);
93 
94         // THEN the management app should still be default
95         verify(mPackageManager, never()).setApplicationEnabledSetting(eq(ADMIN_PACKAGE_NAME),
96                 anyInt(), anyInt());
97         verify(mCallback).onSuccess(mTask);
98         verifyNoMoreInteractions(mCallback);
99     }
100 
101     @SmallTest
testEnableDevicePolicyApp_DisabledToDefault()102     public void testEnableDevicePolicyApp_DisabledToDefault() {
103         // GIVEN that we are provisioning device owner
104         createTask(ACTION_PROVISION_MANAGED_DEVICE);
105         // GIVEN that the management app is currently disabled
106         when(mPackageManager.getApplicationEnabledSetting(ADMIN_PACKAGE_NAME))
107                 .thenReturn(PackageManager.COMPONENT_ENABLED_STATE_DISABLED);
108 
109         // WHEN running the task
110         mTask.run(TEST_USER_ID);
111 
112         // THEN the management app should have been enabled
113         verify(mPackageManager).setApplicationEnabledSetting(ADMIN_PACKAGE_NAME,
114                 PackageManager.COMPONENT_ENABLED_STATE_DEFAULT,
115                 PackageManager.DONT_KILL_APP);
116         verify(mCallback).onSuccess(mTask);
117         verifyNoMoreInteractions(mCallback);
118     }
119 
120     @SmallTest
testEnableDevicePolicyApp_EnabledToEnabled()121     public void testEnableDevicePolicyApp_EnabledToEnabled() {
122         // GIVEN that we are provisioning device owner
123         createTask(ACTION_PROVISION_MANAGED_DEVICE);
124         // GIVEN that the management app is currently enabled
125         when(mPackageManager.getApplicationEnabledSetting(ADMIN_PACKAGE_NAME))
126                 .thenReturn(PackageManager.COMPONENT_ENABLED_STATE_ENABLED);
127 
128         // WHEN running the task
129         mTask.run(TEST_USER_ID);
130 
131         // THEN the management app should have been untouched
132         verify(mPackageManager, never()).setApplicationEnabledSetting(eq(ADMIN_PACKAGE_NAME),
133                 anyInt(), anyInt());
134         verify(mCallback).onSuccess(mTask);
135         verifyNoMoreInteractions(mCallback);
136     }
137 
138     @SmallTest
testEnableDevicePolicyApp_PackageNotFound()139     public void testEnableDevicePolicyApp_PackageNotFound() {
140         // GIVEN that we are provisioning device owner
141         createTask(ACTION_PROVISION_MANAGED_DEVICE);
142         // GIVEN that the management app is not present on the device
143         when(mPackageManager.getApplicationEnabledSetting(ADMIN_PACKAGE_NAME))
144                 .thenThrow(new IllegalArgumentException());
145 
146         // WHEN running the task
147         mTask.run(TEST_USER_ID);
148 
149         // THEN an error should be returned
150         verify(mCallback).onError(mTask, 0, /* errorMessage= */ null);
151         verifyNoMoreInteractions(mCallback);
152     }
153 
154     @SmallTest
testSetActiveAdmin()155     public void testSetActiveAdmin() {
156         // GIVEN that we are provisioning device owner
157         createTask(ACTION_PROVISION_MANAGED_DEVICE);
158 
159         // WHEN running the task
160         mTask.run(TEST_USER_ID);
161 
162         // THEN the management app should have been set as active admin
163         verify(mDevicePolicyManager).setActiveAdmin(ADMIN_COMPONENT_NAME, true, TEST_USER_ID);
164         verify(mCallback).onSuccess(mTask);
165         verifyNoMoreInteractions(mCallback);
166     }
167 
168     @SmallTest
testSetDeviceOwner()169     public void testSetDeviceOwner() {
170         // GIVEN that we are provisioning device owner
171         createTask(ACTION_PROVISION_MANAGED_DEVICE);
172 
173         // WHEN running the task
174         mTask.run(TEST_USER_ID);
175 
176         // THEN the management app should have been set as device owner
177         verify(mDevicePolicyManager).setDeviceOwner(ADMIN_COMPONENT_NAME, TEST_USER_ID);
178         verify(mCallback).onSuccess(mTask);
179         verifyNoMoreInteractions(mCallback);
180     }
181 
182     @SmallTest
testSetDeviceOwner_PreconditionsNotMet()183     public void testSetDeviceOwner_PreconditionsNotMet() {
184         // GIVEN that we are provisioning device owner
185         createTask(ACTION_PROVISION_MANAGED_DEVICE);
186 
187         // GIVEN that setting device owner is not currently allowed
188         when(mDevicePolicyManager.setDeviceOwner(ADMIN_COMPONENT_NAME,
189                 TEST_USER_ID)).thenThrow(new IllegalStateException());
190 
191         // WHEN running the task
192         mTask.run(TEST_USER_ID);
193 
194         // THEN an error should be returned
195         verify(mCallback).onError(mTask, 0, /* errorMessage= */ null);
196         verifyNoMoreInteractions(mCallback);
197     }
198 
199     @SmallTest
testSetDeviceOwner_ReturnFalse()200     public void testSetDeviceOwner_ReturnFalse() {
201         // GIVEN that we are provisioning device owner
202         createTask(ACTION_PROVISION_MANAGED_DEVICE);
203 
204         // GIVEN that setting device owner fails
205         when(mDevicePolicyManager.setDeviceOwner(
206                 ADMIN_COMPONENT_NAME, TEST_USER_ID)).thenReturn(false);
207 
208         // WHEN running the task
209         mTask.run(TEST_USER_ID);
210 
211         // THEN an error should be returned
212         verify(mCallback).onError(mTask, 0, /* errorMessage= */ null);
213         verifyNoMoreInteractions(mCallback);
214     }
215 
216     @SmallTest
testSetDeviceOwnerType_asDeviceOwner_toFinancedDevice()217     public void testSetDeviceOwnerType_asDeviceOwner_toFinancedDevice() {
218         // GIVEN that we are provisioning a financed device.
219         createTask(ACTION_PROVISION_FINANCED_DEVICE);
220 
221         // WHEN running the task.
222         mTask.run(TEST_USER_ID);
223 
224         // THEN the device owner type should have been set as financed when there is a device
225         // owner.
226         verify(mDevicePolicyManager).setDeviceOwner(ADMIN_COMPONENT_NAME, TEST_USER_ID);
227         verify(mDevicePolicyManager).setDeviceOwnerType(
228                 ADMIN_COMPONENT_NAME, DEVICE_OWNER_TYPE_FINANCED);
229         verify(mCallback).onSuccess(mTask);
230         verifyNoMoreInteractions(mCallback);
231     }
232 
233     @SmallTest
testSetDeviceOwnerType_asDeviceOwner_notCalledWhenProvisioningManagedDevice()234     public void testSetDeviceOwnerType_asDeviceOwner_notCalledWhenProvisioningManagedDevice() {
235         // GIVEN that we are provisioning a managed device.
236         createTask(ACTION_PROVISION_MANAGED_DEVICE);
237 
238         // WHEN running the task
239         mTask.run(TEST_USER_ID);
240 
241         // THEN setting the device owner type should not have been called.
242         verify(mDevicePolicyManager, never()).setDeviceOwnerType(any(), anyInt());
243         verify(mCallback).onSuccess(mTask);
244         verifyNoMoreInteractions(mCallback);
245     }
246 
createTask(String action)247     private void createTask(String action) {
248         ProvisioningParams params = new ProvisioningParams.Builder()
249                 .setDeviceAdminComponentName(ADMIN_COMPONENT_NAME)
250                 .setProvisioningAction(action)
251                 .build();
252         mTask = new SetDeviceOwnerPolicyTask(mUtils, mContext, params, mCallback,
253                 mock(ProvisioningAnalyticsTracker.class));
254     }
255 }
256