diff options
author | Handa Wang <handaw@google.com> | 2024-04-16 08:25:38 +0000 |
---|---|---|
committer | Gerrit Code Review <noreply-gerritcodereview@google.com> | 2024-04-16 08:25:38 +0000 |
commit | f41e6019dd3d10d8296eb585fb623c3e32fd0b94 (patch) | |
tree | 9965ea489b84d3ebcdbe36a79bbe52e08c3e24de | |
parent | 22d42e607956e65295c636bf616050724741e9b1 (diff) | |
parent | 8310a5e2d44c0990eaf40e5e84b6bedece545fc2 (diff) | |
download | Connectivity-f41e6019dd3d10d8296eb585fb623c3e32fd0b94.tar.gz |
Merge "[mdns] allow hostnames beginning with a digit" into main
-rw-r--r-- | service-t/src/com/android/server/NsdService.java | 10 | ||||
-rw-r--r-- | tests/unit/java/com/android/server/NsdServiceTest.java | 32 |
2 files changed, 41 insertions, 1 deletions
diff --git a/service-t/src/com/android/server/NsdService.java b/service-t/src/com/android/server/NsdService.java index 2e258ab686..7c1ca309d6 100644 --- a/service-t/src/com/android/server/NsdService.java +++ b/service-t/src/com/android/server/NsdService.java @@ -1825,12 +1825,20 @@ public class NsdService extends INsdManager.Stub { * <p>For now NsdService only allows single-label hostnames conforming to RFC 1035. In other * words, the hostname should be at most 63 characters long and it only contains letters, digits * and hyphens. + * + * <p>Additionally, this allows hostname starting with a digit to support Matter devices. Per + * Matter spec 4.3.1.1: + * + * <p>The target host name SHALL be constructed using one of the available link-layer addresses, + * such as a 48-bit device MAC address (for Ethernet and Wi‑Fi) or a 64-bit MAC Extended Address + * (for Thread) expressed as a fixed-length twelve-character (or sixteen-character) hexadecimal + * string, encoded as ASCII (UTF-8) text using capital letters, e.g., B75AFB458ECD.<domain>. */ public static boolean checkHostname(@Nullable String hostname) { if (hostname == null) { return true; } - String HOSTNAME_REGEX = "^[a-zA-Z]([a-zA-Z0-9-_]{0,61}[a-zA-Z0-9])?$"; + String HOSTNAME_REGEX = "^[a-zA-Z0-9]([a-zA-Z0-9-_]{0,61}[a-zA-Z0-9])?$"; return Pattern.compile(HOSTNAME_REGEX).matcher(hostname).matches(); } diff --git a/tests/unit/java/com/android/server/NsdServiceTest.java b/tests/unit/java/com/android/server/NsdServiceTest.java index 5731d012d0..aece3f7adc 100644 --- a/tests/unit/java/com/android/server/NsdServiceTest.java +++ b/tests/unit/java/com/android/server/NsdServiceTest.java @@ -41,6 +41,7 @@ import static com.android.networkstack.apishim.api33.ConstantsShim.REGISTER_NSD_ import static com.android.server.NsdService.DEFAULT_RUNNING_APP_ACTIVE_IMPORTANCE_CUTOFF; import static com.android.server.NsdService.MdnsListener; import static com.android.server.NsdService.NO_TRANSACTION; +import static com.android.server.NsdService.checkHostname; import static com.android.server.NsdService.parseTypeAndSubtype; import static com.android.testutils.ContextUtils.mockService; @@ -53,6 +54,7 @@ import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertThrows; import static org.junit.Assert.assertTrue; +import static org.junit.Assert.assertFalse; import static org.mockito.ArgumentMatchers.anyInt; import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.ArgumentMatchers.argThat; @@ -1726,6 +1728,36 @@ public class NsdServiceTest { } @Test + public void TestCheckHostname() { + // Valid cases + assertTrue(checkHostname(null)); + assertTrue(checkHostname("a")); + assertTrue(checkHostname("1")); + assertTrue(checkHostname("a-1234-bbbb-cccc000")); + assertTrue(checkHostname("A-1234-BBbb-CCCC000")); + assertTrue(checkHostname("1234-bbbb-cccc000")); + assertTrue(checkHostname("0123456789abcdef" + + "0123456789abcdef" + + "0123456789abcdef" + + "0123456789abcde" // 63 characters + )); + + // Invalid cases + assertFalse(checkHostname("?")); + assertFalse(checkHostname("/")); + assertFalse(checkHostname("a-")); + assertFalse(checkHostname("B-")); + assertFalse(checkHostname("-A")); + assertFalse(checkHostname("-b")); + assertFalse(checkHostname("-1-")); + assertFalse(checkHostname("0123456789abcdef" + + "0123456789abcdef" + + "0123456789abcdef" + + "0123456789abcdef" // 64 characters + )); + } + + @Test @EnableCompatChanges(ENABLE_PLATFORM_MDNS_BACKEND) public void testEnablePlatformMdnsBackend() { final NsdManager client = connectClient(mService); |