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_QUALITY_NUMERIC_COMPLEX; 5 import static android.app.admin.DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED; 6 7 import static com.google.common.truth.Truth.assertThat; 8 9 import android.app.admin.DevicePolicyManager; 10 import android.content.BroadcastReceiver; 11 import android.content.ComponentName; 12 import android.os.PersistableBundle; 13 14 /** 15 * Tests that the {@link DevicePolicyManager} APIs that should work for {@link 16 * DevicePolicyManager#getParentProfileInstance(ComponentName)} are supported. 17 * 18 * <p>Minimum restriction APIs are already tested by {@link PasswordMinimumRestrictionsTest}. 19 */ 20 public class DevicePolicyManagerParentSupportTest extends BaseManagedProfileTest { 21 private static final ComponentName FAKE_COMPONENT = new ComponentName( 22 FakeComponent.class.getPackage().getName(), FakeComponent.class.getName()); 23 testSetAndGetPasswordQuality_onParent()24 public void testSetAndGetPasswordQuality_onParent() { 25 mParentDevicePolicyManager.setPasswordQuality( 26 ADMIN_RECEIVER_COMPONENT, PASSWORD_QUALITY_NUMERIC_COMPLEX); 27 final int actualPasswordQuality = 28 mParentDevicePolicyManager.getPasswordQuality(ADMIN_RECEIVER_COMPONENT); 29 30 assertThat(actualPasswordQuality).isEqualTo(PASSWORD_QUALITY_NUMERIC_COMPLEX); 31 } 32 testSetAndGetPasswordHistoryLength_onParent()33 public void testSetAndGetPasswordHistoryLength_onParent() { 34 if (!mHasSecureLockScreen) { 35 return; 36 } 37 final int passwordHistoryLength = 5; 38 39 mParentDevicePolicyManager.setPasswordHistoryLength( 40 ADMIN_RECEIVER_COMPONENT, passwordHistoryLength); 41 final int actualPasswordHistoryLength = 42 mParentDevicePolicyManager.getPasswordHistoryLength(ADMIN_RECEIVER_COMPONENT); 43 44 assertThat(actualPasswordHistoryLength).isEqualTo(passwordHistoryLength); 45 } 46 testSetAndGetPasswordExpirationTimeout_onParent()47 public void testSetAndGetPasswordExpirationTimeout_onParent() { 48 if (!mHasSecureLockScreen) { 49 return; 50 } 51 final int passwordExpirationTimeout = 432000000; 52 53 mParentDevicePolicyManager.setPasswordExpirationTimeout( 54 ADMIN_RECEIVER_COMPONENT, passwordExpirationTimeout); 55 final long actualPasswordExpirationTimeout = 56 mParentDevicePolicyManager.getPasswordExpirationTimeout(ADMIN_RECEIVER_COMPONENT); 57 58 assertThat(actualPasswordExpirationTimeout).isEqualTo(passwordExpirationTimeout); 59 } 60 testGetPasswordExpiration_onParent()61 public void testGetPasswordExpiration_onParent() { 62 if (!mHasSecureLockScreen) { 63 return; 64 } 65 final long passwordExpirationTimeout = 432000000; 66 final long currentTime = System.currentTimeMillis(); 67 68 mParentDevicePolicyManager.setPasswordExpirationTimeout( 69 ADMIN_RECEIVER_COMPONENT, passwordExpirationTimeout); 70 final long actualPasswordExpiration = 71 mParentDevicePolicyManager.getPasswordExpiration(ADMIN_RECEIVER_COMPONENT); 72 73 assertThat(actualPasswordExpiration).isAtLeast(passwordExpirationTimeout + currentTime); 74 } 75 testGetMaximumPasswordLength_onParent()76 public void testGetMaximumPasswordLength_onParent() { 77 final int actualMaximumPasswordLength = 78 mParentDevicePolicyManager.getPasswordMaximumLength( 79 PASSWORD_QUALITY_NUMERIC_COMPLEX); 80 assertThat(actualMaximumPasswordLength).isGreaterThan(0); 81 } 82 testIsActivePasswordSufficient_onParent_isSupported()83 public void testIsActivePasswordSufficient_onParent_isSupported() { 84 setPasswordQuality(PASSWORD_QUALITY_NUMERIC_COMPLEX); 85 assertThat(mParentDevicePolicyManager.isActivePasswordSufficient()).isFalse(); 86 } 87 setPasswordQuality(int quality)88 private void setPasswordQuality(int quality) { 89 mParentDevicePolicyManager.setPasswordQuality(ADMIN_RECEIVER_COMPONENT, quality); 90 } 91 testGetCurrentFailedPasswordAttempts_onParent_isSupported()92 public void testGetCurrentFailedPasswordAttempts_onParent_isSupported() { 93 assertThat(mParentDevicePolicyManager.getCurrentFailedPasswordAttempts()).isEqualTo(0); 94 } 95 testSetAndGetMaximumFailedPasswordsForWipe_onParent()96 public void testSetAndGetMaximumFailedPasswordsForWipe_onParent() { 97 if (!mHasSecureLockScreen) { 98 return; 99 } 100 final int maximumFailedPasswordsForWipe = 15; 101 102 mParentDevicePolicyManager.setMaximumFailedPasswordsForWipe( 103 ADMIN_RECEIVER_COMPONENT, maximumFailedPasswordsForWipe); 104 final int actualMaximumFailedPasswordsForWipe = 105 mParentDevicePolicyManager.getMaximumFailedPasswordsForWipe( 106 ADMIN_RECEIVER_COMPONENT); 107 108 assertThat(actualMaximumFailedPasswordsForWipe).isEqualTo(maximumFailedPasswordsForWipe); 109 } 110 testSetAndGetMaximumTimeToLock_onParent()111 public void testSetAndGetMaximumTimeToLock_onParent() { 112 final int maximumTimeToLock = 6000; 113 114 mParentDevicePolicyManager.setMaximumTimeToLock( 115 ADMIN_RECEIVER_COMPONENT, maximumTimeToLock); 116 final long actualMaximumTimeToLock = 117 mParentDevicePolicyManager.getMaximumTimeToLock(ADMIN_RECEIVER_COMPONENT); 118 119 assertThat(actualMaximumTimeToLock).isEqualTo(maximumTimeToLock); 120 } 121 testLockNow_onParent_isSupported()122 public void testLockNow_onParent_isSupported() { 123 mParentDevicePolicyManager.lockNow(); 124 // Will fail if a SecurityException is thrown. 125 } 126 testSetAndGetKeyguardDisabledFeatures_onParent()127 public void testSetAndGetKeyguardDisabledFeatures_onParent() { 128 mParentDevicePolicyManager.setKeyguardDisabledFeatures( 129 ADMIN_RECEIVER_COMPONENT, KEYGUARD_DISABLE_TRUST_AGENTS); 130 long actualKeyguardDisabledFeatures = 131 mParentDevicePolicyManager.getKeyguardDisabledFeatures(ADMIN_RECEIVER_COMPONENT); 132 133 assertThat(actualKeyguardDisabledFeatures).isEqualTo(KEYGUARD_DISABLE_TRUST_AGENTS); 134 } 135 testSetAndGetTrustAgentConfiguration_onParent()136 public void testSetAndGetTrustAgentConfiguration_onParent() { 137 if (!mHasSecureLockScreen) { 138 return; 139 } 140 final PersistableBundle configuration = new PersistableBundle(); 141 final String key = "key"; 142 final String value = "value"; 143 configuration.putString(key, value); 144 145 mParentDevicePolicyManager.setTrustAgentConfiguration( 146 ADMIN_RECEIVER_COMPONENT, FAKE_COMPONENT, configuration); 147 final PersistableBundle actualConfiguration = 148 mParentDevicePolicyManager.getTrustAgentConfiguration( 149 ADMIN_RECEIVER_COMPONENT, FAKE_COMPONENT).get(0); 150 151 assertThat(actualConfiguration.get(key)).isEqualTo(value); 152 } 153 testSetAndGetRequiredStrongAuthTimeout_onParent()154 public void testSetAndGetRequiredStrongAuthTimeout_onParent() { 155 if (!mHasSecureLockScreen) { 156 return; 157 } 158 final int requiredStrongAuthTimeout = 4600000; 159 160 mParentDevicePolicyManager.setRequiredStrongAuthTimeout( 161 ADMIN_RECEIVER_COMPONENT, requiredStrongAuthTimeout); 162 final long actualRequiredStrongAuthTimeout = 163 mParentDevicePolicyManager.getRequiredStrongAuthTimeout(ADMIN_RECEIVER_COMPONENT); 164 165 assertThat(actualRequiredStrongAuthTimeout).isEqualTo(requiredStrongAuthTimeout); 166 } 167 168 public abstract class FakeComponent extends BroadcastReceiver {} 169 } 170