Remi NGUYEN VAN | 678277c | 2021-05-11 13:37:06 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2020 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 | |
| 19 | import static android.net.ConnectivityDiagnosticsManager.ConnectivityDiagnosticsBinder; |
| 20 | import static android.net.ConnectivityDiagnosticsManager.ConnectivityDiagnosticsCallback; |
| 21 | import static android.net.ConnectivityDiagnosticsManager.ConnectivityReport; |
| 22 | import static android.net.ConnectivityDiagnosticsManager.DataStallReport; |
| 23 | |
| 24 | import static com.android.testutils.ParcelUtils.assertParcelSane; |
| 25 | |
| 26 | import static org.junit.Assert.assertEquals; |
| 27 | import static org.junit.Assert.assertFalse; |
| 28 | import static org.junit.Assert.assertNotEquals; |
| 29 | import static org.junit.Assert.assertTrue; |
| 30 | import static org.junit.Assert.fail; |
| 31 | import static org.mockito.ArgumentMatchers.any; |
| 32 | import static org.mockito.ArgumentMatchers.eq; |
| 33 | import static org.mockito.Mockito.mock; |
| 34 | import static org.mockito.Mockito.times; |
| 35 | import static org.mockito.Mockito.verify; |
| 36 | import static org.mockito.Mockito.verifyNoMoreInteractions; |
| 37 | |
| 38 | import android.content.Context; |
| 39 | import android.os.PersistableBundle; |
| 40 | |
| 41 | import androidx.test.InstrumentationRegistry; |
| 42 | |
| 43 | import org.junit.After; |
| 44 | import org.junit.Before; |
| 45 | import org.junit.Test; |
| 46 | import org.junit.runner.RunWith; |
| 47 | import org.junit.runners.JUnit4; |
| 48 | import org.mockito.Mock; |
| 49 | |
| 50 | import java.util.concurrent.Executor; |
| 51 | |
| 52 | @RunWith(JUnit4.class) |
| 53 | public class ConnectivityDiagnosticsManagerTest { |
| 54 | private static final int NET_ID = 1; |
| 55 | private static final int DETECTION_METHOD = 2; |
| 56 | private static final long TIMESTAMP = 10L; |
| 57 | private static final String INTERFACE_NAME = "interface"; |
| 58 | private static final String BUNDLE_KEY = "key"; |
| 59 | private static final String BUNDLE_VALUE = "value"; |
| 60 | |
| 61 | private static final Executor INLINE_EXECUTOR = x -> x.run(); |
| 62 | |
| 63 | @Mock private IConnectivityManager mService; |
| 64 | @Mock private ConnectivityDiagnosticsCallback mCb; |
| 65 | |
| 66 | private Context mContext; |
| 67 | private ConnectivityDiagnosticsBinder mBinder; |
| 68 | private ConnectivityDiagnosticsManager mManager; |
| 69 | |
| 70 | private String mPackageName; |
| 71 | |
| 72 | @Before |
| 73 | public void setUp() { |
| 74 | mContext = InstrumentationRegistry.getContext(); |
| 75 | |
| 76 | mService = mock(IConnectivityManager.class); |
| 77 | mCb = mock(ConnectivityDiagnosticsCallback.class); |
| 78 | |
| 79 | mBinder = new ConnectivityDiagnosticsBinder(mCb, INLINE_EXECUTOR); |
| 80 | mManager = new ConnectivityDiagnosticsManager(mContext, mService); |
| 81 | |
| 82 | mPackageName = mContext.getOpPackageName(); |
| 83 | } |
| 84 | |
| 85 | @After |
| 86 | public void tearDown() { |
| 87 | // clear ConnectivityDiagnosticsManager callbacks map |
| 88 | ConnectivityDiagnosticsManager.sCallbacks.clear(); |
| 89 | } |
| 90 | |
| 91 | private ConnectivityReport createSampleConnectivityReport() { |
| 92 | final LinkProperties linkProperties = new LinkProperties(); |
| 93 | linkProperties.setInterfaceName(INTERFACE_NAME); |
| 94 | |
| 95 | final NetworkCapabilities networkCapabilities = new NetworkCapabilities(); |
| 96 | networkCapabilities.addCapability(NetworkCapabilities.NET_CAPABILITY_IMS); |
| 97 | |
| 98 | final PersistableBundle bundle = new PersistableBundle(); |
| 99 | bundle.putString(BUNDLE_KEY, BUNDLE_VALUE); |
| 100 | |
| 101 | return new ConnectivityReport( |
| 102 | new Network(NET_ID), TIMESTAMP, linkProperties, networkCapabilities, bundle); |
| 103 | } |
| 104 | |
| 105 | private ConnectivityReport createDefaultConnectivityReport() { |
| 106 | return new ConnectivityReport( |
| 107 | new Network(0), |
| 108 | 0L, |
| 109 | new LinkProperties(), |
| 110 | new NetworkCapabilities(), |
| 111 | PersistableBundle.EMPTY); |
| 112 | } |
| 113 | |
| 114 | @Test |
| 115 | public void testPersistableBundleEquals() { |
| 116 | assertFalse( |
| 117 | ConnectivityDiagnosticsManager.persistableBundleEquals( |
| 118 | null, PersistableBundle.EMPTY)); |
| 119 | assertFalse( |
| 120 | ConnectivityDiagnosticsManager.persistableBundleEquals( |
| 121 | PersistableBundle.EMPTY, null)); |
| 122 | assertTrue( |
| 123 | ConnectivityDiagnosticsManager.persistableBundleEquals( |
| 124 | PersistableBundle.EMPTY, PersistableBundle.EMPTY)); |
| 125 | |
| 126 | final PersistableBundle a = new PersistableBundle(); |
| 127 | a.putString(BUNDLE_KEY, BUNDLE_VALUE); |
| 128 | |
| 129 | final PersistableBundle b = new PersistableBundle(); |
| 130 | b.putString(BUNDLE_KEY, BUNDLE_VALUE); |
| 131 | |
| 132 | final PersistableBundle c = new PersistableBundle(); |
| 133 | c.putString(BUNDLE_KEY, null); |
| 134 | |
| 135 | assertFalse( |
| 136 | ConnectivityDiagnosticsManager.persistableBundleEquals(PersistableBundle.EMPTY, a)); |
| 137 | assertFalse( |
| 138 | ConnectivityDiagnosticsManager.persistableBundleEquals(a, PersistableBundle.EMPTY)); |
| 139 | |
| 140 | assertTrue(ConnectivityDiagnosticsManager.persistableBundleEquals(a, b)); |
| 141 | assertTrue(ConnectivityDiagnosticsManager.persistableBundleEquals(b, a)); |
| 142 | |
| 143 | assertFalse(ConnectivityDiagnosticsManager.persistableBundleEquals(a, c)); |
| 144 | assertFalse(ConnectivityDiagnosticsManager.persistableBundleEquals(c, a)); |
| 145 | } |
| 146 | |
| 147 | @Test |
| 148 | public void testConnectivityReportEquals() { |
| 149 | final ConnectivityReport defaultReport = createDefaultConnectivityReport(); |
| 150 | final ConnectivityReport sampleReport = createSampleConnectivityReport(); |
| 151 | assertEquals(sampleReport, createSampleConnectivityReport()); |
| 152 | assertEquals(defaultReport, createDefaultConnectivityReport()); |
| 153 | |
| 154 | final LinkProperties linkProperties = sampleReport.getLinkProperties(); |
| 155 | final NetworkCapabilities networkCapabilities = sampleReport.getNetworkCapabilities(); |
| 156 | final PersistableBundle bundle = sampleReport.getAdditionalInfo(); |
| 157 | |
| 158 | assertNotEquals( |
| 159 | createDefaultConnectivityReport(), |
| 160 | new ConnectivityReport( |
| 161 | new Network(NET_ID), |
| 162 | 0L, |
| 163 | new LinkProperties(), |
| 164 | new NetworkCapabilities(), |
| 165 | PersistableBundle.EMPTY)); |
| 166 | assertNotEquals( |
| 167 | createDefaultConnectivityReport(), |
| 168 | new ConnectivityReport( |
| 169 | new Network(0), |
| 170 | TIMESTAMP, |
| 171 | new LinkProperties(), |
| 172 | new NetworkCapabilities(), |
| 173 | PersistableBundle.EMPTY)); |
| 174 | assertNotEquals( |
| 175 | createDefaultConnectivityReport(), |
| 176 | new ConnectivityReport( |
| 177 | new Network(0), |
| 178 | 0L, |
| 179 | linkProperties, |
| 180 | new NetworkCapabilities(), |
| 181 | PersistableBundle.EMPTY)); |
| 182 | assertNotEquals( |
| 183 | createDefaultConnectivityReport(), |
| 184 | new ConnectivityReport( |
| 185 | new Network(0), |
| 186 | TIMESTAMP, |
| 187 | new LinkProperties(), |
| 188 | networkCapabilities, |
| 189 | PersistableBundle.EMPTY)); |
| 190 | assertNotEquals( |
| 191 | createDefaultConnectivityReport(), |
| 192 | new ConnectivityReport( |
| 193 | new Network(0), |
| 194 | TIMESTAMP, |
| 195 | new LinkProperties(), |
| 196 | new NetworkCapabilities(), |
| 197 | bundle)); |
| 198 | } |
| 199 | |
| 200 | @Test |
| 201 | public void testConnectivityReportParcelUnparcel() { |
| 202 | assertParcelSane(createSampleConnectivityReport(), 5); |
| 203 | } |
| 204 | |
| 205 | private DataStallReport createSampleDataStallReport() { |
| 206 | final LinkProperties linkProperties = new LinkProperties(); |
| 207 | linkProperties.setInterfaceName(INTERFACE_NAME); |
| 208 | |
| 209 | final PersistableBundle bundle = new PersistableBundle(); |
| 210 | bundle.putString(BUNDLE_KEY, BUNDLE_VALUE); |
| 211 | |
| 212 | final NetworkCapabilities networkCapabilities = new NetworkCapabilities(); |
| 213 | networkCapabilities.addCapability(NetworkCapabilities.NET_CAPABILITY_IMS); |
| 214 | |
| 215 | return new DataStallReport( |
| 216 | new Network(NET_ID), |
| 217 | TIMESTAMP, |
| 218 | DETECTION_METHOD, |
| 219 | linkProperties, |
| 220 | networkCapabilities, |
| 221 | bundle); |
| 222 | } |
| 223 | |
| 224 | private DataStallReport createDefaultDataStallReport() { |
| 225 | return new DataStallReport( |
| 226 | new Network(0), |
| 227 | 0L, |
| 228 | 0, |
| 229 | new LinkProperties(), |
| 230 | new NetworkCapabilities(), |
| 231 | PersistableBundle.EMPTY); |
| 232 | } |
| 233 | |
| 234 | @Test |
| 235 | public void testDataStallReportEquals() { |
| 236 | final DataStallReport defaultReport = createDefaultDataStallReport(); |
| 237 | final DataStallReport sampleReport = createSampleDataStallReport(); |
| 238 | assertEquals(sampleReport, createSampleDataStallReport()); |
| 239 | assertEquals(defaultReport, createDefaultDataStallReport()); |
| 240 | |
| 241 | final LinkProperties linkProperties = sampleReport.getLinkProperties(); |
| 242 | final NetworkCapabilities networkCapabilities = sampleReport.getNetworkCapabilities(); |
| 243 | final PersistableBundle bundle = sampleReport.getStallDetails(); |
| 244 | |
| 245 | assertNotEquals( |
| 246 | defaultReport, |
| 247 | new DataStallReport( |
| 248 | new Network(NET_ID), |
| 249 | 0L, |
| 250 | 0, |
| 251 | new LinkProperties(), |
| 252 | new NetworkCapabilities(), |
| 253 | PersistableBundle.EMPTY)); |
| 254 | assertNotEquals( |
| 255 | defaultReport, |
| 256 | new DataStallReport( |
| 257 | new Network(0), |
| 258 | TIMESTAMP, |
| 259 | 0, |
| 260 | new LinkProperties(), |
| 261 | new NetworkCapabilities(), |
| 262 | PersistableBundle.EMPTY)); |
| 263 | assertNotEquals( |
| 264 | defaultReport, |
| 265 | new DataStallReport( |
| 266 | new Network(0), |
| 267 | 0L, |
| 268 | DETECTION_METHOD, |
| 269 | new LinkProperties(), |
| 270 | new NetworkCapabilities(), |
| 271 | PersistableBundle.EMPTY)); |
| 272 | assertNotEquals( |
| 273 | defaultReport, |
| 274 | new DataStallReport( |
| 275 | new Network(0), |
| 276 | 0L, |
| 277 | 0, |
| 278 | linkProperties, |
| 279 | new NetworkCapabilities(), |
| 280 | PersistableBundle.EMPTY)); |
| 281 | assertNotEquals( |
| 282 | defaultReport, |
| 283 | new DataStallReport( |
| 284 | new Network(0), |
| 285 | 0L, |
| 286 | 0, |
| 287 | new LinkProperties(), |
| 288 | networkCapabilities, |
| 289 | PersistableBundle.EMPTY)); |
| 290 | assertNotEquals( |
| 291 | defaultReport, |
| 292 | new DataStallReport( |
| 293 | new Network(0), |
| 294 | 0L, |
| 295 | 0, |
| 296 | new LinkProperties(), |
| 297 | new NetworkCapabilities(), |
| 298 | bundle)); |
| 299 | } |
| 300 | |
| 301 | @Test |
| 302 | public void testDataStallReportParcelUnparcel() { |
| 303 | assertParcelSane(createSampleDataStallReport(), 6); |
| 304 | } |
| 305 | |
| 306 | @Test |
| 307 | public void testConnectivityDiagnosticsCallbackOnConnectivityReportAvailable() { |
| 308 | mBinder.onConnectivityReportAvailable(createSampleConnectivityReport()); |
| 309 | |
| 310 | // The callback will be invoked synchronously by inline executor. Immediately check the |
| 311 | // latch without waiting. |
| 312 | verify(mCb).onConnectivityReportAvailable(eq(createSampleConnectivityReport())); |
| 313 | } |
| 314 | |
| 315 | @Test |
| 316 | public void testConnectivityDiagnosticsCallbackOnDataStallSuspected() { |
| 317 | mBinder.onDataStallSuspected(createSampleDataStallReport()); |
| 318 | |
| 319 | // The callback will be invoked synchronously by inline executor. Immediately check the |
| 320 | // latch without waiting. |
| 321 | verify(mCb).onDataStallSuspected(eq(createSampleDataStallReport())); |
| 322 | } |
| 323 | |
| 324 | @Test |
| 325 | public void testConnectivityDiagnosticsCallbackOnNetworkConnectivityReported() { |
| 326 | final Network n = new Network(NET_ID); |
| 327 | final boolean connectivity = true; |
| 328 | |
| 329 | mBinder.onNetworkConnectivityReported(n, connectivity); |
| 330 | |
| 331 | // The callback will be invoked synchronously by inline executor. Immediately check the |
| 332 | // latch without waiting. |
| 333 | verify(mCb).onNetworkConnectivityReported(eq(n), eq(connectivity)); |
| 334 | } |
| 335 | |
| 336 | @Test |
| 337 | public void testRegisterConnectivityDiagnosticsCallback() throws Exception { |
| 338 | final NetworkRequest request = new NetworkRequest.Builder().build(); |
| 339 | |
| 340 | mManager.registerConnectivityDiagnosticsCallback(request, INLINE_EXECUTOR, mCb); |
| 341 | |
| 342 | verify(mService).registerConnectivityDiagnosticsCallback( |
| 343 | any(ConnectivityDiagnosticsBinder.class), eq(request), eq(mPackageName)); |
| 344 | assertTrue(ConnectivityDiagnosticsManager.sCallbacks.containsKey(mCb)); |
| 345 | } |
| 346 | |
| 347 | @Test |
| 348 | public void testRegisterDuplicateConnectivityDiagnosticsCallback() throws Exception { |
| 349 | final NetworkRequest request = new NetworkRequest.Builder().build(); |
| 350 | |
| 351 | mManager.registerConnectivityDiagnosticsCallback(request, INLINE_EXECUTOR, mCb); |
| 352 | |
| 353 | try { |
| 354 | mManager.registerConnectivityDiagnosticsCallback(request, INLINE_EXECUTOR, mCb); |
| 355 | fail("Duplicate callback registration should fail"); |
| 356 | } catch (IllegalArgumentException expected) { |
| 357 | } |
| 358 | } |
| 359 | |
| 360 | @Test |
| 361 | public void testUnregisterConnectivityDiagnosticsCallback() throws Exception { |
| 362 | final NetworkRequest request = new NetworkRequest.Builder().build(); |
| 363 | mManager.registerConnectivityDiagnosticsCallback(request, INLINE_EXECUTOR, mCb); |
| 364 | |
| 365 | mManager.unregisterConnectivityDiagnosticsCallback(mCb); |
| 366 | |
| 367 | verify(mService).unregisterConnectivityDiagnosticsCallback( |
| 368 | any(ConnectivityDiagnosticsBinder.class)); |
| 369 | assertFalse(ConnectivityDiagnosticsManager.sCallbacks.containsKey(mCb)); |
| 370 | |
| 371 | // verify that re-registering is successful |
| 372 | mManager.registerConnectivityDiagnosticsCallback(request, INLINE_EXECUTOR, mCb); |
| 373 | verify(mService, times(2)).registerConnectivityDiagnosticsCallback( |
| 374 | any(ConnectivityDiagnosticsBinder.class), eq(request), eq(mPackageName)); |
| 375 | assertTrue(ConnectivityDiagnosticsManager.sCallbacks.containsKey(mCb)); |
| 376 | } |
| 377 | |
| 378 | @Test |
| 379 | public void testUnregisterUnknownConnectivityDiagnosticsCallback() throws Exception { |
| 380 | mManager.unregisterConnectivityDiagnosticsCallback(mCb); |
| 381 | |
| 382 | verifyNoMoreInteractions(mService); |
| 383 | } |
| 384 | } |