Check settings validators for NPE
From b/79910479, we need to have better checks on settings validators.
Catch NPE for various settings validators and add a @Nullable annotation
to try to prevent NPEs in new validators.
Bug: 80067772
Test: atest SettingsValidatorsTest
Change-Id: I9a5058e1bae1ba0dea084c0589aa3e4ef3a51836
diff --git a/core/tests/coretests/src/android/provider/SettingsValidatorsTest.java b/core/tests/coretests/src/android/provider/SettingsValidatorsTest.java
index c4d5b0c..50f24d3 100644
--- a/core/tests/coretests/src/android/provider/SettingsValidatorsTest.java
+++ b/core/tests/coretests/src/android/provider/SettingsValidatorsTest.java
@@ -48,6 +48,11 @@
}
@Test
+ public void testNonNegativeIntegerValidator_onNullValue_returnsFalse() {
+ assertFalse(SettingsValidators.NON_NEGATIVE_INTEGER_VALIDATOR.validate(null));
+ }
+
+ @Test
public void testAnyIntegerValidator() {
assertTrue(SettingsValidators.ANY_INTEGER_VALIDATOR.validate("1"));
assertTrue(SettingsValidators.ANY_INTEGER_VALIDATOR.validate("0"));
@@ -56,6 +61,16 @@
}
@Test
+ public void testAnyIntegerValidator_onNullValue_returnsFalse() {
+ assertFalse(SettingsValidators.ANY_INTEGER_VALIDATOR.validate(null));
+ }
+
+ @Test
+ public void testUriValidator_onNullValue_returnsTrue() {
+ assertTrue(SettingsValidators.URI_VALIDATOR.validate(null));
+ }
+
+ @Test
public void testComponentNameValidator() {
assertTrue(SettingsValidators.COMPONENT_NAME_VALIDATOR.validate(
"android/com.android.internal.backup.LocalTransport"));
@@ -63,11 +78,16 @@
}
@Test
- public void testComponentNameValidator_onNullValue_doesNotThrow() {
+ public void testComponentNameValidator_onNullValue_returnsFalse() {
assertFalse(SettingsValidators.COMPONENT_NAME_VALIDATOR.validate(null));
}
@Test
+ public void testLenientIpAddressValidator_onNullValue_returnsFalse() {
+ assertFalse(SettingsValidators.LENIENT_IP_ADDRESS_VALIDATOR.validate(null));
+ }
+
+ @Test
public void testLocaleValidator() {
assertTrue(SettingsValidators.LOCALE_VALIDATOR.validate("en_US"));
assertTrue(SettingsValidators.LOCALE_VALIDATOR.validate("es"));
@@ -75,6 +95,11 @@
}
@Test
+ public void testLocaleValidator_onNullValue_returnsFalse() {
+ assertFalse(SettingsValidators.LOCALE_VALIDATOR.validate(null));
+ }
+
+ @Test
public void testPackageNameValidator() {
assertTrue(SettingsValidators.PACKAGE_NAME_VALIDATOR.validate(
"com.google.android"));
@@ -84,6 +109,11 @@
}
@Test
+ public void testPackageNameValidator_onNullValue_returnsFalse() {
+ assertFalse(SettingsValidators.PACKAGE_NAME_VALIDATOR.validate(null));
+ }
+
+ @Test
public void testDiscreteValueValidator() {
String[] beerTypes = new String[]{"Ale", "American IPA", "Stout"};
Validator v = new SettingsValidators.DiscreteValueValidator(beerTypes);
@@ -94,6 +124,14 @@
}
@Test
+ public void testDiscreteValueValidator_onNullValue_returnsFalse() {
+ String[] discreteTypes = new String[]{"Type1", "Type2"};
+ Validator v = new SettingsValidators.DiscreteValueValidator(discreteTypes);
+
+ assertFalse(v.validate(null));
+ }
+
+ @Test
public void testInclusiveIntegerRangeValidator() {
Validator v = new SettingsValidators.InclusiveIntegerRangeValidator(0, 5);
assertTrue(v.validate("0"));
@@ -104,6 +142,13 @@
}
@Test
+ public void testInclusiveIntegerRangeValidator_onNullValue_returnsFalse() {
+ Validator v = new SettingsValidators.InclusiveIntegerRangeValidator(0, 5);
+
+ assertFalse(v.validate(null));
+ }
+
+ @Test
public void testInclusiveFloatRangeValidator() {
Validator v = new SettingsValidators.InclusiveFloatRangeValidator(0.0f, 5.0f);
assertTrue(v.validate("0.0"));
@@ -114,6 +159,13 @@
}
@Test
+ public void testInclusiveFloatRangeValidator_onNullValue_returnsFalse() {
+ Validator v = new SettingsValidators.InclusiveFloatRangeValidator(0.0f, 5.0f);
+
+ assertFalse(v.validate(null));
+ }
+
+ @Test
public void testComponentNameListValidator() {
Validator v = new SettingsValidators.ComponentNameListValidator(",");
assertTrue(v.validate("android/com.android.internal.backup.LocalTransport,"
@@ -122,12 +174,30 @@
}
@Test
+ public void testComponentNameListValidator_onNullValue_returnsFalse() {
+ Validator v = new SettingsValidators.ComponentNameListValidator(",");
+
+ assertFalse(v.validate(null));
+ }
+
+ @Test
public void testPackageNameListValidator() {
Validator v = new SettingsValidators.PackageNameListValidator(",");
assertTrue(v.validate("com.android.internal.backup.LocalTransport,com.google.android.gms"));
assertFalse(v.validate("5com.android.internal.backup.LocalTransport,android"));
}
+ @Test
+ public void testPackageNameListValidator_onNullValue_returnsFalse() {
+ Validator v = new SettingsValidators.PackageNameListValidator(",");
+
+ assertFalse(v.validate(null));
+ }
+
+ @Test
+ public void dateFormatValidator_onNullValue_returnsFalse() {
+ assertFalse(Settings.System.DATE_FORMAT_VALIDATOR.validate(null));
+ }
@Test
public void ensureAllBackedUpSystemSettingsHaveValidators() {