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