blob: c769492424a469e199e51b7aca55ca37a527840a [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 Colittiddce7ee2017-08-21 12:34:50 +0900115 when(mHardware.setUpstreamParameters(anyString(), any(), any(), any())).thenReturn(true);
Lorenzo Colitti4ca0ba82017-07-12 15:48:07 +0900116 when(mHardware.getForwardedStats(any())).thenReturn(new ForwardedStats());
Lorenzo Colittiddce7ee2017-08-21 12:34:50 +0900117 when(mHardware.setDataLimit(anyString(), anyLong())).thenReturn(true);
Erik Kline92c4db02017-05-31 10:21:32 +0900118 }
119
Erik Kline5eaa7ed2017-07-07 17:38:30 +0900120 private void enableOffload() {
121 Settings.Global.putInt(mContentResolver, TETHER_OFFLOAD_DISABLED, 0);
122 }
123
Lorenzo Colitti50b60fc2017-08-11 13:47:49 +0900124 private void waitForIdle() {
125 ConditionVariable cv = new ConditionVariable();
126 new Handler(Looper.getMainLooper()).post(() -> { cv.open(); });
127 cv.block();
128 }
129
Lorenzo Colitti4ca0ba82017-07-12 15:48:07 +0900130 private OffloadController makeOffloadController() throws Exception {
131 OffloadController offload = new OffloadController(new Handler(Looper.getMainLooper()),
132 mHardware, mContentResolver, mNMService, new SharedLog("test"));
133 verify(mNMService).registerTetheringStatsProvider(
134 mTetherStatsProviderCaptor.capture(), anyString());
135 return offload;
136 }
137
Erik Kline5eaa7ed2017-07-07 17:38:30 +0900138 // TODO: Restore when FakeSettingsProvider.clearSettingsProvider() is available.
139 // @Test
Lorenzo Colitti4ca0ba82017-07-12 15:48:07 +0900140 public void testNoSettingsValueDefaultDisabledDoesNotStart() throws Exception {
Erik Kline92c4db02017-05-31 10:21:32 +0900141 setupFunctioningHardwareInterface();
Erik Kline5eaa7ed2017-07-07 17:38:30 +0900142 when(mHardware.getDefaultTetherOffloadDisabled()).thenReturn(1);
Erik Kline92c4db02017-05-31 10:21:32 +0900143 try {
144 Settings.Global.getInt(mContentResolver, TETHER_OFFLOAD_DISABLED);
145 fail();
146 } catch (SettingNotFoundException expected) {}
147
Lorenzo Colitti4ca0ba82017-07-12 15:48:07 +0900148 final OffloadController offload = makeOffloadController();
Erik Kline92c4db02017-05-31 10:21:32 +0900149 offload.start();
150
151 final InOrder inOrder = inOrder(mHardware);
Erik Kline5eaa7ed2017-07-07 17:38:30 +0900152 inOrder.verify(mHardware, times(1)).getDefaultTetherOffloadDisabled();
153 inOrder.verify(mHardware, never()).initOffloadConfig();
154 inOrder.verify(mHardware, never()).initOffloadControl(
155 any(OffloadHardwareInterface.ControlCallback.class));
156 inOrder.verifyNoMoreInteractions();
157 }
158
159 // TODO: Restore when FakeSettingsProvider.clearSettingsProvider() is available.
160 // @Test
Lorenzo Colitti4ca0ba82017-07-12 15:48:07 +0900161 public void testNoSettingsValueDefaultEnabledDoesStart() throws Exception {
Erik Kline5eaa7ed2017-07-07 17:38:30 +0900162 setupFunctioningHardwareInterface();
163 when(mHardware.getDefaultTetherOffloadDisabled()).thenReturn(0);
164 try {
165 Settings.Global.getInt(mContentResolver, TETHER_OFFLOAD_DISABLED);
166 fail();
167 } catch (SettingNotFoundException expected) {}
168
Lorenzo Colitti4ca0ba82017-07-12 15:48:07 +0900169 final OffloadController offload = makeOffloadController();
Erik Kline5eaa7ed2017-07-07 17:38:30 +0900170 offload.start();
171
172 final InOrder inOrder = inOrder(mHardware);
173 inOrder.verify(mHardware, times(1)).getDefaultTetherOffloadDisabled();
Erik Kline92c4db02017-05-31 10:21:32 +0900174 inOrder.verify(mHardware, times(1)).initOffloadConfig();
175 inOrder.verify(mHardware, times(1)).initOffloadControl(
176 any(OffloadHardwareInterface.ControlCallback.class));
177 inOrder.verifyNoMoreInteractions();
178 }
179
180 @Test
Lorenzo Colitti4ca0ba82017-07-12 15:48:07 +0900181 public void testSettingsAllowsStart() throws Exception {
Erik Kline92c4db02017-05-31 10:21:32 +0900182 setupFunctioningHardwareInterface();
183 Settings.Global.putInt(mContentResolver, TETHER_OFFLOAD_DISABLED, 0);
184
Lorenzo Colitti4ca0ba82017-07-12 15:48:07 +0900185 final OffloadController offload = makeOffloadController();
Erik Kline92c4db02017-05-31 10:21:32 +0900186 offload.start();
187
188 final InOrder inOrder = inOrder(mHardware);
Erik Kline5eaa7ed2017-07-07 17:38:30 +0900189 inOrder.verify(mHardware, times(1)).getDefaultTetherOffloadDisabled();
Erik Kline92c4db02017-05-31 10:21:32 +0900190 inOrder.verify(mHardware, times(1)).initOffloadConfig();
191 inOrder.verify(mHardware, times(1)).initOffloadControl(
192 any(OffloadHardwareInterface.ControlCallback.class));
193 inOrder.verifyNoMoreInteractions();
194 }
195
196 @Test
Lorenzo Colitti4ca0ba82017-07-12 15:48:07 +0900197 public void testSettingsDisablesStart() throws Exception {
Erik Kline92c4db02017-05-31 10:21:32 +0900198 setupFunctioningHardwareInterface();
199 Settings.Global.putInt(mContentResolver, TETHER_OFFLOAD_DISABLED, 1);
200
Lorenzo Colitti4ca0ba82017-07-12 15:48:07 +0900201 final OffloadController offload = makeOffloadController();
Erik Kline92c4db02017-05-31 10:21:32 +0900202 offload.start();
203
204 final InOrder inOrder = inOrder(mHardware);
Erik Kline5eaa7ed2017-07-07 17:38:30 +0900205 inOrder.verify(mHardware, times(1)).getDefaultTetherOffloadDisabled();
Erik Kline92c4db02017-05-31 10:21:32 +0900206 inOrder.verify(mHardware, never()).initOffloadConfig();
207 inOrder.verify(mHardware, never()).initOffloadControl(anyObject());
208 inOrder.verifyNoMoreInteractions();
209 }
Erik Kline7990aef2017-06-01 20:11:25 +0900210
211 @Test
212 public void testSetUpstreamLinkPropertiesWorking() throws Exception {
213 setupFunctioningHardwareInterface();
Erik Kline5eaa7ed2017-07-07 17:38:30 +0900214 enableOffload();
215
Lorenzo Colitti4ca0ba82017-07-12 15:48:07 +0900216 final OffloadController offload = makeOffloadController();
Erik Kline7990aef2017-06-01 20:11:25 +0900217 offload.start();
218
219 final InOrder inOrder = inOrder(mHardware);
Erik Kline5eaa7ed2017-07-07 17:38:30 +0900220 inOrder.verify(mHardware, times(1)).getDefaultTetherOffloadDisabled();
Erik Kline7990aef2017-06-01 20:11:25 +0900221 inOrder.verify(mHardware, times(1)).initOffloadConfig();
222 inOrder.verify(mHardware, times(1)).initOffloadControl(
223 any(OffloadHardwareInterface.ControlCallback.class));
224 inOrder.verifyNoMoreInteractions();
225
Erik Kline5acb4e32017-07-04 18:28:11 +0900226 // In reality, the UpstreamNetworkMonitor would have passed down to us
227 // a covering set of local prefixes representing a minimum essential
228 // set plus all the prefixes on networks with network agents.
229 //
230 // We simulate that there, and then add upstream elements one by one
231 // and watch what happens.
232 final Set<IpPrefix> minimumLocalPrefixes = new HashSet<>();
233 for (String s : new String[]{
234 "127.0.0.0/8", "192.0.2.0/24", "fe80::/64", "2001:db8::/64"}) {
235 minimumLocalPrefixes.add(new IpPrefix(s));
236 }
237 offload.setLocalPrefixes(minimumLocalPrefixes);
238 inOrder.verify(mHardware, times(1)).setLocalPrefixes(mStringArrayCaptor.capture());
239 ArrayList<String> localPrefixes = mStringArrayCaptor.getValue();
240 assertEquals(4, localPrefixes.size());
241 assertTrue(localPrefixes.contains("127.0.0.0/8"));
242 assertTrue(localPrefixes.contains("192.0.2.0/24"));
243 assertTrue(localPrefixes.contains("fe80::/64"));
244 assertTrue(localPrefixes.contains("2001:db8::/64"));
Erik Kline7990aef2017-06-01 20:11:25 +0900245 inOrder.verifyNoMoreInteractions();
Erik Kline5acb4e32017-07-04 18:28:11 +0900246
247 offload.setUpstreamLinkProperties(null);
248 // No change in local addresses means no call to setLocalPrefixes().
249 inOrder.verify(mHardware, never()).setLocalPrefixes(mStringArrayCaptor.capture());
250 // This LinkProperties value does not differ from the default upstream.
251 // There should be no extraneous call to setUpstreamParameters().
252 inOrder.verify(mHardware, never()).setUpstreamParameters(
253 anyObject(), anyObject(), anyObject(), anyObject());
254 inOrder.verifyNoMoreInteractions();
Erik Kline7990aef2017-06-01 20:11:25 +0900255
256 final LinkProperties lp = new LinkProperties();
257
258 final String testIfName = "rmnet_data17";
259 lp.setInterfaceName(testIfName);
260 offload.setUpstreamLinkProperties(lp);
Erik Kline5acb4e32017-07-04 18:28:11 +0900261 // No change in local addresses means no call to setLocalPrefixes().
262 inOrder.verify(mHardware, never()).setLocalPrefixes(mStringArrayCaptor.capture());
Erik Kline7990aef2017-06-01 20:11:25 +0900263 inOrder.verify(mHardware, times(1)).setUpstreamParameters(
Erik Kline664d2082017-06-15 18:06:34 +0900264 eq(testIfName), eq(null), eq(null), eq(null));
Lorenzo Colittiddce7ee2017-08-21 12:34:50 +0900265 inOrder.verify(mHardware, times(1)).setDataLimit(eq(testIfName), eq(Long.MAX_VALUE));
Erik Kline7990aef2017-06-01 20:11:25 +0900266 inOrder.verifyNoMoreInteractions();
267
268 final String ipv4Addr = "192.0.2.5";
269 final String linkAddr = ipv4Addr + "/24";
270 lp.addLinkAddress(new LinkAddress(linkAddr));
Erik Kline5acb4e32017-07-04 18:28:11 +0900271 lp.addRoute(new RouteInfo(new IpPrefix("192.0.2.0/24")));
Erik Kline7990aef2017-06-01 20:11:25 +0900272 offload.setUpstreamLinkProperties(lp);
Erik Kline5acb4e32017-07-04 18:28:11 +0900273 // IPv4 prefixes and addresses on the upstream are simply left as whole
274 // prefixes (already passed in from UpstreamNetworkMonitor code). If a
275 // tethering client sends traffic to the IPv4 default router or other
276 // clients on the upstream this will not be hardware-forwarded, and that
277 // should be fine for now. Ergo: no change in local addresses, no call
278 // to setLocalPrefixes().
279 inOrder.verify(mHardware, never()).setLocalPrefixes(mStringArrayCaptor.capture());
Erik Kline7990aef2017-06-01 20:11:25 +0900280 inOrder.verify(mHardware, times(1)).setUpstreamParameters(
Erik Kline664d2082017-06-15 18:06:34 +0900281 eq(testIfName), eq(ipv4Addr), eq(null), eq(null));
Lorenzo Colitti4ca0ba82017-07-12 15:48:07 +0900282 inOrder.verify(mHardware, times(1)).getForwardedStats(eq(testIfName));
Lorenzo Colittiddce7ee2017-08-21 12:34:50 +0900283 inOrder.verify(mHardware, times(1)).setDataLimit(eq(testIfName), eq(Long.MAX_VALUE));
Erik Kline7990aef2017-06-01 20:11:25 +0900284 inOrder.verifyNoMoreInteractions();
285
286 final String ipv4Gateway = "192.0.2.1";
287 lp.addRoute(new RouteInfo(InetAddress.getByName(ipv4Gateway)));
288 offload.setUpstreamLinkProperties(lp);
Erik Kline5acb4e32017-07-04 18:28:11 +0900289 // No change in local addresses means no call to setLocalPrefixes().
290 inOrder.verify(mHardware, never()).setLocalPrefixes(mStringArrayCaptor.capture());
Erik Kline7990aef2017-06-01 20:11:25 +0900291 inOrder.verify(mHardware, times(1)).setUpstreamParameters(
Erik Kline664d2082017-06-15 18:06:34 +0900292 eq(testIfName), eq(ipv4Addr), eq(ipv4Gateway), eq(null));
Lorenzo Colitti4ca0ba82017-07-12 15:48:07 +0900293 inOrder.verify(mHardware, times(1)).getForwardedStats(eq(testIfName));
Lorenzo Colittiddce7ee2017-08-21 12:34:50 +0900294 inOrder.verify(mHardware, times(1)).setDataLimit(eq(testIfName), eq(Long.MAX_VALUE));
Erik Kline7990aef2017-06-01 20:11:25 +0900295 inOrder.verifyNoMoreInteractions();
296
297 final String ipv6Gw1 = "fe80::cafe";
298 lp.addRoute(new RouteInfo(InetAddress.getByName(ipv6Gw1)));
299 offload.setUpstreamLinkProperties(lp);
Erik Kline5acb4e32017-07-04 18:28:11 +0900300 // No change in local addresses means no call to setLocalPrefixes().
301 inOrder.verify(mHardware, never()).setLocalPrefixes(mStringArrayCaptor.capture());
Erik Kline7990aef2017-06-01 20:11:25 +0900302 inOrder.verify(mHardware, times(1)).setUpstreamParameters(
303 eq(testIfName), eq(ipv4Addr), eq(ipv4Gateway), mStringArrayCaptor.capture());
Lorenzo Colitti4ca0ba82017-07-12 15:48:07 +0900304 inOrder.verify(mHardware, times(1)).getForwardedStats(eq(testIfName));
Erik Kline7990aef2017-06-01 20:11:25 +0900305 ArrayList<String> v6gws = mStringArrayCaptor.getValue();
306 assertEquals(1, v6gws.size());
307 assertTrue(v6gws.contains(ipv6Gw1));
Lorenzo Colittiddce7ee2017-08-21 12:34:50 +0900308 inOrder.verify(mHardware, times(1)).setDataLimit(eq(testIfName), eq(Long.MAX_VALUE));
Erik Kline7990aef2017-06-01 20:11:25 +0900309 inOrder.verifyNoMoreInteractions();
310
311 final String ipv6Gw2 = "fe80::d00d";
312 lp.addRoute(new RouteInfo(InetAddress.getByName(ipv6Gw2)));
313 offload.setUpstreamLinkProperties(lp);
Erik Kline5acb4e32017-07-04 18:28:11 +0900314 // No change in local addresses means no call to setLocalPrefixes().
315 inOrder.verify(mHardware, never()).setLocalPrefixes(mStringArrayCaptor.capture());
Erik Kline7990aef2017-06-01 20:11:25 +0900316 inOrder.verify(mHardware, times(1)).setUpstreamParameters(
317 eq(testIfName), eq(ipv4Addr), eq(ipv4Gateway), mStringArrayCaptor.capture());
Lorenzo Colitti4ca0ba82017-07-12 15:48:07 +0900318 inOrder.verify(mHardware, times(1)).getForwardedStats(eq(testIfName));
Erik Kline7990aef2017-06-01 20:11:25 +0900319 v6gws = mStringArrayCaptor.getValue();
320 assertEquals(2, v6gws.size());
321 assertTrue(v6gws.contains(ipv6Gw1));
322 assertTrue(v6gws.contains(ipv6Gw2));
Lorenzo Colittiddce7ee2017-08-21 12:34:50 +0900323 inOrder.verify(mHardware, times(1)).setDataLimit(eq(testIfName), eq(Long.MAX_VALUE));
Erik Kline7990aef2017-06-01 20:11:25 +0900324 inOrder.verifyNoMoreInteractions();
325
326 final LinkProperties stacked = new LinkProperties();
327 stacked.setInterfaceName("stacked");
328 stacked.addLinkAddress(new LinkAddress("192.0.2.129/25"));
329 stacked.addRoute(new RouteInfo(InetAddress.getByName("192.0.2.254")));
330 stacked.addRoute(new RouteInfo(InetAddress.getByName("fe80::bad:f00")));
331 assertTrue(lp.addStackedLink(stacked));
332 offload.setUpstreamLinkProperties(lp);
Erik Kline5acb4e32017-07-04 18:28:11 +0900333 // No change in local addresses means no call to setLocalPrefixes().
334 inOrder.verify(mHardware, never()).setLocalPrefixes(mStringArrayCaptor.capture());
Erik Kline7990aef2017-06-01 20:11:25 +0900335 inOrder.verify(mHardware, times(1)).setUpstreamParameters(
336 eq(testIfName), eq(ipv4Addr), eq(ipv4Gateway), mStringArrayCaptor.capture());
Lorenzo Colitti4ca0ba82017-07-12 15:48:07 +0900337 inOrder.verify(mHardware, times(1)).getForwardedStats(eq(testIfName));
Erik Kline7990aef2017-06-01 20:11:25 +0900338 v6gws = mStringArrayCaptor.getValue();
339 assertEquals(2, v6gws.size());
340 assertTrue(v6gws.contains(ipv6Gw1));
341 assertTrue(v6gws.contains(ipv6Gw2));
Lorenzo Colittiddce7ee2017-08-21 12:34:50 +0900342 inOrder.verify(mHardware, times(1)).setDataLimit(eq(testIfName), eq(Long.MAX_VALUE));
Erik Kline7990aef2017-06-01 20:11:25 +0900343 inOrder.verifyNoMoreInteractions();
Erik Kline5acb4e32017-07-04 18:28:11 +0900344
345 // Add in some IPv6 upstream info. When there is a tethered downstream
346 // making use of the IPv6 prefix we would expect to see the /64 route
347 // removed from "local prefixes" and /128s added for the upstream IPv6
348 // addresses. This is not yet implemented, and for now we simply
349 // expect to see these /128s.
350 lp.addRoute(new RouteInfo(new IpPrefix("2001:db8::/64")));
351 // "2001:db8::/64" plus "assigned" ASCII in hex
352 lp.addLinkAddress(new LinkAddress("2001:db8::6173:7369:676e:6564/64"));
353 // "2001:db8::/64" plus "random" ASCII in hex
354 lp.addLinkAddress(new LinkAddress("2001:db8::7261:6e64:6f6d/64"));
355 offload.setUpstreamLinkProperties(lp);
356 inOrder.verify(mHardware, times(1)).setLocalPrefixes(mStringArrayCaptor.capture());
357 localPrefixes = mStringArrayCaptor.getValue();
358 assertEquals(6, localPrefixes.size());
359 assertTrue(localPrefixes.contains("127.0.0.0/8"));
360 assertTrue(localPrefixes.contains("192.0.2.0/24"));
361 assertTrue(localPrefixes.contains("fe80::/64"));
362 assertTrue(localPrefixes.contains("2001:db8::/64"));
363 assertTrue(localPrefixes.contains("2001:db8::6173:7369:676e:6564/128"));
364 assertTrue(localPrefixes.contains("2001:db8::7261:6e64:6f6d/128"));
365 // The relevant parts of the LinkProperties have not changed, but at the
366 // moment we do not de-dup upstream LinkProperties this carefully.
367 inOrder.verify(mHardware, times(1)).setUpstreamParameters(
368 eq(testIfName), eq(ipv4Addr), eq(ipv4Gateway), mStringArrayCaptor.capture());
369 v6gws = mStringArrayCaptor.getValue();
370 assertEquals(2, v6gws.size());
371 assertTrue(v6gws.contains(ipv6Gw1));
372 assertTrue(v6gws.contains(ipv6Gw2));
Lorenzo Colitti4ca0ba82017-07-12 15:48:07 +0900373 inOrder.verify(mHardware, times(1)).getForwardedStats(eq(testIfName));
Lorenzo Colittiddce7ee2017-08-21 12:34:50 +0900374 inOrder.verify(mHardware, times(1)).setDataLimit(eq(testIfName), eq(Long.MAX_VALUE));
Erik Kline5acb4e32017-07-04 18:28:11 +0900375 inOrder.verifyNoMoreInteractions();
376
377 // Completely identical LinkProperties updates are de-duped.
378 offload.setUpstreamLinkProperties(lp);
379 // This LinkProperties value does not differ from the default upstream.
380 // There should be no extraneous call to setUpstreamParameters().
381 inOrder.verify(mHardware, never()).setUpstreamParameters(
382 anyObject(), anyObject(), anyObject(), anyObject());
383 inOrder.verifyNoMoreInteractions();
Erik Kline7990aef2017-06-01 20:11:25 +0900384 }
Lorenzo Colitti4ca0ba82017-07-12 15:48:07 +0900385
386 private void assertNetworkStats(String iface, ForwardedStats stats, NetworkStats.Entry entry) {
387 assertEquals(iface, entry.iface);
388 assertEquals(stats.rxBytes, entry.rxBytes);
389 assertEquals(stats.txBytes, entry.txBytes);
390 assertEquals(SET_DEFAULT, entry.set);
391 assertEquals(TAG_NONE, entry.tag);
Lorenzo Colitti4ca0ba82017-07-12 15:48:07 +0900392 }
393
394 @Test
395 public void testGetForwardedStats() throws Exception {
396 setupFunctioningHardwareInterface();
397 enableOffload();
398
399 final OffloadController offload = makeOffloadController();
400 offload.start();
401
402 final String ethernetIface = "eth1";
403 final String mobileIface = "rmnet_data0";
404
405 ForwardedStats ethernetStats = new ForwardedStats();
406 ethernetStats.rxBytes = 12345;
407 ethernetStats.txBytes = 54321;
408
409 ForwardedStats mobileStats = new ForwardedStats();
410 mobileStats.rxBytes = 999;
411 mobileStats.txBytes = 99999;
412
413 when(mHardware.getForwardedStats(eq(ethernetIface))).thenReturn(ethernetStats);
414 when(mHardware.getForwardedStats(eq(mobileIface))).thenReturn(mobileStats);
415
Hugo Benichi752c1282017-08-22 13:57:41 +0900416 InOrder inOrder = inOrder(mHardware);
417
Lorenzo Colitti4ca0ba82017-07-12 15:48:07 +0900418 final LinkProperties lp = new LinkProperties();
419 lp.setInterfaceName(ethernetIface);
420 offload.setUpstreamLinkProperties(lp);
Hugo Benichi752c1282017-08-22 13:57:41 +0900421 // Previous upstream was null, so no stats are fetched.
422 inOrder.verify(mHardware, never()).getForwardedStats(any());
Lorenzo Colitti4ca0ba82017-07-12 15:48:07 +0900423
424 lp.setInterfaceName(mobileIface);
425 offload.setUpstreamLinkProperties(lp);
Hugo Benichi752c1282017-08-22 13:57:41 +0900426 // Expect that we fetch stats from the previous upstream.
427 inOrder.verify(mHardware, times(1)).getForwardedStats(eq(ethernetIface));
Lorenzo Colitti4ca0ba82017-07-12 15:48:07 +0900428
429 lp.setInterfaceName(ethernetIface);
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(mobileIface));
Lorenzo Colitti4ca0ba82017-07-12 15:48:07 +0900433
Hugo Benichi752c1282017-08-22 13:57:41 +0900434 ethernetStats = new ForwardedStats();
Lorenzo Colitti4ca0ba82017-07-12 15:48:07 +0900435 ethernetStats.rxBytes = 100000;
436 ethernetStats.txBytes = 100000;
Hugo Benichi752c1282017-08-22 13:57:41 +0900437 when(mHardware.getForwardedStats(eq(ethernetIface))).thenReturn(ethernetStats);
Lorenzo Colitti4ca0ba82017-07-12 15:48:07 +0900438 offload.setUpstreamLinkProperties(null);
Hugo Benichi752c1282017-08-22 13:57:41 +0900439 // Expect that we fetch stats from the previous upstream.
440 inOrder.verify(mHardware, times(1)).getForwardedStats(eq(ethernetIface));
Lorenzo Colitti4ca0ba82017-07-12 15:48:07 +0900441
Lorenzo Colittif1912ca2017-08-17 19:23:08 +0900442 ITetheringStatsProvider provider = mTetherStatsProviderCaptor.getValue();
443 NetworkStats stats = provider.getTetherStats(STATS_PER_IFACE);
444 NetworkStats perUidStats = provider.getTetherStats(STATS_PER_UID);
Hugo Benichi752c1282017-08-22 13:57:41 +0900445 waitForIdle();
446 // There is no current upstream, so no stats are fetched.
447 inOrder.verify(mHardware, never()).getForwardedStats(eq(ethernetIface));
448 inOrder.verifyNoMoreInteractions();
Lorenzo Colittif1912ca2017-08-17 19:23:08 +0900449
Lorenzo Colitti4ca0ba82017-07-12 15:48:07 +0900450 assertEquals(2, stats.size());
Lorenzo Colittif1912ca2017-08-17 19:23:08 +0900451 assertEquals(2, perUidStats.size());
Lorenzo Colitti4ca0ba82017-07-12 15:48:07 +0900452
453 NetworkStats.Entry entry = null;
Lorenzo Colittif1912ca2017-08-17 19:23:08 +0900454 for (int i = 0; i < stats.size(); i++) {
455 assertEquals(UID_ALL, stats.getValues(i, entry).uid);
456 assertEquals(UID_TETHERING, perUidStats.getValues(i, entry).uid);
457 }
458
Lorenzo Colitti4ca0ba82017-07-12 15:48:07 +0900459 int ethernetPosition = ethernetIface.equals(stats.getValues(0, entry).iface) ? 0 : 1;
460 int mobilePosition = 1 - ethernetPosition;
461
462 entry = stats.getValues(mobilePosition, entry);
463 assertNetworkStats(mobileIface, mobileStats, entry);
Lorenzo Colittif1912ca2017-08-17 19:23:08 +0900464 entry = perUidStats.getValues(mobilePosition, entry);
465 assertNetworkStats(mobileIface, mobileStats, entry);
Lorenzo Colitti4ca0ba82017-07-12 15:48:07 +0900466
467 ethernetStats.rxBytes = 12345 + 100000;
468 ethernetStats.txBytes = 54321 + 100000;
469 entry = stats.getValues(ethernetPosition, entry);
470 assertNetworkStats(ethernetIface, ethernetStats, entry);
Lorenzo Colittif1912ca2017-08-17 19:23:08 +0900471 entry = perUidStats.getValues(ethernetPosition, entry);
472 assertNetworkStats(ethernetIface, ethernetStats, entry);
Lorenzo Colitti4ca0ba82017-07-12 15:48:07 +0900473 }
Lorenzo Colitti50b60fc2017-08-11 13:47:49 +0900474
475 @Test
476 public void testSetInterfaceQuota() throws Exception {
477 setupFunctioningHardwareInterface();
478 enableOffload();
479
480 final OffloadController offload = makeOffloadController();
481 offload.start();
482
483 final String ethernetIface = "eth1";
484 final String mobileIface = "rmnet_data0";
485 final long ethernetLimit = 12345;
486 final long mobileLimit = 12345678;
487
488 final LinkProperties lp = new LinkProperties();
489 lp.setInterfaceName(ethernetIface);
490 offload.setUpstreamLinkProperties(lp);
491
492 ITetheringStatsProvider provider = mTetherStatsProviderCaptor.getValue();
493 final InOrder inOrder = inOrder(mHardware);
494 when(mHardware.setUpstreamParameters(any(), any(), any(), any())).thenReturn(true);
495 when(mHardware.setDataLimit(anyString(), anyLong())).thenReturn(true);
496
497 // Applying an interface quota to the current upstream immediately sends it to the hardware.
498 provider.setInterfaceQuota(ethernetIface, ethernetLimit);
499 waitForIdle();
500 inOrder.verify(mHardware).setDataLimit(ethernetIface, ethernetLimit);
501 inOrder.verifyNoMoreInteractions();
502
503 // Applying an interface quota to another upstream does not take any immediate action.
504 provider.setInterfaceQuota(mobileIface, mobileLimit);
505 waitForIdle();
506 inOrder.verify(mHardware, never()).setDataLimit(anyString(), anyLong());
507
508 // Switching to that upstream causes the quota to be applied if the parameters were applied
509 // correctly.
510 lp.setInterfaceName(mobileIface);
511 offload.setUpstreamLinkProperties(lp);
512 waitForIdle();
513 inOrder.verify(mHardware).setDataLimit(mobileIface, mobileLimit);
514
515 // Setting a limit of ITetheringStatsProvider.QUOTA_UNLIMITED causes the limit to be set
516 // to Long.MAX_VALUE.
517 provider.setInterfaceQuota(mobileIface, ITetheringStatsProvider.QUOTA_UNLIMITED);
518 waitForIdle();
519 inOrder.verify(mHardware).setDataLimit(mobileIface, Long.MAX_VALUE);
520
521 // If setting upstream parameters fails, then the data limit is not set.
522 when(mHardware.setUpstreamParameters(any(), any(), any(), any())).thenReturn(false);
523 lp.setInterfaceName(ethernetIface);
524 offload.setUpstreamLinkProperties(lp);
525 provider.setInterfaceQuota(mobileIface, mobileLimit);
526 waitForIdle();
527 inOrder.verify(mHardware, never()).setDataLimit(anyString(), anyLong());
528
529 // If setting the data limit fails while changing upstreams, offload is stopped.
530 when(mHardware.setUpstreamParameters(any(), any(), any(), any())).thenReturn(true);
531 when(mHardware.setDataLimit(anyString(), anyLong())).thenReturn(false);
532 lp.setInterfaceName(mobileIface);
533 offload.setUpstreamLinkProperties(lp);
534 provider.setInterfaceQuota(mobileIface, mobileLimit);
535 waitForIdle();
Lorenzo Colittiddce7ee2017-08-21 12:34:50 +0900536 inOrder.verify(mHardware).getForwardedStats(ethernetIface);
Lorenzo Colitti50b60fc2017-08-11 13:47:49 +0900537 inOrder.verify(mHardware).stopOffloadControl();
538 }
Lorenzo Colitti9f0baa92017-08-15 19:25:51 +0900539
540 @Test
541 public void testDataLimitCallback() throws Exception {
542 setupFunctioningHardwareInterface();
543 enableOffload();
544
545 final OffloadController offload = makeOffloadController();
546 offload.start();
547
548 OffloadHardwareInterface.ControlCallback callback = mControlCallbackCaptor.getValue();
549 callback.onStoppedLimitReached();
550 verify(mNMService, times(1)).tetherLimitReached(mTetherStatsProviderCaptor.getValue());
551 }
Erik Kline92c4db02017-05-31 10:21:32 +0900552}