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 with enterprise policies disallowed.
24  */
25 public class DeviceAdminWithEnterprisePoliciesBlockedTest extends DeviceAdminTest {
testCameraDisabled()26     public void testCameraDisabled() {
27         boolean originalValue = dpm.getCameraDisabled(mAdminComponent);
28         assertSecurityException(() -> dpm.setCameraDisabled(mAdminComponent, true));
29         assertEquals(originalValue, dpm.getCameraDisabled(mAdminComponent));
30     }
31 
testKeyguardDisabledFeatures()32     public void testKeyguardDisabledFeatures() {
33         int originalValue = dpm.getKeyguardDisabledFeatures(mAdminComponent);
34         assertSecurityException(() -> dpm.setKeyguardDisabledFeatures(mAdminComponent,
35             DevicePolicyManager.KEYGUARD_DISABLE_FEATURES_ALL));
36         assertEquals(originalValue, dpm.getKeyguardDisabledFeatures(mAdminComponent));
37     }
38 
testIsActivePasswordSufficient()39     public void testIsActivePasswordSufficient() {
40         assertSecurityException(() -> dpm.isActivePasswordSufficient());
41     }
42 
43     @Override
testPasswordHistoryLength()44     public void testPasswordHistoryLength() {
45         if (!mHasSecureLockScreen) {
46             return;
47         }
48         int originalValue = dpm.getPasswordHistoryLength(mAdminComponent);
49         assertSecurityException(() -> dpm.setPasswordHistoryLength(mAdminComponent, 3));
50         assertEquals(originalValue, dpm.getPasswordHistoryLength(mAdminComponent));
51     }
52 
testPasswordMinimumLength()53     public void testPasswordMinimumLength() {
54         int originalValue = dpm.getPasswordMinimumLength(mAdminComponent);
55         assertSecurityException(() -> dpm.setPasswordMinimumLength(mAdminComponent, 4));
56         assertEquals(originalValue, dpm.getPasswordMinimumLength(mAdminComponent));
57     }
58 
testPasswordMinimumLetters()59     public void testPasswordMinimumLetters() {
60         int originalValue = dpm.getPasswordMinimumLetters(mAdminComponent);
61         assertSecurityException(() -> dpm.setPasswordMinimumLetters(mAdminComponent, 3));
62         assertEquals(originalValue, dpm.getPasswordMinimumLetters(mAdminComponent));
63     }
64 
testPasswordMinimumLowerCase()65     public void testPasswordMinimumLowerCase() {
66         int originalValue = dpm.getPasswordMinimumLowerCase(mAdminComponent);
67         assertSecurityException(() -> dpm.setPasswordMinimumLowerCase(mAdminComponent, 3));
68         assertEquals(originalValue, dpm.getPasswordMinimumLowerCase(mAdminComponent));
69     }
70 
testPasswordMinimumNonLetter()71     public void testPasswordMinimumNonLetter() {
72         int originalValue = dpm.getPasswordMinimumNonLetter(mAdminComponent);
73         assertSecurityException(() -> dpm.setPasswordMinimumNonLetter(mAdminComponent, 3));
74         assertEquals(originalValue, dpm.getPasswordMinimumNonLetter(mAdminComponent));
75     }
76 
testPasswordMinimumNumeric()77     public void testPasswordMinimumNumeric() {
78         int originalValue = dpm.getPasswordMinimumNumeric(mAdminComponent);
79         assertSecurityException(() -> dpm.setPasswordMinimumNumeric(mAdminComponent, 2));
80         assertEquals(originalValue, dpm.getPasswordMinimumNumeric(mAdminComponent));
81     }
82 
testPasswordMinimumSymbols()83     public void testPasswordMinimumSymbols() {
84         int originalValue = dpm.getPasswordMinimumSymbols(mAdminComponent);
85         assertSecurityException(() -> dpm.setPasswordMinimumSymbols(mAdminComponent, 2));
86         assertEquals(originalValue, dpm.getPasswordMinimumSymbols(mAdminComponent));
87     }
88 
testPasswordMinimumUpperCase()89     public void testPasswordMinimumUpperCase() {
90         int originalValue = dpm.getPasswordMinimumUpperCase(mAdminComponent);
91         assertSecurityException(() -> dpm.setPasswordMinimumUpperCase(mAdminComponent, 3));
92         assertEquals(originalValue, dpm.getPasswordMinimumUpperCase(mAdminComponent));
93     }
94 
testPasswordQuality()95     public void testPasswordQuality() {
96         int originalValue = dpm.getPasswordQuality(mAdminComponent);
97         assertSecurityException(() -> dpm.setPasswordQuality(mAdminComponent,
98             DevicePolicyManager.PASSWORD_QUALITY_ALPHABETIC));
99         assertEquals(originalValue, dpm.getPasswordQuality(mAdminComponent));
100     }
101 
102     @Override
testPasswordExpirationTimeout()103     public void testPasswordExpirationTimeout() {
104         if (!mHasSecureLockScreen) {
105             return;
106         }
107         long originalValue = dpm.getPasswordExpirationTimeout(mAdminComponent);
108         assertSecurityException(() -> dpm.setPasswordExpirationTimeout(mAdminComponent, 1234L));
109         assertEquals(originalValue, dpm.getPasswordExpirationTimeout(mAdminComponent));
110     }
111 
assertSecurityException(Runnable r)112     private void assertSecurityException(Runnable r) {
113         boolean securityExceptionThrown = false;
114         try {
115             r.run();
116         } catch (SecurityException e) {
117             securityExceptionThrown = true;
118         }
119 
120         assertTrue("Expected SecurityException was not thrown", securityExceptionThrown);
121     }
122 }
123