blob: be54b1a93b5dc3bca174a99e9504164519cb36a8 [file] [log] [blame]
Erik Kline92c4db02017-05-31 10:21:32 +09001/*
2 * Copyright (C) 2017 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.tethering;
18
Lorenzo Colitti5a7dea12017-07-12 15:48:07 +090019import static android.net.NetworkStats.SET_DEFAULT;
Lorenzo Colittif1912ca2017-08-17 19:23:08 +090020import static android.net.NetworkStats.STATS_PER_IFACE;
21import static android.net.NetworkStats.STATS_PER_UID;
Lorenzo Colitti5a7dea12017-07-12 15:48:07 +090022import static android.net.NetworkStats.TAG_NONE;
Lorenzo Colittif1912ca2017-08-17 19:23:08 +090023import static android.net.NetworkStats.UID_ALL;
Lorenzo Colitti5a7dea12017-07-12 15:48:07 +090024import static android.net.TrafficStats.UID_TETHERING;
Erik Kline92c4db02017-05-31 10:21:32 +090025import static android.provider.Settings.Global.TETHER_OFFLOAD_DISABLED;
Brett Chabot1ae2aa62019-03-04 14:14:56 -080026
Lorenzo Colitti5a7dea12017-07-12 15:48:07 +090027import static com.android.server.connectivity.tethering.OffloadHardwareInterface.ForwardedStats;
Brett Chabot1ae2aa62019-03-04 14:14:56 -080028
Erik Kline7990aef2017-06-01 20:11:25 +090029import static org.junit.Assert.assertEquals;
30import static org.junit.Assert.assertTrue;
Erik Kline92c4db02017-05-31 10:21:32 +090031import static org.junit.Assert.fail;
32import static org.mockito.Matchers.any;
Lorenzo Colitti50b60fc2017-08-11 13:47:49 +090033import static org.mockito.Matchers.anyLong;
Erik Kline92c4db02017-05-31 10:21:32 +090034import static org.mockito.Matchers.anyObject;
Lorenzo Colitti5a7dea12017-07-12 15:48:07 +090035import static org.mockito.Matchers.anyString;
Erik Kline7990aef2017-06-01 20:11:25 +090036import static org.mockito.Matchers.eq;
Erik Klineaabdaa92017-08-31 21:09:45 +090037import static org.mockito.Mockito.clearInvocations;
Erik Kline92c4db02017-05-31 10:21:32 +090038import static org.mockito.Mockito.inOrder;
39import static org.mockito.Mockito.never;
40import static org.mockito.Mockito.times;
Lorenzo Colitti5a7dea12017-07-12 15:48:07 +090041import static org.mockito.Mockito.verify;
Erik Klineaabdaa92017-08-31 21:09:45 +090042import static org.mockito.Mockito.verifyNoMoreInteractions;
Erik Kline92c4db02017-05-31 10:21:32 +090043import static org.mockito.Mockito.when;
44
45import android.content.Context;
Erik Klinef3a08b42017-06-07 16:33:19 +090046import android.content.pm.ApplicationInfo;
Lorenzo Colitti5a7dea12017-07-12 15:48:07 +090047import android.net.ITetheringStatsProvider;
Erik Kline32179ff2017-07-04 18:28:11 +090048import android.net.IpPrefix;
Erik Kline7990aef2017-06-01 20:11:25 +090049import android.net.LinkAddress;
50import android.net.LinkProperties;
Lorenzo Colitti5a7dea12017-07-12 15:48:07 +090051import android.net.NetworkStats;
Erik Kline7990aef2017-06-01 20:11:25 +090052import android.net.RouteInfo;
Erik Kline92c4db02017-05-31 10:21:32 +090053import android.net.util.SharedLog;
Lorenzo Colitti50b60fc2017-08-11 13:47:49 +090054import android.os.ConditionVariable;
Lorenzo Colitti5a7dea12017-07-12 15:48:07 +090055import android.os.Handler;
Lorenzo Colitti5a7dea12017-07-12 15:48:07 +090056import android.os.INetworkManagementService;
Brett Chabot1ae2aa62019-03-04 14:14:56 -080057import android.os.Looper;
Erik Kline92c4db02017-05-31 10:21:32 +090058import android.provider.Settings;
59import android.provider.Settings.SettingNotFoundException;
Erik Kline92c4db02017-05-31 10:21:32 +090060import android.test.mock.MockContentResolver;
Erik Kline92c4db02017-05-31 10:21:32 +090061
Brett Chabot1ae2aa62019-03-04 14:14:56 -080062import androidx.test.filters.SmallTest;
63import androidx.test.runner.AndroidJUnit4;
64
65import com.android.internal.util.test.FakeSettingsProvider;
Erik Kline7990aef2017-06-01 20:11:25 +090066
Erik Klinec87cd412017-07-07 17:38:30 +090067import org.junit.After;
Erik Kline92c4db02017-05-31 10:21:32 +090068import org.junit.Before;
Erik Kline92c4db02017-05-31 10:21:32 +090069import org.junit.Test;
Brett Chabot1ae2aa62019-03-04 14:14:56 -080070import org.junit.runner.RunWith;
Erik Kline7990aef2017-06-01 20:11:25 +090071import org.mockito.ArgumentCaptor;
Erik Kline92c4db02017-05-31 10:21:32 +090072import org.mockito.InOrder;
73import org.mockito.Mock;
74import org.mockito.MockitoAnnotations;
75
Brett Chabot1ae2aa62019-03-04 14:14:56 -080076import java.net.InetAddress;
77import java.util.ArrayList;
78import java.util.HashSet;
79import java.util.Set;
Erik Kline92c4db02017-05-31 10:21:32 +090080
81@RunWith(AndroidJUnit4.class)
82@SmallTest
83public class OffloadControllerTest {
Erik Klineb3bb26e2017-07-06 19:49:35 +090084 private static final String RNDIS0 = "test_rndis0";
85 private static final String RMNET0 = "test_rmnet_data0";
86 private static final String WLAN0 = "test_wlan0";
87
88 private static final String IPV6_LINKLOCAL = "fe80::/64";
89 private static final String IPV6_DOC_PREFIX = "2001:db8::/64";
90 private static final String IPV6_DISCARD_PREFIX = "100::/64";
91 private static final String USB_PREFIX = "192.168.42.0/24";
92 private static final String WIFI_PREFIX = "192.168.43.0/24";
Erik Kline92c4db02017-05-31 10:21:32 +090093
94 @Mock private OffloadHardwareInterface mHardware;
Erik Klinef3a08b42017-06-07 16:33:19 +090095 @Mock private ApplicationInfo mApplicationInfo;
Erik Kline92c4db02017-05-31 10:21:32 +090096 @Mock private Context mContext;
Lorenzo Colitti5a7dea12017-07-12 15:48:07 +090097 @Mock private INetworkManagementService mNMService;
Erik Kline7fd696c2017-06-12 18:20:08 +090098 private final ArgumentCaptor<ArrayList> mStringArrayCaptor =
99 ArgumentCaptor.forClass(ArrayList.class);
Lorenzo Colitti5a7dea12017-07-12 15:48:07 +0900100 private final ArgumentCaptor<ITetheringStatsProvider.Stub> mTetherStatsProviderCaptor =
101 ArgumentCaptor.forClass(ITetheringStatsProvider.Stub.class);
Lorenzo Colitti9f0baa92017-08-15 19:25:51 +0900102 private final ArgumentCaptor<OffloadHardwareInterface.ControlCallback> mControlCallbackCaptor =
103 ArgumentCaptor.forClass(OffloadHardwareInterface.ControlCallback.class);
Erik Kline92c4db02017-05-31 10:21:32 +0900104 private MockContentResolver mContentResolver;
105
Lorenzo Colitti5a7dea12017-07-12 15:48:07 +0900106 @Before public void setUp() {
Erik Kline92c4db02017-05-31 10:21:32 +0900107 MockitoAnnotations.initMocks(this);
Erik Klinef3a08b42017-06-07 16:33:19 +0900108 when(mContext.getApplicationInfo()).thenReturn(mApplicationInfo);
109 when(mContext.getPackageName()).thenReturn("OffloadControllerTest");
Erik Kline92c4db02017-05-31 10:21:32 +0900110 mContentResolver = new MockContentResolver(mContext);
111 mContentResolver.addProvider(Settings.AUTHORITY, new FakeSettingsProvider());
Erik Kline92c4db02017-05-31 10:21:32 +0900112 when(mContext.getContentResolver()).thenReturn(mContentResolver);
Erik Klinec87cd412017-07-07 17:38:30 +0900113 FakeSettingsProvider.clearSettingsProvider();
114 }
115
116 @After public void tearDown() throws Exception {
117 FakeSettingsProvider.clearSettingsProvider();
Erik Kline92c4db02017-05-31 10:21:32 +0900118 }
119
120 private void setupFunctioningHardwareInterface() {
121 when(mHardware.initOffloadConfig()).thenReturn(true);
Lorenzo Colitti9f0baa92017-08-15 19:25:51 +0900122 when(mHardware.initOffloadControl(mControlCallbackCaptor.capture()))
Erik Kline92c4db02017-05-31 10:21:32 +0900123 .thenReturn(true);
Lorenzo Colittiddce7ee2017-08-21 12:34:50 +0900124 when(mHardware.setUpstreamParameters(anyString(), any(), any(), any())).thenReturn(true);
Lorenzo Colitti5a7dea12017-07-12 15:48:07 +0900125 when(mHardware.getForwardedStats(any())).thenReturn(new ForwardedStats());
Lorenzo Colittiddce7ee2017-08-21 12:34:50 +0900126 when(mHardware.setDataLimit(anyString(), anyLong())).thenReturn(true);
Erik Kline92c4db02017-05-31 10:21:32 +0900127 }
128
Erik Klinec87cd412017-07-07 17:38:30 +0900129 private void enableOffload() {
130 Settings.Global.putInt(mContentResolver, TETHER_OFFLOAD_DISABLED, 0);
131 }
132
Lorenzo Colitti50b60fc2017-08-11 13:47:49 +0900133 private void waitForIdle() {
134 ConditionVariable cv = new ConditionVariable();
135 new Handler(Looper.getMainLooper()).post(() -> { cv.open(); });
136 cv.block();
137 }
138
Lorenzo Colitti5a7dea12017-07-12 15:48:07 +0900139 private OffloadController makeOffloadController() throws Exception {
140 OffloadController offload = new OffloadController(new Handler(Looper.getMainLooper()),
141 mHardware, mContentResolver, mNMService, new SharedLog("test"));
142 verify(mNMService).registerTetheringStatsProvider(
143 mTetherStatsProviderCaptor.capture(), anyString());
144 return offload;
145 }
146
Erik Kline92c4db02017-05-31 10:21:32 +0900147 @Test
Lorenzo Colitti5a7dea12017-07-12 15:48:07 +0900148 public void testNoSettingsValueDefaultDisabledDoesNotStart() throws Exception {
Erik Kline92c4db02017-05-31 10:21:32 +0900149 setupFunctioningHardwareInterface();
Erik Klinec87cd412017-07-07 17:38:30 +0900150 when(mHardware.getDefaultTetherOffloadDisabled()).thenReturn(1);
Erik Kline92c4db02017-05-31 10:21:32 +0900151 try {
152 Settings.Global.getInt(mContentResolver, TETHER_OFFLOAD_DISABLED);
153 fail();
154 } catch (SettingNotFoundException expected) {}
155
Lorenzo Colitti5a7dea12017-07-12 15:48:07 +0900156 final OffloadController offload = makeOffloadController();
Erik Kline92c4db02017-05-31 10:21:32 +0900157 offload.start();
158
159 final InOrder inOrder = inOrder(mHardware);
Erik Klinec87cd412017-07-07 17:38:30 +0900160 inOrder.verify(mHardware, times(1)).getDefaultTetherOffloadDisabled();
161 inOrder.verify(mHardware, never()).initOffloadConfig();
162 inOrder.verify(mHardware, never()).initOffloadControl(
163 any(OffloadHardwareInterface.ControlCallback.class));
164 inOrder.verifyNoMoreInteractions();
165 }
166
167 @Test
Lorenzo Colitti5a7dea12017-07-12 15:48:07 +0900168 public void testNoSettingsValueDefaultEnabledDoesStart() throws Exception {
Erik Klinec87cd412017-07-07 17:38:30 +0900169 setupFunctioningHardwareInterface();
170 when(mHardware.getDefaultTetherOffloadDisabled()).thenReturn(0);
171 try {
172 Settings.Global.getInt(mContentResolver, TETHER_OFFLOAD_DISABLED);
173 fail();
174 } catch (SettingNotFoundException expected) {}
175
Lorenzo Colitti5a7dea12017-07-12 15:48:07 +0900176 final OffloadController offload = makeOffloadController();
Erik Klinec87cd412017-07-07 17:38:30 +0900177 offload.start();
178
179 final InOrder inOrder = inOrder(mHardware);
180 inOrder.verify(mHardware, times(1)).getDefaultTetherOffloadDisabled();
Erik Kline92c4db02017-05-31 10:21:32 +0900181 inOrder.verify(mHardware, times(1)).initOffloadConfig();
182 inOrder.verify(mHardware, times(1)).initOffloadControl(
183 any(OffloadHardwareInterface.ControlCallback.class));
184 inOrder.verifyNoMoreInteractions();
185 }
186
187 @Test
Lorenzo Colitti5a7dea12017-07-12 15:48:07 +0900188 public void testSettingsAllowsStart() throws Exception {
Erik Kline92c4db02017-05-31 10:21:32 +0900189 setupFunctioningHardwareInterface();
190 Settings.Global.putInt(mContentResolver, TETHER_OFFLOAD_DISABLED, 0);
191
Lorenzo Colitti5a7dea12017-07-12 15:48:07 +0900192 final OffloadController offload = makeOffloadController();
Erik Kline92c4db02017-05-31 10:21:32 +0900193 offload.start();
194
195 final InOrder inOrder = inOrder(mHardware);
Erik Klinec87cd412017-07-07 17:38:30 +0900196 inOrder.verify(mHardware, times(1)).getDefaultTetherOffloadDisabled();
Erik Kline92c4db02017-05-31 10:21:32 +0900197 inOrder.verify(mHardware, times(1)).initOffloadConfig();
198 inOrder.verify(mHardware, times(1)).initOffloadControl(
199 any(OffloadHardwareInterface.ControlCallback.class));
200 inOrder.verifyNoMoreInteractions();
201 }
202
203 @Test
Lorenzo Colitti5a7dea12017-07-12 15:48:07 +0900204 public void testSettingsDisablesStart() throws Exception {
Erik Kline92c4db02017-05-31 10:21:32 +0900205 setupFunctioningHardwareInterface();
206 Settings.Global.putInt(mContentResolver, TETHER_OFFLOAD_DISABLED, 1);
207
Lorenzo Colitti5a7dea12017-07-12 15:48:07 +0900208 final OffloadController offload = makeOffloadController();
Erik Kline92c4db02017-05-31 10:21:32 +0900209 offload.start();
210
211 final InOrder inOrder = inOrder(mHardware);
Erik Klinec87cd412017-07-07 17:38:30 +0900212 inOrder.verify(mHardware, times(1)).getDefaultTetherOffloadDisabled();
Erik Kline92c4db02017-05-31 10:21:32 +0900213 inOrder.verify(mHardware, never()).initOffloadConfig();
214 inOrder.verify(mHardware, never()).initOffloadControl(anyObject());
215 inOrder.verifyNoMoreInteractions();
216 }
Erik Kline7990aef2017-06-01 20:11:25 +0900217
218 @Test
219 public void testSetUpstreamLinkPropertiesWorking() throws Exception {
220 setupFunctioningHardwareInterface();
Erik Klinec87cd412017-07-07 17:38:30 +0900221 enableOffload();
222
Lorenzo Colitti5a7dea12017-07-12 15:48:07 +0900223 final OffloadController offload = makeOffloadController();
Erik Kline7990aef2017-06-01 20:11:25 +0900224 offload.start();
225
226 final InOrder inOrder = inOrder(mHardware);
Erik Klinec87cd412017-07-07 17:38:30 +0900227 inOrder.verify(mHardware, times(1)).getDefaultTetherOffloadDisabled();
Erik Kline7990aef2017-06-01 20:11:25 +0900228 inOrder.verify(mHardware, times(1)).initOffloadConfig();
229 inOrder.verify(mHardware, times(1)).initOffloadControl(
230 any(OffloadHardwareInterface.ControlCallback.class));
231 inOrder.verifyNoMoreInteractions();
232
Erik Kline32179ff2017-07-04 18:28:11 +0900233 // In reality, the UpstreamNetworkMonitor would have passed down to us
234 // a covering set of local prefixes representing a minimum essential
235 // set plus all the prefixes on networks with network agents.
236 //
237 // We simulate that there, and then add upstream elements one by one
238 // and watch what happens.
239 final Set<IpPrefix> minimumLocalPrefixes = new HashSet<>();
240 for (String s : new String[]{
241 "127.0.0.0/8", "192.0.2.0/24", "fe80::/64", "2001:db8::/64"}) {
242 minimumLocalPrefixes.add(new IpPrefix(s));
243 }
244 offload.setLocalPrefixes(minimumLocalPrefixes);
245 inOrder.verify(mHardware, times(1)).setLocalPrefixes(mStringArrayCaptor.capture());
246 ArrayList<String> localPrefixes = mStringArrayCaptor.getValue();
247 assertEquals(4, localPrefixes.size());
Erik Klineb3bb26e2017-07-06 19:49:35 +0900248 assertArrayListContains(localPrefixes,
249 "127.0.0.0/8", "192.0.2.0/24", "fe80::/64", "2001:db8::/64");
Erik Kline7990aef2017-06-01 20:11:25 +0900250 inOrder.verifyNoMoreInteractions();
Erik Kline32179ff2017-07-04 18:28:11 +0900251
252 offload.setUpstreamLinkProperties(null);
253 // No change in local addresses means no call to setLocalPrefixes().
254 inOrder.verify(mHardware, never()).setLocalPrefixes(mStringArrayCaptor.capture());
255 // This LinkProperties value does not differ from the default upstream.
256 // There should be no extraneous call to setUpstreamParameters().
257 inOrder.verify(mHardware, never()).setUpstreamParameters(
258 anyObject(), anyObject(), anyObject(), anyObject());
259 inOrder.verifyNoMoreInteractions();
Erik Kline7990aef2017-06-01 20:11:25 +0900260
261 final LinkProperties lp = new LinkProperties();
262
263 final String testIfName = "rmnet_data17";
264 lp.setInterfaceName(testIfName);
265 offload.setUpstreamLinkProperties(lp);
Erik Kline32179ff2017-07-04 18:28:11 +0900266 // No change in local addresses means no call to setLocalPrefixes().
267 inOrder.verify(mHardware, never()).setLocalPrefixes(mStringArrayCaptor.capture());
Erik Kline7990aef2017-06-01 20:11:25 +0900268 inOrder.verify(mHardware, times(1)).setUpstreamParameters(
Erik Kline11854592017-06-15 18:06:34 +0900269 eq(testIfName), eq(null), eq(null), eq(null));
Lorenzo Colittiddce7ee2017-08-21 12:34:50 +0900270 inOrder.verify(mHardware, times(1)).setDataLimit(eq(testIfName), eq(Long.MAX_VALUE));
Erik Kline7990aef2017-06-01 20:11:25 +0900271 inOrder.verifyNoMoreInteractions();
272
273 final String ipv4Addr = "192.0.2.5";
274 final String linkAddr = ipv4Addr + "/24";
275 lp.addLinkAddress(new LinkAddress(linkAddr));
Erik Kline32179ff2017-07-04 18:28:11 +0900276 lp.addRoute(new RouteInfo(new IpPrefix("192.0.2.0/24")));
Erik Kline7990aef2017-06-01 20:11:25 +0900277 offload.setUpstreamLinkProperties(lp);
Erik Kline32179ff2017-07-04 18:28:11 +0900278 // IPv4 prefixes and addresses on the upstream are simply left as whole
279 // prefixes (already passed in from UpstreamNetworkMonitor code). If a
280 // tethering client sends traffic to the IPv4 default router or other
281 // clients on the upstream this will not be hardware-forwarded, and that
282 // should be fine for now. Ergo: no change in local addresses, no call
283 // to setLocalPrefixes().
284 inOrder.verify(mHardware, never()).setLocalPrefixes(mStringArrayCaptor.capture());
Erik Kline7990aef2017-06-01 20:11:25 +0900285 inOrder.verify(mHardware, times(1)).setUpstreamParameters(
Erik Kline11854592017-06-15 18:06:34 +0900286 eq(testIfName), eq(ipv4Addr), eq(null), eq(null));
Lorenzo Colitti5a7dea12017-07-12 15:48:07 +0900287 inOrder.verify(mHardware, times(1)).getForwardedStats(eq(testIfName));
Lorenzo Colittiddce7ee2017-08-21 12:34:50 +0900288 inOrder.verify(mHardware, times(1)).setDataLimit(eq(testIfName), eq(Long.MAX_VALUE));
Erik Kline7990aef2017-06-01 20:11:25 +0900289 inOrder.verifyNoMoreInteractions();
290
291 final String ipv4Gateway = "192.0.2.1";
292 lp.addRoute(new RouteInfo(InetAddress.getByName(ipv4Gateway)));
293 offload.setUpstreamLinkProperties(lp);
Erik Kline32179ff2017-07-04 18:28:11 +0900294 // No change in local addresses means no call to setLocalPrefixes().
295 inOrder.verify(mHardware, never()).setLocalPrefixes(mStringArrayCaptor.capture());
Erik Kline7990aef2017-06-01 20:11:25 +0900296 inOrder.verify(mHardware, times(1)).setUpstreamParameters(
Erik Kline11854592017-06-15 18:06:34 +0900297 eq(testIfName), eq(ipv4Addr), eq(ipv4Gateway), eq(null));
Lorenzo Colitti5a7dea12017-07-12 15:48:07 +0900298 inOrder.verify(mHardware, times(1)).getForwardedStats(eq(testIfName));
Lorenzo Colittiddce7ee2017-08-21 12:34:50 +0900299 inOrder.verify(mHardware, times(1)).setDataLimit(eq(testIfName), eq(Long.MAX_VALUE));
Erik Kline7990aef2017-06-01 20:11:25 +0900300 inOrder.verifyNoMoreInteractions();
301
302 final String ipv6Gw1 = "fe80::cafe";
303 lp.addRoute(new RouteInfo(InetAddress.getByName(ipv6Gw1)));
304 offload.setUpstreamLinkProperties(lp);
Erik Kline32179ff2017-07-04 18:28:11 +0900305 // No change in local addresses means no call to setLocalPrefixes().
306 inOrder.verify(mHardware, never()).setLocalPrefixes(mStringArrayCaptor.capture());
Erik Kline7990aef2017-06-01 20:11:25 +0900307 inOrder.verify(mHardware, times(1)).setUpstreamParameters(
308 eq(testIfName), eq(ipv4Addr), eq(ipv4Gateway), mStringArrayCaptor.capture());
Lorenzo Colitti5a7dea12017-07-12 15:48:07 +0900309 inOrder.verify(mHardware, times(1)).getForwardedStats(eq(testIfName));
Erik Kline7990aef2017-06-01 20:11:25 +0900310 ArrayList<String> v6gws = mStringArrayCaptor.getValue();
311 assertEquals(1, v6gws.size());
312 assertTrue(v6gws.contains(ipv6Gw1));
Lorenzo Colittiddce7ee2017-08-21 12:34:50 +0900313 inOrder.verify(mHardware, times(1)).setDataLimit(eq(testIfName), eq(Long.MAX_VALUE));
Erik Kline7990aef2017-06-01 20:11:25 +0900314 inOrder.verifyNoMoreInteractions();
315
316 final String ipv6Gw2 = "fe80::d00d";
317 lp.addRoute(new RouteInfo(InetAddress.getByName(ipv6Gw2)));
318 offload.setUpstreamLinkProperties(lp);
Erik Kline32179ff2017-07-04 18:28:11 +0900319 // No change in local addresses means no call to setLocalPrefixes().
320 inOrder.verify(mHardware, never()).setLocalPrefixes(mStringArrayCaptor.capture());
Erik Kline7990aef2017-06-01 20:11:25 +0900321 inOrder.verify(mHardware, times(1)).setUpstreamParameters(
322 eq(testIfName), eq(ipv4Addr), eq(ipv4Gateway), mStringArrayCaptor.capture());
Lorenzo Colitti5a7dea12017-07-12 15:48:07 +0900323 inOrder.verify(mHardware, times(1)).getForwardedStats(eq(testIfName));
Erik Kline7990aef2017-06-01 20:11:25 +0900324 v6gws = mStringArrayCaptor.getValue();
325 assertEquals(2, v6gws.size());
326 assertTrue(v6gws.contains(ipv6Gw1));
327 assertTrue(v6gws.contains(ipv6Gw2));
Lorenzo Colittiddce7ee2017-08-21 12:34:50 +0900328 inOrder.verify(mHardware, times(1)).setDataLimit(eq(testIfName), eq(Long.MAX_VALUE));
Erik Kline7990aef2017-06-01 20:11:25 +0900329 inOrder.verifyNoMoreInteractions();
330
331 final LinkProperties stacked = new LinkProperties();
332 stacked.setInterfaceName("stacked");
333 stacked.addLinkAddress(new LinkAddress("192.0.2.129/25"));
334 stacked.addRoute(new RouteInfo(InetAddress.getByName("192.0.2.254")));
335 stacked.addRoute(new RouteInfo(InetAddress.getByName("fe80::bad:f00")));
336 assertTrue(lp.addStackedLink(stacked));
337 offload.setUpstreamLinkProperties(lp);
Erik Kline32179ff2017-07-04 18:28:11 +0900338 // No change in local addresses means no call to setLocalPrefixes().
339 inOrder.verify(mHardware, never()).setLocalPrefixes(mStringArrayCaptor.capture());
Erik Kline7990aef2017-06-01 20:11:25 +0900340 inOrder.verify(mHardware, times(1)).setUpstreamParameters(
341 eq(testIfName), eq(ipv4Addr), eq(ipv4Gateway), mStringArrayCaptor.capture());
Lorenzo Colitti5a7dea12017-07-12 15:48:07 +0900342 inOrder.verify(mHardware, times(1)).getForwardedStats(eq(testIfName));
Erik Kline7990aef2017-06-01 20:11:25 +0900343 v6gws = mStringArrayCaptor.getValue();
344 assertEquals(2, v6gws.size());
345 assertTrue(v6gws.contains(ipv6Gw1));
346 assertTrue(v6gws.contains(ipv6Gw2));
Lorenzo Colittiddce7ee2017-08-21 12:34:50 +0900347 inOrder.verify(mHardware, times(1)).setDataLimit(eq(testIfName), eq(Long.MAX_VALUE));
Erik Kline7990aef2017-06-01 20:11:25 +0900348 inOrder.verifyNoMoreInteractions();
Erik Kline32179ff2017-07-04 18:28:11 +0900349
350 // Add in some IPv6 upstream info. When there is a tethered downstream
351 // making use of the IPv6 prefix we would expect to see the /64 route
352 // removed from "local prefixes" and /128s added for the upstream IPv6
353 // addresses. This is not yet implemented, and for now we simply
354 // expect to see these /128s.
355 lp.addRoute(new RouteInfo(new IpPrefix("2001:db8::/64")));
356 // "2001:db8::/64" plus "assigned" ASCII in hex
357 lp.addLinkAddress(new LinkAddress("2001:db8::6173:7369:676e:6564/64"));
358 // "2001:db8::/64" plus "random" ASCII in hex
359 lp.addLinkAddress(new LinkAddress("2001:db8::7261:6e64:6f6d/64"));
360 offload.setUpstreamLinkProperties(lp);
361 inOrder.verify(mHardware, times(1)).setLocalPrefixes(mStringArrayCaptor.capture());
362 localPrefixes = mStringArrayCaptor.getValue();
363 assertEquals(6, localPrefixes.size());
Erik Klineb3bb26e2017-07-06 19:49:35 +0900364 assertArrayListContains(localPrefixes,
365 "127.0.0.0/8", "192.0.2.0/24", "fe80::/64", "2001:db8::/64",
366 "2001:db8::6173:7369:676e:6564/128", "2001:db8::7261:6e64:6f6d/128");
Erik Kline32179ff2017-07-04 18:28:11 +0900367 // The relevant parts of the LinkProperties have not changed, but at the
368 // moment we do not de-dup upstream LinkProperties this carefully.
369 inOrder.verify(mHardware, times(1)).setUpstreamParameters(
370 eq(testIfName), eq(ipv4Addr), eq(ipv4Gateway), mStringArrayCaptor.capture());
371 v6gws = mStringArrayCaptor.getValue();
372 assertEquals(2, v6gws.size());
373 assertTrue(v6gws.contains(ipv6Gw1));
374 assertTrue(v6gws.contains(ipv6Gw2));
Lorenzo Colitti5a7dea12017-07-12 15:48:07 +0900375 inOrder.verify(mHardware, times(1)).getForwardedStats(eq(testIfName));
Lorenzo Colittiddce7ee2017-08-21 12:34:50 +0900376 inOrder.verify(mHardware, times(1)).setDataLimit(eq(testIfName), eq(Long.MAX_VALUE));
Erik Kline32179ff2017-07-04 18:28:11 +0900377 inOrder.verifyNoMoreInteractions();
378
379 // Completely identical LinkProperties updates are de-duped.
380 offload.setUpstreamLinkProperties(lp);
381 // This LinkProperties value does not differ from the default upstream.
382 // There should be no extraneous call to setUpstreamParameters().
383 inOrder.verify(mHardware, never()).setUpstreamParameters(
384 anyObject(), anyObject(), anyObject(), anyObject());
385 inOrder.verifyNoMoreInteractions();
Erik Kline7990aef2017-06-01 20:11:25 +0900386 }
Lorenzo Colitti5a7dea12017-07-12 15:48:07 +0900387
388 private void assertNetworkStats(String iface, ForwardedStats stats, NetworkStats.Entry entry) {
389 assertEquals(iface, entry.iface);
390 assertEquals(stats.rxBytes, entry.rxBytes);
391 assertEquals(stats.txBytes, entry.txBytes);
392 assertEquals(SET_DEFAULT, entry.set);
393 assertEquals(TAG_NONE, entry.tag);
Lorenzo Colitti5a7dea12017-07-12 15:48:07 +0900394 }
395
396 @Test
397 public void testGetForwardedStats() throws Exception {
398 setupFunctioningHardwareInterface();
399 enableOffload();
400
401 final OffloadController offload = makeOffloadController();
402 offload.start();
403
404 final String ethernetIface = "eth1";
405 final String mobileIface = "rmnet_data0";
406
407 ForwardedStats ethernetStats = new ForwardedStats();
408 ethernetStats.rxBytes = 12345;
409 ethernetStats.txBytes = 54321;
410
411 ForwardedStats mobileStats = new ForwardedStats();
412 mobileStats.rxBytes = 999;
413 mobileStats.txBytes = 99999;
414
415 when(mHardware.getForwardedStats(eq(ethernetIface))).thenReturn(ethernetStats);
416 when(mHardware.getForwardedStats(eq(mobileIface))).thenReturn(mobileStats);
417
Hugo Benichi752c1282017-08-22 13:57:41 +0900418 InOrder inOrder = inOrder(mHardware);
419
Lorenzo Colitti5a7dea12017-07-12 15:48:07 +0900420 final LinkProperties lp = new LinkProperties();
421 lp.setInterfaceName(ethernetIface);
422 offload.setUpstreamLinkProperties(lp);
Hugo Benichi752c1282017-08-22 13:57:41 +0900423 // Previous upstream was null, so no stats are fetched.
424 inOrder.verify(mHardware, never()).getForwardedStats(any());
Lorenzo Colitti5a7dea12017-07-12 15:48:07 +0900425
426 lp.setInterfaceName(mobileIface);
427 offload.setUpstreamLinkProperties(lp);
Hugo Benichi752c1282017-08-22 13:57:41 +0900428 // Expect that we fetch stats from the previous upstream.
429 inOrder.verify(mHardware, times(1)).getForwardedStats(eq(ethernetIface));
Lorenzo Colitti5a7dea12017-07-12 15:48:07 +0900430
431 lp.setInterfaceName(ethernetIface);
432 offload.setUpstreamLinkProperties(lp);
Hugo Benichi752c1282017-08-22 13:57:41 +0900433 // Expect that we fetch stats from the previous upstream.
434 inOrder.verify(mHardware, times(1)).getForwardedStats(eq(mobileIface));
Lorenzo Colitti5a7dea12017-07-12 15:48:07 +0900435
Hugo Benichi752c1282017-08-22 13:57:41 +0900436 ethernetStats = new ForwardedStats();
Lorenzo Colitti5a7dea12017-07-12 15:48:07 +0900437 ethernetStats.rxBytes = 100000;
438 ethernetStats.txBytes = 100000;
Hugo Benichi752c1282017-08-22 13:57:41 +0900439 when(mHardware.getForwardedStats(eq(ethernetIface))).thenReturn(ethernetStats);
Lorenzo Colitti5a7dea12017-07-12 15:48:07 +0900440 offload.setUpstreamLinkProperties(null);
Erik Klineaabdaa92017-08-31 21:09:45 +0900441 // Expect that we first clear the HAL's upstream parameters.
442 inOrder.verify(mHardware, times(1)).setUpstreamParameters(
443 eq(""), eq("0.0.0.0"), eq("0.0.0.0"), eq(null));
Hugo Benichi752c1282017-08-22 13:57:41 +0900444 // Expect that we fetch stats from the previous upstream.
445 inOrder.verify(mHardware, times(1)).getForwardedStats(eq(ethernetIface));
Lorenzo Colitti5a7dea12017-07-12 15:48:07 +0900446
Lorenzo Colittif1912ca2017-08-17 19:23:08 +0900447 ITetheringStatsProvider provider = mTetherStatsProviderCaptor.getValue();
448 NetworkStats stats = provider.getTetherStats(STATS_PER_IFACE);
449 NetworkStats perUidStats = provider.getTetherStats(STATS_PER_UID);
Hugo Benichi752c1282017-08-22 13:57:41 +0900450 waitForIdle();
451 // There is no current upstream, so no stats are fetched.
Lorenzo Colittif612b602017-08-24 12:40:07 +0900452 inOrder.verify(mHardware, never()).getForwardedStats(any());
Hugo Benichi752c1282017-08-22 13:57:41 +0900453 inOrder.verifyNoMoreInteractions();
Lorenzo Colittif1912ca2017-08-17 19:23:08 +0900454
Lorenzo Colitti5a7dea12017-07-12 15:48:07 +0900455 assertEquals(2, stats.size());
Lorenzo Colittif1912ca2017-08-17 19:23:08 +0900456 assertEquals(2, perUidStats.size());
Lorenzo Colitti5a7dea12017-07-12 15:48:07 +0900457
458 NetworkStats.Entry entry = null;
Lorenzo Colittif1912ca2017-08-17 19:23:08 +0900459 for (int i = 0; i < stats.size(); i++) {
460 assertEquals(UID_ALL, stats.getValues(i, entry).uid);
461 assertEquals(UID_TETHERING, perUidStats.getValues(i, entry).uid);
462 }
463
Lorenzo Colitti5a7dea12017-07-12 15:48:07 +0900464 int ethernetPosition = ethernetIface.equals(stats.getValues(0, entry).iface) ? 0 : 1;
465 int mobilePosition = 1 - ethernetPosition;
466
467 entry = stats.getValues(mobilePosition, entry);
468 assertNetworkStats(mobileIface, mobileStats, entry);
Lorenzo Colittif1912ca2017-08-17 19:23:08 +0900469 entry = perUidStats.getValues(mobilePosition, entry);
470 assertNetworkStats(mobileIface, mobileStats, entry);
Lorenzo Colitti5a7dea12017-07-12 15:48:07 +0900471
472 ethernetStats.rxBytes = 12345 + 100000;
473 ethernetStats.txBytes = 54321 + 100000;
474 entry = stats.getValues(ethernetPosition, entry);
475 assertNetworkStats(ethernetIface, ethernetStats, entry);
Lorenzo Colittif1912ca2017-08-17 19:23:08 +0900476 entry = perUidStats.getValues(ethernetPosition, entry);
477 assertNetworkStats(ethernetIface, ethernetStats, entry);
Lorenzo Colitti5a7dea12017-07-12 15:48:07 +0900478 }
Lorenzo Colitti50b60fc2017-08-11 13:47:49 +0900479
480 @Test
481 public void testSetInterfaceQuota() throws Exception {
482 setupFunctioningHardwareInterface();
483 enableOffload();
484
485 final OffloadController offload = makeOffloadController();
486 offload.start();
487
488 final String ethernetIface = "eth1";
489 final String mobileIface = "rmnet_data0";
490 final long ethernetLimit = 12345;
491 final long mobileLimit = 12345678;
492
493 final LinkProperties lp = new LinkProperties();
494 lp.setInterfaceName(ethernetIface);
495 offload.setUpstreamLinkProperties(lp);
496
497 ITetheringStatsProvider provider = mTetherStatsProviderCaptor.getValue();
498 final InOrder inOrder = inOrder(mHardware);
499 when(mHardware.setUpstreamParameters(any(), any(), any(), any())).thenReturn(true);
500 when(mHardware.setDataLimit(anyString(), anyLong())).thenReturn(true);
501
502 // Applying an interface quota to the current upstream immediately sends it to the hardware.
503 provider.setInterfaceQuota(ethernetIface, ethernetLimit);
504 waitForIdle();
505 inOrder.verify(mHardware).setDataLimit(ethernetIface, ethernetLimit);
506 inOrder.verifyNoMoreInteractions();
507
508 // Applying an interface quota to another upstream does not take any immediate action.
509 provider.setInterfaceQuota(mobileIface, mobileLimit);
510 waitForIdle();
511 inOrder.verify(mHardware, never()).setDataLimit(anyString(), anyLong());
512
513 // Switching to that upstream causes the quota to be applied if the parameters were applied
514 // correctly.
515 lp.setInterfaceName(mobileIface);
516 offload.setUpstreamLinkProperties(lp);
517 waitForIdle();
518 inOrder.verify(mHardware).setDataLimit(mobileIface, mobileLimit);
519
520 // Setting a limit of ITetheringStatsProvider.QUOTA_UNLIMITED causes the limit to be set
521 // to Long.MAX_VALUE.
522 provider.setInterfaceQuota(mobileIface, ITetheringStatsProvider.QUOTA_UNLIMITED);
523 waitForIdle();
524 inOrder.verify(mHardware).setDataLimit(mobileIface, Long.MAX_VALUE);
525
526 // If setting upstream parameters fails, then the data limit is not set.
527 when(mHardware.setUpstreamParameters(any(), any(), any(), any())).thenReturn(false);
528 lp.setInterfaceName(ethernetIface);
529 offload.setUpstreamLinkProperties(lp);
530 provider.setInterfaceQuota(mobileIface, mobileLimit);
531 waitForIdle();
532 inOrder.verify(mHardware, never()).setDataLimit(anyString(), anyLong());
533
534 // If setting the data limit fails while changing upstreams, offload is stopped.
535 when(mHardware.setUpstreamParameters(any(), any(), any(), any())).thenReturn(true);
536 when(mHardware.setDataLimit(anyString(), anyLong())).thenReturn(false);
537 lp.setInterfaceName(mobileIface);
538 offload.setUpstreamLinkProperties(lp);
539 provider.setInterfaceQuota(mobileIface, mobileLimit);
540 waitForIdle();
Lorenzo Colittiddce7ee2017-08-21 12:34:50 +0900541 inOrder.verify(mHardware).getForwardedStats(ethernetIface);
Lorenzo Colitti50b60fc2017-08-11 13:47:49 +0900542 inOrder.verify(mHardware).stopOffloadControl();
543 }
Lorenzo Colitti9f0baa92017-08-15 19:25:51 +0900544
545 @Test
546 public void testDataLimitCallback() throws Exception {
547 setupFunctioningHardwareInterface();
548 enableOffload();
549
550 final OffloadController offload = makeOffloadController();
551 offload.start();
552
553 OffloadHardwareInterface.ControlCallback callback = mControlCallbackCaptor.getValue();
554 callback.onStoppedLimitReached();
555 verify(mNMService, times(1)).tetherLimitReached(mTetherStatsProviderCaptor.getValue());
556 }
Erik Klineb3bb26e2017-07-06 19:49:35 +0900557
558 @Test
559 public void testAddRemoveDownstreams() throws Exception {
560 setupFunctioningHardwareInterface();
561 enableOffload();
562
563 final OffloadController offload = makeOffloadController();
564 offload.start();
565
566 final InOrder inOrder = inOrder(mHardware);
567 inOrder.verify(mHardware, times(1)).initOffloadConfig();
568 inOrder.verify(mHardware, times(1)).initOffloadControl(
569 any(OffloadHardwareInterface.ControlCallback.class));
570 inOrder.verifyNoMoreInteractions();
571
572 // Tethering makes several calls to setLocalPrefixes() before add/remove
573 // downstream calls are made. This is not tested here; only the behavior
574 // of notifyDownstreamLinkProperties() and removeDownstreamInterface()
575 // are tested.
576
577 // [1] USB tethering is started.
578 final LinkProperties usbLinkProperties = new LinkProperties();
579 usbLinkProperties.setInterfaceName(RNDIS0);
580 usbLinkProperties.addLinkAddress(new LinkAddress("192.168.42.1/24"));
581 usbLinkProperties.addRoute(new RouteInfo(new IpPrefix(USB_PREFIX)));
582 offload.notifyDownstreamLinkProperties(usbLinkProperties);
583 inOrder.verify(mHardware, times(1)).addDownstreamPrefix(RNDIS0, USB_PREFIX);
584 inOrder.verifyNoMoreInteractions();
585
586 // [2] Routes for IPv6 link-local prefixes should never be added.
587 usbLinkProperties.addRoute(new RouteInfo(new IpPrefix(IPV6_LINKLOCAL)));
588 offload.notifyDownstreamLinkProperties(usbLinkProperties);
589 inOrder.verify(mHardware, never()).addDownstreamPrefix(eq(RNDIS0), anyString());
590 inOrder.verifyNoMoreInteractions();
591
592 // [3] Add an IPv6 prefix for good measure. Only new offload-able
593 // prefixes should be passed to the HAL.
594 usbLinkProperties.addLinkAddress(new LinkAddress("2001:db8::1/64"));
595 usbLinkProperties.addRoute(new RouteInfo(new IpPrefix(IPV6_DOC_PREFIX)));
596 offload.notifyDownstreamLinkProperties(usbLinkProperties);
597 inOrder.verify(mHardware, times(1)).addDownstreamPrefix(RNDIS0, IPV6_DOC_PREFIX);
598 inOrder.verifyNoMoreInteractions();
599
600 // [4] Adding addresses doesn't affect notifyDownstreamLinkProperties().
601 // The address is passed in by a separate setLocalPrefixes() invocation.
602 usbLinkProperties.addLinkAddress(new LinkAddress("2001:db8::2/64"));
603 offload.notifyDownstreamLinkProperties(usbLinkProperties);
604 inOrder.verify(mHardware, never()).addDownstreamPrefix(eq(RNDIS0), anyString());
605
606 // [5] Differences in local routes are converted into addDownstream()
607 // and removeDownstream() invocations accordingly.
608 usbLinkProperties.removeRoute(new RouteInfo(new IpPrefix(IPV6_DOC_PREFIX), null, RNDIS0));
609 usbLinkProperties.addRoute(new RouteInfo(new IpPrefix(IPV6_DISCARD_PREFIX)));
610 offload.notifyDownstreamLinkProperties(usbLinkProperties);
611 inOrder.verify(mHardware, times(1)).removeDownstreamPrefix(RNDIS0, IPV6_DOC_PREFIX);
612 inOrder.verify(mHardware, times(1)).addDownstreamPrefix(RNDIS0, IPV6_DISCARD_PREFIX);
613 inOrder.verifyNoMoreInteractions();
614
615 // [6] Removing a downstream interface which was never added causes no
616 // interactions with the HAL.
617 offload.removeDownstreamInterface(WLAN0);
618 inOrder.verifyNoMoreInteractions();
619
620 // [7] Removing an active downstream removes all remaining prefixes.
621 offload.removeDownstreamInterface(RNDIS0);
622 inOrder.verify(mHardware, times(1)).removeDownstreamPrefix(RNDIS0, USB_PREFIX);
623 inOrder.verify(mHardware, times(1)).removeDownstreamPrefix(RNDIS0, IPV6_DISCARD_PREFIX);
624 inOrder.verifyNoMoreInteractions();
625 }
626
Erik Klineaabdaa92017-08-31 21:09:45 +0900627 @Test
628 public void testControlCallbackOnStoppedUnsupportedFetchesAllStats() throws Exception {
629 setupFunctioningHardwareInterface();
630 enableOffload();
631
632 final OffloadController offload = makeOffloadController();
633 offload.start();
634
635 // Pretend to set a few different upstreams (only the interface name
636 // matters for this test; we're ignoring IP and route information).
637 final LinkProperties upstreamLp = new LinkProperties();
638 for (String ifname : new String[]{RMNET0, WLAN0, RMNET0}) {
639 upstreamLp.setInterfaceName(ifname);
640 offload.setUpstreamLinkProperties(upstreamLp);
641 }
642
643 // Clear invocation history, especially the getForwardedStats() calls
644 // that happen with setUpstreamParameters().
645 clearInvocations(mHardware);
646
647 OffloadHardwareInterface.ControlCallback callback = mControlCallbackCaptor.getValue();
648 callback.onStoppedUnsupported();
649
650 // Verify forwarded stats behaviour.
651 verify(mHardware, times(1)).getForwardedStats(eq(RMNET0));
652 verify(mHardware, times(1)).getForwardedStats(eq(WLAN0));
653 verifyNoMoreInteractions(mHardware);
654 verify(mNMService, times(1)).tetherLimitReached(mTetherStatsProviderCaptor.getValue());
655 verifyNoMoreInteractions(mNMService);
656 }
657
658 @Test
659 public void testControlCallbackOnSupportAvailableFetchesAllStatsAndPushesAllParameters()
660 throws Exception {
661 setupFunctioningHardwareInterface();
662 enableOffload();
663
664 final OffloadController offload = makeOffloadController();
665 offload.start();
666
667 // Pretend to set a few different upstreams (only the interface name
668 // matters for this test; we're ignoring IP and route information).
669 final LinkProperties upstreamLp = new LinkProperties();
670 for (String ifname : new String[]{RMNET0, WLAN0, RMNET0}) {
671 upstreamLp.setInterfaceName(ifname);
672 offload.setUpstreamLinkProperties(upstreamLp);
673 }
674
Erik Klinebc8b2ee2017-09-19 17:56:10 +0900675 // Pretend that some local prefixes and downstreams have been added
676 // (and removed, for good measure).
677 final Set<IpPrefix> minimumLocalPrefixes = new HashSet<>();
678 for (String s : new String[]{
679 "127.0.0.0/8", "192.0.2.0/24", "fe80::/64", "2001:db8::/64"}) {
680 minimumLocalPrefixes.add(new IpPrefix(s));
681 }
682 offload.setLocalPrefixes(minimumLocalPrefixes);
683
684 final LinkProperties usbLinkProperties = new LinkProperties();
685 usbLinkProperties.setInterfaceName(RNDIS0);
686 usbLinkProperties.addLinkAddress(new LinkAddress("192.168.42.1/24"));
687 usbLinkProperties.addRoute(new RouteInfo(new IpPrefix(USB_PREFIX)));
688 offload.notifyDownstreamLinkProperties(usbLinkProperties);
689
690 final LinkProperties wifiLinkProperties = new LinkProperties();
691 wifiLinkProperties.setInterfaceName(WLAN0);
692 wifiLinkProperties.addLinkAddress(new LinkAddress("192.168.43.1/24"));
693 wifiLinkProperties.addRoute(new RouteInfo(new IpPrefix(WIFI_PREFIX)));
694 wifiLinkProperties.addRoute(new RouteInfo(new IpPrefix(IPV6_LINKLOCAL)));
695 // Use a benchmark prefix (RFC 5180 + erratum), since the documentation
696 // prefix is included in the excluded prefix list.
697 wifiLinkProperties.addLinkAddress(new LinkAddress("2001:2::1/64"));
698 wifiLinkProperties.addLinkAddress(new LinkAddress("2001:2::2/64"));
699 wifiLinkProperties.addRoute(new RouteInfo(new IpPrefix("2001:2::/64")));
700 offload.notifyDownstreamLinkProperties(wifiLinkProperties);
701
702 offload.removeDownstreamInterface(RNDIS0);
703
Erik Klineaabdaa92017-08-31 21:09:45 +0900704 // Clear invocation history, especially the getForwardedStats() calls
705 // that happen with setUpstreamParameters().
706 clearInvocations(mHardware);
707
708 OffloadHardwareInterface.ControlCallback callback = mControlCallbackCaptor.getValue();
709 callback.onSupportAvailable();
710
711 // Verify forwarded stats behaviour.
712 verify(mHardware, times(1)).getForwardedStats(eq(RMNET0));
713 verify(mHardware, times(1)).getForwardedStats(eq(WLAN0));
714 verify(mNMService, times(1)).tetherLimitReached(mTetherStatsProviderCaptor.getValue());
715 verifyNoMoreInteractions(mNMService);
716
717 // TODO: verify local prefixes and downstreams are also pushed to the HAL.
Erik Klinebc8b2ee2017-09-19 17:56:10 +0900718 verify(mHardware, times(1)).setLocalPrefixes(mStringArrayCaptor.capture());
719 ArrayList<String> localPrefixes = mStringArrayCaptor.getValue();
720 assertEquals(4, localPrefixes.size());
721 assertArrayListContains(localPrefixes,
722 // TODO: The logic to find and exclude downstream IP prefixes
723 // is currently in Tethering's OffloadWrapper but must be moved
724 // into OffloadController proper. After this, also check for:
725 // "192.168.43.1/32", "2001:2::1/128", "2001:2::2/128"
726 "127.0.0.0/8", "192.0.2.0/24", "fe80::/64", "2001:db8::/64");
727 verify(mHardware, times(1)).addDownstreamPrefix(WLAN0, "192.168.43.0/24");
728 verify(mHardware, times(1)).addDownstreamPrefix(WLAN0, "2001:2::/64");
Erik Klineaabdaa92017-08-31 21:09:45 +0900729 verify(mHardware, times(1)).setUpstreamParameters(eq(RMNET0), any(), any(), any());
730 verify(mHardware, times(1)).setDataLimit(eq(RMNET0), anyLong());
731 verifyNoMoreInteractions(mHardware);
732 }
733
Erik Klineb3bb26e2017-07-06 19:49:35 +0900734 private static void assertArrayListContains(ArrayList<String> list, String... elems) {
735 for (String element : elems) {
Erik Klinebc8b2ee2017-09-19 17:56:10 +0900736 assertTrue(element + " not in list", list.contains(element));
Erik Klineb3bb26e2017-07-06 19:49:35 +0900737 }
738 }
Erik Kline92c4db02017-05-31 10:21:32 +0900739}