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