blob: 0656c5fd9e944e3da8b230c710560ef5d0da4063 [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 Benichi60c9f632017-09-05 13:34:48 +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 com.android.server.connectivity.MetricsTestUtil.aBool;
22import static com.android.server.connectivity.MetricsTestUtil.aByteArray;
23import static com.android.server.connectivity.MetricsTestUtil.aLong;
24import static com.android.server.connectivity.MetricsTestUtil.aString;
25import static com.android.server.connectivity.MetricsTestUtil.aType;
26import static com.android.server.connectivity.MetricsTestUtil.anInt;
27import static com.android.server.connectivity.MetricsTestUtil.anIntArray;
28import static com.android.server.connectivity.MetricsTestUtil.b;
29import static com.android.server.connectivity.MetricsTestUtil.describeIpEvent;
Tamas Berghammer383db5eb2016-06-22 15:21:38 +010030import static com.android.server.connectivity.metrics.nano.IpConnectivityLogClass.IpConnectivityLog;
Hugo Benichi73fd4d12017-03-15 23:05:01 +090031import static com.android.server.connectivity.metrics.nano.IpConnectivityLogClass.BLUETOOTH;
32import static com.android.server.connectivity.metrics.nano.IpConnectivityLogClass.CELLULAR;
33import static com.android.server.connectivity.metrics.nano.IpConnectivityLogClass.ETHERNET;
34import static com.android.server.connectivity.metrics.nano.IpConnectivityLogClass.MULTIPLE;
35import static com.android.server.connectivity.metrics.nano.IpConnectivityLogClass.WIFI;
Hugo Benichi60c9f632017-09-05 13:34:48 +090036import static org.junit.Assert.assertEquals;
37import static org.junit.Assert.fail;
Glen Kuhne07199402016-10-25 15:47:25 -070038
Hugo Benichi50a84c62016-09-02 09:00:59 +090039import android.net.ConnectivityMetricsEvent;
40import android.net.metrics.ApfProgramEvent;
41import android.net.metrics.ApfStats;
Hugo Benichi60c9f632017-09-05 13:34:48 +090042import android.net.metrics.ConnectStats;
Hugo Benichi50a84c62016-09-02 09:00:59 +090043import android.net.metrics.DefaultNetworkEvent;
44import android.net.metrics.DhcpClientEvent;
45import android.net.metrics.DhcpErrorEvent;
46import android.net.metrics.DnsEvent;
Hugo Benichi60c9f632017-09-05 13:34:48 +090047import android.net.metrics.DnsEvent;
Hugo Benichi50a84c62016-09-02 09:00:59 +090048import android.net.metrics.IpManagerEvent;
49import android.net.metrics.IpReachabilityEvent;
50import android.net.metrics.NetworkEvent;
51import android.net.metrics.RaEvent;
52import android.net.metrics.ValidationProbeEvent;
Hugo Benichi60c9f632017-09-05 13:34:48 +090053import android.net.metrics.WakeupStats;
54import android.support.test.runner.AndroidJUnit4;
Hugo Benichi80df43e2016-11-24 11:28:06 +090055import android.test.suitebuilder.annotation.SmallTest;
Hugo Benichi60c9f632017-09-05 13:34:48 +090056
Hugo Benichi5eb90532017-03-23 18:38:22 +090057import com.android.server.connectivity.metrics.nano.IpConnectivityLogClass.IpConnectivityEvent;
Hugo Benichi60c9f632017-09-05 13:34:48 +090058
Glen Kuhne07199402016-10-25 15:47:25 -070059import java.util.Arrays;
Hugo Benichi5eb90532017-03-23 18:38:22 +090060import java.util.List;
Hugo Benichi60c9f632017-09-05 13:34:48 +090061
62import org.junit.runner.RunWith;
63import org.junit.Test;
Hugo Benichi50a84c62016-09-02 09:00:59 +090064
Hugo Benichif6840502017-03-08 11:59:36 +090065// TODO: instead of comparing textpb to textpb, parse textpb and compare proto to proto.
Hugo Benichi60c9f632017-09-05 13:34:48 +090066@RunWith(AndroidJUnit4.class)
67@SmallTest
68public class IpConnectivityEventBuilderTest {
Hugo Benichi50a84c62016-09-02 09:00:59 +090069
Hugo Benichi60c9f632017-09-05 13:34:48 +090070 @Test
Hugo Benichi73fd4d12017-03-15 23:05:01 +090071 public void testLinkLayerInferrence() {
72 ConnectivityMetricsEvent ev = describeIpEvent(
73 aType(IpReachabilityEvent.class),
Hugo Benichi73fd4d12017-03-15 23:05:01 +090074 anInt(IpReachabilityEvent.NUD_FAILED));
75
Hugo Benichi2a5cfb92017-03-22 22:21:44 +090076 String want = String.join("\n",
Hugo Benichi73fd4d12017-03-15 23:05:01 +090077 "dropped_events: 0",
78 "events <",
79 " if_name: \"\"",
80 " link_layer: 0",
81 " network_id: 0",
82 " time_ms: 1",
83 " transports: 0",
84 " ip_reachability_event <",
85 " event_type: 512",
Hugo Benichi948a8592017-03-16 16:33:47 +090086 " if_name: \"\"",
Hugo Benichi73fd4d12017-03-15 23:05:01 +090087 " >",
88 ">",
Hugo Benichi2a5cfb92017-03-22 22:21:44 +090089 "version: 2\n");
Hugo Benichi73fd4d12017-03-15 23:05:01 +090090 verifySerialization(want, ev);
91
92 ev.netId = 123;
93 ev.transports = 3; // transports have priority for inferrence of link layer
94 ev.ifname = "wlan0";
Hugo Benichi2a5cfb92017-03-22 22:21:44 +090095 want = String.join("\n",
Hugo Benichi73fd4d12017-03-15 23:05:01 +090096 "dropped_events: 0",
97 "events <",
98 " if_name: \"\"",
99 String.format(" link_layer: %d", MULTIPLE),
100 " network_id: 123",
101 " time_ms: 1",
102 " transports: 3",
103 " ip_reachability_event <",
104 " event_type: 512",
Hugo Benichi948a8592017-03-16 16:33:47 +0900105 " if_name: \"\"",
Hugo Benichi73fd4d12017-03-15 23:05:01 +0900106 " >",
107 ">",
Hugo Benichi2a5cfb92017-03-22 22:21:44 +0900108 "version: 2\n");
Hugo Benichi73fd4d12017-03-15 23:05:01 +0900109 verifySerialization(want, ev);
110
111 ev.transports = 1;
112 ev.ifname = null;
Hugo Benichi2a5cfb92017-03-22 22:21:44 +0900113 want = String.join("\n",
Hugo Benichi73fd4d12017-03-15 23:05:01 +0900114 "dropped_events: 0",
115 "events <",
116 " if_name: \"\"",
117 String.format(" link_layer: %d", CELLULAR),
118 " network_id: 123",
119 " time_ms: 1",
120 " transports: 1",
121 " ip_reachability_event <",
122 " event_type: 512",
Hugo Benichi948a8592017-03-16 16:33:47 +0900123 " if_name: \"\"",
Hugo Benichi73fd4d12017-03-15 23:05:01 +0900124 " >",
125 ">",
Hugo Benichi2a5cfb92017-03-22 22:21:44 +0900126 "version: 2\n");
Hugo Benichi73fd4d12017-03-15 23:05:01 +0900127 verifySerialization(want, ev);
128
129 ev.transports = 0;
130 ev.ifname = "not_inferred";
Hugo Benichi2a5cfb92017-03-22 22:21:44 +0900131 want = String.join("\n",
Hugo Benichi73fd4d12017-03-15 23:05:01 +0900132 "dropped_events: 0",
133 "events <",
134 " if_name: \"not_inferred\"",
135 " link_layer: 0",
136 " network_id: 123",
137 " time_ms: 1",
138 " transports: 0",
139 " ip_reachability_event <",
140 " event_type: 512",
Hugo Benichi948a8592017-03-16 16:33:47 +0900141 " if_name: \"\"",
Hugo Benichi73fd4d12017-03-15 23:05:01 +0900142 " >",
143 ">",
Hugo Benichi2a5cfb92017-03-22 22:21:44 +0900144 "version: 2\n");
Hugo Benichi73fd4d12017-03-15 23:05:01 +0900145 verifySerialization(want, ev);
146
147 ev.ifname = "bt-pan";
Hugo Benichi2a5cfb92017-03-22 22:21:44 +0900148 want = String.join("\n",
Hugo Benichi73fd4d12017-03-15 23:05:01 +0900149 "dropped_events: 0",
150 "events <",
151 " if_name: \"\"",
152 String.format(" link_layer: %d", BLUETOOTH),
153 " network_id: 123",
154 " time_ms: 1",
155 " transports: 0",
156 " ip_reachability_event <",
157 " event_type: 512",
Hugo Benichi948a8592017-03-16 16:33:47 +0900158 " if_name: \"\"",
Hugo Benichi73fd4d12017-03-15 23:05:01 +0900159 " >",
160 ">",
Hugo Benichi2a5cfb92017-03-22 22:21:44 +0900161 "version: 2\n");
Hugo Benichi73fd4d12017-03-15 23:05:01 +0900162 verifySerialization(want, ev);
163
164 ev.ifname = "rmnet_ipa0";
Hugo Benichi2a5cfb92017-03-22 22:21:44 +0900165 want = String.join("\n",
Hugo Benichi73fd4d12017-03-15 23:05:01 +0900166 "dropped_events: 0",
167 "events <",
168 " if_name: \"\"",
169 String.format(" link_layer: %d", CELLULAR),
170 " network_id: 123",
171 " time_ms: 1",
172 " transports: 0",
173 " ip_reachability_event <",
174 " event_type: 512",
Hugo Benichi948a8592017-03-16 16:33:47 +0900175 " if_name: \"\"",
Hugo Benichi73fd4d12017-03-15 23:05:01 +0900176 " >",
177 ">",
Hugo Benichi2a5cfb92017-03-22 22:21:44 +0900178 "version: 2\n");
Hugo Benichi73fd4d12017-03-15 23:05:01 +0900179 verifySerialization(want, ev);
180
181 ev.ifname = "wlan0";
Hugo Benichi2a5cfb92017-03-22 22:21:44 +0900182 want = String.join("\n",
Hugo Benichi73fd4d12017-03-15 23:05:01 +0900183 "dropped_events: 0",
184 "events <",
185 " if_name: \"\"",
186 String.format(" link_layer: %d", WIFI),
187 " network_id: 123",
188 " time_ms: 1",
189 " transports: 0",
190 " ip_reachability_event <",
191 " event_type: 512",
Hugo Benichi948a8592017-03-16 16:33:47 +0900192 " if_name: \"\"",
Hugo Benichi73fd4d12017-03-15 23:05:01 +0900193 " >",
194 ">",
Hugo Benichi2a5cfb92017-03-22 22:21:44 +0900195 "version: 2\n");
Hugo Benichi73fd4d12017-03-15 23:05:01 +0900196 verifySerialization(want, ev);
197 }
198
Hugo Benichi60c9f632017-09-05 13:34:48 +0900199 @Test
Hugo Benichi50a84c62016-09-02 09:00:59 +0900200 public void testDefaultNetworkEventSerialization() {
Hugo Benichi380a0632017-10-20 09:25:29 +0900201 DefaultNetworkEvent ev = new DefaultNetworkEvent(1001);
Hugo Benichi1193a9c2017-10-19 14:58:15 +0900202 ev.netId = 102;
Hugo Benichi380a0632017-10-20 09:25:29 +0900203 ev.transports = 2;
204 ev.previousTransports = 4;
205 ev.ipv4 = true;
206 ev.initialScore = 20;
207 ev.finalScore = 60;
208 ev.durationMs = 54;
209 ev.validatedMs = 27;
Hugo Benichi50a84c62016-09-02 09:00:59 +0900210
Hugo Benichi2a5cfb92017-03-22 22:21:44 +0900211 String want = String.join("\n",
Hugo Benichi50a84c62016-09-02 09:00:59 +0900212 "dropped_events: 0",
213 "events <",
Hugo Benichif6840502017-03-08 11:59:36 +0900214 " if_name: \"\"",
Hugo Benichi380a0632017-10-20 09:25:29 +0900215 " link_layer: 4",
Hugo Benichi1193a9c2017-10-19 14:58:15 +0900216 " network_id: 102",
217 " time_ms: 0",
Hugo Benichi380a0632017-10-20 09:25:29 +0900218 " transports: 2",
Hugo Benichi50a84c62016-09-02 09:00:59 +0900219 " default_network_event <",
Hugo Benichi380a0632017-10-20 09:25:29 +0900220 " default_network_duration_ms: 54",
221 " final_score: 60",
222 " initial_score: 20",
223 " ip_support: 1",
Hugo Benichi4eccf782017-07-18 14:28:27 +0900224 " no_default_network_duration_ms: 0",
Hugo Benichi380a0632017-10-20 09:25:29 +0900225 " previous_default_network_link_layer: 1",
226 " previous_network_ip_support: 0",
227 " validation_duration_ms: 27",
Hugo Benichi50a84c62016-09-02 09:00:59 +0900228 " >",
Hugo Benichid680d4c2016-10-13 13:16:16 +0900229 ">",
Hugo Benichi2a5cfb92017-03-22 22:21:44 +0900230 "version: 2\n");
Hugo Benichi50a84c62016-09-02 09:00:59 +0900231
Hugo Benichi1193a9c2017-10-19 14:58:15 +0900232 verifySerialization(want, IpConnectivityEventBuilder.toProto(ev));
Hugo Benichi50a84c62016-09-02 09:00:59 +0900233 }
234
Hugo Benichi60c9f632017-09-05 13:34:48 +0900235 @Test
Hugo Benichi50a84c62016-09-02 09:00:59 +0900236 public void testDhcpClientEventSerialization() {
237 ConnectivityMetricsEvent ev = describeIpEvent(
238 aType(DhcpClientEvent.class),
Hugo Benichi50a84c62016-09-02 09:00:59 +0900239 aString("SomeState"),
240 anInt(192));
241
Hugo Benichi2a5cfb92017-03-22 22:21:44 +0900242 String want = String.join("\n",
Hugo Benichi50a84c62016-09-02 09:00:59 +0900243 "dropped_events: 0",
244 "events <",
Hugo Benichif6840502017-03-08 11:59:36 +0900245 " if_name: \"\"",
Hugo Benichi1af06a62017-01-16 14:42:56 +0900246 " link_layer: 0",
Hugo Benichif6840502017-03-08 11:59:36 +0900247 " network_id: 0",
Hugo Benichi80df43e2016-11-24 11:28:06 +0900248 " time_ms: 1",
Hugo Benichif6840502017-03-08 11:59:36 +0900249 " transports: 0",
Hugo Benichi50a84c62016-09-02 09:00:59 +0900250 " dhcp_event <",
251 " duration_ms: 192",
Hugo Benichi948a8592017-03-16 16:33:47 +0900252 " if_name: \"\"",
Hugo Benichi50a84c62016-09-02 09:00:59 +0900253 " state_transition: \"SomeState\"",
254 " >",
Hugo Benichid680d4c2016-10-13 13:16:16 +0900255 ">",
Hugo Benichi2a5cfb92017-03-22 22:21:44 +0900256 "version: 2\n");
Hugo Benichi50a84c62016-09-02 09:00:59 +0900257
258 verifySerialization(want, ev);
259 }
260
Hugo Benichi60c9f632017-09-05 13:34:48 +0900261 @Test
Hugo Benichi50a84c62016-09-02 09:00:59 +0900262 public void testDhcpErrorEventSerialization() {
263 ConnectivityMetricsEvent ev = describeIpEvent(
264 aType(DhcpErrorEvent.class),
Hugo Benichi50a84c62016-09-02 09:00:59 +0900265 anInt(DhcpErrorEvent.L4_NOT_UDP));
266
Hugo Benichi2a5cfb92017-03-22 22:21:44 +0900267 String want = String.join("\n",
Hugo Benichi50a84c62016-09-02 09:00:59 +0900268 "dropped_events: 0",
269 "events <",
Hugo Benichif6840502017-03-08 11:59:36 +0900270 " if_name: \"\"",
Hugo Benichi1af06a62017-01-16 14:42:56 +0900271 " link_layer: 0",
Hugo Benichif6840502017-03-08 11:59:36 +0900272 " network_id: 0",
Hugo Benichi80df43e2016-11-24 11:28:06 +0900273 " time_ms: 1",
Hugo Benichif6840502017-03-08 11:59:36 +0900274 " transports: 0",
Hugo Benichi50a84c62016-09-02 09:00:59 +0900275 " dhcp_event <",
276 " duration_ms: 0",
Hugo Benichi948a8592017-03-16 16:33:47 +0900277 " if_name: \"\"",
Hugo Benichi80df43e2016-11-24 11:28:06 +0900278 " error_code: 50397184",
Hugo Benichi50a84c62016-09-02 09:00:59 +0900279 " >",
Hugo Benichid680d4c2016-10-13 13:16:16 +0900280 ">",
Hugo Benichi2a5cfb92017-03-22 22:21:44 +0900281 "version: 2\n");
Hugo Benichi50a84c62016-09-02 09:00:59 +0900282
283 verifySerialization(want, ev);
284 }
285
Hugo Benichi60c9f632017-09-05 13:34:48 +0900286 @Test
Hugo Benichi50a84c62016-09-02 09:00:59 +0900287 public void testIpManagerEventSerialization() {
288 ConnectivityMetricsEvent ev = describeIpEvent(
289 aType(IpManagerEvent.class),
Hugo Benichi50a84c62016-09-02 09:00:59 +0900290 anInt(IpManagerEvent.PROVISIONING_OK),
291 aLong(5678));
292
Hugo Benichi2a5cfb92017-03-22 22:21:44 +0900293 String want = String.join("\n",
Hugo Benichi50a84c62016-09-02 09:00:59 +0900294 "dropped_events: 0",
295 "events <",
Hugo Benichif6840502017-03-08 11:59:36 +0900296 " if_name: \"\"",
Hugo Benichi1af06a62017-01-16 14:42:56 +0900297 " link_layer: 0",
Hugo Benichif6840502017-03-08 11:59:36 +0900298 " network_id: 0",
Hugo Benichi80df43e2016-11-24 11:28:06 +0900299 " time_ms: 1",
Hugo Benichif6840502017-03-08 11:59:36 +0900300 " transports: 0",
Hugo Benichi50a84c62016-09-02 09:00:59 +0900301 " ip_provisioning_event <",
302 " event_type: 1",
Hugo Benichi948a8592017-03-16 16:33:47 +0900303 " if_name: \"\"",
Hugo Benichi50a84c62016-09-02 09:00:59 +0900304 " latency_ms: 5678",
305 " >",
Hugo Benichid680d4c2016-10-13 13:16:16 +0900306 ">",
Hugo Benichi2a5cfb92017-03-22 22:21:44 +0900307 "version: 2\n");
Hugo Benichi50a84c62016-09-02 09:00:59 +0900308
309 verifySerialization(want, ev);
310 }
311
Hugo Benichi60c9f632017-09-05 13:34:48 +0900312 @Test
Hugo Benichi50a84c62016-09-02 09:00:59 +0900313 public void testIpReachabilityEventSerialization() {
314 ConnectivityMetricsEvent ev = describeIpEvent(
315 aType(IpReachabilityEvent.class),
Hugo Benichi50a84c62016-09-02 09:00:59 +0900316 anInt(IpReachabilityEvent.NUD_FAILED));
317
Hugo Benichi2a5cfb92017-03-22 22:21:44 +0900318 String want = String.join("\n",
Hugo Benichi50a84c62016-09-02 09:00:59 +0900319 "dropped_events: 0",
320 "events <",
Hugo Benichif6840502017-03-08 11:59:36 +0900321 " if_name: \"\"",
Hugo Benichi1af06a62017-01-16 14:42:56 +0900322 " link_layer: 0",
Hugo Benichif6840502017-03-08 11:59:36 +0900323 " network_id: 0",
Hugo Benichi80df43e2016-11-24 11:28:06 +0900324 " time_ms: 1",
Hugo Benichif6840502017-03-08 11:59:36 +0900325 " transports: 0",
Hugo Benichi50a84c62016-09-02 09:00:59 +0900326 " ip_reachability_event <",
327 " event_type: 512",
Hugo Benichi948a8592017-03-16 16:33:47 +0900328 " if_name: \"\"",
Hugo Benichi50a84c62016-09-02 09:00:59 +0900329 " >",
Hugo Benichid680d4c2016-10-13 13:16:16 +0900330 ">",
Hugo Benichi2a5cfb92017-03-22 22:21:44 +0900331 "version: 2\n");
Hugo Benichi50a84c62016-09-02 09:00:59 +0900332
333 verifySerialization(want, ev);
334 }
335
Hugo Benichi60c9f632017-09-05 13:34:48 +0900336 @Test
Hugo Benichi50a84c62016-09-02 09:00:59 +0900337 public void testNetworkEventSerialization() {
338 ConnectivityMetricsEvent ev = describeIpEvent(
339 aType(NetworkEvent.class),
Hugo Benichi50a84c62016-09-02 09:00:59 +0900340 anInt(5),
341 aLong(20410));
342
Hugo Benichi2a5cfb92017-03-22 22:21:44 +0900343 String want = String.join("\n",
Hugo Benichi50a84c62016-09-02 09:00:59 +0900344 "dropped_events: 0",
345 "events <",
Hugo Benichif6840502017-03-08 11:59:36 +0900346 " if_name: \"\"",
Hugo Benichi1af06a62017-01-16 14:42:56 +0900347 " link_layer: 0",
Hugo Benichif6840502017-03-08 11:59:36 +0900348 " network_id: 0",
Hugo Benichi80df43e2016-11-24 11:28:06 +0900349 " time_ms: 1",
Hugo Benichif6840502017-03-08 11:59:36 +0900350 " transports: 0",
Hugo Benichi50a84c62016-09-02 09:00:59 +0900351 " network_event <",
352 " event_type: 5",
353 " latency_ms: 20410",
Hugo Benichi50a84c62016-09-02 09:00:59 +0900354 " >",
Hugo Benichid680d4c2016-10-13 13:16:16 +0900355 ">",
Hugo Benichi2a5cfb92017-03-22 22:21:44 +0900356 "version: 2\n");
Hugo Benichi50a84c62016-09-02 09:00:59 +0900357
358 verifySerialization(want, ev);
359 }
360
Hugo Benichi60c9f632017-09-05 13:34:48 +0900361 @Test
Hugo Benichi50a84c62016-09-02 09:00:59 +0900362 public void testValidationProbeEventSerialization() {
363 ConnectivityMetricsEvent ev = describeIpEvent(
364 aType(ValidationProbeEvent.class),
Hugo Benichi50a84c62016-09-02 09:00:59 +0900365 aLong(40730),
366 anInt(ValidationProbeEvent.PROBE_HTTP),
367 anInt(204));
368
Hugo Benichi2a5cfb92017-03-22 22:21:44 +0900369 String want = String.join("\n",
Hugo Benichi50a84c62016-09-02 09:00:59 +0900370 "dropped_events: 0",
371 "events <",
Hugo Benichif6840502017-03-08 11:59:36 +0900372 " if_name: \"\"",
Hugo Benichi1af06a62017-01-16 14:42:56 +0900373 " link_layer: 0",
Hugo Benichif6840502017-03-08 11:59:36 +0900374 " network_id: 0",
Hugo Benichi50a84c62016-09-02 09:00:59 +0900375 " time_ms: 1",
Hugo Benichif6840502017-03-08 11:59:36 +0900376 " transports: 0",
Hugo Benichi50a84c62016-09-02 09:00:59 +0900377 " validation_probe_event <",
378 " latency_ms: 40730",
Hugo Benichi50a84c62016-09-02 09:00:59 +0900379 " probe_result: 204",
380 " probe_type: 1",
381 " >",
Hugo Benichid680d4c2016-10-13 13:16:16 +0900382 ">",
Hugo Benichi2a5cfb92017-03-22 22:21:44 +0900383 "version: 2\n");
Hugo Benichi50a84c62016-09-02 09:00:59 +0900384
385 verifySerialization(want, ev);
386 }
387
Hugo Benichi60c9f632017-09-05 13:34:48 +0900388 @Test
Hugo Benichi50a84c62016-09-02 09:00:59 +0900389 public void testApfProgramEventSerialization() {
390 ConnectivityMetricsEvent ev = describeIpEvent(
391 aType(ApfProgramEvent.class),
392 aLong(200),
Hugo Benichi22d9b2d2017-02-22 13:02:27 +0900393 aLong(18),
Hugo Benichi50a84c62016-09-02 09:00:59 +0900394 anInt(7),
395 anInt(9),
396 anInt(2048),
397 anInt(3));
398
Hugo Benichi2a5cfb92017-03-22 22:21:44 +0900399 String want = String.join("\n",
Hugo Benichi50a84c62016-09-02 09:00:59 +0900400 "dropped_events: 0",
401 "events <",
Hugo Benichif6840502017-03-08 11:59:36 +0900402 " if_name: \"\"",
Hugo Benichi1af06a62017-01-16 14:42:56 +0900403 " link_layer: 0",
Hugo Benichif6840502017-03-08 11:59:36 +0900404 " network_id: 0",
Hugo Benichi80df43e2016-11-24 11:28:06 +0900405 " time_ms: 1",
Hugo Benichif6840502017-03-08 11:59:36 +0900406 " transports: 0",
Hugo Benichi50a84c62016-09-02 09:00:59 +0900407 " apf_program_event <",
408 " current_ras: 9",
409 " drop_multicast: true",
Hugo Benichi22d9b2d2017-02-22 13:02:27 +0900410 " effective_lifetime: 18",
Hugo Benichi50a84c62016-09-02 09:00:59 +0900411 " filtered_ras: 7",
412 " has_ipv4_addr: true",
413 " lifetime: 200",
414 " program_length: 2048",
415 " >",
Hugo Benichid680d4c2016-10-13 13:16:16 +0900416 ">",
Hugo Benichi2a5cfb92017-03-22 22:21:44 +0900417 "version: 2\n");
Hugo Benichi50a84c62016-09-02 09:00:59 +0900418
419 verifySerialization(want, ev);
420 }
421
Hugo Benichi60c9f632017-09-05 13:34:48 +0900422 @Test
Hugo Benichi50a84c62016-09-02 09:00:59 +0900423 public void testApfStatsSerialization() {
424 ConnectivityMetricsEvent ev = describeIpEvent(
425 aType(ApfStats.class),
426 aLong(45000),
427 anInt(10),
428 anInt(2),
429 anInt(2),
430 anInt(1),
431 anInt(2),
432 anInt(4),
Hugo Benichi22d9b2d2017-02-22 13:02:27 +0900433 anInt(7),
434 anInt(3),
Hugo Benichi50a84c62016-09-02 09:00:59 +0900435 anInt(2048));
436
Hugo Benichi2a5cfb92017-03-22 22:21:44 +0900437 String want = String.join("\n",
Hugo Benichi50a84c62016-09-02 09:00:59 +0900438 "dropped_events: 0",
439 "events <",
Hugo Benichif6840502017-03-08 11:59:36 +0900440 " if_name: \"\"",
Hugo Benichi1af06a62017-01-16 14:42:56 +0900441 " link_layer: 0",
Hugo Benichif6840502017-03-08 11:59:36 +0900442 " network_id: 0",
Hugo Benichi80df43e2016-11-24 11:28:06 +0900443 " time_ms: 1",
Hugo Benichif6840502017-03-08 11:59:36 +0900444 " transports: 0",
Hugo Benichi50a84c62016-09-02 09:00:59 +0900445 " apf_statistics <",
446 " dropped_ras: 2",
447 " duration_ms: 45000",
448 " matching_ras: 2",
449 " max_program_size: 2048",
450 " parse_errors: 2",
451 " program_updates: 4",
Hugo Benichi22d9b2d2017-02-22 13:02:27 +0900452 " program_updates_all: 7",
453 " program_updates_allowing_multicast: 3",
Hugo Benichi50a84c62016-09-02 09:00:59 +0900454 " received_ras: 10",
Hugo Benichi4eccf782017-07-18 14:28:27 +0900455 " total_packet_dropped: 0",
456 " total_packet_processed: 0",
Hugo Benichi50a84c62016-09-02 09:00:59 +0900457 " zero_lifetime_ras: 1",
458 " >",
Hugo Benichid680d4c2016-10-13 13:16:16 +0900459 ">",
Hugo Benichi2a5cfb92017-03-22 22:21:44 +0900460 "version: 2\n");
Hugo Benichi50a84c62016-09-02 09:00:59 +0900461
462 verifySerialization(want, ev);
463 }
464
Hugo Benichi60c9f632017-09-05 13:34:48 +0900465 @Test
Hugo Benichi50a84c62016-09-02 09:00:59 +0900466 public void testRaEventSerialization() {
467 ConnectivityMetricsEvent ev = describeIpEvent(
468 aType(RaEvent.class),
469 aLong(2000),
470 aLong(400),
471 aLong(300),
472 aLong(-1),
473 aLong(1000),
474 aLong(-1));
475
Hugo Benichi2a5cfb92017-03-22 22:21:44 +0900476 String want = String.join("\n",
Hugo Benichi50a84c62016-09-02 09:00:59 +0900477 "dropped_events: 0",
478 "events <",
Hugo Benichif6840502017-03-08 11:59:36 +0900479 " if_name: \"\"",
Hugo Benichi1af06a62017-01-16 14:42:56 +0900480 " link_layer: 0",
Hugo Benichif6840502017-03-08 11:59:36 +0900481 " network_id: 0",
Hugo Benichi80df43e2016-11-24 11:28:06 +0900482 " time_ms: 1",
Hugo Benichif6840502017-03-08 11:59:36 +0900483 " transports: 0",
Hugo Benichi50a84c62016-09-02 09:00:59 +0900484 " ra_event <",
485 " dnssl_lifetime: -1",
486 " prefix_preferred_lifetime: 300",
487 " prefix_valid_lifetime: 400",
488 " rdnss_lifetime: 1000",
489 " route_info_lifetime: -1",
490 " router_lifetime: 2000",
491 " >",
Hugo Benichid680d4c2016-10-13 13:16:16 +0900492 ">",
Hugo Benichi2a5cfb92017-03-22 22:21:44 +0900493 "version: 2\n");
Hugo Benichi50a84c62016-09-02 09:00:59 +0900494
495 verifySerialization(want, ev);
496 }
497
Hugo Benichi60c9f632017-09-05 13:34:48 +0900498 @Test
499 public void testWakeupStatsSerialization() {
500 WakeupStats stats = new WakeupStats("wlan0");
501 stats.totalWakeups = 14;
502 stats.applicationWakeups = 5;
503 stats.nonApplicationWakeups = 1;
504 stats.rootWakeups = 2;
505 stats.systemWakeups = 3;
Hugo Benichi175b5742017-09-19 13:15:26 +0900506 stats.noUidWakeups = 3;
Hugo Benichid921bce2017-10-12 21:33:40 +0900507 stats.l2UnicastCount = 5;
508 stats.l2MulticastCount = 1;
509 stats.l2BroadcastCount = 2;
510 stats.ethertypes.put(0x800, 3);
511 stats.ethertypes.put(0x86dd, 3);
512 stats.ipNextHeaders.put(6, 5);
513
Hugo Benichi60c9f632017-09-05 13:34:48 +0900514
515 IpConnectivityEvent got = IpConnectivityEventBuilder.toProto(stats);
516 String want = String.join("\n",
517 "dropped_events: 0",
518 "events <",
519 " if_name: \"\"",
520 " link_layer: 4",
521 " network_id: 0",
522 " time_ms: 0",
523 " transports: 0",
524 " wakeup_stats <",
525 " application_wakeups: 5",
526 " duration_sec: 0",
Hugo Benichid921bce2017-10-12 21:33:40 +0900527 " ethertype_counts <",
528 " key: 2048",
529 " value: 3",
530 " >",
531 " ethertype_counts <",
532 " key: 34525",
533 " value: 3",
534 " >",
535 " ip_next_header_counts <",
536 " key: 6",
537 " value: 5",
538 " >",
539 " l2_broadcast_count: 2",
540 " l2_multicast_count: 1",
541 " l2_unicast_count: 5",
Hugo Benichi175b5742017-09-19 13:15:26 +0900542 " no_uid_wakeups: 3",
Hugo Benichi60c9f632017-09-05 13:34:48 +0900543 " non_application_wakeups: 1",
544 " root_wakeups: 2",
545 " system_wakeups: 3",
546 " total_wakeups: 14",
Hugo Benichi60c9f632017-09-05 13:34:48 +0900547 " >",
548 ">",
549 "version: 2\n");
550
551 verifySerialization(want, got);
552 }
553
Hugo Benichi50a84c62016-09-02 09:00:59 +0900554 static void verifySerialization(String want, ConnectivityMetricsEvent... input) {
Hugo Benichi60c9f632017-09-05 13:34:48 +0900555 List<IpConnectivityEvent> protoInput =
556 IpConnectivityEventBuilder.toProto(Arrays.asList(input));
557 verifySerialization(want, protoInput.toArray(new IpConnectivityEvent[0]));
558 }
559
560 static void verifySerialization(String want, IpConnectivityEvent... input) {
Hugo Benichi50a84c62016-09-02 09:00:59 +0900561 try {
Hugo Benichi60c9f632017-09-05 13:34:48 +0900562 byte[] got = IpConnectivityEventBuilder.serialize(0, Arrays.asList(input));
Glen Kuhne07199402016-10-25 15:47:25 -0700563 IpConnectivityLog log = IpConnectivityLog.parseFrom(got);
Hugo Benichi50a84c62016-09-02 09:00:59 +0900564 assertEquals(want, log.toString());
565 } catch (Exception e) {
566 fail(e.toString());
567 }
568 }
Hugo Benichi50a84c62016-09-02 09:00:59 +0900569}