Hugo Benichi | c894b12 | 2017-07-31 12:58:20 +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 | cfff01e | 2019-02-13 20:58:59 +0900 | [diff] [blame] | 19 | import static android.net.CaptivePortal.APP_RETURN_DISMISSED; |
Remi NGUYEN VAN | e67b0c3 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 20 | import static android.net.INetworkMonitor.NETWORK_TEST_RESULT_INVALID; |
| 21 | import static android.net.INetworkMonitor.NETWORK_TEST_RESULT_VALID; |
| 22 | import static android.net.NetworkCapabilities.NET_CAPABILITY_INTERNET; |
Chiachang Wang | 4349dc0 | 2019-03-05 20:31:57 +0800 | [diff] [blame] | 23 | import static android.provider.Settings.Global.DATA_STALL_EVALUATION_TYPE_DNS; |
Remi NGUYEN VAN | e67b0c3 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 24 | |
| 25 | import static junit.framework.Assert.assertEquals; |
Remi NGUYEN VAN | d9a1cd7 | 2018-05-22 13:11:15 +0900 | [diff] [blame] | 26 | import static junit.framework.Assert.assertFalse; |
| 27 | |
| 28 | import static org.junit.Assert.assertTrue; |
Hugo Benichi | c894b12 | 2017-07-31 12:58:20 +0900 | [diff] [blame] | 29 | import static org.junit.Assert.fail; |
Remi NGUYEN VAN | d9a1cd7 | 2018-05-22 13:11:15 +0900 | [diff] [blame] | 30 | import static org.mockito.ArgumentMatchers.anyString; |
| 31 | import static org.mockito.ArgumentMatchers.eq; |
Hugo Benichi | c894b12 | 2017-07-31 12:58:20 +0900 | [diff] [blame] | 32 | import static org.mockito.Mockito.any; |
| 33 | import static org.mockito.Mockito.anyInt; |
Remi NGUYEN VAN | e67b0c3 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 34 | import static org.mockito.Mockito.doAnswer; |
| 35 | import static org.mockito.Mockito.doReturn; |
| 36 | import static org.mockito.Mockito.doThrow; |
Remi NGUYEN VAN | d9a1cd7 | 2018-05-22 13:11:15 +0900 | [diff] [blame] | 37 | import static org.mockito.Mockito.never; |
Remi NGUYEN VAN | e67b0c3 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 38 | import static org.mockito.Mockito.timeout; |
Remi NGUYEN VAN | d9a1cd7 | 2018-05-22 13:11:15 +0900 | [diff] [blame] | 39 | import static org.mockito.Mockito.times; |
Hugo Benichi | c894b12 | 2017-07-31 12:58:20 +0900 | [diff] [blame] | 40 | import static org.mockito.Mockito.verify; |
| 41 | import static org.mockito.Mockito.when; |
| 42 | |
Chiachang Wang | 95489ca | 2019-02-26 11:32:18 +0800 | [diff] [blame] | 43 | import android.annotation.NonNull; |
Hugo Benichi | c894b12 | 2017-07-31 12:58:20 +0900 | [diff] [blame] | 44 | import android.content.Context; |
Remi NGUYEN VAN | d9a1cd7 | 2018-05-22 13:11:15 +0900 | [diff] [blame] | 45 | import android.net.ConnectivityManager; |
Remi NGUYEN VAN | e67b0c3 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 46 | import android.net.INetworkMonitorCallbacks; |
| 47 | import android.net.InetAddresses; |
Remi NGUYEN VAN | d9a1cd7 | 2018-05-22 13:11:15 +0900 | [diff] [blame] | 48 | import android.net.LinkProperties; |
Hugo Benichi | c894b12 | 2017-07-31 12:58:20 +0900 | [diff] [blame] | 49 | import android.net.Network; |
Remi NGUYEN VAN | d9a1cd7 | 2018-05-22 13:11:15 +0900 | [diff] [blame] | 50 | import android.net.NetworkCapabilities; |
| 51 | import android.net.NetworkInfo; |
Remi NGUYEN VAN | d9a1cd7 | 2018-05-22 13:11:15 +0900 | [diff] [blame] | 52 | import android.net.captiveportal.CaptivePortalProbeResult; |
Chiachang Wang | 95489ca | 2019-02-26 11:32:18 +0800 | [diff] [blame] | 53 | import android.net.metrics.DataStallDetectionStats; |
Chiachang Wang | f09e3e3 | 2019-02-22 11:13:07 +0800 | [diff] [blame] | 54 | import android.net.metrics.DataStallStatsUtils; |
Hugo Benichi | c894b12 | 2017-07-31 12:58:20 +0900 | [diff] [blame] | 55 | import android.net.metrics.IpConnectivityLog; |
Remi NGUYEN VAN | e67b0c3 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 56 | import android.net.util.SharedLog; |
Chiachang Wang | 95489ca | 2019-02-26 11:32:18 +0800 | [diff] [blame] | 57 | import android.net.wifi.WifiInfo; |
Hugo Benichi | c894b12 | 2017-07-31 12:58:20 +0900 | [diff] [blame] | 58 | import android.net.wifi.WifiManager; |
Remi NGUYEN VAN | cfff01e | 2019-02-13 20:58:59 +0900 | [diff] [blame] | 59 | import android.os.Bundle; |
Remi NGUYEN VAN | e67b0c3 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 60 | import android.os.ConditionVariable; |
Hugo Benichi | c894b12 | 2017-07-31 12:58:20 +0900 | [diff] [blame] | 61 | import android.os.Handler; |
Chiachang Wang | 7a70a7e | 2018-11-27 18:00:05 +0800 | [diff] [blame] | 62 | import android.os.SystemClock; |
Remi NGUYEN VAN | d9a1cd7 | 2018-05-22 13:11:15 +0900 | [diff] [blame] | 63 | import android.provider.Settings; |
Chiachang Wang | 95489ca | 2019-02-26 11:32:18 +0800 | [diff] [blame] | 64 | import android.telephony.CellSignalStrength; |
Hugo Benichi | c894b12 | 2017-07-31 12:58:20 +0900 | [diff] [blame] | 65 | import android.telephony.TelephonyManager; |
Remi NGUYEN VAN | d9a1cd7 | 2018-05-22 13:11:15 +0900 | [diff] [blame] | 66 | import android.util.ArrayMap; |
Hugo Benichi | c894b12 | 2017-07-31 12:58:20 +0900 | [diff] [blame] | 67 | |
Brett Chabot | 8091d9e | 2019-02-26 14:52:33 -0800 | [diff] [blame] | 68 | import androidx.test.filters.SmallTest; |
| 69 | import androidx.test.runner.AndroidJUnit4; |
| 70 | |
Hugo Benichi | c894b12 | 2017-07-31 12:58:20 +0900 | [diff] [blame] | 71 | import org.junit.Before; |
| 72 | import org.junit.Test; |
| 73 | import org.junit.runner.RunWith; |
Remi NGUYEN VAN | e67b0c3 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 74 | import org.mockito.ArgumentCaptor; |
Hugo Benichi | c894b12 | 2017-07-31 12:58:20 +0900 | [diff] [blame] | 75 | import org.mockito.Mock; |
| 76 | import org.mockito.MockitoAnnotations; |
Remi NGUYEN VAN | e67b0c3 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 77 | import org.mockito.Spy; |
Hugo Benichi | c894b12 | 2017-07-31 12:58:20 +0900 | [diff] [blame] | 78 | |
Remi NGUYEN VAN | d9a1cd7 | 2018-05-22 13:11:15 +0900 | [diff] [blame] | 79 | import java.io.IOException; |
| 80 | import java.net.HttpURLConnection; |
| 81 | import java.net.InetAddress; |
| 82 | import java.net.URL; |
| 83 | import java.util.Random; |
| 84 | |
| 85 | import javax.net.ssl.SSLHandshakeException; |
| 86 | |
Hugo Benichi | c894b12 | 2017-07-31 12:58:20 +0900 | [diff] [blame] | 87 | @RunWith(AndroidJUnit4.class) |
| 88 | @SmallTest |
| 89 | public class NetworkMonitorTest { |
Remi NGUYEN VAN | d9a1cd7 | 2018-05-22 13:11:15 +0900 | [diff] [blame] | 90 | private static final String LOCATION_HEADER = "location"; |
Hugo Benichi | c894b12 | 2017-07-31 12:58:20 +0900 | [diff] [blame] | 91 | |
Remi NGUYEN VAN | d9a1cd7 | 2018-05-22 13:11:15 +0900 | [diff] [blame] | 92 | private @Mock Context mContext; |
Remi NGUYEN VAN | d9a1cd7 | 2018-05-22 13:11:15 +0900 | [diff] [blame] | 93 | private @Mock IpConnectivityLog mLogger; |
Remi NGUYEN VAN | e67b0c3 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 94 | private @Mock SharedLog mValidationLogger; |
Remi NGUYEN VAN | d9a1cd7 | 2018-05-22 13:11:15 +0900 | [diff] [blame] | 95 | private @Mock NetworkInfo mNetworkInfo; |
Remi NGUYEN VAN | e67b0c3 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 96 | private @Mock ConnectivityManager mCm; |
Remi NGUYEN VAN | d9a1cd7 | 2018-05-22 13:11:15 +0900 | [diff] [blame] | 97 | private @Mock TelephonyManager mTelephony; |
| 98 | private @Mock WifiManager mWifi; |
Remi NGUYEN VAN | d9a1cd7 | 2018-05-22 13:11:15 +0900 | [diff] [blame] | 99 | private @Mock HttpURLConnection mHttpConnection; |
| 100 | private @Mock HttpURLConnection mHttpsConnection; |
| 101 | private @Mock HttpURLConnection mFallbackConnection; |
| 102 | private @Mock HttpURLConnection mOtherFallbackConnection; |
| 103 | private @Mock Random mRandom; |
| 104 | private @Mock NetworkMonitor.Dependencies mDependencies; |
Remi NGUYEN VAN | e67b0c3 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 105 | private @Mock INetworkMonitorCallbacks mCallbacks; |
| 106 | private @Spy Network mNetwork = new Network(TEST_NETID); |
Chiachang Wang | f09e3e3 | 2019-02-22 11:13:07 +0800 | [diff] [blame] | 107 | private @Mock DataStallStatsUtils mDataStallStatsUtils; |
Chiachang Wang | 95489ca | 2019-02-26 11:32:18 +0800 | [diff] [blame] | 108 | private @Mock WifiInfo mWifiInfo; |
Remi NGUYEN VAN | e67b0c3 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 109 | |
| 110 | private static final int TEST_NETID = 4242; |
Hugo Benichi | c894b12 | 2017-07-31 12:58:20 +0900 | [diff] [blame] | 111 | |
Remi NGUYEN VAN | d9a1cd7 | 2018-05-22 13:11:15 +0900 | [diff] [blame] | 112 | private static final String TEST_HTTP_URL = "http://www.google.com/gen_204"; |
| 113 | private static final String TEST_HTTPS_URL = "https://www.google.com/gen_204"; |
| 114 | private static final String TEST_FALLBACK_URL = "http://fallback.google.com/gen_204"; |
| 115 | private static final String TEST_OTHER_FALLBACK_URL = "http://otherfallback.google.com/gen_204"; |
Chiachang Wang | 95489ca | 2019-02-26 11:32:18 +0800 | [diff] [blame] | 116 | private static final String TEST_MCCMNC = "123456"; |
Hugo Benichi | c894b12 | 2017-07-31 12:58:20 +0900 | [diff] [blame] | 117 | |
Chiachang Wang | 7a70a7e | 2018-11-27 18:00:05 +0800 | [diff] [blame] | 118 | private static final int RETURN_CODE_DNS_SUCCESS = 0; |
| 119 | private static final int RETURN_CODE_DNS_TIMEOUT = 255; |
Chiachang Wang | 95489ca | 2019-02-26 11:32:18 +0800 | [diff] [blame] | 120 | private static final int DEFAULT_DNS_TIMEOUT_THRESHOLD = 5; |
Chiachang Wang | 7a70a7e | 2018-11-27 18:00:05 +0800 | [diff] [blame] | 121 | |
Remi NGUYEN VAN | e67b0c3 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 122 | private static final int HANDLER_TIMEOUT_MS = 1000; |
| 123 | |
| 124 | private static final LinkProperties TEST_LINKPROPERTIES = new LinkProperties(); |
| 125 | |
| 126 | private static final NetworkCapabilities METERED_CAPABILITIES = new NetworkCapabilities() |
| 127 | .addTransportType(NetworkCapabilities.TRANSPORT_CELLULAR) |
| 128 | .addCapability(NET_CAPABILITY_INTERNET); |
| 129 | |
| 130 | private static final NetworkCapabilities NOT_METERED_CAPABILITIES = new NetworkCapabilities() |
| 131 | .addTransportType(NetworkCapabilities.TRANSPORT_CELLULAR) |
| 132 | .addCapability(NET_CAPABILITY_INTERNET) |
| 133 | .addCapability(NetworkCapabilities.NET_CAPABILITY_NOT_METERED); |
| 134 | |
| 135 | private static final NetworkCapabilities NO_INTERNET_CAPABILITIES = new NetworkCapabilities() |
| 136 | .addTransportType(NetworkCapabilities.TRANSPORT_CELLULAR); |
| 137 | |
Hugo Benichi | c894b12 | 2017-07-31 12:58:20 +0900 | [diff] [blame] | 138 | @Before |
Remi NGUYEN VAN | d9a1cd7 | 2018-05-22 13:11:15 +0900 | [diff] [blame] | 139 | public void setUp() throws IOException { |
Hugo Benichi | c894b12 | 2017-07-31 12:58:20 +0900 | [diff] [blame] | 140 | MockitoAnnotations.initMocks(this); |
Remi NGUYEN VAN | e67b0c3 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 141 | when(mDependencies.getPrivateDnsBypassNetwork(any())).thenReturn(mNetwork); |
Remi NGUYEN VAN | d9a1cd7 | 2018-05-22 13:11:15 +0900 | [diff] [blame] | 142 | when(mDependencies.getRandom()).thenReturn(mRandom); |
| 143 | when(mDependencies.getSetting(any(), eq(Settings.Global.CAPTIVE_PORTAL_MODE), anyInt())) |
| 144 | .thenReturn(Settings.Global.CAPTIVE_PORTAL_MODE_PROMPT); |
| 145 | when(mDependencies.getSetting(any(), eq(Settings.Global.CAPTIVE_PORTAL_USE_HTTPS), |
| 146 | anyInt())).thenReturn(1); |
Remi NGUYEN VAN | e67b0c3 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 147 | when(mDependencies.getCaptivePortalServerHttpUrl(any())).thenReturn(TEST_HTTP_URL); |
Remi NGUYEN VAN | d9a1cd7 | 2018-05-22 13:11:15 +0900 | [diff] [blame] | 148 | when(mDependencies.getSetting(any(), eq(Settings.Global.CAPTIVE_PORTAL_HTTPS_URL), |
| 149 | anyString())).thenReturn(TEST_HTTPS_URL); |
Remi NGUYEN VAN | e67b0c3 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 150 | doReturn(mNetwork).when(mNetwork).getPrivateDnsBypassingCopy(); |
Remi NGUYEN VAN | d9a1cd7 | 2018-05-22 13:11:15 +0900 | [diff] [blame] | 151 | |
Remi NGUYEN VAN | e67b0c3 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 152 | when(mContext.getSystemService(Context.CONNECTIVITY_SERVICE)).thenReturn(mCm); |
Hugo Benichi | c894b12 | 2017-07-31 12:58:20 +0900 | [diff] [blame] | 153 | when(mContext.getSystemService(Context.TELEPHONY_SERVICE)).thenReturn(mTelephony); |
| 154 | when(mContext.getSystemService(Context.WIFI_SERVICE)).thenReturn(mWifi); |
Remi NGUYEN VAN | d9a1cd7 | 2018-05-22 13:11:15 +0900 | [diff] [blame] | 155 | |
| 156 | when(mNetworkInfo.getType()).thenReturn(ConnectivityManager.TYPE_WIFI); |
| 157 | setFallbackUrl(TEST_FALLBACK_URL); |
| 158 | setOtherFallbackUrls(TEST_OTHER_FALLBACK_URL); |
| 159 | setFallbackSpecs(null); // Test with no fallback spec by default |
| 160 | when(mRandom.nextInt()).thenReturn(0); |
| 161 | |
Remi NGUYEN VAN | e67b0c3 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 162 | doAnswer((invocation) -> { |
Remi NGUYEN VAN | d9a1cd7 | 2018-05-22 13:11:15 +0900 | [diff] [blame] | 163 | URL url = invocation.getArgument(0); |
| 164 | switch(url.toString()) { |
| 165 | case TEST_HTTP_URL: |
| 166 | return mHttpConnection; |
| 167 | case TEST_HTTPS_URL: |
| 168 | return mHttpsConnection; |
| 169 | case TEST_FALLBACK_URL: |
| 170 | return mFallbackConnection; |
| 171 | case TEST_OTHER_FALLBACK_URL: |
| 172 | return mOtherFallbackConnection; |
| 173 | default: |
| 174 | fail("URL not mocked: " + url.toString()); |
| 175 | return null; |
| 176 | } |
Remi NGUYEN VAN | e67b0c3 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 177 | }).when(mNetwork).openConnection(any()); |
Remi NGUYEN VAN | d9a1cd7 | 2018-05-22 13:11:15 +0900 | [diff] [blame] | 178 | when(mHttpConnection.getRequestProperties()).thenReturn(new ArrayMap<>()); |
| 179 | when(mHttpsConnection.getRequestProperties()).thenReturn(new ArrayMap<>()); |
Remi NGUYEN VAN | e67b0c3 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 180 | doReturn(new InetAddress[] { |
| 181 | InetAddresses.parseNumericAddress("192.168.0.0") |
| 182 | }).when(mNetwork).getAllByName(any()); |
| 183 | |
Remi NGUYEN VAN | e67b0c3 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 184 | // Default values. Individual tests can override these. |
| 185 | when(mCm.getLinkProperties(any())).thenReturn(TEST_LINKPROPERTIES); |
| 186 | when(mCm.getNetworkCapabilities(any())).thenReturn(METERED_CAPABILITIES); |
Chiachang Wang | 7a70a7e | 2018-11-27 18:00:05 +0800 | [diff] [blame] | 187 | |
| 188 | setMinDataStallEvaluateInterval(500); |
Chiachang Wang | 4349dc0 | 2019-03-05 20:31:57 +0800 | [diff] [blame] | 189 | setDataStallEvaluationType(DATA_STALL_EVALUATION_TYPE_DNS); |
Chiachang Wang | 7a70a7e | 2018-11-27 18:00:05 +0800 | [diff] [blame] | 190 | setValidDataStallDnsTimeThreshold(500); |
| 191 | setConsecutiveDnsTimeoutThreshold(5); |
| 192 | } |
| 193 | |
| 194 | private class WrappedNetworkMonitor extends NetworkMonitor { |
| 195 | private long mProbeTime = 0; |
| 196 | |
Remi NGUYEN VAN | 231b52b | 2019-01-29 15:38:52 +0900 | [diff] [blame] | 197 | WrappedNetworkMonitor(Context context, Network network, IpConnectivityLog logger, |
Chiachang Wang | f09e3e3 | 2019-02-22 11:13:07 +0800 | [diff] [blame] | 198 | Dependencies deps, DataStallStatsUtils statsUtils) { |
Remi NGUYEN VAN | 231b52b | 2019-01-29 15:38:52 +0900 | [diff] [blame] | 199 | super(context, mCallbacks, network, logger, |
Chiachang Wang | f09e3e3 | 2019-02-22 11:13:07 +0800 | [diff] [blame] | 200 | new SharedLog("test_nm"), deps, statsUtils); |
Chiachang Wang | 7a70a7e | 2018-11-27 18:00:05 +0800 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | @Override |
| 204 | protected long getLastProbeTime() { |
| 205 | return mProbeTime; |
| 206 | } |
| 207 | |
| 208 | protected void setLastProbeTime(long time) { |
| 209 | mProbeTime = time; |
| 210 | } |
Chiachang Wang | 95489ca | 2019-02-26 11:32:18 +0800 | [diff] [blame] | 211 | |
| 212 | @Override |
| 213 | protected void addDnsEvents(@NonNull final DataStallDetectionStats.Builder stats) { |
| 214 | generateTimeoutDnsEvent(stats, DEFAULT_DNS_TIMEOUT_THRESHOLD); |
| 215 | } |
Chiachang Wang | 7a70a7e | 2018-11-27 18:00:05 +0800 | [diff] [blame] | 216 | } |
| 217 | |
Remi NGUYEN VAN | e67b0c3 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 218 | private WrappedNetworkMonitor makeMeteredWrappedNetworkMonitor() { |
| 219 | final WrappedNetworkMonitor nm = new WrappedNetworkMonitor( |
Chiachang Wang | f09e3e3 | 2019-02-22 11:13:07 +0800 | [diff] [blame] | 220 | mContext, mNetwork, mLogger, mDependencies, mDataStallStatsUtils); |
Remi NGUYEN VAN | e67b0c3 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 221 | when(mCm.getNetworkCapabilities(any())).thenReturn(METERED_CAPABILITIES); |
| 222 | nm.start(); |
| 223 | waitForIdle(nm.getHandler()); |
| 224 | return nm; |
Chiachang Wang | 7a70a7e | 2018-11-27 18:00:05 +0800 | [diff] [blame] | 225 | } |
| 226 | |
Remi NGUYEN VAN | e67b0c3 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 227 | private WrappedNetworkMonitor makeNotMeteredWrappedNetworkMonitor() { |
| 228 | final WrappedNetworkMonitor nm = new WrappedNetworkMonitor( |
Chiachang Wang | f09e3e3 | 2019-02-22 11:13:07 +0800 | [diff] [blame] | 229 | mContext, mNetwork, mLogger, mDependencies, mDataStallStatsUtils); |
Remi NGUYEN VAN | e67b0c3 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 230 | when(mCm.getNetworkCapabilities(any())).thenReturn(NOT_METERED_CAPABILITIES); |
| 231 | nm.start(); |
| 232 | waitForIdle(nm.getHandler()); |
| 233 | return nm; |
Hugo Benichi | c894b12 | 2017-07-31 12:58:20 +0900 | [diff] [blame] | 234 | } |
| 235 | |
Remi NGUYEN VAN | e67b0c3 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 236 | private NetworkMonitor makeMonitor() { |
| 237 | final NetworkMonitor nm = new NetworkMonitor( |
Remi NGUYEN VAN | 231b52b | 2019-01-29 15:38:52 +0900 | [diff] [blame] | 238 | mContext, mCallbacks, mNetwork, mLogger, mValidationLogger, |
Chiachang Wang | f09e3e3 | 2019-02-22 11:13:07 +0800 | [diff] [blame] | 239 | mDependencies, mDataStallStatsUtils); |
Remi NGUYEN VAN | e67b0c3 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 240 | nm.start(); |
| 241 | waitForIdle(nm.getHandler()); |
| 242 | return nm; |
| 243 | } |
| 244 | |
| 245 | private void waitForIdle(Handler handler) { |
| 246 | final ConditionVariable cv = new ConditionVariable(false); |
| 247 | handler.post(cv::open); |
| 248 | if (!cv.block(HANDLER_TIMEOUT_MS)) { |
| 249 | fail("Timed out waiting for handler"); |
| 250 | } |
Hugo Benichi | c894b12 | 2017-07-31 12:58:20 +0900 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | @Test |
Remi NGUYEN VAN | d9a1cd7 | 2018-05-22 13:11:15 +0900 | [diff] [blame] | 254 | public void testIsCaptivePortal_HttpProbeIsPortal() throws IOException { |
| 255 | setSslException(mHttpsConnection); |
| 256 | setPortal302(mHttpConnection); |
| 257 | |
| 258 | assertPortal(makeMonitor().isCaptivePortal()); |
| 259 | } |
| 260 | |
| 261 | @Test |
| 262 | public void testIsCaptivePortal_HttpsProbeIsNotPortal() throws IOException { |
| 263 | setStatus(mHttpsConnection, 204); |
| 264 | setStatus(mHttpConnection, 500); |
| 265 | |
| 266 | assertNotPortal(makeMonitor().isCaptivePortal()); |
| 267 | } |
| 268 | |
| 269 | @Test |
| 270 | public void testIsCaptivePortal_HttpsProbeFailedHttpSuccessNotUsed() throws IOException { |
| 271 | setSslException(mHttpsConnection); |
| 272 | // Even if HTTP returns a 204, do not use the result unless HTTPS succeeded |
| 273 | setStatus(mHttpConnection, 204); |
| 274 | setStatus(mFallbackConnection, 500); |
| 275 | |
| 276 | assertFailed(makeMonitor().isCaptivePortal()); |
| 277 | } |
| 278 | |
| 279 | @Test |
| 280 | public void testIsCaptivePortal_FallbackProbeIsPortal() throws IOException { |
| 281 | setSslException(mHttpsConnection); |
| 282 | setStatus(mHttpConnection, 500); |
| 283 | setPortal302(mFallbackConnection); |
| 284 | |
| 285 | assertPortal(makeMonitor().isCaptivePortal()); |
| 286 | } |
| 287 | |
| 288 | @Test |
| 289 | public void testIsCaptivePortal_FallbackProbeIsNotPortal() throws IOException { |
| 290 | setSslException(mHttpsConnection); |
| 291 | setStatus(mHttpConnection, 500); |
| 292 | setStatus(mFallbackConnection, 204); |
| 293 | |
| 294 | // Fallback probe did not see portal, HTTPS failed -> inconclusive |
| 295 | assertFailed(makeMonitor().isCaptivePortal()); |
| 296 | } |
| 297 | |
| 298 | @Test |
| 299 | public void testIsCaptivePortal_OtherFallbackProbeIsPortal() throws IOException { |
| 300 | // Set all fallback probes but one to invalid URLs to verify they are being skipped |
| 301 | setFallbackUrl(TEST_FALLBACK_URL); |
| 302 | setOtherFallbackUrls(TEST_FALLBACK_URL + "," + TEST_OTHER_FALLBACK_URL); |
| 303 | |
| 304 | setSslException(mHttpsConnection); |
| 305 | setStatus(mHttpConnection, 500); |
| 306 | setStatus(mFallbackConnection, 500); |
| 307 | setPortal302(mOtherFallbackConnection); |
| 308 | |
| 309 | // TEST_OTHER_FALLBACK_URL is third |
| 310 | when(mRandom.nextInt()).thenReturn(2); |
| 311 | |
| 312 | final NetworkMonitor monitor = makeMonitor(); |
| 313 | |
| 314 | // First check always uses the first fallback URL: inconclusive |
| 315 | assertFailed(monitor.isCaptivePortal()); |
| 316 | verify(mFallbackConnection, times(1)).getResponseCode(); |
| 317 | verify(mOtherFallbackConnection, never()).getResponseCode(); |
| 318 | |
| 319 | // Second check uses the URL chosen by Random |
| 320 | assertPortal(monitor.isCaptivePortal()); |
| 321 | verify(mOtherFallbackConnection, times(1)).getResponseCode(); |
| 322 | } |
| 323 | |
| 324 | @Test |
| 325 | public void testIsCaptivePortal_AllProbesFailed() throws IOException { |
| 326 | setSslException(mHttpsConnection); |
| 327 | setStatus(mHttpConnection, 500); |
| 328 | setStatus(mFallbackConnection, 404); |
| 329 | |
| 330 | assertFailed(makeMonitor().isCaptivePortal()); |
| 331 | verify(mFallbackConnection, times(1)).getResponseCode(); |
| 332 | verify(mOtherFallbackConnection, never()).getResponseCode(); |
| 333 | } |
| 334 | |
| 335 | @Test |
| 336 | public void testIsCaptivePortal_InvalidUrlSkipped() throws IOException { |
| 337 | setFallbackUrl("invalid"); |
| 338 | setOtherFallbackUrls("otherinvalid," + TEST_OTHER_FALLBACK_URL + ",yetanotherinvalid"); |
| 339 | |
| 340 | setSslException(mHttpsConnection); |
| 341 | setStatus(mHttpConnection, 500); |
| 342 | setPortal302(mOtherFallbackConnection); |
| 343 | |
| 344 | assertPortal(makeMonitor().isCaptivePortal()); |
| 345 | verify(mOtherFallbackConnection, times(1)).getResponseCode(); |
| 346 | verify(mFallbackConnection, never()).getResponseCode(); |
| 347 | } |
| 348 | |
| 349 | private void setupFallbackSpec() throws IOException { |
| 350 | setFallbackSpecs("http://example.com@@/@@204@@/@@" |
| 351 | + "@@,@@" |
| 352 | + TEST_OTHER_FALLBACK_URL + "@@/@@30[12]@@/@@https://(www\\.)?google.com/?.*"); |
| 353 | |
| 354 | setSslException(mHttpsConnection); |
| 355 | setStatus(mHttpConnection, 500); |
| 356 | |
| 357 | // Use the 2nd fallback spec |
| 358 | when(mRandom.nextInt()).thenReturn(1); |
| 359 | } |
| 360 | |
| 361 | @Test |
| 362 | public void testIsCaptivePortal_FallbackSpecIsNotPortal() throws IOException { |
| 363 | setupFallbackSpec(); |
| 364 | set302(mOtherFallbackConnection, "https://www.google.com/test?q=3"); |
| 365 | |
| 366 | // HTTPS failed, fallback spec did not see a portal -> inconclusive |
| 367 | assertFailed(makeMonitor().isCaptivePortal()); |
| 368 | verify(mOtherFallbackConnection, times(1)).getResponseCode(); |
| 369 | verify(mFallbackConnection, never()).getResponseCode(); |
| 370 | } |
| 371 | |
| 372 | @Test |
| 373 | public void testIsCaptivePortal_FallbackSpecIsPortal() throws IOException { |
| 374 | setupFallbackSpec(); |
| 375 | set302(mOtherFallbackConnection, "http://login.portal.example.com"); |
| 376 | |
| 377 | assertPortal(makeMonitor().isCaptivePortal()); |
| 378 | } |
| 379 | |
Chiachang Wang | 7a70a7e | 2018-11-27 18:00:05 +0800 | [diff] [blame] | 380 | @Test |
Remi NGUYEN VAN | e67b0c3 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 381 | public void testIsCaptivePortal_IgnorePortals() throws IOException { |
| 382 | setCaptivePortalMode(Settings.Global.CAPTIVE_PORTAL_MODE_IGNORE); |
| 383 | setSslException(mHttpsConnection); |
| 384 | setPortal302(mHttpConnection); |
| 385 | |
| 386 | assertNotPortal(makeMonitor().isCaptivePortal()); |
| 387 | } |
| 388 | |
| 389 | @Test |
Chiachang Wang | 7a70a7e | 2018-11-27 18:00:05 +0800 | [diff] [blame] | 390 | public void testIsDataStall_EvaluationDisabled() { |
| 391 | setDataStallEvaluationType(0); |
| 392 | WrappedNetworkMonitor wrappedMonitor = makeMeteredWrappedNetworkMonitor(); |
| 393 | wrappedMonitor.setLastProbeTime(SystemClock.elapsedRealtime() - 100); |
| 394 | assertFalse(wrappedMonitor.isDataStall()); |
| 395 | } |
| 396 | |
| 397 | @Test |
| 398 | public void testIsDataStall_EvaluationDnsOnNotMeteredNetwork() { |
| 399 | WrappedNetworkMonitor wrappedMonitor = makeNotMeteredWrappedNetworkMonitor(); |
| 400 | wrappedMonitor.setLastProbeTime(SystemClock.elapsedRealtime() - 100); |
Chiachang Wang | 95489ca | 2019-02-26 11:32:18 +0800 | [diff] [blame] | 401 | makeDnsTimeoutEvent(wrappedMonitor, DEFAULT_DNS_TIMEOUT_THRESHOLD); |
Chiachang Wang | 7a70a7e | 2018-11-27 18:00:05 +0800 | [diff] [blame] | 402 | assertTrue(wrappedMonitor.isDataStall()); |
| 403 | } |
| 404 | |
| 405 | @Test |
| 406 | public void testIsDataStall_EvaluationDnsOnMeteredNetwork() { |
| 407 | WrappedNetworkMonitor wrappedMonitor = makeMeteredWrappedNetworkMonitor(); |
| 408 | wrappedMonitor.setLastProbeTime(SystemClock.elapsedRealtime() - 100); |
| 409 | assertFalse(wrappedMonitor.isDataStall()); |
| 410 | |
| 411 | wrappedMonitor.setLastProbeTime(SystemClock.elapsedRealtime() - 1000); |
Chiachang Wang | 95489ca | 2019-02-26 11:32:18 +0800 | [diff] [blame] | 412 | makeDnsTimeoutEvent(wrappedMonitor, DEFAULT_DNS_TIMEOUT_THRESHOLD); |
Chiachang Wang | 7a70a7e | 2018-11-27 18:00:05 +0800 | [diff] [blame] | 413 | assertTrue(wrappedMonitor.isDataStall()); |
| 414 | } |
| 415 | |
| 416 | @Test |
| 417 | public void testIsDataStall_EvaluationDnsWithDnsTimeoutCount() { |
| 418 | WrappedNetworkMonitor wrappedMonitor = makeMeteredWrappedNetworkMonitor(); |
| 419 | wrappedMonitor.setLastProbeTime(SystemClock.elapsedRealtime() - 1000); |
| 420 | makeDnsTimeoutEvent(wrappedMonitor, 3); |
| 421 | assertFalse(wrappedMonitor.isDataStall()); |
| 422 | // Reset consecutive timeout counts. |
| 423 | makeDnsSuccessEvent(wrappedMonitor, 1); |
| 424 | makeDnsTimeoutEvent(wrappedMonitor, 2); |
| 425 | assertFalse(wrappedMonitor.isDataStall()); |
| 426 | |
| 427 | makeDnsTimeoutEvent(wrappedMonitor, 3); |
| 428 | assertTrue(wrappedMonitor.isDataStall()); |
| 429 | |
| 430 | // Set the value to larger than the default dns log size. |
| 431 | setConsecutiveDnsTimeoutThreshold(51); |
| 432 | wrappedMonitor = makeMeteredWrappedNetworkMonitor(); |
| 433 | wrappedMonitor.setLastProbeTime(SystemClock.elapsedRealtime() - 1000); |
| 434 | makeDnsTimeoutEvent(wrappedMonitor, 50); |
| 435 | assertFalse(wrappedMonitor.isDataStall()); |
| 436 | |
| 437 | makeDnsTimeoutEvent(wrappedMonitor, 1); |
| 438 | assertTrue(wrappedMonitor.isDataStall()); |
| 439 | } |
| 440 | |
| 441 | @Test |
| 442 | public void testIsDataStall_EvaluationDnsWithDnsTimeThreshold() { |
| 443 | // Test dns events happened in valid dns time threshold. |
| 444 | WrappedNetworkMonitor wrappedMonitor = makeMeteredWrappedNetworkMonitor(); |
| 445 | wrappedMonitor.setLastProbeTime(SystemClock.elapsedRealtime() - 100); |
Chiachang Wang | 95489ca | 2019-02-26 11:32:18 +0800 | [diff] [blame] | 446 | makeDnsTimeoutEvent(wrappedMonitor, DEFAULT_DNS_TIMEOUT_THRESHOLD); |
Chiachang Wang | 7a70a7e | 2018-11-27 18:00:05 +0800 | [diff] [blame] | 447 | assertFalse(wrappedMonitor.isDataStall()); |
| 448 | wrappedMonitor.setLastProbeTime(SystemClock.elapsedRealtime() - 1000); |
| 449 | assertTrue(wrappedMonitor.isDataStall()); |
| 450 | |
| 451 | // Test dns events happened before valid dns time threshold. |
| 452 | setValidDataStallDnsTimeThreshold(0); |
| 453 | wrappedMonitor = makeMeteredWrappedNetworkMonitor(); |
| 454 | wrappedMonitor.setLastProbeTime(SystemClock.elapsedRealtime() - 100); |
Chiachang Wang | 95489ca | 2019-02-26 11:32:18 +0800 | [diff] [blame] | 455 | makeDnsTimeoutEvent(wrappedMonitor, DEFAULT_DNS_TIMEOUT_THRESHOLD); |
Chiachang Wang | 7a70a7e | 2018-11-27 18:00:05 +0800 | [diff] [blame] | 456 | assertFalse(wrappedMonitor.isDataStall()); |
| 457 | wrappedMonitor.setLastProbeTime(SystemClock.elapsedRealtime() - 1000); |
| 458 | assertFalse(wrappedMonitor.isDataStall()); |
| 459 | } |
| 460 | |
Remi NGUYEN VAN | e67b0c3 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 461 | @Test |
| 462 | public void testBrokenNetworkNotValidated() throws Exception { |
| 463 | setSslException(mHttpsConnection); |
| 464 | setStatus(mHttpConnection, 500); |
| 465 | setStatus(mFallbackConnection, 404); |
| 466 | when(mCm.getNetworkCapabilities(any())).thenReturn(METERED_CAPABILITIES); |
| 467 | |
| 468 | final NetworkMonitor nm = makeMonitor(); |
| 469 | nm.notifyNetworkConnected(); |
| 470 | |
| 471 | verify(mCallbacks, timeout(HANDLER_TIMEOUT_MS).times(1)) |
| 472 | .notifyNetworkTested(NETWORK_TEST_RESULT_INVALID, null); |
| 473 | } |
| 474 | |
| 475 | @Test |
| 476 | public void testNoInternetCapabilityValidated() throws Exception { |
| 477 | when(mCm.getNetworkCapabilities(any())).thenReturn(NO_INTERNET_CAPABILITIES); |
| 478 | |
| 479 | final NetworkMonitor nm = makeMonitor(); |
| 480 | nm.notifyNetworkConnected(); |
| 481 | |
| 482 | verify(mCallbacks, timeout(HANDLER_TIMEOUT_MS).times(1)) |
| 483 | .notifyNetworkTested(NETWORK_TEST_RESULT_VALID, null); |
| 484 | verify(mNetwork, never()).openConnection(any()); |
| 485 | } |
| 486 | |
| 487 | @Test |
| 488 | public void testLaunchCaptivePortalApp() throws Exception { |
| 489 | setSslException(mHttpsConnection); |
| 490 | setPortal302(mHttpConnection); |
| 491 | |
| 492 | final NetworkMonitor nm = makeMonitor(); |
| 493 | nm.notifyNetworkConnected(); |
| 494 | |
| 495 | verify(mCallbacks, timeout(HANDLER_TIMEOUT_MS).times(1)) |
Remi NGUYEN VAN | 9c5d964 | 2019-02-07 21:29:57 +0900 | [diff] [blame] | 496 | .showProvisioningNotification(any(), any()); |
Remi NGUYEN VAN | e67b0c3 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 497 | |
| 498 | // Check that startCaptivePortalApp sends the expected intent. |
| 499 | nm.launchCaptivePortalApp(); |
| 500 | |
Remi NGUYEN VAN | cfff01e | 2019-02-13 20:58:59 +0900 | [diff] [blame] | 501 | final ArgumentCaptor<Bundle> bundleCaptor = ArgumentCaptor.forClass(Bundle.class); |
| 502 | final ArgumentCaptor<Network> networkCaptor = ArgumentCaptor.forClass(Network.class); |
| 503 | verify(mCm, timeout(HANDLER_TIMEOUT_MS).times(1)) |
| 504 | .startCaptivePortalApp(networkCaptor.capture(), bundleCaptor.capture()); |
| 505 | final Bundle bundle = bundleCaptor.getValue(); |
| 506 | final Network bundleNetwork = bundle.getParcelable(ConnectivityManager.EXTRA_NETWORK); |
| 507 | assertEquals(TEST_NETID, bundleNetwork.netId); |
| 508 | // network is passed both in bundle and as parameter, as the bundle is opaque to the |
| 509 | // framework and only intended for the captive portal app, but the framework needs |
| 510 | // the network to identify the right NetworkMonitor. |
| 511 | assertEquals(TEST_NETID, networkCaptor.getValue().netId); |
Remi NGUYEN VAN | e67b0c3 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 512 | |
| 513 | // Have the app report that the captive portal is dismissed, and check that we revalidate. |
| 514 | setStatus(mHttpsConnection, 204); |
| 515 | setStatus(mHttpConnection, 204); |
Remi NGUYEN VAN | cfff01e | 2019-02-13 20:58:59 +0900 | [diff] [blame] | 516 | |
| 517 | nm.notifyCaptivePortalAppFinished(APP_RETURN_DISMISSED); |
Remi NGUYEN VAN | e67b0c3 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 518 | verify(mCallbacks, timeout(HANDLER_TIMEOUT_MS).times(1)) |
| 519 | .notifyNetworkTested(NETWORK_TEST_RESULT_VALID, null); |
| 520 | } |
| 521 | |
Chiachang Wang | f09e3e3 | 2019-02-22 11:13:07 +0800 | [diff] [blame] | 522 | @Test |
| 523 | public void testDataStall_StallSuspectedAndSendMetrics() throws IOException { |
| 524 | WrappedNetworkMonitor wrappedMonitor = makeNotMeteredWrappedNetworkMonitor(); |
| 525 | wrappedMonitor.setLastProbeTime(SystemClock.elapsedRealtime() - 1000); |
| 526 | makeDnsTimeoutEvent(wrappedMonitor, 5); |
| 527 | assertTrue(wrappedMonitor.isDataStall()); |
Chiachang Wang | e43f162 | 2019-03-08 14:34:19 +0800 | [diff] [blame] | 528 | verify(mDataStallStatsUtils, times(1)).write(makeEmptyDataStallDetectionStats(), any()); |
Chiachang Wang | f09e3e3 | 2019-02-22 11:13:07 +0800 | [diff] [blame] | 529 | } |
| 530 | |
| 531 | @Test |
| 532 | public void testDataStall_NoStallSuspectedAndSendMetrics() throws IOException { |
| 533 | WrappedNetworkMonitor wrappedMonitor = makeNotMeteredWrappedNetworkMonitor(); |
| 534 | wrappedMonitor.setLastProbeTime(SystemClock.elapsedRealtime() - 1000); |
| 535 | makeDnsTimeoutEvent(wrappedMonitor, 3); |
| 536 | assertFalse(wrappedMonitor.isDataStall()); |
Chiachang Wang | e43f162 | 2019-03-08 14:34:19 +0800 | [diff] [blame] | 537 | verify(mDataStallStatsUtils, never()).write(makeEmptyDataStallDetectionStats(), any()); |
Chiachang Wang | f09e3e3 | 2019-02-22 11:13:07 +0800 | [diff] [blame] | 538 | } |
Chiachang Wang | 95489ca | 2019-02-26 11:32:18 +0800 | [diff] [blame] | 539 | |
| 540 | @Test |
| 541 | public void testCollectDataStallMetrics() { |
| 542 | WrappedNetworkMonitor wrappedMonitor = makeNotMeteredWrappedNetworkMonitor(); |
| 543 | |
| 544 | when(mTelephony.getDataNetworkType()).thenReturn(TelephonyManager.NETWORK_TYPE_LTE); |
| 545 | when(mTelephony.getNetworkOperator()).thenReturn(TEST_MCCMNC); |
| 546 | when(mTelephony.getSimOperator()).thenReturn(TEST_MCCMNC); |
| 547 | |
| 548 | DataStallDetectionStats.Builder stats = |
| 549 | new DataStallDetectionStats.Builder() |
| 550 | .setEvaluationType(DATA_STALL_EVALUATION_TYPE_DNS) |
| 551 | .setNetworkType(NetworkCapabilities.TRANSPORT_CELLULAR) |
| 552 | .setCellData(TelephonyManager.NETWORK_TYPE_LTE /* radioType */, |
| 553 | true /* roaming */, |
| 554 | TEST_MCCMNC /* networkMccmnc */, |
| 555 | TEST_MCCMNC /* simMccmnc */, |
| 556 | CellSignalStrength.SIGNAL_STRENGTH_NONE_OR_UNKNOWN /* signalStrength */); |
| 557 | generateTimeoutDnsEvent(stats, DEFAULT_DNS_TIMEOUT_THRESHOLD); |
| 558 | |
| 559 | assertEquals(wrappedMonitor.buildDataStallDetectionStats( |
| 560 | NetworkCapabilities.TRANSPORT_CELLULAR), stats.build()); |
| 561 | |
| 562 | when(mWifi.getConnectionInfo()).thenReturn(mWifiInfo); |
| 563 | |
| 564 | stats = new DataStallDetectionStats.Builder() |
| 565 | .setEvaluationType(DATA_STALL_EVALUATION_TYPE_DNS) |
| 566 | .setNetworkType(NetworkCapabilities.TRANSPORT_WIFI) |
| 567 | .setWiFiData(mWifiInfo); |
| 568 | generateTimeoutDnsEvent(stats, DEFAULT_DNS_TIMEOUT_THRESHOLD); |
| 569 | |
| 570 | assertEquals( |
| 571 | wrappedMonitor.buildDataStallDetectionStats(NetworkCapabilities.TRANSPORT_WIFI), |
| 572 | stats.build()); |
| 573 | } |
| 574 | |
Chiachang Wang | 7a70a7e | 2018-11-27 18:00:05 +0800 | [diff] [blame] | 575 | private void makeDnsTimeoutEvent(WrappedNetworkMonitor wrappedMonitor, int count) { |
| 576 | for (int i = 0; i < count; i++) { |
| 577 | wrappedMonitor.getDnsStallDetector().accumulateConsecutiveDnsTimeoutCount( |
| 578 | RETURN_CODE_DNS_TIMEOUT); |
| 579 | } |
| 580 | } |
| 581 | |
| 582 | private void makeDnsSuccessEvent(WrappedNetworkMonitor wrappedMonitor, int count) { |
| 583 | for (int i = 0; i < count; i++) { |
| 584 | wrappedMonitor.getDnsStallDetector().accumulateConsecutiveDnsTimeoutCount( |
| 585 | RETURN_CODE_DNS_SUCCESS); |
| 586 | } |
| 587 | } |
| 588 | |
Chiachang Wang | e43f162 | 2019-03-08 14:34:19 +0800 | [diff] [blame] | 589 | private DataStallDetectionStats makeEmptyDataStallDetectionStats() { |
| 590 | return new DataStallDetectionStats.Builder().build(); |
| 591 | } |
| 592 | |
Chiachang Wang | 7a70a7e | 2018-11-27 18:00:05 +0800 | [diff] [blame] | 593 | private void setDataStallEvaluationType(int type) { |
| 594 | when(mDependencies.getSetting(any(), |
| 595 | eq(Settings.Global.DATA_STALL_EVALUATION_TYPE), anyInt())).thenReturn(type); |
| 596 | } |
| 597 | |
| 598 | private void setMinDataStallEvaluateInterval(int time) { |
| 599 | when(mDependencies.getSetting(any(), |
| 600 | eq(Settings.Global.DATA_STALL_MIN_EVALUATE_INTERVAL), anyInt())).thenReturn(time); |
| 601 | } |
| 602 | |
| 603 | private void setValidDataStallDnsTimeThreshold(int time) { |
| 604 | when(mDependencies.getSetting(any(), |
| 605 | eq(Settings.Global.DATA_STALL_VALID_DNS_TIME_THRESHOLD), anyInt())).thenReturn(time); |
| 606 | } |
| 607 | |
| 608 | private void setConsecutiveDnsTimeoutThreshold(int num) { |
| 609 | when(mDependencies.getSetting(any(), |
| 610 | eq(Settings.Global.DATA_STALL_CONSECUTIVE_DNS_TIMEOUT_THRESHOLD), anyInt())) |
| 611 | .thenReturn(num); |
| 612 | } |
| 613 | |
Remi NGUYEN VAN | d9a1cd7 | 2018-05-22 13:11:15 +0900 | [diff] [blame] | 614 | private void setFallbackUrl(String url) { |
| 615 | when(mDependencies.getSetting(any(), |
| 616 | eq(Settings.Global.CAPTIVE_PORTAL_FALLBACK_URL), any())).thenReturn(url); |
| 617 | } |
| 618 | |
| 619 | private void setOtherFallbackUrls(String urls) { |
| 620 | when(mDependencies.getSetting(any(), |
| 621 | eq(Settings.Global.CAPTIVE_PORTAL_OTHER_FALLBACK_URLS), any())).thenReturn(urls); |
| 622 | } |
| 623 | |
| 624 | private void setFallbackSpecs(String specs) { |
| 625 | when(mDependencies.getSetting(any(), |
| 626 | eq(Settings.Global.CAPTIVE_PORTAL_FALLBACK_PROBE_SPECS), any())).thenReturn(specs); |
| 627 | } |
| 628 | |
Remi NGUYEN VAN | e67b0c3 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 629 | private void setCaptivePortalMode(int mode) { |
| 630 | when(mDependencies.getSetting(any(), |
| 631 | eq(Settings.Global.CAPTIVE_PORTAL_MODE), anyInt())).thenReturn(mode); |
| 632 | } |
| 633 | |
Remi NGUYEN VAN | d9a1cd7 | 2018-05-22 13:11:15 +0900 | [diff] [blame] | 634 | private void assertPortal(CaptivePortalProbeResult result) { |
| 635 | assertTrue(result.isPortal()); |
| 636 | assertFalse(result.isFailed()); |
| 637 | assertFalse(result.isSuccessful()); |
| 638 | } |
| 639 | |
| 640 | private void assertNotPortal(CaptivePortalProbeResult result) { |
| 641 | assertFalse(result.isPortal()); |
| 642 | assertFalse(result.isFailed()); |
| 643 | assertTrue(result.isSuccessful()); |
| 644 | } |
| 645 | |
| 646 | private void assertFailed(CaptivePortalProbeResult result) { |
| 647 | assertFalse(result.isPortal()); |
| 648 | assertTrue(result.isFailed()); |
| 649 | assertFalse(result.isSuccessful()); |
| 650 | } |
| 651 | |
| 652 | private void setSslException(HttpURLConnection connection) throws IOException { |
Remi NGUYEN VAN | e67b0c3 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 653 | doThrow(new SSLHandshakeException("Invalid cert")).when(connection).getResponseCode(); |
Remi NGUYEN VAN | d9a1cd7 | 2018-05-22 13:11:15 +0900 | [diff] [blame] | 654 | } |
| 655 | |
| 656 | private void set302(HttpURLConnection connection, String location) throws IOException { |
| 657 | setStatus(connection, 302); |
Remi NGUYEN VAN | e67b0c3 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 658 | doReturn(location).when(connection).getHeaderField(LOCATION_HEADER); |
Remi NGUYEN VAN | d9a1cd7 | 2018-05-22 13:11:15 +0900 | [diff] [blame] | 659 | } |
| 660 | |
| 661 | private void setPortal302(HttpURLConnection connection) throws IOException { |
| 662 | set302(connection, "http://login.example.com"); |
| 663 | } |
| 664 | |
| 665 | private void setStatus(HttpURLConnection connection, int status) throws IOException { |
Remi NGUYEN VAN | e67b0c3 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 666 | doReturn(status).when(connection).getResponseCode(); |
Hugo Benichi | c894b12 | 2017-07-31 12:58:20 +0900 | [diff] [blame] | 667 | } |
Chiachang Wang | 95489ca | 2019-02-26 11:32:18 +0800 | [diff] [blame] | 668 | |
| 669 | private void generateTimeoutDnsEvent(DataStallDetectionStats.Builder stats, int num) { |
| 670 | for (int i = 0; i < num; i++) { |
| 671 | stats.addDnsEvent(RETURN_CODE_DNS_TIMEOUT, 123456789 /* timeMs */); |
| 672 | } |
| 673 | } |
Hugo Benichi | c894b12 | 2017-07-31 12:58:20 +0900 | [diff] [blame] | 674 | } |
| 675 | |