diff options
| author | 2020-04-17 11:50:17 -0700 | |
|---|---|---|
| committer | 2020-04-18 02:52:14 +0000 | |
| commit | feba8145853a7f08b1ac37d720ca5352f95cb6ae (patch) | |
| tree | 4e269a0f51344bc9cc221349185f2320c033a4d4 | |
| parent | 872afaeaeb15b33073fdc8a650a2e014abbc27da (diff) | |
Comply with naming convention in RFC6335.
Bug: 154268895
Test: atest AdbDebuggingManagerTest
Change-Id: I380e91be005471eb69b70eff6b047c0e6a0356d4
(cherry picked from commit acd10e2f9fbf32fe912d1746c52966248fc4b803)
| -rw-r--r-- | services/core/java/com/android/server/adb/AdbDebuggingManager.java | 10 | ||||
| -rw-r--r-- | services/tests/servicestests/src/com/android/server/adb/AdbDebuggingManagerTest.java | 92 |
2 files changed, 101 insertions, 1 deletions
diff --git a/services/core/java/com/android/server/adb/AdbDebuggingManager.java b/services/core/java/com/android/server/adb/AdbDebuggingManager.java index 86f4966a5356..27ea4716e12d 100644 --- a/services/core/java/com/android/server/adb/AdbDebuggingManager.java +++ b/services/core/java/com/android/server/adb/AdbDebuggingManager.java @@ -177,7 +177,15 @@ public class AdbDebuggingManager { private String mPairingCode; private String mGuid; private String mServiceName; - private final String mServiceType = "_adb_secure_pairing._tcp."; + // From RFC6763 (https://tools.ietf.org/html/rfc6763#section-7.2), + // The rules for Service Names [RFC6335] state that they may be no more + // than fifteen characters long (not counting the mandatory underscore), + // consisting of only letters, digits, and hyphens, must begin and end + // with a letter or digit, must not contain consecutive hyphens, and + // must contain at least one letter. + @VisibleForTesting + static final String SERVICE_PROTOCOL = "adb-tls-pairing"; + private final String mServiceType = String.format("_%s._tcp.", SERVICE_PROTOCOL); private int mPort; private native int native_pairing_start(String guid, String password); diff --git a/services/tests/servicestests/src/com/android/server/adb/AdbDebuggingManagerTest.java b/services/tests/servicestests/src/com/android/server/adb/AdbDebuggingManagerTest.java index 757a2b1280f3..cffff66b64f1 100644 --- a/services/tests/servicestests/src/com/android/server/adb/AdbDebuggingManagerTest.java +++ b/services/tests/servicestests/src/com/android/server/adb/AdbDebuggingManagerTest.java @@ -721,6 +721,98 @@ public final class AdbDebuggingManagerTest { isKeyInFile(TEST_KEY_2, mAdbKeyFile)); } + @Test + public void testIsValidMdnsServiceName() { + // Longer than 15 characters + assertFalse(isValidMdnsServiceName("abcd1234abcd1234")); + + // Contains invalid characters + assertFalse(isValidMdnsServiceName("a*a")); + assertFalse(isValidMdnsServiceName("a_a")); + assertFalse(isValidMdnsServiceName("_a")); + + // Does not begin or end with letter or digit + assertFalse(isValidMdnsServiceName("")); + assertFalse(isValidMdnsServiceName("-")); + assertFalse(isValidMdnsServiceName("-a")); + assertFalse(isValidMdnsServiceName("-1")); + assertFalse(isValidMdnsServiceName("a-")); + assertFalse(isValidMdnsServiceName("1-")); + + // Contains consecutive hyphens + assertFalse(isValidMdnsServiceName("a--a")); + + // Does not contain at least one letter + assertFalse(isValidMdnsServiceName("1")); + assertFalse(isValidMdnsServiceName("12")); + assertFalse(isValidMdnsServiceName("1-2")); + + // letter not within [a-zA-Z] + assertFalse(isValidMdnsServiceName("aés")); + + // Some valid names + assertTrue(isValidMdnsServiceName("a")); + assertTrue(isValidMdnsServiceName("a1")); + assertTrue(isValidMdnsServiceName("1A")); + assertTrue(isValidMdnsServiceName("aZ")); + assertTrue(isValidMdnsServiceName("a-Z")); + assertTrue(isValidMdnsServiceName("a-b-Z")); + assertTrue(isValidMdnsServiceName("abc-def-123-456")); + } + + @Test + public void testPairingThread_MdnsServiceName_RFC6335() { + assertTrue(isValidMdnsServiceName(AdbDebuggingManager.PairingThread.SERVICE_PROTOCOL)); + } + + private boolean isValidMdnsServiceName(String name) { + // The rules for Service Names [RFC6335] state that they may be no more + // than fifteen characters long (not counting the mandatory underscore), + // consisting of only letters, digits, and hyphens, must begin and end + // with a letter or digit, must not contain consecutive hyphens, and + // must contain at least one letter. + // No more than 15 characters long + final int len = name.length(); + if (name.isEmpty() || len > 15) { + return false; + } + + boolean hasAtLeastOneLetter = false; + boolean sawHyphen = false; + for (int i = 0; i < len; ++i) { + // Must contain at least one letter + // Only contains letters, digits and hyphens + char c = name.charAt(i); + if (c == '-') { + // Cannot be at beginning or end + if (i == 0 || i == len - 1) { + return false; + } + if (sawHyphen) { + // Consecutive hyphen found + return false; + } + sawHyphen = true; + continue; + } + + sawHyphen = false; + if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) { + hasAtLeastOneLetter = true; + continue; + } + + if (c >= '0' && c <= '9') { + continue; + } + + // Invalid character + return false; + } + + return hasAtLeastOneLetter; + } + /** * Runs an adb test with the provided configuration. * |