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.DeviceAdminReceiver;
19 import android.app.admin.DevicePolicyManager;
20 import android.content.ComponentName;
21 import android.content.pm.PackageManager;
22 import android.os.Build;
23 import android.test.AndroidTestCase;
24 
25 public class BaseDeviceAdminTest extends AndroidTestCase {
26 
27     public static class AdminReceiver extends DeviceAdminReceiver {
28     }
29 
30     protected String mPackageName;
31     protected ComponentName mAdminComponent;
32     protected boolean mHasSecureLockScreen;
33 
34     public DevicePolicyManager dpm;
35 
36     @Override
setUp()37     protected void setUp() throws Exception {
38         super.setUp();
39 
40         dpm = mContext.getSystemService(DevicePolicyManager.class);
41         mPackageName = mContext.getPackageName();
42         mAdminComponent = new ComponentName(mContext, AdminReceiver.class);
43         mHasSecureLockScreen = mContext.getPackageManager()
44                 .hasSystemFeature(PackageManager.FEATURE_SECURE_LOCK_SCREEN);
45     }
46 
47     /**
48      * @return the target API level.  Note we don't get it from the package manager information
49      * but we just parse the last two digits of the package name.  This is to catch a potential
50      * issue where we forget to change the target API level in the manifest.  (Conversely,
51      * if we forget to change the package name, we'll catch that in the caller side.)
52      */
getTargetApiLevel()53     protected int getTargetApiLevel() {
54         final String packageName = mContext.getPackageName();
55         return Integer.parseInt(packageName.substring(packageName.length() - 2));
56     }
57 
isDeviceOwner()58     protected boolean isDeviceOwner() {
59         return dpm.isDeviceOwnerApp(mAdminComponent.getPackageName());
60     }
61 
assertDeviceOwner()62     protected void assertDeviceOwner() {
63         assertTrue("Not device owner", isDeviceOwner());
64     }
65 
assertNotDeviceOwner()66     protected void assertNotDeviceOwner() {
67         assertFalse("Must not be device owner", isDeviceOwner());
68     }
69 
assertNotActiveAdmin()70     protected void assertNotActiveAdmin() throws Exception {
71         for (int i = 0; i < 1000 && dpm.isAdminActive(mAdminComponent); i++) {
72             Thread.sleep(10);
73         }
74         assertFalse("Still active admin", dpm.isAdminActive(mAdminComponent));
75     }
76 
shouldResetPasswordThrow()77     protected boolean shouldResetPasswordThrow() {
78         return getTargetApiLevel() > Build.VERSION_CODES.M;
79     }
80 
resetComplexPasswordRestrictions()81     protected void resetComplexPasswordRestrictions() {
82         dpm.setPasswordMinimumLength(mAdminComponent, 0);
83         dpm.setPasswordMinimumUpperCase(mAdminComponent, 0);
84         dpm.setPasswordMinimumLowerCase(mAdminComponent, 0);
85         dpm.setPasswordMinimumLetters(mAdminComponent, 0);
86         dpm.setPasswordMinimumNumeric(mAdminComponent, 0);
87         dpm.setPasswordMinimumSymbols(mAdminComponent, 0);
88         dpm.setPasswordMinimumNonLetter(mAdminComponent, 0);
89     }
90 
clearPassword()91     protected void clearPassword() {
92         assertDeviceOwner();
93 
94         resetComplexPasswordRestrictions();
95 
96         dpm.setPasswordQuality(mAdminComponent, DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED);
97         assertTrue(dpm.resetPassword("", /* flags =*/ 0));
98     }
99 }
100