Merge "Check settings validators for NPE" into pi-dev
am: 3a95d0bb94
Change-Id: I75fe10196f29f10f80631f2a11ceda9e8a6d5d87
This commit is contained in:
@@ -3114,10 +3114,10 @@ public final class Settings {
|
||||
|
||||
private static final Validator FONT_SCALE_VALIDATOR = new Validator() {
|
||||
@Override
|
||||
public boolean validate(String value) {
|
||||
public boolean validate(@Nullable String value) {
|
||||
try {
|
||||
return Float.parseFloat(value) >= 0;
|
||||
} catch (NumberFormatException e) {
|
||||
} catch (NumberFormatException | NullPointerException e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -3655,11 +3655,11 @@ public final class Settings {
|
||||
/** @hide */
|
||||
public static final Validator DATE_FORMAT_VALIDATOR = new Validator() {
|
||||
@Override
|
||||
public boolean validate(String value) {
|
||||
public boolean validate(@Nullable String value) {
|
||||
try {
|
||||
new SimpleDateFormat(value);
|
||||
return true;
|
||||
} catch (IllegalArgumentException e) {
|
||||
} catch (IllegalArgumentException | NullPointerException e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -4074,7 +4074,7 @@ public final class Settings {
|
||||
/** @hide */
|
||||
public static final Validator EGG_MODE_VALIDATOR = new Validator() {
|
||||
@Override
|
||||
public boolean validate(String value) {
|
||||
public boolean validate(@Nullable String value) {
|
||||
try {
|
||||
return Long.parseLong(value) >= 0;
|
||||
} catch (NumberFormatException e) {
|
||||
@@ -5973,7 +5973,7 @@ public final class Settings {
|
||||
private static final Validator ACCESSIBILITY_BUTTON_TARGET_COMPONENT_VALIDATOR =
|
||||
new Validator() {
|
||||
@Override
|
||||
public boolean validate(String value) {
|
||||
public boolean validate(@Nullable String value) {
|
||||
// technically either ComponentName or class name, but there's proper value
|
||||
// validation at callsites, so allow any non-null string
|
||||
return value != null;
|
||||
@@ -6490,7 +6490,7 @@ public final class Settings {
|
||||
|
||||
private static final Validator TTS_DEFAULT_LOCALE_VALIDATOR = new Validator() {
|
||||
@Override
|
||||
public boolean validate(String value) {
|
||||
public boolean validate(@Nullable String value) {
|
||||
if (value == null || value.length() == 0) {
|
||||
return false;
|
||||
}
|
||||
@@ -7728,7 +7728,7 @@ public final class Settings {
|
||||
|
||||
private static final Validator QS_TILES_VALIDATOR = new Validator() {
|
||||
@Override
|
||||
public boolean validate(String value) {
|
||||
public boolean validate(@Nullable String value) {
|
||||
if (value == null) {
|
||||
return false;
|
||||
}
|
||||
@@ -7787,7 +7787,7 @@ public final class Settings {
|
||||
|
||||
private static final Validator QS_AUTO_ADDED_TILES_VALIDATOR = new Validator() {
|
||||
@Override
|
||||
public boolean validate(String value) {
|
||||
public boolean validate(@Nullable String value) {
|
||||
if (value == null) {
|
||||
return false;
|
||||
}
|
||||
@@ -8661,7 +8661,7 @@ public final class Settings {
|
||||
|
||||
private static final Validator STAY_ON_WHILE_PLUGGED_IN_VALIDATOR = new Validator() {
|
||||
@Override
|
||||
public boolean validate(String value) {
|
||||
public boolean validate(@Nullable String value) {
|
||||
try {
|
||||
int val = Integer.parseInt(value);
|
||||
return (val == 0)
|
||||
@@ -9695,7 +9695,7 @@ public final class Settings {
|
||||
|
||||
private static final Validator USE_OPEN_WIFI_PACKAGE_VALIDATOR = new Validator() {
|
||||
@Override
|
||||
public boolean validate(String value) {
|
||||
public boolean validate(@Nullable String value) {
|
||||
return (value == null) || PACKAGE_NAME_VALIDATOR.validate(value);
|
||||
}
|
||||
};
|
||||
@@ -11390,7 +11390,7 @@ public final class Settings {
|
||||
private static final Validator ENCODED_SURROUND_OUTPUT_ENABLED_FORMATS_VALIDATOR =
|
||||
new Validator() {
|
||||
@Override
|
||||
public boolean validate(String value) {
|
||||
public boolean validate(@Nullable String value) {
|
||||
try {
|
||||
String[] surroundFormats = TextUtils.split(value, ",");
|
||||
for (String format : surroundFormats) {
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
|
||||
package android.provider;
|
||||
|
||||
import android.annotation.Nullable;
|
||||
import android.content.ComponentName;
|
||||
import android.net.Uri;
|
||||
|
||||
@@ -36,14 +37,14 @@ public class SettingsValidators {
|
||||
|
||||
public static final Validator ANY_STRING_VALIDATOR = new Validator() {
|
||||
@Override
|
||||
public boolean validate(String value) {
|
||||
public boolean validate(@Nullable String value) {
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
public static final Validator NON_NEGATIVE_INTEGER_VALIDATOR = new Validator() {
|
||||
@Override
|
||||
public boolean validate(String value) {
|
||||
public boolean validate(@Nullable String value) {
|
||||
try {
|
||||
return Integer.parseInt(value) >= 0;
|
||||
} catch (NumberFormatException e) {
|
||||
@@ -54,7 +55,7 @@ public class SettingsValidators {
|
||||
|
||||
public static final Validator ANY_INTEGER_VALIDATOR = new Validator() {
|
||||
@Override
|
||||
public boolean validate(String value) {
|
||||
public boolean validate(@Nullable String value) {
|
||||
try {
|
||||
Integer.parseInt(value);
|
||||
return true;
|
||||
@@ -66,7 +67,7 @@ public class SettingsValidators {
|
||||
|
||||
public static final Validator URI_VALIDATOR = new Validator() {
|
||||
@Override
|
||||
public boolean validate(String value) {
|
||||
public boolean validate(@Nullable String value) {
|
||||
try {
|
||||
Uri.decode(value);
|
||||
return true;
|
||||
@@ -78,14 +79,14 @@ public class SettingsValidators {
|
||||
|
||||
public static final Validator COMPONENT_NAME_VALIDATOR = new Validator() {
|
||||
@Override
|
||||
public boolean validate(String value) {
|
||||
public boolean validate(@Nullable String value) {
|
||||
return value != null && ComponentName.unflattenFromString(value) != null;
|
||||
}
|
||||
};
|
||||
|
||||
public static final Validator PACKAGE_NAME_VALIDATOR = new Validator() {
|
||||
@Override
|
||||
public boolean validate(String value) {
|
||||
public boolean validate(@Nullable String value) {
|
||||
return value != null && isStringPackageName(value);
|
||||
}
|
||||
|
||||
@@ -122,7 +123,7 @@ public class SettingsValidators {
|
||||
private static final int MAX_IPV6_LENGTH = 45;
|
||||
|
||||
@Override
|
||||
public boolean validate(String value) {
|
||||
public boolean validate(@Nullable String value) {
|
||||
if (value == null) {
|
||||
return false;
|
||||
}
|
||||
@@ -132,7 +133,7 @@ public class SettingsValidators {
|
||||
|
||||
public static final Validator LOCALE_VALIDATOR = new Validator() {
|
||||
@Override
|
||||
public boolean validate(String value) {
|
||||
public boolean validate(@Nullable String value) {
|
||||
if (value == null) {
|
||||
return false;
|
||||
}
|
||||
@@ -147,7 +148,11 @@ public class SettingsValidators {
|
||||
};
|
||||
|
||||
public interface Validator {
|
||||
boolean validate(String value);
|
||||
/**
|
||||
* Returns whether the input value is valid. Subclasses should handle the case where the
|
||||
* input value is {@code null}.
|
||||
*/
|
||||
boolean validate(@Nullable String value);
|
||||
}
|
||||
|
||||
public static final class DiscreteValueValidator implements Validator {
|
||||
@@ -158,7 +163,7 @@ public class SettingsValidators {
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean validate(String value) {
|
||||
public boolean validate(@Nullable String value) {
|
||||
return ArrayUtils.contains(mValues, value);
|
||||
}
|
||||
}
|
||||
@@ -173,7 +178,7 @@ public class SettingsValidators {
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean validate(String value) {
|
||||
public boolean validate(@Nullable String value) {
|
||||
try {
|
||||
final int intValue = Integer.parseInt(value);
|
||||
return intValue >= mMin && intValue <= mMax;
|
||||
@@ -193,11 +198,11 @@ public class SettingsValidators {
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean validate(String value) {
|
||||
public boolean validate(@Nullable String value) {
|
||||
try {
|
||||
final float floatValue = Float.parseFloat(value);
|
||||
return floatValue >= mMin && floatValue <= mMax;
|
||||
} catch (NumberFormatException e) {
|
||||
} catch (NumberFormatException | NullPointerException e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -211,7 +216,7 @@ public class SettingsValidators {
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean validate(String value) {
|
||||
public boolean validate(@Nullable String value) {
|
||||
if (value == null) {
|
||||
return false;
|
||||
}
|
||||
@@ -233,7 +238,7 @@ public class SettingsValidators {
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean validate(String value) {
|
||||
public boolean validate(@Nullable String value) {
|
||||
if (value == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -47,6 +47,11 @@ public class SettingsValidatorsTest {
|
||||
assertFalse(SettingsValidators.NON_NEGATIVE_INTEGER_VALIDATOR.validate("rectangle"));
|
||||
}
|
||||
|
||||
@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"));
|
||||
@@ -55,6 +60,16 @@ public class SettingsValidatorsTest {
|
||||
assertFalse(SettingsValidators.ANY_INTEGER_VALIDATOR.validate("rectangle"));
|
||||
}
|
||||
|
||||
@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(
|
||||
@@ -63,10 +78,15 @@ public class SettingsValidatorsTest {
|
||||
}
|
||||
|
||||
@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"));
|
||||
@@ -74,6 +94,11 @@ public class SettingsValidatorsTest {
|
||||
assertFalse(SettingsValidators.LOCALE_VALIDATOR.validate("rectangle"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLocaleValidator_onNullValue_returnsFalse() {
|
||||
assertFalse(SettingsValidators.LOCALE_VALIDATOR.validate(null));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPackageNameValidator() {
|
||||
assertTrue(SettingsValidators.PACKAGE_NAME_VALIDATOR.validate(
|
||||
@@ -83,6 +108,11 @@ public class SettingsValidatorsTest {
|
||||
assertFalse(SettingsValidators.PACKAGE_NAME_VALIDATOR.validate(".com.google.5android"));
|
||||
}
|
||||
|
||||
@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"};
|
||||
@@ -93,6 +123,14 @@ public class SettingsValidatorsTest {
|
||||
assertFalse(v.validate("Cider")); // just juice pretending to be beer
|
||||
}
|
||||
|
||||
@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);
|
||||
@@ -103,6 +141,13 @@ public class SettingsValidatorsTest {
|
||||
assertFalse(v.validate("6"));
|
||||
}
|
||||
|
||||
@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);
|
||||
@@ -113,6 +158,13 @@ public class SettingsValidatorsTest {
|
||||
assertFalse(v.validate("6.0"));
|
||||
}
|
||||
|
||||
@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(",");
|
||||
@@ -121,6 +173,13 @@ public class SettingsValidatorsTest {
|
||||
assertFalse(v.validate("com.google.5android,android"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testComponentNameListValidator_onNullValue_returnsFalse() {
|
||||
Validator v = new SettingsValidators.ComponentNameListValidator(",");
|
||||
|
||||
assertFalse(v.validate(null));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPackageNameListValidator() {
|
||||
Validator v = new SettingsValidators.PackageNameListValidator(",");
|
||||
@@ -128,6 +187,17 @@ public class SettingsValidatorsTest {
|
||||
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() {
|
||||
|
||||
Reference in New Issue
Block a user