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