Merge changes Ia677f31a,I8d8f5752,Idc347bce into main

* changes:
  Add abstract getValue() for class SvcParam
  Use DnsPacket.toString() for DnsSvcbPacket
  Test: A minor fix for testDnsSvcbRecord_svcParamMandatory
This commit is contained in:
Mike Yu 2023-11-14 05:34:35 +00:00 • committed by Gerrit Code Review
commit 68219d1031
3 changed files with 36 additions and 23 deletions

View file

@ -21,7 +21,6 @@ import static android.net.DnsResolver.TYPE_AAAA;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;
import java.net.InetAddress;
@ -29,7 +28,6 @@ import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.StringJoiner;
/**
* A class for a DNS SVCB response packet.
@ -159,16 +157,6 @@ public class DnsSvcbPacket extends DnsPacket {
return out;
}
@Override
public String toString() {
final StringJoiner out = new StringJoiner(" ");
out.add("QUERY: [" + TextUtils.join(", ", mRecords[QDSECTION]) + "]");
out.add("ANSWER: [" + TextUtils.join(", ", mRecords[ANSECTION]) + "]");
out.add("AUTHORITY: [" + TextUtils.join(", ", mRecords[NSSECTION]) + "]");
out.add("ADDITIONAL: [" + TextUtils.join(", ", mRecords[ARSECTION]) + "]");
return out.toString();
}
/**
* Creates a DnsSvcbPacket object from the given wire-format DNS answer.
*/

View file

@ -230,7 +230,7 @@ public final class DnsSvcbRecord extends DnsPacket.DnsRecord {
/**
* The base class for all SvcParam.
*/
private abstract static class SvcParam {
private abstract static class SvcParam<T> {
private final int mKey;
SvcParam(int key) {
@ -240,9 +240,11 @@ public final class DnsSvcbRecord extends DnsPacket.DnsRecord {
int getKey() {
return mKey;
}
abstract T getValue();
}
private static class SvcParamMandatory extends SvcParam {
private static class SvcParamMandatory extends SvcParam<short[]> {
private final short[] mValue;
private SvcParamMandatory(@NonNull ByteBuffer buf) throws BufferUnderflowException,
@ -257,6 +259,12 @@ public final class DnsSvcbRecord extends DnsPacket.DnsRecord {
}
}
@Override
short[] getValue() {
/* Not yet implemented */
return null;
}
@Override
public String toString() {
final StringJoiner valueJoiner = new StringJoiner(",");
@ -267,7 +275,7 @@ public final class DnsSvcbRecord extends DnsPacket.DnsRecord {
}
}
private static class SvcParamAlpn extends SvcParam {
private static class SvcParamAlpn extends SvcParam<List<String>> {
private final List<String> mValue;
SvcParamAlpn(@NonNull ByteBuffer buf) throws BufferUnderflowException, ParseException {
@ -281,6 +289,7 @@ public final class DnsSvcbRecord extends DnsPacket.DnsRecord {
}
}
@Override
List<String> getValue() {
return Collections.unmodifiableList(mValue);
}
@ -291,7 +300,7 @@ public final class DnsSvcbRecord extends DnsPacket.DnsRecord {
}
}
private static class SvcParamNoDefaultAlpn extends SvcParam {
private static class SvcParamNoDefaultAlpn extends SvcParam<Void> {
SvcParamNoDefaultAlpn(@NonNull ByteBuffer buf) throws BufferUnderflowException,
ParseException {
super(KEY_NO_DEFAULT_ALPN);
@ -302,13 +311,18 @@ public final class DnsSvcbRecord extends DnsPacket.DnsRecord {
}
}
@Override
Void getValue() {
return null;
}
@Override
public String toString() {
return toKeyName(getKey());
}
}
private static class SvcParamPort extends SvcParam {
private static class SvcParamPort extends SvcParam<Integer> {
private final int mValue;
SvcParamPort(@NonNull ByteBuffer buf) throws BufferUnderflowException, ParseException {
@ -321,7 +335,8 @@ public final class DnsSvcbRecord extends DnsPacket.DnsRecord {
mValue = Short.toUnsignedInt(buf.getShort());
}
int getValue() {
@Override
Integer getValue() {
return mValue;
}
@ -331,7 +346,7 @@ public final class DnsSvcbRecord extends DnsPacket.DnsRecord {
}
}
private static class SvcParamIpHint extends SvcParam {
private static class SvcParamIpHint extends SvcParam<List<InetAddress>> {
private final List<InetAddress> mValue;
private SvcParamIpHint(int key, @NonNull ByteBuffer buf, int addrLen) throws
@ -346,6 +361,7 @@ public final class DnsSvcbRecord extends DnsPacket.DnsRecord {
}
}
@Override
List<InetAddress> getValue() {
return Collections.unmodifiableList(mValue);
}
@ -378,7 +394,7 @@ public final class DnsSvcbRecord extends DnsPacket.DnsRecord {
}
}
private static class SvcParamDohPath extends SvcParam {
private static class SvcParamDohPath extends SvcParam<String> {
private final String mValue;
SvcParamDohPath(@NonNull ByteBuffer buf) throws BufferUnderflowException, ParseException {
@ -390,6 +406,7 @@ public final class DnsSvcbRecord extends DnsPacket.DnsRecord {
mValue = new String(value, StandardCharsets.UTF_8);
}
@Override
String getValue() {
return mValue;
}
@ -401,7 +418,7 @@ public final class DnsSvcbRecord extends DnsPacket.DnsRecord {
}
// For other unrecognized and unimplemented SvcParams, they are stored as SvcParamGeneric.
private static class SvcParamGeneric extends SvcParam {
private static class SvcParamGeneric extends SvcParam<byte[]> {
private final byte[] mValue;
SvcParamGeneric(int key, @NonNull ByteBuffer buf) throws BufferUnderflowException,
@ -413,6 +430,12 @@ public final class DnsSvcbRecord extends DnsPacket.DnsRecord {
buf.get(mValue);
}
@Override
byte[] getValue() {
/* Not yet implemented */
return null;
}
@Override
public String toString() {
final StringBuilder out = new StringBuilder();

View file

@ -207,7 +207,7 @@ public class DnsSvcbPacketTest {
os.write(shortToByteArray((short) mRdataLen));
} else {
final byte[] targetNameLabels =
DnsPacketUtils.DnsRecordParser.domainNameToLabels(mTargetName);
DnsPacketUtils.DnsRecordParser.domainNameToLabels(mTargetName);
mRdataLen += (Short.BYTES + targetNameLabels.length);
os.write(shortToByteArray((short) mRdataLen));
os.write(shortToByteArray(mSvcPriority));
@ -251,7 +251,9 @@ public class DnsSvcbPacketTest {
// Check the content returned from toString() for now because the getter function for
// this SvcParam hasn't been implemented.
// TODO(b/240259333): Consider adding DnsSvcbRecord.isMandatory(String alpn) when needed.
assertTrue(record.toString().contains("mandatory=ipv4hint,alpn,key333"));
assertTrue(record.toString().contains("ipv4hint"));
assertTrue(record.toString().contains("alpn"));
assertTrue(record.toString().contains("key333"));
}
@Test