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 | eb61922 | 2019-07-03 20:52:08 +0800 | [diff] [blame] | 20 | import static android.net.DnsResolver.TYPE_A; |
| 21 | import static android.net.DnsResolver.TYPE_AAAA; |
Chiachang Wang | 813ee47 | 2019-05-23 16:29:30 +0800 | [diff] [blame] | 22 | import static android.net.INetworkMonitor.NETWORK_VALIDATION_PROBE_DNS; |
| 23 | import static android.net.INetworkMonitor.NETWORK_VALIDATION_PROBE_FALLBACK; |
| 24 | import static android.net.INetworkMonitor.NETWORK_VALIDATION_PROBE_HTTP; |
| 25 | import static android.net.INetworkMonitor.NETWORK_VALIDATION_PROBE_HTTPS; |
| 26 | import static android.net.INetworkMonitor.NETWORK_VALIDATION_PROBE_PRIVDNS; |
| 27 | import static android.net.INetworkMonitor.NETWORK_VALIDATION_RESULT_PARTIAL; |
Cody Kesting | 78156e4 | 2020-08-03 18:19:56 -0700 | [diff] [blame] | 28 | import static android.net.INetworkMonitor.NETWORK_VALIDATION_RESULT_SKIPPED; |
Chiachang Wang | 813ee47 | 2019-05-23 16:29:30 +0800 | [diff] [blame] | 29 | import static android.net.INetworkMonitor.NETWORK_VALIDATION_RESULT_VALID; |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 30 | import static android.net.NetworkCapabilities.NET_CAPABILITY_INTERNET; |
lucaslin | e8d5403 | 2020-04-29 02:35:19 +0800 | [diff] [blame] | 31 | import static android.net.NetworkCapabilities.NET_CAPABILITY_NOT_METERED; |
Pavan Kumar M | 2fbc846 | 2021-04-28 11:20:25 +0530 | [diff] [blame] | 32 | import static android.net.NetworkCapabilities.NET_CAPABILITY_NOT_RESTRICTED; |
Remi NGUYEN VAN | 1b7a063 | 2021-07-07 17:54:09 +0900 | [diff] [blame] | 33 | import static android.net.NetworkCapabilities.NET_CAPABILITY_NOT_VCN_MANAGED; |
Pavan Kumar M | 2fbc846 | 2021-04-28 11:20:25 +0530 | [diff] [blame] | 34 | import static android.net.NetworkCapabilities.NET_CAPABILITY_OEM_PAID; |
Remi NGUYEN VAN | 1b7a063 | 2021-07-07 17:54:09 +0900 | [diff] [blame] | 35 | import static android.net.NetworkCapabilities.NET_CAPABILITY_TRUSTED; |
lucaslin | e8d5403 | 2020-04-29 02:35:19 +0800 | [diff] [blame] | 36 | import static android.net.NetworkCapabilities.TRANSPORT_CELLULAR; |
| 37 | import static android.net.NetworkCapabilities.TRANSPORT_VPN; |
| 38 | import static android.net.NetworkCapabilities.TRANSPORT_WIFI; |
Chiachang Wang | 717099f | 2020-03-23 11:31:39 +0800 | [diff] [blame] | 39 | import static android.net.metrics.ValidationProbeEvent.PROBE_HTTP; |
Chiachang Wang | 9a87f80 | 2019-04-08 19:06:21 +0800 | [diff] [blame] | 40 | import static android.net.util.DataStallUtils.CONFIG_DATA_STALL_CONSECUTIVE_DNS_TIMEOUT_THRESHOLD; |
| 41 | import static android.net.util.DataStallUtils.CONFIG_DATA_STALL_EVALUATION_TYPE; |
| 42 | import static android.net.util.DataStallUtils.CONFIG_DATA_STALL_MIN_EVALUATE_INTERVAL; |
Chiachang Wang | a5716bf | 2019-11-20 16:13:07 +0800 | [diff] [blame] | 43 | import static android.net.util.DataStallUtils.CONFIG_DATA_STALL_TCP_POLLING_INTERVAL; |
Chiachang Wang | 9a87f80 | 2019-04-08 19:06:21 +0800 | [diff] [blame] | 44 | import static android.net.util.DataStallUtils.CONFIG_DATA_STALL_VALID_DNS_TIME_THRESHOLD; |
| 45 | import static android.net.util.DataStallUtils.DATA_STALL_EVALUATION_TYPE_DNS; |
Chiachang Wang | a5716bf | 2019-11-20 16:13:07 +0800 | [diff] [blame] | 46 | import static android.net.util.DataStallUtils.DATA_STALL_EVALUATION_TYPE_TCP; |
Chiachang Wang | 9af1cb5 | 2020-05-20 15:14:46 +0800 | [diff] [blame] | 47 | import static android.net.util.DataStallUtils.DEFAULT_DATA_STALL_EVALUATION_TYPES; |
Chiachang Wang | 79a6da3 | 2019-04-17 17:00:54 +0800 | [diff] [blame] | 48 | import static android.net.util.NetworkStackUtils.CAPTIVE_PORTAL_FALLBACK_PROBE_SPECS; |
| 49 | import static android.net.util.NetworkStackUtils.CAPTIVE_PORTAL_OTHER_FALLBACK_URLS; |
| 50 | import static android.net.util.NetworkStackUtils.CAPTIVE_PORTAL_USE_HTTPS; |
Chiachang Wang | 5dbe685 | 2020-04-10 09:02:45 +0800 | [diff] [blame] | 51 | import static android.net.util.NetworkStackUtils.DEFAULT_CAPTIVE_PORTAL_DNS_PROBE_TIMEOUT; |
Chiachang Wang | 26a626a | 2020-03-02 17:41:58 +0800 | [diff] [blame] | 52 | import static android.net.util.NetworkStackUtils.DISMISS_PORTAL_IN_VALIDATED_NETWORK; |
Yeo | 4915faf | 2019-08-26 14:53:23 +0900 | [diff] [blame] | 53 | import static android.net.util.NetworkStackUtils.DNS_PROBE_PRIVATE_IP_NO_INTERNET_VERSION; |
Remi NGUYEN VAN | 943d634 | 2020-03-24 18:22:34 +0900 | [diff] [blame] | 54 | import static android.net.util.NetworkStackUtils.TEST_CAPTIVE_PORTAL_HTTPS_URL; |
| 55 | import static android.net.util.NetworkStackUtils.TEST_CAPTIVE_PORTAL_HTTP_URL; |
| 56 | import static android.net.util.NetworkStackUtils.TEST_URL_EXPIRATION_TIME; |
| 57 | import static android.provider.DeviceConfig.NAMESPACE_CONNECTIVITY; |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 58 | |
Chiachang Wang | eb61922 | 2019-07-03 20:52:08 +0800 | [diff] [blame] | 59 | import static com.android.networkstack.util.DnsUtils.PRIVATE_DNS_PROBE_HOST_SUFFIX; |
Remi NGUYEN VAN | 2f18730 | 2020-05-21 14:35:50 +0900 | [diff] [blame] | 60 | import static com.android.server.connectivity.NetworkMonitor.INITIAL_REEVALUATE_DELAY_MS; |
Remi NGUYEN VAN | 2d909a7 | 2019-12-24 18:15:52 +0900 | [diff] [blame] | 61 | import static com.android.server.connectivity.NetworkMonitor.extractCharset; |
Chiachang Wang | eb61922 | 2019-07-03 20:52:08 +0800 | [diff] [blame] | 62 | |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 63 | import static junit.framework.Assert.assertEquals; |
| 64 | import static junit.framework.Assert.assertFalse; |
| 65 | |
Lorenzo Colitti | 171cfd2 | 2019-04-18 13:44:32 +0900 | [diff] [blame] | 66 | import static org.junit.Assert.assertArrayEquals; |
lucaslin | 9073e7b | 2020-04-15 23:02:14 +0800 | [diff] [blame] | 67 | import static org.junit.Assert.assertNotEquals; |
Remi NGUYEN VAN | dc6e640 | 2019-03-27 15:42:53 +0900 | [diff] [blame] | 68 | import static org.junit.Assert.assertNotNull; |
| 69 | import static org.junit.Assert.assertNull; |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 70 | import static org.junit.Assert.assertTrue; |
| 71 | import static org.junit.Assert.fail; |
Remi NGUYEN VAN | 2fa48c2 | 2019-12-09 16:40:02 +0900 | [diff] [blame] | 72 | import static org.junit.Assume.assumeFalse; |
| 73 | import static org.junit.Assume.assumeTrue; |
Chiachang Wang | 26a626a | 2020-03-02 17:41:58 +0800 | [diff] [blame] | 74 | import static org.mockito.ArgumentMatchers.anyBoolean; |
Cody Kesting | 176bce7 | 2020-01-20 18:09:59 -0800 | [diff] [blame] | 75 | import static org.mockito.ArgumentMatchers.argThat; |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 76 | import static org.mockito.ArgumentMatchers.eq; |
Remi NGUYEN VAN | ebb5a9f | 2020-05-20 19:11:56 +0900 | [diff] [blame] | 77 | import static org.mockito.Mockito.after; |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 78 | import static org.mockito.Mockito.any; |
| 79 | import static org.mockito.Mockito.anyInt; |
Remi NGUYEN VAN | eb5bfa9 | 2020-05-01 10:43:12 +0900 | [diff] [blame] | 80 | import static org.mockito.Mockito.atLeastOnce; |
| 81 | import static org.mockito.Mockito.atMost; |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 82 | import static org.mockito.Mockito.doAnswer; |
| 83 | import static org.mockito.Mockito.doReturn; |
| 84 | import static org.mockito.Mockito.doThrow; |
Remi NGUYEN VAN | 905b0b6 | 2020-04-20 17:06:27 +0900 | [diff] [blame] | 85 | import static org.mockito.Mockito.mock; |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 86 | import static org.mockito.Mockito.never; |
Lorenzo Colitti | cfc1abc | 2019-04-28 00:55:19 +0900 | [diff] [blame] | 87 | import static org.mockito.Mockito.reset; |
Remi NGUYEN VAN | 905b0b6 | 2020-04-20 17:06:27 +0900 | [diff] [blame] | 88 | import static org.mockito.Mockito.spy; |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 89 | import static org.mockito.Mockito.timeout; |
| 90 | import static org.mockito.Mockito.times; |
| 91 | import static org.mockito.Mockito.verify; |
| 92 | import static org.mockito.Mockito.when; |
| 93 | |
Remi NGUYEN VAN | 2fa48c2 | 2019-12-09 16:40:02 +0900 | [diff] [blame] | 94 | import static java.lang.System.currentTimeMillis; |
| 95 | import static java.util.Collections.singletonList; |
Lorenzo Colitti | b891b5e | 2020-02-05 16:48:09 +0900 | [diff] [blame] | 96 | import static java.util.stream.Collectors.toList; |
| 97 | |
Chiachang Wang | caa3520 | 2019-02-26 11:32:18 +0800 | [diff] [blame] | 98 | import android.annotation.NonNull; |
Lorenzo Colitti | 5cba6b6 | 2021-06-28 23:08:04 +0900 | [diff] [blame] | 99 | import android.annotation.SuppressLint; |
Lorenzo Colitti | 37ee46f | 2019-04-08 21:51:41 +0900 | [diff] [blame] | 100 | import android.content.BroadcastReceiver; |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 101 | import android.content.Context; |
lucaslin | 9b4dfab | 2019-12-17 23:06:12 +0800 | [diff] [blame] | 102 | import android.content.ContextWrapper; |
Lorenzo Colitti | 37ee46f | 2019-04-08 21:51:41 +0900 | [diff] [blame] | 103 | import android.content.Intent; |
lucaslin | 9b4dfab | 2019-12-17 23:06:12 +0800 | [diff] [blame] | 104 | import android.content.pm.PackageManager; |
| 105 | import android.content.res.Configuration; |
Niklas Lindgren | 0c90488 | 2018-12-07 11:08:04 +0100 | [diff] [blame] | 106 | import android.content.res.Resources; |
Remi NGUYEN VAN | 2fa48c2 | 2019-12-09 16:40:02 +0900 | [diff] [blame] | 107 | import android.net.CaptivePortalData; |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 108 | import android.net.ConnectivityManager; |
Remi NGUYEN VAN | 0402afc | 2020-04-15 18:56:59 +0900 | [diff] [blame] | 109 | import android.net.DataStallReportParcelable; |
Lorenzo Colitti | 171cfd2 | 2019-04-18 13:44:32 +0900 | [diff] [blame] | 110 | import android.net.DnsResolver; |
Chiachang Wang | cded6ce | 2019-12-18 17:27:57 +0800 | [diff] [blame] | 111 | import android.net.INetd; |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 112 | import android.net.INetworkMonitorCallbacks; |
Yeo | 4915faf | 2019-08-26 14:53:23 +0900 | [diff] [blame] | 113 | import android.net.InetAddresses; |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 114 | import android.net.LinkProperties; |
| 115 | import android.net.Network; |
| 116 | import android.net.NetworkCapabilities; |
Remi NGUYEN VAN | 0402afc | 2020-04-15 18:56:59 +0900 | [diff] [blame] | 117 | import android.net.NetworkTestResultParcelable; |
Remi NGUYEN VAN | 2fa48c2 | 2019-12-09 16:40:02 +0900 | [diff] [blame] | 118 | import android.net.Uri; |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 119 | import android.net.captiveportal.CaptivePortalProbeResult; |
| 120 | import android.net.metrics.IpConnectivityLog; |
Lorenzo Colitti | cfc1abc | 2019-04-28 00:55:19 +0900 | [diff] [blame] | 121 | import android.net.shared.PrivateDnsConfig; |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 122 | import android.net.util.SharedLog; |
Chiachang Wang | caa3520 | 2019-02-26 11:32:18 +0800 | [diff] [blame] | 123 | import android.net.wifi.WifiInfo; |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 124 | import android.net.wifi.WifiManager; |
Remi NGUYEN VAN | 7fc260a | 2020-02-03 13:33:58 +0900 | [diff] [blame] | 125 | import android.os.Build; |
Remi NGUYEN VAN | ad99e54 | 2019-02-13 20:58:59 +0900 | [diff] [blame] | 126 | import android.os.Bundle; |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 127 | import android.os.ConditionVariable; |
| 128 | import android.os.Handler; |
Remi NGUYEN VAN | 905b0b6 | 2020-04-20 17:06:27 +0900 | [diff] [blame] | 129 | import android.os.IBinder; |
Lorenzo Colitti | 171cfd2 | 2019-04-18 13:44:32 +0900 | [diff] [blame] | 130 | import android.os.Looper; |
Lorenzo Colitti | cfc1abc | 2019-04-28 00:55:19 +0900 | [diff] [blame] | 131 | import android.os.Process; |
Remi NGUYEN VAN | dc6e640 | 2019-03-27 15:42:53 +0900 | [diff] [blame] | 132 | import android.os.RemoteException; |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 133 | import android.os.SystemClock; |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 134 | import android.provider.Settings; |
lucaslin | 9b4dfab | 2019-12-17 23:06:12 +0800 | [diff] [blame] | 135 | import android.telephony.CellIdentityGsm; |
| 136 | import android.telephony.CellIdentityLte; |
| 137 | import android.telephony.CellInfo; |
| 138 | import android.telephony.CellInfoGsm; |
| 139 | import android.telephony.CellInfoLte; |
Chiachang Wang | caa3520 | 2019-02-26 11:32:18 +0800 | [diff] [blame] | 140 | import android.telephony.CellSignalStrength; |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 141 | import android.telephony.TelephonyManager; |
| 142 | import android.util.ArrayMap; |
| 143 | |
Brett Chabot | 189c598 | 2019-02-26 14:52:33 -0800 | [diff] [blame] | 144 | import androidx.test.filters.SmallTest; |
| 145 | import androidx.test.runner.AndroidJUnit4; |
| 146 | |
Remi NGUYEN VAN | 89cd026 | 2019-12-29 22:46:00 +0900 | [diff] [blame] | 147 | import com.android.networkstack.NetworkStackNotifier; |
Lorenzo Colitti | 171cfd2 | 2019-04-18 13:44:32 +0900 | [diff] [blame] | 148 | import com.android.networkstack.R; |
Remi NGUYEN VAN | 2fa48c2 | 2019-12-09 16:40:02 +0900 | [diff] [blame] | 149 | import com.android.networkstack.apishim.CaptivePortalDataShimImpl; |
Remi NGUYEN VAN | 0402afc | 2020-04-15 18:56:59 +0900 | [diff] [blame] | 150 | import com.android.networkstack.apishim.ConstantsShim; |
Hai Shalom | e8fe123 | 2021-02-08 11:47:15 -0800 | [diff] [blame] | 151 | import com.android.networkstack.apishim.NetworkInformationShimImpl; |
| 152 | import com.android.networkstack.apishim.common.CaptivePortalDataShim; |
| 153 | import com.android.networkstack.apishim.common.NetworkInformationShim; |
Remi NGUYEN VAN | 046bfa7 | 2020-05-14 14:32:03 +0900 | [diff] [blame] | 154 | import com.android.networkstack.apishim.common.ShimUtils; |
Hai Shalom | e8fe123 | 2021-02-08 11:47:15 -0800 | [diff] [blame] | 155 | import com.android.networkstack.apishim.common.UnsupportedApiLevelException; |
Chiachang Wang | 8024227 | 2019-04-11 21:24:28 +0800 | [diff] [blame] | 156 | import com.android.networkstack.metrics.DataStallDetectionStats; |
| 157 | import com.android.networkstack.metrics.DataStallStatsUtils; |
Chiachang Wang | a5716bf | 2019-11-20 16:13:07 +0800 | [diff] [blame] | 158 | import com.android.networkstack.netlink.TcpSocketTracker; |
Remi NGUYEN VAN | 89cd026 | 2019-12-29 22:46:00 +0900 | [diff] [blame] | 159 | import com.android.server.NetworkStackService.NetworkStackServiceManager; |
Chiachang Wang | 9af1cb5 | 2020-05-20 15:14:46 +0800 | [diff] [blame] | 160 | import com.android.server.connectivity.nano.CellularData; |
| 161 | import com.android.server.connectivity.nano.DnsEvent; |
| 162 | import com.android.server.connectivity.nano.WifiData; |
Remi NGUYEN VAN | 905b0b6 | 2020-04-20 17:06:27 +0900 | [diff] [blame] | 163 | import com.android.testutils.DevSdkIgnoreRule; |
Remi NGUYEN VAN | a750073 | 2021-05-27 14:35:18 +0900 | [diff] [blame] | 164 | import com.android.testutils.DevSdkIgnoreRule.IgnoreAfter; |
Remi NGUYEN VAN | 905b0b6 | 2020-04-20 17:06:27 +0900 | [diff] [blame] | 165 | import com.android.testutils.DevSdkIgnoreRule.IgnoreUpTo; |
Chalard Jean | 23a0630 | 2020-06-26 00:41:00 +0900 | [diff] [blame] | 166 | import com.android.testutils.HandlerUtils; |
Chiachang Wang | 8024227 | 2019-04-11 21:24:28 +0800 | [diff] [blame] | 167 | |
Chiachang Wang | 9af1cb5 | 2020-05-20 15:14:46 +0800 | [diff] [blame] | 168 | import com.google.protobuf.nano.MessageNano; |
| 169 | |
Remi NGUYEN VAN | 0402afc | 2020-04-15 18:56:59 +0900 | [diff] [blame] | 170 | import junit.framework.AssertionFailedError; |
| 171 | |
Lorenzo Colitti | 37ee46f | 2019-04-08 21:51:41 +0900 | [diff] [blame] | 172 | import org.junit.After; |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 173 | import org.junit.Before; |
Remi NGUYEN VAN | 905b0b6 | 2020-04-20 17:06:27 +0900 | [diff] [blame] | 174 | import org.junit.Rule; |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 175 | import org.junit.Test; |
| 176 | import org.junit.runner.RunWith; |
| 177 | import org.mockito.ArgumentCaptor; |
| 178 | import org.mockito.Mock; |
| 179 | import org.mockito.MockitoAnnotations; |
| 180 | import org.mockito.Spy; |
Chiachang Wang | ddb7da6 | 2019-06-03 15:50:53 +0800 | [diff] [blame] | 181 | import org.mockito.invocation.InvocationOnMock; |
| 182 | import org.mockito.stubbing.Answer; |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 183 | |
Remi NGUYEN VAN | 2fa48c2 | 2019-12-09 16:40:02 +0900 | [diff] [blame] | 184 | import java.io.ByteArrayInputStream; |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 185 | import java.io.IOException; |
Remi NGUYEN VAN | 2d909a7 | 2019-12-24 18:15:52 +0900 | [diff] [blame] | 186 | import java.io.InputStream; |
Remi NGUYEN VAN | 7fc260a | 2020-02-03 13:33:58 +0900 | [diff] [blame] | 187 | import java.lang.reflect.Constructor; |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 188 | import java.net.HttpURLConnection; |
Yeo | 4915faf | 2019-08-26 14:53:23 +0900 | [diff] [blame] | 189 | import java.net.Inet6Address; |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 190 | import java.net.InetAddress; |
| 191 | import java.net.URL; |
Lorenzo Colitti | 171cfd2 | 2019-04-18 13:44:32 +0900 | [diff] [blame] | 192 | import java.net.UnknownHostException; |
Remi NGUYEN VAN | 2d909a7 | 2019-12-24 18:15:52 +0900 | [diff] [blame] | 193 | import java.nio.charset.Charset; |
| 194 | import java.nio.charset.StandardCharsets; |
Lorenzo Colitti | 171cfd2 | 2019-04-18 13:44:32 +0900 | [diff] [blame] | 195 | import java.util.ArrayList; |
Chiachang Wang | eb61922 | 2019-07-03 20:52:08 +0800 | [diff] [blame] | 196 | import java.util.Arrays; |
Nathan Harold | 61b3923 | 2020-01-21 16:48:25 -0800 | [diff] [blame] | 197 | import java.util.Collections; |
Remi NGUYEN VAN | 2fa48c2 | 2019-12-09 16:40:02 +0900 | [diff] [blame] | 198 | import java.util.HashMap; |
Lorenzo Colitti | 37ee46f | 2019-04-08 21:51:41 +0900 | [diff] [blame] | 199 | import java.util.HashSet; |
Lorenzo Colitti | 171cfd2 | 2019-04-18 13:44:32 +0900 | [diff] [blame] | 200 | import java.util.List; |
Remi NGUYEN VAN | 2fa48c2 | 2019-12-09 16:40:02 +0900 | [diff] [blame] | 201 | import java.util.Map; |
Remi NGUYEN VAN | 0402afc | 2020-04-15 18:56:59 +0900 | [diff] [blame] | 202 | import java.util.Objects; |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 203 | import java.util.Random; |
Lorenzo Colitti | 171cfd2 | 2019-04-18 13:44:32 +0900 | [diff] [blame] | 204 | import java.util.concurrent.Executor; |
Remi NGUYEN VAN | 943d634 | 2020-03-24 18:22:34 +0900 | [diff] [blame] | 205 | import java.util.concurrent.TimeUnit; |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 206 | |
| 207 | import javax.net.ssl.SSLHandshakeException; |
| 208 | |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 209 | @RunWith(AndroidJUnit4.class) |
| 210 | @SmallTest |
Lorenzo Colitti | 5cba6b6 | 2021-06-28 23:08:04 +0900 | [diff] [blame] | 211 | @SuppressLint("NewApi") // Uses hidden APIs, which the linter would identify as missing APIs. |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 212 | public class NetworkMonitorTest { |
| 213 | private static final String LOCATION_HEADER = "location"; |
Remi NGUYEN VAN | 2fa48c2 | 2019-12-09 16:40:02 +0900 | [diff] [blame] | 214 | private static final String CONTENT_TYPE_HEADER = "Content-Type"; |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 215 | |
Remi NGUYEN VAN | 905b0b6 | 2020-04-20 17:06:27 +0900 | [diff] [blame] | 216 | @Rule |
| 217 | public final DevSdkIgnoreRule mIgnoreRule = new DevSdkIgnoreRule(); |
| 218 | |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 219 | private @Mock Context mContext; |
lucaslin | 9b4dfab | 2019-12-17 23:06:12 +0800 | [diff] [blame] | 220 | private @Mock Configuration mConfiguration; |
Niklas Lindgren | 0c90488 | 2018-12-07 11:08:04 +0100 | [diff] [blame] | 221 | private @Mock Resources mResources; |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 222 | private @Mock IpConnectivityLog mLogger; |
| 223 | private @Mock SharedLog mValidationLogger; |
Lorenzo Colitti | 171cfd2 | 2019-04-18 13:44:32 +0900 | [diff] [blame] | 224 | private @Mock DnsResolver mDnsResolver; |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 225 | private @Mock ConnectivityManager mCm; |
| 226 | private @Mock TelephonyManager mTelephony; |
| 227 | private @Mock WifiManager mWifi; |
Remi NGUYEN VAN | 89cd026 | 2019-12-29 22:46:00 +0900 | [diff] [blame] | 228 | private @Mock NetworkStackServiceManager mServiceManager; |
| 229 | private @Mock NetworkStackNotifier mNotifier; |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 230 | private @Mock HttpURLConnection mHttpConnection; |
Chiachang Wang | 80be441 | 2020-04-14 11:52:10 +0800 | [diff] [blame] | 231 | private @Mock HttpURLConnection mOtherHttpConnection1; |
| 232 | private @Mock HttpURLConnection mOtherHttpConnection2; |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 233 | private @Mock HttpURLConnection mHttpsConnection; |
Chiachang Wang | 80be441 | 2020-04-14 11:52:10 +0800 | [diff] [blame] | 234 | private @Mock HttpURLConnection mOtherHttpsConnection1; |
| 235 | private @Mock HttpURLConnection mOtherHttpsConnection2; |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 236 | private @Mock HttpURLConnection mFallbackConnection; |
| 237 | private @Mock HttpURLConnection mOtherFallbackConnection; |
Remi NGUYEN VAN | 943d634 | 2020-03-24 18:22:34 +0900 | [diff] [blame] | 238 | private @Mock HttpURLConnection mTestOverriddenUrlConnection; |
Remi NGUYEN VAN | 2fa48c2 | 2019-12-09 16:40:02 +0900 | [diff] [blame] | 239 | private @Mock HttpURLConnection mCapportApiConnection; |
lucaslin | e8d5403 | 2020-04-29 02:35:19 +0800 | [diff] [blame] | 240 | private @Mock HttpURLConnection mSpeedTestConnection; |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 241 | private @Mock Random mRandom; |
| 242 | private @Mock NetworkMonitor.Dependencies mDependencies; |
Remi NGUYEN VAN | 905b0b6 | 2020-04-20 17:06:27 +0900 | [diff] [blame] | 243 | // Mockito can't create a mock of INetworkMonitorCallbacks on Q because it can't find |
| 244 | // CaptivePortalData on notifyCaptivePortalDataChanged. Use a spy on a mock IBinder instead. |
| 245 | private INetworkMonitorCallbacks mCallbacks = spy( |
| 246 | INetworkMonitorCallbacks.Stub.asInterface(mock(IBinder.class))); |
Lorenzo Colitti | 7f9734f | 2019-05-09 12:13:54 +0900 | [diff] [blame] | 247 | private @Spy Network mCleartextDnsNetwork = new Network(TEST_NETID); |
| 248 | private @Mock Network mNetwork; |
Chiachang Wang | 8b5f84a | 2019-02-22 11:13:07 +0800 | [diff] [blame] | 249 | private @Mock DataStallStatsUtils mDataStallStatsUtils; |
Chiachang Wang | a5716bf | 2019-11-20 16:13:07 +0800 | [diff] [blame] | 250 | private @Mock TcpSocketTracker.Dependencies mTstDependencies; |
Chiachang Wang | cded6ce | 2019-12-18 17:27:57 +0800 | [diff] [blame] | 251 | private @Mock INetd mNetd; |
Chiachang Wang | 4336b91 | 2019-11-26 15:39:22 +0800 | [diff] [blame] | 252 | private @Mock TcpSocketTracker mTst; |
Lorenzo Colitti | 37ee46f | 2019-04-08 21:51:41 +0900 | [diff] [blame] | 253 | private HashSet<WrappedNetworkMonitor> mCreatedNetworkMonitors; |
| 254 | private HashSet<BroadcastReceiver> mRegisteredReceivers; |
Chiachang Wang | 2482099 | 2020-03-23 21:16:36 +0800 | [diff] [blame] | 255 | private @Mock Context mMccContext; |
| 256 | private @Mock Resources mMccResource; |
Chiachang Wang | 9af1cb5 | 2020-05-20 15:14:46 +0800 | [diff] [blame] | 257 | private @Mock WifiInfo mWifiInfo; |
Lorenzo Colitti | 37ee46f | 2019-04-08 21:51:41 +0900 | [diff] [blame] | 258 | |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 259 | private static final int TEST_NETID = 4242; |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 260 | private static final String TEST_HTTP_URL = "http://www.google.com/gen_204"; |
Chiachang Wang | 80be441 | 2020-04-14 11:52:10 +0800 | [diff] [blame] | 261 | private static final String TEST_HTTP_OTHER_URL1 = "http://other1.google.com/gen_204"; |
| 262 | private static final String TEST_HTTP_OTHER_URL2 = "http://other2.google.com/gen_204"; |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 263 | private static final String TEST_HTTPS_URL = "https://www.google.com/gen_204"; |
Chiachang Wang | 80be441 | 2020-04-14 11:52:10 +0800 | [diff] [blame] | 264 | private static final String TEST_HTTPS_OTHER_URL1 = "https://other1.google.com/gen_204"; |
| 265 | private static final String TEST_HTTPS_OTHER_URL2 = "https://other2.google.com/gen_204"; |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 266 | private static final String TEST_FALLBACK_URL = "http://fallback.google.com/gen_204"; |
| 267 | private static final String TEST_OTHER_FALLBACK_URL = "http://otherfallback.google.com/gen_204"; |
Remi NGUYEN VAN | 943d634 | 2020-03-24 18:22:34 +0900 | [diff] [blame] | 268 | private static final String TEST_INVALID_OVERRIDE_URL = "https://override.example.com/test"; |
| 269 | private static final String TEST_OVERRIDE_URL = "http://localhost:12345/test"; |
Remi NGUYEN VAN | 2fa48c2 | 2019-12-09 16:40:02 +0900 | [diff] [blame] | 270 | private static final String TEST_CAPPORT_API_URL = "https://capport.example.com/api"; |
| 271 | private static final String TEST_LOGIN_URL = "https://testportal.example.com/login"; |
| 272 | private static final String TEST_VENUE_INFO_URL = "https://venue.example.com/info"; |
lucaslin | e8d5403 | 2020-04-29 02:35:19 +0800 | [diff] [blame] | 273 | private static final String TEST_SPEED_TEST_URL = "https://speedtest.example.com"; |
Chiachang Wang | 220428b | 2020-05-29 10:03:41 +0800 | [diff] [blame] | 274 | private static final String TEST_RELATIVE_URL = "/test/relative/gen_204"; |
Chiachang Wang | caa3520 | 2019-02-26 11:32:18 +0800 | [diff] [blame] | 275 | private static final String TEST_MCCMNC = "123456"; |
Hai Shalom | e8fe123 | 2021-02-08 11:47:15 -0800 | [diff] [blame] | 276 | private static final String TEST_FRIENDLY_NAME = "Friendly Name"; |
Chiachang Wang | 80be441 | 2020-04-14 11:52:10 +0800 | [diff] [blame] | 277 | private static final String[] TEST_HTTP_URLS = {TEST_HTTP_OTHER_URL1, TEST_HTTP_OTHER_URL2}; |
| 278 | private static final String[] TEST_HTTPS_URLS = {TEST_HTTPS_OTHER_URL1, TEST_HTTPS_OTHER_URL2}; |
Chiachang Wang | 9af1cb5 | 2020-05-20 15:14:46 +0800 | [diff] [blame] | 279 | private static final int TEST_TCP_FAIL_RATE = 99; |
| 280 | private static final int TEST_TCP_PACKET_COUNT = 50; |
| 281 | private static final long TEST_ELAPSED_TIME_MS = 123456789L; |
| 282 | private static final int TEST_SIGNAL_STRENGTH = -100; |
Chiachang Wang | 813ee47 | 2019-05-23 16:29:30 +0800 | [diff] [blame] | 283 | private static final int VALIDATION_RESULT_INVALID = 0; |
| 284 | private static final int VALIDATION_RESULT_PORTAL = 0; |
| 285 | private static final String TEST_REDIRECT_URL = "android.com"; |
Remi NGUYEN VAN | 0402afc | 2020-04-15 18:56:59 +0900 | [diff] [blame] | 286 | private static final int PROBES_PRIVDNS_VALID = NETWORK_VALIDATION_PROBE_DNS |
lucaslin | 2ce7dcc | 2019-10-22 16:59:39 +0800 | [diff] [blame] | 287 | | NETWORK_VALIDATION_PROBE_HTTPS | NETWORK_VALIDATION_PROBE_PRIVDNS; |
Chiachang Wang | 813ee47 | 2019-05-23 16:29:30 +0800 | [diff] [blame] | 288 | |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 289 | private static final int RETURN_CODE_DNS_SUCCESS = 0; |
| 290 | private static final int RETURN_CODE_DNS_TIMEOUT = 255; |
Chiachang Wang | caa3520 | 2019-02-26 11:32:18 +0800 | [diff] [blame] | 291 | private static final int DEFAULT_DNS_TIMEOUT_THRESHOLD = 5; |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 292 | |
| 293 | private static final int HANDLER_TIMEOUT_MS = 1000; |
Chiachang Wang | 4fcbe91 | 2021-04-26 13:02:22 +0800 | [diff] [blame] | 294 | private static final int TEST_MIN_STALL_EVALUATE_INTERVAL_MS = 500; |
| 295 | private static final int STALL_EXPECTED_LAST_PROBE_TIME_MS = |
| 296 | TEST_MIN_STALL_EVALUATE_INTERVAL_MS + HANDLER_TIMEOUT_MS; |
Remi NGUYEN VAN | dc6e640 | 2019-03-27 15:42:53 +0900 | [diff] [blame] | 297 | private static final LinkProperties TEST_LINK_PROPERTIES = new LinkProperties(); |
Remi NGUYEN VAN | 2fa48c2 | 2019-12-09 16:40:02 +0900 | [diff] [blame] | 298 | |
Remi NGUYEN VAN | 905b0b6 | 2020-04-20 17:06:27 +0900 | [diff] [blame] | 299 | // Cannot have a static member for the LinkProperties with captive portal API information, as |
| 300 | // the initializer would crash on Q (the members in LinkProperties were introduced in R). |
| 301 | private static LinkProperties makeCapportLPs() { |
Remi NGUYEN VAN | 2fa48c2 | 2019-12-09 16:40:02 +0900 | [diff] [blame] | 302 | final LinkProperties lp = new LinkProperties(TEST_LINK_PROPERTIES); |
| 303 | lp.setCaptivePortalApiUrl(Uri.parse(TEST_CAPPORT_API_URL)); |
| 304 | return lp; |
| 305 | } |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 306 | |
Chiachang Wang | daf983b | 2020-05-07 19:20:52 +0800 | [diff] [blame] | 307 | private static final NetworkCapabilities CELL_METERED_CAPABILITIES = new NetworkCapabilities() |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 308 | .addTransportType(NetworkCapabilities.TRANSPORT_CELLULAR) |
| 309 | .addCapability(NET_CAPABILITY_INTERNET); |
| 310 | |
Chiachang Wang | daf983b | 2020-05-07 19:20:52 +0800 | [diff] [blame] | 311 | private static final NetworkCapabilities CELL_NOT_METERED_CAPABILITIES = |
| 312 | new NetworkCapabilities() |
| 313 | .addTransportType(NetworkCapabilities.TRANSPORT_CELLULAR) |
| 314 | .addCapability(NET_CAPABILITY_INTERNET) |
| 315 | .addCapability(NetworkCapabilities.NET_CAPABILITY_NOT_METERED); |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 316 | |
Chiachang Wang | 9af1cb5 | 2020-05-20 15:14:46 +0800 | [diff] [blame] | 317 | private static final NetworkCapabilities WIFI_NOT_METERED_CAPABILITIES = |
| 318 | new NetworkCapabilities() |
| 319 | .addTransportType(NetworkCapabilities.TRANSPORT_WIFI) |
| 320 | .addCapability(NET_CAPABILITY_INTERNET) |
| 321 | .addCapability(NetworkCapabilities.NET_CAPABILITY_NOT_METERED); |
| 322 | |
Chiachang Wang | daf983b | 2020-05-07 19:20:52 +0800 | [diff] [blame] | 323 | private static final NetworkCapabilities CELL_NO_INTERNET_CAPABILITIES = |
| 324 | new NetworkCapabilities().addTransportType(NetworkCapabilities.TRANSPORT_CELLULAR); |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 325 | |
Pavan Kumar M | 2fbc846 | 2021-04-28 11:20:25 +0530 | [diff] [blame] | 326 | private static final NetworkCapabilities WIFI_OEM_PAID_CAPABILITIES = |
| 327 | new NetworkCapabilities() |
| 328 | .addTransportType(NetworkCapabilities.TRANSPORT_WIFI) |
| 329 | .addCapability(NET_CAPABILITY_INTERNET) |
| 330 | .addCapability(NET_CAPABILITY_NOT_METERED) |
| 331 | .addCapability(NET_CAPABILITY_OEM_PAID) |
| 332 | .removeCapability(NET_CAPABILITY_NOT_RESTRICTED); |
| 333 | |
Lorenzo Colitti | cfc1abc | 2019-04-28 00:55:19 +0900 | [diff] [blame] | 334 | /** |
| 335 | * Fakes DNS responses. |
| 336 | * |
| 337 | * Allows test methods to configure the IP addresses that will be resolved by |
| 338 | * Network#getAllByName and by DnsResolver#query. |
| 339 | */ |
| 340 | class FakeDns { |
Chiachang Wang | eb61922 | 2019-07-03 20:52:08 +0800 | [diff] [blame] | 341 | /** Data class to record the Dns entry. */ |
| 342 | class DnsEntry { |
| 343 | final String mHostname; |
| 344 | final int mType; |
| 345 | final List<InetAddress> mAddresses; |
| 346 | DnsEntry(String host, int type, List<InetAddress> addr) { |
| 347 | mHostname = host; |
| 348 | mType = type; |
| 349 | mAddresses = addr; |
| 350 | } |
| 351 | // Full match or partial match that target host contains the entry hostname to support |
| 352 | // random private dns probe hostname. |
| 353 | private boolean matches(String hostname, int type) { |
| 354 | return hostname.endsWith(mHostname) && type == mType; |
| 355 | } |
| 356 | } |
| 357 | private final ArrayList<DnsEntry> mAnswers = new ArrayList<DnsEntry>(); |
Lorenzo Colitti | cfc1abc | 2019-04-28 00:55:19 +0900 | [diff] [blame] | 358 | private boolean mNonBypassPrivateDnsWorking = true; |
| 359 | |
| 360 | /** Whether DNS queries on mNonBypassPrivateDnsWorking should succeed. */ |
| 361 | private void setNonBypassPrivateDnsWorking(boolean working) { |
| 362 | mNonBypassPrivateDnsWorking = working; |
Lorenzo Colitti | 171cfd2 | 2019-04-18 13:44:32 +0900 | [diff] [blame] | 363 | } |
| 364 | |
Lorenzo Colitti | cfc1abc | 2019-04-28 00:55:19 +0900 | [diff] [blame] | 365 | /** Clears all DNS entries. */ |
| 366 | private synchronized void clearAll() { |
| 367 | mAnswers.clear(); |
Lorenzo Colitti | 171cfd2 | 2019-04-18 13:44:32 +0900 | [diff] [blame] | 368 | } |
Lorenzo Colitti | 171cfd2 | 2019-04-18 13:44:32 +0900 | [diff] [blame] | 369 | |
Chiachang Wang | eb61922 | 2019-07-03 20:52:08 +0800 | [diff] [blame] | 370 | /** Returns the answer for a given name and type on the given mock network. */ |
| 371 | private synchronized List<InetAddress> getAnswer(Object mock, String hostname, int type) { |
Lorenzo Colitti | 7f9734f | 2019-05-09 12:13:54 +0900 | [diff] [blame] | 372 | if (mock == mNetwork && !mNonBypassPrivateDnsWorking) { |
Lorenzo Colitti | cfc1abc | 2019-04-28 00:55:19 +0900 | [diff] [blame] | 373 | return null; |
| 374 | } |
Chiachang Wang | eb61922 | 2019-07-03 20:52:08 +0800 | [diff] [blame] | 375 | |
Lorenzo Colitti | b891b5e | 2020-02-05 16:48:09 +0900 | [diff] [blame] | 376 | return mAnswers.stream().filter(e -> e.matches(hostname, type)) |
| 377 | .map(answer -> answer.mAddresses).findFirst().orElse(null); |
Lorenzo Colitti | cfc1abc | 2019-04-28 00:55:19 +0900 | [diff] [blame] | 378 | } |
Lorenzo Colitti | 171cfd2 | 2019-04-18 13:44:32 +0900 | [diff] [blame] | 379 | |
Chiachang Wang | eb61922 | 2019-07-03 20:52:08 +0800 | [diff] [blame] | 380 | /** Sets the answer for a given name and type. */ |
| 381 | private synchronized void setAnswer(String hostname, String[] answer, int type) |
Lorenzo Colitti | cfc1abc | 2019-04-28 00:55:19 +0900 | [diff] [blame] | 382 | throws UnknownHostException { |
Chiachang Wang | eb61922 | 2019-07-03 20:52:08 +0800 | [diff] [blame] | 383 | DnsEntry record = new DnsEntry(hostname, type, generateAnswer(answer)); |
| 384 | // Remove the existing one. |
| 385 | mAnswers.removeIf(entry -> entry.matches(hostname, type)); |
| 386 | // Add or replace a new record. |
| 387 | mAnswers.add(record); |
| 388 | } |
| 389 | |
| 390 | private List<InetAddress> generateAnswer(String[] answer) { |
| 391 | if (answer == null) return new ArrayList<>(); |
Lorenzo Colitti | b891b5e | 2020-02-05 16:48:09 +0900 | [diff] [blame] | 392 | return Arrays.stream(answer).map(addr -> InetAddress.parseNumericAddress(addr)) |
| 393 | .collect(toList()); |
Lorenzo Colitti | cfc1abc | 2019-04-28 00:55:19 +0900 | [diff] [blame] | 394 | } |
| 395 | |
| 396 | /** Simulates a getAllByName call for the specified name on the specified mock network. */ |
| 397 | private InetAddress[] getAllByName(Object mock, String hostname) |
| 398 | throws UnknownHostException { |
Chiachang Wang | eb61922 | 2019-07-03 20:52:08 +0800 | [diff] [blame] | 399 | List<InetAddress> answer = queryAllTypes(mock, hostname); |
Lorenzo Colitti | cfc1abc | 2019-04-28 00:55:19 +0900 | [diff] [blame] | 400 | if (answer == null || answer.size() == 0) { |
| 401 | throw new UnknownHostException(hostname); |
| 402 | } |
| 403 | return answer.toArray(new InetAddress[0]); |
| 404 | } |
| 405 | |
Chiachang Wang | eb61922 | 2019-07-03 20:52:08 +0800 | [diff] [blame] | 406 | // Regardless of the type, depends on what the responses contained in the network. |
| 407 | private List<InetAddress> queryAllTypes(Object mock, String hostname) { |
| 408 | List<InetAddress> answer = new ArrayList<>(); |
| 409 | addAllIfNotNull(answer, getAnswer(mock, hostname, TYPE_A)); |
| 410 | addAllIfNotNull(answer, getAnswer(mock, hostname, TYPE_AAAA)); |
| 411 | return answer; |
| 412 | } |
| 413 | |
| 414 | private void addAllIfNotNull(List<InetAddress> list, List<InetAddress> c) { |
| 415 | if (c != null) { |
| 416 | list.addAll(c); |
| 417 | } |
| 418 | } |
| 419 | |
Lorenzo Colitti | cfc1abc | 2019-04-28 00:55:19 +0900 | [diff] [blame] | 420 | /** Starts mocking DNS queries. */ |
| 421 | private void startMocking() throws UnknownHostException { |
Lorenzo Colitti | 7f9734f | 2019-05-09 12:13:54 +0900 | [diff] [blame] | 422 | // Queries on mNetwork using getAllByName. |
Lorenzo Colitti | cfc1abc | 2019-04-28 00:55:19 +0900 | [diff] [blame] | 423 | doAnswer(invocation -> { |
| 424 | return getAllByName(invocation.getMock(), invocation.getArgument(0)); |
| 425 | }).when(mNetwork).getAllByName(any()); |
| 426 | |
Lorenzo Colitti | 7f9734f | 2019-05-09 12:13:54 +0900 | [diff] [blame] | 427 | // Queries on mCleartextDnsNetwork using DnsResolver#query. |
Lorenzo Colitti | cfc1abc | 2019-04-28 00:55:19 +0900 | [diff] [blame] | 428 | doAnswer(invocation -> { |
Chiachang Wang | ddb7da6 | 2019-06-03 15:50:53 +0800 | [diff] [blame] | 429 | return mockQuery(invocation, 1 /* posHostname */, 3 /* posExecutor */, |
Chiachang Wang | eb61922 | 2019-07-03 20:52:08 +0800 | [diff] [blame] | 430 | 5 /* posCallback */, -1 /* posType */); |
Lorenzo Colitti | cfc1abc | 2019-04-28 00:55:19 +0900 | [diff] [blame] | 431 | }).when(mDnsResolver).query(any(), any(), anyInt(), any(), any(), any()); |
Chiachang Wang | 1c67f4e | 2019-05-09 21:28:47 +0800 | [diff] [blame] | 432 | |
Chiachang Wang | ddb7da6 | 2019-06-03 15:50:53 +0800 | [diff] [blame] | 433 | // Queries on mCleartextDnsNetwork using DnsResolver#query with QueryType. |
Chiachang Wang | 1c67f4e | 2019-05-09 21:28:47 +0800 | [diff] [blame] | 434 | doAnswer(invocation -> { |
Chiachang Wang | ddb7da6 | 2019-06-03 15:50:53 +0800 | [diff] [blame] | 435 | return mockQuery(invocation, 1 /* posHostname */, 4 /* posExecutor */, |
Chiachang Wang | eb61922 | 2019-07-03 20:52:08 +0800 | [diff] [blame] | 436 | 6 /* posCallback */, 2 /* posType */); |
Chiachang Wang | 1c67f4e | 2019-05-09 21:28:47 +0800 | [diff] [blame] | 437 | }).when(mDnsResolver).query(any(), any(), anyInt(), anyInt(), any(), any(), any()); |
Lorenzo Colitti | cfc1abc | 2019-04-28 00:55:19 +0900 | [diff] [blame] | 438 | } |
Chiachang Wang | ddb7da6 | 2019-06-03 15:50:53 +0800 | [diff] [blame] | 439 | |
| 440 | // Mocking queries on DnsResolver#query. |
| 441 | private Answer mockQuery(InvocationOnMock invocation, int posHostname, int posExecutor, |
Chiachang Wang | eb61922 | 2019-07-03 20:52:08 +0800 | [diff] [blame] | 442 | int posCallback, int posType) { |
Chiachang Wang | ddb7da6 | 2019-06-03 15:50:53 +0800 | [diff] [blame] | 443 | String hostname = (String) invocation.getArgument(posHostname); |
| 444 | Executor executor = (Executor) invocation.getArgument(posExecutor); |
| 445 | DnsResolver.Callback<List<InetAddress>> callback = invocation.getArgument(posCallback); |
Chiachang Wang | eb61922 | 2019-07-03 20:52:08 +0800 | [diff] [blame] | 446 | List<InetAddress> answer; |
Chiachang Wang | ddb7da6 | 2019-06-03 15:50:53 +0800 | [diff] [blame] | 447 | |
Chiachang Wang | eb61922 | 2019-07-03 20:52:08 +0800 | [diff] [blame] | 448 | answer = posType != -1 |
| 449 | ? getAnswer(invocation.getMock(), hostname, invocation.getArgument(posType)) : |
| 450 | queryAllTypes(invocation.getMock(), hostname); |
| 451 | |
Chiachang Wang | ddb7da6 | 2019-06-03 15:50:53 +0800 | [diff] [blame] | 452 | if (answer != null && answer.size() > 0) { |
| 453 | new Handler(Looper.getMainLooper()).post(() -> { |
| 454 | executor.execute(() -> callback.onAnswer(answer, 0)); |
| 455 | }); |
| 456 | } |
| 457 | // If no answers, do nothing. sendDnsProbeWithTimeout will time out and throw UHE. |
| 458 | return null; |
| 459 | } |
Lorenzo Colitti | 171cfd2 | 2019-04-18 13:44:32 +0900 | [diff] [blame] | 460 | } |
| 461 | |
Lorenzo Colitti | cfc1abc | 2019-04-28 00:55:19 +0900 | [diff] [blame] | 462 | private FakeDns mFakeDns; |
| 463 | |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 464 | @Before |
Remi NGUYEN VAN | b4af130 | 2019-06-11 16:17:46 +0900 | [diff] [blame] | 465 | public void setUp() throws Exception { |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 466 | MockitoAnnotations.initMocks(this); |
Lorenzo Colitti | 7f9734f | 2019-05-09 12:13:54 +0900 | [diff] [blame] | 467 | when(mDependencies.getPrivateDnsBypassNetwork(any())).thenReturn(mCleartextDnsNetwork); |
Lorenzo Colitti | 171cfd2 | 2019-04-18 13:44:32 +0900 | [diff] [blame] | 468 | when(mDependencies.getDnsResolver()).thenReturn(mDnsResolver); |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 469 | when(mDependencies.getRandom()).thenReturn(mRandom); |
| 470 | when(mDependencies.getSetting(any(), eq(Settings.Global.CAPTIVE_PORTAL_MODE), anyInt())) |
| 471 | .thenReturn(Settings.Global.CAPTIVE_PORTAL_MODE_PROMPT); |
Chiachang Wang | 79a6da3 | 2019-04-17 17:00:54 +0800 | [diff] [blame] | 472 | when(mDependencies.getDeviceConfigPropertyInt(any(), eq(CAPTIVE_PORTAL_USE_HTTPS), |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 473 | anyInt())).thenReturn(1); |
Niklas Lindgren | 0c90488 | 2018-12-07 11:08:04 +0100 | [diff] [blame] | 474 | when(mDependencies.getSetting(any(), eq(Settings.Global.CAPTIVE_PORTAL_HTTP_URL), any())) |
| 475 | .thenReturn(TEST_HTTP_URL); |
| 476 | when(mDependencies.getSetting(any(), eq(Settings.Global.CAPTIVE_PORTAL_HTTPS_URL), any())) |
| 477 | .thenReturn(TEST_HTTPS_URL); |
| 478 | |
Lorenzo Colitti | 7f9734f | 2019-05-09 12:13:54 +0900 | [diff] [blame] | 479 | doReturn(mCleartextDnsNetwork).when(mNetwork).getPrivateDnsBypassingCopy(); |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 480 | |
| 481 | when(mContext.getSystemService(Context.CONNECTIVITY_SERVICE)).thenReturn(mCm); |
| 482 | when(mContext.getSystemService(Context.TELEPHONY_SERVICE)).thenReturn(mTelephony); |
| 483 | when(mContext.getSystemService(Context.WIFI_SERVICE)).thenReturn(mWifi); |
Niklas Lindgren | 0c90488 | 2018-12-07 11:08:04 +0100 | [diff] [blame] | 484 | when(mContext.getResources()).thenReturn(mResources); |
| 485 | |
Remi NGUYEN VAN | 89cd026 | 2019-12-29 22:46:00 +0900 | [diff] [blame] | 486 | when(mServiceManager.getNotifier()).thenReturn(mNotifier); |
| 487 | |
Chiachang Wang | 9af1cb5 | 2020-05-20 15:14:46 +0800 | [diff] [blame] | 488 | when(mTelephony.getDataNetworkType()).thenReturn(TelephonyManager.NETWORK_TYPE_LTE); |
| 489 | when(mTelephony.getNetworkOperator()).thenReturn(TEST_MCCMNC); |
| 490 | when(mTelephony.getSimOperator()).thenReturn(TEST_MCCMNC); |
| 491 | |
Niklas Lindgren | 0c90488 | 2018-12-07 11:08:04 +0100 | [diff] [blame] | 492 | when(mResources.getString(anyInt())).thenReturn(""); |
| 493 | when(mResources.getStringArray(anyInt())).thenReturn(new String[0]); |
Chiachang Wang | 2482099 | 2020-03-23 21:16:36 +0800 | [diff] [blame] | 494 | doReturn(mConfiguration).when(mResources).getConfiguration(); |
| 495 | when(mMccContext.getResources()).thenReturn(mMccResource); |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 496 | |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 497 | setFallbackUrl(TEST_FALLBACK_URL); |
| 498 | setOtherFallbackUrls(TEST_OTHER_FALLBACK_URL); |
| 499 | setFallbackSpecs(null); // Test with no fallback spec by default |
| 500 | when(mRandom.nextInt()).thenReturn(0); |
| 501 | |
Chiachang Wang | cded6ce | 2019-12-18 17:27:57 +0800 | [diff] [blame] | 502 | when(mTstDependencies.getNetd()).thenReturn(mNetd); |
Chiachang Wang | c11639d | 2019-05-15 16:18:56 +0800 | [diff] [blame] | 503 | // DNS probe timeout should not be defined more than half of HANDLER_TIMEOUT_MS. Otherwise, |
| 504 | // 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] | 505 | when(mResources.getInteger(eq(R.integer.config_captive_portal_dns_probe_timeout))) |
Chiachang Wang | c11639d | 2019-05-15 16:18:56 +0800 | [diff] [blame] | 506 | .thenReturn(200); |
Lorenzo Colitti | cfc1abc | 2019-04-28 00:55:19 +0900 | [diff] [blame] | 507 | |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 508 | doAnswer((invocation) -> { |
| 509 | URL url = invocation.getArgument(0); |
| 510 | switch(url.toString()) { |
| 511 | case TEST_HTTP_URL: |
| 512 | return mHttpConnection; |
Chiachang Wang | 80be441 | 2020-04-14 11:52:10 +0800 | [diff] [blame] | 513 | case TEST_HTTP_OTHER_URL1: |
| 514 | return mOtherHttpConnection1; |
| 515 | case TEST_HTTP_OTHER_URL2: |
| 516 | return mOtherHttpConnection2; |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 517 | case TEST_HTTPS_URL: |
| 518 | return mHttpsConnection; |
Chiachang Wang | 80be441 | 2020-04-14 11:52:10 +0800 | [diff] [blame] | 519 | case TEST_HTTPS_OTHER_URL1: |
| 520 | return mOtherHttpsConnection1; |
| 521 | case TEST_HTTPS_OTHER_URL2: |
| 522 | return mOtherHttpsConnection2; |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 523 | case TEST_FALLBACK_URL: |
| 524 | return mFallbackConnection; |
| 525 | case TEST_OTHER_FALLBACK_URL: |
| 526 | return mOtherFallbackConnection; |
Remi NGUYEN VAN | 943d634 | 2020-03-24 18:22:34 +0900 | [diff] [blame] | 527 | case TEST_OVERRIDE_URL: |
| 528 | case TEST_INVALID_OVERRIDE_URL: |
| 529 | return mTestOverriddenUrlConnection; |
Remi NGUYEN VAN | 2fa48c2 | 2019-12-09 16:40:02 +0900 | [diff] [blame] | 530 | case TEST_CAPPORT_API_URL: |
| 531 | return mCapportApiConnection; |
lucaslin | e8d5403 | 2020-04-29 02:35:19 +0800 | [diff] [blame] | 532 | case TEST_SPEED_TEST_URL: |
| 533 | return mSpeedTestConnection; |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 534 | default: |
| 535 | fail("URL not mocked: " + url.toString()); |
| 536 | return null; |
| 537 | } |
Lorenzo Colitti | 7f9734f | 2019-05-09 12:13:54 +0900 | [diff] [blame] | 538 | }).when(mCleartextDnsNetwork).openConnection(any()); |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 539 | when(mHttpConnection.getRequestProperties()).thenReturn(new ArrayMap<>()); |
| 540 | when(mHttpsConnection.getRequestProperties()).thenReturn(new ArrayMap<>()); |
Lorenzo Colitti | 171cfd2 | 2019-04-18 13:44:32 +0900 | [diff] [blame] | 541 | |
Lorenzo Colitti | cfc1abc | 2019-04-28 00:55:19 +0900 | [diff] [blame] | 542 | mFakeDns = new FakeDns(); |
| 543 | mFakeDns.startMocking(); |
Chiachang Wang | eb61922 | 2019-07-03 20:52:08 +0800 | [diff] [blame] | 544 | // Set private dns suffix answer. sendPrivateDnsProbe() in NetworkMonitor send probe with |
| 545 | // one time hostname. The hostname will be [random generated UUID] + HOST_SUFFIX differently |
| 546 | // each time. That means the host answer cannot be pre-set into the answer list. Thus, set |
| 547 | // the host suffix and use partial match in FakeDns to match the target host and reply the |
| 548 | // intended answer. |
| 549 | mFakeDns.setAnswer(PRIVATE_DNS_PROBE_HOST_SUFFIX, new String[]{"192.0.2.2"}, TYPE_A); |
| 550 | mFakeDns.setAnswer(PRIVATE_DNS_PROBE_HOST_SUFFIX, new String[]{"2001:db8::1"}, TYPE_AAAA); |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 551 | |
Lorenzo Colitti | 37ee46f | 2019-04-08 21:51:41 +0900 | [diff] [blame] | 552 | when(mContext.registerReceiver(any(BroadcastReceiver.class), any())).then((invocation) -> { |
| 553 | mRegisteredReceivers.add(invocation.getArgument(0)); |
| 554 | return new Intent(); |
| 555 | }); |
| 556 | |
| 557 | doAnswer((invocation) -> { |
| 558 | mRegisteredReceivers.remove(invocation.getArgument(0)); |
| 559 | return null; |
| 560 | }).when(mContext).unregisterReceiver(any()); |
| 561 | |
Remi NGUYEN VAN | b4af130 | 2019-06-11 16:17:46 +0900 | [diff] [blame] | 562 | resetCallbacks(); |
| 563 | |
Chiachang Wang | 4fcbe91 | 2021-04-26 13:02:22 +0800 | [diff] [blame] | 564 | setMinDataStallEvaluateInterval(TEST_MIN_STALL_EVALUATE_INTERVAL_MS); |
Chiachang Wang | 0e87479 | 2019-03-05 20:31:57 +0800 | [diff] [blame] | 565 | setDataStallEvaluationType(DATA_STALL_EVALUATION_TYPE_DNS); |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 566 | setValidDataStallDnsTimeThreshold(500); |
| 567 | setConsecutiveDnsTimeoutThreshold(5); |
Lorenzo Colitti | 37ee46f | 2019-04-08 21:51:41 +0900 | [diff] [blame] | 568 | mCreatedNetworkMonitors = new HashSet<>(); |
| 569 | mRegisteredReceivers = new HashSet<>(); |
Chiachang Wang | 26a626a | 2020-03-02 17:41:58 +0800 | [diff] [blame] | 570 | setDismissPortalInValidatedNetwork(false); |
Lorenzo Colitti | 37ee46f | 2019-04-08 21:51:41 +0900 | [diff] [blame] | 571 | } |
| 572 | |
| 573 | @After |
| 574 | public void tearDown() { |
Lorenzo Colitti | cfc1abc | 2019-04-28 00:55:19 +0900 | [diff] [blame] | 575 | mFakeDns.clearAll(); |
Lorenzo Colitti | 37ee46f | 2019-04-08 21:51:41 +0900 | [diff] [blame] | 576 | // Make a local copy of mCreatedNetworkMonitors because during the iteration below, |
| 577 | // WrappedNetworkMonitor#onQuitting will delete elements from it on the handler threads. |
| 578 | WrappedNetworkMonitor[] networkMonitors = mCreatedNetworkMonitors.toArray( |
| 579 | new WrappedNetworkMonitor[0]); |
| 580 | for (WrappedNetworkMonitor nm : networkMonitors) { |
| 581 | nm.notifyNetworkDisconnected(); |
| 582 | nm.awaitQuit(); |
| 583 | } |
| 584 | assertEquals("NetworkMonitor still running after disconnect", |
| 585 | 0, mCreatedNetworkMonitors.size()); |
| 586 | assertEquals("BroadcastReceiver still registered after disconnect", |
| 587 | 0, mRegisteredReceivers.size()); |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 588 | } |
| 589 | |
Remi NGUYEN VAN | b4af130 | 2019-06-11 16:17:46 +0900 | [diff] [blame] | 590 | private void resetCallbacks() { |
Cody Kesting | 176bce7 | 2020-01-20 18:09:59 -0800 | [diff] [blame] | 591 | resetCallbacks(6); |
| 592 | } |
| 593 | |
| 594 | private void resetCallbacks(int interfaceVersion) { |
Remi NGUYEN VAN | b4af130 | 2019-06-11 16:17:46 +0900 | [diff] [blame] | 595 | reset(mCallbacks); |
Remi NGUYEN VAN | b4af130 | 2019-06-11 16:17:46 +0900 | [diff] [blame] | 596 | try { |
Remi NGUYEN VAN | 905b0b6 | 2020-04-20 17:06:27 +0900 | [diff] [blame] | 597 | doReturn(interfaceVersion).when(mCallbacks).getInterfaceVersion(); |
Remi NGUYEN VAN | b4af130 | 2019-06-11 16:17:46 +0900 | [diff] [blame] | 598 | } catch (RemoteException e) { |
| 599 | // Can't happen as mCallbacks is a mock |
| 600 | fail("Error mocking getInterfaceVersion" + e); |
| 601 | } |
| 602 | } |
| 603 | |
Chiachang Wang | 9af1cb5 | 2020-05-20 15:14:46 +0800 | [diff] [blame] | 604 | private TcpSocketTracker getTcpSocketTrackerOrNull(NetworkMonitor.Dependencies dp) { |
| 605 | return ((dp.getDeviceConfigPropertyInt( |
| 606 | NAMESPACE_CONNECTIVITY, |
| 607 | CONFIG_DATA_STALL_EVALUATION_TYPE, |
| 608 | DEFAULT_DATA_STALL_EVALUATION_TYPES) |
| 609 | & DATA_STALL_EVALUATION_TYPE_TCP) != 0) ? mTst : null; |
| 610 | } |
| 611 | |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 612 | private class WrappedNetworkMonitor extends NetworkMonitor { |
| 613 | private long mProbeTime = 0; |
Lorenzo Colitti | 37ee46f | 2019-04-08 21:51:41 +0900 | [diff] [blame] | 614 | private final ConditionVariable mQuitCv = new ConditionVariable(false); |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 615 | |
Remi NGUYEN VAN | dc6e640 | 2019-03-27 15:42:53 +0900 | [diff] [blame] | 616 | WrappedNetworkMonitor() { |
Remi NGUYEN VAN | 89cd026 | 2019-12-29 22:46:00 +0900 | [diff] [blame] | 617 | super(mContext, mCallbacks, mNetwork, mLogger, mValidationLogger, mServiceManager, |
Chiachang Wang | 9af1cb5 | 2020-05-20 15:14:46 +0800 | [diff] [blame] | 618 | mDependencies, getTcpSocketTrackerOrNull(mDependencies)); |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 619 | } |
| 620 | |
| 621 | @Override |
| 622 | protected long getLastProbeTime() { |
| 623 | return mProbeTime; |
| 624 | } |
| 625 | |
| 626 | protected void setLastProbeTime(long time) { |
| 627 | mProbeTime = time; |
| 628 | } |
Chiachang Wang | caa3520 | 2019-02-26 11:32:18 +0800 | [diff] [blame] | 629 | |
| 630 | @Override |
| 631 | protected void addDnsEvents(@NonNull final DataStallDetectionStats.Builder stats) { |
Chiachang Wang | 9af1cb5 | 2020-05-20 15:14:46 +0800 | [diff] [blame] | 632 | if ((getDataStallEvaluationType() & DATA_STALL_EVALUATION_TYPE_DNS) != 0) { |
| 633 | generateTimeoutDnsEvent(stats, DEFAULT_DNS_TIMEOUT_THRESHOLD); |
| 634 | } |
Chiachang Wang | caa3520 | 2019-02-26 11:32:18 +0800 | [diff] [blame] | 635 | } |
Lorenzo Colitti | 37ee46f | 2019-04-08 21:51:41 +0900 | [diff] [blame] | 636 | |
| 637 | @Override |
| 638 | protected void onQuitting() { |
| 639 | assertTrue(mCreatedNetworkMonitors.remove(this)); |
| 640 | mQuitCv.open(); |
| 641 | } |
| 642 | |
| 643 | protected void awaitQuit() { |
| 644 | assertTrue("NetworkMonitor did not quit after " + HANDLER_TIMEOUT_MS + "ms", |
| 645 | mQuitCv.block(HANDLER_TIMEOUT_MS)); |
| 646 | } |
lucaslin | 9b4dfab | 2019-12-17 23:06:12 +0800 | [diff] [blame] | 647 | |
| 648 | protected Context getContext() { |
| 649 | return mContext; |
| 650 | } |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 651 | } |
| 652 | |
Lorenzo Colitti | cfc1abc | 2019-04-28 00:55:19 +0900 | [diff] [blame] | 653 | private WrappedNetworkMonitor makeMonitor(NetworkCapabilities nc) { |
Remi NGUYEN VAN | dc6e640 | 2019-03-27 15:42:53 +0900 | [diff] [blame] | 654 | final WrappedNetworkMonitor nm = new WrappedNetworkMonitor(); |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 655 | nm.start(); |
Lorenzo Colitti | cfc1abc | 2019-04-28 00:55:19 +0900 | [diff] [blame] | 656 | setNetworkCapabilities(nm, nc); |
Chalard Jean | 23a0630 | 2020-06-26 00:41:00 +0900 | [diff] [blame] | 657 | HandlerUtils.waitForIdle(nm.getHandler(), HANDLER_TIMEOUT_MS); |
Lorenzo Colitti | 37ee46f | 2019-04-08 21:51:41 +0900 | [diff] [blame] | 658 | mCreatedNetworkMonitors.add(nm); |
Chiachang Wang | a5716bf | 2019-11-20 16:13:07 +0800 | [diff] [blame] | 659 | when(mTstDependencies.isTcpInfoParsingSupported()).thenReturn(false); |
Chiachang Wang | 4336b91 | 2019-11-26 15:39:22 +0800 | [diff] [blame] | 660 | |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 661 | return nm; |
| 662 | } |
| 663 | |
Chiachang Wang | daf983b | 2020-05-07 19:20:52 +0800 | [diff] [blame] | 664 | private WrappedNetworkMonitor makeCellMeteredNetworkMonitor() { |
| 665 | final WrappedNetworkMonitor nm = makeMonitor(CELL_METERED_CAPABILITIES); |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 666 | return nm; |
| 667 | } |
| 668 | |
Chiachang Wang | daf983b | 2020-05-07 19:20:52 +0800 | [diff] [blame] | 669 | private WrappedNetworkMonitor makeCellNotMeteredNetworkMonitor() { |
| 670 | final WrappedNetworkMonitor nm = makeMonitor(CELL_NOT_METERED_CAPABILITIES); |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 671 | return nm; |
| 672 | } |
| 673 | |
Chiachang Wang | 9af1cb5 | 2020-05-20 15:14:46 +0800 | [diff] [blame] | 674 | private WrappedNetworkMonitor makeWifiNotMeteredNetworkMonitor() { |
| 675 | final WrappedNetworkMonitor nm = makeMonitor(WIFI_NOT_METERED_CAPABILITIES); |
| 676 | return nm; |
| 677 | } |
| 678 | |
Remi NGUYEN VAN | dc6e640 | 2019-03-27 15:42:53 +0900 | [diff] [blame] | 679 | private void setNetworkCapabilities(NetworkMonitor nm, NetworkCapabilities nc) { |
| 680 | nm.notifyNetworkCapabilitiesChanged(nc); |
Chalard Jean | 23a0630 | 2020-06-26 00:41:00 +0900 | [diff] [blame] | 681 | HandlerUtils.waitForIdle(nm.getHandler(), HANDLER_TIMEOUT_MS); |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 682 | } |
| 683 | |
| 684 | @Test |
lucaslin | e8d5403 | 2020-04-29 02:35:19 +0800 | [diff] [blame] | 685 | public void testOnlyWifiTransport() { |
Chiachang Wang | acb2b70 | 2020-05-20 15:41:05 +0800 | [diff] [blame] | 686 | final WrappedNetworkMonitor wnm = makeMonitor(CELL_METERED_CAPABILITIES); |
| 687 | assertFalse(wnm.onlyWifiTransport()); |
lucaslin | e8d5403 | 2020-04-29 02:35:19 +0800 | [diff] [blame] | 688 | final NetworkCapabilities nc = new NetworkCapabilities() |
| 689 | .addTransportType(TRANSPORT_WIFI) |
| 690 | .addTransportType(TRANSPORT_VPN); |
| 691 | setNetworkCapabilities(wnm, nc); |
| 692 | assertFalse(wnm.onlyWifiTransport()); |
| 693 | nc.removeTransportType(TRANSPORT_VPN); |
| 694 | setNetworkCapabilities(wnm, nc); |
| 695 | assertTrue(wnm.onlyWifiTransport()); |
| 696 | } |
| 697 | |
| 698 | @Test |
| 699 | public void testNeedEvaluatingBandwidth() throws Exception { |
| 700 | // Make metered network first, the transport type is TRANSPORT_CELLULAR. That means the |
| 701 | // test cannot pass the condition check in needEvaluatingBandwidth(). |
Chiachang Wang | daf983b | 2020-05-07 19:20:52 +0800 | [diff] [blame] | 702 | final WrappedNetworkMonitor wnm1 = makeCellMeteredNetworkMonitor(); |
lucaslin | e8d5403 | 2020-04-29 02:35:19 +0800 | [diff] [blame] | 703 | // Don't set the config_evaluating_bandwidth_url to make |
| 704 | // the condition check fail in needEvaluatingBandwidth(). |
| 705 | assertFalse(wnm1.needEvaluatingBandwidth()); |
| 706 | // Make the NetworkCapabilities to have the TRANSPORT_WIFI but it still cannot meet the |
| 707 | // condition check. |
| 708 | final NetworkCapabilities nc = new NetworkCapabilities() |
| 709 | .addTransportType(TRANSPORT_WIFI); |
| 710 | setNetworkCapabilities(wnm1, nc); |
| 711 | assertFalse(wnm1.needEvaluatingBandwidth()); |
| 712 | // Make the network to be non-metered wifi but it still cannot meet the condition check |
| 713 | // since the config_evaluating_bandwidth_url is not set. |
| 714 | nc.addCapability(NET_CAPABILITY_NOT_METERED); |
| 715 | setNetworkCapabilities(wnm1, nc); |
| 716 | assertFalse(wnm1.needEvaluatingBandwidth()); |
| 717 | // All configurations are set correctly. |
| 718 | doReturn(TEST_SPEED_TEST_URL).when(mResources).getString( |
| 719 | R.string.config_evaluating_bandwidth_url); |
Chiachang Wang | daf983b | 2020-05-07 19:20:52 +0800 | [diff] [blame] | 720 | final WrappedNetworkMonitor wnm2 = makeCellMeteredNetworkMonitor(); |
lucaslin | e8d5403 | 2020-04-29 02:35:19 +0800 | [diff] [blame] | 721 | setNetworkCapabilities(wnm2, nc); |
| 722 | assertTrue(wnm2.needEvaluatingBandwidth()); |
| 723 | // Set mIsBandwidthCheckPassedOrIgnored to true and expect needEvaluatingBandwidth() will |
| 724 | // return false. |
| 725 | wnm2.mIsBandwidthCheckPassedOrIgnored = true; |
| 726 | assertFalse(wnm2.needEvaluatingBandwidth()); |
| 727 | // Reset mIsBandwidthCheckPassedOrIgnored back to false. |
| 728 | wnm2.mIsBandwidthCheckPassedOrIgnored = false; |
| 729 | // Shouldn't evaluate network bandwidth on the metered wifi. |
| 730 | nc.removeCapability(NET_CAPABILITY_NOT_METERED); |
| 731 | setNetworkCapabilities(wnm2, nc); |
| 732 | assertFalse(wnm2.needEvaluatingBandwidth()); |
| 733 | // Shouldn't evaluate network bandwidth on the unmetered cellular. |
| 734 | nc.addCapability(NET_CAPABILITY_NOT_METERED); |
| 735 | nc.removeTransportType(TRANSPORT_WIFI); |
| 736 | nc.addTransportType(TRANSPORT_CELLULAR); |
| 737 | assertFalse(wnm2.needEvaluatingBandwidth()); |
| 738 | } |
| 739 | |
| 740 | @Test |
| 741 | public void testEvaluatingBandwidthState_meteredNetwork() throws Exception { |
| 742 | setStatus(mHttpsConnection, 204); |
| 743 | setStatus(mHttpConnection, 204); |
| 744 | final NetworkCapabilities meteredCap = new NetworkCapabilities() |
| 745 | .addTransportType(TRANSPORT_WIFI) |
| 746 | .addCapability(NET_CAPABILITY_INTERNET); |
| 747 | doReturn(TEST_SPEED_TEST_URL).when(mResources).getString( |
| 748 | R.string.config_evaluating_bandwidth_url); |
| 749 | final NetworkMonitor nm = runNetworkTest(TEST_LINK_PROPERTIES, meteredCap, |
| 750 | NETWORK_VALIDATION_RESULT_VALID, NETWORK_VALIDATION_PROBE_DNS |
| 751 | | NETWORK_VALIDATION_PROBE_HTTPS, null /* redirectUrl */); |
| 752 | // Evaluating bandwidth process won't be executed when the network is metered wifi. |
| 753 | // Check that the connection hasn't been opened and the state should transition to validated |
| 754 | // state directly. |
| 755 | verify(mCleartextDnsNetwork, never()).openConnection(new URL(TEST_SPEED_TEST_URL)); |
| 756 | assertEquals(NETWORK_VALIDATION_RESULT_VALID, |
| 757 | nm.getEvaluationState().getEvaluationResult()); |
| 758 | } |
| 759 | |
| 760 | @Test |
| 761 | public void testEvaluatingBandwidthState_nonMeteredNetworkWithWrongConfig() throws Exception { |
| 762 | setStatus(mHttpsConnection, 204); |
| 763 | setStatus(mHttpConnection, 204); |
| 764 | final NetworkCapabilities nonMeteredCap = new NetworkCapabilities() |
| 765 | .addTransportType(TRANSPORT_WIFI) |
| 766 | .addCapability(NET_CAPABILITY_INTERNET) |
| 767 | .addCapability(NET_CAPABILITY_NOT_METERED); |
| 768 | doReturn("").when(mResources).getString(R.string.config_evaluating_bandwidth_url); |
| 769 | final NetworkMonitor nm = runNetworkTest(TEST_LINK_PROPERTIES, nonMeteredCap, |
| 770 | NETWORK_VALIDATION_RESULT_VALID, NETWORK_VALIDATION_PROBE_DNS |
| 771 | | NETWORK_VALIDATION_PROBE_HTTPS, null /* redirectUrl */); |
| 772 | // Non-metered network with wrong configuration(the config_evaluating_bandwidth_url is |
| 773 | // empty). Check that the connection hasn't been opened and the state should transition to |
| 774 | // validated state directly. |
| 775 | verify(mCleartextDnsNetwork, never()).openConnection(new URL(TEST_SPEED_TEST_URL)); |
| 776 | assertEquals(NETWORK_VALIDATION_RESULT_VALID, |
| 777 | nm.getEvaluationState().getEvaluationResult()); |
| 778 | } |
| 779 | |
| 780 | @Test |
lucaslin | d4c999f | 2020-04-09 00:31:21 +0800 | [diff] [blame] | 781 | public void testMatchesHttpContent() throws Exception { |
Chiachang Wang | daf983b | 2020-05-07 19:20:52 +0800 | [diff] [blame] | 782 | final WrappedNetworkMonitor wnm = makeCellNotMeteredNetworkMonitor(); |
lucaslin | d4c999f | 2020-04-09 00:31:21 +0800 | [diff] [blame] | 783 | doReturn("[\\s\\S]*line2[\\s\\S]*").when(mResources).getString( |
| 784 | R.string.config_network_validation_failed_content_regexp); |
| 785 | assertTrue(wnm.matchesHttpContent("This is line1\nThis is line2\nThis is line3", |
| 786 | R.string.config_network_validation_failed_content_regexp)); |
| 787 | assertFalse(wnm.matchesHttpContent("hello", |
| 788 | R.string.config_network_validation_failed_content_regexp)); |
| 789 | // Set an invalid regex and expect to get the false even though the regex is the same as the |
| 790 | // content. |
| 791 | doReturn("[").when(mResources).getString( |
| 792 | R.string.config_network_validation_failed_content_regexp); |
| 793 | assertFalse(wnm.matchesHttpContent("[", |
| 794 | R.string.config_network_validation_failed_content_regexp)); |
| 795 | } |
| 796 | |
| 797 | @Test |
| 798 | public void testMatchesHttpContentLength() throws Exception { |
Chiachang Wang | daf983b | 2020-05-07 19:20:52 +0800 | [diff] [blame] | 799 | final WrappedNetworkMonitor wnm = makeCellNotMeteredNetworkMonitor(); |
lucaslin | d4c999f | 2020-04-09 00:31:21 +0800 | [diff] [blame] | 800 | // Set the range of content length. |
| 801 | doReturn(100).when(mResources).getInteger(R.integer.config_min_matches_http_content_length); |
| 802 | doReturn(1000).when(mResources).getInteger( |
| 803 | R.integer.config_max_matches_http_content_length); |
| 804 | assertFalse(wnm.matchesHttpContentLength(100)); |
| 805 | assertFalse(wnm.matchesHttpContentLength(1000)); |
| 806 | assertTrue(wnm.matchesHttpContentLength(500)); |
| 807 | |
| 808 | // Test the invalid value. |
| 809 | assertFalse(wnm.matchesHttpContentLength(-1)); |
| 810 | assertFalse(wnm.matchesHttpContentLength(0)); |
| 811 | assertFalse(wnm.matchesHttpContentLength(Integer.MAX_VALUE + 1L)); |
| 812 | |
| 813 | // Set the wrong value for min and max config to make sure the function is working even |
| 814 | // though the config is wrong. |
| 815 | doReturn(1000).when(mResources).getInteger( |
| 816 | R.integer.config_min_matches_http_content_length); |
| 817 | doReturn(100).when(mResources).getInteger( |
| 818 | R.integer.config_max_matches_http_content_length); |
| 819 | assertFalse(wnm.matchesHttpContentLength(100)); |
| 820 | assertFalse(wnm.matchesHttpContentLength(1000)); |
| 821 | assertFalse(wnm.matchesHttpContentLength(500)); |
| 822 | } |
| 823 | |
| 824 | @Test |
| 825 | public void testGetResStringConfig() throws Exception { |
Chiachang Wang | daf983b | 2020-05-07 19:20:52 +0800 | [diff] [blame] | 826 | final WrappedNetworkMonitor wnm = makeCellNotMeteredNetworkMonitor(); |
lucaslin | d4c999f | 2020-04-09 00:31:21 +0800 | [diff] [blame] | 827 | // Set the config and expect to get the customized value. |
| 828 | final String regExp = ".*HTTP.*200.*not a captive portal.*"; |
| 829 | doReturn(regExp).when(mResources).getString( |
| 830 | R.string.config_network_validation_failed_content_regexp); |
| 831 | assertEquals(regExp, wnm.getResStringConfig(mContext, |
| 832 | R.string.config_network_validation_failed_content_regexp, null)); |
| 833 | doThrow(new Resources.NotFoundException()).when(mResources).getString(eq( |
| 834 | R.string.config_network_validation_failed_content_regexp)); |
| 835 | // If the config is not found, then expect to get the default value - null. |
| 836 | assertNull(wnm.getResStringConfig(mContext, |
| 837 | R.string.config_network_validation_failed_content_regexp, null)); |
| 838 | } |
| 839 | |
| 840 | @Test |
| 841 | public void testGetResIntConfig() throws Exception { |
Chiachang Wang | daf983b | 2020-05-07 19:20:52 +0800 | [diff] [blame] | 842 | final WrappedNetworkMonitor wnm = makeCellNotMeteredNetworkMonitor(); |
lucaslin | d4c999f | 2020-04-09 00:31:21 +0800 | [diff] [blame] | 843 | // Set the config and expect to get the customized value. |
| 844 | doReturn(100).when(mResources).getInteger(R.integer.config_min_matches_http_content_length); |
| 845 | doReturn(1000).when(mResources).getInteger( |
| 846 | R.integer.config_max_matches_http_content_length); |
| 847 | assertEquals(100, wnm.getResIntConfig(mContext, |
| 848 | R.integer.config_min_matches_http_content_length, Integer.MAX_VALUE)); |
| 849 | assertEquals(1000, wnm.getResIntConfig(mContext, |
| 850 | R.integer.config_max_matches_http_content_length, 0)); |
| 851 | doThrow(new Resources.NotFoundException()) |
| 852 | .when(mResources).getInteger( |
| 853 | eq(R.integer.config_min_matches_http_content_length)); |
| 854 | doThrow(new Resources.NotFoundException()) |
| 855 | .when(mResources).getInteger(eq(R.integer.config_max_matches_http_content_length)); |
| 856 | // If the config is not found, then expect to get the default value. |
| 857 | assertEquals(Integer.MAX_VALUE, wnm.getResIntConfig(mContext, |
| 858 | R.integer.config_min_matches_http_content_length, Integer.MAX_VALUE)); |
| 859 | assertEquals(0, wnm.getResIntConfig(mContext, |
| 860 | R.integer.config_max_matches_http_content_length, 0)); |
| 861 | } |
| 862 | |
| 863 | @Test |
Chiachang Wang | 5dbe685 | 2020-04-10 09:02:45 +0800 | [diff] [blame] | 864 | public void testGetHttpProbeUrl() { |
Chiachang Wang | daf983b | 2020-05-07 19:20:52 +0800 | [diff] [blame] | 865 | final WrappedNetworkMonitor wnm = makeCellNotMeteredNetworkMonitor(); |
Chiachang Wang | 5dbe685 | 2020-04-10 09:02:45 +0800 | [diff] [blame] | 866 | // If config_captive_portal_http_url is set and the global setting is set, the config is |
| 867 | // used. |
| 868 | doReturn(TEST_HTTP_URL).when(mResources).getString(R.string.config_captive_portal_http_url); |
| 869 | doReturn(TEST_HTTP_OTHER_URL2).when(mResources).getString( |
| 870 | R.string.default_captive_portal_http_url); |
| 871 | when(mDependencies.getSetting(any(), eq(Settings.Global.CAPTIVE_PORTAL_HTTP_URL), any())) |
| 872 | .thenReturn(TEST_HTTP_OTHER_URL1); |
| 873 | assertEquals(TEST_HTTP_URL, wnm.getCaptivePortalServerHttpUrl()); |
| 874 | // If config_captive_portal_http_url is unset and the global setting is set, the global |
| 875 | // setting is used. |
| 876 | doReturn(null).when(mResources).getString(R.string.config_captive_portal_http_url); |
| 877 | assertEquals(TEST_HTTP_OTHER_URL1, wnm.getCaptivePortalServerHttpUrl()); |
| 878 | // If both config_captive_portal_http_url and global setting are unset, |
| 879 | // default_captive_portal_http_url is used. |
| 880 | when(mDependencies.getSetting(any(), eq(Settings.Global.CAPTIVE_PORTAL_HTTP_URL), any())) |
| 881 | .thenReturn(null); |
| 882 | assertEquals(TEST_HTTP_OTHER_URL2, wnm.getCaptivePortalServerHttpUrl()); |
| 883 | } |
| 884 | |
| 885 | @Test |
lucaslin | 9b4dfab | 2019-12-17 23:06:12 +0800 | [diff] [blame] | 886 | public void testGetLocationMcc() throws Exception { |
Chiachang Wang | daf983b | 2020-05-07 19:20:52 +0800 | [diff] [blame] | 887 | final WrappedNetworkMonitor wnm = makeCellNotMeteredNetworkMonitor(); |
lucaslin | 9b4dfab | 2019-12-17 23:06:12 +0800 | [diff] [blame] | 888 | doReturn(PackageManager.PERMISSION_DENIED).when(mContext).checkPermission( |
| 889 | eq(android.Manifest.permission.ACCESS_FINE_LOCATION), anyInt(), anyInt()); |
| 890 | assertNull(wnm.getLocationMcc()); |
| 891 | doReturn(PackageManager.PERMISSION_GRANTED).when(mContext).checkPermission( |
| 892 | eq(android.Manifest.permission.ACCESS_FINE_LOCATION), anyInt(), anyInt()); |
| 893 | doReturn(new ContextWrapper(mContext)).when(mContext).createConfigurationContext(any()); |
lucaslin | c4b59dd | 2020-06-02 16:31:21 +0800 | [diff] [blame] | 894 | doReturn(null).when(mTelephony).getAllCellInfo(); |
| 895 | assertNull(wnm.getLocationMcc()); |
lucaslin | 9b4dfab | 2019-12-17 23:06:12 +0800 | [diff] [blame] | 896 | // Prepare CellInfo and check if the vote mechanism is working or not. |
lucaslin | 9b4dfab | 2019-12-17 23:06:12 +0800 | [diff] [blame] | 897 | final List<CellInfo> cellList = new ArrayList<CellInfo>(); |
lucaslin | c4b59dd | 2020-06-02 16:31:21 +0800 | [diff] [blame] | 898 | doReturn(cellList).when(mTelephony).getAllCellInfo(); |
| 899 | assertNull(wnm.getLocationMcc()); |
Chiachang Wang | 2482099 | 2020-03-23 21:16:36 +0800 | [diff] [blame] | 900 | cellList.add(makeTestCellInfoGsm("460")); |
| 901 | cellList.add(makeTestCellInfoGsm("460")); |
| 902 | cellList.add(makeTestCellInfoLte("466")); |
lucaslin | 9b4dfab | 2019-12-17 23:06:12 +0800 | [diff] [blame] | 903 | // The count of 460 is 2 and the count of 466 is 1, so the getLocationMcc() should return |
| 904 | // 460. |
| 905 | assertEquals("460", wnm.getLocationMcc()); |
lucaslin | 9073e7b | 2020-04-15 23:02:14 +0800 | [diff] [blame] | 906 | // getCustomizedContextOrDefault() shouldn't return mContext when using neighbor mcc |
lucaslin | 9b4dfab | 2019-12-17 23:06:12 +0800 | [diff] [blame] | 907 | // is enabled and the sim is not ready. |
| 908 | doReturn(true).when(mResources).getBoolean(R.bool.config_no_sim_card_uses_neighbor_mcc); |
| 909 | doReturn(TelephonyManager.SIM_STATE_ABSENT).when(mTelephony).getSimState(); |
lucaslin | 9b4dfab | 2019-12-17 23:06:12 +0800 | [diff] [blame] | 910 | assertEquals(460, |
lucaslin | 9073e7b | 2020-04-15 23:02:14 +0800 | [diff] [blame] | 911 | wnm.getCustomizedContextOrDefault().getResources().getConfiguration().mcc); |
lucaslin | 9b4dfab | 2019-12-17 23:06:12 +0800 | [diff] [blame] | 912 | doReturn(false).when(mResources).getBoolean(R.bool.config_no_sim_card_uses_neighbor_mcc); |
lucaslin | 9073e7b | 2020-04-15 23:02:14 +0800 | [diff] [blame] | 913 | assertEquals(wnm.getContext(), wnm.getCustomizedContextOrDefault()); |
| 914 | } |
| 915 | |
| 916 | @Test |
| 917 | public void testGetMccMncOverrideInfo() { |
Chiachang Wang | daf983b | 2020-05-07 19:20:52 +0800 | [diff] [blame] | 918 | final WrappedNetworkMonitor wnm = makeCellNotMeteredNetworkMonitor(); |
lucaslin | 9073e7b | 2020-04-15 23:02:14 +0800 | [diff] [blame] | 919 | doReturn(new ContextWrapper(mContext)).when(mContext).createConfigurationContext(any()); |
| 920 | // 1839 is VZW's carrier id. |
| 921 | doReturn(1839).when(mTelephony).getSimCarrierId(); |
| 922 | assertNull(wnm.getMccMncOverrideInfo()); |
| 923 | // 1854 is CTC's carrier id. |
| 924 | doReturn(1854).when(mTelephony).getSimCarrierId(); |
| 925 | assertNotNull(wnm.getMccMncOverrideInfo()); |
| 926 | // Check if the mcc & mnc has changed as expected. |
| 927 | assertEquals(460, |
| 928 | wnm.getCustomizedContextOrDefault().getResources().getConfiguration().mcc); |
| 929 | assertEquals(03, |
| 930 | wnm.getCustomizedContextOrDefault().getResources().getConfiguration().mnc); |
| 931 | // Every mcc and mnc should be set in sCarrierIdToMccMnc. |
| 932 | // Check if there is any unset value in mcc or mnc. |
| 933 | for (int i = 0; i < wnm.sCarrierIdToMccMnc.size(); i++) { |
| 934 | assertNotEquals(-1, wnm.sCarrierIdToMccMnc.valueAt(i).mcc); |
| 935 | assertNotEquals(-1, wnm.sCarrierIdToMccMnc.valueAt(i).mnc); |
| 936 | } |
lucaslin | 9b4dfab | 2019-12-17 23:06:12 +0800 | [diff] [blame] | 937 | } |
| 938 | |
Chiachang Wang | 2482099 | 2020-03-23 21:16:36 +0800 | [diff] [blame] | 939 | private CellInfoGsm makeTestCellInfoGsm(String mcc) throws Exception { |
| 940 | final CellInfoGsm info = new CellInfoGsm(); |
| 941 | final CellIdentityGsm ci = makeCellIdentityGsm(0, 0, 0, 0, mcc, "01", "", ""); |
| 942 | info.setCellIdentity(ci); |
| 943 | return info; |
| 944 | } |
| 945 | |
| 946 | private CellInfoLte makeTestCellInfoLte(String mcc) throws Exception { |
| 947 | final CellInfoLte info = new CellInfoLte(); |
| 948 | final CellIdentityLte ci = makeCellIdentityLte(0, 0, 0, 0, 0, mcc, "01", "", ""); |
| 949 | info.setCellIdentity(ci); |
| 950 | return info; |
| 951 | } |
| 952 | |
lucaslin | b9826c5 | 2020-06-19 14:47:01 +0800 | [diff] [blame] | 953 | private void setupNoSimCardNeighborMcc() throws Exception { |
| 954 | // Enable using neighbor resource by camping mcc feature. |
| 955 | doReturn(true).when(mResources).getBoolean(R.bool.config_no_sim_card_uses_neighbor_mcc); |
| 956 | final List<CellInfo> cellList = new ArrayList<CellInfo>(); |
| 957 | final int testMcc = 460; |
| 958 | cellList.add(makeTestCellInfoGsm(Integer.toString(testMcc))); |
| 959 | doReturn(cellList).when(mTelephony).getAllCellInfo(); |
| 960 | final Configuration config = mResources.getConfiguration(); |
| 961 | config.mcc = testMcc; |
| 962 | doReturn(mMccContext).when(mContext).createConfigurationContext(eq(config)); |
| 963 | } |
| 964 | |
Chiachang Wang | 2482099 | 2020-03-23 21:16:36 +0800 | [diff] [blame] | 965 | @Test |
| 966 | public void testMakeFallbackUrls() throws Exception { |
Chiachang Wang | daf983b | 2020-05-07 19:20:52 +0800 | [diff] [blame] | 967 | final WrappedNetworkMonitor wnm = makeCellNotMeteredNetworkMonitor(); |
Chiachang Wang | 2482099 | 2020-03-23 21:16:36 +0800 | [diff] [blame] | 968 | // Value exist in setting provider. |
| 969 | URL[] urls = wnm.makeCaptivePortalFallbackUrls(); |
| 970 | assertEquals(urls[0].toString(), TEST_FALLBACK_URL); |
| 971 | |
| 972 | // Clear setting provider value. Verify it to get configuration from resource instead. |
| 973 | setFallbackUrl(null); |
| 974 | // Verify that getting resource with exception. |
| 975 | when(mResources.getStringArray(R.array.config_captive_portal_fallback_urls)) |
| 976 | .thenThrow(Resources.NotFoundException.class); |
| 977 | urls = wnm.makeCaptivePortalFallbackUrls(); |
| 978 | assertEquals(urls.length, 0); |
| 979 | |
| 980 | // Verify resource return 2 different URLs. |
| 981 | doReturn(new String[] {"http://testUrl1.com", "http://testUrl2.com"}).when(mResources) |
| 982 | .getStringArray(R.array.config_captive_portal_fallback_urls); |
| 983 | urls = wnm.makeCaptivePortalFallbackUrls(); |
| 984 | assertEquals(urls.length, 2); |
| 985 | assertEquals("http://testUrl1.com", urls[0].toString()); |
| 986 | assertEquals("http://testUrl2.com", urls[1].toString()); |
| 987 | |
lucaslin | b9826c5 | 2020-06-19 14:47:01 +0800 | [diff] [blame] | 988 | // Even though the using neighbor resource by camping mcc feature is enabled, the |
| 989 | // customized context has been assigned and won't change. So calling |
| 990 | // makeCaptivePortalFallbackUrls() still gets the original value. |
| 991 | setupNoSimCardNeighborMcc(); |
Chiachang Wang | 2482099 | 2020-03-23 21:16:36 +0800 | [diff] [blame] | 992 | doReturn(new String[] {"http://testUrl3.com"}).when(mMccResource) |
| 993 | .getStringArray(R.array.config_captive_portal_fallback_urls); |
| 994 | urls = wnm.makeCaptivePortalFallbackUrls(); |
lucaslin | b9826c5 | 2020-06-19 14:47:01 +0800 | [diff] [blame] | 995 | assertEquals(urls.length, 2); |
| 996 | assertEquals("http://testUrl1.com", urls[0].toString()); |
| 997 | assertEquals("http://testUrl2.com", urls[1].toString()); |
| 998 | } |
| 999 | |
| 1000 | @Test |
| 1001 | public void testMakeFallbackUrlsWithCustomizedContext() throws Exception { |
| 1002 | // Value is expected to be replaced by location resource. |
| 1003 | setupNoSimCardNeighborMcc(); |
| 1004 | doReturn(new String[] {"http://testUrl.com"}).when(mMccResource) |
| 1005 | .getStringArray(R.array.config_captive_portal_fallback_urls); |
| 1006 | final WrappedNetworkMonitor wnm = makeCellNotMeteredNetworkMonitor(); |
| 1007 | final URL[] urls = wnm.makeCaptivePortalFallbackUrls(); |
Chiachang Wang | 2482099 | 2020-03-23 21:16:36 +0800 | [diff] [blame] | 1008 | assertEquals(urls.length, 1); |
lucaslin | b9826c5 | 2020-06-19 14:47:01 +0800 | [diff] [blame] | 1009 | assertEquals("http://testUrl.com", urls[0].toString()); |
Chiachang Wang | 2482099 | 2020-03-23 21:16:36 +0800 | [diff] [blame] | 1010 | } |
| 1011 | |
Remi NGUYEN VAN | 7fc260a | 2020-02-03 13:33:58 +0900 | [diff] [blame] | 1012 | private static CellIdentityGsm makeCellIdentityGsm(int lac, int cid, int arfcn, int bsic, |
| 1013 | String mccStr, String mncStr, String alphal, String alphas) |
| 1014 | throws ReflectiveOperationException { |
Lorenzo Colitti | 0d08b44 | 2020-04-27 11:49:39 +0900 | [diff] [blame] | 1015 | if (ShimUtils.isAtLeastR()) { |
Remi NGUYEN VAN | 7fc260a | 2020-02-03 13:33:58 +0900 | [diff] [blame] | 1016 | return new CellIdentityGsm(lac, cid, arfcn, bsic, mccStr, mncStr, alphal, alphas, |
| 1017 | Collections.emptyList() /* additionalPlmns */); |
| 1018 | } else { |
| 1019 | // API <= Q does not have the additionalPlmns parameter |
| 1020 | final Constructor<CellIdentityGsm> constructor = CellIdentityGsm.class.getConstructor( |
| 1021 | int.class, int.class, int.class, int.class, String.class, String.class, |
| 1022 | String.class, String.class); |
| 1023 | return constructor.newInstance(lac, cid, arfcn, bsic, mccStr, mncStr, alphal, alphas); |
| 1024 | } |
| 1025 | } |
| 1026 | |
| 1027 | private static CellIdentityLte makeCellIdentityLte(int ci, int pci, int tac, int earfcn, |
| 1028 | int bandwidth, String mccStr, String mncStr, String alphal, String alphas) |
| 1029 | throws ReflectiveOperationException { |
Lorenzo Colitti | 0d08b44 | 2020-04-27 11:49:39 +0900 | [diff] [blame] | 1030 | if (ShimUtils.isAtLeastR()) { |
Nathan Harold | badf788 | 2020-03-03 14:40:26 -0800 | [diff] [blame] | 1031 | return new CellIdentityLte(ci, pci, tac, earfcn, new int[] {} /* bands */, |
Jack Yu | cfedb15 | 2020-02-22 23:05:08 -0800 | [diff] [blame] | 1032 | bandwidth, mccStr, mncStr, alphal, alphas, |
| 1033 | Collections.emptyList() /* additionalPlmns */, null /* csgInfo */); |
Remi NGUYEN VAN | 7fc260a | 2020-02-03 13:33:58 +0900 | [diff] [blame] | 1034 | } else { |
| 1035 | // API <= Q does not have the additionalPlmns and csgInfo parameters |
| 1036 | final Constructor<CellIdentityLte> constructor = CellIdentityLte.class.getConstructor( |
| 1037 | int.class, int.class, int.class, int.class, int.class, String.class, |
| 1038 | String.class, String.class, String.class); |
| 1039 | return constructor.newInstance(ci, pci, tac, earfcn, bandwidth, mccStr, mncStr, alphal, |
| 1040 | alphas); |
| 1041 | } |
| 1042 | } |
| 1043 | |
lucaslin | 9b4dfab | 2019-12-17 23:06:12 +0800 | [diff] [blame] | 1044 | @Test |
Lorenzo Colitti | 171cfd2 | 2019-04-18 13:44:32 +0900 | [diff] [blame] | 1045 | public void testGetIntSetting() throws Exception { |
Chiachang Wang | daf983b | 2020-05-07 19:20:52 +0800 | [diff] [blame] | 1046 | WrappedNetworkMonitor wnm = makeCellNotMeteredNetworkMonitor(); |
Lorenzo Colitti | 171cfd2 | 2019-04-18 13:44:32 +0900 | [diff] [blame] | 1047 | |
| 1048 | // No config resource, no device config. Expect to get default resource. |
| 1049 | doThrow(new Resources.NotFoundException()) |
| 1050 | .when(mResources).getInteger(eq(R.integer.config_captive_portal_dns_probe_timeout)); |
| 1051 | doAnswer(invocation -> { |
| 1052 | int defaultValue = invocation.getArgument(2); |
| 1053 | return defaultValue; |
| 1054 | }).when(mDependencies).getDeviceConfigPropertyInt(any(), |
| 1055 | eq(NetworkMonitor.CONFIG_CAPTIVE_PORTAL_DNS_PROBE_TIMEOUT), |
| 1056 | anyInt()); |
Chiachang Wang | 5dbe685 | 2020-04-10 09:02:45 +0800 | [diff] [blame] | 1057 | assertEquals(DEFAULT_CAPTIVE_PORTAL_DNS_PROBE_TIMEOUT, wnm.getIntSetting(mContext, |
Lorenzo Colitti | 171cfd2 | 2019-04-18 13:44:32 +0900 | [diff] [blame] | 1058 | R.integer.config_captive_portal_dns_probe_timeout, |
| 1059 | NetworkMonitor.CONFIG_CAPTIVE_PORTAL_DNS_PROBE_TIMEOUT, |
Chiachang Wang | 5dbe685 | 2020-04-10 09:02:45 +0800 | [diff] [blame] | 1060 | DEFAULT_CAPTIVE_PORTAL_DNS_PROBE_TIMEOUT)); |
Lorenzo Colitti | 171cfd2 | 2019-04-18 13:44:32 +0900 | [diff] [blame] | 1061 | |
| 1062 | // Set device config. Expect to get device config. |
| 1063 | when(mDependencies.getDeviceConfigPropertyInt(any(), |
| 1064 | eq(NetworkMonitor.CONFIG_CAPTIVE_PORTAL_DNS_PROBE_TIMEOUT), anyInt())) |
| 1065 | .thenReturn(1234); |
| 1066 | assertEquals(1234, wnm.getIntSetting(mContext, |
| 1067 | R.integer.config_captive_portal_dns_probe_timeout, |
| 1068 | NetworkMonitor.CONFIG_CAPTIVE_PORTAL_DNS_PROBE_TIMEOUT, |
Chiachang Wang | 5dbe685 | 2020-04-10 09:02:45 +0800 | [diff] [blame] | 1069 | DEFAULT_CAPTIVE_PORTAL_DNS_PROBE_TIMEOUT)); |
Lorenzo Colitti | 171cfd2 | 2019-04-18 13:44:32 +0900 | [diff] [blame] | 1070 | |
| 1071 | // Set config resource. Expect to get config resource. |
| 1072 | when(mResources.getInteger(eq(R.integer.config_captive_portal_dns_probe_timeout))) |
| 1073 | .thenReturn(5678); |
| 1074 | assertEquals(5678, wnm.getIntSetting(mContext, |
| 1075 | R.integer.config_captive_portal_dns_probe_timeout, |
| 1076 | NetworkMonitor.CONFIG_CAPTIVE_PORTAL_DNS_PROBE_TIMEOUT, |
Chiachang Wang | 5dbe685 | 2020-04-10 09:02:45 +0800 | [diff] [blame] | 1077 | DEFAULT_CAPTIVE_PORTAL_DNS_PROBE_TIMEOUT)); |
Lorenzo Colitti | 171cfd2 | 2019-04-18 13:44:32 +0900 | [diff] [blame] | 1078 | } |
| 1079 | |
| 1080 | @Test |
Remi NGUYEN VAN | 0402afc | 2020-04-15 18:56:59 +0900 | [diff] [blame] | 1081 | public void testIsCaptivePortal_HttpProbeIsPortal() throws Exception { |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 1082 | setSslException(mHttpsConnection); |
| 1083 | setPortal302(mHttpConnection); |
Remi NGUYEN VAN | 0402afc | 2020-04-15 18:56:59 +0900 | [diff] [blame] | 1084 | runPortalNetworkTest(); |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 1085 | } |
| 1086 | |
Remi NGUYEN VAN | a750073 | 2021-05-27 14:35:18 +0900 | [diff] [blame] | 1087 | @Test |
| 1088 | public void testIsCaptivePortal_Http200EmptyResponse() throws Exception { |
| 1089 | setSslException(mHttpsConnection); |
| 1090 | setStatus(mHttpConnection, 200); |
| 1091 | // Invalid if there is no content (can't login to an empty page) |
| 1092 | runNetworkTest(VALIDATION_RESULT_INVALID, 0 /* probesSucceeded */, null); |
| 1093 | verify(mCallbacks, never()).showProvisioningNotification(any(), any()); |
| 1094 | } |
| 1095 | |
| 1096 | private void doCaptivePortal200ResponseTest(String expectedRedirectUrl) throws Exception { |
| 1097 | setSslException(mHttpsConnection); |
| 1098 | setStatus(mHttpConnection, 200); |
| 1099 | doReturn(100L).when(mHttpConnection).getContentLengthLong(); |
| 1100 | // Redirect URL was null before S |
| 1101 | runNetworkTest(VALIDATION_RESULT_PORTAL, 0 /* probesSucceeded */, expectedRedirectUrl); |
| 1102 | verify(mCallbacks, timeout(HANDLER_TIMEOUT_MS)).showProvisioningNotification(any(), any()); |
| 1103 | } |
| 1104 | |
| 1105 | @Test @IgnoreAfter(Build.VERSION_CODES.R) |
| 1106 | public void testIsCaptivePortal_HttpProbeIs200Portal_R() throws Exception { |
| 1107 | doCaptivePortal200ResponseTest(null); |
| 1108 | } |
| 1109 | |
| 1110 | @Test @IgnoreUpTo(Build.VERSION_CODES.R) |
| 1111 | public void testIsCaptivePortal_HttpProbeIs200Portal() throws Exception { |
| 1112 | doCaptivePortal200ResponseTest(TEST_HTTP_URL); |
| 1113 | } |
| 1114 | |
Yeo | 4915faf | 2019-08-26 14:53:23 +0900 | [diff] [blame] | 1115 | private void setupPrivateIpResponse(String privateAddr) throws Exception { |
| 1116 | setSslException(mHttpsConnection); |
| 1117 | setPortal302(mHttpConnection); |
| 1118 | final String httpHost = new URL(TEST_HTTP_URL).getHost(); |
| 1119 | mFakeDns.setAnswer(httpHost, new String[] { "2001:db8::123" }, TYPE_AAAA); |
| 1120 | final InetAddress parsedPrivateAddr = InetAddresses.parseNumericAddress(privateAddr); |
| 1121 | mFakeDns.setAnswer(httpHost, new String[] { privateAddr }, |
| 1122 | (parsedPrivateAddr instanceof Inet6Address) ? TYPE_AAAA : TYPE_A); |
| 1123 | } |
| 1124 | |
| 1125 | @Test |
| 1126 | public void testIsCaptivePortal_PrivateIpNotPortal_Enabled_IPv4() throws Exception { |
| 1127 | when(mDependencies.isFeatureEnabled(any(), eq(DNS_PROBE_PRIVATE_IP_NO_INTERNET_VERSION))) |
| 1128 | .thenReturn(true); |
| 1129 | setupPrivateIpResponse("192.168.1.1"); |
| 1130 | runFailedNetworkTest(); |
| 1131 | } |
| 1132 | |
| 1133 | @Test |
| 1134 | public void testIsCaptivePortal_PrivateIpNotPortal_Enabled_IPv6() throws Exception { |
| 1135 | when(mDependencies.isFeatureEnabled(any(), eq(DNS_PROBE_PRIVATE_IP_NO_INTERNET_VERSION))) |
| 1136 | .thenReturn(true); |
| 1137 | setupPrivateIpResponse("fec0:1234::1"); |
| 1138 | runFailedNetworkTest(); |
| 1139 | } |
| 1140 | |
| 1141 | @Test |
| 1142 | public void testIsCaptivePortal_PrivateIpNotPortal_Disabled() throws Exception { |
| 1143 | setupPrivateIpResponse("192.168.1.1"); |
Remi NGUYEN VAN | 0402afc | 2020-04-15 18:56:59 +0900 | [diff] [blame] | 1144 | runPortalNetworkTest(); |
Yeo | 4915faf | 2019-08-26 14:53:23 +0900 | [diff] [blame] | 1145 | } |
| 1146 | |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 1147 | @Test |
Remi NGUYEN VAN | 0402afc | 2020-04-15 18:56:59 +0900 | [diff] [blame] | 1148 | public void testIsCaptivePortal_HttpsProbeIsNotPortal() throws Exception { |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 1149 | setStatus(mHttpsConnection, 204); |
| 1150 | setStatus(mHttpConnection, 500); |
| 1151 | |
Remi NGUYEN VAN | 0402afc | 2020-04-15 18:56:59 +0900 | [diff] [blame] | 1152 | runValidatedNetworkTest(); |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 1153 | } |
| 1154 | |
| 1155 | @Test |
Remi NGUYEN VAN | 0402afc | 2020-04-15 18:56:59 +0900 | [diff] [blame] | 1156 | public void testIsCaptivePortal_FallbackProbeIsPortal() throws Exception { |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 1157 | setSslException(mHttpsConnection); |
| 1158 | setStatus(mHttpConnection, 500); |
| 1159 | setPortal302(mFallbackConnection); |
Remi NGUYEN VAN | 0402afc | 2020-04-15 18:56:59 +0900 | [diff] [blame] | 1160 | runPortalNetworkTest(); |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 1161 | } |
| 1162 | |
| 1163 | @Test |
Chiachang Wang | c9a89aa | 2020-11-13 17:04:35 +0800 | [diff] [blame] | 1164 | public void testIsCaptivePortal_HttpSucceedFallbackProbeIsPortal() throws Exception { |
| 1165 | setSslException(mHttpsConnection); |
| 1166 | setStatus(mHttpConnection, 204); |
| 1167 | setPortal302(mFallbackConnection); |
| 1168 | runPortalNetworkTest(); |
| 1169 | } |
| 1170 | |
| 1171 | @Test |
Remi NGUYEN VAN | 0402afc | 2020-04-15 18:56:59 +0900 | [diff] [blame] | 1172 | public void testIsCaptivePortal_FallbackProbeIsNotPortal() throws Exception { |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 1173 | setSslException(mHttpsConnection); |
| 1174 | setStatus(mHttpConnection, 500); |
Remi NGUYEN VAN | dc6e640 | 2019-03-27 15:42:53 +0900 | [diff] [blame] | 1175 | setStatus(mFallbackConnection, 500); |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 1176 | |
| 1177 | // Fallback probe did not see portal, HTTPS failed -> inconclusive |
Remi NGUYEN VAN | dc6e640 | 2019-03-27 15:42:53 +0900 | [diff] [blame] | 1178 | runFailedNetworkTest(); |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 1179 | } |
| 1180 | |
| 1181 | @Test |
Remi NGUYEN VAN | 0402afc | 2020-04-15 18:56:59 +0900 | [diff] [blame] | 1182 | public void testIsCaptivePortal_OtherFallbackProbeIsPortal() throws Exception { |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 1183 | // Set all fallback probes but one to invalid URLs to verify they are being skipped |
| 1184 | setFallbackUrl(TEST_FALLBACK_URL); |
| 1185 | setOtherFallbackUrls(TEST_FALLBACK_URL + "," + TEST_OTHER_FALLBACK_URL); |
| 1186 | |
| 1187 | setSslException(mHttpsConnection); |
| 1188 | setStatus(mHttpConnection, 500); |
| 1189 | setStatus(mFallbackConnection, 500); |
| 1190 | setPortal302(mOtherFallbackConnection); |
| 1191 | |
| 1192 | // TEST_OTHER_FALLBACK_URL is third |
| 1193 | when(mRandom.nextInt()).thenReturn(2); |
| 1194 | |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 1195 | // First check always uses the first fallback URL: inconclusive |
Remi NGUYEN VAN | 0402afc | 2020-04-15 18:56:59 +0900 | [diff] [blame] | 1196 | final NetworkMonitor monitor = runFailedNetworkTest(); |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 1197 | verify(mFallbackConnection, times(1)).getResponseCode(); |
| 1198 | verify(mOtherFallbackConnection, never()).getResponseCode(); |
| 1199 | |
Remi NGUYEN VAN | 2f18730 | 2020-05-21 14:35:50 +0900 | [diff] [blame] | 1200 | // Second check should be triggered automatically after the reevaluate delay, and uses the |
| 1201 | // URL chosen by mRandom |
Remi NGUYEN VAN | 396931d | 2020-05-28 17:04:21 +0900 | [diff] [blame] | 1202 | // Ensure that the reevaluate delay is not changed to a large value, otherwise this test |
| 1203 | // would block for too long and a different test strategy should be used. |
Remi NGUYEN VAN | 2f18730 | 2020-05-21 14:35:50 +0900 | [diff] [blame] | 1204 | assertTrue(INITIAL_REEVALUATE_DELAY_MS < 2000); |
| 1205 | verify(mOtherFallbackConnection, timeout(INITIAL_REEVALUATE_DELAY_MS + HANDLER_TIMEOUT_MS)) |
| 1206 | .getResponseCode(); |
| 1207 | verifyNetworkTested(VALIDATION_RESULT_PORTAL, 0 /* probesSucceeded */, TEST_LOGIN_URL); |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 1208 | } |
| 1209 | |
| 1210 | @Test |
Remi NGUYEN VAN | 0402afc | 2020-04-15 18:56:59 +0900 | [diff] [blame] | 1211 | public void testIsCaptivePortal_AllProbesFailed() throws Exception { |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 1212 | setSslException(mHttpsConnection); |
| 1213 | setStatus(mHttpConnection, 500); |
| 1214 | setStatus(mFallbackConnection, 404); |
| 1215 | |
Remi NGUYEN VAN | dc6e640 | 2019-03-27 15:42:53 +0900 | [diff] [blame] | 1216 | runFailedNetworkTest(); |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 1217 | verify(mFallbackConnection, times(1)).getResponseCode(); |
| 1218 | verify(mOtherFallbackConnection, never()).getResponseCode(); |
| 1219 | } |
| 1220 | |
| 1221 | @Test |
Remi NGUYEN VAN | 0402afc | 2020-04-15 18:56:59 +0900 | [diff] [blame] | 1222 | public void testIsCaptivePortal_InvalidUrlSkipped() throws Exception { |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 1223 | setFallbackUrl("invalid"); |
| 1224 | setOtherFallbackUrls("otherinvalid," + TEST_OTHER_FALLBACK_URL + ",yetanotherinvalid"); |
| 1225 | |
| 1226 | setSslException(mHttpsConnection); |
| 1227 | setStatus(mHttpConnection, 500); |
| 1228 | setPortal302(mOtherFallbackConnection); |
Remi NGUYEN VAN | 0402afc | 2020-04-15 18:56:59 +0900 | [diff] [blame] | 1229 | runPortalNetworkTest(); |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 1230 | verify(mOtherFallbackConnection, times(1)).getResponseCode(); |
| 1231 | verify(mFallbackConnection, never()).getResponseCode(); |
| 1232 | } |
| 1233 | |
Remi NGUYEN VAN | 2fa48c2 | 2019-12-09 16:40:02 +0900 | [diff] [blame] | 1234 | @Test |
Chiachang Wang | aa55058 | 2020-05-27 17:18:20 +0800 | [diff] [blame] | 1235 | public void testIsCaptivePortal_CapportApiIsPortalWithNullPortalUrl() throws Exception { |
| 1236 | assumeTrue(CaptivePortalDataShimImpl.isSupported()); |
| 1237 | setSslException(mHttpsConnection); |
| 1238 | final long bytesRemaining = 10_000L; |
| 1239 | final long secondsRemaining = 500L; |
| 1240 | // Set content without partal url. |
| 1241 | setApiContent(mCapportApiConnection, "{'captive': true," |
| 1242 | + "'venue-info-url': '" + TEST_VENUE_INFO_URL + "'," |
| 1243 | + "'bytes-remaining': " + bytesRemaining + "," |
| 1244 | + "'seconds-remaining': " + secondsRemaining + "}"); |
| 1245 | setPortal302(mHttpConnection); |
| 1246 | |
| 1247 | runNetworkTest(makeCapportLPs(), CELL_METERED_CAPABILITIES, VALIDATION_RESULT_PORTAL, |
| 1248 | 0 /* probesSucceeded*/, TEST_LOGIN_URL); |
| 1249 | |
| 1250 | verify(mCapportApiConnection).getResponseCode(); |
| 1251 | |
| 1252 | verify(mHttpConnection, times(1)).getResponseCode(); |
| 1253 | verify(mCallbacks, never()).notifyCaptivePortalDataChanged(any()); |
| 1254 | } |
| 1255 | |
| 1256 | @Test |
| 1257 | public void testIsCaptivePortal_CapportApiIsPortalWithValidPortalUrl() throws Exception { |
Remi NGUYEN VAN | 2fa48c2 | 2019-12-09 16:40:02 +0900 | [diff] [blame] | 1258 | assumeTrue(CaptivePortalDataShimImpl.isSupported()); |
| 1259 | setSslException(mHttpsConnection); |
| 1260 | final long bytesRemaining = 10_000L; |
| 1261 | final long secondsRemaining = 500L; |
| 1262 | |
| 1263 | setApiContent(mCapportApiConnection, "{'captive': true," |
| 1264 | + "'user-portal-url': '" + TEST_LOGIN_URL + "'," |
| 1265 | + "'venue-info-url': '" + TEST_VENUE_INFO_URL + "'," |
| 1266 | + "'bytes-remaining': " + bytesRemaining + "," |
| 1267 | + "'seconds-remaining': " + secondsRemaining + "}"); |
| 1268 | |
Chiachang Wang | daf983b | 2020-05-07 19:20:52 +0800 | [diff] [blame] | 1269 | runNetworkTest(makeCapportLPs(), CELL_METERED_CAPABILITIES, VALIDATION_RESULT_PORTAL, |
Remi NGUYEN VAN | 0402afc | 2020-04-15 18:56:59 +0900 | [diff] [blame] | 1270 | 0 /* probesSucceeded*/, TEST_LOGIN_URL); |
Remi NGUYEN VAN | 2fa48c2 | 2019-12-09 16:40:02 +0900 | [diff] [blame] | 1271 | |
| 1272 | verify(mHttpConnection, never()).getResponseCode(); |
| 1273 | verify(mCapportApiConnection).getResponseCode(); |
Remi NGUYEN VAN | 2fa48c2 | 2019-12-09 16:40:02 +0900 | [diff] [blame] | 1274 | |
| 1275 | final ArgumentCaptor<CaptivePortalData> capportDataCaptor = |
| 1276 | ArgumentCaptor.forClass(CaptivePortalData.class); |
| 1277 | verify(mCallbacks).notifyCaptivePortalDataChanged(capportDataCaptor.capture()); |
| 1278 | final CaptivePortalData p = capportDataCaptor.getValue(); |
| 1279 | assertTrue(p.isCaptive()); |
| 1280 | assertEquals(Uri.parse(TEST_LOGIN_URL), p.getUserPortalUrl()); |
| 1281 | assertEquals(Uri.parse(TEST_VENUE_INFO_URL), p.getVenueInfoUrl()); |
| 1282 | assertEquals(bytesRemaining, p.getByteLimit()); |
| 1283 | final long expectedExpiry = currentTimeMillis() + secondsRemaining * 1000; |
| 1284 | // Actual expiry will be slightly lower as some time as passed |
| 1285 | assertTrue(p.getExpiryTimeMillis() <= expectedExpiry); |
| 1286 | assertTrue(p.getExpiryTimeMillis() > expectedExpiry - 30_000); |
| 1287 | } |
| 1288 | |
| 1289 | @Test |
| 1290 | public void testIsCaptivePortal_CapportApiRevalidation() throws Exception { |
| 1291 | assumeTrue(CaptivePortalDataShimImpl.isSupported()); |
Remi NGUYEN VAN | 0402afc | 2020-04-15 18:56:59 +0900 | [diff] [blame] | 1292 | setValidProbes(); |
Remi NGUYEN VAN | 2fa48c2 | 2019-12-09 16:40:02 +0900 | [diff] [blame] | 1293 | final NetworkMonitor nm = runValidatedNetworkTest(); |
| 1294 | |
| 1295 | setApiContent(mCapportApiConnection, "{'captive': true, " |
| 1296 | + "'user-portal-url': '" + TEST_LOGIN_URL + "'}"); |
Remi NGUYEN VAN | 905b0b6 | 2020-04-20 17:06:27 +0900 | [diff] [blame] | 1297 | nm.notifyLinkPropertiesChanged(makeCapportLPs()); |
Remi NGUYEN VAN | 2fa48c2 | 2019-12-09 16:40:02 +0900 | [diff] [blame] | 1298 | |
Remi NGUYEN VAN | 0402afc | 2020-04-15 18:56:59 +0900 | [diff] [blame] | 1299 | verifyNetworkTested(VALIDATION_RESULT_PORTAL, 0 /* probesSucceeded */, |
| 1300 | TEST_LOGIN_URL); |
Remi NGUYEN VAN | 905b0b6 | 2020-04-20 17:06:27 +0900 | [diff] [blame] | 1301 | final ArgumentCaptor<CaptivePortalData> capportCaptor = ArgumentCaptor.forClass( |
| 1302 | CaptivePortalData.class); |
| 1303 | verify(mCallbacks).notifyCaptivePortalDataChanged(capportCaptor.capture()); |
| 1304 | assertEquals(Uri.parse(TEST_LOGIN_URL), capportCaptor.getValue().getUserPortalUrl()); |
Remi NGUYEN VAN | 2fa48c2 | 2019-12-09 16:40:02 +0900 | [diff] [blame] | 1305 | |
| 1306 | // HTTP probe was sent on first validation but not re-sent when there was a portal URL. |
| 1307 | verify(mHttpConnection, times(1)).getResponseCode(); |
| 1308 | verify(mCapportApiConnection, times(1)).getResponseCode(); |
| 1309 | } |
| 1310 | |
| 1311 | @Test |
Remi NGUYEN VAN | ebb5a9f | 2020-05-20 19:11:56 +0900 | [diff] [blame] | 1312 | public void testIsCaptivePortal_NoRevalidationBeforeNetworkConnected() throws Exception { |
| 1313 | assumeTrue(CaptivePortalDataShimImpl.isSupported()); |
| 1314 | |
| 1315 | final NetworkMonitor nm = makeCellMeteredNetworkMonitor(); |
| 1316 | |
| 1317 | final LinkProperties lp = makeCapportLPs(); |
| 1318 | |
| 1319 | // LinkProperties changed, but NM should not revalidate before notifyNetworkConnected |
| 1320 | nm.notifyLinkPropertiesChanged(lp); |
| 1321 | verify(mHttpConnection, after(100).never()).getResponseCode(); |
| 1322 | verify(mHttpsConnection, never()).getResponseCode(); |
| 1323 | verify(mCapportApiConnection, never()).getResponseCode(); |
| 1324 | |
| 1325 | setValidProbes(); |
| 1326 | setApiContent(mCapportApiConnection, "{'captive': true, " |
| 1327 | + "'user-portal-url': '" + TEST_LOGIN_URL + "'}"); |
| 1328 | |
| 1329 | // After notifyNetworkConnected, validation uses the capport API contents |
| 1330 | nm.notifyNetworkConnected(lp, CELL_METERED_CAPABILITIES); |
| 1331 | verifyNetworkTested(VALIDATION_RESULT_PORTAL, 0 /* probesSucceeded */, TEST_LOGIN_URL); |
| 1332 | |
| 1333 | verify(mHttpConnection, never()).getResponseCode(); |
| 1334 | verify(mCapportApiConnection).getResponseCode(); |
| 1335 | } |
| 1336 | |
| 1337 | @Test |
Remi NGUYEN VAN | 2fa48c2 | 2019-12-09 16:40:02 +0900 | [diff] [blame] | 1338 | public void testIsCaptivePortal_CapportApiNotPortalNotValidated() throws Exception { |
| 1339 | assumeTrue(CaptivePortalDataShimImpl.isSupported()); |
| 1340 | setSslException(mHttpsConnection); |
| 1341 | setStatus(mHttpConnection, 500); |
| 1342 | setApiContent(mCapportApiConnection, "{'captive': false," |
| 1343 | + "'venue-info-url': '" + TEST_VENUE_INFO_URL + "'}"); |
Chiachang Wang | daf983b | 2020-05-07 19:20:52 +0800 | [diff] [blame] | 1344 | runNetworkTest(makeCapportLPs(), CELL_METERED_CAPABILITIES, VALIDATION_RESULT_INVALID, |
Remi NGUYEN VAN | 0402afc | 2020-04-15 18:56:59 +0900 | [diff] [blame] | 1345 | 0 /* probesSucceeded */, null /* redirectUrl */); |
Remi NGUYEN VAN | 2fa48c2 | 2019-12-09 16:40:02 +0900 | [diff] [blame] | 1346 | |
Remi NGUYEN VAN | 905b0b6 | 2020-04-20 17:06:27 +0900 | [diff] [blame] | 1347 | final ArgumentCaptor<CaptivePortalData> capportCaptor = ArgumentCaptor.forClass( |
| 1348 | CaptivePortalData.class); |
| 1349 | verify(mCallbacks).notifyCaptivePortalDataChanged(capportCaptor.capture()); |
| 1350 | assertEquals(Uri.parse(TEST_VENUE_INFO_URL), capportCaptor.getValue().getVenueInfoUrl()); |
Remi NGUYEN VAN | 2fa48c2 | 2019-12-09 16:40:02 +0900 | [diff] [blame] | 1351 | } |
| 1352 | |
| 1353 | @Test |
| 1354 | public void testIsCaptivePortal_CapportApiNotPortalPartial() throws Exception { |
| 1355 | assumeTrue(CaptivePortalDataShimImpl.isSupported()); |
| 1356 | setSslException(mHttpsConnection); |
| 1357 | setStatus(mHttpConnection, 204); |
| 1358 | setApiContent(mCapportApiConnection, "{'captive': false," |
| 1359 | + "'venue-info-url': '" + TEST_VENUE_INFO_URL + "'}"); |
Chiachang Wang | daf983b | 2020-05-07 19:20:52 +0800 | [diff] [blame] | 1360 | runNetworkTest(makeCapportLPs(), CELL_METERED_CAPABILITIES, |
Remi NGUYEN VAN | 0402afc | 2020-04-15 18:56:59 +0900 | [diff] [blame] | 1361 | NETWORK_VALIDATION_RESULT_PARTIAL, |
| 1362 | NETWORK_VALIDATION_PROBE_DNS | NETWORK_VALIDATION_PROBE_HTTP, |
| 1363 | null /* redirectUrl */); |
Remi NGUYEN VAN | 2fa48c2 | 2019-12-09 16:40:02 +0900 | [diff] [blame] | 1364 | |
Remi NGUYEN VAN | 905b0b6 | 2020-04-20 17:06:27 +0900 | [diff] [blame] | 1365 | final ArgumentCaptor<CaptivePortalData> capportCaptor = ArgumentCaptor.forClass( |
| 1366 | CaptivePortalData.class); |
| 1367 | verify(mCallbacks).notifyCaptivePortalDataChanged(capportCaptor.capture()); |
| 1368 | assertEquals(Uri.parse(TEST_VENUE_INFO_URL), capportCaptor.getValue().getVenueInfoUrl()); |
Remi NGUYEN VAN | 2fa48c2 | 2019-12-09 16:40:02 +0900 | [diff] [blame] | 1369 | } |
| 1370 | |
| 1371 | @Test |
| 1372 | public void testIsCaptivePortal_CapportApiNotPortalValidated() throws Exception { |
| 1373 | assumeTrue(CaptivePortalDataShimImpl.isSupported()); |
| 1374 | setStatus(mHttpsConnection, 204); |
| 1375 | setStatus(mHttpConnection, 204); |
| 1376 | setApiContent(mCapportApiConnection, "{'captive': false," |
| 1377 | + "'venue-info-url': '" + TEST_VENUE_INFO_URL + "'}"); |
Chiachang Wang | daf983b | 2020-05-07 19:20:52 +0800 | [diff] [blame] | 1378 | runNetworkTest(makeCapportLPs(), CELL_METERED_CAPABILITIES, |
Remi NGUYEN VAN | 0402afc | 2020-04-15 18:56:59 +0900 | [diff] [blame] | 1379 | NETWORK_VALIDATION_RESULT_VALID, |
| 1380 | NETWORK_VALIDATION_PROBE_DNS | NETWORK_VALIDATION_PROBE_HTTP |
| 1381 | | NETWORK_VALIDATION_PROBE_HTTPS, |
| 1382 | null /* redirectUrl */); |
Remi NGUYEN VAN | 2fa48c2 | 2019-12-09 16:40:02 +0900 | [diff] [blame] | 1383 | |
Remi NGUYEN VAN | 905b0b6 | 2020-04-20 17:06:27 +0900 | [diff] [blame] | 1384 | final ArgumentCaptor<CaptivePortalData> capportCaptor = ArgumentCaptor.forClass( |
| 1385 | CaptivePortalData.class); |
| 1386 | verify(mCallbacks).notifyCaptivePortalDataChanged(capportCaptor.capture()); |
| 1387 | assertEquals(Uri.parse(TEST_VENUE_INFO_URL), capportCaptor.getValue().getVenueInfoUrl()); |
Remi NGUYEN VAN | 2fa48c2 | 2019-12-09 16:40:02 +0900 | [diff] [blame] | 1388 | } |
| 1389 | |
| 1390 | @Test |
| 1391 | public void testIsCaptivePortal_CapportApiInvalidContent() throws Exception { |
| 1392 | assumeTrue(CaptivePortalDataShimImpl.isSupported()); |
Remi NGUYEN VAN | b694933 | 2020-05-12 18:03:50 +0900 | [diff] [blame] | 1393 | setSslException(mHttpsConnection); |
Remi NGUYEN VAN | 2fa48c2 | 2019-12-09 16:40:02 +0900 | [diff] [blame] | 1394 | setPortal302(mHttpConnection); |
| 1395 | setApiContent(mCapportApiConnection, "{SomeInvalidText"); |
Chiachang Wang | daf983b | 2020-05-07 19:20:52 +0800 | [diff] [blame] | 1396 | runNetworkTest(makeCapportLPs(), CELL_METERED_CAPABILITIES, |
Remi NGUYEN VAN | 0402afc | 2020-04-15 18:56:59 +0900 | [diff] [blame] | 1397 | VALIDATION_RESULT_PORTAL, 0 /* probesSucceeded */, |
| 1398 | TEST_LOGIN_URL); |
Remi NGUYEN VAN | 2fa48c2 | 2019-12-09 16:40:02 +0900 | [diff] [blame] | 1399 | |
| 1400 | verify(mCallbacks, never()).notifyCaptivePortalDataChanged(any()); |
| 1401 | verify(mHttpConnection).getResponseCode(); |
| 1402 | } |
| 1403 | |
Remi NGUYEN VAN | b694933 | 2020-05-12 18:03:50 +0900 | [diff] [blame] | 1404 | private void runCapportApiInvalidUrlTest(String url) throws Exception { |
| 1405 | assumeTrue(CaptivePortalDataShimImpl.isSupported()); |
| 1406 | setSslException(mHttpsConnection); |
| 1407 | setPortal302(mHttpConnection); |
| 1408 | final LinkProperties lp = new LinkProperties(TEST_LINK_PROPERTIES); |
| 1409 | lp.setCaptivePortalApiUrl(Uri.parse(url)); |
| 1410 | runNetworkTest(makeCapportLPs(), CELL_METERED_CAPABILITIES, |
| 1411 | VALIDATION_RESULT_PORTAL, 0 /* probesSucceeded */, |
| 1412 | TEST_LOGIN_URL); |
| 1413 | |
| 1414 | verify(mCallbacks, never()).notifyCaptivePortalDataChanged(any()); |
| 1415 | verify(mCapportApiConnection, never()).getInputStream(); |
| 1416 | verify(mHttpConnection).getResponseCode(); |
| 1417 | } |
| 1418 | |
| 1419 | @Test |
| 1420 | public void testIsCaptivePortal_HttpIsInvalidCapportApiScheme() throws Exception { |
| 1421 | runCapportApiInvalidUrlTest("http://capport.example.com"); |
| 1422 | } |
| 1423 | |
| 1424 | @Test |
| 1425 | public void testIsCaptivePortal_FileIsInvalidCapportApiScheme() throws Exception { |
| 1426 | runCapportApiInvalidUrlTest("file://localhost/myfile"); |
| 1427 | } |
| 1428 | |
| 1429 | @Test |
| 1430 | public void testIsCaptivePortal_InvalidUrlFormat() throws Exception { |
| 1431 | runCapportApiInvalidUrlTest("ThisIsNotAValidUrl"); |
| 1432 | } |
| 1433 | |
Remi NGUYEN VAN | 905b0b6 | 2020-04-20 17:06:27 +0900 | [diff] [blame] | 1434 | @Test @IgnoreUpTo(Build.VERSION_CODES.Q) |
Remi NGUYEN VAN | 2fa48c2 | 2019-12-09 16:40:02 +0900 | [diff] [blame] | 1435 | public void testIsCaptivePortal_CapportApiNotSupported() throws Exception { |
Remi NGUYEN VAN | 905b0b6 | 2020-04-20 17:06:27 +0900 | [diff] [blame] | 1436 | // Test that on a R+ device, if NetworkStack was compiled without CaptivePortalData support |
| 1437 | // (built against Q), NetworkMonitor behaves as expected. |
Remi NGUYEN VAN | 2fa48c2 | 2019-12-09 16:40:02 +0900 | [diff] [blame] | 1438 | assumeFalse(CaptivePortalDataShimImpl.isSupported()); |
| 1439 | setSslException(mHttpsConnection); |
| 1440 | setPortal302(mHttpConnection); |
| 1441 | setApiContent(mCapportApiConnection, "{'captive': false," |
| 1442 | + "'venue-info-url': '" + TEST_VENUE_INFO_URL + "'}"); |
Chiachang Wang | daf983b | 2020-05-07 19:20:52 +0800 | [diff] [blame] | 1443 | runNetworkTest(makeCapportLPs(), CELL_METERED_CAPABILITIES, VALIDATION_RESULT_PORTAL, |
Remi NGUYEN VAN | 0402afc | 2020-04-15 18:56:59 +0900 | [diff] [blame] | 1444 | 0 /* probesSucceeded */, |
| 1445 | TEST_LOGIN_URL); |
Remi NGUYEN VAN | 2fa48c2 | 2019-12-09 16:40:02 +0900 | [diff] [blame] | 1446 | |
| 1447 | verify(mCallbacks, never()).notifyCaptivePortalDataChanged(any()); |
| 1448 | verify(mHttpConnection).getResponseCode(); |
| 1449 | } |
| 1450 | |
lucaslin | d4c999f | 2020-04-09 00:31:21 +0800 | [diff] [blame] | 1451 | @Test |
| 1452 | public void testIsCaptivePortal_HttpsProbeMatchesFailRegex() throws Exception { |
| 1453 | setStatus(mHttpsConnection, 200); |
| 1454 | setStatus(mHttpConnection, 500); |
| 1455 | final String content = "test"; |
| 1456 | doReturn(new ByteArrayInputStream(content.getBytes(StandardCharsets.UTF_8))) |
| 1457 | .when(mHttpsConnection).getInputStream(); |
| 1458 | doReturn(Long.valueOf(content.length())).when(mHttpsConnection).getContentLengthLong(); |
| 1459 | doReturn(1).when(mResources).getInteger(R.integer.config_min_matches_http_content_length); |
| 1460 | doReturn(10).when(mResources).getInteger( |
| 1461 | R.integer.config_max_matches_http_content_length); |
| 1462 | doReturn("te.t").when(mResources).getString( |
| 1463 | R.string.config_network_validation_failed_content_regexp); |
| 1464 | runFailedNetworkTest(); |
| 1465 | } |
| 1466 | |
| 1467 | @Test |
| 1468 | public void testIsCaptivePortal_HttpProbeMatchesSuccessRegex() throws Exception { |
| 1469 | setStatus(mHttpsConnection, 500); |
| 1470 | setStatus(mHttpConnection, 200); |
| 1471 | final String content = "test"; |
| 1472 | doReturn(new ByteArrayInputStream(content.getBytes(StandardCharsets.UTF_8))) |
| 1473 | .when(mHttpConnection).getInputStream(); |
| 1474 | doReturn(Long.valueOf(content.length())).when(mHttpConnection).getContentLengthLong(); |
| 1475 | doReturn(1).when(mResources).getInteger(R.integer.config_min_matches_http_content_length); |
| 1476 | doReturn(10).when(mResources).getInteger( |
| 1477 | R.integer.config_max_matches_http_content_length); |
| 1478 | doReturn("te.t").when(mResources).getString( |
| 1479 | R.string.config_network_validation_success_content_regexp); |
Remi NGUYEN VAN | 0402afc | 2020-04-15 18:56:59 +0900 | [diff] [blame] | 1480 | runPartialConnectivityNetworkTest( |
| 1481 | NETWORK_VALIDATION_PROBE_DNS | NETWORK_VALIDATION_PROBE_HTTP); |
lucaslin | d4c999f | 2020-04-09 00:31:21 +0800 | [diff] [blame] | 1482 | } |
| 1483 | |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 1484 | private void setupFallbackSpec() throws IOException { |
| 1485 | setFallbackSpecs("http://example.com@@/@@204@@/@@" |
| 1486 | + "@@,@@" |
| 1487 | + TEST_OTHER_FALLBACK_URL + "@@/@@30[12]@@/@@https://(www\\.)?google.com/?.*"); |
| 1488 | |
| 1489 | setSslException(mHttpsConnection); |
| 1490 | setStatus(mHttpConnection, 500); |
| 1491 | |
| 1492 | // Use the 2nd fallback spec |
| 1493 | when(mRandom.nextInt()).thenReturn(1); |
| 1494 | } |
| 1495 | |
| 1496 | @Test |
Chiachang Wang | c9a89aa | 2020-11-13 17:04:35 +0800 | [diff] [blame] | 1497 | public void testIsCaptivePortal_FallbackSpecIsFail() throws Exception { |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 1498 | setupFallbackSpec(); |
| 1499 | set302(mOtherFallbackConnection, "https://www.google.com/test?q=3"); |
| 1500 | |
Chiachang Wang | c9a89aa | 2020-11-13 17:04:35 +0800 | [diff] [blame] | 1501 | runNetworkTest(VALIDATION_RESULT_INVALID, |
| 1502 | NETWORK_VALIDATION_PROBE_DNS | NETWORK_VALIDATION_PROBE_FALLBACK, |
| 1503 | null /* redirectUrl */); |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 1504 | verify(mOtherFallbackConnection, times(1)).getResponseCode(); |
| 1505 | verify(mFallbackConnection, never()).getResponseCode(); |
| 1506 | } |
| 1507 | |
| 1508 | @Test |
Remi NGUYEN VAN | 0402afc | 2020-04-15 18:56:59 +0900 | [diff] [blame] | 1509 | public void testIsCaptivePortal_FallbackSpecIsPortal() throws Exception { |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 1510 | setupFallbackSpec(); |
Remi NGUYEN VAN | 0402afc | 2020-04-15 18:56:59 +0900 | [diff] [blame] | 1511 | setPortal302(mOtherFallbackConnection); |
| 1512 | runPortalNetworkTest(); |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 1513 | } |
| 1514 | |
| 1515 | @Test |
Remi NGUYEN VAN | 0402afc | 2020-04-15 18:56:59 +0900 | [diff] [blame] | 1516 | public void testIsCaptivePortal_IgnorePortals() throws Exception { |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 1517 | setCaptivePortalMode(Settings.Global.CAPTIVE_PORTAL_MODE_IGNORE); |
| 1518 | setSslException(mHttpsConnection); |
| 1519 | setPortal302(mHttpConnection); |
| 1520 | |
Chiachang Wang | 813ee47 | 2019-05-23 16:29:30 +0800 | [diff] [blame] | 1521 | runNoValidationNetworkTest(); |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 1522 | } |
| 1523 | |
| 1524 | @Test |
Remi NGUYEN VAN | 943d634 | 2020-03-24 18:22:34 +0900 | [diff] [blame] | 1525 | public void testIsCaptivePortal_OverriddenHttpsUrlValid() throws Exception { |
| 1526 | setDeviceConfig(TEST_URL_EXPIRATION_TIME, |
| 1527 | String.valueOf(currentTimeMillis() + TimeUnit.MINUTES.toMillis(9))); |
| 1528 | setDeviceConfig(TEST_CAPTIVE_PORTAL_HTTPS_URL, TEST_OVERRIDE_URL); |
| 1529 | setStatus(mTestOverriddenUrlConnection, 204); |
| 1530 | setStatus(mHttpConnection, 204); |
| 1531 | |
| 1532 | runValidatedNetworkTest(); |
| 1533 | verify(mHttpsConnection, never()).getResponseCode(); |
| 1534 | verify(mTestOverriddenUrlConnection).getResponseCode(); |
| 1535 | } |
| 1536 | |
| 1537 | @Test |
| 1538 | public void testIsCaptivePortal_OverriddenHttpUrlPortal() throws Exception { |
| 1539 | setDeviceConfig(TEST_URL_EXPIRATION_TIME, |
| 1540 | String.valueOf(currentTimeMillis() + TimeUnit.MINUTES.toMillis(9))); |
| 1541 | setDeviceConfig(TEST_CAPTIVE_PORTAL_HTTP_URL, TEST_OVERRIDE_URL); |
| 1542 | setStatus(mHttpsConnection, 500); |
| 1543 | setPortal302(mTestOverriddenUrlConnection); |
| 1544 | |
| 1545 | runPortalNetworkTest(); |
| 1546 | verify(mHttpConnection, never()).getResponseCode(); |
| 1547 | verify(mTestOverriddenUrlConnection).getResponseCode(); |
| 1548 | } |
| 1549 | |
| 1550 | @Test |
| 1551 | public void testIsCaptivePortal_InvalidHttpOverrideUrl() throws Exception { |
| 1552 | setDeviceConfig(TEST_URL_EXPIRATION_TIME, |
| 1553 | String.valueOf(currentTimeMillis() + TimeUnit.MINUTES.toMillis(9))); |
| 1554 | setDeviceConfig(TEST_CAPTIVE_PORTAL_HTTP_URL, TEST_INVALID_OVERRIDE_URL); |
| 1555 | setStatus(mHttpsConnection, 500); |
| 1556 | setPortal302(mHttpConnection); |
| 1557 | |
| 1558 | runPortalNetworkTest(); |
| 1559 | verify(mTestOverriddenUrlConnection, never()).getResponseCode(); |
| 1560 | verify(mHttpConnection).getResponseCode(); |
| 1561 | } |
| 1562 | |
| 1563 | @Test |
| 1564 | public void testIsCaptivePortal_InvalidHttpsOverrideUrl() throws Exception { |
| 1565 | setDeviceConfig(TEST_URL_EXPIRATION_TIME, |
| 1566 | String.valueOf(currentTimeMillis() + TimeUnit.MINUTES.toMillis(9))); |
| 1567 | setDeviceConfig(TEST_CAPTIVE_PORTAL_HTTPS_URL, TEST_INVALID_OVERRIDE_URL); |
| 1568 | setStatus(mHttpsConnection, 204); |
| 1569 | setStatus(mHttpConnection, 204); |
| 1570 | |
| 1571 | runValidatedNetworkTest(); |
| 1572 | verify(mTestOverriddenUrlConnection, never()).getResponseCode(); |
| 1573 | verify(mHttpsConnection).getResponseCode(); |
| 1574 | } |
| 1575 | |
| 1576 | @Test |
| 1577 | public void testIsCaptivePortal_ExpiredHttpsOverrideUrl() throws Exception { |
| 1578 | setDeviceConfig(TEST_URL_EXPIRATION_TIME, |
| 1579 | String.valueOf(currentTimeMillis() - TimeUnit.MINUTES.toMillis(1))); |
| 1580 | setDeviceConfig(TEST_CAPTIVE_PORTAL_HTTPS_URL, TEST_OVERRIDE_URL); |
| 1581 | setStatus(mHttpsConnection, 204); |
| 1582 | setStatus(mHttpConnection, 204); |
| 1583 | |
| 1584 | runValidatedNetworkTest(); |
| 1585 | verify(mTestOverriddenUrlConnection, never()).getResponseCode(); |
| 1586 | verify(mHttpsConnection).getResponseCode(); |
| 1587 | } |
| 1588 | |
| 1589 | @Test |
| 1590 | public void testIsCaptivePortal_TestHttpUrlExpirationTooLarge() throws Exception { |
| 1591 | setDeviceConfig(TEST_URL_EXPIRATION_TIME, |
| 1592 | String.valueOf(currentTimeMillis() + TimeUnit.MINUTES.toMillis(20))); |
| 1593 | setDeviceConfig(TEST_CAPTIVE_PORTAL_HTTP_URL, TEST_OVERRIDE_URL); |
| 1594 | setStatus(mHttpsConnection, 500); |
| 1595 | setPortal302(mHttpConnection); |
| 1596 | |
| 1597 | runPortalNetworkTest(); |
| 1598 | verify(mTestOverriddenUrlConnection, never()).getResponseCode(); |
| 1599 | verify(mHttpConnection).getResponseCode(); |
| 1600 | } |
| 1601 | |
| 1602 | @Test |
Remi NGUYEN VAN | 0d8cf03 | 2020-08-21 16:49:46 +0900 | [diff] [blame] | 1603 | public void testIsCaptivePortal_TestUrlsWithUrlOverlays() throws Exception { |
| 1604 | setupResourceForMultipleProbes(); |
| 1605 | doReturn(TEST_HTTPS_URL).when(mResources) |
| 1606 | .getString(R.string.config_captive_portal_https_url); |
| 1607 | doReturn(TEST_HTTP_URL).when(mResources) |
| 1608 | .getString(R.string.config_captive_portal_http_url); |
| 1609 | |
| 1610 | setDeviceConfig(TEST_URL_EXPIRATION_TIME, |
| 1611 | String.valueOf(currentTimeMillis() + TimeUnit.MINUTES.toMillis(9))); |
| 1612 | setDeviceConfig(TEST_CAPTIVE_PORTAL_HTTPS_URL, TEST_OVERRIDE_URL); |
| 1613 | setDeviceConfig(TEST_CAPTIVE_PORTAL_HTTP_URL, TEST_OVERRIDE_URL); |
| 1614 | setStatus(mTestOverriddenUrlConnection, 204); |
| 1615 | |
| 1616 | runValidatedNetworkTest(); |
| 1617 | verify(mHttpsConnection, never()).getResponseCode(); |
| 1618 | verify(mHttpConnection, never()).getResponseCode(); |
| 1619 | verify(mOtherHttpsConnection1, never()).getResponseCode(); |
| 1620 | verify(mOtherHttpsConnection2, never()).getResponseCode(); |
| 1621 | verify(mOtherHttpConnection1, never()).getResponseCode(); |
| 1622 | verify(mOtherHttpConnection2, never()).getResponseCode(); |
| 1623 | |
| 1624 | // Used for both HTTP and HTTPS: can be called once (if HTTPS validates first) or twice |
| 1625 | verify(mTestOverriddenUrlConnection, atLeastOnce()).getResponseCode(); |
| 1626 | } |
| 1627 | |
| 1628 | @Test |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 1629 | public void testIsDataStall_EvaluationDisabled() { |
| 1630 | setDataStallEvaluationType(0); |
Chiachang Wang | daf983b | 2020-05-07 19:20:52 +0800 | [diff] [blame] | 1631 | WrappedNetworkMonitor wrappedMonitor = makeCellMeteredNetworkMonitor(); |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 1632 | wrappedMonitor.setLastProbeTime(SystemClock.elapsedRealtime() - 100); |
| 1633 | assertFalse(wrappedMonitor.isDataStall()); |
| 1634 | } |
| 1635 | |
| 1636 | @Test |
Cody Kesting | 782cbfa | 2020-01-06 15:51:59 -0800 | [diff] [blame] | 1637 | public void testIsDataStall_EvaluationDnsOnNotMeteredNetwork() throws Exception { |
Chiachang Wang | daf983b | 2020-05-07 19:20:52 +0800 | [diff] [blame] | 1638 | WrappedNetworkMonitor wrappedMonitor = makeCellNotMeteredNetworkMonitor(); |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 1639 | wrappedMonitor.setLastProbeTime(SystemClock.elapsedRealtime() - 100); |
Chiachang Wang | caa3520 | 2019-02-26 11:32:18 +0800 | [diff] [blame] | 1640 | makeDnsTimeoutEvent(wrappedMonitor, DEFAULT_DNS_TIMEOUT_THRESHOLD); |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 1641 | assertTrue(wrappedMonitor.isDataStall()); |
Remi NGUYEN VAN | 0402afc | 2020-04-15 18:56:59 +0900 | [diff] [blame] | 1642 | verify(mCallbacks).notifyDataStallSuspected( |
| 1643 | matchDnsDataStallParcelable(DEFAULT_DNS_TIMEOUT_THRESHOLD)); |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 1644 | } |
| 1645 | |
| 1646 | @Test |
Cody Kesting | 782cbfa | 2020-01-06 15:51:59 -0800 | [diff] [blame] | 1647 | public void testIsDataStall_EvaluationDnsOnMeteredNetwork() throws Exception { |
Chiachang Wang | daf983b | 2020-05-07 19:20:52 +0800 | [diff] [blame] | 1648 | WrappedNetworkMonitor wrappedMonitor = makeCellMeteredNetworkMonitor(); |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 1649 | wrappedMonitor.setLastProbeTime(SystemClock.elapsedRealtime() - 100); |
| 1650 | assertFalse(wrappedMonitor.isDataStall()); |
| 1651 | |
Chiachang Wang | 4fcbe91 | 2021-04-26 13:02:22 +0800 | [diff] [blame] | 1652 | wrappedMonitor.setLastProbeTime( |
| 1653 | SystemClock.elapsedRealtime() - STALL_EXPECTED_LAST_PROBE_TIME_MS); |
Chiachang Wang | caa3520 | 2019-02-26 11:32:18 +0800 | [diff] [blame] | 1654 | makeDnsTimeoutEvent(wrappedMonitor, DEFAULT_DNS_TIMEOUT_THRESHOLD); |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 1655 | assertTrue(wrappedMonitor.isDataStall()); |
Remi NGUYEN VAN | 0402afc | 2020-04-15 18:56:59 +0900 | [diff] [blame] | 1656 | verify(mCallbacks).notifyDataStallSuspected( |
| 1657 | matchDnsDataStallParcelable(DEFAULT_DNS_TIMEOUT_THRESHOLD)); |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 1658 | } |
| 1659 | |
| 1660 | @Test |
Cody Kesting | 782cbfa | 2020-01-06 15:51:59 -0800 | [diff] [blame] | 1661 | public void testIsDataStall_EvaluationDnsWithDnsTimeoutCount() throws Exception { |
Chiachang Wang | daf983b | 2020-05-07 19:20:52 +0800 | [diff] [blame] | 1662 | WrappedNetworkMonitor wrappedMonitor = makeCellMeteredNetworkMonitor(); |
Chiachang Wang | 4fcbe91 | 2021-04-26 13:02:22 +0800 | [diff] [blame] | 1663 | wrappedMonitor.setLastProbeTime( |
| 1664 | SystemClock.elapsedRealtime() - STALL_EXPECTED_LAST_PROBE_TIME_MS); |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 1665 | makeDnsTimeoutEvent(wrappedMonitor, 3); |
| 1666 | assertFalse(wrappedMonitor.isDataStall()); |
| 1667 | // Reset consecutive timeout counts. |
| 1668 | makeDnsSuccessEvent(wrappedMonitor, 1); |
| 1669 | makeDnsTimeoutEvent(wrappedMonitor, 2); |
| 1670 | assertFalse(wrappedMonitor.isDataStall()); |
| 1671 | |
| 1672 | makeDnsTimeoutEvent(wrappedMonitor, 3); |
| 1673 | assertTrue(wrappedMonitor.isDataStall()); |
Cody Kesting | bfe0e00 | 2020-02-04 12:52:44 -0800 | [diff] [blame] | 1674 | |
| 1675 | // The expected timeout count is the previous 2 DNS timeouts + the most recent 3 timeouts |
Remi NGUYEN VAN | 0402afc | 2020-04-15 18:56:59 +0900 | [diff] [blame] | 1676 | verify(mCallbacks).notifyDataStallSuspected( |
| 1677 | matchDnsDataStallParcelable(5 /* timeoutCount */)); |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 1678 | |
| 1679 | // Set the value to larger than the default dns log size. |
| 1680 | setConsecutiveDnsTimeoutThreshold(51); |
Chiachang Wang | daf983b | 2020-05-07 19:20:52 +0800 | [diff] [blame] | 1681 | wrappedMonitor = makeCellMeteredNetworkMonitor(); |
Chiachang Wang | 4fcbe91 | 2021-04-26 13:02:22 +0800 | [diff] [blame] | 1682 | wrappedMonitor.setLastProbeTime( |
| 1683 | SystemClock.elapsedRealtime() - STALL_EXPECTED_LAST_PROBE_TIME_MS); |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 1684 | makeDnsTimeoutEvent(wrappedMonitor, 50); |
| 1685 | assertFalse(wrappedMonitor.isDataStall()); |
| 1686 | |
| 1687 | makeDnsTimeoutEvent(wrappedMonitor, 1); |
| 1688 | assertTrue(wrappedMonitor.isDataStall()); |
Cody Kesting | bfe0e00 | 2020-02-04 12:52:44 -0800 | [diff] [blame] | 1689 | |
| 1690 | // The expected timeout count is the previous 50 DNS timeouts + the most recent timeout |
Remi NGUYEN VAN | 0402afc | 2020-04-15 18:56:59 +0900 | [diff] [blame] | 1691 | verify(mCallbacks).notifyDataStallSuspected( |
| 1692 | matchDnsDataStallParcelable(51 /* timeoutCount */)); |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 1693 | } |
| 1694 | |
| 1695 | @Test |
Chiachang Wang | b6dbfb4 | 2020-01-21 14:47:34 +0800 | [diff] [blame] | 1696 | public void testIsDataStall_SkipEvaluateOnValidationNotRequiredNetwork() { |
| 1697 | // Make DNS and TCP stall condition satisfied. |
| 1698 | setDataStallEvaluationType(DATA_STALL_EVALUATION_TYPE_DNS | DATA_STALL_EVALUATION_TYPE_TCP); |
| 1699 | when(mTstDependencies.isTcpInfoParsingSupported()).thenReturn(true); |
| 1700 | when(mTst.getLatestReceivedCount()).thenReturn(0); |
| 1701 | when(mTst.isDataStallSuspected()).thenReturn(true); |
Chiachang Wang | daf983b | 2020-05-07 19:20:52 +0800 | [diff] [blame] | 1702 | final WrappedNetworkMonitor nm = makeMonitor(CELL_NO_INTERNET_CAPABILITIES); |
Chiachang Wang | b6dbfb4 | 2020-01-21 14:47:34 +0800 | [diff] [blame] | 1703 | nm.setLastProbeTime(SystemClock.elapsedRealtime() - 1000); |
| 1704 | makeDnsTimeoutEvent(nm, DEFAULT_DNS_TIMEOUT_THRESHOLD); |
| 1705 | assertFalse(nm.isDataStall()); |
| 1706 | } |
| 1707 | |
| 1708 | @Test |
Cody Kesting | 782cbfa | 2020-01-06 15:51:59 -0800 | [diff] [blame] | 1709 | public void testIsDataStall_EvaluationDnsWithDnsTimeThreshold() throws Exception { |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 1710 | // Test dns events happened in valid dns time threshold. |
Chiachang Wang | daf983b | 2020-05-07 19:20:52 +0800 | [diff] [blame] | 1711 | WrappedNetworkMonitor wrappedMonitor = makeCellMeteredNetworkMonitor(); |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 1712 | wrappedMonitor.setLastProbeTime(SystemClock.elapsedRealtime() - 100); |
Chiachang Wang | caa3520 | 2019-02-26 11:32:18 +0800 | [diff] [blame] | 1713 | makeDnsTimeoutEvent(wrappedMonitor, DEFAULT_DNS_TIMEOUT_THRESHOLD); |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 1714 | assertFalse(wrappedMonitor.isDataStall()); |
Chiachang Wang | 4fcbe91 | 2021-04-26 13:02:22 +0800 | [diff] [blame] | 1715 | wrappedMonitor.setLastProbeTime( |
| 1716 | SystemClock.elapsedRealtime() - STALL_EXPECTED_LAST_PROBE_TIME_MS); |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 1717 | assertTrue(wrappedMonitor.isDataStall()); |
Remi NGUYEN VAN | 0402afc | 2020-04-15 18:56:59 +0900 | [diff] [blame] | 1718 | verify(mCallbacks).notifyDataStallSuspected( |
| 1719 | matchDnsDataStallParcelable(DEFAULT_DNS_TIMEOUT_THRESHOLD)); |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 1720 | |
| 1721 | // Test dns events happened before valid dns time threshold. |
| 1722 | setValidDataStallDnsTimeThreshold(0); |
Chiachang Wang | daf983b | 2020-05-07 19:20:52 +0800 | [diff] [blame] | 1723 | wrappedMonitor = makeCellMeteredNetworkMonitor(); |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 1724 | wrappedMonitor.setLastProbeTime(SystemClock.elapsedRealtime() - 100); |
Chiachang Wang | caa3520 | 2019-02-26 11:32:18 +0800 | [diff] [blame] | 1725 | makeDnsTimeoutEvent(wrappedMonitor, DEFAULT_DNS_TIMEOUT_THRESHOLD); |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 1726 | assertFalse(wrappedMonitor.isDataStall()); |
Chiachang Wang | 4fcbe91 | 2021-04-26 13:02:22 +0800 | [diff] [blame] | 1727 | wrappedMonitor.setLastProbeTime( |
| 1728 | SystemClock.elapsedRealtime() - STALL_EXPECTED_LAST_PROBE_TIME_MS); |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 1729 | assertFalse(wrappedMonitor.isDataStall()); |
| 1730 | } |
| 1731 | |
| 1732 | @Test |
Cody Kesting | 782cbfa | 2020-01-06 15:51:59 -0800 | [diff] [blame] | 1733 | public void testIsDataStall_EvaluationTcp() throws Exception { |
Chiachang Wang | a5716bf | 2019-11-20 16:13:07 +0800 | [diff] [blame] | 1734 | // Evaluate TCP only. Expect ignoring DNS signal. |
| 1735 | setDataStallEvaluationType(DATA_STALL_EVALUATION_TYPE_TCP); |
Chiachang Wang | daf983b | 2020-05-07 19:20:52 +0800 | [diff] [blame] | 1736 | WrappedNetworkMonitor wrappedMonitor = makeMonitor(CELL_METERED_CAPABILITIES); |
Chiachang Wang | a5716bf | 2019-11-20 16:13:07 +0800 | [diff] [blame] | 1737 | assertFalse(wrappedMonitor.isDataStall()); |
Chiachang Wang | 4336b91 | 2019-11-26 15:39:22 +0800 | [diff] [blame] | 1738 | // Packet received. |
| 1739 | when(mTstDependencies.isTcpInfoParsingSupported()).thenReturn(true); |
| 1740 | when(mTst.getLatestReceivedCount()).thenReturn(5); |
| 1741 | // Trigger a tcp event immediately. |
| 1742 | setTcpPollingInterval(0); |
| 1743 | wrappedMonitor.sendTcpPollingEvent(); |
Chalard Jean | 23a0630 | 2020-06-26 00:41:00 +0900 | [diff] [blame] | 1744 | HandlerUtils.waitForIdle(wrappedMonitor.getHandler(), HANDLER_TIMEOUT_MS); |
Chiachang Wang | 4336b91 | 2019-11-26 15:39:22 +0800 | [diff] [blame] | 1745 | assertFalse(wrappedMonitor.isDataStall()); |
Chiachang Wang | e797c9b | 2019-11-28 14:18:47 +0800 | [diff] [blame] | 1746 | |
Chiachang Wang | 4336b91 | 2019-11-26 15:39:22 +0800 | [diff] [blame] | 1747 | when(mTst.getLatestReceivedCount()).thenReturn(0); |
| 1748 | when(mTst.isDataStallSuspected()).thenReturn(true); |
Chiachang Wang | a5716bf | 2019-11-20 16:13:07 +0800 | [diff] [blame] | 1749 | // Trigger a tcp event immediately. |
| 1750 | setTcpPollingInterval(0); |
| 1751 | wrappedMonitor.sendTcpPollingEvent(); |
Chalard Jean | 23a0630 | 2020-06-26 00:41:00 +0900 | [diff] [blame] | 1752 | HandlerUtils.waitForIdle(wrappedMonitor.getHandler(), HANDLER_TIMEOUT_MS); |
Chiachang Wang | a5716bf | 2019-11-20 16:13:07 +0800 | [diff] [blame] | 1753 | assertTrue(wrappedMonitor.isDataStall()); |
Remi NGUYEN VAN | 0402afc | 2020-04-15 18:56:59 +0900 | [diff] [blame] | 1754 | verify(mCallbacks).notifyDataStallSuspected(matchTcpDataStallParcelable()); |
Chiachang Wang | a5716bf | 2019-11-20 16:13:07 +0800 | [diff] [blame] | 1755 | } |
| 1756 | |
| 1757 | @Test |
Chiachang Wang | 340d275 | 2020-05-21 17:51:22 +0800 | [diff] [blame] | 1758 | public void testIsDataStall_EvaluationDnsAndTcp() throws Exception { |
| 1759 | setDataStallEvaluationType(DATA_STALL_EVALUATION_TYPE_DNS | DATA_STALL_EVALUATION_TYPE_TCP); |
| 1760 | setupTcpDataStall(); |
| 1761 | final WrappedNetworkMonitor nm = makeMonitor(CELL_METERED_CAPABILITIES); |
Chiachang Wang | 4fcbe91 | 2021-04-26 13:02:22 +0800 | [diff] [blame] | 1762 | nm.setLastProbeTime(SystemClock.elapsedRealtime() - STALL_EXPECTED_LAST_PROBE_TIME_MS); |
Chiachang Wang | 340d275 | 2020-05-21 17:51:22 +0800 | [diff] [blame] | 1763 | makeDnsTimeoutEvent(nm, DEFAULT_DNS_TIMEOUT_THRESHOLD); |
| 1764 | assertTrue(nm.isDataStall()); |
| 1765 | verify(mCallbacks).notifyDataStallSuspected( |
| 1766 | matchDnsAndTcpDataStallParcelable(DEFAULT_DNS_TIMEOUT_THRESHOLD)); |
| 1767 | |
| 1768 | when(mTst.getLatestReceivedCount()).thenReturn(5); |
| 1769 | // Trigger a tcp event immediately. |
| 1770 | setTcpPollingInterval(0); |
| 1771 | nm.sendTcpPollingEvent(); |
Chalard Jean | 23a0630 | 2020-06-26 00:41:00 +0900 | [diff] [blame] | 1772 | HandlerUtils.waitForIdle(nm.getHandler(), HANDLER_TIMEOUT_MS); |
Chiachang Wang | 340d275 | 2020-05-21 17:51:22 +0800 | [diff] [blame] | 1773 | assertFalse(nm.isDataStall()); |
| 1774 | } |
| 1775 | |
| 1776 | @Test |
Chiachang Wang | e797c9b | 2019-11-28 14:18:47 +0800 | [diff] [blame] | 1777 | public void testIsDataStall_DisableTcp() { |
| 1778 | // Disable tcp detection with only DNS detect. keep the tcp signal but set to no DNS signal. |
| 1779 | setDataStallEvaluationType(DATA_STALL_EVALUATION_TYPE_DNS); |
Chiachang Wang | daf983b | 2020-05-07 19:20:52 +0800 | [diff] [blame] | 1780 | WrappedNetworkMonitor wrappedMonitor = makeMonitor(CELL_METERED_CAPABILITIES); |
Chiachang Wang | e797c9b | 2019-11-28 14:18:47 +0800 | [diff] [blame] | 1781 | makeDnsSuccessEvent(wrappedMonitor, 1); |
| 1782 | wrappedMonitor.sendTcpPollingEvent(); |
Chalard Jean | 23a0630 | 2020-06-26 00:41:00 +0900 | [diff] [blame] | 1783 | HandlerUtils.waitForIdle(wrappedMonitor.getHandler(), HANDLER_TIMEOUT_MS); |
Chiachang Wang | e797c9b | 2019-11-28 14:18:47 +0800 | [diff] [blame] | 1784 | assertFalse(wrappedMonitor.isDataStall()); |
| 1785 | verify(mTst, never()).isDataStallSuspected(); |
| 1786 | verify(mTst, never()).pollSocketsInfo(); |
| 1787 | } |
| 1788 | |
| 1789 | @Test |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 1790 | public void testBrokenNetworkNotValidated() throws Exception { |
| 1791 | setSslException(mHttpsConnection); |
| 1792 | setStatus(mHttpConnection, 500); |
| 1793 | setStatus(mFallbackConnection, 404); |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 1794 | |
Remi NGUYEN VAN | dc6e640 | 2019-03-27 15:42:53 +0900 | [diff] [blame] | 1795 | runFailedNetworkTest(); |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 1796 | } |
| 1797 | |
Remi NGUYEN VAN | 1b7a063 | 2021-07-07 17:54:09 +0900 | [diff] [blame] | 1798 | private void doValidationSkippedTest(NetworkCapabilities nc) throws Exception { |
Cody Kesting | 78156e4 | 2020-08-03 18:19:56 -0700 | [diff] [blame] | 1799 | // For S+, the RESULT_SKIPPED bit will be included on networks that both do not require |
| 1800 | // validation and for which validation is not performed. |
| 1801 | final int validationResult = ShimUtils.isAtLeastS() |
| 1802 | ? NETWORK_VALIDATION_RESULT_VALID | NETWORK_VALIDATION_RESULT_SKIPPED |
| 1803 | : NETWORK_VALIDATION_RESULT_VALID; |
Remi NGUYEN VAN | 1b7a063 | 2021-07-07 17:54:09 +0900 | [diff] [blame] | 1804 | runNetworkTest(TEST_LINK_PROPERTIES, nc, validationResult, |
Cody Kesting | 78156e4 | 2020-08-03 18:19:56 -0700 | [diff] [blame] | 1805 | 0 /* probesSucceeded */, null /* redirectUrl */); |
Lorenzo Colitti | 7f9734f | 2019-05-09 12:13:54 +0900 | [diff] [blame] | 1806 | verify(mCleartextDnsNetwork, never()).openConnection(any()); |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 1807 | } |
| 1808 | |
Remi NGUYEN VAN | 1b7a063 | 2021-07-07 17:54:09 +0900 | [diff] [blame] | 1809 | @Test |
| 1810 | public void testNoInternetCapabilityValidated() throws Exception { |
| 1811 | doValidationSkippedTest(CELL_NO_INTERNET_CAPABILITIES); |
| 1812 | } |
| 1813 | |
| 1814 | @Test |
| 1815 | public void testNoTrustedCapabilityValidated() throws Exception { |
Remi NGUYEN VAN | 38b1a38 | 2021-07-15 12:54:25 +0900 | [diff] [blame^] | 1816 | // Cannot use the NetworkCapabilities builder on Q |
| 1817 | final NetworkCapabilities nc = new NetworkCapabilities() |
Remi NGUYEN VAN | 1b7a063 | 2021-07-07 17:54:09 +0900 | [diff] [blame] | 1818 | .addCapability(NET_CAPABILITY_INTERNET) |
| 1819 | .removeCapability(NET_CAPABILITY_TRUSTED) |
| 1820 | .addTransportType(TRANSPORT_CELLULAR); |
| 1821 | if (ShimUtils.isAtLeastS()) { |
| 1822 | nc.addCapability(NET_CAPABILITY_NOT_VCN_MANAGED); |
| 1823 | } |
Remi NGUYEN VAN | 38b1a38 | 2021-07-15 12:54:25 +0900 | [diff] [blame^] | 1824 | doValidationSkippedTest(nc); |
Remi NGUYEN VAN | 1b7a063 | 2021-07-07 17:54:09 +0900 | [diff] [blame] | 1825 | } |
| 1826 | |
| 1827 | @Test |
| 1828 | public void testRestrictedCapabilityValidated() throws Exception { |
Remi NGUYEN VAN | 38b1a38 | 2021-07-15 12:54:25 +0900 | [diff] [blame^] | 1829 | // Cannot use the NetworkCapabilities builder on Q |
| 1830 | final NetworkCapabilities nc = new NetworkCapabilities() |
Remi NGUYEN VAN | 1b7a063 | 2021-07-07 17:54:09 +0900 | [diff] [blame] | 1831 | .addCapability(NET_CAPABILITY_INTERNET) |
| 1832 | .removeCapability(NET_CAPABILITY_NOT_RESTRICTED) |
| 1833 | .addTransportType(TRANSPORT_CELLULAR); |
| 1834 | if (ShimUtils.isAtLeastS()) { |
| 1835 | nc.addCapability(NET_CAPABILITY_NOT_VCN_MANAGED); |
| 1836 | } |
Remi NGUYEN VAN | 38b1a38 | 2021-07-15 12:54:25 +0900 | [diff] [blame^] | 1837 | doValidationSkippedTest(nc); |
Remi NGUYEN VAN | 1b7a063 | 2021-07-07 17:54:09 +0900 | [diff] [blame] | 1838 | } |
| 1839 | |
Benedict Wong | e3f5347 | 2021-05-12 10:09:00 -0700 | [diff] [blame] | 1840 | private NetworkCapabilities getVcnUnderlyingCarrierWifiCaps() { |
| 1841 | // Must be called from within the test because NOT_VCN_MANAGED is an invalid capability |
| 1842 | // value up to Android R. Thus, this must be guarded by an SDK check in tests that use this. |
| 1843 | return new NetworkCapabilities.Builder() |
| 1844 | .addTransportType(NetworkCapabilities.TRANSPORT_WIFI) |
Remi NGUYEN VAN | 1b7a063 | 2021-07-07 17:54:09 +0900 | [diff] [blame] | 1845 | .removeCapability(NetworkCapabilities.NET_CAPABILITY_NOT_VCN_MANAGED) |
Benedict Wong | e3f5347 | 2021-05-12 10:09:00 -0700 | [diff] [blame] | 1846 | .removeCapability(NetworkCapabilities.NET_CAPABILITY_NOT_RESTRICTED) |
| 1847 | .removeCapability(NetworkCapabilities.NET_CAPABILITY_TRUSTED) |
| 1848 | .addCapability(NET_CAPABILITY_INTERNET) |
| 1849 | .build(); |
| 1850 | } |
| 1851 | |
| 1852 | @Test |
| 1853 | public void testVcnUnderlyingNetwork() throws Exception { |
| 1854 | assumeTrue(ShimUtils.isAtLeastS()); |
| 1855 | setStatus(mHttpsConnection, 204); |
| 1856 | setStatus(mHttpConnection, 204); |
| 1857 | |
| 1858 | final NetworkMonitor nm = runNetworkTest( |
| 1859 | TEST_LINK_PROPERTIES, getVcnUnderlyingCarrierWifiCaps(), |
| 1860 | NETWORK_VALIDATION_RESULT_VALID, |
| 1861 | NETWORK_VALIDATION_PROBE_DNS | NETWORK_VALIDATION_PROBE_HTTPS, |
| 1862 | null /* redirectUrl */); |
| 1863 | assertEquals(NETWORK_VALIDATION_RESULT_VALID, |
| 1864 | nm.getEvaluationState().getEvaluationResult()); |
| 1865 | } |
| 1866 | |
| 1867 | @Test |
| 1868 | public void testVcnUnderlyingNetworkBadNetwork() throws Exception { |
| 1869 | assumeTrue(ShimUtils.isAtLeastS()); |
| 1870 | setSslException(mHttpsConnection); |
| 1871 | setStatus(mHttpConnection, 500); |
| 1872 | setStatus(mFallbackConnection, 404); |
| 1873 | |
| 1874 | final NetworkMonitor nm = runNetworkTest( |
| 1875 | TEST_LINK_PROPERTIES, getVcnUnderlyingCarrierWifiCaps(), |
| 1876 | VALIDATION_RESULT_INVALID, 0 /* probesSucceeded */, null /* redirectUrl */); |
| 1877 | assertEquals(VALIDATION_RESULT_INVALID, |
| 1878 | nm.getEvaluationState().getEvaluationResult()); |
| 1879 | } |
| 1880 | |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 1881 | @Test |
| 1882 | public void testLaunchCaptivePortalApp() throws Exception { |
| 1883 | setSslException(mHttpsConnection); |
| 1884 | setPortal302(mHttpConnection); |
Chiachang Wang | 26a626a | 2020-03-02 17:41:58 +0800 | [diff] [blame] | 1885 | when(mHttpConnection.getHeaderField(eq("location"))).thenReturn(TEST_LOGIN_URL); |
Chiachang Wang | daf983b | 2020-05-07 19:20:52 +0800 | [diff] [blame] | 1886 | final NetworkMonitor nm = makeMonitor(CELL_METERED_CAPABILITIES); |
| 1887 | notifyNetworkConnected(nm, CELL_METERED_CAPABILITIES); |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 1888 | |
| 1889 | verify(mCallbacks, timeout(HANDLER_TIMEOUT_MS).times(1)) |
Remi NGUYEN VAN | 1e3eb37 | 2019-02-07 21:29:57 +0900 | [diff] [blame] | 1890 | .showProvisioningNotification(any(), any()); |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 1891 | |
Lorenzo Colitti | 37ee46f | 2019-04-08 21:51:41 +0900 | [diff] [blame] | 1892 | assertEquals(1, mRegisteredReceivers.size()); |
| 1893 | |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 1894 | // Check that startCaptivePortalApp sends the expected intent. |
| 1895 | nm.launchCaptivePortalApp(); |
| 1896 | |
Remi NGUYEN VAN | ad99e54 | 2019-02-13 20:58:59 +0900 | [diff] [blame] | 1897 | final ArgumentCaptor<Bundle> bundleCaptor = ArgumentCaptor.forClass(Bundle.class); |
| 1898 | final ArgumentCaptor<Network> networkCaptor = ArgumentCaptor.forClass(Network.class); |
| 1899 | verify(mCm, timeout(HANDLER_TIMEOUT_MS).times(1)) |
| 1900 | .startCaptivePortalApp(networkCaptor.capture(), bundleCaptor.capture()); |
Remi NGUYEN VAN | 89cd026 | 2019-12-29 22:46:00 +0900 | [diff] [blame] | 1901 | verify(mNotifier).notifyCaptivePortalValidationPending(networkCaptor.getValue()); |
Remi NGUYEN VAN | ad99e54 | 2019-02-13 20:58:59 +0900 | [diff] [blame] | 1902 | final Bundle bundle = bundleCaptor.getValue(); |
| 1903 | final Network bundleNetwork = bundle.getParcelable(ConnectivityManager.EXTRA_NETWORK); |
| 1904 | assertEquals(TEST_NETID, bundleNetwork.netId); |
| 1905 | // network is passed both in bundle and as parameter, as the bundle is opaque to the |
| 1906 | // framework and only intended for the captive portal app, but the framework needs |
| 1907 | // the network to identify the right NetworkMonitor. |
| 1908 | assertEquals(TEST_NETID, networkCaptor.getValue().netId); |
Chiachang Wang | 26a626a | 2020-03-02 17:41:58 +0800 | [diff] [blame] | 1909 | // Portal URL should be detection URL. |
| 1910 | final String redirectUrl = bundle.getString(ConnectivityManager.EXTRA_CAPTIVE_PORTAL_URL); |
| 1911 | assertEquals(TEST_HTTP_URL, redirectUrl); |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 1912 | |
| 1913 | // Have the app report that the captive portal is dismissed, and check that we revalidate. |
| 1914 | setStatus(mHttpsConnection, 204); |
| 1915 | setStatus(mHttpConnection, 204); |
Remi NGUYEN VAN | ad99e54 | 2019-02-13 20:58:59 +0900 | [diff] [blame] | 1916 | |
Remi NGUYEN VAN | b4af130 | 2019-06-11 16:17:46 +0900 | [diff] [blame] | 1917 | resetCallbacks(); |
Remi NGUYEN VAN | ad99e54 | 2019-02-13 20:58:59 +0900 | [diff] [blame] | 1918 | nm.notifyCaptivePortalAppFinished(APP_RETURN_DISMISSED); |
Chiachang Wang | 813ee47 | 2019-05-23 16:29:30 +0800 | [diff] [blame] | 1919 | verify(mCallbacks, timeout(HANDLER_TIMEOUT_MS).atLeastOnce()) |
Remi NGUYEN VAN | 0402afc | 2020-04-15 18:56:59 +0900 | [diff] [blame] | 1920 | .notifyNetworkTestedWithExtras(matchNetworkTestResultParcelable( |
| 1921 | NETWORK_VALIDATION_RESULT_VALID, |
| 1922 | NETWORK_VALIDATION_PROBE_DNS | NETWORK_VALIDATION_PROBE_HTTP)); |
Lorenzo Colitti | 37ee46f | 2019-04-08 21:51:41 +0900 | [diff] [blame] | 1923 | assertEquals(0, mRegisteredReceivers.size()); |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 1924 | } |
| 1925 | |
Chiachang Wang | 8b5f84a | 2019-02-22 11:13:07 +0800 | [diff] [blame] | 1926 | @Test |
Lorenzo Colitti | cfc1abc | 2019-04-28 00:55:19 +0900 | [diff] [blame] | 1927 | public void testPrivateDnsSuccess() throws Exception { |
| 1928 | setStatus(mHttpsConnection, 204); |
| 1929 | setStatus(mHttpConnection, 204); |
Lorenzo Colitti | cfc1abc | 2019-04-28 00:55:19 +0900 | [diff] [blame] | 1930 | |
Chiachang Wang | eb61922 | 2019-07-03 20:52:08 +0800 | [diff] [blame] | 1931 | // Verify dns query only get v6 address. |
| 1932 | mFakeDns.setAnswer("dns6.google", new String[]{"2001:db8::53"}, TYPE_AAAA); |
Chiachang Wang | daf983b | 2020-05-07 19:20:52 +0800 | [diff] [blame] | 1933 | WrappedNetworkMonitor wnm = makeCellNotMeteredNetworkMonitor(); |
Chiachang Wang | eb61922 | 2019-07-03 20:52:08 +0800 | [diff] [blame] | 1934 | wnm.notifyPrivateDnsSettingsChanged(new PrivateDnsConfig("dns6.google", |
| 1935 | new InetAddress[0])); |
Chiachang Wang | daf983b | 2020-05-07 19:20:52 +0800 | [diff] [blame] | 1936 | notifyNetworkConnected(wnm, CELL_NOT_METERED_CAPABILITIES); |
Remi NGUYEN VAN | 0402afc | 2020-04-15 18:56:59 +0900 | [diff] [blame] | 1937 | verifyNetworkTested(NETWORK_VALIDATION_RESULT_VALID, PROBES_PRIVDNS_VALID); |
lucaslin | 2ce7dcc | 2019-10-22 16:59:39 +0800 | [diff] [blame] | 1938 | verify(mCallbacks, timeout(HANDLER_TIMEOUT_MS).times(1)).notifyProbeStatusChanged( |
Remi NGUYEN VAN | 0402afc | 2020-04-15 18:56:59 +0900 | [diff] [blame] | 1939 | eq(PROBES_PRIVDNS_VALID), eq(PROBES_PRIVDNS_VALID)); |
Chiachang Wang | eb61922 | 2019-07-03 20:52:08 +0800 | [diff] [blame] | 1940 | |
| 1941 | // Verify dns query only get v4 address. |
| 1942 | resetCallbacks(); |
| 1943 | mFakeDns.setAnswer("dns4.google", new String[]{"192.0.2.1"}, TYPE_A); |
| 1944 | wnm.notifyPrivateDnsSettingsChanged(new PrivateDnsConfig("dns4.google", |
| 1945 | new InetAddress[0])); |
Remi NGUYEN VAN | 0402afc | 2020-04-15 18:56:59 +0900 | [diff] [blame] | 1946 | verifyNetworkTested(NETWORK_VALIDATION_RESULT_VALID, PROBES_PRIVDNS_VALID); |
lucaslin | 2ce7dcc | 2019-10-22 16:59:39 +0800 | [diff] [blame] | 1947 | // NetworkMonitor will check if the probes has changed or not, if the probes has not |
| 1948 | // changed, the callback won't be fired. |
| 1949 | verify(mCallbacks, never()).notifyProbeStatusChanged( |
Remi NGUYEN VAN | 0402afc | 2020-04-15 18:56:59 +0900 | [diff] [blame] | 1950 | eq(PROBES_PRIVDNS_VALID), eq(PROBES_PRIVDNS_VALID)); |
Chiachang Wang | eb61922 | 2019-07-03 20:52:08 +0800 | [diff] [blame] | 1951 | |
| 1952 | // Verify dns query get both v4 and v6 address. |
| 1953 | resetCallbacks(); |
| 1954 | mFakeDns.setAnswer("dns.google", new String[]{"2001:db8::54"}, TYPE_AAAA); |
| 1955 | mFakeDns.setAnswer("dns.google", new String[]{"192.0.2.3"}, TYPE_A); |
| 1956 | wnm.notifyPrivateDnsSettingsChanged(new PrivateDnsConfig("dns.google", new InetAddress[0])); |
Remi NGUYEN VAN | 0402afc | 2020-04-15 18:56:59 +0900 | [diff] [blame] | 1957 | verifyNetworkTested(NETWORK_VALIDATION_RESULT_VALID, PROBES_PRIVDNS_VALID); |
lucaslin | 2ce7dcc | 2019-10-22 16:59:39 +0800 | [diff] [blame] | 1958 | verify(mCallbacks, never()).notifyProbeStatusChanged( |
Remi NGUYEN VAN | 0402afc | 2020-04-15 18:56:59 +0900 | [diff] [blame] | 1959 | eq(PROBES_PRIVDNS_VALID), eq(PROBES_PRIVDNS_VALID)); |
lucaslin | 2ce7dcc | 2019-10-22 16:59:39 +0800 | [diff] [blame] | 1960 | } |
| 1961 | |
| 1962 | @Test |
| 1963 | public void testProbeStatusChanged() throws Exception { |
| 1964 | // Set no record in FakeDns and expect validation to fail. |
| 1965 | setStatus(mHttpsConnection, 204); |
| 1966 | setStatus(mHttpConnection, 204); |
| 1967 | |
Chiachang Wang | daf983b | 2020-05-07 19:20:52 +0800 | [diff] [blame] | 1968 | WrappedNetworkMonitor wnm = makeCellNotMeteredNetworkMonitor(); |
lucaslin | 2ce7dcc | 2019-10-22 16:59:39 +0800 | [diff] [blame] | 1969 | wnm.notifyPrivateDnsSettingsChanged(new PrivateDnsConfig("dns.google", new InetAddress[0])); |
Chiachang Wang | daf983b | 2020-05-07 19:20:52 +0800 | [diff] [blame] | 1970 | wnm.notifyNetworkConnected(TEST_LINK_PROPERTIES, CELL_NOT_METERED_CAPABILITIES); |
Remi NGUYEN VAN | 0402afc | 2020-04-15 18:56:59 +0900 | [diff] [blame] | 1971 | verifyNetworkTested(VALIDATION_RESULT_INVALID, |
| 1972 | NETWORK_VALIDATION_PROBE_DNS | NETWORK_VALIDATION_PROBE_HTTPS); |
| 1973 | verify(mCallbacks, timeout(HANDLER_TIMEOUT_MS)).notifyProbeStatusChanged( |
| 1974 | eq(PROBES_PRIVDNS_VALID), eq(NETWORK_VALIDATION_PROBE_DNS |
lucaslin | 2ce7dcc | 2019-10-22 16:59:39 +0800 | [diff] [blame] | 1975 | | NETWORK_VALIDATION_PROBE_HTTPS)); |
| 1976 | // Fix DNS and retry, expect validation to succeed. |
| 1977 | resetCallbacks(); |
| 1978 | mFakeDns.setAnswer("dns.google", new String[]{"2001:db8::1"}, TYPE_AAAA); |
| 1979 | |
| 1980 | wnm.forceReevaluation(Process.myUid()); |
| 1981 | // ProbeCompleted should be reset to 0 |
Chalard Jean | 23a0630 | 2020-06-26 00:41:00 +0900 | [diff] [blame] | 1982 | HandlerUtils.waitForIdle(wnm.getHandler(), HANDLER_TIMEOUT_MS); |
lucaslin | 2ce7dcc | 2019-10-22 16:59:39 +0800 | [diff] [blame] | 1983 | assertEquals(wnm.getEvaluationState().getProbeCompletedResult(), 0); |
Remi NGUYEN VAN | 0402afc | 2020-04-15 18:56:59 +0900 | [diff] [blame] | 1984 | verifyNetworkTested(NETWORK_VALIDATION_RESULT_VALID, PROBES_PRIVDNS_VALID); |
| 1985 | verify(mCallbacks, timeout(HANDLER_TIMEOUT_MS)).notifyProbeStatusChanged( |
| 1986 | eq(PROBES_PRIVDNS_VALID), eq(PROBES_PRIVDNS_VALID)); |
Lorenzo Colitti | cfc1abc | 2019-04-28 00:55:19 +0900 | [diff] [blame] | 1987 | } |
| 1988 | |
| 1989 | @Test |
| 1990 | public void testPrivateDnsResolutionRetryUpdate() throws Exception { |
Chiachang Wang | eb61922 | 2019-07-03 20:52:08 +0800 | [diff] [blame] | 1991 | // Set no record in FakeDns and expect validation to fail. |
Lorenzo Colitti | cfc1abc | 2019-04-28 00:55:19 +0900 | [diff] [blame] | 1992 | setStatus(mHttpsConnection, 204); |
| 1993 | setStatus(mHttpConnection, 204); |
| 1994 | |
Chiachang Wang | daf983b | 2020-05-07 19:20:52 +0800 | [diff] [blame] | 1995 | WrappedNetworkMonitor wnm = makeCellNotMeteredNetworkMonitor(); |
Lorenzo Colitti | cfc1abc | 2019-04-28 00:55:19 +0900 | [diff] [blame] | 1996 | wnm.notifyPrivateDnsSettingsChanged(new PrivateDnsConfig("dns.google", new InetAddress[0])); |
Chiachang Wang | daf983b | 2020-05-07 19:20:52 +0800 | [diff] [blame] | 1997 | wnm.notifyNetworkConnected(TEST_LINK_PROPERTIES, CELL_NOT_METERED_CAPABILITIES); |
Remi NGUYEN VAN | 0402afc | 2020-04-15 18:56:59 +0900 | [diff] [blame] | 1998 | verifyNetworkTested(VALIDATION_RESULT_INVALID, |
| 1999 | NETWORK_VALIDATION_PROBE_DNS | NETWORK_VALIDATION_PROBE_HTTPS); |
lucaslin | 2ce7dcc | 2019-10-22 16:59:39 +0800 | [diff] [blame] | 2000 | verify(mCallbacks, timeout(HANDLER_TIMEOUT_MS).times(1)).notifyProbeStatusChanged( |
Remi NGUYEN VAN | 0402afc | 2020-04-15 18:56:59 +0900 | [diff] [blame] | 2001 | eq(PROBES_PRIVDNS_VALID), eq(NETWORK_VALIDATION_PROBE_DNS |
lucaslin | 2ce7dcc | 2019-10-22 16:59:39 +0800 | [diff] [blame] | 2002 | | NETWORK_VALIDATION_PROBE_HTTPS)); |
Lorenzo Colitti | cfc1abc | 2019-04-28 00:55:19 +0900 | [diff] [blame] | 2003 | |
| 2004 | // Fix DNS and retry, expect validation to succeed. |
Remi NGUYEN VAN | b4af130 | 2019-06-11 16:17:46 +0900 | [diff] [blame] | 2005 | resetCallbacks(); |
Chiachang Wang | eb61922 | 2019-07-03 20:52:08 +0800 | [diff] [blame] | 2006 | mFakeDns.setAnswer("dns.google", new String[]{"2001:db8::1"}, TYPE_AAAA); |
Lorenzo Colitti | cfc1abc | 2019-04-28 00:55:19 +0900 | [diff] [blame] | 2007 | |
| 2008 | wnm.forceReevaluation(Process.myUid()); |
Chiachang Wang | 813ee47 | 2019-05-23 16:29:30 +0800 | [diff] [blame] | 2009 | verify(mCallbacks, timeout(HANDLER_TIMEOUT_MS).atLeastOnce()) |
Remi NGUYEN VAN | 0402afc | 2020-04-15 18:56:59 +0900 | [diff] [blame] | 2010 | .notifyNetworkTestedWithExtras(matchNetworkTestResultParcelable( |
| 2011 | NETWORK_VALIDATION_RESULT_VALID, PROBES_PRIVDNS_VALID)); |
lucaslin | 2ce7dcc | 2019-10-22 16:59:39 +0800 | [diff] [blame] | 2012 | verify(mCallbacks, timeout(HANDLER_TIMEOUT_MS).times(1)).notifyProbeStatusChanged( |
Remi NGUYEN VAN | 0402afc | 2020-04-15 18:56:59 +0900 | [diff] [blame] | 2013 | eq(PROBES_PRIVDNS_VALID), eq(PROBES_PRIVDNS_VALID)); |
Lorenzo Colitti | cfc1abc | 2019-04-28 00:55:19 +0900 | [diff] [blame] | 2014 | |
| 2015 | // Change configuration to an invalid DNS name, expect validation to fail. |
Remi NGUYEN VAN | b4af130 | 2019-06-11 16:17:46 +0900 | [diff] [blame] | 2016 | resetCallbacks(); |
Chiachang Wang | eb61922 | 2019-07-03 20:52:08 +0800 | [diff] [blame] | 2017 | mFakeDns.setAnswer("dns.bad", new String[0], TYPE_A); |
| 2018 | mFakeDns.setAnswer("dns.bad", new String[0], TYPE_AAAA); |
Lorenzo Colitti | cfc1abc | 2019-04-28 00:55:19 +0900 | [diff] [blame] | 2019 | wnm.notifyPrivateDnsSettingsChanged(new PrivateDnsConfig("dns.bad", new InetAddress[0])); |
Chiachang Wang | 813ee47 | 2019-05-23 16:29:30 +0800 | [diff] [blame] | 2020 | // Strict mode hostname resolve fail. Expect only notification for evaluation fail. No probe |
| 2021 | // notification. |
Remi NGUYEN VAN | 0402afc | 2020-04-15 18:56:59 +0900 | [diff] [blame] | 2022 | verifyNetworkTested(VALIDATION_RESULT_INVALID, |
| 2023 | NETWORK_VALIDATION_PROBE_DNS | NETWORK_VALIDATION_PROBE_HTTPS); |
lucaslin | 2ce7dcc | 2019-10-22 16:59:39 +0800 | [diff] [blame] | 2024 | verify(mCallbacks, timeout(HANDLER_TIMEOUT_MS).times(1)).notifyProbeStatusChanged( |
Remi NGUYEN VAN | 0402afc | 2020-04-15 18:56:59 +0900 | [diff] [blame] | 2025 | eq(PROBES_PRIVDNS_VALID), eq(NETWORK_VALIDATION_PROBE_DNS |
lucaslin | 2ce7dcc | 2019-10-22 16:59:39 +0800 | [diff] [blame] | 2026 | | NETWORK_VALIDATION_PROBE_HTTPS)); |
Lorenzo Colitti | cfc1abc | 2019-04-28 00:55:19 +0900 | [diff] [blame] | 2027 | |
| 2028 | // Change configuration back to working again, but make private DNS not work. |
| 2029 | // Expect validation to fail. |
Remi NGUYEN VAN | b4af130 | 2019-06-11 16:17:46 +0900 | [diff] [blame] | 2030 | resetCallbacks(); |
Lorenzo Colitti | cfc1abc | 2019-04-28 00:55:19 +0900 | [diff] [blame] | 2031 | mFakeDns.setNonBypassPrivateDnsWorking(false); |
Chiachang Wang | 813ee47 | 2019-05-23 16:29:30 +0800 | [diff] [blame] | 2032 | wnm.notifyPrivateDnsSettingsChanged(new PrivateDnsConfig("dns.google", |
| 2033 | new InetAddress[0])); |
Remi NGUYEN VAN | 0402afc | 2020-04-15 18:56:59 +0900 | [diff] [blame] | 2034 | verifyNetworkTested(VALIDATION_RESULT_INVALID, |
| 2035 | NETWORK_VALIDATION_PROBE_DNS | NETWORK_VALIDATION_PROBE_HTTPS); |
lucaslin | 2ce7dcc | 2019-10-22 16:59:39 +0800 | [diff] [blame] | 2036 | // NetworkMonitor will check if the probes has changed or not, if the probes has not |
| 2037 | // changed, the callback won't be fired. |
| 2038 | verify(mCallbacks, never()).notifyProbeStatusChanged( |
Remi NGUYEN VAN | 0402afc | 2020-04-15 18:56:59 +0900 | [diff] [blame] | 2039 | eq(PROBES_PRIVDNS_VALID), eq(NETWORK_VALIDATION_PROBE_DNS |
lucaslin | 2ce7dcc | 2019-10-22 16:59:39 +0800 | [diff] [blame] | 2040 | | NETWORK_VALIDATION_PROBE_HTTPS)); |
Lorenzo Colitti | cfc1abc | 2019-04-28 00:55:19 +0900 | [diff] [blame] | 2041 | |
| 2042 | // Make private DNS work again. Expect validation to succeed. |
Remi NGUYEN VAN | b4af130 | 2019-06-11 16:17:46 +0900 | [diff] [blame] | 2043 | resetCallbacks(); |
Lorenzo Colitti | cfc1abc | 2019-04-28 00:55:19 +0900 | [diff] [blame] | 2044 | mFakeDns.setNonBypassPrivateDnsWorking(true); |
| 2045 | wnm.forceReevaluation(Process.myUid()); |
Remi NGUYEN VAN | 0402afc | 2020-04-15 18:56:59 +0900 | [diff] [blame] | 2046 | verifyNetworkTested(NETWORK_VALIDATION_RESULT_VALID, PROBES_PRIVDNS_VALID); |
lucaslin | 2ce7dcc | 2019-10-22 16:59:39 +0800 | [diff] [blame] | 2047 | verify(mCallbacks, timeout(HANDLER_TIMEOUT_MS).times(1)).notifyProbeStatusChanged( |
Remi NGUYEN VAN | 0402afc | 2020-04-15 18:56:59 +0900 | [diff] [blame] | 2048 | eq(PROBES_PRIVDNS_VALID), eq(PROBES_PRIVDNS_VALID)); |
Lorenzo Colitti | cfc1abc | 2019-04-28 00:55:19 +0900 | [diff] [blame] | 2049 | } |
| 2050 | |
| 2051 | @Test |
Chiachang Wang | 9af1cb5 | 2020-05-20 15:14:46 +0800 | [diff] [blame] | 2052 | public void testDataStall_StallDnsSuspectedAndSendMetricsOnCell() throws Exception { |
| 2053 | testDataStall_StallDnsSuspectedAndSendMetrics(NetworkCapabilities.TRANSPORT_CELLULAR, |
| 2054 | CELL_METERED_CAPABILITIES); |
| 2055 | } |
| 2056 | |
| 2057 | @Test |
| 2058 | public void testDataStall_StallDnsSuspectedAndSendMetricsOnWifi() throws Exception { |
| 2059 | testDataStall_StallDnsSuspectedAndSendMetrics(NetworkCapabilities.TRANSPORT_WIFI, |
| 2060 | WIFI_NOT_METERED_CAPABILITIES); |
| 2061 | } |
| 2062 | |
| 2063 | private void testDataStall_StallDnsSuspectedAndSendMetrics(int transport, |
| 2064 | NetworkCapabilities nc) throws Exception { |
| 2065 | // NM suspects data stall from DNS signal and sends data stall metrics. |
| 2066 | final WrappedNetworkMonitor nm = prepareNetworkMonitorForVerifyDataStall(nc); |
| 2067 | makeDnsTimeoutEvent(nm, 5); |
Chiachang Wang | eae4105 | 2020-03-25 17:22:55 +0800 | [diff] [blame] | 2068 | // Trigger a dns signal to start evaluate data stall and upload metrics. |
Chiachang Wang | 9af1cb5 | 2020-05-20 15:14:46 +0800 | [diff] [blame] | 2069 | nm.notifyDnsResponse(RETURN_CODE_DNS_TIMEOUT); |
| 2070 | // Verify data sent as expected. |
| 2071 | verifySendDataStallDetectionStats(nm, DATA_STALL_EVALUATION_TYPE_DNS, transport); |
Chiachang Wang | 8b5f84a | 2019-02-22 11:13:07 +0800 | [diff] [blame] | 2072 | } |
| 2073 | |
| 2074 | @Test |
Remi NGUYEN VAN | 0402afc | 2020-04-15 18:56:59 +0900 | [diff] [blame] | 2075 | public void testDataStall_NoStallSuspectedAndSendMetrics() throws Exception { |
Chiachang Wang | 9af1cb5 | 2020-05-20 15:14:46 +0800 | [diff] [blame] | 2076 | final WrappedNetworkMonitor nm = prepareNetworkMonitorForVerifyDataStall( |
| 2077 | CELL_METERED_CAPABILITIES); |
Chiachang Wang | eae4105 | 2020-03-25 17:22:55 +0800 | [diff] [blame] | 2078 | // Setup no data stall dns signal. |
Chiachang Wang | 9af1cb5 | 2020-05-20 15:14:46 +0800 | [diff] [blame] | 2079 | makeDnsTimeoutEvent(nm, 3); |
| 2080 | assertFalse(nm.isDataStall()); |
Chiachang Wang | eae4105 | 2020-03-25 17:22:55 +0800 | [diff] [blame] | 2081 | // Trigger a dns signal to start evaluate data stall. |
Chiachang Wang | 9af1cb5 | 2020-05-20 15:14:46 +0800 | [diff] [blame] | 2082 | nm.notifyDnsResponse(RETURN_CODE_DNS_SUCCESS); |
Chiachang Wang | eae4105 | 2020-03-25 17:22:55 +0800 | [diff] [blame] | 2083 | verify(mDependencies, never()).writeDataStallDetectionStats(any(), any()); |
Chiachang Wang | 8b5f84a | 2019-02-22 11:13:07 +0800 | [diff] [blame] | 2084 | } |
Chiachang Wang | caa3520 | 2019-02-26 11:32:18 +0800 | [diff] [blame] | 2085 | |
| 2086 | @Test |
Chiachang Wang | 9af1cb5 | 2020-05-20 15:14:46 +0800 | [diff] [blame] | 2087 | public void testDataStall_StallTcpSuspectedAndSendMetricsOnCell() throws Exception { |
Chiachang Wang | 2e099cc | 2020-05-21 16:46:05 +0800 | [diff] [blame] | 2088 | testDataStall_StallTcpSuspectedAndSendMetrics(CELL_METERED_CAPABILITIES); |
Chiachang Wang | 9af1cb5 | 2020-05-20 15:14:46 +0800 | [diff] [blame] | 2089 | } |
Chiachang Wang | caa3520 | 2019-02-26 11:32:18 +0800 | [diff] [blame] | 2090 | |
Chiachang Wang | 9af1cb5 | 2020-05-20 15:14:46 +0800 | [diff] [blame] | 2091 | @Test |
| 2092 | public void testDataStall_StallTcpSuspectedAndSendMetricsOnWifi() throws Exception { |
Chiachang Wang | 2e099cc | 2020-05-21 16:46:05 +0800 | [diff] [blame] | 2093 | testDataStall_StallTcpSuspectedAndSendMetrics(WIFI_NOT_METERED_CAPABILITIES); |
Chiachang Wang | 9af1cb5 | 2020-05-20 15:14:46 +0800 | [diff] [blame] | 2094 | } |
Chiachang Wang | caa3520 | 2019-02-26 11:32:18 +0800 | [diff] [blame] | 2095 | |
Chiachang Wang | 2e099cc | 2020-05-21 16:46:05 +0800 | [diff] [blame] | 2096 | private void testDataStall_StallTcpSuspectedAndSendMetrics(NetworkCapabilities nc) |
| 2097 | throws Exception { |
Chiachang Wang | 9af1cb5 | 2020-05-20 15:14:46 +0800 | [diff] [blame] | 2098 | assumeTrue(ShimUtils.isReleaseOrDevelopmentApiAbove(Build.VERSION_CODES.Q)); |
Chiachang Wang | 57218af | 2020-05-28 15:55:33 +0800 | [diff] [blame] | 2099 | setupTcpDataStall(); |
Chiachang Wang | 0cbaa8d | 2020-07-21 09:55:58 +0800 | [diff] [blame] | 2100 | setTcpPollingInterval(0); |
Chiachang Wang | 9af1cb5 | 2020-05-20 15:14:46 +0800 | [diff] [blame] | 2101 | // NM suspects data stall from TCP signal and sends data stall metrics. |
| 2102 | setDataStallEvaluationType(DATA_STALL_EVALUATION_TYPE_TCP); |
| 2103 | final WrappedNetworkMonitor nm = prepareNetworkMonitorForVerifyDataStall(nc); |
Chiachang Wang | 9af1cb5 | 2020-05-20 15:14:46 +0800 | [diff] [blame] | 2104 | // Trigger a tcp event immediately. |
Chiachang Wang | 9af1cb5 | 2020-05-20 15:14:46 +0800 | [diff] [blame] | 2105 | nm.sendTcpPollingEvent(); |
Chiachang Wang | 2e099cc | 2020-05-21 16:46:05 +0800 | [diff] [blame] | 2106 | // Allow only one transport type in the context of this test for simplification. |
| 2107 | final int[] transports = nc.getTransportTypes(); |
| 2108 | assertEquals(1, transports.length); |
| 2109 | verifySendDataStallDetectionStats(nm, DATA_STALL_EVALUATION_TYPE_TCP, transports[0]); |
Chiachang Wang | 9af1cb5 | 2020-05-20 15:14:46 +0800 | [diff] [blame] | 2110 | } |
| 2111 | |
| 2112 | private WrappedNetworkMonitor prepareNetworkMonitorForVerifyDataStall(NetworkCapabilities nc) |
| 2113 | throws Exception { |
| 2114 | // Connect a VALID network to simulate the data stall detection because data stall |
| 2115 | // evaluation will only start from validated state. |
| 2116 | setStatus(mHttpsConnection, 204); |
| 2117 | final WrappedNetworkMonitor nm; |
Chiachang Wang | 2e099cc | 2020-05-21 16:46:05 +0800 | [diff] [blame] | 2118 | // Allow only one transport type in the context of this test for simplification. |
| 2119 | if (nc.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)) { |
| 2120 | nm = makeCellMeteredNetworkMonitor(); |
| 2121 | } else if (nc.hasTransport(NetworkCapabilities.TRANSPORT_WIFI)) { |
| 2122 | nm = makeWifiNotMeteredNetworkMonitor(); |
| 2123 | setupTestWifiInfo(); |
| 2124 | } else { |
| 2125 | nm = null; |
| 2126 | fail("Undefined transport type"); |
Chiachang Wang | 9af1cb5 | 2020-05-20 15:14:46 +0800 | [diff] [blame] | 2127 | } |
| 2128 | nm.notifyNetworkConnected(TEST_LINK_PROPERTIES, nc); |
| 2129 | verifyNetworkTested(NETWORK_VALIDATION_RESULT_VALID, |
| 2130 | NETWORK_VALIDATION_PROBE_DNS | NETWORK_VALIDATION_PROBE_HTTPS); |
Chiachang Wang | 4fcbe91 | 2021-04-26 13:02:22 +0800 | [diff] [blame] | 2131 | nm.setLastProbeTime(SystemClock.elapsedRealtime() - STALL_EXPECTED_LAST_PROBE_TIME_MS); |
Chiachang Wang | 9af1cb5 | 2020-05-20 15:14:46 +0800 | [diff] [blame] | 2132 | return nm; |
| 2133 | } |
| 2134 | |
| 2135 | private void setupTcpDataStall() { |
| 2136 | when(mTstDependencies.isTcpInfoParsingSupported()).thenReturn(true); |
| 2137 | when(mTst.getLatestReceivedCount()).thenReturn(0); |
| 2138 | when(mTst.getLatestPacketFailPercentage()).thenReturn(TEST_TCP_FAIL_RATE); |
| 2139 | when(mTst.getSentSinceLastRecv()).thenReturn(TEST_TCP_PACKET_COUNT); |
| 2140 | when(mTst.isDataStallSuspected()).thenReturn(true); |
| 2141 | when(mTst.pollSocketsInfo()).thenReturn(true); |
| 2142 | } |
| 2143 | |
| 2144 | private void verifySendDataStallDetectionStats(WrappedNetworkMonitor nm, int evalType, |
| 2145 | int transport) { |
| 2146 | // Verify data sent as expectated. |
| 2147 | final ArgumentCaptor<CaptivePortalProbeResult> probeResultCaptor = |
| 2148 | ArgumentCaptor.forClass(CaptivePortalProbeResult.class); |
| 2149 | final ArgumentCaptor<DataStallDetectionStats> statsCaptor = |
| 2150 | ArgumentCaptor.forClass(DataStallDetectionStats.class); |
| 2151 | verify(mDependencies, timeout(HANDLER_TIMEOUT_MS).times(1)) |
| 2152 | .writeDataStallDetectionStats(statsCaptor.capture(), probeResultCaptor.capture()); |
Chiachang Wang | e752407 | 2021-04-26 08:36:00 +0800 | [diff] [blame] | 2153 | // Ensure probe will not stop due to rate-limiting mechanism. |
Chiachang Wang | 4fcbe91 | 2021-04-26 13:02:22 +0800 | [diff] [blame] | 2154 | nm.setLastProbeTime(SystemClock.elapsedRealtime() - STALL_EXPECTED_LAST_PROBE_TIME_MS); |
Chiachang Wang | 9af1cb5 | 2020-05-20 15:14:46 +0800 | [diff] [blame] | 2155 | assertTrue(nm.isDataStall()); |
| 2156 | assertTrue(probeResultCaptor.getValue().isSuccessful()); |
| 2157 | verifyTestDataStallDetectionStats(evalType, transport, statsCaptor.getValue()); |
| 2158 | } |
| 2159 | |
| 2160 | private void verifyTestDataStallDetectionStats(int evalType, int transport, |
| 2161 | DataStallDetectionStats stats) { |
| 2162 | assertEquals(transport, stats.mNetworkType); |
| 2163 | switch (transport) { |
| 2164 | case NetworkCapabilities.TRANSPORT_WIFI: |
| 2165 | assertArrayEquals(makeTestWifiDataNano(), stats.mWifiInfo); |
| 2166 | // Expedient way to check stats.mCellularInfo contains the neutral byte array that |
| 2167 | // is sent to represent a lack of data, as stats.mCellularInfo is not supposed to |
| 2168 | // contain null. |
| 2169 | assertArrayEquals(DataStallDetectionStats.emptyCellDataIfNull(null), |
| 2170 | stats.mCellularInfo); |
| 2171 | break; |
| 2172 | case NetworkCapabilities.TRANSPORT_CELLULAR: |
| 2173 | // Expedient way to check stats.mWifiInfo contains the neutral byte array that is |
| 2174 | // sent to represent a lack of data, as stats.mWifiInfo is not supposed to contain |
| 2175 | // null. |
| 2176 | assertArrayEquals(DataStallDetectionStats.emptyWifiInfoIfNull(null), |
| 2177 | stats.mWifiInfo); |
| 2178 | assertArrayEquals(makeTestCellDataNano(), stats.mCellularInfo); |
| 2179 | break; |
| 2180 | default: |
| 2181 | // Add other cases. |
| 2182 | fail("Unexpected transport type"); |
| 2183 | } |
| 2184 | |
| 2185 | assertEquals(evalType, stats.mEvaluationType); |
| 2186 | if ((evalType & DATA_STALL_EVALUATION_TYPE_TCP) != 0) { |
| 2187 | assertEquals(TEST_TCP_FAIL_RATE, stats.mTcpFailRate); |
| 2188 | assertEquals(TEST_TCP_PACKET_COUNT, stats.mTcpSentSinceLastRecv); |
| 2189 | } else { |
| 2190 | assertEquals(DataStallDetectionStats.UNSPECIFIED_TCP_FAIL_RATE, stats.mTcpFailRate); |
| 2191 | assertEquals(DataStallDetectionStats.UNSPECIFIED_TCP_PACKETS_COUNT, |
| 2192 | stats.mTcpSentSinceLastRecv); |
| 2193 | } |
| 2194 | |
| 2195 | if ((evalType & DATA_STALL_EVALUATION_TYPE_DNS) != 0) { |
| 2196 | assertArrayEquals(stats.mDns, makeTestDnsTimeoutNano(DEFAULT_DNS_TIMEOUT_THRESHOLD)); |
| 2197 | } else { |
| 2198 | assertArrayEquals(stats.mDns, makeTestDnsTimeoutNano(0 /* times */)); |
| 2199 | } |
| 2200 | } |
| 2201 | |
| 2202 | private DataStallDetectionStats makeTestDataStallDetectionStats(int evaluationType, |
| 2203 | int transportType) { |
| 2204 | final DataStallDetectionStats.Builder stats = new DataStallDetectionStats.Builder() |
| 2205 | .setEvaluationType(evaluationType) |
| 2206 | .setNetworkType(transportType); |
| 2207 | switch (transportType) { |
| 2208 | case NetworkCapabilities.TRANSPORT_CELLULAR: |
| 2209 | stats.setCellData(TelephonyManager.NETWORK_TYPE_LTE /* radioType */, |
Chiachang Wang | caa3520 | 2019-02-26 11:32:18 +0800 | [diff] [blame] | 2210 | true /* roaming */, |
| 2211 | TEST_MCCMNC /* networkMccmnc */, |
| 2212 | TEST_MCCMNC /* simMccmnc */, |
| 2213 | CellSignalStrength.SIGNAL_STRENGTH_NONE_OR_UNKNOWN /* signalStrength */); |
Chiachang Wang | 9af1cb5 | 2020-05-20 15:14:46 +0800 | [diff] [blame] | 2214 | break; |
| 2215 | case NetworkCapabilities.TRANSPORT_WIFI: |
| 2216 | setupTestWifiInfo(); |
| 2217 | stats.setWiFiData(mWifiInfo); |
| 2218 | break; |
| 2219 | default: |
| 2220 | break; |
| 2221 | } |
Chiachang Wang | caa3520 | 2019-02-26 11:32:18 +0800 | [diff] [blame] | 2222 | |
Chiachang Wang | 9af1cb5 | 2020-05-20 15:14:46 +0800 | [diff] [blame] | 2223 | if ((evaluationType & DATA_STALL_EVALUATION_TYPE_TCP) != 0) { |
| 2224 | generateTestTcpStats(stats); |
| 2225 | } |
Chiachang Wang | caa3520 | 2019-02-26 11:32:18 +0800 | [diff] [blame] | 2226 | |
Chiachang Wang | 9af1cb5 | 2020-05-20 15:14:46 +0800 | [diff] [blame] | 2227 | if ((evaluationType & DATA_STALL_EVALUATION_TYPE_DNS) != 0) { |
| 2228 | generateTimeoutDnsEvent(stats, DEFAULT_DNS_TIMEOUT_THRESHOLD); |
| 2229 | } |
| 2230 | |
| 2231 | return stats.build(); |
| 2232 | } |
| 2233 | |
| 2234 | private byte[] makeTestDnsTimeoutNano(int timeoutCount) { |
| 2235 | // Make a expected nano dns message. |
| 2236 | final DnsEvent event = new DnsEvent(); |
| 2237 | event.dnsReturnCode = new int[timeoutCount]; |
| 2238 | event.dnsTime = new long[timeoutCount]; |
| 2239 | Arrays.fill(event.dnsReturnCode, RETURN_CODE_DNS_TIMEOUT); |
| 2240 | Arrays.fill(event.dnsTime, TEST_ELAPSED_TIME_MS); |
| 2241 | return MessageNano.toByteArray(event); |
| 2242 | } |
| 2243 | |
| 2244 | private byte[] makeTestCellDataNano() { |
| 2245 | final CellularData data = new CellularData(); |
| 2246 | data.ratType = DataStallEventProto.RADIO_TECHNOLOGY_LTE; |
| 2247 | data.networkMccmnc = TEST_MCCMNC; |
| 2248 | data.simMccmnc = TEST_MCCMNC; |
| 2249 | data.isRoaming = true; |
| 2250 | data.signalStrength = 0; |
| 2251 | return MessageNano.toByteArray(data); |
| 2252 | } |
| 2253 | |
| 2254 | private byte[] makeTestWifiDataNano() { |
| 2255 | final WifiData data = new WifiData(); |
| 2256 | data.wifiBand = DataStallEventProto.AP_BAND_2GHZ; |
| 2257 | data.signalStrength = TEST_SIGNAL_STRENGTH; |
| 2258 | return MessageNano.toByteArray(data); |
| 2259 | } |
| 2260 | |
| 2261 | private void setupTestWifiInfo() { |
Chiachang Wang | caa3520 | 2019-02-26 11:32:18 +0800 | [diff] [blame] | 2262 | when(mWifi.getConnectionInfo()).thenReturn(mWifiInfo); |
Chiachang Wang | 9af1cb5 | 2020-05-20 15:14:46 +0800 | [diff] [blame] | 2263 | when(mWifiInfo.getRssi()).thenReturn(TEST_SIGNAL_STRENGTH); |
| 2264 | // Set to 2.4G band. Map to DataStallEventProto.AP_BAND_2GHZ proto definition. |
| 2265 | when(mWifiInfo.getFrequency()).thenReturn(2450); |
| 2266 | } |
Chiachang Wang | caa3520 | 2019-02-26 11:32:18 +0800 | [diff] [blame] | 2267 | |
Chiachang Wang | 9af1cb5 | 2020-05-20 15:14:46 +0800 | [diff] [blame] | 2268 | private void testDataStallMetricsWithCellular(int evalType) { |
| 2269 | testDataStallMetrics(evalType, NetworkCapabilities.TRANSPORT_CELLULAR); |
| 2270 | } |
Chiachang Wang | caa3520 | 2019-02-26 11:32:18 +0800 | [diff] [blame] | 2271 | |
Chiachang Wang | 9af1cb5 | 2020-05-20 15:14:46 +0800 | [diff] [blame] | 2272 | private void testDataStallMetricsWithWiFi(int evalType) { |
| 2273 | testDataStallMetrics(evalType, NetworkCapabilities.TRANSPORT_WIFI); |
| 2274 | } |
| 2275 | |
| 2276 | private void testDataStallMetrics(int evalType, int transportType) { |
| 2277 | setDataStallEvaluationType(evalType); |
| 2278 | final NetworkCapabilities nc = new NetworkCapabilities() |
| 2279 | .addTransportType(transportType) |
| 2280 | .addCapability(NET_CAPABILITY_INTERNET); |
| 2281 | final WrappedNetworkMonitor wrappedMonitor = makeMonitor(nc); |
| 2282 | setupTestWifiInfo(); |
| 2283 | final DataStallDetectionStats stats = |
| 2284 | makeTestDataStallDetectionStats(evalType, transportType); |
| 2285 | assertEquals(wrappedMonitor.buildDataStallDetectionStats(transportType, evalType), stats); |
| 2286 | |
| 2287 | if ((evalType & DATA_STALL_EVALUATION_TYPE_TCP) != 0) { |
| 2288 | verify(mTst, timeout(HANDLER_TIMEOUT_MS).atLeastOnce()).getLatestPacketFailPercentage(); |
| 2289 | } else { |
| 2290 | verify(mTst, never()).getLatestPacketFailPercentage(); |
| 2291 | } |
| 2292 | } |
| 2293 | |
| 2294 | @Test |
| 2295 | public void testCollectDataStallMetrics_DnsWithCellular() { |
| 2296 | testDataStallMetricsWithCellular(DATA_STALL_EVALUATION_TYPE_DNS); |
| 2297 | } |
| 2298 | |
| 2299 | @Test |
| 2300 | public void testCollectDataStallMetrics_DnsWithWiFi() { |
| 2301 | testDataStallMetricsWithWiFi(DATA_STALL_EVALUATION_TYPE_DNS); |
| 2302 | } |
| 2303 | |
| 2304 | @Test |
| 2305 | public void testCollectDataStallMetrics_TcpWithCellular() { |
| 2306 | assumeTrue(ShimUtils.isReleaseOrDevelopmentApiAbove(Build.VERSION_CODES.Q)); |
| 2307 | testDataStallMetricsWithCellular(DATA_STALL_EVALUATION_TYPE_TCP); |
| 2308 | } |
| 2309 | |
| 2310 | @Test |
| 2311 | public void testCollectDataStallMetrics_TcpWithWiFi() { |
| 2312 | assumeTrue(ShimUtils.isReleaseOrDevelopmentApiAbove(Build.VERSION_CODES.Q)); |
| 2313 | testDataStallMetricsWithWiFi(DATA_STALL_EVALUATION_TYPE_TCP); |
| 2314 | } |
| 2315 | |
| 2316 | @Test |
| 2317 | public void testCollectDataStallMetrics_TcpAndDnsWithWifi() { |
| 2318 | assumeTrue(ShimUtils.isReleaseOrDevelopmentApiAbove(Build.VERSION_CODES.Q)); |
| 2319 | testDataStallMetricsWithWiFi( |
| 2320 | DATA_STALL_EVALUATION_TYPE_TCP | DATA_STALL_EVALUATION_TYPE_DNS); |
| 2321 | } |
| 2322 | |
| 2323 | @Test |
| 2324 | public void testCollectDataStallMetrics_TcpAndDnsWithCellular() { |
| 2325 | assumeTrue(ShimUtils.isReleaseOrDevelopmentApiAbove(Build.VERSION_CODES.Q)); |
| 2326 | testDataStallMetricsWithCellular( |
| 2327 | DATA_STALL_EVALUATION_TYPE_TCP | DATA_STALL_EVALUATION_TYPE_DNS); |
Chiachang Wang | caa3520 | 2019-02-26 11:32:18 +0800 | [diff] [blame] | 2328 | } |
| 2329 | |
lucaslin | b057396 | 2019-03-12 13:08:03 +0800 | [diff] [blame] | 2330 | @Test |
| 2331 | public void testIgnoreHttpsProbe() throws Exception { |
| 2332 | setSslException(mHttpsConnection); |
| 2333 | setStatus(mHttpConnection, 204); |
Chiachang Wang | 813ee47 | 2019-05-23 16:29:30 +0800 | [diff] [blame] | 2334 | // Expect to send HTTP, HTTPS, FALLBACK probe and evaluation result notifications to CS. |
Remi NGUYEN VAN | 0402afc | 2020-04-15 18:56:59 +0900 | [diff] [blame] | 2335 | final NetworkMonitor nm = runNetworkTest(NETWORK_VALIDATION_RESULT_PARTIAL, |
| 2336 | NETWORK_VALIDATION_PROBE_DNS | NETWORK_VALIDATION_PROBE_HTTP, |
| 2337 | null /* redirectUrl */); |
lucaslin | b057396 | 2019-03-12 13:08:03 +0800 | [diff] [blame] | 2338 | |
Remi NGUYEN VAN | b4af130 | 2019-06-11 16:17:46 +0900 | [diff] [blame] | 2339 | resetCallbacks(); |
lucaslin | d01ea62 | 2019-03-20 18:21:59 +0800 | [diff] [blame] | 2340 | nm.setAcceptPartialConnectivity(); |
Chiachang Wang | 813ee47 | 2019-05-23 16:29:30 +0800 | [diff] [blame] | 2341 | // Expect to update evaluation result notifications to CS. |
Remi NGUYEN VAN | 0402afc | 2020-04-15 18:56:59 +0900 | [diff] [blame] | 2342 | verifyNetworkTested(NETWORK_VALIDATION_RESULT_PARTIAL | NETWORK_VALIDATION_RESULT_VALID, |
| 2343 | NETWORK_VALIDATION_PROBE_DNS | NETWORK_VALIDATION_PROBE_HTTP); |
lucaslin | b057396 | 2019-03-12 13:08:03 +0800 | [diff] [blame] | 2344 | } |
| 2345 | |
| 2346 | @Test |
Remi NGUYEN VAN | 0402afc | 2020-04-15 18:56:59 +0900 | [diff] [blame] | 2347 | public void testIsPartialConnectivity() throws Exception { |
lucaslin | b057396 | 2019-03-12 13:08:03 +0800 | [diff] [blame] | 2348 | setStatus(mHttpsConnection, 500); |
| 2349 | setStatus(mHttpConnection, 204); |
| 2350 | setStatus(mFallbackConnection, 500); |
Remi NGUYEN VAN | 0402afc | 2020-04-15 18:56:59 +0900 | [diff] [blame] | 2351 | runPartialConnectivityNetworkTest( |
| 2352 | NETWORK_VALIDATION_PROBE_DNS | NETWORK_VALIDATION_PROBE_HTTP); |
Chiachang Wang | c9a89aa | 2020-11-13 17:04:35 +0800 | [diff] [blame] | 2353 | } |
lucaslin | b057396 | 2019-03-12 13:08:03 +0800 | [diff] [blame] | 2354 | |
Chiachang Wang | c9a89aa | 2020-11-13 17:04:35 +0800 | [diff] [blame] | 2355 | @Test |
| 2356 | public void testIsCaptivePortal_OnlyFallbackSucceed() throws Exception { |
lucaslin | b057396 | 2019-03-12 13:08:03 +0800 | [diff] [blame] | 2357 | setStatus(mHttpsConnection, 500); |
| 2358 | setStatus(mHttpConnection, 500); |
| 2359 | setStatus(mFallbackConnection, 204); |
Chiachang Wang | c9a89aa | 2020-11-13 17:04:35 +0800 | [diff] [blame] | 2360 | runNetworkTest(VALIDATION_RESULT_INVALID, |
| 2361 | NETWORK_VALIDATION_PROBE_DNS | NETWORK_VALIDATION_PROBE_FALLBACK, |
| 2362 | null /* redirectUrl */); |
lucaslin | b057396 | 2019-03-12 13:08:03 +0800 | [diff] [blame] | 2363 | } |
| 2364 | |
Lorenzo Colitti | 171cfd2 | 2019-04-18 13:44:32 +0900 | [diff] [blame] | 2365 | private void assertIpAddressArrayEquals(String[] expected, InetAddress[] actual) { |
| 2366 | String[] actualStrings = new String[actual.length]; |
| 2367 | for (int i = 0; i < actual.length; i++) { |
| 2368 | actualStrings[i] = actual[i].getHostAddress(); |
| 2369 | } |
| 2370 | assertArrayEquals("Array of IP addresses differs", expected, actualStrings); |
| 2371 | } |
| 2372 | |
| 2373 | @Test |
| 2374 | public void testSendDnsProbeWithTimeout() throws Exception { |
Chiachang Wang | daf983b | 2020-05-07 19:20:52 +0800 | [diff] [blame] | 2375 | WrappedNetworkMonitor wnm = makeCellNotMeteredNetworkMonitor(); |
Lorenzo Colitti | 171cfd2 | 2019-04-18 13:44:32 +0900 | [diff] [blame] | 2376 | final int shortTimeoutMs = 200; |
Chiachang Wang | eb61922 | 2019-07-03 20:52:08 +0800 | [diff] [blame] | 2377 | // v6 only. |
Lorenzo Colitti | 171cfd2 | 2019-04-18 13:44:32 +0900 | [diff] [blame] | 2378 | String[] expected = new String[]{"2001:db8::"}; |
Chiachang Wang | eb61922 | 2019-07-03 20:52:08 +0800 | [diff] [blame] | 2379 | mFakeDns.setAnswer("www.google.com", expected, TYPE_AAAA); |
Lorenzo Colitti | 171cfd2 | 2019-04-18 13:44:32 +0900 | [diff] [blame] | 2380 | InetAddress[] actual = wnm.sendDnsProbeWithTimeout("www.google.com", shortTimeoutMs); |
| 2381 | assertIpAddressArrayEquals(expected, actual); |
Chiachang Wang | eb61922 | 2019-07-03 20:52:08 +0800 | [diff] [blame] | 2382 | // v4 only. |
| 2383 | expected = new String[]{"192.0.2.1"}; |
| 2384 | mFakeDns.setAnswer("www.android.com", expected, TYPE_A); |
| 2385 | actual = wnm.sendDnsProbeWithTimeout("www.android.com", shortTimeoutMs); |
| 2386 | assertIpAddressArrayEquals(expected, actual); |
| 2387 | // Both v4 & v6. |
| 2388 | expected = new String[]{"192.0.2.1", "2001:db8::"}; |
| 2389 | mFakeDns.setAnswer("www.googleapis.com", new String[]{"192.0.2.1"}, TYPE_A); |
| 2390 | mFakeDns.setAnswer("www.googleapis.com", new String[]{"2001:db8::"}, TYPE_AAAA); |
Lorenzo Colitti | cfc1abc | 2019-04-28 00:55:19 +0900 | [diff] [blame] | 2391 | actual = wnm.sendDnsProbeWithTimeout("www.googleapis.com", shortTimeoutMs); |
Lorenzo Colitti | 171cfd2 | 2019-04-18 13:44:32 +0900 | [diff] [blame] | 2392 | assertIpAddressArrayEquals(expected, actual); |
Chiachang Wang | eb61922 | 2019-07-03 20:52:08 +0800 | [diff] [blame] | 2393 | // Clear DNS response. |
| 2394 | mFakeDns.setAnswer("www.android.com", new String[0], TYPE_A); |
Lorenzo Colitti | 171cfd2 | 2019-04-18 13:44:32 +0900 | [diff] [blame] | 2395 | try { |
Chiachang Wang | eb61922 | 2019-07-03 20:52:08 +0800 | [diff] [blame] | 2396 | actual = wnm.sendDnsProbeWithTimeout("www.android.com", shortTimeoutMs); |
Lorenzo Colitti | 171cfd2 | 2019-04-18 13:44:32 +0900 | [diff] [blame] | 2397 | fail("No DNS results, expected UnknownHostException"); |
| 2398 | } catch (UnknownHostException e) { |
| 2399 | } |
| 2400 | |
Chiachang Wang | eb61922 | 2019-07-03 20:52:08 +0800 | [diff] [blame] | 2401 | mFakeDns.setAnswer("www.android.com", null, TYPE_A); |
| 2402 | mFakeDns.setAnswer("www.android.com", null, TYPE_AAAA); |
Lorenzo Colitti | 171cfd2 | 2019-04-18 13:44:32 +0900 | [diff] [blame] | 2403 | try { |
Chiachang Wang | eb61922 | 2019-07-03 20:52:08 +0800 | [diff] [blame] | 2404 | wnm.sendDnsProbeWithTimeout("www.android.com", shortTimeoutMs); |
Lorenzo Colitti | 171cfd2 | 2019-04-18 13:44:32 +0900 | [diff] [blame] | 2405 | fail("DNS query timed out, expected UnknownHostException"); |
| 2406 | } catch (UnknownHostException e) { |
| 2407 | } |
| 2408 | } |
| 2409 | |
Chiachang Wang | 813ee47 | 2019-05-23 16:29:30 +0800 | [diff] [blame] | 2410 | @Test |
| 2411 | public void testNotifyNetwork_WithforceReevaluation() throws Exception { |
Remi NGUYEN VAN | 0402afc | 2020-04-15 18:56:59 +0900 | [diff] [blame] | 2412 | setValidProbes(); |
Chiachang Wang | 813ee47 | 2019-05-23 16:29:30 +0800 | [diff] [blame] | 2413 | final NetworkMonitor nm = runValidatedNetworkTest(); |
Remi NGUYEN VAN | 0402afc | 2020-04-15 18:56:59 +0900 | [diff] [blame] | 2414 | // Verify forceReevaluation will not reset the validation result but only probe result until |
Chiachang Wang | 813ee47 | 2019-05-23 16:29:30 +0800 | [diff] [blame] | 2415 | // getting the validation result. |
Remi NGUYEN VAN | b4af130 | 2019-06-11 16:17:46 +0900 | [diff] [blame] | 2416 | resetCallbacks(); |
Chiachang Wang | 813ee47 | 2019-05-23 16:29:30 +0800 | [diff] [blame] | 2417 | setSslException(mHttpsConnection); |
| 2418 | setStatus(mHttpConnection, 500); |
| 2419 | setStatus(mFallbackConnection, 204); |
| 2420 | nm.forceReevaluation(Process.myUid()); |
Chiachang Wang | 813ee47 | 2019-05-23 16:29:30 +0800 | [diff] [blame] | 2421 | // Expect to send HTTP, HTTPs, FALLBACK and evaluation results. |
Chiachang Wang | a1a987f | 2020-12-30 11:49:32 +0800 | [diff] [blame] | 2422 | verifyNetworkTested(VALIDATION_RESULT_INVALID, |
Chiachang Wang | c9a89aa | 2020-11-13 17:04:35 +0800 | [diff] [blame] | 2423 | NETWORK_VALIDATION_PROBE_DNS | NETWORK_VALIDATION_PROBE_FALLBACK, |
| 2424 | null /* redirectUrl */); |
Chiachang Wang | a1a987f | 2020-12-30 11:49:32 +0800 | [diff] [blame] | 2425 | HandlerUtils.waitForIdle(nm.getHandler(), HANDLER_TIMEOUT_MS); |
Cody Kesting | 176bce7 | 2020-01-20 18:09:59 -0800 | [diff] [blame] | 2426 | } |
| 2427 | |
| 2428 | @Test |
| 2429 | public void testNotifyNetwork_NotifyNetworkTestedOldInterfaceVersion() throws Exception { |
| 2430 | // Use old interface version so notifyNetworkTested is used over |
| 2431 | // notifyNetworkTestedWithExtras |
Cody Kesting | 2b26c8b | 2020-02-10 09:48:56 -0800 | [diff] [blame] | 2432 | resetCallbacks(4); |
Cody Kesting | 176bce7 | 2020-01-20 18:09:59 -0800 | [diff] [blame] | 2433 | |
| 2434 | // Trigger Network validation |
| 2435 | setStatus(mHttpsConnection, 204); |
| 2436 | setStatus(mHttpConnection, 204); |
Chiachang Wang | daf983b | 2020-05-07 19:20:52 +0800 | [diff] [blame] | 2437 | final NetworkMonitor nm = makeMonitor(CELL_METERED_CAPABILITIES); |
| 2438 | nm.notifyNetworkConnected(TEST_LINK_PROPERTIES, CELL_METERED_CAPABILITIES); |
Cody Kesting | 176bce7 | 2020-01-20 18:09:59 -0800 | [diff] [blame] | 2439 | |
| 2440 | verify(mCallbacks, timeout(HANDLER_TIMEOUT_MS)) |
Remi NGUYEN VAN | 0402afc | 2020-04-15 18:56:59 +0900 | [diff] [blame] | 2441 | .notifyNetworkTested(eq(NETWORK_VALIDATION_RESULT_VALID |
| 2442 | | NETWORK_VALIDATION_PROBE_DNS | NETWORK_VALIDATION_PROBE_HTTPS), |
| 2443 | eq(null) /* redirectUrl */); |
Chiachang Wang | 813ee47 | 2019-05-23 16:29:30 +0800 | [diff] [blame] | 2444 | } |
| 2445 | |
| 2446 | @Test |
Chiachang Wang | 26a626a | 2020-03-02 17:41:58 +0800 | [diff] [blame] | 2447 | public void testDismissPortalInValidatedNetworkEnabledOsSupported() throws Exception { |
Lorenzo Colitti | 0d08b44 | 2020-04-27 11:49:39 +0900 | [diff] [blame] | 2448 | assumeTrue(ShimUtils.isAtLeastR()); |
Chiachang Wang | 220428b | 2020-05-29 10:03:41 +0800 | [diff] [blame] | 2449 | testDismissPortalInValidatedNetworkEnabled(TEST_LOGIN_URL, TEST_LOGIN_URL); |
| 2450 | } |
| 2451 | |
| 2452 | @Test |
| 2453 | public void testDismissPortalInValidatedNetworkEnabledOsSupported_NullLocationUrl() |
| 2454 | throws Exception { |
| 2455 | assumeTrue(ShimUtils.isAtLeastR()); |
| 2456 | testDismissPortalInValidatedNetworkEnabled(TEST_HTTP_URL, null /* locationUrl */); |
| 2457 | } |
| 2458 | |
| 2459 | @Test |
| 2460 | public void testDismissPortalInValidatedNetworkEnabledOsSupported_InvalidLocationUrl() |
| 2461 | throws Exception { |
| 2462 | assumeTrue(ShimUtils.isAtLeastR()); |
| 2463 | testDismissPortalInValidatedNetworkEnabled(TEST_HTTP_URL, TEST_RELATIVE_URL); |
Chiachang Wang | 26a626a | 2020-03-02 17:41:58 +0800 | [diff] [blame] | 2464 | } |
| 2465 | |
| 2466 | @Test |
| 2467 | public void testDismissPortalInValidatedNetworkEnabledOsNotSupported() throws Exception { |
Lorenzo Colitti | 0d08b44 | 2020-04-27 11:49:39 +0900 | [diff] [blame] | 2468 | assumeFalse(ShimUtils.isAtLeastR()); |
Chiachang Wang | 220428b | 2020-05-29 10:03:41 +0800 | [diff] [blame] | 2469 | testDismissPortalInValidatedNetworkEnabled(TEST_HTTP_URL, TEST_LOGIN_URL); |
Chiachang Wang | 26a626a | 2020-03-02 17:41:58 +0800 | [diff] [blame] | 2470 | } |
| 2471 | |
Chiachang Wang | 220428b | 2020-05-29 10:03:41 +0800 | [diff] [blame] | 2472 | private void testDismissPortalInValidatedNetworkEnabled(String expectedUrl, String locationUrl) |
| 2473 | throws Exception { |
Chiachang Wang | 26a626a | 2020-03-02 17:41:58 +0800 | [diff] [blame] | 2474 | setDismissPortalInValidatedNetwork(true); |
| 2475 | setSslException(mHttpsConnection); |
| 2476 | setPortal302(mHttpConnection); |
Chiachang Wang | 220428b | 2020-05-29 10:03:41 +0800 | [diff] [blame] | 2477 | when(mHttpConnection.getHeaderField(eq("location"))).thenReturn(locationUrl); |
Chiachang Wang | daf983b | 2020-05-07 19:20:52 +0800 | [diff] [blame] | 2478 | final NetworkMonitor nm = makeMonitor(CELL_METERED_CAPABILITIES); |
| 2479 | notifyNetworkConnected(nm, CELL_METERED_CAPABILITIES); |
Chiachang Wang | 26a626a | 2020-03-02 17:41:58 +0800 | [diff] [blame] | 2480 | |
| 2481 | verify(mCallbacks, timeout(HANDLER_TIMEOUT_MS).times(1)) |
| 2482 | .showProvisioningNotification(any(), any()); |
| 2483 | |
| 2484 | assertEquals(1, mRegisteredReceivers.size()); |
| 2485 | // Check that startCaptivePortalApp sends the expected intent. |
| 2486 | nm.launchCaptivePortalApp(); |
| 2487 | |
| 2488 | final ArgumentCaptor<Bundle> bundleCaptor = ArgumentCaptor.forClass(Bundle.class); |
| 2489 | final ArgumentCaptor<Network> networkCaptor = ArgumentCaptor.forClass(Network.class); |
| 2490 | verify(mCm, timeout(HANDLER_TIMEOUT_MS).times(1)) |
| 2491 | .startCaptivePortalApp(networkCaptor.capture(), bundleCaptor.capture()); |
| 2492 | verify(mNotifier).notifyCaptivePortalValidationPending(networkCaptor.getValue()); |
| 2493 | final Bundle bundle = bundleCaptor.getValue(); |
| 2494 | final Network bundleNetwork = bundle.getParcelable(ConnectivityManager.EXTRA_NETWORK); |
| 2495 | assertEquals(TEST_NETID, bundleNetwork.netId); |
| 2496 | // Network is passed both in bundle and as parameter, as the bundle is opaque to the |
| 2497 | // framework and only intended for the captive portal app, but the framework needs |
| 2498 | // the network to identify the right NetworkMonitor. |
| 2499 | assertEquals(TEST_NETID, networkCaptor.getValue().netId); |
| 2500 | // Portal URL should be redirect URL. |
| 2501 | final String redirectUrl = bundle.getString(ConnectivityManager.EXTRA_CAPTIVE_PORTAL_URL); |
Chiachang Wang | 220428b | 2020-05-29 10:03:41 +0800 | [diff] [blame] | 2502 | assertEquals(expectedUrl, redirectUrl); |
Chiachang Wang | 26a626a | 2020-03-02 17:41:58 +0800 | [diff] [blame] | 2503 | } |
| 2504 | |
| 2505 | @Test |
Chiachang Wang | 813ee47 | 2019-05-23 16:29:30 +0800 | [diff] [blame] | 2506 | public void testEvaluationState_clearProbeResults() throws Exception { |
Remi NGUYEN VAN | 0402afc | 2020-04-15 18:56:59 +0900 | [diff] [blame] | 2507 | setValidProbes(); |
Chiachang Wang | 813ee47 | 2019-05-23 16:29:30 +0800 | [diff] [blame] | 2508 | final NetworkMonitor nm = runValidatedNetworkTest(); |
| 2509 | nm.getEvaluationState().clearProbeResults(); |
| 2510 | // Verify probe results are all reset and only evaluation result left. |
| 2511 | assertEquals(NETWORK_VALIDATION_RESULT_VALID, |
Remi NGUYEN VAN | 0402afc | 2020-04-15 18:56:59 +0900 | [diff] [blame] | 2512 | nm.getEvaluationState().getEvaluationResult()); |
| 2513 | assertEquals(0, nm.getEvaluationState().getProbeResults()); |
Chiachang Wang | 813ee47 | 2019-05-23 16:29:30 +0800 | [diff] [blame] | 2514 | } |
| 2515 | |
| 2516 | @Test |
| 2517 | public void testEvaluationState_reportProbeResult() throws Exception { |
Remi NGUYEN VAN | 0402afc | 2020-04-15 18:56:59 +0900 | [diff] [blame] | 2518 | setValidProbes(); |
Chiachang Wang | 813ee47 | 2019-05-23 16:29:30 +0800 | [diff] [blame] | 2519 | final NetworkMonitor nm = runValidatedNetworkTest(); |
| 2520 | |
Remi NGUYEN VAN | b4af130 | 2019-06-11 16:17:46 +0900 | [diff] [blame] | 2521 | resetCallbacks(); |
Chiachang Wang | 813ee47 | 2019-05-23 16:29:30 +0800 | [diff] [blame] | 2522 | |
Chiachang Wang | 717099f | 2020-03-23 11:31:39 +0800 | [diff] [blame] | 2523 | nm.reportHttpProbeResult(NETWORK_VALIDATION_PROBE_HTTP, |
Chiachang Wang | 5696b90 | 2020-03-23 15:09:57 +0800 | [diff] [blame] | 2524 | CaptivePortalProbeResult.success(1 << PROBE_HTTP)); |
Cody Kesting | 176bce7 | 2020-01-20 18:09:59 -0800 | [diff] [blame] | 2525 | // Verify result should be appended and notifyNetworkTestedWithExtras callback is triggered |
| 2526 | // once. |
Remi NGUYEN VAN | 0402afc | 2020-04-15 18:56:59 +0900 | [diff] [blame] | 2527 | assertEquals(NETWORK_VALIDATION_RESULT_VALID, |
| 2528 | nm.getEvaluationState().getEvaluationResult()); |
| 2529 | assertEquals(NETWORK_VALIDATION_PROBE_DNS | NETWORK_VALIDATION_PROBE_HTTPS |
| 2530 | | NETWORK_VALIDATION_PROBE_HTTP, nm.getEvaluationState().getProbeResults()); |
Chiachang Wang | 813ee47 | 2019-05-23 16:29:30 +0800 | [diff] [blame] | 2531 | |
Chiachang Wang | 717099f | 2020-03-23 11:31:39 +0800 | [diff] [blame] | 2532 | nm.reportHttpProbeResult(NETWORK_VALIDATION_PROBE_HTTP, |
Chiachang Wang | 5696b90 | 2020-03-23 15:09:57 +0800 | [diff] [blame] | 2533 | CaptivePortalProbeResult.failed(1 << PROBE_HTTP)); |
Chiachang Wang | 813ee47 | 2019-05-23 16:29:30 +0800 | [diff] [blame] | 2534 | // Verify DNS probe result should not be cleared. |
Remi NGUYEN VAN | 0402afc | 2020-04-15 18:56:59 +0900 | [diff] [blame] | 2535 | assertEquals(NETWORK_VALIDATION_PROBE_DNS, |
| 2536 | nm.getEvaluationState().getProbeResults() & NETWORK_VALIDATION_PROBE_DNS); |
Chiachang Wang | 813ee47 | 2019-05-23 16:29:30 +0800 | [diff] [blame] | 2537 | } |
| 2538 | |
| 2539 | @Test |
| 2540 | public void testEvaluationState_reportEvaluationResult() throws Exception { |
Remi NGUYEN VAN | 0402afc | 2020-04-15 18:56:59 +0900 | [diff] [blame] | 2541 | setStatus(mHttpsConnection, 500); |
| 2542 | setStatus(mHttpConnection, 204); |
| 2543 | final NetworkMonitor nm = runNetworkTest(NETWORK_VALIDATION_RESULT_PARTIAL, |
| 2544 | NETWORK_VALIDATION_PROBE_DNS | NETWORK_VALIDATION_PROBE_HTTP, |
Chiachang Wang | 813ee47 | 2019-05-23 16:29:30 +0800 | [diff] [blame] | 2545 | null /* redirectUrl */); |
Remi NGUYEN VAN | 0402afc | 2020-04-15 18:56:59 +0900 | [diff] [blame] | 2546 | |
| 2547 | nm.getEvaluationState().reportEvaluationResult(NETWORK_VALIDATION_RESULT_VALID, |
| 2548 | null /* redirectUrl */); |
| 2549 | verifyNetworkTested(NETWORK_VALIDATION_RESULT_VALID, |
| 2550 | NETWORK_VALIDATION_PROBE_DNS | NETWORK_VALIDATION_PROBE_HTTP); |
Chiachang Wang | 813ee47 | 2019-05-23 16:29:30 +0800 | [diff] [blame] | 2551 | |
| 2552 | nm.getEvaluationState().reportEvaluationResult( |
| 2553 | NETWORK_VALIDATION_RESULT_VALID | NETWORK_VALIDATION_RESULT_PARTIAL, |
| 2554 | null /* redirectUrl */); |
Remi NGUYEN VAN | 0402afc | 2020-04-15 18:56:59 +0900 | [diff] [blame] | 2555 | verifyNetworkTested( |
| 2556 | NETWORK_VALIDATION_RESULT_VALID | NETWORK_VALIDATION_RESULT_PARTIAL, |
| 2557 | NETWORK_VALIDATION_PROBE_DNS | NETWORK_VALIDATION_PROBE_HTTP); |
Chiachang Wang | 813ee47 | 2019-05-23 16:29:30 +0800 | [diff] [blame] | 2558 | |
| 2559 | nm.getEvaluationState().reportEvaluationResult(VALIDATION_RESULT_INVALID, |
| 2560 | TEST_REDIRECT_URL); |
Remi NGUYEN VAN | 0402afc | 2020-04-15 18:56:59 +0900 | [diff] [blame] | 2561 | verifyNetworkTested(VALIDATION_RESULT_INVALID, |
| 2562 | NETWORK_VALIDATION_PROBE_DNS | NETWORK_VALIDATION_PROBE_HTTP, |
| 2563 | TEST_REDIRECT_URL); |
Chiachang Wang | 813ee47 | 2019-05-23 16:29:30 +0800 | [diff] [blame] | 2564 | } |
| 2565 | |
Remi NGUYEN VAN | 2d909a7 | 2019-12-24 18:15:52 +0900 | [diff] [blame] | 2566 | @Test |
| 2567 | public void testExtractCharset() { |
| 2568 | assertEquals(StandardCharsets.UTF_8, extractCharset(null)); |
| 2569 | assertEquals(StandardCharsets.UTF_8, extractCharset("text/html;charset=utf-8")); |
| 2570 | assertEquals(StandardCharsets.UTF_8, extractCharset("text/html;charset=UtF-8")); |
| 2571 | assertEquals(StandardCharsets.UTF_8, extractCharset("text/html; Charset=\"utf-8\"")); |
| 2572 | assertEquals(StandardCharsets.UTF_8, extractCharset("image/png")); |
| 2573 | assertEquals(StandardCharsets.UTF_8, extractCharset("Text/HTML;")); |
| 2574 | assertEquals(StandardCharsets.UTF_8, extractCharset("multipart/form-data; boundary=-aa*-")); |
| 2575 | assertEquals(StandardCharsets.UTF_8, extractCharset("text/plain;something=else")); |
| 2576 | assertEquals(StandardCharsets.UTF_8, extractCharset("text/plain;charset=ImNotACharset")); |
| 2577 | |
| 2578 | assertEquals(StandardCharsets.ISO_8859_1, extractCharset("text/plain; CharSeT=ISO-8859-1")); |
| 2579 | assertEquals(Charset.forName("Shift_JIS"), extractCharset("text/plain;charset=Shift_JIS")); |
| 2580 | assertEquals(Charset.forName("Windows-1251"), extractCharset( |
| 2581 | "text/plain;charset=Windows-1251 ; somethingelse")); |
| 2582 | } |
| 2583 | |
| 2584 | @Test |
| 2585 | public void testReadAsString() throws IOException { |
| 2586 | final String repeatedString = "1aテスト-?"; |
| 2587 | // Infinite stream repeating characters |
| 2588 | class TestInputStream extends InputStream { |
| 2589 | private final byte[] mBytes = repeatedString.getBytes(StandardCharsets.UTF_8); |
| 2590 | private int mPosition = -1; |
| 2591 | |
| 2592 | @Override |
| 2593 | public int read() { |
| 2594 | mPosition = (mPosition + 1) % mBytes.length; |
| 2595 | return mBytes[mPosition]; |
| 2596 | } |
| 2597 | } |
| 2598 | |
| 2599 | final String readString = NetworkMonitor.readAsString(new TestInputStream(), |
| 2600 | 1500 /* maxLength */, StandardCharsets.UTF_8); |
| 2601 | |
| 2602 | assertEquals(1500, readString.length()); |
| 2603 | for (int i = 0; i < readString.length(); i++) { |
| 2604 | assertEquals(repeatedString.charAt(i % repeatedString.length()), readString.charAt(i)); |
| 2605 | } |
| 2606 | } |
| 2607 | |
lucaslin | d4c999f | 2020-04-09 00:31:21 +0800 | [diff] [blame] | 2608 | @Test |
| 2609 | public void testReadAsString_StreamShorterThanLimit() throws Exception { |
Chiachang Wang | daf983b | 2020-05-07 19:20:52 +0800 | [diff] [blame] | 2610 | final WrappedNetworkMonitor wnm = makeCellNotMeteredNetworkMonitor(); |
lucaslin | 139738f | 2020-04-17 12:22:55 +0800 | [diff] [blame] | 2611 | final byte[] content = "The HTTP response code is 200 but it is not a captive portal." |
| 2612 | .getBytes(StandardCharsets.UTF_8); |
| 2613 | assertEquals(new String(content), wnm.readAsString(new ByteArrayInputStream(content), |
| 2614 | content.length, StandardCharsets.UTF_8)); |
| 2615 | // Test the case that the stream ends earlier than the limit. |
| 2616 | assertEquals(new String(content), wnm.readAsString(new ByteArrayInputStream(content), |
| 2617 | content.length + 10, StandardCharsets.UTF_8)); |
lucaslin | d4c999f | 2020-04-09 00:31:21 +0800 | [diff] [blame] | 2618 | } |
| 2619 | |
Chiachang Wang | 80be441 | 2020-04-14 11:52:10 +0800 | [diff] [blame] | 2620 | @Test |
| 2621 | public void testMultipleProbesOnPortalNetwork() throws Exception { |
| 2622 | setupResourceForMultipleProbes(); |
| 2623 | // One of the http probes is portal, then result is portal. |
| 2624 | setPortal302(mOtherHttpConnection1); |
Remi NGUYEN VAN | 0402afc | 2020-04-15 18:56:59 +0900 | [diff] [blame] | 2625 | runPortalNetworkTest(); |
Chiachang Wang | 80be441 | 2020-04-14 11:52:10 +0800 | [diff] [blame] | 2626 | // Get conclusive result from one of the HTTP probe. Expect to create 2 HTTP and 2 HTTPS |
Remi NGUYEN VAN | eb5bfa9 | 2020-05-01 10:43:12 +0900 | [diff] [blame] | 2627 | // probes as resource configuration, but the portal can be detected before other probes |
| 2628 | // start. |
| 2629 | verify(mCleartextDnsNetwork, atMost(4)).openConnection(any()); |
| 2630 | verify(mCleartextDnsNetwork, atLeastOnce()).openConnection(any()); |
| 2631 | verify(mOtherHttpConnection1).getResponseCode(); |
Chiachang Wang | 80be441 | 2020-04-14 11:52:10 +0800 | [diff] [blame] | 2632 | } |
| 2633 | |
| 2634 | @Test |
| 2635 | public void testMultipleProbesOnValidNetwork() throws Exception { |
| 2636 | setupResourceForMultipleProbes(); |
| 2637 | // One of the https probes succeeds, then it's validated. |
| 2638 | setStatus(mOtherHttpsConnection2, 204); |
Remi NGUYEN VAN | 0402afc | 2020-04-15 18:56:59 +0900 | [diff] [blame] | 2639 | runValidatedNetworkTest(); |
Chiachang Wang | 80be441 | 2020-04-14 11:52:10 +0800 | [diff] [blame] | 2640 | // Get conclusive result from one of the HTTPS probe. Expect to create 2 HTTP and 2 HTTPS |
Remi NGUYEN VAN | eb5bfa9 | 2020-05-01 10:43:12 +0900 | [diff] [blame] | 2641 | // probes as resource configuration, but the network may validate from the HTTPS probe |
| 2642 | // before other probes start. |
| 2643 | verify(mCleartextDnsNetwork, atMost(4)).openConnection(any()); |
| 2644 | verify(mCleartextDnsNetwork, atLeastOnce()).openConnection(any()); |
| 2645 | verify(mOtherHttpsConnection2).getResponseCode(); |
Chiachang Wang | 80be441 | 2020-04-14 11:52:10 +0800 | [diff] [blame] | 2646 | } |
| 2647 | |
| 2648 | @Test |
Remi NGUYEN VAN | eb5bfa9 | 2020-05-01 10:43:12 +0900 | [diff] [blame] | 2649 | public void testMultipleProbesOnInValidNetworkForPrioritizedResource() throws Exception { |
Chiachang Wang | 80be441 | 2020-04-14 11:52:10 +0800 | [diff] [blame] | 2650 | setupResourceForMultipleProbes(); |
| 2651 | // The configuration resource is prioritized. Only use configurations from resource.(i.e |
| 2652 | // Only configuration for mOtherHttpsConnection2, mOtherHttpsConnection2, |
| 2653 | // mOtherHttpConnection2, mOtherHttpConnection2 will affect the result.) |
| 2654 | // Configure mHttpsConnection is no-op. |
| 2655 | setStatus(mHttpsConnection, 204); |
Remi NGUYEN VAN | 0402afc | 2020-04-15 18:56:59 +0900 | [diff] [blame] | 2656 | runFailedNetworkTest(); |
Chiachang Wang | 80be441 | 2020-04-14 11:52:10 +0800 | [diff] [blame] | 2657 | // No conclusive result from both HTTP and HTTPS probes. Expect to create 2 HTTP and 2 HTTPS |
Remi NGUYEN VAN | eb5bfa9 | 2020-05-01 10:43:12 +0900 | [diff] [blame] | 2658 | // probes as resource configuration. All probes are expected to have been run because this |
| 2659 | // network is set to never validate (no probe has a success or portal result), so NM tests |
| 2660 | // all probes to completion. |
Chiachang Wang | 80be441 | 2020-04-14 11:52:10 +0800 | [diff] [blame] | 2661 | verify(mCleartextDnsNetwork, times(4)).openConnection(any()); |
Remi NGUYEN VAN | eb5bfa9 | 2020-05-01 10:43:12 +0900 | [diff] [blame] | 2662 | verify(mHttpsConnection, never()).getResponseCode(); |
Chiachang Wang | 80be441 | 2020-04-14 11:52:10 +0800 | [diff] [blame] | 2663 | } |
| 2664 | |
| 2665 | @Test |
Remi NGUYEN VAN | 0402afc | 2020-04-15 18:56:59 +0900 | [diff] [blame] | 2666 | public void testMultipleProbesOnInValidNetwork() throws Exception { |
Chiachang Wang | 80be441 | 2020-04-14 11:52:10 +0800 | [diff] [blame] | 2667 | setupResourceForMultipleProbes(); |
Remi NGUYEN VAN | 0402afc | 2020-04-15 18:56:59 +0900 | [diff] [blame] | 2668 | runFailedNetworkTest(); |
Chiachang Wang | 80be441 | 2020-04-14 11:52:10 +0800 | [diff] [blame] | 2669 | // No conclusive result from both HTTP and HTTPS probes. Expect to create 2 HTTP and 2 HTTPS |
| 2670 | // probes as resource configuration. |
| 2671 | verify(mCleartextDnsNetwork, times(4)).openConnection(any()); |
| 2672 | } |
| 2673 | |
Hai Shalom | e8fe123 | 2021-02-08 11:47:15 -0800 | [diff] [blame] | 2674 | @Test |
| 2675 | public void testIsCaptivePortal_FromExternalSource() throws Exception { |
| 2676 | assumeTrue(CaptivePortalDataShimImpl.isSupported()); |
| 2677 | assumeTrue(ShimUtils.isAtLeastS()); |
| 2678 | when(mDependencies.isFeatureEnabled(any(), eq(NAMESPACE_CONNECTIVITY), |
| 2679 | eq(DISMISS_PORTAL_IN_VALIDATED_NETWORK), anyBoolean())).thenReturn(true); |
| 2680 | final NetworkMonitor monitor = makeMonitor(WIFI_NOT_METERED_CAPABILITIES); |
Lorenzo Colitti | a83f653 | 2021-02-21 17:51:09 +0900 | [diff] [blame] | 2681 | |
Hai Shalom | e8fe123 | 2021-02-08 11:47:15 -0800 | [diff] [blame] | 2682 | NetworkInformationShim networkShim = NetworkInformationShimImpl.newInstance(); |
Lorenzo Colitti | a83f653 | 2021-02-21 17:51:09 +0900 | [diff] [blame] | 2683 | CaptivePortalDataShim captivePortalData = new CaptivePortalDataShimImpl( |
| 2684 | new CaptivePortalData.Builder().setCaptive(true).build()); |
| 2685 | final LinkProperties linkProperties = new LinkProperties(TEST_LINK_PROPERTIES); |
| 2686 | networkShim.setCaptivePortalData(linkProperties, captivePortalData); |
Hai Shalom | e8fe123 | 2021-02-08 11:47:15 -0800 | [diff] [blame] | 2687 | CaptivePortalDataShim captivePortalDataShim = |
| 2688 | networkShim.getCaptivePortalData(linkProperties); |
| 2689 | |
| 2690 | try { |
| 2691 | // Set up T&C captive portal info from Passpoint |
| 2692 | captivePortalData = captivePortalDataShim.withPasspointInfo(TEST_FRIENDLY_NAME, |
| 2693 | Uri.parse(TEST_VENUE_INFO_URL), Uri.parse(TEST_LOGIN_URL)); |
| 2694 | } catch (UnsupportedApiLevelException e) { |
| 2695 | // Minimum API level for this test is 31 |
| 2696 | return; |
| 2697 | } |
| 2698 | |
Lorenzo Colitti | a83f653 | 2021-02-21 17:51:09 +0900 | [diff] [blame] | 2699 | networkShim.setCaptivePortalData(linkProperties, captivePortalData); |
Hai Shalom | e8fe123 | 2021-02-08 11:47:15 -0800 | [diff] [blame] | 2700 | monitor.notifyLinkPropertiesChanged(linkProperties); |
| 2701 | final NetworkCapabilities networkCapabilities = |
| 2702 | new NetworkCapabilities(WIFI_NOT_METERED_CAPABILITIES); |
| 2703 | monitor.notifyNetworkConnected(linkProperties, networkCapabilities); |
| 2704 | verify(mCallbacks, timeout(HANDLER_TIMEOUT_MS).times(1)) |
| 2705 | .showProvisioningNotification(any(), any()); |
| 2706 | assertEquals(1, mRegisteredReceivers.size()); |
| 2707 | verifyNetworkTested(VALIDATION_RESULT_PORTAL, 0 /* probesSucceeded */, TEST_LOGIN_URL); |
| 2708 | |
| 2709 | // Force reevaluation and confirm that the network is still captive |
| 2710 | HandlerUtils.waitForIdle(monitor.getHandler(), HANDLER_TIMEOUT_MS); |
| 2711 | resetCallbacks(); |
| 2712 | monitor.forceReevaluation(Process.myUid()); |
| 2713 | assertEquals(monitor.getEvaluationState().getProbeCompletedResult(), 0); |
| 2714 | verifyNetworkTested(VALIDATION_RESULT_PORTAL, 0 /* probesSucceeded */, TEST_LOGIN_URL); |
| 2715 | |
| 2716 | // Check that startCaptivePortalApp sends the expected intent. |
| 2717 | monitor.launchCaptivePortalApp(); |
| 2718 | |
| 2719 | verify(mCm, timeout(HANDLER_TIMEOUT_MS).times(1)).startCaptivePortalApp( |
| 2720 | argThat(network -> TEST_NETID == network.netId), |
| 2721 | argThat(bundle -> bundle.getString( |
| 2722 | ConnectivityManager.EXTRA_CAPTIVE_PORTAL_URL).equals(TEST_LOGIN_URL) |
| 2723 | && TEST_NETID == ((Network) bundle.getParcelable( |
| 2724 | ConnectivityManager.EXTRA_NETWORK)).netId)); |
| 2725 | } |
| 2726 | |
Pavan Kumar M | 2fbc846 | 2021-04-28 11:20:25 +0530 | [diff] [blame] | 2727 | @Test |
| 2728 | public void testOemPaidNetworkValidated() throws Exception { |
| 2729 | setValidProbes(); |
| 2730 | |
| 2731 | final NetworkMonitor nm = runNetworkTest(TEST_LINK_PROPERTIES, |
| 2732 | WIFI_OEM_PAID_CAPABILITIES, |
| 2733 | NETWORK_VALIDATION_RESULT_VALID, |
| 2734 | NETWORK_VALIDATION_PROBE_DNS | NETWORK_VALIDATION_PROBE_HTTPS, |
| 2735 | null /* redirectUrl */); |
| 2736 | assertEquals(NETWORK_VALIDATION_RESULT_VALID, |
| 2737 | nm.getEvaluationState().getEvaluationResult()); |
| 2738 | } |
| 2739 | |
| 2740 | @Test |
| 2741 | public void testOemPaidNetwork_AllProbesFailed() throws Exception { |
| 2742 | setSslException(mHttpsConnection); |
| 2743 | setStatus(mHttpConnection, 500); |
| 2744 | setStatus(mFallbackConnection, 404); |
| 2745 | |
| 2746 | runNetworkTest(TEST_LINK_PROPERTIES, |
| 2747 | WIFI_OEM_PAID_CAPABILITIES, |
| 2748 | VALIDATION_RESULT_INVALID, 0 /* probesSucceeded */, null /* redirectUrl */); |
| 2749 | } |
| 2750 | |
| 2751 | @Test |
| 2752 | public void testOemPaidNetworkNoInternetCapabilityValidated() throws Exception { |
| 2753 | setSslException(mHttpsConnection); |
| 2754 | setStatus(mHttpConnection, 500); |
| 2755 | setStatus(mFallbackConnection, 404); |
| 2756 | |
| 2757 | final NetworkCapabilities networkCapabilities = |
| 2758 | new NetworkCapabilities(WIFI_OEM_PAID_CAPABILITIES); |
| 2759 | networkCapabilities.removeCapability(NET_CAPABILITY_INTERNET); |
Cody Kesting | 78156e4 | 2020-08-03 18:19:56 -0700 | [diff] [blame] | 2760 | |
| 2761 | final int validationResult = ShimUtils.isAtLeastS() |
| 2762 | ? NETWORK_VALIDATION_RESULT_VALID | NETWORK_VALIDATION_RESULT_SKIPPED |
| 2763 | : NETWORK_VALIDATION_RESULT_VALID; |
Pavan Kumar M | 2fbc846 | 2021-04-28 11:20:25 +0530 | [diff] [blame] | 2764 | runNetworkTest(TEST_LINK_PROPERTIES, networkCapabilities, |
Cody Kesting | 78156e4 | 2020-08-03 18:19:56 -0700 | [diff] [blame] | 2765 | validationResult, 0 /* probesSucceeded */, null /* redirectUrl */); |
Pavan Kumar M | 2fbc846 | 2021-04-28 11:20:25 +0530 | [diff] [blame] | 2766 | |
| 2767 | verify(mCleartextDnsNetwork, never()).openConnection(any()); |
| 2768 | verify(mHttpsConnection, never()).getResponseCode(); |
| 2769 | verify(mHttpConnection, never()).getResponseCode(); |
| 2770 | verify(mFallbackConnection, never()).getResponseCode(); |
| 2771 | } |
| 2772 | |
| 2773 | @Test |
| 2774 | public void testOemPaidNetwork_CaptivePortalNotLaunched() throws Exception { |
| 2775 | setSslException(mHttpsConnection); |
| 2776 | setStatus(mFallbackConnection, 404); |
| 2777 | setPortal302(mHttpConnection); |
| 2778 | |
| 2779 | runNetworkTest(TEST_LINK_PROPERTIES, WIFI_OEM_PAID_CAPABILITIES, |
| 2780 | VALIDATION_RESULT_PORTAL, 0 /* probesSucceeded */, |
| 2781 | TEST_LOGIN_URL); |
| 2782 | |
| 2783 | verify(mCallbacks, never()).showProvisioningNotification(any(), any()); |
| 2784 | } |
| 2785 | |
Chiachang Wang | 80be441 | 2020-04-14 11:52:10 +0800 | [diff] [blame] | 2786 | private void setupResourceForMultipleProbes() { |
| 2787 | // Configure the resource to send multiple probe. |
| 2788 | when(mResources.getStringArray(R.array.config_captive_portal_https_urls)) |
| 2789 | .thenReturn(TEST_HTTPS_URLS); |
| 2790 | when(mResources.getStringArray(R.array.config_captive_portal_http_urls)) |
| 2791 | .thenReturn(TEST_HTTP_URLS); |
| 2792 | } |
| 2793 | |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 2794 | private void makeDnsTimeoutEvent(WrappedNetworkMonitor wrappedMonitor, int count) { |
| 2795 | for (int i = 0; i < count; i++) { |
| 2796 | wrappedMonitor.getDnsStallDetector().accumulateConsecutiveDnsTimeoutCount( |
| 2797 | RETURN_CODE_DNS_TIMEOUT); |
| 2798 | } |
| 2799 | } |
| 2800 | |
| 2801 | private void makeDnsSuccessEvent(WrappedNetworkMonitor wrappedMonitor, int count) { |
| 2802 | for (int i = 0; i < count; i++) { |
| 2803 | wrappedMonitor.getDnsStallDetector().accumulateConsecutiveDnsTimeoutCount( |
| 2804 | RETURN_CODE_DNS_SUCCESS); |
| 2805 | } |
| 2806 | } |
| 2807 | |
Chiachang Wang | 899d596 | 2019-03-08 14:34:19 +0800 | [diff] [blame] | 2808 | private DataStallDetectionStats makeEmptyDataStallDetectionStats() { |
| 2809 | return new DataStallDetectionStats.Builder().build(); |
| 2810 | } |
| 2811 | |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 2812 | private void setDataStallEvaluationType(int type) { |
Chiachang Wang | 9a87f80 | 2019-04-08 19:06:21 +0800 | [diff] [blame] | 2813 | when(mDependencies.getDeviceConfigPropertyInt(any(), |
| 2814 | eq(CONFIG_DATA_STALL_EVALUATION_TYPE), anyInt())).thenReturn(type); |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 2815 | } |
| 2816 | |
| 2817 | private void setMinDataStallEvaluateInterval(int time) { |
Chiachang Wang | 9a87f80 | 2019-04-08 19:06:21 +0800 | [diff] [blame] | 2818 | when(mDependencies.getDeviceConfigPropertyInt(any(), |
| 2819 | eq(CONFIG_DATA_STALL_MIN_EVALUATE_INTERVAL), anyInt())).thenReturn(time); |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 2820 | } |
| 2821 | |
| 2822 | private void setValidDataStallDnsTimeThreshold(int time) { |
Chiachang Wang | 9a87f80 | 2019-04-08 19:06:21 +0800 | [diff] [blame] | 2823 | when(mDependencies.getDeviceConfigPropertyInt(any(), |
| 2824 | eq(CONFIG_DATA_STALL_VALID_DNS_TIME_THRESHOLD), anyInt())).thenReturn(time); |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 2825 | } |
| 2826 | |
| 2827 | private void setConsecutiveDnsTimeoutThreshold(int num) { |
Chiachang Wang | 9a87f80 | 2019-04-08 19:06:21 +0800 | [diff] [blame] | 2828 | when(mDependencies.getDeviceConfigPropertyInt(any(), |
| 2829 | eq(CONFIG_DATA_STALL_CONSECUTIVE_DNS_TIMEOUT_THRESHOLD), anyInt())).thenReturn(num); |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 2830 | } |
| 2831 | |
Chiachang Wang | a5716bf | 2019-11-20 16:13:07 +0800 | [diff] [blame] | 2832 | private void setTcpPollingInterval(int time) { |
Chiachang Wang | 0cbaa8d | 2020-07-21 09:55:58 +0800 | [diff] [blame] | 2833 | doReturn(time).when(mDependencies).getDeviceConfigPropertyInt(any(), |
| 2834 | eq(CONFIG_DATA_STALL_TCP_POLLING_INTERVAL), anyInt()); |
Chiachang Wang | a5716bf | 2019-11-20 16:13:07 +0800 | [diff] [blame] | 2835 | } |
| 2836 | |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 2837 | private void setFallbackUrl(String url) { |
| 2838 | when(mDependencies.getSetting(any(), |
| 2839 | eq(Settings.Global.CAPTIVE_PORTAL_FALLBACK_URL), any())).thenReturn(url); |
| 2840 | } |
| 2841 | |
| 2842 | private void setOtherFallbackUrls(String urls) { |
Chiachang Wang | 79a6da3 | 2019-04-17 17:00:54 +0800 | [diff] [blame] | 2843 | when(mDependencies.getDeviceConfigProperty(any(), |
| 2844 | eq(CAPTIVE_PORTAL_OTHER_FALLBACK_URLS), any())).thenReturn(urls); |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 2845 | } |
| 2846 | |
| 2847 | private void setFallbackSpecs(String specs) { |
Chiachang Wang | 79a6da3 | 2019-04-17 17:00:54 +0800 | [diff] [blame] | 2848 | when(mDependencies.getDeviceConfigProperty(any(), |
| 2849 | eq(CAPTIVE_PORTAL_FALLBACK_PROBE_SPECS), any())).thenReturn(specs); |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 2850 | } |
| 2851 | |
| 2852 | private void setCaptivePortalMode(int mode) { |
| 2853 | when(mDependencies.getSetting(any(), |
| 2854 | eq(Settings.Global.CAPTIVE_PORTAL_MODE), anyInt())).thenReturn(mode); |
| 2855 | } |
| 2856 | |
Chiachang Wang | 26a626a | 2020-03-02 17:41:58 +0800 | [diff] [blame] | 2857 | private void setDismissPortalInValidatedNetwork(boolean enabled) { |
| 2858 | when(mDependencies.isFeatureEnabled(any(), any(), |
| 2859 | eq(DISMISS_PORTAL_IN_VALIDATED_NETWORK), anyBoolean())).thenReturn(enabled); |
| 2860 | } |
| 2861 | |
Remi NGUYEN VAN | 943d634 | 2020-03-24 18:22:34 +0900 | [diff] [blame] | 2862 | private void setDeviceConfig(String key, String value) { |
| 2863 | doReturn(value).when(mDependencies).getDeviceConfigProperty(eq(NAMESPACE_CONNECTIVITY), |
| 2864 | eq(key), any() /* defaultValue */); |
| 2865 | } |
| 2866 | |
Remi NGUYEN VAN | 0402afc | 2020-04-15 18:56:59 +0900 | [diff] [blame] | 2867 | private NetworkMonitor runPortalNetworkTest() throws RemoteException { |
| 2868 | final NetworkMonitor nm = runNetworkTest(VALIDATION_RESULT_PORTAL, |
| 2869 | 0 /* probesSucceeded */, TEST_LOGIN_URL); |
Lorenzo Colitti | 37ee46f | 2019-04-08 21:51:41 +0900 | [diff] [blame] | 2870 | assertEquals(1, mRegisteredReceivers.size()); |
Remi NGUYEN VAN | 0402afc | 2020-04-15 18:56:59 +0900 | [diff] [blame] | 2871 | return nm; |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 2872 | } |
| 2873 | |
Remi NGUYEN VAN | 0402afc | 2020-04-15 18:56:59 +0900 | [diff] [blame] | 2874 | private NetworkMonitor runNoValidationNetworkTest() throws RemoteException { |
| 2875 | final NetworkMonitor nm = runNetworkTest(NETWORK_VALIDATION_RESULT_VALID, |
| 2876 | 0 /* probesSucceeded */, null /* redirectUrl */); |
Chiachang Wang | 813ee47 | 2019-05-23 16:29:30 +0800 | [diff] [blame] | 2877 | assertEquals(0, mRegisteredReceivers.size()); |
Remi NGUYEN VAN | 0402afc | 2020-04-15 18:56:59 +0900 | [diff] [blame] | 2878 | return nm; |
Chiachang Wang | 813ee47 | 2019-05-23 16:29:30 +0800 | [diff] [blame] | 2879 | } |
| 2880 | |
Remi NGUYEN VAN | 0402afc | 2020-04-15 18:56:59 +0900 | [diff] [blame] | 2881 | private NetworkMonitor runFailedNetworkTest() throws RemoteException { |
| 2882 | final NetworkMonitor nm = runNetworkTest( |
| 2883 | VALIDATION_RESULT_INVALID, 0 /* probesSucceeded */, null /* redirectUrl */); |
Lorenzo Colitti | 37ee46f | 2019-04-08 21:51:41 +0900 | [diff] [blame] | 2884 | assertEquals(0, mRegisteredReceivers.size()); |
Remi NGUYEN VAN | 0402afc | 2020-04-15 18:56:59 +0900 | [diff] [blame] | 2885 | return nm; |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 2886 | } |
| 2887 | |
Remi NGUYEN VAN | 0402afc | 2020-04-15 18:56:59 +0900 | [diff] [blame] | 2888 | private NetworkMonitor runPartialConnectivityNetworkTest(int probesSucceeded) |
| 2889 | throws RemoteException { |
| 2890 | final NetworkMonitor nm = runNetworkTest(NETWORK_VALIDATION_RESULT_PARTIAL, |
| 2891 | probesSucceeded, null /* redirectUrl */); |
Lorenzo Colitti | 37ee46f | 2019-04-08 21:51:41 +0900 | [diff] [blame] | 2892 | assertEquals(0, mRegisteredReceivers.size()); |
Remi NGUYEN VAN | 0402afc | 2020-04-15 18:56:59 +0900 | [diff] [blame] | 2893 | return nm; |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 2894 | } |
| 2895 | |
Remi NGUYEN VAN | 0402afc | 2020-04-15 18:56:59 +0900 | [diff] [blame] | 2896 | private NetworkMonitor runValidatedNetworkTest() throws RemoteException { |
| 2897 | // Expect to send HTTPS and evaluation results. |
| 2898 | return runNetworkTest(NETWORK_VALIDATION_RESULT_VALID, |
| 2899 | NETWORK_VALIDATION_PROBE_DNS | NETWORK_VALIDATION_PROBE_HTTPS, |
| 2900 | null /* redirectUrl */); |
Remi NGUYEN VAN | dc6e640 | 2019-03-27 15:42:53 +0900 | [diff] [blame] | 2901 | } |
| 2902 | |
Remi NGUYEN VAN | 0402afc | 2020-04-15 18:56:59 +0900 | [diff] [blame] | 2903 | private NetworkMonitor runNetworkTest(int testResult, int probesSucceeded, String redirectUrl) |
| 2904 | throws RemoteException { |
Chiachang Wang | daf983b | 2020-05-07 19:20:52 +0800 | [diff] [blame] | 2905 | return runNetworkTest(TEST_LINK_PROPERTIES, CELL_METERED_CAPABILITIES, testResult, |
Remi NGUYEN VAN | 0402afc | 2020-04-15 18:56:59 +0900 | [diff] [blame] | 2906 | probesSucceeded, redirectUrl); |
Remi NGUYEN VAN | dc6e640 | 2019-03-27 15:42:53 +0900 | [diff] [blame] | 2907 | } |
| 2908 | |
Remi NGUYEN VAN | 2fa48c2 | 2019-12-09 16:40:02 +0900 | [diff] [blame] | 2909 | private NetworkMonitor runNetworkTest(LinkProperties lp, NetworkCapabilities nc, |
Remi NGUYEN VAN | 0402afc | 2020-04-15 18:56:59 +0900 | [diff] [blame] | 2910 | int testResult, int probesSucceeded, String redirectUrl) throws RemoteException { |
Lorenzo Colitti | cfc1abc | 2019-04-28 00:55:19 +0900 | [diff] [blame] | 2911 | final NetworkMonitor monitor = makeMonitor(nc); |
Remi NGUYEN VAN | 2fa48c2 | 2019-12-09 16:40:02 +0900 | [diff] [blame] | 2912 | monitor.notifyNetworkConnected(lp, nc); |
Remi NGUYEN VAN | 0402afc | 2020-04-15 18:56:59 +0900 | [diff] [blame] | 2913 | verifyNetworkTested(testResult, probesSucceeded, redirectUrl); |
Chalard Jean | 23a0630 | 2020-06-26 00:41:00 +0900 | [diff] [blame] | 2914 | HandlerUtils.waitForIdle(monitor.getHandler(), HANDLER_TIMEOUT_MS); |
Remi NGUYEN VAN | 2fa48c2 | 2019-12-09 16:40:02 +0900 | [diff] [blame] | 2915 | |
| 2916 | return monitor; |
| 2917 | } |
| 2918 | |
Remi NGUYEN VAN | 0402afc | 2020-04-15 18:56:59 +0900 | [diff] [blame] | 2919 | private void verifyNetworkTested(int testResult, int probesSucceeded) throws RemoteException { |
| 2920 | verifyNetworkTested(testResult, probesSucceeded, null /* redirectUrl */); |
| 2921 | } |
| 2922 | |
| 2923 | private void verifyNetworkTested(int testResult, int probesSucceeded, String redirectUrl) |
| 2924 | throws RemoteException { |
Remi NGUYEN VAN | dc6e640 | 2019-03-27 15:42:53 +0900 | [diff] [blame] | 2925 | try { |
Remi NGUYEN VAN | 0402afc | 2020-04-15 18:56:59 +0900 | [diff] [blame] | 2926 | verify(mCallbacks, timeout(HANDLER_TIMEOUT_MS)).notifyNetworkTestedWithExtras( |
| 2927 | matchNetworkTestResultParcelable(testResult, probesSucceeded, redirectUrl)); |
| 2928 | } catch (AssertionFailedError e) { |
| 2929 | // Capture the callbacks up to now to give a better error message |
| 2930 | final ArgumentCaptor<NetworkTestResultParcelable> captor = |
| 2931 | ArgumentCaptor.forClass(NetworkTestResultParcelable.class); |
| 2932 | |
| 2933 | // Call verify() again to verify the same method call verified by the previous verify |
| 2934 | // call which failed, but this time use a captor to log the exact parcel sent by |
| 2935 | // NetworkMonitor. |
| 2936 | // This assertion will fail if notifyNetworkTested was not called at all. |
| 2937 | verify(mCallbacks).notifyNetworkTestedWithExtras(captor.capture()); |
| 2938 | |
| 2939 | final NetworkTestResultParcelable lastResult = captor.getValue(); |
| 2940 | fail(String.format("notifyNetworkTestedWithExtras was not called with the " |
| 2941 | + "expected result within timeout. " |
| 2942 | + "Expected result %d, probes succeeded %d, redirect URL %s, " |
| 2943 | + "last result was (%d, %d, %s).", |
| 2944 | testResult, probesSucceeded, redirectUrl, |
| 2945 | lastResult.result, lastResult.probesSucceeded, lastResult.redirectUrl)); |
Remi NGUYEN VAN | dc6e640 | 2019-03-27 15:42:53 +0900 | [diff] [blame] | 2946 | } |
Remi NGUYEN VAN | 2fa48c2 | 2019-12-09 16:40:02 +0900 | [diff] [blame] | 2947 | } |
Remi NGUYEN VAN | dc6e640 | 2019-03-27 15:42:53 +0900 | [diff] [blame] | 2948 | |
Remi NGUYEN VAN | 2fa48c2 | 2019-12-09 16:40:02 +0900 | [diff] [blame] | 2949 | private void notifyNetworkConnected(NetworkMonitor nm, NetworkCapabilities nc) { |
| 2950 | nm.notifyNetworkConnected(TEST_LINK_PROPERTIES, nc); |
lucaslin | b057396 | 2019-03-12 13:08:03 +0800 | [diff] [blame] | 2951 | } |
| 2952 | |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 2953 | private void setSslException(HttpURLConnection connection) throws IOException { |
| 2954 | doThrow(new SSLHandshakeException("Invalid cert")).when(connection).getResponseCode(); |
| 2955 | } |
| 2956 | |
Remi NGUYEN VAN | 0402afc | 2020-04-15 18:56:59 +0900 | [diff] [blame] | 2957 | private void setValidProbes() throws IOException { |
| 2958 | setStatus(mHttpsConnection, 204); |
| 2959 | setStatus(mHttpConnection, 204); |
| 2960 | } |
| 2961 | |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 2962 | private void set302(HttpURLConnection connection, String location) throws IOException { |
| 2963 | setStatus(connection, 302); |
| 2964 | doReturn(location).when(connection).getHeaderField(LOCATION_HEADER); |
| 2965 | } |
| 2966 | |
| 2967 | private void setPortal302(HttpURLConnection connection) throws IOException { |
Remi NGUYEN VAN | 0402afc | 2020-04-15 18:56:59 +0900 | [diff] [blame] | 2968 | set302(connection, TEST_LOGIN_URL); |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 2969 | } |
| 2970 | |
Remi NGUYEN VAN | 2fa48c2 | 2019-12-09 16:40:02 +0900 | [diff] [blame] | 2971 | private void setApiContent(HttpURLConnection connection, String content) throws IOException { |
| 2972 | setStatus(connection, 200); |
| 2973 | final Map<String, List<String>> headerFields = new HashMap<>(); |
| 2974 | headerFields.put( |
| 2975 | CONTENT_TYPE_HEADER, singletonList("application/captive+json;charset=UTF-8")); |
| 2976 | doReturn(headerFields).when(connection).getHeaderFields(); |
| 2977 | doReturn(new ByteArrayInputStream(content.getBytes(StandardCharsets.UTF_8))) |
| 2978 | .when(connection).getInputStream(); |
| 2979 | } |
| 2980 | |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 2981 | private void setStatus(HttpURLConnection connection, int status) throws IOException { |
| 2982 | doReturn(status).when(connection).getResponseCode(); |
| 2983 | } |
Chiachang Wang | caa3520 | 2019-02-26 11:32:18 +0800 | [diff] [blame] | 2984 | |
| 2985 | private void generateTimeoutDnsEvent(DataStallDetectionStats.Builder stats, int num) { |
| 2986 | for (int i = 0; i < num; i++) { |
Chiachang Wang | 9af1cb5 | 2020-05-20 15:14:46 +0800 | [diff] [blame] | 2987 | stats.addDnsEvent(RETURN_CODE_DNS_TIMEOUT, TEST_ELAPSED_TIME_MS /* timeMs */); |
Chiachang Wang | caa3520 | 2019-02-26 11:32:18 +0800 | [diff] [blame] | 2988 | } |
| 2989 | } |
Chiachang Wang | 813ee47 | 2019-05-23 16:29:30 +0800 | [diff] [blame] | 2990 | |
Chiachang Wang | 9af1cb5 | 2020-05-20 15:14:46 +0800 | [diff] [blame] | 2991 | private void generateTestTcpStats(DataStallDetectionStats.Builder stats) { |
| 2992 | when(mTst.getLatestPacketFailPercentage()).thenReturn(TEST_TCP_FAIL_RATE); |
| 2993 | when(mTst.getSentSinceLastRecv()).thenReturn(TEST_TCP_PACKET_COUNT); |
| 2994 | stats.setTcpFailRate(TEST_TCP_FAIL_RATE).setTcpSentSinceLastRecv(TEST_TCP_PACKET_COUNT); |
| 2995 | } |
| 2996 | |
Remi NGUYEN VAN | 0402afc | 2020-04-15 18:56:59 +0900 | [diff] [blame] | 2997 | private NetworkTestResultParcelable matchNetworkTestResultParcelable(final int result, |
| 2998 | final int probesSucceeded) { |
| 2999 | return matchNetworkTestResultParcelable(result, probesSucceeded, null /* redirectUrl */); |
Cody Kesting | 176bce7 | 2020-01-20 18:09:59 -0800 | [diff] [blame] | 3000 | } |
| 3001 | |
Remi NGUYEN VAN | 0402afc | 2020-04-15 18:56:59 +0900 | [diff] [blame] | 3002 | private NetworkTestResultParcelable matchNetworkTestResultParcelable(final int result, |
| 3003 | final int probesSucceeded, String redirectUrl) { |
| 3004 | // TODO: also verify probesAttempted |
| 3005 | return argThat(p -> p.result == result && p.probesSucceeded == probesSucceeded |
| 3006 | && Objects.equals(p.redirectUrl, redirectUrl)); |
Cody Kesting | 176bce7 | 2020-01-20 18:09:59 -0800 | [diff] [blame] | 3007 | } |
| 3008 | |
Chiachang Wang | 340d275 | 2020-05-21 17:51:22 +0800 | [diff] [blame] | 3009 | private DataStallReportParcelable matchDnsAndTcpDataStallParcelable(final int timeoutCount) { |
| 3010 | return argThat(p -> |
| 3011 | (p.detectionMethod & ConstantsShim.DETECTION_METHOD_DNS_EVENTS) != 0 |
| 3012 | && (p.detectionMethod & ConstantsShim.DETECTION_METHOD_TCP_METRICS) != 0 |
| 3013 | && p.dnsConsecutiveTimeouts == timeoutCount); |
| 3014 | } |
| 3015 | |
Remi NGUYEN VAN | 0402afc | 2020-04-15 18:56:59 +0900 | [diff] [blame] | 3016 | private DataStallReportParcelable matchDnsDataStallParcelable(final int timeoutCount) { |
Chiachang Wang | 340d275 | 2020-05-21 17:51:22 +0800 | [diff] [blame] | 3017 | return argThat(p -> (p.detectionMethod & ConstantsShim.DETECTION_METHOD_DNS_EVENTS) != 0 |
Remi NGUYEN VAN | 0402afc | 2020-04-15 18:56:59 +0900 | [diff] [blame] | 3018 | && p.dnsConsecutiveTimeouts == timeoutCount); |
| 3019 | } |
| 3020 | |
| 3021 | private DataStallReportParcelable matchTcpDataStallParcelable() { |
Chiachang Wang | 340d275 | 2020-05-21 17:51:22 +0800 | [diff] [blame] | 3022 | return argThat(p -> (p.detectionMethod & ConstantsShim.DETECTION_METHOD_TCP_METRICS) != 0); |
Cody Kesting | 176bce7 | 2020-01-20 18:09:59 -0800 | [diff] [blame] | 3023 | } |
Remi NGUYEN VAN | 5daa370 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 3024 | } |
| 3025 | |