blob: 70495cced5366b688bec136e1f0d838d2aefe55a [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
Glen Kuhne07199402016-10-25 15:47:25 -070019import static com.android.server.connectivity.MetricsTestUtil.aLong;
20import static com.android.server.connectivity.MetricsTestUtil.aString;
21import static com.android.server.connectivity.MetricsTestUtil.aType;
22import static com.android.server.connectivity.MetricsTestUtil.anInt;
Glen Kuhne07199402016-10-25 15:47:25 -070023import static com.android.server.connectivity.MetricsTestUtil.describeIpEvent;
Hugo Benichi73fd4d12017-03-15 23:05:01 +090024import static com.android.server.connectivity.metrics.nano.IpConnectivityLogClass.BLUETOOTH;
25import static com.android.server.connectivity.metrics.nano.IpConnectivityLogClass.CELLULAR;
Brett Chabot1ae2aa62019-03-04 14:14:56 -080026import static com.android.server.connectivity.metrics.nano.IpConnectivityLogClass.IpConnectivityLog;
Hugo Benichi73fd4d12017-03-15 23:05:01 +090027import static com.android.server.connectivity.metrics.nano.IpConnectivityLogClass.MULTIPLE;
28import static com.android.server.connectivity.metrics.nano.IpConnectivityLogClass.WIFI;
Brett Chabot1ae2aa62019-03-04 14:14:56 -080029
Hugo Benichi60c9f632017-09-05 13:34:48 +090030import static org.junit.Assert.assertEquals;
31import static org.junit.Assert.fail;
Glen Kuhne07199402016-10-25 15:47:25 -070032
Hugo Benichi50a84c62016-09-02 09:00:59 +090033import android.net.ConnectivityMetricsEvent;
34import android.net.metrics.ApfProgramEvent;
35import android.net.metrics.ApfStats;
36import android.net.metrics.DefaultNetworkEvent;
37import android.net.metrics.DhcpClientEvent;
38import android.net.metrics.DhcpErrorEvent;
Hugo Benichi50a84c62016-09-02 09:00:59 +090039import android.net.metrics.IpManagerEvent;
40import android.net.metrics.IpReachabilityEvent;
41import android.net.metrics.NetworkEvent;
42import android.net.metrics.RaEvent;
43import android.net.metrics.ValidationProbeEvent;
Hugo Benichi60c9f632017-09-05 13:34:48 +090044import android.net.metrics.WakeupStats;
Hugo Benichi80df43e2016-11-24 11:28:06 +090045import android.test.suitebuilder.annotation.SmallTest;
Hugo Benichi60c9f632017-09-05 13:34:48 +090046
Brett Chabot1ae2aa62019-03-04 14:14:56 -080047import androidx.test.runner.AndroidJUnit4;
48
Hugo Benichi5eb90532017-03-23 18:38:22 +090049import com.android.server.connectivity.metrics.nano.IpConnectivityLogClass.IpConnectivityEvent;
Hugo Benichi60c9f632017-09-05 13:34:48 +090050
Brett Chabot1ae2aa62019-03-04 14:14:56 -080051import org.junit.Test;
52import org.junit.runner.RunWith;
53
Glen Kuhne07199402016-10-25 15:47:25 -070054import java.util.Arrays;
Hugo Benichi5eb90532017-03-23 18:38:22 +090055import java.util.List;
Hugo Benichi60c9f632017-09-05 13:34:48 +090056
Hugo Benichif6840502017-03-08 11:59:36 +090057// TODO: instead of comparing textpb to textpb, parse textpb and compare proto to proto.
Hugo Benichi60c9f632017-09-05 13:34:48 +090058@RunWith(AndroidJUnit4.class)
59@SmallTest
60public class IpConnectivityEventBuilderTest {
Hugo Benichi50a84c62016-09-02 09:00:59 +090061
Hugo Benichi60c9f632017-09-05 13:34:48 +090062 @Test
Hugo Benichi73fd4d12017-03-15 23:05:01 +090063 public void testLinkLayerInferrence() {
64 ConnectivityMetricsEvent ev = describeIpEvent(
65 aType(IpReachabilityEvent.class),
Hugo Benichi73fd4d12017-03-15 23:05:01 +090066 anInt(IpReachabilityEvent.NUD_FAILED));
67
Hugo Benichi2a5cfb92017-03-22 22:21:44 +090068 String want = String.join("\n",
Hugo Benichi73fd4d12017-03-15 23:05:01 +090069 "dropped_events: 0",
70 "events <",
71 " if_name: \"\"",
72 " link_layer: 0",
73 " network_id: 0",
74 " time_ms: 1",
75 " transports: 0",
76 " ip_reachability_event <",
77 " event_type: 512",
Hugo Benichi948a8592017-03-16 16:33:47 +090078 " if_name: \"\"",
Hugo Benichi73fd4d12017-03-15 23:05:01 +090079 " >",
80 ">",
Hugo Benichi2a5cfb92017-03-22 22:21:44 +090081 "version: 2\n");
Hugo Benichi73fd4d12017-03-15 23:05:01 +090082 verifySerialization(want, ev);
83
84 ev.netId = 123;
85 ev.transports = 3; // transports have priority for inferrence of link layer
86 ev.ifname = "wlan0";
Hugo Benichi2a5cfb92017-03-22 22:21:44 +090087 want = String.join("\n",
Hugo Benichi73fd4d12017-03-15 23:05:01 +090088 "dropped_events: 0",
89 "events <",
90 " if_name: \"\"",
91 String.format(" link_layer: %d", MULTIPLE),
92 " network_id: 123",
93 " time_ms: 1",
94 " transports: 3",
95 " ip_reachability_event <",
96 " event_type: 512",
Hugo Benichi948a8592017-03-16 16:33:47 +090097 " if_name: \"\"",
Hugo Benichi73fd4d12017-03-15 23:05:01 +090098 " >",
99 ">",
Hugo Benichi2a5cfb92017-03-22 22:21:44 +0900100 "version: 2\n");
Hugo Benichi73fd4d12017-03-15 23:05:01 +0900101 verifySerialization(want, ev);
102
103 ev.transports = 1;
104 ev.ifname = null;
Hugo Benichi2a5cfb92017-03-22 22:21:44 +0900105 want = String.join("\n",
Hugo Benichi73fd4d12017-03-15 23:05:01 +0900106 "dropped_events: 0",
107 "events <",
108 " if_name: \"\"",
109 String.format(" link_layer: %d", CELLULAR),
110 " network_id: 123",
111 " time_ms: 1",
112 " transports: 1",
113 " ip_reachability_event <",
114 " event_type: 512",
Hugo Benichi948a8592017-03-16 16:33:47 +0900115 " if_name: \"\"",
Hugo Benichi73fd4d12017-03-15 23:05:01 +0900116 " >",
117 ">",
Hugo Benichi2a5cfb92017-03-22 22:21:44 +0900118 "version: 2\n");
Hugo Benichi73fd4d12017-03-15 23:05:01 +0900119 verifySerialization(want, ev);
120
121 ev.transports = 0;
122 ev.ifname = "not_inferred";
Hugo Benichi2a5cfb92017-03-22 22:21:44 +0900123 want = String.join("\n",
Hugo Benichi73fd4d12017-03-15 23:05:01 +0900124 "dropped_events: 0",
125 "events <",
126 " if_name: \"not_inferred\"",
127 " link_layer: 0",
128 " network_id: 123",
129 " time_ms: 1",
130 " transports: 0",
131 " ip_reachability_event <",
132 " event_type: 512",
Hugo Benichi948a8592017-03-16 16:33:47 +0900133 " if_name: \"\"",
Hugo Benichi73fd4d12017-03-15 23:05:01 +0900134 " >",
135 ">",
Hugo Benichi2a5cfb92017-03-22 22:21:44 +0900136 "version: 2\n");
Hugo Benichi73fd4d12017-03-15 23:05:01 +0900137 verifySerialization(want, ev);
138
139 ev.ifname = "bt-pan";
Hugo Benichi2a5cfb92017-03-22 22:21:44 +0900140 want = String.join("\n",
Hugo Benichi73fd4d12017-03-15 23:05:01 +0900141 "dropped_events: 0",
142 "events <",
143 " if_name: \"\"",
144 String.format(" link_layer: %d", BLUETOOTH),
145 " network_id: 123",
146 " time_ms: 1",
147 " transports: 0",
148 " ip_reachability_event <",
149 " event_type: 512",
Hugo Benichi948a8592017-03-16 16:33:47 +0900150 " if_name: \"\"",
Hugo Benichi73fd4d12017-03-15 23:05:01 +0900151 " >",
152 ">",
Hugo Benichi2a5cfb92017-03-22 22:21:44 +0900153 "version: 2\n");
Hugo Benichi73fd4d12017-03-15 23:05:01 +0900154 verifySerialization(want, ev);
155
156 ev.ifname = "rmnet_ipa0";
Hugo Benichi2a5cfb92017-03-22 22:21:44 +0900157 want = String.join("\n",
Hugo Benichi73fd4d12017-03-15 23:05:01 +0900158 "dropped_events: 0",
159 "events <",
160 " if_name: \"\"",
161 String.format(" link_layer: %d", CELLULAR),
162 " network_id: 123",
163 " time_ms: 1",
164 " transports: 0",
165 " ip_reachability_event <",
166 " event_type: 512",
Hugo Benichi948a8592017-03-16 16:33:47 +0900167 " if_name: \"\"",
Hugo Benichi73fd4d12017-03-15 23:05:01 +0900168 " >",
169 ">",
Hugo Benichi2a5cfb92017-03-22 22:21:44 +0900170 "version: 2\n");
Hugo Benichi73fd4d12017-03-15 23:05:01 +0900171 verifySerialization(want, ev);
172
173 ev.ifname = "wlan0";
Hugo Benichi2a5cfb92017-03-22 22:21:44 +0900174 want = String.join("\n",
Hugo Benichi73fd4d12017-03-15 23:05:01 +0900175 "dropped_events: 0",
176 "events <",
177 " if_name: \"\"",
178 String.format(" link_layer: %d", WIFI),
179 " network_id: 123",
180 " time_ms: 1",
181 " transports: 0",
182 " ip_reachability_event <",
183 " event_type: 512",
Hugo Benichi948a8592017-03-16 16:33:47 +0900184 " if_name: \"\"",
Hugo Benichi73fd4d12017-03-15 23:05:01 +0900185 " >",
186 ">",
Hugo Benichi2a5cfb92017-03-22 22:21:44 +0900187 "version: 2\n");
Hugo Benichi73fd4d12017-03-15 23:05:01 +0900188 verifySerialization(want, ev);
189 }
190
Hugo Benichi60c9f632017-09-05 13:34:48 +0900191 @Test
Hugo Benichi50a84c62016-09-02 09:00:59 +0900192 public void testDefaultNetworkEventSerialization() {
Hugo Benichi380a0632017-10-20 09:25:29 +0900193 DefaultNetworkEvent ev = new DefaultNetworkEvent(1001);
Hugo Benichi1193a9c2017-10-19 14:58:15 +0900194 ev.netId = 102;
Hugo Benichi380a0632017-10-20 09:25:29 +0900195 ev.transports = 2;
196 ev.previousTransports = 4;
197 ev.ipv4 = true;
198 ev.initialScore = 20;
199 ev.finalScore = 60;
200 ev.durationMs = 54;
201 ev.validatedMs = 27;
Hugo Benichi50a84c62016-09-02 09:00:59 +0900202
Hugo Benichi2a5cfb92017-03-22 22:21:44 +0900203 String want = String.join("\n",
Hugo Benichi50a84c62016-09-02 09:00:59 +0900204 "dropped_events: 0",
205 "events <",
Hugo Benichif6840502017-03-08 11:59:36 +0900206 " if_name: \"\"",
Hugo Benichi380a0632017-10-20 09:25:29 +0900207 " link_layer: 4",
Hugo Benichi1193a9c2017-10-19 14:58:15 +0900208 " network_id: 102",
209 " time_ms: 0",
Hugo Benichi380a0632017-10-20 09:25:29 +0900210 " transports: 2",
Hugo Benichi50a84c62016-09-02 09:00:59 +0900211 " default_network_event <",
Hugo Benichi380a0632017-10-20 09:25:29 +0900212 " default_network_duration_ms: 54",
213 " final_score: 60",
214 " initial_score: 20",
215 " ip_support: 1",
Hugo Benichi4eccf782017-07-18 14:28:27 +0900216 " no_default_network_duration_ms: 0",
Hugo Benichi380a0632017-10-20 09:25:29 +0900217 " previous_default_network_link_layer: 1",
218 " previous_network_ip_support: 0",
219 " validation_duration_ms: 27",
Hugo Benichi50a84c62016-09-02 09:00:59 +0900220 " >",
Hugo Benichid680d4c2016-10-13 13:16:16 +0900221 ">",
Hugo Benichi2a5cfb92017-03-22 22:21:44 +0900222 "version: 2\n");
Hugo Benichi50a84c62016-09-02 09:00:59 +0900223
Hugo Benichi1193a9c2017-10-19 14:58:15 +0900224 verifySerialization(want, IpConnectivityEventBuilder.toProto(ev));
Hugo Benichi50a84c62016-09-02 09:00:59 +0900225 }
226
Hugo Benichi60c9f632017-09-05 13:34:48 +0900227 @Test
Hugo Benichi50a84c62016-09-02 09:00:59 +0900228 public void testDhcpClientEventSerialization() {
229 ConnectivityMetricsEvent ev = describeIpEvent(
230 aType(DhcpClientEvent.class),
Hugo Benichi50a84c62016-09-02 09:00:59 +0900231 aString("SomeState"),
232 anInt(192));
233
Hugo Benichi2a5cfb92017-03-22 22:21:44 +0900234 String want = String.join("\n",
Hugo Benichi50a84c62016-09-02 09:00:59 +0900235 "dropped_events: 0",
236 "events <",
Hugo Benichif6840502017-03-08 11:59:36 +0900237 " if_name: \"\"",
Hugo Benichi1af06a62017-01-16 14:42:56 +0900238 " link_layer: 0",
Hugo Benichif6840502017-03-08 11:59:36 +0900239 " network_id: 0",
Hugo Benichi80df43e2016-11-24 11:28:06 +0900240 " time_ms: 1",
Hugo Benichif6840502017-03-08 11:59:36 +0900241 " transports: 0",
Hugo Benichi50a84c62016-09-02 09:00:59 +0900242 " dhcp_event <",
243 " duration_ms: 192",
Hugo Benichi948a8592017-03-16 16:33:47 +0900244 " if_name: \"\"",
Hugo Benichi50a84c62016-09-02 09:00:59 +0900245 " state_transition: \"SomeState\"",
246 " >",
Hugo Benichid680d4c2016-10-13 13:16:16 +0900247 ">",
Hugo Benichi2a5cfb92017-03-22 22:21:44 +0900248 "version: 2\n");
Hugo Benichi50a84c62016-09-02 09:00:59 +0900249
250 verifySerialization(want, ev);
251 }
252
Hugo Benichi60c9f632017-09-05 13:34:48 +0900253 @Test
Hugo Benichi50a84c62016-09-02 09:00:59 +0900254 public void testDhcpErrorEventSerialization() {
255 ConnectivityMetricsEvent ev = describeIpEvent(
256 aType(DhcpErrorEvent.class),
Hugo Benichi50a84c62016-09-02 09:00:59 +0900257 anInt(DhcpErrorEvent.L4_NOT_UDP));
258
Hugo Benichi2a5cfb92017-03-22 22:21:44 +0900259 String want = String.join("\n",
Hugo Benichi50a84c62016-09-02 09:00:59 +0900260 "dropped_events: 0",
261 "events <",
Hugo Benichif6840502017-03-08 11:59:36 +0900262 " if_name: \"\"",
Hugo Benichi1af06a62017-01-16 14:42:56 +0900263 " link_layer: 0",
Hugo Benichif6840502017-03-08 11:59:36 +0900264 " network_id: 0",
Hugo Benichi80df43e2016-11-24 11:28:06 +0900265 " time_ms: 1",
Hugo Benichif6840502017-03-08 11:59:36 +0900266 " transports: 0",
Hugo Benichi50a84c62016-09-02 09:00:59 +0900267 " dhcp_event <",
268 " duration_ms: 0",
Hugo Benichi948a8592017-03-16 16:33:47 +0900269 " if_name: \"\"",
Hugo Benichi80df43e2016-11-24 11:28:06 +0900270 " error_code: 50397184",
Hugo Benichi50a84c62016-09-02 09:00:59 +0900271 " >",
Hugo Benichid680d4c2016-10-13 13:16:16 +0900272 ">",
Hugo Benichi2a5cfb92017-03-22 22:21:44 +0900273 "version: 2\n");
Hugo Benichi50a84c62016-09-02 09:00:59 +0900274
275 verifySerialization(want, ev);
276 }
277
Hugo Benichi60c9f632017-09-05 13:34:48 +0900278 @Test
Hugo Benichi50a84c62016-09-02 09:00:59 +0900279 public void testIpManagerEventSerialization() {
280 ConnectivityMetricsEvent ev = describeIpEvent(
281 aType(IpManagerEvent.class),
Hugo Benichi50a84c62016-09-02 09:00:59 +0900282 anInt(IpManagerEvent.PROVISIONING_OK),
283 aLong(5678));
284
Hugo Benichi2a5cfb92017-03-22 22:21:44 +0900285 String want = String.join("\n",
Hugo Benichi50a84c62016-09-02 09:00:59 +0900286 "dropped_events: 0",
287 "events <",
Hugo Benichif6840502017-03-08 11:59:36 +0900288 " if_name: \"\"",
Hugo Benichi1af06a62017-01-16 14:42:56 +0900289 " link_layer: 0",
Hugo Benichif6840502017-03-08 11:59:36 +0900290 " network_id: 0",
Hugo Benichi80df43e2016-11-24 11:28:06 +0900291 " time_ms: 1",
Hugo Benichif6840502017-03-08 11:59:36 +0900292 " transports: 0",
Hugo Benichi50a84c62016-09-02 09:00:59 +0900293 " ip_provisioning_event <",
294 " event_type: 1",
Hugo Benichi948a8592017-03-16 16:33:47 +0900295 " if_name: \"\"",
Hugo Benichi50a84c62016-09-02 09:00:59 +0900296 " latency_ms: 5678",
297 " >",
Hugo Benichid680d4c2016-10-13 13:16:16 +0900298 ">",
Hugo Benichi2a5cfb92017-03-22 22:21:44 +0900299 "version: 2\n");
Hugo Benichi50a84c62016-09-02 09:00:59 +0900300
301 verifySerialization(want, ev);
302 }
303
Hugo Benichi60c9f632017-09-05 13:34:48 +0900304 @Test
Hugo Benichi50a84c62016-09-02 09:00:59 +0900305 public void testIpReachabilityEventSerialization() {
306 ConnectivityMetricsEvent ev = describeIpEvent(
307 aType(IpReachabilityEvent.class),
Hugo Benichi50a84c62016-09-02 09:00:59 +0900308 anInt(IpReachabilityEvent.NUD_FAILED));
309
Hugo Benichi2a5cfb92017-03-22 22:21:44 +0900310 String want = String.join("\n",
Hugo Benichi50a84c62016-09-02 09:00:59 +0900311 "dropped_events: 0",
312 "events <",
Hugo Benichif6840502017-03-08 11:59:36 +0900313 " if_name: \"\"",
Hugo Benichi1af06a62017-01-16 14:42:56 +0900314 " link_layer: 0",
Hugo Benichif6840502017-03-08 11:59:36 +0900315 " network_id: 0",
Hugo Benichi80df43e2016-11-24 11:28:06 +0900316 " time_ms: 1",
Hugo Benichif6840502017-03-08 11:59:36 +0900317 " transports: 0",
Hugo Benichi50a84c62016-09-02 09:00:59 +0900318 " ip_reachability_event <",
319 " event_type: 512",
Hugo Benichi948a8592017-03-16 16:33:47 +0900320 " if_name: \"\"",
Hugo Benichi50a84c62016-09-02 09:00:59 +0900321 " >",
Hugo Benichid680d4c2016-10-13 13:16:16 +0900322 ">",
Hugo Benichi2a5cfb92017-03-22 22:21:44 +0900323 "version: 2\n");
Hugo Benichi50a84c62016-09-02 09:00:59 +0900324
325 verifySerialization(want, ev);
326 }
327
Hugo Benichi60c9f632017-09-05 13:34:48 +0900328 @Test
Hugo Benichi50a84c62016-09-02 09:00:59 +0900329 public void testNetworkEventSerialization() {
330 ConnectivityMetricsEvent ev = describeIpEvent(
331 aType(NetworkEvent.class),
Hugo Benichi50a84c62016-09-02 09:00:59 +0900332 anInt(5),
333 aLong(20410));
334
Hugo Benichi2a5cfb92017-03-22 22:21:44 +0900335 String want = String.join("\n",
Hugo Benichi50a84c62016-09-02 09:00:59 +0900336 "dropped_events: 0",
337 "events <",
Hugo Benichif6840502017-03-08 11:59:36 +0900338 " if_name: \"\"",
Hugo Benichi1af06a62017-01-16 14:42:56 +0900339 " link_layer: 0",
Hugo Benichif6840502017-03-08 11:59:36 +0900340 " network_id: 0",
Hugo Benichi80df43e2016-11-24 11:28:06 +0900341 " time_ms: 1",
Hugo Benichif6840502017-03-08 11:59:36 +0900342 " transports: 0",
Hugo Benichi50a84c62016-09-02 09:00:59 +0900343 " network_event <",
344 " event_type: 5",
345 " latency_ms: 20410",
Hugo Benichi50a84c62016-09-02 09:00:59 +0900346 " >",
Hugo Benichid680d4c2016-10-13 13:16:16 +0900347 ">",
Hugo Benichi2a5cfb92017-03-22 22:21:44 +0900348 "version: 2\n");
Hugo Benichi50a84c62016-09-02 09:00:59 +0900349
350 verifySerialization(want, ev);
351 }
352
Hugo Benichi60c9f632017-09-05 13:34:48 +0900353 @Test
Hugo Benichi50a84c62016-09-02 09:00:59 +0900354 public void testValidationProbeEventSerialization() {
355 ConnectivityMetricsEvent ev = describeIpEvent(
356 aType(ValidationProbeEvent.class),
Hugo Benichi50a84c62016-09-02 09:00:59 +0900357 aLong(40730),
358 anInt(ValidationProbeEvent.PROBE_HTTP),
359 anInt(204));
360
Hugo Benichi2a5cfb92017-03-22 22:21:44 +0900361 String want = String.join("\n",
Hugo Benichi50a84c62016-09-02 09:00:59 +0900362 "dropped_events: 0",
363 "events <",
Hugo Benichif6840502017-03-08 11:59:36 +0900364 " if_name: \"\"",
Hugo Benichi1af06a62017-01-16 14:42:56 +0900365 " link_layer: 0",
Hugo Benichif6840502017-03-08 11:59:36 +0900366 " network_id: 0",
Hugo Benichi50a84c62016-09-02 09:00:59 +0900367 " time_ms: 1",
Hugo Benichif6840502017-03-08 11:59:36 +0900368 " transports: 0",
Hugo Benichi50a84c62016-09-02 09:00:59 +0900369 " validation_probe_event <",
370 " latency_ms: 40730",
Hugo Benichi50a84c62016-09-02 09:00:59 +0900371 " probe_result: 204",
372 " probe_type: 1",
373 " >",
Hugo Benichid680d4c2016-10-13 13:16:16 +0900374 ">",
Hugo Benichi2a5cfb92017-03-22 22:21:44 +0900375 "version: 2\n");
Hugo Benichi50a84c62016-09-02 09:00:59 +0900376
377 verifySerialization(want, ev);
378 }
379
Hugo Benichi60c9f632017-09-05 13:34:48 +0900380 @Test
Hugo Benichi50a84c62016-09-02 09:00:59 +0900381 public void testApfProgramEventSerialization() {
382 ConnectivityMetricsEvent ev = describeIpEvent(
383 aType(ApfProgramEvent.class),
384 aLong(200),
Hugo Benichi22d9b2d2017-02-22 13:02:27 +0900385 aLong(18),
Hugo Benichi50a84c62016-09-02 09:00:59 +0900386 anInt(7),
387 anInt(9),
388 anInt(2048),
389 anInt(3));
390
Hugo Benichi2a5cfb92017-03-22 22:21:44 +0900391 String want = String.join("\n",
Hugo Benichi50a84c62016-09-02 09:00:59 +0900392 "dropped_events: 0",
393 "events <",
Hugo Benichif6840502017-03-08 11:59:36 +0900394 " if_name: \"\"",
Hugo Benichi1af06a62017-01-16 14:42:56 +0900395 " link_layer: 0",
Hugo Benichif6840502017-03-08 11:59:36 +0900396 " network_id: 0",
Hugo Benichi80df43e2016-11-24 11:28:06 +0900397 " time_ms: 1",
Hugo Benichif6840502017-03-08 11:59:36 +0900398 " transports: 0",
Hugo Benichi50a84c62016-09-02 09:00:59 +0900399 " apf_program_event <",
400 " current_ras: 9",
401 " drop_multicast: true",
Hugo Benichi22d9b2d2017-02-22 13:02:27 +0900402 " effective_lifetime: 18",
Hugo Benichi50a84c62016-09-02 09:00:59 +0900403 " filtered_ras: 7",
404 " has_ipv4_addr: true",
405 " lifetime: 200",
406 " program_length: 2048",
407 " >",
Hugo Benichid680d4c2016-10-13 13:16:16 +0900408 ">",
Hugo Benichi2a5cfb92017-03-22 22:21:44 +0900409 "version: 2\n");
Hugo Benichi50a84c62016-09-02 09:00:59 +0900410
411 verifySerialization(want, ev);
412 }
413
Hugo Benichi60c9f632017-09-05 13:34:48 +0900414 @Test
Hugo Benichi50a84c62016-09-02 09:00:59 +0900415 public void testApfStatsSerialization() {
416 ConnectivityMetricsEvent ev = describeIpEvent(
417 aType(ApfStats.class),
418 aLong(45000),
419 anInt(10),
420 anInt(2),
421 anInt(2),
422 anInt(1),
423 anInt(2),
424 anInt(4),
Hugo Benichi22d9b2d2017-02-22 13:02:27 +0900425 anInt(7),
426 anInt(3),
Hugo Benichi50a84c62016-09-02 09:00:59 +0900427 anInt(2048));
428
Hugo Benichi2a5cfb92017-03-22 22:21:44 +0900429 String want = String.join("\n",
Hugo Benichi50a84c62016-09-02 09:00:59 +0900430 "dropped_events: 0",
431 "events <",
Hugo Benichif6840502017-03-08 11:59:36 +0900432 " if_name: \"\"",
Hugo Benichi1af06a62017-01-16 14:42:56 +0900433 " link_layer: 0",
Hugo Benichif6840502017-03-08 11:59:36 +0900434 " network_id: 0",
Hugo Benichi80df43e2016-11-24 11:28:06 +0900435 " time_ms: 1",
Hugo Benichif6840502017-03-08 11:59:36 +0900436 " transports: 0",
Hugo Benichi50a84c62016-09-02 09:00:59 +0900437 " apf_statistics <",
438 " dropped_ras: 2",
439 " duration_ms: 45000",
440 " matching_ras: 2",
441 " max_program_size: 2048",
442 " parse_errors: 2",
443 " program_updates: 4",
Hugo Benichi22d9b2d2017-02-22 13:02:27 +0900444 " program_updates_all: 7",
445 " program_updates_allowing_multicast: 3",
Hugo Benichi50a84c62016-09-02 09:00:59 +0900446 " received_ras: 10",
Hugo Benichi4eccf782017-07-18 14:28:27 +0900447 " total_packet_dropped: 0",
448 " total_packet_processed: 0",
Hugo Benichi50a84c62016-09-02 09:00:59 +0900449 " zero_lifetime_ras: 1",
450 " >",
Hugo Benichid680d4c2016-10-13 13:16:16 +0900451 ">",
Hugo Benichi2a5cfb92017-03-22 22:21:44 +0900452 "version: 2\n");
Hugo Benichi50a84c62016-09-02 09:00:59 +0900453
454 verifySerialization(want, ev);
455 }
456
Hugo Benichi60c9f632017-09-05 13:34:48 +0900457 @Test
Hugo Benichi50a84c62016-09-02 09:00:59 +0900458 public void testRaEventSerialization() {
459 ConnectivityMetricsEvent ev = describeIpEvent(
460 aType(RaEvent.class),
461 aLong(2000),
462 aLong(400),
463 aLong(300),
464 aLong(-1),
465 aLong(1000),
466 aLong(-1));
467
Hugo Benichi2a5cfb92017-03-22 22:21:44 +0900468 String want = String.join("\n",
Hugo Benichi50a84c62016-09-02 09:00:59 +0900469 "dropped_events: 0",
470 "events <",
Hugo Benichif6840502017-03-08 11:59:36 +0900471 " if_name: \"\"",
Hugo Benichi1af06a62017-01-16 14:42:56 +0900472 " link_layer: 0",
Hugo Benichif6840502017-03-08 11:59:36 +0900473 " network_id: 0",
Hugo Benichi80df43e2016-11-24 11:28:06 +0900474 " time_ms: 1",
Hugo Benichif6840502017-03-08 11:59:36 +0900475 " transports: 0",
Hugo Benichi50a84c62016-09-02 09:00:59 +0900476 " ra_event <",
477 " dnssl_lifetime: -1",
478 " prefix_preferred_lifetime: 300",
479 " prefix_valid_lifetime: 400",
480 " rdnss_lifetime: 1000",
481 " route_info_lifetime: -1",
482 " router_lifetime: 2000",
483 " >",
Hugo Benichid680d4c2016-10-13 13:16:16 +0900484 ">",
Hugo Benichi2a5cfb92017-03-22 22:21:44 +0900485 "version: 2\n");
Hugo Benichi50a84c62016-09-02 09:00:59 +0900486
487 verifySerialization(want, ev);
488 }
489
Hugo Benichi60c9f632017-09-05 13:34:48 +0900490 @Test
491 public void testWakeupStatsSerialization() {
492 WakeupStats stats = new WakeupStats("wlan0");
493 stats.totalWakeups = 14;
494 stats.applicationWakeups = 5;
495 stats.nonApplicationWakeups = 1;
496 stats.rootWakeups = 2;
497 stats.systemWakeups = 3;
Hugo Benichi175b5742017-09-19 13:15:26 +0900498 stats.noUidWakeups = 3;
Hugo Benichid921bce2017-10-12 21:33:40 +0900499 stats.l2UnicastCount = 5;
500 stats.l2MulticastCount = 1;
501 stats.l2BroadcastCount = 2;
502 stats.ethertypes.put(0x800, 3);
503 stats.ethertypes.put(0x86dd, 3);
504 stats.ipNextHeaders.put(6, 5);
505
Hugo Benichi60c9f632017-09-05 13:34:48 +0900506
507 IpConnectivityEvent got = IpConnectivityEventBuilder.toProto(stats);
508 String want = String.join("\n",
509 "dropped_events: 0",
510 "events <",
511 " if_name: \"\"",
512 " link_layer: 4",
513 " network_id: 0",
514 " time_ms: 0",
515 " transports: 0",
516 " wakeup_stats <",
517 " application_wakeups: 5",
518 " duration_sec: 0",
Hugo Benichid921bce2017-10-12 21:33:40 +0900519 " ethertype_counts <",
520 " key: 2048",
521 " value: 3",
522 " >",
523 " ethertype_counts <",
524 " key: 34525",
525 " value: 3",
526 " >",
527 " ip_next_header_counts <",
528 " key: 6",
529 " value: 5",
530 " >",
531 " l2_broadcast_count: 2",
532 " l2_multicast_count: 1",
533 " l2_unicast_count: 5",
Hugo Benichi175b5742017-09-19 13:15:26 +0900534 " no_uid_wakeups: 3",
Hugo Benichi60c9f632017-09-05 13:34:48 +0900535 " non_application_wakeups: 1",
536 " root_wakeups: 2",
537 " system_wakeups: 3",
538 " total_wakeups: 14",
Hugo Benichi60c9f632017-09-05 13:34:48 +0900539 " >",
540 ">",
541 "version: 2\n");
542
543 verifySerialization(want, got);
544 }
545
Hugo Benichi50a84c62016-09-02 09:00:59 +0900546 static void verifySerialization(String want, ConnectivityMetricsEvent... input) {
Hugo Benichi60c9f632017-09-05 13:34:48 +0900547 List<IpConnectivityEvent> protoInput =
548 IpConnectivityEventBuilder.toProto(Arrays.asList(input));
549 verifySerialization(want, protoInput.toArray(new IpConnectivityEvent[0]));
550 }
551
552 static void verifySerialization(String want, IpConnectivityEvent... input) {
Hugo Benichi50a84c62016-09-02 09:00:59 +0900553 try {
Hugo Benichi60c9f632017-09-05 13:34:48 +0900554 byte[] got = IpConnectivityEventBuilder.serialize(0, Arrays.asList(input));
Glen Kuhne07199402016-10-25 15:47:25 -0700555 IpConnectivityLog log = IpConnectivityLog.parseFrom(got);
Hugo Benichi50a84c62016-09-02 09:00:59 +0900556 assertEquals(want, log.toString());
557 } catch (Exception e) {
558 fail(e.toString());
559 }
560 }
Hugo Benichi50a84c62016-09-02 09:00:59 +0900561}