Erik Kline | 92c4db0 | 2017-05-31 10:21:32 +0900 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.server.connectivity.tethering; |
| 18 | |
Lorenzo Colitti | 5a7dea1 | 2017-07-12 15:48:07 +0900 | [diff] [blame] | 19 | import static android.net.NetworkStats.SET_DEFAULT; |
Lorenzo Colitti | f1912ca | 2017-08-17 19:23:08 +0900 | [diff] [blame] | 20 | import static android.net.NetworkStats.STATS_PER_IFACE; |
| 21 | import static android.net.NetworkStats.STATS_PER_UID; |
Lorenzo Colitti | 5a7dea1 | 2017-07-12 15:48:07 +0900 | [diff] [blame] | 22 | import static android.net.NetworkStats.TAG_NONE; |
Lorenzo Colitti | f1912ca | 2017-08-17 19:23:08 +0900 | [diff] [blame] | 23 | import static android.net.NetworkStats.UID_ALL; |
Lorenzo Colitti | 5a7dea1 | 2017-07-12 15:48:07 +0900 | [diff] [blame] | 24 | import static android.net.TrafficStats.UID_TETHERING; |
Erik Kline | 92c4db0 | 2017-05-31 10:21:32 +0900 | [diff] [blame] | 25 | import static android.provider.Settings.Global.TETHER_OFFLOAD_DISABLED; |
Brett Chabot | 1ae2aa6 | 2019-03-04 14:14:56 -0800 | [diff] [blame^] | 26 | |
Lorenzo Colitti | 5a7dea1 | 2017-07-12 15:48:07 +0900 | [diff] [blame] | 27 | import static com.android.server.connectivity.tethering.OffloadHardwareInterface.ForwardedStats; |
Brett Chabot | 1ae2aa6 | 2019-03-04 14:14:56 -0800 | [diff] [blame^] | 28 | |
Erik Kline | 7990aef | 2017-06-01 20:11:25 +0900 | [diff] [blame] | 29 | import static org.junit.Assert.assertEquals; |
| 30 | import static org.junit.Assert.assertTrue; |
Erik Kline | 92c4db0 | 2017-05-31 10:21:32 +0900 | [diff] [blame] | 31 | import static org.junit.Assert.fail; |
| 32 | import static org.mockito.Matchers.any; |
Lorenzo Colitti | 50b60fc | 2017-08-11 13:47:49 +0900 | [diff] [blame] | 33 | import static org.mockito.Matchers.anyLong; |
Erik Kline | 92c4db0 | 2017-05-31 10:21:32 +0900 | [diff] [blame] | 34 | import static org.mockito.Matchers.anyObject; |
Lorenzo Colitti | 5a7dea1 | 2017-07-12 15:48:07 +0900 | [diff] [blame] | 35 | import static org.mockito.Matchers.anyString; |
Erik Kline | 7990aef | 2017-06-01 20:11:25 +0900 | [diff] [blame] | 36 | import static org.mockito.Matchers.eq; |
Erik Kline | aabdaa9 | 2017-08-31 21:09:45 +0900 | [diff] [blame] | 37 | import static org.mockito.Mockito.clearInvocations; |
Erik Kline | 92c4db0 | 2017-05-31 10:21:32 +0900 | [diff] [blame] | 38 | import static org.mockito.Mockito.inOrder; |
| 39 | import static org.mockito.Mockito.never; |
| 40 | import static org.mockito.Mockito.times; |
Lorenzo Colitti | 5a7dea1 | 2017-07-12 15:48:07 +0900 | [diff] [blame] | 41 | import static org.mockito.Mockito.verify; |
Erik Kline | aabdaa9 | 2017-08-31 21:09:45 +0900 | [diff] [blame] | 42 | import static org.mockito.Mockito.verifyNoMoreInteractions; |
Erik Kline | 92c4db0 | 2017-05-31 10:21:32 +0900 | [diff] [blame] | 43 | import static org.mockito.Mockito.when; |
| 44 | |
| 45 | import android.content.Context; |
Erik Kline | f3a08b4 | 2017-06-07 16:33:19 +0900 | [diff] [blame] | 46 | import android.content.pm.ApplicationInfo; |
Lorenzo Colitti | 5a7dea1 | 2017-07-12 15:48:07 +0900 | [diff] [blame] | 47 | import android.net.ITetheringStatsProvider; |
Erik Kline | 32179ff | 2017-07-04 18:28:11 +0900 | [diff] [blame] | 48 | import android.net.IpPrefix; |
Erik Kline | 7990aef | 2017-06-01 20:11:25 +0900 | [diff] [blame] | 49 | import android.net.LinkAddress; |
| 50 | import android.net.LinkProperties; |
Lorenzo Colitti | 5a7dea1 | 2017-07-12 15:48:07 +0900 | [diff] [blame] | 51 | import android.net.NetworkStats; |
Erik Kline | 7990aef | 2017-06-01 20:11:25 +0900 | [diff] [blame] | 52 | import android.net.RouteInfo; |
Erik Kline | 92c4db0 | 2017-05-31 10:21:32 +0900 | [diff] [blame] | 53 | import android.net.util.SharedLog; |
Lorenzo Colitti | 50b60fc | 2017-08-11 13:47:49 +0900 | [diff] [blame] | 54 | import android.os.ConditionVariable; |
Lorenzo Colitti | 5a7dea1 | 2017-07-12 15:48:07 +0900 | [diff] [blame] | 55 | import android.os.Handler; |
Lorenzo Colitti | 5a7dea1 | 2017-07-12 15:48:07 +0900 | [diff] [blame] | 56 | import android.os.INetworkManagementService; |
Brett Chabot | 1ae2aa6 | 2019-03-04 14:14:56 -0800 | [diff] [blame^] | 57 | import android.os.Looper; |
Erik Kline | 92c4db0 | 2017-05-31 10:21:32 +0900 | [diff] [blame] | 58 | import android.provider.Settings; |
| 59 | import android.provider.Settings.SettingNotFoundException; |
Erik Kline | 92c4db0 | 2017-05-31 10:21:32 +0900 | [diff] [blame] | 60 | import android.test.mock.MockContentResolver; |
Erik Kline | 92c4db0 | 2017-05-31 10:21:32 +0900 | [diff] [blame] | 61 | |
Brett Chabot | 1ae2aa6 | 2019-03-04 14:14:56 -0800 | [diff] [blame^] | 62 | import androidx.test.filters.SmallTest; |
| 63 | import androidx.test.runner.AndroidJUnit4; |
| 64 | |
| 65 | import com.android.internal.util.test.FakeSettingsProvider; |
Erik Kline | 7990aef | 2017-06-01 20:11:25 +0900 | [diff] [blame] | 66 | |
Erik Kline | c87cd41 | 2017-07-07 17:38:30 +0900 | [diff] [blame] | 67 | import org.junit.After; |
Erik Kline | 92c4db0 | 2017-05-31 10:21:32 +0900 | [diff] [blame] | 68 | import org.junit.Before; |
Erik Kline | 92c4db0 | 2017-05-31 10:21:32 +0900 | [diff] [blame] | 69 | import org.junit.Test; |
Brett Chabot | 1ae2aa6 | 2019-03-04 14:14:56 -0800 | [diff] [blame^] | 70 | import org.junit.runner.RunWith; |
Erik Kline | 7990aef | 2017-06-01 20:11:25 +0900 | [diff] [blame] | 71 | import org.mockito.ArgumentCaptor; |
Erik Kline | 92c4db0 | 2017-05-31 10:21:32 +0900 | [diff] [blame] | 72 | import org.mockito.InOrder; |
| 73 | import org.mockito.Mock; |
| 74 | import org.mockito.MockitoAnnotations; |
| 75 | |
Brett Chabot | 1ae2aa6 | 2019-03-04 14:14:56 -0800 | [diff] [blame^] | 76 | import java.net.InetAddress; |
| 77 | import java.util.ArrayList; |
| 78 | import java.util.HashSet; |
| 79 | import java.util.Set; |
Erik Kline | 92c4db0 | 2017-05-31 10:21:32 +0900 | [diff] [blame] | 80 | |
| 81 | @RunWith(AndroidJUnit4.class) |
| 82 | @SmallTest |
| 83 | public class OffloadControllerTest { |
Erik Kline | b3bb26e | 2017-07-06 19:49:35 +0900 | [diff] [blame] | 84 | private static final String RNDIS0 = "test_rndis0"; |
| 85 | private static final String RMNET0 = "test_rmnet_data0"; |
| 86 | private static final String WLAN0 = "test_wlan0"; |
| 87 | |
| 88 | private static final String IPV6_LINKLOCAL = "fe80::/64"; |
| 89 | private static final String IPV6_DOC_PREFIX = "2001:db8::/64"; |
| 90 | private static final String IPV6_DISCARD_PREFIX = "100::/64"; |
| 91 | private static final String USB_PREFIX = "192.168.42.0/24"; |
| 92 | private static final String WIFI_PREFIX = "192.168.43.0/24"; |
Erik Kline | 92c4db0 | 2017-05-31 10:21:32 +0900 | [diff] [blame] | 93 | |
| 94 | @Mock private OffloadHardwareInterface mHardware; |
Erik Kline | f3a08b4 | 2017-06-07 16:33:19 +0900 | [diff] [blame] | 95 | @Mock private ApplicationInfo mApplicationInfo; |
Erik Kline | 92c4db0 | 2017-05-31 10:21:32 +0900 | [diff] [blame] | 96 | @Mock private Context mContext; |
Lorenzo Colitti | 5a7dea1 | 2017-07-12 15:48:07 +0900 | [diff] [blame] | 97 | @Mock private INetworkManagementService mNMService; |
Erik Kline | 7fd696c | 2017-06-12 18:20:08 +0900 | [diff] [blame] | 98 | private final ArgumentCaptor<ArrayList> mStringArrayCaptor = |
| 99 | ArgumentCaptor.forClass(ArrayList.class); |
Lorenzo Colitti | 5a7dea1 | 2017-07-12 15:48:07 +0900 | [diff] [blame] | 100 | private final ArgumentCaptor<ITetheringStatsProvider.Stub> mTetherStatsProviderCaptor = |
| 101 | ArgumentCaptor.forClass(ITetheringStatsProvider.Stub.class); |
Lorenzo Colitti | 9f0baa9 | 2017-08-15 19:25:51 +0900 | [diff] [blame] | 102 | private final ArgumentCaptor<OffloadHardwareInterface.ControlCallback> mControlCallbackCaptor = |
| 103 | ArgumentCaptor.forClass(OffloadHardwareInterface.ControlCallback.class); |
Erik Kline | 92c4db0 | 2017-05-31 10:21:32 +0900 | [diff] [blame] | 104 | private MockContentResolver mContentResolver; |
| 105 | |
Lorenzo Colitti | 5a7dea1 | 2017-07-12 15:48:07 +0900 | [diff] [blame] | 106 | @Before public void setUp() { |
Erik Kline | 92c4db0 | 2017-05-31 10:21:32 +0900 | [diff] [blame] | 107 | MockitoAnnotations.initMocks(this); |
Erik Kline | f3a08b4 | 2017-06-07 16:33:19 +0900 | [diff] [blame] | 108 | when(mContext.getApplicationInfo()).thenReturn(mApplicationInfo); |
| 109 | when(mContext.getPackageName()).thenReturn("OffloadControllerTest"); |
Erik Kline | 92c4db0 | 2017-05-31 10:21:32 +0900 | [diff] [blame] | 110 | mContentResolver = new MockContentResolver(mContext); |
| 111 | mContentResolver.addProvider(Settings.AUTHORITY, new FakeSettingsProvider()); |
Erik Kline | 92c4db0 | 2017-05-31 10:21:32 +0900 | [diff] [blame] | 112 | when(mContext.getContentResolver()).thenReturn(mContentResolver); |
Erik Kline | c87cd41 | 2017-07-07 17:38:30 +0900 | [diff] [blame] | 113 | FakeSettingsProvider.clearSettingsProvider(); |
| 114 | } |
| 115 | |
| 116 | @After public void tearDown() throws Exception { |
| 117 | FakeSettingsProvider.clearSettingsProvider(); |
Erik Kline | 92c4db0 | 2017-05-31 10:21:32 +0900 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | private void setupFunctioningHardwareInterface() { |
| 121 | when(mHardware.initOffloadConfig()).thenReturn(true); |
Lorenzo Colitti | 9f0baa9 | 2017-08-15 19:25:51 +0900 | [diff] [blame] | 122 | when(mHardware.initOffloadControl(mControlCallbackCaptor.capture())) |
Erik Kline | 92c4db0 | 2017-05-31 10:21:32 +0900 | [diff] [blame] | 123 | .thenReturn(true); |
Lorenzo Colitti | ddce7ee | 2017-08-21 12:34:50 +0900 | [diff] [blame] | 124 | when(mHardware.setUpstreamParameters(anyString(), any(), any(), any())).thenReturn(true); |
Lorenzo Colitti | 5a7dea1 | 2017-07-12 15:48:07 +0900 | [diff] [blame] | 125 | when(mHardware.getForwardedStats(any())).thenReturn(new ForwardedStats()); |
Lorenzo Colitti | ddce7ee | 2017-08-21 12:34:50 +0900 | [diff] [blame] | 126 | when(mHardware.setDataLimit(anyString(), anyLong())).thenReturn(true); |
Erik Kline | 92c4db0 | 2017-05-31 10:21:32 +0900 | [diff] [blame] | 127 | } |
| 128 | |
Erik Kline | c87cd41 | 2017-07-07 17:38:30 +0900 | [diff] [blame] | 129 | private void enableOffload() { |
| 130 | Settings.Global.putInt(mContentResolver, TETHER_OFFLOAD_DISABLED, 0); |
| 131 | } |
| 132 | |
Lorenzo Colitti | 50b60fc | 2017-08-11 13:47:49 +0900 | [diff] [blame] | 133 | private void waitForIdle() { |
| 134 | ConditionVariable cv = new ConditionVariable(); |
| 135 | new Handler(Looper.getMainLooper()).post(() -> { cv.open(); }); |
| 136 | cv.block(); |
| 137 | } |
| 138 | |
Lorenzo Colitti | 5a7dea1 | 2017-07-12 15:48:07 +0900 | [diff] [blame] | 139 | private OffloadController makeOffloadController() throws Exception { |
| 140 | OffloadController offload = new OffloadController(new Handler(Looper.getMainLooper()), |
| 141 | mHardware, mContentResolver, mNMService, new SharedLog("test")); |
| 142 | verify(mNMService).registerTetheringStatsProvider( |
| 143 | mTetherStatsProviderCaptor.capture(), anyString()); |
| 144 | return offload; |
| 145 | } |
| 146 | |
Erik Kline | 92c4db0 | 2017-05-31 10:21:32 +0900 | [diff] [blame] | 147 | @Test |
Lorenzo Colitti | 5a7dea1 | 2017-07-12 15:48:07 +0900 | [diff] [blame] | 148 | public void testNoSettingsValueDefaultDisabledDoesNotStart() throws Exception { |
Erik Kline | 92c4db0 | 2017-05-31 10:21:32 +0900 | [diff] [blame] | 149 | setupFunctioningHardwareInterface(); |
Erik Kline | c87cd41 | 2017-07-07 17:38:30 +0900 | [diff] [blame] | 150 | when(mHardware.getDefaultTetherOffloadDisabled()).thenReturn(1); |
Erik Kline | 92c4db0 | 2017-05-31 10:21:32 +0900 | [diff] [blame] | 151 | try { |
| 152 | Settings.Global.getInt(mContentResolver, TETHER_OFFLOAD_DISABLED); |
| 153 | fail(); |
| 154 | } catch (SettingNotFoundException expected) {} |
| 155 | |
Lorenzo Colitti | 5a7dea1 | 2017-07-12 15:48:07 +0900 | [diff] [blame] | 156 | final OffloadController offload = makeOffloadController(); |
Erik Kline | 92c4db0 | 2017-05-31 10:21:32 +0900 | [diff] [blame] | 157 | offload.start(); |
| 158 | |
| 159 | final InOrder inOrder = inOrder(mHardware); |
Erik Kline | c87cd41 | 2017-07-07 17:38:30 +0900 | [diff] [blame] | 160 | inOrder.verify(mHardware, times(1)).getDefaultTetherOffloadDisabled(); |
| 161 | inOrder.verify(mHardware, never()).initOffloadConfig(); |
| 162 | inOrder.verify(mHardware, never()).initOffloadControl( |
| 163 | any(OffloadHardwareInterface.ControlCallback.class)); |
| 164 | inOrder.verifyNoMoreInteractions(); |
| 165 | } |
| 166 | |
| 167 | @Test |
Lorenzo Colitti | 5a7dea1 | 2017-07-12 15:48:07 +0900 | [diff] [blame] | 168 | public void testNoSettingsValueDefaultEnabledDoesStart() throws Exception { |
Erik Kline | c87cd41 | 2017-07-07 17:38:30 +0900 | [diff] [blame] | 169 | setupFunctioningHardwareInterface(); |
| 170 | when(mHardware.getDefaultTetherOffloadDisabled()).thenReturn(0); |
| 171 | try { |
| 172 | Settings.Global.getInt(mContentResolver, TETHER_OFFLOAD_DISABLED); |
| 173 | fail(); |
| 174 | } catch (SettingNotFoundException expected) {} |
| 175 | |
Lorenzo Colitti | 5a7dea1 | 2017-07-12 15:48:07 +0900 | [diff] [blame] | 176 | final OffloadController offload = makeOffloadController(); |
Erik Kline | c87cd41 | 2017-07-07 17:38:30 +0900 | [diff] [blame] | 177 | offload.start(); |
| 178 | |
| 179 | final InOrder inOrder = inOrder(mHardware); |
| 180 | inOrder.verify(mHardware, times(1)).getDefaultTetherOffloadDisabled(); |
Erik Kline | 92c4db0 | 2017-05-31 10:21:32 +0900 | [diff] [blame] | 181 | inOrder.verify(mHardware, times(1)).initOffloadConfig(); |
| 182 | inOrder.verify(mHardware, times(1)).initOffloadControl( |
| 183 | any(OffloadHardwareInterface.ControlCallback.class)); |
| 184 | inOrder.verifyNoMoreInteractions(); |
| 185 | } |
| 186 | |
| 187 | @Test |
Lorenzo Colitti | 5a7dea1 | 2017-07-12 15:48:07 +0900 | [diff] [blame] | 188 | public void testSettingsAllowsStart() throws Exception { |
Erik Kline | 92c4db0 | 2017-05-31 10:21:32 +0900 | [diff] [blame] | 189 | setupFunctioningHardwareInterface(); |
| 190 | Settings.Global.putInt(mContentResolver, TETHER_OFFLOAD_DISABLED, 0); |
| 191 | |
Lorenzo Colitti | 5a7dea1 | 2017-07-12 15:48:07 +0900 | [diff] [blame] | 192 | final OffloadController offload = makeOffloadController(); |
Erik Kline | 92c4db0 | 2017-05-31 10:21:32 +0900 | [diff] [blame] | 193 | offload.start(); |
| 194 | |
| 195 | final InOrder inOrder = inOrder(mHardware); |
Erik Kline | c87cd41 | 2017-07-07 17:38:30 +0900 | [diff] [blame] | 196 | inOrder.verify(mHardware, times(1)).getDefaultTetherOffloadDisabled(); |
Erik Kline | 92c4db0 | 2017-05-31 10:21:32 +0900 | [diff] [blame] | 197 | inOrder.verify(mHardware, times(1)).initOffloadConfig(); |
| 198 | inOrder.verify(mHardware, times(1)).initOffloadControl( |
| 199 | any(OffloadHardwareInterface.ControlCallback.class)); |
| 200 | inOrder.verifyNoMoreInteractions(); |
| 201 | } |
| 202 | |
| 203 | @Test |
Lorenzo Colitti | 5a7dea1 | 2017-07-12 15:48:07 +0900 | [diff] [blame] | 204 | public void testSettingsDisablesStart() throws Exception { |
Erik Kline | 92c4db0 | 2017-05-31 10:21:32 +0900 | [diff] [blame] | 205 | setupFunctioningHardwareInterface(); |
| 206 | Settings.Global.putInt(mContentResolver, TETHER_OFFLOAD_DISABLED, 1); |
| 207 | |
Lorenzo Colitti | 5a7dea1 | 2017-07-12 15:48:07 +0900 | [diff] [blame] | 208 | final OffloadController offload = makeOffloadController(); |
Erik Kline | 92c4db0 | 2017-05-31 10:21:32 +0900 | [diff] [blame] | 209 | offload.start(); |
| 210 | |
| 211 | final InOrder inOrder = inOrder(mHardware); |
Erik Kline | c87cd41 | 2017-07-07 17:38:30 +0900 | [diff] [blame] | 212 | inOrder.verify(mHardware, times(1)).getDefaultTetherOffloadDisabled(); |
Erik Kline | 92c4db0 | 2017-05-31 10:21:32 +0900 | [diff] [blame] | 213 | inOrder.verify(mHardware, never()).initOffloadConfig(); |
| 214 | inOrder.verify(mHardware, never()).initOffloadControl(anyObject()); |
| 215 | inOrder.verifyNoMoreInteractions(); |
| 216 | } |
Erik Kline | 7990aef | 2017-06-01 20:11:25 +0900 | [diff] [blame] | 217 | |
| 218 | @Test |
| 219 | public void testSetUpstreamLinkPropertiesWorking() throws Exception { |
| 220 | setupFunctioningHardwareInterface(); |
Erik Kline | c87cd41 | 2017-07-07 17:38:30 +0900 | [diff] [blame] | 221 | enableOffload(); |
| 222 | |
Lorenzo Colitti | 5a7dea1 | 2017-07-12 15:48:07 +0900 | [diff] [blame] | 223 | final OffloadController offload = makeOffloadController(); |
Erik Kline | 7990aef | 2017-06-01 20:11:25 +0900 | [diff] [blame] | 224 | offload.start(); |
| 225 | |
| 226 | final InOrder inOrder = inOrder(mHardware); |
Erik Kline | c87cd41 | 2017-07-07 17:38:30 +0900 | [diff] [blame] | 227 | inOrder.verify(mHardware, times(1)).getDefaultTetherOffloadDisabled(); |
Erik Kline | 7990aef | 2017-06-01 20:11:25 +0900 | [diff] [blame] | 228 | inOrder.verify(mHardware, times(1)).initOffloadConfig(); |
| 229 | inOrder.verify(mHardware, times(1)).initOffloadControl( |
| 230 | any(OffloadHardwareInterface.ControlCallback.class)); |
| 231 | inOrder.verifyNoMoreInteractions(); |
| 232 | |
Erik Kline | 32179ff | 2017-07-04 18:28:11 +0900 | [diff] [blame] | 233 | // In reality, the UpstreamNetworkMonitor would have passed down to us |
| 234 | // a covering set of local prefixes representing a minimum essential |
| 235 | // set plus all the prefixes on networks with network agents. |
| 236 | // |
| 237 | // We simulate that there, and then add upstream elements one by one |
| 238 | // and watch what happens. |
| 239 | final Set<IpPrefix> minimumLocalPrefixes = new HashSet<>(); |
| 240 | for (String s : new String[]{ |
| 241 | "127.0.0.0/8", "192.0.2.0/24", "fe80::/64", "2001:db8::/64"}) { |
| 242 | minimumLocalPrefixes.add(new IpPrefix(s)); |
| 243 | } |
| 244 | offload.setLocalPrefixes(minimumLocalPrefixes); |
| 245 | inOrder.verify(mHardware, times(1)).setLocalPrefixes(mStringArrayCaptor.capture()); |
| 246 | ArrayList<String> localPrefixes = mStringArrayCaptor.getValue(); |
| 247 | assertEquals(4, localPrefixes.size()); |
Erik Kline | b3bb26e | 2017-07-06 19:49:35 +0900 | [diff] [blame] | 248 | assertArrayListContains(localPrefixes, |
| 249 | "127.0.0.0/8", "192.0.2.0/24", "fe80::/64", "2001:db8::/64"); |
Erik Kline | 7990aef | 2017-06-01 20:11:25 +0900 | [diff] [blame] | 250 | inOrder.verifyNoMoreInteractions(); |
Erik Kline | 32179ff | 2017-07-04 18:28:11 +0900 | [diff] [blame] | 251 | |
| 252 | offload.setUpstreamLinkProperties(null); |
| 253 | // No change in local addresses means no call to setLocalPrefixes(). |
| 254 | inOrder.verify(mHardware, never()).setLocalPrefixes(mStringArrayCaptor.capture()); |
| 255 | // This LinkProperties value does not differ from the default upstream. |
| 256 | // There should be no extraneous call to setUpstreamParameters(). |
| 257 | inOrder.verify(mHardware, never()).setUpstreamParameters( |
| 258 | anyObject(), anyObject(), anyObject(), anyObject()); |
| 259 | inOrder.verifyNoMoreInteractions(); |
Erik Kline | 7990aef | 2017-06-01 20:11:25 +0900 | [diff] [blame] | 260 | |
| 261 | final LinkProperties lp = new LinkProperties(); |
| 262 | |
| 263 | final String testIfName = "rmnet_data17"; |
| 264 | lp.setInterfaceName(testIfName); |
| 265 | offload.setUpstreamLinkProperties(lp); |
Erik Kline | 32179ff | 2017-07-04 18:28:11 +0900 | [diff] [blame] | 266 | // No change in local addresses means no call to setLocalPrefixes(). |
| 267 | inOrder.verify(mHardware, never()).setLocalPrefixes(mStringArrayCaptor.capture()); |
Erik Kline | 7990aef | 2017-06-01 20:11:25 +0900 | [diff] [blame] | 268 | inOrder.verify(mHardware, times(1)).setUpstreamParameters( |
Erik Kline | 1185459 | 2017-06-15 18:06:34 +0900 | [diff] [blame] | 269 | eq(testIfName), eq(null), eq(null), eq(null)); |
Lorenzo Colitti | ddce7ee | 2017-08-21 12:34:50 +0900 | [diff] [blame] | 270 | inOrder.verify(mHardware, times(1)).setDataLimit(eq(testIfName), eq(Long.MAX_VALUE)); |
Erik Kline | 7990aef | 2017-06-01 20:11:25 +0900 | [diff] [blame] | 271 | inOrder.verifyNoMoreInteractions(); |
| 272 | |
| 273 | final String ipv4Addr = "192.0.2.5"; |
| 274 | final String linkAddr = ipv4Addr + "/24"; |
| 275 | lp.addLinkAddress(new LinkAddress(linkAddr)); |
Erik Kline | 32179ff | 2017-07-04 18:28:11 +0900 | [diff] [blame] | 276 | lp.addRoute(new RouteInfo(new IpPrefix("192.0.2.0/24"))); |
Erik Kline | 7990aef | 2017-06-01 20:11:25 +0900 | [diff] [blame] | 277 | offload.setUpstreamLinkProperties(lp); |
Erik Kline | 32179ff | 2017-07-04 18:28:11 +0900 | [diff] [blame] | 278 | // IPv4 prefixes and addresses on the upstream are simply left as whole |
| 279 | // prefixes (already passed in from UpstreamNetworkMonitor code). If a |
| 280 | // tethering client sends traffic to the IPv4 default router or other |
| 281 | // clients on the upstream this will not be hardware-forwarded, and that |
| 282 | // should be fine for now. Ergo: no change in local addresses, no call |
| 283 | // to setLocalPrefixes(). |
| 284 | inOrder.verify(mHardware, never()).setLocalPrefixes(mStringArrayCaptor.capture()); |
Erik Kline | 7990aef | 2017-06-01 20:11:25 +0900 | [diff] [blame] | 285 | inOrder.verify(mHardware, times(1)).setUpstreamParameters( |
Erik Kline | 1185459 | 2017-06-15 18:06:34 +0900 | [diff] [blame] | 286 | eq(testIfName), eq(ipv4Addr), eq(null), eq(null)); |
Lorenzo Colitti | 5a7dea1 | 2017-07-12 15:48:07 +0900 | [diff] [blame] | 287 | inOrder.verify(mHardware, times(1)).getForwardedStats(eq(testIfName)); |
Lorenzo Colitti | ddce7ee | 2017-08-21 12:34:50 +0900 | [diff] [blame] | 288 | inOrder.verify(mHardware, times(1)).setDataLimit(eq(testIfName), eq(Long.MAX_VALUE)); |
Erik Kline | 7990aef | 2017-06-01 20:11:25 +0900 | [diff] [blame] | 289 | inOrder.verifyNoMoreInteractions(); |
| 290 | |
| 291 | final String ipv4Gateway = "192.0.2.1"; |
| 292 | lp.addRoute(new RouteInfo(InetAddress.getByName(ipv4Gateway))); |
| 293 | offload.setUpstreamLinkProperties(lp); |
Erik Kline | 32179ff | 2017-07-04 18:28:11 +0900 | [diff] [blame] | 294 | // No change in local addresses means no call to setLocalPrefixes(). |
| 295 | inOrder.verify(mHardware, never()).setLocalPrefixes(mStringArrayCaptor.capture()); |
Erik Kline | 7990aef | 2017-06-01 20:11:25 +0900 | [diff] [blame] | 296 | inOrder.verify(mHardware, times(1)).setUpstreamParameters( |
Erik Kline | 1185459 | 2017-06-15 18:06:34 +0900 | [diff] [blame] | 297 | eq(testIfName), eq(ipv4Addr), eq(ipv4Gateway), eq(null)); |
Lorenzo Colitti | 5a7dea1 | 2017-07-12 15:48:07 +0900 | [diff] [blame] | 298 | inOrder.verify(mHardware, times(1)).getForwardedStats(eq(testIfName)); |
Lorenzo Colitti | ddce7ee | 2017-08-21 12:34:50 +0900 | [diff] [blame] | 299 | inOrder.verify(mHardware, times(1)).setDataLimit(eq(testIfName), eq(Long.MAX_VALUE)); |
Erik Kline | 7990aef | 2017-06-01 20:11:25 +0900 | [diff] [blame] | 300 | inOrder.verifyNoMoreInteractions(); |
| 301 | |
| 302 | final String ipv6Gw1 = "fe80::cafe"; |
| 303 | lp.addRoute(new RouteInfo(InetAddress.getByName(ipv6Gw1))); |
| 304 | offload.setUpstreamLinkProperties(lp); |
Erik Kline | 32179ff | 2017-07-04 18:28:11 +0900 | [diff] [blame] | 305 | // No change in local addresses means no call to setLocalPrefixes(). |
| 306 | inOrder.verify(mHardware, never()).setLocalPrefixes(mStringArrayCaptor.capture()); |
Erik Kline | 7990aef | 2017-06-01 20:11:25 +0900 | [diff] [blame] | 307 | inOrder.verify(mHardware, times(1)).setUpstreamParameters( |
| 308 | eq(testIfName), eq(ipv4Addr), eq(ipv4Gateway), mStringArrayCaptor.capture()); |
Lorenzo Colitti | 5a7dea1 | 2017-07-12 15:48:07 +0900 | [diff] [blame] | 309 | inOrder.verify(mHardware, times(1)).getForwardedStats(eq(testIfName)); |
Erik Kline | 7990aef | 2017-06-01 20:11:25 +0900 | [diff] [blame] | 310 | ArrayList<String> v6gws = mStringArrayCaptor.getValue(); |
| 311 | assertEquals(1, v6gws.size()); |
| 312 | assertTrue(v6gws.contains(ipv6Gw1)); |
Lorenzo Colitti | ddce7ee | 2017-08-21 12:34:50 +0900 | [diff] [blame] | 313 | inOrder.verify(mHardware, times(1)).setDataLimit(eq(testIfName), eq(Long.MAX_VALUE)); |
Erik Kline | 7990aef | 2017-06-01 20:11:25 +0900 | [diff] [blame] | 314 | inOrder.verifyNoMoreInteractions(); |
| 315 | |
| 316 | final String ipv6Gw2 = "fe80::d00d"; |
| 317 | lp.addRoute(new RouteInfo(InetAddress.getByName(ipv6Gw2))); |
| 318 | offload.setUpstreamLinkProperties(lp); |
Erik Kline | 32179ff | 2017-07-04 18:28:11 +0900 | [diff] [blame] | 319 | // No change in local addresses means no call to setLocalPrefixes(). |
| 320 | inOrder.verify(mHardware, never()).setLocalPrefixes(mStringArrayCaptor.capture()); |
Erik Kline | 7990aef | 2017-06-01 20:11:25 +0900 | [diff] [blame] | 321 | inOrder.verify(mHardware, times(1)).setUpstreamParameters( |
| 322 | eq(testIfName), eq(ipv4Addr), eq(ipv4Gateway), mStringArrayCaptor.capture()); |
Lorenzo Colitti | 5a7dea1 | 2017-07-12 15:48:07 +0900 | [diff] [blame] | 323 | inOrder.verify(mHardware, times(1)).getForwardedStats(eq(testIfName)); |
Erik Kline | 7990aef | 2017-06-01 20:11:25 +0900 | [diff] [blame] | 324 | v6gws = mStringArrayCaptor.getValue(); |
| 325 | assertEquals(2, v6gws.size()); |
| 326 | assertTrue(v6gws.contains(ipv6Gw1)); |
| 327 | assertTrue(v6gws.contains(ipv6Gw2)); |
Lorenzo Colitti | ddce7ee | 2017-08-21 12:34:50 +0900 | [diff] [blame] | 328 | inOrder.verify(mHardware, times(1)).setDataLimit(eq(testIfName), eq(Long.MAX_VALUE)); |
Erik Kline | 7990aef | 2017-06-01 20:11:25 +0900 | [diff] [blame] | 329 | inOrder.verifyNoMoreInteractions(); |
| 330 | |
| 331 | final LinkProperties stacked = new LinkProperties(); |
| 332 | stacked.setInterfaceName("stacked"); |
| 333 | stacked.addLinkAddress(new LinkAddress("192.0.2.129/25")); |
| 334 | stacked.addRoute(new RouteInfo(InetAddress.getByName("192.0.2.254"))); |
| 335 | stacked.addRoute(new RouteInfo(InetAddress.getByName("fe80::bad:f00"))); |
| 336 | assertTrue(lp.addStackedLink(stacked)); |
| 337 | offload.setUpstreamLinkProperties(lp); |
Erik Kline | 32179ff | 2017-07-04 18:28:11 +0900 | [diff] [blame] | 338 | // No change in local addresses means no call to setLocalPrefixes(). |
| 339 | inOrder.verify(mHardware, never()).setLocalPrefixes(mStringArrayCaptor.capture()); |
Erik Kline | 7990aef | 2017-06-01 20:11:25 +0900 | [diff] [blame] | 340 | inOrder.verify(mHardware, times(1)).setUpstreamParameters( |
| 341 | eq(testIfName), eq(ipv4Addr), eq(ipv4Gateway), mStringArrayCaptor.capture()); |
Lorenzo Colitti | 5a7dea1 | 2017-07-12 15:48:07 +0900 | [diff] [blame] | 342 | inOrder.verify(mHardware, times(1)).getForwardedStats(eq(testIfName)); |
Erik Kline | 7990aef | 2017-06-01 20:11:25 +0900 | [diff] [blame] | 343 | v6gws = mStringArrayCaptor.getValue(); |
| 344 | assertEquals(2, v6gws.size()); |
| 345 | assertTrue(v6gws.contains(ipv6Gw1)); |
| 346 | assertTrue(v6gws.contains(ipv6Gw2)); |
Lorenzo Colitti | ddce7ee | 2017-08-21 12:34:50 +0900 | [diff] [blame] | 347 | inOrder.verify(mHardware, times(1)).setDataLimit(eq(testIfName), eq(Long.MAX_VALUE)); |
Erik Kline | 7990aef | 2017-06-01 20:11:25 +0900 | [diff] [blame] | 348 | inOrder.verifyNoMoreInteractions(); |
Erik Kline | 32179ff | 2017-07-04 18:28:11 +0900 | [diff] [blame] | 349 | |
| 350 | // Add in some IPv6 upstream info. When there is a tethered downstream |
| 351 | // making use of the IPv6 prefix we would expect to see the /64 route |
| 352 | // removed from "local prefixes" and /128s added for the upstream IPv6 |
| 353 | // addresses. This is not yet implemented, and for now we simply |
| 354 | // expect to see these /128s. |
| 355 | lp.addRoute(new RouteInfo(new IpPrefix("2001:db8::/64"))); |
| 356 | // "2001:db8::/64" plus "assigned" ASCII in hex |
| 357 | lp.addLinkAddress(new LinkAddress("2001:db8::6173:7369:676e:6564/64")); |
| 358 | // "2001:db8::/64" plus "random" ASCII in hex |
| 359 | lp.addLinkAddress(new LinkAddress("2001:db8::7261:6e64:6f6d/64")); |
| 360 | offload.setUpstreamLinkProperties(lp); |
| 361 | inOrder.verify(mHardware, times(1)).setLocalPrefixes(mStringArrayCaptor.capture()); |
| 362 | localPrefixes = mStringArrayCaptor.getValue(); |
| 363 | assertEquals(6, localPrefixes.size()); |
Erik Kline | b3bb26e | 2017-07-06 19:49:35 +0900 | [diff] [blame] | 364 | assertArrayListContains(localPrefixes, |
| 365 | "127.0.0.0/8", "192.0.2.0/24", "fe80::/64", "2001:db8::/64", |
| 366 | "2001:db8::6173:7369:676e:6564/128", "2001:db8::7261:6e64:6f6d/128"); |
Erik Kline | 32179ff | 2017-07-04 18:28:11 +0900 | [diff] [blame] | 367 | // The relevant parts of the LinkProperties have not changed, but at the |
| 368 | // moment we do not de-dup upstream LinkProperties this carefully. |
| 369 | inOrder.verify(mHardware, times(1)).setUpstreamParameters( |
| 370 | eq(testIfName), eq(ipv4Addr), eq(ipv4Gateway), mStringArrayCaptor.capture()); |
| 371 | v6gws = mStringArrayCaptor.getValue(); |
| 372 | assertEquals(2, v6gws.size()); |
| 373 | assertTrue(v6gws.contains(ipv6Gw1)); |
| 374 | assertTrue(v6gws.contains(ipv6Gw2)); |
Lorenzo Colitti | 5a7dea1 | 2017-07-12 15:48:07 +0900 | [diff] [blame] | 375 | inOrder.verify(mHardware, times(1)).getForwardedStats(eq(testIfName)); |
Lorenzo Colitti | ddce7ee | 2017-08-21 12:34:50 +0900 | [diff] [blame] | 376 | inOrder.verify(mHardware, times(1)).setDataLimit(eq(testIfName), eq(Long.MAX_VALUE)); |
Erik Kline | 32179ff | 2017-07-04 18:28:11 +0900 | [diff] [blame] | 377 | inOrder.verifyNoMoreInteractions(); |
| 378 | |
| 379 | // Completely identical LinkProperties updates are de-duped. |
| 380 | offload.setUpstreamLinkProperties(lp); |
| 381 | // This LinkProperties value does not differ from the default upstream. |
| 382 | // There should be no extraneous call to setUpstreamParameters(). |
| 383 | inOrder.verify(mHardware, never()).setUpstreamParameters( |
| 384 | anyObject(), anyObject(), anyObject(), anyObject()); |
| 385 | inOrder.verifyNoMoreInteractions(); |
Erik Kline | 7990aef | 2017-06-01 20:11:25 +0900 | [diff] [blame] | 386 | } |
Lorenzo Colitti | 5a7dea1 | 2017-07-12 15:48:07 +0900 | [diff] [blame] | 387 | |
| 388 | private void assertNetworkStats(String iface, ForwardedStats stats, NetworkStats.Entry entry) { |
| 389 | assertEquals(iface, entry.iface); |
| 390 | assertEquals(stats.rxBytes, entry.rxBytes); |
| 391 | assertEquals(stats.txBytes, entry.txBytes); |
| 392 | assertEquals(SET_DEFAULT, entry.set); |
| 393 | assertEquals(TAG_NONE, entry.tag); |
Lorenzo Colitti | 5a7dea1 | 2017-07-12 15:48:07 +0900 | [diff] [blame] | 394 | } |
| 395 | |
| 396 | @Test |
| 397 | public void testGetForwardedStats() throws Exception { |
| 398 | setupFunctioningHardwareInterface(); |
| 399 | enableOffload(); |
| 400 | |
| 401 | final OffloadController offload = makeOffloadController(); |
| 402 | offload.start(); |
| 403 | |
| 404 | final String ethernetIface = "eth1"; |
| 405 | final String mobileIface = "rmnet_data0"; |
| 406 | |
| 407 | ForwardedStats ethernetStats = new ForwardedStats(); |
| 408 | ethernetStats.rxBytes = 12345; |
| 409 | ethernetStats.txBytes = 54321; |
| 410 | |
| 411 | ForwardedStats mobileStats = new ForwardedStats(); |
| 412 | mobileStats.rxBytes = 999; |
| 413 | mobileStats.txBytes = 99999; |
| 414 | |
| 415 | when(mHardware.getForwardedStats(eq(ethernetIface))).thenReturn(ethernetStats); |
| 416 | when(mHardware.getForwardedStats(eq(mobileIface))).thenReturn(mobileStats); |
| 417 | |
Hugo Benichi | 752c128 | 2017-08-22 13:57:41 +0900 | [diff] [blame] | 418 | InOrder inOrder = inOrder(mHardware); |
| 419 | |
Lorenzo Colitti | 5a7dea1 | 2017-07-12 15:48:07 +0900 | [diff] [blame] | 420 | final LinkProperties lp = new LinkProperties(); |
| 421 | lp.setInterfaceName(ethernetIface); |
| 422 | offload.setUpstreamLinkProperties(lp); |
Hugo Benichi | 752c128 | 2017-08-22 13:57:41 +0900 | [diff] [blame] | 423 | // Previous upstream was null, so no stats are fetched. |
| 424 | inOrder.verify(mHardware, never()).getForwardedStats(any()); |
Lorenzo Colitti | 5a7dea1 | 2017-07-12 15:48:07 +0900 | [diff] [blame] | 425 | |
| 426 | lp.setInterfaceName(mobileIface); |
| 427 | offload.setUpstreamLinkProperties(lp); |
Hugo Benichi | 752c128 | 2017-08-22 13:57:41 +0900 | [diff] [blame] | 428 | // Expect that we fetch stats from the previous upstream. |
| 429 | inOrder.verify(mHardware, times(1)).getForwardedStats(eq(ethernetIface)); |
Lorenzo Colitti | 5a7dea1 | 2017-07-12 15:48:07 +0900 | [diff] [blame] | 430 | |
| 431 | lp.setInterfaceName(ethernetIface); |
| 432 | offload.setUpstreamLinkProperties(lp); |
Hugo Benichi | 752c128 | 2017-08-22 13:57:41 +0900 | [diff] [blame] | 433 | // Expect that we fetch stats from the previous upstream. |
| 434 | inOrder.verify(mHardware, times(1)).getForwardedStats(eq(mobileIface)); |
Lorenzo Colitti | 5a7dea1 | 2017-07-12 15:48:07 +0900 | [diff] [blame] | 435 | |
Hugo Benichi | 752c128 | 2017-08-22 13:57:41 +0900 | [diff] [blame] | 436 | ethernetStats = new ForwardedStats(); |
Lorenzo Colitti | 5a7dea1 | 2017-07-12 15:48:07 +0900 | [diff] [blame] | 437 | ethernetStats.rxBytes = 100000; |
| 438 | ethernetStats.txBytes = 100000; |
Hugo Benichi | 752c128 | 2017-08-22 13:57:41 +0900 | [diff] [blame] | 439 | when(mHardware.getForwardedStats(eq(ethernetIface))).thenReturn(ethernetStats); |
Lorenzo Colitti | 5a7dea1 | 2017-07-12 15:48:07 +0900 | [diff] [blame] | 440 | offload.setUpstreamLinkProperties(null); |
Erik Kline | aabdaa9 | 2017-08-31 21:09:45 +0900 | [diff] [blame] | 441 | // Expect that we first clear the HAL's upstream parameters. |
| 442 | inOrder.verify(mHardware, times(1)).setUpstreamParameters( |
| 443 | eq(""), eq("0.0.0.0"), eq("0.0.0.0"), eq(null)); |
Hugo Benichi | 752c128 | 2017-08-22 13:57:41 +0900 | [diff] [blame] | 444 | // Expect that we fetch stats from the previous upstream. |
| 445 | inOrder.verify(mHardware, times(1)).getForwardedStats(eq(ethernetIface)); |
Lorenzo Colitti | 5a7dea1 | 2017-07-12 15:48:07 +0900 | [diff] [blame] | 446 | |
Lorenzo Colitti | f1912ca | 2017-08-17 19:23:08 +0900 | [diff] [blame] | 447 | ITetheringStatsProvider provider = mTetherStatsProviderCaptor.getValue(); |
| 448 | NetworkStats stats = provider.getTetherStats(STATS_PER_IFACE); |
| 449 | NetworkStats perUidStats = provider.getTetherStats(STATS_PER_UID); |
Hugo Benichi | 752c128 | 2017-08-22 13:57:41 +0900 | [diff] [blame] | 450 | waitForIdle(); |
| 451 | // There is no current upstream, so no stats are fetched. |
Lorenzo Colitti | f612b60 | 2017-08-24 12:40:07 +0900 | [diff] [blame] | 452 | inOrder.verify(mHardware, never()).getForwardedStats(any()); |
Hugo Benichi | 752c128 | 2017-08-22 13:57:41 +0900 | [diff] [blame] | 453 | inOrder.verifyNoMoreInteractions(); |
Lorenzo Colitti | f1912ca | 2017-08-17 19:23:08 +0900 | [diff] [blame] | 454 | |
Lorenzo Colitti | 5a7dea1 | 2017-07-12 15:48:07 +0900 | [diff] [blame] | 455 | assertEquals(2, stats.size()); |
Lorenzo Colitti | f1912ca | 2017-08-17 19:23:08 +0900 | [diff] [blame] | 456 | assertEquals(2, perUidStats.size()); |
Lorenzo Colitti | 5a7dea1 | 2017-07-12 15:48:07 +0900 | [diff] [blame] | 457 | |
| 458 | NetworkStats.Entry entry = null; |
Lorenzo Colitti | f1912ca | 2017-08-17 19:23:08 +0900 | [diff] [blame] | 459 | for (int i = 0; i < stats.size(); i++) { |
| 460 | assertEquals(UID_ALL, stats.getValues(i, entry).uid); |
| 461 | assertEquals(UID_TETHERING, perUidStats.getValues(i, entry).uid); |
| 462 | } |
| 463 | |
Lorenzo Colitti | 5a7dea1 | 2017-07-12 15:48:07 +0900 | [diff] [blame] | 464 | int ethernetPosition = ethernetIface.equals(stats.getValues(0, entry).iface) ? 0 : 1; |
| 465 | int mobilePosition = 1 - ethernetPosition; |
| 466 | |
| 467 | entry = stats.getValues(mobilePosition, entry); |
| 468 | assertNetworkStats(mobileIface, mobileStats, entry); |
Lorenzo Colitti | f1912ca | 2017-08-17 19:23:08 +0900 | [diff] [blame] | 469 | entry = perUidStats.getValues(mobilePosition, entry); |
| 470 | assertNetworkStats(mobileIface, mobileStats, entry); |
Lorenzo Colitti | 5a7dea1 | 2017-07-12 15:48:07 +0900 | [diff] [blame] | 471 | |
| 472 | ethernetStats.rxBytes = 12345 + 100000; |
| 473 | ethernetStats.txBytes = 54321 + 100000; |
| 474 | entry = stats.getValues(ethernetPosition, entry); |
| 475 | assertNetworkStats(ethernetIface, ethernetStats, entry); |
Lorenzo Colitti | f1912ca | 2017-08-17 19:23:08 +0900 | [diff] [blame] | 476 | entry = perUidStats.getValues(ethernetPosition, entry); |
| 477 | assertNetworkStats(ethernetIface, ethernetStats, entry); |
Lorenzo Colitti | 5a7dea1 | 2017-07-12 15:48:07 +0900 | [diff] [blame] | 478 | } |
Lorenzo Colitti | 50b60fc | 2017-08-11 13:47:49 +0900 | [diff] [blame] | 479 | |
| 480 | @Test |
| 481 | public void testSetInterfaceQuota() throws Exception { |
| 482 | setupFunctioningHardwareInterface(); |
| 483 | enableOffload(); |
| 484 | |
| 485 | final OffloadController offload = makeOffloadController(); |
| 486 | offload.start(); |
| 487 | |
| 488 | final String ethernetIface = "eth1"; |
| 489 | final String mobileIface = "rmnet_data0"; |
| 490 | final long ethernetLimit = 12345; |
| 491 | final long mobileLimit = 12345678; |
| 492 | |
| 493 | final LinkProperties lp = new LinkProperties(); |
| 494 | lp.setInterfaceName(ethernetIface); |
| 495 | offload.setUpstreamLinkProperties(lp); |
| 496 | |
| 497 | ITetheringStatsProvider provider = mTetherStatsProviderCaptor.getValue(); |
| 498 | final InOrder inOrder = inOrder(mHardware); |
| 499 | when(mHardware.setUpstreamParameters(any(), any(), any(), any())).thenReturn(true); |
| 500 | when(mHardware.setDataLimit(anyString(), anyLong())).thenReturn(true); |
| 501 | |
| 502 | // Applying an interface quota to the current upstream immediately sends it to the hardware. |
| 503 | provider.setInterfaceQuota(ethernetIface, ethernetLimit); |
| 504 | waitForIdle(); |
| 505 | inOrder.verify(mHardware).setDataLimit(ethernetIface, ethernetLimit); |
| 506 | inOrder.verifyNoMoreInteractions(); |
| 507 | |
| 508 | // Applying an interface quota to another upstream does not take any immediate action. |
| 509 | provider.setInterfaceQuota(mobileIface, mobileLimit); |
| 510 | waitForIdle(); |
| 511 | inOrder.verify(mHardware, never()).setDataLimit(anyString(), anyLong()); |
| 512 | |
| 513 | // Switching to that upstream causes the quota to be applied if the parameters were applied |
| 514 | // correctly. |
| 515 | lp.setInterfaceName(mobileIface); |
| 516 | offload.setUpstreamLinkProperties(lp); |
| 517 | waitForIdle(); |
| 518 | inOrder.verify(mHardware).setDataLimit(mobileIface, mobileLimit); |
| 519 | |
| 520 | // Setting a limit of ITetheringStatsProvider.QUOTA_UNLIMITED causes the limit to be set |
| 521 | // to Long.MAX_VALUE. |
| 522 | provider.setInterfaceQuota(mobileIface, ITetheringStatsProvider.QUOTA_UNLIMITED); |
| 523 | waitForIdle(); |
| 524 | inOrder.verify(mHardware).setDataLimit(mobileIface, Long.MAX_VALUE); |
| 525 | |
| 526 | // If setting upstream parameters fails, then the data limit is not set. |
| 527 | when(mHardware.setUpstreamParameters(any(), any(), any(), any())).thenReturn(false); |
| 528 | lp.setInterfaceName(ethernetIface); |
| 529 | offload.setUpstreamLinkProperties(lp); |
| 530 | provider.setInterfaceQuota(mobileIface, mobileLimit); |
| 531 | waitForIdle(); |
| 532 | inOrder.verify(mHardware, never()).setDataLimit(anyString(), anyLong()); |
| 533 | |
| 534 | // If setting the data limit fails while changing upstreams, offload is stopped. |
| 535 | when(mHardware.setUpstreamParameters(any(), any(), any(), any())).thenReturn(true); |
| 536 | when(mHardware.setDataLimit(anyString(), anyLong())).thenReturn(false); |
| 537 | lp.setInterfaceName(mobileIface); |
| 538 | offload.setUpstreamLinkProperties(lp); |
| 539 | provider.setInterfaceQuota(mobileIface, mobileLimit); |
| 540 | waitForIdle(); |
Lorenzo Colitti | ddce7ee | 2017-08-21 12:34:50 +0900 | [diff] [blame] | 541 | inOrder.verify(mHardware).getForwardedStats(ethernetIface); |
Lorenzo Colitti | 50b60fc | 2017-08-11 13:47:49 +0900 | [diff] [blame] | 542 | inOrder.verify(mHardware).stopOffloadControl(); |
| 543 | } |
Lorenzo Colitti | 9f0baa9 | 2017-08-15 19:25:51 +0900 | [diff] [blame] | 544 | |
| 545 | @Test |
| 546 | public void testDataLimitCallback() throws Exception { |
| 547 | setupFunctioningHardwareInterface(); |
| 548 | enableOffload(); |
| 549 | |
| 550 | final OffloadController offload = makeOffloadController(); |
| 551 | offload.start(); |
| 552 | |
| 553 | OffloadHardwareInterface.ControlCallback callback = mControlCallbackCaptor.getValue(); |
| 554 | callback.onStoppedLimitReached(); |
| 555 | verify(mNMService, times(1)).tetherLimitReached(mTetherStatsProviderCaptor.getValue()); |
| 556 | } |
Erik Kline | b3bb26e | 2017-07-06 19:49:35 +0900 | [diff] [blame] | 557 | |
| 558 | @Test |
| 559 | public void testAddRemoveDownstreams() throws Exception { |
| 560 | setupFunctioningHardwareInterface(); |
| 561 | enableOffload(); |
| 562 | |
| 563 | final OffloadController offload = makeOffloadController(); |
| 564 | offload.start(); |
| 565 | |
| 566 | final InOrder inOrder = inOrder(mHardware); |
| 567 | inOrder.verify(mHardware, times(1)).initOffloadConfig(); |
| 568 | inOrder.verify(mHardware, times(1)).initOffloadControl( |
| 569 | any(OffloadHardwareInterface.ControlCallback.class)); |
| 570 | inOrder.verifyNoMoreInteractions(); |
| 571 | |
| 572 | // Tethering makes several calls to setLocalPrefixes() before add/remove |
| 573 | // downstream calls are made. This is not tested here; only the behavior |
| 574 | // of notifyDownstreamLinkProperties() and removeDownstreamInterface() |
| 575 | // are tested. |
| 576 | |
| 577 | // [1] USB tethering is started. |
| 578 | final LinkProperties usbLinkProperties = new LinkProperties(); |
| 579 | usbLinkProperties.setInterfaceName(RNDIS0); |
| 580 | usbLinkProperties.addLinkAddress(new LinkAddress("192.168.42.1/24")); |
| 581 | usbLinkProperties.addRoute(new RouteInfo(new IpPrefix(USB_PREFIX))); |
| 582 | offload.notifyDownstreamLinkProperties(usbLinkProperties); |
| 583 | inOrder.verify(mHardware, times(1)).addDownstreamPrefix(RNDIS0, USB_PREFIX); |
| 584 | inOrder.verifyNoMoreInteractions(); |
| 585 | |
| 586 | // [2] Routes for IPv6 link-local prefixes should never be added. |
| 587 | usbLinkProperties.addRoute(new RouteInfo(new IpPrefix(IPV6_LINKLOCAL))); |
| 588 | offload.notifyDownstreamLinkProperties(usbLinkProperties); |
| 589 | inOrder.verify(mHardware, never()).addDownstreamPrefix(eq(RNDIS0), anyString()); |
| 590 | inOrder.verifyNoMoreInteractions(); |
| 591 | |
| 592 | // [3] Add an IPv6 prefix for good measure. Only new offload-able |
| 593 | // prefixes should be passed to the HAL. |
| 594 | usbLinkProperties.addLinkAddress(new LinkAddress("2001:db8::1/64")); |
| 595 | usbLinkProperties.addRoute(new RouteInfo(new IpPrefix(IPV6_DOC_PREFIX))); |
| 596 | offload.notifyDownstreamLinkProperties(usbLinkProperties); |
| 597 | inOrder.verify(mHardware, times(1)).addDownstreamPrefix(RNDIS0, IPV6_DOC_PREFIX); |
| 598 | inOrder.verifyNoMoreInteractions(); |
| 599 | |
| 600 | // [4] Adding addresses doesn't affect notifyDownstreamLinkProperties(). |
| 601 | // The address is passed in by a separate setLocalPrefixes() invocation. |
| 602 | usbLinkProperties.addLinkAddress(new LinkAddress("2001:db8::2/64")); |
| 603 | offload.notifyDownstreamLinkProperties(usbLinkProperties); |
| 604 | inOrder.verify(mHardware, never()).addDownstreamPrefix(eq(RNDIS0), anyString()); |
| 605 | |
| 606 | // [5] Differences in local routes are converted into addDownstream() |
| 607 | // and removeDownstream() invocations accordingly. |
| 608 | usbLinkProperties.removeRoute(new RouteInfo(new IpPrefix(IPV6_DOC_PREFIX), null, RNDIS0)); |
| 609 | usbLinkProperties.addRoute(new RouteInfo(new IpPrefix(IPV6_DISCARD_PREFIX))); |
| 610 | offload.notifyDownstreamLinkProperties(usbLinkProperties); |
| 611 | inOrder.verify(mHardware, times(1)).removeDownstreamPrefix(RNDIS0, IPV6_DOC_PREFIX); |
| 612 | inOrder.verify(mHardware, times(1)).addDownstreamPrefix(RNDIS0, IPV6_DISCARD_PREFIX); |
| 613 | inOrder.verifyNoMoreInteractions(); |
| 614 | |
| 615 | // [6] Removing a downstream interface which was never added causes no |
| 616 | // interactions with the HAL. |
| 617 | offload.removeDownstreamInterface(WLAN0); |
| 618 | inOrder.verifyNoMoreInteractions(); |
| 619 | |
| 620 | // [7] Removing an active downstream removes all remaining prefixes. |
| 621 | offload.removeDownstreamInterface(RNDIS0); |
| 622 | inOrder.verify(mHardware, times(1)).removeDownstreamPrefix(RNDIS0, USB_PREFIX); |
| 623 | inOrder.verify(mHardware, times(1)).removeDownstreamPrefix(RNDIS0, IPV6_DISCARD_PREFIX); |
| 624 | inOrder.verifyNoMoreInteractions(); |
| 625 | } |
| 626 | |
Erik Kline | aabdaa9 | 2017-08-31 21:09:45 +0900 | [diff] [blame] | 627 | @Test |
| 628 | public void testControlCallbackOnStoppedUnsupportedFetchesAllStats() throws Exception { |
| 629 | setupFunctioningHardwareInterface(); |
| 630 | enableOffload(); |
| 631 | |
| 632 | final OffloadController offload = makeOffloadController(); |
| 633 | offload.start(); |
| 634 | |
| 635 | // Pretend to set a few different upstreams (only the interface name |
| 636 | // matters for this test; we're ignoring IP and route information). |
| 637 | final LinkProperties upstreamLp = new LinkProperties(); |
| 638 | for (String ifname : new String[]{RMNET0, WLAN0, RMNET0}) { |
| 639 | upstreamLp.setInterfaceName(ifname); |
| 640 | offload.setUpstreamLinkProperties(upstreamLp); |
| 641 | } |
| 642 | |
| 643 | // Clear invocation history, especially the getForwardedStats() calls |
| 644 | // that happen with setUpstreamParameters(). |
| 645 | clearInvocations(mHardware); |
| 646 | |
| 647 | OffloadHardwareInterface.ControlCallback callback = mControlCallbackCaptor.getValue(); |
| 648 | callback.onStoppedUnsupported(); |
| 649 | |
| 650 | // Verify forwarded stats behaviour. |
| 651 | verify(mHardware, times(1)).getForwardedStats(eq(RMNET0)); |
| 652 | verify(mHardware, times(1)).getForwardedStats(eq(WLAN0)); |
| 653 | verifyNoMoreInteractions(mHardware); |
| 654 | verify(mNMService, times(1)).tetherLimitReached(mTetherStatsProviderCaptor.getValue()); |
| 655 | verifyNoMoreInteractions(mNMService); |
| 656 | } |
| 657 | |
| 658 | @Test |
| 659 | public void testControlCallbackOnSupportAvailableFetchesAllStatsAndPushesAllParameters() |
| 660 | throws Exception { |
| 661 | setupFunctioningHardwareInterface(); |
| 662 | enableOffload(); |
| 663 | |
| 664 | final OffloadController offload = makeOffloadController(); |
| 665 | offload.start(); |
| 666 | |
| 667 | // Pretend to set a few different upstreams (only the interface name |
| 668 | // matters for this test; we're ignoring IP and route information). |
| 669 | final LinkProperties upstreamLp = new LinkProperties(); |
| 670 | for (String ifname : new String[]{RMNET0, WLAN0, RMNET0}) { |
| 671 | upstreamLp.setInterfaceName(ifname); |
| 672 | offload.setUpstreamLinkProperties(upstreamLp); |
| 673 | } |
| 674 | |
Erik Kline | bc8b2ee | 2017-09-19 17:56:10 +0900 | [diff] [blame] | 675 | // Pretend that some local prefixes and downstreams have been added |
| 676 | // (and removed, for good measure). |
| 677 | final Set<IpPrefix> minimumLocalPrefixes = new HashSet<>(); |
| 678 | for (String s : new String[]{ |
| 679 | "127.0.0.0/8", "192.0.2.0/24", "fe80::/64", "2001:db8::/64"}) { |
| 680 | minimumLocalPrefixes.add(new IpPrefix(s)); |
| 681 | } |
| 682 | offload.setLocalPrefixes(minimumLocalPrefixes); |
| 683 | |
| 684 | final LinkProperties usbLinkProperties = new LinkProperties(); |
| 685 | usbLinkProperties.setInterfaceName(RNDIS0); |
| 686 | usbLinkProperties.addLinkAddress(new LinkAddress("192.168.42.1/24")); |
| 687 | usbLinkProperties.addRoute(new RouteInfo(new IpPrefix(USB_PREFIX))); |
| 688 | offload.notifyDownstreamLinkProperties(usbLinkProperties); |
| 689 | |
| 690 | final LinkProperties wifiLinkProperties = new LinkProperties(); |
| 691 | wifiLinkProperties.setInterfaceName(WLAN0); |
| 692 | wifiLinkProperties.addLinkAddress(new LinkAddress("192.168.43.1/24")); |
| 693 | wifiLinkProperties.addRoute(new RouteInfo(new IpPrefix(WIFI_PREFIX))); |
| 694 | wifiLinkProperties.addRoute(new RouteInfo(new IpPrefix(IPV6_LINKLOCAL))); |
| 695 | // Use a benchmark prefix (RFC 5180 + erratum), since the documentation |
| 696 | // prefix is included in the excluded prefix list. |
| 697 | wifiLinkProperties.addLinkAddress(new LinkAddress("2001:2::1/64")); |
| 698 | wifiLinkProperties.addLinkAddress(new LinkAddress("2001:2::2/64")); |
| 699 | wifiLinkProperties.addRoute(new RouteInfo(new IpPrefix("2001:2::/64"))); |
| 700 | offload.notifyDownstreamLinkProperties(wifiLinkProperties); |
| 701 | |
| 702 | offload.removeDownstreamInterface(RNDIS0); |
| 703 | |
Erik Kline | aabdaa9 | 2017-08-31 21:09:45 +0900 | [diff] [blame] | 704 | // Clear invocation history, especially the getForwardedStats() calls |
| 705 | // that happen with setUpstreamParameters(). |
| 706 | clearInvocations(mHardware); |
| 707 | |
| 708 | OffloadHardwareInterface.ControlCallback callback = mControlCallbackCaptor.getValue(); |
| 709 | callback.onSupportAvailable(); |
| 710 | |
| 711 | // Verify forwarded stats behaviour. |
| 712 | verify(mHardware, times(1)).getForwardedStats(eq(RMNET0)); |
| 713 | verify(mHardware, times(1)).getForwardedStats(eq(WLAN0)); |
| 714 | verify(mNMService, times(1)).tetherLimitReached(mTetherStatsProviderCaptor.getValue()); |
| 715 | verifyNoMoreInteractions(mNMService); |
| 716 | |
| 717 | // TODO: verify local prefixes and downstreams are also pushed to the HAL. |
Erik Kline | bc8b2ee | 2017-09-19 17:56:10 +0900 | [diff] [blame] | 718 | verify(mHardware, times(1)).setLocalPrefixes(mStringArrayCaptor.capture()); |
| 719 | ArrayList<String> localPrefixes = mStringArrayCaptor.getValue(); |
| 720 | assertEquals(4, localPrefixes.size()); |
| 721 | assertArrayListContains(localPrefixes, |
| 722 | // TODO: The logic to find and exclude downstream IP prefixes |
| 723 | // is currently in Tethering's OffloadWrapper but must be moved |
| 724 | // into OffloadController proper. After this, also check for: |
| 725 | // "192.168.43.1/32", "2001:2::1/128", "2001:2::2/128" |
| 726 | "127.0.0.0/8", "192.0.2.0/24", "fe80::/64", "2001:db8::/64"); |
| 727 | verify(mHardware, times(1)).addDownstreamPrefix(WLAN0, "192.168.43.0/24"); |
| 728 | verify(mHardware, times(1)).addDownstreamPrefix(WLAN0, "2001:2::/64"); |
Erik Kline | aabdaa9 | 2017-08-31 21:09:45 +0900 | [diff] [blame] | 729 | verify(mHardware, times(1)).setUpstreamParameters(eq(RMNET0), any(), any(), any()); |
| 730 | verify(mHardware, times(1)).setDataLimit(eq(RMNET0), anyLong()); |
| 731 | verifyNoMoreInteractions(mHardware); |
| 732 | } |
| 733 | |
Erik Kline | b3bb26e | 2017-07-06 19:49:35 +0900 | [diff] [blame] | 734 | private static void assertArrayListContains(ArrayList<String> list, String... elems) { |
| 735 | for (String element : elems) { |
Erik Kline | bc8b2ee | 2017-09-19 17:56:10 +0900 | [diff] [blame] | 736 | assertTrue(element + " not in list", list.contains(element)); |
Erik Kline | b3bb26e | 2017-07-06 19:49:35 +0900 | [diff] [blame] | 737 | } |
| 738 | } |
Erik Kline | 92c4db0 | 2017-05-31 10:21:32 +0900 | [diff] [blame] | 739 | } |