Factor out adding records to a generic
- Make a method addOrReplaceRecord() that can support point,
inet4Address and inet6Address records adding.
- Also fix some leftover comments in aosp/2431933
Bug: 268586836
Test: atest FrameworksNetTests
Change-Id: I01462a967a04e88f14dcbb92ca207deb4268612c
diff --git a/service-t/src/com/android/server/connectivity/mdns/MdnsResponse.java b/service-t/src/com/android/server/connectivity/mdns/MdnsResponse.java
index be2555b..e0100f6 100644
--- a/service-t/src/com/android/server/connectivity/mdns/MdnsResponse.java
+++ b/service-t/src/com/android/server/connectivity/mdns/MdnsResponse.java
@@ -81,6 +81,21 @@
return a == null || a.getTtl() == b.getTtl();
}
+ private <T extends MdnsRecord> boolean addOrReplaceRecord(@NonNull T record,
+ @NonNull List<T> recordsList) {
+ final int existing = recordsList.indexOf(record);
+ if (existing >= 0) {
+ if (recordsAreSame(record, recordsList.get(existing))) {
+ return false;
+ }
+ final MdnsRecord existedRecord = recordsList.remove(existing);
+ records.remove(existedRecord);
+ }
+ recordsList.add(record);
+ records.add(record);
+ return true;
+ }
+
/**
* Adds a pointer record.
*
@@ -92,17 +107,7 @@
throw new IllegalArgumentException(
"Pointer records for different service names cannot be added");
}
- final int existing = pointerRecords.indexOf(pointerRecord);
- if (existing >= 0) {
- if (recordsAreSame(pointerRecord, pointerRecords.get(existing))) {
- return false;
- }
- final MdnsRecord record = pointerRecords.remove(existing);
- records.remove(record);
- }
- pointerRecords.add(pointerRecord);
- records.add(pointerRecord);
- return true;
+ return addOrReplaceRecord(pointerRecord, pointerRecords);
}
/** Gets the pointer records. */
@@ -207,17 +212,7 @@
/** Add the IPv4 address record. */
public synchronized boolean addInet4AddressRecord(
@NonNull MdnsInetAddressRecord newInet4AddressRecord) {
- final int existing = inet4AddressRecords.indexOf(newInet4AddressRecord);
- if (existing >= 0) {
- if (recordsAreSame(newInet4AddressRecord, inet4AddressRecords.get(existing))) {
- return false;
- }
- final MdnsRecord record = inet4AddressRecords.remove(existing);
- records.remove(record);
- }
- inet4AddressRecords.add(newInet4AddressRecord);
- records.add(newInet4AddressRecord);
- return true;
+ return addOrReplaceRecord(newInet4AddressRecord, inet4AddressRecords);
}
/** Gets the IPv4 address records. */
@@ -248,17 +243,7 @@
/** Sets the IPv6 address records. */
public synchronized boolean addInet6AddressRecord(
@NonNull MdnsInetAddressRecord newInet6AddressRecord) {
- final int existing = inet6AddressRecords.indexOf(newInet6AddressRecord);
- if (existing >= 0) {
- if (recordsAreSame(newInet6AddressRecord, inet6AddressRecords.get(existing))) {
- return false;
- }
- final MdnsRecord record = inet6AddressRecords.remove(existing);
- records.remove(record);
- }
- inet6AddressRecords.add(newInet6AddressRecord);
- records.add(newInet6AddressRecord);
- return true;
+ return addOrReplaceRecord(newInet6AddressRecord, inet6AddressRecords);
}
/**
diff --git a/service-t/src/com/android/server/connectivity/mdns/MdnsResponseDecoder.java b/service-t/src/com/android/server/connectivity/mdns/MdnsResponseDecoder.java
index 0151202..129db7e 100644
--- a/service-t/src/com/android/server/connectivity/mdns/MdnsResponseDecoder.java
+++ b/service-t/src/com/android/server/connectivity/mdns/MdnsResponseDecoder.java
@@ -220,9 +220,10 @@
// This bit, the cache-flush bit, tells neighboring hosts
// that this is not a shared record type. Instead of merging this new
// record additively into the cache in addition to any previous records with
- // the same name, rrtype, and rrclass, all old records with that name,
- // rrtype, and rrclass that were received more than one second ago are
- // declared invalid, and marked to expire from the cache in one second.
+ // the same name, rrtype, and rrclass.
+ // TODO: All old records with that name, rrtype, and rrclass that were
+ // received more than one second ago are declared invalid, and marked
+ // to expire from the cache in one second.
if (inetRecord.getCacheFlush()) {
response.clearInet4AddressRecords();
response.clearInet6AddressRecords();
@@ -236,9 +237,10 @@
// This bit, the cache-flush bit, tells neighboring hosts
// that this is not a shared record type. Instead of merging this new
// record additively into the cache in addition to any previous records with
- // the same name, rrtype, and rrclass, all old records with that name,
- // rrtype, and rrclass that were received more than one second ago are
- // declared invalid, and marked to expire from the cache in one second.
+ // the same name, rrtype, and rrclass.
+ // TODO: All old records with that name, rrtype, and rrclass that were
+ // received more than one second ago are declared invalid, and marked
+ // to expire from the cache in one second.
if (inetRecord.getCacheFlush()) {
response.clearInet4AddressRecords();
response.clearInet6AddressRecords();
diff --git a/tests/unit/java/com/android/server/connectivity/mdns/MdnsResponseDecoderTests.java b/tests/unit/java/com/android/server/connectivity/mdns/MdnsResponseDecoderTests.java
index a80c078..b0a1f18 100644
--- a/tests/unit/java/com/android/server/connectivity/mdns/MdnsResponseDecoderTests.java
+++ b/tests/unit/java/com/android/server/connectivity/mdns/MdnsResponseDecoderTests.java
@@ -156,16 +156,20 @@
+ "010001000000780004C0A8018A0000000000000000000000000000"
+ "000000");
- // MDNS record for name "testhost1" with an IPv4 address of 10.1.2.3
+ // MDNS record for name "testhost1" with an IPv4 address of 10.1.2.3. Also set cache flush bit
+ // for the records changed.
private static final byte[] DATAIN_IPV4_1 = HexDump.hexStringToByteArray(
"0974657374686f73743100000180010000007800040a010203");
- // MDNS record for name "testhost1" with an IPv4 address of 10.1.2.4
+ // MDNS record for name "testhost1" with an IPv4 address of 10.1.2.4. Also set cache flush bit
+ // for the records changed.
private static final byte[] DATAIN_IPV4_2 = HexDump.hexStringToByteArray(
"0974657374686f73743100000180010000007800040a010204");
- // MDNS record w/name "testhost1" & IPv6 address of aabb:ccdd:1122:3344:a0b0:c0d0:1020:3040
+ // MDNS record w/name "testhost1" & IPv6 address of aabb:ccdd:1122:3344:a0b0:c0d0:1020:3040.
+ // Also set cache flush bit for the records changed.
private static final byte[] DATAIN_IPV6_1 = HexDump.hexStringToByteArray(
"0974657374686f73743100001c8001000000780010aabbccdd11223344a0b0c0d010203040");
- // MDNS record w/name "testhost1" & IPv6 address of aabb:ccdd:1122:3344:a0b0:c0d0:1020:3030
+ // MDNS record w/name "testhost1" & IPv6 address of aabb:ccdd:1122:3344:a0b0:c0d0:1020:3030.
+ // Also set cache flush bit for the records changed.
private static final byte[] DATAIN_IPV6_2 = HexDump.hexStringToByteArray(
"0974657374686f73743100001c8001000000780010aabbccdd11223344a0b0c0d010203030");
// MDNS record w/name "test" & PTR to foo.bar.quxx
diff --git a/tests/unit/java/com/android/server/connectivity/mdns/MdnsServiceTypeClientTests.java b/tests/unit/java/com/android/server/connectivity/mdns/MdnsServiceTypeClientTests.java
index d9fa10f..bdd1f8d 100644
--- a/tests/unit/java/com/android/server/connectivity/mdns/MdnsServiceTypeClientTests.java
+++ b/tests/unit/java/com/android/server/connectivity/mdns/MdnsServiceTypeClientTests.java
@@ -449,8 +449,8 @@
verifyServiceInfo(serviceInfoCaptor.getAllValues().get(0),
"service-instance-1",
SERVICE_TYPE_LABELS,
- /* ipv4Address= */ List.of(),
- /* ipv6Address= */ List.of(),
+ /* ipv4Addresses= */ List.of(),
+ /* ipv6Addresses= */ List.of(),
/* port= */ 0,
/* subTypes= */ List.of(),
Collections.emptyMap(),