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; |
| 20 | import static android.net.ConnectivityManager.PRIVATE_DNS_MODE_OPPORTUNISTIC; |
| 21 | import static android.net.ConnectivityManager.PRIVATE_DNS_MODE_PROVIDER_HOSTNAME; |
| 22 | |
| 23 | import static org.junit.Assert.assertEquals; |
| 24 | import static org.junit.Assert.assertFalse; |
| 25 | import static org.junit.Assert.assertNull; |
| 26 | import static org.junit.Assert.assertTrue; |
| 27 | import static org.mockito.Mockito.when; |
| 28 | |
| 29 | import android.content.ContentResolver; |
| 30 | import android.content.Context; |
| 31 | import android.net.LinkProperties; |
| 32 | import android.net.Network; |
| 33 | import android.os.INetworkManagementService; |
| 34 | import android.provider.Settings; |
| 35 | import android.support.test.filters.SmallTest; |
| 36 | import android.support.test.runner.AndroidJUnit4; |
| 37 | import android.test.mock.MockContentResolver; |
| 38 | |
| 39 | import com.android.internal.util.test.FakeSettingsProvider; |
| 40 | import com.android.server.connectivity.MockableSystemProperties; |
| 41 | |
| 42 | import java.net.InetAddress; |
| 43 | |
| 44 | import org.junit.runner.RunWith; |
| 45 | import org.junit.Before; |
| 46 | import org.junit.Test; |
| 47 | import org.mockito.Mock; |
| 48 | import org.mockito.MockitoAnnotations; |
| 49 | |
| 50 | /** |
| 51 | * Tests for {@link DnsManager}. |
| 52 | * |
| 53 | * Build, install and run with: |
| 54 | * runtest frameworks-net -c com.android.server.connectivity.DnsManagerTest |
| 55 | */ |
| 56 | @RunWith(AndroidJUnit4.class) |
| 57 | @SmallTest |
| 58 | public class DnsManagerTest { |
| 59 | static final int TEST_NETID = 100; |
| 60 | static final int TEST_NETID_ALTERNATE = 101; |
| 61 | static final int TEST_NETID_UNTRACKED = 102; |
| 62 | final boolean IS_DEFAULT = true; |
| 63 | final boolean NOT_DEFAULT = false; |
| 64 | |
| 65 | DnsManager mDnsManager; |
| 66 | MockContentResolver mContentResolver; |
| 67 | |
| 68 | @Mock Context mCtx; |
| 69 | @Mock INetworkManagementService mNMService; |
| 70 | @Mock MockableSystemProperties mSystemProperties; |
| 71 | |
| 72 | @Before |
| 73 | public void setUp() throws Exception { |
| 74 | MockitoAnnotations.initMocks(this); |
| 75 | mContentResolver = new MockContentResolver(); |
| 76 | mContentResolver.addProvider(Settings.AUTHORITY, |
| 77 | new FakeSettingsProvider()); |
| 78 | when(mCtx.getContentResolver()).thenReturn(mContentResolver); |
| 79 | mDnsManager = new DnsManager(mCtx, mNMService, mSystemProperties); |
| 80 | |
| 81 | // Clear the private DNS settings |
| 82 | Settings.Global.putString(mContentResolver, |
| 83 | Settings.Global.PRIVATE_DNS_MODE, ""); |
| 84 | Settings.Global.putString(mContentResolver, |
| 85 | Settings.Global.PRIVATE_DNS_SPECIFIER, ""); |
| 86 | } |
| 87 | |
| 88 | @Test |
| 89 | public void testTrackedValidationUpdates() throws Exception { |
| 90 | mDnsManager.updatePrivateDns(new Network(TEST_NETID), |
| 91 | mDnsManager.getPrivateDnsConfig()); |
| 92 | mDnsManager.updatePrivateDns(new Network(TEST_NETID_ALTERNATE), |
| 93 | mDnsManager.getPrivateDnsConfig()); |
| 94 | LinkProperties lp = new LinkProperties(); |
| 95 | lp.addDnsServer(InetAddress.getByName("3.3.3.3")); |
| 96 | lp.addDnsServer(InetAddress.getByName("4.4.4.4")); |
| 97 | |
| 98 | // Send a validation event that is tracked on the alternate netId |
| 99 | mDnsManager.setDnsConfigurationForNetwork(TEST_NETID, lp, IS_DEFAULT); |
| 100 | mDnsManager.setDnsConfigurationForNetwork(TEST_NETID_ALTERNATE, lp, NOT_DEFAULT); |
| 101 | mDnsManager.updatePrivateDnsValidation( |
| 102 | new DnsManager.PrivateDnsValidationUpdate(TEST_NETID_ALTERNATE, |
| 103 | InetAddress.parseNumericAddress("4.4.4.4"), "", true)); |
| 104 | LinkProperties fixedLp = new LinkProperties(lp); |
| 105 | mDnsManager.updatePrivateDnsStatus(TEST_NETID, fixedLp); |
| 106 | assertFalse(fixedLp.isPrivateDnsActive()); |
| 107 | assertNull(fixedLp.getPrivateDnsServerName()); |
| 108 | fixedLp = new LinkProperties(lp); |
| 109 | mDnsManager.updatePrivateDnsStatus(TEST_NETID_ALTERNATE, fixedLp); |
| 110 | assertTrue(fixedLp.isPrivateDnsActive()); |
| 111 | assertNull(fixedLp.getPrivateDnsServerName()); |
| 112 | |
| 113 | // Switch to strict mode |
| 114 | Settings.Global.putString(mContentResolver, |
| 115 | Settings.Global.PRIVATE_DNS_MODE, |
| 116 | PRIVATE_DNS_MODE_PROVIDER_HOSTNAME); |
| 117 | Settings.Global.putString(mContentResolver, |
| 118 | Settings.Global.PRIVATE_DNS_SPECIFIER, "strictmode.com"); |
| 119 | mDnsManager.updatePrivateDns(new Network(TEST_NETID), |
| 120 | mDnsManager.getPrivateDnsConfig()); |
| 121 | mDnsManager.setDnsConfigurationForNetwork(TEST_NETID, lp, IS_DEFAULT); |
| 122 | fixedLp = new LinkProperties(lp); |
| 123 | mDnsManager.updatePrivateDnsStatus(TEST_NETID, fixedLp); |
| 124 | assertTrue(fixedLp.isPrivateDnsActive()); |
| 125 | assertEquals("strictmode.com", fixedLp.getPrivateDnsServerName()); |
| 126 | fixedLp = new LinkProperties(lp); |
| 127 | } |
| 128 | |
| 129 | @Test |
| 130 | public void testIgnoreUntrackedValidationUpdates() throws Exception { |
| 131 | // The PrivateDnsConfig map is empty, so no validation events will |
| 132 | // be tracked. |
| 133 | LinkProperties lp = new LinkProperties(); |
| 134 | lp.addDnsServer(InetAddress.getByName("3.3.3.3")); |
| 135 | mDnsManager.setDnsConfigurationForNetwork(TEST_NETID, lp, IS_DEFAULT); |
| 136 | mDnsManager.updatePrivateDnsValidation( |
| 137 | new DnsManager.PrivateDnsValidationUpdate(TEST_NETID, |
| 138 | InetAddress.parseNumericAddress("3.3.3.3"), "", true)); |
| 139 | mDnsManager.updatePrivateDnsStatus(TEST_NETID, lp); |
| 140 | assertFalse(lp.isPrivateDnsActive()); |
| 141 | assertNull(lp.getPrivateDnsServerName()); |
| 142 | |
| 143 | // Validation event has untracked netId |
| 144 | mDnsManager.updatePrivateDns(new Network(TEST_NETID), |
| 145 | mDnsManager.getPrivateDnsConfig()); |
| 146 | mDnsManager.setDnsConfigurationForNetwork(TEST_NETID, lp, IS_DEFAULT); |
| 147 | mDnsManager.updatePrivateDnsValidation( |
| 148 | new DnsManager.PrivateDnsValidationUpdate(TEST_NETID_UNTRACKED, |
| 149 | InetAddress.parseNumericAddress("3.3.3.3"), "", true)); |
| 150 | mDnsManager.updatePrivateDnsStatus(TEST_NETID, lp); |
| 151 | assertFalse(lp.isPrivateDnsActive()); |
| 152 | assertNull(lp.getPrivateDnsServerName()); |
| 153 | |
| 154 | // Validation event has untracked ipAddress |
| 155 | mDnsManager.updatePrivateDnsValidation( |
| 156 | new DnsManager.PrivateDnsValidationUpdate(TEST_NETID, |
| 157 | InetAddress.parseNumericAddress("4.4.4.4"), "", true)); |
| 158 | mDnsManager.updatePrivateDnsStatus(TEST_NETID, lp); |
| 159 | assertFalse(lp.isPrivateDnsActive()); |
| 160 | assertNull(lp.getPrivateDnsServerName()); |
| 161 | |
| 162 | // Validation event has untracked hostname |
| 163 | mDnsManager.updatePrivateDnsValidation( |
| 164 | new DnsManager.PrivateDnsValidationUpdate(TEST_NETID, |
| 165 | InetAddress.parseNumericAddress("3.3.3.3"), "hostname", |
| 166 | true)); |
| 167 | mDnsManager.updatePrivateDnsStatus(TEST_NETID, lp); |
| 168 | assertFalse(lp.isPrivateDnsActive()); |
| 169 | assertNull(lp.getPrivateDnsServerName()); |
| 170 | |
| 171 | // Validation event failed |
| 172 | mDnsManager.updatePrivateDnsValidation( |
| 173 | new DnsManager.PrivateDnsValidationUpdate(TEST_NETID, |
| 174 | InetAddress.parseNumericAddress("3.3.3.3"), "", false)); |
| 175 | mDnsManager.updatePrivateDnsStatus(TEST_NETID, lp); |
| 176 | assertFalse(lp.isPrivateDnsActive()); |
| 177 | assertNull(lp.getPrivateDnsServerName()); |
| 178 | |
| 179 | // Network removed |
| 180 | mDnsManager.removeNetwork(new Network(TEST_NETID)); |
| 181 | mDnsManager.updatePrivateDnsValidation( |
| 182 | new DnsManager.PrivateDnsValidationUpdate(TEST_NETID, |
| 183 | InetAddress.parseNumericAddress("3.3.3.3"), "", true)); |
| 184 | mDnsManager.updatePrivateDnsStatus(TEST_NETID, lp); |
| 185 | assertFalse(lp.isPrivateDnsActive()); |
| 186 | assertNull(lp.getPrivateDnsServerName()); |
| 187 | |
| 188 | // Turn private DNS mode off |
| 189 | Settings.Global.putString(mContentResolver, |
| 190 | Settings.Global.PRIVATE_DNS_MODE, PRIVATE_DNS_MODE_OFF); |
| 191 | mDnsManager.updatePrivateDns(new Network(TEST_NETID), |
| 192 | mDnsManager.getPrivateDnsConfig()); |
| 193 | mDnsManager.setDnsConfigurationForNetwork(TEST_NETID, lp, IS_DEFAULT); |
| 194 | mDnsManager.updatePrivateDnsValidation( |
| 195 | new DnsManager.PrivateDnsValidationUpdate(TEST_NETID, |
| 196 | InetAddress.parseNumericAddress("3.3.3.3"), "", true)); |
| 197 | mDnsManager.updatePrivateDnsStatus(TEST_NETID, lp); |
| 198 | assertFalse(lp.isPrivateDnsActive()); |
| 199 | assertNull(lp.getPrivateDnsServerName()); |
| 200 | } |
| 201 | } |