Cody Kesting | beb41b5 | 2019-12-17 08:51:32 -0800 | [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 | |
Cody Kesting | 3e7fb38 | 2019-12-17 16:46:11 -0800 | [diff] [blame] | 19 | import static android.net.ConnectivityDiagnosticsManager.ConnectivityDiagnosticsBinder; |
| 20 | import static android.net.ConnectivityDiagnosticsManager.ConnectivityDiagnosticsCallback; |
Cody Kesting | beb41b5 | 2019-12-17 08:51:32 -0800 | [diff] [blame] | 21 | import static android.net.ConnectivityDiagnosticsManager.ConnectivityReport; |
Cody Kesting | 3d97b5e | 2019-12-17 09:28:06 -0800 | [diff] [blame] | 22 | import static android.net.ConnectivityDiagnosticsManager.DataStallReport; |
Cody Kesting | beb41b5 | 2019-12-17 08:51:32 -0800 | [diff] [blame] | 23 | |
| 24 | import static com.android.testutils.ParcelUtilsKt.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; |
Cody Kesting | 73708bf | 2019-12-18 10:57:50 -0800 | [diff] [blame] | 30 | import static org.junit.Assert.fail; |
| 31 | import static org.mockito.ArgumentMatchers.any; |
Cody Kesting | 3e7fb38 | 2019-12-17 16:46:11 -0800 | [diff] [blame] | 32 | import static org.mockito.ArgumentMatchers.eq; |
| 33 | import static org.mockito.Mockito.mock; |
Cody Kesting | 73708bf | 2019-12-18 10:57:50 -0800 | [diff] [blame] | 34 | import static org.mockito.Mockito.times; |
Cody Kesting | 3e7fb38 | 2019-12-17 16:46:11 -0800 | [diff] [blame] | 35 | import static org.mockito.Mockito.verify; |
Cody Kesting | 73708bf | 2019-12-18 10:57:50 -0800 | [diff] [blame] | 36 | import static org.mockito.Mockito.verifyNoMoreInteractions; |
Cody Kesting | beb41b5 | 2019-12-17 08:51:32 -0800 | [diff] [blame] | 37 | |
Cody Kesting | 73708bf | 2019-12-18 10:57:50 -0800 | [diff] [blame] | 38 | import android.content.Context; |
Cody Kesting | beb41b5 | 2019-12-17 08:51:32 -0800 | [diff] [blame] | 39 | import android.os.PersistableBundle; |
| 40 | |
Cody Kesting | 83bb5fa | 2020-01-05 14:06:39 -0800 | [diff] [blame^] | 41 | import androidx.test.InstrumentationRegistry; |
| 42 | |
Cody Kesting | 73708bf | 2019-12-18 10:57:50 -0800 | [diff] [blame] | 43 | import org.junit.After; |
Cody Kesting | 3e7fb38 | 2019-12-17 16:46:11 -0800 | [diff] [blame] | 44 | import org.junit.Before; |
Cody Kesting | beb41b5 | 2019-12-17 08:51:32 -0800 | [diff] [blame] | 45 | import org.junit.Test; |
| 46 | import org.junit.runner.RunWith; |
| 47 | import org.junit.runners.JUnit4; |
Cody Kesting | 3e7fb38 | 2019-12-17 16:46:11 -0800 | [diff] [blame] | 48 | import org.mockito.Mock; |
| 49 | |
| 50 | import java.util.concurrent.Executor; |
Cody Kesting | beb41b5 | 2019-12-17 08:51:32 -0800 | [diff] [blame] | 51 | |
| 52 | @RunWith(JUnit4.class) |
| 53 | public class ConnectivityDiagnosticsManagerTest { |
| 54 | private static final int NET_ID = 1; |
Cody Kesting | 3d97b5e | 2019-12-17 09:28:06 -0800 | [diff] [blame] | 55 | private static final int DETECTION_METHOD = 2; |
Cody Kesting | beb41b5 | 2019-12-17 08:51:32 -0800 | [diff] [blame] | 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 | |
Cody Kesting | 3e7fb38 | 2019-12-17 16:46:11 -0800 | [diff] [blame] | 61 | private static final Executor INLINE_EXECUTOR = x -> x.run(); |
| 62 | |
Cody Kesting | 73708bf | 2019-12-18 10:57:50 -0800 | [diff] [blame] | 63 | @Mock private IConnectivityManager mService; |
Cody Kesting | 3e7fb38 | 2019-12-17 16:46:11 -0800 | [diff] [blame] | 64 | @Mock private ConnectivityDiagnosticsCallback mCb; |
| 65 | |
Cody Kesting | 83bb5fa | 2020-01-05 14:06:39 -0800 | [diff] [blame^] | 66 | private Context mContext; |
Cody Kesting | 3e7fb38 | 2019-12-17 16:46:11 -0800 | [diff] [blame] | 67 | private ConnectivityDiagnosticsBinder mBinder; |
Cody Kesting | 73708bf | 2019-12-18 10:57:50 -0800 | [diff] [blame] | 68 | private ConnectivityDiagnosticsManager mManager; |
Cody Kesting | 3e7fb38 | 2019-12-17 16:46:11 -0800 | [diff] [blame] | 69 | |
Cody Kesting | 83bb5fa | 2020-01-05 14:06:39 -0800 | [diff] [blame^] | 70 | private String mPackageName; |
| 71 | |
Cody Kesting | 3e7fb38 | 2019-12-17 16:46:11 -0800 | [diff] [blame] | 72 | @Before |
| 73 | public void setUp() { |
Cody Kesting | 83bb5fa | 2020-01-05 14:06:39 -0800 | [diff] [blame^] | 74 | mContext = InstrumentationRegistry.getContext(); |
| 75 | |
Cody Kesting | 73708bf | 2019-12-18 10:57:50 -0800 | [diff] [blame] | 76 | mService = mock(IConnectivityManager.class); |
Cody Kesting | 3e7fb38 | 2019-12-17 16:46:11 -0800 | [diff] [blame] | 77 | mCb = mock(ConnectivityDiagnosticsCallback.class); |
| 78 | |
| 79 | mBinder = new ConnectivityDiagnosticsBinder(mCb, INLINE_EXECUTOR); |
Cody Kesting | 73708bf | 2019-12-18 10:57:50 -0800 | [diff] [blame] | 80 | mManager = new ConnectivityDiagnosticsManager(mContext, mService); |
Cody Kesting | 83bb5fa | 2020-01-05 14:06:39 -0800 | [diff] [blame^] | 81 | |
| 82 | mPackageName = mContext.getOpPackageName(); |
Cody Kesting | 73708bf | 2019-12-18 10:57:50 -0800 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | @After |
| 86 | public void tearDown() { |
| 87 | // clear ConnectivityDiagnosticsManager callbacks map |
| 88 | ConnectivityDiagnosticsManager.sCallbacks.clear(); |
Cody Kesting | 3e7fb38 | 2019-12-17 16:46:11 -0800 | [diff] [blame] | 89 | } |
| 90 | |
Cody Kesting | beb41b5 | 2019-12-17 08:51:32 -0800 | [diff] [blame] | 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 | assertEquals(createSampleConnectivityReport(), createSampleConnectivityReport()); |
| 150 | assertEquals(createDefaultConnectivityReport(), createDefaultConnectivityReport()); |
| 151 | |
| 152 | final LinkProperties linkProperties = new LinkProperties(); |
| 153 | linkProperties.setInterfaceName(INTERFACE_NAME); |
| 154 | |
| 155 | final NetworkCapabilities networkCapabilities = new NetworkCapabilities(); |
| 156 | networkCapabilities.addCapability(NetworkCapabilities.NET_CAPABILITY_IMS); |
| 157 | |
| 158 | final PersistableBundle bundle = new PersistableBundle(); |
| 159 | bundle.putString(BUNDLE_KEY, BUNDLE_VALUE); |
| 160 | |
| 161 | assertNotEquals( |
| 162 | createDefaultConnectivityReport(), |
| 163 | new ConnectivityReport( |
| 164 | new Network(NET_ID), |
| 165 | 0L, |
| 166 | new LinkProperties(), |
| 167 | new NetworkCapabilities(), |
| 168 | PersistableBundle.EMPTY)); |
| 169 | assertNotEquals( |
| 170 | createDefaultConnectivityReport(), |
| 171 | new ConnectivityReport( |
| 172 | new Network(0), |
| 173 | TIMESTAMP, |
| 174 | new LinkProperties(), |
| 175 | new NetworkCapabilities(), |
| 176 | PersistableBundle.EMPTY)); |
| 177 | assertNotEquals( |
| 178 | createDefaultConnectivityReport(), |
| 179 | new ConnectivityReport( |
| 180 | new Network(0), |
| 181 | 0L, |
| 182 | linkProperties, |
| 183 | new NetworkCapabilities(), |
| 184 | PersistableBundle.EMPTY)); |
| 185 | assertNotEquals( |
| 186 | createDefaultConnectivityReport(), |
| 187 | new ConnectivityReport( |
| 188 | new Network(0), |
| 189 | TIMESTAMP, |
| 190 | new LinkProperties(), |
| 191 | networkCapabilities, |
| 192 | PersistableBundle.EMPTY)); |
| 193 | assertNotEquals( |
| 194 | createDefaultConnectivityReport(), |
| 195 | new ConnectivityReport( |
| 196 | new Network(0), |
| 197 | TIMESTAMP, |
| 198 | new LinkProperties(), |
| 199 | new NetworkCapabilities(), |
| 200 | bundle)); |
| 201 | } |
| 202 | |
| 203 | @Test |
| 204 | public void testConnectivityReportParcelUnparcel() { |
| 205 | assertParcelSane(createSampleConnectivityReport(), 5); |
| 206 | } |
Cody Kesting | 3d97b5e | 2019-12-17 09:28:06 -0800 | [diff] [blame] | 207 | |
| 208 | private DataStallReport createSampleDataStallReport() { |
| 209 | final PersistableBundle bundle = new PersistableBundle(); |
| 210 | bundle.putString(BUNDLE_KEY, BUNDLE_VALUE); |
| 211 | return new DataStallReport(new Network(NET_ID), TIMESTAMP, DETECTION_METHOD, bundle); |
| 212 | } |
| 213 | |
| 214 | private DataStallReport createDefaultDataStallReport() { |
| 215 | return new DataStallReport(new Network(0), 0L, 0, PersistableBundle.EMPTY); |
| 216 | } |
| 217 | |
| 218 | @Test |
| 219 | public void testDataStallReportEquals() { |
| 220 | assertEquals(createSampleDataStallReport(), createSampleDataStallReport()); |
| 221 | assertEquals(createDefaultDataStallReport(), createDefaultDataStallReport()); |
| 222 | |
| 223 | final PersistableBundle bundle = new PersistableBundle(); |
| 224 | bundle.putString(BUNDLE_KEY, BUNDLE_VALUE); |
| 225 | |
| 226 | assertNotEquals( |
| 227 | createDefaultDataStallReport(), |
| 228 | new DataStallReport(new Network(NET_ID), 0L, 0, PersistableBundle.EMPTY)); |
| 229 | assertNotEquals( |
| 230 | createDefaultDataStallReport(), |
| 231 | new DataStallReport(new Network(0), TIMESTAMP, 0, PersistableBundle.EMPTY)); |
| 232 | assertNotEquals( |
| 233 | createDefaultDataStallReport(), |
| 234 | new DataStallReport(new Network(0), 0L, DETECTION_METHOD, PersistableBundle.EMPTY)); |
| 235 | assertNotEquals( |
| 236 | createDefaultDataStallReport(), new DataStallReport(new Network(0), 0L, 0, bundle)); |
| 237 | } |
| 238 | |
| 239 | @Test |
| 240 | public void testDataStallReportParcelUnparcel() { |
| 241 | assertParcelSane(createSampleDataStallReport(), 4); |
| 242 | } |
Cody Kesting | 3e7fb38 | 2019-12-17 16:46:11 -0800 | [diff] [blame] | 243 | |
| 244 | @Test |
| 245 | public void testConnectivityDiagnosticsCallbackOnConnectivityReport() { |
| 246 | mBinder.onConnectivityReport(createSampleConnectivityReport()); |
| 247 | |
| 248 | // The callback will be invoked synchronously by inline executor. Immediately check the |
| 249 | // latch without waiting. |
| 250 | verify(mCb).onConnectivityReport(eq(createSampleConnectivityReport())); |
| 251 | } |
| 252 | |
| 253 | @Test |
| 254 | public void testConnectivityDiagnosticsCallbackOnDataStallSuspected() { |
| 255 | mBinder.onDataStallSuspected(createSampleDataStallReport()); |
| 256 | |
| 257 | // The callback will be invoked synchronously by inline executor. Immediately check the |
| 258 | // latch without waiting. |
| 259 | verify(mCb).onDataStallSuspected(eq(createSampleDataStallReport())); |
| 260 | } |
| 261 | |
| 262 | @Test |
| 263 | public void testConnectivityDiagnosticsCallbackOnNetworkConnectivityReported() { |
| 264 | final Network n = new Network(NET_ID); |
| 265 | final boolean connectivity = true; |
| 266 | |
| 267 | mBinder.onNetworkConnectivityReported(n, connectivity); |
| 268 | |
| 269 | // The callback will be invoked synchronously by inline executor. Immediately check the |
| 270 | // latch without waiting. |
| 271 | verify(mCb).onNetworkConnectivityReported(eq(n), eq(connectivity)); |
| 272 | } |
Cody Kesting | 73708bf | 2019-12-18 10:57:50 -0800 | [diff] [blame] | 273 | |
| 274 | @Test |
| 275 | public void testRegisterConnectivityDiagnosticsCallback() throws Exception { |
| 276 | final NetworkRequest request = new NetworkRequest.Builder().build(); |
| 277 | |
| 278 | mManager.registerConnectivityDiagnosticsCallback(request, INLINE_EXECUTOR, mCb); |
| 279 | |
| 280 | verify(mService).registerConnectivityDiagnosticsCallback( |
Cody Kesting | 83bb5fa | 2020-01-05 14:06:39 -0800 | [diff] [blame^] | 281 | any(ConnectivityDiagnosticsBinder.class), eq(request), eq(mPackageName)); |
Cody Kesting | 73708bf | 2019-12-18 10:57:50 -0800 | [diff] [blame] | 282 | assertTrue(ConnectivityDiagnosticsManager.sCallbacks.containsKey(mCb)); |
| 283 | } |
| 284 | |
| 285 | @Test |
| 286 | public void testRegisterDuplicateConnectivityDiagnosticsCallback() throws Exception { |
| 287 | final NetworkRequest request = new NetworkRequest.Builder().build(); |
| 288 | |
| 289 | mManager.registerConnectivityDiagnosticsCallback(request, INLINE_EXECUTOR, mCb); |
| 290 | |
| 291 | try { |
| 292 | mManager.registerConnectivityDiagnosticsCallback(request, INLINE_EXECUTOR, mCb); |
| 293 | fail("Duplicate callback registration should fail"); |
| 294 | } catch (IllegalArgumentException expected) { |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | @Test |
| 299 | public void testUnregisterConnectivityDiagnosticsCallback() throws Exception { |
| 300 | final NetworkRequest request = new NetworkRequest.Builder().build(); |
| 301 | mManager.registerConnectivityDiagnosticsCallback(request, INLINE_EXECUTOR, mCb); |
| 302 | |
| 303 | mManager.unregisterConnectivityDiagnosticsCallback(mCb); |
| 304 | |
| 305 | verify(mService).unregisterConnectivityDiagnosticsCallback( |
| 306 | any(ConnectivityDiagnosticsBinder.class)); |
| 307 | assertFalse(ConnectivityDiagnosticsManager.sCallbacks.containsKey(mCb)); |
| 308 | |
| 309 | // verify that re-registering is successful |
| 310 | mManager.registerConnectivityDiagnosticsCallback(request, INLINE_EXECUTOR, mCb); |
| 311 | verify(mService, times(2)).registerConnectivityDiagnosticsCallback( |
Cody Kesting | 83bb5fa | 2020-01-05 14:06:39 -0800 | [diff] [blame^] | 312 | any(ConnectivityDiagnosticsBinder.class), eq(request), eq(mPackageName)); |
Cody Kesting | 73708bf | 2019-12-18 10:57:50 -0800 | [diff] [blame] | 313 | assertTrue(ConnectivityDiagnosticsManager.sCallbacks.containsKey(mCb)); |
| 314 | } |
| 315 | |
| 316 | @Test |
| 317 | public void testUnregisterUnknownConnectivityDiagnosticsCallback() throws Exception { |
| 318 | mManager.unregisterConnectivityDiagnosticsCallback(mCb); |
| 319 | |
| 320 | verifyNoMoreInteractions(mService); |
| 321 | } |
Cody Kesting | beb41b5 | 2019-12-17 08:51:32 -0800 | [diff] [blame] | 322 | } |