1 /* 2 * Copyright (C) 2016 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.managedprovisioning.analytics; 18 19 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_ACCOUNT_TO_MIGRATE; 20 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_DEVICE_ADMIN_COMPONENT_NAME; 21 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_MAIN_COLOR; 22 import static android.app.admin.DevicePolicyManager.MIME_TYPE_PROVISIONING_NFC; 23 import static com.android.managedprovisioning.common.Globals.ACTION_RESUME_PROVISIONING; 24 import static org.mockito.Mockito.when; 25 26 import android.app.admin.DevicePolicyManager; 27 import android.content.Context; 28 import android.content.Intent; 29 import android.content.pm.PackageManager; 30 import android.nfc.NdefMessage; 31 import android.nfc.NdefRecord; 32 import android.nfc.NfcAdapter; 33 import android.test.AndroidTestCase; 34 import android.test.suitebuilder.annotation.SmallTest; 35 36 import java.io.ByteArrayOutputStream; 37 import java.util.List; 38 import java.util.Properties; 39 40 import org.mockito.Mock; 41 import org.mockito.MockitoAnnotations; 42 43 /** 44 * Unit-tests for {@link AnalyticsUtils}. 45 */ 46 @SmallTest 47 public class AnalyticsUtilsTest extends AndroidTestCase { 48 private static final String INVALID_PACKAGE_NAME = "invalid-package-name"; 49 private static final String VALID_PACKAGE_NAME = "valid-package-name"; 50 private static final String VALID_INSTALLER_PACKAGE = "valid-installer-package"; 51 private static final String INVALID_PROVISIONING_EXTRA = "invalid-provisioning-extra"; 52 53 @Mock private Context mockContext; 54 @Mock private PackageManager mockPackageManager; 55 56 @Override setUp()57 public void setUp() { 58 // this is necessary for mockito to work 59 System.setProperty("dexmaker.dexcache", getContext().getCacheDir().toString()); 60 61 MockitoAnnotations.initMocks(this); 62 63 when(mockContext.getPackageManager()).thenReturn(mockPackageManager); 64 } 65 testGetInstallerPackageName_invalidPackage()66 public void testGetInstallerPackageName_invalidPackage() { 67 // WHEN getting installer package name for an invalid package. 68 when(mockPackageManager.getInstallerPackageName(INVALID_PACKAGE_NAME)) 69 .thenThrow(new IllegalArgumentException()); 70 // THEN null should be returned and exception should be digested. 71 assertNull(AnalyticsUtils.getInstallerPackageName(mockContext, INVALID_PACKAGE_NAME)); 72 } 73 testGetInstallerPackageName_validPackage()74 public void testGetInstallerPackageName_validPackage() { 75 // WHEN getting installer package name for a valid package. 76 when(mockPackageManager.getInstallerPackageName(VALID_PACKAGE_NAME)) 77 .thenReturn(VALID_INSTALLER_PACKAGE); 78 // THEN valid installer package name should be returned. 79 assertEquals(VALID_INSTALLER_PACKAGE, 80 AnalyticsUtils.getInstallerPackageName(mockContext, VALID_PACKAGE_NAME)); 81 } 82 testGetAllProvisioningExtras_NullIntent()83 public void testGetAllProvisioningExtras_NullIntent() { 84 // WHEN getting provisioning extras using null Intent. 85 List<String> provisioningExtras = AnalyticsUtils.getAllProvisioningExtras(null); 86 // THEN an empty list of valid provisioning extras should be returned. 87 assertEquals(0, provisioningExtras.size()); 88 } 89 testGetAllProvisioningExtras_ProvisioningResume()90 public void testGetAllProvisioningExtras_ProvisioningResume() { 91 // GIVEN provisioning was resumed 92 Intent intent = new Intent(ACTION_RESUME_PROVISIONING); 93 // WHEN getting provisioning extras using resume provisioning intent. 94 List<String> provisioningExtras = AnalyticsUtils.getAllProvisioningExtras(intent); 95 // THEN an empty list of valid provisioning extras should be returned. 96 assertEquals(0, provisioningExtras.size()); 97 } 98 testGetAllProvisioningExtras_NullBundleExtras()99 public void testGetAllProvisioningExtras_NullBundleExtras() { 100 // GIVEN intent has null extras 101 Intent intent = new Intent(DevicePolicyManager.ACTION_PROVISION_MANAGED_PROFILE); 102 // WHEN getting provisioning extras with null extras 103 List<String> provisioningExtras = AnalyticsUtils.getAllProvisioningExtras(intent); 104 // THEN an empty list of valid provisioning extras should be returned. 105 assertEquals(0, provisioningExtras.size()); 106 } 107 testGetAllProvisioningExtras_NullNfcProperties()108 public void testGetAllProvisioningExtras_NullNfcProperties() throws Exception { 109 // GIVEN intent has null extras 110 Intent intent = new Intent(NfcAdapter.ACTION_NDEF_DISCOVERED); 111 // WHEN getting provisioning extras with null extras 112 List<String> provisioningExtras = AnalyticsUtils.getAllProvisioningExtras(intent); 113 // THEN an empty list of valid provisioning extras should be returned. 114 assertEquals(0, provisioningExtras.size()); 115 } 116 testGetAllProvisioningExtras()117 public void testGetAllProvisioningExtras() { 118 // GIVEN intent with both valid and invalid provisioning extras 119 Intent intent = new Intent(DevicePolicyManager.ACTION_PROVISION_MANAGED_PROFILE); 120 intent.putExtra(EXTRA_PROVISIONING_ACCOUNT_TO_MIGRATE, ""); 121 intent.putExtra(INVALID_PROVISIONING_EXTRA, ""); 122 intent.putExtra(EXTRA_PROVISIONING_DEVICE_ADMIN_COMPONENT_NAME, ""); 123 // WHEN getting provisioning extras using the intent 124 List<String> provisioningExtras = AnalyticsUtils.getAllProvisioningExtras(intent); 125 // THEN a list of valid provisioning extras should be returned. 126 assertEquals(2, provisioningExtras.size()); 127 provisioningExtras.contains(EXTRA_PROVISIONING_DEVICE_ADMIN_COMPONENT_NAME); 128 provisioningExtras.contains(EXTRA_PROVISIONING_ACCOUNT_TO_MIGRATE); 129 } 130 testGetAllProvisioningExtras_Nfc()131 public void testGetAllProvisioningExtras_Nfc() throws Exception { 132 // GIVEN a Nfc intent with both valid and invalid provisioning extras 133 Properties props = new Properties(); 134 ByteArrayOutputStream stream = new ByteArrayOutputStream(); 135 props.setProperty(EXTRA_PROVISIONING_MAIN_COLOR, ""); 136 props.setProperty(INVALID_PROVISIONING_EXTRA, ""); 137 props.setProperty(EXTRA_PROVISIONING_DEVICE_ADMIN_COMPONENT_NAME, ""); 138 props.store(stream, "NFC provisioning intent" /* data description */); 139 140 NdefRecord record = NdefRecord.createMime( 141 DevicePolicyManager.MIME_TYPE_PROVISIONING_NFC, 142 stream.toByteArray()); 143 NdefMessage ndfMsg = new NdefMessage(new NdefRecord[]{record}); 144 145 Intent intent = new Intent(NfcAdapter.ACTION_NDEF_DISCOVERED) 146 .setType(MIME_TYPE_PROVISIONING_NFC) 147 .putExtra(NfcAdapter.EXTRA_NDEF_MESSAGES, new NdefMessage[]{ndfMsg}); 148 149 // WHEN getting provisioning extras using the intent 150 List<String> provisioningExtras = AnalyticsUtils.getAllProvisioningExtras(intent); 151 // THEN a list of valid provisioning extras should be returned. 152 assertEquals(2, provisioningExtras.size()); 153 provisioningExtras.contains(EXTRA_PROVISIONING_DEVICE_ADMIN_COMPONENT_NAME); 154 provisioningExtras.contains(EXTRA_PROVISIONING_MAIN_COLOR); 155 } 156 } 157