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_MANAGED_DEVICE;
19 import static android.app.admin.DevicePolicyManager.ACTION_PROVISION_MANAGED_DEVICE_FROM_TRUSTED_SOURCE;
20 import static android.app.admin.DevicePolicyManager.ACTION_PROVISION_MANAGED_USER;
21 import static android.app.admin.DevicePolicyManager.ACTION_PROVISION_MANAGED_PROFILE;
22 import static android.app.admin.DevicePolicyManager.ACTION_PROVISION_MANAGED_SHAREABLE_DEVICE;
23 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_ACCOUNT_TO_MIGRATE;
24 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_ADMIN_EXTRAS_BUNDLE;
25 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_DEVICE_ADMIN_COMPONENT_NAME;
26 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_DEVICE_ADMIN_MINIMUM_VERSION_CODE;
27 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_DEVICE_ADMIN_PACKAGE_CHECKSUM;
28 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_DEVICE_ADMIN_PACKAGE_DOWNLOAD_COOKIE_HEADER;
29 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_DEVICE_ADMIN_PACKAGE_DOWNLOAD_LOCATION;
30 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_DEVICE_ADMIN_PACKAGE_NAME;
31 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_DEVICE_ADMIN_SIGNATURE_CHECKSUM;
32 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_LEAVE_ALL_SYSTEM_APPS_ENABLED;
33 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_LOCALE;
34 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_LOCAL_TIME;
35 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_MAIN_COLOR;
36 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_SKIP_ENCRYPTION;
37 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_TIME_ZONE;
38 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_WIFI_HIDDEN;
39 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_WIFI_PAC_URL;
40 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_WIFI_PASSWORD;
41 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_WIFI_PROXY_BYPASS;
42 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_WIFI_PROXY_HOST;
43 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_WIFI_PROXY_PORT;
44 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_WIFI_SECURITY_TYPE;
45 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_WIFI_SSID;
46 import static android.nfc.NfcAdapter.ACTION_NDEF_DISCOVERED;
47 import static com.android.managedprovisioning.TestUtils.createTestAdminExtras;
48 import static com.android.managedprovisioning.parser.MessageParser.EXTRA_PROVISIONING_ACTION;
49 import static com.android.managedprovisioning.parser.MessageParser.EXTRA_PROVISIONING_DEVICE_ADMIN_SUPPORT_SHA1_PACKAGE_CHECKSUM;
50 import static com.android.managedprovisioning.parser.MessageParser.EXTRA_PROVISIONING_STARTED_BY_TRUSTED_SOURCE;
51 import static org.mockito.Mockito.doReturn;
52 import static org.mockito.Mockito.spy;
53 
54 import android.accounts.Account;
55 import android.app.admin.DevicePolicyManager;
56 import android.content.ComponentName;
57 import android.content.Context;
58 import android.content.Intent;
59 import android.os.Bundle;
60 import android.os.PersistableBundle;
61 import android.test.AndroidTestCase;
62 import android.test.suitebuilder.annotation.SmallTest;
63 import android.util.Base64;
64 
65 import com.android.managedprovisioning.common.Globals;
66 import com.android.managedprovisioning.common.IllegalProvisioningArgumentException;
67 import com.android.managedprovisioning.common.Utils;
68 import com.android.managedprovisioning.model.PackageDownloadInfo;
69 import com.android.managedprovisioning.model.ProvisioningParams;
70 import com.android.managedprovisioning.model.WifiInfo;
71 
72 import org.mockito.Mock;
73 import org.mockito.MockitoAnnotations;
74 
75 import java.util.Locale;
76 
77 /** Tests for {@link ExtrasProvisioningDataParser}. */
78 @SmallTest
79 public class ExtrasProvisioningDataParserTest extends AndroidTestCase {
80     private static final String TEST_PACKAGE_NAME = "com.afwsamples.testdpc";
81     private static final ComponentName TEST_COMPONENT_NAME =
82             ComponentName.unflattenFromString(
83                     "com.afwsamples.testdpc/com.afwsamples.testdpc.DeviceAdminReceiver");
84     private static final long TEST_LOCAL_TIME = 1456939524713L;
85     private static final Locale TEST_LOCALE = Locale.UK;
86     private static final String TEST_TIME_ZONE = "GMT";
87     private static final Integer TEST_MAIN_COLOR = 65280;
88     private static final boolean TEST_STARTED_BY_TRUSTED_SOURCE = true;
89     private static final boolean TEST_LEAVE_ALL_SYSTEM_APP_ENABLED = true;
90     private static final boolean TEST_SKIP_ENCRYPTION = true;
91     private static final boolean TEST_SKIP_USER_SETUP = true;
92     private static final Account TEST_ACCOUNT_TO_MIGRATE =
93             new Account("user@gmail.com", "com.google");
94 
95     // Wifi info
96     private static final String TEST_SSID = "TestWifi";
97     private static final boolean TEST_HIDDEN = true;
98     private static final String TEST_SECURITY_TYPE = "WPA2";
99     private static final String TEST_PASSWORD = "GoogleRock";
100     private static final String TEST_PROXY_HOST = "testhost.com";
101     private static final int TEST_PROXY_PORT = 7689;
102     private static final String TEST_PROXY_BYPASS_HOSTS = "http://host1.com;https://host2.com";
103     private static final String TEST_PAC_URL = "pac.test.com";
104     private static final WifiInfo TEST_WIFI_INFO = WifiInfo.Builder.builder()
105             .setSsid(TEST_SSID)
106             .setHidden(TEST_HIDDEN)
107             .setSecurityType(TEST_SECURITY_TYPE)
108             .setPassword(TEST_PASSWORD)
109             .setProxyHost(TEST_PROXY_HOST)
110             .setProxyPort(TEST_PROXY_PORT)
111             .setProxyBypassHosts(TEST_PROXY_BYPASS_HOSTS)
112             .setPacUrl(TEST_PAC_URL)
113             .build();
114 
115     // Device admin package download info
116     private static final String TEST_DOWNLOAD_LOCATION =
117             "http://example/dpc.apk";
118     private static final String TEST_COOKIE_HEADER =
119             "Set-Cookie: sessionToken=foobar; Expires=Thu, 18 Feb 2016 23:59:59 GMT";
120     private static final byte[] TEST_PACKAGE_CHECKSUM = new byte[] { '1', '2', '3', '4', '5' };
121     private static final byte[] TEST_SIGNATURE_CHECKSUM = new byte[] { '5', '4', '3', '2', '1' };
122     private static final int TEST_MIN_SUPPORT_VERSION = 17689;
123     private static final PackageDownloadInfo TEST_DOWNLOAD_INFO =
124             PackageDownloadInfo.Builder.builder()
125                     .setLocation(TEST_DOWNLOAD_LOCATION)
126                     .setCookieHeader(TEST_COOKIE_HEADER)
127                     .setPackageChecksum(TEST_PACKAGE_CHECKSUM)
128                     .setSignatureChecksum(TEST_SIGNATURE_CHECKSUM)
129                     .setMinVersion(TEST_MIN_SUPPORT_VERSION)
130                     .build();
131     @Mock
132     private Context mContext;
133 
134     private ExtrasProvisioningDataParser mExtrasProvisioningDataParser;
135 
136     private Utils mUtils;
137 
138     @Override
setUp()139     public void setUp() {
140         // this is necessary for mockito to work
141         System.setProperty("dexmaker.dexcache", getContext().getCacheDir().toString());
142 
143         MockitoAnnotations.initMocks(this);
144 
145         mExtrasProvisioningDataParser = new ExtrasProvisioningDataParser(mUtils = spy(new Utils()));
146     }
147 
testParse_trustedSourceProvisioningIntent()148     public void testParse_trustedSourceProvisioningIntent() throws Exception {
149         // GIVEN a ACTION_PROVISION_MANAGED_DEVICE_FROM_TRUSTED_SOURCE intent and other extras.
150         Intent intent = new Intent(ACTION_PROVISION_MANAGED_DEVICE_FROM_TRUSTED_SOURCE)
151                 // GIVEN a device admin package name and component name
152                 .putExtra(EXTRA_PROVISIONING_DEVICE_ADMIN_PACKAGE_NAME, TEST_PACKAGE_NAME)
153                 .putExtra(EXTRA_PROVISIONING_DEVICE_ADMIN_COMPONENT_NAME, TEST_COMPONENT_NAME)
154                 .putExtras(getTestTimeTimeZoneAndLocaleExtras())
155                 .putExtras(getTestWifiInfoExtras())
156                 .putExtras(getTestDeviceAdminDownloadExtras())
157                 .putExtra(EXTRA_PROVISIONING_ADMIN_EXTRAS_BUNDLE, createTestAdminExtras())
158                 .putExtra(EXTRA_PROVISIONING_SKIP_ENCRYPTION, TEST_SKIP_ENCRYPTION)
159                 .putExtra(EXTRA_PROVISIONING_MAIN_COLOR, TEST_MAIN_COLOR)
160                 .putExtra(EXTRA_PROVISIONING_ACCOUNT_TO_MIGRATE, TEST_ACCOUNT_TO_MIGRATE);
161 
162         // WHEN the intent is parsed by the parser.
163         ProvisioningParams params = mExtrasProvisioningDataParser.parse(intent, mContext);
164 
165         // THEN ProvisionParams is constructed as expected.
166         assertEquals(
167                 ProvisioningParams.Builder.builder()
168                         // THEN ACTION_PROVISION_MANAGED_DEVICE_FROM_TRUSTED_SOURCE is translated to
169                         // ACTION_PROVISION_MANAGED_DEVICE
170                         .setProvisioningAction(ACTION_PROVISION_MANAGED_DEVICE)
171                         .setDeviceAdminComponentName(TEST_COMPONENT_NAME)
172                         .setDeviceAdminDownloadInfo(TEST_DOWNLOAD_INFO)
173                         .setLocalTime(TEST_LOCAL_TIME)
174                         .setLocale(TEST_LOCALE)
175                         .setTimeZone(TEST_TIME_ZONE)
176                         // THEN customizable color is not supported.
177                         .setMainColor(ProvisioningParams.DEFAULT_MAIN_COLOR)
178                         // THEN the trusted source is set to true.
179                         .setStartedByTrustedSource(true)
180                         .setSkipEncryption(TEST_SKIP_ENCRYPTION)
181                         .setWifiInfo(TEST_WIFI_INFO)
182                         .setAdminExtrasBundle(createTestAdminExtras())
183                         .setAccountToMigrate(TEST_ACCOUNT_TO_MIGRATE)
184                         .build(),
185                 params);
186     }
187 
testParse_resumeProvisioningIntent()188     public void testParse_resumeProvisioningIntent() throws Exception {
189         // GIVEN a resume provisioning intent which stores a device provisioning intent and other
190         // extras.
191         Intent intent = new Intent(Globals.ACTION_RESUME_PROVISIONING)
192                 .putExtra(EXTRA_PROVISIONING_ACTION, ACTION_PROVISION_MANAGED_DEVICE)
193                 .putExtra(EXTRA_PROVISIONING_DEVICE_ADMIN_COMPONENT_NAME, TEST_COMPONENT_NAME)
194                 .putExtra(EXTRA_PROVISIONING_DEVICE_ADMIN_PACKAGE_NAME, TEST_PACKAGE_NAME)
195                 .putExtras(getTestTimeTimeZoneAndLocaleExtras())
196                 .putExtras(getTestWifiInfoExtras())
197                 .putExtras(getTestDeviceAdminDownloadExtras())
198                 // GIVEN the package checksum support sha1 is set to true.
199                 .putExtra(EXTRA_PROVISIONING_DEVICE_ADMIN_SUPPORT_SHA1_PACKAGE_CHECKSUM, true)
200                 .putExtra(EXTRA_PROVISIONING_ADMIN_EXTRAS_BUNDLE, createTestAdminExtras())
201                 .putExtra(EXTRA_PROVISIONING_SKIP_ENCRYPTION, TEST_SKIP_ENCRYPTION)
202                 .putExtra(EXTRA_PROVISIONING_MAIN_COLOR, TEST_MAIN_COLOR)
203                 .putExtra(EXTRA_PROVISIONING_ACCOUNT_TO_MIGRATE, TEST_ACCOUNT_TO_MIGRATE)
204                 // GIVEN this original provisioning intent was started by a trusted source.
205                 .putExtra(EXTRA_PROVISIONING_STARTED_BY_TRUSTED_SOURCE, true);
206 
207         // WHEN the intent is parsed by the parser.
208         ProvisioningParams params = mExtrasProvisioningDataParser.parse(intent, mContext);
209 
210         // THEN ProvisionParams is constructed as expected.
211         assertEquals(
212                 ProvisioningParams.Builder.builder()
213                         // THEN provisioning action is restored to ACTION_PROVISION_MANAGED_DEVICE
214                         .setProvisioningAction(ACTION_PROVISION_MANAGED_DEVICE)
215                         .setDeviceAdminComponentName(TEST_COMPONENT_NAME)
216                         // THEN device admin package name is supported for resume intents
217                         .setDeviceAdminPackageName(TEST_PACKAGE_NAME)
218                         .setDeviceAdminDownloadInfo(
219                                 PackageDownloadInfo.Builder.builder()
220                                         .setLocation(TEST_DOWNLOAD_LOCATION)
221                                         .setCookieHeader(TEST_COOKIE_HEADER)
222                                         .setPackageChecksum(TEST_PACKAGE_CHECKSUM)
223                                         .setSignatureChecksum(TEST_SIGNATURE_CHECKSUM)
224                                         .setMinVersion(TEST_MIN_SUPPORT_VERSION)
225                                         // THEN the package checksum support sha1 is set to true.
226                                         .setPackageChecksumSupportsSha1(true)
227                                         .build())
228                         .setLocalTime(TEST_LOCAL_TIME)
229                         .setLocale(TEST_LOCALE)
230                         .setTimeZone(TEST_TIME_ZONE)
231                         .setMainColor(TEST_MAIN_COLOR)
232                         // THEN the trusted source is set to true.
233                         .setStartedByTrustedSource(true)
234                         .setSkipEncryption(TEST_SKIP_ENCRYPTION)
235                         .setWifiInfo(TEST_WIFI_INFO)
236                         .setAdminExtrasBundle(createTestAdminExtras())
237                         .setAccountToMigrate(TEST_ACCOUNT_TO_MIGRATE)
238                         .build(),
239                 params);
240     }
241 
testParse_managedProfileIntent()242     public void testParse_managedProfileIntent() throws Exception {
243         // GIVEN a managed profile provisioning intent and other extras.
244         Intent intent = new Intent(ACTION_PROVISION_MANAGED_PROFILE)
245                 // GIVEN a device admin package name and component name
246                 .putExtra(EXTRA_PROVISIONING_DEVICE_ADMIN_PACKAGE_NAME, TEST_PACKAGE_NAME)
247                 .putExtra(EXTRA_PROVISIONING_DEVICE_ADMIN_COMPONENT_NAME, TEST_COMPONENT_NAME)
248                 .putExtras(getTestTimeTimeZoneAndLocaleExtras())
249                 .putExtras(getTestWifiInfoExtras())
250                 .putExtras(getTestDeviceAdminDownloadExtras())
251                 .putExtra(EXTRA_PROVISIONING_ADMIN_EXTRAS_BUNDLE, createTestAdminExtras())
252                 .putExtra(EXTRA_PROVISIONING_SKIP_ENCRYPTION, TEST_SKIP_ENCRYPTION)
253                 .putExtra(EXTRA_PROVISIONING_MAIN_COLOR, TEST_MAIN_COLOR)
254                 .putExtra(EXTRA_PROVISIONING_ACCOUNT_TO_MIGRATE, TEST_ACCOUNT_TO_MIGRATE);
255 
256         // GIVEN the device admin is installed.
257         doReturn(TEST_COMPONENT_NAME)
258                 .when(mUtils)
259                 .findDeviceAdmin(TEST_PACKAGE_NAME, TEST_COMPONENT_NAME, mContext);
260 
261         // WHEN the intent is parsed by the parser.
262         ProvisioningParams params = mExtrasProvisioningDataParser.parse(intent, mContext);
263 
264         // THEN ProvisionParams is constructed as expected.
265         assertEquals(
266                 ProvisioningParams.Builder.builder()
267                         // THEN provisioning action is ACTION_PROVISION_MANAGED_PROFILE
268                         .setProvisioningAction(ACTION_PROVISION_MANAGED_PROFILE)
269                         .setDeviceAdminComponentName(TEST_COMPONENT_NAME)
270                         // THEN device admin package name is not supported.
271                         .setDeviceAdminPackageName(null)
272                         // THEN device admin download info is not supported.
273                         .setDeviceAdminDownloadInfo(null)
274                         // THEN wifi info is not supported.
275                         .setWifiInfo(null)
276                         .setMainColor(TEST_MAIN_COLOR)
277                         .setSkipEncryption(TEST_SKIP_ENCRYPTION)
278                         .setAdminExtrasBundle(createTestAdminExtras())
279                         .setAccountToMigrate(TEST_ACCOUNT_TO_MIGRATE)
280                         .build(),
281                 params);
282     }
283 
testParse_managedUserIntent()284     public void testParse_managedUserIntent() throws Exception {
285         // GIVEN a managed user provisioning intent and other extras.
286         Intent intent = new Intent(ACTION_PROVISION_MANAGED_USER)
287                 // GIVEN a device admin package name and component name
288                 .putExtra(EXTRA_PROVISIONING_DEVICE_ADMIN_PACKAGE_NAME, TEST_PACKAGE_NAME)
289                 .putExtra(EXTRA_PROVISIONING_DEVICE_ADMIN_COMPONENT_NAME, TEST_COMPONENT_NAME)
290                 .putExtras(getTestTimeTimeZoneAndLocaleExtras())
291                 .putExtras(getTestWifiInfoExtras())
292                 .putExtras(getTestDeviceAdminDownloadExtras())
293                 .putExtra(EXTRA_PROVISIONING_ADMIN_EXTRAS_BUNDLE, createTestAdminExtras())
294                 .putExtra(EXTRA_PROVISIONING_SKIP_ENCRYPTION, TEST_SKIP_ENCRYPTION)
295                 .putExtra(EXTRA_PROVISIONING_MAIN_COLOR, TEST_MAIN_COLOR)
296                 .putExtra(EXTRA_PROVISIONING_ACCOUNT_TO_MIGRATE, TEST_ACCOUNT_TO_MIGRATE);
297 
298         // WHEN the intent is parsed by the parser.
299         ProvisioningParams params = mExtrasProvisioningDataParser.parse(intent, mContext);
300 
301         // THEN ProvisionParams is constructed as expected.
302         assertEquals(
303                 ProvisioningParams.Builder.builder()
304                         // THEN provisioning action is ACTION_PROVISION_MANAGED_USER
305                         .setProvisioningAction(ACTION_PROVISION_MANAGED_USER)
306                         .setDeviceAdminComponentName(TEST_COMPONENT_NAME)
307                         // THEN device admin package name is not supported in Managed User
308                         // provisioning.
309                         .setDeviceAdminPackageName(null)
310                         // THEN device admin download info is not supported.
311                         .setDeviceAdminDownloadInfo(null)
312                         // THEN wifi info is not supported.
313                         .setWifiInfo(null)
314                         .setMainColor(TEST_MAIN_COLOR)
315                         .setSkipEncryption(TEST_SKIP_ENCRYPTION)
316                         .setAdminExtrasBundle(createTestAdminExtras())
317                         .setAccountToMigrate(TEST_ACCOUNT_TO_MIGRATE)
318                         .build(),
319                 params);
320     }
321 
testParse_managedDeviceIntent()322     public void testParse_managedDeviceIntent() throws Exception {
323         // GIVEN a managed device provisioning intent and other extras.
324         Intent intent = new Intent(ACTION_PROVISION_MANAGED_DEVICE)
325                 // GIVEN a device admin package name and component name
326                 .putExtra(EXTRA_PROVISIONING_DEVICE_ADMIN_PACKAGE_NAME, TEST_PACKAGE_NAME)
327                 .putExtra(EXTRA_PROVISIONING_DEVICE_ADMIN_COMPONENT_NAME, TEST_COMPONENT_NAME)
328                 .putExtras(getTestTimeTimeZoneAndLocaleExtras())
329                 .putExtras(getTestWifiInfoExtras())
330                 .putExtras(getTestDeviceAdminDownloadExtras())
331                 .putExtra(EXTRA_PROVISIONING_ADMIN_EXTRAS_BUNDLE, createTestAdminExtras())
332                 .putExtra(EXTRA_PROVISIONING_SKIP_ENCRYPTION, TEST_SKIP_ENCRYPTION)
333                 .putExtra(EXTRA_PROVISIONING_MAIN_COLOR, TEST_MAIN_COLOR)
334                 .putExtra(EXTRA_PROVISIONING_ACCOUNT_TO_MIGRATE, TEST_ACCOUNT_TO_MIGRATE);
335 
336         // WHEN the intent is parsed by the parser.
337         ProvisioningParams params = mExtrasProvisioningDataParser.parse(intent, mContext);
338 
339         // THEN ProvisionParams is constructed as expected.
340         assertEquals(
341                 ProvisioningParams.Builder.builder()
342                         // THEN provisioning action is ACTION_PROVISION_MANAGED_DEVICE
343                         .setProvisioningAction(ACTION_PROVISION_MANAGED_DEVICE)
344                         .setDeviceAdminComponentName(TEST_COMPONENT_NAME)
345                         // THEN device admin package name is not supported in Device Owner
346                         // provisioning.
347                         .setDeviceAdminPackageName(null)
348                         // THEN Device Admin download info is not supported.
349                         .setDeviceAdminDownloadInfo(null)
350                         // THEN time, time zone and locale are not supported.
351                         .setMainColor(TEST_MAIN_COLOR)
352                         .setSkipEncryption(TEST_SKIP_ENCRYPTION)
353                         // THEN wifi configuration is not supported.
354                         .setWifiInfo(null)
355                         .setAdminExtrasBundle(createTestAdminExtras())
356                         .setAccountToMigrate(TEST_ACCOUNT_TO_MIGRATE)
357                         .build(),
358                 params);
359     }
360 
testParse_managedSharableDeviceIntent()361     public void testParse_managedSharableDeviceIntent() throws Exception {
362         // GIVEN a managed device provisioning intent and other extras.
363         Intent intent = new Intent(ACTION_PROVISION_MANAGED_SHAREABLE_DEVICE)
364                 // GIVEN a device admin package name and component name
365                 .putExtra(EXTRA_PROVISIONING_DEVICE_ADMIN_PACKAGE_NAME, TEST_PACKAGE_NAME)
366                 .putExtra(EXTRA_PROVISIONING_DEVICE_ADMIN_COMPONENT_NAME, TEST_COMPONENT_NAME)
367                 .putExtras(getTestTimeTimeZoneAndLocaleExtras())
368                 .putExtras(getTestWifiInfoExtras())
369                 .putExtras(getTestDeviceAdminDownloadExtras())
370                 .putExtra(EXTRA_PROVISIONING_ADMIN_EXTRAS_BUNDLE, createTestAdminExtras())
371                 .putExtra(EXTRA_PROVISIONING_SKIP_ENCRYPTION, TEST_SKIP_ENCRYPTION)
372                 .putExtra(EXTRA_PROVISIONING_MAIN_COLOR, TEST_MAIN_COLOR)
373                 .putExtra(EXTRA_PROVISIONING_ACCOUNT_TO_MIGRATE, TEST_ACCOUNT_TO_MIGRATE);
374 
375         // WHEN the intent is parsed by the parser.
376         ProvisioningParams params = mExtrasProvisioningDataParser.parse(intent, mContext);
377 
378         // THEN ProvisionParams is constructed as expected.
379         assertEquals(
380                 ProvisioningParams.Builder.builder()
381                         // THEN provisioning action is ACTION_PROVISION_MANAGED_SHAREABLE_DEVICE
382                         .setProvisioningAction(ACTION_PROVISION_MANAGED_SHAREABLE_DEVICE)
383                         .setDeviceAdminComponentName(TEST_COMPONENT_NAME)
384                         // THEN device admin package name is not supported in Device Owner
385                         // provisioning.
386                         .setDeviceAdminPackageName(null)
387                         // THEN Device Admin download info is not supported.
388                         .setDeviceAdminDownloadInfo(null)
389                         // THEN time, time zone and locale are not supported.
390                         .setMainColor(TEST_MAIN_COLOR)
391                         .setSkipEncryption(TEST_SKIP_ENCRYPTION)
392                         // THEN wifi configuration is not supported.
393                         .setWifiInfo(null)
394                         .setAdminExtrasBundle(createTestAdminExtras())
395                         .setAccountToMigrate(TEST_ACCOUNT_TO_MIGRATE)
396                         .build(),
397                 params);
398     }
399 
testParse_nfcProvisioningIntentThrowsException()400     public void testParse_nfcProvisioningIntentThrowsException() {
401         // GIVEN a NFC provisioning intent and other extras.
402         Intent intent = new Intent(ACTION_NDEF_DISCOVERED)
403                 // GIVEN a device admin package name and component name
404                 .putExtra(EXTRA_PROVISIONING_DEVICE_ADMIN_PACKAGE_NAME, TEST_PACKAGE_NAME)
405                 .putExtra(EXTRA_PROVISIONING_DEVICE_ADMIN_COMPONENT_NAME, TEST_COMPONENT_NAME)
406                 .putExtras(getTestTimeTimeZoneAndLocaleExtras())
407                 .putExtras(getTestWifiInfoExtras())
408                 .putExtras(getTestDeviceAdminDownloadExtras())
409                 .putExtra(EXTRA_PROVISIONING_ADMIN_EXTRAS_BUNDLE, createTestAdminExtras())
410                 .putExtra(EXTRA_PROVISIONING_SKIP_ENCRYPTION, TEST_SKIP_ENCRYPTION)
411                 .putExtra(EXTRA_PROVISIONING_MAIN_COLOR, TEST_MAIN_COLOR)
412                 .putExtra(EXTRA_PROVISIONING_ACCOUNT_TO_MIGRATE, TEST_ACCOUNT_TO_MIGRATE);
413 
414         try {
415             // WHEN the intent is parsed by the parser.
416             ProvisioningParams params = mExtrasProvisioningDataParser.parse(intent, mContext);
417             fail("ExtrasProvisioningDataParser doesn't support NFC intent. "
418                     + "IllegalProvisioningArgumentException should be thrown");
419         } catch (IllegalProvisioningArgumentException e) {
420             // THEN IllegalProvisioningArgumentException is thrown.
421         }
422     }
423 
getTestWifiInfoExtras()424     private static Bundle getTestWifiInfoExtras() {
425         Bundle wifiInfoExtras = new Bundle();
426         wifiInfoExtras.putString(EXTRA_PROVISIONING_WIFI_SSID, TEST_SSID);
427         wifiInfoExtras.putString(EXTRA_PROVISIONING_WIFI_SECURITY_TYPE, TEST_SECURITY_TYPE);
428         wifiInfoExtras.putString(EXTRA_PROVISIONING_WIFI_PASSWORD, TEST_PASSWORD);
429         wifiInfoExtras.putString(EXTRA_PROVISIONING_WIFI_PROXY_HOST, TEST_PROXY_HOST);
430         wifiInfoExtras.putString(EXTRA_PROVISIONING_WIFI_PROXY_BYPASS, TEST_PROXY_BYPASS_HOSTS);
431         wifiInfoExtras.putString(EXTRA_PROVISIONING_WIFI_PAC_URL, TEST_PAC_URL);
432         wifiInfoExtras.putInt(EXTRA_PROVISIONING_WIFI_PROXY_PORT, TEST_PROXY_PORT);
433         wifiInfoExtras.putBoolean(EXTRA_PROVISIONING_WIFI_HIDDEN, TEST_HIDDEN);
434         return wifiInfoExtras;
435     }
436 
getTestTimeTimeZoneAndLocaleExtras()437     private static Bundle getTestTimeTimeZoneAndLocaleExtras() {
438         Bundle timeTimezoneAndLocaleExtras = new Bundle();
439         timeTimezoneAndLocaleExtras.putLong(EXTRA_PROVISIONING_LOCAL_TIME, TEST_LOCAL_TIME);
440         timeTimezoneAndLocaleExtras.putString(EXTRA_PROVISIONING_TIME_ZONE, TEST_TIME_ZONE);
441         timeTimezoneAndLocaleExtras.putString(
442                 EXTRA_PROVISIONING_LOCALE, MessageParser.localeToString(TEST_LOCALE));
443         return timeTimezoneAndLocaleExtras;
444     }
445 
getTestDeviceAdminDownloadExtras()446     private static Bundle getTestDeviceAdminDownloadExtras() {
447         Bundle downloadInfoExtras = new Bundle();
448         downloadInfoExtras.putInt(
449                 EXTRA_PROVISIONING_DEVICE_ADMIN_MINIMUM_VERSION_CODE, TEST_MIN_SUPPORT_VERSION);
450         downloadInfoExtras.putString(
451                 EXTRA_PROVISIONING_DEVICE_ADMIN_PACKAGE_DOWNLOAD_LOCATION, TEST_DOWNLOAD_LOCATION);
452         downloadInfoExtras.putString(
453                 EXTRA_PROVISIONING_DEVICE_ADMIN_PACKAGE_DOWNLOAD_COOKIE_HEADER, TEST_COOKIE_HEADER);
454         downloadInfoExtras.putString(
455                 EXTRA_PROVISIONING_DEVICE_ADMIN_PACKAGE_CHECKSUM,
456                 Base64.encodeToString(TEST_PACKAGE_CHECKSUM,
457                         Base64.URL_SAFE | Base64.NO_PADDING | Base64.NO_WRAP));
458         downloadInfoExtras.putString(
459                 EXTRA_PROVISIONING_DEVICE_ADMIN_SIGNATURE_CHECKSUM,
460                 Base64.encodeToString(TEST_SIGNATURE_CHECKSUM,
461                         Base64.URL_SAFE | Base64.NO_PADDING | Base64.NO_WRAP));
462         return downloadInfoExtras;
463     }
464 }
465