1 /*
2  * Copyright (C) 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.cts.deviceadmin;
17 
18 import android.app.admin.DevicePolicyManager;
19 import android.content.pm.PackageInfo;
20 import android.content.pm.PackageManager;
21 import android.os.Build;
22 
23 /**
24  * Device admin device side tests.
25  */
26 public class DeviceAdminTest extends BaseDeviceAdminTest {
27     @Override
setUp()28     protected void setUp() throws Exception {
29         super.setUp();
30 
31         assertNotDeviceOwner();
32     }
33 
testTargetApiLevel()34     public void testTargetApiLevel() throws Exception {
35         final PackageManager pm = mContext.getPackageManager();
36 
37         final PackageInfo pi = pm.getPackageInfo(mContext.getPackageName(), /* flags =*/ 0);
38 
39         assertTrue(getTargetApiLevel() == pi.applicationInfo.targetSdkVersion ||
40                 Build.VERSION_CODES.CUR_DEVELOPMENT == pi.applicationInfo.targetSdkVersion);
41     }
42 
testGetMaximumFailedPasswordsForWipe()43     public void testGetMaximumFailedPasswordsForWipe() {
44         if (!mHasSecureLockScreen) {
45             return;
46         }
47         dpm.setMaximumFailedPasswordsForWipe(mAdminComponent, 3);
48         assertEquals(3, dpm.getMaximumFailedPasswordsForWipe(mAdminComponent));
49 
50         dpm.setMaximumFailedPasswordsForWipe(mAdminComponent, 5);
51         assertEquals(5, dpm.getMaximumFailedPasswordsForWipe(mAdminComponent));
52     }
53 
testPasswordHistoryLength()54     public void testPasswordHistoryLength() {
55         if (!mHasSecureLockScreen) {
56             return;
57         }
58         // Password history length restriction is only imposed if password quality is at least
59         // numeric.
60         dpm.setPasswordQuality(mAdminComponent,
61                 DevicePolicyManager.PASSWORD_QUALITY_ALPHABETIC);
62         int originalValue = dpm.getPasswordHistoryLength(mAdminComponent);
63         try {
64             dpm.setPasswordHistoryLength(mAdminComponent, 3);
65             assertEquals(3, dpm.getPasswordHistoryLength(mAdminComponent));
66             // Although it would make sense we cannot test if password history restrictions
67             // are enforced as DevicePolicyManagerService.resetPassword fails to do so at the
68             // moment. See b/17707820
69         } finally {
70             dpm.setPasswordHistoryLength(mAdminComponent, originalValue);
71         }
72     }
73 
testPasswordExpirationTimeout()74     public void testPasswordExpirationTimeout() {
75         if (!mHasSecureLockScreen) {
76             return;
77         }
78         long originalValue = dpm.getPasswordExpirationTimeout(mAdminComponent);
79         try {
80             for (long testLength : new long[] {
81                     0L, 864000000L /* ten days */, 8640000000L /* 100 days */}) {
82                 dpm.setPasswordExpirationTimeout(mAdminComponent, testLength);
83                 assertEquals(testLength,
84                         dpm.getPasswordExpirationTimeout(mAdminComponent));
85             }
86         } finally {
87             dpm.setPasswordExpirationTimeout(mAdminComponent, originalValue);
88         }
89     }
90 }
91