• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 package com.android.managedprovisioning.parser;
17 
18 import static android.app.admin.DevicePolicyManager.ACTION_PROVISION_FINANCED_DEVICE;
19 import static android.app.admin.DevicePolicyManager.ACTION_PROVISION_MANAGED_DEVICE;
20 import static android.app.admin.DevicePolicyManager.ACTION_PROVISION_MANAGED_DEVICE_FROM_TRUSTED_SOURCE;
21 import static android.app.admin.DevicePolicyManager.ACTION_PROVISION_MANAGED_PROFILE;
22 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_ACCOUNT_TO_MIGRATE;
23 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_ADMIN_EXTRAS_BUNDLE;
24 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_DEVICE_ADMIN_COMPONENT_NAME;
25 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_DEVICE_ADMIN_MINIMUM_VERSION_CODE;
26 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_DEVICE_ADMIN_PACKAGE_CHECKSUM;
27 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_DEVICE_ADMIN_PACKAGE_DOWNLOAD_COOKIE_HEADER;
28 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_DEVICE_ADMIN_PACKAGE_DOWNLOAD_LOCATION;
29 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_DEVICE_ADMIN_PACKAGE_NAME;
30 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_DEVICE_ADMIN_SIGNATURE_CHECKSUM;
31 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_DISCLAIMERS;
32 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_DISCLAIMER_CONTENT;
33 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_DISCLAIMER_HEADER;
34 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_KEEP_ACCOUNT_ON_MIGRATION;
35 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_LEAVE_ALL_SYSTEM_APPS_ENABLED;
36 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_LOCALE;
37 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_LOCAL_TIME;
38 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_LOGO_URI;
39 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_ORGANIZATION_NAME;
40 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_RETURN_BEFORE_POLICY_COMPLIANCE;
41 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_SENSORS_PERMISSION_GRANT_OPT_OUT;
42 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_SKIP_EDUCATION_SCREENS;
43 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_SKIP_ENCRYPTION;
44 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_SKIP_OWNERSHIP_DISCLAIMER;
45 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_SUPPORTED_MODES;
46 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_SUPPORT_URL;
47 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_TIME_ZONE;
48 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_TRIGGER;
49 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_USE_MOBILE_DATA;
50 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_WIFI_ANONYMOUS_IDENTITY;
51 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_WIFI_CA_CERTIFICATE;
52 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_WIFI_DOMAIN;
53 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_WIFI_EAP_METHOD;
54 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_WIFI_HIDDEN;
55 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_WIFI_IDENTITY;
56 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_WIFI_PAC_URL;
57 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_WIFI_PASSWORD;
58 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_WIFI_PHASE2_AUTH;
59 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_WIFI_PROXY_BYPASS;
60 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_WIFI_PROXY_HOST;
61 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_WIFI_PROXY_PORT;
62 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_WIFI_SECURITY_TYPE;
63 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_WIFI_SSID;
64 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_WIFI_USER_CERTIFICATE;
65 import static android.app.admin.DevicePolicyManager.FLAG_SUPPORTED_MODES_DEVICE_OWNER;
66 import static android.app.admin.DevicePolicyManager.FLAG_SUPPORTED_MODES_ORGANIZATION_OWNED;
67 import static android.app.admin.DevicePolicyManager.FLAG_SUPPORTED_MODES_PERSONALLY_OWNED;
68 import static android.app.admin.DevicePolicyManager.PROVISIONING_MODE_FULLY_MANAGED_DEVICE;
69 import static android.app.admin.DevicePolicyManager.PROVISIONING_MODE_MANAGED_PROFILE;
70 import static android.app.admin.DevicePolicyManager.PROVISIONING_TRIGGER_MANAGED_ACCOUNT;
71 import static android.app.admin.DevicePolicyManager.PROVISIONING_TRIGGER_QR_CODE;
72 import static android.content.pm.PackageManager.FEATURE_MANAGED_USERS;
73 import static android.nfc.NfcAdapter.ACTION_NDEF_DISCOVERED;
74 
75 import static com.android.managedprovisioning.TestUtils.createTestAdminExtras;
76 import static com.android.managedprovisioning.model.ProvisioningParams.DEFAULT_EXTRA_PROVISIONING_SUPPORTED_MODES;
77 import static com.android.managedprovisioning.parser.ExtrasProvisioningDataParser.EXTRA_PROVISIONING_ACCOUNT_TO_MIGRATE_SHORT;
78 import static com.android.managedprovisioning.parser.ExtrasProvisioningDataParser.EXTRA_PROVISIONING_ADMIN_EXTRAS_BUNDLE_SHORT;
79 import static com.android.managedprovisioning.parser.ExtrasProvisioningDataParser.EXTRA_PROVISIONING_DEVICE_ADMIN_COMPONENT_NAME_SHORT;
80 import static com.android.managedprovisioning.parser.ExtrasProvisioningDataParser.EXTRA_PROVISIONING_DEVICE_ADMIN_MINIMUM_VERSION_CODE_SHORT;
81 import static com.android.managedprovisioning.parser.ExtrasProvisioningDataParser.EXTRA_PROVISIONING_DEVICE_ADMIN_PACKAGE_CHECKSUM_SHORT;
82 import static com.android.managedprovisioning.parser.ExtrasProvisioningDataParser.EXTRA_PROVISIONING_DEVICE_ADMIN_PACKAGE_DOWNLOAD_COOKIE_HEADER_SHORT;
83 import static com.android.managedprovisioning.parser.ExtrasProvisioningDataParser.EXTRA_PROVISIONING_DEVICE_ADMIN_PACKAGE_DOWNLOAD_LOCATION_SHORT;
84 import static com.android.managedprovisioning.parser.ExtrasProvisioningDataParser.EXTRA_PROVISIONING_DEVICE_ADMIN_PACKAGE_NAME_SHORT;
85 import static com.android.managedprovisioning.parser.ExtrasProvisioningDataParser.EXTRA_PROVISIONING_DEVICE_ADMIN_SIGNATURE_CHECKSUM_SHORT;
86 import static com.android.managedprovisioning.parser.ExtrasProvisioningDataParser.EXTRA_PROVISIONING_DISCLAIMERS_SHORT;
87 import static com.android.managedprovisioning.parser.ExtrasProvisioningDataParser.EXTRA_PROVISIONING_DISCLAIMER_CONTENT_SHORT;
88 import static com.android.managedprovisioning.parser.ExtrasProvisioningDataParser.EXTRA_PROVISIONING_DISCLAIMER_HEADER_SHORT;
89 import static com.android.managedprovisioning.parser.ExtrasProvisioningDataParser.EXTRA_PROVISIONING_KEEP_ACCOUNT_ON_MIGRATION_SHORT;
90 import static com.android.managedprovisioning.parser.ExtrasProvisioningDataParser.EXTRA_PROVISIONING_LEAVE_ALL_SYSTEM_APPS_ENABLED_SHORT;
91 import static com.android.managedprovisioning.parser.ExtrasProvisioningDataParser.EXTRA_PROVISIONING_LOCALE_SHORT;
92 import static com.android.managedprovisioning.parser.ExtrasProvisioningDataParser.EXTRA_PROVISIONING_LOCAL_TIME_SHORT;
93 import static com.android.managedprovisioning.parser.ExtrasProvisioningDataParser.EXTRA_PROVISIONING_LOGO_URI_SHORT;
94 import static com.android.managedprovisioning.parser.ExtrasProvisioningDataParser.EXTRA_PROVISIONING_ORGANIZATION_NAME_SHORT;
95 import static com.android.managedprovisioning.parser.ExtrasProvisioningDataParser.EXTRA_PROVISIONING_SENSORS_PERMISSION_GRANT_OPT_OUT_SHORT;
96 import static com.android.managedprovisioning.parser.ExtrasProvisioningDataParser.EXTRA_PROVISIONING_SKIP_ENCRYPTION_SHORT;
97 import static com.android.managedprovisioning.parser.ExtrasProvisioningDataParser.EXTRA_PROVISIONING_SUPPORT_URL_SHORT;
98 import static com.android.managedprovisioning.parser.ExtrasProvisioningDataParser.EXTRA_PROVISIONING_TIME_ZONE_SHORT;
99 import static com.android.managedprovisioning.parser.ExtrasProvisioningDataParser.EXTRA_PROVISIONING_USE_MOBILE_DATA_SHORT;
100 import static com.android.managedprovisioning.parser.ExtrasProvisioningDataParser.EXTRA_PROVISIONING_WIFI_ANONYMOUS_IDENTITY_SHORT;
101 import static com.android.managedprovisioning.parser.ExtrasProvisioningDataParser.EXTRA_PROVISIONING_WIFI_CA_CERTIFICATE_SHORT;
102 import static com.android.managedprovisioning.parser.ExtrasProvisioningDataParser.EXTRA_PROVISIONING_WIFI_DOMAIN_SHORT;
103 import static com.android.managedprovisioning.parser.ExtrasProvisioningDataParser.EXTRA_PROVISIONING_WIFI_EAP_METHOD_SHORT;
104 import static com.android.managedprovisioning.parser.ExtrasProvisioningDataParser.EXTRA_PROVISIONING_WIFI_HIDDEN_SHORT;
105 import static com.android.managedprovisioning.parser.ExtrasProvisioningDataParser.EXTRA_PROVISIONING_WIFI_IDENTITY_SHORT;
106 import static com.android.managedprovisioning.parser.ExtrasProvisioningDataParser.EXTRA_PROVISIONING_WIFI_PAC_URL_SHORT;
107 import static com.android.managedprovisioning.parser.ExtrasProvisioningDataParser.EXTRA_PROVISIONING_WIFI_PASSWORD_SHORT;
108 import static com.android.managedprovisioning.parser.ExtrasProvisioningDataParser.EXTRA_PROVISIONING_WIFI_PHASE2_AUTH_SHORT;
109 import static com.android.managedprovisioning.parser.ExtrasProvisioningDataParser.EXTRA_PROVISIONING_WIFI_PROXY_BYPASS_SHORT;
110 import static com.android.managedprovisioning.parser.ExtrasProvisioningDataParser.EXTRA_PROVISIONING_WIFI_PROXY_HOST_SHORT;
111 import static com.android.managedprovisioning.parser.ExtrasProvisioningDataParser.EXTRA_PROVISIONING_WIFI_PROXY_PORT_SHORT;
112 import static com.android.managedprovisioning.parser.ExtrasProvisioningDataParser.EXTRA_PROVISIONING_WIFI_SECURITY_TYPE_SHORT;
113 import static com.android.managedprovisioning.parser.ExtrasProvisioningDataParser.EXTRA_PROVISIONING_WIFI_SSID_SHORT;
114 import static com.android.managedprovisioning.parser.ExtrasProvisioningDataParser.EXTRA_PROVISIONING_WIFI_USER_CERTIFICATE_SHORT;
115 
116 import static com.google.common.truth.Truth.assertThat;
117 
118 import static org.mockito.ArgumentMatchers.eq;
119 import static org.mockito.Mockito.doReturn;
120 import static org.mockito.Mockito.spy;
121 import static org.mockito.Mockito.when;
122 import static org.testng.Assert.assertThrows;
123 
124 import android.accounts.Account;
125 import android.app.admin.DevicePolicyManager;
126 import android.content.ComponentName;
127 import android.content.Context;
128 import android.content.Intent;
129 import android.content.pm.PackageManager;
130 import android.net.Uri;
131 import android.os.Bundle;
132 import android.os.Parcelable;
133 import android.os.UserHandle;
134 import android.test.AndroidTestCase;
135 import android.test.suitebuilder.annotation.SmallTest;
136 import android.util.Base64;
137 
138 import com.android.managedprovisioning.common.Globals;
139 import com.android.managedprovisioning.common.IllegalProvisioningArgumentException;
140 import com.android.managedprovisioning.common.ManagedProvisioningSharedPreferences;
141 import com.android.managedprovisioning.common.SettingsFacade;
142 import com.android.managedprovisioning.common.StoreUtils;
143 import com.android.managedprovisioning.common.Utils;
144 import com.android.managedprovisioning.model.PackageDownloadInfo;
145 import com.android.managedprovisioning.model.ProvisioningParams;
146 import com.android.managedprovisioning.model.WifiInfo;
147 
148 import org.mockito.Mock;
149 import org.mockito.MockitoAnnotations;
150 
151 import java.lang.reflect.Field;
152 import java.util.ArrayList;
153 import java.util.Arrays;
154 import java.util.List;
155 import java.util.Locale;
156 import java.util.stream.Stream;
157 
158 /** Tests for {@link ExtrasProvisioningDataParser}. */
159 @SmallTest
160 public class ExtrasProvisioningDataParserTest extends AndroidTestCase {
161     private static final String TEST_PACKAGE_NAME = "com.afwsamples.testdpc";
162     private static final ComponentName TEST_COMPONENT_NAME =
163             ComponentName.unflattenFromString(
164                     "com.afwsamples.testdpc/com.afwsamples.testdpc.DeviceAdminReceiver");
165     private static final ComponentName TEST_COMPONENT_NAME_2 =
166             ComponentName.unflattenFromString(
167                     "com.afwsamples.testdpc2/com.afwsamples.testdpc.DeviceAdminReceiver");
168     private static final long TEST_LOCAL_TIME = 1456939524713L;
169     private static final Locale TEST_LOCALE = Locale.UK;
170     private static final String TEST_TIME_ZONE = "GMT";
171     private static final boolean TEST_LEAVE_ALL_SYSTEM_APP_ENABLED = true;
172     private static final boolean TEST_SKIP_ENCRYPTION = true;
173     private static final boolean TEST_KEEP_ACCOUNT_MIGRATED = true;
174     private static final long TEST_PROVISIONING_ID = 1000L;
175     private static final Account TEST_ACCOUNT_TO_MIGRATE =
176             new Account("user@gmail.com", "com.google");
177     private static final String TEST_ORGANIZATION_NAME = "TestOrganizationName";
178     private static final String TEST_SUPPORT_URL = "https://www.support.url/";
179     private static final String TEST_ILL_FORMED_LOCALE = "aaa_";
180 
181     // Wifi info
182     private static final String TEST_SSID = "TestWifi";
183     private static final boolean TEST_HIDDEN = true;
184     private static final String TEST_SECURITY_TYPE = "WPA2";
185     private static final String TEST_SECURITY_TYPE_EAP = "EAP";
186     private static final String TEST_PASSWORD = "GoogleRock";
187     private static final String TEST_PROXY_HOST = "testhost.com";
188     private static final int TEST_PROXY_PORT = 7689;
189     private static final String TEST_PROXY_BYPASS_HOSTS = "http://host1.com;https://host2.com";
190     private static final String TEST_PAC_URL = "pac.test.com";
191     private static final String TEST_EAP_METHOD = "TTLS";
192     private static final String TEST_PHASE2_AUTH = "PAP";
193     private static final String TEST_CA_CERT = "certificate";
194     private static final String TEST_USER_CERT = "certificate";
195     private static final String TEST_IDENTITY = "TestUser";
196     private static final String TEST_ANONYMOUS_IDENTITY = "TestAUser";
197     private static final String TEST_DOMAIN = "google.com";
198     private static final WifiInfo TEST_WIFI_INFO = WifiInfo.Builder.builder()
199             .setSsid(TEST_SSID)
200             .setHidden(TEST_HIDDEN)
201             .setSecurityType(TEST_SECURITY_TYPE)
202             .setPassword(TEST_PASSWORD)
203             .setProxyHost(TEST_PROXY_HOST)
204             .setProxyPort(TEST_PROXY_PORT)
205             .setProxyBypassHosts(TEST_PROXY_BYPASS_HOSTS)
206             .setPacUrl(TEST_PAC_URL)
207             .build();
208 
209     // Device admin package download info
210     private static final String TEST_DOWNLOAD_LOCATION =
211             "http://example/dpc.apk";
212     private static final String TEST_COOKIE_HEADER =
213             "Set-Cookie: sessionToken=foobar; Expires=Thu, 18 Feb 2016 23:59:59 GMT";
214     private static final byte[] TEST_PACKAGE_CHECKSUM = new byte[] { '1', '2', '3', '4', '5' };
215     private static final byte[] TEST_SIGNATURE_CHECKSUM = new byte[] { '5', '4', '3', '2', '1' };
216     private static final int TEST_MIN_SUPPORT_VERSION = 17689;
217     private static final PackageDownloadInfo TEST_DOWNLOAD_INFO =
218             PackageDownloadInfo.Builder.builder()
219                     .setLocation(TEST_DOWNLOAD_LOCATION)
220                     .setCookieHeader(TEST_COOKIE_HEADER)
221                     .setPackageChecksum(TEST_PACKAGE_CHECKSUM)
222                     .setSignatureChecksum(TEST_SIGNATURE_CHECKSUM)
223                     .setMinVersion(TEST_MIN_SUPPORT_VERSION)
224                     .build();
225     private static final boolean TEST_USE_MOBILE_DATA = true;
226     private static final Uri TEST_URI = Uri.parse("https://www.google.com/");
227     private static final String TEST_DISCLAMER_HEADER = "Google";
228     private static final int INVALID_SUPPORTED_MODES = 123;
229 
230     @Mock
231     private Context mContext;
232 
233     @Mock
234     private DevicePolicyManager mDpm;
235 
236     @Mock
237     private ManagedProvisioningSharedPreferences mSharedPreferences;
238 
239     @Mock
240     private SettingsFacade mSettingsFacade;
241 
242     @Mock
243     private PackageManager mPackageManager;
244 
245     private ExtrasProvisioningDataParser mExtrasProvisioningDataParser;
246 
247     private Utils mUtils;
248 
249     @Override
setUp()250     public void setUp() {
251         // this is necessary for mockito to work
252         System.setProperty("dexmaker.dexcache", getContext().getCacheDir().toString());
253 
254         MockitoAnnotations.initMocks(this);
255         when(mContext.getSystemServiceName(DevicePolicyManager.class))
256                 .thenReturn(Context.DEVICE_POLICY_SERVICE);
257         when(mContext.getSystemService(Context.DEVICE_POLICY_SERVICE)).thenReturn(mDpm);
258         when(mContext.getContentResolver()).thenReturn(getContext().getContentResolver());
259         when(mContext.getFilesDir()).thenReturn(getContext().getFilesDir());
260         when(mSharedPreferences.incrementAndGetProvisioningId()).thenReturn(TEST_PROVISIONING_ID);
261         when(mPackageManager.hasSystemFeature(eq(FEATURE_MANAGED_USERS))).thenReturn(true);
262         when(mContext.getPackageManager()).thenReturn(mPackageManager);
263         mUtils = spy(new Utils());
264         mExtrasProvisioningDataParser = new ExtrasProvisioningDataParser(mContext, mUtils,
265                 new ParserUtils(), mSettingsFacade, mSharedPreferences);
266     }
267 
testParse_trustedSourceProvisioningIntent()268     public void testParse_trustedSourceProvisioningIntent() throws Exception {
269         // GIVEN parsing happens during the setup wizard
270         when(mSettingsFacade.isDuringSetupWizard(mContext)).thenReturn(true);
271         // GIVEN a ACTION_PROVISION_MANAGED_DEVICE_FROM_TRUSTED_SOURCE intent and other extras.
272         Intent intent = new Intent(ACTION_PROVISION_MANAGED_DEVICE_FROM_TRUSTED_SOURCE)
273                 // GIVEN a device admin package name and component name
274                 .putExtra(EXTRA_PROVISIONING_DEVICE_ADMIN_PACKAGE_NAME, TEST_PACKAGE_NAME)
275                 .putExtra(EXTRA_PROVISIONING_DEVICE_ADMIN_COMPONENT_NAME, TEST_COMPONENT_NAME)
276                 .putExtras(getTestTimeTimeZoneAndLocaleExtras())
277                 .putExtras(getTestWifiInfoExtras())
278                 .putExtras(getTestDeviceAdminDownloadExtras())
279                 .putExtra(EXTRA_PROVISIONING_ADMIN_EXTRAS_BUNDLE, createTestAdminExtras())
280                 .putExtra(EXTRA_PROVISIONING_SKIP_ENCRYPTION, TEST_SKIP_ENCRYPTION)
281                 .putExtra(EXTRA_PROVISIONING_KEEP_ACCOUNT_ON_MIGRATION, TEST_KEEP_ACCOUNT_MIGRATED)
282                 .putExtra(EXTRA_PROVISIONING_LEAVE_ALL_SYSTEM_APPS_ENABLED,
283                         TEST_LEAVE_ALL_SYSTEM_APP_ENABLED)
284                 .putExtra(EXTRA_PROVISIONING_ACCOUNT_TO_MIGRATE, TEST_ACCOUNT_TO_MIGRATE)
285                 .putExtra(EXTRA_PROVISIONING_ORGANIZATION_NAME, TEST_ORGANIZATION_NAME)
286                 .putExtra(EXTRA_PROVISIONING_SUPPORT_URL, TEST_SUPPORT_URL);
287 
288         // WHEN the intent is parsed by the parser.
289         ProvisioningParams params = mExtrasProvisioningDataParser.parse(intent);
290 
291         // THEN ProvisionParams is constructed as expected.
292         assertThat(
293                 ProvisioningParams.Builder.builder()
294                         // THEN ACTION_PROVISION_MANAGED_DEVICE_FROM_TRUSTED_SOURCE is translated to
295                         // ACTION_PROVISION_MANAGED_DEVICE
296                         .setProvisioningAction(ACTION_PROVISION_MANAGED_DEVICE)
297                         .setDeviceAdminComponentName(TEST_COMPONENT_NAME)
298                         .setDeviceAdminDownloadInfo(TEST_DOWNLOAD_INFO)
299                         .setProvisioningId(TEST_PROVISIONING_ID)
300                         .setLocalTime(TEST_LOCAL_TIME)
301                         .setLocale(TEST_LOCALE)
302                         .setTimeZone(TEST_TIME_ZONE)
303                         // THEN the trusted source is set to true.
304                         .setStartedByTrustedSource(true)
305                         .setSkipEncryption(TEST_SKIP_ENCRYPTION)
306                         // THEN keep account migrated flag is ignored
307                         .setKeepAccountMigrated(false)
308                         .setLeaveAllSystemAppsEnabled(true)
309                         .setWifiInfo(TEST_WIFI_INFO)
310                         .setAdminExtrasBundle(createTestAdminExtras())
311                         .setAccountToMigrate(TEST_ACCOUNT_TO_MIGRATE)
312                         .setOrganizationName(TEST_ORGANIZATION_NAME)
313                         .setSupportUrl(TEST_SUPPORT_URL)
314                         .setInitiatorRequestedProvisioningModes(
315                                 FLAG_SUPPORTED_MODES_ORGANIZATION_OWNED)
316                         .setAllowedProvisioningModes(new ArrayList<>(List.of(
317                                 PROVISIONING_MODE_MANAGED_PROFILE,
318                                 PROVISIONING_MODE_FULLY_MANAGED_DEVICE
319                         )))
320                         .setReturnBeforePolicyCompliance(true)
321                         .build())
322                 .isEqualTo(params);
323     }
324 
testParse_financedDeviceProvisioningIntent()325     public void testParse_financedDeviceProvisioningIntent() throws Exception {
326         // GIVEN a ACTION_PROVISION_FINANCED_DEVICE intent and other extras.
327         Intent intent = new Intent(ACTION_PROVISION_FINANCED_DEVICE)
328                 // GIVEN a device admin package name and component name
329                 .putExtra(EXTRA_PROVISIONING_DEVICE_ADMIN_PACKAGE_NAME, TEST_PACKAGE_NAME)
330                 .putExtra(EXTRA_PROVISIONING_DEVICE_ADMIN_COMPONENT_NAME, TEST_COMPONENT_NAME)
331                 .putExtras(getTestDeviceAdminDownloadExtras())
332                 .putExtra(EXTRA_PROVISIONING_ADMIN_EXTRAS_BUNDLE, createTestAdminExtras())
333                 .putExtra(EXTRA_PROVISIONING_KEEP_ACCOUNT_ON_MIGRATION, TEST_KEEP_ACCOUNT_MIGRATED)
334                 .putExtra(EXTRA_PROVISIONING_LEAVE_ALL_SYSTEM_APPS_ENABLED,
335                     TEST_LEAVE_ALL_SYSTEM_APP_ENABLED)
336                 .putExtra(EXTRA_PROVISIONING_ORGANIZATION_NAME, TEST_ORGANIZATION_NAME)
337                 .putExtra(EXTRA_PROVISIONING_SUPPORT_URL, TEST_SUPPORT_URL);
338 
339         // WHEN the intent is parsed by the parser.
340         ProvisioningParams params = mExtrasProvisioningDataParser.parse(intent);
341 
342         // THEN ProvisionParams is constructed as expected.
343         assertThat(
344                 ProvisioningParams.Builder.builder()
345                         .setProvisioningAction(ACTION_PROVISION_FINANCED_DEVICE)
346                         .setDeviceAdminComponentName(TEST_COMPONENT_NAME)
347                         .setDeviceAdminDownloadInfo(TEST_DOWNLOAD_INFO)
348                         .setProvisioningId(TEST_PROVISIONING_ID)
349                         // THEN keep account migrated flag is ignored
350                         .setKeepAccountMigrated(false)
351                         // THEN leave all system apps is always true
352                         .setLeaveAllSystemAppsEnabled(true)
353                         .setAdminExtrasBundle(createTestAdminExtras())
354                         .setOrganizationName(TEST_ORGANIZATION_NAME)
355                         .setSupportUrl(TEST_SUPPORT_URL)
356                         .setReturnBeforePolicyCompliance(true)
357                         .build())
358                 .isEqualTo(params);
359     }
360 
testParse_resumeProvisioningIntent()361     public void testParse_resumeProvisioningIntent() throws Exception {
362         // GIVEN a ProvisioningParams stored in an intent
363         ProvisioningParams expected = ProvisioningParams.Builder.builder()
364                 .setProvisioningAction(ACTION_PROVISION_MANAGED_DEVICE)
365                 .setDeviceAdminComponentName(TEST_COMPONENT_NAME)
366                 .build();
367         Intent intent = new Intent(Globals.ACTION_RESUME_PROVISIONING)
368                 .putExtra(ProvisioningParams.EXTRA_PROVISIONING_PARAMS, expected);
369         // WHEN the intent is parsed by the parser
370         ProvisioningParams params = mExtrasProvisioningDataParser.parse(intent);
371         // THEN we get back the original ProvisioningParams.
372         assertThat(expected).isEqualTo(params);
373     }
374 
testParse_managedProfileIntent()375     public void testParse_managedProfileIntent() throws Exception {
376         // GIVEN a managed profile provisioning intent and other extras.
377         Intent intent = new Intent(ACTION_PROVISION_MANAGED_PROFILE)
378                 .putExtra(EXTRA_PROVISIONING_DEVICE_ADMIN_PACKAGE_NAME, TEST_PACKAGE_NAME)
379                 .putExtra(EXTRA_PROVISIONING_DEVICE_ADMIN_COMPONENT_NAME, TEST_COMPONENT_NAME)
380                 .putExtras(getTestTimeTimeZoneAndLocaleExtras())
381                 .putExtras(getTestWifiInfoExtras())
382                 .putExtras(getTestDeviceAdminDownloadExtras())
383                 .putExtra(EXTRA_PROVISIONING_ADMIN_EXTRAS_BUNDLE, createTestAdminExtras())
384                 .putExtra(EXTRA_PROVISIONING_SKIP_ENCRYPTION, TEST_SKIP_ENCRYPTION)
385                 .putExtra(EXTRA_PROVISIONING_KEEP_ACCOUNT_ON_MIGRATION, TEST_KEEP_ACCOUNT_MIGRATED)
386                 .putExtra(EXTRA_PROVISIONING_LEAVE_ALL_SYSTEM_APPS_ENABLED,
387                         TEST_LEAVE_ALL_SYSTEM_APP_ENABLED)
388                 .putExtra(EXTRA_PROVISIONING_ACCOUNT_TO_MIGRATE, TEST_ACCOUNT_TO_MIGRATE);
389 
390         // GIVEN the device admin is installed.
391         mockInstalledDeviceAdminForTestPackageName();
392 
393         // WHEN the intent is parsed by the parser.
394         ProvisioningParams params = mExtrasProvisioningDataParser.parse(intent);
395 
396         // THEN ProvisionParams is constructed as expected.
397         assertThat(
398                 ProvisioningParams.Builder.builder()
399                         // THEN provisioning action is ACTION_PROVISION_MANAGED_PROFILE
400                         .setProvisioningAction(ACTION_PROVISION_MANAGED_PROFILE)
401                         .setDeviceAdminComponentName(TEST_COMPONENT_NAME)
402                         .setProvisioningId(TEST_PROVISIONING_ID)
403                         // THEN device admin package name is not supported.
404                         .setDeviceAdminPackageName(null)
405                         // THEN device admin download info is not supported.
406                         .setDeviceAdminDownloadInfo(null)
407                         // THEN wifi info is not supported.
408                         .setWifiInfo(null)
409                         .setSkipEncryption(TEST_SKIP_ENCRYPTION)
410                         // THEN leave all system apps flag is ignored
411                         .setLeaveAllSystemAppsEnabled(false)
412                         .setKeepAccountMigrated(TEST_KEEP_ACCOUNT_MIGRATED)
413                         .setAdminExtrasBundle(createTestAdminExtras())
414                         .setAccountToMigrate(TEST_ACCOUNT_TO_MIGRATE)
415                         .build())
416                 .isEqualTo(params);
417     }
418 
testParse_managedProfileIntent_CompProvisioning()419     public void testParse_managedProfileIntent_CompProvisioning() throws Exception {
420         // GIVEN a managed profile provisioning intent and other extras.
421         Intent intent = buildTestManagedProfileIntent();
422 
423         // GIVEN the device admin is installed.
424         mockInstalledDeviceAdminForTestPackageName();
425 
426         // GIVEN the device admin is also device owner in primary user.
427         when(mDpm.getDeviceOwnerComponentOnCallingUser()).thenReturn(TEST_COMPONENT_NAME);
428 
429         // WHEN the intent is parsed by the parser.
430         ProvisioningParams params = mExtrasProvisioningDataParser.parse(intent);
431 
432         // THEN ProvisionParams is constructed as expected.
433         assertThat(
434                 ProvisioningParams.Builder.builder()
435                         // THEN provisioning action is ACTION_PROVISION_MANAGED_PROFILE
436                         .setProvisioningAction(ACTION_PROVISION_MANAGED_PROFILE)
437                         .setDeviceAdminComponentName(TEST_COMPONENT_NAME)
438                         .setProvisioningId(TEST_PROVISIONING_ID)
439                         // THEN device admin package name is not supported.
440                         .setDeviceAdminPackageName(null)
441                         // THEN device admin download info is not supported.
442                         .setDeviceAdminDownloadInfo(null)
443                         // THEN wifi info is not supported.
444                         .setWifiInfo(null)
445                         .setSkipEncryption(TEST_SKIP_ENCRYPTION)
446                         .setKeepAccountMigrated(TEST_KEEP_ACCOUNT_MIGRATED)
447                         .setAdminExtrasBundle(createTestAdminExtras())
448                         .setAccountToMigrate(TEST_ACCOUNT_TO_MIGRATE)
449                         .build())
450                 .isEqualTo(params);
451     }
452 
testParse_managedProfileIntent_DeviceOwnerWithByodProvisioning()453     public void testParse_managedProfileIntent_DeviceOwnerWithByodProvisioning() throws Exception {
454         // GIVEN a managed profile provisioning intent and other extras.
455         Intent intent = new Intent(ACTION_PROVISION_MANAGED_PROFILE)
456                 .putExtra(EXTRA_PROVISIONING_DEVICE_ADMIN_COMPONENT_NAME, TEST_COMPONENT_NAME)
457                 .putExtra(EXTRA_PROVISIONING_KEEP_ACCOUNT_ON_MIGRATION, TEST_KEEP_ACCOUNT_MIGRATED);
458 
459         // GIVEN the device admin is installed.
460         mockInstalledDeviceAdminForNullPackageName();
461 
462         // GIVEN a different device admin is a device owner in primary user.
463         when(mDpm.getDeviceOwnerComponentOnCallingUser()).thenReturn(TEST_COMPONENT_NAME_2);
464 
465         // WHEN the intent is parsed by the parser.
466         ProvisioningParams params = mExtrasProvisioningDataParser.parse(intent);
467 
468         // THEN ProvisionParams is constructed as expected.
469         assertThat(
470                 ProvisioningParams.Builder.builder()
471                         .setProvisioningAction(ACTION_PROVISION_MANAGED_PROFILE)
472                         .setDeviceAdminComponentName(TEST_COMPONENT_NAME)
473                         .setProvisioningId(TEST_PROVISIONING_ID)
474                         .setKeepAccountMigrated(TEST_KEEP_ACCOUNT_MIGRATED)
475                         .build())
476                 .isEqualTo(params);
477     }
478 
testParse_shortExtras_sameAsLongExtras()479     public void testParse_shortExtras_sameAsLongExtras() throws Exception {
480         assertThat(mExtrasProvisioningDataParser.parse(buildIntentWithAllLongExtras()))
481             .isEqualTo(mExtrasProvisioningDataParser.parse(buildIntentWithAllShortExtras()));
482     }
483 
testParse_managedDeviceIntent()484     public void testParse_managedDeviceIntent() throws Exception {
485         // GIVEN a managed device provisioning intent and other extras.
486         Intent intent = new Intent(ACTION_PROVISION_MANAGED_DEVICE)
487                 // GIVEN a device admin package name and component name
488                 .putExtra(EXTRA_PROVISIONING_DEVICE_ADMIN_PACKAGE_NAME, TEST_PACKAGE_NAME)
489                 .putExtra(EXTRA_PROVISIONING_DEVICE_ADMIN_COMPONENT_NAME, TEST_COMPONENT_NAME)
490                 .putExtras(getTestTimeTimeZoneAndLocaleExtras())
491                 .putExtras(getTestWifiInfoExtras())
492                 .putExtras(getTestDeviceAdminDownloadExtras())
493                 .putExtra(EXTRA_PROVISIONING_ADMIN_EXTRAS_BUNDLE, createTestAdminExtras())
494                 .putExtra(EXTRA_PROVISIONING_SKIP_ENCRYPTION, TEST_SKIP_ENCRYPTION)
495                 .putExtra(EXTRA_PROVISIONING_LEAVE_ALL_SYSTEM_APPS_ENABLED,
496                         TEST_LEAVE_ALL_SYSTEM_APP_ENABLED)
497                 .putExtra(EXTRA_PROVISIONING_ACCOUNT_TO_MIGRATE, TEST_ACCOUNT_TO_MIGRATE);
498 
499         // WHEN the intent is parsed by the parser.
500         ProvisioningParams params = mExtrasProvisioningDataParser.parse(intent);
501 
502         // THEN ProvisionParams is constructed as expected.
503         assertThat(
504                 ProvisioningParams.Builder.builder()
505                         // THEN provisioning action is ACTION_PROVISION_MANAGED_DEVICE
506                         .setProvisioningAction(ACTION_PROVISION_MANAGED_DEVICE)
507                         .setDeviceAdminComponentName(TEST_COMPONENT_NAME)
508                         .setProvisioningId(TEST_PROVISIONING_ID)
509                         // THEN device admin package name is not supported in Device Owner
510                         // provisioning.
511                         .setDeviceAdminPackageName(null)
512                         // THEN Device Admin download info is not supported.
513                         .setDeviceAdminDownloadInfo(null)
514                         .setSkipEncryption(TEST_SKIP_ENCRYPTION)
515                         .setLeaveAllSystemAppsEnabled(true)
516                         // THEN wifi configuration is not supported.
517                         .setWifiInfo(null)
518                         .setAdminExtrasBundle(createTestAdminExtras())
519                         .setAccountToMigrate(TEST_ACCOUNT_TO_MIGRATE)
520                         .setReturnBeforePolicyCompliance(true)
521                         .build())
522                 .isEqualTo(params);
523     }
524 
testParse_nfcProvisioningIntentThrowsException()525     public void testParse_nfcProvisioningIntentThrowsException() {
526         // GIVEN a NFC provisioning intent and other extras.
527         Intent intent = new Intent(ACTION_NDEF_DISCOVERED)
528                 // GIVEN a device admin package name and component name
529                 .putExtra(EXTRA_PROVISIONING_DEVICE_ADMIN_PACKAGE_NAME, TEST_PACKAGE_NAME)
530                 .putExtra(EXTRA_PROVISIONING_DEVICE_ADMIN_COMPONENT_NAME, TEST_COMPONENT_NAME)
531                 .putExtras(getTestTimeTimeZoneAndLocaleExtras())
532                 .putExtras(getTestWifiInfoExtras())
533                 .putExtras(getTestDeviceAdminDownloadExtras())
534                 .putExtra(EXTRA_PROVISIONING_ADMIN_EXTRAS_BUNDLE, createTestAdminExtras())
535                 .putExtra(EXTRA_PROVISIONING_SKIP_ENCRYPTION, TEST_SKIP_ENCRYPTION)
536                 .putExtra(EXTRA_PROVISIONING_ACCOUNT_TO_MIGRATE, TEST_ACCOUNT_TO_MIGRATE);
537 
538         try {
539             // WHEN the intent is parsed by the parser.
540             ProvisioningParams params = mExtrasProvisioningDataParser.parse(intent);
541             fail("ExtrasProvisioningDataParser doesn't support NFC intent. "
542                     + "IllegalProvisioningArgumentException should be thrown");
543         } catch (IllegalProvisioningArgumentException e) {
544             // THEN IllegalProvisioningArgumentException is thrown.
545         }
546     }
547 
testParse_illFormedLocaleThrowsException()548     public void testParse_illFormedLocaleThrowsException() throws Exception {
549         // GIVEN parsing happens during the setup wizard
550         when(mSettingsFacade.isDuringSetupWizard(mContext)).thenReturn(true);
551 
552         // GIVEN a managed device provisioning intent and other extras.
553         Intent intent = new Intent(ACTION_PROVISION_MANAGED_DEVICE_FROM_TRUSTED_SOURCE)
554                 // GIVEN a device admin package name and component name
555                 .putExtra(EXTRA_PROVISIONING_DEVICE_ADMIN_PACKAGE_NAME, TEST_PACKAGE_NAME)
556                 .putExtra(EXTRA_PROVISIONING_DEVICE_ADMIN_COMPONENT_NAME, TEST_COMPONENT_NAME)
557                 // GIVEN a ill formed locale string.
558                 .putExtras(getTestTimeTimeZoneAndLocaleExtras(TEST_ILL_FORMED_LOCALE))
559                 .putExtras(getTestWifiInfoExtras())
560                 .putExtras(getTestDeviceAdminDownloadExtras())
561                 .putExtra(EXTRA_PROVISIONING_ADMIN_EXTRAS_BUNDLE, createTestAdminExtras())
562                 .putExtra(EXTRA_PROVISIONING_SKIP_ENCRYPTION, TEST_SKIP_ENCRYPTION)
563                 .putExtra(EXTRA_PROVISIONING_ACCOUNT_TO_MIGRATE, TEST_ACCOUNT_TO_MIGRATE);
564 
565         try {
566             // WHEN the intent is parsed by the parser.
567             ProvisioningParams params = mExtrasProvisioningDataParser.parse(intent);
568             fail("ExtrasProvisioningDataParser parsing an ill formed locale string. "
569                     + "IllegalProvisioningArgumentException should be thrown");
570         } catch (IllegalProvisioningArgumentException e) {
571             // THEN IllegalProvisioningArgumentException is thrown.
572         }
573     }
574 
testSetUseMobileData_forManagedProfile_alwaysFalse()575     public void testSetUseMobileData_forManagedProfile_alwaysFalse() throws Exception {
576         Intent intent =
577                 buildTestManagedProfileIntent().putExtra(EXTRA_PROVISIONING_USE_MOBILE_DATA, true);
578         mockInstalledDeviceAdminForTestPackageName();
579 
580         assertThat(mExtrasProvisioningDataParser.parse(intent).useMobileData).isFalse();
581     }
582 
testSetUseMobileData_fromTrustedSource_toFalse()583     public void testSetUseMobileData_fromTrustedSource_toFalse() throws Exception {
584         Intent intent =
585                 buildTestTrustedSourceIntent().putExtra(EXTRA_PROVISIONING_USE_MOBILE_DATA, true);
586         mockInstalledDeviceAdminForTestPackageName();
587 
588         assertThat(mExtrasProvisioningDataParser.parse(intent).useMobileData).isTrue();
589     }
590 
testSetUseMobileData_fromTrustedSource_toTrue()591     public void testSetUseMobileData_fromTrustedSource_toTrue() throws Exception {
592         Intent intent =
593                 buildTestTrustedSourceIntent().putExtra(EXTRA_PROVISIONING_USE_MOBILE_DATA, true);
594         mockInstalledDeviceAdminForTestPackageName();
595 
596         assertThat(mExtrasProvisioningDataParser.parse(intent).useMobileData).isTrue();
597     }
598 
testSetUseMobileData_fromTrustedSource_defaultsToFalse()599     public void testSetUseMobileData_fromTrustedSource_defaultsToFalse() throws Exception {
600         Intent intent = buildTestTrustedSourceIntent();
601         intent.removeExtra(EXTRA_PROVISIONING_USE_MOBILE_DATA);
602         mockInstalledDeviceAdminForTestPackageName();
603 
604         assertThat(mExtrasProvisioningDataParser.parse(intent).useMobileData).isFalse();
605     }
606 
testParse_WifiInfoWithCertificates()607     public void testParse_WifiInfoWithCertificates() throws Exception {
608         when(mSettingsFacade.isDuringSetupWizard(mContext)).thenReturn(true);
609         Intent intent = new Intent(ACTION_PROVISION_MANAGED_DEVICE_FROM_TRUSTED_SOURCE)
610                 .putExtra(EXTRA_PROVISIONING_DEVICE_ADMIN_COMPONENT_NAME, TEST_COMPONENT_NAME)
611                 .putExtra(EXTRA_PROVISIONING_WIFI_SSID, TEST_SSID)
612                 .putExtra(EXTRA_PROVISIONING_WIFI_HIDDEN, TEST_HIDDEN)
613                 .putExtra(EXTRA_PROVISIONING_WIFI_SECURITY_TYPE, TEST_SECURITY_TYPE_EAP)
614                 .putExtra(EXTRA_PROVISIONING_WIFI_PASSWORD, TEST_PASSWORD)
615                 .putExtra(EXTRA_PROVISIONING_WIFI_EAP_METHOD, TEST_EAP_METHOD)
616                 .putExtra(EXTRA_PROVISIONING_WIFI_PHASE2_AUTH, TEST_PHASE2_AUTH)
617                 .putExtra(EXTRA_PROVISIONING_WIFI_CA_CERTIFICATE, TEST_CA_CERT)
618                 .putExtra(EXTRA_PROVISIONING_WIFI_USER_CERTIFICATE, TEST_USER_CERT)
619                 .putExtra(EXTRA_PROVISIONING_WIFI_IDENTITY, TEST_IDENTITY)
620                 .putExtra(EXTRA_PROVISIONING_WIFI_ANONYMOUS_IDENTITY, TEST_ANONYMOUS_IDENTITY)
621                 .putExtra(EXTRA_PROVISIONING_WIFI_DOMAIN, TEST_DOMAIN)
622                 .putExtra(EXTRA_PROVISIONING_WIFI_PROXY_HOST, TEST_PROXY_HOST)
623                 .putExtra(EXTRA_PROVISIONING_WIFI_PROXY_PORT, TEST_PROXY_PORT)
624                 .putExtra(EXTRA_PROVISIONING_WIFI_PROXY_BYPASS, TEST_PROXY_BYPASS_HOSTS)
625                 .putExtra(EXTRA_PROVISIONING_WIFI_PAC_URL, TEST_PAC_URL);
626 
627         ProvisioningParams params = mExtrasProvisioningDataParser.parse(intent);
628 
629         assertThat(params).isEqualTo(createTestProvisioningParamsBuilder()
630                 .setProvisioningAction(ACTION_PROVISION_MANAGED_DEVICE)
631                 .setStartedByTrustedSource(true)
632                 .setDeviceAdminComponentName(TEST_COMPONENT_NAME)
633                 .setInitiatorRequestedProvisioningModes(FLAG_SUPPORTED_MODES_ORGANIZATION_OWNED)
634                 .setAllowedProvisioningModes(new ArrayList<>(List.of(
635                         PROVISIONING_MODE_MANAGED_PROFILE,
636                         PROVISIONING_MODE_FULLY_MANAGED_DEVICE)))
637                 .setReturnBeforePolicyCompliance(true)
638                 .setWifiInfo(WifiInfo.Builder.builder()
639                         .setSsid(TEST_SSID)
640                         .setHidden(TEST_HIDDEN)
641                         .setSecurityType(TEST_SECURITY_TYPE_EAP)
642                         .setPassword(TEST_PASSWORD)
643                         .setEapMethod(TEST_EAP_METHOD)
644                         .setPhase2Auth(TEST_PHASE2_AUTH)
645                         .setCaCertificate(TEST_CA_CERT)
646                         .setUserCertificate(TEST_USER_CERT)
647                         .setIdentity(TEST_IDENTITY)
648                         .setAnonymousIdentity(TEST_ANONYMOUS_IDENTITY)
649                         .setDomain(TEST_DOMAIN)
650                         .setProxyHost(TEST_PROXY_HOST)
651                         .setProxyPort(TEST_PROXY_PORT)
652                         .setProxyBypassHosts(TEST_PROXY_BYPASS_HOSTS)
653                         .setPacUrl(TEST_PAC_URL)
654                         .build())
655                 .build());
656     }
657 
testParse_PermissionGrantOptOut()658     public void testParse_PermissionGrantOptOut() throws IllegalProvisioningArgumentException {
659         Intent provisionIntent = new Intent(ACTION_PROVISION_MANAGED_PROFILE)
660                 .putExtra(EXTRA_PROVISIONING_DEVICE_ADMIN_PACKAGE_NAME, TEST_PACKAGE_NAME)
661                 .putExtra(EXTRA_PROVISIONING_DEVICE_ADMIN_COMPONENT_NAME, TEST_COMPONENT_NAME)
662                 .putExtra(DevicePolicyManager.EXTRA_PROVISIONING_SENSORS_PERMISSION_GRANT_OPT_OUT,
663                         true);
664         mockInstalledDeviceAdminForTestPackageName();
665 
666         ProvisioningParams params = mExtrasProvisioningDataParser.parse(provisionIntent);
667         assertThat(params.deviceOwnerPermissionGrantOptOut).isEqualTo(true);
668     }
669 
testShortNamesOfExtrasAreUnique()670     public void testShortNamesOfExtrasAreUnique() {
671         assertEquals(buildAllShortExtras().distinct().count(), buildAllShortExtras().count());
672     }
673 
testParse_organizationOwnedIsFalse()674     public void testParse_organizationOwnedIsFalse() throws Exception {
675         Intent intent = buildTestIntent();
676         mockInstalledDeviceAdminForTestPackageName();
677 
678         assertThat(mExtrasProvisioningDataParser.parse(intent)
679                 .isOrganizationOwnedProvisioning).isFalse();
680     }
681 
682 
683     public void
testParse_managedAccountProvisioningWithSkipEduExtra_skipEdu()684             testParse_managedAccountProvisioningWithSkipEduExtra_skipEdu() throws Exception {
685         Intent intent = buildTestProvisionManagedAccountIntent()
686                 .putExtra(EXTRA_PROVISIONING_SKIP_EDUCATION_SCREENS, true);
687         mockInstalledDeviceAdminForTestPackageName();
688 
689         ProvisioningParams params = mExtrasProvisioningDataParser.parse(intent);
690 
691         assertThat(params.skipEducationScreens).isTrue();
692     }
693 
694     public void
testParse_managedAccountProvisioningWithoutSkipEduExtra_noSkipEdu()695             testParse_managedAccountProvisioningWithoutSkipEduExtra_noSkipEdu() throws Exception {
696         Intent intent = buildTestProvisionManagedAccountIntent();
697         mockInstalledDeviceAdminForTestPackageName();
698 
699         ProvisioningParams params = mExtrasProvisioningDataParser.parse(intent);
700 
701         assertThat(params.skipEducationScreens).isFalse();
702     }
703 
704     public void
testParse_qrProvisioningWithSkipEduExtra_noSkipEdu()705             testParse_qrProvisioningWithSkipEduExtra_noSkipEdu() throws Exception {
706         Intent intent = buildTestQrCodeIntent()
707                 .putExtra(EXTRA_PROVISIONING_SKIP_EDUCATION_SCREENS, true);
708         mockInstalledDeviceAdminForTestPackageName();
709 
710         ProvisioningParams params = mExtrasProvisioningDataParser.parse(intent);
711 
712         assertThat(params.skipEducationScreens).isFalse();
713     }
714 
715     public void
testParse_managedProfileProvisioningWithSkipEduExtra_noSkipEdu()716             testParse_managedProfileProvisioningWithSkipEduExtra_noSkipEdu() throws Exception {
717         Intent intent = buildTestManagedProfileIntent()
718                 .putExtra(EXTRA_PROVISIONING_SKIP_EDUCATION_SCREENS, true);
719         mockInstalledDeviceAdminForTestPackageName();
720 
721         ProvisioningParams params = mExtrasProvisioningDataParser.parse(intent);
722 
723         assertThat(params.skipEducationScreens).isFalse();
724     }
725 
726     public void
testParse_trustedSourceWithPersonallyOwnedSupportedModes_areEqual()727             testParse_trustedSourceWithPersonallyOwnedSupportedModes_areEqual() throws Exception {
728         Intent intent = buildTestTrustedSourceIntent()
729                 .putExtra(EXTRA_PROVISIONING_SUPPORTED_MODES,
730                         FLAG_SUPPORTED_MODES_PERSONALLY_OWNED);
731         mockInstalledDeviceAdminForTestPackageName();
732 
733         ProvisioningParams params = mExtrasProvisioningDataParser.parse(intent);
734 
735         assertThat(params.initiatorRequestedProvisioningModes)
736                 .isEqualTo(FLAG_SUPPORTED_MODES_PERSONALLY_OWNED);
737     }
738 
739     public void
testParse_trustedSourceWithOrganizationOwnedSupportedModes_areEqual()740             testParse_trustedSourceWithOrganizationOwnedSupportedModes_areEqual() throws Exception {
741         Intent intent = buildTestTrustedSourceIntent()
742                 .putExtra(EXTRA_PROVISIONING_SUPPORTED_MODES,
743                         FLAG_SUPPORTED_MODES_ORGANIZATION_OWNED);
744         mockInstalledDeviceAdminForTestPackageName();
745 
746         ProvisioningParams params = mExtrasProvisioningDataParser.parse(intent);
747 
748         assertThat(params.initiatorRequestedProvisioningModes)
749                 .isEqualTo(FLAG_SUPPORTED_MODES_ORGANIZATION_OWNED);
750     }
751 
testParse_trustedSourceWithOrganizationAndPersonallyOwnedSupportedModes_areEqual()752     public void testParse_trustedSourceWithOrganizationAndPersonallyOwnedSupportedModes_areEqual()
753             throws Exception {
754         Intent intent = buildTestTrustedSourceIntent()
755                 .putExtra(EXTRA_PROVISIONING_SUPPORTED_MODES,
756                         FLAG_SUPPORTED_MODES_ORGANIZATION_OWNED
757                                 | FLAG_SUPPORTED_MODES_PERSONALLY_OWNED);
758         mockInstalledDeviceAdminForTestPackageName();
759 
760         ProvisioningParams params = mExtrasProvisioningDataParser.parse(intent);
761 
762         assertThat(params.initiatorRequestedProvisioningModes)
763                 .isEqualTo(FLAG_SUPPORTED_MODES_ORGANIZATION_OWNED
764                         | FLAG_SUPPORTED_MODES_PERSONALLY_OWNED);
765     }
766 
testParse_trustedSourceWithDeviceOwnerSupportedMode_areEqual()767     public void testParse_trustedSourceWithDeviceOwnerSupportedMode_areEqual()
768             throws Exception {
769         Intent intent = buildTestTrustedSourceIntent()
770                 .putExtra(EXTRA_PROVISIONING_SUPPORTED_MODES, FLAG_SUPPORTED_MODES_DEVICE_OWNER);
771         mockInstalledDeviceAdminForTestPackageName();
772 
773         ProvisioningParams params = mExtrasProvisioningDataParser.parse(intent);
774 
775         assertThat(params.initiatorRequestedProvisioningModes)
776                 .isEqualTo(FLAG_SUPPORTED_MODES_DEVICE_OWNER);
777     }
778 
779     public void
testParse_nonTrustedSourceIntentWithOrganizationOwnedSupportedModes_hasDefaultValue()780     testParse_nonTrustedSourceIntentWithOrganizationOwnedSupportedModes_hasDefaultValue()
781             throws Exception {
782         Intent intent = bildTestNonTrustedSourceIntent()
783                 .putExtra(EXTRA_PROVISIONING_SUPPORTED_MODES,
784                         FLAG_SUPPORTED_MODES_ORGANIZATION_OWNED);
785         mockInstalledDeviceAdminForTestPackageName();
786 
787         ProvisioningParams params = mExtrasProvisioningDataParser.parse(intent);
788 
789         assertThat(params.initiatorRequestedProvisioningModes)
790                 .isEqualTo(DEFAULT_EXTRA_PROVISIONING_SUPPORTED_MODES);
791     }
792 
testParse_trustedSourceWithoutSupportedModes_defaultsToOrganizationOwned()793     public void testParse_trustedSourceWithoutSupportedModes_defaultsToOrganizationOwned()
794             throws Exception {
795         Intent intent = buildTestTrustedSourceIntent();
796         mockInstalledDeviceAdminForTestPackageName();
797 
798         ProvisioningParams params = mExtrasProvisioningDataParser.parse(intent);
799 
800         assertThat(params.initiatorRequestedProvisioningModes)
801                 .isEqualTo(FLAG_SUPPORTED_MODES_ORGANIZATION_OWNED);
802     }
803 
testParse_trustedSourceWithInvalidSupportedModes_throwsException()804     public void testParse_trustedSourceWithInvalidSupportedModes_throwsException()
805             throws IllegalProvisioningArgumentException {
806         Intent intent = buildTestTrustedSourceIntent()
807                 .putExtra(EXTRA_PROVISIONING_SUPPORTED_MODES, INVALID_SUPPORTED_MODES);
808         mockInstalledDeviceAdminForTestPackageName();
809 
810         assertThrows(
811                 IllegalProvisioningArgumentException.class,
812                 () -> mExtrasProvisioningDataParser.parse(intent));
813     }
814 
testParse_trustedSourceWithSkipOwnershipDisclaimerTrue_areEqual()815     public void testParse_trustedSourceWithSkipOwnershipDisclaimerTrue_areEqual()
816             throws IllegalProvisioningArgumentException {
817         Intent intent = buildTestTrustedSourceIntent()
818                 .putExtra(EXTRA_PROVISIONING_SKIP_OWNERSHIP_DISCLAIMER, true);
819         mockInstalledDeviceAdminForTestPackageName();
820 
821         ProvisioningParams params = mExtrasProvisioningDataParser.parse(intent);
822 
823         assertThat(params.skipOwnershipDisclaimer).isTrue();
824     }
825 
testParse_trustedSourceWithSkipOwnershipDisclaimerFalse_areEqual()826     public void testParse_trustedSourceWithSkipOwnershipDisclaimerFalse_areEqual()
827             throws IllegalProvisioningArgumentException {
828         Intent intent = buildTestTrustedSourceIntent()
829                 .putExtra(EXTRA_PROVISIONING_SKIP_OWNERSHIP_DISCLAIMER, false);
830         mockInstalledDeviceAdminForTestPackageName();
831 
832         ProvisioningParams params = mExtrasProvisioningDataParser.parse(intent);
833 
834         assertThat(params.skipOwnershipDisclaimer).isFalse();
835     }
836 
testParse_trustedSourceWithoutSkipOwnershipDisclaimer_isFalse()837     public void testParse_trustedSourceWithoutSkipOwnershipDisclaimer_isFalse()
838             throws IllegalProvisioningArgumentException {
839         Intent intent = buildTestTrustedSourceIntent();
840         mockInstalledDeviceAdminForTestPackageName();
841 
842         ProvisioningParams params = mExtrasProvisioningDataParser.parse(intent);
843 
844         assertThat(params.skipOwnershipDisclaimer).isFalse();
845     }
846 
testParse_managedProfileWithSkipOwnershipDisclaimerTrue_isFalse()847     public void testParse_managedProfileWithSkipOwnershipDisclaimerTrue_isFalse()
848             throws IllegalProvisioningArgumentException {
849         Intent intent = buildTestManagedProfileIntent()
850                 .putExtra(EXTRA_PROVISIONING_SKIP_OWNERSHIP_DISCLAIMER, true);
851         mockInstalledDeviceAdminForTestPackageName();
852 
853         ProvisioningParams params = mExtrasProvisioningDataParser.parse(intent);
854 
855         assertThat(params.skipOwnershipDisclaimer).isFalse();
856     }
857 
testParse_trustedSourceWithReturnBeforePolicyComplianceTrue_isTrue()858     public void testParse_trustedSourceWithReturnBeforePolicyComplianceTrue_isTrue()
859             throws IllegalProvisioningArgumentException {
860         Intent intent = buildTestTrustedSourceIntent()
861                 .putExtra(EXTRA_PROVISIONING_RETURN_BEFORE_POLICY_COMPLIANCE, true);
862         mockInstalledDeviceAdminForTestPackageName();
863 
864         ProvisioningParams params = mExtrasProvisioningDataParser.parse(intent);
865 
866         assertThat(params.returnBeforePolicyCompliance).isTrue();
867     }
868 
testParse_trustedSourceWithReturnBeforePolicyComplianceFalse_isFalse()869     public void testParse_trustedSourceWithReturnBeforePolicyComplianceFalse_isFalse()
870             throws IllegalProvisioningArgumentException {
871         Intent intent = buildTestTrustedSourceIntent()
872                 .putExtra(EXTRA_PROVISIONING_RETURN_BEFORE_POLICY_COMPLIANCE, false);
873         mockInstalledDeviceAdminForTestPackageName();
874 
875         ProvisioningParams params = mExtrasProvisioningDataParser.parse(intent);
876 
877         assertThat(params.returnBeforePolicyCompliance).isFalse();
878     }
879 
testParse_trustedSourceWithReturnBeforePolicyComplianceNotSet_isTrue()880     public void testParse_trustedSourceWithReturnBeforePolicyComplianceNotSet_isTrue()
881             throws IllegalProvisioningArgumentException {
882         Intent intent = buildTestTrustedSourceIntent();
883         mockInstalledDeviceAdminForTestPackageName();
884 
885         ProvisioningParams params = mExtrasProvisioningDataParser.parse(intent);
886 
887         assertThat(params.returnBeforePolicyCompliance).isTrue();
888     }
889 
testParse_managedProfileWithReturnBeforePolicyComplianceTrue_afterSetupWizard_isFalse()890     public void testParse_managedProfileWithReturnBeforePolicyComplianceTrue_afterSetupWizard_isFalse()
891             throws IllegalProvisioningArgumentException {
892         when(mSettingsFacade.isDuringSetupWizard(mContext)).thenReturn(false);
893         Intent intent = buildTestManagedProfileIntent()
894                 .putExtra(EXTRA_PROVISIONING_RETURN_BEFORE_POLICY_COMPLIANCE, true);
895         mockInstalledDeviceAdminForTestPackageName();
896 
897         ProvisioningParams params = mExtrasProvisioningDataParser.parse(intent);
898 
899         assertThat(params.returnBeforePolicyCompliance).isFalse();
900     }
901 
testParse_managedProfileWithReturnBeforePolicyComplianceTrue_duringSetupWizard_isTrue()902     public void testParse_managedProfileWithReturnBeforePolicyComplianceTrue_duringSetupWizard_isTrue()
903             throws IllegalProvisioningArgumentException {
904         when(mSettingsFacade.isDuringSetupWizard(mContext)).thenReturn(true);
905         Intent intent = buildTestManagedProfileIntent()
906                 .putExtra(EXTRA_PROVISIONING_RETURN_BEFORE_POLICY_COMPLIANCE, true);
907         mockInstalledDeviceAdminForTestPackageName();
908 
909         ProvisioningParams params = mExtrasProvisioningDataParser.parse(intent);
910 
911         assertThat(params.returnBeforePolicyCompliance).isTrue();
912     }
913 
testParse_managedProfileWithReturnBeforePolicyComplianceNotSet_isFalse()914     public void testParse_managedProfileWithReturnBeforePolicyComplianceNotSet_isFalse()
915             throws IllegalProvisioningArgumentException {
916         Intent intent = buildTestManagedProfileIntent();
917         mockInstalledDeviceAdminForTestPackageName();
918 
919         ProvisioningParams params = mExtrasProvisioningDataParser.parse(intent);
920 
921         assertThat(params.returnBeforePolicyCompliance).isFalse();
922     }
923 
testParse_financedDeviceProvisioningWithReturnBeforePolicyComplianceTrue_isTrue()924     public void testParse_financedDeviceProvisioningWithReturnBeforePolicyComplianceTrue_isTrue()
925             throws IllegalProvisioningArgumentException {
926         Intent intent = buildTestFinancedDeviceIntent()
927                 .putExtra(EXTRA_PROVISIONING_RETURN_BEFORE_POLICY_COMPLIANCE, true);
928         mockInstalledDeviceAdminForTestPackageName();
929 
930         ProvisioningParams params = mExtrasProvisioningDataParser.parse(intent);
931 
932         assertThat(params.returnBeforePolicyCompliance).isTrue();
933     }
934 
testParse_financedDeviceProvisioningWithReturnBeforePolicyComplianceFalse_isTrue()935     public void testParse_financedDeviceProvisioningWithReturnBeforePolicyComplianceFalse_isTrue()
936             throws IllegalProvisioningArgumentException {
937         Intent intent = buildTestFinancedDeviceIntent()
938                 .putExtra(EXTRA_PROVISIONING_RETURN_BEFORE_POLICY_COMPLIANCE, false);
939         mockInstalledDeviceAdminForTestPackageName();
940 
941         ProvisioningParams params = mExtrasProvisioningDataParser.parse(intent);
942 
943         assertThat(params.returnBeforePolicyCompliance).isTrue();
944     }
945 
testParse_financedDeviceProvisioningWithReturnBeforePolicyComplianceNotSet_isTrue()946     public void testParse_financedDeviceProvisioningWithReturnBeforePolicyComplianceNotSet_isTrue()
947             throws IllegalProvisioningArgumentException {
948         Intent intent = buildTestFinancedDeviceIntent();
949         mockInstalledDeviceAdminForTestPackageName();
950 
951         ProvisioningParams params = mExtrasProvisioningDataParser.parse(intent);
952 
953         assertThat(params.returnBeforePolicyCompliance).isTrue();
954     }
955 
buildAllShortExtras()956     private Stream<Field> buildAllShortExtras() {
957         Field[] fields = ExtrasProvisioningDataParser.class.getDeclaredFields();
958         return Arrays.stream(fields)
959                 .filter(field -> field.getName().startsWith("EXTRA_")
960                         && field.getName().endsWith("_SHORT"));
961     }
962 
createTestProvisioningParamsBuilder()963     private ProvisioningParams.Builder createTestProvisioningParamsBuilder() {
964         return ProvisioningParams.Builder.builder().setProvisioningId(TEST_PROVISIONING_ID);
965     }
966 
buildIntentWithAllShortExtras()967     private Intent buildIntentWithAllShortExtras() {
968         Bundle bundleShort = new Bundle();
969         bundleShort.putString(
970                 EXTRA_PROVISIONING_DISCLAIMER_HEADER_SHORT, TEST_DISCLAMER_HEADER);
971         bundleShort.putParcelable(EXTRA_PROVISIONING_DISCLAIMER_CONTENT_SHORT, TEST_URI);
972         Parcelable[] parcelablesShort = {bundleShort};
973         return new Intent(ACTION_PROVISION_MANAGED_DEVICE)
974                 .putExtra(EXTRA_PROVISIONING_DEVICE_ADMIN_PACKAGE_NAME_SHORT,
975                         TEST_PACKAGE_NAME)
976                 .putExtra(EXTRA_PROVISIONING_DEVICE_ADMIN_COMPONENT_NAME_SHORT,
977                         TEST_COMPONENT_NAME)
978                 .putExtras(getShortTestTimeTimeZoneAndLocaleExtras())
979                 .putExtras(getAllShortTestWifiInfoExtras())
980                 .putExtras(getShortTestDeviceAdminDownloadExtras())
981                 .putExtra(EXTRA_PROVISIONING_ADMIN_EXTRAS_BUNDLE_SHORT,
982                         createTestAdminExtras())
983                 .putExtra(EXTRA_PROVISIONING_SKIP_ENCRYPTION_SHORT, TEST_SKIP_ENCRYPTION)
984                 .putExtra(EXTRA_PROVISIONING_LEAVE_ALL_SYSTEM_APPS_ENABLED_SHORT,
985                         TEST_LEAVE_ALL_SYSTEM_APP_ENABLED)
986                 .putExtra(EXTRA_PROVISIONING_ACCOUNT_TO_MIGRATE_SHORT, TEST_ACCOUNT_TO_MIGRATE)
987                 .putExtra(EXTRA_PROVISIONING_KEEP_ACCOUNT_ON_MIGRATION_SHORT,
988                         TEST_KEEP_ACCOUNT_MIGRATED)
989                 .putExtra(EXTRA_PROVISIONING_ORGANIZATION_NAME_SHORT,
990                         TEST_ORGANIZATION_NAME)
991                 .putExtra(EXTRA_PROVISIONING_SUPPORT_URL_SHORT, TEST_SUPPORT_URL)
992                 .putExtra(EXTRA_PROVISIONING_USE_MOBILE_DATA_SHORT,
993                         TEST_USE_MOBILE_DATA)
994                 .putExtra(EXTRA_PROVISIONING_LOGO_URI_SHORT, TEST_URI)
995                 .putExtra(EXTRA_PROVISIONING_DISCLAIMERS_SHORT, parcelablesShort)
996                 .putExtra(EXTRA_PROVISIONING_SENSORS_PERMISSION_GRANT_OPT_OUT_SHORT, true);
997     }
998 
buildIntentWithAllLongExtras()999     private Intent buildIntentWithAllLongExtras() {
1000         Bundle bundleLong = new Bundle();
1001         bundleLong.putString(EXTRA_PROVISIONING_DISCLAIMER_HEADER, TEST_DISCLAMER_HEADER);
1002         bundleLong.putParcelable(EXTRA_PROVISIONING_DISCLAIMER_CONTENT, TEST_URI);
1003         Parcelable[] parcelablesLong = {bundleLong};
1004         return new Intent(ACTION_PROVISION_MANAGED_DEVICE)
1005                 .putExtra(EXTRA_PROVISIONING_DEVICE_ADMIN_PACKAGE_NAME, TEST_PACKAGE_NAME)
1006                 .putExtra(EXTRA_PROVISIONING_DEVICE_ADMIN_COMPONENT_NAME, TEST_COMPONENT_NAME)
1007                 .putExtras(getTestTimeTimeZoneAndLocaleExtras())
1008                 .putExtras(getAllTestWifiInfoExtras())
1009                 .putExtras(getTestDeviceAdminDownloadExtras())
1010                 .putExtra(EXTRA_PROVISIONING_ADMIN_EXTRAS_BUNDLE, createTestAdminExtras())
1011                 .putExtra(EXTRA_PROVISIONING_SKIP_ENCRYPTION, TEST_SKIP_ENCRYPTION)
1012                 .putExtra(EXTRA_PROVISIONING_LEAVE_ALL_SYSTEM_APPS_ENABLED,
1013                         TEST_LEAVE_ALL_SYSTEM_APP_ENABLED)
1014                 .putExtra(EXTRA_PROVISIONING_ACCOUNT_TO_MIGRATE, TEST_ACCOUNT_TO_MIGRATE)
1015                 .putExtra(EXTRA_PROVISIONING_KEEP_ACCOUNT_ON_MIGRATION, TEST_KEEP_ACCOUNT_MIGRATED)
1016                 .putExtra(EXTRA_PROVISIONING_ORGANIZATION_NAME, TEST_ORGANIZATION_NAME)
1017                 .putExtra(EXTRA_PROVISIONING_SUPPORT_URL, TEST_SUPPORT_URL)
1018                 .putExtra(EXTRA_PROVISIONING_USE_MOBILE_DATA, TEST_USE_MOBILE_DATA)
1019                 .putExtra(EXTRA_PROVISIONING_LOGO_URI, TEST_URI)
1020                 .putExtra(EXTRA_PROVISIONING_DISCLAIMERS, parcelablesLong)
1021                 .putExtra(EXTRA_PROVISIONING_SENSORS_PERMISSION_GRANT_OPT_OUT, true);
1022     }
1023 
buildTestManagedProfileIntent()1024     private static Intent buildTestManagedProfileIntent() {
1025         return new Intent(ACTION_PROVISION_MANAGED_PROFILE)
1026                 .putExtra(EXTRA_PROVISIONING_DEVICE_ADMIN_PACKAGE_NAME, TEST_PACKAGE_NAME)
1027                 .putExtra(EXTRA_PROVISIONING_DEVICE_ADMIN_COMPONENT_NAME, TEST_COMPONENT_NAME)
1028                 .putExtras(getTestTimeTimeZoneAndLocaleExtras())
1029                 .putExtras(getTestWifiInfoExtras())
1030                 .putExtras(getTestDeviceAdminDownloadExtras())
1031                 .putExtra(EXTRA_PROVISIONING_ADMIN_EXTRAS_BUNDLE, createTestAdminExtras())
1032                 .putExtra(EXTRA_PROVISIONING_SKIP_ENCRYPTION, TEST_SKIP_ENCRYPTION)
1033                 .putExtra(EXTRA_PROVISIONING_KEEP_ACCOUNT_ON_MIGRATION, TEST_KEEP_ACCOUNT_MIGRATED)
1034                 .putExtra(EXTRA_PROVISIONING_LEAVE_ALL_SYSTEM_APPS_ENABLED,
1035                         TEST_LEAVE_ALL_SYSTEM_APP_ENABLED)
1036                 .putExtra(EXTRA_PROVISIONING_ACCOUNT_TO_MIGRATE, TEST_ACCOUNT_TO_MIGRATE);
1037     }
1038 
buildTestFinancedDeviceIntent()1039     private static Intent buildTestFinancedDeviceIntent() {
1040         return new Intent(ACTION_PROVISION_FINANCED_DEVICE)
1041                 .putExtra(EXTRA_PROVISIONING_DEVICE_ADMIN_PACKAGE_NAME, TEST_PACKAGE_NAME)
1042                 .putExtra(EXTRA_PROVISIONING_DEVICE_ADMIN_COMPONENT_NAME, TEST_COMPONENT_NAME);
1043     }
1044 
bildTestNonTrustedSourceIntent()1045     private static Intent bildTestNonTrustedSourceIntent() {
1046         return buildTestManagedProfileIntent();
1047     }
1048 
buildTestIntent()1049     private static Intent buildTestIntent() {
1050         return buildTestTrustedSourceIntent();
1051     }
1052 
buildTestTrustedSourceIntent()1053     private static Intent buildTestTrustedSourceIntent() {
1054         return  new Intent(ACTION_PROVISION_MANAGED_DEVICE_FROM_TRUSTED_SOURCE)
1055                 .putExtra(EXTRA_PROVISIONING_DEVICE_ADMIN_PACKAGE_NAME, TEST_PACKAGE_NAME)
1056                 .putExtra(EXTRA_PROVISIONING_DEVICE_ADMIN_COMPONENT_NAME, TEST_COMPONENT_NAME)
1057                 .putExtras(getTestTimeTimeZoneAndLocaleExtras())
1058                 .putExtras(getTestWifiInfoExtras())
1059                 .putExtras(getTestDeviceAdminDownloadExtras())
1060                 .putExtra(EXTRA_PROVISIONING_ADMIN_EXTRAS_BUNDLE, createTestAdminExtras())
1061                 .putExtra(EXTRA_PROVISIONING_SKIP_ENCRYPTION, TEST_SKIP_ENCRYPTION)
1062                 .putExtra(EXTRA_PROVISIONING_KEEP_ACCOUNT_ON_MIGRATION, TEST_KEEP_ACCOUNT_MIGRATED)
1063                 .putExtra(EXTRA_PROVISIONING_LEAVE_ALL_SYSTEM_APPS_ENABLED,
1064                         TEST_LEAVE_ALL_SYSTEM_APP_ENABLED)
1065                 .putExtra(EXTRA_PROVISIONING_ACCOUNT_TO_MIGRATE, TEST_ACCOUNT_TO_MIGRATE)
1066                 .putExtra(EXTRA_PROVISIONING_ORGANIZATION_NAME, TEST_ORGANIZATION_NAME)
1067                 .putExtra(EXTRA_PROVISIONING_SUPPORT_URL, TEST_SUPPORT_URL);
1068     }
1069 
buildTestQrCodeIntent()1070     private Intent buildTestQrCodeIntent() {
1071         return buildTestTrustedSourceIntent()
1072                 .putExtra(EXTRA_PROVISIONING_TRIGGER, PROVISIONING_TRIGGER_QR_CODE);
1073     }
1074 
buildTestProvisionManagedAccountIntent()1075     private Intent buildTestProvisionManagedAccountIntent() {
1076         return buildTestTrustedSourceIntent()
1077                 .putExtra(EXTRA_PROVISIONING_TRIGGER, PROVISIONING_TRIGGER_MANAGED_ACCOUNT);
1078     }
1079 
getTestWifiInfoExtras()1080     private static Bundle getTestWifiInfoExtras() {
1081         Bundle wifiInfoExtras = new Bundle();
1082         wifiInfoExtras.putString(EXTRA_PROVISIONING_WIFI_SSID, TEST_SSID);
1083         wifiInfoExtras.putString(EXTRA_PROVISIONING_WIFI_SECURITY_TYPE, TEST_SECURITY_TYPE);
1084         wifiInfoExtras.putString(EXTRA_PROVISIONING_WIFI_PASSWORD, TEST_PASSWORD);
1085         wifiInfoExtras.putString(EXTRA_PROVISIONING_WIFI_PROXY_HOST, TEST_PROXY_HOST);
1086         wifiInfoExtras.putString(EXTRA_PROVISIONING_WIFI_PROXY_BYPASS, TEST_PROXY_BYPASS_HOSTS);
1087         wifiInfoExtras.putString(EXTRA_PROVISIONING_WIFI_PAC_URL, TEST_PAC_URL);
1088         wifiInfoExtras.putInt(EXTRA_PROVISIONING_WIFI_PROXY_PORT, TEST_PROXY_PORT);
1089         wifiInfoExtras.putBoolean(EXTRA_PROVISIONING_WIFI_HIDDEN, TEST_HIDDEN);
1090         return wifiInfoExtras;
1091     }
1092 
getAllTestWifiInfoExtras()1093     private static Bundle getAllTestWifiInfoExtras() {
1094         Bundle wifiInfoExtras = new Bundle();
1095         wifiInfoExtras.putString(EXTRA_PROVISIONING_WIFI_SSID, TEST_SSID);
1096         wifiInfoExtras.putString(EXTRA_PROVISIONING_WIFI_SECURITY_TYPE, TEST_SECURITY_TYPE);
1097         wifiInfoExtras.putString(EXTRA_PROVISIONING_WIFI_PASSWORD, TEST_PASSWORD);
1098         wifiInfoExtras.putString(EXTRA_PROVISIONING_WIFI_EAP_METHOD, TEST_EAP_METHOD);
1099         wifiInfoExtras.putString(EXTRA_PROVISIONING_WIFI_PHASE2_AUTH, TEST_PHASE2_AUTH);
1100         wifiInfoExtras.putString(EXTRA_PROVISIONING_WIFI_CA_CERTIFICATE, TEST_CA_CERT);
1101         wifiInfoExtras.putString(EXTRA_PROVISIONING_WIFI_USER_CERTIFICATE, TEST_USER_CERT);
1102         wifiInfoExtras.putString(EXTRA_PROVISIONING_WIFI_IDENTITY, TEST_IDENTITY);
1103         wifiInfoExtras.putString(
1104                 EXTRA_PROVISIONING_WIFI_ANONYMOUS_IDENTITY, TEST_ANONYMOUS_IDENTITY);
1105         wifiInfoExtras.putString(EXTRA_PROVISIONING_WIFI_DOMAIN, TEST_DOMAIN);
1106         wifiInfoExtras.putString(EXTRA_PROVISIONING_WIFI_PROXY_HOST, TEST_PROXY_HOST);
1107         wifiInfoExtras.putString(EXTRA_PROVISIONING_WIFI_PROXY_BYPASS, TEST_PROXY_BYPASS_HOSTS);
1108         wifiInfoExtras.putString(EXTRA_PROVISIONING_WIFI_PAC_URL, TEST_PAC_URL);
1109         wifiInfoExtras.putInt(EXTRA_PROVISIONING_WIFI_PROXY_PORT, TEST_PROXY_PORT);
1110         wifiInfoExtras.putBoolean(EXTRA_PROVISIONING_WIFI_HIDDEN, TEST_HIDDEN);
1111         return wifiInfoExtras;
1112     }
1113 
getAllShortTestWifiInfoExtras()1114     private static Bundle getAllShortTestWifiInfoExtras() {
1115         Bundle wifiInfoExtras = new Bundle();
1116         wifiInfoExtras.putString(EXTRA_PROVISIONING_WIFI_SSID_SHORT, TEST_SSID);
1117         wifiInfoExtras.putString(
1118                 EXTRA_PROVISIONING_WIFI_SECURITY_TYPE_SHORT, TEST_SECURITY_TYPE);
1119         wifiInfoExtras.putString(EXTRA_PROVISIONING_WIFI_PASSWORD_SHORT, TEST_PASSWORD);
1120         wifiInfoExtras.putString(EXTRA_PROVISIONING_WIFI_EAP_METHOD_SHORT, TEST_EAP_METHOD);
1121         wifiInfoExtras.putString(EXTRA_PROVISIONING_WIFI_PHASE2_AUTH_SHORT, TEST_PHASE2_AUTH);
1122         wifiInfoExtras.putString(EXTRA_PROVISIONING_WIFI_CA_CERTIFICATE_SHORT, TEST_CA_CERT);
1123         wifiInfoExtras.putString(EXTRA_PROVISIONING_WIFI_USER_CERTIFICATE_SHORT, TEST_USER_CERT);
1124         wifiInfoExtras.putString(EXTRA_PROVISIONING_WIFI_IDENTITY_SHORT, TEST_IDENTITY);
1125         wifiInfoExtras.putString(
1126                 EXTRA_PROVISIONING_WIFI_ANONYMOUS_IDENTITY_SHORT, TEST_ANONYMOUS_IDENTITY);
1127         wifiInfoExtras.putString(EXTRA_PROVISIONING_WIFI_DOMAIN_SHORT, TEST_DOMAIN);
1128         wifiInfoExtras.putString(EXTRA_PROVISIONING_WIFI_PROXY_HOST_SHORT, TEST_PROXY_HOST);
1129         wifiInfoExtras.putString(
1130                 EXTRA_PROVISIONING_WIFI_PROXY_BYPASS_SHORT, TEST_PROXY_BYPASS_HOSTS);
1131         wifiInfoExtras.putString(EXTRA_PROVISIONING_WIFI_PAC_URL_SHORT, TEST_PAC_URL);
1132         wifiInfoExtras.putInt(EXTRA_PROVISIONING_WIFI_PROXY_PORT_SHORT, TEST_PROXY_PORT);
1133         wifiInfoExtras.putBoolean(EXTRA_PROVISIONING_WIFI_HIDDEN_SHORT, TEST_HIDDEN);
1134         return wifiInfoExtras;
1135     }
1136 
mockInstalledDeviceAdminForTestPackageName()1137     private void mockInstalledDeviceAdminForTestPackageName()
1138             throws IllegalProvisioningArgumentException {
1139         mockInstalledDeviceAdmin(TEST_PACKAGE_NAME);
1140     }
1141 
mockInstalledDeviceAdminForNullPackageName()1142     private void mockInstalledDeviceAdminForNullPackageName()
1143             throws IllegalProvisioningArgumentException {
1144         mockInstalledDeviceAdmin(null);
1145     }
1146 
mockInstalledDeviceAdmin(String packageName)1147     private void mockInstalledDeviceAdmin(String packageName)
1148             throws IllegalProvisioningArgumentException {
1149         doReturn(TEST_COMPONENT_NAME)
1150                 .when(mUtils)
1151                 .findDeviceAdmin(packageName, TEST_COMPONENT_NAME, mContext, UserHandle.myUserId());
1152     }
1153 
buildTestLocaleString()1154     private static String buildTestLocaleString() {
1155         return StoreUtils.localeToString(TEST_LOCALE);
1156     }
1157 
getTestTimeTimeZoneAndLocaleExtras()1158     private static Bundle getTestTimeTimeZoneAndLocaleExtras() {
1159         return getTestTimeTimeZoneAndLocaleExtrasInternal(buildTestLocaleString());
1160     }
1161 
getShortTestTimeTimeZoneAndLocaleExtras()1162     private static Bundle getShortTestTimeTimeZoneAndLocaleExtras() {
1163         return getShortTestTimeTimeZoneAndLocaleExtrasInternal(buildTestLocaleString());
1164     }
1165 
getTestTimeTimeZoneAndLocaleExtras(String locale)1166     private static Bundle getTestTimeTimeZoneAndLocaleExtras(String locale) {
1167         return getTestTimeTimeZoneAndLocaleExtrasInternal(locale);
1168     }
1169 
getTestTimeTimeZoneAndLocaleExtrasInternal(String locale)1170     private static Bundle getTestTimeTimeZoneAndLocaleExtrasInternal(String locale){
1171         Bundle timeTimezoneAndLocaleExtras = new Bundle();
1172         timeTimezoneAndLocaleExtras.putLong(EXTRA_PROVISIONING_LOCAL_TIME, TEST_LOCAL_TIME);
1173         timeTimezoneAndLocaleExtras.putString(EXTRA_PROVISIONING_TIME_ZONE, TEST_TIME_ZONE);
1174         timeTimezoneAndLocaleExtras.putString(EXTRA_PROVISIONING_LOCALE, locale);
1175         return timeTimezoneAndLocaleExtras;
1176     }
1177 
getShortTestTimeTimeZoneAndLocaleExtrasInternal(String locale)1178     private static Bundle getShortTestTimeTimeZoneAndLocaleExtrasInternal(String locale){
1179         Bundle timeTimezoneAndLocaleExtras = new Bundle();
1180         timeTimezoneAndLocaleExtras.putLong(
1181                 EXTRA_PROVISIONING_LOCAL_TIME_SHORT, TEST_LOCAL_TIME);
1182         timeTimezoneAndLocaleExtras.putString(
1183                 EXTRA_PROVISIONING_TIME_ZONE_SHORT, TEST_TIME_ZONE);
1184         timeTimezoneAndLocaleExtras.putString(
1185                 EXTRA_PROVISIONING_LOCALE_SHORT, locale);
1186         return timeTimezoneAndLocaleExtras;
1187     }
1188 
getTestDeviceAdminDownloadExtras()1189     private static Bundle getTestDeviceAdminDownloadExtras() {
1190         Bundle downloadInfoExtras = new Bundle();
1191         downloadInfoExtras.putInt(
1192                 EXTRA_PROVISIONING_DEVICE_ADMIN_MINIMUM_VERSION_CODE, TEST_MIN_SUPPORT_VERSION);
1193         downloadInfoExtras.putString(
1194                 EXTRA_PROVISIONING_DEVICE_ADMIN_PACKAGE_DOWNLOAD_LOCATION, TEST_DOWNLOAD_LOCATION);
1195         downloadInfoExtras.putString(
1196                 EXTRA_PROVISIONING_DEVICE_ADMIN_PACKAGE_DOWNLOAD_COOKIE_HEADER, TEST_COOKIE_HEADER);
1197         downloadInfoExtras.putString(
1198                 EXTRA_PROVISIONING_DEVICE_ADMIN_PACKAGE_CHECKSUM,
1199                 buildTestPackageChecksum());
1200         downloadInfoExtras.putString(
1201                 EXTRA_PROVISIONING_DEVICE_ADMIN_SIGNATURE_CHECKSUM,
1202                 buildTestSignatureChecksum());
1203         return downloadInfoExtras;
1204     }
1205 
buildTestPackageChecksum()1206     private static String buildTestPackageChecksum() {
1207         return Base64.encodeToString(TEST_PACKAGE_CHECKSUM,
1208                 Base64.URL_SAFE | Base64.NO_PADDING | Base64.NO_WRAP);
1209     }
1210 
getShortTestDeviceAdminDownloadExtras()1211     private static Bundle getShortTestDeviceAdminDownloadExtras() {
1212         Bundle downloadInfoExtras = new Bundle();
1213         downloadInfoExtras.putInt(
1214                 EXTRA_PROVISIONING_DEVICE_ADMIN_MINIMUM_VERSION_CODE_SHORT,
1215                 TEST_MIN_SUPPORT_VERSION);
1216         downloadInfoExtras.putString(
1217                 EXTRA_PROVISIONING_DEVICE_ADMIN_PACKAGE_DOWNLOAD_LOCATION_SHORT,
1218                 TEST_DOWNLOAD_LOCATION);
1219         downloadInfoExtras.putString(
1220                 EXTRA_PROVISIONING_DEVICE_ADMIN_PACKAGE_DOWNLOAD_COOKIE_HEADER_SHORT,
1221                 TEST_COOKIE_HEADER);
1222         downloadInfoExtras.putString(
1223                 EXTRA_PROVISIONING_DEVICE_ADMIN_PACKAGE_CHECKSUM_SHORT,
1224                 buildTestPackageChecksum());
1225         downloadInfoExtras.putString(
1226                 EXTRA_PROVISIONING_DEVICE_ADMIN_SIGNATURE_CHECKSUM_SHORT,
1227                 buildTestSignatureChecksum());
1228         return downloadInfoExtras;
1229     }
1230 
buildTestSignatureChecksum()1231     private static String buildTestSignatureChecksum() {
1232         return Base64.encodeToString(TEST_SIGNATURE_CHECKSUM,
1233                 Base64.URL_SAFE | Base64.NO_PADDING | Base64.NO_WRAP);
1234     }
1235 }
1236