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