From 7cde306fa38ba72b4f646c64a3ce5cd137736822 Mon Sep 17 00:00:00 2001 From: Lorenzo Colitti Date: Fri, 23 Aug 2024 17:49:56 +0900 Subject: [PATCH] Inform DNS resolver whether DDR is enabled. This is needed to cleanly flag on/off DDR and to ensure that when it is flagged on, DnsResolver ignores the hardcoded list of DoH providers and only uses DDR. Bug: 240259333 Test: updated existing tests Change-Id: I84c103a5a4d18294b6a4cc3301d0037d1a6673a4 --- .../server/connectivity/DnsManager.java | 19 ++---- .../server/connectivity/DnsManagerTest.java | 66 +++++++++++++++---- 2 files changed, 58 insertions(+), 27 deletions(-) diff --git a/service/src/com/android/server/connectivity/DnsManager.java b/service/src/com/android/server/connectivity/DnsManager.java index b95e3b1e6c..c940eecd46 100644 --- a/service/src/com/android/server/connectivity/DnsManager.java +++ b/service/src/com/android/server/connectivity/DnsManager.java @@ -29,6 +29,7 @@ import static android.net.resolv.aidl.IDnsResolverUnsolicitedEventListener.VALID import static android.net.resolv.aidl.IDnsResolverUnsolicitedEventListener.VALIDATION_RESULT_SUCCESS; import android.annotation.NonNull; +import android.annotation.Nullable; import android.content.ContentResolver; import android.content.Context; import android.content.Intent; @@ -404,22 +405,11 @@ public class DnsManager { mPrivateDnsValidationMap.remove(netId); } - Log.d(TAG, String.format("sendDnsConfigurationForNetwork(%d, %s, %s, %d, %d, %d, %d, " - + "%d, %d, %s, %s, %s, %b, %s, %s, %s, %s, %d)", paramsParcel.netId, - Arrays.toString(paramsParcel.servers), Arrays.toString(paramsParcel.domains), - paramsParcel.sampleValiditySeconds, paramsParcel.successThreshold, - paramsParcel.minSamples, paramsParcel.maxSamples, paramsParcel.baseTimeoutMsec, - paramsParcel.retryCount, paramsParcel.tlsName, - Arrays.toString(paramsParcel.tlsServers), - Arrays.toString(paramsParcel.transportTypes), paramsParcel.meteredNetwork, - Arrays.toString(paramsParcel.interfaceNames), - paramsParcel.dohParams.name, Arrays.toString(paramsParcel.dohParams.ips), - paramsParcel.dohParams.dohpath, paramsParcel.dohParams.port)); + Log.d(TAG, "sendDnsConfigurationForNetwork(" + paramsParcel + ")"); try { mDnsResolver.setResolverConfiguration(paramsParcel); } catch (RemoteException | ServiceSpecificException e) { Log.e(TAG, "Error setting DNS configuration: " + e); - return; } } @@ -509,9 +499,12 @@ public class DnsManager { return out; } - @NonNull + @Nullable private DohParamsParcel makeDohParamsParcel(@NonNull PrivateDnsConfig cfg, @NonNull LinkProperties lp) { + if (!cfg.ddrEnabled) { + return null; + } if (cfg.mode == PRIVATE_DNS_MODE_OFF) { return new DohParamsParcel.Builder().build(); } diff --git a/tests/unit/java/com/android/server/connectivity/DnsManagerTest.java b/tests/unit/java/com/android/server/connectivity/DnsManagerTest.java index b47b97d605..fb3004a3a1 100644 --- a/tests/unit/java/com/android/server/connectivity/DnsManagerTest.java +++ b/tests/unit/java/com/android/server/connectivity/DnsManagerTest.java @@ -325,18 +325,9 @@ public class DnsManagerTest { assertEquals(new InetAddress[0], cfgStrict.ips); } - @Test - public void testSendDnsConfiguration() throws Exception { + private void doTestSendDnsConfiguration(PrivateDnsConfig cfg, DohParamsParcel expectedDohParams) + throws Exception { reset(mMockDnsResolver); - final PrivateDnsConfig cfg = new PrivateDnsConfig( - PRIVATE_DNS_MODE_OPPORTUNISTIC /* mode */, - null /* hostname */, - null /* ips */, - "doh.com" /* dohName */, - null /* dohIps */, - "/some-path{?dns}" /* dohPath */, - 5353 /* dohPort */); - mDnsManager.updatePrivateDns(new Network(TEST_NETID), cfg); final LinkProperties lp = new LinkProperties(); lp.setInterfaceName(TEST_IFACENAME); @@ -361,13 +352,60 @@ public class DnsManagerTest { expectedParams.transportTypes = TEST_TRANSPORT_TYPES; expectedParams.resolverOptions = null; expectedParams.meteredNetwork = true; - expectedParams.dohParams = new DohParamsParcel.Builder() + expectedParams.dohParams = expectedDohParams; + expectedParams.interfaceNames = new String[]{TEST_IFACENAME}; + verify(mMockDnsResolver, times(1)).setResolverConfiguration(eq(expectedParams)); + } + + @Test + public void testSendDnsConfiguration_ddrDisabled() throws Exception { + final PrivateDnsConfig cfg = new PrivateDnsConfig( + PRIVATE_DNS_MODE_OPPORTUNISTIC /* mode */, + null /* hostname */, + null /* ips */, + false /* ddrEnabled */, + null /* dohName */, + null /* dohIps */, + null /* dohPath */, + -1 /* dohPort */); + doTestSendDnsConfiguration(cfg, null /* expectedDohParams */); + } + + @Test + public void testSendDnsConfiguration_ddrEnabledEmpty() throws Exception { + final PrivateDnsConfig cfg = new PrivateDnsConfig( + PRIVATE_DNS_MODE_OPPORTUNISTIC /* mode */, + null /* hostname */, + null /* ips */, + true /* ddrEnabled */, + null /* dohName */, + null /* dohIps */, + null /* dohPath */, + -1 /* dohPort */); + + final DohParamsParcel params = new DohParamsParcel.Builder().build(); + doTestSendDnsConfiguration(cfg, params); + } + + @Test + public void testSendDnsConfiguration_ddrEnabled() throws Exception { + final PrivateDnsConfig cfg = new PrivateDnsConfig( + PRIVATE_DNS_MODE_OPPORTUNISTIC /* mode */, + null /* hostname */, + null /* ips */, + true /* ddrEnabled */, + "doh.com" /* dohName */, + null /* dohIps */, + "/some-path{?dns}" /* dohPath */, + 5353 /* dohPort */); + + final DohParamsParcel params = new DohParamsParcel.Builder() .setName("doh.com") .setDohpath("/some-path{?dns}") .setPort(5353) .build(); - expectedParams.interfaceNames = new String[]{TEST_IFACENAME}; - verify(mMockDnsResolver, times(1)).setResolverConfiguration(eq(expectedParams)); + + doTestSendDnsConfiguration(cfg, params); } @Test