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_PROFILE; 21 import static android.app.admin.DevicePolicyManager.ACTION_PROVISION_MANAGED_SHAREABLE_DEVICE; 22 import static android.app.admin.DevicePolicyManager.ACTION_PROVISION_MANAGED_USER; 23 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_DEVICE_ADMIN_COMPONENT_NAME; 24 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_DEVICE_ADMIN_PACKAGE_NAME; 25 import static android.app.admin.DevicePolicyManager.MIME_TYPE_PROVISIONING_NFC; 26 import static org.mockito.Mockito.doReturn; 27 import static org.mockito.Mockito.spy; 28 29 import android.accounts.Account; 30 import android.app.admin.DevicePolicyManager; 31 import android.content.ComponentName; 32 import android.content.Context; 33 import android.content.Intent; 34 import android.nfc.NdefMessage; 35 import android.nfc.NdefRecord; 36 import android.nfc.NfcAdapter; 37 import android.os.UserHandle; 38 import android.test.AndroidTestCase; 39 import android.test.suitebuilder.annotation.SmallTest; 40 41 import com.android.managedprovisioning.common.Utils; 42 43 import org.mockito.Mock; 44 import org.mockito.MockitoAnnotations; 45 46 import java.io.ByteArrayOutputStream; 47 import java.util.Locale; 48 import java.util.Properties; 49 50 /** Tests {@link MessageParser} */ 51 @SmallTest 52 public class MessageParserTest extends AndroidTestCase { 53 private static final String TEST_PACKAGE_NAME = "com.afwsamples.testdpc"; 54 private static final ComponentName TEST_COMPONENT_NAME = 55 ComponentName.unflattenFromString( 56 "com.afwsamples.testdpc/com.afwsamples.testdpc.DeviceAdminReceiver"); 57 private static final long TEST_LOCAL_TIME = 1456939524713L; 58 private static final Locale TEST_LOCALE = Locale.UK; 59 private static final String TEST_TIME_ZONE = "GMT"; 60 private static final Integer TEST_MAIN_COLOR = 65280; 61 private static final boolean TEST_SKIP_ENCRYPTION = true; 62 private static final Account TEST_ACCOUNT_TO_MIGRATE = 63 new Account("user@gmail.com", "com.google"); 64 65 @Mock 66 private Context mContext; 67 68 private Utils mUtils; 69 70 private MessageParser mMessageParser; 71 72 @Override setUp()73 public void setUp() { 74 // this is necessary for mockito to work 75 System.setProperty("dexmaker.dexcache", getContext().getCacheDir().toString()); 76 77 MockitoAnnotations.initMocks(this); 78 79 mMessageParser = new MessageParser(mContext, mUtils = spy(new Utils())); 80 } 81 test_correctParserUsedToParseNfcIntent()82 public void test_correctParserUsedToParseNfcIntent() throws Exception { 83 // GIVEN a NFC provisioning intent with some supported data. 84 Properties props = new Properties(); 85 ByteArrayOutputStream stream = new ByteArrayOutputStream(); 86 props.setProperty(EXTRA_PROVISIONING_DEVICE_ADMIN_PACKAGE_NAME, TEST_PACKAGE_NAME); 87 props.setProperty( 88 EXTRA_PROVISIONING_DEVICE_ADMIN_COMPONENT_NAME, 89 TEST_COMPONENT_NAME.flattenToString()); 90 props.store(stream, "NFC provisioning intent" /* data description */); 91 NdefRecord record = NdefRecord.createMime( 92 DevicePolicyManager.MIME_TYPE_PROVISIONING_NFC, 93 stream.toByteArray()); 94 NdefMessage ndfMsg = new NdefMessage(new NdefRecord[]{record}); 95 96 Intent intent = new Intent(NfcAdapter.ACTION_NDEF_DISCOVERED) 97 .setType(MIME_TYPE_PROVISIONING_NFC) 98 .putExtra(NfcAdapter.EXTRA_NDEF_MESSAGES, new NdefMessage[]{ndfMsg}); 99 100 // WHEN the mMessageParser.getParser is invoked. 101 ProvisioningDataParser parser = mMessageParser.getParser(intent); 102 103 // THEN the properties parser is returned. 104 assertTrue(parser instanceof PropertiesProvisioningDataParser); 105 } 106 test_correctParserUsedToParseOtherSupportedProvisioningIntent()107 public void test_correctParserUsedToParseOtherSupportedProvisioningIntent() throws Exception { 108 // GIVEN the device admin app is installed. 109 doReturn(TEST_COMPONENT_NAME) 110 .when(mUtils) 111 .findDeviceAdmin(null, TEST_COMPONENT_NAME, mContext, UserHandle.myUserId()); 112 // GIVEN a list of supported provisioning actions, except NFC. 113 String[] supportedProvisioningActions = new String[] { 114 ACTION_PROVISION_MANAGED_DEVICE, 115 ACTION_PROVISION_MANAGED_DEVICE_FROM_TRUSTED_SOURCE, 116 ACTION_PROVISION_MANAGED_USER, 117 ACTION_PROVISION_MANAGED_PROFILE, 118 ACTION_PROVISION_MANAGED_SHAREABLE_DEVICE 119 }; 120 121 for (String provisioningAction : supportedProvisioningActions) { 122 Intent intent = new Intent(provisioningAction) 123 .putExtra(EXTRA_PROVISIONING_DEVICE_ADMIN_COMPONENT_NAME, TEST_COMPONENT_NAME); 124 125 // WHEN the mMessageParser.getParser is invoked. 126 ProvisioningDataParser parser = mMessageParser.getParser(intent); 127 128 // THEN the extras parser is returned. 129 assertTrue(parser instanceof ExtrasProvisioningDataParser); 130 } 131 } 132 } 133