dalyk | 1fcb739 | 2018-03-05 12:42:22 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018, The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.server.connectivity; |
| 18 | |
| 19 | import static android.net.ConnectivityManager.PRIVATE_DNS_MODE_OFF; |
dalyk | 1fcb739 | 2018-03-05 12:42:22 -0500 | [diff] [blame] | 20 | import static android.net.ConnectivityManager.PRIVATE_DNS_MODE_PROVIDER_HOSTNAME; |
Bruce Chen | 36ad02a | 2020-04-17 04:28:04 +0000 | [diff] [blame] | 21 | import static android.net.NetworkCapabilities.MAX_TRANSPORT; |
| 22 | import static android.net.NetworkCapabilities.MIN_TRANSPORT; |
chenbruce | 5d95562 | 2020-02-20 14:28:31 +0800 | [diff] [blame] | 23 | import static android.net.NetworkCapabilities.TRANSPORT_VPN; |
| 24 | import static android.net.NetworkCapabilities.TRANSPORT_WIFI; |
Erik Kline | 1984179 | 2018-05-16 16:41:57 +0900 | [diff] [blame] | 25 | import static android.provider.Settings.Global.PRIVATE_DNS_DEFAULT_MODE; |
| 26 | import static android.provider.Settings.Global.PRIVATE_DNS_MODE; |
| 27 | import static android.provider.Settings.Global.PRIVATE_DNS_SPECIFIER; |
dalyk | 1fcb739 | 2018-03-05 12:42:22 -0500 | [diff] [blame] | 28 | |
chenbruce | 5d95562 | 2020-02-20 14:28:31 +0800 | [diff] [blame] | 29 | import static com.android.testutils.MiscAssertsKt.assertContainsExactly; |
| 30 | import static com.android.testutils.MiscAssertsKt.assertContainsStringsExactly; |
| 31 | import static com.android.testutils.MiscAssertsKt.assertFieldCountEquals; |
| 32 | |
dalyk | 1fcb739 | 2018-03-05 12:42:22 -0500 | [diff] [blame] | 33 | import static org.junit.Assert.assertEquals; |
| 34 | import static org.junit.Assert.assertFalse; |
Bruce Chen | 36ad02a | 2020-04-17 04:28:04 +0000 | [diff] [blame] | 35 | import static org.junit.Assert.assertNotNull; |
dalyk | 1fcb739 | 2018-03-05 12:42:22 -0500 | [diff] [blame] | 36 | import static org.junit.Assert.assertNull; |
| 37 | import static org.junit.Assert.assertTrue; |
chenbruce | 5d95562 | 2020-02-20 14:28:31 +0800 | [diff] [blame] | 38 | import static org.mockito.Mockito.reset; |
| 39 | import static org.mockito.Mockito.times; |
| 40 | import static org.mockito.Mockito.verify; |
dalyk | 1fcb739 | 2018-03-05 12:42:22 -0500 | [diff] [blame] | 41 | import static org.mockito.Mockito.when; |
| 42 | |
chenbruce | 5d95562 | 2020-02-20 14:28:31 +0800 | [diff] [blame] | 43 | import android.annotation.NonNull; |
dalyk | 1fcb739 | 2018-03-05 12:42:22 -0500 | [diff] [blame] | 44 | import android.content.Context; |
Luke Huang | 6591477 | 2019-03-16 00:31:46 +0800 | [diff] [blame] | 45 | import android.net.IDnsResolver; |
Chalard Jean | f0f364f | 2018-04-11 18:58:52 +0900 | [diff] [blame] | 46 | import android.net.IpPrefix; |
| 47 | import android.net.LinkAddress; |
dalyk | 1fcb739 | 2018-03-05 12:42:22 -0500 | [diff] [blame] | 48 | import android.net.LinkProperties; |
| 49 | import android.net.Network; |
Bruce Chen | 36ad02a | 2020-04-17 04:28:04 +0000 | [diff] [blame] | 50 | import android.net.NetworkCapabilities; |
chenbruce | 5d95562 | 2020-02-20 14:28:31 +0800 | [diff] [blame] | 51 | import android.net.ResolverOptionsParcel; |
| 52 | import android.net.ResolverParamsParcel; |
Chalard Jean | f0f364f | 2018-04-11 18:58:52 +0900 | [diff] [blame] | 53 | import android.net.RouteInfo; |
Remi NGUYEN VAN | e67b0c3 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 54 | import android.net.shared.PrivateDnsConfig; |
dalyk | 1fcb739 | 2018-03-05 12:42:22 -0500 | [diff] [blame] | 55 | import android.provider.Settings; |
dalyk | 1fcb739 | 2018-03-05 12:42:22 -0500 | [diff] [blame] | 56 | import android.test.mock.MockContentResolver; |
Bruce Chen | 36ad02a | 2020-04-17 04:28:04 +0000 | [diff] [blame] | 57 | import android.util.SparseArray; |
dalyk | 1fcb739 | 2018-03-05 12:42:22 -0500 | [diff] [blame] | 58 | |
Brett Chabot | 1ae2aa6 | 2019-03-04 14:14:56 -0800 | [diff] [blame] | 59 | import androidx.test.filters.SmallTest; |
| 60 | import androidx.test.runner.AndroidJUnit4; |
| 61 | |
Bruce Chen | 36ad02a | 2020-04-17 04:28:04 +0000 | [diff] [blame] | 62 | import com.android.internal.util.MessageUtils; |
dalyk | 1fcb739 | 2018-03-05 12:42:22 -0500 | [diff] [blame] | 63 | import com.android.internal.util.test.FakeSettingsProvider; |
Remi NGUYEN VAN | e67b0c3 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 64 | |
| 65 | import org.junit.Before; |
| 66 | import org.junit.Test; |
| 67 | import org.junit.runner.RunWith; |
chenbruce | 5d95562 | 2020-02-20 14:28:31 +0800 | [diff] [blame] | 68 | import org.mockito.ArgumentCaptor; |
Remi NGUYEN VAN | e67b0c3 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 69 | import org.mockito.Mock; |
| 70 | import org.mockito.MockitoAnnotations; |
dalyk | 1fcb739 | 2018-03-05 12:42:22 -0500 | [diff] [blame] | 71 | |
| 72 | import java.net.InetAddress; |
Chalard Jean | f0f364f | 2018-04-11 18:58:52 +0900 | [diff] [blame] | 73 | import java.util.Arrays; |
dalyk | 1fcb739 | 2018-03-05 12:42:22 -0500 | [diff] [blame] | 74 | |
dalyk | 1fcb739 | 2018-03-05 12:42:22 -0500 | [diff] [blame] | 75 | /** |
| 76 | * Tests for {@link DnsManager}. |
| 77 | * |
| 78 | * Build, install and run with: |
| 79 | * runtest frameworks-net -c com.android.server.connectivity.DnsManagerTest |
| 80 | */ |
| 81 | @RunWith(AndroidJUnit4.class) |
| 82 | @SmallTest |
| 83 | public class DnsManagerTest { |
Chalard Jean | f0f364f | 2018-04-11 18:58:52 +0900 | [diff] [blame] | 84 | static final String TEST_IFACENAME = "test_wlan0"; |
dalyk | 1fcb739 | 2018-03-05 12:42:22 -0500 | [diff] [blame] | 85 | static final int TEST_NETID = 100; |
| 86 | static final int TEST_NETID_ALTERNATE = 101; |
| 87 | static final int TEST_NETID_UNTRACKED = 102; |
chenbruce | 5d95562 | 2020-02-20 14:28:31 +0800 | [diff] [blame] | 88 | static final int TEST_DEFAULT_SAMPLE_VALIDITY_SECONDS = 1800; |
| 89 | static final int TEST_DEFAULT_SUCCESS_THRESHOLD_PERCENT = 25; |
| 90 | static final int TEST_DEFAULT_MIN_SAMPLES = 8; |
| 91 | static final int TEST_DEFAULT_MAX_SAMPLES = 64; |
| 92 | static final int[] TEST_TRANSPORT_TYPES = {TRANSPORT_WIFI, TRANSPORT_VPN}; |
dalyk | 1fcb739 | 2018-03-05 12:42:22 -0500 | [diff] [blame] | 93 | |
| 94 | DnsManager mDnsManager; |
| 95 | MockContentResolver mContentResolver; |
| 96 | |
| 97 | @Mock Context mCtx; |
Luke Huang | 6591477 | 2019-03-16 00:31:46 +0800 | [diff] [blame] | 98 | @Mock IDnsResolver mMockDnsResolver; |
dalyk | 1fcb739 | 2018-03-05 12:42:22 -0500 | [diff] [blame] | 99 | @Mock MockableSystemProperties mSystemProperties; |
| 100 | |
chenbruce | 5d95562 | 2020-02-20 14:28:31 +0800 | [diff] [blame] | 101 | private void assertResolverOptionsEquals( |
| 102 | @NonNull ResolverOptionsParcel actual, |
| 103 | @NonNull ResolverOptionsParcel expected) { |
| 104 | assertEquals(actual.hosts, expected.hosts); |
| 105 | assertEquals(actual.tcMode, expected.tcMode); |
| 106 | assertFieldCountEquals(2, ResolverOptionsParcel.class); |
| 107 | } |
| 108 | |
| 109 | private void assertResolverParamsEquals(@NonNull ResolverParamsParcel actual, |
| 110 | @NonNull ResolverParamsParcel expected) { |
| 111 | assertEquals(actual.netId, expected.netId); |
| 112 | assertEquals(actual.sampleValiditySeconds, expected.sampleValiditySeconds); |
| 113 | assertEquals(actual.successThreshold, expected.successThreshold); |
| 114 | assertEquals(actual.minSamples, expected.minSamples); |
| 115 | assertEquals(actual.maxSamples, expected.maxSamples); |
| 116 | assertEquals(actual.baseTimeoutMsec, expected.baseTimeoutMsec); |
| 117 | assertEquals(actual.retryCount, expected.retryCount); |
| 118 | assertContainsStringsExactly(actual.servers, expected.servers); |
| 119 | assertContainsStringsExactly(actual.domains, expected.domains); |
| 120 | assertEquals(actual.tlsName, expected.tlsName); |
| 121 | assertContainsStringsExactly(actual.tlsServers, expected.tlsServers); |
| 122 | assertContainsStringsExactly(actual.tlsFingerprints, expected.tlsFingerprints); |
| 123 | assertEquals(actual.caCertificate, expected.caCertificate); |
| 124 | assertEquals(actual.tlsConnectTimeoutMs, expected.tlsConnectTimeoutMs); |
| 125 | assertResolverOptionsEquals(actual.resolverOptions, expected.resolverOptions); |
| 126 | assertContainsExactly(actual.transportTypes, expected.transportTypes); |
| 127 | assertFieldCountEquals(16, ResolverParamsParcel.class); |
| 128 | } |
| 129 | |
dalyk | 1fcb739 | 2018-03-05 12:42:22 -0500 | [diff] [blame] | 130 | @Before |
| 131 | public void setUp() throws Exception { |
| 132 | MockitoAnnotations.initMocks(this); |
| 133 | mContentResolver = new MockContentResolver(); |
| 134 | mContentResolver.addProvider(Settings.AUTHORITY, |
| 135 | new FakeSettingsProvider()); |
| 136 | when(mCtx.getContentResolver()).thenReturn(mContentResolver); |
Luke Huang | 6591477 | 2019-03-16 00:31:46 +0800 | [diff] [blame] | 137 | mDnsManager = new DnsManager(mCtx, mMockDnsResolver, mSystemProperties); |
dalyk | 1fcb739 | 2018-03-05 12:42:22 -0500 | [diff] [blame] | 138 | |
| 139 | // Clear the private DNS settings |
Erik Kline | 1984179 | 2018-05-16 16:41:57 +0900 | [diff] [blame] | 140 | Settings.Global.putString(mContentResolver, PRIVATE_DNS_DEFAULT_MODE, ""); |
| 141 | Settings.Global.putString(mContentResolver, PRIVATE_DNS_MODE, ""); |
| 142 | Settings.Global.putString(mContentResolver, PRIVATE_DNS_SPECIFIER, ""); |
dalyk | 1fcb739 | 2018-03-05 12:42:22 -0500 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | @Test |
| 146 | public void testTrackedValidationUpdates() throws Exception { |
| 147 | mDnsManager.updatePrivateDns(new Network(TEST_NETID), |
| 148 | mDnsManager.getPrivateDnsConfig()); |
| 149 | mDnsManager.updatePrivateDns(new Network(TEST_NETID_ALTERNATE), |
| 150 | mDnsManager.getPrivateDnsConfig()); |
| 151 | LinkProperties lp = new LinkProperties(); |
Chalard Jean | f0f364f | 2018-04-11 18:58:52 +0900 | [diff] [blame] | 152 | lp.setInterfaceName(TEST_IFACENAME); |
dalyk | 1fcb739 | 2018-03-05 12:42:22 -0500 | [diff] [blame] | 153 | lp.addDnsServer(InetAddress.getByName("3.3.3.3")); |
| 154 | lp.addDnsServer(InetAddress.getByName("4.4.4.4")); |
| 155 | |
| 156 | // Send a validation event that is tracked on the alternate netId |
chenbruce | 5d95562 | 2020-02-20 14:28:31 +0800 | [diff] [blame] | 157 | mDnsManager.updateTransportsForNetwork(TEST_NETID, TEST_TRANSPORT_TYPES); |
| 158 | mDnsManager.noteDnsServersForNetwork(TEST_NETID, lp); |
| 159 | mDnsManager.setDefaultDnsSystemProperties(lp.getDnsServers()); |
| 160 | mDnsManager.flushVmDnsCache(); |
| 161 | mDnsManager.updateTransportsForNetwork(TEST_NETID_ALTERNATE, TEST_TRANSPORT_TYPES); |
| 162 | mDnsManager.noteDnsServersForNetwork(TEST_NETID_ALTERNATE, lp); |
| 163 | mDnsManager.flushVmDnsCache(); |
dalyk | 1fcb739 | 2018-03-05 12:42:22 -0500 | [diff] [blame] | 164 | mDnsManager.updatePrivateDnsValidation( |
| 165 | new DnsManager.PrivateDnsValidationUpdate(TEST_NETID_ALTERNATE, |
| 166 | InetAddress.parseNumericAddress("4.4.4.4"), "", true)); |
| 167 | LinkProperties fixedLp = new LinkProperties(lp); |
| 168 | mDnsManager.updatePrivateDnsStatus(TEST_NETID, fixedLp); |
| 169 | assertFalse(fixedLp.isPrivateDnsActive()); |
| 170 | assertNull(fixedLp.getPrivateDnsServerName()); |
| 171 | fixedLp = new LinkProperties(lp); |
| 172 | mDnsManager.updatePrivateDnsStatus(TEST_NETID_ALTERNATE, fixedLp); |
| 173 | assertTrue(fixedLp.isPrivateDnsActive()); |
| 174 | assertNull(fixedLp.getPrivateDnsServerName()); |
Chalard Jean | f0f364f | 2018-04-11 18:58:52 +0900 | [diff] [blame] | 175 | assertEquals(Arrays.asList(InetAddress.getByName("4.4.4.4")), |
| 176 | fixedLp.getValidatedPrivateDnsServers()); |
dalyk | 1fcb739 | 2018-03-05 12:42:22 -0500 | [diff] [blame] | 177 | |
Chalard Jean | f0f364f | 2018-04-11 18:58:52 +0900 | [diff] [blame] | 178 | // Set up addresses for strict mode and switch to it. |
| 179 | lp.addLinkAddress(new LinkAddress("192.0.2.4/24")); |
| 180 | lp.addRoute(new RouteInfo((IpPrefix) null, InetAddress.getByName("192.0.2.4"), |
| 181 | TEST_IFACENAME)); |
| 182 | lp.addLinkAddress(new LinkAddress("2001:db8:1::1/64")); |
| 183 | lp.addRoute(new RouteInfo((IpPrefix) null, InetAddress.getByName("2001:db8:1::1"), |
| 184 | TEST_IFACENAME)); |
| 185 | |
dalyk | 1fcb739 | 2018-03-05 12:42:22 -0500 | [diff] [blame] | 186 | Settings.Global.putString(mContentResolver, |
Erik Kline | 1984179 | 2018-05-16 16:41:57 +0900 | [diff] [blame] | 187 | PRIVATE_DNS_MODE, PRIVATE_DNS_MODE_PROVIDER_HOSTNAME); |
| 188 | Settings.Global.putString(mContentResolver, PRIVATE_DNS_SPECIFIER, "strictmode.com"); |
dalyk | 1fcb739 | 2018-03-05 12:42:22 -0500 | [diff] [blame] | 189 | mDnsManager.updatePrivateDns(new Network(TEST_NETID), |
Remi NGUYEN VAN | e67b0c3 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 190 | new PrivateDnsConfig("strictmode.com", new InetAddress[] { |
Chalard Jean | f0f364f | 2018-04-11 18:58:52 +0900 | [diff] [blame] | 191 | InetAddress.parseNumericAddress("6.6.6.6"), |
| 192 | InetAddress.parseNumericAddress("2001:db8:66:66::1") |
| 193 | })); |
chenbruce | 5d95562 | 2020-02-20 14:28:31 +0800 | [diff] [blame] | 194 | mDnsManager.updateTransportsForNetwork(TEST_NETID, TEST_TRANSPORT_TYPES); |
| 195 | mDnsManager.noteDnsServersForNetwork(TEST_NETID, lp); |
| 196 | mDnsManager.setDefaultDnsSystemProperties(lp.getDnsServers()); |
| 197 | mDnsManager.flushVmDnsCache(); |
dalyk | 1fcb739 | 2018-03-05 12:42:22 -0500 | [diff] [blame] | 198 | fixedLp = new LinkProperties(lp); |
| 199 | mDnsManager.updatePrivateDnsStatus(TEST_NETID, fixedLp); |
| 200 | assertTrue(fixedLp.isPrivateDnsActive()); |
| 201 | assertEquals("strictmode.com", fixedLp.getPrivateDnsServerName()); |
Chalard Jean | f0f364f | 2018-04-11 18:58:52 +0900 | [diff] [blame] | 202 | // No validation events yet. |
| 203 | assertEquals(Arrays.asList(new InetAddress[0]), fixedLp.getValidatedPrivateDnsServers()); |
| 204 | // Validate one. |
| 205 | mDnsManager.updatePrivateDnsValidation( |
| 206 | new DnsManager.PrivateDnsValidationUpdate(TEST_NETID, |
| 207 | InetAddress.parseNumericAddress("6.6.6.6"), "strictmode.com", true)); |
dalyk | 1fcb739 | 2018-03-05 12:42:22 -0500 | [diff] [blame] | 208 | fixedLp = new LinkProperties(lp); |
Chalard Jean | f0f364f | 2018-04-11 18:58:52 +0900 | [diff] [blame] | 209 | mDnsManager.updatePrivateDnsStatus(TEST_NETID, fixedLp); |
| 210 | assertEquals(Arrays.asList(InetAddress.parseNumericAddress("6.6.6.6")), |
| 211 | fixedLp.getValidatedPrivateDnsServers()); |
| 212 | // Validate the 2nd one. |
| 213 | mDnsManager.updatePrivateDnsValidation( |
| 214 | new DnsManager.PrivateDnsValidationUpdate(TEST_NETID, |
| 215 | InetAddress.parseNumericAddress("2001:db8:66:66::1"), "strictmode.com", true)); |
| 216 | fixedLp = new LinkProperties(lp); |
| 217 | mDnsManager.updatePrivateDnsStatus(TEST_NETID, fixedLp); |
| 218 | assertEquals(Arrays.asList( |
| 219 | InetAddress.parseNumericAddress("2001:db8:66:66::1"), |
| 220 | InetAddress.parseNumericAddress("6.6.6.6")), |
| 221 | fixedLp.getValidatedPrivateDnsServers()); |
dalyk | 1fcb739 | 2018-03-05 12:42:22 -0500 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | @Test |
| 225 | public void testIgnoreUntrackedValidationUpdates() throws Exception { |
| 226 | // The PrivateDnsConfig map is empty, so no validation events will |
| 227 | // be tracked. |
| 228 | LinkProperties lp = new LinkProperties(); |
| 229 | lp.addDnsServer(InetAddress.getByName("3.3.3.3")); |
chenbruce | 5d95562 | 2020-02-20 14:28:31 +0800 | [diff] [blame] | 230 | mDnsManager.updateTransportsForNetwork(TEST_NETID, TEST_TRANSPORT_TYPES); |
| 231 | mDnsManager.noteDnsServersForNetwork(TEST_NETID, lp); |
| 232 | mDnsManager.setDefaultDnsSystemProperties(lp.getDnsServers()); |
| 233 | mDnsManager.flushVmDnsCache(); |
dalyk | 1fcb739 | 2018-03-05 12:42:22 -0500 | [diff] [blame] | 234 | mDnsManager.updatePrivateDnsValidation( |
| 235 | new DnsManager.PrivateDnsValidationUpdate(TEST_NETID, |
| 236 | InetAddress.parseNumericAddress("3.3.3.3"), "", true)); |
| 237 | mDnsManager.updatePrivateDnsStatus(TEST_NETID, lp); |
| 238 | assertFalse(lp.isPrivateDnsActive()); |
| 239 | assertNull(lp.getPrivateDnsServerName()); |
| 240 | |
| 241 | // Validation event has untracked netId |
| 242 | mDnsManager.updatePrivateDns(new Network(TEST_NETID), |
| 243 | mDnsManager.getPrivateDnsConfig()); |
chenbruce | 5d95562 | 2020-02-20 14:28:31 +0800 | [diff] [blame] | 244 | mDnsManager.updateTransportsForNetwork(TEST_NETID, TEST_TRANSPORT_TYPES); |
| 245 | mDnsManager.noteDnsServersForNetwork(TEST_NETID, lp); |
| 246 | mDnsManager.setDefaultDnsSystemProperties(lp.getDnsServers()); |
| 247 | mDnsManager.flushVmDnsCache(); |
dalyk | 1fcb739 | 2018-03-05 12:42:22 -0500 | [diff] [blame] | 248 | mDnsManager.updatePrivateDnsValidation( |
| 249 | new DnsManager.PrivateDnsValidationUpdate(TEST_NETID_UNTRACKED, |
| 250 | InetAddress.parseNumericAddress("3.3.3.3"), "", true)); |
| 251 | mDnsManager.updatePrivateDnsStatus(TEST_NETID, lp); |
| 252 | assertFalse(lp.isPrivateDnsActive()); |
| 253 | assertNull(lp.getPrivateDnsServerName()); |
| 254 | |
| 255 | // Validation event has untracked ipAddress |
| 256 | mDnsManager.updatePrivateDnsValidation( |
| 257 | new DnsManager.PrivateDnsValidationUpdate(TEST_NETID, |
| 258 | InetAddress.parseNumericAddress("4.4.4.4"), "", true)); |
| 259 | mDnsManager.updatePrivateDnsStatus(TEST_NETID, lp); |
| 260 | assertFalse(lp.isPrivateDnsActive()); |
| 261 | assertNull(lp.getPrivateDnsServerName()); |
| 262 | |
| 263 | // Validation event has untracked hostname |
| 264 | mDnsManager.updatePrivateDnsValidation( |
| 265 | new DnsManager.PrivateDnsValidationUpdate(TEST_NETID, |
| 266 | InetAddress.parseNumericAddress("3.3.3.3"), "hostname", |
| 267 | true)); |
| 268 | mDnsManager.updatePrivateDnsStatus(TEST_NETID, lp); |
| 269 | assertFalse(lp.isPrivateDnsActive()); |
| 270 | assertNull(lp.getPrivateDnsServerName()); |
| 271 | |
| 272 | // Validation event failed |
| 273 | mDnsManager.updatePrivateDnsValidation( |
| 274 | new DnsManager.PrivateDnsValidationUpdate(TEST_NETID, |
| 275 | InetAddress.parseNumericAddress("3.3.3.3"), "", false)); |
| 276 | mDnsManager.updatePrivateDnsStatus(TEST_NETID, lp); |
| 277 | assertFalse(lp.isPrivateDnsActive()); |
| 278 | assertNull(lp.getPrivateDnsServerName()); |
| 279 | |
| 280 | // Network removed |
| 281 | mDnsManager.removeNetwork(new Network(TEST_NETID)); |
| 282 | mDnsManager.updatePrivateDnsValidation( |
| 283 | new DnsManager.PrivateDnsValidationUpdate(TEST_NETID, |
| 284 | InetAddress.parseNumericAddress("3.3.3.3"), "", true)); |
| 285 | mDnsManager.updatePrivateDnsStatus(TEST_NETID, lp); |
| 286 | assertFalse(lp.isPrivateDnsActive()); |
| 287 | assertNull(lp.getPrivateDnsServerName()); |
| 288 | |
| 289 | // Turn private DNS mode off |
Erik Kline | 1984179 | 2018-05-16 16:41:57 +0900 | [diff] [blame] | 290 | Settings.Global.putString(mContentResolver, PRIVATE_DNS_MODE, PRIVATE_DNS_MODE_OFF); |
dalyk | 1fcb739 | 2018-03-05 12:42:22 -0500 | [diff] [blame] | 291 | mDnsManager.updatePrivateDns(new Network(TEST_NETID), |
| 292 | mDnsManager.getPrivateDnsConfig()); |
chenbruce | 5d95562 | 2020-02-20 14:28:31 +0800 | [diff] [blame] | 293 | mDnsManager.updateTransportsForNetwork(TEST_NETID, TEST_TRANSPORT_TYPES); |
| 294 | mDnsManager.noteDnsServersForNetwork(TEST_NETID, lp); |
| 295 | mDnsManager.setDefaultDnsSystemProperties(lp.getDnsServers()); |
| 296 | mDnsManager.flushVmDnsCache(); |
dalyk | 1fcb739 | 2018-03-05 12:42:22 -0500 | [diff] [blame] | 297 | mDnsManager.updatePrivateDnsValidation( |
| 298 | new DnsManager.PrivateDnsValidationUpdate(TEST_NETID, |
| 299 | InetAddress.parseNumericAddress("3.3.3.3"), "", true)); |
| 300 | mDnsManager.updatePrivateDnsStatus(TEST_NETID, lp); |
| 301 | assertFalse(lp.isPrivateDnsActive()); |
| 302 | assertNull(lp.getPrivateDnsServerName()); |
| 303 | } |
Erik Kline | 1984179 | 2018-05-16 16:41:57 +0900 | [diff] [blame] | 304 | |
| 305 | @Test |
| 306 | public void testOverrideDefaultMode() throws Exception { |
| 307 | // Hard-coded default is opportunistic mode. |
| 308 | final PrivateDnsConfig cfgAuto = DnsManager.getPrivateDnsConfig(mContentResolver); |
| 309 | assertTrue(cfgAuto.useTls); |
| 310 | assertEquals("", cfgAuto.hostname); |
| 311 | assertEquals(new InetAddress[0], cfgAuto.ips); |
| 312 | |
| 313 | // Pretend a gservices push sets the default to "off". |
| 314 | Settings.Global.putString(mContentResolver, PRIVATE_DNS_DEFAULT_MODE, "off"); |
| 315 | final PrivateDnsConfig cfgOff = DnsManager.getPrivateDnsConfig(mContentResolver); |
| 316 | assertFalse(cfgOff.useTls); |
| 317 | assertEquals("", cfgOff.hostname); |
| 318 | assertEquals(new InetAddress[0], cfgOff.ips); |
| 319 | |
| 320 | // Strict mode still works. |
| 321 | Settings.Global.putString( |
| 322 | mContentResolver, PRIVATE_DNS_MODE, PRIVATE_DNS_MODE_PROVIDER_HOSTNAME); |
| 323 | Settings.Global.putString(mContentResolver, PRIVATE_DNS_SPECIFIER, "strictmode.com"); |
| 324 | final PrivateDnsConfig cfgStrict = DnsManager.getPrivateDnsConfig(mContentResolver); |
| 325 | assertTrue(cfgStrict.useTls); |
| 326 | assertEquals("strictmode.com", cfgStrict.hostname); |
| 327 | assertEquals(new InetAddress[0], cfgStrict.ips); |
| 328 | } |
chenbruce | 5d95562 | 2020-02-20 14:28:31 +0800 | [diff] [blame] | 329 | |
| 330 | @Test |
| 331 | public void testSendDnsConfiguration() throws Exception { |
| 332 | reset(mMockDnsResolver); |
| 333 | mDnsManager.updatePrivateDns(new Network(TEST_NETID), |
| 334 | mDnsManager.getPrivateDnsConfig()); |
| 335 | final LinkProperties lp = new LinkProperties(); |
| 336 | lp.setInterfaceName(TEST_IFACENAME); |
| 337 | lp.addDnsServer(InetAddress.getByName("3.3.3.3")); |
| 338 | lp.addDnsServer(InetAddress.getByName("4.4.4.4")); |
| 339 | mDnsManager.updateTransportsForNetwork(TEST_NETID, TEST_TRANSPORT_TYPES); |
| 340 | mDnsManager.noteDnsServersForNetwork(TEST_NETID, lp); |
| 341 | mDnsManager.setDefaultDnsSystemProperties(lp.getDnsServers()); |
| 342 | mDnsManager.flushVmDnsCache(); |
| 343 | |
| 344 | final ArgumentCaptor<ResolverParamsParcel> resolverParamsParcelCaptor = |
| 345 | ArgumentCaptor.forClass(ResolverParamsParcel.class); |
| 346 | verify(mMockDnsResolver, times(1)).setResolverConfiguration( |
| 347 | resolverParamsParcelCaptor.capture()); |
| 348 | final ResolverParamsParcel actualParams = resolverParamsParcelCaptor.getValue(); |
| 349 | final ResolverParamsParcel expectedParams = new ResolverParamsParcel(); |
| 350 | expectedParams.netId = TEST_NETID; |
| 351 | expectedParams.sampleValiditySeconds = TEST_DEFAULT_SAMPLE_VALIDITY_SECONDS; |
| 352 | expectedParams.successThreshold = TEST_DEFAULT_SUCCESS_THRESHOLD_PERCENT; |
| 353 | expectedParams.minSamples = TEST_DEFAULT_MIN_SAMPLES; |
| 354 | expectedParams.maxSamples = TEST_DEFAULT_MAX_SAMPLES; |
| 355 | expectedParams.servers = new String[]{"3.3.3.3", "4.4.4.4"}; |
| 356 | expectedParams.domains = new String[]{}; |
| 357 | expectedParams.tlsName = ""; |
| 358 | expectedParams.tlsServers = new String[]{"3.3.3.3", "4.4.4.4"}; |
| 359 | expectedParams.transportTypes = TEST_TRANSPORT_TYPES; |
| 360 | expectedParams.resolverOptions = new ResolverOptionsParcel(); |
| 361 | assertResolverParamsEquals(actualParams, expectedParams); |
| 362 | } |
Bruce Chen | 36ad02a | 2020-04-17 04:28:04 +0000 | [diff] [blame] | 363 | |
| 364 | @Test |
| 365 | public void testTransportTypesEqual() throws Exception { |
| 366 | SparseArray<String> ncTransTypes = MessageUtils.findMessageNames( |
| 367 | new Class[] { NetworkCapabilities.class }, new String[]{ "TRANSPORT_" }); |
| 368 | SparseArray<String> dnsTransTypes = MessageUtils.findMessageNames( |
| 369 | new Class[] { IDnsResolver.class }, new String[]{ "TRANSPORT_" }); |
| 370 | assertEquals(0, MIN_TRANSPORT); |
| 371 | assertEquals(MAX_TRANSPORT + 1, ncTransTypes.size()); |
| 372 | // TRANSPORT_UNKNOWN in IDnsResolver is defined to -1 and only for resolver. |
| 373 | assertEquals("TRANSPORT_UNKNOWN", dnsTransTypes.get(-1)); |
| 374 | assertEquals(ncTransTypes.size(), dnsTransTypes.size() - 1); |
| 375 | for (int i = MIN_TRANSPORT; i < MAX_TRANSPORT; i++) { |
| 376 | String name = ncTransTypes.get(i, null); |
| 377 | assertNotNull("Could not find NetworkCapabilies.TRANSPORT_* constant equal to " |
| 378 | + i, name); |
| 379 | assertEquals(name, dnsTransTypes.get(i)); |
| 380 | } |
| 381 | } |
dalyk | 1fcb739 | 2018-03-05 12:42:22 -0500 | [diff] [blame] | 382 | } |