Hugo Benichi | 00a42d4 | 2016-09-13 15:55:09 +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 | |
| 19 | import android.content.Context; |
| 20 | import android.net.ConnectivityMetricsEvent; |
| 21 | import android.net.IIpConnectivityMetrics; |
| 22 | import android.net.metrics.ApfStats; |
| 23 | import android.net.metrics.DefaultNetworkEvent; |
| 24 | import android.net.metrics.DhcpClientEvent; |
| 25 | import android.net.metrics.IpConnectivityLog; |
| 26 | import android.net.metrics.IpManagerEvent; |
| 27 | import android.net.metrics.IpReachabilityEvent; |
| 28 | import android.net.metrics.RaEvent; |
| 29 | import android.net.metrics.ValidationProbeEvent; |
| 30 | import android.os.Parcelable; |
| 31 | import android.util.Base64; |
| 32 | import com.android.server.connectivity.metrics.IpConnectivityLogClass; |
| 33 | import com.google.protobuf.nano.MessageNano; |
| 34 | import java.io.PrintWriter; |
| 35 | import java.io.StringWriter; |
| 36 | import java.util.Collections; |
| 37 | import java.util.Comparator; |
| 38 | import java.util.Iterator; |
| 39 | import java.util.List; |
| 40 | import junit.framework.TestCase; |
| 41 | import org.mockito.ArgumentCaptor; |
| 42 | import org.mockito.Mock; |
| 43 | import org.mockito.MockitoAnnotations; |
| 44 | |
| 45 | import static org.mockito.Mockito.timeout; |
| 46 | import static org.mockito.Mockito.times; |
| 47 | import static org.mockito.Mockito.verify; |
| 48 | |
| 49 | public 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); |
| 60 | mService = new IpConnectivityMetrics(mCtx); |
| 61 | } |
| 62 | |
| 63 | public void testLoggingEvents() throws Exception { |
| 64 | IpConnectivityLog logger = new IpConnectivityLog(mMockService); |
| 65 | |
| 66 | assertTrue(logger.log(1, FAKE_EV)); |
| 67 | assertTrue(logger.log(2, FAKE_EV)); |
| 68 | assertTrue(logger.log(3, FAKE_EV)); |
| 69 | |
| 70 | List<ConnectivityMetricsEvent> got = verifyEvents(3); |
| 71 | assertEventsEqual(expectedEvent(1), got.get(0)); |
| 72 | assertEventsEqual(expectedEvent(2), got.get(1)); |
| 73 | assertEventsEqual(expectedEvent(3), got.get(2)); |
| 74 | } |
| 75 | |
| 76 | public void testLoggingEventsWithMultipleCallers() throws Exception { |
| 77 | IpConnectivityLog logger = new IpConnectivityLog(mMockService); |
| 78 | |
| 79 | final int nCallers = 10; |
| 80 | final int nEvents = 10; |
| 81 | for (int n = 0; n < nCallers; n++) { |
| 82 | final int i = n; |
| 83 | new Thread() { |
| 84 | public void run() { |
| 85 | for (int j = 0; j < nEvents; j++) { |
| 86 | assertTrue(logger.log(i * 100 + j, FAKE_EV)); |
| 87 | } |
| 88 | } |
| 89 | }.start(); |
| 90 | } |
| 91 | |
| 92 | List<ConnectivityMetricsEvent> got = verifyEvents(nCallers * nEvents, 100); |
| 93 | Collections.sort(got, EVENT_COMPARATOR); |
| 94 | Iterator<ConnectivityMetricsEvent> iter = got.iterator(); |
| 95 | for (int i = 0; i < nCallers; i++) { |
| 96 | for (int j = 0; j < nEvents; j++) { |
| 97 | int expectedTimestamp = i * 100 + j; |
| 98 | assertEventsEqual(expectedEvent(expectedTimestamp), iter.next()); |
| 99 | } |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | public void testBufferFlushing() { |
| 104 | String output1 = getdump("flush"); |
| 105 | assertEquals("", output1); |
| 106 | |
| 107 | new IpConnectivityLog(mService.impl).log(1, FAKE_EV); |
| 108 | String output2 = getdump("flush"); |
| 109 | assertFalse("".equals(output2)); |
| 110 | |
| 111 | String output3 = getdump("flush"); |
| 112 | assertEquals("", output3); |
| 113 | } |
| 114 | |
| 115 | public void testEndToEndLogging() { |
| 116 | IpConnectivityLog logger = new IpConnectivityLog(mService.impl); |
| 117 | |
| 118 | Parcelable[] events = { |
| 119 | new IpReachabilityEvent("wlan0", IpReachabilityEvent.NUD_FAILED), |
| 120 | new DhcpClientEvent("wlan0", "SomeState", 192), |
| 121 | new DefaultNetworkEvent(102, new int[]{1,2,3}, 101, true, false), |
| 122 | new IpManagerEvent("wlan0", IpManagerEvent.PROVISIONING_OK, 5678), |
| 123 | new ValidationProbeEvent(120, 40730, ValidationProbeEvent.PROBE_HTTP, 204), |
| 124 | new ApfStats(45000, 10, 2, 2, 1, 2, 4, 2048), |
| 125 | new RaEvent(2000, 400, 300, -1, 1000, -1) |
| 126 | }; |
| 127 | |
| 128 | for (int i = 0; i < events.length; i++) { |
| 129 | logger.log(100 * (i + 1), events[i]); |
| 130 | } |
| 131 | |
| 132 | String want = joinLines( |
| 133 | "dropped_events: 0", |
| 134 | "events <", |
| 135 | " ip_reachability_event <", |
| 136 | " event_type: 512", |
| 137 | " if_name: \"wlan0\"", |
| 138 | " >", |
| 139 | " time_ms: 100", |
| 140 | ">", |
| 141 | "events <", |
| 142 | " dhcp_event <", |
| 143 | " duration_ms: 192", |
| 144 | " error_code: 0", |
| 145 | " if_name: \"wlan0\"", |
| 146 | " state_transition: \"SomeState\"", |
| 147 | " >", |
| 148 | " time_ms: 200", |
| 149 | ">", |
| 150 | "events <", |
| 151 | " default_network_event <", |
| 152 | " network_id <", |
| 153 | " network_id: 102", |
| 154 | " >", |
| 155 | " previous_network_id <", |
| 156 | " network_id: 101", |
| 157 | " >", |
| 158 | " previous_network_ip_support: 1", |
| 159 | " transport_types: 1", |
| 160 | " transport_types: 2", |
| 161 | " transport_types: 3", |
| 162 | " >", |
| 163 | " time_ms: 300", |
| 164 | ">", |
| 165 | "events <", |
| 166 | " ip_provisioning_event <", |
| 167 | " event_type: 1", |
| 168 | " if_name: \"wlan0\"", |
| 169 | " latency_ms: 5678", |
| 170 | " >", |
| 171 | " time_ms: 400", |
| 172 | ">", |
| 173 | "events <", |
| 174 | " time_ms: 500", |
| 175 | " validation_probe_event <", |
| 176 | " latency_ms: 40730", |
| 177 | " network_id <", |
| 178 | " network_id: 120", |
| 179 | " >", |
| 180 | " probe_result: 204", |
| 181 | " probe_type: 1", |
| 182 | " >", |
| 183 | ">", |
| 184 | "events <", |
| 185 | " apf_statistics <", |
| 186 | " dropped_ras: 2", |
| 187 | " duration_ms: 45000", |
| 188 | " matching_ras: 2", |
| 189 | " max_program_size: 2048", |
| 190 | " parse_errors: 2", |
| 191 | " program_updates: 4", |
| 192 | " received_ras: 10", |
| 193 | " zero_lifetime_ras: 1", |
| 194 | " >", |
| 195 | " time_ms: 600", |
| 196 | ">", |
| 197 | "events <", |
| 198 | " ra_event <", |
| 199 | " dnssl_lifetime: -1", |
| 200 | " prefix_preferred_lifetime: 300", |
| 201 | " prefix_valid_lifetime: 400", |
| 202 | " rdnss_lifetime: 1000", |
| 203 | " route_info_lifetime: -1", |
| 204 | " router_lifetime: 2000", |
| 205 | " >", |
| 206 | " time_ms: 700", |
Hugo Benichi | d680d4c | 2016-10-13 13:16:16 +0900 | [diff] [blame^] | 207 | ">", |
| 208 | "version: 2"); |
Hugo Benichi | 00a42d4 | 2016-09-13 15:55:09 +0900 | [diff] [blame] | 209 | |
| 210 | verifySerialization(want, getdump("flush")); |
| 211 | } |
| 212 | |
| 213 | String getdump(String ... command) { |
| 214 | StringWriter buffer = new StringWriter(); |
| 215 | PrintWriter writer = new PrintWriter(buffer); |
| 216 | mService.impl.dump(null, writer, command); |
| 217 | return buffer.toString(); |
| 218 | } |
| 219 | |
| 220 | List<ConnectivityMetricsEvent> verifyEvents(int n, int timeoutMs) throws Exception { |
| 221 | ArgumentCaptor<ConnectivityMetricsEvent> captor = |
| 222 | ArgumentCaptor.forClass(ConnectivityMetricsEvent.class); |
| 223 | verify(mMockService, timeout(timeoutMs).times(n)).logEvent(captor.capture()); |
| 224 | return captor.getAllValues(); |
| 225 | } |
| 226 | |
| 227 | List<ConnectivityMetricsEvent> verifyEvents(int n) throws Exception { |
| 228 | return verifyEvents(n, 10); |
| 229 | } |
| 230 | |
| 231 | static void verifySerialization(String want, String output) { |
| 232 | try { |
| 233 | byte[] got = Base64.decode(output, Base64.DEFAULT); |
| 234 | IpConnectivityLogClass.IpConnectivityLog log = |
| 235 | new IpConnectivityLogClass.IpConnectivityLog(); |
| 236 | MessageNano.mergeFrom(log, got); |
| 237 | assertEquals(want, log.toString()); |
| 238 | } catch (Exception e) { |
| 239 | fail(e.toString()); |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | static String joinLines(String ... elems) { |
| 244 | StringBuilder b = new StringBuilder(); |
| 245 | for (String s : elems) { |
| 246 | b.append(s).append("\n"); |
| 247 | } |
| 248 | return b.toString(); |
| 249 | } |
| 250 | |
| 251 | static ConnectivityMetricsEvent expectedEvent(int timestamp) { |
| 252 | return new ConnectivityMetricsEvent((long)timestamp, 0, 0, FAKE_EV); |
| 253 | } |
| 254 | |
| 255 | /** Outer equality for ConnectivityMetricsEvent to avoid overriding equals() and hashCode(). */ |
| 256 | static void assertEventsEqual(ConnectivityMetricsEvent expected, ConnectivityMetricsEvent got) { |
| 257 | assertEquals(expected.timestamp, got.timestamp); |
| 258 | assertEquals(expected.componentTag, got.componentTag); |
| 259 | assertEquals(expected.eventTag, got.eventTag); |
| 260 | assertEquals(expected.data, got.data); |
| 261 | } |
| 262 | |
| 263 | static final Comparator<ConnectivityMetricsEvent> EVENT_COMPARATOR = |
| 264 | new Comparator<ConnectivityMetricsEvent>() { |
| 265 | @Override |
| 266 | public int compare(ConnectivityMetricsEvent ev1, ConnectivityMetricsEvent ev2) { |
| 267 | return (int) (ev1.timestamp - ev2.timestamp); |
| 268 | } |
| 269 | }; |
| 270 | } |