blob: 27e90bcf98beded0e07b2058531aec417b5a269b [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 Colitti4ca0ba82017-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 Colitti4ca0ba82017-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 Colitti4ca0ba82017-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;
Lorenzo Colitti4ca0ba82017-07-12 15:48:07 +090026import static com.android.server.connectivity.tethering.OffloadHardwareInterface.ForwardedStats;
Erik Kline7990aef2017-06-01 20:11:25 +090027import static org.junit.Assert.assertEquals;
28import static org.junit.Assert.assertTrue;
Erik Kline92c4db02017-05-31 10:21:32 +090029import static org.junit.Assert.fail;
30import static org.mockito.Matchers.any;
Lorenzo Colitti50b60fc2017-08-11 13:47:49 +090031import static org.mockito.Matchers.anyLong;
Erik Kline92c4db02017-05-31 10:21:32 +090032import static org.mockito.Matchers.anyObject;
Lorenzo Colitti4ca0ba82017-07-12 15:48:07 +090033import static org.mockito.Matchers.anyString;
Erik Kline7990aef2017-06-01 20:11:25 +090034import static org.mockito.Matchers.eq;
Lorenzo Colitti4ca0ba82017-07-12 15:48:07 +090035import static org.mockito.Mockito.doNothing;
Erik Kline92c4db02017-05-31 10:21:32 +090036import static org.mockito.Mockito.inOrder;
37import static org.mockito.Mockito.never;
Erik Kline7990aef2017-06-01 20:11:25 +090038import static org.mockito.Mockito.reset;
Erik Kline92c4db02017-05-31 10:21:32 +090039import static org.mockito.Mockito.times;
Lorenzo Colitti4ca0ba82017-07-12 15:48:07 +090040import static org.mockito.Mockito.verify;
Erik Kline92c4db02017-05-31 10:21:32 +090041import static org.mockito.Mockito.when;
42
43import android.content.Context;
Erik Klineec372752017-06-07 16:33:19 +090044import android.content.pm.ApplicationInfo;
Lorenzo Colitti4ca0ba82017-07-12 15:48:07 +090045import android.net.ITetheringStatsProvider;
Erik Kline5acb4e32017-07-04 18:28:11 +090046import android.net.IpPrefix;
Erik Kline7990aef2017-06-01 20:11:25 +090047import android.net.LinkAddress;
48import android.net.LinkProperties;
Lorenzo Colitti4ca0ba82017-07-12 15:48:07 +090049import android.net.NetworkStats;
Erik Kline7990aef2017-06-01 20:11:25 +090050import android.net.RouteInfo;
Erik Kline92c4db02017-05-31 10:21:32 +090051import android.net.util.SharedLog;
Lorenzo Colitti50b60fc2017-08-11 13:47:49 +090052import android.os.ConditionVariable;
Lorenzo Colitti4ca0ba82017-07-12 15:48:07 +090053import android.os.Handler;
54import android.os.Looper;
55import android.os.INetworkManagementService;
Erik Kline92c4db02017-05-31 10:21:32 +090056import android.provider.Settings;
57import android.provider.Settings.SettingNotFoundException;
58
59import android.support.test.filters.SmallTest;
60import android.support.test.runner.AndroidJUnit4;
61import android.test.mock.MockContentResolver;
62import com.android.internal.util.test.FakeSettingsProvider;
63
Erik Kline7990aef2017-06-01 20:11:25 +090064import java.net.InetAddress;
65import java.util.ArrayList;
Erik Kline5acb4e32017-07-04 18:28:11 +090066import java.util.HashSet;
67import java.util.Set;
Erik Kline7990aef2017-06-01 20:11:25 +090068
Erik Kline5eaa7ed2017-07-07 17:38:30 +090069import org.junit.After;
Erik Kline92c4db02017-05-31 10:21:32 +090070import org.junit.Before;
Colin Cross46a435a2017-08-29 12:18:55 -070071import org.junit.Ignore;
Erik Kline92c4db02017-05-31 10:21:32 +090072import org.junit.runner.RunWith;
73import org.junit.Test;
Erik Kline7990aef2017-06-01 20:11:25 +090074import org.mockito.ArgumentCaptor;
Erik Kline92c4db02017-05-31 10:21:32 +090075import org.mockito.InOrder;
76import org.mockito.Mock;
77import org.mockito.MockitoAnnotations;
78
79
80@RunWith(AndroidJUnit4.class)
81@SmallTest
82public class OffloadControllerTest {
Erik Klineb3bb26e2017-07-06 19:49:35 +090083 private static final String RNDIS0 = "test_rndis0";
84 private static final String RMNET0 = "test_rmnet_data0";
85 private static final String WLAN0 = "test_wlan0";
86
87 private static final String IPV6_LINKLOCAL = "fe80::/64";
88 private static final String IPV6_DOC_PREFIX = "2001:db8::/64";
89 private static final String IPV6_DISCARD_PREFIX = "100::/64";
90 private static final String USB_PREFIX = "192.168.42.0/24";
91 private static final String WIFI_PREFIX = "192.168.43.0/24";
Erik Kline92c4db02017-05-31 10:21:32 +090092
93 @Mock private OffloadHardwareInterface mHardware;
Erik Klineec372752017-06-07 16:33:19 +090094 @Mock private ApplicationInfo mApplicationInfo;
Erik Kline92c4db02017-05-31 10:21:32 +090095 @Mock private Context mContext;
Lorenzo Colitti4ca0ba82017-07-12 15:48:07 +090096 @Mock private INetworkManagementService mNMService;
Erik Klinedd4d5822017-06-12 18:20:08 +090097 private final ArgumentCaptor<ArrayList> mStringArrayCaptor =
98 ArgumentCaptor.forClass(ArrayList.class);
Lorenzo Colitti4ca0ba82017-07-12 15:48:07 +090099 private final ArgumentCaptor<ITetheringStatsProvider.Stub> mTetherStatsProviderCaptor =
100 ArgumentCaptor.forClass(ITetheringStatsProvider.Stub.class);
Lorenzo Colitti9f0baa92017-08-15 19:25:51 +0900101 private final ArgumentCaptor<OffloadHardwareInterface.ControlCallback> mControlCallbackCaptor =
102 ArgumentCaptor.forClass(OffloadHardwareInterface.ControlCallback.class);
Erik Kline92c4db02017-05-31 10:21:32 +0900103 private MockContentResolver mContentResolver;
104
Lorenzo Colitti4ca0ba82017-07-12 15:48:07 +0900105 @Before public void setUp() {
Erik Kline92c4db02017-05-31 10:21:32 +0900106 MockitoAnnotations.initMocks(this);
Erik Klineec372752017-06-07 16:33:19 +0900107 when(mContext.getApplicationInfo()).thenReturn(mApplicationInfo);
108 when(mContext.getPackageName()).thenReturn("OffloadControllerTest");
Erik Kline92c4db02017-05-31 10:21:32 +0900109 mContentResolver = new MockContentResolver(mContext);
110 mContentResolver.addProvider(Settings.AUTHORITY, new FakeSettingsProvider());
Erik Kline92c4db02017-05-31 10:21:32 +0900111 when(mContext.getContentResolver()).thenReturn(mContentResolver);
Erik Kline5eaa7ed2017-07-07 17:38:30 +0900112 // TODO: call this when available.
113 // FakeSettingsProvider.clearSettingsProvider();
114 }
115
116 @After public void tearDown() throws Exception {
117 // TODO: call this when available.
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 Colitti4ca0ba82017-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 Kline5eaa7ed2017-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() {
135 ConditionVariable cv = new ConditionVariable();
136 new Handler(Looper.getMainLooper()).post(() -> { cv.open(); });
137 cv.block();
138 }
139
Lorenzo Colitti4ca0ba82017-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
Colin Cross46a435a2017-08-29 12:18:55 -0700148 @Test
149 @Ignore("Restore when FakeSettingsProvider.clearSettingsProvider() is available.")
Lorenzo Colitti4ca0ba82017-07-12 15:48:07 +0900150 public void testNoSettingsValueDefaultDisabledDoesNotStart() throws Exception {
Erik Kline92c4db02017-05-31 10:21:32 +0900151 setupFunctioningHardwareInterface();
Erik Kline5eaa7ed2017-07-07 17:38:30 +0900152 when(mHardware.getDefaultTetherOffloadDisabled()).thenReturn(1);
Erik Kline92c4db02017-05-31 10:21:32 +0900153 try {
154 Settings.Global.getInt(mContentResolver, TETHER_OFFLOAD_DISABLED);
155 fail();
156 } catch (SettingNotFoundException expected) {}
157
Lorenzo Colitti4ca0ba82017-07-12 15:48:07 +0900158 final OffloadController offload = makeOffloadController();
Erik Kline92c4db02017-05-31 10:21:32 +0900159 offload.start();
160
161 final InOrder inOrder = inOrder(mHardware);
Erik Kline5eaa7ed2017-07-07 17:38:30 +0900162 inOrder.verify(mHardware, times(1)).getDefaultTetherOffloadDisabled();
163 inOrder.verify(mHardware, never()).initOffloadConfig();
164 inOrder.verify(mHardware, never()).initOffloadControl(
165 any(OffloadHardwareInterface.ControlCallback.class));
166 inOrder.verifyNoMoreInteractions();
167 }
168
Colin Cross46a435a2017-08-29 12:18:55 -0700169 @Test
170 @Ignore("Restore when FakeSettingsProvider.clearSettingsProvider() is available.")
Lorenzo Colitti4ca0ba82017-07-12 15:48:07 +0900171 public void testNoSettingsValueDefaultEnabledDoesStart() throws Exception {
Erik Kline5eaa7ed2017-07-07 17:38:30 +0900172 setupFunctioningHardwareInterface();
173 when(mHardware.getDefaultTetherOffloadDisabled()).thenReturn(0);
174 try {
175 Settings.Global.getInt(mContentResolver, TETHER_OFFLOAD_DISABLED);
176 fail();
177 } catch (SettingNotFoundException expected) {}
178
Lorenzo Colitti4ca0ba82017-07-12 15:48:07 +0900179 final OffloadController offload = makeOffloadController();
Erik Kline5eaa7ed2017-07-07 17:38:30 +0900180 offload.start();
181
182 final InOrder inOrder = inOrder(mHardware);
183 inOrder.verify(mHardware, times(1)).getDefaultTetherOffloadDisabled();
Erik Kline92c4db02017-05-31 10:21:32 +0900184 inOrder.verify(mHardware, times(1)).initOffloadConfig();
185 inOrder.verify(mHardware, times(1)).initOffloadControl(
186 any(OffloadHardwareInterface.ControlCallback.class));
187 inOrder.verifyNoMoreInteractions();
188 }
189
190 @Test
Lorenzo Colitti4ca0ba82017-07-12 15:48:07 +0900191 public void testSettingsAllowsStart() throws Exception {
Erik Kline92c4db02017-05-31 10:21:32 +0900192 setupFunctioningHardwareInterface();
193 Settings.Global.putInt(mContentResolver, TETHER_OFFLOAD_DISABLED, 0);
194
Lorenzo Colitti4ca0ba82017-07-12 15:48:07 +0900195 final OffloadController offload = makeOffloadController();
Erik Kline92c4db02017-05-31 10:21:32 +0900196 offload.start();
197
198 final InOrder inOrder = inOrder(mHardware);
Erik Kline5eaa7ed2017-07-07 17:38:30 +0900199 inOrder.verify(mHardware, times(1)).getDefaultTetherOffloadDisabled();
Erik Kline92c4db02017-05-31 10:21:32 +0900200 inOrder.verify(mHardware, times(1)).initOffloadConfig();
201 inOrder.verify(mHardware, times(1)).initOffloadControl(
202 any(OffloadHardwareInterface.ControlCallback.class));
203 inOrder.verifyNoMoreInteractions();
204 }
205
206 @Test
Lorenzo Colitti4ca0ba82017-07-12 15:48:07 +0900207 public void testSettingsDisablesStart() throws Exception {
Erik Kline92c4db02017-05-31 10:21:32 +0900208 setupFunctioningHardwareInterface();
209 Settings.Global.putInt(mContentResolver, TETHER_OFFLOAD_DISABLED, 1);
210
Lorenzo Colitti4ca0ba82017-07-12 15:48:07 +0900211 final OffloadController offload = makeOffloadController();
Erik Kline92c4db02017-05-31 10:21:32 +0900212 offload.start();
213
214 final InOrder inOrder = inOrder(mHardware);
Erik Kline5eaa7ed2017-07-07 17:38:30 +0900215 inOrder.verify(mHardware, times(1)).getDefaultTetherOffloadDisabled();
Erik Kline92c4db02017-05-31 10:21:32 +0900216 inOrder.verify(mHardware, never()).initOffloadConfig();
217 inOrder.verify(mHardware, never()).initOffloadControl(anyObject());
218 inOrder.verifyNoMoreInteractions();
219 }
Erik Kline7990aef2017-06-01 20:11:25 +0900220
221 @Test
222 public void testSetUpstreamLinkPropertiesWorking() throws Exception {
223 setupFunctioningHardwareInterface();
Erik Kline5eaa7ed2017-07-07 17:38:30 +0900224 enableOffload();
225
Lorenzo Colitti4ca0ba82017-07-12 15:48:07 +0900226 final OffloadController offload = makeOffloadController();
Erik Kline7990aef2017-06-01 20:11:25 +0900227 offload.start();
228
229 final InOrder inOrder = inOrder(mHardware);
Erik Kline5eaa7ed2017-07-07 17:38:30 +0900230 inOrder.verify(mHardware, times(1)).getDefaultTetherOffloadDisabled();
Erik Kline7990aef2017-06-01 20:11:25 +0900231 inOrder.verify(mHardware, times(1)).initOffloadConfig();
232 inOrder.verify(mHardware, times(1)).initOffloadControl(
233 any(OffloadHardwareInterface.ControlCallback.class));
234 inOrder.verifyNoMoreInteractions();
235
Erik Kline5acb4e32017-07-04 18:28:11 +0900236 // In reality, the UpstreamNetworkMonitor would have passed down to us
237 // a covering set of local prefixes representing a minimum essential
238 // set plus all the prefixes on networks with network agents.
239 //
240 // We simulate that there, and then add upstream elements one by one
241 // and watch what happens.
242 final Set<IpPrefix> minimumLocalPrefixes = new HashSet<>();
243 for (String s : new String[]{
244 "127.0.0.0/8", "192.0.2.0/24", "fe80::/64", "2001:db8::/64"}) {
245 minimumLocalPrefixes.add(new IpPrefix(s));
246 }
247 offload.setLocalPrefixes(minimumLocalPrefixes);
248 inOrder.verify(mHardware, times(1)).setLocalPrefixes(mStringArrayCaptor.capture());
249 ArrayList<String> localPrefixes = mStringArrayCaptor.getValue();
250 assertEquals(4, localPrefixes.size());
Erik Klineb3bb26e2017-07-06 19:49:35 +0900251 assertArrayListContains(localPrefixes,
252 "127.0.0.0/8", "192.0.2.0/24", "fe80::/64", "2001:db8::/64");
Erik Kline7990aef2017-06-01 20:11:25 +0900253 inOrder.verifyNoMoreInteractions();
Erik Kline5acb4e32017-07-04 18:28:11 +0900254
255 offload.setUpstreamLinkProperties(null);
256 // No change in local addresses means no call to setLocalPrefixes().
257 inOrder.verify(mHardware, never()).setLocalPrefixes(mStringArrayCaptor.capture());
258 // This LinkProperties value does not differ from the default upstream.
259 // There should be no extraneous call to setUpstreamParameters().
260 inOrder.verify(mHardware, never()).setUpstreamParameters(
261 anyObject(), anyObject(), anyObject(), anyObject());
262 inOrder.verifyNoMoreInteractions();
Erik Kline7990aef2017-06-01 20:11:25 +0900263
264 final LinkProperties lp = new LinkProperties();
265
266 final String testIfName = "rmnet_data17";
267 lp.setInterfaceName(testIfName);
268 offload.setUpstreamLinkProperties(lp);
Erik Kline5acb4e32017-07-04 18:28:11 +0900269 // No change in local addresses means no call to setLocalPrefixes().
270 inOrder.verify(mHardware, never()).setLocalPrefixes(mStringArrayCaptor.capture());
Erik Kline7990aef2017-06-01 20:11:25 +0900271 inOrder.verify(mHardware, times(1)).setUpstreamParameters(
Erik Kline664d2082017-06-15 18:06:34 +0900272 eq(testIfName), eq(null), eq(null), eq(null));
Lorenzo Colittiddce7ee2017-08-21 12:34:50 +0900273 inOrder.verify(mHardware, times(1)).setDataLimit(eq(testIfName), eq(Long.MAX_VALUE));
Erik Kline7990aef2017-06-01 20:11:25 +0900274 inOrder.verifyNoMoreInteractions();
275
276 final String ipv4Addr = "192.0.2.5";
277 final String linkAddr = ipv4Addr + "/24";
278 lp.addLinkAddress(new LinkAddress(linkAddr));
Erik Kline5acb4e32017-07-04 18:28:11 +0900279 lp.addRoute(new RouteInfo(new IpPrefix("192.0.2.0/24")));
Erik Kline7990aef2017-06-01 20:11:25 +0900280 offload.setUpstreamLinkProperties(lp);
Erik Kline5acb4e32017-07-04 18:28:11 +0900281 // IPv4 prefixes and addresses on the upstream are simply left as whole
282 // prefixes (already passed in from UpstreamNetworkMonitor code). If a
283 // tethering client sends traffic to the IPv4 default router or other
284 // clients on the upstream this will not be hardware-forwarded, and that
285 // should be fine for now. Ergo: no change in local addresses, no call
286 // to setLocalPrefixes().
287 inOrder.verify(mHardware, never()).setLocalPrefixes(mStringArrayCaptor.capture());
Erik Kline7990aef2017-06-01 20:11:25 +0900288 inOrder.verify(mHardware, times(1)).setUpstreamParameters(
Erik Kline664d2082017-06-15 18:06:34 +0900289 eq(testIfName), eq(ipv4Addr), eq(null), eq(null));
Lorenzo Colitti4ca0ba82017-07-12 15:48:07 +0900290 inOrder.verify(mHardware, times(1)).getForwardedStats(eq(testIfName));
Lorenzo Colittiddce7ee2017-08-21 12:34:50 +0900291 inOrder.verify(mHardware, times(1)).setDataLimit(eq(testIfName), eq(Long.MAX_VALUE));
Erik Kline7990aef2017-06-01 20:11:25 +0900292 inOrder.verifyNoMoreInteractions();
293
294 final String ipv4Gateway = "192.0.2.1";
295 lp.addRoute(new RouteInfo(InetAddress.getByName(ipv4Gateway)));
296 offload.setUpstreamLinkProperties(lp);
Erik Kline5acb4e32017-07-04 18:28:11 +0900297 // No change in local addresses means no call to setLocalPrefixes().
298 inOrder.verify(mHardware, never()).setLocalPrefixes(mStringArrayCaptor.capture());
Erik Kline7990aef2017-06-01 20:11:25 +0900299 inOrder.verify(mHardware, times(1)).setUpstreamParameters(
Erik Kline664d2082017-06-15 18:06:34 +0900300 eq(testIfName), eq(ipv4Addr), eq(ipv4Gateway), eq(null));
Lorenzo Colitti4ca0ba82017-07-12 15:48:07 +0900301 inOrder.verify(mHardware, times(1)).getForwardedStats(eq(testIfName));
Lorenzo Colittiddce7ee2017-08-21 12:34:50 +0900302 inOrder.verify(mHardware, times(1)).setDataLimit(eq(testIfName), eq(Long.MAX_VALUE));
Erik Kline7990aef2017-06-01 20:11:25 +0900303 inOrder.verifyNoMoreInteractions();
304
305 final String ipv6Gw1 = "fe80::cafe";
306 lp.addRoute(new RouteInfo(InetAddress.getByName(ipv6Gw1)));
307 offload.setUpstreamLinkProperties(lp);
Erik Kline5acb4e32017-07-04 18:28:11 +0900308 // No change in local addresses means no call to setLocalPrefixes().
309 inOrder.verify(mHardware, never()).setLocalPrefixes(mStringArrayCaptor.capture());
Erik Kline7990aef2017-06-01 20:11:25 +0900310 inOrder.verify(mHardware, times(1)).setUpstreamParameters(
311 eq(testIfName), eq(ipv4Addr), eq(ipv4Gateway), mStringArrayCaptor.capture());
Lorenzo Colitti4ca0ba82017-07-12 15:48:07 +0900312 inOrder.verify(mHardware, times(1)).getForwardedStats(eq(testIfName));
Erik Kline7990aef2017-06-01 20:11:25 +0900313 ArrayList<String> v6gws = mStringArrayCaptor.getValue();
314 assertEquals(1, v6gws.size());
315 assertTrue(v6gws.contains(ipv6Gw1));
Lorenzo Colittiddce7ee2017-08-21 12:34:50 +0900316 inOrder.verify(mHardware, times(1)).setDataLimit(eq(testIfName), eq(Long.MAX_VALUE));
Erik Kline7990aef2017-06-01 20:11:25 +0900317 inOrder.verifyNoMoreInteractions();
318
319 final String ipv6Gw2 = "fe80::d00d";
320 lp.addRoute(new RouteInfo(InetAddress.getByName(ipv6Gw2)));
321 offload.setUpstreamLinkProperties(lp);
Erik Kline5acb4e32017-07-04 18:28:11 +0900322 // No change in local addresses means no call to setLocalPrefixes().
323 inOrder.verify(mHardware, never()).setLocalPrefixes(mStringArrayCaptor.capture());
Erik Kline7990aef2017-06-01 20:11:25 +0900324 inOrder.verify(mHardware, times(1)).setUpstreamParameters(
325 eq(testIfName), eq(ipv4Addr), eq(ipv4Gateway), mStringArrayCaptor.capture());
Lorenzo Colitti4ca0ba82017-07-12 15:48:07 +0900326 inOrder.verify(mHardware, times(1)).getForwardedStats(eq(testIfName));
Erik Kline7990aef2017-06-01 20:11:25 +0900327 v6gws = mStringArrayCaptor.getValue();
328 assertEquals(2, v6gws.size());
329 assertTrue(v6gws.contains(ipv6Gw1));
330 assertTrue(v6gws.contains(ipv6Gw2));
Lorenzo Colittiddce7ee2017-08-21 12:34:50 +0900331 inOrder.verify(mHardware, times(1)).setDataLimit(eq(testIfName), eq(Long.MAX_VALUE));
Erik Kline7990aef2017-06-01 20:11:25 +0900332 inOrder.verifyNoMoreInteractions();
333
334 final LinkProperties stacked = new LinkProperties();
335 stacked.setInterfaceName("stacked");
336 stacked.addLinkAddress(new LinkAddress("192.0.2.129/25"));
337 stacked.addRoute(new RouteInfo(InetAddress.getByName("192.0.2.254")));
338 stacked.addRoute(new RouteInfo(InetAddress.getByName("fe80::bad:f00")));
339 assertTrue(lp.addStackedLink(stacked));
340 offload.setUpstreamLinkProperties(lp);
Erik Kline5acb4e32017-07-04 18:28:11 +0900341 // No change in local addresses means no call to setLocalPrefixes().
342 inOrder.verify(mHardware, never()).setLocalPrefixes(mStringArrayCaptor.capture());
Erik Kline7990aef2017-06-01 20:11:25 +0900343 inOrder.verify(mHardware, times(1)).setUpstreamParameters(
344 eq(testIfName), eq(ipv4Addr), eq(ipv4Gateway), mStringArrayCaptor.capture());
Lorenzo Colitti4ca0ba82017-07-12 15:48:07 +0900345 inOrder.verify(mHardware, times(1)).getForwardedStats(eq(testIfName));
Erik Kline7990aef2017-06-01 20:11:25 +0900346 v6gws = mStringArrayCaptor.getValue();
347 assertEquals(2, v6gws.size());
348 assertTrue(v6gws.contains(ipv6Gw1));
349 assertTrue(v6gws.contains(ipv6Gw2));
Lorenzo Colittiddce7ee2017-08-21 12:34:50 +0900350 inOrder.verify(mHardware, times(1)).setDataLimit(eq(testIfName), eq(Long.MAX_VALUE));
Erik Kline7990aef2017-06-01 20:11:25 +0900351 inOrder.verifyNoMoreInteractions();
Erik Kline5acb4e32017-07-04 18:28:11 +0900352
353 // Add in some IPv6 upstream info. When there is a tethered downstream
354 // making use of the IPv6 prefix we would expect to see the /64 route
355 // removed from "local prefixes" and /128s added for the upstream IPv6
356 // addresses. This is not yet implemented, and for now we simply
357 // expect to see these /128s.
358 lp.addRoute(new RouteInfo(new IpPrefix("2001:db8::/64")));
359 // "2001:db8::/64" plus "assigned" ASCII in hex
360 lp.addLinkAddress(new LinkAddress("2001:db8::6173:7369:676e:6564/64"));
361 // "2001:db8::/64" plus "random" ASCII in hex
362 lp.addLinkAddress(new LinkAddress("2001:db8::7261:6e64:6f6d/64"));
363 offload.setUpstreamLinkProperties(lp);
364 inOrder.verify(mHardware, times(1)).setLocalPrefixes(mStringArrayCaptor.capture());
365 localPrefixes = mStringArrayCaptor.getValue();
366 assertEquals(6, localPrefixes.size());
Erik Klineb3bb26e2017-07-06 19:49:35 +0900367 assertArrayListContains(localPrefixes,
368 "127.0.0.0/8", "192.0.2.0/24", "fe80::/64", "2001:db8::/64",
369 "2001:db8::6173:7369:676e:6564/128", "2001:db8::7261:6e64:6f6d/128");
Erik Kline5acb4e32017-07-04 18:28:11 +0900370 // The relevant parts of the LinkProperties have not changed, but at the
371 // moment we do not de-dup upstream LinkProperties this carefully.
372 inOrder.verify(mHardware, times(1)).setUpstreamParameters(
373 eq(testIfName), eq(ipv4Addr), eq(ipv4Gateway), mStringArrayCaptor.capture());
374 v6gws = mStringArrayCaptor.getValue();
375 assertEquals(2, v6gws.size());
376 assertTrue(v6gws.contains(ipv6Gw1));
377 assertTrue(v6gws.contains(ipv6Gw2));
Lorenzo Colitti4ca0ba82017-07-12 15:48:07 +0900378 inOrder.verify(mHardware, times(1)).getForwardedStats(eq(testIfName));
Lorenzo Colittiddce7ee2017-08-21 12:34:50 +0900379 inOrder.verify(mHardware, times(1)).setDataLimit(eq(testIfName), eq(Long.MAX_VALUE));
Erik Kline5acb4e32017-07-04 18:28:11 +0900380 inOrder.verifyNoMoreInteractions();
381
382 // Completely identical LinkProperties updates are de-duped.
383 offload.setUpstreamLinkProperties(lp);
384 // This LinkProperties value does not differ from the default upstream.
385 // There should be no extraneous call to setUpstreamParameters().
386 inOrder.verify(mHardware, never()).setUpstreamParameters(
387 anyObject(), anyObject(), anyObject(), anyObject());
388 inOrder.verifyNoMoreInteractions();
Erik Kline7990aef2017-06-01 20:11:25 +0900389 }
Lorenzo Colitti4ca0ba82017-07-12 15:48:07 +0900390
391 private void assertNetworkStats(String iface, ForwardedStats stats, NetworkStats.Entry entry) {
392 assertEquals(iface, entry.iface);
393 assertEquals(stats.rxBytes, entry.rxBytes);
394 assertEquals(stats.txBytes, entry.txBytes);
395 assertEquals(SET_DEFAULT, entry.set);
396 assertEquals(TAG_NONE, entry.tag);
Lorenzo Colitti4ca0ba82017-07-12 15:48:07 +0900397 }
398
399 @Test
400 public void testGetForwardedStats() throws Exception {
401 setupFunctioningHardwareInterface();
402 enableOffload();
403
404 final OffloadController offload = makeOffloadController();
405 offload.start();
406
407 final String ethernetIface = "eth1";
408 final String mobileIface = "rmnet_data0";
409
410 ForwardedStats ethernetStats = new ForwardedStats();
411 ethernetStats.rxBytes = 12345;
412 ethernetStats.txBytes = 54321;
413
414 ForwardedStats mobileStats = new ForwardedStats();
415 mobileStats.rxBytes = 999;
416 mobileStats.txBytes = 99999;
417
418 when(mHardware.getForwardedStats(eq(ethernetIface))).thenReturn(ethernetStats);
419 when(mHardware.getForwardedStats(eq(mobileIface))).thenReturn(mobileStats);
420
Hugo Benichi752c1282017-08-22 13:57:41 +0900421 InOrder inOrder = inOrder(mHardware);
422
Lorenzo Colitti4ca0ba82017-07-12 15:48:07 +0900423 final LinkProperties lp = new LinkProperties();
424 lp.setInterfaceName(ethernetIface);
425 offload.setUpstreamLinkProperties(lp);
Hugo Benichi752c1282017-08-22 13:57:41 +0900426 // Previous upstream was null, so no stats are fetched.
427 inOrder.verify(mHardware, never()).getForwardedStats(any());
Lorenzo Colitti4ca0ba82017-07-12 15:48:07 +0900428
429 lp.setInterfaceName(mobileIface);
430 offload.setUpstreamLinkProperties(lp);
Hugo Benichi752c1282017-08-22 13:57:41 +0900431 // Expect that we fetch stats from the previous upstream.
432 inOrder.verify(mHardware, times(1)).getForwardedStats(eq(ethernetIface));
Lorenzo Colitti4ca0ba82017-07-12 15:48:07 +0900433
434 lp.setInterfaceName(ethernetIface);
435 offload.setUpstreamLinkProperties(lp);
Hugo Benichi752c1282017-08-22 13:57:41 +0900436 // Expect that we fetch stats from the previous upstream.
437 inOrder.verify(mHardware, times(1)).getForwardedStats(eq(mobileIface));
Lorenzo Colitti4ca0ba82017-07-12 15:48:07 +0900438
Hugo Benichi752c1282017-08-22 13:57:41 +0900439 ethernetStats = new ForwardedStats();
Lorenzo Colitti4ca0ba82017-07-12 15:48:07 +0900440 ethernetStats.rxBytes = 100000;
441 ethernetStats.txBytes = 100000;
Hugo Benichi752c1282017-08-22 13:57:41 +0900442 when(mHardware.getForwardedStats(eq(ethernetIface))).thenReturn(ethernetStats);
Lorenzo Colitti4ca0ba82017-07-12 15:48:07 +0900443 offload.setUpstreamLinkProperties(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 Colitti4ca0ba82017-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());
Erik Klineb3bb26e2017-07-06 19:49:35 +0900453 inOrder.verify(mHardware, times(1)).setUpstreamParameters(
454 eq(null), eq(null), eq(null), eq(null));
Hugo Benichi752c1282017-08-22 13:57:41 +0900455 inOrder.verifyNoMoreInteractions();
Lorenzo Colittif1912ca2017-08-17 19:23:08 +0900456
Lorenzo Colitti4ca0ba82017-07-12 15:48:07 +0900457 assertEquals(2, stats.size());
Lorenzo Colittif1912ca2017-08-17 19:23:08 +0900458 assertEquals(2, perUidStats.size());
Lorenzo Colitti4ca0ba82017-07-12 15:48:07 +0900459
460 NetworkStats.Entry entry = null;
Lorenzo Colittif1912ca2017-08-17 19:23:08 +0900461 for (int i = 0; i < stats.size(); i++) {
462 assertEquals(UID_ALL, stats.getValues(i, entry).uid);
463 assertEquals(UID_TETHERING, perUidStats.getValues(i, entry).uid);
464 }
465
Lorenzo Colitti4ca0ba82017-07-12 15:48:07 +0900466 int ethernetPosition = ethernetIface.equals(stats.getValues(0, entry).iface) ? 0 : 1;
467 int mobilePosition = 1 - ethernetPosition;
468
469 entry = stats.getValues(mobilePosition, entry);
470 assertNetworkStats(mobileIface, mobileStats, entry);
Lorenzo Colittif1912ca2017-08-17 19:23:08 +0900471 entry = perUidStats.getValues(mobilePosition, entry);
472 assertNetworkStats(mobileIface, mobileStats, entry);
Lorenzo Colitti4ca0ba82017-07-12 15:48:07 +0900473
474 ethernetStats.rxBytes = 12345 + 100000;
475 ethernetStats.txBytes = 54321 + 100000;
476 entry = stats.getValues(ethernetPosition, entry);
477 assertNetworkStats(ethernetIface, ethernetStats, entry);
Lorenzo Colittif1912ca2017-08-17 19:23:08 +0900478 entry = perUidStats.getValues(ethernetPosition, entry);
479 assertNetworkStats(ethernetIface, ethernetStats, entry);
Lorenzo Colitti4ca0ba82017-07-12 15:48:07 +0900480 }
Lorenzo Colitti50b60fc2017-08-11 13:47:49 +0900481
482 @Test
483 public void testSetInterfaceQuota() throws Exception {
484 setupFunctioningHardwareInterface();
485 enableOffload();
486
487 final OffloadController offload = makeOffloadController();
488 offload.start();
489
490 final String ethernetIface = "eth1";
491 final String mobileIface = "rmnet_data0";
492 final long ethernetLimit = 12345;
493 final long mobileLimit = 12345678;
494
495 final LinkProperties lp = new LinkProperties();
496 lp.setInterfaceName(ethernetIface);
497 offload.setUpstreamLinkProperties(lp);
498
499 ITetheringStatsProvider provider = mTetherStatsProviderCaptor.getValue();
500 final InOrder inOrder = inOrder(mHardware);
501 when(mHardware.setUpstreamParameters(any(), any(), any(), any())).thenReturn(true);
502 when(mHardware.setDataLimit(anyString(), anyLong())).thenReturn(true);
503
504 // Applying an interface quota to the current upstream immediately sends it to the hardware.
505 provider.setInterfaceQuota(ethernetIface, ethernetLimit);
506 waitForIdle();
507 inOrder.verify(mHardware).setDataLimit(ethernetIface, ethernetLimit);
508 inOrder.verifyNoMoreInteractions();
509
510 // Applying an interface quota to another upstream does not take any immediate action.
511 provider.setInterfaceQuota(mobileIface, mobileLimit);
512 waitForIdle();
513 inOrder.verify(mHardware, never()).setDataLimit(anyString(), anyLong());
514
515 // Switching to that upstream causes the quota to be applied if the parameters were applied
516 // correctly.
517 lp.setInterfaceName(mobileIface);
518 offload.setUpstreamLinkProperties(lp);
519 waitForIdle();
520 inOrder.verify(mHardware).setDataLimit(mobileIface, mobileLimit);
521
522 // Setting a limit of ITetheringStatsProvider.QUOTA_UNLIMITED causes the limit to be set
523 // to Long.MAX_VALUE.
524 provider.setInterfaceQuota(mobileIface, ITetheringStatsProvider.QUOTA_UNLIMITED);
525 waitForIdle();
526 inOrder.verify(mHardware).setDataLimit(mobileIface, Long.MAX_VALUE);
527
528 // If setting upstream parameters fails, then the data limit is not set.
529 when(mHardware.setUpstreamParameters(any(), any(), any(), any())).thenReturn(false);
530 lp.setInterfaceName(ethernetIface);
531 offload.setUpstreamLinkProperties(lp);
532 provider.setInterfaceQuota(mobileIface, mobileLimit);
533 waitForIdle();
534 inOrder.verify(mHardware, never()).setDataLimit(anyString(), anyLong());
535
536 // If setting the data limit fails while changing upstreams, offload is stopped.
537 when(mHardware.setUpstreamParameters(any(), any(), any(), any())).thenReturn(true);
538 when(mHardware.setDataLimit(anyString(), anyLong())).thenReturn(false);
539 lp.setInterfaceName(mobileIface);
540 offload.setUpstreamLinkProperties(lp);
541 provider.setInterfaceQuota(mobileIface, mobileLimit);
542 waitForIdle();
Lorenzo Colittiddce7ee2017-08-21 12:34:50 +0900543 inOrder.verify(mHardware).getForwardedStats(ethernetIface);
Lorenzo Colitti50b60fc2017-08-11 13:47:49 +0900544 inOrder.verify(mHardware).stopOffloadControl();
545 }
Lorenzo Colitti9f0baa92017-08-15 19:25:51 +0900546
547 @Test
548 public void testDataLimitCallback() throws Exception {
549 setupFunctioningHardwareInterface();
550 enableOffload();
551
552 final OffloadController offload = makeOffloadController();
553 offload.start();
554
555 OffloadHardwareInterface.ControlCallback callback = mControlCallbackCaptor.getValue();
556 callback.onStoppedLimitReached();
557 verify(mNMService, times(1)).tetherLimitReached(mTetherStatsProviderCaptor.getValue());
558 }
Erik Klineb3bb26e2017-07-06 19:49:35 +0900559
560 @Test
561 public void testAddRemoveDownstreams() throws Exception {
562 setupFunctioningHardwareInterface();
563 enableOffload();
564
565 final OffloadController offload = makeOffloadController();
566 offload.start();
567
568 final InOrder inOrder = inOrder(mHardware);
569 inOrder.verify(mHardware, times(1)).initOffloadConfig();
570 inOrder.verify(mHardware, times(1)).initOffloadControl(
571 any(OffloadHardwareInterface.ControlCallback.class));
572 inOrder.verifyNoMoreInteractions();
573
574 // Tethering makes several calls to setLocalPrefixes() before add/remove
575 // downstream calls are made. This is not tested here; only the behavior
576 // of notifyDownstreamLinkProperties() and removeDownstreamInterface()
577 // are tested.
578
579 // [1] USB tethering is started.
580 final LinkProperties usbLinkProperties = new LinkProperties();
581 usbLinkProperties.setInterfaceName(RNDIS0);
582 usbLinkProperties.addLinkAddress(new LinkAddress("192.168.42.1/24"));
583 usbLinkProperties.addRoute(new RouteInfo(new IpPrefix(USB_PREFIX)));
584 offload.notifyDownstreamLinkProperties(usbLinkProperties);
585 inOrder.verify(mHardware, times(1)).addDownstreamPrefix(RNDIS0, USB_PREFIX);
586 inOrder.verifyNoMoreInteractions();
587
588 // [2] Routes for IPv6 link-local prefixes should never be added.
589 usbLinkProperties.addRoute(new RouteInfo(new IpPrefix(IPV6_LINKLOCAL)));
590 offload.notifyDownstreamLinkProperties(usbLinkProperties);
591 inOrder.verify(mHardware, never()).addDownstreamPrefix(eq(RNDIS0), anyString());
592 inOrder.verifyNoMoreInteractions();
593
594 // [3] Add an IPv6 prefix for good measure. Only new offload-able
595 // prefixes should be passed to the HAL.
596 usbLinkProperties.addLinkAddress(new LinkAddress("2001:db8::1/64"));
597 usbLinkProperties.addRoute(new RouteInfo(new IpPrefix(IPV6_DOC_PREFIX)));
598 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.
610 usbLinkProperties.removeRoute(new RouteInfo(new IpPrefix(IPV6_DOC_PREFIX), null, RNDIS0));
611 usbLinkProperties.addRoute(new RouteInfo(new IpPrefix(IPV6_DISCARD_PREFIX)));
612 offload.notifyDownstreamLinkProperties(usbLinkProperties);
613 inOrder.verify(mHardware, times(1)).removeDownstreamPrefix(RNDIS0, IPV6_DOC_PREFIX);
614 inOrder.verify(mHardware, times(1)).addDownstreamPrefix(RNDIS0, IPV6_DISCARD_PREFIX);
615 inOrder.verifyNoMoreInteractions();
616
617 // [6] Removing a downstream interface which was never added causes no
618 // interactions with the HAL.
619 offload.removeDownstreamInterface(WLAN0);
620 inOrder.verifyNoMoreInteractions();
621
622 // [7] Removing an active downstream removes all remaining prefixes.
623 offload.removeDownstreamInterface(RNDIS0);
624 inOrder.verify(mHardware, times(1)).removeDownstreamPrefix(RNDIS0, USB_PREFIX);
625 inOrder.verify(mHardware, times(1)).removeDownstreamPrefix(RNDIS0, IPV6_DISCARD_PREFIX);
626 inOrder.verifyNoMoreInteractions();
627 }
628
629 private static void assertArrayListContains(ArrayList<String> list, String... elems) {
630 for (String element : elems) {
631 assertTrue(list.contains(element));
632 }
633 }
Erik Kline92c4db02017-05-31 10:21:32 +0900634}