blob: 360e5f6ead099496b746d5d209961d22291ff394 [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;
71import org.junit.runner.RunWith;
72import org.junit.Test;
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
78
79@RunWith(AndroidJUnit4.class)
80@SmallTest
81public class OffloadControllerTest {
82
83 @Mock private OffloadHardwareInterface mHardware;
Erik Klineec372752017-06-07 16:33:19 +090084 @Mock private ApplicationInfo mApplicationInfo;
Erik Kline92c4db02017-05-31 10:21:32 +090085 @Mock private Context mContext;
Lorenzo Colitti4ca0ba82017-07-12 15:48:07 +090086 @Mock private INetworkManagementService mNMService;
Erik Klinedd4d5822017-06-12 18:20:08 +090087 private final ArgumentCaptor<ArrayList> mStringArrayCaptor =
88 ArgumentCaptor.forClass(ArrayList.class);
Lorenzo Colitti4ca0ba82017-07-12 15:48:07 +090089 private final ArgumentCaptor<ITetheringStatsProvider.Stub> mTetherStatsProviderCaptor =
90 ArgumentCaptor.forClass(ITetheringStatsProvider.Stub.class);
Lorenzo Colitti9f0baa92017-08-15 19:25:51 +090091 private final ArgumentCaptor<OffloadHardwareInterface.ControlCallback> mControlCallbackCaptor =
92 ArgumentCaptor.forClass(OffloadHardwareInterface.ControlCallback.class);
Erik Kline92c4db02017-05-31 10:21:32 +090093 private MockContentResolver mContentResolver;
94
Lorenzo Colitti4ca0ba82017-07-12 15:48:07 +090095 @Before public void setUp() {
Erik Kline92c4db02017-05-31 10:21:32 +090096 MockitoAnnotations.initMocks(this);
Erik Klineec372752017-06-07 16:33:19 +090097 when(mContext.getApplicationInfo()).thenReturn(mApplicationInfo);
98 when(mContext.getPackageName()).thenReturn("OffloadControllerTest");
Erik Kline92c4db02017-05-31 10:21:32 +090099 mContentResolver = new MockContentResolver(mContext);
100 mContentResolver.addProvider(Settings.AUTHORITY, new FakeSettingsProvider());
Erik Kline92c4db02017-05-31 10:21:32 +0900101 when(mContext.getContentResolver()).thenReturn(mContentResolver);
Erik Kline5eaa7ed2017-07-07 17:38:30 +0900102 // TODO: call this when available.
103 // FakeSettingsProvider.clearSettingsProvider();
104 }
105
106 @After public void tearDown() throws Exception {
107 // TODO: call this when available.
108 // FakeSettingsProvider.clearSettingsProvider();
Erik Kline92c4db02017-05-31 10:21:32 +0900109 }
110
111 private void setupFunctioningHardwareInterface() {
112 when(mHardware.initOffloadConfig()).thenReturn(true);
Lorenzo Colitti9f0baa92017-08-15 19:25:51 +0900113 when(mHardware.initOffloadControl(mControlCallbackCaptor.capture()))
Erik Kline92c4db02017-05-31 10:21:32 +0900114 .thenReturn(true);
Lorenzo Colitti4ca0ba82017-07-12 15:48:07 +0900115 when(mHardware.getForwardedStats(any())).thenReturn(new ForwardedStats());
Erik Kline92c4db02017-05-31 10:21:32 +0900116 }
117
Erik Kline5eaa7ed2017-07-07 17:38:30 +0900118 private void enableOffload() {
119 Settings.Global.putInt(mContentResolver, TETHER_OFFLOAD_DISABLED, 0);
120 }
121
Lorenzo Colitti50b60fc2017-08-11 13:47:49 +0900122 private void waitForIdle() {
123 ConditionVariable cv = new ConditionVariable();
124 new Handler(Looper.getMainLooper()).post(() -> { cv.open(); });
125 cv.block();
126 }
127
Lorenzo Colitti4ca0ba82017-07-12 15:48:07 +0900128 private OffloadController makeOffloadController() throws Exception {
129 OffloadController offload = new OffloadController(new Handler(Looper.getMainLooper()),
130 mHardware, mContentResolver, mNMService, new SharedLog("test"));
131 verify(mNMService).registerTetheringStatsProvider(
132 mTetherStatsProviderCaptor.capture(), anyString());
133 return offload;
134 }
135
Erik Kline5eaa7ed2017-07-07 17:38:30 +0900136 // TODO: Restore when FakeSettingsProvider.clearSettingsProvider() is available.
137 // @Test
Lorenzo Colitti4ca0ba82017-07-12 15:48:07 +0900138 public void testNoSettingsValueDefaultDisabledDoesNotStart() throws Exception {
Erik Kline92c4db02017-05-31 10:21:32 +0900139 setupFunctioningHardwareInterface();
Erik Kline5eaa7ed2017-07-07 17:38:30 +0900140 when(mHardware.getDefaultTetherOffloadDisabled()).thenReturn(1);
Erik Kline92c4db02017-05-31 10:21:32 +0900141 try {
142 Settings.Global.getInt(mContentResolver, TETHER_OFFLOAD_DISABLED);
143 fail();
144 } catch (SettingNotFoundException expected) {}
145
Lorenzo Colitti4ca0ba82017-07-12 15:48:07 +0900146 final OffloadController offload = makeOffloadController();
Erik Kline92c4db02017-05-31 10:21:32 +0900147 offload.start();
148
149 final InOrder inOrder = inOrder(mHardware);
Erik Kline5eaa7ed2017-07-07 17:38:30 +0900150 inOrder.verify(mHardware, times(1)).getDefaultTetherOffloadDisabled();
151 inOrder.verify(mHardware, never()).initOffloadConfig();
152 inOrder.verify(mHardware, never()).initOffloadControl(
153 any(OffloadHardwareInterface.ControlCallback.class));
154 inOrder.verifyNoMoreInteractions();
155 }
156
157 // TODO: Restore when FakeSettingsProvider.clearSettingsProvider() is available.
158 // @Test
Lorenzo Colitti4ca0ba82017-07-12 15:48:07 +0900159 public void testNoSettingsValueDefaultEnabledDoesStart() throws Exception {
Erik Kline5eaa7ed2017-07-07 17:38:30 +0900160 setupFunctioningHardwareInterface();
161 when(mHardware.getDefaultTetherOffloadDisabled()).thenReturn(0);
162 try {
163 Settings.Global.getInt(mContentResolver, TETHER_OFFLOAD_DISABLED);
164 fail();
165 } catch (SettingNotFoundException expected) {}
166
Lorenzo Colitti4ca0ba82017-07-12 15:48:07 +0900167 final OffloadController offload = makeOffloadController();
Erik Kline5eaa7ed2017-07-07 17:38:30 +0900168 offload.start();
169
170 final InOrder inOrder = inOrder(mHardware);
171 inOrder.verify(mHardware, times(1)).getDefaultTetherOffloadDisabled();
Erik Kline92c4db02017-05-31 10:21:32 +0900172 inOrder.verify(mHardware, times(1)).initOffloadConfig();
173 inOrder.verify(mHardware, times(1)).initOffloadControl(
174 any(OffloadHardwareInterface.ControlCallback.class));
175 inOrder.verifyNoMoreInteractions();
176 }
177
178 @Test
Lorenzo Colitti4ca0ba82017-07-12 15:48:07 +0900179 public void testSettingsAllowsStart() throws Exception {
Erik Kline92c4db02017-05-31 10:21:32 +0900180 setupFunctioningHardwareInterface();
181 Settings.Global.putInt(mContentResolver, TETHER_OFFLOAD_DISABLED, 0);
182
Lorenzo Colitti4ca0ba82017-07-12 15:48:07 +0900183 final OffloadController offload = makeOffloadController();
Erik Kline92c4db02017-05-31 10:21:32 +0900184 offload.start();
185
186 final InOrder inOrder = inOrder(mHardware);
Erik Kline5eaa7ed2017-07-07 17:38:30 +0900187 inOrder.verify(mHardware, times(1)).getDefaultTetherOffloadDisabled();
Erik Kline92c4db02017-05-31 10:21:32 +0900188 inOrder.verify(mHardware, times(1)).initOffloadConfig();
189 inOrder.verify(mHardware, times(1)).initOffloadControl(
190 any(OffloadHardwareInterface.ControlCallback.class));
191 inOrder.verifyNoMoreInteractions();
192 }
193
194 @Test
Lorenzo Colitti4ca0ba82017-07-12 15:48:07 +0900195 public void testSettingsDisablesStart() throws Exception {
Erik Kline92c4db02017-05-31 10:21:32 +0900196 setupFunctioningHardwareInterface();
197 Settings.Global.putInt(mContentResolver, TETHER_OFFLOAD_DISABLED, 1);
198
Lorenzo Colitti4ca0ba82017-07-12 15:48:07 +0900199 final OffloadController offload = makeOffloadController();
Erik Kline92c4db02017-05-31 10:21:32 +0900200 offload.start();
201
202 final InOrder inOrder = inOrder(mHardware);
Erik Kline5eaa7ed2017-07-07 17:38:30 +0900203 inOrder.verify(mHardware, times(1)).getDefaultTetherOffloadDisabled();
Erik Kline92c4db02017-05-31 10:21:32 +0900204 inOrder.verify(mHardware, never()).initOffloadConfig();
205 inOrder.verify(mHardware, never()).initOffloadControl(anyObject());
206 inOrder.verifyNoMoreInteractions();
207 }
Erik Kline7990aef2017-06-01 20:11:25 +0900208
209 @Test
210 public void testSetUpstreamLinkPropertiesWorking() throws Exception {
211 setupFunctioningHardwareInterface();
Erik Kline5eaa7ed2017-07-07 17:38:30 +0900212 enableOffload();
213
Lorenzo Colitti4ca0ba82017-07-12 15:48:07 +0900214 final OffloadController offload = makeOffloadController();
Erik Kline7990aef2017-06-01 20:11:25 +0900215 offload.start();
216
217 final InOrder inOrder = inOrder(mHardware);
Erik Kline5eaa7ed2017-07-07 17:38:30 +0900218 inOrder.verify(mHardware, times(1)).getDefaultTetherOffloadDisabled();
Erik Kline7990aef2017-06-01 20:11:25 +0900219 inOrder.verify(mHardware, times(1)).initOffloadConfig();
220 inOrder.verify(mHardware, times(1)).initOffloadControl(
221 any(OffloadHardwareInterface.ControlCallback.class));
222 inOrder.verifyNoMoreInteractions();
223
Erik Kline5acb4e32017-07-04 18:28:11 +0900224 // In reality, the UpstreamNetworkMonitor would have passed down to us
225 // a covering set of local prefixes representing a minimum essential
226 // set plus all the prefixes on networks with network agents.
227 //
228 // We simulate that there, and then add upstream elements one by one
229 // and watch what happens.
230 final Set<IpPrefix> minimumLocalPrefixes = new HashSet<>();
231 for (String s : new String[]{
232 "127.0.0.0/8", "192.0.2.0/24", "fe80::/64", "2001:db8::/64"}) {
233 minimumLocalPrefixes.add(new IpPrefix(s));
234 }
235 offload.setLocalPrefixes(minimumLocalPrefixes);
236 inOrder.verify(mHardware, times(1)).setLocalPrefixes(mStringArrayCaptor.capture());
237 ArrayList<String> localPrefixes = mStringArrayCaptor.getValue();
238 assertEquals(4, localPrefixes.size());
239 assertTrue(localPrefixes.contains("127.0.0.0/8"));
240 assertTrue(localPrefixes.contains("192.0.2.0/24"));
241 assertTrue(localPrefixes.contains("fe80::/64"));
242 assertTrue(localPrefixes.contains("2001:db8::/64"));
Erik Kline7990aef2017-06-01 20:11:25 +0900243 inOrder.verifyNoMoreInteractions();
Erik Kline5acb4e32017-07-04 18:28:11 +0900244
245 offload.setUpstreamLinkProperties(null);
246 // No change in local addresses means no call to setLocalPrefixes().
247 inOrder.verify(mHardware, never()).setLocalPrefixes(mStringArrayCaptor.capture());
248 // This LinkProperties value does not differ from the default upstream.
249 // There should be no extraneous call to setUpstreamParameters().
250 inOrder.verify(mHardware, never()).setUpstreamParameters(
251 anyObject(), anyObject(), anyObject(), anyObject());
252 inOrder.verifyNoMoreInteractions();
Erik Kline7990aef2017-06-01 20:11:25 +0900253
254 final LinkProperties lp = new LinkProperties();
255
256 final String testIfName = "rmnet_data17";
257 lp.setInterfaceName(testIfName);
258 offload.setUpstreamLinkProperties(lp);
Erik Kline5acb4e32017-07-04 18:28:11 +0900259 // No change in local addresses means no call to setLocalPrefixes().
260 inOrder.verify(mHardware, never()).setLocalPrefixes(mStringArrayCaptor.capture());
Erik Kline7990aef2017-06-01 20:11:25 +0900261 inOrder.verify(mHardware, times(1)).setUpstreamParameters(
Erik Kline664d2082017-06-15 18:06:34 +0900262 eq(testIfName), eq(null), eq(null), eq(null));
Erik Kline7990aef2017-06-01 20:11:25 +0900263 inOrder.verifyNoMoreInteractions();
264
265 final String ipv4Addr = "192.0.2.5";
266 final String linkAddr = ipv4Addr + "/24";
267 lp.addLinkAddress(new LinkAddress(linkAddr));
Erik Kline5acb4e32017-07-04 18:28:11 +0900268 lp.addRoute(new RouteInfo(new IpPrefix("192.0.2.0/24")));
Erik Kline7990aef2017-06-01 20:11:25 +0900269 offload.setUpstreamLinkProperties(lp);
Erik Kline5acb4e32017-07-04 18:28:11 +0900270 // IPv4 prefixes and addresses on the upstream are simply left as whole
271 // prefixes (already passed in from UpstreamNetworkMonitor code). If a
272 // tethering client sends traffic to the IPv4 default router or other
273 // clients on the upstream this will not be hardware-forwarded, and that
274 // should be fine for now. Ergo: no change in local addresses, no call
275 // to setLocalPrefixes().
276 inOrder.verify(mHardware, never()).setLocalPrefixes(mStringArrayCaptor.capture());
Erik Kline7990aef2017-06-01 20:11:25 +0900277 inOrder.verify(mHardware, times(1)).setUpstreamParameters(
Erik Kline664d2082017-06-15 18:06:34 +0900278 eq(testIfName), eq(ipv4Addr), eq(null), eq(null));
Lorenzo Colitti4ca0ba82017-07-12 15:48:07 +0900279 inOrder.verify(mHardware, times(1)).getForwardedStats(eq(testIfName));
Erik Kline7990aef2017-06-01 20:11:25 +0900280 inOrder.verifyNoMoreInteractions();
281
282 final String ipv4Gateway = "192.0.2.1";
283 lp.addRoute(new RouteInfo(InetAddress.getByName(ipv4Gateway)));
284 offload.setUpstreamLinkProperties(lp);
Erik Kline5acb4e32017-07-04 18:28:11 +0900285 // No change in local addresses means no call to setLocalPrefixes().
286 inOrder.verify(mHardware, never()).setLocalPrefixes(mStringArrayCaptor.capture());
Erik Kline7990aef2017-06-01 20:11:25 +0900287 inOrder.verify(mHardware, times(1)).setUpstreamParameters(
Erik Kline664d2082017-06-15 18:06:34 +0900288 eq(testIfName), eq(ipv4Addr), eq(ipv4Gateway), eq(null));
Lorenzo Colitti4ca0ba82017-07-12 15:48:07 +0900289 inOrder.verify(mHardware, times(1)).getForwardedStats(eq(testIfName));
Erik Kline7990aef2017-06-01 20:11:25 +0900290 inOrder.verifyNoMoreInteractions();
291
292 final String ipv6Gw1 = "fe80::cafe";
293 lp.addRoute(new RouteInfo(InetAddress.getByName(ipv6Gw1)));
294 offload.setUpstreamLinkProperties(lp);
Erik Kline5acb4e32017-07-04 18:28:11 +0900295 // No change in local addresses means no call to setLocalPrefixes().
296 inOrder.verify(mHardware, never()).setLocalPrefixes(mStringArrayCaptor.capture());
Erik Kline7990aef2017-06-01 20:11:25 +0900297 inOrder.verify(mHardware, times(1)).setUpstreamParameters(
298 eq(testIfName), eq(ipv4Addr), eq(ipv4Gateway), mStringArrayCaptor.capture());
Lorenzo Colitti4ca0ba82017-07-12 15:48:07 +0900299 inOrder.verify(mHardware, times(1)).getForwardedStats(eq(testIfName));
Erik Kline7990aef2017-06-01 20:11:25 +0900300 ArrayList<String> v6gws = mStringArrayCaptor.getValue();
301 assertEquals(1, v6gws.size());
302 assertTrue(v6gws.contains(ipv6Gw1));
303 inOrder.verifyNoMoreInteractions();
304
305 final String ipv6Gw2 = "fe80::d00d";
306 lp.addRoute(new RouteInfo(InetAddress.getByName(ipv6Gw2)));
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 v6gws = mStringArrayCaptor.getValue();
314 assertEquals(2, v6gws.size());
315 assertTrue(v6gws.contains(ipv6Gw1));
316 assertTrue(v6gws.contains(ipv6Gw2));
317 inOrder.verifyNoMoreInteractions();
318
319 final LinkProperties stacked = new LinkProperties();
320 stacked.setInterfaceName("stacked");
321 stacked.addLinkAddress(new LinkAddress("192.0.2.129/25"));
322 stacked.addRoute(new RouteInfo(InetAddress.getByName("192.0.2.254")));
323 stacked.addRoute(new RouteInfo(InetAddress.getByName("fe80::bad:f00")));
324 assertTrue(lp.addStackedLink(stacked));
325 offload.setUpstreamLinkProperties(lp);
Erik Kline5acb4e32017-07-04 18:28:11 +0900326 // No change in local addresses means no call to setLocalPrefixes().
327 inOrder.verify(mHardware, never()).setLocalPrefixes(mStringArrayCaptor.capture());
Erik Kline7990aef2017-06-01 20:11:25 +0900328 inOrder.verify(mHardware, times(1)).setUpstreamParameters(
329 eq(testIfName), eq(ipv4Addr), eq(ipv4Gateway), mStringArrayCaptor.capture());
Lorenzo Colitti4ca0ba82017-07-12 15:48:07 +0900330 inOrder.verify(mHardware, times(1)).getForwardedStats(eq(testIfName));
Erik Kline7990aef2017-06-01 20:11:25 +0900331 v6gws = mStringArrayCaptor.getValue();
332 assertEquals(2, v6gws.size());
333 assertTrue(v6gws.contains(ipv6Gw1));
334 assertTrue(v6gws.contains(ipv6Gw2));
335 inOrder.verifyNoMoreInteractions();
Erik Kline5acb4e32017-07-04 18:28:11 +0900336
337 // Add in some IPv6 upstream info. When there is a tethered downstream
338 // making use of the IPv6 prefix we would expect to see the /64 route
339 // removed from "local prefixes" and /128s added for the upstream IPv6
340 // addresses. This is not yet implemented, and for now we simply
341 // expect to see these /128s.
342 lp.addRoute(new RouteInfo(new IpPrefix("2001:db8::/64")));
343 // "2001:db8::/64" plus "assigned" ASCII in hex
344 lp.addLinkAddress(new LinkAddress("2001:db8::6173:7369:676e:6564/64"));
345 // "2001:db8::/64" plus "random" ASCII in hex
346 lp.addLinkAddress(new LinkAddress("2001:db8::7261:6e64:6f6d/64"));
347 offload.setUpstreamLinkProperties(lp);
348 inOrder.verify(mHardware, times(1)).setLocalPrefixes(mStringArrayCaptor.capture());
349 localPrefixes = mStringArrayCaptor.getValue();
350 assertEquals(6, localPrefixes.size());
351 assertTrue(localPrefixes.contains("127.0.0.0/8"));
352 assertTrue(localPrefixes.contains("192.0.2.0/24"));
353 assertTrue(localPrefixes.contains("fe80::/64"));
354 assertTrue(localPrefixes.contains("2001:db8::/64"));
355 assertTrue(localPrefixes.contains("2001:db8::6173:7369:676e:6564/128"));
356 assertTrue(localPrefixes.contains("2001:db8::7261:6e64:6f6d/128"));
357 // The relevant parts of the LinkProperties have not changed, but at the
358 // moment we do not de-dup upstream LinkProperties this carefully.
359 inOrder.verify(mHardware, times(1)).setUpstreamParameters(
360 eq(testIfName), eq(ipv4Addr), eq(ipv4Gateway), mStringArrayCaptor.capture());
361 v6gws = mStringArrayCaptor.getValue();
362 assertEquals(2, v6gws.size());
363 assertTrue(v6gws.contains(ipv6Gw1));
364 assertTrue(v6gws.contains(ipv6Gw2));
Lorenzo Colitti4ca0ba82017-07-12 15:48:07 +0900365 inOrder.verify(mHardware, times(1)).getForwardedStats(eq(testIfName));
Erik Kline5acb4e32017-07-04 18:28:11 +0900366 inOrder.verifyNoMoreInteractions();
367
368 // Completely identical LinkProperties updates are de-duped.
369 offload.setUpstreamLinkProperties(lp);
370 // This LinkProperties value does not differ from the default upstream.
371 // There should be no extraneous call to setUpstreamParameters().
372 inOrder.verify(mHardware, never()).setUpstreamParameters(
373 anyObject(), anyObject(), anyObject(), anyObject());
374 inOrder.verifyNoMoreInteractions();
Erik Kline7990aef2017-06-01 20:11:25 +0900375 }
Lorenzo Colitti4ca0ba82017-07-12 15:48:07 +0900376
377 private void assertNetworkStats(String iface, ForwardedStats stats, NetworkStats.Entry entry) {
378 assertEquals(iface, entry.iface);
379 assertEquals(stats.rxBytes, entry.rxBytes);
380 assertEquals(stats.txBytes, entry.txBytes);
381 assertEquals(SET_DEFAULT, entry.set);
382 assertEquals(TAG_NONE, entry.tag);
Lorenzo Colitti4ca0ba82017-07-12 15:48:07 +0900383 }
384
385 @Test
386 public void testGetForwardedStats() throws Exception {
387 setupFunctioningHardwareInterface();
388 enableOffload();
389
390 final OffloadController offload = makeOffloadController();
391 offload.start();
392
393 final String ethernetIface = "eth1";
394 final String mobileIface = "rmnet_data0";
395
396 ForwardedStats ethernetStats = new ForwardedStats();
397 ethernetStats.rxBytes = 12345;
398 ethernetStats.txBytes = 54321;
399
400 ForwardedStats mobileStats = new ForwardedStats();
401 mobileStats.rxBytes = 999;
402 mobileStats.txBytes = 99999;
403
404 when(mHardware.getForwardedStats(eq(ethernetIface))).thenReturn(ethernetStats);
405 when(mHardware.getForwardedStats(eq(mobileIface))).thenReturn(mobileStats);
406
407 final LinkProperties lp = new LinkProperties();
408 lp.setInterfaceName(ethernetIface);
409 offload.setUpstreamLinkProperties(lp);
410
411 lp.setInterfaceName(mobileIface);
412 offload.setUpstreamLinkProperties(lp);
413
414 lp.setInterfaceName(ethernetIface);
415 offload.setUpstreamLinkProperties(lp);
416
417 ethernetStats.rxBytes = 100000;
418 ethernetStats.txBytes = 100000;
419 offload.setUpstreamLinkProperties(null);
420
Lorenzo Colittif1912ca2017-08-17 19:23:08 +0900421 ITetheringStatsProvider provider = mTetherStatsProviderCaptor.getValue();
422 NetworkStats stats = provider.getTetherStats(STATS_PER_IFACE);
423 NetworkStats perUidStats = provider.getTetherStats(STATS_PER_UID);
424
Lorenzo Colitti4ca0ba82017-07-12 15:48:07 +0900425 assertEquals(2, stats.size());
Lorenzo Colittif1912ca2017-08-17 19:23:08 +0900426 assertEquals(2, perUidStats.size());
Lorenzo Colitti4ca0ba82017-07-12 15:48:07 +0900427
428 NetworkStats.Entry entry = null;
Lorenzo Colittif1912ca2017-08-17 19:23:08 +0900429 for (int i = 0; i < stats.size(); i++) {
430 assertEquals(UID_ALL, stats.getValues(i, entry).uid);
431 assertEquals(UID_TETHERING, perUidStats.getValues(i, entry).uid);
432 }
433
Lorenzo Colitti4ca0ba82017-07-12 15:48:07 +0900434 int ethernetPosition = ethernetIface.equals(stats.getValues(0, entry).iface) ? 0 : 1;
435 int mobilePosition = 1 - ethernetPosition;
436
437 entry = stats.getValues(mobilePosition, entry);
438 assertNetworkStats(mobileIface, mobileStats, entry);
Lorenzo Colittif1912ca2017-08-17 19:23:08 +0900439 entry = perUidStats.getValues(mobilePosition, entry);
440 assertNetworkStats(mobileIface, mobileStats, entry);
Lorenzo Colitti4ca0ba82017-07-12 15:48:07 +0900441
442 ethernetStats.rxBytes = 12345 + 100000;
443 ethernetStats.txBytes = 54321 + 100000;
444 entry = stats.getValues(ethernetPosition, entry);
445 assertNetworkStats(ethernetIface, ethernetStats, entry);
Lorenzo Colittif1912ca2017-08-17 19:23:08 +0900446 entry = perUidStats.getValues(ethernetPosition, entry);
447 assertNetworkStats(ethernetIface, ethernetStats, entry);
Lorenzo Colitti4ca0ba82017-07-12 15:48:07 +0900448 }
Lorenzo Colitti50b60fc2017-08-11 13:47:49 +0900449
450 @Test
451 public void testSetInterfaceQuota() throws Exception {
452 setupFunctioningHardwareInterface();
453 enableOffload();
454
455 final OffloadController offload = makeOffloadController();
456 offload.start();
457
458 final String ethernetIface = "eth1";
459 final String mobileIface = "rmnet_data0";
460 final long ethernetLimit = 12345;
461 final long mobileLimit = 12345678;
462
463 final LinkProperties lp = new LinkProperties();
464 lp.setInterfaceName(ethernetIface);
465 offload.setUpstreamLinkProperties(lp);
466
467 ITetheringStatsProvider provider = mTetherStatsProviderCaptor.getValue();
468 final InOrder inOrder = inOrder(mHardware);
469 when(mHardware.setUpstreamParameters(any(), any(), any(), any())).thenReturn(true);
470 when(mHardware.setDataLimit(anyString(), anyLong())).thenReturn(true);
471
472 // Applying an interface quota to the current upstream immediately sends it to the hardware.
473 provider.setInterfaceQuota(ethernetIface, ethernetLimit);
474 waitForIdle();
475 inOrder.verify(mHardware).setDataLimit(ethernetIface, ethernetLimit);
476 inOrder.verifyNoMoreInteractions();
477
478 // Applying an interface quota to another upstream does not take any immediate action.
479 provider.setInterfaceQuota(mobileIface, mobileLimit);
480 waitForIdle();
481 inOrder.verify(mHardware, never()).setDataLimit(anyString(), anyLong());
482
483 // Switching to that upstream causes the quota to be applied if the parameters were applied
484 // correctly.
485 lp.setInterfaceName(mobileIface);
486 offload.setUpstreamLinkProperties(lp);
487 waitForIdle();
488 inOrder.verify(mHardware).setDataLimit(mobileIface, mobileLimit);
489
490 // Setting a limit of ITetheringStatsProvider.QUOTA_UNLIMITED causes the limit to be set
491 // to Long.MAX_VALUE.
492 provider.setInterfaceQuota(mobileIface, ITetheringStatsProvider.QUOTA_UNLIMITED);
493 waitForIdle();
494 inOrder.verify(mHardware).setDataLimit(mobileIface, Long.MAX_VALUE);
495
496 // If setting upstream parameters fails, then the data limit is not set.
497 when(mHardware.setUpstreamParameters(any(), any(), any(), any())).thenReturn(false);
498 lp.setInterfaceName(ethernetIface);
499 offload.setUpstreamLinkProperties(lp);
500 provider.setInterfaceQuota(mobileIface, mobileLimit);
501 waitForIdle();
502 inOrder.verify(mHardware, never()).setDataLimit(anyString(), anyLong());
503
504 // If setting the data limit fails while changing upstreams, offload is stopped.
505 when(mHardware.setUpstreamParameters(any(), any(), any(), any())).thenReturn(true);
506 when(mHardware.setDataLimit(anyString(), anyLong())).thenReturn(false);
507 lp.setInterfaceName(mobileIface);
508 offload.setUpstreamLinkProperties(lp);
509 provider.setInterfaceQuota(mobileIface, mobileLimit);
510 waitForIdle();
511 inOrder.verify(mHardware).stopOffloadControl();
512 }
Lorenzo Colitti9f0baa92017-08-15 19:25:51 +0900513
514 @Test
515 public void testDataLimitCallback() throws Exception {
516 setupFunctioningHardwareInterface();
517 enableOffload();
518
519 final OffloadController offload = makeOffloadController();
520 offload.start();
521
522 OffloadHardwareInterface.ControlCallback callback = mControlCallbackCaptor.getValue();
523 callback.onStoppedLimitReached();
524 verify(mNMService, times(1)).tetherLimitReached(mTetherStatsProviderCaptor.getValue());
525 }
Erik Kline92c4db02017-05-31 10:21:32 +0900526}