Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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 | |
Remi NGUYEN VAN | ad99e54 | 2019-02-13 20:58:59 +0900 | [diff] [blame] | 19 | import static android.net.CaptivePortal.APP_RETURN_DISMISSED; |
Chiachang Wang | 813ee47 | 2019-05-23 16:29:30 +0800 | [diff] [blame] | 20 | import static android.net.INetworkMonitor.NETWORK_VALIDATION_PROBE_DNS; |
| 21 | import static android.net.INetworkMonitor.NETWORK_VALIDATION_PROBE_FALLBACK; |
| 22 | import static android.net.INetworkMonitor.NETWORK_VALIDATION_PROBE_HTTP; |
| 23 | import static android.net.INetworkMonitor.NETWORK_VALIDATION_PROBE_HTTPS; |
| 24 | import static android.net.INetworkMonitor.NETWORK_VALIDATION_PROBE_PRIVDNS; |
| 25 | import static android.net.INetworkMonitor.NETWORK_VALIDATION_RESULT_PARTIAL; |
| 26 | import static android.net.INetworkMonitor.NETWORK_VALIDATION_RESULT_VALID; |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 27 | import static android.net.NetworkCapabilities.NET_CAPABILITY_INTERNET; |
Chiachang Wang | 9a87f80 | 2019-04-08 19:06:21 +0800 | [diff] [blame] | 28 | import static android.net.util.DataStallUtils.CONFIG_DATA_STALL_CONSECUTIVE_DNS_TIMEOUT_THRESHOLD; |
| 29 | import static android.net.util.DataStallUtils.CONFIG_DATA_STALL_EVALUATION_TYPE; |
| 30 | import static android.net.util.DataStallUtils.CONFIG_DATA_STALL_MIN_EVALUATE_INTERVAL; |
| 31 | import static android.net.util.DataStallUtils.CONFIG_DATA_STALL_VALID_DNS_TIME_THRESHOLD; |
| 32 | import static android.net.util.DataStallUtils.DATA_STALL_EVALUATION_TYPE_DNS; |
Chiachang Wang | 79a6da3 | 2019-04-17 17:00:54 +0800 | [diff] [blame] | 33 | import static android.net.util.NetworkStackUtils.CAPTIVE_PORTAL_FALLBACK_PROBE_SPECS; |
| 34 | import static android.net.util.NetworkStackUtils.CAPTIVE_PORTAL_OTHER_FALLBACK_URLS; |
| 35 | import static android.net.util.NetworkStackUtils.CAPTIVE_PORTAL_USE_HTTPS; |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 36 | |
| 37 | import static junit.framework.Assert.assertEquals; |
| 38 | import static junit.framework.Assert.assertFalse; |
| 39 | |
Lorenzo Colitti | 171cfd2 | 2019-04-18 13:44:32 +0900 | [diff] [blame] | 40 | import static org.junit.Assert.assertArrayEquals; |
Remi NGUYEN VAN | dc6e640 | 2019-03-27 15:42:53 +0900 | [diff] [blame] | 41 | import static org.junit.Assert.assertNotNull; |
| 42 | import static org.junit.Assert.assertNull; |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 43 | import static org.junit.Assert.assertTrue; |
| 44 | import static org.junit.Assert.fail; |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 45 | import static org.mockito.ArgumentMatchers.eq; |
| 46 | import static org.mockito.Mockito.any; |
| 47 | import static org.mockito.Mockito.anyInt; |
| 48 | import static org.mockito.Mockito.doAnswer; |
| 49 | import static org.mockito.Mockito.doReturn; |
| 50 | import static org.mockito.Mockito.doThrow; |
| 51 | import static org.mockito.Mockito.never; |
Lorenzo Colitti | cfc1abc | 2019-04-28 00:55:19 +0900 | [diff] [blame] | 52 | import static org.mockito.Mockito.reset; |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 53 | import static org.mockito.Mockito.timeout; |
| 54 | import static org.mockito.Mockito.times; |
| 55 | import static org.mockito.Mockito.verify; |
| 56 | import static org.mockito.Mockito.when; |
| 57 | |
Chiachang Wang | caa3520 | 2019-02-26 11:32:18 +0800 | [diff] [blame] | 58 | import android.annotation.NonNull; |
Lorenzo Colitti | 37ee46f | 2019-04-08 21:51:41 +0900 | [diff] [blame] | 59 | import android.content.BroadcastReceiver; |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 60 | import android.content.Context; |
Lorenzo Colitti | 37ee46f | 2019-04-08 21:51:41 +0900 | [diff] [blame] | 61 | import android.content.Intent; |
Niklas Lindgren | 0c90488 | 2018-12-07 11:08:04 +0100 | [diff] [blame] | 62 | import android.content.res.Resources; |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 63 | import android.net.ConnectivityManager; |
Lorenzo Colitti | 171cfd2 | 2019-04-18 13:44:32 +0900 | [diff] [blame] | 64 | import android.net.DnsResolver; |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 65 | import android.net.INetworkMonitorCallbacks; |
| 66 | import android.net.InetAddresses; |
| 67 | import android.net.LinkProperties; |
| 68 | import android.net.Network; |
| 69 | import android.net.NetworkCapabilities; |
| 70 | import android.net.NetworkInfo; |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 71 | import android.net.captiveportal.CaptivePortalProbeResult; |
| 72 | import android.net.metrics.IpConnectivityLog; |
Lorenzo Colitti | cfc1abc | 2019-04-28 00:55:19 +0900 | [diff] [blame] | 73 | import android.net.shared.PrivateDnsConfig; |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 74 | import android.net.util.SharedLog; |
Chiachang Wang | caa3520 | 2019-02-26 11:32:18 +0800 | [diff] [blame] | 75 | import android.net.wifi.WifiInfo; |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 76 | import android.net.wifi.WifiManager; |
Remi NGUYEN VAN | ad99e54 | 2019-02-13 20:58:59 +0900 | [diff] [blame] | 77 | import android.os.Bundle; |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 78 | import android.os.ConditionVariable; |
| 79 | import android.os.Handler; |
Lorenzo Colitti | 171cfd2 | 2019-04-18 13:44:32 +0900 | [diff] [blame] | 80 | import android.os.Looper; |
Lorenzo Colitti | cfc1abc | 2019-04-28 00:55:19 +0900 | [diff] [blame] | 81 | import android.os.Process; |
Remi NGUYEN VAN | dc6e640 | 2019-03-27 15:42:53 +0900 | [diff] [blame] | 82 | import android.os.RemoteException; |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 83 | import android.os.SystemClock; |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 84 | import android.provider.Settings; |
Chiachang Wang | caa3520 | 2019-02-26 11:32:18 +0800 | [diff] [blame] | 85 | import android.telephony.CellSignalStrength; |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 86 | import android.telephony.TelephonyManager; |
| 87 | import android.util.ArrayMap; |
| 88 | |
Brett Chabot | 189c598 | 2019-02-26 14:52:33 -0800 | [diff] [blame] | 89 | import androidx.test.filters.SmallTest; |
| 90 | import androidx.test.runner.AndroidJUnit4; |
| 91 | |
Lorenzo Colitti | 171cfd2 | 2019-04-18 13:44:32 +0900 | [diff] [blame] | 92 | import com.android.networkstack.R; |
Chiachang Wang | 8024227 | 2019-04-11 21:24:28 +0800 | [diff] [blame] | 93 | import com.android.networkstack.metrics.DataStallDetectionStats; |
| 94 | import com.android.networkstack.metrics.DataStallStatsUtils; |
Chalard Jean | 0d0cc8c | 2019-05-28 16:48:32 +0900 | [diff] [blame] | 95 | import com.android.testutils.HandlerUtilsKt; |
Chiachang Wang | 8024227 | 2019-04-11 21:24:28 +0800 | [diff] [blame] | 96 | |
Lorenzo Colitti | 37ee46f | 2019-04-08 21:51:41 +0900 | [diff] [blame] | 97 | import org.junit.After; |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 98 | import org.junit.Before; |
| 99 | import org.junit.Test; |
| 100 | import org.junit.runner.RunWith; |
| 101 | import org.mockito.ArgumentCaptor; |
Remi NGUYEN VAN | dc6e640 | 2019-03-27 15:42:53 +0900 | [diff] [blame] | 102 | import org.mockito.Captor; |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 103 | import org.mockito.Mock; |
| 104 | import org.mockito.MockitoAnnotations; |
| 105 | import org.mockito.Spy; |
Chiachang Wang | ddb7da6 | 2019-06-03 15:50:53 +0800 | [diff] [blame^] | 106 | import org.mockito.invocation.InvocationOnMock; |
| 107 | import org.mockito.stubbing.Answer; |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 108 | |
| 109 | import java.io.IOException; |
| 110 | import java.net.HttpURLConnection; |
| 111 | import java.net.InetAddress; |
| 112 | import java.net.URL; |
Lorenzo Colitti | 171cfd2 | 2019-04-18 13:44:32 +0900 | [diff] [blame] | 113 | import java.net.UnknownHostException; |
| 114 | import java.util.ArrayList; |
Lorenzo Colitti | 37ee46f | 2019-04-08 21:51:41 +0900 | [diff] [blame] | 115 | import java.util.HashSet; |
Lorenzo Colitti | 171cfd2 | 2019-04-18 13:44:32 +0900 | [diff] [blame] | 116 | import java.util.List; |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 117 | import java.util.Random; |
Lorenzo Colitti | 171cfd2 | 2019-04-18 13:44:32 +0900 | [diff] [blame] | 118 | import java.util.concurrent.Executor; |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 119 | |
| 120 | import javax.net.ssl.SSLHandshakeException; |
| 121 | |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 122 | @RunWith(AndroidJUnit4.class) |
| 123 | @SmallTest |
| 124 | public class NetworkMonitorTest { |
| 125 | private static final String LOCATION_HEADER = "location"; |
| 126 | |
| 127 | private @Mock Context mContext; |
Niklas Lindgren | 0c90488 | 2018-12-07 11:08:04 +0100 | [diff] [blame] | 128 | private @Mock Resources mResources; |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 129 | private @Mock IpConnectivityLog mLogger; |
| 130 | private @Mock SharedLog mValidationLogger; |
| 131 | private @Mock NetworkInfo mNetworkInfo; |
Lorenzo Colitti | 171cfd2 | 2019-04-18 13:44:32 +0900 | [diff] [blame] | 132 | private @Mock DnsResolver mDnsResolver; |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 133 | private @Mock ConnectivityManager mCm; |
| 134 | private @Mock TelephonyManager mTelephony; |
| 135 | private @Mock WifiManager mWifi; |
| 136 | private @Mock HttpURLConnection mHttpConnection; |
| 137 | private @Mock HttpURLConnection mHttpsConnection; |
| 138 | private @Mock HttpURLConnection mFallbackConnection; |
| 139 | private @Mock HttpURLConnection mOtherFallbackConnection; |
| 140 | private @Mock Random mRandom; |
| 141 | private @Mock NetworkMonitor.Dependencies mDependencies; |
| 142 | private @Mock INetworkMonitorCallbacks mCallbacks; |
Lorenzo Colitti | 7f9734f | 2019-05-09 12:13:54 +0900 | [diff] [blame] | 143 | private @Spy Network mCleartextDnsNetwork = new Network(TEST_NETID); |
| 144 | private @Mock Network mNetwork; |
Chiachang Wang | 8b5f84a | 2019-02-22 11:13:07 +0800 | [diff] [blame] | 145 | private @Mock DataStallStatsUtils mDataStallStatsUtils; |
Chiachang Wang | caa3520 | 2019-02-26 11:32:18 +0800 | [diff] [blame] | 146 | private @Mock WifiInfo mWifiInfo; |
Remi NGUYEN VAN | dc6e640 | 2019-03-27 15:42:53 +0900 | [diff] [blame] | 147 | private @Captor ArgumentCaptor<String> mNetworkTestedRedirectUrlCaptor; |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 148 | |
Lorenzo Colitti | 37ee46f | 2019-04-08 21:51:41 +0900 | [diff] [blame] | 149 | private HashSet<WrappedNetworkMonitor> mCreatedNetworkMonitors; |
| 150 | private HashSet<BroadcastReceiver> mRegisteredReceivers; |
| 151 | |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 152 | private static final int TEST_NETID = 4242; |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 153 | private static final String TEST_HTTP_URL = "http://www.google.com/gen_204"; |
| 154 | private static final String TEST_HTTPS_URL = "https://www.google.com/gen_204"; |
| 155 | private static final String TEST_FALLBACK_URL = "http://fallback.google.com/gen_204"; |
| 156 | private static final String TEST_OTHER_FALLBACK_URL = "http://otherfallback.google.com/gen_204"; |
Chiachang Wang | caa3520 | 2019-02-26 11:32:18 +0800 | [diff] [blame] | 157 | private static final String TEST_MCCMNC = "123456"; |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 158 | |
Chiachang Wang | 813ee47 | 2019-05-23 16:29:30 +0800 | [diff] [blame] | 159 | private static final int VALIDATION_RESULT_INVALID = 0; |
| 160 | private static final int VALIDATION_RESULT_PORTAL = 0; |
| 161 | private static final String TEST_REDIRECT_URL = "android.com"; |
| 162 | private static final int VALIDATION_RESULT_PARTIAL = NETWORK_VALIDATION_PROBE_DNS |
| 163 | | NETWORK_VALIDATION_PROBE_HTTP |
| 164 | | NETWORK_VALIDATION_RESULT_PARTIAL; |
| 165 | private static final int VALIDATION_RESULT_FALLBACK_PARTIAL = NETWORK_VALIDATION_PROBE_DNS |
| 166 | | NETWORK_VALIDATION_PROBE_FALLBACK |
| 167 | | NETWORK_VALIDATION_RESULT_PARTIAL; |
| 168 | private static final int VALIDATION_RESULT_VALID = NETWORK_VALIDATION_PROBE_DNS |
| 169 | | NETWORK_VALIDATION_PROBE_HTTPS |
| 170 | | NETWORK_VALIDATION_RESULT_VALID; |
| 171 | |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 172 | private static final int RETURN_CODE_DNS_SUCCESS = 0; |
| 173 | private static final int RETURN_CODE_DNS_TIMEOUT = 255; |
Chiachang Wang | caa3520 | 2019-02-26 11:32:18 +0800 | [diff] [blame] | 174 | private static final int DEFAULT_DNS_TIMEOUT_THRESHOLD = 5; |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 175 | |
| 176 | private static final int HANDLER_TIMEOUT_MS = 1000; |
| 177 | |
Remi NGUYEN VAN | dc6e640 | 2019-03-27 15:42:53 +0900 | [diff] [blame] | 178 | private static final LinkProperties TEST_LINK_PROPERTIES = new LinkProperties(); |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 179 | |
| 180 | private static final NetworkCapabilities METERED_CAPABILITIES = new NetworkCapabilities() |
| 181 | .addTransportType(NetworkCapabilities.TRANSPORT_CELLULAR) |
| 182 | .addCapability(NET_CAPABILITY_INTERNET); |
| 183 | |
| 184 | private static final NetworkCapabilities NOT_METERED_CAPABILITIES = new NetworkCapabilities() |
| 185 | .addTransportType(NetworkCapabilities.TRANSPORT_CELLULAR) |
| 186 | .addCapability(NET_CAPABILITY_INTERNET) |
| 187 | .addCapability(NetworkCapabilities.NET_CAPABILITY_NOT_METERED); |
| 188 | |
| 189 | private static final NetworkCapabilities NO_INTERNET_CAPABILITIES = new NetworkCapabilities() |
| 190 | .addTransportType(NetworkCapabilities.TRANSPORT_CELLULAR); |
| 191 | |
Lorenzo Colitti | cfc1abc | 2019-04-28 00:55:19 +0900 | [diff] [blame] | 192 | /** |
| 193 | * Fakes DNS responses. |
| 194 | * |
| 195 | * Allows test methods to configure the IP addresses that will be resolved by |
| 196 | * Network#getAllByName and by DnsResolver#query. |
| 197 | */ |
| 198 | class FakeDns { |
| 199 | private final ArrayMap<String, List<InetAddress>> mAnswers = new ArrayMap<>(); |
| 200 | private boolean mNonBypassPrivateDnsWorking = true; |
| 201 | |
| 202 | /** Whether DNS queries on mNonBypassPrivateDnsWorking should succeed. */ |
| 203 | private void setNonBypassPrivateDnsWorking(boolean working) { |
| 204 | mNonBypassPrivateDnsWorking = working; |
Lorenzo Colitti | 171cfd2 | 2019-04-18 13:44:32 +0900 | [diff] [blame] | 205 | } |
| 206 | |
Lorenzo Colitti | cfc1abc | 2019-04-28 00:55:19 +0900 | [diff] [blame] | 207 | /** Clears all DNS entries. */ |
| 208 | private synchronized void clearAll() { |
| 209 | mAnswers.clear(); |
Lorenzo Colitti | 171cfd2 | 2019-04-18 13:44:32 +0900 | [diff] [blame] | 210 | } |
Lorenzo Colitti | 171cfd2 | 2019-04-18 13:44:32 +0900 | [diff] [blame] | 211 | |
Lorenzo Colitti | cfc1abc | 2019-04-28 00:55:19 +0900 | [diff] [blame] | 212 | /** Returns the answer for a given name on the given mock network. */ |
| 213 | private synchronized List<InetAddress> getAnswer(Object mock, String hostname) { |
Lorenzo Colitti | 7f9734f | 2019-05-09 12:13:54 +0900 | [diff] [blame] | 214 | if (mock == mNetwork && !mNonBypassPrivateDnsWorking) { |
Lorenzo Colitti | cfc1abc | 2019-04-28 00:55:19 +0900 | [diff] [blame] | 215 | return null; |
| 216 | } |
| 217 | if (mAnswers.containsKey(hostname)) { |
| 218 | return mAnswers.get(hostname); |
| 219 | } |
| 220 | return mAnswers.get("*"); |
| 221 | } |
Lorenzo Colitti | 171cfd2 | 2019-04-18 13:44:32 +0900 | [diff] [blame] | 222 | |
Lorenzo Colitti | cfc1abc | 2019-04-28 00:55:19 +0900 | [diff] [blame] | 223 | /** Sets the answer for a given name. */ |
| 224 | private synchronized void setAnswer(String hostname, String[] answer) |
| 225 | throws UnknownHostException { |
| 226 | if (answer == null) { |
| 227 | mAnswers.remove(hostname); |
| 228 | } else { |
| 229 | List<InetAddress> answerList = new ArrayList<>(); |
| 230 | for (String addr : answer) { |
| 231 | answerList.add(InetAddresses.parseNumericAddress(addr)); |
| 232 | } |
| 233 | mAnswers.put(hostname, answerList); |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | /** Simulates a getAllByName call for the specified name on the specified mock network. */ |
| 238 | private InetAddress[] getAllByName(Object mock, String hostname) |
| 239 | throws UnknownHostException { |
| 240 | List<InetAddress> answer = getAnswer(mock, hostname); |
| 241 | if (answer == null || answer.size() == 0) { |
| 242 | throw new UnknownHostException(hostname); |
| 243 | } |
| 244 | return answer.toArray(new InetAddress[0]); |
| 245 | } |
| 246 | |
| 247 | /** Starts mocking DNS queries. */ |
| 248 | private void startMocking() throws UnknownHostException { |
Lorenzo Colitti | 7f9734f | 2019-05-09 12:13:54 +0900 | [diff] [blame] | 249 | // Queries on mNetwork using getAllByName. |
Lorenzo Colitti | cfc1abc | 2019-04-28 00:55:19 +0900 | [diff] [blame] | 250 | doAnswer(invocation -> { |
| 251 | return getAllByName(invocation.getMock(), invocation.getArgument(0)); |
| 252 | }).when(mNetwork).getAllByName(any()); |
| 253 | |
Lorenzo Colitti | 7f9734f | 2019-05-09 12:13:54 +0900 | [diff] [blame] | 254 | // Queries on mCleartextDnsNetwork using DnsResolver#query. |
Lorenzo Colitti | cfc1abc | 2019-04-28 00:55:19 +0900 | [diff] [blame] | 255 | doAnswer(invocation -> { |
Chiachang Wang | ddb7da6 | 2019-06-03 15:50:53 +0800 | [diff] [blame^] | 256 | return mockQuery(invocation, 1 /* posHostname */, 3 /* posExecutor */, |
| 257 | 5 /* posCallback */); |
Lorenzo Colitti | cfc1abc | 2019-04-28 00:55:19 +0900 | [diff] [blame] | 258 | }).when(mDnsResolver).query(any(), any(), anyInt(), any(), any(), any()); |
Chiachang Wang | 1c67f4e | 2019-05-09 21:28:47 +0800 | [diff] [blame] | 259 | |
Chiachang Wang | ddb7da6 | 2019-06-03 15:50:53 +0800 | [diff] [blame^] | 260 | // Queries on mCleartextDnsNetwork using DnsResolver#query with QueryType. |
Chiachang Wang | 1c67f4e | 2019-05-09 21:28:47 +0800 | [diff] [blame] | 261 | doAnswer(invocation -> { |
Chiachang Wang | ddb7da6 | 2019-06-03 15:50:53 +0800 | [diff] [blame^] | 262 | return mockQuery(invocation, 1 /* posHostname */, 4 /* posExecutor */, |
| 263 | 6 /* posCallback */); |
Chiachang Wang | 1c67f4e | 2019-05-09 21:28:47 +0800 | [diff] [blame] | 264 | }).when(mDnsResolver).query(any(), any(), anyInt(), anyInt(), any(), any(), any()); |
Lorenzo Colitti | cfc1abc | 2019-04-28 00:55:19 +0900 | [diff] [blame] | 265 | } |
Chiachang Wang | ddb7da6 | 2019-06-03 15:50:53 +0800 | [diff] [blame^] | 266 | |
| 267 | // Mocking queries on DnsResolver#query. |
| 268 | private Answer mockQuery(InvocationOnMock invocation, int posHostname, int posExecutor, |
| 269 | int posCallback) { |
| 270 | String hostname = (String) invocation.getArgument(posHostname); |
| 271 | Executor executor = (Executor) invocation.getArgument(posExecutor); |
| 272 | DnsResolver.Callback<List<InetAddress>> callback = invocation.getArgument(posCallback); |
| 273 | |
| 274 | List<InetAddress> answer = getAnswer(invocation.getMock(), hostname); |
| 275 | if (answer != null && answer.size() > 0) { |
| 276 | new Handler(Looper.getMainLooper()).post(() -> { |
| 277 | executor.execute(() -> callback.onAnswer(answer, 0)); |
| 278 | }); |
| 279 | } |
| 280 | // If no answers, do nothing. sendDnsProbeWithTimeout will time out and throw UHE. |
| 281 | return null; |
| 282 | } |
Lorenzo Colitti | 171cfd2 | 2019-04-18 13:44:32 +0900 | [diff] [blame] | 283 | } |
| 284 | |
Lorenzo Colitti | cfc1abc | 2019-04-28 00:55:19 +0900 | [diff] [blame] | 285 | private FakeDns mFakeDns; |
| 286 | |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 287 | @Before |
Remi NGUYEN VAN | b4af130 | 2019-06-11 16:17:46 +0900 | [diff] [blame] | 288 | public void setUp() throws Exception { |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 289 | MockitoAnnotations.initMocks(this); |
Lorenzo Colitti | 7f9734f | 2019-05-09 12:13:54 +0900 | [diff] [blame] | 290 | when(mDependencies.getPrivateDnsBypassNetwork(any())).thenReturn(mCleartextDnsNetwork); |
Lorenzo Colitti | 171cfd2 | 2019-04-18 13:44:32 +0900 | [diff] [blame] | 291 | when(mDependencies.getDnsResolver()).thenReturn(mDnsResolver); |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 292 | when(mDependencies.getRandom()).thenReturn(mRandom); |
| 293 | when(mDependencies.getSetting(any(), eq(Settings.Global.CAPTIVE_PORTAL_MODE), anyInt())) |
| 294 | .thenReturn(Settings.Global.CAPTIVE_PORTAL_MODE_PROMPT); |
Chiachang Wang | 79a6da3 | 2019-04-17 17:00:54 +0800 | [diff] [blame] | 295 | when(mDependencies.getDeviceConfigPropertyInt(any(), eq(CAPTIVE_PORTAL_USE_HTTPS), |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 296 | anyInt())).thenReturn(1); |
Niklas Lindgren | 0c90488 | 2018-12-07 11:08:04 +0100 | [diff] [blame] | 297 | when(mDependencies.getSetting(any(), eq(Settings.Global.CAPTIVE_PORTAL_HTTP_URL), any())) |
| 298 | .thenReturn(TEST_HTTP_URL); |
| 299 | when(mDependencies.getSetting(any(), eq(Settings.Global.CAPTIVE_PORTAL_HTTPS_URL), any())) |
| 300 | .thenReturn(TEST_HTTPS_URL); |
| 301 | |
Lorenzo Colitti | 7f9734f | 2019-05-09 12:13:54 +0900 | [diff] [blame] | 302 | doReturn(mCleartextDnsNetwork).when(mNetwork).getPrivateDnsBypassingCopy(); |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 303 | |
| 304 | when(mContext.getSystemService(Context.CONNECTIVITY_SERVICE)).thenReturn(mCm); |
| 305 | when(mContext.getSystemService(Context.TELEPHONY_SERVICE)).thenReturn(mTelephony); |
| 306 | when(mContext.getSystemService(Context.WIFI_SERVICE)).thenReturn(mWifi); |
Niklas Lindgren | 0c90488 | 2018-12-07 11:08:04 +0100 | [diff] [blame] | 307 | when(mContext.getResources()).thenReturn(mResources); |
| 308 | |
| 309 | when(mResources.getString(anyInt())).thenReturn(""); |
| 310 | when(mResources.getStringArray(anyInt())).thenReturn(new String[0]); |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 311 | |
| 312 | when(mNetworkInfo.getType()).thenReturn(ConnectivityManager.TYPE_WIFI); |
| 313 | setFallbackUrl(TEST_FALLBACK_URL); |
| 314 | setOtherFallbackUrls(TEST_OTHER_FALLBACK_URL); |
| 315 | setFallbackSpecs(null); // Test with no fallback spec by default |
| 316 | when(mRandom.nextInt()).thenReturn(0); |
| 317 | |
Chiachang Wang | c11639d | 2019-05-15 16:18:56 +0800 | [diff] [blame] | 318 | // DNS probe timeout should not be defined more than half of HANDLER_TIMEOUT_MS. Otherwise, |
| 319 | // it will fail the test because of timeout expired for querying AAAA and A sequentially. |
Lorenzo Colitti | cfc1abc | 2019-04-28 00:55:19 +0900 | [diff] [blame] | 320 | when(mResources.getInteger(eq(R.integer.config_captive_portal_dns_probe_timeout))) |
Chiachang Wang | c11639d | 2019-05-15 16:18:56 +0800 | [diff] [blame] | 321 | .thenReturn(200); |
Lorenzo Colitti | cfc1abc | 2019-04-28 00:55:19 +0900 | [diff] [blame] | 322 | |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 323 | doAnswer((invocation) -> { |
| 324 | URL url = invocation.getArgument(0); |
| 325 | switch(url.toString()) { |
| 326 | case TEST_HTTP_URL: |
| 327 | return mHttpConnection; |
| 328 | case TEST_HTTPS_URL: |
| 329 | return mHttpsConnection; |
| 330 | case TEST_FALLBACK_URL: |
| 331 | return mFallbackConnection; |
| 332 | case TEST_OTHER_FALLBACK_URL: |
| 333 | return mOtherFallbackConnection; |
| 334 | default: |
| 335 | fail("URL not mocked: " + url.toString()); |
| 336 | return null; |
| 337 | } |
Lorenzo Colitti | 7f9734f | 2019-05-09 12:13:54 +0900 | [diff] [blame] | 338 | }).when(mCleartextDnsNetwork).openConnection(any()); |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 339 | when(mHttpConnection.getRequestProperties()).thenReturn(new ArrayMap<>()); |
| 340 | when(mHttpsConnection.getRequestProperties()).thenReturn(new ArrayMap<>()); |
Lorenzo Colitti | 171cfd2 | 2019-04-18 13:44:32 +0900 | [diff] [blame] | 341 | |
Lorenzo Colitti | cfc1abc | 2019-04-28 00:55:19 +0900 | [diff] [blame] | 342 | mFakeDns = new FakeDns(); |
| 343 | mFakeDns.startMocking(); |
| 344 | mFakeDns.setAnswer("*", new String[]{"2001:db8::1", "192.0.2.2"}); |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 345 | |
Lorenzo Colitti | 37ee46f | 2019-04-08 21:51:41 +0900 | [diff] [blame] | 346 | when(mContext.registerReceiver(any(BroadcastReceiver.class), any())).then((invocation) -> { |
| 347 | mRegisteredReceivers.add(invocation.getArgument(0)); |
| 348 | return new Intent(); |
| 349 | }); |
| 350 | |
| 351 | doAnswer((invocation) -> { |
| 352 | mRegisteredReceivers.remove(invocation.getArgument(0)); |
| 353 | return null; |
| 354 | }).when(mContext).unregisterReceiver(any()); |
| 355 | |
Remi NGUYEN VAN | b4af130 | 2019-06-11 16:17:46 +0900 | [diff] [blame] | 356 | resetCallbacks(); |
| 357 | |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 358 | setMinDataStallEvaluateInterval(500); |
Chiachang Wang | 0e87479 | 2019-03-05 20:31:57 +0800 | [diff] [blame] | 359 | setDataStallEvaluationType(DATA_STALL_EVALUATION_TYPE_DNS); |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 360 | setValidDataStallDnsTimeThreshold(500); |
| 361 | setConsecutiveDnsTimeoutThreshold(5); |
Lorenzo Colitti | 37ee46f | 2019-04-08 21:51:41 +0900 | [diff] [blame] | 362 | |
| 363 | mCreatedNetworkMonitors = new HashSet<>(); |
| 364 | mRegisteredReceivers = new HashSet<>(); |
| 365 | } |
| 366 | |
| 367 | @After |
| 368 | public void tearDown() { |
Lorenzo Colitti | cfc1abc | 2019-04-28 00:55:19 +0900 | [diff] [blame] | 369 | mFakeDns.clearAll(); |
Lorenzo Colitti | 37ee46f | 2019-04-08 21:51:41 +0900 | [diff] [blame] | 370 | assertTrue(mCreatedNetworkMonitors.size() > 0); |
| 371 | // Make a local copy of mCreatedNetworkMonitors because during the iteration below, |
| 372 | // WrappedNetworkMonitor#onQuitting will delete elements from it on the handler threads. |
| 373 | WrappedNetworkMonitor[] networkMonitors = mCreatedNetworkMonitors.toArray( |
| 374 | new WrappedNetworkMonitor[0]); |
| 375 | for (WrappedNetworkMonitor nm : networkMonitors) { |
| 376 | nm.notifyNetworkDisconnected(); |
| 377 | nm.awaitQuit(); |
| 378 | } |
| 379 | assertEquals("NetworkMonitor still running after disconnect", |
| 380 | 0, mCreatedNetworkMonitors.size()); |
| 381 | assertEquals("BroadcastReceiver still registered after disconnect", |
| 382 | 0, mRegisteredReceivers.size()); |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 383 | } |
| 384 | |
Remi NGUYEN VAN | b4af130 | 2019-06-11 16:17:46 +0900 | [diff] [blame] | 385 | private void resetCallbacks() { |
| 386 | reset(mCallbacks); |
| 387 | // TODO: make this a parameterized test. |
| 388 | try { |
| 389 | when(mCallbacks.getInterfaceVersion()).thenReturn(3); |
| 390 | } catch (RemoteException e) { |
| 391 | // Can't happen as mCallbacks is a mock |
| 392 | fail("Error mocking getInterfaceVersion" + e); |
| 393 | } |
| 394 | } |
| 395 | |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 396 | private class WrappedNetworkMonitor extends NetworkMonitor { |
| 397 | private long mProbeTime = 0; |
Lorenzo Colitti | 37ee46f | 2019-04-08 21:51:41 +0900 | [diff] [blame] | 398 | private final ConditionVariable mQuitCv = new ConditionVariable(false); |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 399 | |
Remi NGUYEN VAN | dc6e640 | 2019-03-27 15:42:53 +0900 | [diff] [blame] | 400 | WrappedNetworkMonitor() { |
Lorenzo Colitti | 7f9734f | 2019-05-09 12:13:54 +0900 | [diff] [blame] | 401 | super(mContext, mCallbacks, mNetwork, mLogger, mValidationLogger, |
Lorenzo Colitti | cfc1abc | 2019-04-28 00:55:19 +0900 | [diff] [blame] | 402 | mDependencies, mDataStallStatsUtils); |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 403 | } |
| 404 | |
| 405 | @Override |
| 406 | protected long getLastProbeTime() { |
| 407 | return mProbeTime; |
| 408 | } |
| 409 | |
| 410 | protected void setLastProbeTime(long time) { |
| 411 | mProbeTime = time; |
| 412 | } |
Chiachang Wang | caa3520 | 2019-02-26 11:32:18 +0800 | [diff] [blame] | 413 | |
| 414 | @Override |
| 415 | protected void addDnsEvents(@NonNull final DataStallDetectionStats.Builder stats) { |
| 416 | generateTimeoutDnsEvent(stats, DEFAULT_DNS_TIMEOUT_THRESHOLD); |
| 417 | } |
Lorenzo Colitti | 37ee46f | 2019-04-08 21:51:41 +0900 | [diff] [blame] | 418 | |
| 419 | @Override |
| 420 | protected void onQuitting() { |
| 421 | assertTrue(mCreatedNetworkMonitors.remove(this)); |
| 422 | mQuitCv.open(); |
| 423 | } |
| 424 | |
| 425 | protected void awaitQuit() { |
| 426 | assertTrue("NetworkMonitor did not quit after " + HANDLER_TIMEOUT_MS + "ms", |
| 427 | mQuitCv.block(HANDLER_TIMEOUT_MS)); |
| 428 | } |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 429 | } |
| 430 | |
Lorenzo Colitti | cfc1abc | 2019-04-28 00:55:19 +0900 | [diff] [blame] | 431 | private WrappedNetworkMonitor makeMonitor(NetworkCapabilities nc) { |
Remi NGUYEN VAN | dc6e640 | 2019-03-27 15:42:53 +0900 | [diff] [blame] | 432 | final WrappedNetworkMonitor nm = new WrappedNetworkMonitor(); |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 433 | nm.start(); |
Lorenzo Colitti | cfc1abc | 2019-04-28 00:55:19 +0900 | [diff] [blame] | 434 | setNetworkCapabilities(nm, nc); |
Chalard Jean | 0d0cc8c | 2019-05-28 16:48:32 +0900 | [diff] [blame] | 435 | HandlerUtilsKt.waitForIdle(nm.getHandler(), HANDLER_TIMEOUT_MS); |
Lorenzo Colitti | 37ee46f | 2019-04-08 21:51:41 +0900 | [diff] [blame] | 436 | mCreatedNetworkMonitors.add(nm); |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 437 | return nm; |
| 438 | } |
| 439 | |
Remi NGUYEN VAN | dc6e640 | 2019-03-27 15:42:53 +0900 | [diff] [blame] | 440 | private WrappedNetworkMonitor makeMeteredNetworkMonitor() { |
Lorenzo Colitti | cfc1abc | 2019-04-28 00:55:19 +0900 | [diff] [blame] | 441 | final WrappedNetworkMonitor nm = makeMonitor(METERED_CAPABILITIES); |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 442 | return nm; |
| 443 | } |
| 444 | |
Remi NGUYEN VAN | dc6e640 | 2019-03-27 15:42:53 +0900 | [diff] [blame] | 445 | private WrappedNetworkMonitor makeNotMeteredNetworkMonitor() { |
Lorenzo Colitti | cfc1abc | 2019-04-28 00:55:19 +0900 | [diff] [blame] | 446 | final WrappedNetworkMonitor nm = makeMonitor(NOT_METERED_CAPABILITIES); |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 447 | return nm; |
| 448 | } |
| 449 | |
Remi NGUYEN VAN | dc6e640 | 2019-03-27 15:42:53 +0900 | [diff] [blame] | 450 | private void setNetworkCapabilities(NetworkMonitor nm, NetworkCapabilities nc) { |
| 451 | nm.notifyNetworkCapabilitiesChanged(nc); |
Chalard Jean | 0d0cc8c | 2019-05-28 16:48:32 +0900 | [diff] [blame] | 452 | HandlerUtilsKt.waitForIdle(nm.getHandler(), HANDLER_TIMEOUT_MS); |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 453 | } |
| 454 | |
| 455 | @Test |
Lorenzo Colitti | 171cfd2 | 2019-04-18 13:44:32 +0900 | [diff] [blame] | 456 | public void testGetIntSetting() throws Exception { |
| 457 | WrappedNetworkMonitor wnm = makeNotMeteredNetworkMonitor(); |
| 458 | |
| 459 | // No config resource, no device config. Expect to get default resource. |
| 460 | doThrow(new Resources.NotFoundException()) |
| 461 | .when(mResources).getInteger(eq(R.integer.config_captive_portal_dns_probe_timeout)); |
| 462 | doAnswer(invocation -> { |
| 463 | int defaultValue = invocation.getArgument(2); |
| 464 | return defaultValue; |
| 465 | }).when(mDependencies).getDeviceConfigPropertyInt(any(), |
| 466 | eq(NetworkMonitor.CONFIG_CAPTIVE_PORTAL_DNS_PROBE_TIMEOUT), |
| 467 | anyInt()); |
| 468 | when(mResources.getInteger(eq(R.integer.default_captive_portal_dns_probe_timeout))) |
| 469 | .thenReturn(42); |
| 470 | assertEquals(42, wnm.getIntSetting(mContext, |
| 471 | R.integer.config_captive_portal_dns_probe_timeout, |
| 472 | NetworkMonitor.CONFIG_CAPTIVE_PORTAL_DNS_PROBE_TIMEOUT, |
| 473 | R.integer.default_captive_portal_dns_probe_timeout)); |
| 474 | |
| 475 | // Set device config. Expect to get device config. |
| 476 | when(mDependencies.getDeviceConfigPropertyInt(any(), |
| 477 | eq(NetworkMonitor.CONFIG_CAPTIVE_PORTAL_DNS_PROBE_TIMEOUT), anyInt())) |
| 478 | .thenReturn(1234); |
| 479 | assertEquals(1234, wnm.getIntSetting(mContext, |
| 480 | R.integer.config_captive_portal_dns_probe_timeout, |
| 481 | NetworkMonitor.CONFIG_CAPTIVE_PORTAL_DNS_PROBE_TIMEOUT, |
| 482 | R.integer.default_captive_portal_dns_probe_timeout)); |
| 483 | |
| 484 | // Set config resource. Expect to get config resource. |
| 485 | when(mResources.getInteger(eq(R.integer.config_captive_portal_dns_probe_timeout))) |
| 486 | .thenReturn(5678); |
| 487 | assertEquals(5678, wnm.getIntSetting(mContext, |
| 488 | R.integer.config_captive_portal_dns_probe_timeout, |
| 489 | NetworkMonitor.CONFIG_CAPTIVE_PORTAL_DNS_PROBE_TIMEOUT, |
| 490 | R.integer.default_captive_portal_dns_probe_timeout)); |
| 491 | } |
| 492 | |
| 493 | @Test |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 494 | public void testIsCaptivePortal_HttpProbeIsPortal() throws IOException { |
| 495 | setSslException(mHttpsConnection); |
| 496 | setPortal302(mHttpConnection); |
Chiachang Wang | 813ee47 | 2019-05-23 16:29:30 +0800 | [diff] [blame] | 497 | runPortalNetworkTest(VALIDATION_RESULT_PORTAL); |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 498 | } |
| 499 | |
| 500 | @Test |
| 501 | public void testIsCaptivePortal_HttpsProbeIsNotPortal() throws IOException { |
| 502 | setStatus(mHttpsConnection, 204); |
| 503 | setStatus(mHttpConnection, 500); |
| 504 | |
Remi NGUYEN VAN | dc6e640 | 2019-03-27 15:42:53 +0900 | [diff] [blame] | 505 | runNotPortalNetworkTest(); |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 506 | } |
| 507 | |
| 508 | @Test |
| 509 | public void testIsCaptivePortal_FallbackProbeIsPortal() throws IOException { |
| 510 | setSslException(mHttpsConnection); |
| 511 | setStatus(mHttpConnection, 500); |
| 512 | setPortal302(mFallbackConnection); |
Chiachang Wang | 813ee47 | 2019-05-23 16:29:30 +0800 | [diff] [blame] | 513 | runPortalNetworkTest(VALIDATION_RESULT_INVALID); |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 514 | } |
| 515 | |
| 516 | @Test |
| 517 | public void testIsCaptivePortal_FallbackProbeIsNotPortal() throws IOException { |
| 518 | setSslException(mHttpsConnection); |
| 519 | setStatus(mHttpConnection, 500); |
Remi NGUYEN VAN | dc6e640 | 2019-03-27 15:42:53 +0900 | [diff] [blame] | 520 | setStatus(mFallbackConnection, 500); |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 521 | |
| 522 | // Fallback probe did not see portal, HTTPS failed -> inconclusive |
Remi NGUYEN VAN | dc6e640 | 2019-03-27 15:42:53 +0900 | [diff] [blame] | 523 | runFailedNetworkTest(); |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 524 | } |
| 525 | |
| 526 | @Test |
| 527 | public void testIsCaptivePortal_OtherFallbackProbeIsPortal() throws IOException { |
| 528 | // Set all fallback probes but one to invalid URLs to verify they are being skipped |
| 529 | setFallbackUrl(TEST_FALLBACK_URL); |
| 530 | setOtherFallbackUrls(TEST_FALLBACK_URL + "," + TEST_OTHER_FALLBACK_URL); |
| 531 | |
| 532 | setSslException(mHttpsConnection); |
| 533 | setStatus(mHttpConnection, 500); |
| 534 | setStatus(mFallbackConnection, 500); |
| 535 | setPortal302(mOtherFallbackConnection); |
| 536 | |
| 537 | // TEST_OTHER_FALLBACK_URL is third |
| 538 | when(mRandom.nextInt()).thenReturn(2); |
| 539 | |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 540 | // First check always uses the first fallback URL: inconclusive |
Chiachang Wang | 813ee47 | 2019-05-23 16:29:30 +0800 | [diff] [blame] | 541 | final NetworkMonitor monitor = runNetworkTest(VALIDATION_RESULT_INVALID); |
Remi NGUYEN VAN | dc6e640 | 2019-03-27 15:42:53 +0900 | [diff] [blame] | 542 | assertNull(mNetworkTestedRedirectUrlCaptor.getValue()); |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 543 | verify(mFallbackConnection, times(1)).getResponseCode(); |
| 544 | verify(mOtherFallbackConnection, never()).getResponseCode(); |
| 545 | |
| 546 | // Second check uses the URL chosen by Random |
Remi NGUYEN VAN | dc6e640 | 2019-03-27 15:42:53 +0900 | [diff] [blame] | 547 | final CaptivePortalProbeResult result = monitor.isCaptivePortal(); |
| 548 | assertTrue(result.isPortal()); |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 549 | verify(mOtherFallbackConnection, times(1)).getResponseCode(); |
| 550 | } |
| 551 | |
| 552 | @Test |
| 553 | public void testIsCaptivePortal_AllProbesFailed() throws IOException { |
| 554 | setSslException(mHttpsConnection); |
| 555 | setStatus(mHttpConnection, 500); |
| 556 | setStatus(mFallbackConnection, 404); |
| 557 | |
Remi NGUYEN VAN | dc6e640 | 2019-03-27 15:42:53 +0900 | [diff] [blame] | 558 | runFailedNetworkTest(); |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 559 | verify(mFallbackConnection, times(1)).getResponseCode(); |
| 560 | verify(mOtherFallbackConnection, never()).getResponseCode(); |
| 561 | } |
| 562 | |
| 563 | @Test |
| 564 | public void testIsCaptivePortal_InvalidUrlSkipped() throws IOException { |
| 565 | setFallbackUrl("invalid"); |
| 566 | setOtherFallbackUrls("otherinvalid," + TEST_OTHER_FALLBACK_URL + ",yetanotherinvalid"); |
| 567 | |
| 568 | setSslException(mHttpsConnection); |
| 569 | setStatus(mHttpConnection, 500); |
| 570 | setPortal302(mOtherFallbackConnection); |
Chiachang Wang | 813ee47 | 2019-05-23 16:29:30 +0800 | [diff] [blame] | 571 | runPortalNetworkTest(VALIDATION_RESULT_INVALID); |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 572 | verify(mOtherFallbackConnection, times(1)).getResponseCode(); |
| 573 | verify(mFallbackConnection, never()).getResponseCode(); |
| 574 | } |
| 575 | |
| 576 | private void setupFallbackSpec() throws IOException { |
| 577 | setFallbackSpecs("http://example.com@@/@@204@@/@@" |
| 578 | + "@@,@@" |
| 579 | + TEST_OTHER_FALLBACK_URL + "@@/@@30[12]@@/@@https://(www\\.)?google.com/?.*"); |
| 580 | |
| 581 | setSslException(mHttpsConnection); |
| 582 | setStatus(mHttpConnection, 500); |
| 583 | |
| 584 | // Use the 2nd fallback spec |
| 585 | when(mRandom.nextInt()).thenReturn(1); |
| 586 | } |
| 587 | |
| 588 | @Test |
Remi NGUYEN VAN | dc6e640 | 2019-03-27 15:42:53 +0900 | [diff] [blame] | 589 | public void testIsCaptivePortal_FallbackSpecIsPartial() throws IOException { |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 590 | setupFallbackSpec(); |
| 591 | set302(mOtherFallbackConnection, "https://www.google.com/test?q=3"); |
| 592 | |
Remi NGUYEN VAN | dc6e640 | 2019-03-27 15:42:53 +0900 | [diff] [blame] | 593 | // HTTPS failed, fallback spec went through -> partial connectivity |
Chiachang Wang | 813ee47 | 2019-05-23 16:29:30 +0800 | [diff] [blame] | 594 | runPartialConnectivityNetworkTest(VALIDATION_RESULT_FALLBACK_PARTIAL); |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 595 | verify(mOtherFallbackConnection, times(1)).getResponseCode(); |
| 596 | verify(mFallbackConnection, never()).getResponseCode(); |
| 597 | } |
| 598 | |
| 599 | @Test |
| 600 | public void testIsCaptivePortal_FallbackSpecIsPortal() throws IOException { |
| 601 | setupFallbackSpec(); |
| 602 | set302(mOtherFallbackConnection, "http://login.portal.example.com"); |
Chiachang Wang | 813ee47 | 2019-05-23 16:29:30 +0800 | [diff] [blame] | 603 | runPortalNetworkTest(VALIDATION_RESULT_INVALID); |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 604 | } |
| 605 | |
| 606 | @Test |
| 607 | public void testIsCaptivePortal_IgnorePortals() throws IOException { |
| 608 | setCaptivePortalMode(Settings.Global.CAPTIVE_PORTAL_MODE_IGNORE); |
| 609 | setSslException(mHttpsConnection); |
| 610 | setPortal302(mHttpConnection); |
| 611 | |
Chiachang Wang | 813ee47 | 2019-05-23 16:29:30 +0800 | [diff] [blame] | 612 | runNoValidationNetworkTest(); |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 613 | } |
| 614 | |
| 615 | @Test |
| 616 | public void testIsDataStall_EvaluationDisabled() { |
| 617 | setDataStallEvaluationType(0); |
Remi NGUYEN VAN | dc6e640 | 2019-03-27 15:42:53 +0900 | [diff] [blame] | 618 | WrappedNetworkMonitor wrappedMonitor = makeMeteredNetworkMonitor(); |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 619 | wrappedMonitor.setLastProbeTime(SystemClock.elapsedRealtime() - 100); |
| 620 | assertFalse(wrappedMonitor.isDataStall()); |
| 621 | } |
| 622 | |
| 623 | @Test |
| 624 | public void testIsDataStall_EvaluationDnsOnNotMeteredNetwork() { |
Remi NGUYEN VAN | dc6e640 | 2019-03-27 15:42:53 +0900 | [diff] [blame] | 625 | WrappedNetworkMonitor wrappedMonitor = makeNotMeteredNetworkMonitor(); |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 626 | wrappedMonitor.setLastProbeTime(SystemClock.elapsedRealtime() - 100); |
Chiachang Wang | caa3520 | 2019-02-26 11:32:18 +0800 | [diff] [blame] | 627 | makeDnsTimeoutEvent(wrappedMonitor, DEFAULT_DNS_TIMEOUT_THRESHOLD); |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 628 | assertTrue(wrappedMonitor.isDataStall()); |
| 629 | } |
| 630 | |
| 631 | @Test |
| 632 | public void testIsDataStall_EvaluationDnsOnMeteredNetwork() { |
Remi NGUYEN VAN | dc6e640 | 2019-03-27 15:42:53 +0900 | [diff] [blame] | 633 | WrappedNetworkMonitor wrappedMonitor = makeMeteredNetworkMonitor(); |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 634 | wrappedMonitor.setLastProbeTime(SystemClock.elapsedRealtime() - 100); |
| 635 | assertFalse(wrappedMonitor.isDataStall()); |
| 636 | |
| 637 | wrappedMonitor.setLastProbeTime(SystemClock.elapsedRealtime() - 1000); |
Chiachang Wang | caa3520 | 2019-02-26 11:32:18 +0800 | [diff] [blame] | 638 | makeDnsTimeoutEvent(wrappedMonitor, DEFAULT_DNS_TIMEOUT_THRESHOLD); |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 639 | assertTrue(wrappedMonitor.isDataStall()); |
| 640 | } |
| 641 | |
| 642 | @Test |
| 643 | public void testIsDataStall_EvaluationDnsWithDnsTimeoutCount() { |
Remi NGUYEN VAN | dc6e640 | 2019-03-27 15:42:53 +0900 | [diff] [blame] | 644 | WrappedNetworkMonitor wrappedMonitor = makeMeteredNetworkMonitor(); |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 645 | wrappedMonitor.setLastProbeTime(SystemClock.elapsedRealtime() - 1000); |
| 646 | makeDnsTimeoutEvent(wrappedMonitor, 3); |
| 647 | assertFalse(wrappedMonitor.isDataStall()); |
| 648 | // Reset consecutive timeout counts. |
| 649 | makeDnsSuccessEvent(wrappedMonitor, 1); |
| 650 | makeDnsTimeoutEvent(wrappedMonitor, 2); |
| 651 | assertFalse(wrappedMonitor.isDataStall()); |
| 652 | |
| 653 | makeDnsTimeoutEvent(wrappedMonitor, 3); |
| 654 | assertTrue(wrappedMonitor.isDataStall()); |
| 655 | |
| 656 | // Set the value to larger than the default dns log size. |
| 657 | setConsecutiveDnsTimeoutThreshold(51); |
Remi NGUYEN VAN | dc6e640 | 2019-03-27 15:42:53 +0900 | [diff] [blame] | 658 | wrappedMonitor = makeMeteredNetworkMonitor(); |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 659 | wrappedMonitor.setLastProbeTime(SystemClock.elapsedRealtime() - 1000); |
| 660 | makeDnsTimeoutEvent(wrappedMonitor, 50); |
| 661 | assertFalse(wrappedMonitor.isDataStall()); |
| 662 | |
| 663 | makeDnsTimeoutEvent(wrappedMonitor, 1); |
| 664 | assertTrue(wrappedMonitor.isDataStall()); |
| 665 | } |
| 666 | |
| 667 | @Test |
| 668 | public void testIsDataStall_EvaluationDnsWithDnsTimeThreshold() { |
| 669 | // Test dns events happened in valid dns time threshold. |
Remi NGUYEN VAN | dc6e640 | 2019-03-27 15:42:53 +0900 | [diff] [blame] | 670 | WrappedNetworkMonitor wrappedMonitor = makeMeteredNetworkMonitor(); |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 671 | wrappedMonitor.setLastProbeTime(SystemClock.elapsedRealtime() - 100); |
Chiachang Wang | caa3520 | 2019-02-26 11:32:18 +0800 | [diff] [blame] | 672 | makeDnsTimeoutEvent(wrappedMonitor, DEFAULT_DNS_TIMEOUT_THRESHOLD); |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 673 | assertFalse(wrappedMonitor.isDataStall()); |
| 674 | wrappedMonitor.setLastProbeTime(SystemClock.elapsedRealtime() - 1000); |
| 675 | assertTrue(wrappedMonitor.isDataStall()); |
| 676 | |
| 677 | // Test dns events happened before valid dns time threshold. |
| 678 | setValidDataStallDnsTimeThreshold(0); |
Remi NGUYEN VAN | dc6e640 | 2019-03-27 15:42:53 +0900 | [diff] [blame] | 679 | wrappedMonitor = makeMeteredNetworkMonitor(); |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 680 | wrappedMonitor.setLastProbeTime(SystemClock.elapsedRealtime() - 100); |
Chiachang Wang | caa3520 | 2019-02-26 11:32:18 +0800 | [diff] [blame] | 681 | makeDnsTimeoutEvent(wrappedMonitor, DEFAULT_DNS_TIMEOUT_THRESHOLD); |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 682 | assertFalse(wrappedMonitor.isDataStall()); |
| 683 | wrappedMonitor.setLastProbeTime(SystemClock.elapsedRealtime() - 1000); |
| 684 | assertFalse(wrappedMonitor.isDataStall()); |
| 685 | } |
| 686 | |
| 687 | @Test |
| 688 | public void testBrokenNetworkNotValidated() throws Exception { |
| 689 | setSslException(mHttpsConnection); |
| 690 | setStatus(mHttpConnection, 500); |
| 691 | setStatus(mFallbackConnection, 404); |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 692 | |
Remi NGUYEN VAN | dc6e640 | 2019-03-27 15:42:53 +0900 | [diff] [blame] | 693 | runFailedNetworkTest(); |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 694 | } |
| 695 | |
| 696 | @Test |
| 697 | public void testNoInternetCapabilityValidated() throws Exception { |
Lorenzo Colitti | aa16f2d | 2019-06-07 18:42:34 +0900 | [diff] [blame] | 698 | runNetworkTest(NO_INTERNET_CAPABILITIES, NETWORK_VALIDATION_RESULT_VALID); |
Lorenzo Colitti | 7f9734f | 2019-05-09 12:13:54 +0900 | [diff] [blame] | 699 | verify(mCleartextDnsNetwork, never()).openConnection(any()); |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 700 | } |
| 701 | |
| 702 | @Test |
| 703 | public void testLaunchCaptivePortalApp() throws Exception { |
| 704 | setSslException(mHttpsConnection); |
| 705 | setPortal302(mHttpConnection); |
| 706 | |
Lorenzo Colitti | cfc1abc | 2019-04-28 00:55:19 +0900 | [diff] [blame] | 707 | final NetworkMonitor nm = makeMonitor(METERED_CAPABILITIES); |
Remi NGUYEN VAN | dc6e640 | 2019-03-27 15:42:53 +0900 | [diff] [blame] | 708 | nm.notifyNetworkConnected(TEST_LINK_PROPERTIES, METERED_CAPABILITIES); |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 709 | |
| 710 | verify(mCallbacks, timeout(HANDLER_TIMEOUT_MS).times(1)) |
Remi NGUYEN VAN | 1e3eb37 | 2019-02-07 21:29:57 +0900 | [diff] [blame] | 711 | .showProvisioningNotification(any(), any()); |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 712 | |
Lorenzo Colitti | 37ee46f | 2019-04-08 21:51:41 +0900 | [diff] [blame] | 713 | assertEquals(1, mRegisteredReceivers.size()); |
| 714 | |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 715 | // Check that startCaptivePortalApp sends the expected intent. |
| 716 | nm.launchCaptivePortalApp(); |
| 717 | |
Remi NGUYEN VAN | ad99e54 | 2019-02-13 20:58:59 +0900 | [diff] [blame] | 718 | final ArgumentCaptor<Bundle> bundleCaptor = ArgumentCaptor.forClass(Bundle.class); |
| 719 | final ArgumentCaptor<Network> networkCaptor = ArgumentCaptor.forClass(Network.class); |
| 720 | verify(mCm, timeout(HANDLER_TIMEOUT_MS).times(1)) |
| 721 | .startCaptivePortalApp(networkCaptor.capture(), bundleCaptor.capture()); |
| 722 | final Bundle bundle = bundleCaptor.getValue(); |
| 723 | final Network bundleNetwork = bundle.getParcelable(ConnectivityManager.EXTRA_NETWORK); |
| 724 | assertEquals(TEST_NETID, bundleNetwork.netId); |
| 725 | // network is passed both in bundle and as parameter, as the bundle is opaque to the |
| 726 | // framework and only intended for the captive portal app, but the framework needs |
| 727 | // the network to identify the right NetworkMonitor. |
| 728 | assertEquals(TEST_NETID, networkCaptor.getValue().netId); |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 729 | |
| 730 | // Have the app report that the captive portal is dismissed, and check that we revalidate. |
| 731 | setStatus(mHttpsConnection, 204); |
| 732 | setStatus(mHttpConnection, 204); |
Remi NGUYEN VAN | ad99e54 | 2019-02-13 20:58:59 +0900 | [diff] [blame] | 733 | |
Remi NGUYEN VAN | b4af130 | 2019-06-11 16:17:46 +0900 | [diff] [blame] | 734 | resetCallbacks(); |
Remi NGUYEN VAN | ad99e54 | 2019-02-13 20:58:59 +0900 | [diff] [blame] | 735 | nm.notifyCaptivePortalAppFinished(APP_RETURN_DISMISSED); |
Chiachang Wang | 813ee47 | 2019-05-23 16:29:30 +0800 | [diff] [blame] | 736 | verify(mCallbacks, timeout(HANDLER_TIMEOUT_MS).atLeastOnce()) |
| 737 | .notifyNetworkTested(eq(NETWORK_VALIDATION_PROBE_DNS | NETWORK_VALIDATION_PROBE_HTTP |
| 738 | | NETWORK_VALIDATION_RESULT_VALID), any()); |
Lorenzo Colitti | 37ee46f | 2019-04-08 21:51:41 +0900 | [diff] [blame] | 739 | assertEquals(0, mRegisteredReceivers.size()); |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 740 | } |
| 741 | |
Chiachang Wang | 8b5f84a | 2019-02-22 11:13:07 +0800 | [diff] [blame] | 742 | @Test |
Lorenzo Colitti | cfc1abc | 2019-04-28 00:55:19 +0900 | [diff] [blame] | 743 | public void testPrivateDnsSuccess() throws Exception { |
| 744 | setStatus(mHttpsConnection, 204); |
| 745 | setStatus(mHttpConnection, 204); |
| 746 | mFakeDns.setAnswer("dns.google", new String[]{"2001:db8::53"}); |
| 747 | |
| 748 | WrappedNetworkMonitor wnm = makeNotMeteredNetworkMonitor(); |
| 749 | wnm.notifyPrivateDnsSettingsChanged(new PrivateDnsConfig("dns.google", new InetAddress[0])); |
| 750 | wnm.notifyNetworkConnected(TEST_LINK_PROPERTIES, NOT_METERED_CAPABILITIES); |
| 751 | verify(mCallbacks, timeout(HANDLER_TIMEOUT_MS).times(1)) |
Chiachang Wang | 813ee47 | 2019-05-23 16:29:30 +0800 | [diff] [blame] | 752 | .notifyNetworkTested(eq(VALIDATION_RESULT_VALID | NETWORK_VALIDATION_PROBE_PRIVDNS), |
| 753 | eq(null)); |
Lorenzo Colitti | cfc1abc | 2019-04-28 00:55:19 +0900 | [diff] [blame] | 754 | } |
| 755 | |
| 756 | @Test |
| 757 | public void testPrivateDnsResolutionRetryUpdate() throws Exception { |
| 758 | // Set a private DNS hostname that doesn't resolve and expect validation to fail. |
| 759 | mFakeDns.setAnswer("dns.google", new String[0]); |
| 760 | setStatus(mHttpsConnection, 204); |
| 761 | setStatus(mHttpConnection, 204); |
| 762 | |
| 763 | WrappedNetworkMonitor wnm = makeNotMeteredNetworkMonitor(); |
| 764 | wnm.notifyPrivateDnsSettingsChanged(new PrivateDnsConfig("dns.google", new InetAddress[0])); |
| 765 | wnm.notifyNetworkConnected(TEST_LINK_PROPERTIES, NOT_METERED_CAPABILITIES); |
Chiachang Wang | 813ee47 | 2019-05-23 16:29:30 +0800 | [diff] [blame] | 766 | verify(mCallbacks, timeout(HANDLER_TIMEOUT_MS).atLeastOnce()) |
| 767 | .notifyNetworkTested( |
| 768 | eq(NETWORK_VALIDATION_PROBE_DNS | NETWORK_VALIDATION_PROBE_HTTPS), |
| 769 | eq(null)); |
Lorenzo Colitti | cfc1abc | 2019-04-28 00:55:19 +0900 | [diff] [blame] | 770 | |
| 771 | // Fix DNS and retry, expect validation to succeed. |
Remi NGUYEN VAN | b4af130 | 2019-06-11 16:17:46 +0900 | [diff] [blame] | 772 | resetCallbacks(); |
Lorenzo Colitti | cfc1abc | 2019-04-28 00:55:19 +0900 | [diff] [blame] | 773 | mFakeDns.setAnswer("dns.google", new String[]{"2001:db8::1"}); |
| 774 | |
| 775 | wnm.forceReevaluation(Process.myUid()); |
Chiachang Wang | 813ee47 | 2019-05-23 16:29:30 +0800 | [diff] [blame] | 776 | verify(mCallbacks, timeout(HANDLER_TIMEOUT_MS).atLeastOnce()) |
| 777 | .notifyNetworkTested(eq(VALIDATION_RESULT_VALID | NETWORK_VALIDATION_PROBE_PRIVDNS), |
| 778 | eq(null)); |
Lorenzo Colitti | cfc1abc | 2019-04-28 00:55:19 +0900 | [diff] [blame] | 779 | |
| 780 | // Change configuration to an invalid DNS name, expect validation to fail. |
Remi NGUYEN VAN | b4af130 | 2019-06-11 16:17:46 +0900 | [diff] [blame] | 781 | resetCallbacks(); |
Lorenzo Colitti | cfc1abc | 2019-04-28 00:55:19 +0900 | [diff] [blame] | 782 | mFakeDns.setAnswer("dns.bad", new String[0]); |
| 783 | wnm.notifyPrivateDnsSettingsChanged(new PrivateDnsConfig("dns.bad", new InetAddress[0])); |
Chiachang Wang | 813ee47 | 2019-05-23 16:29:30 +0800 | [diff] [blame] | 784 | // Strict mode hostname resolve fail. Expect only notification for evaluation fail. No probe |
| 785 | // notification. |
Lorenzo Colitti | aa16f2d | 2019-06-07 18:42:34 +0900 | [diff] [blame] | 786 | verify(mCallbacks, timeout(HANDLER_TIMEOUT_MS)) |
| 787 | .notifyNetworkTested(eq(NETWORK_VALIDATION_PROBE_DNS |
| 788 | | NETWORK_VALIDATION_PROBE_HTTPS), eq(null)); |
Lorenzo Colitti | cfc1abc | 2019-04-28 00:55:19 +0900 | [diff] [blame] | 789 | |
| 790 | // Change configuration back to working again, but make private DNS not work. |
| 791 | // Expect validation to fail. |
Remi NGUYEN VAN | b4af130 | 2019-06-11 16:17:46 +0900 | [diff] [blame] | 792 | resetCallbacks(); |
Lorenzo Colitti | cfc1abc | 2019-04-28 00:55:19 +0900 | [diff] [blame] | 793 | mFakeDns.setNonBypassPrivateDnsWorking(false); |
Chiachang Wang | 813ee47 | 2019-05-23 16:29:30 +0800 | [diff] [blame] | 794 | wnm.notifyPrivateDnsSettingsChanged(new PrivateDnsConfig("dns.google", |
| 795 | new InetAddress[0])); |
| 796 | verify(mCallbacks, timeout(HANDLER_TIMEOUT_MS).atLeastOnce()) |
| 797 | .notifyNetworkTested( |
| 798 | eq(NETWORK_VALIDATION_PROBE_DNS | NETWORK_VALIDATION_PROBE_HTTPS), |
| 799 | eq(null)); |
Lorenzo Colitti | cfc1abc | 2019-04-28 00:55:19 +0900 | [diff] [blame] | 800 | |
| 801 | // Make private DNS work again. Expect validation to succeed. |
Remi NGUYEN VAN | b4af130 | 2019-06-11 16:17:46 +0900 | [diff] [blame] | 802 | resetCallbacks(); |
Lorenzo Colitti | cfc1abc | 2019-04-28 00:55:19 +0900 | [diff] [blame] | 803 | mFakeDns.setNonBypassPrivateDnsWorking(true); |
| 804 | wnm.forceReevaluation(Process.myUid()); |
Chiachang Wang | 813ee47 | 2019-05-23 16:29:30 +0800 | [diff] [blame] | 805 | verify(mCallbacks, timeout(HANDLER_TIMEOUT_MS).atLeastOnce()) |
| 806 | .notifyNetworkTested( |
| 807 | eq(VALIDATION_RESULT_VALID | NETWORK_VALIDATION_PROBE_PRIVDNS), eq(null)); |
Lorenzo Colitti | cfc1abc | 2019-04-28 00:55:19 +0900 | [diff] [blame] | 808 | } |
| 809 | |
| 810 | @Test |
Chiachang Wang | 8b5f84a | 2019-02-22 11:13:07 +0800 | [diff] [blame] | 811 | public void testDataStall_StallSuspectedAndSendMetrics() throws IOException { |
Remi NGUYEN VAN | dc6e640 | 2019-03-27 15:42:53 +0900 | [diff] [blame] | 812 | WrappedNetworkMonitor wrappedMonitor = makeNotMeteredNetworkMonitor(); |
Chiachang Wang | 8b5f84a | 2019-02-22 11:13:07 +0800 | [diff] [blame] | 813 | wrappedMonitor.setLastProbeTime(SystemClock.elapsedRealtime() - 1000); |
| 814 | makeDnsTimeoutEvent(wrappedMonitor, 5); |
| 815 | assertTrue(wrappedMonitor.isDataStall()); |
Chiachang Wang | 899d596 | 2019-03-08 14:34:19 +0800 | [diff] [blame] | 816 | verify(mDataStallStatsUtils, times(1)).write(makeEmptyDataStallDetectionStats(), any()); |
Chiachang Wang | 8b5f84a | 2019-02-22 11:13:07 +0800 | [diff] [blame] | 817 | } |
| 818 | |
| 819 | @Test |
| 820 | public void testDataStall_NoStallSuspectedAndSendMetrics() throws IOException { |
Remi NGUYEN VAN | dc6e640 | 2019-03-27 15:42:53 +0900 | [diff] [blame] | 821 | WrappedNetworkMonitor wrappedMonitor = makeNotMeteredNetworkMonitor(); |
Chiachang Wang | 8b5f84a | 2019-02-22 11:13:07 +0800 | [diff] [blame] | 822 | wrappedMonitor.setLastProbeTime(SystemClock.elapsedRealtime() - 1000); |
| 823 | makeDnsTimeoutEvent(wrappedMonitor, 3); |
| 824 | assertFalse(wrappedMonitor.isDataStall()); |
Chiachang Wang | 899d596 | 2019-03-08 14:34:19 +0800 | [diff] [blame] | 825 | verify(mDataStallStatsUtils, never()).write(makeEmptyDataStallDetectionStats(), any()); |
Chiachang Wang | 8b5f84a | 2019-02-22 11:13:07 +0800 | [diff] [blame] | 826 | } |
Chiachang Wang | caa3520 | 2019-02-26 11:32:18 +0800 | [diff] [blame] | 827 | |
| 828 | @Test |
| 829 | public void testCollectDataStallMetrics() { |
Remi NGUYEN VAN | dc6e640 | 2019-03-27 15:42:53 +0900 | [diff] [blame] | 830 | WrappedNetworkMonitor wrappedMonitor = makeNotMeteredNetworkMonitor(); |
Chiachang Wang | caa3520 | 2019-02-26 11:32:18 +0800 | [diff] [blame] | 831 | |
| 832 | when(mTelephony.getDataNetworkType()).thenReturn(TelephonyManager.NETWORK_TYPE_LTE); |
| 833 | when(mTelephony.getNetworkOperator()).thenReturn(TEST_MCCMNC); |
| 834 | when(mTelephony.getSimOperator()).thenReturn(TEST_MCCMNC); |
| 835 | |
| 836 | DataStallDetectionStats.Builder stats = |
| 837 | new DataStallDetectionStats.Builder() |
| 838 | .setEvaluationType(DATA_STALL_EVALUATION_TYPE_DNS) |
| 839 | .setNetworkType(NetworkCapabilities.TRANSPORT_CELLULAR) |
| 840 | .setCellData(TelephonyManager.NETWORK_TYPE_LTE /* radioType */, |
| 841 | true /* roaming */, |
| 842 | TEST_MCCMNC /* networkMccmnc */, |
| 843 | TEST_MCCMNC /* simMccmnc */, |
| 844 | CellSignalStrength.SIGNAL_STRENGTH_NONE_OR_UNKNOWN /* signalStrength */); |
| 845 | generateTimeoutDnsEvent(stats, DEFAULT_DNS_TIMEOUT_THRESHOLD); |
| 846 | |
| 847 | assertEquals(wrappedMonitor.buildDataStallDetectionStats( |
| 848 | NetworkCapabilities.TRANSPORT_CELLULAR), stats.build()); |
| 849 | |
| 850 | when(mWifi.getConnectionInfo()).thenReturn(mWifiInfo); |
| 851 | |
| 852 | stats = new DataStallDetectionStats.Builder() |
| 853 | .setEvaluationType(DATA_STALL_EVALUATION_TYPE_DNS) |
| 854 | .setNetworkType(NetworkCapabilities.TRANSPORT_WIFI) |
| 855 | .setWiFiData(mWifiInfo); |
| 856 | generateTimeoutDnsEvent(stats, DEFAULT_DNS_TIMEOUT_THRESHOLD); |
| 857 | |
| 858 | assertEquals( |
| 859 | wrappedMonitor.buildDataStallDetectionStats(NetworkCapabilities.TRANSPORT_WIFI), |
| 860 | stats.build()); |
| 861 | } |
| 862 | |
lucaslin | b057396 | 2019-03-12 13:08:03 +0800 | [diff] [blame] | 863 | @Test |
| 864 | public void testIgnoreHttpsProbe() throws Exception { |
| 865 | setSslException(mHttpsConnection); |
| 866 | setStatus(mHttpConnection, 204); |
Chiachang Wang | 813ee47 | 2019-05-23 16:29:30 +0800 | [diff] [blame] | 867 | // Expect to send HTTP, HTTPS, FALLBACK probe and evaluation result notifications to CS. |
| 868 | final NetworkMonitor nm = runNetworkTest(VALIDATION_RESULT_PARTIAL); |
lucaslin | b057396 | 2019-03-12 13:08:03 +0800 | [diff] [blame] | 869 | |
Remi NGUYEN VAN | b4af130 | 2019-06-11 16:17:46 +0900 | [diff] [blame] | 870 | resetCallbacks(); |
lucaslin | d01ea62 | 2019-03-20 18:21:59 +0800 | [diff] [blame] | 871 | nm.setAcceptPartialConnectivity(); |
Chiachang Wang | 813ee47 | 2019-05-23 16:29:30 +0800 | [diff] [blame] | 872 | // Expect to update evaluation result notifications to CS. |
| 873 | verify(mCallbacks, timeout(HANDLER_TIMEOUT_MS).times(1)).notifyNetworkTested( |
| 874 | eq(VALIDATION_RESULT_PARTIAL | NETWORK_VALIDATION_RESULT_VALID), eq(null)); |
lucaslin | b057396 | 2019-03-12 13:08:03 +0800 | [diff] [blame] | 875 | } |
| 876 | |
| 877 | @Test |
| 878 | public void testIsPartialConnectivity() throws IOException { |
| 879 | setStatus(mHttpsConnection, 500); |
| 880 | setStatus(mHttpConnection, 204); |
| 881 | setStatus(mFallbackConnection, 500); |
Chiachang Wang | 813ee47 | 2019-05-23 16:29:30 +0800 | [diff] [blame] | 882 | runPartialConnectivityNetworkTest(VALIDATION_RESULT_PARTIAL); |
lucaslin | b057396 | 2019-03-12 13:08:03 +0800 | [diff] [blame] | 883 | |
Remi NGUYEN VAN | b4af130 | 2019-06-11 16:17:46 +0900 | [diff] [blame] | 884 | resetCallbacks(); |
lucaslin | b057396 | 2019-03-12 13:08:03 +0800 | [diff] [blame] | 885 | setStatus(mHttpsConnection, 500); |
| 886 | setStatus(mHttpConnection, 500); |
| 887 | setStatus(mFallbackConnection, 204); |
Chiachang Wang | 813ee47 | 2019-05-23 16:29:30 +0800 | [diff] [blame] | 888 | runPartialConnectivityNetworkTest(VALIDATION_RESULT_FALLBACK_PARTIAL); |
lucaslin | b057396 | 2019-03-12 13:08:03 +0800 | [diff] [blame] | 889 | } |
| 890 | |
Lorenzo Colitti | 171cfd2 | 2019-04-18 13:44:32 +0900 | [diff] [blame] | 891 | private void assertIpAddressArrayEquals(String[] expected, InetAddress[] actual) { |
| 892 | String[] actualStrings = new String[actual.length]; |
| 893 | for (int i = 0; i < actual.length; i++) { |
| 894 | actualStrings[i] = actual[i].getHostAddress(); |
| 895 | } |
| 896 | assertArrayEquals("Array of IP addresses differs", expected, actualStrings); |
| 897 | } |
| 898 | |
| 899 | @Test |
| 900 | public void testSendDnsProbeWithTimeout() throws Exception { |
| 901 | WrappedNetworkMonitor wnm = makeNotMeteredNetworkMonitor(); |
| 902 | final int shortTimeoutMs = 200; |
| 903 | |
Lorenzo Colitti | cfc1abc | 2019-04-28 00:55:19 +0900 | [diff] [blame] | 904 | // Clear the wildcard DNS response created in setUp. |
| 905 | mFakeDns.setAnswer("*", null); |
| 906 | |
Lorenzo Colitti | 171cfd2 | 2019-04-18 13:44:32 +0900 | [diff] [blame] | 907 | String[] expected = new String[]{"2001:db8::"}; |
Lorenzo Colitti | cfc1abc | 2019-04-28 00:55:19 +0900 | [diff] [blame] | 908 | mFakeDns.setAnswer("www.google.com", expected); |
Lorenzo Colitti | 171cfd2 | 2019-04-18 13:44:32 +0900 | [diff] [blame] | 909 | InetAddress[] actual = wnm.sendDnsProbeWithTimeout("www.google.com", shortTimeoutMs); |
| 910 | assertIpAddressArrayEquals(expected, actual); |
| 911 | |
| 912 | expected = new String[]{"2001:db8::", "192.0.2.1"}; |
Lorenzo Colitti | cfc1abc | 2019-04-28 00:55:19 +0900 | [diff] [blame] | 913 | mFakeDns.setAnswer("www.googleapis.com", expected); |
| 914 | actual = wnm.sendDnsProbeWithTimeout("www.googleapis.com", shortTimeoutMs); |
Lorenzo Colitti | 171cfd2 | 2019-04-18 13:44:32 +0900 | [diff] [blame] | 915 | assertIpAddressArrayEquals(expected, actual); |
| 916 | |
Lorenzo Colitti | cfc1abc | 2019-04-28 00:55:19 +0900 | [diff] [blame] | 917 | mFakeDns.setAnswer("www.google.com", new String[0]); |
Lorenzo Colitti | 171cfd2 | 2019-04-18 13:44:32 +0900 | [diff] [blame] | 918 | try { |
| 919 | wnm.sendDnsProbeWithTimeout("www.google.com", shortTimeoutMs); |
| 920 | fail("No DNS results, expected UnknownHostException"); |
| 921 | } catch (UnknownHostException e) { |
| 922 | } |
| 923 | |
Lorenzo Colitti | cfc1abc | 2019-04-28 00:55:19 +0900 | [diff] [blame] | 924 | mFakeDns.setAnswer("www.google.com", null); |
Lorenzo Colitti | 171cfd2 | 2019-04-18 13:44:32 +0900 | [diff] [blame] | 925 | try { |
| 926 | wnm.sendDnsProbeWithTimeout("www.google.com", shortTimeoutMs); |
| 927 | fail("DNS query timed out, expected UnknownHostException"); |
| 928 | } catch (UnknownHostException e) { |
| 929 | } |
| 930 | } |
| 931 | |
Chiachang Wang | 813ee47 | 2019-05-23 16:29:30 +0800 | [diff] [blame] | 932 | @Test |
| 933 | public void testNotifyNetwork_WithforceReevaluation() throws Exception { |
| 934 | final NetworkMonitor nm = runValidatedNetworkTest(); |
| 935 | // Verify forceReevalution will not reset the validation result but only probe result until |
| 936 | // getting the validation result. |
Remi NGUYEN VAN | b4af130 | 2019-06-11 16:17:46 +0900 | [diff] [blame] | 937 | resetCallbacks(); |
Chiachang Wang | 813ee47 | 2019-05-23 16:29:30 +0800 | [diff] [blame] | 938 | setSslException(mHttpsConnection); |
| 939 | setStatus(mHttpConnection, 500); |
| 940 | setStatus(mFallbackConnection, 204); |
| 941 | nm.forceReevaluation(Process.myUid()); |
| 942 | final ArgumentCaptor<Integer> intCaptor = ArgumentCaptor.forClass(Integer.class); |
| 943 | // Expect to send HTTP, HTTPs, FALLBACK and evaluation results. |
Lorenzo Colitti | aa16f2d | 2019-06-07 18:42:34 +0900 | [diff] [blame] | 944 | verify(mCallbacks, timeout(HANDLER_TIMEOUT_MS)) |
| 945 | .notifyNetworkTested(eq(NETWORK_VALIDATION_PROBE_DNS | |
| 946 | NETWORK_VALIDATION_PROBE_FALLBACK | NETWORK_VALIDATION_RESULT_PARTIAL), any()); |
Chiachang Wang | 813ee47 | 2019-05-23 16:29:30 +0800 | [diff] [blame] | 947 | } |
| 948 | |
| 949 | @Test |
| 950 | public void testEvaluationState_clearProbeResults() throws Exception { |
| 951 | final NetworkMonitor nm = runValidatedNetworkTest(); |
| 952 | nm.getEvaluationState().clearProbeResults(); |
| 953 | // Verify probe results are all reset and only evaluation result left. |
| 954 | assertEquals(NETWORK_VALIDATION_RESULT_VALID, |
| 955 | nm.getEvaluationState().getNetworkTestResult()); |
| 956 | } |
| 957 | |
| 958 | @Test |
| 959 | public void testEvaluationState_reportProbeResult() throws Exception { |
| 960 | final NetworkMonitor nm = runValidatedNetworkTest(); |
| 961 | |
Remi NGUYEN VAN | b4af130 | 2019-06-11 16:17:46 +0900 | [diff] [blame] | 962 | resetCallbacks(); |
Chiachang Wang | 813ee47 | 2019-05-23 16:29:30 +0800 | [diff] [blame] | 963 | |
| 964 | nm.reportHttpProbeResult(NETWORK_VALIDATION_PROBE_HTTP, CaptivePortalProbeResult.SUCCESS); |
| 965 | // Verify result should be appended and notifyNetworkTested callback is triggered once. |
| 966 | assertEquals(nm.getEvaluationState().getNetworkTestResult(), |
| 967 | VALIDATION_RESULT_VALID | NETWORK_VALIDATION_PROBE_HTTP); |
Chiachang Wang | 813ee47 | 2019-05-23 16:29:30 +0800 | [diff] [blame] | 968 | |
| 969 | nm.reportHttpProbeResult(NETWORK_VALIDATION_PROBE_HTTP, CaptivePortalProbeResult.FAILED); |
| 970 | // Verify DNS probe result should not be cleared. |
| 971 | assertTrue((nm.getEvaluationState().getNetworkTestResult() & NETWORK_VALIDATION_PROBE_DNS) |
| 972 | == NETWORK_VALIDATION_PROBE_DNS); |
| 973 | } |
| 974 | |
| 975 | @Test |
| 976 | public void testEvaluationState_reportEvaluationResult() throws Exception { |
| 977 | final NetworkMonitor nm = runValidatedNetworkTest(); |
| 978 | |
| 979 | nm.getEvaluationState().reportEvaluationResult(NETWORK_VALIDATION_RESULT_PARTIAL, |
| 980 | null /* redirectUrl */); |
| 981 | verify(mCallbacks, timeout(HANDLER_TIMEOUT_MS).times(1)).notifyNetworkTested( |
| 982 | eq(NETWORK_VALIDATION_PROBE_DNS | NETWORK_VALIDATION_PROBE_HTTPS |
| 983 | | NETWORK_VALIDATION_RESULT_PARTIAL), eq(null)); |
| 984 | |
| 985 | nm.getEvaluationState().reportEvaluationResult( |
| 986 | NETWORK_VALIDATION_RESULT_VALID | NETWORK_VALIDATION_RESULT_PARTIAL, |
| 987 | null /* redirectUrl */); |
| 988 | verify(mCallbacks, timeout(HANDLER_TIMEOUT_MS).times(1)).notifyNetworkTested( |
| 989 | eq(VALIDATION_RESULT_VALID | NETWORK_VALIDATION_RESULT_PARTIAL), eq(null)); |
| 990 | |
| 991 | nm.getEvaluationState().reportEvaluationResult(VALIDATION_RESULT_INVALID, |
| 992 | TEST_REDIRECT_URL); |
| 993 | verify(mCallbacks, timeout(HANDLER_TIMEOUT_MS).times(1)).notifyNetworkTested( |
| 994 | eq(NETWORK_VALIDATION_PROBE_DNS | NETWORK_VALIDATION_PROBE_HTTPS), |
| 995 | eq(TEST_REDIRECT_URL)); |
| 996 | } |
| 997 | |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 998 | private void makeDnsTimeoutEvent(WrappedNetworkMonitor wrappedMonitor, int count) { |
| 999 | for (int i = 0; i < count; i++) { |
| 1000 | wrappedMonitor.getDnsStallDetector().accumulateConsecutiveDnsTimeoutCount( |
| 1001 | RETURN_CODE_DNS_TIMEOUT); |
| 1002 | } |
| 1003 | } |
| 1004 | |
| 1005 | private void makeDnsSuccessEvent(WrappedNetworkMonitor wrappedMonitor, int count) { |
| 1006 | for (int i = 0; i < count; i++) { |
| 1007 | wrappedMonitor.getDnsStallDetector().accumulateConsecutiveDnsTimeoutCount( |
| 1008 | RETURN_CODE_DNS_SUCCESS); |
| 1009 | } |
| 1010 | } |
| 1011 | |
Chiachang Wang | 899d596 | 2019-03-08 14:34:19 +0800 | [diff] [blame] | 1012 | private DataStallDetectionStats makeEmptyDataStallDetectionStats() { |
| 1013 | return new DataStallDetectionStats.Builder().build(); |
| 1014 | } |
| 1015 | |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 1016 | private void setDataStallEvaluationType(int type) { |
Chiachang Wang | 9a87f80 | 2019-04-08 19:06:21 +0800 | [diff] [blame] | 1017 | when(mDependencies.getDeviceConfigPropertyInt(any(), |
| 1018 | eq(CONFIG_DATA_STALL_EVALUATION_TYPE), anyInt())).thenReturn(type); |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 1019 | } |
| 1020 | |
| 1021 | private void setMinDataStallEvaluateInterval(int time) { |
Chiachang Wang | 9a87f80 | 2019-04-08 19:06:21 +0800 | [diff] [blame] | 1022 | when(mDependencies.getDeviceConfigPropertyInt(any(), |
| 1023 | eq(CONFIG_DATA_STALL_MIN_EVALUATE_INTERVAL), anyInt())).thenReturn(time); |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 1024 | } |
| 1025 | |
| 1026 | private void setValidDataStallDnsTimeThreshold(int time) { |
Chiachang Wang | 9a87f80 | 2019-04-08 19:06:21 +0800 | [diff] [blame] | 1027 | when(mDependencies.getDeviceConfigPropertyInt(any(), |
| 1028 | eq(CONFIG_DATA_STALL_VALID_DNS_TIME_THRESHOLD), anyInt())).thenReturn(time); |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 1029 | } |
| 1030 | |
| 1031 | private void setConsecutiveDnsTimeoutThreshold(int num) { |
Chiachang Wang | 9a87f80 | 2019-04-08 19:06:21 +0800 | [diff] [blame] | 1032 | when(mDependencies.getDeviceConfigPropertyInt(any(), |
| 1033 | eq(CONFIG_DATA_STALL_CONSECUTIVE_DNS_TIMEOUT_THRESHOLD), anyInt())).thenReturn(num); |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 1034 | } |
| 1035 | |
| 1036 | private void setFallbackUrl(String url) { |
| 1037 | when(mDependencies.getSetting(any(), |
| 1038 | eq(Settings.Global.CAPTIVE_PORTAL_FALLBACK_URL), any())).thenReturn(url); |
| 1039 | } |
| 1040 | |
| 1041 | private void setOtherFallbackUrls(String urls) { |
Chiachang Wang | 79a6da3 | 2019-04-17 17:00:54 +0800 | [diff] [blame] | 1042 | when(mDependencies.getDeviceConfigProperty(any(), |
| 1043 | eq(CAPTIVE_PORTAL_OTHER_FALLBACK_URLS), any())).thenReturn(urls); |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 1044 | } |
| 1045 | |
| 1046 | private void setFallbackSpecs(String specs) { |
Chiachang Wang | 79a6da3 | 2019-04-17 17:00:54 +0800 | [diff] [blame] | 1047 | when(mDependencies.getDeviceConfigProperty(any(), |
| 1048 | eq(CAPTIVE_PORTAL_FALLBACK_PROBE_SPECS), any())).thenReturn(specs); |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 1049 | } |
| 1050 | |
| 1051 | private void setCaptivePortalMode(int mode) { |
| 1052 | when(mDependencies.getSetting(any(), |
| 1053 | eq(Settings.Global.CAPTIVE_PORTAL_MODE), anyInt())).thenReturn(mode); |
| 1054 | } |
| 1055 | |
Chiachang Wang | 813ee47 | 2019-05-23 16:29:30 +0800 | [diff] [blame] | 1056 | private void runPortalNetworkTest(int result) { |
Lorenzo Colitti | aa16f2d | 2019-06-07 18:42:34 +0900 | [diff] [blame] | 1057 | runNetworkTest(result); |
Lorenzo Colitti | 37ee46f | 2019-04-08 21:51:41 +0900 | [diff] [blame] | 1058 | assertEquals(1, mRegisteredReceivers.size()); |
Remi NGUYEN VAN | dc6e640 | 2019-03-27 15:42:53 +0900 | [diff] [blame] | 1059 | assertNotNull(mNetworkTestedRedirectUrlCaptor.getValue()); |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 1060 | } |
| 1061 | |
Remi NGUYEN VAN | dc6e640 | 2019-03-27 15:42:53 +0900 | [diff] [blame] | 1062 | private void runNotPortalNetworkTest() { |
Chiachang Wang | 813ee47 | 2019-05-23 16:29:30 +0800 | [diff] [blame] | 1063 | runNetworkTest(VALIDATION_RESULT_VALID); |
| 1064 | assertEquals(0, mRegisteredReceivers.size()); |
| 1065 | assertNull(mNetworkTestedRedirectUrlCaptor.getValue()); |
| 1066 | } |
| 1067 | |
| 1068 | private void runNoValidationNetworkTest() { |
| 1069 | runNetworkTest(NETWORK_VALIDATION_RESULT_VALID); |
Lorenzo Colitti | 37ee46f | 2019-04-08 21:51:41 +0900 | [diff] [blame] | 1070 | assertEquals(0, mRegisteredReceivers.size()); |
Remi NGUYEN VAN | dc6e640 | 2019-03-27 15:42:53 +0900 | [diff] [blame] | 1071 | assertNull(mNetworkTestedRedirectUrlCaptor.getValue()); |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 1072 | } |
| 1073 | |
Remi NGUYEN VAN | dc6e640 | 2019-03-27 15:42:53 +0900 | [diff] [blame] | 1074 | private void runFailedNetworkTest() { |
Chiachang Wang | 813ee47 | 2019-05-23 16:29:30 +0800 | [diff] [blame] | 1075 | runNetworkTest(VALIDATION_RESULT_INVALID); |
Lorenzo Colitti | 37ee46f | 2019-04-08 21:51:41 +0900 | [diff] [blame] | 1076 | assertEquals(0, mRegisteredReceivers.size()); |
Remi NGUYEN VAN | dc6e640 | 2019-03-27 15:42:53 +0900 | [diff] [blame] | 1077 | assertNull(mNetworkTestedRedirectUrlCaptor.getValue()); |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 1078 | } |
| 1079 | |
Chiachang Wang | 813ee47 | 2019-05-23 16:29:30 +0800 | [diff] [blame] | 1080 | private void runPartialConnectivityNetworkTest(int result) { |
| 1081 | runNetworkTest(result); |
Lorenzo Colitti | 37ee46f | 2019-04-08 21:51:41 +0900 | [diff] [blame] | 1082 | assertEquals(0, mRegisteredReceivers.size()); |
Remi NGUYEN VAN | dc6e640 | 2019-03-27 15:42:53 +0900 | [diff] [blame] | 1083 | assertNull(mNetworkTestedRedirectUrlCaptor.getValue()); |
| 1084 | } |
| 1085 | |
Chiachang Wang | 813ee47 | 2019-05-23 16:29:30 +0800 | [diff] [blame] | 1086 | private NetworkMonitor runValidatedNetworkTest() throws Exception { |
| 1087 | setStatus(mHttpsConnection, 204); |
| 1088 | setStatus(mHttpConnection, 204); |
| 1089 | // Expect to send HTTPs and evaluation results. |
| 1090 | return runNetworkTest(VALIDATION_RESULT_VALID); |
| 1091 | } |
| 1092 | |
Remi NGUYEN VAN | dc6e640 | 2019-03-27 15:42:53 +0900 | [diff] [blame] | 1093 | private NetworkMonitor runNetworkTest(int testResult) { |
Lorenzo Colitti | aa16f2d | 2019-06-07 18:42:34 +0900 | [diff] [blame] | 1094 | return runNetworkTest(METERED_CAPABILITIES, testResult); |
Remi NGUYEN VAN | dc6e640 | 2019-03-27 15:42:53 +0900 | [diff] [blame] | 1095 | } |
| 1096 | |
Lorenzo Colitti | aa16f2d | 2019-06-07 18:42:34 +0900 | [diff] [blame] | 1097 | private NetworkMonitor runNetworkTest(NetworkCapabilities nc, int testResult) { |
Lorenzo Colitti | cfc1abc | 2019-04-28 00:55:19 +0900 | [diff] [blame] | 1098 | final NetworkMonitor monitor = makeMonitor(nc); |
Remi NGUYEN VAN | dc6e640 | 2019-03-27 15:42:53 +0900 | [diff] [blame] | 1099 | monitor.notifyNetworkConnected(TEST_LINK_PROPERTIES, nc); |
| 1100 | try { |
Lorenzo Colitti | aa16f2d | 2019-06-07 18:42:34 +0900 | [diff] [blame] | 1101 | verify(mCallbacks, timeout(HANDLER_TIMEOUT_MS)) |
Remi NGUYEN VAN | dc6e640 | 2019-03-27 15:42:53 +0900 | [diff] [blame] | 1102 | .notifyNetworkTested(eq(testResult), mNetworkTestedRedirectUrlCaptor.capture()); |
| 1103 | } catch (RemoteException e) { |
| 1104 | fail("Unexpected exception: " + e); |
| 1105 | } |
Chalard Jean | 0d0cc8c | 2019-05-28 16:48:32 +0900 | [diff] [blame] | 1106 | HandlerUtilsKt.waitForIdle(monitor.getHandler(), HANDLER_TIMEOUT_MS); |
Remi NGUYEN VAN | dc6e640 | 2019-03-27 15:42:53 +0900 | [diff] [blame] | 1107 | |
| 1108 | return monitor; |
lucaslin | b057396 | 2019-03-12 13:08:03 +0800 | [diff] [blame] | 1109 | } |
| 1110 | |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 1111 | private void setSslException(HttpURLConnection connection) throws IOException { |
| 1112 | doThrow(new SSLHandshakeException("Invalid cert")).when(connection).getResponseCode(); |
| 1113 | } |
| 1114 | |
| 1115 | private void set302(HttpURLConnection connection, String location) throws IOException { |
| 1116 | setStatus(connection, 302); |
| 1117 | doReturn(location).when(connection).getHeaderField(LOCATION_HEADER); |
| 1118 | } |
| 1119 | |
| 1120 | private void setPortal302(HttpURLConnection connection) throws IOException { |
| 1121 | set302(connection, "http://login.example.com"); |
| 1122 | } |
| 1123 | |
| 1124 | private void setStatus(HttpURLConnection connection, int status) throws IOException { |
| 1125 | doReturn(status).when(connection).getResponseCode(); |
| 1126 | } |
Chiachang Wang | caa3520 | 2019-02-26 11:32:18 +0800 | [diff] [blame] | 1127 | |
| 1128 | private void generateTimeoutDnsEvent(DataStallDetectionStats.Builder stats, int num) { |
| 1129 | for (int i = 0; i < num; i++) { |
| 1130 | stats.addDnsEvent(RETURN_CODE_DNS_TIMEOUT, 123456789 /* timeMs */); |
| 1131 | } |
| 1132 | } |
Chiachang Wang | 813ee47 | 2019-05-23 16:29:30 +0800 | [diff] [blame] | 1133 | |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 1134 | } |
| 1135 | |