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.ArgumentMatchers.anyString;
22 import static org.mockito.Matchers.anyInt;
23 import static org.mockito.Matchers.eq;
24 import static org.mockito.Mockito.verify;
25 
26 import android.content.ComponentName;
27 import android.content.Context;
28 
29 import androidx.test.InstrumentationRegistry;
30 import androidx.test.filters.SmallTest;
31 
32 import com.android.managedprovisioning.R;
33 import com.android.managedprovisioning.common.Utils;
34 import com.android.managedprovisioning.model.PackageDownloadInfo;
35 import com.android.managedprovisioning.model.ProvisioningParams;
36 import com.android.managedprovisioning.model.WifiInfo;
37 import com.android.managedprovisioning.task.AbstractProvisioningTask;
38 import com.android.managedprovisioning.task.AddWifiNetworkTask;
39 import com.android.managedprovisioning.task.ConnectMobileNetworkTask;
40 import com.android.managedprovisioning.task.DownloadPackageTask;
41 import com.android.managedprovisioning.task.ProvisionFullyManagedDeviceTask;
42 import com.android.managedprovisioning.task.VerifyAdminPackageTask;
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     private static final String TEST_ERROR_MESSAGE = "test error message";
67     private static final Context mContext = InstrumentationRegistry.getTargetContext();
68 
69     @Mock
70     private ProvisioningControllerCallback mCallback;
71 
72     private static final AbstractProvisioningTask TASK =
73             new AddWifiNetworkTask(mContext,
74                     createProvisioningParamsBuilder().build(),
75                     createTaskCallback());
76 
77     @Mock
78     private Utils mUtils;
79 
80     @SmallTest
testRunAllTasks()81     public void testRunAllTasks() throws Exception {
82         // GIVEN device owner provisioning was invoked with a wifi and download info
83         createController(createProvisioningParamsBuilder().build());
84 
85         // WHEN starting the test run
86         mController.start(mHandler);
87 
88         // THEN the add wifi task should be run
89         taskSucceeded(AddWifiNetworkTask.class);
90 
91         // THEN the download package task should be run
92         taskSucceeded(DownloadPackageTask.class);
93 
94         // THEN the verify package task should be run
95         taskSucceeded(VerifyAdminPackageTask.class);
96 
97         // THEN the install package tasks should be run
98         tasksDownloadAndInstallDeviceOwnerPackageSucceeded(TEST_USER_ID);
99 
100         // THEN the provisioning task should be run
101         taskSucceeded(ProvisionFullyManagedDeviceTask.class);
102 
103         // THEN the provisioning complete callback should have happened
104         verify(mCallback).provisioningTasksCompleted();
105     }
106 
107     @SmallTest
testNoWifiInfo()108     public void testNoWifiInfo() throws Exception {
109         // GIVEN device owner provisioning was invoked with a wifi and download info
110         createController(createProvisioningParamsBuilder().setWifiInfo(null).build());
111 
112         // WHEN starting the test run
113         mController.start(mHandler);
114 
115         // THEN the download package task should be run
116         taskSucceeded(DownloadPackageTask.class);
117 
118         // THEN the verify package task should be run
119         taskSucceeded(VerifyAdminPackageTask.class);
120 
121         // THEN the install package tasks should be run
122         tasksDownloadAndInstallDeviceOwnerPackageSucceeded(TEST_USER_ID);
123 
124         // THEN the provisioning task should be run
125         taskSucceeded(ProvisionFullyManagedDeviceTask.class);
126 
127         // THEN the provisioning complete callback should have happened
128         verify(mCallback).provisioningTasksCompleted();
129     }
130 
131     @SmallTest
testNoDownloadInfo()132     public void testNoDownloadInfo() throws Exception {
133         // GIVEN device owner provisioning was invoked with a wifi and download info
134         createController(
135                 createProvisioningParamsBuilder().setDeviceAdminDownloadInfo(null).build());
136 
137         // WHEN starting the test run
138         mController.start(mHandler);
139 
140         // THEN the add wifi task should be run
141         taskSucceeded(AddWifiNetworkTask.class);
142 
143         // THEN the provisioning task should be run
144         taskSucceeded(ProvisionFullyManagedDeviceTask.class);
145 
146         // THEN the provisioning complete callback should have happened
147         verify(mCallback).provisioningTasksCompleted();
148     }
149 
150     @SmallTest
testErrorAddWifiTask()151     public void testErrorAddWifiTask() throws Exception {
152         // GIVEN device owner provisioning was invoked with a wifi and download info
153         createController(createProvisioningParamsBuilder().build());
154 
155         // WHEN starting the test run
156         mController.start(mHandler);
157 
158         // THEN the add wifi task should be run
159         AbstractProvisioningTask task = verifyTaskRun(AddWifiNetworkTask.class);
160 
161         // WHEN the task causes an error
162         mController.onError(task, 0, /* errorMessage= */ null);
163 
164         // THEN the onError callback should have been called without factory reset being required
165         verify(mCallback).error(eq(R.string.cant_set_up_device), anyString(), eq(false));
166     }
167 
168     @SmallTest
testErrorDownloadAppTask()169     public void testErrorDownloadAppTask() throws Exception {
170         // GIVEN device owner provisioning was invoked with a wifi and download info
171         createController(createProvisioningParamsBuilder().build());
172 
173         // WHEN starting the test run
174         mController.start(mHandler);
175 
176         // THEN the add wifi task should be run
177         taskSucceeded(AddWifiNetworkTask.class);
178 
179         // THEN the download package task should be run
180         AbstractProvisioningTask task = verifyTaskRun(DownloadPackageTask.class);
181 
182         // WHEN the task causes an error
183         mController.onError(task, 0, /* errorMessage= */ null);
184 
185         // THEN the onError callback should have been called with factory reset being required
186         verify(mCallback).error(anyInt(), anyString(), eq(true));
187     }
188 
189     @SmallTest
testErrorWithStringMessage()190     public void testErrorWithStringMessage() {
191         createController(createProvisioningParamsBuilder().build());
192         mController.start(mHandler);
193 
194         mController.onError(TASK, /* errorCode= */ 0, TEST_ERROR_MESSAGE);
195 
196         verify(mCallback).error(anyInt(), eq(TEST_ERROR_MESSAGE), eq(false));
197     }
198 
199     @SmallTest
testStart_useMobileDataTrueAndNoWifiInfo_runsConnectMobileNetworkTask()200     public void testStart_useMobileDataTrueAndNoWifiInfo_runsConnectMobileNetworkTask()
201             throws Exception {
202         createController(
203                 createProvisioningParamsBuilder().setWifiInfo(null).setUseMobileData(true).build());
204         mController.start(mHandler);
205         taskSucceeded(ConnectMobileNetworkTask.class);
206     }
207 
208     @SmallTest
testStart_useMobileDataTrueAndWifiInfo_runsAddWifiNetworkTask()209     public void testStart_useMobileDataTrueAndWifiInfo_runsAddWifiNetworkTask()
210             throws Exception {
211         createController(
212                 createProvisioningParamsBuilder()
213                         .setWifiInfo(TEST_WIFI_INFO)
214                         .setUseMobileData(true)
215                         .build());
216         mController.start(mHandler);
217         taskSucceeded(AddWifiNetworkTask.class);
218     }
219 
createController(ProvisioningParams params)220     private void createController(ProvisioningParams params) {
221         mController = DeviceOwnerProvisioningController.createInstance(
222                 getContext(),
223                 params,
224                 TEST_USER_ID,
225                 mCallback,
226                 mUtils);
227     }
228 
createProvisioningParamsBuilder()229     private static ProvisioningParams.Builder createProvisioningParamsBuilder() {
230         return new ProvisioningParams.Builder()
231                 .setDeviceAdminComponentName(TEST_ADMIN)
232                 .setProvisioningAction(ACTION_PROVISION_MANAGED_DEVICE)
233                 .setWifiInfo(TEST_WIFI_INFO)
234                 .setDeviceAdminDownloadInfo(TEST_DOWNLOAD_INFO);
235     }
236 
createTaskCallback()237     private static AbstractProvisioningTask.Callback createTaskCallback() {
238         return new AbstractProvisioningTask.Callback() {
239             @Override
240             public void onSuccess(AbstractProvisioningTask task) {
241 
242             }
243 
244             @Override
245             public void onError(AbstractProvisioningTask task, int errorCode,
246                     String errorMessage) {
247 
248             }
249         };
250     }
251 }
252