blob: 762b76c0e18a547ef9e2cbb33304e5572ba3dabf [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 Colitti5a7dea12017-07-12 15:48:07 +090054import android.os.Handler;
Lorenzo Colitti5a7dea12017-07-12 15:48:07 +090055import android.os.INetworkManagementService;
Brett Chabot1ae2aa62019-03-04 14:14:56 -080056import android.os.Looper;
Erik Kline92c4db02017-05-31 10:21:32 +090057import android.provider.Settings;
58import android.provider.Settings.SettingNotFoundException;
Erik Kline92c4db02017-05-31 10:21:32 +090059import android.test.mock.MockContentResolver;
Erik Kline92c4db02017-05-31 10:21:32 +090060
Brett Chabot1ae2aa62019-03-04 14:14:56 -080061import androidx.test.filters.SmallTest;
62import androidx.test.runner.AndroidJUnit4;
63
64import com.android.internal.util.test.FakeSettingsProvider;
Chalard Jeance75e0e2019-05-28 16:48:32 +090065import com.android.testutils.HandlerUtilsKt;
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";
Chalard Jeance75e0e2019-05-28 16:48:32 +090093 private static final long WAIT_FOR_IDLE_TIMEOUT = 2 * 1000;
Erik Kline92c4db02017-05-31 10:21:32 +090094
95 @Mock private OffloadHardwareInterface mHardware;
Erik Klinef3a08b42017-06-07 16:33:19 +090096 @Mock private ApplicationInfo mApplicationInfo;
Erik Kline92c4db02017-05-31 10:21:32 +090097 @Mock private Context mContext;
Lorenzo Colitti5a7dea12017-07-12 15:48:07 +090098 @Mock private INetworkManagementService mNMService;
Erik Kline7fd696c2017-06-12 18:20:08 +090099 private final ArgumentCaptor<ArrayList> mStringArrayCaptor =
100 ArgumentCaptor.forClass(ArrayList.class);
Lorenzo Colitti5a7dea12017-07-12 15:48:07 +0900101 private final ArgumentCaptor<ITetheringStatsProvider.Stub> mTetherStatsProviderCaptor =
102 ArgumentCaptor.forClass(ITetheringStatsProvider.Stub.class);
Lorenzo Colitti9f0baa92017-08-15 19:25:51 +0900103 private final ArgumentCaptor<OffloadHardwareInterface.ControlCallback> mControlCallbackCaptor =
104 ArgumentCaptor.forClass(OffloadHardwareInterface.ControlCallback.class);
Erik Kline92c4db02017-05-31 10:21:32 +0900105 private MockContentResolver mContentResolver;
106
Lorenzo Colitti5a7dea12017-07-12 15:48:07 +0900107 @Before public void setUp() {
Erik Kline92c4db02017-05-31 10:21:32 +0900108 MockitoAnnotations.initMocks(this);
Erik Klinef3a08b42017-06-07 16:33:19 +0900109 when(mContext.getApplicationInfo()).thenReturn(mApplicationInfo);
110 when(mContext.getPackageName()).thenReturn("OffloadControllerTest");
Erik Kline92c4db02017-05-31 10:21:32 +0900111 mContentResolver = new MockContentResolver(mContext);
112 mContentResolver.addProvider(Settings.AUTHORITY, new FakeSettingsProvider());
Erik Kline92c4db02017-05-31 10:21:32 +0900113 when(mContext.getContentResolver()).thenReturn(mContentResolver);
Erik Klinec87cd412017-07-07 17:38:30 +0900114 FakeSettingsProvider.clearSettingsProvider();
115 }
116
117 @After public void tearDown() throws Exception {
118 FakeSettingsProvider.clearSettingsProvider();
Erik Kline92c4db02017-05-31 10:21:32 +0900119 }
120
121 private void setupFunctioningHardwareInterface() {
122 when(mHardware.initOffloadConfig()).thenReturn(true);
Lorenzo Colitti9f0baa92017-08-15 19:25:51 +0900123 when(mHardware.initOffloadControl(mControlCallbackCaptor.capture()))
Erik Kline92c4db02017-05-31 10:21:32 +0900124 .thenReturn(true);
Lorenzo Colittiddce7ee2017-08-21 12:34:50 +0900125 when(mHardware.setUpstreamParameters(anyString(), any(), any(), any())).thenReturn(true);
Lorenzo Colitti5a7dea12017-07-12 15:48:07 +0900126 when(mHardware.getForwardedStats(any())).thenReturn(new ForwardedStats());
Lorenzo Colittiddce7ee2017-08-21 12:34:50 +0900127 when(mHardware.setDataLimit(anyString(), anyLong())).thenReturn(true);
Erik Kline92c4db02017-05-31 10:21:32 +0900128 }
129
Erik Klinec87cd412017-07-07 17:38:30 +0900130 private void enableOffload() {
131 Settings.Global.putInt(mContentResolver, TETHER_OFFLOAD_DISABLED, 0);
132 }
133
Lorenzo Colitti50b60fc2017-08-11 13:47:49 +0900134 private void waitForIdle() {
Chalard Jeance75e0e2019-05-28 16:48:32 +0900135 HandlerUtilsKt.waitForIdle(new Handler(Looper.getMainLooper()), WAIT_FOR_IDLE_TIMEOUT);
Lorenzo Colitti50b60fc2017-08-11 13:47:49 +0900136 }
137
Lorenzo Colitti5a7dea12017-07-12 15:48:07 +0900138 private OffloadController makeOffloadController() throws Exception {
139 OffloadController offload = new OffloadController(new Handler(Looper.getMainLooper()),
140 mHardware, mContentResolver, mNMService, new SharedLog("test"));
141 verify(mNMService).registerTetheringStatsProvider(
142 mTetherStatsProviderCaptor.capture(), anyString());
143 return offload;
144 }
145
Erik Kline92c4db02017-05-31 10:21:32 +0900146 @Test
Lorenzo Colitti5a7dea12017-07-12 15:48:07 +0900147 public void testNoSettingsValueDefaultDisabledDoesNotStart() throws Exception {
Erik Kline92c4db02017-05-31 10:21:32 +0900148 setupFunctioningHardwareInterface();
Erik Klinec87cd412017-07-07 17:38:30 +0900149 when(mHardware.getDefaultTetherOffloadDisabled()).thenReturn(1);
Erik Kline92c4db02017-05-31 10:21:32 +0900150 try {
151 Settings.Global.getInt(mContentResolver, TETHER_OFFLOAD_DISABLED);
152 fail();
153 } catch (SettingNotFoundException expected) {}
154
Lorenzo Colitti5a7dea12017-07-12 15:48:07 +0900155 final OffloadController offload = makeOffloadController();
Erik Kline92c4db02017-05-31 10:21:32 +0900156 offload.start();
157
158 final InOrder inOrder = inOrder(mHardware);
Erik Klinec87cd412017-07-07 17:38:30 +0900159 inOrder.verify(mHardware, times(1)).getDefaultTetherOffloadDisabled();
160 inOrder.verify(mHardware, never()).initOffloadConfig();
161 inOrder.verify(mHardware, never()).initOffloadControl(
162 any(OffloadHardwareInterface.ControlCallback.class));
163 inOrder.verifyNoMoreInteractions();
164 }
165
166 @Test
Lorenzo Colitti5a7dea12017-07-12 15:48:07 +0900167 public void testNoSettingsValueDefaultEnabledDoesStart() throws Exception {
Erik Klinec87cd412017-07-07 17:38:30 +0900168 setupFunctioningHardwareInterface();
169 when(mHardware.getDefaultTetherOffloadDisabled()).thenReturn(0);
170 try {
171 Settings.Global.getInt(mContentResolver, TETHER_OFFLOAD_DISABLED);
172 fail();
173 } catch (SettingNotFoundException expected) {}
174
Lorenzo Colitti5a7dea12017-07-12 15:48:07 +0900175 final OffloadController offload = makeOffloadController();
Erik Klinec87cd412017-07-07 17:38:30 +0900176 offload.start();
177
178 final InOrder inOrder = inOrder(mHardware);
179 inOrder.verify(mHardware, times(1)).getDefaultTetherOffloadDisabled();
Erik Kline92c4db02017-05-31 10:21:32 +0900180 inOrder.verify(mHardware, times(1)).initOffloadConfig();
181 inOrder.verify(mHardware, times(1)).initOffloadControl(
182 any(OffloadHardwareInterface.ControlCallback.class));
183 inOrder.verifyNoMoreInteractions();
184 }
185
186 @Test
Lorenzo Colitti5a7dea12017-07-12 15:48:07 +0900187 public void testSettingsAllowsStart() throws Exception {
Erik Kline92c4db02017-05-31 10:21:32 +0900188 setupFunctioningHardwareInterface();
189 Settings.Global.putInt(mContentResolver, TETHER_OFFLOAD_DISABLED, 0);
190
Lorenzo Colitti5a7dea12017-07-12 15:48:07 +0900191 final OffloadController offload = makeOffloadController();
Erik Kline92c4db02017-05-31 10:21:32 +0900192 offload.start();
193
194 final InOrder inOrder = inOrder(mHardware);
Erik Klinec87cd412017-07-07 17:38:30 +0900195 inOrder.verify(mHardware, times(1)).getDefaultTetherOffloadDisabled();
Erik Kline92c4db02017-05-31 10:21:32 +0900196 inOrder.verify(mHardware, times(1)).initOffloadConfig();
197 inOrder.verify(mHardware, times(1)).initOffloadControl(
198 any(OffloadHardwareInterface.ControlCallback.class));
199 inOrder.verifyNoMoreInteractions();
200 }
201
202 @Test
Lorenzo Colitti5a7dea12017-07-12 15:48:07 +0900203 public void testSettingsDisablesStart() throws Exception {
Erik Kline92c4db02017-05-31 10:21:32 +0900204 setupFunctioningHardwareInterface();
205 Settings.Global.putInt(mContentResolver, TETHER_OFFLOAD_DISABLED, 1);
206
Lorenzo Colitti5a7dea12017-07-12 15:48:07 +0900207 final OffloadController offload = makeOffloadController();
Erik Kline92c4db02017-05-31 10:21:32 +0900208 offload.start();
209
210 final InOrder inOrder = inOrder(mHardware);
Erik Klinec87cd412017-07-07 17:38:30 +0900211 inOrder.verify(mHardware, times(1)).getDefaultTetherOffloadDisabled();
Erik Kline92c4db02017-05-31 10:21:32 +0900212 inOrder.verify(mHardware, never()).initOffloadConfig();
213 inOrder.verify(mHardware, never()).initOffloadControl(anyObject());
214 inOrder.verifyNoMoreInteractions();
215 }
Erik Kline7990aef2017-06-01 20:11:25 +0900216
217 @Test
218 public void testSetUpstreamLinkPropertiesWorking() throws Exception {
219 setupFunctioningHardwareInterface();
Erik Klinec87cd412017-07-07 17:38:30 +0900220 enableOffload();
221
Lorenzo Colitti5a7dea12017-07-12 15:48:07 +0900222 final OffloadController offload = makeOffloadController();
Erik Kline7990aef2017-06-01 20:11:25 +0900223 offload.start();
224
225 final InOrder inOrder = inOrder(mHardware);
Erik Klinec87cd412017-07-07 17:38:30 +0900226 inOrder.verify(mHardware, times(1)).getDefaultTetherOffloadDisabled();
Erik Kline7990aef2017-06-01 20:11:25 +0900227 inOrder.verify(mHardware, times(1)).initOffloadConfig();
228 inOrder.verify(mHardware, times(1)).initOffloadControl(
229 any(OffloadHardwareInterface.ControlCallback.class));
230 inOrder.verifyNoMoreInteractions();
231
Erik Kline32179ff2017-07-04 18:28:11 +0900232 // In reality, the UpstreamNetworkMonitor would have passed down to us
233 // a covering set of local prefixes representing a minimum essential
234 // set plus all the prefixes on networks with network agents.
235 //
236 // We simulate that there, and then add upstream elements one by one
237 // and watch what happens.
238 final Set<IpPrefix> minimumLocalPrefixes = new HashSet<>();
239 for (String s : new String[]{
240 "127.0.0.0/8", "192.0.2.0/24", "fe80::/64", "2001:db8::/64"}) {
241 minimumLocalPrefixes.add(new IpPrefix(s));
242 }
243 offload.setLocalPrefixes(minimumLocalPrefixes);
244 inOrder.verify(mHardware, times(1)).setLocalPrefixes(mStringArrayCaptor.capture());
245 ArrayList<String> localPrefixes = mStringArrayCaptor.getValue();
246 assertEquals(4, localPrefixes.size());
Erik Klineb3bb26e2017-07-06 19:49:35 +0900247 assertArrayListContains(localPrefixes,
248 "127.0.0.0/8", "192.0.2.0/24", "fe80::/64", "2001:db8::/64");
Erik Kline7990aef2017-06-01 20:11:25 +0900249 inOrder.verifyNoMoreInteractions();
Erik Kline32179ff2017-07-04 18:28:11 +0900250
251 offload.setUpstreamLinkProperties(null);
252 // No change in local addresses means no call to setLocalPrefixes().
253 inOrder.verify(mHardware, never()).setLocalPrefixes(mStringArrayCaptor.capture());
254 // This LinkProperties value does not differ from the default upstream.
255 // There should be no extraneous call to setUpstreamParameters().
256 inOrder.verify(mHardware, never()).setUpstreamParameters(
257 anyObject(), anyObject(), anyObject(), anyObject());
258 inOrder.verifyNoMoreInteractions();
Erik Kline7990aef2017-06-01 20:11:25 +0900259
260 final LinkProperties lp = new LinkProperties();
261
262 final String testIfName = "rmnet_data17";
263 lp.setInterfaceName(testIfName);
264 offload.setUpstreamLinkProperties(lp);
Erik Kline32179ff2017-07-04 18:28:11 +0900265 // No change in local addresses means no call to setLocalPrefixes().
266 inOrder.verify(mHardware, never()).setLocalPrefixes(mStringArrayCaptor.capture());
Erik Kline7990aef2017-06-01 20:11:25 +0900267 inOrder.verify(mHardware, times(1)).setUpstreamParameters(
Erik Kline11854592017-06-15 18:06:34 +0900268 eq(testIfName), eq(null), eq(null), eq(null));
Lorenzo Colittiddce7ee2017-08-21 12:34:50 +0900269 inOrder.verify(mHardware, times(1)).setDataLimit(eq(testIfName), eq(Long.MAX_VALUE));
Erik Kline7990aef2017-06-01 20:11:25 +0900270 inOrder.verifyNoMoreInteractions();
271
272 final String ipv4Addr = "192.0.2.5";
273 final String linkAddr = ipv4Addr + "/24";
274 lp.addLinkAddress(new LinkAddress(linkAddr));
Erik Kline32179ff2017-07-04 18:28:11 +0900275 lp.addRoute(new RouteInfo(new IpPrefix("192.0.2.0/24")));
Erik Kline7990aef2017-06-01 20:11:25 +0900276 offload.setUpstreamLinkProperties(lp);
Erik Kline32179ff2017-07-04 18:28:11 +0900277 // IPv4 prefixes and addresses on the upstream are simply left as whole
278 // prefixes (already passed in from UpstreamNetworkMonitor code). If a
279 // tethering client sends traffic to the IPv4 default router or other
280 // clients on the upstream this will not be hardware-forwarded, and that
281 // should be fine for now. Ergo: no change in local addresses, no call
282 // to setLocalPrefixes().
283 inOrder.verify(mHardware, never()).setLocalPrefixes(mStringArrayCaptor.capture());
Erik Kline7990aef2017-06-01 20:11:25 +0900284 inOrder.verify(mHardware, times(1)).setUpstreamParameters(
Erik Kline11854592017-06-15 18:06:34 +0900285 eq(testIfName), eq(ipv4Addr), eq(null), eq(null));
Lorenzo Colitti5a7dea12017-07-12 15:48:07 +0900286 inOrder.verify(mHardware, times(1)).getForwardedStats(eq(testIfName));
Lorenzo Colittiddce7ee2017-08-21 12:34:50 +0900287 inOrder.verify(mHardware, times(1)).setDataLimit(eq(testIfName), eq(Long.MAX_VALUE));
Erik Kline7990aef2017-06-01 20:11:25 +0900288 inOrder.verifyNoMoreInteractions();
289
290 final String ipv4Gateway = "192.0.2.1";
291 lp.addRoute(new RouteInfo(InetAddress.getByName(ipv4Gateway)));
292 offload.setUpstreamLinkProperties(lp);
Erik Kline32179ff2017-07-04 18:28:11 +0900293 // No change in local addresses means no call to setLocalPrefixes().
294 inOrder.verify(mHardware, never()).setLocalPrefixes(mStringArrayCaptor.capture());
Erik Kline7990aef2017-06-01 20:11:25 +0900295 inOrder.verify(mHardware, times(1)).setUpstreamParameters(
Erik Kline11854592017-06-15 18:06:34 +0900296 eq(testIfName), eq(ipv4Addr), eq(ipv4Gateway), eq(null));
Lorenzo Colitti5a7dea12017-07-12 15:48:07 +0900297 inOrder.verify(mHardware, times(1)).getForwardedStats(eq(testIfName));
Lorenzo Colittiddce7ee2017-08-21 12:34:50 +0900298 inOrder.verify(mHardware, times(1)).setDataLimit(eq(testIfName), eq(Long.MAX_VALUE));
Erik Kline7990aef2017-06-01 20:11:25 +0900299 inOrder.verifyNoMoreInteractions();
300
301 final String ipv6Gw1 = "fe80::cafe";
302 lp.addRoute(new RouteInfo(InetAddress.getByName(ipv6Gw1)));
303 offload.setUpstreamLinkProperties(lp);
Erik Kline32179ff2017-07-04 18:28:11 +0900304 // No change in local addresses means no call to setLocalPrefixes().
305 inOrder.verify(mHardware, never()).setLocalPrefixes(mStringArrayCaptor.capture());
Erik Kline7990aef2017-06-01 20:11:25 +0900306 inOrder.verify(mHardware, times(1)).setUpstreamParameters(
307 eq(testIfName), eq(ipv4Addr), eq(ipv4Gateway), mStringArrayCaptor.capture());
Lorenzo Colitti5a7dea12017-07-12 15:48:07 +0900308 inOrder.verify(mHardware, times(1)).getForwardedStats(eq(testIfName));
Erik Kline7990aef2017-06-01 20:11:25 +0900309 ArrayList<String> v6gws = mStringArrayCaptor.getValue();
310 assertEquals(1, v6gws.size());
311 assertTrue(v6gws.contains(ipv6Gw1));
Lorenzo Colittiddce7ee2017-08-21 12:34:50 +0900312 inOrder.verify(mHardware, times(1)).setDataLimit(eq(testIfName), eq(Long.MAX_VALUE));
Erik Kline7990aef2017-06-01 20:11:25 +0900313 inOrder.verifyNoMoreInteractions();
314
315 final String ipv6Gw2 = "fe80::d00d";
316 lp.addRoute(new RouteInfo(InetAddress.getByName(ipv6Gw2)));
317 offload.setUpstreamLinkProperties(lp);
Erik Kline32179ff2017-07-04 18:28:11 +0900318 // No change in local addresses means no call to setLocalPrefixes().
319 inOrder.verify(mHardware, never()).setLocalPrefixes(mStringArrayCaptor.capture());
Erik Kline7990aef2017-06-01 20:11:25 +0900320 inOrder.verify(mHardware, times(1)).setUpstreamParameters(
321 eq(testIfName), eq(ipv4Addr), eq(ipv4Gateway), mStringArrayCaptor.capture());
Lorenzo Colitti5a7dea12017-07-12 15:48:07 +0900322 inOrder.verify(mHardware, times(1)).getForwardedStats(eq(testIfName));
Erik Kline7990aef2017-06-01 20:11:25 +0900323 v6gws = mStringArrayCaptor.getValue();
324 assertEquals(2, v6gws.size());
325 assertTrue(v6gws.contains(ipv6Gw1));
326 assertTrue(v6gws.contains(ipv6Gw2));
Lorenzo Colittiddce7ee2017-08-21 12:34:50 +0900327 inOrder.verify(mHardware, times(1)).setDataLimit(eq(testIfName), eq(Long.MAX_VALUE));
Erik Kline7990aef2017-06-01 20:11:25 +0900328 inOrder.verifyNoMoreInteractions();
329
330 final LinkProperties stacked = new LinkProperties();
331 stacked.setInterfaceName("stacked");
332 stacked.addLinkAddress(new LinkAddress("192.0.2.129/25"));
333 stacked.addRoute(new RouteInfo(InetAddress.getByName("192.0.2.254")));
334 stacked.addRoute(new RouteInfo(InetAddress.getByName("fe80::bad:f00")));
335 assertTrue(lp.addStackedLink(stacked));
336 offload.setUpstreamLinkProperties(lp);
Erik Kline32179ff2017-07-04 18:28:11 +0900337 // No change in local addresses means no call to setLocalPrefixes().
338 inOrder.verify(mHardware, never()).setLocalPrefixes(mStringArrayCaptor.capture());
Erik Kline7990aef2017-06-01 20:11:25 +0900339 inOrder.verify(mHardware, times(1)).setUpstreamParameters(
340 eq(testIfName), eq(ipv4Addr), eq(ipv4Gateway), mStringArrayCaptor.capture());
Lorenzo Colitti5a7dea12017-07-12 15:48:07 +0900341 inOrder.verify(mHardware, times(1)).getForwardedStats(eq(testIfName));
Erik Kline7990aef2017-06-01 20:11:25 +0900342 v6gws = mStringArrayCaptor.getValue();
343 assertEquals(2, v6gws.size());
344 assertTrue(v6gws.contains(ipv6Gw1));
345 assertTrue(v6gws.contains(ipv6Gw2));
Lorenzo Colittiddce7ee2017-08-21 12:34:50 +0900346 inOrder.verify(mHardware, times(1)).setDataLimit(eq(testIfName), eq(Long.MAX_VALUE));
Erik Kline7990aef2017-06-01 20:11:25 +0900347 inOrder.verifyNoMoreInteractions();
Erik Kline32179ff2017-07-04 18:28:11 +0900348
349 // Add in some IPv6 upstream info. When there is a tethered downstream
350 // making use of the IPv6 prefix we would expect to see the /64 route
351 // removed from "local prefixes" and /128s added for the upstream IPv6
352 // addresses. This is not yet implemented, and for now we simply
353 // expect to see these /128s.
354 lp.addRoute(new RouteInfo(new IpPrefix("2001:db8::/64")));
355 // "2001:db8::/64" plus "assigned" ASCII in hex
356 lp.addLinkAddress(new LinkAddress("2001:db8::6173:7369:676e:6564/64"));
357 // "2001:db8::/64" plus "random" ASCII in hex
358 lp.addLinkAddress(new LinkAddress("2001:db8::7261:6e64:6f6d/64"));
359 offload.setUpstreamLinkProperties(lp);
360 inOrder.verify(mHardware, times(1)).setLocalPrefixes(mStringArrayCaptor.capture());
361 localPrefixes = mStringArrayCaptor.getValue();
362 assertEquals(6, localPrefixes.size());
Erik Klineb3bb26e2017-07-06 19:49:35 +0900363 assertArrayListContains(localPrefixes,
364 "127.0.0.0/8", "192.0.2.0/24", "fe80::/64", "2001:db8::/64",
365 "2001:db8::6173:7369:676e:6564/128", "2001:db8::7261:6e64:6f6d/128");
Erik Kline32179ff2017-07-04 18:28:11 +0900366 // The relevant parts of the LinkProperties have not changed, but at the
367 // moment we do not de-dup upstream LinkProperties this carefully.
368 inOrder.verify(mHardware, times(1)).setUpstreamParameters(
369 eq(testIfName), eq(ipv4Addr), eq(ipv4Gateway), mStringArrayCaptor.capture());
370 v6gws = mStringArrayCaptor.getValue();
371 assertEquals(2, v6gws.size());
372 assertTrue(v6gws.contains(ipv6Gw1));
373 assertTrue(v6gws.contains(ipv6Gw2));
Lorenzo Colitti5a7dea12017-07-12 15:48:07 +0900374 inOrder.verify(mHardware, times(1)).getForwardedStats(eq(testIfName));
Lorenzo Colittiddce7ee2017-08-21 12:34:50 +0900375 inOrder.verify(mHardware, times(1)).setDataLimit(eq(testIfName), eq(Long.MAX_VALUE));
Erik Kline32179ff2017-07-04 18:28:11 +0900376 inOrder.verifyNoMoreInteractions();
377
378 // Completely identical LinkProperties updates are de-duped.
379 offload.setUpstreamLinkProperties(lp);
380 // This LinkProperties value does not differ from the default upstream.
381 // There should be no extraneous call to setUpstreamParameters().
382 inOrder.verify(mHardware, never()).setUpstreamParameters(
383 anyObject(), anyObject(), anyObject(), anyObject());
384 inOrder.verifyNoMoreInteractions();
Erik Kline7990aef2017-06-01 20:11:25 +0900385 }
Lorenzo Colitti5a7dea12017-07-12 15:48:07 +0900386
387 private void assertNetworkStats(String iface, ForwardedStats stats, NetworkStats.Entry entry) {
388 assertEquals(iface, entry.iface);
389 assertEquals(stats.rxBytes, entry.rxBytes);
390 assertEquals(stats.txBytes, entry.txBytes);
391 assertEquals(SET_DEFAULT, entry.set);
392 assertEquals(TAG_NONE, entry.tag);
Lorenzo Colitti5a7dea12017-07-12 15:48:07 +0900393 }
394
395 @Test
396 public void testGetForwardedStats() throws Exception {
397 setupFunctioningHardwareInterface();
398 enableOffload();
399
400 final OffloadController offload = makeOffloadController();
401 offload.start();
402
403 final String ethernetIface = "eth1";
404 final String mobileIface = "rmnet_data0";
405
406 ForwardedStats ethernetStats = new ForwardedStats();
407 ethernetStats.rxBytes = 12345;
408 ethernetStats.txBytes = 54321;
409
410 ForwardedStats mobileStats = new ForwardedStats();
411 mobileStats.rxBytes = 999;
412 mobileStats.txBytes = 99999;
413
414 when(mHardware.getForwardedStats(eq(ethernetIface))).thenReturn(ethernetStats);
415 when(mHardware.getForwardedStats(eq(mobileIface))).thenReturn(mobileStats);
416
Hugo Benichi752c1282017-08-22 13:57:41 +0900417 InOrder inOrder = inOrder(mHardware);
418
Lorenzo Colitti5a7dea12017-07-12 15:48:07 +0900419 final LinkProperties lp = new LinkProperties();
420 lp.setInterfaceName(ethernetIface);
421 offload.setUpstreamLinkProperties(lp);
Hugo Benichi752c1282017-08-22 13:57:41 +0900422 // Previous upstream was null, so no stats are fetched.
423 inOrder.verify(mHardware, never()).getForwardedStats(any());
Lorenzo Colitti5a7dea12017-07-12 15:48:07 +0900424
425 lp.setInterfaceName(mobileIface);
426 offload.setUpstreamLinkProperties(lp);
Hugo Benichi752c1282017-08-22 13:57:41 +0900427 // Expect that we fetch stats from the previous upstream.
428 inOrder.verify(mHardware, times(1)).getForwardedStats(eq(ethernetIface));
Lorenzo Colitti5a7dea12017-07-12 15:48:07 +0900429
430 lp.setInterfaceName(ethernetIface);
431 offload.setUpstreamLinkProperties(lp);
Hugo Benichi752c1282017-08-22 13:57:41 +0900432 // Expect that we fetch stats from the previous upstream.
433 inOrder.verify(mHardware, times(1)).getForwardedStats(eq(mobileIface));
Lorenzo Colitti5a7dea12017-07-12 15:48:07 +0900434
Hugo Benichi752c1282017-08-22 13:57:41 +0900435 ethernetStats = new ForwardedStats();
Lorenzo Colitti5a7dea12017-07-12 15:48:07 +0900436 ethernetStats.rxBytes = 100000;
437 ethernetStats.txBytes = 100000;
Hugo Benichi752c1282017-08-22 13:57:41 +0900438 when(mHardware.getForwardedStats(eq(ethernetIface))).thenReturn(ethernetStats);
Lorenzo Colitti5a7dea12017-07-12 15:48:07 +0900439 offload.setUpstreamLinkProperties(null);
Erik Klineaabdaa92017-08-31 21:09:45 +0900440 // Expect that we first clear the HAL's upstream parameters.
441 inOrder.verify(mHardware, times(1)).setUpstreamParameters(
442 eq(""), eq("0.0.0.0"), eq("0.0.0.0"), eq(null));
Hugo Benichi752c1282017-08-22 13:57:41 +0900443 // Expect that we fetch stats from the previous upstream.
444 inOrder.verify(mHardware, times(1)).getForwardedStats(eq(ethernetIface));
Lorenzo Colitti5a7dea12017-07-12 15:48:07 +0900445
Lorenzo Colittif1912ca2017-08-17 19:23:08 +0900446 ITetheringStatsProvider provider = mTetherStatsProviderCaptor.getValue();
447 NetworkStats stats = provider.getTetherStats(STATS_PER_IFACE);
448 NetworkStats perUidStats = provider.getTetherStats(STATS_PER_UID);
Hugo Benichi752c1282017-08-22 13:57:41 +0900449 waitForIdle();
450 // There is no current upstream, so no stats are fetched.
Lorenzo Colittif612b602017-08-24 12:40:07 +0900451 inOrder.verify(mHardware, never()).getForwardedStats(any());
Hugo Benichi752c1282017-08-22 13:57:41 +0900452 inOrder.verifyNoMoreInteractions();
Lorenzo Colittif1912ca2017-08-17 19:23:08 +0900453
Lorenzo Colitti5a7dea12017-07-12 15:48:07 +0900454 assertEquals(2, stats.size());
Lorenzo Colittif1912ca2017-08-17 19:23:08 +0900455 assertEquals(2, perUidStats.size());
Lorenzo Colitti5a7dea12017-07-12 15:48:07 +0900456
457 NetworkStats.Entry entry = null;
Lorenzo Colittif1912ca2017-08-17 19:23:08 +0900458 for (int i = 0; i < stats.size(); i++) {
459 assertEquals(UID_ALL, stats.getValues(i, entry).uid);
460 assertEquals(UID_TETHERING, perUidStats.getValues(i, entry).uid);
461 }
462
Lorenzo Colitti5a7dea12017-07-12 15:48:07 +0900463 int ethernetPosition = ethernetIface.equals(stats.getValues(0, entry).iface) ? 0 : 1;
464 int mobilePosition = 1 - ethernetPosition;
465
466 entry = stats.getValues(mobilePosition, entry);
467 assertNetworkStats(mobileIface, mobileStats, entry);
Lorenzo Colittif1912ca2017-08-17 19:23:08 +0900468 entry = perUidStats.getValues(mobilePosition, entry);
469 assertNetworkStats(mobileIface, mobileStats, entry);
Lorenzo Colitti5a7dea12017-07-12 15:48:07 +0900470
471 ethernetStats.rxBytes = 12345 + 100000;
472 ethernetStats.txBytes = 54321 + 100000;
473 entry = stats.getValues(ethernetPosition, entry);
474 assertNetworkStats(ethernetIface, ethernetStats, entry);
Lorenzo Colittif1912ca2017-08-17 19:23:08 +0900475 entry = perUidStats.getValues(ethernetPosition, entry);
476 assertNetworkStats(ethernetIface, ethernetStats, entry);
Lorenzo Colitti5a7dea12017-07-12 15:48:07 +0900477 }
Lorenzo Colitti50b60fc2017-08-11 13:47:49 +0900478
479 @Test
480 public void testSetInterfaceQuota() throws Exception {
481 setupFunctioningHardwareInterface();
482 enableOffload();
483
484 final OffloadController offload = makeOffloadController();
485 offload.start();
486
487 final String ethernetIface = "eth1";
488 final String mobileIface = "rmnet_data0";
489 final long ethernetLimit = 12345;
490 final long mobileLimit = 12345678;
491
492 final LinkProperties lp = new LinkProperties();
493 lp.setInterfaceName(ethernetIface);
494 offload.setUpstreamLinkProperties(lp);
495
496 ITetheringStatsProvider provider = mTetherStatsProviderCaptor.getValue();
497 final InOrder inOrder = inOrder(mHardware);
498 when(mHardware.setUpstreamParameters(any(), any(), any(), any())).thenReturn(true);
499 when(mHardware.setDataLimit(anyString(), anyLong())).thenReturn(true);
500
501 // Applying an interface quota to the current upstream immediately sends it to the hardware.
502 provider.setInterfaceQuota(ethernetIface, ethernetLimit);
503 waitForIdle();
504 inOrder.verify(mHardware).setDataLimit(ethernetIface, ethernetLimit);
505 inOrder.verifyNoMoreInteractions();
506
507 // Applying an interface quota to another upstream does not take any immediate action.
508 provider.setInterfaceQuota(mobileIface, mobileLimit);
509 waitForIdle();
510 inOrder.verify(mHardware, never()).setDataLimit(anyString(), anyLong());
511
512 // Switching to that upstream causes the quota to be applied if the parameters were applied
513 // correctly.
514 lp.setInterfaceName(mobileIface);
515 offload.setUpstreamLinkProperties(lp);
516 waitForIdle();
517 inOrder.verify(mHardware).setDataLimit(mobileIface, mobileLimit);
518
519 // Setting a limit of ITetheringStatsProvider.QUOTA_UNLIMITED causes the limit to be set
520 // to Long.MAX_VALUE.
521 provider.setInterfaceQuota(mobileIface, ITetheringStatsProvider.QUOTA_UNLIMITED);
522 waitForIdle();
523 inOrder.verify(mHardware).setDataLimit(mobileIface, Long.MAX_VALUE);
524
525 // If setting upstream parameters fails, then the data limit is not set.
526 when(mHardware.setUpstreamParameters(any(), any(), any(), any())).thenReturn(false);
527 lp.setInterfaceName(ethernetIface);
528 offload.setUpstreamLinkProperties(lp);
529 provider.setInterfaceQuota(mobileIface, mobileLimit);
530 waitForIdle();
531 inOrder.verify(mHardware, never()).setDataLimit(anyString(), anyLong());
532
533 // If setting the data limit fails while changing upstreams, offload is stopped.
534 when(mHardware.setUpstreamParameters(any(), any(), any(), any())).thenReturn(true);
535 when(mHardware.setDataLimit(anyString(), anyLong())).thenReturn(false);
536 lp.setInterfaceName(mobileIface);
537 offload.setUpstreamLinkProperties(lp);
538 provider.setInterfaceQuota(mobileIface, mobileLimit);
539 waitForIdle();
Lorenzo Colittiddce7ee2017-08-21 12:34:50 +0900540 inOrder.verify(mHardware).getForwardedStats(ethernetIface);
Lorenzo Colitti50b60fc2017-08-11 13:47:49 +0900541 inOrder.verify(mHardware).stopOffloadControl();
542 }
Lorenzo Colitti9f0baa92017-08-15 19:25:51 +0900543
544 @Test
545 public void testDataLimitCallback() throws Exception {
546 setupFunctioningHardwareInterface();
547 enableOffload();
548
549 final OffloadController offload = makeOffloadController();
550 offload.start();
551
552 OffloadHardwareInterface.ControlCallback callback = mControlCallbackCaptor.getValue();
553 callback.onStoppedLimitReached();
554 verify(mNMService, times(1)).tetherLimitReached(mTetherStatsProviderCaptor.getValue());
555 }
Erik Klineb3bb26e2017-07-06 19:49:35 +0900556
557 @Test
558 public void testAddRemoveDownstreams() throws Exception {
559 setupFunctioningHardwareInterface();
560 enableOffload();
561
562 final OffloadController offload = makeOffloadController();
563 offload.start();
564
565 final InOrder inOrder = inOrder(mHardware);
566 inOrder.verify(mHardware, times(1)).initOffloadConfig();
567 inOrder.verify(mHardware, times(1)).initOffloadControl(
568 any(OffloadHardwareInterface.ControlCallback.class));
569 inOrder.verifyNoMoreInteractions();
570
571 // Tethering makes several calls to setLocalPrefixes() before add/remove
572 // downstream calls are made. This is not tested here; only the behavior
573 // of notifyDownstreamLinkProperties() and removeDownstreamInterface()
574 // are tested.
575
576 // [1] USB tethering is started.
577 final LinkProperties usbLinkProperties = new LinkProperties();
578 usbLinkProperties.setInterfaceName(RNDIS0);
579 usbLinkProperties.addLinkAddress(new LinkAddress("192.168.42.1/24"));
580 usbLinkProperties.addRoute(new RouteInfo(new IpPrefix(USB_PREFIX)));
581 offload.notifyDownstreamLinkProperties(usbLinkProperties);
582 inOrder.verify(mHardware, times(1)).addDownstreamPrefix(RNDIS0, USB_PREFIX);
583 inOrder.verifyNoMoreInteractions();
584
585 // [2] Routes for IPv6 link-local prefixes should never be added.
586 usbLinkProperties.addRoute(new RouteInfo(new IpPrefix(IPV6_LINKLOCAL)));
587 offload.notifyDownstreamLinkProperties(usbLinkProperties);
588 inOrder.verify(mHardware, never()).addDownstreamPrefix(eq(RNDIS0), anyString());
589 inOrder.verifyNoMoreInteractions();
590
591 // [3] Add an IPv6 prefix for good measure. Only new offload-able
592 // prefixes should be passed to the HAL.
593 usbLinkProperties.addLinkAddress(new LinkAddress("2001:db8::1/64"));
594 usbLinkProperties.addRoute(new RouteInfo(new IpPrefix(IPV6_DOC_PREFIX)));
595 offload.notifyDownstreamLinkProperties(usbLinkProperties);
596 inOrder.verify(mHardware, times(1)).addDownstreamPrefix(RNDIS0, IPV6_DOC_PREFIX);
597 inOrder.verifyNoMoreInteractions();
598
599 // [4] Adding addresses doesn't affect notifyDownstreamLinkProperties().
600 // The address is passed in by a separate setLocalPrefixes() invocation.
601 usbLinkProperties.addLinkAddress(new LinkAddress("2001:db8::2/64"));
602 offload.notifyDownstreamLinkProperties(usbLinkProperties);
603 inOrder.verify(mHardware, never()).addDownstreamPrefix(eq(RNDIS0), anyString());
604
605 // [5] Differences in local routes are converted into addDownstream()
606 // and removeDownstream() invocations accordingly.
607 usbLinkProperties.removeRoute(new RouteInfo(new IpPrefix(IPV6_DOC_PREFIX), null, RNDIS0));
608 usbLinkProperties.addRoute(new RouteInfo(new IpPrefix(IPV6_DISCARD_PREFIX)));
609 offload.notifyDownstreamLinkProperties(usbLinkProperties);
610 inOrder.verify(mHardware, times(1)).removeDownstreamPrefix(RNDIS0, IPV6_DOC_PREFIX);
611 inOrder.verify(mHardware, times(1)).addDownstreamPrefix(RNDIS0, IPV6_DISCARD_PREFIX);
612 inOrder.verifyNoMoreInteractions();
613
614 // [6] Removing a downstream interface which was never added causes no
615 // interactions with the HAL.
616 offload.removeDownstreamInterface(WLAN0);
617 inOrder.verifyNoMoreInteractions();
618
619 // [7] Removing an active downstream removes all remaining prefixes.
620 offload.removeDownstreamInterface(RNDIS0);
621 inOrder.verify(mHardware, times(1)).removeDownstreamPrefix(RNDIS0, USB_PREFIX);
622 inOrder.verify(mHardware, times(1)).removeDownstreamPrefix(RNDIS0, IPV6_DISCARD_PREFIX);
623 inOrder.verifyNoMoreInteractions();
624 }
625
Erik Klineaabdaa92017-08-31 21:09:45 +0900626 @Test
627 public void testControlCallbackOnStoppedUnsupportedFetchesAllStats() throws Exception {
628 setupFunctioningHardwareInterface();
629 enableOffload();
630
631 final OffloadController offload = makeOffloadController();
632 offload.start();
633
634 // Pretend to set a few different upstreams (only the interface name
635 // matters for this test; we're ignoring IP and route information).
636 final LinkProperties upstreamLp = new LinkProperties();
637 for (String ifname : new String[]{RMNET0, WLAN0, RMNET0}) {
638 upstreamLp.setInterfaceName(ifname);
639 offload.setUpstreamLinkProperties(upstreamLp);
640 }
641
642 // Clear invocation history, especially the getForwardedStats() calls
643 // that happen with setUpstreamParameters().
644 clearInvocations(mHardware);
645
646 OffloadHardwareInterface.ControlCallback callback = mControlCallbackCaptor.getValue();
647 callback.onStoppedUnsupported();
648
649 // Verify forwarded stats behaviour.
650 verify(mHardware, times(1)).getForwardedStats(eq(RMNET0));
651 verify(mHardware, times(1)).getForwardedStats(eq(WLAN0));
652 verifyNoMoreInteractions(mHardware);
653 verify(mNMService, times(1)).tetherLimitReached(mTetherStatsProviderCaptor.getValue());
654 verifyNoMoreInteractions(mNMService);
655 }
656
657 @Test
658 public void testControlCallbackOnSupportAvailableFetchesAllStatsAndPushesAllParameters()
659 throws Exception {
660 setupFunctioningHardwareInterface();
661 enableOffload();
662
663 final OffloadController offload = makeOffloadController();
664 offload.start();
665
666 // Pretend to set a few different upstreams (only the interface name
667 // matters for this test; we're ignoring IP and route information).
668 final LinkProperties upstreamLp = new LinkProperties();
669 for (String ifname : new String[]{RMNET0, WLAN0, RMNET0}) {
670 upstreamLp.setInterfaceName(ifname);
671 offload.setUpstreamLinkProperties(upstreamLp);
672 }
673
Erik Klinebc8b2ee2017-09-19 17:56:10 +0900674 // Pretend that some local prefixes and downstreams have been added
675 // (and removed, for good measure).
676 final Set<IpPrefix> minimumLocalPrefixes = new HashSet<>();
677 for (String s : new String[]{
678 "127.0.0.0/8", "192.0.2.0/24", "fe80::/64", "2001:db8::/64"}) {
679 minimumLocalPrefixes.add(new IpPrefix(s));
680 }
681 offload.setLocalPrefixes(minimumLocalPrefixes);
682
683 final LinkProperties usbLinkProperties = new LinkProperties();
684 usbLinkProperties.setInterfaceName(RNDIS0);
685 usbLinkProperties.addLinkAddress(new LinkAddress("192.168.42.1/24"));
686 usbLinkProperties.addRoute(new RouteInfo(new IpPrefix(USB_PREFIX)));
687 offload.notifyDownstreamLinkProperties(usbLinkProperties);
688
689 final LinkProperties wifiLinkProperties = new LinkProperties();
690 wifiLinkProperties.setInterfaceName(WLAN0);
691 wifiLinkProperties.addLinkAddress(new LinkAddress("192.168.43.1/24"));
692 wifiLinkProperties.addRoute(new RouteInfo(new IpPrefix(WIFI_PREFIX)));
693 wifiLinkProperties.addRoute(new RouteInfo(new IpPrefix(IPV6_LINKLOCAL)));
694 // Use a benchmark prefix (RFC 5180 + erratum), since the documentation
695 // prefix is included in the excluded prefix list.
696 wifiLinkProperties.addLinkAddress(new LinkAddress("2001:2::1/64"));
697 wifiLinkProperties.addLinkAddress(new LinkAddress("2001:2::2/64"));
698 wifiLinkProperties.addRoute(new RouteInfo(new IpPrefix("2001:2::/64")));
699 offload.notifyDownstreamLinkProperties(wifiLinkProperties);
700
701 offload.removeDownstreamInterface(RNDIS0);
702
Erik Klineaabdaa92017-08-31 21:09:45 +0900703 // Clear invocation history, especially the getForwardedStats() calls
704 // that happen with setUpstreamParameters().
705 clearInvocations(mHardware);
706
707 OffloadHardwareInterface.ControlCallback callback = mControlCallbackCaptor.getValue();
708 callback.onSupportAvailable();
709
710 // Verify forwarded stats behaviour.
711 verify(mHardware, times(1)).getForwardedStats(eq(RMNET0));
712 verify(mHardware, times(1)).getForwardedStats(eq(WLAN0));
713 verify(mNMService, times(1)).tetherLimitReached(mTetherStatsProviderCaptor.getValue());
714 verifyNoMoreInteractions(mNMService);
715
716 // TODO: verify local prefixes and downstreams are also pushed to the HAL.
Erik Klinebc8b2ee2017-09-19 17:56:10 +0900717 verify(mHardware, times(1)).setLocalPrefixes(mStringArrayCaptor.capture());
718 ArrayList<String> localPrefixes = mStringArrayCaptor.getValue();
719 assertEquals(4, localPrefixes.size());
720 assertArrayListContains(localPrefixes,
721 // TODO: The logic to find and exclude downstream IP prefixes
722 // is currently in Tethering's OffloadWrapper but must be moved
723 // into OffloadController proper. After this, also check for:
724 // "192.168.43.1/32", "2001:2::1/128", "2001:2::2/128"
725 "127.0.0.0/8", "192.0.2.0/24", "fe80::/64", "2001:db8::/64");
726 verify(mHardware, times(1)).addDownstreamPrefix(WLAN0, "192.168.43.0/24");
727 verify(mHardware, times(1)).addDownstreamPrefix(WLAN0, "2001:2::/64");
Erik Klineaabdaa92017-08-31 21:09:45 +0900728 verify(mHardware, times(1)).setUpstreamParameters(eq(RMNET0), any(), any(), any());
729 verify(mHardware, times(1)).setDataLimit(eq(RMNET0), anyLong());
730 verifyNoMoreInteractions(mHardware);
731 }
732
Erik Klineb3bb26e2017-07-06 19:49:35 +0900733 private static void assertArrayListContains(ArrayList<String> list, String... elems) {
734 for (String element : elems) {
Erik Klinebc8b2ee2017-09-19 17:56:10 +0900735 assertTrue(element + " not in list", list.contains(element));
Erik Klineb3bb26e2017-07-06 19:49:35 +0900736 }
737 }
Erik Kline92c4db02017-05-31 10:21:32 +0900738}