1 /*
2  * Copyright 2016, 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.provisioning;
18 
19 import static android.app.admin.DevicePolicyManager.ACTION_PROVISION_MANAGED_DEVICE;
20 
21 import static org.mockito.Matchers.anyInt;
22 import static org.mockito.Matchers.eq;
23 import static org.mockito.Mockito.verify;
24 
25 import android.content.ComponentName;
26 
27 import androidx.test.filters.SmallTest;
28 
29 import com.android.managedprovisioning.R;
30 import com.android.managedprovisioning.model.PackageDownloadInfo;
31 import com.android.managedprovisioning.model.ProvisioningParams;
32 import com.android.managedprovisioning.model.WifiInfo;
33 import com.android.managedprovisioning.task.AbstractProvisioningTask;
34 import com.android.managedprovisioning.task.AddWifiNetworkTask;
35 import com.android.managedprovisioning.task.ConnectMobileNetworkTask;
36 import com.android.managedprovisioning.task.DeleteNonRequiredAppsTask;
37 import com.android.managedprovisioning.task.DeviceOwnerInitializeProvisioningTask;
38 import com.android.managedprovisioning.task.DisallowAddUserTask;
39 import com.android.managedprovisioning.task.DownloadPackageTask;
40 import com.android.managedprovisioning.task.InstallPackageTask;
41 import com.android.managedprovisioning.task.SetDevicePolicyTask;
42 import com.android.managedprovisioning.task.VerifyPackageTask;
43 
44 import org.mockito.Mock;
45 
46 /**
47  * Unit tests for {@link DeviceOwnerProvisioningController}.
48  */
49 public class DeviceOwnerProvisioningControllerTest extends ProvisioningControllerBaseTest {
50 
51     private static final int TEST_USER_ID = 123;
52     private static final ComponentName TEST_ADMIN = new ComponentName("com.test.admin",
53             "com.test.admin.AdminReceiver");
54 
55     private static final String TEST_SSID = "SomeSsid";
56     private static final WifiInfo TEST_WIFI_INFO = new WifiInfo.Builder()
57             .setSsid(TEST_SSID)
58             .build();
59 
60     private static final String TEST_DOWNLOAD_LOCATION = "http://www.some.uri.com";
61     private static final byte[] TEST_PACKAGE_CHECKSUM = new byte[] { '1', '2', '3', '4', '5' };
62     private static final PackageDownloadInfo TEST_DOWNLOAD_INFO = new PackageDownloadInfo.Builder()
63             .setLocation(TEST_DOWNLOAD_LOCATION)
64             .setSignatureChecksum(TEST_PACKAGE_CHECKSUM)
65             .build();
66 
67     @Mock private ProvisioningControllerCallback mCallback;
68 
69     @SmallTest
testRunAllTasks()70     public void testRunAllTasks() throws Exception {
71         // GIVEN device owner provisioning was invoked with a wifi and download info
72         createController(createProvisioningParamsBuilder().build());
73 
74         // WHEN starting the test run
75         mController.start(mHandler);
76 
77         // THEN the initialization task is run first
78         taskSucceeded(DeviceOwnerInitializeProvisioningTask.class);
79 
80         // THEN the add wifi task should be run
81         taskSucceeded(AddWifiNetworkTask.class);
82 
83         // THEN the download package task should be run
84         taskSucceeded(DownloadPackageTask.class);
85 
86         // THEN the verify package task should be run
87         taskSucceeded(VerifyPackageTask.class);
88 
89         // THEN the install package task should be run
90         taskSucceeded(InstallPackageTask.class);
91 
92         // THEN the delete non-required apps task should be run
93         taskSucceeded(DeleteNonRequiredAppsTask.class);
94 
95         // THEN the set device policy task should be run
96         taskSucceeded(SetDevicePolicyTask.class);
97 
98         // THEN the disallow add user task should be run
99         taskSucceeded(DisallowAddUserTask.class);
100 
101         // THEN the provisioning complete callback should have happened
102         verify(mCallback).provisioningTasksCompleted();
103     }
104 
105     @SmallTest
testNoWifiInfo()106     public void testNoWifiInfo() throws Exception {
107         // GIVEN device owner provisioning was invoked with a wifi and download info
108         createController(createProvisioningParamsBuilder().setWifiInfo(null).build());
109 
110         // WHEN starting the test run
111         mController.start(mHandler);
112 
113         // THEN the initialization task is run first
114         taskSucceeded(DeviceOwnerInitializeProvisioningTask.class);
115 
116         // THEN the download package task should be run
117         taskSucceeded(DownloadPackageTask.class);
118 
119         // THEN the verify package task should be run
120         taskSucceeded(VerifyPackageTask.class);
121 
122         // THEN the install package task should be run
123         taskSucceeded(InstallPackageTask.class);
124 
125         // THEN the delete non-required apps task should be run
126         taskSucceeded(DeleteNonRequiredAppsTask.class);
127 
128         // THEN the set device policy task should be run
129         taskSucceeded(SetDevicePolicyTask.class);
130 
131         // THEN the disallow add user task should be run
132         taskSucceeded(DisallowAddUserTask.class);
133 
134         // THEN the provisioning complete callback should have happened
135         verify(mCallback).provisioningTasksCompleted();
136     }
137 
138     @SmallTest
testNoDownloadInfo()139     public void testNoDownloadInfo() throws Exception {
140         // GIVEN device owner provisioning was invoked with a wifi and download info
141         createController(
142                 createProvisioningParamsBuilder().setDeviceAdminDownloadInfo(null).build());
143 
144         // WHEN starting the test run
145         mController.start(mHandler);
146 
147         // THEN the initialization task is run first
148         taskSucceeded(DeviceOwnerInitializeProvisioningTask.class);
149 
150         // THEN the add wifi task should be run
151         taskSucceeded(AddWifiNetworkTask.class);
152 
153         // THEN the delete non-required apps task should be run
154         taskSucceeded(DeleteNonRequiredAppsTask.class);
155 
156         // THEN the set device policy task should be run
157         taskSucceeded(SetDevicePolicyTask.class);
158 
159         // THEN the disallow add user task should be run
160         taskSucceeded(DisallowAddUserTask.class);
161 
162         // THEN the provisioning complete callback should have happened
163         verify(mCallback).provisioningTasksCompleted();
164     }
165 
166     @SmallTest
testErrorAddWifiTask()167     public void testErrorAddWifiTask() throws Exception {
168         // GIVEN device owner provisioning was invoked with a wifi and download info
169         createController(createProvisioningParamsBuilder().build());
170 
171         // WHEN starting the test run
172         mController.start(mHandler);
173 
174         // THEN the initialization task is run first
175         taskSucceeded(DeviceOwnerInitializeProvisioningTask.class);
176 
177         // THEN the add wifi task should be run
178         AbstractProvisioningTask task = verifyTaskRun(AddWifiNetworkTask.class);
179 
180         // WHEN the task causes an error
181         mController.onError(task, 0);
182 
183         // THEN the onError callback should have been called without factory reset being required
184         verify(mCallback).error(eq(R.string.cant_set_up_device), anyInt(), eq(false));
185     }
186 
187     @SmallTest
testErrorDownloadAppTask()188     public void testErrorDownloadAppTask() throws Exception {
189         // GIVEN device owner provisioning was invoked with a wifi and download info
190         createController(createProvisioningParamsBuilder().build());
191 
192         // WHEN starting the test run
193         mController.start(mHandler);
194 
195         // THEN the initialization task is run first
196         taskSucceeded(DeviceOwnerInitializeProvisioningTask.class);
197 
198         // THEN the add wifi task should be run
199         taskSucceeded(AddWifiNetworkTask.class);
200 
201         // THEN the download package task should be run
202         AbstractProvisioningTask task = verifyTaskRun(DownloadPackageTask.class);
203 
204         // WHEN the task causes an error
205         mController.onError(task, 0);
206 
207         // THEN the onError callback should have been called with factory reset being required
208         verify(mCallback).error(anyInt(), anyInt(), eq(true));
209     }
210 
211     @SmallTest
testStart_useMobileDataTrueAndNoWifiInfo_runsConnectMobileNetworkTask()212     public void testStart_useMobileDataTrueAndNoWifiInfo_runsConnectMobileNetworkTask()
213             throws Exception {
214         createController(
215                 createProvisioningParamsBuilder().setWifiInfo(null).setUseMobileData(true).build());
216         mController.start(mHandler);
217         taskSucceeded(DeviceOwnerInitializeProvisioningTask.class);
218         taskSucceeded(ConnectMobileNetworkTask.class);
219     }
220 
221     @SmallTest
testStart_useMobileDataTrueAndWifiInfo_runsAddWifiNetworkTask()222     public void testStart_useMobileDataTrueAndWifiInfo_runsAddWifiNetworkTask()
223             throws Exception {
224         createController(
225                 createProvisioningParamsBuilder()
226                         .setWifiInfo(TEST_WIFI_INFO)
227                         .setUseMobileData(true)
228                         .build());
229         mController.start(mHandler);
230         taskSucceeded(DeviceOwnerInitializeProvisioningTask.class);
231         taskSucceeded(AddWifiNetworkTask.class);
232     }
233 
createController(ProvisioningParams params)234     private void createController(ProvisioningParams params) {
235         mController = new DeviceOwnerProvisioningController(
236                 getContext(),
237                 params,
238                 TEST_USER_ID,
239                 mCallback);
240     }
241 
createProvisioningParamsBuilder()242     private ProvisioningParams.Builder createProvisioningParamsBuilder() {
243         return new ProvisioningParams.Builder()
244                 .setDeviceAdminComponentName(TEST_ADMIN)
245                 .setProvisioningAction(ACTION_PROVISION_MANAGED_DEVICE)
246                 .setWifiInfo(TEST_WIFI_INFO)
247                 .setDeviceAdminDownloadInfo(TEST_DOWNLOAD_INFO);
248     }
249 }
250