Hugo Benichi | 5e05518 | 2016-06-01 08:50:38 +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 | |
Hugo Benichi | 2a5cfb9 | 2017-03-22 22:21:44 +0900 | [diff] [blame] | 19 | import static android.net.metrics.INetdEventListener.EVENT_GETADDRINFO; |
| 20 | import static android.net.metrics.INetdEventListener.EVENT_GETHOSTBYNAME; |
| 21 | import static org.junit.Assert.assertEquals; |
Hugo Benichi | f562ac3 | 2017-09-04 13:24:43 +0900 | [diff] [blame] | 22 | import static org.junit.Assert.assertTrue; |
Hugo Benichi | 2a5cfb9 | 2017-03-22 22:21:44 +0900 | [diff] [blame] | 23 | import static org.junit.Assert.fail; |
Hugo Benichi | 5e05518 | 2016-06-01 08:50:38 +0900 | [diff] [blame] | 24 | import static org.mockito.Mockito.any; |
Hugo Benichi | 2a5cfb9 | 2017-03-22 22:21:44 +0900 | [diff] [blame] | 25 | import static org.mockito.Mockito.mock; |
Hugo Benichi | 5e05518 | 2016-06-01 08:50:38 +0900 | [diff] [blame] | 26 | import static org.mockito.Mockito.anyInt; |
| 27 | import static org.mockito.Mockito.eq; |
| 28 | import static org.mockito.Mockito.timeout; |
| 29 | import static org.mockito.Mockito.times; |
| 30 | import static org.mockito.Mockito.verify; |
Hugo Benichi | 2a5cfb9 | 2017-03-22 22:21:44 +0900 | [diff] [blame] | 31 | import static org.mockito.Mockito.when; |
Hugo Benichi | 5e05518 | 2016-06-01 08:50:38 +0900 | [diff] [blame] | 32 | |
Hugo Benichi | 2a5cfb9 | 2017-03-22 22:21:44 +0900 | [diff] [blame] | 33 | import android.content.Context; |
| 34 | import android.net.ConnectivityManager; |
| 35 | import android.net.Network; |
| 36 | import android.net.NetworkCapabilities; |
| 37 | import android.support.test.runner.AndroidJUnit4; |
| 38 | import android.system.OsConstants; |
| 39 | import android.test.suitebuilder.annotation.SmallTest; |
| 40 | import android.util.Base64; |
Hugo Benichi | 60c9f63 | 2017-09-05 13:34:48 +0900 | [diff] [blame] | 41 | |
Hugo Benichi | 2a5cfb9 | 2017-03-22 22:21:44 +0900 | [diff] [blame] | 42 | import com.android.server.connectivity.metrics.nano.IpConnectivityLogClass.DNSLookupBatch; |
| 43 | import com.android.server.connectivity.metrics.nano.IpConnectivityLogClass.IpConnectivityEvent; |
| 44 | import com.android.server.connectivity.metrics.nano.IpConnectivityLogClass.IpConnectivityLog; |
Hugo Benichi | 60c9f63 | 2017-09-05 13:34:48 +0900 | [diff] [blame] | 45 | |
Hugo Benichi | 2a5cfb9 | 2017-03-22 22:21:44 +0900 | [diff] [blame] | 46 | import java.io.FileOutputStream; |
| 47 | import java.io.PrintWriter; |
| 48 | import java.io.StringWriter; |
| 49 | import java.util.ArrayList; |
| 50 | import java.util.Arrays; |
| 51 | import java.util.Comparator; |
| 52 | import java.util.List; |
Hugo Benichi | 60c9f63 | 2017-09-05 13:34:48 +0900 | [diff] [blame] | 53 | |
Hugo Benichi | 2a5cfb9 | 2017-03-22 22:21:44 +0900 | [diff] [blame] | 54 | import org.junit.Before; |
| 55 | import org.junit.Test; |
| 56 | import org.junit.runner.RunWith; |
Hugo Benichi | 5e05518 | 2016-06-01 08:50:38 +0900 | [diff] [blame] | 57 | |
Hugo Benichi | 2a5cfb9 | 2017-03-22 22:21:44 +0900 | [diff] [blame] | 58 | @RunWith(AndroidJUnit4.class) |
| 59 | @SmallTest |
| 60 | public class NetdEventListenerServiceTest { |
Hugo Benichi | 0d4a398 | 2016-11-25 11:24:22 +0900 | [diff] [blame] | 61 | private static final String EXAMPLE_IPV4 = "192.0.2.1"; |
| 62 | private static final String EXAMPLE_IPV6 = "2001:db8:1200::2:1"; |
| 63 | |
Michal Karpinski | 4f26103 | 2016-09-26 09:20:25 +0100 | [diff] [blame] | 64 | NetdEventListenerService mNetdEventListenerService; |
Hugo Benichi | 2a5cfb9 | 2017-03-22 22:21:44 +0900 | [diff] [blame] | 65 | ConnectivityManager mCm; |
Hugo Benichi | 5e05518 | 2016-06-01 08:50:38 +0900 | [diff] [blame] | 66 | |
Hugo Benichi | 2a5cfb9 | 2017-03-22 22:21:44 +0900 | [diff] [blame] | 67 | @Before |
Hugo Benichi | 5e05518 | 2016-06-01 08:50:38 +0900 | [diff] [blame] | 68 | public void setUp() { |
Hugo Benichi | 2a5cfb9 | 2017-03-22 22:21:44 +0900 | [diff] [blame] | 69 | NetworkCapabilities ncWifi = new NetworkCapabilities(); |
| 70 | NetworkCapabilities ncCell = new NetworkCapabilities(); |
| 71 | ncWifi.addTransportType(NetworkCapabilities.TRANSPORT_WIFI); |
| 72 | ncCell.addTransportType(NetworkCapabilities.TRANSPORT_CELLULAR); |
Hugo Benichi | 5e05518 | 2016-06-01 08:50:38 +0900 | [diff] [blame] | 73 | |
Hugo Benichi | 5eb9053 | 2017-03-23 18:38:22 +0900 | [diff] [blame] | 74 | mCm = mock(ConnectivityManager.class); |
Hugo Benichi | 2a5cfb9 | 2017-03-22 22:21:44 +0900 | [diff] [blame] | 75 | when(mCm.getNetworkCapabilities(new Network(100))).thenReturn(ncWifi); |
| 76 | when(mCm.getNetworkCapabilities(new Network(101))).thenReturn(ncCell); |
Hugo Benichi | 5e05518 | 2016-06-01 08:50:38 +0900 | [diff] [blame] | 77 | |
Hugo Benichi | 5eb9053 | 2017-03-23 18:38:22 +0900 | [diff] [blame] | 78 | mNetdEventListenerService = new NetdEventListenerService(mCm); |
| 79 | } |
| 80 | |
| 81 | @Test |
Hugo Benichi | f562ac3 | 2017-09-04 13:24:43 +0900 | [diff] [blame] | 82 | public void testWakeupEventLogging() throws Exception { |
| 83 | final int BUFFER_LENGTH = NetdEventListenerService.WAKEUP_EVENT_BUFFER_LENGTH; |
| 84 | |
Hugo Benichi | 380a063 | 2017-10-20 09:25:29 +0900 | [diff] [blame^] | 85 | // Baseline without any event |
| 86 | String[] baseline = listNetdEvent(); |
Hugo Benichi | f562ac3 | 2017-09-04 13:24:43 +0900 | [diff] [blame] | 87 | |
| 88 | long now = System.currentTimeMillis(); |
| 89 | String prefix = "iface:wlan0"; |
| 90 | int[] uids = { 10001, 10002, 10004, 1000, 10052, 10023, 10002, 10123, 10004 }; |
| 91 | for (int uid : uids) { |
| 92 | mNetdEventListenerService.onWakeupEvent(prefix, uid, uid, now); |
| 93 | } |
| 94 | |
Hugo Benichi | 380a063 | 2017-10-20 09:25:29 +0900 | [diff] [blame^] | 95 | String[] events2 = remove(listNetdEvent(), baseline); |
Hugo Benichi | 60c9f63 | 2017-09-05 13:34:48 +0900 | [diff] [blame] | 96 | int expectedLength2 = uids.length + 1; // +1 for the WakeupStats line |
| 97 | assertEquals(expectedLength2, events2.length); |
| 98 | assertContains(events2[0], "WakeupStats"); |
| 99 | assertContains(events2[0], "wlan0"); |
Hugo Benichi | f562ac3 | 2017-09-04 13:24:43 +0900 | [diff] [blame] | 100 | for (int i = 0; i < uids.length; i++) { |
Hugo Benichi | 60c9f63 | 2017-09-05 13:34:48 +0900 | [diff] [blame] | 101 | String got = events2[i+1]; |
| 102 | assertContains(got, "WakeupEvent"); |
Hugo Benichi | f562ac3 | 2017-09-04 13:24:43 +0900 | [diff] [blame] | 103 | assertContains(got, "wlan0"); |
| 104 | assertContains(got, "uid: " + uids[i]); |
| 105 | } |
| 106 | |
| 107 | int uid = 20000; |
| 108 | for (int i = 0; i < BUFFER_LENGTH * 2; i++) { |
| 109 | long ts = now + 10; |
| 110 | mNetdEventListenerService.onWakeupEvent(prefix, uid, uid, ts); |
| 111 | } |
| 112 | |
Hugo Benichi | 380a063 | 2017-10-20 09:25:29 +0900 | [diff] [blame^] | 113 | String[] events3 = remove(listNetdEvent(), baseline); |
Hugo Benichi | 60c9f63 | 2017-09-05 13:34:48 +0900 | [diff] [blame] | 114 | int expectedLength3 = BUFFER_LENGTH + 1; // +1 for the WakeupStats line |
| 115 | assertEquals(expectedLength3, events3.length); |
| 116 | assertContains(events2[0], "WakeupStats"); |
| 117 | assertContains(events2[0], "wlan0"); |
| 118 | for (int i = 1; i < expectedLength3; i++) { |
| 119 | String got = events3[i]; |
| 120 | assertContains(got, "WakeupEvent"); |
Hugo Benichi | f562ac3 | 2017-09-04 13:24:43 +0900 | [diff] [blame] | 121 | assertContains(got, "wlan0"); |
| 122 | assertContains(got, "uid: " + uid); |
| 123 | } |
| 124 | |
| 125 | uid = 45678; |
| 126 | mNetdEventListenerService.onWakeupEvent(prefix, uid, uid, now); |
| 127 | |
Hugo Benichi | 380a063 | 2017-10-20 09:25:29 +0900 | [diff] [blame^] | 128 | String[] events4 = remove(listNetdEvent(), baseline); |
Hugo Benichi | f562ac3 | 2017-09-04 13:24:43 +0900 | [diff] [blame] | 129 | String lastEvent = events4[events4.length - 1]; |
Hugo Benichi | 60c9f63 | 2017-09-05 13:34:48 +0900 | [diff] [blame] | 130 | assertContains(lastEvent, "WakeupEvent"); |
Hugo Benichi | f562ac3 | 2017-09-04 13:24:43 +0900 | [diff] [blame] | 131 | assertContains(lastEvent, "wlan0"); |
| 132 | assertContains(lastEvent, "uid: " + uid); |
| 133 | } |
| 134 | |
| 135 | @Test |
Hugo Benichi | 60c9f63 | 2017-09-05 13:34:48 +0900 | [diff] [blame] | 136 | public void testWakeupStatsLogging() throws Exception { |
| 137 | wakeupEvent("wlan0", 1000); |
| 138 | wakeupEvent("rmnet0", 10123); |
| 139 | wakeupEvent("wlan0", 1000); |
| 140 | wakeupEvent("rmnet0", 10008); |
| 141 | wakeupEvent("wlan0", -1); |
| 142 | wakeupEvent("wlan0", 10008); |
| 143 | wakeupEvent("rmnet0", 1000); |
| 144 | wakeupEvent("wlan0", 10004); |
| 145 | wakeupEvent("wlan0", 1000); |
| 146 | wakeupEvent("wlan0", 0); |
| 147 | wakeupEvent("wlan0", -1); |
| 148 | wakeupEvent("rmnet0", 10052); |
| 149 | wakeupEvent("wlan0", 0); |
| 150 | wakeupEvent("rmnet0", 1000); |
| 151 | wakeupEvent("wlan0", 1010); |
| 152 | |
| 153 | String got = flushStatistics(); |
| 154 | String want = String.join("\n", |
| 155 | "dropped_events: 0", |
| 156 | "events <", |
| 157 | " if_name: \"\"", |
| 158 | " link_layer: 2", |
| 159 | " network_id: 0", |
| 160 | " time_ms: 0", |
| 161 | " transports: 0", |
| 162 | " wakeup_stats <", |
| 163 | " application_wakeups: 3", |
| 164 | " duration_sec: 0", |
Hugo Benichi | 175b574 | 2017-09-19 13:15:26 +0900 | [diff] [blame] | 165 | " no_uid_wakeups: 0", |
Hugo Benichi | 60c9f63 | 2017-09-05 13:34:48 +0900 | [diff] [blame] | 166 | " non_application_wakeups: 0", |
| 167 | " root_wakeups: 0", |
| 168 | " system_wakeups: 2", |
| 169 | " total_wakeups: 5", |
Hugo Benichi | 60c9f63 | 2017-09-05 13:34:48 +0900 | [diff] [blame] | 170 | " >", |
| 171 | ">", |
| 172 | "events <", |
| 173 | " if_name: \"\"", |
| 174 | " link_layer: 4", |
| 175 | " network_id: 0", |
| 176 | " time_ms: 0", |
| 177 | " transports: 0", |
| 178 | " wakeup_stats <", |
| 179 | " application_wakeups: 2", |
| 180 | " duration_sec: 0", |
Hugo Benichi | 175b574 | 2017-09-19 13:15:26 +0900 | [diff] [blame] | 181 | " no_uid_wakeups: 2", |
Hugo Benichi | 60c9f63 | 2017-09-05 13:34:48 +0900 | [diff] [blame] | 182 | " non_application_wakeups: 1", |
| 183 | " root_wakeups: 2", |
| 184 | " system_wakeups: 3", |
| 185 | " total_wakeups: 10", |
Hugo Benichi | 60c9f63 | 2017-09-05 13:34:48 +0900 | [diff] [blame] | 186 | " >", |
| 187 | ">", |
| 188 | "version: 2\n"); |
| 189 | assertEquals(want, got); |
| 190 | } |
| 191 | |
| 192 | @Test |
Hugo Benichi | 5eb9053 | 2017-03-23 18:38:22 +0900 | [diff] [blame] | 193 | public void testDnsLogging() throws Exception { |
| 194 | asyncDump(100); |
| 195 | |
Hugo Benichi | 2a5cfb9 | 2017-03-22 22:21:44 +0900 | [diff] [blame] | 196 | dnsEvent(100, EVENT_GETADDRINFO, 0, 3456); |
| 197 | dnsEvent(100, EVENT_GETADDRINFO, 0, 267); |
| 198 | dnsEvent(100, EVENT_GETHOSTBYNAME, 22, 1230); |
| 199 | dnsEvent(100, EVENT_GETADDRINFO, 3, 45); |
| 200 | dnsEvent(100, EVENT_GETADDRINFO, 1, 2111); |
| 201 | dnsEvent(100, EVENT_GETADDRINFO, 0, 450); |
| 202 | dnsEvent(100, EVENT_GETHOSTBYNAME, 200, 638); |
| 203 | dnsEvent(100, EVENT_GETHOSTBYNAME, 178, 1300); |
| 204 | dnsEvent(101, EVENT_GETADDRINFO, 0, 56); |
| 205 | dnsEvent(101, EVENT_GETADDRINFO, 0, 78); |
| 206 | dnsEvent(101, EVENT_GETADDRINFO, 0, 14); |
| 207 | dnsEvent(101, EVENT_GETHOSTBYNAME, 0, 56); |
| 208 | dnsEvent(101, EVENT_GETADDRINFO, 0, 78); |
| 209 | dnsEvent(101, EVENT_GETADDRINFO, 0, 14); |
Hugo Benichi | 5e05518 | 2016-06-01 08:50:38 +0900 | [diff] [blame] | 210 | |
Hugo Benichi | 2a5cfb9 | 2017-03-22 22:21:44 +0900 | [diff] [blame] | 211 | String got = flushStatistics(); |
| 212 | String want = String.join("\n", |
| 213 | "dropped_events: 0", |
| 214 | "events <", |
| 215 | " if_name: \"\"", |
Hugo Benichi | 2a5cfb9 | 2017-03-22 22:21:44 +0900 | [diff] [blame] | 216 | " link_layer: 4", |
| 217 | " network_id: 100", |
| 218 | " time_ms: 0", |
| 219 | " transports: 2", |
| 220 | " dns_lookup_batch <", |
| 221 | " event_types: 1", |
| 222 | " event_types: 1", |
| 223 | " event_types: 2", |
| 224 | " event_types: 1", |
| 225 | " event_types: 1", |
| 226 | " event_types: 1", |
| 227 | " event_types: 2", |
| 228 | " event_types: 2", |
Hugo Benichi | 4eccf78 | 2017-07-18 14:28:27 +0900 | [diff] [blame] | 229 | " getaddrinfo_error_count: 0", |
| 230 | " getaddrinfo_query_count: 0", |
| 231 | " gethostbyname_error_count: 0", |
| 232 | " gethostbyname_query_count: 0", |
Hugo Benichi | 2a5cfb9 | 2017-03-22 22:21:44 +0900 | [diff] [blame] | 233 | " latencies_ms: 3456", |
| 234 | " latencies_ms: 267", |
| 235 | " latencies_ms: 1230", |
| 236 | " latencies_ms: 45", |
| 237 | " latencies_ms: 2111", |
| 238 | " latencies_ms: 450", |
| 239 | " latencies_ms: 638", |
| 240 | " latencies_ms: 1300", |
| 241 | " return_codes: 0", |
| 242 | " return_codes: 0", |
| 243 | " return_codes: 22", |
| 244 | " return_codes: 3", |
| 245 | " return_codes: 1", |
| 246 | " return_codes: 0", |
| 247 | " return_codes: 200", |
| 248 | " return_codes: 178", |
| 249 | " >", |
| 250 | ">", |
| 251 | "events <", |
| 252 | " if_name: \"\"", |
| 253 | " link_layer: 2", |
| 254 | " network_id: 101", |
| 255 | " time_ms: 0", |
| 256 | " transports: 1", |
| 257 | " dns_lookup_batch <", |
| 258 | " event_types: 1", |
| 259 | " event_types: 1", |
| 260 | " event_types: 1", |
| 261 | " event_types: 2", |
| 262 | " event_types: 1", |
| 263 | " event_types: 1", |
Hugo Benichi | 4eccf78 | 2017-07-18 14:28:27 +0900 | [diff] [blame] | 264 | " getaddrinfo_error_count: 0", |
| 265 | " getaddrinfo_query_count: 0", |
| 266 | " gethostbyname_error_count: 0", |
| 267 | " gethostbyname_query_count: 0", |
Hugo Benichi | 2a5cfb9 | 2017-03-22 22:21:44 +0900 | [diff] [blame] | 268 | " latencies_ms: 56", |
| 269 | " latencies_ms: 78", |
| 270 | " latencies_ms: 14", |
| 271 | " latencies_ms: 56", |
| 272 | " latencies_ms: 78", |
| 273 | " latencies_ms: 14", |
| 274 | " return_codes: 0", |
| 275 | " return_codes: 0", |
| 276 | " return_codes: 0", |
| 277 | " return_codes: 0", |
| 278 | " return_codes: 0", |
| 279 | " return_codes: 0", |
| 280 | " >", |
| 281 | ">", |
| 282 | "version: 2\n"); |
| 283 | assertEquals(want, got); |
Hugo Benichi | 5e05518 | 2016-06-01 08:50:38 +0900 | [diff] [blame] | 284 | } |
| 285 | |
Hugo Benichi | 2a5cfb9 | 2017-03-22 22:21:44 +0900 | [diff] [blame] | 286 | @Test |
Hugo Benichi | 0d4a398 | 2016-11-25 11:24:22 +0900 | [diff] [blame] | 287 | public void testConnectLogging() throws Exception { |
Hugo Benichi | 5eb9053 | 2017-03-23 18:38:22 +0900 | [diff] [blame] | 288 | asyncDump(100); |
| 289 | |
Hugo Benichi | 0d4a398 | 2016-11-25 11:24:22 +0900 | [diff] [blame] | 290 | final int OK = 0; |
| 291 | Thread[] logActions = { |
| 292 | // ignored |
Hugo Benichi | 5eb9053 | 2017-03-23 18:38:22 +0900 | [diff] [blame] | 293 | connectEventAction(100, OsConstants.EALREADY, 0, EXAMPLE_IPV4), |
| 294 | connectEventAction(100, OsConstants.EALREADY, 0, EXAMPLE_IPV6), |
| 295 | connectEventAction(100, OsConstants.EINPROGRESS, 0, EXAMPLE_IPV4), |
| 296 | connectEventAction(101, OsConstants.EINPROGRESS, 0, EXAMPLE_IPV6), |
| 297 | connectEventAction(101, OsConstants.EINPROGRESS, 0, EXAMPLE_IPV6), |
Hugo Benichi | 0d4a398 | 2016-11-25 11:24:22 +0900 | [diff] [blame] | 298 | // valid latencies |
Hugo Benichi | 5eb9053 | 2017-03-23 18:38:22 +0900 | [diff] [blame] | 299 | connectEventAction(100, OK, 110, EXAMPLE_IPV4), |
| 300 | connectEventAction(100, OK, 23, EXAMPLE_IPV4), |
| 301 | connectEventAction(100, OK, 45, EXAMPLE_IPV4), |
| 302 | connectEventAction(101, OK, 56, EXAMPLE_IPV4), |
| 303 | connectEventAction(101, OK, 523, EXAMPLE_IPV6), |
| 304 | connectEventAction(101, OK, 214, EXAMPLE_IPV6), |
| 305 | connectEventAction(101, OK, 67, EXAMPLE_IPV6), |
Hugo Benichi | 0d4a398 | 2016-11-25 11:24:22 +0900 | [diff] [blame] | 306 | // errors |
Hugo Benichi | 5eb9053 | 2017-03-23 18:38:22 +0900 | [diff] [blame] | 307 | connectEventAction(100, OsConstants.EPERM, 0, EXAMPLE_IPV4), |
| 308 | connectEventAction(101, OsConstants.EPERM, 0, EXAMPLE_IPV4), |
| 309 | connectEventAction(100, OsConstants.EAGAIN, 0, EXAMPLE_IPV4), |
| 310 | connectEventAction(100, OsConstants.EACCES, 0, EXAMPLE_IPV4), |
| 311 | connectEventAction(101, OsConstants.EACCES, 0, EXAMPLE_IPV4), |
| 312 | connectEventAction(101, OsConstants.EACCES, 0, EXAMPLE_IPV6), |
| 313 | connectEventAction(100, OsConstants.EADDRINUSE, 0, EXAMPLE_IPV4), |
| 314 | connectEventAction(101, OsConstants.ETIMEDOUT, 0, EXAMPLE_IPV4), |
| 315 | connectEventAction(100, OsConstants.ETIMEDOUT, 0, EXAMPLE_IPV6), |
| 316 | connectEventAction(100, OsConstants.ETIMEDOUT, 0, EXAMPLE_IPV6), |
| 317 | connectEventAction(101, OsConstants.ECONNREFUSED, 0, EXAMPLE_IPV4), |
Hugo Benichi | 0d4a398 | 2016-11-25 11:24:22 +0900 | [diff] [blame] | 318 | }; |
| 319 | |
| 320 | for (Thread t : logActions) { |
| 321 | t.start(); |
| 322 | } |
| 323 | for (Thread t : logActions) { |
| 324 | t.join(); |
| 325 | } |
| 326 | |
Hugo Benichi | 5eb9053 | 2017-03-23 18:38:22 +0900 | [diff] [blame] | 327 | String got = flushStatistics(); |
Hugo Benichi | 0d4a398 | 2016-11-25 11:24:22 +0900 | [diff] [blame] | 328 | String want = String.join("\n", |
Hugo Benichi | 5eb9053 | 2017-03-23 18:38:22 +0900 | [diff] [blame] | 329 | "dropped_events: 0", |
| 330 | "events <", |
| 331 | " if_name: \"\"", |
| 332 | " link_layer: 4", |
| 333 | " network_id: 100", |
| 334 | " time_ms: 0", |
| 335 | " transports: 2", |
| 336 | " connect_statistics <", |
| 337 | " connect_blocking_count: 3", |
| 338 | " connect_count: 6", |
| 339 | " errnos_counters <", |
| 340 | " key: 1", |
| 341 | " value: 1", |
| 342 | " >", |
| 343 | " errnos_counters <", |
| 344 | " key: 11", |
| 345 | " value: 1", |
| 346 | " >", |
| 347 | " errnos_counters <", |
| 348 | " key: 13", |
| 349 | " value: 1", |
| 350 | " >", |
| 351 | " errnos_counters <", |
| 352 | " key: 98", |
| 353 | " value: 1", |
| 354 | " >", |
| 355 | " errnos_counters <", |
| 356 | " key: 110", |
| 357 | " value: 2", |
| 358 | " >", |
| 359 | " ipv6_addr_count: 1", |
| 360 | " latencies_ms: 23", |
| 361 | " latencies_ms: 45", |
| 362 | " latencies_ms: 110", |
Hugo Benichi | 0d4a398 | 2016-11-25 11:24:22 +0900 | [diff] [blame] | 363 | " >", |
Hugo Benichi | 5eb9053 | 2017-03-23 18:38:22 +0900 | [diff] [blame] | 364 | ">", |
| 365 | "events <", |
| 366 | " if_name: \"\"", |
| 367 | " link_layer: 2", |
| 368 | " network_id: 101", |
| 369 | " time_ms: 0", |
| 370 | " transports: 1", |
| 371 | " connect_statistics <", |
| 372 | " connect_blocking_count: 4", |
| 373 | " connect_count: 6", |
| 374 | " errnos_counters <", |
| 375 | " key: 1", |
| 376 | " value: 1", |
| 377 | " >", |
| 378 | " errnos_counters <", |
| 379 | " key: 13", |
| 380 | " value: 2", |
| 381 | " >", |
| 382 | " errnos_counters <", |
| 383 | " key: 110", |
| 384 | " value: 1", |
| 385 | " >", |
| 386 | " errnos_counters <", |
| 387 | " key: 111", |
| 388 | " value: 1", |
| 389 | " >", |
| 390 | " ipv6_addr_count: 5", |
| 391 | " latencies_ms: 56", |
| 392 | " latencies_ms: 67", |
| 393 | " latencies_ms: 214", |
| 394 | " latencies_ms: 523", |
Hugo Benichi | 0d4a398 | 2016-11-25 11:24:22 +0900 | [diff] [blame] | 395 | " >", |
Hugo Benichi | 5eb9053 | 2017-03-23 18:38:22 +0900 | [diff] [blame] | 396 | ">", |
| 397 | "version: 2\n"); |
| 398 | assertEquals(want, got); |
Hugo Benichi | 0d4a398 | 2016-11-25 11:24:22 +0900 | [diff] [blame] | 399 | } |
| 400 | |
Hugo Benichi | 5eb9053 | 2017-03-23 18:38:22 +0900 | [diff] [blame] | 401 | Thread connectEventAction(int netId, int error, int latencyMs, String ipAddr) { |
Hugo Benichi | 0d4a398 | 2016-11-25 11:24:22 +0900 | [diff] [blame] | 402 | return new Thread(() -> { |
| 403 | try { |
Hugo Benichi | 5eb9053 | 2017-03-23 18:38:22 +0900 | [diff] [blame] | 404 | mNetdEventListenerService.onConnectEvent(netId, error, latencyMs, ipAddr, 80, 1); |
Hugo Benichi | 0d4a398 | 2016-11-25 11:24:22 +0900 | [diff] [blame] | 405 | } catch (Exception e) { |
| 406 | fail(e.toString()); |
| 407 | } |
| 408 | }); |
| 409 | } |
| 410 | |
Hugo Benichi | 2a5cfb9 | 2017-03-22 22:21:44 +0900 | [diff] [blame] | 411 | void dnsEvent(int netId, int type, int result, int latency) throws Exception { |
| 412 | mNetdEventListenerService.onDnsEvent(netId, type, result, latency, "", null, 0, 0); |
| 413 | } |
| 414 | |
Hugo Benichi | 60c9f63 | 2017-09-05 13:34:48 +0900 | [diff] [blame] | 415 | void wakeupEvent(String iface, int uid) throws Exception { |
| 416 | String prefix = NetdEventListenerService.WAKEUP_EVENT_IFACE_PREFIX + iface; |
| 417 | mNetdEventListenerService.onWakeupEvent(prefix, uid, uid, 0); |
| 418 | } |
| 419 | |
Hugo Benichi | 5eb9053 | 2017-03-23 18:38:22 +0900 | [diff] [blame] | 420 | void asyncDump(long durationMs) throws Exception { |
Hugo Benichi | 2a5cfb9 | 2017-03-22 22:21:44 +0900 | [diff] [blame] | 421 | final long stop = System.currentTimeMillis() + durationMs; |
| 422 | final PrintWriter pw = new PrintWriter(new FileOutputStream("/dev/null")); |
Hugo Benichi | 5eb9053 | 2017-03-23 18:38:22 +0900 | [diff] [blame] | 423 | new Thread(() -> { |
Hugo Benichi | 2a5cfb9 | 2017-03-22 22:21:44 +0900 | [diff] [blame] | 424 | while (System.currentTimeMillis() < stop) { |
Hugo Benichi | 380a063 | 2017-10-20 09:25:29 +0900 | [diff] [blame^] | 425 | mNetdEventListenerService.list(pw); |
Michal Karpinski | dd9bb4f | 2016-10-12 14:59:26 +0100 | [diff] [blame] | 426 | } |
Hugo Benichi | 5eb9053 | 2017-03-23 18:38:22 +0900 | [diff] [blame] | 427 | }).start(); |
Hugo Benichi | 0d4a398 | 2016-11-25 11:24:22 +0900 | [diff] [blame] | 428 | } |
Hugo Benichi | 2a5cfb9 | 2017-03-22 22:21:44 +0900 | [diff] [blame] | 429 | |
| 430 | // TODO: instead of comparing textpb to textpb, parse textpb and compare proto to proto. |
| 431 | String flushStatistics() throws Exception { |
| 432 | IpConnectivityMetrics metricsService = |
| 433 | new IpConnectivityMetrics(mock(Context.class), (ctx) -> 2000); |
| 434 | metricsService.mNetdListener = mNetdEventListenerService; |
| 435 | |
| 436 | StringWriter buffer = new StringWriter(); |
| 437 | PrintWriter writer = new PrintWriter(buffer); |
| 438 | metricsService.impl.dump(null, writer, new String[]{"flush"}); |
| 439 | byte[] bytes = Base64.decode(buffer.toString(), Base64.DEFAULT); |
Hugo Benichi | 5eb9053 | 2017-03-23 18:38:22 +0900 | [diff] [blame] | 440 | IpConnectivityLog log = IpConnectivityLog.parseFrom(bytes); |
| 441 | for (IpConnectivityEvent ev : log.events) { |
| 442 | if (ev.getConnectStatistics() == null) { |
| 443 | continue; |
| 444 | } |
| 445 | // Sort repeated fields of connect() events arriving in non-deterministic order. |
| 446 | Arrays.sort(ev.getConnectStatistics().latenciesMs); |
| 447 | Arrays.sort(ev.getConnectStatistics().errnosCounters, |
| 448 | Comparator.comparingInt((p) -> p.key)); |
| 449 | } |
| 450 | return log.toString(); |
Hugo Benichi | 2a5cfb9 | 2017-03-22 22:21:44 +0900 | [diff] [blame] | 451 | } |
Hugo Benichi | f562ac3 | 2017-09-04 13:24:43 +0900 | [diff] [blame] | 452 | |
| 453 | String[] listNetdEvent() throws Exception { |
| 454 | StringWriter buffer = new StringWriter(); |
| 455 | PrintWriter writer = new PrintWriter(buffer); |
| 456 | mNetdEventListenerService.list(writer); |
| 457 | return buffer.toString().split("\\n"); |
| 458 | } |
| 459 | |
| 460 | static void assertContains(String got, String want) { |
| 461 | assertTrue(got + " did not contain \"" + want + "\"", got.contains(want)); |
| 462 | } |
Hugo Benichi | 380a063 | 2017-10-20 09:25:29 +0900 | [diff] [blame^] | 463 | |
| 464 | static <T> T[] remove(T[] array, T[] filtered) { |
| 465 | List<T> c = Arrays.asList(filtered); |
| 466 | int next = 0; |
| 467 | for (int i = 0; i < array.length; i++) { |
| 468 | if (c.contains(array[i])) { |
| 469 | continue; |
| 470 | } |
| 471 | array[next++] = array[i]; |
| 472 | } |
| 473 | return Arrays.copyOf(array, next); |
| 474 | } |
Hugo Benichi | 5e05518 | 2016-06-01 08:50:38 +0900 | [diff] [blame] | 475 | } |