blob: 7886ca6c132d911368aecb4a98c86edf33158bd4 [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;
markchien986750b2019-12-06 15:24:53 +080024import static android.net.RouteInfo.RTN_UNICAST;
Lorenzo Colitti5a7dea12017-07-12 15:48:07 +090025import static android.net.TrafficStats.UID_TETHERING;
Erik Kline92c4db02017-05-31 10:21:32 +090026import static android.provider.Settings.Global.TETHER_OFFLOAD_DISABLED;
Brett Chabot1ae2aa62019-03-04 14:14:56 -080027
Lorenzo Colitti5a7dea12017-07-12 15:48:07 +090028import static com.android.server.connectivity.tethering.OffloadHardwareInterface.ForwardedStats;
Chalard Jean9c0ff1b2019-05-30 17:11:14 +090029import static com.android.testutils.MiscAssertsKt.assertContainsAll;
markchien77c17be62019-09-25 14:33:39 +080030import static com.android.testutils.MiscAssertsKt.assertThrows;
Brett Chabot1ae2aa62019-03-04 14:14:56 -080031
Erik Kline7990aef2017-06-01 20:11:25 +090032import static org.junit.Assert.assertEquals;
33import static org.junit.Assert.assertTrue;
Erik Kline92c4db02017-05-31 10:21:32 +090034import static org.mockito.Matchers.any;
Lorenzo Colitti50b60fc2017-08-11 13:47:49 +090035import static org.mockito.Matchers.anyLong;
Erik Kline92c4db02017-05-31 10:21:32 +090036import static org.mockito.Matchers.anyObject;
Lorenzo Colitti5a7dea12017-07-12 15:48:07 +090037import static org.mockito.Matchers.anyString;
Erik Kline7990aef2017-06-01 20:11:25 +090038import static org.mockito.Matchers.eq;
Erik Klineaabdaa92017-08-31 21:09:45 +090039import static org.mockito.Mockito.clearInvocations;
Erik Kline92c4db02017-05-31 10:21:32 +090040import static org.mockito.Mockito.inOrder;
41import static org.mockito.Mockito.never;
42import static org.mockito.Mockito.times;
Lorenzo Colitti5a7dea12017-07-12 15:48:07 +090043import static org.mockito.Mockito.verify;
Erik Klineaabdaa92017-08-31 21:09:45 +090044import static org.mockito.Mockito.verifyNoMoreInteractions;
Erik Kline92c4db02017-05-31 10:21:32 +090045import static org.mockito.Mockito.when;
46
47import android.content.Context;
Erik Klinef3a08b42017-06-07 16:33:19 +090048import android.content.pm.ApplicationInfo;
Lorenzo Colitti5a7dea12017-07-12 15:48:07 +090049import android.net.ITetheringStatsProvider;
Erik Kline32179ff2017-07-04 18:28:11 +090050import android.net.IpPrefix;
Erik Kline7990aef2017-06-01 20:11:25 +090051import android.net.LinkAddress;
52import android.net.LinkProperties;
Lorenzo Colitti5a7dea12017-07-12 15:48:07 +090053import android.net.NetworkStats;
Erik Kline7990aef2017-06-01 20:11:25 +090054import android.net.RouteInfo;
Erik Kline92c4db02017-05-31 10:21:32 +090055import android.net.util.SharedLog;
Lorenzo Colitti5a7dea12017-07-12 15:48:07 +090056import android.os.Handler;
Lorenzo Colitti5a7dea12017-07-12 15:48:07 +090057import android.os.INetworkManagementService;
Brett Chabot1ae2aa62019-03-04 14:14:56 -080058import android.os.Looper;
Erik Kline92c4db02017-05-31 10:21:32 +090059import android.provider.Settings;
60import android.provider.Settings.SettingNotFoundException;
Erik Kline92c4db02017-05-31 10:21:32 +090061import android.test.mock.MockContentResolver;
Erik Kline92c4db02017-05-31 10:21:32 +090062
Brett Chabot1ae2aa62019-03-04 14:14:56 -080063import androidx.test.filters.SmallTest;
64import androidx.test.runner.AndroidJUnit4;
65
66import com.android.internal.util.test.FakeSettingsProvider;
Chalard Jeance75e0e2019-05-28 16:48:32 +090067import com.android.testutils.HandlerUtilsKt;
Erik Kline7990aef2017-06-01 20:11:25 +090068
Erik Klinec87cd412017-07-07 17:38:30 +090069import org.junit.After;
Erik Kline92c4db02017-05-31 10:21:32 +090070import org.junit.Before;
Erik Kline92c4db02017-05-31 10:21:32 +090071import org.junit.Test;
Brett Chabot1ae2aa62019-03-04 14:14:56 -080072import org.junit.runner.RunWith;
Erik Kline7990aef2017-06-01 20:11:25 +090073import org.mockito.ArgumentCaptor;
Erik Kline92c4db02017-05-31 10:21:32 +090074import org.mockito.InOrder;
75import org.mockito.Mock;
76import org.mockito.MockitoAnnotations;
77
Brett Chabot1ae2aa62019-03-04 14:14:56 -080078import java.net.InetAddress;
79import java.util.ArrayList;
80import java.util.HashSet;
81import java.util.Set;
Erik Kline92c4db02017-05-31 10:21:32 +090082
83@RunWith(AndroidJUnit4.class)
84@SmallTest
85public class OffloadControllerTest {
Erik Klineb3bb26e2017-07-06 19:49:35 +090086 private static final String RNDIS0 = "test_rndis0";
87 private static final String RMNET0 = "test_rmnet_data0";
88 private static final String WLAN0 = "test_wlan0";
89
90 private static final String IPV6_LINKLOCAL = "fe80::/64";
91 private static final String IPV6_DOC_PREFIX = "2001:db8::/64";
92 private static final String IPV6_DISCARD_PREFIX = "100::/64";
93 private static final String USB_PREFIX = "192.168.42.0/24";
94 private static final String WIFI_PREFIX = "192.168.43.0/24";
Chalard Jeance75e0e2019-05-28 16:48:32 +090095 private static final long WAIT_FOR_IDLE_TIMEOUT = 2 * 1000;
Erik Kline92c4db02017-05-31 10:21:32 +090096
97 @Mock private OffloadHardwareInterface mHardware;
Erik Klinef3a08b42017-06-07 16:33:19 +090098 @Mock private ApplicationInfo mApplicationInfo;
Erik Kline92c4db02017-05-31 10:21:32 +090099 @Mock private Context mContext;
Lorenzo Colitti5a7dea12017-07-12 15:48:07 +0900100 @Mock private INetworkManagementService mNMService;
Erik Kline7fd696c2017-06-12 18:20:08 +0900101 private final ArgumentCaptor<ArrayList> mStringArrayCaptor =
102 ArgumentCaptor.forClass(ArrayList.class);
Lorenzo Colitti5a7dea12017-07-12 15:48:07 +0900103 private final ArgumentCaptor<ITetheringStatsProvider.Stub> mTetherStatsProviderCaptor =
104 ArgumentCaptor.forClass(ITetheringStatsProvider.Stub.class);
Lorenzo Colitti9f0baa92017-08-15 19:25:51 +0900105 private final ArgumentCaptor<OffloadHardwareInterface.ControlCallback> mControlCallbackCaptor =
106 ArgumentCaptor.forClass(OffloadHardwareInterface.ControlCallback.class);
Erik Kline92c4db02017-05-31 10:21:32 +0900107 private MockContentResolver mContentResolver;
108
Lorenzo Colitti5a7dea12017-07-12 15:48:07 +0900109 @Before public void setUp() {
Erik Kline92c4db02017-05-31 10:21:32 +0900110 MockitoAnnotations.initMocks(this);
Erik Klinef3a08b42017-06-07 16:33:19 +0900111 when(mContext.getApplicationInfo()).thenReturn(mApplicationInfo);
112 when(mContext.getPackageName()).thenReturn("OffloadControllerTest");
Erik Kline92c4db02017-05-31 10:21:32 +0900113 mContentResolver = new MockContentResolver(mContext);
114 mContentResolver.addProvider(Settings.AUTHORITY, new FakeSettingsProvider());
Erik Kline92c4db02017-05-31 10:21:32 +0900115 when(mContext.getContentResolver()).thenReturn(mContentResolver);
Erik Klinec87cd412017-07-07 17:38:30 +0900116 FakeSettingsProvider.clearSettingsProvider();
117 }
118
119 @After public void tearDown() throws Exception {
120 FakeSettingsProvider.clearSettingsProvider();
Erik Kline92c4db02017-05-31 10:21:32 +0900121 }
122
123 private void setupFunctioningHardwareInterface() {
124 when(mHardware.initOffloadConfig()).thenReturn(true);
Lorenzo Colitti9f0baa92017-08-15 19:25:51 +0900125 when(mHardware.initOffloadControl(mControlCallbackCaptor.capture()))
Erik Kline92c4db02017-05-31 10:21:32 +0900126 .thenReturn(true);
Lorenzo Colittiddce7ee2017-08-21 12:34:50 +0900127 when(mHardware.setUpstreamParameters(anyString(), any(), any(), any())).thenReturn(true);
Lorenzo Colitti5a7dea12017-07-12 15:48:07 +0900128 when(mHardware.getForwardedStats(any())).thenReturn(new ForwardedStats());
Lorenzo Colittiddce7ee2017-08-21 12:34:50 +0900129 when(mHardware.setDataLimit(anyString(), anyLong())).thenReturn(true);
Erik Kline92c4db02017-05-31 10:21:32 +0900130 }
131
Erik Klinec87cd412017-07-07 17:38:30 +0900132 private void enableOffload() {
133 Settings.Global.putInt(mContentResolver, TETHER_OFFLOAD_DISABLED, 0);
134 }
135
Lorenzo Colitti50b60fc2017-08-11 13:47:49 +0900136 private void waitForIdle() {
Chalard Jeance75e0e2019-05-28 16:48:32 +0900137 HandlerUtilsKt.waitForIdle(new Handler(Looper.getMainLooper()), WAIT_FOR_IDLE_TIMEOUT);
Lorenzo Colitti50b60fc2017-08-11 13:47:49 +0900138 }
139
Lorenzo Colitti5a7dea12017-07-12 15:48:07 +0900140 private OffloadController makeOffloadController() throws Exception {
141 OffloadController offload = new OffloadController(new Handler(Looper.getMainLooper()),
142 mHardware, mContentResolver, mNMService, new SharedLog("test"));
143 verify(mNMService).registerTetheringStatsProvider(
144 mTetherStatsProviderCaptor.capture(), anyString());
145 return offload;
146 }
147
Erik Kline92c4db02017-05-31 10:21:32 +0900148 @Test
Lorenzo Colitti5a7dea12017-07-12 15:48:07 +0900149 public void testNoSettingsValueDefaultDisabledDoesNotStart() throws Exception {
Erik Kline92c4db02017-05-31 10:21:32 +0900150 setupFunctioningHardwareInterface();
Erik Klinec87cd412017-07-07 17:38:30 +0900151 when(mHardware.getDefaultTetherOffloadDisabled()).thenReturn(1);
markchien77c17be62019-09-25 14:33:39 +0800152 assertThrows(SettingNotFoundException.class, () ->
153 Settings.Global.getInt(mContentResolver, TETHER_OFFLOAD_DISABLED));
Erik Kline92c4db02017-05-31 10:21:32 +0900154
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);
markchien77c17be62019-09-25 14:33:39 +0800170 assertThrows(SettingNotFoundException.class, () ->
171 Settings.Global.getInt(mContentResolver, TETHER_OFFLOAD_DISABLED));
Erik Klinec87cd412017-07-07 17:38:30 +0900172
Lorenzo Colitti5a7dea12017-07-12 15:48:07 +0900173 final OffloadController offload = makeOffloadController();
Erik Klinec87cd412017-07-07 17:38:30 +0900174 offload.start();
175
176 final InOrder inOrder = inOrder(mHardware);
177 inOrder.verify(mHardware, times(1)).getDefaultTetherOffloadDisabled();
Erik Kline92c4db02017-05-31 10:21:32 +0900178 inOrder.verify(mHardware, times(1)).initOffloadConfig();
179 inOrder.verify(mHardware, times(1)).initOffloadControl(
180 any(OffloadHardwareInterface.ControlCallback.class));
181 inOrder.verifyNoMoreInteractions();
182 }
183
184 @Test
Lorenzo Colitti5a7dea12017-07-12 15:48:07 +0900185 public void testSettingsAllowsStart() throws Exception {
Erik Kline92c4db02017-05-31 10:21:32 +0900186 setupFunctioningHardwareInterface();
187 Settings.Global.putInt(mContentResolver, TETHER_OFFLOAD_DISABLED, 0);
188
Lorenzo Colitti5a7dea12017-07-12 15:48:07 +0900189 final OffloadController offload = makeOffloadController();
Erik Kline92c4db02017-05-31 10:21:32 +0900190 offload.start();
191
192 final InOrder inOrder = inOrder(mHardware);
Erik Klinec87cd412017-07-07 17:38:30 +0900193 inOrder.verify(mHardware, times(1)).getDefaultTetherOffloadDisabled();
Erik Kline92c4db02017-05-31 10:21:32 +0900194 inOrder.verify(mHardware, times(1)).initOffloadConfig();
195 inOrder.verify(mHardware, times(1)).initOffloadControl(
196 any(OffloadHardwareInterface.ControlCallback.class));
197 inOrder.verifyNoMoreInteractions();
198 }
199
200 @Test
Lorenzo Colitti5a7dea12017-07-12 15:48:07 +0900201 public void testSettingsDisablesStart() throws Exception {
Erik Kline92c4db02017-05-31 10:21:32 +0900202 setupFunctioningHardwareInterface();
203 Settings.Global.putInt(mContentResolver, TETHER_OFFLOAD_DISABLED, 1);
204
Lorenzo Colitti5a7dea12017-07-12 15:48:07 +0900205 final OffloadController offload = makeOffloadController();
Erik Kline92c4db02017-05-31 10:21:32 +0900206 offload.start();
207
208 final InOrder inOrder = inOrder(mHardware);
Erik Klinec87cd412017-07-07 17:38:30 +0900209 inOrder.verify(mHardware, times(1)).getDefaultTetherOffloadDisabled();
Erik Kline92c4db02017-05-31 10:21:32 +0900210 inOrder.verify(mHardware, never()).initOffloadConfig();
211 inOrder.verify(mHardware, never()).initOffloadControl(anyObject());
212 inOrder.verifyNoMoreInteractions();
213 }
Erik Kline7990aef2017-06-01 20:11:25 +0900214
215 @Test
216 public void testSetUpstreamLinkPropertiesWorking() throws Exception {
217 setupFunctioningHardwareInterface();
Erik Klinec87cd412017-07-07 17:38:30 +0900218 enableOffload();
219
Lorenzo Colitti5a7dea12017-07-12 15:48:07 +0900220 final OffloadController offload = makeOffloadController();
Erik Kline7990aef2017-06-01 20:11:25 +0900221 offload.start();
222
223 final InOrder inOrder = inOrder(mHardware);
Erik Klinec87cd412017-07-07 17:38:30 +0900224 inOrder.verify(mHardware, times(1)).getDefaultTetherOffloadDisabled();
Erik Kline7990aef2017-06-01 20:11:25 +0900225 inOrder.verify(mHardware, times(1)).initOffloadConfig();
226 inOrder.verify(mHardware, times(1)).initOffloadControl(
227 any(OffloadHardwareInterface.ControlCallback.class));
228 inOrder.verifyNoMoreInteractions();
229
Erik Kline32179ff2017-07-04 18:28:11 +0900230 // In reality, the UpstreamNetworkMonitor would have passed down to us
231 // a covering set of local prefixes representing a minimum essential
232 // set plus all the prefixes on networks with network agents.
233 //
234 // We simulate that there, and then add upstream elements one by one
235 // and watch what happens.
236 final Set<IpPrefix> minimumLocalPrefixes = new HashSet<>();
237 for (String s : new String[]{
238 "127.0.0.0/8", "192.0.2.0/24", "fe80::/64", "2001:db8::/64"}) {
239 minimumLocalPrefixes.add(new IpPrefix(s));
240 }
241 offload.setLocalPrefixes(minimumLocalPrefixes);
242 inOrder.verify(mHardware, times(1)).setLocalPrefixes(mStringArrayCaptor.capture());
243 ArrayList<String> localPrefixes = mStringArrayCaptor.getValue();
244 assertEquals(4, localPrefixes.size());
Chalard Jean9c0ff1b2019-05-30 17:11:14 +0900245 assertContainsAll(localPrefixes,
Erik Klineb3bb26e2017-07-06 19:49:35 +0900246 "127.0.0.0/8", "192.0.2.0/24", "fe80::/64", "2001:db8::/64");
Erik Kline7990aef2017-06-01 20:11:25 +0900247 inOrder.verifyNoMoreInteractions();
Erik Kline32179ff2017-07-04 18:28:11 +0900248
249 offload.setUpstreamLinkProperties(null);
250 // No change in local addresses means no call to setLocalPrefixes().
251 inOrder.verify(mHardware, never()).setLocalPrefixes(mStringArrayCaptor.capture());
252 // This LinkProperties value does not differ from the default upstream.
253 // There should be no extraneous call to setUpstreamParameters().
254 inOrder.verify(mHardware, never()).setUpstreamParameters(
255 anyObject(), anyObject(), anyObject(), anyObject());
256 inOrder.verifyNoMoreInteractions();
Erik Kline7990aef2017-06-01 20:11:25 +0900257
258 final LinkProperties lp = new LinkProperties();
259
260 final String testIfName = "rmnet_data17";
261 lp.setInterfaceName(testIfName);
262 offload.setUpstreamLinkProperties(lp);
Erik Kline32179ff2017-07-04 18:28:11 +0900263 // No change in local addresses means no call to setLocalPrefixes().
264 inOrder.verify(mHardware, never()).setLocalPrefixes(mStringArrayCaptor.capture());
Erik Kline7990aef2017-06-01 20:11:25 +0900265 inOrder.verify(mHardware, times(1)).setUpstreamParameters(
Erik Kline11854592017-06-15 18:06:34 +0900266 eq(testIfName), eq(null), eq(null), eq(null));
Lorenzo Colittiddce7ee2017-08-21 12:34:50 +0900267 inOrder.verify(mHardware, times(1)).setDataLimit(eq(testIfName), eq(Long.MAX_VALUE));
Erik Kline7990aef2017-06-01 20:11:25 +0900268 inOrder.verifyNoMoreInteractions();
269
270 final String ipv4Addr = "192.0.2.5";
271 final String linkAddr = ipv4Addr + "/24";
272 lp.addLinkAddress(new LinkAddress(linkAddr));
markchien986750b2019-12-06 15:24:53 +0800273 lp.addRoute(new RouteInfo(new IpPrefix("192.0.2.0/24"), null, null, RTN_UNICAST));
Erik Kline7990aef2017-06-01 20:11:25 +0900274 offload.setUpstreamLinkProperties(lp);
Erik Kline32179ff2017-07-04 18:28:11 +0900275 // IPv4 prefixes and addresses on the upstream are simply left as whole
276 // prefixes (already passed in from UpstreamNetworkMonitor code). If a
277 // tethering client sends traffic to the IPv4 default router or other
278 // clients on the upstream this will not be hardware-forwarded, and that
279 // should be fine for now. Ergo: no change in local addresses, no call
280 // to setLocalPrefixes().
281 inOrder.verify(mHardware, never()).setLocalPrefixes(mStringArrayCaptor.capture());
Erik Kline7990aef2017-06-01 20:11:25 +0900282 inOrder.verify(mHardware, times(1)).setUpstreamParameters(
Erik Kline11854592017-06-15 18:06:34 +0900283 eq(testIfName), eq(ipv4Addr), eq(null), eq(null));
Lorenzo Colitti5a7dea12017-07-12 15:48:07 +0900284 inOrder.verify(mHardware, times(1)).getForwardedStats(eq(testIfName));
Lorenzo Colittiddce7ee2017-08-21 12:34:50 +0900285 inOrder.verify(mHardware, times(1)).setDataLimit(eq(testIfName), eq(Long.MAX_VALUE));
Erik Kline7990aef2017-06-01 20:11:25 +0900286 inOrder.verifyNoMoreInteractions();
287
288 final String ipv4Gateway = "192.0.2.1";
markchien986750b2019-12-06 15:24:53 +0800289 lp.addRoute(new RouteInfo(null, InetAddress.getByName(ipv4Gateway), null, RTN_UNICAST));
Erik Kline7990aef2017-06-01 20:11:25 +0900290 offload.setUpstreamLinkProperties(lp);
Erik Kline32179ff2017-07-04 18:28:11 +0900291 // No change in local addresses means no call to setLocalPrefixes().
292 inOrder.verify(mHardware, never()).setLocalPrefixes(mStringArrayCaptor.capture());
Erik Kline7990aef2017-06-01 20:11:25 +0900293 inOrder.verify(mHardware, times(1)).setUpstreamParameters(
Erik Kline11854592017-06-15 18:06:34 +0900294 eq(testIfName), eq(ipv4Addr), eq(ipv4Gateway), eq(null));
Lorenzo Colitti5a7dea12017-07-12 15:48:07 +0900295 inOrder.verify(mHardware, times(1)).getForwardedStats(eq(testIfName));
Lorenzo Colittiddce7ee2017-08-21 12:34:50 +0900296 inOrder.verify(mHardware, times(1)).setDataLimit(eq(testIfName), eq(Long.MAX_VALUE));
Erik Kline7990aef2017-06-01 20:11:25 +0900297 inOrder.verifyNoMoreInteractions();
298
299 final String ipv6Gw1 = "fe80::cafe";
markchien986750b2019-12-06 15:24:53 +0800300 lp.addRoute(new RouteInfo(null, InetAddress.getByName(ipv6Gw1), null, RTN_UNICAST));
Erik Kline7990aef2017-06-01 20:11:25 +0900301 offload.setUpstreamLinkProperties(lp);
Erik Kline32179ff2017-07-04 18:28:11 +0900302 // No change in local addresses means no call to setLocalPrefixes().
303 inOrder.verify(mHardware, never()).setLocalPrefixes(mStringArrayCaptor.capture());
Erik Kline7990aef2017-06-01 20:11:25 +0900304 inOrder.verify(mHardware, times(1)).setUpstreamParameters(
305 eq(testIfName), eq(ipv4Addr), eq(ipv4Gateway), mStringArrayCaptor.capture());
Lorenzo Colitti5a7dea12017-07-12 15:48:07 +0900306 inOrder.verify(mHardware, times(1)).getForwardedStats(eq(testIfName));
Erik Kline7990aef2017-06-01 20:11:25 +0900307 ArrayList<String> v6gws = mStringArrayCaptor.getValue();
308 assertEquals(1, v6gws.size());
309 assertTrue(v6gws.contains(ipv6Gw1));
Lorenzo Colittiddce7ee2017-08-21 12:34:50 +0900310 inOrder.verify(mHardware, times(1)).setDataLimit(eq(testIfName), eq(Long.MAX_VALUE));
Erik Kline7990aef2017-06-01 20:11:25 +0900311 inOrder.verifyNoMoreInteractions();
312
313 final String ipv6Gw2 = "fe80::d00d";
markchien986750b2019-12-06 15:24:53 +0800314 lp.addRoute(new RouteInfo(null, InetAddress.getByName(ipv6Gw2), null, RTN_UNICAST));
Erik Kline7990aef2017-06-01 20:11:25 +0900315 offload.setUpstreamLinkProperties(lp);
Erik Kline32179ff2017-07-04 18:28:11 +0900316 // No change in local addresses means no call to setLocalPrefixes().
317 inOrder.verify(mHardware, never()).setLocalPrefixes(mStringArrayCaptor.capture());
Erik Kline7990aef2017-06-01 20:11:25 +0900318 inOrder.verify(mHardware, times(1)).setUpstreamParameters(
319 eq(testIfName), eq(ipv4Addr), eq(ipv4Gateway), mStringArrayCaptor.capture());
Lorenzo Colitti5a7dea12017-07-12 15:48:07 +0900320 inOrder.verify(mHardware, times(1)).getForwardedStats(eq(testIfName));
Erik Kline7990aef2017-06-01 20:11:25 +0900321 v6gws = mStringArrayCaptor.getValue();
322 assertEquals(2, v6gws.size());
323 assertTrue(v6gws.contains(ipv6Gw1));
324 assertTrue(v6gws.contains(ipv6Gw2));
Lorenzo Colittiddce7ee2017-08-21 12:34:50 +0900325 inOrder.verify(mHardware, times(1)).setDataLimit(eq(testIfName), eq(Long.MAX_VALUE));
Erik Kline7990aef2017-06-01 20:11:25 +0900326 inOrder.verifyNoMoreInteractions();
327
328 final LinkProperties stacked = new LinkProperties();
329 stacked.setInterfaceName("stacked");
330 stacked.addLinkAddress(new LinkAddress("192.0.2.129/25"));
markchien986750b2019-12-06 15:24:53 +0800331 stacked.addRoute(new RouteInfo(null, InetAddress.getByName("192.0.2.254"), null,
332 RTN_UNICAST));
333 stacked.addRoute(new RouteInfo(null, InetAddress.getByName("fe80::bad:f00"), null,
334 RTN_UNICAST));
Erik Kline7990aef2017-06-01 20:11:25 +0900335 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.
markchien986750b2019-12-06 15:24:53 +0800354 lp.addRoute(new RouteInfo(new IpPrefix("2001:db8::/64"), null, null, RTN_UNICAST));
Erik Kline32179ff2017-07-04 18:28:11 +0900355 // "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());
Chalard Jean9c0ff1b2019-05-30 17:11:14 +0900363 assertContainsAll(localPrefixes,
Erik Klineb3bb26e2017-07-06 19:49:35 +0900364 "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"));
markchien986750b2019-12-06 15:24:53 +0800580 usbLinkProperties.addRoute(
581 new RouteInfo(new IpPrefix(USB_PREFIX), null, null, RTN_UNICAST));
Erik Klineb3bb26e2017-07-06 19:49:35 +0900582 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.
markchien986750b2019-12-06 15:24:53 +0800587 usbLinkProperties.addRoute(
588 new RouteInfo(new IpPrefix(IPV6_LINKLOCAL), null, null, RTN_UNICAST));
Erik Klineb3bb26e2017-07-06 19:49:35 +0900589 offload.notifyDownstreamLinkProperties(usbLinkProperties);
590 inOrder.verify(mHardware, never()).addDownstreamPrefix(eq(RNDIS0), anyString());
591 inOrder.verifyNoMoreInteractions();
592
593 // [3] Add an IPv6 prefix for good measure. Only new offload-able
594 // prefixes should be passed to the HAL.
595 usbLinkProperties.addLinkAddress(new LinkAddress("2001:db8::1/64"));
markchien986750b2019-12-06 15:24:53 +0800596 usbLinkProperties.addRoute(
597 new RouteInfo(new IpPrefix(IPV6_DOC_PREFIX), null, null, RTN_UNICAST));
Erik Klineb3bb26e2017-07-06 19:49:35 +0900598 offload.notifyDownstreamLinkProperties(usbLinkProperties);
599 inOrder.verify(mHardware, times(1)).addDownstreamPrefix(RNDIS0, IPV6_DOC_PREFIX);
600 inOrder.verifyNoMoreInteractions();
601
602 // [4] Adding addresses doesn't affect notifyDownstreamLinkProperties().
603 // The address is passed in by a separate setLocalPrefixes() invocation.
604 usbLinkProperties.addLinkAddress(new LinkAddress("2001:db8::2/64"));
605 offload.notifyDownstreamLinkProperties(usbLinkProperties);
606 inOrder.verify(mHardware, never()).addDownstreamPrefix(eq(RNDIS0), anyString());
607
608 // [5] Differences in local routes are converted into addDownstream()
609 // and removeDownstream() invocations accordingly.
markchien986750b2019-12-06 15:24:53 +0800610 usbLinkProperties.removeRoute(
611 new RouteInfo(new IpPrefix(IPV6_DOC_PREFIX), null, RNDIS0, RTN_UNICAST));
612 usbLinkProperties.addRoute(
613 new RouteInfo(new IpPrefix(IPV6_DISCARD_PREFIX), null, null, RTN_UNICAST));
Erik Klineb3bb26e2017-07-06 19:49:35 +0900614 offload.notifyDownstreamLinkProperties(usbLinkProperties);
615 inOrder.verify(mHardware, times(1)).removeDownstreamPrefix(RNDIS0, IPV6_DOC_PREFIX);
616 inOrder.verify(mHardware, times(1)).addDownstreamPrefix(RNDIS0, IPV6_DISCARD_PREFIX);
617 inOrder.verifyNoMoreInteractions();
618
619 // [6] Removing a downstream interface which was never added causes no
620 // interactions with the HAL.
621 offload.removeDownstreamInterface(WLAN0);
622 inOrder.verifyNoMoreInteractions();
623
624 // [7] Removing an active downstream removes all remaining prefixes.
625 offload.removeDownstreamInterface(RNDIS0);
626 inOrder.verify(mHardware, times(1)).removeDownstreamPrefix(RNDIS0, USB_PREFIX);
627 inOrder.verify(mHardware, times(1)).removeDownstreamPrefix(RNDIS0, IPV6_DISCARD_PREFIX);
628 inOrder.verifyNoMoreInteractions();
629 }
630
Erik Klineaabdaa92017-08-31 21:09:45 +0900631 @Test
632 public void testControlCallbackOnStoppedUnsupportedFetchesAllStats() throws Exception {
633 setupFunctioningHardwareInterface();
634 enableOffload();
635
636 final OffloadController offload = makeOffloadController();
637 offload.start();
638
639 // Pretend to set a few different upstreams (only the interface name
640 // matters for this test; we're ignoring IP and route information).
641 final LinkProperties upstreamLp = new LinkProperties();
642 for (String ifname : new String[]{RMNET0, WLAN0, RMNET0}) {
643 upstreamLp.setInterfaceName(ifname);
644 offload.setUpstreamLinkProperties(upstreamLp);
645 }
646
647 // Clear invocation history, especially the getForwardedStats() calls
648 // that happen with setUpstreamParameters().
649 clearInvocations(mHardware);
650
651 OffloadHardwareInterface.ControlCallback callback = mControlCallbackCaptor.getValue();
652 callback.onStoppedUnsupported();
653
654 // Verify forwarded stats behaviour.
655 verify(mHardware, times(1)).getForwardedStats(eq(RMNET0));
656 verify(mHardware, times(1)).getForwardedStats(eq(WLAN0));
657 verifyNoMoreInteractions(mHardware);
658 verify(mNMService, times(1)).tetherLimitReached(mTetherStatsProviderCaptor.getValue());
659 verifyNoMoreInteractions(mNMService);
660 }
661
662 @Test
663 public void testControlCallbackOnSupportAvailableFetchesAllStatsAndPushesAllParameters()
664 throws Exception {
665 setupFunctioningHardwareInterface();
666 enableOffload();
667
668 final OffloadController offload = makeOffloadController();
669 offload.start();
670
671 // Pretend to set a few different upstreams (only the interface name
672 // matters for this test; we're ignoring IP and route information).
673 final LinkProperties upstreamLp = new LinkProperties();
674 for (String ifname : new String[]{RMNET0, WLAN0, RMNET0}) {
675 upstreamLp.setInterfaceName(ifname);
676 offload.setUpstreamLinkProperties(upstreamLp);
677 }
678
Erik Klinebc8b2ee2017-09-19 17:56:10 +0900679 // Pretend that some local prefixes and downstreams have been added
680 // (and removed, for good measure).
681 final Set<IpPrefix> minimumLocalPrefixes = new HashSet<>();
682 for (String s : new String[]{
683 "127.0.0.0/8", "192.0.2.0/24", "fe80::/64", "2001:db8::/64"}) {
684 minimumLocalPrefixes.add(new IpPrefix(s));
685 }
686 offload.setLocalPrefixes(minimumLocalPrefixes);
687
688 final LinkProperties usbLinkProperties = new LinkProperties();
689 usbLinkProperties.setInterfaceName(RNDIS0);
690 usbLinkProperties.addLinkAddress(new LinkAddress("192.168.42.1/24"));
markchien986750b2019-12-06 15:24:53 +0800691 usbLinkProperties.addRoute(
692 new RouteInfo(new IpPrefix(USB_PREFIX), null, null, RTN_UNICAST));
Erik Klinebc8b2ee2017-09-19 17:56:10 +0900693 offload.notifyDownstreamLinkProperties(usbLinkProperties);
694
695 final LinkProperties wifiLinkProperties = new LinkProperties();
696 wifiLinkProperties.setInterfaceName(WLAN0);
697 wifiLinkProperties.addLinkAddress(new LinkAddress("192.168.43.1/24"));
markchien986750b2019-12-06 15:24:53 +0800698 wifiLinkProperties.addRoute(
699 new RouteInfo(new IpPrefix(WIFI_PREFIX), null, null, RTN_UNICAST));
700 wifiLinkProperties.addRoute(
701 new RouteInfo(new IpPrefix(IPV6_LINKLOCAL), null, null, RTN_UNICAST));
Erik Klinebc8b2ee2017-09-19 17:56:10 +0900702 // Use a benchmark prefix (RFC 5180 + erratum), since the documentation
703 // prefix is included in the excluded prefix list.
704 wifiLinkProperties.addLinkAddress(new LinkAddress("2001:2::1/64"));
705 wifiLinkProperties.addLinkAddress(new LinkAddress("2001:2::2/64"));
markchien986750b2019-12-06 15:24:53 +0800706 wifiLinkProperties.addRoute(
707 new RouteInfo(new IpPrefix("2001:2::/64"), null, null, RTN_UNICAST));
Erik Klinebc8b2ee2017-09-19 17:56:10 +0900708 offload.notifyDownstreamLinkProperties(wifiLinkProperties);
709
710 offload.removeDownstreamInterface(RNDIS0);
711
Erik Klineaabdaa92017-08-31 21:09:45 +0900712 // Clear invocation history, especially the getForwardedStats() calls
713 // that happen with setUpstreamParameters().
714 clearInvocations(mHardware);
715
716 OffloadHardwareInterface.ControlCallback callback = mControlCallbackCaptor.getValue();
717 callback.onSupportAvailable();
718
719 // Verify forwarded stats behaviour.
720 verify(mHardware, times(1)).getForwardedStats(eq(RMNET0));
721 verify(mHardware, times(1)).getForwardedStats(eq(WLAN0));
722 verify(mNMService, times(1)).tetherLimitReached(mTetherStatsProviderCaptor.getValue());
723 verifyNoMoreInteractions(mNMService);
724
725 // TODO: verify local prefixes and downstreams are also pushed to the HAL.
Erik Klinebc8b2ee2017-09-19 17:56:10 +0900726 verify(mHardware, times(1)).setLocalPrefixes(mStringArrayCaptor.capture());
727 ArrayList<String> localPrefixes = mStringArrayCaptor.getValue();
728 assertEquals(4, localPrefixes.size());
Chalard Jean9c0ff1b2019-05-30 17:11:14 +0900729 assertContainsAll(localPrefixes,
Erik Klinebc8b2ee2017-09-19 17:56:10 +0900730 // TODO: The logic to find and exclude downstream IP prefixes
731 // is currently in Tethering's OffloadWrapper but must be moved
732 // into OffloadController proper. After this, also check for:
733 // "192.168.43.1/32", "2001:2::1/128", "2001:2::2/128"
734 "127.0.0.0/8", "192.0.2.0/24", "fe80::/64", "2001:db8::/64");
735 verify(mHardware, times(1)).addDownstreamPrefix(WLAN0, "192.168.43.0/24");
736 verify(mHardware, times(1)).addDownstreamPrefix(WLAN0, "2001:2::/64");
Erik Klineaabdaa92017-08-31 21:09:45 +0900737 verify(mHardware, times(1)).setUpstreamParameters(eq(RMNET0), any(), any(), any());
738 verify(mHardware, times(1)).setDataLimit(eq(RMNET0), anyLong());
739 verifyNoMoreInteractions(mHardware);
740 }
741
Erik Kline92c4db02017-05-31 10:21:32 +0900742}