Skip to content
Snippets Groups Projects
Commit 42871a37 authored by youngtaecha's avatar youngtaecha
Browse files

Add util methods to validate satellite config data

isValidPattern
isValidCountryCode

Bug: 332963485
Test: atest com.android.internal.telephony.tests

Change-Id: I6ebfdadc4a51f34d1eb4976176f8343c53791cff
parent 30da9b25
No related branches found
No related tags found
No related merge requests found
......@@ -349,21 +349,38 @@ public final class TelephonyUtils {
}
/**
* @param plmn target plmn for validation.
* @return {@code true} if the target plmn is valid {@code false} otherwise.
* @param input string that want to be compared.
* @param regex string that express regular expression
* @return {@code true} if matched {@code false} otherwise.
*/
public static boolean isValidPlmn(@Nullable String plmn) {
if (TextUtils.isEmpty(plmn)) {
private static boolean isValidPattern(@Nullable String input, @Nullable String regex) {
if (TextUtils.isEmpty(input) || TextUtils.isEmpty(regex)) {
return false;
}
Pattern pattern = Pattern.compile("^(?:[0-9]{3})(?:[0-9]{2}|[0-9]{3})$");
Matcher matcher = pattern.matcher(plmn);
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(input);
if (!matcher.matches()) {
return false;
}
return true;
}
/**
* @param countryCode two letters country code based on the ISO 3166-1.
* @return {@code true} if the countryCode is valid {@code false} otherwise.
*/
public static boolean isValidCountryCode(@Nullable String countryCode) {
return isValidPattern(countryCode, "^[A-Za-z]{2}$");
}
/**
* @param plmn target plmn for validation.
* @return {@code true} if the target plmn is valid {@code false} otherwise.
*/
public static boolean isValidPlmn(@Nullable String plmn) {
return isValidPattern(plmn, "^(?:[0-9]{3})(?:[0-9]{2}|[0-9]{3})$");
}
/**
* @param serviceType target serviceType for validation.
* @return {@code true} if the target serviceType is valid {@code false} otherwise.
......
......@@ -85,6 +85,8 @@ public class TelephonyUtilsTest {
assertTrue(TelephonyUtils.isValidPlmn("45006"));
assertFalse(TelephonyUtils.isValidPlmn("1234567"));
assertFalse(TelephonyUtils.isValidPlmn("1234"));
assertFalse(TelephonyUtils.isValidPlmn(""));
assertFalse(TelephonyUtils.isValidPlmn(null));
}
@Test
......@@ -94,6 +96,19 @@ public class TelephonyUtilsTest {
assertFalse(TelephonyUtils.isValidService(FIRST_SERVICE_TYPE - 1));
assertFalse(TelephonyUtils.isValidService(LAST_SERVICE_TYPE + 1));
}
@Test
public void testIsValidCountryCode() {
assertTrue(TelephonyUtils.isValidCountryCode("US"));
assertTrue(TelephonyUtils.isValidCountryCode("cn"));
assertFalse(TelephonyUtils.isValidCountryCode("11"));
assertFalse(TelephonyUtils.isValidCountryCode("USA"));
assertFalse(TelephonyUtils.isValidCountryCode("chn"));
assertFalse(TelephonyUtils.isValidCountryCode("U"));
assertFalse(TelephonyUtils.isValidCountryCode("G7"));
assertFalse(TelephonyUtils.isValidCountryCode(""));
assertFalse(TelephonyUtils.isValidCountryCode(null));
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment