blob: cc18b7f3220828c28e3ba632e465e1926729b473 [file] [log] [blame]
Hugo Benichi00a42d42016-09-13 15:55:09 +09001/*
2 * Copyright (C) 2016, 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 com.android.server.connectivity;
18
Hugo Benichi5eb90532017-03-23 18:38:22 +090019import static android.net.metrics.INetdEventListener.EVENT_GETADDRINFO;
20import static android.net.metrics.INetdEventListener.EVENT_GETHOSTBYNAME;
Glen Kuhne07199402016-10-25 15:47:25 -070021import static org.mockito.Mockito.timeout;
22import static org.mockito.Mockito.verify;
Hugo Benichi5eb90532017-03-23 18:38:22 +090023import static org.mockito.Mockito.when;
24import static org.junit.Assert.assertEquals;
25import static org.junit.Assert.assertFalse;
26import static org.junit.Assert.assertTrue;
27import static org.junit.Assert.fail;
Glen Kuhne07199402016-10-25 15:47:25 -070028
Hugo Benichi00a42d42016-09-13 15:55:09 +090029import android.content.Context;
Hugo Benichi5eb90532017-03-23 18:38:22 +090030import android.net.ConnectivityManager;
Hugo Benichi00a42d42016-09-13 15:55:09 +090031import android.net.ConnectivityMetricsEvent;
32import android.net.IIpConnectivityMetrics;
Hugo Benichi5eb90532017-03-23 18:38:22 +090033import android.net.Network;
34import android.net.NetworkCapabilities;
Hugo Benichie1c173d2016-10-18 10:36:33 +090035import android.net.metrics.ApfProgramEvent;
Hugo Benichi00a42d42016-09-13 15:55:09 +090036import android.net.metrics.ApfStats;
37import android.net.metrics.DefaultNetworkEvent;
38import android.net.metrics.DhcpClientEvent;
39import android.net.metrics.IpConnectivityLog;
40import android.net.metrics.IpManagerEvent;
41import android.net.metrics.IpReachabilityEvent;
42import android.net.metrics.RaEvent;
43import android.net.metrics.ValidationProbeEvent;
Hugo Benichi5eb90532017-03-23 18:38:22 +090044import android.system.OsConstants;
Hugo Benichi00a42d42016-09-13 15:55:09 +090045import android.os.Parcelable;
Hugo Benichi5eb90532017-03-23 18:38:22 +090046import android.support.test.runner.AndroidJUnit4;
Hugo Benichi80df43e2016-11-24 11:28:06 +090047import android.test.suitebuilder.annotation.SmallTest;
Hugo Benichi00a42d42016-09-13 15:55:09 +090048import android.util.Base64;
Tamas Berghammer383db5eb2016-06-22 15:21:38 +010049import com.android.server.connectivity.metrics.nano.IpConnectivityLogClass;
Hugo Benichi00a42d42016-09-13 15:55:09 +090050import java.io.PrintWriter;
51import java.io.StringWriter;
52import java.util.Collections;
53import java.util.Comparator;
54import java.util.Iterator;
55import java.util.List;
Hugo Benichi80df43e2016-11-24 11:28:06 +090056import org.mockito.ArgumentCaptor;
57import org.mockito.Mock;
58import org.mockito.MockitoAnnotations;
Hugo Benichi5eb90532017-03-23 18:38:22 +090059import org.junit.Before;
60import org.junit.Test;
61import org.junit.runner.RunWith;
Hugo Benichi00a42d42016-09-13 15:55:09 +090062
Hugo Benichi5eb90532017-03-23 18:38:22 +090063@RunWith(AndroidJUnit4.class)
64@SmallTest
65public class IpConnectivityMetricsTest {
Hugo Benichi00a42d42016-09-13 15:55:09 +090066 static final IpReachabilityEvent FAKE_EV =
Hugo Benichi948a8592017-03-16 16:33:47 +090067 new IpReachabilityEvent(IpReachabilityEvent.NUD_FAILED);
Hugo Benichi00a42d42016-09-13 15:55:09 +090068
Hugo Benichi5eb90532017-03-23 18:38:22 +090069 private static final String EXAMPLE_IPV4 = "192.0.2.1";
70 private static final String EXAMPLE_IPV6 = "2001:db8:1200::2:1";
71
Hugo Benichi00a42d42016-09-13 15:55:09 +090072 @Mock Context mCtx;
73 @Mock IIpConnectivityMetrics mMockService;
Hugo Benichi5eb90532017-03-23 18:38:22 +090074 @Mock ConnectivityManager mCm;
Hugo Benichi00a42d42016-09-13 15:55:09 +090075
76 IpConnectivityMetrics mService;
Hugo Benichi5eb90532017-03-23 18:38:22 +090077 NetdEventListenerService mNetdListener;
Hugo Benichi00a42d42016-09-13 15:55:09 +090078
Hugo Benichi5eb90532017-03-23 18:38:22 +090079 @Before
Hugo Benichi00a42d42016-09-13 15:55:09 +090080 public void setUp() {
81 MockitoAnnotations.initMocks(this);
Hugo Benichi05686db2016-10-19 11:17:28 +090082 mService = new IpConnectivityMetrics(mCtx, (ctx) -> 2000);
Hugo Benichi5eb90532017-03-23 18:38:22 +090083 mNetdListener = new NetdEventListenerService(mCm);
84 mService.mNetdListener = mNetdListener;
Hugo Benichi00a42d42016-09-13 15:55:09 +090085 }
86
Hugo Benichi5eb90532017-03-23 18:38:22 +090087 @Test
Hugo Benichi00a42d42016-09-13 15:55:09 +090088 public void testLoggingEvents() throws Exception {
89 IpConnectivityLog logger = new IpConnectivityLog(mMockService);
90
91 assertTrue(logger.log(1, FAKE_EV));
92 assertTrue(logger.log(2, FAKE_EV));
93 assertTrue(logger.log(3, FAKE_EV));
94
95 List<ConnectivityMetricsEvent> got = verifyEvents(3);
96 assertEventsEqual(expectedEvent(1), got.get(0));
97 assertEventsEqual(expectedEvent(2), got.get(1));
98 assertEventsEqual(expectedEvent(3), got.get(2));
99 }
100
Hugo Benichi5eb90532017-03-23 18:38:22 +0900101 @Test
Hugo Benichi00a42d42016-09-13 15:55:09 +0900102 public void testLoggingEventsWithMultipleCallers() throws Exception {
103 IpConnectivityLog logger = new IpConnectivityLog(mMockService);
104
105 final int nCallers = 10;
106 final int nEvents = 10;
107 for (int n = 0; n < nCallers; n++) {
108 final int i = n;
109 new Thread() {
110 public void run() {
111 for (int j = 0; j < nEvents; j++) {
Hugo Benichi946b7e42017-03-15 16:35:26 +0900112 assertTrue(logger.log(1 + i * 100 + j, FAKE_EV));
Hugo Benichi00a42d42016-09-13 15:55:09 +0900113 }
114 }
115 }.start();
116 }
117
Hugo Benichi5eb90532017-03-23 18:38:22 +0900118 List<ConnectivityMetricsEvent> got = verifyEvents(nCallers * nEvents, 200);
Hugo Benichi00a42d42016-09-13 15:55:09 +0900119 Collections.sort(got, EVENT_COMPARATOR);
120 Iterator<ConnectivityMetricsEvent> iter = got.iterator();
121 for (int i = 0; i < nCallers; i++) {
122 for (int j = 0; j < nEvents; j++) {
Hugo Benichi946b7e42017-03-15 16:35:26 +0900123 int expectedTimestamp = 1 + i * 100 + j;
Hugo Benichi00a42d42016-09-13 15:55:09 +0900124 assertEventsEqual(expectedEvent(expectedTimestamp), iter.next());
125 }
126 }
127 }
128
Hugo Benichi5eb90532017-03-23 18:38:22 +0900129 @Test
Hugo Benichi00a42d42016-09-13 15:55:09 +0900130 public void testBufferFlushing() {
131 String output1 = getdump("flush");
132 assertEquals("", output1);
133
134 new IpConnectivityLog(mService.impl).log(1, FAKE_EV);
135 String output2 = getdump("flush");
136 assertFalse("".equals(output2));
137
138 String output3 = getdump("flush");
139 assertEquals("", output3);
140 }
141
Hugo Benichi5eb90532017-03-23 18:38:22 +0900142 @Test
Hugo Benichie1c173d2016-10-18 10:36:33 +0900143 public void testRateLimiting() {
144 final IpConnectivityLog logger = new IpConnectivityLog(mService.impl);
Hugo Benichi22d9b2d2017-02-22 13:02:27 +0900145 final ApfProgramEvent ev = new ApfProgramEvent();
Hugo Benichie1c173d2016-10-18 10:36:33 +0900146 final long fakeTimestamp = 1;
147
148 int attempt = 100; // More than burst quota, but less than buffer size.
149 for (int i = 0; i < attempt; i++) {
150 logger.log(ev);
151 }
152
153 String output1 = getdump("flush");
154 assertFalse("".equals(output1));
155
156 for (int i = 0; i < attempt; i++) {
157 assertFalse("expected event to be dropped", logger.log(fakeTimestamp, ev));
158 }
159
160 String output2 = getdump("flush");
161 assertEquals("", output2);
162 }
163
Hugo Benichi5eb90532017-03-23 18:38:22 +0900164 @Test
165 public void testEndToEndLogging() throws Exception {
Hugo Benichif6840502017-03-08 11:59:36 +0900166 // TODO: instead of comparing textpb to textpb, parse textpb and compare proto to proto.
Hugo Benichi00a42d42016-09-13 15:55:09 +0900167 IpConnectivityLog logger = new IpConnectivityLog(mService.impl);
168
Hugo Benichi5eb90532017-03-23 18:38:22 +0900169 NetworkCapabilities ncWifi = new NetworkCapabilities();
170 NetworkCapabilities ncCell = new NetworkCapabilities();
171 ncWifi.addTransportType(NetworkCapabilities.TRANSPORT_WIFI);
172 ncCell.addTransportType(NetworkCapabilities.TRANSPORT_CELLULAR);
173
174 when(mCm.getNetworkCapabilities(new Network(100))).thenReturn(ncWifi);
175 when(mCm.getNetworkCapabilities(new Network(101))).thenReturn(ncCell);
176
Hugo Benichi22d9b2d2017-02-22 13:02:27 +0900177 ApfStats apfStats = new ApfStats();
178 apfStats.durationMs = 45000;
179 apfStats.receivedRas = 10;
180 apfStats.matchingRas = 2;
181 apfStats.droppedRas = 2;
182 apfStats.parseErrors = 2;
183 apfStats.zeroLifetimeRas = 1;
184 apfStats.programUpdates = 4;
185 apfStats.programUpdatesAll = 7;
186 apfStats.programUpdatesAllowingMulticast = 3;
187 apfStats.maxProgramSize = 2048;
Hugo Benichif927f0c2017-03-17 15:42:40 +0900188
189 ValidationProbeEvent validationEv = new ValidationProbeEvent();
190 validationEv.durationMs = 40730;
191 validationEv.probeType = ValidationProbeEvent.PROBE_HTTP;
192 validationEv.returnCode = 204;
193
Hugo Benichi00a42d42016-09-13 15:55:09 +0900194 Parcelable[] events = {
Hugo Benichi948a8592017-03-16 16:33:47 +0900195 new IpReachabilityEvent(IpReachabilityEvent.NUD_FAILED),
196 new DhcpClientEvent("SomeState", 192),
Hugo Benichi00a42d42016-09-13 15:55:09 +0900197 new DefaultNetworkEvent(102, new int[]{1,2,3}, 101, true, false),
Hugo Benichi948a8592017-03-16 16:33:47 +0900198 new IpManagerEvent(IpManagerEvent.PROVISIONING_OK, 5678),
Hugo Benichif927f0c2017-03-17 15:42:40 +0900199 validationEv,
Hugo Benichi22d9b2d2017-02-22 13:02:27 +0900200 apfStats,
Hugo Benichi00a42d42016-09-13 15:55:09 +0900201 new RaEvent(2000, 400, 300, -1, 1000, -1)
202 };
203
204 for (int i = 0; i < events.length; i++) {
Hugo Benichi948a8592017-03-16 16:33:47 +0900205 ConnectivityMetricsEvent ev = new ConnectivityMetricsEvent();
206 ev.timestamp = 100 * (i + 1);
207 ev.ifname = "wlan0";
208 ev.data = events[i];
209 logger.log(ev);
Hugo Benichi00a42d42016-09-13 15:55:09 +0900210 }
211
Hugo Benichi5eb90532017-03-23 18:38:22 +0900212 // netId, errno, latency, destination
213 connectEvent(100, OsConstants.EALREADY, 0, EXAMPLE_IPV4);
214 connectEvent(100, OsConstants.EINPROGRESS, 0, EXAMPLE_IPV6);
215 connectEvent(100, 0, 110, EXAMPLE_IPV4);
216 connectEvent(101, 0, 23, EXAMPLE_IPV4);
217 connectEvent(101, 0, 45, EXAMPLE_IPV6);
218 connectEvent(100, OsConstants.EAGAIN, 0, EXAMPLE_IPV4);
219
220 // netId, type, return code, latency
221 dnsEvent(100, EVENT_GETADDRINFO, 0, 3456);
222 dnsEvent(100, EVENT_GETADDRINFO, 3, 45);
223 dnsEvent(100, EVENT_GETHOSTBYNAME, 0, 638);
224 dnsEvent(101, EVENT_GETADDRINFO, 0, 56);
225 dnsEvent(101, EVENT_GETHOSTBYNAME, 0, 34);
226
Hugo Benichi2a5cfb92017-03-22 22:21:44 +0900227 String want = String.join("\n",
Hugo Benichi00a42d42016-09-13 15:55:09 +0900228 "dropped_events: 0",
229 "events <",
Hugo Benichif6840502017-03-08 11:59:36 +0900230 " if_name: \"\"",
Hugo Benichi948a8592017-03-16 16:33:47 +0900231 " link_layer: 4",
Hugo Benichif6840502017-03-08 11:59:36 +0900232 " network_id: 0",
Hugo Benichi80df43e2016-11-24 11:28:06 +0900233 " time_ms: 100",
Hugo Benichif6840502017-03-08 11:59:36 +0900234 " transports: 0",
Hugo Benichi00a42d42016-09-13 15:55:09 +0900235 " ip_reachability_event <",
236 " event_type: 512",
Hugo Benichi948a8592017-03-16 16:33:47 +0900237 " if_name: \"\"",
Hugo Benichi00a42d42016-09-13 15:55:09 +0900238 " >",
Hugo Benichi00a42d42016-09-13 15:55:09 +0900239 ">",
240 "events <",
Hugo Benichif6840502017-03-08 11:59:36 +0900241 " if_name: \"\"",
Hugo Benichi948a8592017-03-16 16:33:47 +0900242 " link_layer: 4",
Hugo Benichif6840502017-03-08 11:59:36 +0900243 " network_id: 0",
Hugo Benichi80df43e2016-11-24 11:28:06 +0900244 " time_ms: 200",
Hugo Benichif6840502017-03-08 11:59:36 +0900245 " transports: 0",
Hugo Benichi00a42d42016-09-13 15:55:09 +0900246 " dhcp_event <",
247 " duration_ms: 192",
Hugo Benichi948a8592017-03-16 16:33:47 +0900248 " if_name: \"\"",
Hugo Benichi00a42d42016-09-13 15:55:09 +0900249 " state_transition: \"SomeState\"",
250 " >",
Hugo Benichi00a42d42016-09-13 15:55:09 +0900251 ">",
252 "events <",
Hugo Benichif6840502017-03-08 11:59:36 +0900253 " if_name: \"\"",
Hugo Benichi948a8592017-03-16 16:33:47 +0900254 " link_layer: 4",
Hugo Benichif6840502017-03-08 11:59:36 +0900255 " network_id: 0",
Hugo Benichi80df43e2016-11-24 11:28:06 +0900256 " time_ms: 300",
Hugo Benichif6840502017-03-08 11:59:36 +0900257 " transports: 0",
Hugo Benichi00a42d42016-09-13 15:55:09 +0900258 " default_network_event <",
Hugo Benichi4eccf782017-07-18 14:28:27 +0900259 " default_network_duration_ms: 0",
260 " final_score: 0",
261 " initial_score: 0",
262 " ip_support: 0",
Hugo Benichi00a42d42016-09-13 15:55:09 +0900263 " network_id <",
264 " network_id: 102",
265 " >",
Hugo Benichi4eccf782017-07-18 14:28:27 +0900266 " no_default_network_duration_ms: 0",
Hugo Benichi00a42d42016-09-13 15:55:09 +0900267 " previous_network_id <",
268 " network_id: 101",
269 " >",
270 " previous_network_ip_support: 1",
271 " transport_types: 1",
272 " transport_types: 2",
273 " transport_types: 3",
274 " >",
Hugo Benichi00a42d42016-09-13 15:55:09 +0900275 ">",
276 "events <",
Hugo Benichif6840502017-03-08 11:59:36 +0900277 " if_name: \"\"",
Hugo Benichi948a8592017-03-16 16:33:47 +0900278 " link_layer: 4",
Hugo Benichif6840502017-03-08 11:59:36 +0900279 " network_id: 0",
Hugo Benichi80df43e2016-11-24 11:28:06 +0900280 " time_ms: 400",
Hugo Benichif6840502017-03-08 11:59:36 +0900281 " transports: 0",
Hugo Benichi00a42d42016-09-13 15:55:09 +0900282 " ip_provisioning_event <",
283 " event_type: 1",
Hugo Benichi948a8592017-03-16 16:33:47 +0900284 " if_name: \"\"",
Hugo Benichi00a42d42016-09-13 15:55:09 +0900285 " latency_ms: 5678",
286 " >",
Hugo Benichi00a42d42016-09-13 15:55:09 +0900287 ">",
288 "events <",
Hugo Benichif6840502017-03-08 11:59:36 +0900289 " if_name: \"\"",
Hugo Benichi948a8592017-03-16 16:33:47 +0900290 " link_layer: 4",
Hugo Benichif6840502017-03-08 11:59:36 +0900291 " network_id: 0",
Hugo Benichi00a42d42016-09-13 15:55:09 +0900292 " time_ms: 500",
Hugo Benichif6840502017-03-08 11:59:36 +0900293 " transports: 0",
Hugo Benichi00a42d42016-09-13 15:55:09 +0900294 " validation_probe_event <",
295 " latency_ms: 40730",
Hugo Benichi00a42d42016-09-13 15:55:09 +0900296 " probe_result: 204",
297 " probe_type: 1",
298 " >",
299 ">",
300 "events <",
Hugo Benichif6840502017-03-08 11:59:36 +0900301 " if_name: \"\"",
Hugo Benichi948a8592017-03-16 16:33:47 +0900302 " link_layer: 4",
Hugo Benichif6840502017-03-08 11:59:36 +0900303 " network_id: 0",
Hugo Benichi80df43e2016-11-24 11:28:06 +0900304 " time_ms: 600",
Hugo Benichif6840502017-03-08 11:59:36 +0900305 " transports: 0",
Hugo Benichi00a42d42016-09-13 15:55:09 +0900306 " apf_statistics <",
307 " dropped_ras: 2",
308 " duration_ms: 45000",
309 " matching_ras: 2",
310 " max_program_size: 2048",
311 " parse_errors: 2",
312 " program_updates: 4",
Hugo Benichi22d9b2d2017-02-22 13:02:27 +0900313 " program_updates_all: 7",
314 " program_updates_allowing_multicast: 3",
Hugo Benichi00a42d42016-09-13 15:55:09 +0900315 " received_ras: 10",
Hugo Benichi4eccf782017-07-18 14:28:27 +0900316 " total_packet_dropped: 0",
317 " total_packet_processed: 0",
Hugo Benichi00a42d42016-09-13 15:55:09 +0900318 " zero_lifetime_ras: 1",
319 " >",
Hugo Benichi00a42d42016-09-13 15:55:09 +0900320 ">",
321 "events <",
Hugo Benichif6840502017-03-08 11:59:36 +0900322 " if_name: \"\"",
Hugo Benichi948a8592017-03-16 16:33:47 +0900323 " link_layer: 4",
Hugo Benichif6840502017-03-08 11:59:36 +0900324 " network_id: 0",
Hugo Benichi80df43e2016-11-24 11:28:06 +0900325 " time_ms: 700",
Hugo Benichif6840502017-03-08 11:59:36 +0900326 " transports: 0",
Hugo Benichi00a42d42016-09-13 15:55:09 +0900327 " ra_event <",
328 " dnssl_lifetime: -1",
329 " prefix_preferred_lifetime: 300",
330 " prefix_valid_lifetime: 400",
331 " rdnss_lifetime: 1000",
332 " route_info_lifetime: -1",
333 " router_lifetime: 2000",
334 " >",
Hugo Benichid680d4c2016-10-13 13:16:16 +0900335 ">",
Hugo Benichi5eb90532017-03-23 18:38:22 +0900336 "events <",
337 " if_name: \"\"",
338 " link_layer: 4",
339 " network_id: 100",
340 " time_ms: 0",
341 " transports: 2",
342 " connect_statistics <",
343 " connect_blocking_count: 1",
344 " connect_count: 3",
345 " errnos_counters <",
346 " key: 11",
347 " value: 1",
348 " >",
349 " ipv6_addr_count: 1",
350 " latencies_ms: 110",
351 " >",
352 ">",
353 "events <",
354 " if_name: \"\"",
355 " link_layer: 2",
356 " network_id: 101",
357 " time_ms: 0",
358 " transports: 1",
359 " connect_statistics <",
360 " connect_blocking_count: 2",
361 " connect_count: 2",
362 " ipv6_addr_count: 1",
363 " latencies_ms: 23",
364 " latencies_ms: 45",
365 " >",
366 ">",
367 "events <",
368 " if_name: \"\"",
369 " link_layer: 4",
370 " network_id: 100",
371 " time_ms: 0",
372 " transports: 2",
373 " dns_lookup_batch <",
374 " event_types: 1",
375 " event_types: 1",
376 " event_types: 2",
Hugo Benichi4eccf782017-07-18 14:28:27 +0900377 " getaddrinfo_error_count: 0",
378 " getaddrinfo_query_count: 0",
379 " gethostbyname_error_count: 0",
380 " gethostbyname_query_count: 0",
Hugo Benichi5eb90532017-03-23 18:38:22 +0900381 " latencies_ms: 3456",
382 " latencies_ms: 45",
383 " latencies_ms: 638",
384 " return_codes: 0",
385 " return_codes: 3",
386 " return_codes: 0",
387 " >",
388 ">",
389 "events <",
390 " if_name: \"\"",
391 " link_layer: 2",
392 " network_id: 101",
393 " time_ms: 0",
394 " transports: 1",
395 " dns_lookup_batch <",
396 " event_types: 1",
397 " event_types: 2",
Hugo Benichi4eccf782017-07-18 14:28:27 +0900398 " getaddrinfo_error_count: 0",
399 " getaddrinfo_query_count: 0",
400 " gethostbyname_error_count: 0",
401 " gethostbyname_query_count: 0",
Hugo Benichi5eb90532017-03-23 18:38:22 +0900402 " latencies_ms: 56",
403 " latencies_ms: 34",
404 " return_codes: 0",
405 " return_codes: 0",
406 " >",
407 ">",
Hugo Benichi2a5cfb92017-03-22 22:21:44 +0900408 "version: 2\n");
Hugo Benichi00a42d42016-09-13 15:55:09 +0900409
410 verifySerialization(want, getdump("flush"));
411 }
412
413 String getdump(String ... command) {
414 StringWriter buffer = new StringWriter();
415 PrintWriter writer = new PrintWriter(buffer);
416 mService.impl.dump(null, writer, command);
417 return buffer.toString();
418 }
419
Hugo Benichi5eb90532017-03-23 18:38:22 +0900420 void connectEvent(int netid, int error, int latencyMs, String ipAddr) throws Exception {
421 mNetdListener.onConnectEvent(netid, error, latencyMs, ipAddr, 80, 1);
422 }
423
424 void dnsEvent(int netId, int type, int result, int latency) throws Exception {
425 mNetdListener.onDnsEvent(netId, type, result, latency, "", null, 0, 0);
426 }
427
Hugo Benichi00a42d42016-09-13 15:55:09 +0900428 List<ConnectivityMetricsEvent> verifyEvents(int n, int timeoutMs) throws Exception {
429 ArgumentCaptor<ConnectivityMetricsEvent> captor =
430 ArgumentCaptor.forClass(ConnectivityMetricsEvent.class);
431 verify(mMockService, timeout(timeoutMs).times(n)).logEvent(captor.capture());
432 return captor.getAllValues();
433 }
434
435 List<ConnectivityMetricsEvent> verifyEvents(int n) throws Exception {
436 return verifyEvents(n, 10);
437 }
438
439 static void verifySerialization(String want, String output) {
440 try {
441 byte[] got = Base64.decode(output, Base64.DEFAULT);
442 IpConnectivityLogClass.IpConnectivityLog log =
Glen Kuhne07199402016-10-25 15:47:25 -0700443 IpConnectivityLogClass.IpConnectivityLog.parseFrom(got);
Hugo Benichi00a42d42016-09-13 15:55:09 +0900444 assertEquals(want, log.toString());
445 } catch (Exception e) {
446 fail(e.toString());
447 }
448 }
449
450 static String joinLines(String ... elems) {
451 StringBuilder b = new StringBuilder();
452 for (String s : elems) {
453 b.append(s).append("\n");
454 }
455 return b.toString();
456 }
457
458 static ConnectivityMetricsEvent expectedEvent(int timestamp) {
Hugo Benichiec27c4d2017-03-15 15:07:42 +0900459 ConnectivityMetricsEvent ev = new ConnectivityMetricsEvent();
460 ev.timestamp = timestamp;
461 ev.data = FAKE_EV;
462 return ev;
Hugo Benichi00a42d42016-09-13 15:55:09 +0900463 }
464
465 /** Outer equality for ConnectivityMetricsEvent to avoid overriding equals() and hashCode(). */
466 static void assertEventsEqual(ConnectivityMetricsEvent expected, ConnectivityMetricsEvent got) {
467 assertEquals(expected.timestamp, got.timestamp);
Hugo Benichi00a42d42016-09-13 15:55:09 +0900468 assertEquals(expected.data, got.data);
469 }
470
471 static final Comparator<ConnectivityMetricsEvent> EVENT_COMPARATOR =
Hugo Benichi0d4a3982016-11-25 11:24:22 +0900472 Comparator.comparingLong((ev) -> ev.timestamp);
Hugo Benichi00a42d42016-09-13 15:55:09 +0900473}