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