diff --git a/services/tests/servicestests/src/com/android/server/devicepolicy/DevicePolicyManagerTest.java b/services/tests/servicestests/src/com/android/server/devicepolicy/DevicePolicyManagerTest.java index 10d1e0e608c27..2777bdf910b7e 100644 --- a/services/tests/servicestests/src/com/android/server/devicepolicy/DevicePolicyManagerTest.java +++ b/services/tests/servicestests/src/com/android/server/devicepolicy/DevicePolicyManagerTest.java @@ -51,6 +51,7 @@ import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertWithMessage; import static org.junit.Assert.fail; +import static org.junit.Assume.assumeTrue; import static org.mockito.Matchers.any; import static org.mockito.Matchers.anyBoolean; import static org.mockito.Matchers.anyInt; @@ -185,6 +186,8 @@ public class DevicePolicyManagerTest extends DpmTestBase { public DevicePolicyManager parentDpm; public DevicePolicyManagerServiceTestable dpms; + private boolean mIsAutomotive; + /* * The CA cert below is the content of cacert.pem as generated by: * @@ -267,6 +270,9 @@ public class DevicePolicyManagerTest extends DpmTestBase { setUpUserManager(); when(getServices().lockPatternUtils.hasSecureLockScreen()).thenReturn(true); + + mIsAutomotive = mContext.getPackageManager() + .hasSystemFeature(PackageManager.FEATURE_AUTOMOTIVE); } private TransferOwnershipMetadataManager getMockTransferMetadataManager() { @@ -2122,10 +2128,13 @@ public class DevicePolicyManagerTest extends DpmTestBase { assertThat(dpm.getPasswordExpirationTimeout(admin1)) .isEqualTo(originalPasswordExpirationTimeout); - int originalPasswordQuality = dpm.getPasswordQuality(admin1); - assertExpectException(SecurityException.class, /* messageRegex= */ null, - () -> dpm.setPasswordQuality(admin1, DevicePolicyManager.PASSWORD_QUALITY_NUMERIC)); - assertThat(dpm.getPasswordQuality(admin1)).isEqualTo(originalPasswordQuality); + if (isDeprecatedPasswordApisSupported()) { + int originalPasswordQuality = dpm.getPasswordQuality(admin1); + assertExpectException(SecurityException.class, /* messageRegex= */ null, + () -> dpm.setPasswordQuality(admin1, + DevicePolicyManager.PASSWORD_QUALITY_NUMERIC)); + assertThat(dpm.getPasswordQuality(admin1)).isEqualTo(originalPasswordQuality); + } } @Test @@ -5236,6 +5245,8 @@ public class DevicePolicyManagerTest extends DpmTestBase { @Test public void testIsActivePasswordSufficient() throws Exception { + assumeDeprecatedPasswordApisSupported(); + mContext.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID; mContext.packageName = admin1.getPackageName(); setupDeviceOwner(); @@ -5288,6 +5299,8 @@ public class DevicePolicyManagerTest extends DpmTestBase { @Test public void testIsActivePasswordSufficient_noLockScreen() throws Exception { + assumeDeprecatedPasswordApisSupported(); + // If there is no lock screen, the password is considered empty no matter what, because // it provides no security. when(getServices().lockPatternUtils.hasSecureLockScreen()).thenReturn(false); @@ -5368,6 +5381,8 @@ public class DevicePolicyManagerTest extends DpmTestBase { @Test public void testGetAggregatedPasswordMetrics_IgnoreProfileRequirement() throws Exception { + assumeDeprecatedPasswordApisSupported(); + final int managedProfileUserId = CALLER_USER_HANDLE; final int managedProfileAdminUid = UserHandle.getUid(managedProfileUserId, DpmMockContext.SYSTEM_UID); @@ -5397,6 +5412,8 @@ public class DevicePolicyManagerTest extends DpmTestBase { @Test public void testCanSetPasswordRequirementOnParentPreS() throws Exception { + assumeDeprecatedPasswordApisSupported(); + final int managedProfileUserId = CALLER_USER_HANDLE; final int managedProfileAdminUid = UserHandle.getUid(managedProfileUserId, DpmMockContext.SYSTEM_UID); @@ -5412,6 +5429,8 @@ public class DevicePolicyManagerTest extends DpmTestBase { @Test public void testCannotSetPasswordRequirementOnParent() throws Exception { + assumeDeprecatedPasswordApisSupported(); + final int managedProfileUserId = CALLER_USER_HANDLE; final int managedProfileAdminUid = UserHandle.getUid(managedProfileUserId, DpmMockContext.SYSTEM_UID); @@ -5432,6 +5451,8 @@ public class DevicePolicyManagerTest extends DpmTestBase { @Test public void isActivePasswordSufficient_SeparateWorkChallenge_ProfileQualityRequirementMet() throws Exception { + assumeDeprecatedPasswordApisSupported(); + // Create work profile with empty separate challenge final int managedProfileUserId = 15; final int managedProfileAdminUid = UserHandle.getUid(managedProfileUserId, 19436); @@ -5455,6 +5476,8 @@ public class DevicePolicyManagerTest extends DpmTestBase { @Test public void isActivePasswordSufficient_SeparateWorkChallenge_ProfileComplexityRequirementMet() throws Exception { + assumeDeprecatedPasswordApisSupported(); + // Create work profile with empty separate challenge final int managedProfileUserId = 15; final int managedProfileAdminUid = UserHandle.getUid(managedProfileUserId, 19436); @@ -5478,6 +5501,8 @@ public class DevicePolicyManagerTest extends DpmTestBase { @Test public void isActivePasswordSufficient_SeparateWorkChallenge_ParentQualityRequirementMet() throws Exception { + assumeDeprecatedPasswordApisSupported(); + // Create work profile with empty separate challenge final int managedProfileUserId = 15; final int managedProfileAdminUid = UserHandle.getUid(managedProfileUserId, 19436); @@ -5524,6 +5549,8 @@ public class DevicePolicyManagerTest extends DpmTestBase { @Test public void isActivePasswordSufficient_UnifiedWorkChallenge_ProfileQualityRequirementMet() throws Exception { + assumeDeprecatedPasswordApisSupported(); + // Create work profile with unified challenge final int managedProfileUserId = 15; final int managedProfileAdminUid = UserHandle.getUid(managedProfileUserId, 19436); @@ -5570,6 +5597,8 @@ public class DevicePolicyManagerTest extends DpmTestBase { @Test public void isActivePasswordSufficient_UnifiedWorkChallenge_ParentQualityRequirementMet() throws Exception { + assumeDeprecatedPasswordApisSupported(); + // Create work profile with unified challenge final int managedProfileUserId = 15; final int managedProfileAdminUid = UserHandle.getUid(managedProfileUserId, 19436); @@ -5630,6 +5659,8 @@ public class DevicePolicyManagerTest extends DpmTestBase { @Test public void testPasswordQualityAppliesToParentPreS() throws Exception { + assumeDeprecatedPasswordApisSupported(); + final int managedProfileUserId = CALLER_USER_HANDLE; final int managedProfileAdminUid = UserHandle.getUid(managedProfileUserId, DpmMockContext.SYSTEM_UID); @@ -7290,6 +7321,8 @@ public class DevicePolicyManagerTest extends DpmTestBase { @Test public void testSetRequiredPasswordComplexity_UnauthorizedCallersOnDO() throws Exception { + assumeDeprecatedPasswordApisSupported(); + mContext.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID; setupDeviceOwner(); // DO must be able to set it. @@ -7305,6 +7338,8 @@ public class DevicePolicyManagerTest extends DpmTestBase { @Test public void testSetRequiredPasswordComplexity_UnauthorizedCallersOnPO() throws Exception { + assumeDeprecatedPasswordApisSupported(); + mContext.binder.callingUid = DpmMockContext.CALLER_UID; setupProfileOwner(); // PO must be able to set it. @@ -7319,6 +7354,8 @@ public class DevicePolicyManagerTest extends DpmTestBase { @Test public void testSetRequiredPasswordComplexity_validValuesOnly() throws Exception { + assumeDeprecatedPasswordApisSupported(); + mContext.binder.callingUid = DpmMockContext.CALLER_UID; setupProfileOwner(); @@ -7340,6 +7377,8 @@ public class DevicePolicyManagerTest extends DpmTestBase { @Test public void testSetRequiredPasswordComplexity_setAndGet() throws Exception { + assumeDeprecatedPasswordApisSupported(); + mContext.binder.callingUid = DpmMockContext.CALLER_UID; setupProfileOwner(); @@ -7353,6 +7392,8 @@ public class DevicePolicyManagerTest extends DpmTestBase { @Test public void testSetRequiredPasswordComplexityOnParent_setAndGet() throws Exception { + assumeDeprecatedPasswordApisSupported(); + final int managedProfileUserId = 15; final int managedProfileAdminUid = UserHandle.getUid(managedProfileUserId, 19436); @@ -7371,6 +7412,8 @@ public class DevicePolicyManagerTest extends DpmTestBase { @Test public void testSetRequiredPasswordComplexity_isSufficient() throws Exception { + assumeDeprecatedPasswordApisSupported(); + mContext.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID; mContext.packageName = admin1.getPackageName(); setupDeviceOwner(); @@ -7400,6 +7443,8 @@ public class DevicePolicyManagerTest extends DpmTestBase { @Test public void testSetRequiredPasswordComplexity_resetBySettingQuality() throws Exception { + assumeDeprecatedPasswordApisSupported(); + mContext.binder.callingUid = DpmMockContext.CALLER_UID; setupProfileOwner(); @@ -7412,6 +7457,8 @@ public class DevicePolicyManagerTest extends DpmTestBase { @Test public void testSetRequiredPasswordComplexity_overridesQuality() throws Exception { + assumeDeprecatedPasswordApisSupported(); + mContext.binder.callingUid = DpmMockContext.CALLER_UID; setupProfileOwner(); @@ -7426,6 +7473,8 @@ public class DevicePolicyManagerTest extends DpmTestBase { @Test public void testSetRequiredPasswordComplexityFailsWithQualityOnParent() throws Exception { + assumeDeprecatedPasswordApisSupported(); + final int managedProfileUserId = CALLER_USER_HANDLE; final int managedProfileAdminUid = UserHandle.getUid(managedProfileUserId, DpmMockContext.SYSTEM_UID); @@ -7440,6 +7489,8 @@ public class DevicePolicyManagerTest extends DpmTestBase { @Test public void testSetQualityOnParentFailsWithComplexityOnProfile() throws Exception { + assumeDeprecatedPasswordApisSupported(); + final int managedProfileUserId = CALLER_USER_HANDLE; final int managedProfileAdminUid = UserHandle.getUid(managedProfileUserId, DpmMockContext.SYSTEM_UID); @@ -8020,4 +8071,13 @@ public class DevicePolicyManagerTest extends DpmTestBase { when(mContext.getResources().getStringArray(R.array.vendor_policy_exempt_apps)) .thenReturn(new String[0]); } + + private boolean isDeprecatedPasswordApisSupported() { + return !mIsAutomotive; + } + + private void assumeDeprecatedPasswordApisSupported() { + assumeTrue("device doesn't support deprecated password APIs", + isDeprecatedPasswordApisSupported()); + } }