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