Merge "Factor out response decoding into MdnsPacket"
This commit is contained in:
@@ -91,7 +91,7 @@ class MdnsAnnouncerTest {
|
||||
scapy.raw(scapy.dns_compress(scapy.DNS(rd=0, qr=1, aa=1,
|
||||
qd = None,
|
||||
an =
|
||||
scapy.DNSRR(type='PTR', rrname='123.0.2.192.in-addr.arpa.', rdata='Android.local',
|
||||
scapy.DNSRR(type='PTR', rrname='123.2.0.192.in-addr.arpa.', rdata='Android.local',
|
||||
rclass=0x8001, ttl=120) /
|
||||
scapy.DNSRR(type='PTR',
|
||||
rrname='3.2.1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.8.B.D.0.1.0.0.2.ip6.arpa',
|
||||
@@ -111,8 +111,8 @@ class MdnsAnnouncerTest {
|
||||
scapy.DNSRR(type='AAAA', rrname='Android.local', rclass=0x8001, rdata='2001:db8::456',
|
||||
ttl=120),
|
||||
ar =
|
||||
scapy.DNSRRNSEC(rrname='123.0.2.192.in-addr.arpa.', rclass=0x8001, ttl=120,
|
||||
nextname='123.0.2.192.in-addr.arpa.', typebitmaps=[12]) /
|
||||
scapy.DNSRRNSEC(rrname='123.2.0.192.in-addr.arpa.', rclass=0x8001, ttl=120,
|
||||
nextname='123.2.0.192.in-addr.arpa.', typebitmaps=[12]) /
|
||||
scapy.DNSRRNSEC(
|
||||
rrname='3.2.1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.8.B.D.0.1.0.0.2.ip6.arpa',
|
||||
rclass=0x8001, ttl=120,
|
||||
@@ -131,7 +131,7 @@ class MdnsAnnouncerTest {
|
||||
typebitmaps=[1, 28]))
|
||||
)).hex().upper()
|
||||
*/
|
||||
val expected = "00008400000000090000000503313233013001320331393207696E2D61646472046172706" +
|
||||
val expected = "00008400000000090000000503313233013201300331393207696E2D61646472046172706" +
|
||||
"100000C800100000078000F07416E64726F6964056C6F63616C00013301320131013001300130013" +
|
||||
"00130013001300130013001300130013001300130013001300130013001300130013001380142014" +
|
||||
"40130013101300130013203697036C020000C8001000000780002C030013601350134C045000C800" +
|
||||
@@ -149,7 +149,7 @@ class MdnsAnnouncerTest {
|
||||
val v4Addr = parseNumericAddress("192.0.2.123")
|
||||
val v6Addr1 = parseNumericAddress("2001:DB8::123")
|
||||
val v6Addr2 = parseNumericAddress("2001:DB8::456")
|
||||
val v4AddrRev = arrayOf("123", "0", "2", "192", "in-addr", "arpa")
|
||||
val v4AddrRev = getReverseDnsAddress(v4Addr)
|
||||
val v6Addr1Rev = getReverseDnsAddress(v6Addr1)
|
||||
val v6Addr2Rev = getReverseDnsAddress(v6Addr2)
|
||||
|
||||
@@ -254,7 +254,10 @@ class MdnsAnnouncerTest {
|
||||
verify(socket, atLeast(i + 1)).send(any())
|
||||
val now = SystemClock.elapsedRealtime()
|
||||
assertTrue(now > timeStart + startDelay + i * FIRST_ANNOUNCES_DELAY)
|
||||
assertTrue(now < timeStart + startDelay + (i + 1) * FIRST_ANNOUNCES_DELAY)
|
||||
// Loops can be much slower than the expected timing (>100ms delay), use
|
||||
// TEST_TIMEOUT_MS as tolerance.
|
||||
assertTrue(now < timeStart + startDelay + (i + 1) * FIRST_ANNOUNCES_DELAY +
|
||||
TEST_TIMEOUT_MS)
|
||||
}
|
||||
|
||||
// Subsequent announces should happen quickly (NEXT_ANNOUNCES_DELAY)
|
||||
|
||||
@@ -0,0 +1,70 @@
|
||||
/*
|
||||
* Copyright (C) 2022 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.android.server.connectivity.mdns
|
||||
|
||||
import android.net.InetAddresses
|
||||
import com.android.net.module.util.HexDump
|
||||
import com.android.testutils.DevSdkIgnoreRunner
|
||||
import kotlin.test.assertContentEquals
|
||||
import kotlin.test.assertEquals
|
||||
import kotlin.test.assertTrue
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
|
||||
@RunWith(DevSdkIgnoreRunner::class)
|
||||
class MdnsPacketTest {
|
||||
@Test
|
||||
fun testParseQuery() {
|
||||
// Probe packet with 1 question for Android.local, and 4 additionalRecords with 4 addresses
|
||||
// for Android.local (similar to legacy mdnsresponder probes, although it used to put 4
|
||||
// identical questions(!!) for Android.local when there were 4 addresses).
|
||||
val packetHex = "00000000000100000004000007416e64726f6964056c6f63616c0000ff0001c00c000100" +
|
||||
"01000000780004c000027bc00c001c000100000078001020010db8000000000000000000000123c0" +
|
||||
"0c001c000100000078001020010db8000000000000000000000456c00c001c000100000078001020" +
|
||||
"010db8000000000000000000000789"
|
||||
|
||||
val bytes = HexDump.hexStringToByteArray(packetHex)
|
||||
val reader = MdnsPacketReader(bytes, bytes.size)
|
||||
val packet = MdnsPacket.parse(reader)
|
||||
|
||||
assertEquals(1, packet.questions.size)
|
||||
assertEquals(0, packet.answers.size)
|
||||
assertEquals(4, packet.authorityRecords.size)
|
||||
assertEquals(0, packet.additionalRecords.size)
|
||||
|
||||
val hostname = arrayOf("Android", "local")
|
||||
packet.questions[0].let {
|
||||
assertTrue(it is MdnsAnyRecord)
|
||||
assertContentEquals(hostname, it.name)
|
||||
}
|
||||
|
||||
packet.authorityRecords.forEach {
|
||||
assertTrue(it is MdnsInetAddressRecord)
|
||||
assertContentEquals(hostname, it.name)
|
||||
assertEquals(120000, it.ttl)
|
||||
}
|
||||
|
||||
assertEquals(InetAddresses.parseNumericAddress("192.0.2.123"),
|
||||
(packet.authorityRecords[0] as MdnsInetAddressRecord).inet4Address)
|
||||
assertEquals(InetAddresses.parseNumericAddress("2001:db8::123"),
|
||||
(packet.authorityRecords[1] as MdnsInetAddressRecord).inet6Address)
|
||||
assertEquals(InetAddresses.parseNumericAddress("2001:db8::456"),
|
||||
(packet.authorityRecords[2] as MdnsInetAddressRecord).inet6Address)
|
||||
assertEquals(InetAddresses.parseNumericAddress("2001:db8::789"),
|
||||
(packet.authorityRecords[3] as MdnsInetAddressRecord).inet6Address)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user