blob: 1f7c5f418d7488eb03529fbf71a55ab9eea85793 [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
Glen Kuhne07199402016-10-25 15:47:25 -070019import static org.mockito.Mockito.timeout;
20import static org.mockito.Mockito.verify;
21
Hugo Benichi00a42d42016-09-13 15:55:09 +090022import android.content.Context;
23import android.net.ConnectivityMetricsEvent;
24import android.net.IIpConnectivityMetrics;
Hugo Benichie1c173d2016-10-18 10:36:33 +090025import android.net.metrics.ApfProgramEvent;
Hugo Benichi00a42d42016-09-13 15:55:09 +090026import android.net.metrics.ApfStats;
27import android.net.metrics.DefaultNetworkEvent;
28import android.net.metrics.DhcpClientEvent;
29import android.net.metrics.IpConnectivityLog;
30import android.net.metrics.IpManagerEvent;
31import android.net.metrics.IpReachabilityEvent;
32import android.net.metrics.RaEvent;
33import android.net.metrics.ValidationProbeEvent;
34import android.os.Parcelable;
Hugo Benichi80df43e2016-11-24 11:28:06 +090035import android.test.suitebuilder.annotation.SmallTest;
Hugo Benichi00a42d42016-09-13 15:55:09 +090036import android.util.Base64;
Tamas Berghammer383db5eb2016-06-22 15:21:38 +010037import com.android.server.connectivity.metrics.nano.IpConnectivityLogClass;
Hugo Benichi00a42d42016-09-13 15:55:09 +090038import java.io.PrintWriter;
39import java.io.StringWriter;
40import java.util.Collections;
41import java.util.Comparator;
42import java.util.Iterator;
43import java.util.List;
Hugo Benichi80df43e2016-11-24 11:28:06 +090044import junit.framework.TestCase;
45import org.mockito.ArgumentCaptor;
46import org.mockito.Mock;
47import org.mockito.MockitoAnnotations;
Hugo Benichi00a42d42016-09-13 15:55:09 +090048
49public class IpConnectivityMetricsTest extends TestCase {
50 static final IpReachabilityEvent FAKE_EV =
51 new IpReachabilityEvent("wlan0", IpReachabilityEvent.NUD_FAILED);
52
53 @Mock Context mCtx;
54 @Mock IIpConnectivityMetrics mMockService;
55
56 IpConnectivityMetrics mService;
57
58 public void setUp() {
59 MockitoAnnotations.initMocks(this);
Hugo Benichi05686db2016-10-19 11:17:28 +090060 mService = new IpConnectivityMetrics(mCtx, (ctx) -> 2000);
Hugo Benichi00a42d42016-09-13 15:55:09 +090061 }
62
Hugo Benichi80df43e2016-11-24 11:28:06 +090063 @SmallTest
Hugo Benichi00a42d42016-09-13 15:55:09 +090064 public void testLoggingEvents() throws Exception {
65 IpConnectivityLog logger = new IpConnectivityLog(mMockService);
66
67 assertTrue(logger.log(1, FAKE_EV));
68 assertTrue(logger.log(2, FAKE_EV));
69 assertTrue(logger.log(3, FAKE_EV));
70
71 List<ConnectivityMetricsEvent> got = verifyEvents(3);
72 assertEventsEqual(expectedEvent(1), got.get(0));
73 assertEventsEqual(expectedEvent(2), got.get(1));
74 assertEventsEqual(expectedEvent(3), got.get(2));
75 }
76
Hugo Benichi80df43e2016-11-24 11:28:06 +090077 @SmallTest
Hugo Benichi00a42d42016-09-13 15:55:09 +090078 public void testLoggingEventsWithMultipleCallers() throws Exception {
79 IpConnectivityLog logger = new IpConnectivityLog(mMockService);
80
81 final int nCallers = 10;
82 final int nEvents = 10;
83 for (int n = 0; n < nCallers; n++) {
84 final int i = n;
85 new Thread() {
86 public void run() {
87 for (int j = 0; j < nEvents; j++) {
88 assertTrue(logger.log(i * 100 + j, FAKE_EV));
89 }
90 }
91 }.start();
92 }
93
94 List<ConnectivityMetricsEvent> got = verifyEvents(nCallers * nEvents, 100);
95 Collections.sort(got, EVENT_COMPARATOR);
96 Iterator<ConnectivityMetricsEvent> iter = got.iterator();
97 for (int i = 0; i < nCallers; i++) {
98 for (int j = 0; j < nEvents; j++) {
99 int expectedTimestamp = i * 100 + j;
100 assertEventsEqual(expectedEvent(expectedTimestamp), iter.next());
101 }
102 }
103 }
104
Hugo Benichi80df43e2016-11-24 11:28:06 +0900105 @SmallTest
Hugo Benichi00a42d42016-09-13 15:55:09 +0900106 public void testBufferFlushing() {
107 String output1 = getdump("flush");
108 assertEquals("", output1);
109
110 new IpConnectivityLog(mService.impl).log(1, FAKE_EV);
111 String output2 = getdump("flush");
112 assertFalse("".equals(output2));
113
114 String output3 = getdump("flush");
115 assertEquals("", output3);
116 }
117
Hugo Benichi80df43e2016-11-24 11:28:06 +0900118 @SmallTest
Hugo Benichie1c173d2016-10-18 10:36:33 +0900119 public void testRateLimiting() {
120 final IpConnectivityLog logger = new IpConnectivityLog(mService.impl);
121 final ApfProgramEvent ev = new ApfProgramEvent(0, 0, 0, 0, 0);
122 final long fakeTimestamp = 1;
123
124 int attempt = 100; // More than burst quota, but less than buffer size.
125 for (int i = 0; i < attempt; i++) {
126 logger.log(ev);
127 }
128
129 String output1 = getdump("flush");
130 assertFalse("".equals(output1));
131
132 for (int i = 0; i < attempt; i++) {
133 assertFalse("expected event to be dropped", logger.log(fakeTimestamp, ev));
134 }
135
136 String output2 = getdump("flush");
137 assertEquals("", output2);
138 }
139
Hugo Benichi80df43e2016-11-24 11:28:06 +0900140 @SmallTest
Hugo Benichi00a42d42016-09-13 15:55:09 +0900141 public void testEndToEndLogging() {
Hugo Benichif6840502017-03-08 11:59:36 +0900142 // TODO: instead of comparing textpb to textpb, parse textpb and compare proto to proto.
Hugo Benichi00a42d42016-09-13 15:55:09 +0900143 IpConnectivityLog logger = new IpConnectivityLog(mService.impl);
144
145 Parcelable[] events = {
146 new IpReachabilityEvent("wlan0", IpReachabilityEvent.NUD_FAILED),
147 new DhcpClientEvent("wlan0", "SomeState", 192),
148 new DefaultNetworkEvent(102, new int[]{1,2,3}, 101, true, false),
149 new IpManagerEvent("wlan0", IpManagerEvent.PROVISIONING_OK, 5678),
150 new ValidationProbeEvent(120, 40730, ValidationProbeEvent.PROBE_HTTP, 204),
151 new ApfStats(45000, 10, 2, 2, 1, 2, 4, 2048),
152 new RaEvent(2000, 400, 300, -1, 1000, -1)
153 };
154
155 for (int i = 0; i < events.length; i++) {
156 logger.log(100 * (i + 1), events[i]);
157 }
158
159 String want = joinLines(
160 "dropped_events: 0",
161 "events <",
Hugo Benichif6840502017-03-08 11:59:36 +0900162 " if_name: \"\"",
Hugo Benichi1af06a62017-01-16 14:42:56 +0900163 " link_layer: 0",
Hugo Benichif6840502017-03-08 11:59:36 +0900164 " network_id: 0",
Hugo Benichi80df43e2016-11-24 11:28:06 +0900165 " time_ms: 100",
Hugo Benichif6840502017-03-08 11:59:36 +0900166 " transports: 0",
Hugo Benichi00a42d42016-09-13 15:55:09 +0900167 " ip_reachability_event <",
168 " event_type: 512",
169 " if_name: \"wlan0\"",
170 " >",
Hugo Benichi00a42d42016-09-13 15:55:09 +0900171 ">",
172 "events <",
Hugo Benichif6840502017-03-08 11:59:36 +0900173 " if_name: \"\"",
Hugo Benichi1af06a62017-01-16 14:42:56 +0900174 " link_layer: 0",
Hugo Benichif6840502017-03-08 11:59:36 +0900175 " network_id: 0",
Hugo Benichi80df43e2016-11-24 11:28:06 +0900176 " time_ms: 200",
Hugo Benichif6840502017-03-08 11:59:36 +0900177 " transports: 0",
Hugo Benichi00a42d42016-09-13 15:55:09 +0900178 " dhcp_event <",
179 " duration_ms: 192",
Hugo Benichi00a42d42016-09-13 15:55:09 +0900180 " if_name: \"wlan0\"",
181 " state_transition: \"SomeState\"",
182 " >",
Hugo Benichi00a42d42016-09-13 15:55:09 +0900183 ">",
184 "events <",
Hugo Benichif6840502017-03-08 11:59:36 +0900185 " if_name: \"\"",
Hugo Benichi1af06a62017-01-16 14:42:56 +0900186 " link_layer: 0",
Hugo Benichif6840502017-03-08 11:59:36 +0900187 " network_id: 0",
Hugo Benichi80df43e2016-11-24 11:28:06 +0900188 " time_ms: 300",
Hugo Benichif6840502017-03-08 11:59:36 +0900189 " transports: 0",
Hugo Benichi00a42d42016-09-13 15:55:09 +0900190 " default_network_event <",
191 " network_id <",
192 " network_id: 102",
193 " >",
194 " previous_network_id <",
195 " network_id: 101",
196 " >",
197 " previous_network_ip_support: 1",
198 " transport_types: 1",
199 " transport_types: 2",
200 " transport_types: 3",
201 " >",
Hugo Benichi00a42d42016-09-13 15:55:09 +0900202 ">",
203 "events <",
Hugo Benichif6840502017-03-08 11:59:36 +0900204 " if_name: \"\"",
Hugo Benichi1af06a62017-01-16 14:42:56 +0900205 " link_layer: 0",
Hugo Benichif6840502017-03-08 11:59:36 +0900206 " network_id: 0",
Hugo Benichi80df43e2016-11-24 11:28:06 +0900207 " time_ms: 400",
Hugo Benichif6840502017-03-08 11:59:36 +0900208 " transports: 0",
Hugo Benichi00a42d42016-09-13 15:55:09 +0900209 " ip_provisioning_event <",
210 " event_type: 1",
211 " if_name: \"wlan0\"",
212 " latency_ms: 5678",
213 " >",
Hugo Benichi00a42d42016-09-13 15:55:09 +0900214 ">",
215 "events <",
Hugo Benichif6840502017-03-08 11:59:36 +0900216 " if_name: \"\"",
Hugo Benichi1af06a62017-01-16 14:42:56 +0900217 " link_layer: 0",
Hugo Benichif6840502017-03-08 11:59:36 +0900218 " network_id: 0",
Hugo Benichi00a42d42016-09-13 15:55:09 +0900219 " time_ms: 500",
Hugo Benichif6840502017-03-08 11:59:36 +0900220 " transports: 0",
Hugo Benichi00a42d42016-09-13 15:55:09 +0900221 " validation_probe_event <",
222 " latency_ms: 40730",
223 " network_id <",
224 " network_id: 120",
225 " >",
226 " probe_result: 204",
227 " probe_type: 1",
228 " >",
229 ">",
230 "events <",
Hugo Benichif6840502017-03-08 11:59:36 +0900231 " if_name: \"\"",
Hugo Benichi1af06a62017-01-16 14:42:56 +0900232 " link_layer: 0",
Hugo Benichif6840502017-03-08 11:59:36 +0900233 " network_id: 0",
Hugo Benichi80df43e2016-11-24 11:28:06 +0900234 " time_ms: 600",
Hugo Benichif6840502017-03-08 11:59:36 +0900235 " transports: 0",
Hugo Benichi00a42d42016-09-13 15:55:09 +0900236 " apf_statistics <",
237 " dropped_ras: 2",
238 " duration_ms: 45000",
239 " matching_ras: 2",
240 " max_program_size: 2048",
241 " parse_errors: 2",
242 " program_updates: 4",
Hugo Benichif6840502017-03-08 11:59:36 +0900243 " program_updates_all: 0",
244 " program_updates_allowing_multicast: 0",
Hugo Benichi00a42d42016-09-13 15:55:09 +0900245 " received_ras: 10",
246 " zero_lifetime_ras: 1",
247 " >",
Hugo Benichi00a42d42016-09-13 15:55:09 +0900248 ">",
249 "events <",
Hugo Benichif6840502017-03-08 11:59:36 +0900250 " if_name: \"\"",
Hugo Benichi1af06a62017-01-16 14:42:56 +0900251 " link_layer: 0",
Hugo Benichif6840502017-03-08 11:59:36 +0900252 " network_id: 0",
Hugo Benichi80df43e2016-11-24 11:28:06 +0900253 " time_ms: 700",
Hugo Benichif6840502017-03-08 11:59:36 +0900254 " transports: 0",
Hugo Benichi00a42d42016-09-13 15:55:09 +0900255 " ra_event <",
256 " dnssl_lifetime: -1",
257 " prefix_preferred_lifetime: 300",
258 " prefix_valid_lifetime: 400",
259 " rdnss_lifetime: 1000",
260 " route_info_lifetime: -1",
261 " router_lifetime: 2000",
262 " >",
Hugo Benichid680d4c2016-10-13 13:16:16 +0900263 ">",
264 "version: 2");
Hugo Benichi00a42d42016-09-13 15:55:09 +0900265
266 verifySerialization(want, getdump("flush"));
267 }
268
269 String getdump(String ... command) {
270 StringWriter buffer = new StringWriter();
271 PrintWriter writer = new PrintWriter(buffer);
272 mService.impl.dump(null, writer, command);
273 return buffer.toString();
274 }
275
276 List<ConnectivityMetricsEvent> verifyEvents(int n, int timeoutMs) throws Exception {
277 ArgumentCaptor<ConnectivityMetricsEvent> captor =
278 ArgumentCaptor.forClass(ConnectivityMetricsEvent.class);
279 verify(mMockService, timeout(timeoutMs).times(n)).logEvent(captor.capture());
280 return captor.getAllValues();
281 }
282
283 List<ConnectivityMetricsEvent> verifyEvents(int n) throws Exception {
284 return verifyEvents(n, 10);
285 }
286
287 static void verifySerialization(String want, String output) {
288 try {
289 byte[] got = Base64.decode(output, Base64.DEFAULT);
290 IpConnectivityLogClass.IpConnectivityLog log =
Glen Kuhne07199402016-10-25 15:47:25 -0700291 IpConnectivityLogClass.IpConnectivityLog.parseFrom(got);
Hugo Benichi00a42d42016-09-13 15:55:09 +0900292 assertEquals(want, log.toString());
293 } catch (Exception e) {
294 fail(e.toString());
295 }
296 }
297
298 static String joinLines(String ... elems) {
299 StringBuilder b = new StringBuilder();
300 for (String s : elems) {
301 b.append(s).append("\n");
302 }
303 return b.toString();
304 }
305
306 static ConnectivityMetricsEvent expectedEvent(int timestamp) {
307 return new ConnectivityMetricsEvent((long)timestamp, 0, 0, FAKE_EV);
308 }
309
310 /** Outer equality for ConnectivityMetricsEvent to avoid overriding equals() and hashCode(). */
311 static void assertEventsEqual(ConnectivityMetricsEvent expected, ConnectivityMetricsEvent got) {
312 assertEquals(expected.timestamp, got.timestamp);
313 assertEquals(expected.componentTag, got.componentTag);
314 assertEquals(expected.eventTag, got.eventTag);
315 assertEquals(expected.data, got.data);
316 }
317
318 static final Comparator<ConnectivityMetricsEvent> EVENT_COMPARATOR =
Hugo Benichi0d4a3982016-11-25 11:24:22 +0900319 Comparator.comparingLong((ev) -> ev.timestamp);
Hugo Benichi00a42d42016-09-13 15:55:09 +0900320}