blob: 9e2fd6231ba9d12481efeabb891038a1e6c0e07e [file] [log] [blame]
Hugo Benichi5e055182016-06-01 08:50:38 +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
19import android.net.ConnectivityManager.NetworkCallback;
20import android.net.ConnectivityManager;
21import android.net.Network;
22import android.net.metrics.DnsEvent;
Michal Karpinskia2d58602016-09-21 18:45:59 +090023import android.net.metrics.INetdEventListener;
Hugo Benichi5e055182016-06-01 08:50:38 +090024import android.net.metrics.IpConnectivityLog;
25
26import junit.framework.TestCase;
27import org.junit.Before;
28import org.junit.Test;
29import static org.junit.Assert.assertArrayEquals;
30import static org.junit.Assert.assertTrue;
31
32import org.mockito.ArgumentCaptor;
33import org.mockito.Mock;
34import org.mockito.Mockito;
35import org.mockito.MockitoAnnotations;
36import static org.mockito.Mockito.any;
37import static org.mockito.Mockito.anyInt;
38import static org.mockito.Mockito.eq;
39import static org.mockito.Mockito.timeout;
40import static org.mockito.Mockito.times;
41import static org.mockito.Mockito.verify;
42
43import java.io.FileOutputStream;
44import java.io.PrintWriter;
45import java.util.Arrays;
46import java.util.List;
47import java.util.OptionalInt;
48import java.util.stream.IntStream;
49
Michal Karpinskia2d58602016-09-21 18:45:59 +090050public class NetdEventListenerServiceTest extends TestCase {
Hugo Benichi5e055182016-06-01 08:50:38 +090051
Michal Karpinskia2d58602016-09-21 18:45:59 +090052 // TODO: read from NetdEventListenerService after this constant is read from system property
Hugo Benichi5e055182016-06-01 08:50:38 +090053 static final int BATCH_SIZE = 100;
Michal Karpinskia2d58602016-09-21 18:45:59 +090054 static final int EVENT_TYPE = INetdEventListener.EVENT_GETADDRINFO;
55 // TODO: read from INetdEventListener
Hugo Benichi5e055182016-06-01 08:50:38 +090056 static final int RETURN_CODE = 1;
57
58 static final byte[] EVENT_TYPES = new byte[BATCH_SIZE];
59 static final byte[] RETURN_CODES = new byte[BATCH_SIZE];
60 static final int[] LATENCIES = new int[BATCH_SIZE];
61 static {
62 for (int i = 0; i < BATCH_SIZE; i++) {
63 EVENT_TYPES[i] = EVENT_TYPE;
64 RETURN_CODES[i] = RETURN_CODE;
65 LATENCIES[i] = i;
66 }
67 }
68
Michal Karpinski4f261032016-09-26 09:20:25 +010069 NetdEventListenerService mNetdEventListenerService;
Hugo Benichi5e055182016-06-01 08:50:38 +090070
71 @Mock ConnectivityManager mCm;
72 @Mock IpConnectivityLog mLog;
73 ArgumentCaptor<NetworkCallback> mCallbackCaptor;
74 ArgumentCaptor<DnsEvent> mEvCaptor;
75
76 public void setUp() {
77 MockitoAnnotations.initMocks(this);
78 mCallbackCaptor = ArgumentCaptor.forClass(NetworkCallback.class);
79 mEvCaptor = ArgumentCaptor.forClass(DnsEvent.class);
Michal Karpinski4f261032016-09-26 09:20:25 +010080 mNetdEventListenerService = new NetdEventListenerService(mCm, mLog);
Hugo Benichi5e055182016-06-01 08:50:38 +090081
82 verify(mCm, times(1)).registerNetworkCallback(any(), mCallbackCaptor.capture());
83 }
84
85 public void testOneBatch() throws Exception {
86 log(105, LATENCIES);
87 log(106, Arrays.copyOf(LATENCIES, BATCH_SIZE - 1)); // one lookup short of a batch event
88
89 verifyLoggedEvents(new DnsEvent(105, EVENT_TYPES, RETURN_CODES, LATENCIES));
90
91 log(106, Arrays.copyOfRange(LATENCIES, BATCH_SIZE - 1, BATCH_SIZE));
92
93 mEvCaptor = ArgumentCaptor.forClass(DnsEvent.class); // reset argument captor
94 verifyLoggedEvents(
95 new DnsEvent(105, EVENT_TYPES, RETURN_CODES, LATENCIES),
96 new DnsEvent(106, EVENT_TYPES, RETURN_CODES, LATENCIES));
97 }
98
99 public void testSeveralBatches() throws Exception {
100 log(105, LATENCIES);
101 log(106, LATENCIES);
102 log(105, LATENCIES);
103 log(107, LATENCIES);
104
105 verifyLoggedEvents(
106 new DnsEvent(105, EVENT_TYPES, RETURN_CODES, LATENCIES),
107 new DnsEvent(106, EVENT_TYPES, RETURN_CODES, LATENCIES),
108 new DnsEvent(105, EVENT_TYPES, RETURN_CODES, LATENCIES),
109 new DnsEvent(107, EVENT_TYPES, RETURN_CODES, LATENCIES));
110 }
111
112 public void testBatchAndNetworkLost() throws Exception {
113 byte[] eventTypes = Arrays.copyOf(EVENT_TYPES, 20);
114 byte[] returnCodes = Arrays.copyOf(RETURN_CODES, 20);
115 int[] latencies = Arrays.copyOf(LATENCIES, 20);
116
117 log(105, LATENCIES);
118 log(105, latencies);
119 mCallbackCaptor.getValue().onLost(new Network(105));
120 log(105, LATENCIES);
121
122 verifyLoggedEvents(
123 new DnsEvent(105, eventTypes, returnCodes, latencies),
124 new DnsEvent(105, EVENT_TYPES, RETURN_CODES, LATENCIES),
125 new DnsEvent(105, EVENT_TYPES, RETURN_CODES, LATENCIES));
126 }
127
128 public void testConcurrentBatchesAndDumps() throws Exception {
129 final long stop = System.currentTimeMillis() + 100;
130 final PrintWriter pw = new PrintWriter(new FileOutputStream("/dev/null"));
131 new Thread() {
132 public void run() {
133 while (System.currentTimeMillis() < stop) {
Michal Karpinski4f261032016-09-26 09:20:25 +0100134 mNetdEventListenerService.dump(pw);
Hugo Benichi5e055182016-06-01 08:50:38 +0900135 }
136 }
137 }.start();
138
139 logAsync(105, LATENCIES);
140 logAsync(106, LATENCIES);
141 logAsync(107, LATENCIES);
142
143 verifyLoggedEvents(500,
144 new DnsEvent(105, EVENT_TYPES, RETURN_CODES, LATENCIES),
145 new DnsEvent(106, EVENT_TYPES, RETURN_CODES, LATENCIES),
146 new DnsEvent(107, EVENT_TYPES, RETURN_CODES, LATENCIES));
147 }
148
149 public void testConcurrentBatchesAndNetworkLoss() throws Exception {
150 logAsync(105, LATENCIES);
151 Thread.sleep(10L);
152 // call onLost() asynchronously to logAsync's onDnsEvent() calls.
153 mCallbackCaptor.getValue().onLost(new Network(105));
154
155 // do not verify unpredictable batch
156 verify(mLog, timeout(500).times(1)).log(any());
157 }
158
159 void log(int netId, int[] latencies) {
160 for (int l : latencies) {
Michal Karpinski14c9d2d2016-09-27 17:13:57 +0100161 mNetdEventListenerService.onDnsEvent(netId, EVENT_TYPE, RETURN_CODE, l, null, null, 0,
162 0);
Hugo Benichi5e055182016-06-01 08:50:38 +0900163 }
164 }
165
166 void logAsync(int netId, int[] latencies) {
167 new Thread() {
168 public void run() {
169 log(netId, latencies);
170 }
171 }.start();
172 }
173
174 void verifyLoggedEvents(DnsEvent... expected) {
175 verifyLoggedEvents(0, expected);
176 }
177
178 void verifyLoggedEvents(int wait, DnsEvent... expectedEvents) {
179 verify(mLog, timeout(wait).times(expectedEvents.length)).log(mEvCaptor.capture());
180 for (DnsEvent got : mEvCaptor.getAllValues()) {
181 OptionalInt index = IntStream.range(0, expectedEvents.length)
182 .filter(i -> eventsEqual(expectedEvents[i], got))
183 .findFirst();
184 // Don't match same expected event more than once.
185 index.ifPresent(i -> expectedEvents[i] = null);
186 assertTrue(index.isPresent());
187 }
188 }
189
190 /** equality function for DnsEvent to avoid overriding equals() and hashCode(). */
191 static boolean eventsEqual(DnsEvent expected, DnsEvent got) {
192 return (expected == got) || ((expected != null) && (got != null)
193 && (expected.netId == got.netId)
194 && Arrays.equals(expected.eventTypes, got.eventTypes)
195 && Arrays.equals(expected.returnCodes, got.returnCodes)
196 && Arrays.equals(expected.latenciesMs, got.latenciesMs));
197 }
198}