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