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 | |
Hugo Benichi | d921bce | 2017-10-12 21:33:40 +0900 | [diff] [blame] | 64 | private static final byte[] MAC_ADDR = |
| 65 | {(byte)0x84, (byte)0xc9, (byte)0xb2, (byte)0x6a, (byte)0xed, (byte)0x4b}; |
| 66 | |
| 67 | NetdEventListenerService mService; |
Hugo Benichi | 2a5cfb9 | 2017-03-22 22:21:44 +0900 | [diff] [blame] | 68 | ConnectivityManager mCm; |
Hugo Benichi | 5e05518 | 2016-06-01 08:50:38 +0900 | [diff] [blame] | 69 | |
Hugo Benichi | 2a5cfb9 | 2017-03-22 22:21:44 +0900 | [diff] [blame] | 70 | @Before |
Hugo Benichi | 5e05518 | 2016-06-01 08:50:38 +0900 | [diff] [blame] | 71 | public void setUp() { |
Hugo Benichi | 2a5cfb9 | 2017-03-22 22:21:44 +0900 | [diff] [blame] | 72 | NetworkCapabilities ncWifi = new NetworkCapabilities(); |
| 73 | NetworkCapabilities ncCell = new NetworkCapabilities(); |
| 74 | ncWifi.addTransportType(NetworkCapabilities.TRANSPORT_WIFI); |
| 75 | ncCell.addTransportType(NetworkCapabilities.TRANSPORT_CELLULAR); |
Hugo Benichi | 5e05518 | 2016-06-01 08:50:38 +0900 | [diff] [blame] | 76 | |
Hugo Benichi | 5eb9053 | 2017-03-23 18:38:22 +0900 | [diff] [blame] | 77 | mCm = mock(ConnectivityManager.class); |
Hugo Benichi | 2a5cfb9 | 2017-03-22 22:21:44 +0900 | [diff] [blame] | 78 | when(mCm.getNetworkCapabilities(new Network(100))).thenReturn(ncWifi); |
| 79 | when(mCm.getNetworkCapabilities(new Network(101))).thenReturn(ncCell); |
Hugo Benichi | 5e05518 | 2016-06-01 08:50:38 +0900 | [diff] [blame] | 80 | |
Hugo Benichi | d921bce | 2017-10-12 21:33:40 +0900 | [diff] [blame] | 81 | mService = new NetdEventListenerService(mCm); |
Hugo Benichi | 5eb9053 | 2017-03-23 18:38:22 +0900 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | @Test |
Hugo Benichi | f562ac3 | 2017-09-04 13:24:43 +0900 | [diff] [blame] | 85 | public void testWakeupEventLogging() throws Exception { |
| 86 | final int BUFFER_LENGTH = NetdEventListenerService.WAKEUP_EVENT_BUFFER_LENGTH; |
Hugo Benichi | d921bce | 2017-10-12 21:33:40 +0900 | [diff] [blame] | 87 | final long now = System.currentTimeMillis(); |
| 88 | final String iface = "wlan0"; |
| 89 | final byte[] mac = MAC_ADDR; |
| 90 | final String srcIp = "192.168.2.1"; |
| 91 | final String dstIp = "192.168.2.23"; |
| 92 | final String srcIp6 = "2001:db8:4:fd00:a585:13d1:6a23:4fb4"; |
| 93 | final String dstIp6 = "2001:db8:4006:807::200a"; |
| 94 | final int sport = 2356; |
| 95 | final int dport = 13489; |
| 96 | |
| 97 | final int v4 = 0x800; |
| 98 | final int v6 = 0x86dd; |
| 99 | final int tcp = 6; |
| 100 | final int udp = 17; |
| 101 | final int icmp6 = 58; |
Hugo Benichi | f562ac3 | 2017-09-04 13:24:43 +0900 | [diff] [blame] | 102 | |
Hugo Benichi | 380a063 | 2017-10-20 09:25:29 +0900 | [diff] [blame] | 103 | // Baseline without any event |
| 104 | String[] baseline = listNetdEvent(); |
Hugo Benichi | f562ac3 | 2017-09-04 13:24:43 +0900 | [diff] [blame] | 105 | |
Hugo Benichi | d921bce | 2017-10-12 21:33:40 +0900 | [diff] [blame] | 106 | int[] uids = {10001, 10002, 10004, 1000, 10052, 10023, 10002, 10123, 10004}; |
| 107 | wakeupEvent(iface, uids[0], v4, tcp, mac, srcIp, dstIp, sport, dport, now); |
| 108 | wakeupEvent(iface, uids[1], v6, udp, mac, srcIp6, dstIp6, sport, dport, now); |
| 109 | wakeupEvent(iface, uids[2], v6, udp, mac, srcIp6, dstIp6, sport, dport, now); |
| 110 | wakeupEvent(iface, uids[3], v4, icmp6, mac, srcIp, dstIp, sport, dport, now); |
| 111 | wakeupEvent(iface, uids[4], v6, tcp, mac, srcIp6, dstIp6, sport, dport, now); |
| 112 | wakeupEvent(iface, uids[5], v4, tcp, mac, srcIp, dstIp, sport, dport, now); |
| 113 | wakeupEvent(iface, uids[6], v6, udp, mac, srcIp6, dstIp6, sport, dport, now); |
| 114 | wakeupEvent(iface, uids[7], v6, tcp, mac, srcIp6, dstIp6, sport, dport, now); |
| 115 | wakeupEvent(iface, uids[8], v6, udp, mac, srcIp6, dstIp6, sport, dport, now); |
Hugo Benichi | f562ac3 | 2017-09-04 13:24:43 +0900 | [diff] [blame] | 116 | |
Hugo Benichi | 380a063 | 2017-10-20 09:25:29 +0900 | [diff] [blame] | 117 | String[] events2 = remove(listNetdEvent(), baseline); |
Hugo Benichi | 60c9f63 | 2017-09-05 13:34:48 +0900 | [diff] [blame] | 118 | int expectedLength2 = uids.length + 1; // +1 for the WakeupStats line |
| 119 | assertEquals(expectedLength2, events2.length); |
| 120 | assertContains(events2[0], "WakeupStats"); |
| 121 | assertContains(events2[0], "wlan0"); |
Hugo Benichi | d921bce | 2017-10-12 21:33:40 +0900 | [diff] [blame] | 122 | assertContains(events2[0], "0x800"); |
| 123 | assertContains(events2[0], "0x86dd"); |
Hugo Benichi | f562ac3 | 2017-09-04 13:24:43 +0900 | [diff] [blame] | 124 | for (int i = 0; i < uids.length; i++) { |
Hugo Benichi | 60c9f63 | 2017-09-05 13:34:48 +0900 | [diff] [blame] | 125 | String got = events2[i+1]; |
| 126 | assertContains(got, "WakeupEvent"); |
Hugo Benichi | f562ac3 | 2017-09-04 13:24:43 +0900 | [diff] [blame] | 127 | assertContains(got, "wlan0"); |
| 128 | assertContains(got, "uid: " + uids[i]); |
| 129 | } |
| 130 | |
| 131 | int uid = 20000; |
| 132 | for (int i = 0; i < BUFFER_LENGTH * 2; i++) { |
| 133 | long ts = now + 10; |
Hugo Benichi | d921bce | 2017-10-12 21:33:40 +0900 | [diff] [blame] | 134 | wakeupEvent(iface, uid, 0x800, 6, mac, srcIp, dstIp, 23, 24, ts); |
Hugo Benichi | f562ac3 | 2017-09-04 13:24:43 +0900 | [diff] [blame] | 135 | } |
| 136 | |
Hugo Benichi | 380a063 | 2017-10-20 09:25:29 +0900 | [diff] [blame] | 137 | String[] events3 = remove(listNetdEvent(), baseline); |
Hugo Benichi | 60c9f63 | 2017-09-05 13:34:48 +0900 | [diff] [blame] | 138 | int expectedLength3 = BUFFER_LENGTH + 1; // +1 for the WakeupStats line |
| 139 | assertEquals(expectedLength3, events3.length); |
| 140 | assertContains(events2[0], "WakeupStats"); |
| 141 | assertContains(events2[0], "wlan0"); |
| 142 | for (int i = 1; i < expectedLength3; i++) { |
| 143 | String got = events3[i]; |
| 144 | assertContains(got, "WakeupEvent"); |
Hugo Benichi | f562ac3 | 2017-09-04 13:24:43 +0900 | [diff] [blame] | 145 | assertContains(got, "wlan0"); |
| 146 | assertContains(got, "uid: " + uid); |
| 147 | } |
| 148 | |
| 149 | uid = 45678; |
Hugo Benichi | d921bce | 2017-10-12 21:33:40 +0900 | [diff] [blame] | 150 | wakeupEvent(iface, uid, 0x800, 6, mac, srcIp, dstIp, 23, 24, now); |
Hugo Benichi | f562ac3 | 2017-09-04 13:24:43 +0900 | [diff] [blame] | 151 | |
Hugo Benichi | 380a063 | 2017-10-20 09:25:29 +0900 | [diff] [blame] | 152 | String[] events4 = remove(listNetdEvent(), baseline); |
Hugo Benichi | f562ac3 | 2017-09-04 13:24:43 +0900 | [diff] [blame] | 153 | String lastEvent = events4[events4.length - 1]; |
Hugo Benichi | 60c9f63 | 2017-09-05 13:34:48 +0900 | [diff] [blame] | 154 | assertContains(lastEvent, "WakeupEvent"); |
Hugo Benichi | f562ac3 | 2017-09-04 13:24:43 +0900 | [diff] [blame] | 155 | assertContains(lastEvent, "wlan0"); |
| 156 | assertContains(lastEvent, "uid: " + uid); |
| 157 | } |
| 158 | |
| 159 | @Test |
Hugo Benichi | 60c9f63 | 2017-09-05 13:34:48 +0900 | [diff] [blame] | 160 | public void testWakeupStatsLogging() throws Exception { |
Hugo Benichi | d921bce | 2017-10-12 21:33:40 +0900 | [diff] [blame] | 161 | final byte[] mac = MAC_ADDR; |
| 162 | final String srcIp = "192.168.2.1"; |
| 163 | final String dstIp = "192.168.2.23"; |
| 164 | final String srcIp6 = "2401:fa00:4:fd00:a585:13d1:6a23:4fb4"; |
| 165 | final String dstIp6 = "2404:6800:4006:807::200a"; |
| 166 | final int sport = 2356; |
| 167 | final int dport = 13489; |
| 168 | final long now = 1001L; |
| 169 | |
| 170 | final int v4 = 0x800; |
| 171 | final int v6 = 0x86dd; |
| 172 | final int tcp = 6; |
| 173 | final int udp = 17; |
| 174 | final int icmp6 = 58; |
| 175 | |
| 176 | wakeupEvent("wlan0", 1000, v4, tcp, mac, srcIp, dstIp, sport, dport, now); |
| 177 | wakeupEvent("rmnet0", 10123, v4, tcp, mac, srcIp, dstIp, sport, dport, now); |
| 178 | wakeupEvent("wlan0", 1000, v4, udp, mac, srcIp, dstIp, sport, dport, now); |
| 179 | wakeupEvent("rmnet0", 10008, v4, tcp, mac, srcIp, dstIp, sport, dport, now); |
| 180 | wakeupEvent("wlan0", -1, v6, icmp6, mac, srcIp6, dstIp6, sport, dport, now); |
| 181 | wakeupEvent("wlan0", 10008, v4, tcp, mac, srcIp, dstIp, sport, dport, now); |
| 182 | wakeupEvent("rmnet0", 1000, v4, tcp, mac, srcIp, dstIp, sport, dport, now); |
| 183 | wakeupEvent("wlan0", 10004, v4, udp, mac, srcIp, dstIp, sport, dport, now); |
| 184 | wakeupEvent("wlan0", 1000, v6, tcp, mac, srcIp6, dstIp6, sport, dport, now); |
| 185 | wakeupEvent("wlan0", 0, v6, udp, mac, srcIp6, dstIp6, sport, dport, now); |
| 186 | wakeupEvent("wlan0", -1, v6, icmp6, mac, srcIp6, dstIp6, sport, dport, now); |
| 187 | wakeupEvent("rmnet0", 10052, v4, tcp, mac, srcIp, dstIp, sport, dport, now); |
| 188 | wakeupEvent("wlan0", 0, v6, udp, mac, srcIp6, dstIp6, sport, dport, now); |
| 189 | wakeupEvent("rmnet0", 1000, v6, tcp, mac, srcIp6, dstIp6, sport, dport, now); |
| 190 | wakeupEvent("wlan0", 1010, v4, udp, mac, srcIp, dstIp, sport, dport, now); |
Hugo Benichi | 60c9f63 | 2017-09-05 13:34:48 +0900 | [diff] [blame] | 191 | |
| 192 | String got = flushStatistics(); |
| 193 | String want = String.join("\n", |
| 194 | "dropped_events: 0", |
| 195 | "events <", |
| 196 | " if_name: \"\"", |
| 197 | " link_layer: 2", |
| 198 | " network_id: 0", |
| 199 | " time_ms: 0", |
| 200 | " transports: 0", |
| 201 | " wakeup_stats <", |
| 202 | " application_wakeups: 3", |
| 203 | " duration_sec: 0", |
Hugo Benichi | d921bce | 2017-10-12 21:33:40 +0900 | [diff] [blame] | 204 | " ethertype_counts <", |
| 205 | " key: 2048", |
| 206 | " value: 4", |
| 207 | " >", |
| 208 | " ethertype_counts <", |
| 209 | " key: 34525", |
| 210 | " value: 1", |
| 211 | " >", |
| 212 | " ip_next_header_counts <", |
| 213 | " key: 6", |
| 214 | " value: 5", |
| 215 | " >", |
| 216 | " l2_broadcast_count: 0", |
| 217 | " l2_multicast_count: 0", |
| 218 | " l2_unicast_count: 5", |
Hugo Benichi | 175b574 | 2017-09-19 13:15:26 +0900 | [diff] [blame] | 219 | " no_uid_wakeups: 0", |
Hugo Benichi | 60c9f63 | 2017-09-05 13:34:48 +0900 | [diff] [blame] | 220 | " non_application_wakeups: 0", |
| 221 | " root_wakeups: 0", |
| 222 | " system_wakeups: 2", |
| 223 | " total_wakeups: 5", |
Hugo Benichi | 60c9f63 | 2017-09-05 13:34:48 +0900 | [diff] [blame] | 224 | " >", |
| 225 | ">", |
| 226 | "events <", |
| 227 | " if_name: \"\"", |
| 228 | " link_layer: 4", |
| 229 | " network_id: 0", |
| 230 | " time_ms: 0", |
| 231 | " transports: 0", |
| 232 | " wakeup_stats <", |
| 233 | " application_wakeups: 2", |
| 234 | " duration_sec: 0", |
Hugo Benichi | d921bce | 2017-10-12 21:33:40 +0900 | [diff] [blame] | 235 | " ethertype_counts <", |
| 236 | " key: 2048", |
| 237 | " value: 5", |
| 238 | " >", |
| 239 | " ethertype_counts <", |
| 240 | " key: 34525", |
| 241 | " value: 5", |
| 242 | " >", |
| 243 | " ip_next_header_counts <", |
| 244 | " key: 6", |
| 245 | " value: 3", |
| 246 | " >", |
| 247 | " ip_next_header_counts <", |
| 248 | " key: 17", |
| 249 | " value: 5", |
| 250 | " >", |
| 251 | " ip_next_header_counts <", |
| 252 | " key: 58", |
| 253 | " value: 2", |
| 254 | " >", |
| 255 | " l2_broadcast_count: 0", |
| 256 | " l2_multicast_count: 0", |
| 257 | " l2_unicast_count: 10", |
Hugo Benichi | 175b574 | 2017-09-19 13:15:26 +0900 | [diff] [blame] | 258 | " no_uid_wakeups: 2", |
Hugo Benichi | 60c9f63 | 2017-09-05 13:34:48 +0900 | [diff] [blame] | 259 | " non_application_wakeups: 1", |
| 260 | " root_wakeups: 2", |
| 261 | " system_wakeups: 3", |
| 262 | " total_wakeups: 10", |
Hugo Benichi | 60c9f63 | 2017-09-05 13:34:48 +0900 | [diff] [blame] | 263 | " >", |
| 264 | ">", |
| 265 | "version: 2\n"); |
| 266 | assertEquals(want, got); |
| 267 | } |
| 268 | |
| 269 | @Test |
Hugo Benichi | 5eb9053 | 2017-03-23 18:38:22 +0900 | [diff] [blame] | 270 | public void testDnsLogging() throws Exception { |
| 271 | asyncDump(100); |
| 272 | |
Hugo Benichi | 2a5cfb9 | 2017-03-22 22:21:44 +0900 | [diff] [blame] | 273 | dnsEvent(100, EVENT_GETADDRINFO, 0, 3456); |
| 274 | dnsEvent(100, EVENT_GETADDRINFO, 0, 267); |
| 275 | dnsEvent(100, EVENT_GETHOSTBYNAME, 22, 1230); |
| 276 | dnsEvent(100, EVENT_GETADDRINFO, 3, 45); |
| 277 | dnsEvent(100, EVENT_GETADDRINFO, 1, 2111); |
| 278 | dnsEvent(100, EVENT_GETADDRINFO, 0, 450); |
| 279 | dnsEvent(100, EVENT_GETHOSTBYNAME, 200, 638); |
| 280 | dnsEvent(100, EVENT_GETHOSTBYNAME, 178, 1300); |
| 281 | dnsEvent(101, EVENT_GETADDRINFO, 0, 56); |
| 282 | dnsEvent(101, EVENT_GETADDRINFO, 0, 78); |
| 283 | dnsEvent(101, EVENT_GETADDRINFO, 0, 14); |
| 284 | dnsEvent(101, EVENT_GETHOSTBYNAME, 0, 56); |
| 285 | dnsEvent(101, EVENT_GETADDRINFO, 0, 78); |
| 286 | dnsEvent(101, EVENT_GETADDRINFO, 0, 14); |
Hugo Benichi | 5e05518 | 2016-06-01 08:50:38 +0900 | [diff] [blame] | 287 | |
Hugo Benichi | 2a5cfb9 | 2017-03-22 22:21:44 +0900 | [diff] [blame] | 288 | String got = flushStatistics(); |
| 289 | String want = String.join("\n", |
| 290 | "dropped_events: 0", |
| 291 | "events <", |
| 292 | " if_name: \"\"", |
Hugo Benichi | 2a5cfb9 | 2017-03-22 22:21:44 +0900 | [diff] [blame] | 293 | " link_layer: 4", |
| 294 | " network_id: 100", |
| 295 | " time_ms: 0", |
| 296 | " transports: 2", |
| 297 | " dns_lookup_batch <", |
| 298 | " event_types: 1", |
| 299 | " event_types: 1", |
| 300 | " event_types: 2", |
| 301 | " event_types: 1", |
| 302 | " event_types: 1", |
| 303 | " event_types: 1", |
| 304 | " event_types: 2", |
| 305 | " event_types: 2", |
Hugo Benichi | 4eccf78 | 2017-07-18 14:28:27 +0900 | [diff] [blame] | 306 | " getaddrinfo_error_count: 0", |
| 307 | " getaddrinfo_query_count: 0", |
| 308 | " gethostbyname_error_count: 0", |
| 309 | " gethostbyname_query_count: 0", |
Hugo Benichi | 2a5cfb9 | 2017-03-22 22:21:44 +0900 | [diff] [blame] | 310 | " latencies_ms: 3456", |
| 311 | " latencies_ms: 267", |
| 312 | " latencies_ms: 1230", |
| 313 | " latencies_ms: 45", |
| 314 | " latencies_ms: 2111", |
| 315 | " latencies_ms: 450", |
| 316 | " latencies_ms: 638", |
| 317 | " latencies_ms: 1300", |
| 318 | " return_codes: 0", |
| 319 | " return_codes: 0", |
| 320 | " return_codes: 22", |
| 321 | " return_codes: 3", |
| 322 | " return_codes: 1", |
| 323 | " return_codes: 0", |
| 324 | " return_codes: 200", |
| 325 | " return_codes: 178", |
| 326 | " >", |
| 327 | ">", |
| 328 | "events <", |
| 329 | " if_name: \"\"", |
| 330 | " link_layer: 2", |
| 331 | " network_id: 101", |
| 332 | " time_ms: 0", |
| 333 | " transports: 1", |
| 334 | " dns_lookup_batch <", |
| 335 | " event_types: 1", |
| 336 | " event_types: 1", |
| 337 | " event_types: 1", |
| 338 | " event_types: 2", |
| 339 | " event_types: 1", |
| 340 | " event_types: 1", |
Hugo Benichi | 4eccf78 | 2017-07-18 14:28:27 +0900 | [diff] [blame] | 341 | " getaddrinfo_error_count: 0", |
| 342 | " getaddrinfo_query_count: 0", |
| 343 | " gethostbyname_error_count: 0", |
| 344 | " gethostbyname_query_count: 0", |
Hugo Benichi | 2a5cfb9 | 2017-03-22 22:21:44 +0900 | [diff] [blame] | 345 | " latencies_ms: 56", |
| 346 | " latencies_ms: 78", |
| 347 | " latencies_ms: 14", |
| 348 | " latencies_ms: 56", |
| 349 | " latencies_ms: 78", |
| 350 | " latencies_ms: 14", |
| 351 | " return_codes: 0", |
| 352 | " return_codes: 0", |
| 353 | " return_codes: 0", |
| 354 | " return_codes: 0", |
| 355 | " return_codes: 0", |
| 356 | " return_codes: 0", |
| 357 | " >", |
| 358 | ">", |
| 359 | "version: 2\n"); |
| 360 | assertEquals(want, got); |
Hugo Benichi | 5e05518 | 2016-06-01 08:50:38 +0900 | [diff] [blame] | 361 | } |
| 362 | |
Hugo Benichi | 2a5cfb9 | 2017-03-22 22:21:44 +0900 | [diff] [blame] | 363 | @Test |
Hugo Benichi | 0d4a398 | 2016-11-25 11:24:22 +0900 | [diff] [blame] | 364 | public void testConnectLogging() throws Exception { |
Hugo Benichi | 5eb9053 | 2017-03-23 18:38:22 +0900 | [diff] [blame] | 365 | asyncDump(100); |
| 366 | |
Hugo Benichi | 0d4a398 | 2016-11-25 11:24:22 +0900 | [diff] [blame] | 367 | final int OK = 0; |
| 368 | Thread[] logActions = { |
| 369 | // ignored |
Hugo Benichi | 5eb9053 | 2017-03-23 18:38:22 +0900 | [diff] [blame] | 370 | connectEventAction(100, OsConstants.EALREADY, 0, EXAMPLE_IPV4), |
| 371 | connectEventAction(100, OsConstants.EALREADY, 0, EXAMPLE_IPV6), |
| 372 | connectEventAction(100, OsConstants.EINPROGRESS, 0, EXAMPLE_IPV4), |
| 373 | connectEventAction(101, OsConstants.EINPROGRESS, 0, EXAMPLE_IPV6), |
| 374 | connectEventAction(101, OsConstants.EINPROGRESS, 0, EXAMPLE_IPV6), |
Hugo Benichi | 0d4a398 | 2016-11-25 11:24:22 +0900 | [diff] [blame] | 375 | // valid latencies |
Hugo Benichi | 5eb9053 | 2017-03-23 18:38:22 +0900 | [diff] [blame] | 376 | connectEventAction(100, OK, 110, EXAMPLE_IPV4), |
| 377 | connectEventAction(100, OK, 23, EXAMPLE_IPV4), |
| 378 | connectEventAction(100, OK, 45, EXAMPLE_IPV4), |
| 379 | connectEventAction(101, OK, 56, EXAMPLE_IPV4), |
| 380 | connectEventAction(101, OK, 523, EXAMPLE_IPV6), |
| 381 | connectEventAction(101, OK, 214, EXAMPLE_IPV6), |
| 382 | connectEventAction(101, OK, 67, EXAMPLE_IPV6), |
Hugo Benichi | 0d4a398 | 2016-11-25 11:24:22 +0900 | [diff] [blame] | 383 | // errors |
Hugo Benichi | 5eb9053 | 2017-03-23 18:38:22 +0900 | [diff] [blame] | 384 | connectEventAction(100, OsConstants.EPERM, 0, EXAMPLE_IPV4), |
| 385 | connectEventAction(101, OsConstants.EPERM, 0, EXAMPLE_IPV4), |
| 386 | connectEventAction(100, OsConstants.EAGAIN, 0, EXAMPLE_IPV4), |
| 387 | connectEventAction(100, OsConstants.EACCES, 0, EXAMPLE_IPV4), |
| 388 | connectEventAction(101, OsConstants.EACCES, 0, EXAMPLE_IPV4), |
| 389 | connectEventAction(101, OsConstants.EACCES, 0, EXAMPLE_IPV6), |
| 390 | connectEventAction(100, OsConstants.EADDRINUSE, 0, EXAMPLE_IPV4), |
| 391 | connectEventAction(101, OsConstants.ETIMEDOUT, 0, EXAMPLE_IPV4), |
| 392 | connectEventAction(100, OsConstants.ETIMEDOUT, 0, EXAMPLE_IPV6), |
| 393 | connectEventAction(100, OsConstants.ETIMEDOUT, 0, EXAMPLE_IPV6), |
| 394 | connectEventAction(101, OsConstants.ECONNREFUSED, 0, EXAMPLE_IPV4), |
Hugo Benichi | 0d4a398 | 2016-11-25 11:24:22 +0900 | [diff] [blame] | 395 | }; |
| 396 | |
| 397 | for (Thread t : logActions) { |
| 398 | t.start(); |
| 399 | } |
| 400 | for (Thread t : logActions) { |
| 401 | t.join(); |
| 402 | } |
| 403 | |
Hugo Benichi | 5eb9053 | 2017-03-23 18:38:22 +0900 | [diff] [blame] | 404 | String got = flushStatistics(); |
Hugo Benichi | 0d4a398 | 2016-11-25 11:24:22 +0900 | [diff] [blame] | 405 | String want = String.join("\n", |
Hugo Benichi | 5eb9053 | 2017-03-23 18:38:22 +0900 | [diff] [blame] | 406 | "dropped_events: 0", |
| 407 | "events <", |
| 408 | " if_name: \"\"", |
| 409 | " link_layer: 4", |
| 410 | " network_id: 100", |
| 411 | " time_ms: 0", |
| 412 | " transports: 2", |
| 413 | " connect_statistics <", |
| 414 | " connect_blocking_count: 3", |
| 415 | " connect_count: 6", |
| 416 | " errnos_counters <", |
| 417 | " key: 1", |
| 418 | " value: 1", |
| 419 | " >", |
| 420 | " errnos_counters <", |
| 421 | " key: 11", |
| 422 | " value: 1", |
| 423 | " >", |
| 424 | " errnos_counters <", |
| 425 | " key: 13", |
| 426 | " value: 1", |
| 427 | " >", |
| 428 | " errnos_counters <", |
| 429 | " key: 98", |
| 430 | " value: 1", |
| 431 | " >", |
| 432 | " errnos_counters <", |
| 433 | " key: 110", |
| 434 | " value: 2", |
| 435 | " >", |
| 436 | " ipv6_addr_count: 1", |
| 437 | " latencies_ms: 23", |
| 438 | " latencies_ms: 45", |
| 439 | " latencies_ms: 110", |
Hugo Benichi | 0d4a398 | 2016-11-25 11:24:22 +0900 | [diff] [blame] | 440 | " >", |
Hugo Benichi | 5eb9053 | 2017-03-23 18:38:22 +0900 | [diff] [blame] | 441 | ">", |
| 442 | "events <", |
| 443 | " if_name: \"\"", |
| 444 | " link_layer: 2", |
| 445 | " network_id: 101", |
| 446 | " time_ms: 0", |
| 447 | " transports: 1", |
| 448 | " connect_statistics <", |
| 449 | " connect_blocking_count: 4", |
| 450 | " connect_count: 6", |
| 451 | " errnos_counters <", |
| 452 | " key: 1", |
| 453 | " value: 1", |
| 454 | " >", |
| 455 | " errnos_counters <", |
| 456 | " key: 13", |
| 457 | " value: 2", |
| 458 | " >", |
| 459 | " errnos_counters <", |
| 460 | " key: 110", |
| 461 | " value: 1", |
| 462 | " >", |
| 463 | " errnos_counters <", |
| 464 | " key: 111", |
| 465 | " value: 1", |
| 466 | " >", |
| 467 | " ipv6_addr_count: 5", |
| 468 | " latencies_ms: 56", |
| 469 | " latencies_ms: 67", |
| 470 | " latencies_ms: 214", |
| 471 | " latencies_ms: 523", |
Hugo Benichi | 0d4a398 | 2016-11-25 11:24:22 +0900 | [diff] [blame] | 472 | " >", |
Hugo Benichi | 5eb9053 | 2017-03-23 18:38:22 +0900 | [diff] [blame] | 473 | ">", |
| 474 | "version: 2\n"); |
| 475 | assertEquals(want, got); |
Hugo Benichi | 0d4a398 | 2016-11-25 11:24:22 +0900 | [diff] [blame] | 476 | } |
| 477 | |
Hugo Benichi | 5eb9053 | 2017-03-23 18:38:22 +0900 | [diff] [blame] | 478 | Thread connectEventAction(int netId, int error, int latencyMs, String ipAddr) { |
Hugo Benichi | 0d4a398 | 2016-11-25 11:24:22 +0900 | [diff] [blame] | 479 | return new Thread(() -> { |
| 480 | try { |
Hugo Benichi | d921bce | 2017-10-12 21:33:40 +0900 | [diff] [blame] | 481 | mService.onConnectEvent(netId, error, latencyMs, ipAddr, 80, 1); |
Hugo Benichi | 0d4a398 | 2016-11-25 11:24:22 +0900 | [diff] [blame] | 482 | } catch (Exception e) { |
| 483 | fail(e.toString()); |
| 484 | } |
| 485 | }); |
| 486 | } |
| 487 | |
Hugo Benichi | 2a5cfb9 | 2017-03-22 22:21:44 +0900 | [diff] [blame] | 488 | void dnsEvent(int netId, int type, int result, int latency) throws Exception { |
Hugo Benichi | d921bce | 2017-10-12 21:33:40 +0900 | [diff] [blame] | 489 | mService.onDnsEvent(netId, type, result, latency, "", null, 0, 0); |
Hugo Benichi | 2a5cfb9 | 2017-03-22 22:21:44 +0900 | [diff] [blame] | 490 | } |
| 491 | |
Hugo Benichi | d921bce | 2017-10-12 21:33:40 +0900 | [diff] [blame] | 492 | void wakeupEvent(String iface, int uid, int ether, int ip, byte[] mac, String srcIp, |
| 493 | String dstIp, int sport, int dport, long now) throws Exception { |
Hugo Benichi | 60c9f63 | 2017-09-05 13:34:48 +0900 | [diff] [blame] | 494 | String prefix = NetdEventListenerService.WAKEUP_EVENT_IFACE_PREFIX + iface; |
Hugo Benichi | d921bce | 2017-10-12 21:33:40 +0900 | [diff] [blame] | 495 | mService.onWakeupEvent(prefix, uid, ether, ip, mac, srcIp, dstIp, sport, dport, now); |
Hugo Benichi | 60c9f63 | 2017-09-05 13:34:48 +0900 | [diff] [blame] | 496 | } |
| 497 | |
Hugo Benichi | 5eb9053 | 2017-03-23 18:38:22 +0900 | [diff] [blame] | 498 | void asyncDump(long durationMs) throws Exception { |
Hugo Benichi | 2a5cfb9 | 2017-03-22 22:21:44 +0900 | [diff] [blame] | 499 | final long stop = System.currentTimeMillis() + durationMs; |
| 500 | final PrintWriter pw = new PrintWriter(new FileOutputStream("/dev/null")); |
Hugo Benichi | 5eb9053 | 2017-03-23 18:38:22 +0900 | [diff] [blame] | 501 | new Thread(() -> { |
Hugo Benichi | 2a5cfb9 | 2017-03-22 22:21:44 +0900 | [diff] [blame] | 502 | while (System.currentTimeMillis() < stop) { |
Hugo Benichi | d921bce | 2017-10-12 21:33:40 +0900 | [diff] [blame] | 503 | mService.list(pw); |
Michal Karpinski | dd9bb4f | 2016-10-12 14:59:26 +0100 | [diff] [blame] | 504 | } |
Hugo Benichi | 5eb9053 | 2017-03-23 18:38:22 +0900 | [diff] [blame] | 505 | }).start(); |
Hugo Benichi | 0d4a398 | 2016-11-25 11:24:22 +0900 | [diff] [blame] | 506 | } |
Hugo Benichi | 2a5cfb9 | 2017-03-22 22:21:44 +0900 | [diff] [blame] | 507 | |
| 508 | // TODO: instead of comparing textpb to textpb, parse textpb and compare proto to proto. |
| 509 | String flushStatistics() throws Exception { |
| 510 | IpConnectivityMetrics metricsService = |
| 511 | new IpConnectivityMetrics(mock(Context.class), (ctx) -> 2000); |
Hugo Benichi | d921bce | 2017-10-12 21:33:40 +0900 | [diff] [blame] | 512 | metricsService.mNetdListener = mService; |
Hugo Benichi | 2a5cfb9 | 2017-03-22 22:21:44 +0900 | [diff] [blame] | 513 | |
| 514 | StringWriter buffer = new StringWriter(); |
| 515 | PrintWriter writer = new PrintWriter(buffer); |
| 516 | metricsService.impl.dump(null, writer, new String[]{"flush"}); |
| 517 | byte[] bytes = Base64.decode(buffer.toString(), Base64.DEFAULT); |
Hugo Benichi | 5eb9053 | 2017-03-23 18:38:22 +0900 | [diff] [blame] | 518 | IpConnectivityLog log = IpConnectivityLog.parseFrom(bytes); |
| 519 | for (IpConnectivityEvent ev : log.events) { |
| 520 | if (ev.getConnectStatistics() == null) { |
| 521 | continue; |
| 522 | } |
| 523 | // Sort repeated fields of connect() events arriving in non-deterministic order. |
| 524 | Arrays.sort(ev.getConnectStatistics().latenciesMs); |
| 525 | Arrays.sort(ev.getConnectStatistics().errnosCounters, |
| 526 | Comparator.comparingInt((p) -> p.key)); |
| 527 | } |
| 528 | return log.toString(); |
Hugo Benichi | 2a5cfb9 | 2017-03-22 22:21:44 +0900 | [diff] [blame] | 529 | } |
Hugo Benichi | f562ac3 | 2017-09-04 13:24:43 +0900 | [diff] [blame] | 530 | |
| 531 | String[] listNetdEvent() throws Exception { |
| 532 | StringWriter buffer = new StringWriter(); |
| 533 | PrintWriter writer = new PrintWriter(buffer); |
Hugo Benichi | d921bce | 2017-10-12 21:33:40 +0900 | [diff] [blame] | 534 | mService.list(writer); |
Hugo Benichi | f562ac3 | 2017-09-04 13:24:43 +0900 | [diff] [blame] | 535 | return buffer.toString().split("\\n"); |
| 536 | } |
| 537 | |
| 538 | static void assertContains(String got, String want) { |
| 539 | assertTrue(got + " did not contain \"" + want + "\"", got.contains(want)); |
| 540 | } |
Hugo Benichi | 380a063 | 2017-10-20 09:25:29 +0900 | [diff] [blame] | 541 | |
| 542 | static <T> T[] remove(T[] array, T[] filtered) { |
| 543 | List<T> c = Arrays.asList(filtered); |
| 544 | int next = 0; |
| 545 | for (int i = 0; i < array.length; i++) { |
| 546 | if (c.contains(array[i])) { |
| 547 | continue; |
| 548 | } |
| 549 | array[next++] = array[i]; |
| 550 | } |
| 551 | return Arrays.copyOf(array, next); |
| 552 | } |
Hugo Benichi | 5e05518 | 2016-06-01 08:50:38 +0900 | [diff] [blame] | 553 | } |