• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
25 import static com.google.common.truth.Truth.assertThat;
26 
27 import static org.mockito.ArgumentMatchers.anyInt;
28 import static org.mockito.ArgumentMatchers.anyString;
29 import static org.mockito.Mockito.mock;
30 import static org.mockito.Mockito.when;
31 
32 import android.app.admin.DevicePolicyManager;
33 import android.content.Context;
34 import android.content.Intent;
35 import android.content.SharedPreferences;
36 import android.content.pm.PackageManager;
37 import android.nfc.NdefMessage;
38 import android.nfc.NdefRecord;
39 import android.nfc.NfcAdapter;
40 import android.test.AndroidTestCase;
41 import android.test.suitebuilder.annotation.SmallTest;
42 
43 import com.android.managedprovisioning.common.ManagedProvisioningSharedPreferences;
44 
45 import java.io.ByteArrayOutputStream;
46 import java.util.List;
47 import java.util.Properties;
48 import java.util.function.LongSupplier;
49 
50 import org.mockito.Mock;
51 import org.mockito.MockitoAnnotations;
52 
53 /**
54  * Unit-tests for {@link AnalyticsUtils}.
55  */
56 @SmallTest
57 public class AnalyticsUtilsTest extends AndroidTestCase {
58     private static final String INVALID_PACKAGE_NAME = "invalid-package-name";
59     private static final String VALID_PACKAGE_NAME = "valid-package-name";
60     private static final String VALID_INSTALLER_PACKAGE = "valid-installer-package";
61     private static final String INVALID_PROVISIONING_EXTRA = "invalid-provisioning-extra";
62 
63     @Mock private Context mockContext;
64     @Mock private PackageManager mockPackageManager;
65 
66     @Override
setUp()67     public void setUp() {
68         // this is necessary for mockito to work
69         System.setProperty("dexmaker.dexcache", getContext().getCacheDir().toString());
70 
71         MockitoAnnotations.initMocks(this);
72 
73         when(mockContext.getPackageManager()).thenReturn(mockPackageManager);
74     }
75 
testGetInstallerPackageName_invalidPackage()76     public void testGetInstallerPackageName_invalidPackage() {
77         // WHEN getting installer package name for an invalid package.
78         when(mockPackageManager.getInstallerPackageName(INVALID_PACKAGE_NAME))
79                 .thenThrow(new IllegalArgumentException());
80         // THEN null should be returned and exception should be digested.
81         assertNull(AnalyticsUtils.getInstallerPackageName(mockContext, INVALID_PACKAGE_NAME));
82     }
83 
testGetInstallerPackageName_validPackage()84     public void testGetInstallerPackageName_validPackage() {
85         // WHEN getting installer package name for a valid package.
86         when(mockPackageManager.getInstallerPackageName(VALID_PACKAGE_NAME))
87                 .thenReturn(VALID_INSTALLER_PACKAGE);
88         // THEN valid installer package name should be returned.
89         assertEquals(VALID_INSTALLER_PACKAGE,
90                 AnalyticsUtils.getInstallerPackageName(mockContext, VALID_PACKAGE_NAME));
91     }
92 
testGetAllProvisioningExtras_NullIntent()93     public void testGetAllProvisioningExtras_NullIntent() {
94         // WHEN getting provisioning extras using null Intent.
95         List<String> provisioningExtras = AnalyticsUtils.getAllProvisioningExtras(null);
96         // THEN an empty list of valid provisioning extras should be returned.
97         assertEquals(0, provisioningExtras.size());
98     }
99 
testGetAllProvisioningExtras_ProvisioningResume()100     public void testGetAllProvisioningExtras_ProvisioningResume() {
101         // GIVEN provisioning was resumed
102         Intent intent = new Intent(ACTION_RESUME_PROVISIONING);
103         // WHEN getting provisioning extras using resume provisioning intent.
104         List<String> provisioningExtras = AnalyticsUtils.getAllProvisioningExtras(intent);
105         // THEN an empty list of valid provisioning extras should be returned.
106         assertEquals(0, provisioningExtras.size());
107     }
108 
testGetAllProvisioningExtras_NullBundleExtras()109     public void testGetAllProvisioningExtras_NullBundleExtras() {
110         // GIVEN intent has null extras
111         Intent intent = new Intent(DevicePolicyManager.ACTION_PROVISION_MANAGED_PROFILE);
112         // WHEN getting provisioning extras with null extras
113         List<String> provisioningExtras = AnalyticsUtils.getAllProvisioningExtras(intent);
114         // THEN an empty list of valid provisioning extras should be returned.
115         assertEquals(0, provisioningExtras.size());
116     }
117 
testGetAllProvisioningExtras_NullNfcProperties()118     public void testGetAllProvisioningExtras_NullNfcProperties() throws Exception {
119         // GIVEN intent has null extras
120         Intent intent = new Intent(NfcAdapter.ACTION_NDEF_DISCOVERED);
121         // WHEN getting provisioning extras with null extras
122         List<String> provisioningExtras = AnalyticsUtils.getAllProvisioningExtras(intent);
123         // THEN an empty list of valid provisioning extras should be returned.
124         assertEquals(0, provisioningExtras.size());
125     }
126 
testGetAllProvisioningExtras()127     public void testGetAllProvisioningExtras() {
128         // GIVEN intent with both valid and invalid provisioning extras
129         Intent intent = new Intent(DevicePolicyManager.ACTION_PROVISION_MANAGED_PROFILE);
130         intent.putExtra(EXTRA_PROVISIONING_ACCOUNT_TO_MIGRATE, "");
131         intent.putExtra(INVALID_PROVISIONING_EXTRA, "");
132         intent.putExtra(EXTRA_PROVISIONING_DEVICE_ADMIN_COMPONENT_NAME, "");
133         // WHEN getting provisioning extras using the intent
134         List<String> provisioningExtras = AnalyticsUtils.getAllProvisioningExtras(intent);
135         // THEN a list of valid provisioning extras should be returned.
136         assertEquals(2, provisioningExtras.size());
137         provisioningExtras.contains(EXTRA_PROVISIONING_DEVICE_ADMIN_COMPONENT_NAME);
138         provisioningExtras.contains(EXTRA_PROVISIONING_ACCOUNT_TO_MIGRATE);
139     }
140 
testGetAllProvisioningExtras_Nfc()141     public void testGetAllProvisioningExtras_Nfc() throws Exception {
142         // GIVEN a Nfc intent with both valid and invalid provisioning extras
143         Properties props = new Properties();
144         ByteArrayOutputStream stream = new ByteArrayOutputStream();
145         props.setProperty(EXTRA_PROVISIONING_MAIN_COLOR, "");
146         props.setProperty(INVALID_PROVISIONING_EXTRA, "");
147         props.setProperty(EXTRA_PROVISIONING_DEVICE_ADMIN_COMPONENT_NAME, "");
148         props.store(stream, "NFC provisioning intent" /* data description */);
149 
150         NdefRecord record = NdefRecord.createMime(
151                 DevicePolicyManager.MIME_TYPE_PROVISIONING_NFC,
152                 stream.toByteArray());
153         NdefMessage ndfMsg = new NdefMessage(new NdefRecord[]{record});
154 
155         Intent intent = new Intent(NfcAdapter.ACTION_NDEF_DISCOVERED)
156                 .setType(MIME_TYPE_PROVISIONING_NFC)
157                 .putExtra(NfcAdapter.EXTRA_NDEF_MESSAGES, new NdefMessage[]{ndfMsg});
158 
159         // WHEN getting provisioning extras using the intent
160         List<String> provisioningExtras = AnalyticsUtils.getAllProvisioningExtras(intent);
161         // THEN a list of valid provisioning extras should be returned.
162         assertEquals(2, provisioningExtras.size());
163         provisioningExtras.contains(EXTRA_PROVISIONING_DEVICE_ADMIN_COMPONENT_NAME);
164         provisioningExtras.contains(EXTRA_PROVISIONING_MAIN_COLOR);
165     }
166 
testGetProvisioningTime_shouldReturnDiff()167     public void testGetProvisioningTime_shouldReturnDiff() {
168         final ManagedProvisioningSharedPreferences mockPrefs =
169                 mock(ManagedProvisioningSharedPreferences.class);
170         when(mockPrefs.getProvisioningStartedTimestamp()).thenReturn(5L);
171 
172         assertThat(AnalyticsUtils.getProvisioningTime(mockPrefs, () -> 20L)).isEqualTo(15L);
173     }
174 
testGetProvisioningTime_shouldReturnZero()175     public void testGetProvisioningTime_shouldReturnZero() {
176         final ManagedProvisioningSharedPreferences mockPrefs =
177                 mock(ManagedProvisioningSharedPreferences.class);
178         when(mockPrefs.getProvisioningStartedTimestamp()).thenReturn(0L);
179 
180         assertThat(AnalyticsUtils.getProvisioningTime(mockPrefs, () -> 20L)).isEqualTo(-1);
181     }
182 }
183