1 package com.android.cts.managedprofile;
2 
3 import static android.app.admin.DevicePolicyManager.KEYGUARD_DISABLE_TRUST_AGENTS;
4 import static android.app.admin.DevicePolicyManager.PASSWORD_COMPLEXITY_HIGH;
5 import static android.app.admin.DevicePolicyManager.PASSWORD_COMPLEXITY_NONE;
6 import static android.app.admin.DevicePolicyManager.PASSWORD_QUALITY_NUMERIC_COMPLEX;
7 import static android.app.admin.DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED;
8 
9 import static com.google.common.truth.Truth.assertThat;
10 
11 import static org.testng.Assert.assertThrows;
12 
13 import android.app.admin.DevicePolicyManager;
14 import android.content.BroadcastReceiver;
15 import android.content.ComponentName;
16 import android.os.PersistableBundle;
17 
18 /**
19  * Tests that the {@link DevicePolicyManager} APIs that should work for {@link
20  * DevicePolicyManager#getParentProfileInstance(ComponentName)} are supported.
21  *
22  * <p>Minimum restriction APIs are already tested by {@link PasswordMinimumRestrictionsTest}.
23  */
24 public class DevicePolicyManagerParentSupportTest extends BaseManagedProfileTest {
25     private static final ComponentName FAKE_COMPONENT = new ComponentName(
26             FakeComponent.class.getPackage().getName(), FakeComponent.class.getName());
27 
testSetAndGetRequiredPasswordComplexity_onParent()28     public void testSetAndGetRequiredPasswordComplexity_onParent() {
29        if (!mHasSecureLockScreen) {
30             return;
31         }
32 
33         mParentDevicePolicyManager.setRequiredPasswordComplexity(PASSWORD_COMPLEXITY_HIGH);
34         try {
35             final int actualPasswordComplexity =
36                     mParentDevicePolicyManager.getRequiredPasswordComplexity();
37 
38             assertThat(actualPasswordComplexity).isEqualTo(PASSWORD_COMPLEXITY_HIGH);
39         } finally {
40             mParentDevicePolicyManager.setRequiredPasswordComplexity(
41                     PASSWORD_COMPLEXITY_NONE);
42         }
43     }
44 
testSetAndGetPasswordHistoryLength_onParent()45     public void testSetAndGetPasswordHistoryLength_onParent() {
46         if (!mHasSecureLockScreen) {
47             return;
48         }
49         final int passwordHistoryLength = 5;
50 
51         mParentDevicePolicyManager.setPasswordHistoryLength(
52                 ADMIN_RECEIVER_COMPONENT, passwordHistoryLength);
53         final int actualPasswordHistoryLength =
54                 mParentDevicePolicyManager.getPasswordHistoryLength(ADMIN_RECEIVER_COMPONENT);
55 
56         assertThat(actualPasswordHistoryLength).isEqualTo(passwordHistoryLength);
57     }
58 
testGetPasswordComplexity_onParent()59     public void testGetPasswordComplexity_onParent() {
60         if (!mHasSecureLockScreen) {
61             return;
62         }
63 
64         final int actualPasswordComplexity =
65                 mParentDevicePolicyManager.getPasswordComplexity();
66         assertThat(actualPasswordComplexity).isEqualTo(
67                 PASSWORD_COMPLEXITY_NONE);
68     }
69 
testSetAndGetPasswordExpirationTimeout_onParent()70     public void testSetAndGetPasswordExpirationTimeout_onParent() {
71         if (!mHasSecureLockScreen) {
72             return;
73         }
74         final int passwordExpirationTimeout = 432000000;
75 
76         mParentDevicePolicyManager.setPasswordExpirationTimeout(
77                 ADMIN_RECEIVER_COMPONENT, passwordExpirationTimeout);
78         final long actualPasswordExpirationTimeout =
79                 mParentDevicePolicyManager.getPasswordExpirationTimeout(ADMIN_RECEIVER_COMPONENT);
80 
81         assertThat(actualPasswordExpirationTimeout).isEqualTo(passwordExpirationTimeout);
82     }
83 
testGetPasswordExpiration_onParent()84     public void testGetPasswordExpiration_onParent() {
85         if (!mHasSecureLockScreen) {
86             return;
87         }
88         final long passwordExpirationTimeout = 432000000;
89         final long currentTime = System.currentTimeMillis();
90 
91         mParentDevicePolicyManager.setPasswordExpirationTimeout(
92                 ADMIN_RECEIVER_COMPONENT, passwordExpirationTimeout);
93         final long actualPasswordExpiration =
94                 mParentDevicePolicyManager.getPasswordExpiration(ADMIN_RECEIVER_COMPONENT);
95 
96         assertThat(actualPasswordExpiration).isAtLeast(passwordExpirationTimeout + currentTime);
97     }
98 
testGetMaximumPasswordLength_onParent()99     public void testGetMaximumPasswordLength_onParent() {
100         if (!mHasSecureLockScreen) {
101             return;
102         }
103         final int actualMaximumPasswordLength =
104                 mParentDevicePolicyManager.getPasswordMaximumLength(
105                         PASSWORD_QUALITY_NUMERIC_COMPLEX);
106         assertThat(actualMaximumPasswordLength).isGreaterThan(0);
107     }
108 
testIsActivePasswordSufficient_onParent_setOnParent_isSupported()109     public void testIsActivePasswordSufficient_onParent_setOnParent_isSupported() {
110         try {
111             mParentDevicePolicyManager.setRequiredPasswordComplexity(PASSWORD_COMPLEXITY_HIGH);
112             assertThat(mParentDevicePolicyManager.isActivePasswordSufficient()).isFalse();
113         } finally {
114             mParentDevicePolicyManager.setRequiredPasswordComplexity(PASSWORD_COMPLEXITY_NONE);
115         }
116     }
117 
testIsActivePasswordSufficient_onParent_setOnProfile_isSupported()118     public void testIsActivePasswordSufficient_onParent_setOnProfile_isSupported() {
119         try {
120             mDevicePolicyManager.setRequiredPasswordComplexity(PASSWORD_COMPLEXITY_HIGH);
121             assertThat(mParentDevicePolicyManager.isActivePasswordSufficient()).isFalse();
122         } finally {
123             mDevicePolicyManager.setRequiredPasswordComplexity(PASSWORD_COMPLEXITY_NONE);
124         }
125     }
126 
testSetPasswordQuality_onParent_isNotSupported()127     public void testSetPasswordQuality_onParent_isNotSupported() {
128         assertThrows(SecurityException.class,
129                 () -> setPasswordQuality(PASSWORD_QUALITY_NUMERIC_COMPLEX));
130     }
131 
setPasswordQuality(int quality)132     private void setPasswordQuality(int quality) {
133         mParentDevicePolicyManager.setPasswordQuality(ADMIN_RECEIVER_COMPONENT, quality);
134     }
135 
testIsActivePasswordSufficient_onParent_respectsProfileQualityWhenUnified()136     public void testIsActivePasswordSufficient_onParent_respectsProfileQualityWhenUnified() {
137         mDevicePolicyManager.setPasswordQuality(
138                 ADMIN_RECEIVER_COMPONENT, PASSWORD_QUALITY_UNSPECIFIED);
139         assertThat(mParentDevicePolicyManager.isActivePasswordSufficient()).isTrue();
140         mDevicePolicyManager.setPasswordQuality(
141                 ADMIN_RECEIVER_COMPONENT, PASSWORD_QUALITY_NUMERIC_COMPLEX);
142         try {
143             assertThat(mParentDevicePolicyManager.isActivePasswordSufficient()).isFalse();
144         } finally {
145             mDevicePolicyManager.setPasswordQuality(
146                     ADMIN_RECEIVER_COMPONENT, PASSWORD_QUALITY_UNSPECIFIED);
147         }
148     }
149 
testGetCurrentFailedPasswordAttempts_onParent_isSupported()150     public void testGetCurrentFailedPasswordAttempts_onParent_isSupported() {
151         assertThat(mParentDevicePolicyManager.getCurrentFailedPasswordAttempts()).isEqualTo(0);
152     }
153 
testSetAndGetMaximumFailedPasswordsForWipe_onParent()154     public void testSetAndGetMaximumFailedPasswordsForWipe_onParent() {
155         if (!mHasSecureLockScreen) {
156             return;
157         }
158         final int maximumFailedPasswordsForWipe = 15;
159 
160         mParentDevicePolicyManager.setMaximumFailedPasswordsForWipe(
161                 ADMIN_RECEIVER_COMPONENT, maximumFailedPasswordsForWipe);
162         final int actualMaximumFailedPasswordsForWipe =
163                 mParentDevicePolicyManager.getMaximumFailedPasswordsForWipe(
164                         ADMIN_RECEIVER_COMPONENT);
165 
166         assertThat(actualMaximumFailedPasswordsForWipe).isEqualTo(maximumFailedPasswordsForWipe);
167     }
168 
testSetAndGetMaximumTimeToLock_onParent()169     public void testSetAndGetMaximumTimeToLock_onParent() {
170         final int maximumTimeToLock = 6000;
171 
172         mParentDevicePolicyManager.setMaximumTimeToLock(
173                 ADMIN_RECEIVER_COMPONENT, maximumTimeToLock);
174         final long actualMaximumTimeToLock =
175                 mParentDevicePolicyManager.getMaximumTimeToLock(ADMIN_RECEIVER_COMPONENT);
176 
177         assertThat(actualMaximumTimeToLock).isEqualTo(maximumTimeToLock);
178     }
179 
testLockNow_onParent_isSupported()180     public void testLockNow_onParent_isSupported() {
181         mParentDevicePolicyManager.lockNow();
182         // Will fail if a SecurityException is thrown.
183     }
184 
testSetAndGetKeyguardDisabledFeatures_onParent()185     public void testSetAndGetKeyguardDisabledFeatures_onParent() {
186         mParentDevicePolicyManager.setKeyguardDisabledFeatures(
187                 ADMIN_RECEIVER_COMPONENT, KEYGUARD_DISABLE_TRUST_AGENTS);
188         long actualKeyguardDisabledFeatures =
189                 mParentDevicePolicyManager.getKeyguardDisabledFeatures(ADMIN_RECEIVER_COMPONENT);
190 
191         assertThat(actualKeyguardDisabledFeatures).isEqualTo(KEYGUARD_DISABLE_TRUST_AGENTS);
192     }
193 
testSetAndGetTrustAgentConfiguration_onParent()194     public void testSetAndGetTrustAgentConfiguration_onParent() {
195         if (!mHasSecureLockScreen) {
196             return;
197         }
198         final PersistableBundle configuration = new PersistableBundle();
199         final String key = "key";
200         final String value = "value";
201         configuration.putString(key, value);
202 
203         mParentDevicePolicyManager.setTrustAgentConfiguration(
204                 ADMIN_RECEIVER_COMPONENT, FAKE_COMPONENT, configuration);
205         final PersistableBundle actualConfiguration =
206                 mParentDevicePolicyManager.getTrustAgentConfiguration(
207                         ADMIN_RECEIVER_COMPONENT, FAKE_COMPONENT).get(0);
208 
209         assertThat(actualConfiguration.get(key)).isEqualTo(value);
210     }
211 
testSetAndGetRequiredStrongAuthTimeout_onParent()212     public void testSetAndGetRequiredStrongAuthTimeout_onParent() {
213         if (!mHasSecureLockScreen) {
214             return;
215         }
216         final int requiredStrongAuthTimeout = 4600000;
217 
218         mParentDevicePolicyManager.setRequiredStrongAuthTimeout(
219                 ADMIN_RECEIVER_COMPONENT, requiredStrongAuthTimeout);
220         final long actualRequiredStrongAuthTimeout =
221                 mParentDevicePolicyManager.getRequiredStrongAuthTimeout(ADMIN_RECEIVER_COMPONENT);
222 
223         assertThat(actualRequiredStrongAuthTimeout).isEqualTo(requiredStrongAuthTimeout);
224     }
225 
226     public abstract class FakeComponent extends BroadcastReceiver {}
227 }
228