Hugo Benichi | 50a84c6 | 2016-09-02 09:00:59 +0900 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.server.connectivity; |
| 18 | |
Glen Kuhne | 0719940 | 2016-10-25 15:47:25 -0700 | [diff] [blame] | 19 | import static com.android.server.connectivity.MetricsTestUtil.aBool; |
| 20 | import static com.android.server.connectivity.MetricsTestUtil.aByteArray; |
| 21 | import static com.android.server.connectivity.MetricsTestUtil.aLong; |
| 22 | import static com.android.server.connectivity.MetricsTestUtil.aString; |
| 23 | import static com.android.server.connectivity.MetricsTestUtil.aType; |
| 24 | import static com.android.server.connectivity.MetricsTestUtil.anInt; |
| 25 | import static com.android.server.connectivity.MetricsTestUtil.anIntArray; |
| 26 | import static com.android.server.connectivity.MetricsTestUtil.b; |
| 27 | import static com.android.server.connectivity.MetricsTestUtil.describeIpEvent; |
| 28 | import static com.android.server.connectivity.metrics.IpConnectivityLogClass.IpConnectivityLog; |
| 29 | |
Hugo Benichi | 50a84c6 | 2016-09-02 09:00:59 +0900 | [diff] [blame] | 30 | import android.net.ConnectivityMetricsEvent; |
| 31 | import android.net.metrics.ApfProgramEvent; |
| 32 | import android.net.metrics.ApfStats; |
| 33 | import android.net.metrics.DefaultNetworkEvent; |
| 34 | import android.net.metrics.DhcpClientEvent; |
| 35 | import android.net.metrics.DhcpErrorEvent; |
| 36 | import android.net.metrics.DnsEvent; |
| 37 | import android.net.metrics.IpManagerEvent; |
| 38 | import android.net.metrics.IpReachabilityEvent; |
| 39 | import android.net.metrics.NetworkEvent; |
| 40 | import android.net.metrics.RaEvent; |
| 41 | import android.net.metrics.ValidationProbeEvent; |
Glen Kuhne | 0719940 | 2016-10-25 15:47:25 -0700 | [diff] [blame] | 42 | |
Hugo Benichi | 50a84c6 | 2016-09-02 09:00:59 +0900 | [diff] [blame] | 43 | import junit.framework.TestCase; |
| 44 | |
Glen Kuhne | 0719940 | 2016-10-25 15:47:25 -0700 | [diff] [blame] | 45 | import java.util.Arrays; |
Hugo Benichi | 50a84c6 | 2016-09-02 09:00:59 +0900 | [diff] [blame] | 46 | |
| 47 | public class IpConnectivityEventBuilderTest extends TestCase { |
| 48 | |
| 49 | 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 <", |
| 61 | " default_network_event <", |
| 62 | " network_id <", |
| 63 | " network_id: 102", |
| 64 | " >", |
| 65 | " previous_network_id <", |
| 66 | " network_id: 101", |
| 67 | " >", |
| 68 | " previous_network_ip_support: 1", |
| 69 | " transport_types: 1", |
| 70 | " transport_types: 2", |
| 71 | " transport_types: 3", |
| 72 | " >", |
| 73 | " time_ms: 1", |
Hugo Benichi | d680d4c | 2016-10-13 13:16:16 +0900 | [diff] [blame] | 74 | ">", |
| 75 | "version: 2"); |
Hugo Benichi | 50a84c6 | 2016-09-02 09:00:59 +0900 | [diff] [blame] | 76 | |
| 77 | verifySerialization(want, ev); |
| 78 | } |
| 79 | |
| 80 | public void testDhcpClientEventSerialization() { |
| 81 | ConnectivityMetricsEvent ev = describeIpEvent( |
| 82 | aType(DhcpClientEvent.class), |
| 83 | aString("wlan0"), |
| 84 | aString("SomeState"), |
| 85 | anInt(192)); |
| 86 | |
| 87 | String want = joinLines( |
| 88 | "dropped_events: 0", |
| 89 | "events <", |
| 90 | " dhcp_event <", |
| 91 | " duration_ms: 192", |
| 92 | " error_code: 0", |
| 93 | " if_name: \"wlan0\"", |
| 94 | " state_transition: \"SomeState\"", |
| 95 | " >", |
| 96 | " time_ms: 1", |
Hugo Benichi | d680d4c | 2016-10-13 13:16:16 +0900 | [diff] [blame] | 97 | ">", |
| 98 | "version: 2"); |
Hugo Benichi | 50a84c6 | 2016-09-02 09:00:59 +0900 | [diff] [blame] | 99 | |
| 100 | verifySerialization(want, ev); |
| 101 | } |
| 102 | |
| 103 | public void testDhcpErrorEventSerialization() { |
| 104 | ConnectivityMetricsEvent ev = describeIpEvent( |
| 105 | aType(DhcpErrorEvent.class), |
| 106 | aString("wlan0"), |
| 107 | anInt(DhcpErrorEvent.L4_NOT_UDP)); |
| 108 | |
| 109 | String want = joinLines( |
| 110 | "dropped_events: 0", |
| 111 | "events <", |
| 112 | " dhcp_event <", |
| 113 | " duration_ms: 0", |
| 114 | " error_code: 50397184", |
| 115 | " if_name: \"wlan0\"", |
| 116 | " state_transition: \"\"", |
| 117 | " >", |
| 118 | " time_ms: 1", |
Hugo Benichi | d680d4c | 2016-10-13 13:16:16 +0900 | [diff] [blame] | 119 | ">", |
| 120 | "version: 2"); |
Hugo Benichi | 50a84c6 | 2016-09-02 09:00:59 +0900 | [diff] [blame] | 121 | |
| 122 | verifySerialization(want, ev); |
| 123 | } |
| 124 | |
| 125 | public void testDnsEventSerialization() { |
| 126 | ConnectivityMetricsEvent ev = describeIpEvent( |
| 127 | aType(DnsEvent.class), |
| 128 | anInt(101), |
| 129 | aByteArray(b(1), b(1), b(2), b(1), b(1), b(1), b(2), b(2)), |
| 130 | aByteArray(b(0), b(0), b(22), b(3), b(1), b(0), b(200), b(178)), |
| 131 | anIntArray(3456, 267, 1230, 45, 2111, 450, 638, 1300)); |
| 132 | |
| 133 | String want = joinLines( |
| 134 | "dropped_events: 0", |
| 135 | "events <", |
| 136 | " dns_lookup_batch <", |
| 137 | " event_types: 1", |
| 138 | " event_types: 1", |
| 139 | " event_types: 2", |
| 140 | " event_types: 1", |
| 141 | " event_types: 1", |
| 142 | " event_types: 1", |
| 143 | " event_types: 2", |
| 144 | " event_types: 2", |
| 145 | " latencies_ms: 3456", |
| 146 | " latencies_ms: 267", |
| 147 | " latencies_ms: 1230", |
| 148 | " latencies_ms: 45", |
| 149 | " latencies_ms: 2111", |
| 150 | " latencies_ms: 450", |
| 151 | " latencies_ms: 638", |
| 152 | " latencies_ms: 1300", |
| 153 | " network_id <", |
| 154 | " network_id: 101", |
| 155 | " >", |
| 156 | " return_codes: 0", |
| 157 | " return_codes: 0", |
| 158 | " return_codes: 22", |
| 159 | " return_codes: 3", |
| 160 | " return_codes: 1", |
| 161 | " return_codes: 0", |
| 162 | " return_codes: 200", |
| 163 | " return_codes: 178", |
| 164 | " >", |
| 165 | " time_ms: 1", |
Hugo Benichi | d680d4c | 2016-10-13 13:16:16 +0900 | [diff] [blame] | 166 | ">", |
| 167 | "version: 2"); |
Hugo Benichi | 50a84c6 | 2016-09-02 09:00:59 +0900 | [diff] [blame] | 168 | |
| 169 | verifySerialization(want, ev); |
| 170 | } |
| 171 | |
| 172 | public void testIpManagerEventSerialization() { |
| 173 | ConnectivityMetricsEvent ev = describeIpEvent( |
| 174 | aType(IpManagerEvent.class), |
| 175 | aString("wlan0"), |
| 176 | anInt(IpManagerEvent.PROVISIONING_OK), |
| 177 | aLong(5678)); |
| 178 | |
| 179 | String want = joinLines( |
| 180 | "dropped_events: 0", |
| 181 | "events <", |
| 182 | " ip_provisioning_event <", |
| 183 | " event_type: 1", |
| 184 | " if_name: \"wlan0\"", |
| 185 | " latency_ms: 5678", |
| 186 | " >", |
| 187 | " time_ms: 1", |
Hugo Benichi | d680d4c | 2016-10-13 13:16:16 +0900 | [diff] [blame] | 188 | ">", |
| 189 | "version: 2"); |
Hugo Benichi | 50a84c6 | 2016-09-02 09:00:59 +0900 | [diff] [blame] | 190 | |
| 191 | verifySerialization(want, ev); |
| 192 | } |
| 193 | |
| 194 | public void testIpReachabilityEventSerialization() { |
| 195 | ConnectivityMetricsEvent ev = describeIpEvent( |
| 196 | aType(IpReachabilityEvent.class), |
| 197 | aString("wlan0"), |
| 198 | anInt(IpReachabilityEvent.NUD_FAILED)); |
| 199 | |
| 200 | String want = joinLines( |
| 201 | "dropped_events: 0", |
| 202 | "events <", |
| 203 | " ip_reachability_event <", |
| 204 | " event_type: 512", |
| 205 | " if_name: \"wlan0\"", |
| 206 | " >", |
| 207 | " time_ms: 1", |
Hugo Benichi | d680d4c | 2016-10-13 13:16:16 +0900 | [diff] [blame] | 208 | ">", |
| 209 | "version: 2"); |
Hugo Benichi | 50a84c6 | 2016-09-02 09:00:59 +0900 | [diff] [blame] | 210 | |
| 211 | verifySerialization(want, ev); |
| 212 | } |
| 213 | |
| 214 | public void testNetworkEventSerialization() { |
| 215 | ConnectivityMetricsEvent ev = describeIpEvent( |
| 216 | aType(NetworkEvent.class), |
| 217 | anInt(100), |
| 218 | anInt(5), |
| 219 | aLong(20410)); |
| 220 | |
| 221 | String want = joinLines( |
| 222 | "dropped_events: 0", |
| 223 | "events <", |
| 224 | " network_event <", |
| 225 | " event_type: 5", |
| 226 | " latency_ms: 20410", |
| 227 | " network_id <", |
| 228 | " network_id: 100", |
| 229 | " >", |
| 230 | " >", |
| 231 | " time_ms: 1", |
Hugo Benichi | d680d4c | 2016-10-13 13:16:16 +0900 | [diff] [blame] | 232 | ">", |
| 233 | "version: 2"); |
Hugo Benichi | 50a84c6 | 2016-09-02 09:00:59 +0900 | [diff] [blame] | 234 | |
| 235 | verifySerialization(want, ev); |
| 236 | } |
| 237 | |
| 238 | public void testValidationProbeEventSerialization() { |
| 239 | ConnectivityMetricsEvent ev = describeIpEvent( |
| 240 | aType(ValidationProbeEvent.class), |
| 241 | anInt(120), |
| 242 | aLong(40730), |
| 243 | anInt(ValidationProbeEvent.PROBE_HTTP), |
| 244 | anInt(204)); |
| 245 | |
| 246 | String want = joinLines( |
| 247 | "dropped_events: 0", |
| 248 | "events <", |
| 249 | " time_ms: 1", |
| 250 | " validation_probe_event <", |
| 251 | " latency_ms: 40730", |
| 252 | " network_id <", |
| 253 | " network_id: 120", |
| 254 | " >", |
| 255 | " probe_result: 204", |
| 256 | " probe_type: 1", |
| 257 | " >", |
Hugo Benichi | d680d4c | 2016-10-13 13:16:16 +0900 | [diff] [blame] | 258 | ">", |
| 259 | "version: 2"); |
Hugo Benichi | 50a84c6 | 2016-09-02 09:00:59 +0900 | [diff] [blame] | 260 | |
| 261 | verifySerialization(want, ev); |
| 262 | } |
| 263 | |
| 264 | public void testApfProgramEventSerialization() { |
| 265 | ConnectivityMetricsEvent ev = describeIpEvent( |
| 266 | aType(ApfProgramEvent.class), |
| 267 | aLong(200), |
| 268 | anInt(7), |
| 269 | anInt(9), |
| 270 | anInt(2048), |
| 271 | anInt(3)); |
| 272 | |
| 273 | String want = joinLines( |
| 274 | "dropped_events: 0", |
| 275 | "events <", |
| 276 | " apf_program_event <", |
| 277 | " current_ras: 9", |
| 278 | " drop_multicast: true", |
| 279 | " filtered_ras: 7", |
| 280 | " has_ipv4_addr: true", |
| 281 | " lifetime: 200", |
| 282 | " program_length: 2048", |
| 283 | " >", |
| 284 | " time_ms: 1", |
Hugo Benichi | d680d4c | 2016-10-13 13:16:16 +0900 | [diff] [blame] | 285 | ">", |
| 286 | "version: 2"); |
Hugo Benichi | 50a84c6 | 2016-09-02 09:00:59 +0900 | [diff] [blame] | 287 | |
| 288 | verifySerialization(want, ev); |
| 289 | } |
| 290 | |
| 291 | public void testApfStatsSerialization() { |
| 292 | ConnectivityMetricsEvent ev = describeIpEvent( |
| 293 | aType(ApfStats.class), |
| 294 | aLong(45000), |
| 295 | anInt(10), |
| 296 | anInt(2), |
| 297 | anInt(2), |
| 298 | anInt(1), |
| 299 | anInt(2), |
| 300 | anInt(4), |
| 301 | anInt(2048)); |
| 302 | |
| 303 | String want = joinLines( |
| 304 | "dropped_events: 0", |
| 305 | "events <", |
| 306 | " 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", |
| 313 | " received_ras: 10", |
| 314 | " zero_lifetime_ras: 1", |
| 315 | " >", |
| 316 | " time_ms: 1", |
Hugo Benichi | d680d4c | 2016-10-13 13:16:16 +0900 | [diff] [blame] | 317 | ">", |
| 318 | "version: 2"); |
Hugo Benichi | 50a84c6 | 2016-09-02 09:00:59 +0900 | [diff] [blame] | 319 | |
| 320 | verifySerialization(want, ev); |
| 321 | } |
| 322 | |
| 323 | public void testRaEventSerialization() { |
| 324 | ConnectivityMetricsEvent ev = describeIpEvent( |
| 325 | aType(RaEvent.class), |
| 326 | aLong(2000), |
| 327 | aLong(400), |
| 328 | aLong(300), |
| 329 | aLong(-1), |
| 330 | aLong(1000), |
| 331 | aLong(-1)); |
| 332 | |
| 333 | String want = joinLines( |
| 334 | "dropped_events: 0", |
| 335 | "events <", |
| 336 | " ra_event <", |
| 337 | " dnssl_lifetime: -1", |
| 338 | " prefix_preferred_lifetime: 300", |
| 339 | " prefix_valid_lifetime: 400", |
| 340 | " rdnss_lifetime: 1000", |
| 341 | " route_info_lifetime: -1", |
| 342 | " router_lifetime: 2000", |
| 343 | " >", |
| 344 | " time_ms: 1", |
Hugo Benichi | d680d4c | 2016-10-13 13:16:16 +0900 | [diff] [blame] | 345 | ">", |
| 346 | "version: 2"); |
Hugo Benichi | 50a84c6 | 2016-09-02 09:00:59 +0900 | [diff] [blame] | 347 | |
| 348 | verifySerialization(want, ev); |
| 349 | } |
| 350 | |
| 351 | static void verifySerialization(String want, ConnectivityMetricsEvent... input) { |
| 352 | try { |
| 353 | byte[] got = IpConnectivityEventBuilder.serialize(0, Arrays.asList(input)); |
Glen Kuhne | 0719940 | 2016-10-25 15:47:25 -0700 | [diff] [blame] | 354 | IpConnectivityLog log = IpConnectivityLog.parseFrom(got); |
Hugo Benichi | 50a84c6 | 2016-09-02 09:00:59 +0900 | [diff] [blame] | 355 | assertEquals(want, log.toString()); |
| 356 | } catch (Exception e) { |
| 357 | fail(e.toString()); |
| 358 | } |
| 359 | } |
| 360 | |
| 361 | static String joinLines(String ... elems) { |
| 362 | StringBuilder b = new StringBuilder(); |
| 363 | for (String s : elems) { |
| 364 | b.append(s); |
| 365 | b.append("\n"); |
| 366 | } |
| 367 | return b.toString(); |
| 368 | } |
| 369 | } |