blob: 7ab4b56fae8064f259e10bd39a969835532e4d17 [file] [log] [blame]
Cody Kestingbeb41b52019-12-17 08:51:32 -08001/*
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
17package android.net;
18
Cody Kesting3e7fb382019-12-17 16:46:11 -080019import static android.net.ConnectivityDiagnosticsManager.ConnectivityDiagnosticsBinder;
20import static android.net.ConnectivityDiagnosticsManager.ConnectivityDiagnosticsCallback;
Cody Kestingbeb41b52019-12-17 08:51:32 -080021import static android.net.ConnectivityDiagnosticsManager.ConnectivityReport;
Cody Kesting3d97b5e2019-12-17 09:28:06 -080022import static android.net.ConnectivityDiagnosticsManager.DataStallReport;
Cody Kestingbeb41b52019-12-17 08:51:32 -080023
24import static com.android.testutils.ParcelUtilsKt.assertParcelSane;
25
26import static org.junit.Assert.assertEquals;
27import static org.junit.Assert.assertFalse;
28import static org.junit.Assert.assertNotEquals;
29import static org.junit.Assert.assertTrue;
Cody Kesting3e7fb382019-12-17 16:46:11 -080030import static org.mockito.ArgumentMatchers.eq;
31import static org.mockito.Mockito.mock;
32import static org.mockito.Mockito.verify;
Cody Kestingbeb41b52019-12-17 08:51:32 -080033
34import android.os.PersistableBundle;
35
Cody Kesting3e7fb382019-12-17 16:46:11 -080036import org.junit.Before;
Cody Kestingbeb41b52019-12-17 08:51:32 -080037import org.junit.Test;
38import org.junit.runner.RunWith;
39import org.junit.runners.JUnit4;
Cody Kesting3e7fb382019-12-17 16:46:11 -080040import org.mockito.Mock;
41
42import java.util.concurrent.Executor;
Cody Kestingbeb41b52019-12-17 08:51:32 -080043
44@RunWith(JUnit4.class)
45public class ConnectivityDiagnosticsManagerTest {
46 private static final int NET_ID = 1;
Cody Kesting3d97b5e2019-12-17 09:28:06 -080047 private static final int DETECTION_METHOD = 2;
Cody Kestingbeb41b52019-12-17 08:51:32 -080048 private static final long TIMESTAMP = 10L;
49 private static final String INTERFACE_NAME = "interface";
50 private static final String BUNDLE_KEY = "key";
51 private static final String BUNDLE_VALUE = "value";
52
Cody Kesting3e7fb382019-12-17 16:46:11 -080053 private static final Executor INLINE_EXECUTOR = x -> x.run();
54
55 @Mock private ConnectivityDiagnosticsCallback mCb;
56
57 private ConnectivityDiagnosticsBinder mBinder;
58
59 @Before
60 public void setUp() {
61 mCb = mock(ConnectivityDiagnosticsCallback.class);
62
63 mBinder = new ConnectivityDiagnosticsBinder(mCb, INLINE_EXECUTOR);
64 }
65
Cody Kestingbeb41b52019-12-17 08:51:32 -080066 private ConnectivityReport createSampleConnectivityReport() {
67 final LinkProperties linkProperties = new LinkProperties();
68 linkProperties.setInterfaceName(INTERFACE_NAME);
69
70 final NetworkCapabilities networkCapabilities = new NetworkCapabilities();
71 networkCapabilities.addCapability(NetworkCapabilities.NET_CAPABILITY_IMS);
72
73 final PersistableBundle bundle = new PersistableBundle();
74 bundle.putString(BUNDLE_KEY, BUNDLE_VALUE);
75
76 return new ConnectivityReport(
77 new Network(NET_ID), TIMESTAMP, linkProperties, networkCapabilities, bundle);
78 }
79
80 private ConnectivityReport createDefaultConnectivityReport() {
81 return new ConnectivityReport(
82 new Network(0),
83 0L,
84 new LinkProperties(),
85 new NetworkCapabilities(),
86 PersistableBundle.EMPTY);
87 }
88
89 @Test
90 public void testPersistableBundleEquals() {
91 assertFalse(
92 ConnectivityDiagnosticsManager.persistableBundleEquals(
93 null, PersistableBundle.EMPTY));
94 assertFalse(
95 ConnectivityDiagnosticsManager.persistableBundleEquals(
96 PersistableBundle.EMPTY, null));
97 assertTrue(
98 ConnectivityDiagnosticsManager.persistableBundleEquals(
99 PersistableBundle.EMPTY, PersistableBundle.EMPTY));
100
101 final PersistableBundle a = new PersistableBundle();
102 a.putString(BUNDLE_KEY, BUNDLE_VALUE);
103
104 final PersistableBundle b = new PersistableBundle();
105 b.putString(BUNDLE_KEY, BUNDLE_VALUE);
106
107 final PersistableBundle c = new PersistableBundle();
108 c.putString(BUNDLE_KEY, null);
109
110 assertFalse(
111 ConnectivityDiagnosticsManager.persistableBundleEquals(PersistableBundle.EMPTY, a));
112 assertFalse(
113 ConnectivityDiagnosticsManager.persistableBundleEquals(a, PersistableBundle.EMPTY));
114
115 assertTrue(ConnectivityDiagnosticsManager.persistableBundleEquals(a, b));
116 assertTrue(ConnectivityDiagnosticsManager.persistableBundleEquals(b, a));
117
118 assertFalse(ConnectivityDiagnosticsManager.persistableBundleEquals(a, c));
119 assertFalse(ConnectivityDiagnosticsManager.persistableBundleEquals(c, a));
120 }
121
122 @Test
123 public void testConnectivityReportEquals() {
124 assertEquals(createSampleConnectivityReport(), createSampleConnectivityReport());
125 assertEquals(createDefaultConnectivityReport(), createDefaultConnectivityReport());
126
127 final LinkProperties linkProperties = new LinkProperties();
128 linkProperties.setInterfaceName(INTERFACE_NAME);
129
130 final NetworkCapabilities networkCapabilities = new NetworkCapabilities();
131 networkCapabilities.addCapability(NetworkCapabilities.NET_CAPABILITY_IMS);
132
133 final PersistableBundle bundle = new PersistableBundle();
134 bundle.putString(BUNDLE_KEY, BUNDLE_VALUE);
135
136 assertNotEquals(
137 createDefaultConnectivityReport(),
138 new ConnectivityReport(
139 new Network(NET_ID),
140 0L,
141 new LinkProperties(),
142 new NetworkCapabilities(),
143 PersistableBundle.EMPTY));
144 assertNotEquals(
145 createDefaultConnectivityReport(),
146 new ConnectivityReport(
147 new Network(0),
148 TIMESTAMP,
149 new LinkProperties(),
150 new NetworkCapabilities(),
151 PersistableBundle.EMPTY));
152 assertNotEquals(
153 createDefaultConnectivityReport(),
154 new ConnectivityReport(
155 new Network(0),
156 0L,
157 linkProperties,
158 new NetworkCapabilities(),
159 PersistableBundle.EMPTY));
160 assertNotEquals(
161 createDefaultConnectivityReport(),
162 new ConnectivityReport(
163 new Network(0),
164 TIMESTAMP,
165 new LinkProperties(),
166 networkCapabilities,
167 PersistableBundle.EMPTY));
168 assertNotEquals(
169 createDefaultConnectivityReport(),
170 new ConnectivityReport(
171 new Network(0),
172 TIMESTAMP,
173 new LinkProperties(),
174 new NetworkCapabilities(),
175 bundle));
176 }
177
178 @Test
179 public void testConnectivityReportParcelUnparcel() {
180 assertParcelSane(createSampleConnectivityReport(), 5);
181 }
Cody Kesting3d97b5e2019-12-17 09:28:06 -0800182
183 private DataStallReport createSampleDataStallReport() {
184 final PersistableBundle bundle = new PersistableBundle();
185 bundle.putString(BUNDLE_KEY, BUNDLE_VALUE);
186 return new DataStallReport(new Network(NET_ID), TIMESTAMP, DETECTION_METHOD, bundle);
187 }
188
189 private DataStallReport createDefaultDataStallReport() {
190 return new DataStallReport(new Network(0), 0L, 0, PersistableBundle.EMPTY);
191 }
192
193 @Test
194 public void testDataStallReportEquals() {
195 assertEquals(createSampleDataStallReport(), createSampleDataStallReport());
196 assertEquals(createDefaultDataStallReport(), createDefaultDataStallReport());
197
198 final PersistableBundle bundle = new PersistableBundle();
199 bundle.putString(BUNDLE_KEY, BUNDLE_VALUE);
200
201 assertNotEquals(
202 createDefaultDataStallReport(),
203 new DataStallReport(new Network(NET_ID), 0L, 0, PersistableBundle.EMPTY));
204 assertNotEquals(
205 createDefaultDataStallReport(),
206 new DataStallReport(new Network(0), TIMESTAMP, 0, PersistableBundle.EMPTY));
207 assertNotEquals(
208 createDefaultDataStallReport(),
209 new DataStallReport(new Network(0), 0L, DETECTION_METHOD, PersistableBundle.EMPTY));
210 assertNotEquals(
211 createDefaultDataStallReport(), new DataStallReport(new Network(0), 0L, 0, bundle));
212 }
213
214 @Test
215 public void testDataStallReportParcelUnparcel() {
216 assertParcelSane(createSampleDataStallReport(), 4);
217 }
Cody Kesting3e7fb382019-12-17 16:46:11 -0800218
219 @Test
220 public void testConnectivityDiagnosticsCallbackOnConnectivityReport() {
221 mBinder.onConnectivityReport(createSampleConnectivityReport());
222
223 // The callback will be invoked synchronously by inline executor. Immediately check the
224 // latch without waiting.
225 verify(mCb).onConnectivityReport(eq(createSampleConnectivityReport()));
226 }
227
228 @Test
229 public void testConnectivityDiagnosticsCallbackOnDataStallSuspected() {
230 mBinder.onDataStallSuspected(createSampleDataStallReport());
231
232 // The callback will be invoked synchronously by inline executor. Immediately check the
233 // latch without waiting.
234 verify(mCb).onDataStallSuspected(eq(createSampleDataStallReport()));
235 }
236
237 @Test
238 public void testConnectivityDiagnosticsCallbackOnNetworkConnectivityReported() {
239 final Network n = new Network(NET_ID);
240 final boolean connectivity = true;
241
242 mBinder.onNetworkConnectivityReported(n, connectivity);
243
244 // The callback will be invoked synchronously by inline executor. Immediately check the
245 // latch without waiting.
246 verify(mCb).onNetworkConnectivityReported(eq(n), eq(connectivity));
247 }
Cody Kestingbeb41b52019-12-17 08:51:32 -0800248}