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.PackageManager;
20 import android.util.Log;
21 
22 /**
23  * Device admin device side tests with enterprise policies disallowed.
24  */
25 public class DeviceAdminWithEnterprisePoliciesBlockedTest extends DeviceAdminTest {
26 
27     private static final String TAG = DeviceAdminWithEnterprisePoliciesBlockedTest.class
28             .getSimpleName();
29 
testCameraDisabled()30     public void testCameraDisabled() {
31         boolean originalValue = dpm.getCameraDisabled(mAdminComponent);
32         assertSecurityException(() -> dpm.setCameraDisabled(mAdminComponent, true));
33         assertEquals(originalValue, dpm.getCameraDisabled(mAdminComponent));
34     }
35 
testKeyguardDisabledFeatures()36     public void testKeyguardDisabledFeatures() {
37         int originalValue = dpm.getKeyguardDisabledFeatures(mAdminComponent);
38         assertSecurityException(() -> dpm.setKeyguardDisabledFeatures(mAdminComponent,
39             DevicePolicyManager.KEYGUARD_DISABLE_FEATURES_ALL));
40         assertEquals(originalValue, dpm.getKeyguardDisabledFeatures(mAdminComponent));
41     }
42 
testIsActivePasswordSufficient()43     public void testIsActivePasswordSufficient() {
44         assertSecurityException(() -> dpm.isActivePasswordSufficient());
45     }
46 
47     @Override
testPasswordHistoryLength()48     public void testPasswordHistoryLength() {
49         if (!mHasSecureLockScreen) {
50             return;
51         }
52         int originalValue = dpm.getPasswordHistoryLength(mAdminComponent);
53         assertSecurityException(() -> dpm.setPasswordHistoryLength(mAdminComponent, 3));
54         assertEquals(originalValue, dpm.getPasswordHistoryLength(mAdminComponent));
55     }
56 
testPasswordMinimumLength()57     public void testPasswordMinimumLength() {
58         int originalValue = dpm.getPasswordMinimumLength(mAdminComponent);
59         runDeprecatedPasswordQualityApi(() -> dpm.setPasswordMinimumLength(mAdminComponent, 4));
60         assertEquals(originalValue, dpm.getPasswordMinimumLength(mAdminComponent));
61     }
62 
testPasswordMinimumLetters()63     public void testPasswordMinimumLetters() {
64         int originalValue = dpm.getPasswordMinimumLetters(mAdminComponent);
65         runDeprecatedPasswordQualityApi(() -> dpm.setPasswordMinimumLetters(mAdminComponent, 3));
66         assertEquals(originalValue, dpm.getPasswordMinimumLetters(mAdminComponent));
67     }
68 
testPasswordMinimumLowerCase()69     public void testPasswordMinimumLowerCase() {
70         int originalValue = dpm.getPasswordMinimumLowerCase(mAdminComponent);
71         runDeprecatedPasswordQualityApi(() -> dpm.setPasswordMinimumLowerCase(mAdminComponent, 3));
72         assertEquals(originalValue, dpm.getPasswordMinimumLowerCase(mAdminComponent));
73     }
74 
testPasswordMinimumNonLetter()75     public void testPasswordMinimumNonLetter() {
76         int originalValue = dpm.getPasswordMinimumNonLetter(mAdminComponent);
77         runDeprecatedPasswordQualityApi(() -> dpm.setPasswordMinimumNonLetter(mAdminComponent, 3));
78         assertEquals(originalValue, dpm.getPasswordMinimumNonLetter(mAdminComponent));
79     }
80 
testPasswordMinimumNumeric()81     public void testPasswordMinimumNumeric() {
82         int originalValue = dpm.getPasswordMinimumNumeric(mAdminComponent);
83         runDeprecatedPasswordQualityApi(() -> dpm.setPasswordMinimumNumeric(mAdminComponent, 2));
84         assertEquals(originalValue, dpm.getPasswordMinimumNumeric(mAdminComponent));
85     }
86 
testPasswordMinimumSymbols()87     public void testPasswordMinimumSymbols() {
88         int originalValue = dpm.getPasswordMinimumSymbols(mAdminComponent);
89         runDeprecatedPasswordQualityApi(() -> dpm.setPasswordMinimumSymbols(mAdminComponent, 2));
90         assertEquals(originalValue, dpm.getPasswordMinimumSymbols(mAdminComponent));
91     }
92 
testPasswordMinimumUpperCase()93     public void testPasswordMinimumUpperCase() {
94         int originalValue = dpm.getPasswordMinimumUpperCase(mAdminComponent);
95         runDeprecatedPasswordQualityApi(() -> dpm.setPasswordMinimumUpperCase(mAdminComponent, 3));
96         assertEquals(originalValue, dpm.getPasswordMinimumUpperCase(mAdminComponent));
97     }
98 
testPasswordQuality()99     public void testPasswordQuality() {
100         int originalValue = dpm.getPasswordQuality(mAdminComponent);
101         runDeprecatedPasswordQualityApi(() -> dpm.setPasswordQuality(mAdminComponent,
102             DevicePolicyManager.PASSWORD_QUALITY_ALPHABETIC));
103         assertEquals(originalValue, dpm.getPasswordQuality(mAdminComponent));
104     }
105 
106     @Override
testPasswordExpirationTimeout()107     public void testPasswordExpirationTimeout() {
108         if (!mHasSecureLockScreen) {
109             return;
110         }
111         long originalValue = dpm.getPasswordExpirationTimeout(mAdminComponent);
112         assertSecurityException(() -> dpm.setPasswordExpirationTimeout(mAdminComponent, 1234L));
113         assertEquals(originalValue, dpm.getPasswordExpirationTimeout(mAdminComponent));
114     }
115 
assertSecurityException(Runnable r)116     private void assertSecurityException(Runnable r) {
117         boolean securityExceptionThrown = false;
118         try {
119             r.run();
120         } catch (SecurityException e) {
121             securityExceptionThrown = true;
122         }
123 
124         assertTrue("Expected SecurityException was not thrown", securityExceptionThrown);
125     }
126 
runDeprecatedPasswordQualityApi(Runnable r)127     private void runDeprecatedPasswordQualityApi(Runnable r) {
128         if (supportDeprecatedPasswordQualityApis()) {
129             Log.v(TAG, "Running operation and expecting a SecurityException");
130             assertSecurityException(r);
131         } else {
132             Log.v(TAG, "Running operation without expecting exception - it should just be ignored");
133             r.run();
134         }
135     }
136 
supportDeprecatedPasswordQualityApis()137     private boolean supportDeprecatedPasswordQualityApis() {
138         return !getContext().getPackageManager()
139                 .hasSystemFeature(PackageManager.FEATURE_AUTOMOTIVE);
140     }
141 }
142