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