1 /*
2  * Copyright (C) 2011 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 android.admin.cts;
18 
19 import android.app.admin.DevicePolicyManager;
20 import android.content.ComponentName;
21 import android.content.Context;
22 import android.content.Intent;
23 import android.content.IntentFilter;
24 import android.content.pm.ApplicationInfo;
25 import android.content.pm.PackageManager;
26 import android.content.pm.PackageManager.NameNotFoundException;
27 import android.content.pm.ResolveInfo;
28 import android.os.Build;
29 import android.os.UserManager;
30 import android.provider.Settings;
31 import android.test.AndroidTestCase;
32 import android.util.Log;
33 
34 import java.util.List;
35 
36 import static android.app.admin.DevicePolicyManager.PASSWORD_QUALITY_COMPLEX;
37 
38 /**
39  * Test that exercises {@link DevicePolicyManager}. The test requires that the
40  * CtsDeviceAdminReceiver be installed via the CtsDeviceAdmin.apk and be
41  * activated via "Settings > Location & security > Select device administrators".
42  */
43 public class DevicePolicyManagerTest extends AndroidTestCase {
44 
45     private static final String TAG = DevicePolicyManagerTest.class.getSimpleName();
46 
47     private DevicePolicyManager mDevicePolicyManager;
48     private ComponentName mComponent;
49     private ComponentName mSecondComponent;
50     private boolean mDeviceAdmin;
51     private boolean mManagedProfiles;
52     private PackageManager mPackageManager;
53 
54     private static final String TEST_CA_STRING1 =
55             "-----BEGIN CERTIFICATE-----\n" +
56             "MIICVzCCAgGgAwIBAgIJAMvnLHnnfO/IMA0GCSqGSIb3DQEBBQUAMIGGMQswCQYD\n" +
57             "VQQGEwJJTjELMAkGA1UECAwCQVAxDDAKBgNVBAcMA0hZRDEVMBMGA1UECgwMSU1G\n" +
58             "TCBQVlQgTFREMRAwDgYDVQQLDAdJTUZMIE9VMRIwEAYDVQQDDAlJTUZMLklORk8x\n" +
59             "HzAdBgkqhkiG9w0BCQEWEHJhbWVzaEBpbWZsLmluZm8wHhcNMTMwODI4MDk0NDA5\n" +
60             "WhcNMjMwODI2MDk0NDA5WjCBhjELMAkGA1UEBhMCSU4xCzAJBgNVBAgMAkFQMQww\n" +
61             "CgYDVQQHDANIWUQxFTATBgNVBAoMDElNRkwgUFZUIExURDEQMA4GA1UECwwHSU1G\n" +
62             "TCBPVTESMBAGA1UEAwwJSU1GTC5JTkZPMR8wHQYJKoZIhvcNAQkBFhByYW1lc2hA\n" +
63             "aW1mbC5pbmZvMFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAJ738cbTQlNIO7O6nV/f\n" +
64             "DJTMvWbPkyHYX8CQ7yXiAzEiZ5bzKJjDJmpRAkUrVinljKns2l6C4++l/5A7pFOO\n" +
65             "33kCAwEAAaNQME4wHQYDVR0OBBYEFOdbZP7LaMbgeZYPuds2CeSonmYxMB8GA1Ud\n" +
66             "IwQYMBaAFOdbZP7LaMbgeZYPuds2CeSonmYxMAwGA1UdEwQFMAMBAf8wDQYJKoZI\n" +
67             "hvcNAQEFBQADQQBdrk6J9koyylMtl/zRfiMAc2zgeC825fgP6421NTxs1rjLs1HG\n" +
68             "VcUyQ1/e7WQgOaBHi9TefUJi+4PSVSluOXon\n" +
69             "-----END CERTIFICATE-----";
70 
71     private static final String MANAGED_PROVISIONING_PKG = "com.android.managedprovisioning";
72 
73     @Override
setUp()74     protected void setUp() throws Exception {
75         super.setUp();
76         mDevicePolicyManager = (DevicePolicyManager)
77                 mContext.getSystemService(Context.DEVICE_POLICY_SERVICE);
78         mComponent = DeviceAdminInfoTest.getReceiverComponent();
79         mPackageManager = mContext.getPackageManager();
80         mSecondComponent = DeviceAdminInfoTest.getSecondReceiverComponent();
81         mDeviceAdmin = mPackageManager.hasSystemFeature(PackageManager.FEATURE_DEVICE_ADMIN);
82         mManagedProfiles = mDeviceAdmin
83                 && mPackageManager.hasSystemFeature(PackageManager.FEATURE_MANAGED_USERS);
84         setBlankPassword();
85     }
86 
87     @Override
tearDown()88     protected void tearDown() throws Exception {
89         super.tearDown();
90         setBlankPassword();
91     }
92 
setBlankPassword()93     private void setBlankPassword() {
94         if (!mDeviceAdmin) {
95             return;
96         }
97         // Reset the password to nothing for future tests...
98         mDevicePolicyManager.setPasswordQuality(mComponent,
99                 DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED);
100         mDevicePolicyManager.setPasswordMinimumLength(mComponent, 0);
101         assertTrue(mDevicePolicyManager.resetPassword("", 0));
102     }
103 
testGetActiveAdmins()104     public void testGetActiveAdmins() {
105         if (!mDeviceAdmin) {
106             Log.w(TAG, "Skipping testGetActiveAdmins");
107             return;
108         }
109         List<ComponentName> activeAdmins = mDevicePolicyManager.getActiveAdmins();
110         assertFalse(activeAdmins.isEmpty());
111         assertTrue(activeAdmins.contains(mComponent));
112         assertTrue(mDevicePolicyManager.isAdminActive(mComponent));
113     }
114 
testGetMaximumFailedPasswordsForWipe()115     public void testGetMaximumFailedPasswordsForWipe() {
116         if (!mDeviceAdmin) {
117             Log.w(TAG, "Skipping testGetMaximumFailedPasswordsForWipe");
118             return;
119         }
120         mDevicePolicyManager.setMaximumFailedPasswordsForWipe(mComponent, 3);
121         assertEquals(3, mDevicePolicyManager.getMaximumFailedPasswordsForWipe(mComponent));
122 
123         mDevicePolicyManager.setMaximumFailedPasswordsForWipe(mComponent, 5);
124         assertEquals(5, mDevicePolicyManager.getMaximumFailedPasswordsForWipe(mComponent));
125     }
126 
testPasswordQuality_something()127     public void testPasswordQuality_something() {
128         if (!mDeviceAdmin) {
129             Log.w(TAG, "Skipping testPasswordQuality_something");
130             return;
131         }
132         mDevicePolicyManager.setPasswordQuality(mComponent,
133                 DevicePolicyManager.PASSWORD_QUALITY_SOMETHING);
134         assertEquals(DevicePolicyManager.PASSWORD_QUALITY_SOMETHING,
135                 mDevicePolicyManager.getPasswordQuality(mComponent));
136         assertFalse(mDevicePolicyManager.isActivePasswordSufficient());
137 
138         String caseDescription = "initial";
139         assertPasswordSucceeds("1234", caseDescription);
140         assertPasswordSucceeds("abcd", caseDescription);
141         assertPasswordSucceeds("abcd1234", caseDescription);
142 
143         mDevicePolicyManager.setPasswordMinimumLength(mComponent, 10);
144         caseDescription = "minimum password length = 10";
145         assertEquals(10, mDevicePolicyManager.getPasswordMinimumLength(mComponent));
146         assertFalse(mDevicePolicyManager.isActivePasswordSufficient());
147 
148         assertPasswordFails("1234", caseDescription);
149         assertPasswordFails("abcd", caseDescription);
150         assertPasswordFails("abcd1234", caseDescription);
151 
152         mDevicePolicyManager.setPasswordMinimumLength(mComponent, 4);
153         caseDescription = "minimum password length = 4";
154         assertEquals(4, mDevicePolicyManager.getPasswordMinimumLength(
155                 mComponent));
156         assertTrue(mDevicePolicyManager.isActivePasswordSufficient());
157 
158         assertPasswordSucceeds("1234", caseDescription);
159         assertPasswordSucceeds("abcd", caseDescription);
160         assertPasswordSucceeds("abcd1234", caseDescription);
161     }
162 
testPasswordQuality_numeric()163     public void testPasswordQuality_numeric() {
164         if (!mDeviceAdmin) {
165             Log.w(TAG, "Skipping testPasswordQuality_numeric");
166             return;
167         }
168         mDevicePolicyManager.setPasswordQuality(mComponent,
169                 DevicePolicyManager.PASSWORD_QUALITY_NUMERIC);
170         assertEquals(DevicePolicyManager.PASSWORD_QUALITY_NUMERIC,
171                 mDevicePolicyManager.getPasswordQuality(mComponent));
172         assertFalse(mDevicePolicyManager.isActivePasswordSufficient());
173 
174         String caseDescription = "initial";
175         assertPasswordSucceeds("1234", caseDescription);
176         assertPasswordSucceeds("abcd", caseDescription);
177         assertPasswordSucceeds("abcd1234", caseDescription);
178 
179         mDevicePolicyManager.setPasswordMinimumLength(mComponent, 10);
180         caseDescription = "minimum password length = 10";
181         assertEquals(10, mDevicePolicyManager.getPasswordMinimumLength(mComponent));
182         assertFalse(mDevicePolicyManager.isActivePasswordSufficient());
183 
184         assertPasswordFails("1234", caseDescription);
185         assertPasswordFails("abcd", caseDescription);
186         assertPasswordFails("abcd1234", caseDescription);
187 
188         mDevicePolicyManager.setPasswordMinimumLength(mComponent, 4);
189         caseDescription = "minimum password length = 4";
190         assertEquals(4, mDevicePolicyManager.getPasswordMinimumLength(
191                 mComponent));
192         assertTrue(mDevicePolicyManager.isActivePasswordSufficient());
193 
194         assertPasswordSucceeds("1234", caseDescription);
195         assertPasswordSucceeds("abcd", caseDescription);
196         assertPasswordSucceeds("abcd1234", caseDescription);
197     }
198 
testPasswordQuality_alphabetic()199     public void testPasswordQuality_alphabetic() {
200         if (!mDeviceAdmin) {
201             Log.w(TAG, "Skipping testPasswordQuality_alphabetic");
202             return;
203         }
204         mDevicePolicyManager.setPasswordQuality(mComponent,
205                 DevicePolicyManager.PASSWORD_QUALITY_ALPHABETIC);
206         assertEquals(DevicePolicyManager.PASSWORD_QUALITY_ALPHABETIC,
207                 mDevicePolicyManager.getPasswordQuality(mComponent));
208         assertFalse(mDevicePolicyManager.isActivePasswordSufficient());
209 
210         String caseDescription = "initial";
211         assertPasswordFails("1234", caseDescription);
212         assertPasswordSucceeds("abcd", caseDescription);
213         assertPasswordSucceeds("abcd1234", caseDescription);
214 
215         mDevicePolicyManager.setPasswordMinimumLength(mComponent, 10);
216         caseDescription = "minimum password length = 10";
217         assertEquals(10, mDevicePolicyManager.getPasswordMinimumLength(mComponent));
218         assertFalse(mDevicePolicyManager.isActivePasswordSufficient());
219 
220         assertPasswordFails("1234", caseDescription);
221         assertPasswordFails("abcd", caseDescription);
222         assertPasswordFails("abcd1234", caseDescription);
223 
224         mDevicePolicyManager.setPasswordMinimumLength(mComponent, 4);
225         caseDescription = "minimum password length = 4";
226         assertEquals(4, mDevicePolicyManager.getPasswordMinimumLength(
227                 mComponent));
228         assertTrue(mDevicePolicyManager.isActivePasswordSufficient());
229 
230         assertPasswordFails("1234", caseDescription);
231         assertPasswordSucceeds("abcd", caseDescription);
232         assertPasswordSucceeds("abcd1234", caseDescription);
233     }
234 
testPasswordQuality_alphanumeric()235     public void testPasswordQuality_alphanumeric() {
236         if (!mDeviceAdmin) {
237             Log.w(TAG, "Skipping testPasswordQuality_alphanumeric");
238             return;
239         }
240         mDevicePolicyManager.setPasswordQuality(mComponent,
241                 DevicePolicyManager.PASSWORD_QUALITY_ALPHANUMERIC);
242         assertEquals(DevicePolicyManager.PASSWORD_QUALITY_ALPHANUMERIC,
243                 mDevicePolicyManager.getPasswordQuality(mComponent));
244         assertFalse(mDevicePolicyManager.isActivePasswordSufficient());
245 
246         String caseDescription = "initial";
247         assertPasswordFails("1234", caseDescription);
248         assertPasswordFails("abcd", caseDescription);
249         assertPasswordSucceeds("abcd1234", caseDescription);
250 
251         mDevicePolicyManager.setPasswordMinimumLength(mComponent, 10);
252         caseDescription = "minimum password length = 10";
253         assertEquals(10, mDevicePolicyManager.getPasswordMinimumLength(mComponent));
254         assertFalse(mDevicePolicyManager.isActivePasswordSufficient());
255 
256         assertPasswordFails("1234", caseDescription);
257         assertPasswordFails("abcd", caseDescription);
258         assertPasswordFails("abcd1234", caseDescription);
259 
260         mDevicePolicyManager.setPasswordMinimumLength(mComponent, 4);
261         caseDescription = "minimum password length = 4";
262         assertEquals(4, mDevicePolicyManager.getPasswordMinimumLength(
263                 mComponent));
264         assertTrue(mDevicePolicyManager.isActivePasswordSufficient());
265 
266         assertPasswordFails("1234", caseDescription);
267         assertPasswordFails("abcd", caseDescription);
268         assertPasswordSucceeds("abcd1234", caseDescription);
269     }
270 
testPasswordQuality_complexUpperCase()271     public void testPasswordQuality_complexUpperCase() {
272         if (!mDeviceAdmin) {
273             Log.w(TAG, "Skipping testPasswordQuality_complexUpperCase");
274             return;
275         }
276 
277         mDevicePolicyManager.setPasswordQuality(mComponent, PASSWORD_QUALITY_COMPLEX);
278         assertEquals(PASSWORD_QUALITY_COMPLEX, mDevicePolicyManager.getPasswordQuality(mComponent));
279         resetComplexPasswordRestrictions();
280 
281         String caseDescription = "minimum UpperCase=0";
282         assertPasswordSucceeds("abc1", caseDescription);
283         assertPasswordSucceeds("aBc1", caseDescription);
284         assertPasswordSucceeds("ABC1", caseDescription);
285         assertPasswordSucceeds("ABCD", caseDescription);
286         assertPasswordFails("123", caseDescription); // too short
287 
288         mDevicePolicyManager.setPasswordMinimumUpperCase(mComponent, 1);
289         assertEquals(1, mDevicePolicyManager.getPasswordMinimumUpperCase(mComponent));
290         caseDescription = "minimum UpperCase=1";
291         assertPasswordFails("abc1", caseDescription);
292         assertPasswordSucceeds("aBc1", caseDescription);
293         assertPasswordSucceeds("ABC1", caseDescription);
294         assertPasswordSucceeds("ABCD", caseDescription);
295         assertPasswordFails("123", caseDescription); // too short
296 
297         mDevicePolicyManager.setPasswordMinimumUpperCase(mComponent, 3);
298         assertEquals(3, mDevicePolicyManager.getPasswordMinimumUpperCase(mComponent));
299         caseDescription = "minimum UpperCase=3";
300         assertPasswordFails("abc1", caseDescription);
301         assertPasswordFails("aBC1", caseDescription);
302         assertPasswordSucceeds("ABC1", caseDescription);
303         assertPasswordSucceeds("ABCD", caseDescription);
304         assertPasswordFails("123", caseDescription); // too short
305     }
306 
testPasswordQuality_complexLowerCase()307     public void testPasswordQuality_complexLowerCase() {
308         if (!mDeviceAdmin) {
309             Log.w(TAG, "Skipping testPasswordQuality_complexLowerCase");
310             return;
311         }
312 
313         mDevicePolicyManager.setPasswordQuality(mComponent, PASSWORD_QUALITY_COMPLEX);
314         assertEquals(PASSWORD_QUALITY_COMPLEX, mDevicePolicyManager.getPasswordQuality(mComponent));
315         resetComplexPasswordRestrictions();
316 
317         String caseDescription = "minimum LowerCase=0";
318         assertPasswordSucceeds("ABCD", caseDescription);
319         assertPasswordSucceeds("aBC1", caseDescription);
320         assertPasswordSucceeds("abc1", caseDescription);
321         assertPasswordSucceeds("abcd", caseDescription);
322         assertPasswordFails("123", caseDescription); // too short
323 
324         mDevicePolicyManager.setPasswordMinimumLowerCase(mComponent, 1);
325         assertEquals(1, mDevicePolicyManager.getPasswordMinimumLowerCase(mComponent));
326         caseDescription = "minimum LowerCase=1";
327         assertPasswordFails("ABCD", caseDescription);
328         assertPasswordSucceeds("aBC1", caseDescription);
329         assertPasswordSucceeds("abc1", caseDescription);
330         assertPasswordSucceeds("abcd", caseDescription);
331         assertPasswordFails("123", caseDescription); // too short
332 
333         mDevicePolicyManager.setPasswordMinimumLowerCase(mComponent, 3);
334         assertEquals(3, mDevicePolicyManager.getPasswordMinimumLowerCase(mComponent));
335         caseDescription = "minimum LowerCase=3";
336         assertPasswordFails("ABCD", caseDescription);
337         assertPasswordFails("aBC1", caseDescription);
338         assertPasswordSucceeds("abc1", caseDescription);
339         assertPasswordSucceeds("abcd", caseDescription);
340         assertPasswordFails("123", caseDescription); // too short
341     }
342 
testPasswordQuality_complexLetters()343     public void testPasswordQuality_complexLetters() {
344         if (!mDeviceAdmin) {
345             Log.w(TAG, "Skipping testPasswordQuality_complexLetters");
346             return;
347         }
348 
349         mDevicePolicyManager.setPasswordQuality(mComponent, PASSWORD_QUALITY_COMPLEX);
350         assertEquals(PASSWORD_QUALITY_COMPLEX, mDevicePolicyManager.getPasswordQuality(mComponent));
351         resetComplexPasswordRestrictions();
352 
353         String caseDescription = "minimum Letters=0";
354         assertPasswordSucceeds("1234", caseDescription);
355         assertPasswordSucceeds("a123", caseDescription);
356         assertPasswordSucceeds("abc1", caseDescription);
357         assertPasswordSucceeds("abcd", caseDescription);
358         assertPasswordFails("123", caseDescription); // too short
359 
360         mDevicePolicyManager.setPasswordMinimumLetters(mComponent, 1);
361         assertEquals(1, mDevicePolicyManager.getPasswordMinimumLetters(mComponent));
362         caseDescription = "minimum Letters=1";
363         assertPasswordFails("1234", caseDescription);
364         assertPasswordSucceeds("a123", caseDescription);
365         assertPasswordSucceeds("abc1", caseDescription);
366         assertPasswordSucceeds("abcd", caseDescription);
367         assertPasswordFails("123", caseDescription); // too short
368 
369         mDevicePolicyManager.setPasswordMinimumLetters(mComponent, 3);
370         assertEquals(3, mDevicePolicyManager.getPasswordMinimumLetters(mComponent));
371         caseDescription = "minimum Letters=3";
372         assertPasswordFails("1234", caseDescription);
373         assertPasswordFails("a123", caseDescription);
374         assertPasswordSucceeds("abc1", caseDescription);
375         assertPasswordSucceeds("abcd", caseDescription);
376         assertPasswordFails("123", caseDescription); // too short
377     }
378 
testPasswordQuality_complexNumeric()379     public void testPasswordQuality_complexNumeric() {
380         if (!mDeviceAdmin) {
381             Log.w(TAG, "Skipping testPasswordQuality_complexNumeric");
382             return;
383         }
384 
385         mDevicePolicyManager.setPasswordQuality(mComponent, PASSWORD_QUALITY_COMPLEX);
386         assertEquals(PASSWORD_QUALITY_COMPLEX, mDevicePolicyManager.getPasswordQuality(mComponent));
387         resetComplexPasswordRestrictions();
388 
389         String caseDescription = "minimum Numeric=0";
390         assertPasswordSucceeds("abcd", caseDescription);
391         assertPasswordSucceeds("1abc", caseDescription);
392         assertPasswordSucceeds("123a", caseDescription);
393         assertPasswordSucceeds("1234", caseDescription);
394         assertPasswordFails("123", caseDescription); // too short
395 
396         mDevicePolicyManager.setPasswordMinimumNumeric(mComponent, 1);
397         assertEquals(1, mDevicePolicyManager.getPasswordMinimumNumeric(mComponent));
398         caseDescription = "minimum Numeric=1";
399         assertPasswordFails("abcd", caseDescription);
400         assertPasswordSucceeds("1abc", caseDescription);
401         assertPasswordSucceeds("123a", caseDescription);
402         assertPasswordSucceeds("1234", caseDescription);
403         assertPasswordFails("123", caseDescription); // too short
404 
405         mDevicePolicyManager.setPasswordMinimumNumeric(mComponent, 3);
406         assertEquals(3, mDevicePolicyManager.getPasswordMinimumNumeric(mComponent));
407         caseDescription = "minimum Numeric=3";
408         assertPasswordFails("abcd", caseDescription);
409         assertPasswordFails("1abc", caseDescription);
410         assertPasswordSucceeds("123a", caseDescription);
411         assertPasswordSucceeds("1234", caseDescription);
412         assertPasswordFails("123", caseDescription); // too short
413     }
414 
testPasswordQuality_complexSymbols()415     public void testPasswordQuality_complexSymbols() {
416         if (!mDeviceAdmin) {
417             Log.w(TAG, "Skipping testPasswordQuality_complexSymbols");
418             return;
419         }
420 
421         mDevicePolicyManager.setPasswordQuality(mComponent, PASSWORD_QUALITY_COMPLEX);
422         assertEquals(PASSWORD_QUALITY_COMPLEX, mDevicePolicyManager.getPasswordQuality(mComponent));
423         resetComplexPasswordRestrictions();
424 
425         String caseDescription = "minimum Symbols=0";
426         assertPasswordSucceeds("abcd", caseDescription);
427         assertPasswordSucceeds("_bc1", caseDescription);
428         assertPasswordSucceeds("@#!1", caseDescription);
429         assertPasswordSucceeds("_@#!", caseDescription);
430         assertPasswordFails("123", caseDescription); // too short
431 
432         mDevicePolicyManager.setPasswordMinimumSymbols(mComponent, 1);
433         assertEquals(1, mDevicePolicyManager.getPasswordMinimumSymbols(mComponent));
434         caseDescription = "minimum Symbols=1";
435         assertPasswordFails("abcd", caseDescription);
436         assertPasswordSucceeds("_bc1", caseDescription);
437         assertPasswordSucceeds("@#!1", caseDescription);
438         assertPasswordSucceeds("_@#!", caseDescription);
439         assertPasswordFails("123", caseDescription); // too short
440 
441         mDevicePolicyManager.setPasswordMinimumSymbols(mComponent, 3);
442         assertEquals(3, mDevicePolicyManager.getPasswordMinimumSymbols(mComponent));
443         caseDescription = "minimum Symbols=3";
444         assertPasswordFails("abcd", caseDescription);
445         assertPasswordFails("_bc1", caseDescription);
446         assertPasswordSucceeds("@#!1", caseDescription);
447         assertPasswordSucceeds("_@#!", caseDescription);
448         assertPasswordFails("123", caseDescription); // too short
449     }
450 
testPasswordQuality_complexNonLetter()451     public void testPasswordQuality_complexNonLetter() {
452         if (!mDeviceAdmin) {
453             Log.w(TAG, "Skipping testPasswordQuality_complexNonLetter");
454             return;
455         }
456 
457         mDevicePolicyManager.setPasswordQuality(mComponent, PASSWORD_QUALITY_COMPLEX);
458         assertEquals(PASSWORD_QUALITY_COMPLEX, mDevicePolicyManager.getPasswordQuality(mComponent));
459         resetComplexPasswordRestrictions();
460 
461         String caseDescription = "minimum NonLetter=0";
462         assertPasswordSucceeds("Abcd", caseDescription);
463         assertPasswordSucceeds("_bcd", caseDescription);
464         assertPasswordSucceeds("3bcd", caseDescription);
465         assertPasswordSucceeds("_@3c", caseDescription);
466         assertPasswordSucceeds("_25!", caseDescription);
467         assertPasswordFails("123", caseDescription); // too short
468 
469         mDevicePolicyManager.setPasswordMinimumNonLetter(mComponent, 1);
470         assertEquals(1, mDevicePolicyManager.getPasswordMinimumNonLetter(mComponent));
471         caseDescription = "minimum NonLetter=1";
472         assertPasswordFails("Abcd", caseDescription);
473         assertPasswordSucceeds("_bcd", caseDescription);
474         assertPasswordSucceeds("3bcd", caseDescription);
475         assertPasswordSucceeds("_@3c", caseDescription);
476         assertPasswordSucceeds("_25!", caseDescription);
477         assertPasswordFails("123", caseDescription); // too short
478 
479         mDevicePolicyManager.setPasswordMinimumNonLetter(mComponent, 3);
480         assertEquals(3, mDevicePolicyManager.getPasswordMinimumNonLetter(mComponent));
481         caseDescription = "minimum NonLetter=3";
482         assertPasswordFails("Abcd", caseDescription);
483         assertPasswordFails("_bcd", caseDescription);
484         assertPasswordFails("3bcd", caseDescription);
485         assertPasswordSucceeds("_@3c", caseDescription);
486         assertPasswordSucceeds("_25!", caseDescription);
487         assertPasswordFails("123", caseDescription); // too short
488     }
489 
testPasswordHistoryLength()490     public void testPasswordHistoryLength() {
491         if (!mDeviceAdmin) {
492             Log.w(TAG, "Skipping testPasswordHistoryLength");
493             return;
494         }
495         // Password history length restriction is only imposed if password quality is at least
496         // numeric.
497         mDevicePolicyManager.setPasswordQuality(mComponent,
498                 DevicePolicyManager.PASSWORD_QUALITY_ALPHABETIC);
499         int originalValue = mDevicePolicyManager.getPasswordHistoryLength(mComponent);
500         try {
501             mDevicePolicyManager.setPasswordHistoryLength(mComponent, 3);
502             assertEquals(3, mDevicePolicyManager.getPasswordHistoryLength(mComponent));
503             // Although it would make sense we cannot test if password history restrictions
504             // are enforced as DevicePolicyManagerService.resetPassword fails to do so at the
505             // moment. See b/17707820
506         } finally {
507             mDevicePolicyManager.setPasswordHistoryLength(mComponent, originalValue);
508         }
509     }
510 
testPasswordExpirationTimeout()511     public void testPasswordExpirationTimeout() {
512         if (!mDeviceAdmin) {
513             Log.w(TAG, "Skipping testPasswordExpirationTimeout");
514             return;
515         }
516         long originalValue = mDevicePolicyManager.getPasswordExpirationTimeout(mComponent);
517         try {
518             for (long testLength : new long[] {
519                     0L, 864000000L /* ten days */, 8640000000L /* 100 days */}) {
520                 mDevicePolicyManager.setPasswordExpirationTimeout(mComponent, testLength);
521                 assertEquals(testLength,
522                         mDevicePolicyManager.getPasswordExpirationTimeout(mComponent));
523             }
524         } finally {
525             mDevicePolicyManager.setPasswordExpirationTimeout(mComponent, originalValue);
526         }
527     }
528 
testKeyguardDisabledFeatures()529     public void testKeyguardDisabledFeatures() {
530         if (!mDeviceAdmin) {
531             Log.w(TAG, "Skipping testKeyguardDisabledFeatures");
532             return;
533         }
534         int originalValue = mDevicePolicyManager.getKeyguardDisabledFeatures(mComponent);
535         try {
536             for (int which = DevicePolicyManager.KEYGUARD_DISABLE_FEATURES_NONE;
537                     which < 2 * DevicePolicyManager.KEYGUARD_DISABLE_FINGERPRINT; ++which) {
538                 mDevicePolicyManager.setKeyguardDisabledFeatures(mComponent, which);
539                 assertEquals(which, mDevicePolicyManager.getKeyguardDisabledFeatures(mComponent));
540             }
541         } finally {
542             mDevicePolicyManager.setKeyguardDisabledFeatures(mComponent, originalValue);
543         }
544     }
545 
testCreateUser_failIfNotDeviceOwner()546     public void testCreateUser_failIfNotDeviceOwner() {
547         if (!mDeviceAdmin) {
548             Log.w(TAG, "Skipping testCreateUser_failIfNotDeviceOwner");
549             return;
550         }
551         try {
552             mDevicePolicyManager.createUser(mComponent, "user name");
553             fail("did not throw expected SecurityException");
554         } catch (SecurityException e) {
555             assertDeviceOwnerMessage(e.getMessage());
556         }
557     }
558 
testRemoveUser_failIfNotDeviceOwner()559     public void testRemoveUser_failIfNotDeviceOwner() {
560         if (!mDeviceAdmin) {
561             Log.w(TAG, "Skipping testRemoveUser_failIfNotDeviceOwner");
562             return;
563         }
564         try {
565             mDevicePolicyManager.removeUser(mComponent, null);
566             fail("did not throw expected SecurityException");
567         } catch (SecurityException e) {
568             assertDeviceOwnerMessage(e.getMessage());
569         }
570     }
571 
testSetApplicationHidden_failIfNotDeviceOrProfileOwner()572     public void testSetApplicationHidden_failIfNotDeviceOrProfileOwner() {
573         if (!mDeviceAdmin) {
574             Log.w(TAG, "Skipping testSetApplicationHidden_failIfNotDeviceOrProfileOwner");
575             return;
576         }
577         try {
578             mDevicePolicyManager.setApplicationHidden(mComponent, "com.google.anything", true);
579             fail("did not throw expected SecurityException");
580         } catch (SecurityException e) {
581             assertProfileOwnerMessage(e.getMessage());
582         }
583     }
584 
testIsApplicationHidden_failIfNotDeviceOrProfileOwner()585     public void testIsApplicationHidden_failIfNotDeviceOrProfileOwner() {
586         if (!mDeviceAdmin) {
587             Log.w(TAG, "Skipping testIsApplicationHidden_failIfNotDeviceOrProfileOwner");
588             return;
589         }
590         try {
591             mDevicePolicyManager.isApplicationHidden(mComponent, "com.google.anything");
592             fail("did not throw expected SecurityException");
593         } catch (SecurityException e) {
594             assertProfileOwnerMessage(e.getMessage());
595         }
596     }
597 
testSetGlobalSetting_failIfNotDeviceOwner()598     public void testSetGlobalSetting_failIfNotDeviceOwner() {
599         if (!mDeviceAdmin) {
600             Log.w(TAG, "Skipping testSetGlobalSetting_failIfNotDeviceOwner");
601             return;
602         }
603         try {
604             mDevicePolicyManager.setGlobalSetting(mComponent,
605                     Settings.Global.WIFI_NETWORKS_AVAILABLE_NOTIFICATION_ON, "1");
606             fail("did not throw expected SecurityException");
607         } catch (SecurityException e) {
608             assertDeviceOwnerMessage(e.getMessage());
609         }
610     }
611 
testSetSecureSetting_failIfNotDeviceOrProfileOwner()612     public void testSetSecureSetting_failIfNotDeviceOrProfileOwner() {
613         if (!mDeviceAdmin) {
614             Log.w(TAG, "Skipping testSetSecureSetting_failIfNotDeviceOrProfileOwner");
615             return;
616         }
617         try {
618             mDevicePolicyManager.setSecureSetting(mComponent,
619                     Settings.Secure.INSTALL_NON_MARKET_APPS, "1");
620             fail("did not throw expected SecurityException");
621         } catch (SecurityException e) {
622             assertProfileOwnerMessage(e.getMessage());
623         }
624     }
625 
testSetMasterVolumeMuted_failIfNotDeviceOrProfileOwner()626     public void testSetMasterVolumeMuted_failIfNotDeviceOrProfileOwner() {
627         if (!mDeviceAdmin) {
628             Log.w(TAG, "Skipping testSetMasterVolumeMuted_failIfNotDeviceOrProfileOwner");
629             return;
630         }
631         try {
632             mDevicePolicyManager.setMasterVolumeMuted(mComponent, true);
633             fail("did not throw expected SecurityException");
634         } catch (SecurityException e) {
635             assertProfileOwnerMessage(e.getMessage());
636         }
637     }
638 
testIsMasterVolumeMuted_failIfNotDeviceOrProfileOwner()639     public void testIsMasterVolumeMuted_failIfNotDeviceOrProfileOwner() {
640         if (!mDeviceAdmin) {
641             Log.w(TAG, "Skipping testSetMasterVolumeMuted_failIfNotDeviceOrProfileOwner");
642             return;
643         }
644         try {
645             mDevicePolicyManager.isMasterVolumeMuted(mComponent);
646             fail("did not throw expected SecurityException");
647         } catch (SecurityException e) {
648             assertProfileOwnerMessage(e.getMessage());
649         }
650     }
651 
testSetRecommendedGlobalProxy_failIfNotDeviceOwner()652     public void testSetRecommendedGlobalProxy_failIfNotDeviceOwner() {
653         if (!mDeviceAdmin) {
654             Log.w(TAG, "Skipping testSetRecommendedGlobalProxy_failIfNotDeviceOwner");
655             return;
656         }
657         try {
658             mDevicePolicyManager.setRecommendedGlobalProxy(mComponent, null);
659             fail("did not throw expected SecurityException");
660         } catch (SecurityException e) {
661             assertDeviceOwnerMessage(e.getMessage());
662         }
663     }
664 
testSetLockTaskPackages_failIfNotDeviceOwner()665     public void testSetLockTaskPackages_failIfNotDeviceOwner() {
666         if (!mDeviceAdmin) {
667             Log.w(TAG, "Skipping testSetLockTaskPackages_failIfNotDeviceOwner");
668             return;
669         }
670         try {
671             mDevicePolicyManager.setLockTaskPackages(mComponent, new String[] {"package"});
672             fail("did not throw expected SecurityException");
673         } catch (SecurityException e) {
674         }
675     }
676 
testClearDeviceOwnerApp_failIfNotDeviceOwner()677     public void testClearDeviceOwnerApp_failIfNotDeviceOwner() {
678         if (!mDeviceAdmin) {
679             Log.w(TAG, "Skipping testClearDeviceOwnerApp_failIfNotDeviceOwner");
680             return;
681         }
682         try {
683             mDevicePolicyManager.clearDeviceOwnerApp("android.deviceadmin.cts");
684             fail("did not throw expected SecurityException");
685         } catch (SecurityException e) {
686             assertDeviceOwnerMessage(e.getMessage());
687         }
688     }
689 
testSwitchUser_failIfNotDeviceOwner()690     public void testSwitchUser_failIfNotDeviceOwner() {
691         if (!mDeviceAdmin) {
692             Log.w(TAG, "Skipping testSwitchUser_failIfNotDeviceOwner");
693             return;
694         }
695         try {
696             mDevicePolicyManager.switchUser(mComponent, null);
697             fail("did not throw expected SecurityException");
698         } catch (SecurityException e) {
699             assertDeviceOwnerMessage(e.getMessage());
700         }
701     }
702 
testCreateAndInitializeUser_failIfNotDeviceOwner()703     public void testCreateAndInitializeUser_failIfNotDeviceOwner() {
704         if (!mDeviceAdmin) {
705             Log.w(TAG, "Skipping testCreateAndInitializeUser_failIfNotDeviceOwner");
706             return;
707         }
708         try {
709             mDevicePolicyManager.createAndInitializeUser(mComponent, "name", "admin name",
710                         mComponent, null);
711             fail("did not throw expected SecurityException");
712         } catch (SecurityException e) {
713             assertDeviceOwnerMessage(e.getMessage());
714         }
715     }
716 
testInstallCaCert_failIfNotProfileOwner()717     public void testInstallCaCert_failIfNotProfileOwner() {
718         if (!mDeviceAdmin) {
719             Log.w(TAG, "Skipping testInstallCaCert_failIfNotProfileOwner");
720             return;
721         }
722         try {
723             mDevicePolicyManager.installCaCert(mComponent,
724                     TEST_CA_STRING1.getBytes());
725             fail("did not throw expected SecurityException");
726         } catch (SecurityException e) {
727             assertProfileOwnerMessage(e.getMessage());
728         }
729     }
730 
testInstallCaCert_failIfNotCertInstaller()731     public void testInstallCaCert_failIfNotCertInstaller() {
732         if (!mDeviceAdmin) {
733             Log.w(TAG, "Skipping testInstallCaCert_failIfNotCertInstaller");
734             return;
735         }
736         try {
737             // Delegated cert installer is identified by using null as the first argument.
738             mDevicePolicyManager.installCaCert(null, TEST_CA_STRING1.getBytes());
739             fail("did not throw expected SecurityException");
740         } catch (SecurityException expected) {
741         }
742     }
743 
testUninstallCaCert_failIfNotProfileOwner()744     public void testUninstallCaCert_failIfNotProfileOwner() {
745         if (!mDeviceAdmin) {
746             Log.w(TAG, "Skipping testUninstallCaCert_failIfNotProfileOwner");
747             return;
748         }
749         try {
750             mDevicePolicyManager.uninstallCaCert(mComponent,
751                     TEST_CA_STRING1.getBytes());
752             fail("did not throw expected SecurityException");
753         } catch (SecurityException e) {
754             assertProfileOwnerMessage(e.getMessage());
755         }
756     }
757 
testUninstallCaCert_failIfNotCertInstaller()758     public void testUninstallCaCert_failIfNotCertInstaller() {
759         if (!mDeviceAdmin) {
760             Log.w(TAG, "Skipping testUninstallCaCert_failIfNotCertInstaller");
761             return;
762         }
763         try {
764             // Delegated cert installer is identified by using null as the first argument.
765             mDevicePolicyManager.uninstallCaCert(null, TEST_CA_STRING1.getBytes());
766             fail("did not throw expected SecurityException");
767         } catch (SecurityException expected) {
768         }
769     }
770 
testGetInstalledCaCerts_failIfNotProfileOwner()771     public void testGetInstalledCaCerts_failIfNotProfileOwner() {
772         if (!mDeviceAdmin) {
773             Log.w(TAG, "Skipping testGetInstalledCaCerts_failIfNotProfileOwner");
774             return;
775         }
776         try {
777             mDevicePolicyManager.getInstalledCaCerts(mComponent);
778             fail("did not throw expected SecurityException");
779         } catch (SecurityException e) {
780             assertProfileOwnerMessage(e.getMessage());
781         }
782     }
783 
testGetInstalledCaCerts_failIfNotCertInstaller()784     public void testGetInstalledCaCerts_failIfNotCertInstaller() {
785         if (!mDeviceAdmin) {
786             Log.w(TAG, "Skipping testGetInstalledCaCerts_failIfNotCertInstaller");
787             return;
788         }
789         try {
790             // Delegated cert installer is identified by using null as the first argument.
791             mDevicePolicyManager.getInstalledCaCerts(null);
792             fail("did not throw expected SecurityException");
793         } catch (SecurityException expected) {
794         }
795     }
796 
testHasCaCertInstalled_failIfNotProfileOwner()797     public void testHasCaCertInstalled_failIfNotProfileOwner() {
798         if (!mDeviceAdmin) {
799             Log.w(TAG, "Skipping testHasCaCertInstalled_failIfNotProfileOwner");
800             return;
801         }
802         try {
803             mDevicePolicyManager.hasCaCertInstalled(mComponent,
804                     TEST_CA_STRING1.getBytes());
805             fail("did not throw expected SecurityException");
806         } catch (SecurityException e) {
807             assertProfileOwnerMessage(e.getMessage());
808         }
809     }
810 
testHasCaCertInstalled_failIfNotCertInstaller()811     public void testHasCaCertInstalled_failIfNotCertInstaller() {
812         if (!mDeviceAdmin) {
813             Log.w(TAG, "Skipping testHasCaCertInstalled_failIfNotCertInstaller");
814             return;
815         }
816         try {
817             // Delegated cert installer is identified by using null as the first argument.
818             mDevicePolicyManager.hasCaCertInstalled(null, TEST_CA_STRING1.getBytes());
819             fail("did not throw expected SecurityException");
820         } catch (SecurityException expected) {
821         }
822     }
823 
testUninstallAllUserCaCerts_failIfNotProfileOwner()824     public void testUninstallAllUserCaCerts_failIfNotProfileOwner() {
825         if (!mDeviceAdmin) {
826             Log.w(TAG, "Skipping testUninstallAllUserCaCerts_failIfNotProfileOwner");
827             return;
828         }
829         try {
830             mDevicePolicyManager.uninstallAllUserCaCerts(mComponent);
831             fail("did not throw expected SecurityException");
832         } catch (SecurityException e) {
833             assertProfileOwnerMessage(e.getMessage());
834         }
835     }
836 
testUninstallAllUserCaCerts_failIfNotCertInstaller()837     public void testUninstallAllUserCaCerts_failIfNotCertInstaller() {
838         if (!mDeviceAdmin) {
839             Log.w(TAG, "Skipping testUninstallAllUserCaCerts_failIfNotCertInstaller");
840             return;
841         }
842         try {
843             // Delegated cert installer is identified by using null as the first argument.
844             mDevicePolicyManager.uninstallAllUserCaCerts(null);
845             fail("did not throw expected SecurityException");
846         } catch (SecurityException expected) {
847         }
848     }
849 
testSetScreenCaptureDisabled_failIfNotProfileOwner()850     public void testSetScreenCaptureDisabled_failIfNotProfileOwner() {
851         if (!mDeviceAdmin) {
852             Log.w(TAG, "Skipping testSetScreenCaptureDisabled_failIfNotProfileOwner");
853             return;
854         }
855         try {
856             mDevicePolicyManager.setScreenCaptureDisabled(mComponent, true);
857             fail("did not throw expected SecurityException");
858         } catch (SecurityException e) {
859             assertProfileOwnerMessage(e.getMessage());
860         }
861     }
862 
testSetAutoTimeRequired_failIfNotDeviceOwner()863     public void testSetAutoTimeRequired_failIfNotDeviceOwner() {
864         if (!mDeviceAdmin) {
865             Log.w(TAG, "Skipping testSetAutoTimeRequired_failIfNotDeviceOwner");
866             return;
867         }
868         try {
869             mDevicePolicyManager.setAutoTimeRequired(mComponent, true);
870             fail("did not throw expected SecurityException");
871         } catch (SecurityException e) {
872             assertDeviceOwnerMessage(e.getMessage());
873         }
874     }
875 
testAddPersistentPreferredActivity_failIfNotProfileOwner()876     public void testAddPersistentPreferredActivity_failIfNotProfileOwner() {
877         if (!mDeviceAdmin) {
878             Log.w(TAG, "Skipping testAddPersistentPreferredActivity_failIfNotProfileOwner");
879             return;
880         }
881         try {
882             mDevicePolicyManager.addPersistentPreferredActivity(mComponent,
883                     new IntentFilter(Intent.ACTION_MAIN),
884                     new ComponentName("android.admin.cts", "dummy"));
885             fail("did not throw expected SecurityException");
886         } catch (SecurityException e) {
887             assertProfileOwnerMessage(e.getMessage());
888         }
889     }
890 
testClearPackagePersistentPreferredActivities_failIfNotProfileOwner()891     public void testClearPackagePersistentPreferredActivities_failIfNotProfileOwner() {
892         if (!mDeviceAdmin) {
893             Log.w(TAG, "Skipping testClearPackagePersistentPreferredActivities_failIfNotProfileOwner");
894             return;
895         }
896         try {
897             mDevicePolicyManager.clearPackagePersistentPreferredActivities(mComponent,
898                     "android.admin.cts");
899             fail("did not throw expected SecurityException");
900         } catch (SecurityException e) {
901             assertProfileOwnerMessage(e.getMessage());
902         }
903     }
904 
testSetApplicationRestrictions_failIfNotProfileOwner()905     public void testSetApplicationRestrictions_failIfNotProfileOwner() {
906         if (!mDeviceAdmin) {
907             Log.w(TAG, "Skipping testSetApplicationRestrictions_failIfNotProfileOwner");
908             return;
909         }
910         try {
911             mDevicePolicyManager.setApplicationRestrictions(mComponent,
912                     "android.admin.cts", null);
913             fail("did not throw expected SecurityException");
914         } catch (SecurityException e) {
915             assertProfileOwnerMessage(e.getMessage());
916         }
917     }
918 
testAddUserRestriction_failIfNotProfileOwner()919     public void testAddUserRestriction_failIfNotProfileOwner() {
920         if (!mDeviceAdmin) {
921             Log.w(TAG, "Skipping testAddUserRestriction_failIfNotProfileOwner");
922             return;
923         }
924         try {
925             mDevicePolicyManager.addUserRestriction(mComponent,
926                     UserManager.DISALLOW_SMS);
927             fail("did not throw expected SecurityException");
928         } catch (SecurityException e) {
929             assertProfileOwnerMessage(e.getMessage());
930         }
931     }
932 
testSetAccountManagementDisabled_failIfNotProfileOwner()933     public void testSetAccountManagementDisabled_failIfNotProfileOwner() {
934         if (!mDeviceAdmin) {
935             Log.w(TAG, "Skipping testSetAccountManagementDisabled_failIfNotProfileOwner");
936             return;
937         }
938         try {
939             mDevicePolicyManager.setAccountManagementDisabled(mComponent,
940                     "dummy", true);
941             fail("did not throw expected SecurityException");
942         } catch (SecurityException e) {
943             assertProfileOwnerMessage(e.getMessage());
944         }
945     }
946 
testSetRestrictionsProvider_failIfNotProfileOwner()947     public void testSetRestrictionsProvider_failIfNotProfileOwner() {
948         if (!mDeviceAdmin) {
949             Log.w(TAG, "Skipping testSetRestrictionsProvider_failIfNotProfileOwner");
950             return;
951         }
952         try {
953             mDevicePolicyManager.setRestrictionsProvider(mComponent,
954                     new ComponentName("android.admin.cts", "dummy"));
955             fail("did not throw expected SecurityException");
956         } catch (SecurityException e) {
957             assertProfileOwnerMessage(e.getMessage());
958         }
959     }
960 
testSetUninstallBlocked_failIfNotProfileOwner()961     public void testSetUninstallBlocked_failIfNotProfileOwner() {
962         if (!mDeviceAdmin) {
963             Log.w(TAG, "Skipping testSetUninstallBlocked_failIfNotProfileOwner");
964             return;
965         }
966         try {
967             mDevicePolicyManager.setUninstallBlocked(mComponent,
968                     "android.admin.cts", true);
969             fail("did not throw expected SecurityException");
970         } catch (SecurityException e) {
971             assertProfileOwnerMessage(e.getMessage());
972         }
973     }
974 
testSetPermittedAccessibilityServices_failIfNotProfileOwner()975     public void testSetPermittedAccessibilityServices_failIfNotProfileOwner() {
976         if (!mDeviceAdmin) {
977             Log.w(TAG, "Skipping testSetPermittedAccessibilityServices_failIfNotProfileOwner");
978             return;
979         }
980         try {
981             mDevicePolicyManager.setPermittedAccessibilityServices(mComponent, null);
982             fail("did not throw expected SecurityException");
983         } catch (SecurityException e) {
984             assertProfileOwnerMessage(e.getMessage());
985         }
986     }
987 
testSetBluetoothContactSharingDisabled_failIfNotProfileOwner()988     public void testSetBluetoothContactSharingDisabled_failIfNotProfileOwner() {
989         if (!mDeviceAdmin) {
990             Log.w(TAG, "Skipping testSetBluetoothContactSharingDisabled_failIfNotProfileOwner");
991             return;
992         }
993         try {
994             mDevicePolicyManager.setBluetoothContactSharingDisabled(mComponent, true);
995             fail("did not throw expected SecurityException");
996         } catch (SecurityException e) {
997             assertProfileOwnerMessage(e.getMessage());
998         }
999     }
1000 
testSetPermittedInputMethods_failIfNotProfileOwner()1001     public void testSetPermittedInputMethods_failIfNotProfileOwner() {
1002         if (!mDeviceAdmin) {
1003             Log.w(TAG, "Skipping testSetPermittedInputMethods_failIfNotProfileOwner");
1004             return;
1005         }
1006         try {
1007             mDevicePolicyManager.setPermittedInputMethods(mComponent, null);
1008             fail("did not throw expected SecurityException");
1009         } catch (SecurityException e) {
1010             assertProfileOwnerMessage(e.getMessage());
1011         }
1012     }
1013 
1014     /**
1015      * Test whether the version of the pre-installed launcher is at least L. This is needed for
1016      * managed profile support.
1017      */
testLauncherVersionAtLeastL()1018     public void testLauncherVersionAtLeastL() throws Exception {
1019         if (!mManagedProfiles) {
1020             return;
1021         }
1022 
1023         Intent intent = new Intent(Intent.ACTION_MAIN);
1024         intent.addCategory(Intent.CATEGORY_HOME);
1025         List<ResolveInfo> resolveInfos = mPackageManager.queryIntentActivities(intent,
1026                 0 /* default flags */);
1027         assertFalse("No launcher present", resolveInfos.isEmpty());
1028 
1029         for (ResolveInfo resolveInfo : resolveInfos) {
1030             ApplicationInfo launcherAppInfo = mPackageManager.getApplicationInfo(
1031                     resolveInfo.activityInfo.packageName, 0 /* default flags */);
1032             if ((launcherAppInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0 &&
1033                     launcherAppInfo.targetSdkVersion >= Build.VERSION_CODES.LOLLIPOP) {
1034                 return;
1035             }
1036         }
1037         fail("No system launcher with version L+ present present on device.");
1038     }
1039 
1040     /**
1041      * Test that managed provisioning is pre-installed if and only if the device declares the
1042      * device admin feature.
1043      */
testManagedProvisioningPreInstalled()1044     public void testManagedProvisioningPreInstalled() throws Exception {
1045         assertEquals(mDeviceAdmin, isPackageInstalledOnSystemImage(MANAGED_PROVISIONING_PKG));
1046     }
1047 
assertDeviceOwnerMessage(String message)1048     private void assertDeviceOwnerMessage(String message) {
1049         assertTrue("message is: "+ message, message.contains("does not own the device")
1050                 || message.contains("can only be called by the device owner"));
1051     }
1052 
assertProfileOwnerMessage(String message)1053     private void assertProfileOwnerMessage(String message) {
1054         assertTrue("message is: "+ message,
1055                 message.contains("does not own the profile"));
1056     }
1057 
resetComplexPasswordRestrictions()1058     private void resetComplexPasswordRestrictions() {
1059         /**
1060          * Not enough to reset only mComponent as
1061          * {@link DevicePolicyManager#resetPassword(String, int)} checks restrictions across all
1062          * admin components.
1063          */
1064         for (ComponentName adminComponent : new ComponentName[] {mComponent, mSecondComponent}) {
1065             mDevicePolicyManager.setPasswordMinimumLength(adminComponent, 0);
1066             mDevicePolicyManager.setPasswordMinimumUpperCase(adminComponent, 0);
1067             mDevicePolicyManager.setPasswordMinimumLowerCase(adminComponent, 0);
1068             mDevicePolicyManager.setPasswordMinimumLetters(adminComponent, 0);
1069             mDevicePolicyManager.setPasswordMinimumNumeric(adminComponent, 0);
1070             mDevicePolicyManager.setPasswordMinimumSymbols(adminComponent, 0);
1071             mDevicePolicyManager.setPasswordMinimumNonLetter(adminComponent, 0);
1072         }
1073     }
1074 
assertPasswordFails(String password, String restriction)1075     private void assertPasswordFails(String password, String restriction) {
1076         try {
1077             boolean passwordResetResult = mDevicePolicyManager.resetPassword(password, /* flags= */0);
1078             assertFalse("Password '" + password + "' should have failed on " + restriction,
1079                     passwordResetResult);
1080         } catch (IllegalArgumentException e) {
1081             // yesss, we have failed!
1082         }
1083     }
1084 
assertPasswordSucceeds(String password, String restriction)1085     private void assertPasswordSucceeds(String password, String restriction) {
1086         boolean passwordResetResult = mDevicePolicyManager.resetPassword(password, /* flags= */0);
1087         assertTrue("Password '" + password + "' failed on " + restriction, passwordResetResult);
1088         assertTrue(mDevicePolicyManager.isActivePasswordSufficient());
1089     }
1090 
testSetDelegatedCertInstaller_failIfNotProfileOwner()1091     public void testSetDelegatedCertInstaller_failIfNotProfileOwner() {
1092         if (!mDeviceAdmin) {
1093             Log.w(TAG, "Skipping testSetDelegatedCertInstaller_failIfNotProfileOwner");
1094             return;
1095         }
1096         try {
1097             mDevicePolicyManager.setCertInstallerPackage(mComponent, "com.test.package");
1098             fail("did not throw expected SecurityException");
1099         } catch (SecurityException e) {
1100             assertProfileOwnerMessage(e.getMessage());
1101         }
1102     }
1103 
testGetDelegatedCertInstaller_failIfNotProfileOwner()1104     public void testGetDelegatedCertInstaller_failIfNotProfileOwner() {
1105         if (!mDeviceAdmin) {
1106             Log.w(TAG, "Skipping testGetDelegatedCertInstaller_failIfNotProfileOwner");
1107             return;
1108         }
1109         try {
1110             mDevicePolicyManager.getCertInstallerPackage(mComponent);
1111             fail("did not throw expected SecurityException");
1112         } catch (SecurityException e) {
1113             assertProfileOwnerMessage(e.getMessage());
1114         }
1115     }
1116 
testSetSystemUpdatePolicy_failIfNotDeviceOwner()1117     public void testSetSystemUpdatePolicy_failIfNotDeviceOwner() {
1118         if (!mDeviceAdmin) {
1119             Log.w(TAG, "Skipping testSetSystemUpdatePolicy_failIfNotDeviceOwner");
1120             return;
1121         }
1122         try {
1123             mDevicePolicyManager.setSystemUpdatePolicy(mComponent, null);
1124             fail("did not throw expected SecurityException");
1125         } catch (SecurityException e) {
1126             assertDeviceOwnerMessage(e.getMessage());
1127         }
1128     }
1129 
isPackageInstalledOnSystemImage(String packagename)1130     private boolean isPackageInstalledOnSystemImage(String packagename) {
1131         try {
1132             ApplicationInfo info = mPackageManager.getApplicationInfo(packagename,
1133                     0 /* default flags */);
1134             return (info.flags & ApplicationInfo.FLAG_SYSTEM) != 0;
1135         } catch (NameNotFoundException e) {
1136             return false;
1137         }
1138     }
1139 }
1140