Avoid going over max service name length when renaming NSD services
As per RFC 1034/1035, the max size of the label is 63 bytes. It should also be guaranteed when the serviceName is renamed due to the conflict. Bug: 265865456 Test: atest FrameworksNetTests Change-Id: I077d8abdb91071db62b9618d9918e3a12682aaf4
This commit is contained in:
@@ -47,6 +47,8 @@ import org.mockito.Mockito.verify
|
||||
|
||||
private const val SERVICE_ID_1 = 1
|
||||
private const val SERVICE_ID_2 = 2
|
||||
private const val LONG_SERVICE_ID_1 = 3
|
||||
private const val LONG_SERVICE_ID_2 = 4
|
||||
private const val TIMEOUT_MS = 10_000L
|
||||
private val TEST_ADDR = parseNumericAddress("2001:db8::123")
|
||||
private val TEST_LINKADDR = LinkAddress(TEST_ADDR, 64 /* prefixLength */)
|
||||
@@ -56,16 +58,30 @@ private val TEST_HOSTNAME = arrayOf("Android_test", "local")
|
||||
|
||||
private val SERVICE_1 = NsdServiceInfo("TestServiceName", "_advertisertest._tcp").apply {
|
||||
port = 12345
|
||||
host = TEST_ADDR
|
||||
hostAddresses = listOf(TEST_ADDR)
|
||||
network = TEST_NETWORK_1
|
||||
}
|
||||
|
||||
private val LONG_SERVICE_1 =
|
||||
NsdServiceInfo("a".repeat(48) + "TestServiceName", "_longadvertisertest._tcp").apply {
|
||||
port = 12345
|
||||
hostAddresses = listOf(TEST_ADDR)
|
||||
network = TEST_NETWORK_1
|
||||
}
|
||||
|
||||
private val ALL_NETWORKS_SERVICE = NsdServiceInfo("TestServiceName", "_advertisertest._tcp").apply {
|
||||
port = 12345
|
||||
host = TEST_ADDR
|
||||
hostAddresses = listOf(TEST_ADDR)
|
||||
network = null
|
||||
}
|
||||
|
||||
private val LONG_ALL_NETWORKS_SERVICE =
|
||||
NsdServiceInfo("a".repeat(48) + "TestServiceName", "_longadvertisertest._tcp").apply {
|
||||
port = 12345
|
||||
hostAddresses = listOf(TEST_ADDR)
|
||||
network = null
|
||||
}
|
||||
|
||||
@RunWith(DevSdkIgnoreRunner::class)
|
||||
@IgnoreUpTo(Build.VERSION_CODES.S_V2)
|
||||
class MdnsAdvertiserTest {
|
||||
@@ -191,6 +207,9 @@ class MdnsAdvertiserTest {
|
||||
verify(socketProvider).requestSocket(eq(null), allNetSocketCbCaptor.capture())
|
||||
val allNetSocketCb = allNetSocketCbCaptor.value
|
||||
|
||||
postSync { advertiser.addService(LONG_SERVICE_ID_1, LONG_SERVICE_1) }
|
||||
postSync { advertiser.addService(LONG_SERVICE_ID_2, LONG_ALL_NETWORKS_SERVICE) }
|
||||
|
||||
// Callbacks for matching network and all networks both get the socket
|
||||
postSync {
|
||||
oneNetSocketCb.onSocketCreated(TEST_NETWORK_1, mockSocket1, listOf(TEST_LINKADDR))
|
||||
@@ -200,10 +219,18 @@ class MdnsAdvertiserTest {
|
||||
val expectedRenamed = NsdServiceInfo(
|
||||
"${ALL_NETWORKS_SERVICE.serviceName} (2)", ALL_NETWORKS_SERVICE.serviceType).apply {
|
||||
port = ALL_NETWORKS_SERVICE.port
|
||||
host = ALL_NETWORKS_SERVICE.host
|
||||
hostAddresses = ALL_NETWORKS_SERVICE.hostAddresses
|
||||
network = ALL_NETWORKS_SERVICE.network
|
||||
}
|
||||
|
||||
val expectedLongRenamed = NsdServiceInfo(
|
||||
"${LONG_ALL_NETWORKS_SERVICE.serviceName.dropLast(4)} (2)",
|
||||
LONG_ALL_NETWORKS_SERVICE.serviceType).apply {
|
||||
port = LONG_ALL_NETWORKS_SERVICE.port
|
||||
hostAddresses = LONG_ALL_NETWORKS_SERVICE.hostAddresses
|
||||
network = LONG_ALL_NETWORKS_SERVICE.network
|
||||
}
|
||||
|
||||
val intAdvCbCaptor = ArgumentCaptor.forClass(MdnsInterfaceAdvertiser.Callback::class.java)
|
||||
verify(mockDeps).makeAdvertiser(eq(mockSocket1), eq(listOf(TEST_LINKADDR)),
|
||||
eq(thread.looper), any(), intAdvCbCaptor.capture(), eq(TEST_HOSTNAME)
|
||||
@@ -212,6 +239,10 @@ class MdnsAdvertiserTest {
|
||||
argThat { it.matches(SERVICE_1) })
|
||||
verify(mockInterfaceAdvertiser1).addService(eq(SERVICE_ID_2),
|
||||
argThat { it.matches(expectedRenamed) })
|
||||
verify(mockInterfaceAdvertiser1).addService(eq(LONG_SERVICE_ID_1),
|
||||
argThat { it.matches(LONG_SERVICE_1) })
|
||||
verify(mockInterfaceAdvertiser1).addService(eq(LONG_SERVICE_ID_2),
|
||||
argThat { it.matches(expectedLongRenamed) })
|
||||
|
||||
doReturn(false).`when`(mockInterfaceAdvertiser1).isProbing(SERVICE_ID_1)
|
||||
postSync { intAdvCbCaptor.value.onRegisterServiceSucceeded(
|
||||
@@ -251,7 +282,7 @@ private fun NsdServiceInfo.matches(other: NsdServiceInfo): Boolean {
|
||||
return Objects.equals(serviceName, other.serviceName) &&
|
||||
Objects.equals(serviceType, other.serviceType) &&
|
||||
Objects.equals(attributes, other.attributes) &&
|
||||
Objects.equals(host, other.host) &&
|
||||
Objects.equals(hostAddresses, other.hostAddresses) &&
|
||||
port == other.port &&
|
||||
Objects.equals(network, other.network)
|
||||
}
|
||||
|
||||
@@ -19,6 +19,7 @@ package com.android.server.connectivity.mdns.util
|
||||
import android.os.Build
|
||||
import com.android.server.connectivity.mdns.util.MdnsUtils.equalsIgnoreDnsCase
|
||||
import com.android.server.connectivity.mdns.util.MdnsUtils.toDnsLowerCase
|
||||
import com.android.server.connectivity.mdns.util.MdnsUtils.truncateServiceName
|
||||
import com.android.testutils.DevSdkIgnoreRule
|
||||
import com.android.testutils.DevSdkIgnoreRunner
|
||||
import org.junit.Assert.assertEquals
|
||||
@@ -65,4 +66,10 @@ class MdnsUtilsTest {
|
||||
"Test: >\ud83c\udff4\udb40\udc67\udb40\udc62\udb40" +
|
||||
"\udc77\udb40\udc6c\udb40\udc73\udb40\udc7f<"))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testTruncateServiceName() {
|
||||
assertEquals(truncateServiceName("测试abcde", 7), "测试a")
|
||||
assertEquals(truncateServiceName("测试abcde", 100), "测试abcde")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user