• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.model;
17 
18 import static com.android.managedprovisioning.TestUtils.createTestAdminExtras;
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import static org.mockito.ArgumentMatchers.eq;
22 import static org.mockito.ArgumentMatchers.nullable;
23 import static org.mockito.Mockito.when;
24 
25 import android.accounts.Account;
26 import android.app.admin.DevicePolicyManager;
27 import android.content.ComponentName;
28 import android.os.Parcel;
29 import android.os.UserHandle;
30 import android.test.AndroidTestCase;
31 
32 import androidx.test.filters.SmallTest;
33 
34 import com.android.managedprovisioning.common.IllegalProvisioningArgumentException;
35 import com.android.managedprovisioning.common.Utils;
36 
37 import org.mockito.Mock;
38 import org.mockito.MockitoAnnotations;
39 
40 import java.io.File;
41 import java.util.Locale;
42 
43 /** Tests for {@link ProvisioningParams} */
44 public class ProvisioningParamsTest extends AndroidTestCase {
45     private static final String TEST_PROVISIONING_ACTION =
46             DevicePolicyManager.ACTION_PROVISION_MANAGED_PROFILE;
47 
48     private static final String TEST_PACKAGE_NAME = "com.afwsamples.testdpc";
49     private static final ComponentName TEST_COMPONENT_NAME =
50             ComponentName.unflattenFromString(
51                     "com.afwsamples.testdpc/com.afwsamples.testdpc.DeviceAdminReceiver");
52     private static final long TEST_LOCAL_TIME = 1456939524713L;
53     private static final Locale TEST_LOCALE = Locale.UK;
54     private static final String TEST_TIME_ZONE = "GMT";
55     private static final Integer TEST_MAIN_COLOR = 65280;
56     private static final boolean TEST_STARTED_BY_TRUSTED_SOURCE = true;
57     private static final boolean TEST_IS_NFC = true;
58     private static final boolean TEST_LEAVE_ALL_SYSTEM_APP_ENABLED = true;
59     private static final boolean TEST_SKIP_ENCRYPTION = true;
60     private static final boolean TEST_SKIP_USER_SETUP = true;
61     private static final boolean TEST_SKIP_USER_CONSENT = true;
62     private static final Account TEST_ACCOUNT_TO_MIGRATE =
63             new Account("user@gmail.com", "com.google");
64     private static final boolean TEST_USE_MOBILE_DATA = true;
65 
66     // Wifi info
67     private static final String TEST_SSID = "TestWifi";
68     private static final boolean TEST_HIDDEN = true;
69     private static final String TEST_SECURITY_TYPE = "WPA2";
70     private static final String TEST_PASSWORD = "GoogleRock";
71     private static final String TEST_PROXY_HOST = "testhost.com";
72     private static final int TEST_PROXY_PORT = 7689;
73     private static final String TEST_PROXY_BYPASS_HOSTS = "http://host1.com;https://host2.com";
74     private static final String TEST_PAC_URL = "pac.test.com";
75     private static final WifiInfo TEST_WIFI_INFO = WifiInfo.Builder.builder()
76             .setSsid(TEST_SSID)
77             .setHidden(TEST_HIDDEN)
78             .setSecurityType(TEST_SECURITY_TYPE)
79             .setPassword(TEST_PASSWORD)
80             .setProxyHost(TEST_PROXY_HOST)
81             .setProxyPort(TEST_PROXY_PORT)
82             .setProxyBypassHosts(TEST_PROXY_BYPASS_HOSTS)
83             .setPacUrl(TEST_PAC_URL)
84             .build();
85 
86     // Device admin package download info
87     private static final String TEST_DOWNLOAD_LOCATION =
88             "http://example/dpc.apk";
89     private static final String TEST_COOKIE_HEADER =
90             "Set-Cookie: sessionToken=foobar; Expires=Thu, 18 Feb 2016 23:59:59 GMT";
91     private static final byte[] TEST_PACKAGE_CHECKSUM = new byte[] { '1', '2', '3', '4', '5' };
92     private static final byte[] TEST_SIGNATURE_CHECKSUM = new byte[] { '5', '4', '3', '2', '1' };
93     private static final int TEST_MIN_SUPPORT_VERSION = 17689;
94     private static final PackageDownloadInfo TEST_DOWNLOAD_INFO =
95             PackageDownloadInfo.Builder.builder()
96                     .setLocation(TEST_DOWNLOAD_LOCATION)
97                     .setCookieHeader(TEST_COOKIE_HEADER)
98                     .setPackageChecksum(TEST_PACKAGE_CHECKSUM)
99                     .setSignatureChecksum(TEST_SIGNATURE_CHECKSUM)
100                     .setMinVersion(TEST_MIN_SUPPORT_VERSION)
101                     .build();
102 
103     @Mock
104     private Utils mUtils;
105 
106     @Override
setUp()107     protected void setUp() throws Exception {
108         super.setUp();
109         MockitoAnnotations.initMocks(this);
110     }
111 
112     @SmallTest
testFailToConstructProvisioningParamsWithoutPackageNameOrComponentName()113     public void testFailToConstructProvisioningParamsWithoutPackageNameOrComponentName() {
114         // WHEN the ProvisioningParams is constructed by with neither a package name nor a component
115         // name
116         try {
117             ProvisioningParams provisioningParams = ProvisioningParams.Builder.builder()
118                     .setProvisioningAction(TEST_PROVISIONING_ACTION)
119                     .build();
120             fail("Package name or component name is mandatory.");
121         } catch (IllegalArgumentException e) {
122             // THEN the ProvisioningParams fails to construct.
123         }
124     }
125 
126     @SmallTest
testFailToConstructProvisioningParamsWithoutProvisioningAction()127     public void testFailToConstructProvisioningParamsWithoutProvisioningAction() {
128         // WHEN the ProvisioningParams is constructed by without a provisioning action.
129         try {
130             ProvisioningParams provisioningParams = ProvisioningParams.Builder.builder()
131                     .setDeviceAdminComponentName(TEST_COMPONENT_NAME)
132                     .build();
133             fail("Provisioning action is mandatory");
134         } catch (NullPointerException e) {
135             // THEN the ProvisioningParams fails to construct.
136         }
137     }
138 
139     @SmallTest
testEquals()140     public void testEquals() {
141         // GIVEN 2 ProvisioningParams objects created by the same set of parameters
142         ProvisioningParams provisioningParams1 = getCompleteProvisioningParams();
143         ProvisioningParams provisioningParams2 = getCompleteProvisioningParams();
144 
145         // WHEN these two objects compare.
146         // THEN they are the same.
147         assertThat(provisioningParams1).isEqualTo(provisioningParams2);
148     }
149 
150     @SmallTest
testNotEquals()151     public void testNotEquals() {
152         // GIVEN 2 ProvisioningParams objects created by different sets of parameters
153         ProvisioningParams provisioningParams1 = ProvisioningParams.Builder.builder()
154                 .setProvisioningAction(TEST_PROVISIONING_ACTION)
155                 .setDeviceAdminPackageName(TEST_PACKAGE_NAME)
156                 .setDeviceAdminComponentName(TEST_COMPONENT_NAME)
157                 .setDeviceAdminDownloadInfo(TEST_DOWNLOAD_INFO)
158                 .setLocalTime(TEST_LOCAL_TIME)
159                 .setLocale(TEST_LOCALE)
160                 .setTimeZone(TEST_TIME_ZONE)
161                 .setMainColor(TEST_MAIN_COLOR)
162                 .setStartedByTrustedSource(TEST_STARTED_BY_TRUSTED_SOURCE)
163                 .setLeaveAllSystemAppsEnabled(TEST_LEAVE_ALL_SYSTEM_APP_ENABLED)
164                 .setSkipEncryption(TEST_SKIP_ENCRYPTION)
165                 .setSkipUserSetup(TEST_SKIP_USER_SETUP)
166                 .setAccountToMigrate(TEST_ACCOUNT_TO_MIGRATE)
167                 .setWifiInfo(TEST_WIFI_INFO)
168                 .setUseMobileData(TEST_USE_MOBILE_DATA)
169                 .setAdminExtrasBundle(createTestAdminExtras())
170                 .build();
171         ProvisioningParams provisioningParams2 = ProvisioningParams.Builder.builder()
172                 .setProvisioningAction("different.action")
173                 .setDeviceAdminPackageName(TEST_PACKAGE_NAME)
174                 .setDeviceAdminComponentName(TEST_COMPONENT_NAME)
175                 .setDeviceAdminDownloadInfo(TEST_DOWNLOAD_INFO)
176                 .setLocalTime(TEST_LOCAL_TIME)
177                 .setLocale(TEST_LOCALE)
178                 .setTimeZone(TEST_TIME_ZONE)
179                 .setMainColor(TEST_MAIN_COLOR)
180                 .setStartedByTrustedSource(TEST_STARTED_BY_TRUSTED_SOURCE)
181                 .setLeaveAllSystemAppsEnabled(TEST_LEAVE_ALL_SYSTEM_APP_ENABLED)
182                 .setSkipEncryption(TEST_SKIP_ENCRYPTION)
183                 .setSkipUserSetup(TEST_SKIP_USER_SETUP)
184                 .setAccountToMigrate(TEST_ACCOUNT_TO_MIGRATE)
185                 .setWifiInfo(TEST_WIFI_INFO)
186                 .setUseMobileData(TEST_USE_MOBILE_DATA)
187                 .setAdminExtrasBundle(createTestAdminExtras())
188                 .build();
189 
190         // WHEN these two objects compare.
191         // THEN they are not the same.
192         assertThat(provisioningParams1).isNotEqualTo(provisioningParams2);
193     }
194 
195     @SmallTest
testSaveAndRestoreComplete()196     public void testSaveAndRestoreComplete() throws Exception {
197         testSaveAndRestore(getCompleteProvisioningParams());
198     }
199 
200     // Testing with a minimum set of parameters to cover all null use cases.
201     @SmallTest
testSaveAndRestoreMinimalist()202     public void testSaveAndRestoreMinimalist() throws Exception {
203         testSaveAndRestore(ProvisioningParams.Builder.builder()
204                 .setProvisioningAction(TEST_PROVISIONING_ACTION)
205                 .setDeviceAdminPackageName(TEST_PACKAGE_NAME)
206                 .build());
207     }
208 
testSaveAndRestore(ProvisioningParams original)209     private void testSaveAndRestore(ProvisioningParams original) {
210         // GIVEN a ProvisioningParams object
211         // WHEN the ProvisioningParams is written to xml and then read back
212         File file = new File(mContext.getFilesDir(), "test_store.xml");
213         original.save(file);
214         ProvisioningParams copy = ProvisioningParams.load(file);
215         // THEN the same ProvisioningParams is obtained
216         assertThat(original).isEqualTo(copy);
217     }
218 
219     @SmallTest
testParceable()220     public void testParceable() {
221         // GIVEN a ProvisioningParams object.
222         ProvisioningParams expectedProvisioningParams = getCompleteProvisioningParams();
223 
224         // WHEN the ProvisioningParams is written to parcel and then read back.
225         Parcel parcel = Parcel.obtain();
226         expectedProvisioningParams.writeToParcel(parcel, 0);
227         parcel.setDataPosition(0);
228         ProvisioningParams actualProvisioningParams =
229                 ProvisioningParams.CREATOR.createFromParcel(parcel);
230 
231         // THEN the same ProvisioningParams is obtained.
232         assertThat(expectedProvisioningParams).isEqualTo(actualProvisioningParams);
233     }
234 
235     @SmallTest
testInferDeviceAdminComponentName_componentNameIsGiven()236     public void testInferDeviceAdminComponentName_componentNameIsGiven()
237             throws IllegalProvisioningArgumentException {
238         ProvisioningParams provisioningParams = new
239                 ProvisioningParams.Builder()
240                 .setDeviceAdminComponentName(TEST_COMPONENT_NAME)
241                 .setProvisioningAction(DevicePolicyManager.ACTION_PROVISION_MANAGED_PROFILE)
242                 .build();
243 
244         assertThat(TEST_COMPONENT_NAME)
245                 .isEqualTo(provisioningParams.inferDeviceAdminComponentName(
246                         mUtils, mContext, UserHandle.myUserId()));
247     }
248 
249     @SmallTest
testInferDeviceAdminComponentName_componentNameIsNotGiven()250     public void testInferDeviceAdminComponentName_componentNameIsNotGiven()
251             throws IllegalProvisioningArgumentException {
252         ProvisioningParams provisioningParams = new
253                 ProvisioningParams.Builder()
254                 .setDeviceAdminPackageName(TEST_PACKAGE_NAME)
255                 .setProvisioningAction(DevicePolicyManager.ACTION_PROVISION_MANAGED_PROFILE)
256                 .build();
257 
258         when(mUtils.findDeviceAdmin(eq(TEST_PACKAGE_NAME), nullable(ComponentName.class),
259                 eq(mContext), eq(UserHandle.myUserId()))).thenReturn(TEST_COMPONENT_NAME);
260 
261         assertThat(TEST_COMPONENT_NAME)
262                 .isEqualTo(provisioningParams.inferDeviceAdminComponentName(
263                         mUtils, mContext, UserHandle.myUserId()));
264     }
265 
266     @SmallTest
testSetUseMobileData_true()267     public void testSetUseMobileData_true() {
268         ProvisioningParams provisioningParams =
269                 createDefaultProvisioningParamsBuilder().setUseMobileData(true).build();
270         assertThat(provisioningParams.useMobileData).isTrue();
271     }
272 
273     @SmallTest
testSetUseMobileData_false()274     public void testSetUseMobileData_false() {
275         ProvisioningParams provisioningParams =
276                 createDefaultProvisioningParamsBuilder().setUseMobileData(false).build();
277         assertThat(provisioningParams.useMobileData).isFalse();
278     }
279 
280     @SmallTest
testSetUseMobileData_defaultsToFalse()281     public void testSetUseMobileData_defaultsToFalse() {
282         assertThat(createDefaultProvisioningParamsBuilder().build().useMobileData).isFalse();
283     }
284 
createDefaultProvisioningParamsBuilder()285     private ProvisioningParams.Builder createDefaultProvisioningParamsBuilder() {
286         return ProvisioningParams.Builder
287                 .builder()
288                 .setProvisioningAction(TEST_PROVISIONING_ACTION)
289                 .setDeviceAdminComponentName(TEST_COMPONENT_NAME);
290     }
291 
getCompleteProvisioningParams()292     private ProvisioningParams getCompleteProvisioningParams() {
293         return ProvisioningParams.Builder.builder()
294                 .setProvisioningAction(TEST_PROVISIONING_ACTION)
295                 .setDeviceAdminPackageName(TEST_PACKAGE_NAME)
296                 .setDeviceAdminComponentName(TEST_COMPONENT_NAME)
297                 .setDeviceAdminDownloadInfo(TEST_DOWNLOAD_INFO)
298                 .setLocalTime(TEST_LOCAL_TIME)
299                 .setLocale(TEST_LOCALE)
300                 .setTimeZone(TEST_TIME_ZONE)
301                 .setMainColor(TEST_MAIN_COLOR)
302                 .setStartedByTrustedSource(TEST_STARTED_BY_TRUSTED_SOURCE)
303                 .setIsNfc(TEST_IS_NFC)
304                 .setLeaveAllSystemAppsEnabled(TEST_LEAVE_ALL_SYSTEM_APP_ENABLED)
305                 .setSkipEncryption(TEST_SKIP_ENCRYPTION)
306                 .setSkipUserSetup(TEST_SKIP_USER_SETUP)
307                 .setSkipUserConsent(TEST_SKIP_USER_CONSENT)
308                 .setAccountToMigrate(TEST_ACCOUNT_TO_MIGRATE)
309                 .setWifiInfo(TEST_WIFI_INFO)
310                 .setUseMobileData(TEST_USE_MOBILE_DATA)
311                 .setAdminExtrasBundle(createTestAdminExtras())
312                 .build();
313     }
314 }
315