blob: 011e505c46c4085e0b9323d0eaf19afa64a87659 [file] [log] [blame]
Hugo Benichi50a84c62016-09-02 09:00:59 +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 Benichi93c0de02016-11-24 11:28:06 +090019import static com.android.server.connectivity.MetricsTestUtil.aBool;
20import static com.android.server.connectivity.MetricsTestUtil.aByteArray;
21import static com.android.server.connectivity.MetricsTestUtil.aLong;
22import static com.android.server.connectivity.MetricsTestUtil.aString;
23import static com.android.server.connectivity.MetricsTestUtil.aType;
24import static com.android.server.connectivity.MetricsTestUtil.anInt;
25import static com.android.server.connectivity.MetricsTestUtil.anIntArray;
26import static com.android.server.connectivity.MetricsTestUtil.b;
27import static com.android.server.connectivity.MetricsTestUtil.describeIpEvent;
28import static com.android.server.connectivity.metrics.IpConnectivityLogClass.IpConnectivityLog;
29
Hugo Benichi50a84c62016-09-02 09:00:59 +090030import android.net.ConnectivityMetricsEvent;
31import android.net.metrics.ApfProgramEvent;
32import android.net.metrics.ApfStats;
33import android.net.metrics.DefaultNetworkEvent;
34import android.net.metrics.DhcpClientEvent;
35import android.net.metrics.DhcpErrorEvent;
36import android.net.metrics.DnsEvent;
37import android.net.metrics.IpManagerEvent;
38import android.net.metrics.IpReachabilityEvent;
39import android.net.metrics.NetworkEvent;
40import android.net.metrics.RaEvent;
41import android.net.metrics.ValidationProbeEvent;
Hugo Benichi93c0de02016-11-24 11:28:06 +090042import android.test.suitebuilder.annotation.SmallTest;
Hugo Benichi50a84c62016-09-02 09:00:59 +090043import java.util.Arrays;
44import junit.framework.TestCase;
45
Hugo Benichi50a84c62016-09-02 09:00:59 +090046public class IpConnectivityEventBuilderTest extends TestCase {
47
Hugo Benichi93c0de02016-11-24 11:28:06 +090048 @SmallTest
Hugo Benichi50a84c62016-09-02 09:00:59 +090049 public void testDefaultNetworkEventSerialization() {
50 ConnectivityMetricsEvent ev = describeIpEvent(
51 aType(DefaultNetworkEvent.class),
52 anInt(102),
53 anIntArray(1, 2, 3),
54 anInt(101),
55 aBool(true),
56 aBool(false));
57
58 String want = joinLines(
59 "dropped_events: 0",
60 "events <",
Hugo Benichi93c0de02016-11-24 11:28:06 +090061 " time_ms: 1",
62 " transport: 0",
Hugo Benichi50a84c62016-09-02 09:00:59 +090063 " default_network_event <",
64 " network_id <",
65 " network_id: 102",
66 " >",
67 " previous_network_id <",
68 " network_id: 101",
69 " >",
70 " previous_network_ip_support: 1",
71 " transport_types: 1",
72 " transport_types: 2",
73 " transport_types: 3",
74 " >",
Hugo Benichi31cf55b2016-10-13 13:16:16 +090075 ">",
76 "version: 2");
Hugo Benichi50a84c62016-09-02 09:00:59 +090077
78 verifySerialization(want, ev);
79 }
80
Hugo Benichi93c0de02016-11-24 11:28:06 +090081 @SmallTest
Hugo Benichi50a84c62016-09-02 09:00:59 +090082 public void testDhcpClientEventSerialization() {
83 ConnectivityMetricsEvent ev = describeIpEvent(
84 aType(DhcpClientEvent.class),
85 aString("wlan0"),
86 aString("SomeState"),
87 anInt(192));
88
89 String want = joinLines(
90 "dropped_events: 0",
91 "events <",
Hugo Benichi93c0de02016-11-24 11:28:06 +090092 " time_ms: 1",
93 " transport: 0",
Hugo Benichi50a84c62016-09-02 09:00:59 +090094 " dhcp_event <",
95 " duration_ms: 192",
Hugo Benichi50a84c62016-09-02 09:00:59 +090096 " if_name: \"wlan0\"",
97 " state_transition: \"SomeState\"",
98 " >",
Hugo Benichi31cf55b2016-10-13 13:16:16 +090099 ">",
100 "version: 2");
Hugo Benichi50a84c62016-09-02 09:00:59 +0900101
102 verifySerialization(want, ev);
103 }
104
Hugo Benichi93c0de02016-11-24 11:28:06 +0900105 @SmallTest
Hugo Benichi50a84c62016-09-02 09:00:59 +0900106 public void testDhcpErrorEventSerialization() {
107 ConnectivityMetricsEvent ev = describeIpEvent(
108 aType(DhcpErrorEvent.class),
109 aString("wlan0"),
110 anInt(DhcpErrorEvent.L4_NOT_UDP));
111
112 String want = joinLines(
113 "dropped_events: 0",
114 "events <",
Hugo Benichi93c0de02016-11-24 11:28:06 +0900115 " time_ms: 1",
116 " transport: 0",
Hugo Benichi50a84c62016-09-02 09:00:59 +0900117 " dhcp_event <",
118 " duration_ms: 0",
Hugo Benichi50a84c62016-09-02 09:00:59 +0900119 " if_name: \"wlan0\"",
Hugo Benichi93c0de02016-11-24 11:28:06 +0900120 " error_code: 50397184",
Hugo Benichi50a84c62016-09-02 09:00:59 +0900121 " >",
Hugo Benichi31cf55b2016-10-13 13:16:16 +0900122 ">",
123 "version: 2");
Hugo Benichi50a84c62016-09-02 09:00:59 +0900124
125 verifySerialization(want, ev);
126 }
127
Hugo Benichi93c0de02016-11-24 11:28:06 +0900128 @SmallTest
Hugo Benichi50a84c62016-09-02 09:00:59 +0900129 public void testDnsEventSerialization() {
130 ConnectivityMetricsEvent ev = describeIpEvent(
131 aType(DnsEvent.class),
132 anInt(101),
133 aByteArray(b(1), b(1), b(2), b(1), b(1), b(1), b(2), b(2)),
134 aByteArray(b(0), b(0), b(22), b(3), b(1), b(0), b(200), b(178)),
135 anIntArray(3456, 267, 1230, 45, 2111, 450, 638, 1300));
136
137 String want = joinLines(
138 "dropped_events: 0",
139 "events <",
Hugo Benichi93c0de02016-11-24 11:28:06 +0900140 " time_ms: 1",
141 " transport: 0",
Hugo Benichi50a84c62016-09-02 09:00:59 +0900142 " dns_lookup_batch <",
143 " event_types: 1",
144 " event_types: 1",
145 " event_types: 2",
146 " event_types: 1",
147 " event_types: 1",
148 " event_types: 1",
149 " event_types: 2",
150 " event_types: 2",
151 " latencies_ms: 3456",
152 " latencies_ms: 267",
153 " latencies_ms: 1230",
154 " latencies_ms: 45",
155 " latencies_ms: 2111",
156 " latencies_ms: 450",
157 " latencies_ms: 638",
158 " latencies_ms: 1300",
159 " network_id <",
160 " network_id: 101",
161 " >",
162 " return_codes: 0",
163 " return_codes: 0",
164 " return_codes: 22",
165 " return_codes: 3",
166 " return_codes: 1",
167 " return_codes: 0",
168 " return_codes: 200",
169 " return_codes: 178",
170 " >",
Hugo Benichi31cf55b2016-10-13 13:16:16 +0900171 ">",
172 "version: 2");
Hugo Benichi50a84c62016-09-02 09:00:59 +0900173
174 verifySerialization(want, ev);
175 }
176
Hugo Benichi93c0de02016-11-24 11:28:06 +0900177 @SmallTest
Hugo Benichi50a84c62016-09-02 09:00:59 +0900178 public void testIpManagerEventSerialization() {
179 ConnectivityMetricsEvent ev = describeIpEvent(
180 aType(IpManagerEvent.class),
181 aString("wlan0"),
182 anInt(IpManagerEvent.PROVISIONING_OK),
183 aLong(5678));
184
185 String want = joinLines(
186 "dropped_events: 0",
187 "events <",
Hugo Benichi93c0de02016-11-24 11:28:06 +0900188 " time_ms: 1",
189 " transport: 0",
Hugo Benichi50a84c62016-09-02 09:00:59 +0900190 " ip_provisioning_event <",
191 " event_type: 1",
192 " if_name: \"wlan0\"",
193 " latency_ms: 5678",
194 " >",
Hugo Benichi31cf55b2016-10-13 13:16:16 +0900195 ">",
196 "version: 2");
Hugo Benichi50a84c62016-09-02 09:00:59 +0900197
198 verifySerialization(want, ev);
199 }
200
Hugo Benichi93c0de02016-11-24 11:28:06 +0900201 @SmallTest
Hugo Benichi50a84c62016-09-02 09:00:59 +0900202 public void testIpReachabilityEventSerialization() {
203 ConnectivityMetricsEvent ev = describeIpEvent(
204 aType(IpReachabilityEvent.class),
205 aString("wlan0"),
206 anInt(IpReachabilityEvent.NUD_FAILED));
207
208 String want = joinLines(
209 "dropped_events: 0",
210 "events <",
Hugo Benichi93c0de02016-11-24 11:28:06 +0900211 " time_ms: 1",
212 " transport: 0",
Hugo Benichi50a84c62016-09-02 09:00:59 +0900213 " ip_reachability_event <",
214 " event_type: 512",
215 " if_name: \"wlan0\"",
216 " >",
Hugo Benichi31cf55b2016-10-13 13:16:16 +0900217 ">",
218 "version: 2");
Hugo Benichi50a84c62016-09-02 09:00:59 +0900219
220 verifySerialization(want, ev);
221 }
222
Hugo Benichi93c0de02016-11-24 11:28:06 +0900223 @SmallTest
Hugo Benichi50a84c62016-09-02 09:00:59 +0900224 public void testNetworkEventSerialization() {
225 ConnectivityMetricsEvent ev = describeIpEvent(
226 aType(NetworkEvent.class),
227 anInt(100),
228 anInt(5),
229 aLong(20410));
230
231 String want = joinLines(
232 "dropped_events: 0",
233 "events <",
Hugo Benichi93c0de02016-11-24 11:28:06 +0900234 " time_ms: 1",
235 " transport: 0",
Hugo Benichi50a84c62016-09-02 09:00:59 +0900236 " network_event <",
237 " event_type: 5",
238 " latency_ms: 20410",
239 " network_id <",
240 " network_id: 100",
241 " >",
242 " >",
Hugo Benichi31cf55b2016-10-13 13:16:16 +0900243 ">",
244 "version: 2");
Hugo Benichi50a84c62016-09-02 09:00:59 +0900245
246 verifySerialization(want, ev);
247 }
248
Hugo Benichi93c0de02016-11-24 11:28:06 +0900249 @SmallTest
Hugo Benichi50a84c62016-09-02 09:00:59 +0900250 public void testValidationProbeEventSerialization() {
251 ConnectivityMetricsEvent ev = describeIpEvent(
252 aType(ValidationProbeEvent.class),
253 anInt(120),
254 aLong(40730),
255 anInt(ValidationProbeEvent.PROBE_HTTP),
256 anInt(204));
257
258 String want = joinLines(
259 "dropped_events: 0",
260 "events <",
261 " time_ms: 1",
Hugo Benichi93c0de02016-11-24 11:28:06 +0900262 " transport: 0",
Hugo Benichi50a84c62016-09-02 09:00:59 +0900263 " validation_probe_event <",
264 " latency_ms: 40730",
265 " network_id <",
266 " network_id: 120",
267 " >",
268 " probe_result: 204",
269 " probe_type: 1",
270 " >",
Hugo Benichi31cf55b2016-10-13 13:16:16 +0900271 ">",
272 "version: 2");
Hugo Benichi50a84c62016-09-02 09:00:59 +0900273
274 verifySerialization(want, ev);
275 }
276
Hugo Benichi93c0de02016-11-24 11:28:06 +0900277 @SmallTest
Hugo Benichi50a84c62016-09-02 09:00:59 +0900278 public void testApfProgramEventSerialization() {
279 ConnectivityMetricsEvent ev = describeIpEvent(
280 aType(ApfProgramEvent.class),
281 aLong(200),
282 anInt(7),
283 anInt(9),
284 anInt(2048),
285 anInt(3));
286
287 String want = joinLines(
288 "dropped_events: 0",
289 "events <",
Hugo Benichi93c0de02016-11-24 11:28:06 +0900290 " time_ms: 1",
291 " transport: 0",
Hugo Benichi50a84c62016-09-02 09:00:59 +0900292 " apf_program_event <",
293 " current_ras: 9",
294 " drop_multicast: true",
295 " filtered_ras: 7",
296 " has_ipv4_addr: true",
297 " lifetime: 200",
298 " program_length: 2048",
299 " >",
Hugo Benichi31cf55b2016-10-13 13:16:16 +0900300 ">",
301 "version: 2");
Hugo Benichi50a84c62016-09-02 09:00:59 +0900302
303 verifySerialization(want, ev);
304 }
305
Hugo Benichi93c0de02016-11-24 11:28:06 +0900306 @SmallTest
Hugo Benichi50a84c62016-09-02 09:00:59 +0900307 public void testApfStatsSerialization() {
308 ConnectivityMetricsEvent ev = describeIpEvent(
309 aType(ApfStats.class),
310 aLong(45000),
311 anInt(10),
312 anInt(2),
313 anInt(2),
314 anInt(1),
315 anInt(2),
316 anInt(4),
317 anInt(2048));
318
319 String want = joinLines(
320 "dropped_events: 0",
321 "events <",
Hugo Benichi93c0de02016-11-24 11:28:06 +0900322 " time_ms: 1",
323 " transport: 0",
Hugo Benichi50a84c62016-09-02 09:00:59 +0900324 " apf_statistics <",
325 " dropped_ras: 2",
326 " duration_ms: 45000",
327 " matching_ras: 2",
328 " max_program_size: 2048",
329 " parse_errors: 2",
330 " program_updates: 4",
331 " received_ras: 10",
332 " zero_lifetime_ras: 1",
333 " >",
Hugo Benichi31cf55b2016-10-13 13:16:16 +0900334 ">",
335 "version: 2");
Hugo Benichi50a84c62016-09-02 09:00:59 +0900336
337 verifySerialization(want, ev);
338 }
339
Hugo Benichi93c0de02016-11-24 11:28:06 +0900340 @SmallTest
Hugo Benichi50a84c62016-09-02 09:00:59 +0900341 public void testRaEventSerialization() {
342 ConnectivityMetricsEvent ev = describeIpEvent(
343 aType(RaEvent.class),
344 aLong(2000),
345 aLong(400),
346 aLong(300),
347 aLong(-1),
348 aLong(1000),
349 aLong(-1));
350
351 String want = joinLines(
352 "dropped_events: 0",
353 "events <",
Hugo Benichi93c0de02016-11-24 11:28:06 +0900354 " time_ms: 1",
355 " transport: 0",
Hugo Benichi50a84c62016-09-02 09:00:59 +0900356 " ra_event <",
357 " dnssl_lifetime: -1",
358 " prefix_preferred_lifetime: 300",
359 " prefix_valid_lifetime: 400",
360 " rdnss_lifetime: 1000",
361 " route_info_lifetime: -1",
362 " router_lifetime: 2000",
363 " >",
Hugo Benichi31cf55b2016-10-13 13:16:16 +0900364 ">",
365 "version: 2");
Hugo Benichi50a84c62016-09-02 09:00:59 +0900366
367 verifySerialization(want, ev);
368 }
369
370 static void verifySerialization(String want, ConnectivityMetricsEvent... input) {
371 try {
Hugo Benichi2299a1c2016-11-25 11:24:22 +0900372 byte[] got = IpConnectivityEventBuilder.serialize(0,
373 IpConnectivityEventBuilder.toProto(Arrays.asList(input)));
Hugo Benichi93c0de02016-11-24 11:28:06 +0900374 IpConnectivityLog log = IpConnectivityLog.parseFrom(got);
Hugo Benichi50a84c62016-09-02 09:00:59 +0900375 assertEquals(want, log.toString());
376 } catch (Exception e) {
377 fail(e.toString());
378 }
379 }
380
381 static String joinLines(String ... elems) {
382 StringBuilder b = new StringBuilder();
383 for (String s : elems) {
384 b.append(s);
385 b.append("\n");
386 }
387 return b.toString();
388 }
389}