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.model;
17
18import static com.android.managedprovisioning.TestUtils.createTestAdminExtras;
19
20import android.accounts.Account;
21import android.app.admin.DevicePolicyManager;
22import android.content.ComponentName;
23import android.os.Parcel;
24import android.test.AndroidTestCase;
25import android.test.MoreAsserts;
26import android.test.suitebuilder.annotation.SmallTest;
27import java.io.File;
28import java.util.Locale;
29
30/** Tests for {@link ProvisioningParams} */
31public class ProvisioningParamsTest extends AndroidTestCase {
32    private static final String TEST_PROVISIONING_ACTION =
33            DevicePolicyManager.ACTION_PROVISION_MANAGED_PROFILE;
34
35    private static final String TEST_PACKAGE_NAME = "com.afwsamples.testdpc";
36    private static final ComponentName TEST_COMPONENT_NAME =
37            ComponentName.unflattenFromString(
38                    "com.afwsamples.testdpc/com.afwsamples.testdpc.DeviceAdminReceiver");
39    private static final long TEST_LOCAL_TIME = 1456939524713L;
40    private static final Locale TEST_LOCALE = Locale.UK;
41    private static final String TEST_TIME_ZONE = "GMT";
42    private static final Integer TEST_MAIN_COLOR = 65280;
43    private static final boolean TEST_STARTED_BY_TRUSTED_SOURCE = true;
44    private static final boolean TEST_IS_NFC = true;
45    private static final boolean TEST_LEAVE_ALL_SYSTEM_APP_ENABLED = true;
46    private static final boolean TEST_SKIP_ENCRYPTION = true;
47    private static final boolean TEST_SKIP_USER_SETUP = true;
48    private static final boolean TEST_SKIP_USER_CONSENT = true;
49    private static final Account TEST_ACCOUNT_TO_MIGRATE =
50            new Account("user@gmail.com", "com.google");
51
52    // Wifi info
53    private static final String TEST_SSID = "TestWifi";
54    private static final boolean TEST_HIDDEN = true;
55    private static final String TEST_SECURITY_TYPE = "WPA2";
56    private static final String TEST_PASSWORD = "GoogleRock";
57    private static final String TEST_PROXY_HOST = "testhost.com";
58    private static final int TEST_PROXY_PORT = 7689;
59    private static final String TEST_PROXY_BYPASS_HOSTS = "http://host1.com;https://host2.com";
60    private static final String TEST_PAC_URL = "pac.test.com";
61    private static final WifiInfo TEST_WIFI_INFO = WifiInfo.Builder.builder()
62            .setSsid(TEST_SSID)
63            .setHidden(TEST_HIDDEN)
64            .setSecurityType(TEST_SECURITY_TYPE)
65            .setPassword(TEST_PASSWORD)
66            .setProxyHost(TEST_PROXY_HOST)
67            .setProxyPort(TEST_PROXY_PORT)
68            .setProxyBypassHosts(TEST_PROXY_BYPASS_HOSTS)
69            .setPacUrl(TEST_PAC_URL)
70            .build();
71
72    // Device admin package download info
73    private static final String TEST_DOWNLOAD_LOCATION =
74            "http://example/dpc.apk";
75    private static final String TEST_COOKIE_HEADER =
76            "Set-Cookie: sessionToken=foobar; Expires=Thu, 18 Feb 2016 23:59:59 GMT";
77    private static final byte[] TEST_PACKAGE_CHECKSUM = new byte[] { '1', '2', '3', '4', '5' };
78    private static final byte[] TEST_SIGNATURE_CHECKSUM = new byte[] { '5', '4', '3', '2', '1' };
79    private static final int TEST_MIN_SUPPORT_VERSION = 17689;
80    private static final PackageDownloadInfo TEST_DOWNLOAD_INFO =
81            PackageDownloadInfo.Builder.builder()
82                    .setLocation(TEST_DOWNLOAD_LOCATION)
83                    .setCookieHeader(TEST_COOKIE_HEADER)
84                    .setPackageChecksum(TEST_PACKAGE_CHECKSUM)
85                    .setSignatureChecksum(TEST_SIGNATURE_CHECKSUM)
86                    .setMinVersion(TEST_MIN_SUPPORT_VERSION)
87                    .build();
88
89    @SmallTest
90    public void testFailToConstructProvisioningParamsWithoutPackageNameOrComponentName() {
91        // WHEN the ProvisioningParams is constructed by with neither a package name nor a component
92        // name
93        try {
94            ProvisioningParams provisioningParams = ProvisioningParams.Builder.builder()
95                    .setProvisioningAction(TEST_PROVISIONING_ACTION)
96                    .build();
97            fail("Package name or component name is mandatory.");
98        } catch (IllegalArgumentException e) {
99            // THEN the ProvisioningParams fails to construct.
100        }
101    }
102
103    @SmallTest
104    public void testFailToConstructProvisioningParamsWithoutProvisioningAction() {
105        // WHEN the ProvisioningParams is constructed by without a provisioning action.
106        try {
107            ProvisioningParams provisioningParams = ProvisioningParams.Builder.builder()
108                    .setDeviceAdminComponentName(TEST_COMPONENT_NAME)
109                    .build();
110            fail("Provisioning action is mandatory");
111        } catch (NullPointerException e) {
112            // THEN the ProvisioningParams fails to construct.
113        }
114    }
115
116    @SmallTest
117    public void testEquals() {
118        // GIVEN 2 ProvisioningParams objects created by the same set of parameters
119        ProvisioningParams provisioningParams1 = getCompleteProvisioningParams();
120        ProvisioningParams provisioningParams2 = getCompleteProvisioningParams();
121
122        // WHEN these two objects compare.
123        // THEN they are the same.
124        assertEquals(provisioningParams1, provisioningParams2);
125    }
126
127    @SmallTest
128    public void testNotEquals() {
129        // GIVEN 2 ProvisioningParams objects created by different sets of parameters
130        ProvisioningParams provisioningParams1 = ProvisioningParams.Builder.builder()
131                .setProvisioningAction(TEST_PROVISIONING_ACTION)
132                .setDeviceAdminPackageName(TEST_PACKAGE_NAME)
133                .setDeviceAdminComponentName(TEST_COMPONENT_NAME)
134                .setDeviceAdminDownloadInfo(TEST_DOWNLOAD_INFO)
135                .setLocalTime(TEST_LOCAL_TIME)
136                .setLocale(TEST_LOCALE)
137                .setTimeZone(TEST_TIME_ZONE)
138                .setMainColor(TEST_MAIN_COLOR)
139                .setStartedByTrustedSource(TEST_STARTED_BY_TRUSTED_SOURCE)
140                .setLeaveAllSystemAppsEnabled(TEST_LEAVE_ALL_SYSTEM_APP_ENABLED)
141                .setSkipEncryption(TEST_SKIP_ENCRYPTION)
142                .setSkipUserSetup(TEST_SKIP_USER_SETUP)
143                .setAccountToMigrate(TEST_ACCOUNT_TO_MIGRATE)
144                .setWifiInfo(TEST_WIFI_INFO)
145                .setAdminExtrasBundle(createTestAdminExtras())
146                .build();
147        ProvisioningParams provisioningParams2 = ProvisioningParams.Builder.builder()
148                .setProvisioningAction("different.action")
149                .setDeviceAdminPackageName(TEST_PACKAGE_NAME)
150                .setDeviceAdminComponentName(TEST_COMPONENT_NAME)
151                .setDeviceAdminDownloadInfo(TEST_DOWNLOAD_INFO)
152                .setLocalTime(TEST_LOCAL_TIME)
153                .setLocale(TEST_LOCALE)
154                .setTimeZone(TEST_TIME_ZONE)
155                .setMainColor(TEST_MAIN_COLOR)
156                .setStartedByTrustedSource(TEST_STARTED_BY_TRUSTED_SOURCE)
157                .setLeaveAllSystemAppsEnabled(TEST_LEAVE_ALL_SYSTEM_APP_ENABLED)
158                .setSkipEncryption(TEST_SKIP_ENCRYPTION)
159                .setSkipUserSetup(TEST_SKIP_USER_SETUP)
160                .setAccountToMigrate(TEST_ACCOUNT_TO_MIGRATE)
161                .setWifiInfo(TEST_WIFI_INFO)
162                .setAdminExtrasBundle(createTestAdminExtras())
163                .build();
164
165        // WHEN these two objects compare.
166        // THEN they are not the same.
167        MoreAsserts.assertNotEqual(provisioningParams1, provisioningParams2);
168    }
169
170    @SmallTest
171    public void testSaveAndRestoreComplete() throws Exception {
172        testSaveAndRestore(getCompleteProvisioningParams());
173    }
174
175    // Testing with a minimum set of parameters to cover all null use cases.
176    @SmallTest
177    public void testSaveAndRestoreMinimalist() throws Exception {
178        testSaveAndRestore(ProvisioningParams.Builder.builder()
179                .setProvisioningAction(TEST_PROVISIONING_ACTION)
180                .setDeviceAdminPackageName(TEST_PACKAGE_NAME)
181                .build());
182    }
183
184    private void testSaveAndRestore(ProvisioningParams original) {
185        // GIVEN a ProvisioningParams object
186        // WHEN the ProvisioningParams is written to xml and then read back
187        File file = new File(mContext.getFilesDir(), "test_store.xml");
188        original.save(file);
189        ProvisioningParams copy = ProvisioningParams.load(file);
190        // THEN the same ProvisioningParams is obtained
191        assertEquals(original, copy);
192    }
193
194    @SmallTest
195    public void testParceable() {
196        // GIVEN a ProvisioningParams object.
197        ProvisioningParams expectedProvisioningParams = getCompleteProvisioningParams();
198
199        // WHEN the ProvisioningParams is written to parcel and then read back.
200        Parcel parcel = Parcel.obtain();
201        expectedProvisioningParams.writeToParcel(parcel, 0);
202        parcel.setDataPosition(0);
203        ProvisioningParams actualProvisioningParams =
204                ProvisioningParams.CREATOR.createFromParcel(parcel);
205
206        // THEN the same ProvisioningParams is obtained.
207        assertEquals(expectedProvisioningParams, actualProvisioningParams);
208    }
209
210    private ProvisioningParams getCompleteProvisioningParams() {
211        return ProvisioningParams.Builder.builder()
212                .setProvisioningAction(TEST_PROVISIONING_ACTION)
213                .setDeviceAdminPackageName(TEST_PACKAGE_NAME)
214                .setDeviceAdminComponentName(TEST_COMPONENT_NAME)
215                .setDeviceAdminDownloadInfo(TEST_DOWNLOAD_INFO)
216                .setLocalTime(TEST_LOCAL_TIME)
217                .setLocale(TEST_LOCALE)
218                .setTimeZone(TEST_TIME_ZONE)
219                .setMainColor(TEST_MAIN_COLOR)
220                .setStartedByTrustedSource(TEST_STARTED_BY_TRUSTED_SOURCE)
221                .setIsNfc(TEST_IS_NFC)
222                .setLeaveAllSystemAppsEnabled(TEST_LEAVE_ALL_SYSTEM_APP_ENABLED)
223                .setSkipEncryption(TEST_SKIP_ENCRYPTION)
224                .setSkipUserSetup(TEST_SKIP_USER_SETUP)
225                .setSkipUserConsent(TEST_SKIP_USER_CONSENT)
226                .setAccountToMigrate(TEST_ACCOUNT_TO_MIGRATE)
227                .setWifiInfo(TEST_WIFI_INFO)
228                .setAdminExtrasBundle(createTestAdminExtras())
229                .build();
230    }
231}
232