Erik Kline | ce55eb1 | 2017-01-26 18:08:28 +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 android.net; |
| 18 | |
junyulai | ad01079 | 2021-01-11 16:53:38 +0800 | [diff] [blame] | 19 | import static android.net.ConnectivityManager.TYPE_NONE; |
Erik Kline | ce55eb1 | 2017-01-26 18:08:28 +0900 | [diff] [blame] | 20 | import static android.net.NetworkCapabilities.NET_CAPABILITY_CBS; |
| 21 | import static android.net.NetworkCapabilities.NET_CAPABILITY_DUN; |
| 22 | import static android.net.NetworkCapabilities.NET_CAPABILITY_FOTA; |
| 23 | import static android.net.NetworkCapabilities.NET_CAPABILITY_IMS; |
| 24 | import static android.net.NetworkCapabilities.NET_CAPABILITY_INTERNET; |
| 25 | import static android.net.NetworkCapabilities.NET_CAPABILITY_MMS; |
| 26 | import static android.net.NetworkCapabilities.NET_CAPABILITY_NOT_RESTRICTED; |
| 27 | import static android.net.NetworkCapabilities.NET_CAPABILITY_NOT_VPN; |
| 28 | import static android.net.NetworkCapabilities.NET_CAPABILITY_SUPL; |
| 29 | import static android.net.NetworkCapabilities.NET_CAPABILITY_TRUSTED; |
| 30 | import static android.net.NetworkCapabilities.NET_CAPABILITY_WIFI_P2P; |
| 31 | import static android.net.NetworkCapabilities.TRANSPORT_BLUETOOTH; |
| 32 | import static android.net.NetworkCapabilities.TRANSPORT_CELLULAR; |
| 33 | import static android.net.NetworkCapabilities.TRANSPORT_ETHERNET; |
| 34 | import static android.net.NetworkCapabilities.TRANSPORT_WIFI; |
Junyu Lai | a62493f | 2021-01-19 11:10:56 +0000 | [diff] [blame] | 35 | import static android.net.NetworkRequest.Type.BACKGROUND_REQUEST; |
junyulai | ad01079 | 2021-01-11 16:53:38 +0800 | [diff] [blame] | 36 | import static android.net.NetworkRequest.Type.REQUEST; |
| 37 | import static android.net.NetworkRequest.Type.TRACK_DEFAULT; |
Lorenzo Colitti | 76b639e | 2021-01-29 20:14:04 +0900 | [diff] [blame] | 38 | import static android.net.NetworkRequest.Type.TRACK_SYSTEM_DEFAULT; |
Erik Kline | ce55eb1 | 2017-01-26 18:08:28 +0900 | [diff] [blame] | 39 | |
Chiachang Wang | 859218c | 2021-09-16 14:38:52 +0800 | [diff] [blame] | 40 | import static com.android.testutils.MiscAsserts.assertThrows; |
| 41 | |
Erik Kline | ce55eb1 | 2017-01-26 18:08:28 +0900 | [diff] [blame] | 42 | import static org.junit.Assert.assertFalse; |
| 43 | import static org.junit.Assert.assertNotNull; |
markchien | d201566 | 2022-04-26 18:08:03 +0800 | [diff] [blame] | 44 | import static org.junit.Assert.assertNull; |
Erik Kline | ce55eb1 | 2017-01-26 18:08:28 +0900 | [diff] [blame] | 45 | import static org.junit.Assert.assertTrue; |
Hugo Benichi | 2aa65af | 2017-03-06 09:17:06 +0900 | [diff] [blame] | 46 | import static org.junit.Assert.fail; |
Lorenzo Colitti | 79c6f22 | 2021-03-18 00:54:57 +0900 | [diff] [blame] | 47 | import static org.mockito.ArgumentMatchers.anyBoolean; |
junyulai | ad01079 | 2021-01-11 16:53:38 +0800 | [diff] [blame] | 48 | import static org.mockito.ArgumentMatchers.eq; |
Philip P. Moltmann | 7bc33df | 2020-03-26 11:50:35 -0700 | [diff] [blame] | 49 | import static org.mockito.ArgumentMatchers.nullable; |
Remi NGUYEN VAN | c9c745f | 2021-06-15 16:05:56 +0900 | [diff] [blame] | 50 | import static org.mockito.Mockito.CALLS_REAL_METHODS; |
Chalard Jean | 1afb2e3 | 2021-10-04 16:36:32 +0900 | [diff] [blame] | 51 | import static org.mockito.Mockito.after; |
Hugo Benichi | 2aa65af | 2017-03-06 09:17:06 +0900 | [diff] [blame] | 52 | import static org.mockito.Mockito.any; |
| 53 | import static org.mockito.Mockito.anyInt; |
| 54 | import static org.mockito.Mockito.mock; |
junyulai | ad01079 | 2021-01-11 16:53:38 +0800 | [diff] [blame] | 55 | import static org.mockito.Mockito.never; |
| 56 | import static org.mockito.Mockito.reset; |
Hugo Benichi | 2aa65af | 2017-03-06 09:17:06 +0900 | [diff] [blame] | 57 | import static org.mockito.Mockito.timeout; |
| 58 | import static org.mockito.Mockito.times; |
| 59 | import static org.mockito.Mockito.verify; |
| 60 | import static org.mockito.Mockito.when; |
Erik Kline | ce55eb1 | 2017-01-26 18:08:28 +0900 | [diff] [blame] | 61 | |
Hugo Benichi | 7abd43f | 2017-05-09 14:09:02 +0900 | [diff] [blame] | 62 | import android.app.PendingIntent; |
Hugo Benichi | 2aa65af | 2017-03-06 09:17:06 +0900 | [diff] [blame] | 63 | import android.content.Context; |
Brett Chabot | 147f6cf | 2019-03-04 14:14:56 -0800 | [diff] [blame] | 64 | import android.content.pm.ApplicationInfo; |
Brett Chabot | 147f6cf | 2019-03-04 14:14:56 -0800 | [diff] [blame] | 65 | import android.net.ConnectivityManager.NetworkCallback; |
Brett Chabot | 147f6cf | 2019-03-04 14:14:56 -0800 | [diff] [blame] | 66 | import android.os.Build.VERSION_CODES; |
Hugo Benichi | 2aa65af | 2017-03-06 09:17:06 +0900 | [diff] [blame] | 67 | import android.os.Bundle; |
| 68 | import android.os.Handler; |
| 69 | import android.os.Looper; |
| 70 | import android.os.Message; |
| 71 | import android.os.Messenger; |
Lorenzo Colitti | 2a49c5d | 2021-03-12 22:48:07 +0900 | [diff] [blame] | 72 | import android.os.Process; |
Brett Chabot | 147f6cf | 2019-03-04 14:14:56 -0800 | [diff] [blame] | 73 | |
| 74 | import androidx.test.filters.SmallTest; |
Remi NGUYEN VAN | 154cf1d | 2021-06-29 17:16:28 +0900 | [diff] [blame] | 75 | |
markchien | ed0fab0 | 2022-04-26 16:33:47 +0800 | [diff] [blame] | 76 | import com.android.internal.util.test.BroadcastInterceptingContext; |
Remi NGUYEN VAN | 154cf1d | 2021-06-29 17:16:28 +0900 | [diff] [blame] | 77 | import com.android.testutils.DevSdkIgnoreRule; |
| 78 | import com.android.testutils.DevSdkIgnoreRunner; |
Erik Kline | ce55eb1 | 2017-01-26 18:08:28 +0900 | [diff] [blame] | 79 | |
Hugo Benichi | 2aa65af | 2017-03-06 09:17:06 +0900 | [diff] [blame] | 80 | import org.junit.Before; |
Hugo Benichi | 1d6742c | 2017-03-06 06:16:51 +0000 | [diff] [blame] | 81 | import org.junit.Test; |
Hugo Benichi | 2aa65af | 2017-03-06 09:17:06 +0900 | [diff] [blame] | 82 | import org.junit.runner.RunWith; |
| 83 | import org.mockito.ArgumentCaptor; |
| 84 | import org.mockito.Mock; |
| 85 | import org.mockito.MockitoAnnotations; |
Hugo Benichi | 1d6742c | 2017-03-06 06:16:51 +0000 | [diff] [blame] | 86 | |
markchien | ed0fab0 | 2022-04-26 16:33:47 +0800 | [diff] [blame] | 87 | import java.lang.ref.WeakReference; |
| 88 | |
Remi NGUYEN VAN | 154cf1d | 2021-06-29 17:16:28 +0900 | [diff] [blame] | 89 | @RunWith(DevSdkIgnoreRunner.class) |
Erik Kline | ce55eb1 | 2017-01-26 18:08:28 +0900 | [diff] [blame] | 90 | @SmallTest |
Remi NGUYEN VAN | 154cf1d | 2021-06-29 17:16:28 +0900 | [diff] [blame] | 91 | @DevSdkIgnoreRule.IgnoreUpTo(VERSION_CODES.R) |
Erik Kline | ce55eb1 | 2017-01-26 18:08:28 +0900 | [diff] [blame] | 92 | public class ConnectivityManagerTest { |
Chalard Jean | 1afb2e3 | 2021-10-04 16:36:32 +0900 | [diff] [blame] | 93 | private static final int TIMEOUT_MS = 30_000; |
| 94 | private static final int SHORT_TIMEOUT_MS = 150; |
Hugo Benichi | 2aa65af | 2017-03-06 09:17:06 +0900 | [diff] [blame] | 95 | |
| 96 | @Mock Context mCtx; |
| 97 | @Mock IConnectivityManager mService; |
| 98 | |
| 99 | @Before |
| 100 | public void setUp() { |
| 101 | MockitoAnnotations.initMocks(this); |
| 102 | } |
| 103 | |
Erik Kline | ce55eb1 | 2017-01-26 18:08:28 +0900 | [diff] [blame] | 104 | static NetworkCapabilities verifyNetworkCapabilities( |
| 105 | int legacyType, int transportType, int... capabilities) { |
| 106 | final NetworkCapabilities nc = ConnectivityManager.networkCapabilitiesForType(legacyType); |
| 107 | assertNotNull(nc); |
| 108 | assertTrue(nc.hasTransport(transportType)); |
| 109 | for (int capability : capabilities) { |
| 110 | assertTrue(nc.hasCapability(capability)); |
| 111 | } |
| 112 | |
| 113 | return nc; |
| 114 | } |
| 115 | |
| 116 | static void verifyUnrestrictedNetworkCapabilities(int legacyType, int transportType) { |
| 117 | verifyNetworkCapabilities( |
| 118 | legacyType, |
| 119 | transportType, |
| 120 | NET_CAPABILITY_INTERNET, |
| 121 | NET_CAPABILITY_NOT_RESTRICTED, |
| 122 | NET_CAPABILITY_NOT_VPN, |
| 123 | NET_CAPABILITY_TRUSTED); |
| 124 | } |
| 125 | |
| 126 | static void verifyRestrictedMobileNetworkCapabilities(int legacyType, int capability) { |
| 127 | final NetworkCapabilities nc = verifyNetworkCapabilities( |
| 128 | legacyType, |
| 129 | TRANSPORT_CELLULAR, |
| 130 | capability, |
| 131 | NET_CAPABILITY_NOT_VPN, |
| 132 | NET_CAPABILITY_TRUSTED); |
| 133 | |
| 134 | assertFalse(nc.hasCapability(NET_CAPABILITY_INTERNET)); |
| 135 | assertFalse(nc.hasCapability(NET_CAPABILITY_NOT_RESTRICTED)); |
| 136 | } |
| 137 | |
| 138 | @Test |
| 139 | public void testNetworkCapabilitiesForTypeMobile() { |
| 140 | verifyUnrestrictedNetworkCapabilities( |
| 141 | ConnectivityManager.TYPE_MOBILE, TRANSPORT_CELLULAR); |
| 142 | } |
| 143 | |
| 144 | @Test |
| 145 | public void testNetworkCapabilitiesForTypeMobileCbs() { |
| 146 | verifyRestrictedMobileNetworkCapabilities( |
| 147 | ConnectivityManager.TYPE_MOBILE_CBS, NET_CAPABILITY_CBS); |
| 148 | } |
| 149 | |
| 150 | @Test |
| 151 | public void testNetworkCapabilitiesForTypeMobileDun() { |
| 152 | verifyRestrictedMobileNetworkCapabilities( |
| 153 | ConnectivityManager.TYPE_MOBILE_DUN, NET_CAPABILITY_DUN); |
| 154 | } |
| 155 | |
| 156 | @Test |
| 157 | public void testNetworkCapabilitiesForTypeMobileFota() { |
| 158 | verifyRestrictedMobileNetworkCapabilities( |
| 159 | ConnectivityManager.TYPE_MOBILE_FOTA, NET_CAPABILITY_FOTA); |
| 160 | } |
| 161 | |
| 162 | @Test |
| 163 | public void testNetworkCapabilitiesForTypeMobileHipri() { |
| 164 | verifyUnrestrictedNetworkCapabilities( |
| 165 | ConnectivityManager.TYPE_MOBILE_HIPRI, TRANSPORT_CELLULAR); |
| 166 | } |
| 167 | |
| 168 | @Test |
| 169 | public void testNetworkCapabilitiesForTypeMobileIms() { |
| 170 | verifyRestrictedMobileNetworkCapabilities( |
| 171 | ConnectivityManager.TYPE_MOBILE_IMS, NET_CAPABILITY_IMS); |
| 172 | } |
| 173 | |
| 174 | @Test |
| 175 | public void testNetworkCapabilitiesForTypeMobileMms() { |
| 176 | final NetworkCapabilities nc = verifyNetworkCapabilities( |
| 177 | ConnectivityManager.TYPE_MOBILE_MMS, |
| 178 | TRANSPORT_CELLULAR, |
| 179 | NET_CAPABILITY_MMS, |
| 180 | NET_CAPABILITY_NOT_VPN, |
| 181 | NET_CAPABILITY_TRUSTED); |
| 182 | |
| 183 | assertFalse(nc.hasCapability(NET_CAPABILITY_INTERNET)); |
| 184 | } |
| 185 | |
| 186 | @Test |
| 187 | public void testNetworkCapabilitiesForTypeMobileSupl() { |
| 188 | final NetworkCapabilities nc = verifyNetworkCapabilities( |
| 189 | ConnectivityManager.TYPE_MOBILE_SUPL, |
| 190 | TRANSPORT_CELLULAR, |
| 191 | NET_CAPABILITY_SUPL, |
| 192 | NET_CAPABILITY_NOT_VPN, |
| 193 | NET_CAPABILITY_TRUSTED); |
| 194 | |
| 195 | assertFalse(nc.hasCapability(NET_CAPABILITY_INTERNET)); |
| 196 | } |
| 197 | |
| 198 | @Test |
| 199 | public void testNetworkCapabilitiesForTypeWifi() { |
| 200 | verifyUnrestrictedNetworkCapabilities( |
| 201 | ConnectivityManager.TYPE_WIFI, TRANSPORT_WIFI); |
| 202 | } |
| 203 | |
| 204 | @Test |
| 205 | public void testNetworkCapabilitiesForTypeWifiP2p() { |
| 206 | final NetworkCapabilities nc = verifyNetworkCapabilities( |
| 207 | ConnectivityManager.TYPE_WIFI_P2P, |
| 208 | TRANSPORT_WIFI, |
| 209 | NET_CAPABILITY_NOT_RESTRICTED, NET_CAPABILITY_NOT_VPN, |
| 210 | NET_CAPABILITY_TRUSTED, NET_CAPABILITY_WIFI_P2P); |
| 211 | |
| 212 | assertFalse(nc.hasCapability(NET_CAPABILITY_INTERNET)); |
| 213 | } |
| 214 | |
| 215 | @Test |
| 216 | public void testNetworkCapabilitiesForTypeBluetooth() { |
| 217 | verifyUnrestrictedNetworkCapabilities( |
| 218 | ConnectivityManager.TYPE_BLUETOOTH, TRANSPORT_BLUETOOTH); |
| 219 | } |
| 220 | |
| 221 | @Test |
| 222 | public void testNetworkCapabilitiesForTypeEthernet() { |
| 223 | verifyUnrestrictedNetworkCapabilities( |
| 224 | ConnectivityManager.TYPE_ETHERNET, TRANSPORT_ETHERNET); |
| 225 | } |
Hugo Benichi | 2aa65af | 2017-03-06 09:17:06 +0900 | [diff] [blame] | 226 | |
| 227 | @Test |
| 228 | public void testCallbackRelease() throws Exception { |
| 229 | ConnectivityManager manager = new ConnectivityManager(mCtx, mService); |
| 230 | NetworkRequest request = makeRequest(1); |
Remi NGUYEN VAN | c9c745f | 2021-06-15 16:05:56 +0900 | [diff] [blame] | 231 | NetworkCallback callback = mock(ConnectivityManager.NetworkCallback.class, |
| 232 | CALLS_REAL_METHODS); |
Hugo Benichi | 2aa65af | 2017-03-06 09:17:06 +0900 | [diff] [blame] | 233 | Handler handler = new Handler(Looper.getMainLooper()); |
| 234 | ArgumentCaptor<Messenger> captor = ArgumentCaptor.forClass(Messenger.class); |
| 235 | |
| 236 | // register callback |
Lorenzo Colitti | 2a49c5d | 2021-03-12 22:48:07 +0900 | [diff] [blame] | 237 | when(mService.requestNetwork(anyInt(), any(), anyInt(), captor.capture(), anyInt(), any(), |
| 238 | anyInt(), anyInt(), any(), nullable(String.class))).thenReturn(request); |
Hugo Benichi | 2aa65af | 2017-03-06 09:17:06 +0900 | [diff] [blame] | 239 | manager.requestNetwork(request, callback, handler); |
| 240 | |
| 241 | // callback triggers |
| 242 | captor.getValue().send(makeMessage(request, ConnectivityManager.CALLBACK_AVAILABLE)); |
Chalard Jean | 1afb2e3 | 2021-10-04 16:36:32 +0900 | [diff] [blame] | 243 | verify(callback, timeout(TIMEOUT_MS).times(1)).onAvailable(any(Network.class), |
Mike Yu | 96612ae | 2018-08-17 15:22:05 +0800 | [diff] [blame] | 244 | any(NetworkCapabilities.class), any(LinkProperties.class), anyBoolean()); |
Hugo Benichi | 2aa65af | 2017-03-06 09:17:06 +0900 | [diff] [blame] | 245 | |
| 246 | // unregister callback |
| 247 | manager.unregisterNetworkCallback(callback); |
| 248 | verify(mService, times(1)).releaseNetworkRequest(request); |
| 249 | |
| 250 | // callback does not trigger anymore. |
| 251 | captor.getValue().send(makeMessage(request, ConnectivityManager.CALLBACK_LOSING)); |
Chalard Jean | 1afb2e3 | 2021-10-04 16:36:32 +0900 | [diff] [blame] | 252 | verify(callback, after(SHORT_TIMEOUT_MS).never()).onLosing(any(), anyInt()); |
Hugo Benichi | 2aa65af | 2017-03-06 09:17:06 +0900 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | @Test |
| 256 | public void testCallbackRecycling() throws Exception { |
| 257 | ConnectivityManager manager = new ConnectivityManager(mCtx, mService); |
| 258 | NetworkRequest req1 = makeRequest(1); |
| 259 | NetworkRequest req2 = makeRequest(2); |
Remi NGUYEN VAN | c9c745f | 2021-06-15 16:05:56 +0900 | [diff] [blame] | 260 | NetworkCallback callback = mock(ConnectivityManager.NetworkCallback.class, |
| 261 | CALLS_REAL_METHODS); |
Hugo Benichi | 2aa65af | 2017-03-06 09:17:06 +0900 | [diff] [blame] | 262 | Handler handler = new Handler(Looper.getMainLooper()); |
| 263 | ArgumentCaptor<Messenger> captor = ArgumentCaptor.forClass(Messenger.class); |
| 264 | |
| 265 | // register callback |
Lorenzo Colitti | 2a49c5d | 2021-03-12 22:48:07 +0900 | [diff] [blame] | 266 | when(mService.requestNetwork(anyInt(), any(), anyInt(), captor.capture(), anyInt(), any(), |
| 267 | anyInt(), anyInt(), any(), nullable(String.class))).thenReturn(req1); |
Hugo Benichi | 2aa65af | 2017-03-06 09:17:06 +0900 | [diff] [blame] | 268 | manager.requestNetwork(req1, callback, handler); |
| 269 | |
| 270 | // callback triggers |
| 271 | captor.getValue().send(makeMessage(req1, ConnectivityManager.CALLBACK_AVAILABLE)); |
Chalard Jean | 1afb2e3 | 2021-10-04 16:36:32 +0900 | [diff] [blame] | 272 | verify(callback, timeout(TIMEOUT_MS).times(1)).onAvailable(any(Network.class), |
Mike Yu | 96612ae | 2018-08-17 15:22:05 +0800 | [diff] [blame] | 273 | any(NetworkCapabilities.class), any(LinkProperties.class), anyBoolean()); |
Hugo Benichi | 2aa65af | 2017-03-06 09:17:06 +0900 | [diff] [blame] | 274 | |
| 275 | // unregister callback |
| 276 | manager.unregisterNetworkCallback(callback); |
| 277 | verify(mService, times(1)).releaseNetworkRequest(req1); |
| 278 | |
| 279 | // callback does not trigger anymore. |
| 280 | captor.getValue().send(makeMessage(req1, ConnectivityManager.CALLBACK_LOSING)); |
Chalard Jean | 1afb2e3 | 2021-10-04 16:36:32 +0900 | [diff] [blame] | 281 | verify(callback, after(SHORT_TIMEOUT_MS).never()).onLosing(any(), anyInt()); |
Hugo Benichi | 2aa65af | 2017-03-06 09:17:06 +0900 | [diff] [blame] | 282 | |
| 283 | // callback can be registered again |
Lorenzo Colitti | 2a49c5d | 2021-03-12 22:48:07 +0900 | [diff] [blame] | 284 | when(mService.requestNetwork(anyInt(), any(), anyInt(), captor.capture(), anyInt(), any(), |
| 285 | anyInt(), anyInt(), any(), nullable(String.class))).thenReturn(req2); |
Hugo Benichi | 2aa65af | 2017-03-06 09:17:06 +0900 | [diff] [blame] | 286 | manager.requestNetwork(req2, callback, handler); |
| 287 | |
| 288 | // callback triggers |
| 289 | captor.getValue().send(makeMessage(req2, ConnectivityManager.CALLBACK_LOST)); |
Chalard Jean | 1afb2e3 | 2021-10-04 16:36:32 +0900 | [diff] [blame] | 290 | verify(callback, timeout(TIMEOUT_MS).times(1)).onLost(any()); |
Hugo Benichi | 2aa65af | 2017-03-06 09:17:06 +0900 | [diff] [blame] | 291 | |
| 292 | // unregister callback |
| 293 | manager.unregisterNetworkCallback(callback); |
| 294 | verify(mService, times(1)).releaseNetworkRequest(req2); |
| 295 | } |
| 296 | |
| 297 | // TODO: turn on this test when request callback 1:1 mapping is enforced |
| 298 | //@Test |
| 299 | private void noDoubleCallbackRegistration() throws Exception { |
| 300 | ConnectivityManager manager = new ConnectivityManager(mCtx, mService); |
| 301 | NetworkRequest request = makeRequest(1); |
| 302 | NetworkCallback callback = new ConnectivityManager.NetworkCallback(); |
| 303 | ApplicationInfo info = new ApplicationInfo(); |
| 304 | // TODO: update version when starting to enforce 1:1 mapping |
| 305 | info.targetSdkVersion = VERSION_CODES.N_MR1 + 1; |
| 306 | |
| 307 | when(mCtx.getApplicationInfo()).thenReturn(info); |
Lorenzo Colitti | 2a49c5d | 2021-03-12 22:48:07 +0900 | [diff] [blame] | 308 | when(mService.requestNetwork(anyInt(), any(), anyInt(), any(), anyInt(), any(), anyInt(), |
| 309 | anyInt(), any(), nullable(String.class))).thenReturn(request); |
Hugo Benichi | 2aa65af | 2017-03-06 09:17:06 +0900 | [diff] [blame] | 310 | |
| 311 | Handler handler = new Handler(Looper.getMainLooper()); |
| 312 | manager.requestNetwork(request, callback, handler); |
| 313 | |
| 314 | // callback is already registered, reregistration should fail. |
| 315 | Class<IllegalArgumentException> wantException = IllegalArgumentException.class; |
| 316 | expectThrowable(() -> manager.requestNetwork(request, callback), wantException); |
| 317 | |
| 318 | manager.unregisterNetworkCallback(callback); |
| 319 | verify(mService, times(1)).releaseNetworkRequest(request); |
| 320 | |
| 321 | // unregistering the callback should make it registrable again. |
| 322 | manager.requestNetwork(request, callback); |
| 323 | } |
| 324 | |
Hugo Benichi | 7abd43f | 2017-05-09 14:09:02 +0900 | [diff] [blame] | 325 | @Test |
Chiachang Wang | 859218c | 2021-09-16 14:38:52 +0800 | [diff] [blame] | 326 | public void testDefaultNetworkActiveListener() throws Exception { |
| 327 | final ConnectivityManager manager = new ConnectivityManager(mCtx, mService); |
| 328 | final ConnectivityManager.OnNetworkActiveListener listener = |
| 329 | mock(ConnectivityManager.OnNetworkActiveListener.class); |
| 330 | assertThrows(IllegalArgumentException.class, |
| 331 | () -> manager.removeDefaultNetworkActiveListener(listener)); |
| 332 | manager.addDefaultNetworkActiveListener(listener); |
| 333 | verify(mService, times(1)).registerNetworkActivityListener(any()); |
| 334 | manager.removeDefaultNetworkActiveListener(listener); |
| 335 | verify(mService, times(1)).unregisterNetworkActivityListener(any()); |
Chiachang Wang | 2de4168 | 2021-09-23 10:46:03 +0800 | [diff] [blame] | 336 | assertThrows(IllegalArgumentException.class, |
| 337 | () -> manager.removeDefaultNetworkActiveListener(listener)); |
Chiachang Wang | 859218c | 2021-09-16 14:38:52 +0800 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | @Test |
Hugo Benichi | 7abd43f | 2017-05-09 14:09:02 +0900 | [diff] [blame] | 341 | public void testArgumentValidation() throws Exception { |
| 342 | ConnectivityManager manager = new ConnectivityManager(mCtx, mService); |
| 343 | |
| 344 | NetworkRequest request = mock(NetworkRequest.class); |
| 345 | NetworkCallback callback = mock(NetworkCallback.class); |
| 346 | Handler handler = mock(Handler.class); |
| 347 | NetworkCallback nullCallback = null; |
| 348 | PendingIntent nullIntent = null; |
| 349 | |
Junyu Lai | 92c29c5 | 2021-06-22 03:03:51 +0000 | [diff] [blame] | 350 | mustFail(() -> manager.requestNetwork(null, callback)); |
| 351 | mustFail(() -> manager.requestNetwork(request, nullCallback)); |
| 352 | mustFail(() -> manager.requestNetwork(request, callback, null)); |
| 353 | mustFail(() -> manager.requestNetwork(request, callback, -1)); |
| 354 | mustFail(() -> manager.requestNetwork(request, nullIntent)); |
Hugo Benichi | 7abd43f | 2017-05-09 14:09:02 +0900 | [diff] [blame] | 355 | |
Junyu Lai | 92c29c5 | 2021-06-22 03:03:51 +0000 | [diff] [blame] | 356 | mustFail(() -> manager.requestBackgroundNetwork(null, callback, handler)); |
| 357 | mustFail(() -> manager.requestBackgroundNetwork(request, null, handler)); |
| 358 | mustFail(() -> manager.requestBackgroundNetwork(request, callback, null)); |
Hugo Benichi | 7abd43f | 2017-05-09 14:09:02 +0900 | [diff] [blame] | 359 | |
Junyu Lai | 92c29c5 | 2021-06-22 03:03:51 +0000 | [diff] [blame] | 360 | mustFail(() -> manager.registerNetworkCallback(null, callback, handler)); |
| 361 | mustFail(() -> manager.registerNetworkCallback(request, null, handler)); |
| 362 | mustFail(() -> manager.registerNetworkCallback(request, callback, null)); |
| 363 | mustFail(() -> manager.registerNetworkCallback(request, nullIntent)); |
Hugo Benichi | 7abd43f | 2017-05-09 14:09:02 +0900 | [diff] [blame] | 364 | |
Junyu Lai | 92c29c5 | 2021-06-22 03:03:51 +0000 | [diff] [blame] | 365 | mustFail(() -> manager.registerDefaultNetworkCallback(null, handler)); |
| 366 | mustFail(() -> manager.registerDefaultNetworkCallback(callback, null)); |
Lorenzo Colitti | 76b639e | 2021-01-29 20:14:04 +0900 | [diff] [blame] | 367 | |
Junyu Lai | 92c29c5 | 2021-06-22 03:03:51 +0000 | [diff] [blame] | 368 | mustFail(() -> manager.registerSystemDefaultNetworkCallback(null, handler)); |
| 369 | mustFail(() -> manager.registerSystemDefaultNetworkCallback(callback, null)); |
| 370 | |
| 371 | mustFail(() -> manager.registerBestMatchingNetworkCallback(null, callback, handler)); |
| 372 | mustFail(() -> manager.registerBestMatchingNetworkCallback(request, null, handler)); |
| 373 | mustFail(() -> manager.registerBestMatchingNetworkCallback(request, callback, null)); |
| 374 | |
| 375 | mustFail(() -> manager.unregisterNetworkCallback(nullCallback)); |
| 376 | mustFail(() -> manager.unregisterNetworkCallback(nullIntent)); |
| 377 | mustFail(() -> manager.releaseNetworkRequest(nullIntent)); |
Hugo Benichi | 7abd43f | 2017-05-09 14:09:02 +0900 | [diff] [blame] | 378 | } |
| 379 | |
| 380 | static void mustFail(Runnable fn) { |
| 381 | try { |
| 382 | fn.run(); |
| 383 | fail(); |
| 384 | } catch (Exception expected) { |
| 385 | } |
| 386 | } |
| 387 | |
junyulai | ad01079 | 2021-01-11 16:53:38 +0800 | [diff] [blame] | 388 | @Test |
| 389 | public void testRequestType() throws Exception { |
| 390 | final String testPkgName = "MyPackage"; |
Roshan Pius | aa24fde | 2020-12-17 14:53:09 -0800 | [diff] [blame] | 391 | final String testAttributionTag = "MyTag"; |
junyulai | ad01079 | 2021-01-11 16:53:38 +0800 | [diff] [blame] | 392 | final ConnectivityManager manager = new ConnectivityManager(mCtx, mService); |
| 393 | when(mCtx.getOpPackageName()).thenReturn(testPkgName); |
Roshan Pius | aa24fde | 2020-12-17 14:53:09 -0800 | [diff] [blame] | 394 | when(mCtx.getAttributionTag()).thenReturn(testAttributionTag); |
junyulai | ad01079 | 2021-01-11 16:53:38 +0800 | [diff] [blame] | 395 | final NetworkRequest request = makeRequest(1); |
| 396 | final NetworkCallback callback = new ConnectivityManager.NetworkCallback(); |
| 397 | |
| 398 | manager.requestNetwork(request, callback); |
Lorenzo Colitti | 2a49c5d | 2021-03-12 22:48:07 +0900 | [diff] [blame] | 399 | verify(mService).requestNetwork(eq(Process.INVALID_UID), eq(request.networkCapabilities), |
Roshan Pius | 951c003 | 2020-12-22 15:10:42 -0800 | [diff] [blame] | 400 | eq(REQUEST.ordinal()), any(), anyInt(), any(), eq(TYPE_NONE), anyInt(), |
Roshan Pius | aa24fde | 2020-12-17 14:53:09 -0800 | [diff] [blame] | 401 | eq(testPkgName), eq(testAttributionTag)); |
junyulai | ad01079 | 2021-01-11 16:53:38 +0800 | [diff] [blame] | 402 | reset(mService); |
| 403 | |
| 404 | // Verify that register network callback does not calls requestNetwork at all. |
| 405 | manager.registerNetworkCallback(request, callback); |
Lorenzo Colitti | 2a49c5d | 2021-03-12 22:48:07 +0900 | [diff] [blame] | 406 | verify(mService, never()).requestNetwork(anyInt(), any(), anyInt(), any(), anyInt(), any(), |
| 407 | anyInt(), anyInt(), any(), any()); |
Roshan Pius | 951c003 | 2020-12-22 15:10:42 -0800 | [diff] [blame] | 408 | verify(mService).listenForNetwork(eq(request.networkCapabilities), any(), any(), anyInt(), |
Roshan Pius | aa24fde | 2020-12-17 14:53:09 -0800 | [diff] [blame] | 409 | eq(testPkgName), eq(testAttributionTag)); |
junyulai | ad01079 | 2021-01-11 16:53:38 +0800 | [diff] [blame] | 410 | reset(mService); |
| 411 | |
Lorenzo Colitti | 2a49c5d | 2021-03-12 22:48:07 +0900 | [diff] [blame] | 412 | Handler handler = new Handler(ConnectivityThread.getInstanceLooper()); |
| 413 | |
junyulai | ad01079 | 2021-01-11 16:53:38 +0800 | [diff] [blame] | 414 | manager.registerDefaultNetworkCallback(callback); |
Lorenzo Colitti | 2a49c5d | 2021-03-12 22:48:07 +0900 | [diff] [blame] | 415 | verify(mService).requestNetwork(eq(Process.INVALID_UID), eq(null), |
Roshan Pius | 951c003 | 2020-12-22 15:10:42 -0800 | [diff] [blame] | 416 | eq(TRACK_DEFAULT.ordinal()), any(), anyInt(), any(), eq(TYPE_NONE), anyInt(), |
Roshan Pius | aa24fde | 2020-12-17 14:53:09 -0800 | [diff] [blame] | 417 | eq(testPkgName), eq(testAttributionTag)); |
junyulai | ad01079 | 2021-01-11 16:53:38 +0800 | [diff] [blame] | 418 | reset(mService); |
Junyu Lai | a62493f | 2021-01-19 11:10:56 +0000 | [diff] [blame] | 419 | |
Chiachang Wang | 6ec9b8d | 2021-04-20 15:41:24 +0800 | [diff] [blame] | 420 | manager.registerDefaultNetworkCallbackForUid(42, callback, handler); |
Lorenzo Colitti | 2a49c5d | 2021-03-12 22:48:07 +0900 | [diff] [blame] | 421 | verify(mService).requestNetwork(eq(42), eq(null), |
| 422 | eq(TRACK_DEFAULT.ordinal()), any(), anyInt(), any(), eq(TYPE_NONE), anyInt(), |
| 423 | eq(testPkgName), eq(testAttributionTag)); |
| 424 | |
junyulai | 05738ae | 2021-04-15 00:39:49 +0800 | [diff] [blame] | 425 | manager.requestBackgroundNetwork(request, callback, handler); |
Lorenzo Colitti | 2a49c5d | 2021-03-12 22:48:07 +0900 | [diff] [blame] | 426 | verify(mService).requestNetwork(eq(Process.INVALID_UID), eq(request.networkCapabilities), |
Roshan Pius | 951c003 | 2020-12-22 15:10:42 -0800 | [diff] [blame] | 427 | eq(BACKGROUND_REQUEST.ordinal()), any(), anyInt(), any(), eq(TYPE_NONE), anyInt(), |
Roshan Pius | aa24fde | 2020-12-17 14:53:09 -0800 | [diff] [blame] | 428 | eq(testPkgName), eq(testAttributionTag)); |
Junyu Lai | a62493f | 2021-01-19 11:10:56 +0000 | [diff] [blame] | 429 | reset(mService); |
Lorenzo Colitti | 76b639e | 2021-01-29 20:14:04 +0900 | [diff] [blame] | 430 | |
Lorenzo Colitti | 76b639e | 2021-01-29 20:14:04 +0900 | [diff] [blame] | 431 | manager.registerSystemDefaultNetworkCallback(callback, handler); |
Lorenzo Colitti | 2a49c5d | 2021-03-12 22:48:07 +0900 | [diff] [blame] | 432 | verify(mService).requestNetwork(eq(Process.INVALID_UID), eq(null), |
Roshan Pius | 951c003 | 2020-12-22 15:10:42 -0800 | [diff] [blame] | 433 | eq(TRACK_SYSTEM_DEFAULT.ordinal()), any(), anyInt(), any(), eq(TYPE_NONE), anyInt(), |
Lorenzo Colitti | 76b639e | 2021-01-29 20:14:04 +0900 | [diff] [blame] | 434 | eq(testPkgName), eq(testAttributionTag)); |
| 435 | reset(mService); |
junyulai | ad01079 | 2021-01-11 16:53:38 +0800 | [diff] [blame] | 436 | } |
| 437 | |
Hugo Benichi | 2aa65af | 2017-03-06 09:17:06 +0900 | [diff] [blame] | 438 | static Message makeMessage(NetworkRequest req, int messageType) { |
| 439 | Bundle bundle = new Bundle(); |
| 440 | bundle.putParcelable(NetworkRequest.class.getSimpleName(), req); |
Chalard Jean | a23bc9e | 2018-01-30 22:41:41 +0900 | [diff] [blame] | 441 | // Pass default objects as we don't care which get passed here |
| 442 | bundle.putParcelable(Network.class.getSimpleName(), new Network(1)); |
| 443 | bundle.putParcelable(NetworkCapabilities.class.getSimpleName(), new NetworkCapabilities()); |
| 444 | bundle.putParcelable(LinkProperties.class.getSimpleName(), new LinkProperties()); |
Hugo Benichi | 2aa65af | 2017-03-06 09:17:06 +0900 | [diff] [blame] | 445 | Message msg = Message.obtain(); |
| 446 | msg.what = messageType; |
| 447 | msg.setData(bundle); |
| 448 | return msg; |
| 449 | } |
| 450 | |
| 451 | static NetworkRequest makeRequest(int requestId) { |
| 452 | NetworkRequest request = new NetworkRequest.Builder().clearCapabilities().build(); |
| 453 | return new NetworkRequest(request.networkCapabilities, ConnectivityManager.TYPE_NONE, |
| 454 | requestId, NetworkRequest.Type.NONE); |
| 455 | } |
| 456 | |
| 457 | static void expectThrowable(Runnable block, Class<? extends Throwable> throwableType) { |
| 458 | try { |
| 459 | block.run(); |
| 460 | } catch (Throwable t) { |
| 461 | if (t.getClass().equals(throwableType)) { |
| 462 | return; |
| 463 | } |
| 464 | fail("expected exception of type " + throwableType + ", but was " + t.getClass()); |
| 465 | } |
| 466 | fail("expected exception of type " + throwableType); |
| 467 | } |
markchien | ed0fab0 | 2022-04-26 16:33:47 +0800 | [diff] [blame] | 468 | |
| 469 | private static class MockContext extends BroadcastInterceptingContext { |
| 470 | MockContext(Context base) { |
| 471 | super(base); |
| 472 | } |
| 473 | |
| 474 | @Override |
| 475 | public Context getApplicationContext() { |
| 476 | return mock(Context.class); |
| 477 | } |
| 478 | } |
| 479 | |
| 480 | private WeakReference<Context> makeConnectivityManagerAndReturnContext() { |
| 481 | // Mockito may have an internal reference to the mock, creating MockContext for testing. |
| 482 | final Context c = new MockContext(mock(Context.class)); |
| 483 | |
| 484 | new ConnectivityManager(c, mService); |
| 485 | |
| 486 | return new WeakReference<>(c); |
| 487 | } |
| 488 | |
| 489 | private void forceGC() { |
| 490 | // First GC ensures that objects are collected for finalization, then second GC ensures |
| 491 | // they're garbage-collected after being finalized. |
| 492 | System.gc(); |
| 493 | System.runFinalization(); |
| 494 | System.gc(); |
| 495 | } |
| 496 | |
| 497 | @Test |
| 498 | public void testConnectivityManagerDoesNotLeakContext() throws Exception { |
| 499 | final WeakReference<Context> ref = makeConnectivityManagerAndReturnContext(); |
| 500 | |
Mark Chien | 82eb31b | 2022-12-06 05:34:50 +0000 | [diff] [blame] | 501 | final int attempts = 600; |
markchien | ed0fab0 | 2022-04-26 16:33:47 +0800 | [diff] [blame] | 502 | final long waitIntervalMs = 50; |
| 503 | for (int i = 0; i < attempts; i++) { |
| 504 | forceGC(); |
| 505 | if (ref.get() == null) break; |
| 506 | |
| 507 | Thread.sleep(waitIntervalMs); |
| 508 | } |
| 509 | |
markchien | d201566 | 2022-04-26 18:08:03 +0800 | [diff] [blame] | 510 | assertNull("ConnectivityManager weak reference still not null after " + attempts |
| 511 | + " attempts", ref.get()); |
markchien | ed0fab0 | 2022-04-26 16:33:47 +0800 | [diff] [blame] | 512 | } |
Erik Kline | ce55eb1 | 2017-01-26 18:08:28 +0900 | [diff] [blame] | 513 | } |