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.EXTRA_PROVISIONING_ACCOUNT_TO_MIGRATE;
20 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_ADMIN_EXTRAS_BUNDLE;
21 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_DEVICE_ADMIN_COMPONENT_NAME;
22 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_DEVICE_ADMIN_MINIMUM_VERSION_CODE;
23 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_DEVICE_ADMIN_PACKAGE_CHECKSUM;
24 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_DEVICE_ADMIN_PACKAGE_DOWNLOAD_COOKIE_HEADER;
25 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_DEVICE_ADMIN_PACKAGE_DOWNLOAD_LOCATION;
26 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_DEVICE_ADMIN_PACKAGE_ICON_URI;
27 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_DEVICE_ADMIN_PACKAGE_LABEL;
28 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_DEVICE_ADMIN_PACKAGE_NAME;
29 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_DEVICE_ADMIN_SIGNATURE_CHECKSUM;
30 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_DISCLAIMERS;
31 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_DISCLAIMER_CONTENT;
32 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_DISCLAIMER_HEADER;
33 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_KEEP_ACCOUNT_ON_MIGRATION;
34 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_LEAVE_ALL_SYSTEM_APPS_ENABLED;
35 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_LOCALE;
36 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_LOCAL_TIME;
37 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_LOGO_URI;
38 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_MAIN_COLOR;
39 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_ORGANIZATION_NAME;
40 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_SKIP_ENCRYPTION;
41 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_SKIP_USER_CONSENT;
42 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_SKIP_USER_SETUP;
43 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_SUPPORT_URL;
44 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_TIME_ZONE;
45 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_USE_MOBILE_DATA;
46 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_WIFI_ANONYMOUS_IDENTITY;
47 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_WIFI_CA_CERTIFICATE;
48 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_WIFI_DOMAIN;
49 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_WIFI_EAP_METHOD;
50 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_WIFI_HIDDEN;
51 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_WIFI_IDENTITY;
52 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_WIFI_PAC_URL;
53 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_WIFI_PASSWORD;
54 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_WIFI_PHASE2_AUTH;
55 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_WIFI_PROXY_BYPASS;
56 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_WIFI_PROXY_HOST;
57 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_WIFI_PROXY_PORT;
58 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_WIFI_SECURITY_TYPE;
59 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_WIFI_SSID;
60 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_WIFI_USER_CERTIFICATE;
61 import static android.app.admin.DevicePolicyManager.MIME_TYPE_PROVISIONING_NFC;
62 
63 import static com.android.managedprovisioning.parser.ExtrasProvisioningDataParser.EXTRA_PROVISIONING_ACCOUNT_TO_MIGRATE_SHORT;
64 import static com.android.managedprovisioning.parser.ExtrasProvisioningDataParser.EXTRA_PROVISIONING_ADMIN_EXTRAS_BUNDLE_SHORT;
65 import static com.android.managedprovisioning.parser.ExtrasProvisioningDataParser.EXTRA_PROVISIONING_DEVICE_ADMIN_COMPONENT_NAME_SHORT;
66 import static com.android.managedprovisioning.parser.ExtrasProvisioningDataParser.EXTRA_PROVISIONING_DEVICE_ADMIN_MINIMUM_VERSION_CODE_SHORT;
67 import static com.android.managedprovisioning.parser.ExtrasProvisioningDataParser.EXTRA_PROVISIONING_DEVICE_ADMIN_PACKAGE_CHECKSUM_SHORT;
68 import static com.android.managedprovisioning.parser.ExtrasProvisioningDataParser.EXTRA_PROVISIONING_DEVICE_ADMIN_PACKAGE_DOWNLOAD_COOKIE_HEADER_SHORT;
69 import static com.android.managedprovisioning.parser.ExtrasProvisioningDataParser.EXTRA_PROVISIONING_DEVICE_ADMIN_PACKAGE_DOWNLOAD_LOCATION_SHORT;
70 import static com.android.managedprovisioning.parser.ExtrasProvisioningDataParser.EXTRA_PROVISIONING_DEVICE_ADMIN_PACKAGE_ICON_URI_SHORT;
71 import static com.android.managedprovisioning.parser.ExtrasProvisioningDataParser.EXTRA_PROVISIONING_DEVICE_ADMIN_PACKAGE_LABEL_SHORT;
72 import static com.android.managedprovisioning.parser.ExtrasProvisioningDataParser.EXTRA_PROVISIONING_DEVICE_ADMIN_PACKAGE_NAME_SHORT;
73 import static com.android.managedprovisioning.parser.ExtrasProvisioningDataParser.EXTRA_PROVISIONING_DEVICE_ADMIN_SIGNATURE_CHECKSUM_SHORT;
74 import static com.android.managedprovisioning.parser.ExtrasProvisioningDataParser.EXTRA_PROVISIONING_DISCLAIMERS_SHORT;
75 import static com.android.managedprovisioning.parser.ExtrasProvisioningDataParser.EXTRA_PROVISIONING_DISCLAIMER_CONTENT_SHORT;
76 import static com.android.managedprovisioning.parser.ExtrasProvisioningDataParser.EXTRA_PROVISIONING_DISCLAIMER_HEADER_SHORT;
77 import static com.android.managedprovisioning.parser.ExtrasProvisioningDataParser.EXTRA_PROVISIONING_KEEP_ACCOUNT_ON_MIGRATION_SHORT;
78 import static com.android.managedprovisioning.parser.ExtrasProvisioningDataParser.EXTRA_PROVISIONING_LEAVE_ALL_SYSTEM_APPS_ENABLED_SHORT;
79 import static com.android.managedprovisioning.parser.ExtrasProvisioningDataParser.EXTRA_PROVISIONING_LOCALE_SHORT;
80 import static com.android.managedprovisioning.parser.ExtrasProvisioningDataParser.EXTRA_PROVISIONING_LOCAL_TIME_SHORT;
81 import static com.android.managedprovisioning.parser.ExtrasProvisioningDataParser.EXTRA_PROVISIONING_LOGO_URI_SHORT;
82 import static com.android.managedprovisioning.parser.ExtrasProvisioningDataParser.EXTRA_PROVISIONING_MAIN_COLOR_SHORT;
83 import static com.android.managedprovisioning.parser.ExtrasProvisioningDataParser.EXTRA_PROVISIONING_ORGANIZATION_NAME_SHORT;
84 import static com.android.managedprovisioning.parser.ExtrasProvisioningDataParser.EXTRA_PROVISIONING_SKIP_ENCRYPTION_SHORT;
85 import static com.android.managedprovisioning.parser.ExtrasProvisioningDataParser.EXTRA_PROVISIONING_SKIP_USER_CONSENT_SHORT;
86 import static com.android.managedprovisioning.parser.ExtrasProvisioningDataParser.EXTRA_PROVISIONING_SKIP_USER_SETUP_SHORT;
87 import static com.android.managedprovisioning.parser.ExtrasProvisioningDataParser.EXTRA_PROVISIONING_SUPPORT_URL_SHORT;
88 import static com.android.managedprovisioning.parser.ExtrasProvisioningDataParser.EXTRA_PROVISIONING_TIME_ZONE_SHORT;
89 import static com.android.managedprovisioning.parser.ExtrasProvisioningDataParser.EXTRA_PROVISIONING_USE_MOBILE_DATA_SHORT;
90 import static com.android.managedprovisioning.parser.ExtrasProvisioningDataParser.EXTRA_PROVISIONING_WIFI_ANONYMOUS_IDENTITY_SHORT;
91 import static com.android.managedprovisioning.parser.ExtrasProvisioningDataParser.EXTRA_PROVISIONING_WIFI_CA_CERTIFICATE_SHORT;
92 import static com.android.managedprovisioning.parser.ExtrasProvisioningDataParser.EXTRA_PROVISIONING_WIFI_DOMAIN_SHORT;
93 import static com.android.managedprovisioning.parser.ExtrasProvisioningDataParser.EXTRA_PROVISIONING_WIFI_EAP_METHOD_SHORT;
94 import static com.android.managedprovisioning.parser.ExtrasProvisioningDataParser.EXTRA_PROVISIONING_WIFI_HIDDEN_SHORT;
95 import static com.android.managedprovisioning.parser.ExtrasProvisioningDataParser.EXTRA_PROVISIONING_WIFI_IDENTITY_SHORT;
96 import static com.android.managedprovisioning.parser.ExtrasProvisioningDataParser.EXTRA_PROVISIONING_WIFI_PAC_URL_SHORT;
97 import static com.android.managedprovisioning.parser.ExtrasProvisioningDataParser.EXTRA_PROVISIONING_WIFI_PASSWORD_SHORT;
98 import static com.android.managedprovisioning.parser.ExtrasProvisioningDataParser.EXTRA_PROVISIONING_WIFI_PHASE2_AUTH_SHORT;
99 import static com.android.managedprovisioning.parser.ExtrasProvisioningDataParser.EXTRA_PROVISIONING_WIFI_PROXY_BYPASS_SHORT;
100 import static com.android.managedprovisioning.parser.ExtrasProvisioningDataParser.EXTRA_PROVISIONING_WIFI_PROXY_HOST_SHORT;
101 import static com.android.managedprovisioning.parser.ExtrasProvisioningDataParser.EXTRA_PROVISIONING_WIFI_PROXY_PORT_SHORT;
102 import static com.android.managedprovisioning.parser.ExtrasProvisioningDataParser.EXTRA_PROVISIONING_WIFI_SECURITY_TYPE_SHORT;
103 import static com.android.managedprovisioning.parser.ExtrasProvisioningDataParser.EXTRA_PROVISIONING_WIFI_SSID_SHORT;
104 import static com.android.managedprovisioning.parser.ExtrasProvisioningDataParser.EXTRA_PROVISIONING_WIFI_USER_CERTIFICATE_SHORT;
105 import static com.google.common.truth.Truth.assertThat;
106 
107 import static org.mockito.Mockito.when;
108 
109 import android.app.admin.DevicePolicyManager;
110 import android.content.ComponentName;
111 import android.content.Context;
112 import android.content.Intent;
113 import android.net.Uri;
114 import android.nfc.NdefMessage;
115 import android.nfc.NdefRecord;
116 import android.nfc.NfcAdapter;
117 import android.os.Bundle;
118 import android.os.Parcelable;
119 import android.os.PersistableBundle;
120 import android.test.AndroidTestCase;
121 import android.util.Base64;
122 
123 import androidx.test.filters.SmallTest;
124 
125 import com.android.managedprovisioning.common.IllegalProvisioningArgumentException;
126 import com.android.managedprovisioning.common.ManagedProvisioningSharedPreferences;
127 import com.android.managedprovisioning.common.StoreUtils;
128 import com.android.managedprovisioning.common.Utils;
129 import com.android.managedprovisioning.model.PackageDownloadInfo;
130 import com.android.managedprovisioning.model.ProvisioningParams;
131 import com.android.managedprovisioning.model.WifiInfo;
132 
133 import org.mockito.Mock;
134 import org.mockito.MockitoAnnotations;
135 
136 import java.io.ByteArrayOutputStream;
137 import java.io.IOException;
138 import java.util.Locale;
139 import java.util.Properties;
140 
141 /** Tests for {@link PropertiesProvisioningDataParser} */
142 @SmallTest
143 public class PropertiesProvisioningDataParserTest extends AndroidTestCase {
144     private static final String TEST_PACKAGE_NAME = "com.afwsamples.testdpc";
145     private static final ComponentName TEST_COMPONENT_NAME =
146             ComponentName.unflattenFromString(
147                     "com.afwsamples.testdpc/com.afwsamples.testdpc.DeviceAdminReceiver");
148     private static final long TEST_LOCAL_TIME = 1456939524713L;
149     private static final Locale TEST_LOCALE = Locale.UK;
150     private static final String TEST_TIME_ZONE = "GMT";
151     private static final Integer TEST_MAIN_COLOR = 65280;
152     private static final boolean TEST_STARTED_BY_TRUSTED_SOURCE = true;
153     private static final String TEST_LEAVE_ALL_SYSTEM_APP_ENABLED = "true";
154     private static final boolean TEST_SKIP_ENCRYPTION = true;
155     private static final boolean TEST_USE_MOBILE_DATA = false;
156     private static final boolean TEST_SKIP_USER_SETUP = true;
157     private static final long TEST_PROVISIONING_ID = 2000L;
158     private static final String TEST_ACCOUNT_TO_MIGRATE ="user@gmail.com";
159     private static final String INVALID_MIME_TYPE = "invalid-mime-type";
160 
161     // Wifi info
162     private static final String TEST_SSID = "TestWifi";
163     private static final boolean TEST_HIDDEN = true;
164     private static final String TEST_SECURITY_TYPE = "WPA2";
165     private static final String TEST_SECURITY_TYPE_EAP = "EAP";
166     private static final String TEST_PASSWORD = "GoogleRock";
167     private static final String TEST_EAP_METHOD = "TTLS";
168     private static final String TEST_PHASE2_AUTH = "PAP";
169     private static final String TEST_CA_CERT = "caCertificate";
170     private static final String TEST_USER_CERT = "userCertificate";
171     private static final String TEST_IDENTITY = "TestUser";
172     private static final String TEST_ANONYMOUS_IDENTITY = "TestAUser";
173     private static final String TEST_DOMAIN = "google.com";
174     private static final String TEST_PROXY_HOST = "testhost.com";
175     private static final int TEST_PROXY_PORT = 7689;
176     private static final String TEST_PROXY_BYPASS_HOSTS = "http://host1.com;https://host2.com";
177     private static final String TEST_PAC_URL = "pac.test.com";
178     private static final WifiInfo TEST_WIFI_INFO = WifiInfo.Builder.builder()
179             .setSsid(TEST_SSID)
180             .setHidden(TEST_HIDDEN)
181             .setSecurityType(TEST_SECURITY_TYPE)
182             .setPassword(TEST_PASSWORD)
183             .setProxyHost(TEST_PROXY_HOST)
184             .setProxyPort(TEST_PROXY_PORT)
185             .setProxyBypassHosts(TEST_PROXY_BYPASS_HOSTS)
186             .setPacUrl(TEST_PAC_URL)
187             .build();
188 
189     // Device admin package download info
190     private static final String TEST_DOWNLOAD_LOCATION =
191             "http://example/dpc.apk";
192     private static final String TEST_COOKIE_HEADER =
193             "Set-Cookie: sessionToken=foobar; Expires=Thu, 18 Feb 2016 23:59:59 GMT";
194     private static final byte[] TEST_PACKAGE_CHECKSUM = new byte[] { '1', '2', '3', '4', '5' };
195     private static final byte[] TEST_SIGNATURE_CHECKSUM = new byte[] { '5', '4', '3', '2', '1' };
196     private static final int TEST_MIN_SUPPORT_VERSION = 17689;
197     private static final PackageDownloadInfo TEST_DOWNLOAD_INFO =
198             PackageDownloadInfo.Builder.builder()
199                     .setLocation(TEST_DOWNLOAD_LOCATION)
200                     .setCookieHeader(TEST_COOKIE_HEADER)
201                     .setPackageChecksum(TEST_PACKAGE_CHECKSUM)
202                     .setSignatureChecksum(TEST_SIGNATURE_CHECKSUM)
203                     .setMinVersion(TEST_MIN_SUPPORT_VERSION)
204                     .build();
205     private static final String TEST_KEEP_ACCOUNT_MIGRATED = "true";
206     private static final String TEST_ORGANIZATION_NAME = "TestOrganizationName";
207     private static final String TEST_SUPPORT_URL = "https://www.support.url/";
208     private static final String TEST_DEVICE_ADMIN_PACKAGE_LABEL = "TestPackage";
209     private static final String TEST_SKIP_USER_CONSENT = "true";
210     private static final Uri TEST_URI = Uri.parse("https://www.google.com/");
211     private static final String TEST_URI_STRING = "https://www.google.com/";
212     private static final String TEST_DISCLAMER_HEADER = "Google";
213 
214     @Mock
215     private Context mContext;
216 
217     @Mock
218     private ManagedProvisioningSharedPreferences mSharedPreferences;
219 
220     private PropertiesProvisioningDataParser mPropertiesProvisioningDataParser;
221 
222     @Override
setUp()223     public void setUp() {
224         // this is necessary for mockito to work
225         System.setProperty("dexmaker.dexcache", getContext().getCacheDir().toString());
226 
227         MockitoAnnotations.initMocks(this);
228 
229         when(mSharedPreferences.incrementAndGetProvisioningId()).thenReturn(TEST_PROVISIONING_ID);
230         mPropertiesProvisioningDataParser = new PropertiesProvisioningDataParser(mContext,
231                 new Utils(), mSharedPreferences);
232     }
233 
234     // TODO(alexkershaw): split this huge test into individual tests using
235     // buildNfcProvisioningProperties and buildNfcProvisioningIntent.
testParse_nfcProvisioningIntent()236     public void testParse_nfcProvisioningIntent() throws Exception {
237         // GIVEN a NFC provisioning intent with all supported data.
238         Properties props = new Properties();
239         props.setProperty(EXTRA_PROVISIONING_DEVICE_ADMIN_PACKAGE_NAME, TEST_PACKAGE_NAME);
240         props.setProperty(
241                 EXTRA_PROVISIONING_DEVICE_ADMIN_COMPONENT_NAME,
242                 TEST_COMPONENT_NAME.flattenToString());
243         setTestWifiInfo(props);
244         setTestTimeTimeZoneAndLocale(props);
245         setTestDeviceAdminDownload(props);
246         props.setProperty(EXTRA_PROVISIONING_ADMIN_EXTRAS_BUNDLE, getTestAdminExtrasString());
247         props.setProperty(
248                 EXTRA_PROVISIONING_SKIP_ENCRYPTION,
249                 Boolean.toString(TEST_SKIP_ENCRYPTION));
250         // GIVEN main color is supplied even though it is not supported in NFC provisioning.
251         props.setProperty(EXTRA_PROVISIONING_MAIN_COLOR, Integer.toString(TEST_MAIN_COLOR));
252 
253         Intent intent = buildNfcProvisioningIntent(props);
254 
255         // WHEN the intent is parsed by the parser.
256         ProvisioningParams params = mPropertiesProvisioningDataParser.parse(intent);
257 
258 
259         // THEN ProvisionParams is constructed as expected.
260         assertThat(
261                 ProvisioningParams.Builder.builder()
262                         // THEN NFC provisioning is translated to ACTION_PROVISION_MANAGED_DEVICE
263                         // action.
264                         .setProvisioningAction(ACTION_PROVISION_MANAGED_DEVICE)
265                         .setDeviceAdminComponentName(TEST_COMPONENT_NAME)
266                         .setDeviceAdminPackageName(TEST_PACKAGE_NAME)
267                         .setProvisioningId(TEST_PROVISIONING_ID)
268                         .setDeviceAdminDownloadInfo(
269                                 PackageDownloadInfo.Builder.builder()
270                                         .setLocation(TEST_DOWNLOAD_LOCATION)
271                                         .setCookieHeader(TEST_COOKIE_HEADER)
272                                         .setPackageChecksum(TEST_PACKAGE_CHECKSUM)
273                                         .setSignatureChecksum(TEST_SIGNATURE_CHECKSUM)
274                                         .setMinVersion(TEST_MIN_SUPPORT_VERSION)
275                                         .build())
276                         .setLocalTime(TEST_LOCAL_TIME)
277                         .setLocale(TEST_LOCALE)
278                         .setTimeZone(TEST_TIME_ZONE)
279                         // THEN main color is not supported in NFC intent.
280                         .setMainColor(null)
281                         .setSkipEncryption(TEST_SKIP_ENCRYPTION)
282                         .setWifiInfo(TEST_WIFI_INFO)
283                         .setAdminExtrasBundle(getTestAdminExtrasPersistableBundle())
284                         .setStartedByTrustedSource(true)
285                         .setIsNfc(true)
286                         .setIsOrganizationOwnedProvisioning(true)
287                         .build())
288                 .isEqualTo(params);
289     }
290 
testParse_shortExtras_sameAsLongExtras()291     public void testParse_shortExtras_sameAsLongExtras() throws Exception {
292         assertThat(mPropertiesProvisioningDataParser.parse(buildIntentWithAllLongExtras()))
293             .isEqualTo(mPropertiesProvisioningDataParser.parse(buildIntentWithAllShortExtras()));
294     }
295 
testParse_nfcProvisioningIntent_setUseMobileDataTrue()296     public void testParse_nfcProvisioningIntent_setUseMobileDataTrue()
297             throws Exception {
298         Properties properties = buildNfcProvisioningProperties();
299         properties.setProperty(EXTRA_PROVISIONING_USE_MOBILE_DATA, Boolean.toString(true));
300         Intent intent = buildNfcProvisioningIntent(properties);
301 
302         assertThat(mPropertiesProvisioningDataParser.parse(intent).useMobileData).isTrue();
303     }
304 
testParse_nfcProvisioningIntent_setUseMobileDataFalse()305     public void testParse_nfcProvisioningIntent_setUseMobileDataFalse()
306             throws Exception {
307         Properties properties = buildNfcProvisioningProperties();
308         properties.setProperty(EXTRA_PROVISIONING_USE_MOBILE_DATA, Boolean.toString(false));
309         Intent intent = buildNfcProvisioningIntent(properties);
310 
311         assertThat(mPropertiesProvisioningDataParser.parse(intent).useMobileData).isFalse();
312     }
313 
testParse_nfcProvisioningIntent_useMobileDataDefaultsToFalse()314     public void testParse_nfcProvisioningIntent_useMobileDataDefaultsToFalse() throws Exception {
315         Properties properties = buildNfcProvisioningProperties();
316         properties.remove(EXTRA_PROVISIONING_USE_MOBILE_DATA);
317         Intent intent = buildNfcProvisioningIntent(properties);
318 
319         assertThat(mPropertiesProvisioningDataParser.parse(intent).useMobileData).isFalse();
320     }
321 
testParse_nfcProvisioning_wifiWithCertificates()322     public void testParse_nfcProvisioning_wifiWithCertificates() throws Exception {
323         Properties props = new Properties();
324         props.setProperty(
325                 EXTRA_PROVISIONING_DEVICE_ADMIN_COMPONENT_NAME,
326                 TEST_COMPONENT_NAME.flattenToString());
327         props.setProperty(EXTRA_PROVISIONING_WIFI_SSID, TEST_SSID);
328         props.setProperty(EXTRA_PROVISIONING_WIFI_HIDDEN, Boolean.toString(TEST_HIDDEN));
329         props.setProperty(EXTRA_PROVISIONING_WIFI_SECURITY_TYPE, TEST_SECURITY_TYPE_EAP);
330         props.setProperty(EXTRA_PROVISIONING_WIFI_PASSWORD, TEST_PASSWORD);
331         props.setProperty(EXTRA_PROVISIONING_WIFI_EAP_METHOD, TEST_EAP_METHOD);
332         props.setProperty(EXTRA_PROVISIONING_WIFI_PHASE2_AUTH, TEST_PHASE2_AUTH);
333         props.setProperty(EXTRA_PROVISIONING_WIFI_CA_CERTIFICATE, TEST_CA_CERT);
334         props.setProperty(EXTRA_PROVISIONING_WIFI_USER_CERTIFICATE, TEST_USER_CERT);
335         props.setProperty(EXTRA_PROVISIONING_WIFI_IDENTITY, TEST_IDENTITY);
336         props.setProperty(EXTRA_PROVISIONING_WIFI_ANONYMOUS_IDENTITY, TEST_ANONYMOUS_IDENTITY);
337         props.setProperty(EXTRA_PROVISIONING_WIFI_DOMAIN, TEST_DOMAIN);
338         props.setProperty(EXTRA_PROVISIONING_WIFI_PROXY_HOST, TEST_PROXY_HOST);
339         props.setProperty(EXTRA_PROVISIONING_WIFI_PROXY_PORT, Integer.toString(TEST_PROXY_PORT));
340         props.setProperty(EXTRA_PROVISIONING_WIFI_PROXY_BYPASS, TEST_PROXY_BYPASS_HOSTS);
341         props.setProperty(EXTRA_PROVISIONING_WIFI_PAC_URL, TEST_PAC_URL);
342         Intent intent = buildNfcProvisioningIntent(props);
343 
344         ProvisioningParams params = mPropertiesProvisioningDataParser.parse(intent);
345 
346         assertThat(params).isEqualTo(createTestProvisioningParamsBuilder()
347             .setDeviceAdminComponentName(TEST_COMPONENT_NAME)
348             .setWifiInfo(WifiInfo.Builder.builder()
349                     .setSsid(TEST_SSID)
350                     .setHidden(TEST_HIDDEN)
351                     .setSecurityType(TEST_SECURITY_TYPE_EAP)
352                     .setPassword(TEST_PASSWORD)
353                     .setEapMethod(TEST_EAP_METHOD)
354                     .setPhase2Auth(TEST_PHASE2_AUTH)
355                     .setCaCertificate(TEST_CA_CERT)
356                     .setUserCertificate(TEST_USER_CERT)
357                     .setIdentity(TEST_IDENTITY)
358                     .setAnonymousIdentity(TEST_ANONYMOUS_IDENTITY)
359                     .setDomain(TEST_DOMAIN)
360                     .setProxyHost(TEST_PROXY_HOST)
361                     .setProxyPort(TEST_PROXY_PORT)
362                     .setProxyBypassHosts(TEST_PROXY_BYPASS_HOSTS)
363                     .setPacUrl(TEST_PAC_URL)
364                     .build())
365             .build());
366     }
367 
createTestProvisioningParamsBuilder()368     private ProvisioningParams.Builder createTestProvisioningParamsBuilder() {
369         return ProvisioningParams.Builder.builder()
370                 .setProvisioningId(TEST_PROVISIONING_ID)
371                 .setProvisioningAction(ACTION_PROVISION_MANAGED_DEVICE)
372                 .setStartedByTrustedSource(true)
373                 .setIsNfc(true)
374                 .setIsOrganizationOwnedProvisioning(true);
375     }
376 
testParse_OtherIntentsThrowsException()377     public void testParse_OtherIntentsThrowsException() {
378         // GIVEN a managed device provisioning intent and some extras.
379         Intent intent = new Intent(ACTION_PROVISION_MANAGED_DEVICE)
380                 .putExtra(EXTRA_PROVISIONING_DEVICE_ADMIN_PACKAGE_NAME, TEST_PACKAGE_NAME)
381                 .putExtra(EXTRA_PROVISIONING_DEVICE_ADMIN_COMPONENT_NAME, TEST_COMPONENT_NAME)
382                 .putExtra(EXTRA_PROVISIONING_SKIP_ENCRYPTION, TEST_SKIP_ENCRYPTION)
383                 .putExtra(EXTRA_PROVISIONING_MAIN_COLOR, TEST_MAIN_COLOR)
384                 .putExtra(EXTRA_PROVISIONING_ACCOUNT_TO_MIGRATE, TEST_ACCOUNT_TO_MIGRATE);
385 
386         try {
387             // WHEN the intent is parsed by the parser.
388             ProvisioningParams params = mPropertiesProvisioningDataParser.parse(intent);
389             fail("PropertiesProvisioningDataParser doesn't support intent other than NFC. "
390                     + "IllegalProvisioningArgumentException should be thrown");
391         } catch (IllegalProvisioningArgumentException e) {
392             // THEN IllegalProvisioningArgumentException is thrown.
393         }
394     }
395 
testGetFirstNdefRecord_nullNdefMessages()396     public void testGetFirstNdefRecord_nullNdefMessages() {
397         // GIVEN nfc intent with no ndef messages
398         Intent nfcIntent = new Intent(NfcAdapter.ACTION_NDEF_DISCOVERED);
399         // WHEN getting first ndef record
400         // THEN it should return null
401         assertThat(PropertiesProvisioningDataParser.getFirstNdefRecord(nfcIntent)).isNull();
402     }
403 
testGetFirstNdefRecord_noNfcMimeType()404     public void testGetFirstNdefRecord_noNfcMimeType() {
405         // GIVEN nfc intent with no ndf message with a record with a valid mime type.
406         ByteArrayOutputStream stream = new ByteArrayOutputStream();
407         NdefRecord record = NdefRecord.createMime(INVALID_MIME_TYPE, stream.toByteArray());
408         NdefMessage ndfMsg = new NdefMessage(new NdefRecord[]{record});
409         Intent nfcIntent = new Intent(NfcAdapter.ACTION_NDEF_DISCOVERED)
410                 .setType(MIME_TYPE_PROVISIONING_NFC)
411                 .putExtra(NfcAdapter.EXTRA_NDEF_MESSAGES, new NdefMessage[]{ndfMsg});
412         // WHEN getting first ndef record
413         // THEN it should return null
414         assertThat(PropertiesProvisioningDataParser.getFirstNdefRecord(nfcIntent)).isNull();
415     }
416 
testGetFirstNdefRecord()417     public void testGetFirstNdefRecord() {
418         // GIVEN nfc intent with valid ndf message with a record with mime type nfc.
419         ByteArrayOutputStream stream = new ByteArrayOutputStream();
420         NdefRecord record = NdefRecord.createMime(
421                 DevicePolicyManager.MIME_TYPE_PROVISIONING_NFC,
422                 stream.toByteArray());
423         NdefMessage ndfMsg = new NdefMessage(new NdefRecord[]{record});
424         Intent nfcIntent = new Intent(NfcAdapter.ACTION_NDEF_DISCOVERED)
425                 .setType(MIME_TYPE_PROVISIONING_NFC)
426                 .putExtra(NfcAdapter.EXTRA_NDEF_MESSAGES, new NdefMessage[]{ndfMsg});
427         // WHEN getting first ndef record
428         // THEN valid record should be returned
429         assertThat(PropertiesProvisioningDataParser.getFirstNdefRecord(nfcIntent))
430                 .isEqualTo(record);
431     }
432 
buildIntentWithAllShortExtras()433     private Intent buildIntentWithAllShortExtras() throws Exception {
434         Properties propsShort = new Properties();
435         Bundle bundleShort = new Bundle();
436         bundleShort.putString(
437                 EXTRA_PROVISIONING_DISCLAIMER_HEADER_SHORT, TEST_DISCLAMER_HEADER);
438         bundleShort.putParcelable(EXTRA_PROVISIONING_DISCLAIMER_CONTENT_SHORT, TEST_URI);
439         Parcelable[] parcelablesShort = {bundleShort};
440         propsShort.setProperty(
441                 EXTRA_PROVISIONING_DEVICE_ADMIN_PACKAGE_NAME_SHORT, TEST_PACKAGE_NAME);
442         propsShort.setProperty(
443                 EXTRA_PROVISIONING_DEVICE_ADMIN_COMPONENT_NAME_SHORT,
444                 TEST_COMPONENT_NAME.flattenToString());
445         setAllShortTestWifiInfo(propsShort);
446         setShortTestTimeTimeZoneAndLocale(propsShort);
447         setShortTestDeviceAdminDownload(propsShort);
448         propsShort.setProperty(
449                 EXTRA_PROVISIONING_ADMIN_EXTRAS_BUNDLE_SHORT, getTestAdminExtrasString());
450         propsShort.setProperty(
451                 EXTRA_PROVISIONING_SKIP_ENCRYPTION_SHORT,
452                 Boolean.toString(TEST_SKIP_ENCRYPTION));
453         propsShort.setProperty(
454                 EXTRA_PROVISIONING_MAIN_COLOR_SHORT, Integer.toString(TEST_MAIN_COLOR));
455         propsShort.setProperty(EXTRA_PROVISIONING_LEAVE_ALL_SYSTEM_APPS_ENABLED_SHORT,
456                 TEST_LEAVE_ALL_SYSTEM_APP_ENABLED);
457         propsShort.setProperty(
458                 EXTRA_PROVISIONING_ACCOUNT_TO_MIGRATE_SHORT, TEST_ACCOUNT_TO_MIGRATE);
459         propsShort.setProperty(
460                 EXTRA_PROVISIONING_KEEP_ACCOUNT_ON_MIGRATION_SHORT,
461                 TEST_KEEP_ACCOUNT_MIGRATED);
462         propsShort.setProperty(
463                 EXTRA_PROVISIONING_ORGANIZATION_NAME_SHORT, TEST_ORGANIZATION_NAME);
464         propsShort.setProperty(EXTRA_PROVISIONING_SUPPORT_URL_SHORT, TEST_SUPPORT_URL);
465         propsShort.setProperty(
466                 EXTRA_PROVISIONING_DEVICE_ADMIN_PACKAGE_LABEL_SHORT,
467                 TEST_DEVICE_ADMIN_PACKAGE_LABEL);
468         propsShort.setProperty(
469                 EXTRA_PROVISIONING_SKIP_USER_CONSENT_SHORT, TEST_SKIP_USER_CONSENT);
470         propsShort.setProperty(
471                 EXTRA_PROVISIONING_USE_MOBILE_DATA_SHORT,
472                 Boolean.toString(TEST_USE_MOBILE_DATA));
473         propsShort.setProperty(
474                 EXTRA_PROVISIONING_SKIP_USER_SETUP_SHORT,
475                 Boolean.toString(TEST_SKIP_USER_SETUP));
476         propsShort.setProperty(
477                 EXTRA_PROVISIONING_DEVICE_ADMIN_PACKAGE_ICON_URI_SHORT, TEST_URI_STRING);
478         propsShort.setProperty(EXTRA_PROVISIONING_LOGO_URI_SHORT, TEST_URI_STRING);
479 
480         Intent intentShort = buildNfcProvisioningIntent(propsShort);
481         intentShort.putExtra(EXTRA_PROVISIONING_DISCLAIMERS_SHORT, parcelablesShort);
482         return intentShort;
483     }
484 
buildIntentWithAllLongExtras()485     private Intent buildIntentWithAllLongExtras() throws Exception {
486         Properties propsLong = new Properties();
487         Bundle bundleLong = new Bundle();
488         bundleLong.putString(EXTRA_PROVISIONING_DISCLAIMER_HEADER, TEST_DISCLAMER_HEADER);
489         bundleLong.putParcelable(EXTRA_PROVISIONING_DISCLAIMER_CONTENT, TEST_URI);
490         Parcelable[] parcelablesLong = {bundleLong};
491         propsLong.setProperty(EXTRA_PROVISIONING_DEVICE_ADMIN_PACKAGE_NAME, TEST_PACKAGE_NAME);
492         propsLong.setProperty(
493                 EXTRA_PROVISIONING_DEVICE_ADMIN_COMPONENT_NAME,
494                 TEST_COMPONENT_NAME.flattenToString());
495         setAllTestWifiInfo(propsLong);
496         setTestTimeTimeZoneAndLocale(propsLong);
497         setTestDeviceAdminDownload(propsLong);
498         propsLong.setProperty(EXTRA_PROVISIONING_ADMIN_EXTRAS_BUNDLE, getTestAdminExtrasString());
499         propsLong.setProperty(
500                 EXTRA_PROVISIONING_SKIP_ENCRYPTION,
501                 Boolean.toString(TEST_SKIP_ENCRYPTION));
502         propsLong.setProperty(EXTRA_PROVISIONING_MAIN_COLOR, Integer.toString(TEST_MAIN_COLOR));
503         propsLong.setProperty(
504                 EXTRA_PROVISIONING_LEAVE_ALL_SYSTEM_APPS_ENABLED,
505                 TEST_LEAVE_ALL_SYSTEM_APP_ENABLED);
506         propsLong.setProperty(EXTRA_PROVISIONING_ACCOUNT_TO_MIGRATE, TEST_ACCOUNT_TO_MIGRATE);
507 
508         propsLong.setProperty(
509                 EXTRA_PROVISIONING_KEEP_ACCOUNT_ON_MIGRATION, TEST_KEEP_ACCOUNT_MIGRATED);
510         propsLong.setProperty(EXTRA_PROVISIONING_ORGANIZATION_NAME, TEST_ORGANIZATION_NAME);
511         propsLong.setProperty(EXTRA_PROVISIONING_SUPPORT_URL, TEST_SUPPORT_URL);
512         propsLong.setProperty(
513                 EXTRA_PROVISIONING_DEVICE_ADMIN_PACKAGE_LABEL, TEST_DEVICE_ADMIN_PACKAGE_LABEL);
514         propsLong.setProperty(EXTRA_PROVISIONING_SKIP_USER_CONSENT, TEST_SKIP_USER_CONSENT);
515         propsLong.setProperty(
516                 EXTRA_PROVISIONING_USE_MOBILE_DATA, Boolean.toString(TEST_USE_MOBILE_DATA));
517         propsLong.setProperty(
518                 EXTRA_PROVISIONING_SKIP_USER_SETUP, Boolean.toString(TEST_SKIP_USER_SETUP));
519         propsLong.setProperty(EXTRA_PROVISIONING_DEVICE_ADMIN_PACKAGE_ICON_URI, TEST_URI_STRING);
520         propsLong.setProperty(EXTRA_PROVISIONING_LOGO_URI, TEST_URI_STRING);
521 
522         Intent intentLong = buildNfcProvisioningIntent(propsLong);
523         intentLong.putExtra(EXTRA_PROVISIONING_DISCLAIMERS, parcelablesLong);
524         return intentLong;
525     }
526 
setTestWifiInfo(Properties props)527     private static Properties setTestWifiInfo(Properties props) {
528         props.setProperty(EXTRA_PROVISIONING_WIFI_SSID, TEST_SSID);
529         props.setProperty(EXTRA_PROVISIONING_WIFI_SECURITY_TYPE, TEST_SECURITY_TYPE);
530         props.setProperty(EXTRA_PROVISIONING_WIFI_PASSWORD, TEST_PASSWORD);
531         props.setProperty(EXTRA_PROVISIONING_WIFI_PROXY_HOST, TEST_PROXY_HOST);
532         props.setProperty(EXTRA_PROVISIONING_WIFI_PROXY_BYPASS, TEST_PROXY_BYPASS_HOSTS);
533         props.setProperty(EXTRA_PROVISIONING_WIFI_PAC_URL, TEST_PAC_URL);
534         props.setProperty(EXTRA_PROVISIONING_WIFI_PROXY_PORT, Integer.toString(TEST_PROXY_PORT));
535         props.setProperty(EXTRA_PROVISIONING_WIFI_HIDDEN, Boolean.toString(TEST_HIDDEN));
536         return props;
537     }
538 
setAllTestWifiInfo(Properties props)539     private static Properties setAllTestWifiInfo(Properties props) {
540         props.setProperty(EXTRA_PROVISIONING_WIFI_SSID, TEST_SSID);
541         props.setProperty(EXTRA_PROVISIONING_WIFI_SECURITY_TYPE, TEST_SECURITY_TYPE);
542         props.setProperty(EXTRA_PROVISIONING_WIFI_PASSWORD, TEST_PASSWORD);
543         props.setProperty(EXTRA_PROVISIONING_WIFI_EAP_METHOD, TEST_EAP_METHOD);
544         props.setProperty(EXTRA_PROVISIONING_WIFI_PHASE2_AUTH, TEST_PHASE2_AUTH);
545         props.setProperty(EXTRA_PROVISIONING_WIFI_CA_CERTIFICATE, TEST_CA_CERT);
546         props.setProperty(EXTRA_PROVISIONING_WIFI_USER_CERTIFICATE, TEST_USER_CERT);
547         props.setProperty(EXTRA_PROVISIONING_WIFI_IDENTITY, TEST_IDENTITY);
548         props.setProperty(
549                 EXTRA_PROVISIONING_WIFI_ANONYMOUS_IDENTITY, TEST_ANONYMOUS_IDENTITY);
550         props.setProperty(EXTRA_PROVISIONING_WIFI_DOMAIN, TEST_DOMAIN);
551         props.setProperty(EXTRA_PROVISIONING_WIFI_PROXY_HOST, TEST_PROXY_HOST);
552         props.setProperty(EXTRA_PROVISIONING_WIFI_PROXY_BYPASS, TEST_PROXY_BYPASS_HOSTS);
553         props.setProperty(EXTRA_PROVISIONING_WIFI_PAC_URL, TEST_PAC_URL);
554         props.setProperty(EXTRA_PROVISIONING_WIFI_PROXY_PORT, Integer.toString(TEST_PROXY_PORT));
555         props.setProperty(EXTRA_PROVISIONING_WIFI_HIDDEN, Boolean.toString(TEST_HIDDEN));
556         return props;
557     }
558 
setAllShortTestWifiInfo(Properties props)559     private static Properties setAllShortTestWifiInfo(Properties props) {
560         props.setProperty(
561                 EXTRA_PROVISIONING_WIFI_SSID_SHORT, TEST_SSID);
562         props.setProperty(
563                 EXTRA_PROVISIONING_WIFI_SECURITY_TYPE_SHORT, TEST_SECURITY_TYPE);
564         props.setProperty(
565                 EXTRA_PROVISIONING_WIFI_PASSWORD_SHORT, TEST_PASSWORD);
566         props.setProperty(EXTRA_PROVISIONING_WIFI_EAP_METHOD_SHORT, TEST_EAP_METHOD);
567         props.setProperty(EXTRA_PROVISIONING_WIFI_PHASE2_AUTH_SHORT, TEST_PHASE2_AUTH);
568         props.setProperty(EXTRA_PROVISIONING_WIFI_CA_CERTIFICATE_SHORT, TEST_CA_CERT);
569         props.setProperty(EXTRA_PROVISIONING_WIFI_USER_CERTIFICATE_SHORT, TEST_USER_CERT);
570         props.setProperty(EXTRA_PROVISIONING_WIFI_IDENTITY_SHORT, TEST_IDENTITY);
571         props.setProperty(
572                 EXTRA_PROVISIONING_WIFI_ANONYMOUS_IDENTITY_SHORT, TEST_ANONYMOUS_IDENTITY);
573         props.setProperty(EXTRA_PROVISIONING_WIFI_DOMAIN_SHORT, TEST_DOMAIN);
574 
575         props.setProperty(
576                 EXTRA_PROVISIONING_WIFI_PROXY_HOST_SHORT, TEST_PROXY_HOST);
577         props.setProperty(
578                 EXTRA_PROVISIONING_WIFI_PROXY_BYPASS_SHORT, TEST_PROXY_BYPASS_HOSTS);
579         props.setProperty(
580                 EXTRA_PROVISIONING_WIFI_PAC_URL_SHORT, TEST_PAC_URL);
581         props.setProperty(
582                 EXTRA_PROVISIONING_WIFI_PROXY_PORT_SHORT,
583                 Integer.toString(TEST_PROXY_PORT));
584         props.setProperty(
585                 EXTRA_PROVISIONING_WIFI_HIDDEN_SHORT, Boolean.toString(TEST_HIDDEN));
586         return props;
587     }
588 
setTestTimeTimeZoneAndLocale(Properties props)589     private static Properties setTestTimeTimeZoneAndLocale(Properties props) {
590         props.setProperty(EXTRA_PROVISIONING_LOCAL_TIME, Long.toString(TEST_LOCAL_TIME));
591         props.setProperty(EXTRA_PROVISIONING_TIME_ZONE, TEST_TIME_ZONE);
592         props.setProperty(EXTRA_PROVISIONING_LOCALE, StoreUtils.localeToString(TEST_LOCALE));
593         return props;
594     }
595 
setShortTestTimeTimeZoneAndLocale(Properties props)596     private static Properties setShortTestTimeTimeZoneAndLocale(Properties props) {
597         props.setProperty(
598                 EXTRA_PROVISIONING_LOCAL_TIME_SHORT, Long.toString(TEST_LOCAL_TIME));
599         props.setProperty(
600                 EXTRA_PROVISIONING_TIME_ZONE_SHORT, TEST_TIME_ZONE);
601         props.setProperty(
602                 EXTRA_PROVISIONING_LOCALE_SHORT, StoreUtils.localeToString(TEST_LOCALE));
603         return props;
604     }
605 
setTestDeviceAdminDownload(Properties props)606     private static Properties setTestDeviceAdminDownload(Properties props) {
607         props.setProperty(
608                 EXTRA_PROVISIONING_DEVICE_ADMIN_MINIMUM_VERSION_CODE,
609                 Integer.toString(TEST_MIN_SUPPORT_VERSION));
610         props.setProperty(
611                 EXTRA_PROVISIONING_DEVICE_ADMIN_PACKAGE_DOWNLOAD_LOCATION,
612                 TEST_DOWNLOAD_LOCATION);
613         props.setProperty(EXTRA_PROVISIONING_DEVICE_ADMIN_PACKAGE_DOWNLOAD_COOKIE_HEADER,
614                 TEST_COOKIE_HEADER);
615         props.setProperty(
616                 EXTRA_PROVISIONING_DEVICE_ADMIN_PACKAGE_CHECKSUM, buildTestPackageChecksum());
617         props.setProperty(
618                 EXTRA_PROVISIONING_DEVICE_ADMIN_SIGNATURE_CHECKSUM, buildTestSignatureChecksum());
619         return props;
620     }
621 
setShortTestDeviceAdminDownload(Properties props)622     private static Properties setShortTestDeviceAdminDownload(Properties props) {
623         props.setProperty(
624                 EXTRA_PROVISIONING_DEVICE_ADMIN_MINIMUM_VERSION_CODE_SHORT,
625                 Integer.toString(TEST_MIN_SUPPORT_VERSION));
626         props.setProperty(
627                 EXTRA_PROVISIONING_DEVICE_ADMIN_PACKAGE_DOWNLOAD_LOCATION_SHORT,
628                 TEST_DOWNLOAD_LOCATION);
629         props.setProperty(
630                 EXTRA_PROVISIONING_DEVICE_ADMIN_PACKAGE_DOWNLOAD_COOKIE_HEADER_SHORT,
631                 TEST_COOKIE_HEADER);
632         props.setProperty(
633                 EXTRA_PROVISIONING_DEVICE_ADMIN_PACKAGE_CHECKSUM_SHORT, buildTestPackageChecksum());
634         props.setProperty(
635                 EXTRA_PROVISIONING_DEVICE_ADMIN_SIGNATURE_CHECKSUM_SHORT,
636                 buildTestSignatureChecksum());
637         return props;
638     }
639 
getTestAdminExtrasString()640     private static String getTestAdminExtrasString() throws Exception {
641         Properties props = new Properties();
642         ByteArrayOutputStream stream = new ByteArrayOutputStream();
643 
644         PersistableBundle bundle = getTestAdminExtrasPersistableBundle();
645         for (String key : bundle.keySet()) {
646             props.setProperty(key, bundle.getString(key));
647         }
648         props.store(stream, "ADMIN_EXTRAS_BUNDLE" /* data description */);
649 
650         return stream.toString();
651     }
652 
getTestAdminExtrasPersistableBundle()653     private static PersistableBundle getTestAdminExtrasPersistableBundle() {
654         PersistableBundle bundle = new PersistableBundle();
655         bundle.putString("key1", "val1");
656         bundle.putString("key2", "val2");
657         bundle.putString("key3", "val3");
658         return bundle;
659     }
660 
buildNfcProvisioningProperties()661     private Properties buildNfcProvisioningProperties() throws Exception {
662         Properties properties = new Properties();
663         properties.setProperty(EXTRA_PROVISIONING_DEVICE_ADMIN_PACKAGE_NAME, TEST_PACKAGE_NAME);
664         properties.setProperty(
665                 EXTRA_PROVISIONING_DEVICE_ADMIN_COMPONENT_NAME,
666                 TEST_COMPONENT_NAME.flattenToString());
667         setTestWifiInfo(properties);
668         setTestTimeTimeZoneAndLocale(properties);
669         setTestDeviceAdminDownload(properties);
670         properties.setProperty(EXTRA_PROVISIONING_ADMIN_EXTRAS_BUNDLE, getTestAdminExtrasString());
671         properties.setProperty(
672                 EXTRA_PROVISIONING_SKIP_ENCRYPTION,
673                 Boolean.toString(TEST_SKIP_ENCRYPTION));
674         properties.setProperty(
675                 EXTRA_PROVISIONING_USE_MOBILE_DATA, Boolean.toString(TEST_USE_MOBILE_DATA));
676         properties.setProperty(EXTRA_PROVISIONING_MAIN_COLOR, Integer.toString(TEST_MAIN_COLOR));
677         return properties;
678     }
679 
buildNfcProvisioningIntent(Properties properties)680     private Intent buildNfcProvisioningIntent(Properties properties) throws IOException {
681         NdefMessage[] ndefMessages = new NdefMessage[] {
682             new NdefMessage(new NdefRecord[] {
683                 NdefRecord.createMime(
684                         DevicePolicyManager.MIME_TYPE_PROVISIONING_NFC,
685                         buildNfcProvisioningMimeData(properties))
686             })
687         };
688         return new Intent(NfcAdapter.ACTION_NDEF_DISCOVERED)
689                 .setType(MIME_TYPE_PROVISIONING_NFC)
690                 .putExtra(NfcAdapter.EXTRA_NDEF_MESSAGES, ndefMessages);
691 
692     }
693 
buildNfcProvisioningMimeData(Properties properties)694     private byte[] buildNfcProvisioningMimeData(Properties properties)
695             throws IOException {
696         ByteArrayOutputStream stream = new ByteArrayOutputStream();
697         properties.store(stream, /* comments= */ "NFC provisioning intent");
698         return stream.toByteArray();
699     }
700 
buildTestPackageChecksum()701     private static String buildTestPackageChecksum() {
702         return Base64.encodeToString(TEST_PACKAGE_CHECKSUM,
703                 Base64.URL_SAFE | Base64.NO_PADDING | Base64.NO_WRAP);
704     }
705 
buildTestSignatureChecksum()706     private static String buildTestSignatureChecksum() {
707         return Base64.encodeToString(TEST_SIGNATURE_CHECKSUM,
708                 Base64.URL_SAFE | Base64.NO_PADDING | Base64.NO_WRAP);
709     }
710 }
711