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; |
Lorenzo Colitti | 4ca0ba8 | 2017-07-12 15:48:07 +0900 | [diff] [blame] | 35 | import static org.mockito.Mockito.doNothing; |
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 | 92c4db0 | 2017-05-31 10:21:32 +0900 | [diff] [blame] | 41 | import static org.mockito.Mockito.when; |
| 42 | |
| 43 | import android.content.Context; |
Erik Kline | ec37275 | 2017-06-07 16:33:19 +0900 | [diff] [blame] | 44 | import android.content.pm.ApplicationInfo; |
Lorenzo Colitti | 4ca0ba8 | 2017-07-12 15:48:07 +0900 | [diff] [blame] | 45 | import android.net.ITetheringStatsProvider; |
Erik Kline | 5acb4e3 | 2017-07-04 18:28:11 +0900 | [diff] [blame] | 46 | import android.net.IpPrefix; |
Erik Kline | 7990aef | 2017-06-01 20:11:25 +0900 | [diff] [blame] | 47 | import android.net.LinkAddress; |
| 48 | import android.net.LinkProperties; |
Lorenzo Colitti | 4ca0ba8 | 2017-07-12 15:48:07 +0900 | [diff] [blame] | 49 | import android.net.NetworkStats; |
Erik Kline | 7990aef | 2017-06-01 20:11:25 +0900 | [diff] [blame] | 50 | import android.net.RouteInfo; |
Erik Kline | 92c4db0 | 2017-05-31 10:21:32 +0900 | [diff] [blame] | 51 | import android.net.util.SharedLog; |
Lorenzo Colitti | 50b60fc | 2017-08-11 13:47:49 +0900 | [diff] [blame] | 52 | import android.os.ConditionVariable; |
Lorenzo Colitti | 4ca0ba8 | 2017-07-12 15:48:07 +0900 | [diff] [blame] | 53 | import android.os.Handler; |
| 54 | import android.os.Looper; |
| 55 | import android.os.INetworkManagementService; |
Erik Kline | 92c4db0 | 2017-05-31 10:21:32 +0900 | [diff] [blame] | 56 | import android.provider.Settings; |
| 57 | import android.provider.Settings.SettingNotFoundException; |
| 58 | |
| 59 | import android.support.test.filters.SmallTest; |
| 60 | import android.support.test.runner.AndroidJUnit4; |
| 61 | import android.test.mock.MockContentResolver; |
| 62 | import com.android.internal.util.test.FakeSettingsProvider; |
| 63 | |
Erik Kline | 7990aef | 2017-06-01 20:11:25 +0900 | [diff] [blame] | 64 | import java.net.InetAddress; |
| 65 | import java.util.ArrayList; |
Erik Kline | 5acb4e3 | 2017-07-04 18:28:11 +0900 | [diff] [blame] | 66 | import java.util.HashSet; |
| 67 | import java.util.Set; |
Erik Kline | 7990aef | 2017-06-01 20:11:25 +0900 | [diff] [blame] | 68 | |
Erik Kline | 5eaa7ed | 2017-07-07 17:38:30 +0900 | [diff] [blame] | 69 | import org.junit.After; |
Erik Kline | 92c4db0 | 2017-05-31 10:21:32 +0900 | [diff] [blame] | 70 | import org.junit.Before; |
| 71 | import org.junit.runner.RunWith; |
| 72 | import org.junit.Test; |
Erik Kline | 7990aef | 2017-06-01 20:11:25 +0900 | [diff] [blame] | 73 | import org.mockito.ArgumentCaptor; |
Erik Kline | 92c4db0 | 2017-05-31 10:21:32 +0900 | [diff] [blame] | 74 | import org.mockito.InOrder; |
| 75 | import org.mockito.Mock; |
| 76 | import org.mockito.MockitoAnnotations; |
| 77 | |
| 78 | |
| 79 | @RunWith(AndroidJUnit4.class) |
| 80 | @SmallTest |
| 81 | public class OffloadControllerTest { |
| 82 | |
| 83 | @Mock private OffloadHardwareInterface mHardware; |
Erik Kline | ec37275 | 2017-06-07 16:33:19 +0900 | [diff] [blame] | 84 | @Mock private ApplicationInfo mApplicationInfo; |
Erik Kline | 92c4db0 | 2017-05-31 10:21:32 +0900 | [diff] [blame] | 85 | @Mock private Context mContext; |
Lorenzo Colitti | 4ca0ba8 | 2017-07-12 15:48:07 +0900 | [diff] [blame] | 86 | @Mock private INetworkManagementService mNMService; |
Erik Kline | dd4d582 | 2017-06-12 18:20:08 +0900 | [diff] [blame] | 87 | private final ArgumentCaptor<ArrayList> mStringArrayCaptor = |
| 88 | ArgumentCaptor.forClass(ArrayList.class); |
Lorenzo Colitti | 4ca0ba8 | 2017-07-12 15:48:07 +0900 | [diff] [blame] | 89 | private final ArgumentCaptor<ITetheringStatsProvider.Stub> mTetherStatsProviderCaptor = |
| 90 | ArgumentCaptor.forClass(ITetheringStatsProvider.Stub.class); |
Lorenzo Colitti | 9f0baa9 | 2017-08-15 19:25:51 +0900 | [diff] [blame] | 91 | private final ArgumentCaptor<OffloadHardwareInterface.ControlCallback> mControlCallbackCaptor = |
| 92 | ArgumentCaptor.forClass(OffloadHardwareInterface.ControlCallback.class); |
Erik Kline | 92c4db0 | 2017-05-31 10:21:32 +0900 | [diff] [blame] | 93 | private MockContentResolver mContentResolver; |
| 94 | |
Lorenzo Colitti | 4ca0ba8 | 2017-07-12 15:48:07 +0900 | [diff] [blame] | 95 | @Before public void setUp() { |
Erik Kline | 92c4db0 | 2017-05-31 10:21:32 +0900 | [diff] [blame] | 96 | MockitoAnnotations.initMocks(this); |
Erik Kline | ec37275 | 2017-06-07 16:33:19 +0900 | [diff] [blame] | 97 | when(mContext.getApplicationInfo()).thenReturn(mApplicationInfo); |
| 98 | when(mContext.getPackageName()).thenReturn("OffloadControllerTest"); |
Erik Kline | 92c4db0 | 2017-05-31 10:21:32 +0900 | [diff] [blame] | 99 | mContentResolver = new MockContentResolver(mContext); |
| 100 | mContentResolver.addProvider(Settings.AUTHORITY, new FakeSettingsProvider()); |
Erik Kline | 92c4db0 | 2017-05-31 10:21:32 +0900 | [diff] [blame] | 101 | when(mContext.getContentResolver()).thenReturn(mContentResolver); |
Erik Kline | 5eaa7ed | 2017-07-07 17:38:30 +0900 | [diff] [blame] | 102 | // TODO: call this when available. |
| 103 | // FakeSettingsProvider.clearSettingsProvider(); |
| 104 | } |
| 105 | |
| 106 | @After public void tearDown() throws Exception { |
| 107 | // TODO: call this when available. |
| 108 | // FakeSettingsProvider.clearSettingsProvider(); |
Erik Kline | 92c4db0 | 2017-05-31 10:21:32 +0900 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | private void setupFunctioningHardwareInterface() { |
| 112 | when(mHardware.initOffloadConfig()).thenReturn(true); |
Lorenzo Colitti | 9f0baa9 | 2017-08-15 19:25:51 +0900 | [diff] [blame] | 113 | when(mHardware.initOffloadControl(mControlCallbackCaptor.capture())) |
Erik Kline | 92c4db0 | 2017-05-31 10:21:32 +0900 | [diff] [blame] | 114 | .thenReturn(true); |
Lorenzo Colitti | ddce7ee | 2017-08-21 12:34:50 +0900 | [diff] [blame^] | 115 | when(mHardware.setUpstreamParameters(anyString(), any(), any(), any())).thenReturn(true); |
Lorenzo Colitti | 4ca0ba8 | 2017-07-12 15:48:07 +0900 | [diff] [blame] | 116 | when(mHardware.getForwardedStats(any())).thenReturn(new ForwardedStats()); |
Lorenzo Colitti | ddce7ee | 2017-08-21 12:34:50 +0900 | [diff] [blame^] | 117 | when(mHardware.setDataLimit(anyString(), anyLong())).thenReturn(true); |
Erik Kline | 92c4db0 | 2017-05-31 10:21:32 +0900 | [diff] [blame] | 118 | } |
| 119 | |
Erik Kline | 5eaa7ed | 2017-07-07 17:38:30 +0900 | [diff] [blame] | 120 | private void enableOffload() { |
| 121 | Settings.Global.putInt(mContentResolver, TETHER_OFFLOAD_DISABLED, 0); |
| 122 | } |
| 123 | |
Lorenzo Colitti | 50b60fc | 2017-08-11 13:47:49 +0900 | [diff] [blame] | 124 | private void waitForIdle() { |
| 125 | ConditionVariable cv = new ConditionVariable(); |
| 126 | new Handler(Looper.getMainLooper()).post(() -> { cv.open(); }); |
| 127 | cv.block(); |
| 128 | } |
| 129 | |
Lorenzo Colitti | 4ca0ba8 | 2017-07-12 15:48:07 +0900 | [diff] [blame] | 130 | private OffloadController makeOffloadController() throws Exception { |
| 131 | OffloadController offload = new OffloadController(new Handler(Looper.getMainLooper()), |
| 132 | mHardware, mContentResolver, mNMService, new SharedLog("test")); |
| 133 | verify(mNMService).registerTetheringStatsProvider( |
| 134 | mTetherStatsProviderCaptor.capture(), anyString()); |
| 135 | return offload; |
| 136 | } |
| 137 | |
Erik Kline | 5eaa7ed | 2017-07-07 17:38:30 +0900 | [diff] [blame] | 138 | // TODO: Restore when FakeSettingsProvider.clearSettingsProvider() is available. |
| 139 | // @Test |
Lorenzo Colitti | 4ca0ba8 | 2017-07-12 15:48:07 +0900 | [diff] [blame] | 140 | public void testNoSettingsValueDefaultDisabledDoesNotStart() throws Exception { |
Erik Kline | 92c4db0 | 2017-05-31 10:21:32 +0900 | [diff] [blame] | 141 | setupFunctioningHardwareInterface(); |
Erik Kline | 5eaa7ed | 2017-07-07 17:38:30 +0900 | [diff] [blame] | 142 | when(mHardware.getDefaultTetherOffloadDisabled()).thenReturn(1); |
Erik Kline | 92c4db0 | 2017-05-31 10:21:32 +0900 | [diff] [blame] | 143 | try { |
| 144 | Settings.Global.getInt(mContentResolver, TETHER_OFFLOAD_DISABLED); |
| 145 | fail(); |
| 146 | } catch (SettingNotFoundException expected) {} |
| 147 | |
Lorenzo Colitti | 4ca0ba8 | 2017-07-12 15:48:07 +0900 | [diff] [blame] | 148 | final OffloadController offload = makeOffloadController(); |
Erik Kline | 92c4db0 | 2017-05-31 10:21:32 +0900 | [diff] [blame] | 149 | offload.start(); |
| 150 | |
| 151 | final InOrder inOrder = inOrder(mHardware); |
Erik Kline | 5eaa7ed | 2017-07-07 17:38:30 +0900 | [diff] [blame] | 152 | inOrder.verify(mHardware, times(1)).getDefaultTetherOffloadDisabled(); |
| 153 | inOrder.verify(mHardware, never()).initOffloadConfig(); |
| 154 | inOrder.verify(mHardware, never()).initOffloadControl( |
| 155 | any(OffloadHardwareInterface.ControlCallback.class)); |
| 156 | inOrder.verifyNoMoreInteractions(); |
| 157 | } |
| 158 | |
| 159 | // TODO: Restore when FakeSettingsProvider.clearSettingsProvider() is available. |
| 160 | // @Test |
Lorenzo Colitti | 4ca0ba8 | 2017-07-12 15:48:07 +0900 | [diff] [blame] | 161 | public void testNoSettingsValueDefaultEnabledDoesStart() throws Exception { |
Erik Kline | 5eaa7ed | 2017-07-07 17:38:30 +0900 | [diff] [blame] | 162 | setupFunctioningHardwareInterface(); |
| 163 | when(mHardware.getDefaultTetherOffloadDisabled()).thenReturn(0); |
| 164 | try { |
| 165 | Settings.Global.getInt(mContentResolver, TETHER_OFFLOAD_DISABLED); |
| 166 | fail(); |
| 167 | } catch (SettingNotFoundException expected) {} |
| 168 | |
Lorenzo Colitti | 4ca0ba8 | 2017-07-12 15:48:07 +0900 | [diff] [blame] | 169 | final OffloadController offload = makeOffloadController(); |
Erik Kline | 5eaa7ed | 2017-07-07 17:38:30 +0900 | [diff] [blame] | 170 | offload.start(); |
| 171 | |
| 172 | final InOrder inOrder = inOrder(mHardware); |
| 173 | inOrder.verify(mHardware, times(1)).getDefaultTetherOffloadDisabled(); |
Erik Kline | 92c4db0 | 2017-05-31 10:21:32 +0900 | [diff] [blame] | 174 | inOrder.verify(mHardware, times(1)).initOffloadConfig(); |
| 175 | inOrder.verify(mHardware, times(1)).initOffloadControl( |
| 176 | any(OffloadHardwareInterface.ControlCallback.class)); |
| 177 | inOrder.verifyNoMoreInteractions(); |
| 178 | } |
| 179 | |
| 180 | @Test |
Lorenzo Colitti | 4ca0ba8 | 2017-07-12 15:48:07 +0900 | [diff] [blame] | 181 | public void testSettingsAllowsStart() throws Exception { |
Erik Kline | 92c4db0 | 2017-05-31 10:21:32 +0900 | [diff] [blame] | 182 | setupFunctioningHardwareInterface(); |
| 183 | Settings.Global.putInt(mContentResolver, TETHER_OFFLOAD_DISABLED, 0); |
| 184 | |
Lorenzo Colitti | 4ca0ba8 | 2017-07-12 15:48:07 +0900 | [diff] [blame] | 185 | final OffloadController offload = makeOffloadController(); |
Erik Kline | 92c4db0 | 2017-05-31 10:21:32 +0900 | [diff] [blame] | 186 | offload.start(); |
| 187 | |
| 188 | final InOrder inOrder = inOrder(mHardware); |
Erik Kline | 5eaa7ed | 2017-07-07 17:38:30 +0900 | [diff] [blame] | 189 | inOrder.verify(mHardware, times(1)).getDefaultTetherOffloadDisabled(); |
Erik Kline | 92c4db0 | 2017-05-31 10:21:32 +0900 | [diff] [blame] | 190 | inOrder.verify(mHardware, times(1)).initOffloadConfig(); |
| 191 | inOrder.verify(mHardware, times(1)).initOffloadControl( |
| 192 | any(OffloadHardwareInterface.ControlCallback.class)); |
| 193 | inOrder.verifyNoMoreInteractions(); |
| 194 | } |
| 195 | |
| 196 | @Test |
Lorenzo Colitti | 4ca0ba8 | 2017-07-12 15:48:07 +0900 | [diff] [blame] | 197 | public void testSettingsDisablesStart() throws Exception { |
Erik Kline | 92c4db0 | 2017-05-31 10:21:32 +0900 | [diff] [blame] | 198 | setupFunctioningHardwareInterface(); |
| 199 | Settings.Global.putInt(mContentResolver, TETHER_OFFLOAD_DISABLED, 1); |
| 200 | |
Lorenzo Colitti | 4ca0ba8 | 2017-07-12 15:48:07 +0900 | [diff] [blame] | 201 | final OffloadController offload = makeOffloadController(); |
Erik Kline | 92c4db0 | 2017-05-31 10:21:32 +0900 | [diff] [blame] | 202 | offload.start(); |
| 203 | |
| 204 | final InOrder inOrder = inOrder(mHardware); |
Erik Kline | 5eaa7ed | 2017-07-07 17:38:30 +0900 | [diff] [blame] | 205 | inOrder.verify(mHardware, times(1)).getDefaultTetherOffloadDisabled(); |
Erik Kline | 92c4db0 | 2017-05-31 10:21:32 +0900 | [diff] [blame] | 206 | inOrder.verify(mHardware, never()).initOffloadConfig(); |
| 207 | inOrder.verify(mHardware, never()).initOffloadControl(anyObject()); |
| 208 | inOrder.verifyNoMoreInteractions(); |
| 209 | } |
Erik Kline | 7990aef | 2017-06-01 20:11:25 +0900 | [diff] [blame] | 210 | |
| 211 | @Test |
| 212 | public void testSetUpstreamLinkPropertiesWorking() throws Exception { |
| 213 | setupFunctioningHardwareInterface(); |
Erik Kline | 5eaa7ed | 2017-07-07 17:38:30 +0900 | [diff] [blame] | 214 | enableOffload(); |
| 215 | |
Lorenzo Colitti | 4ca0ba8 | 2017-07-12 15:48:07 +0900 | [diff] [blame] | 216 | final OffloadController offload = makeOffloadController(); |
Erik Kline | 7990aef | 2017-06-01 20:11:25 +0900 | [diff] [blame] | 217 | offload.start(); |
| 218 | |
| 219 | final InOrder inOrder = inOrder(mHardware); |
Erik Kline | 5eaa7ed | 2017-07-07 17:38:30 +0900 | [diff] [blame] | 220 | inOrder.verify(mHardware, times(1)).getDefaultTetherOffloadDisabled(); |
Erik Kline | 7990aef | 2017-06-01 20:11:25 +0900 | [diff] [blame] | 221 | inOrder.verify(mHardware, times(1)).initOffloadConfig(); |
| 222 | inOrder.verify(mHardware, times(1)).initOffloadControl( |
| 223 | any(OffloadHardwareInterface.ControlCallback.class)); |
| 224 | inOrder.verifyNoMoreInteractions(); |
| 225 | |
Erik Kline | 5acb4e3 | 2017-07-04 18:28:11 +0900 | [diff] [blame] | 226 | // In reality, the UpstreamNetworkMonitor would have passed down to us |
| 227 | // a covering set of local prefixes representing a minimum essential |
| 228 | // set plus all the prefixes on networks with network agents. |
| 229 | // |
| 230 | // We simulate that there, and then add upstream elements one by one |
| 231 | // and watch what happens. |
| 232 | final Set<IpPrefix> minimumLocalPrefixes = new HashSet<>(); |
| 233 | for (String s : new String[]{ |
| 234 | "127.0.0.0/8", "192.0.2.0/24", "fe80::/64", "2001:db8::/64"}) { |
| 235 | minimumLocalPrefixes.add(new IpPrefix(s)); |
| 236 | } |
| 237 | offload.setLocalPrefixes(minimumLocalPrefixes); |
| 238 | inOrder.verify(mHardware, times(1)).setLocalPrefixes(mStringArrayCaptor.capture()); |
| 239 | ArrayList<String> localPrefixes = mStringArrayCaptor.getValue(); |
| 240 | assertEquals(4, localPrefixes.size()); |
| 241 | assertTrue(localPrefixes.contains("127.0.0.0/8")); |
| 242 | assertTrue(localPrefixes.contains("192.0.2.0/24")); |
| 243 | assertTrue(localPrefixes.contains("fe80::/64")); |
| 244 | assertTrue(localPrefixes.contains("2001:db8::/64")); |
Erik Kline | 7990aef | 2017-06-01 20:11:25 +0900 | [diff] [blame] | 245 | inOrder.verifyNoMoreInteractions(); |
Erik Kline | 5acb4e3 | 2017-07-04 18:28:11 +0900 | [diff] [blame] | 246 | |
| 247 | offload.setUpstreamLinkProperties(null); |
| 248 | // No change in local addresses means no call to setLocalPrefixes(). |
| 249 | inOrder.verify(mHardware, never()).setLocalPrefixes(mStringArrayCaptor.capture()); |
| 250 | // This LinkProperties value does not differ from the default upstream. |
| 251 | // There should be no extraneous call to setUpstreamParameters(). |
| 252 | inOrder.verify(mHardware, never()).setUpstreamParameters( |
| 253 | anyObject(), anyObject(), anyObject(), anyObject()); |
| 254 | inOrder.verifyNoMoreInteractions(); |
Erik Kline | 7990aef | 2017-06-01 20:11:25 +0900 | [diff] [blame] | 255 | |
| 256 | final LinkProperties lp = new LinkProperties(); |
| 257 | |
| 258 | final String testIfName = "rmnet_data17"; |
| 259 | lp.setInterfaceName(testIfName); |
| 260 | offload.setUpstreamLinkProperties(lp); |
Erik Kline | 5acb4e3 | 2017-07-04 18:28:11 +0900 | [diff] [blame] | 261 | // No change in local addresses means no call to setLocalPrefixes(). |
| 262 | inOrder.verify(mHardware, never()).setLocalPrefixes(mStringArrayCaptor.capture()); |
Erik Kline | 7990aef | 2017-06-01 20:11:25 +0900 | [diff] [blame] | 263 | inOrder.verify(mHardware, times(1)).setUpstreamParameters( |
Erik Kline | 664d208 | 2017-06-15 18:06:34 +0900 | [diff] [blame] | 264 | eq(testIfName), eq(null), eq(null), eq(null)); |
Lorenzo Colitti | ddce7ee | 2017-08-21 12:34:50 +0900 | [diff] [blame^] | 265 | inOrder.verify(mHardware, times(1)).setDataLimit(eq(testIfName), eq(Long.MAX_VALUE)); |
Erik Kline | 7990aef | 2017-06-01 20:11:25 +0900 | [diff] [blame] | 266 | inOrder.verifyNoMoreInteractions(); |
| 267 | |
| 268 | final String ipv4Addr = "192.0.2.5"; |
| 269 | final String linkAddr = ipv4Addr + "/24"; |
| 270 | lp.addLinkAddress(new LinkAddress(linkAddr)); |
Erik Kline | 5acb4e3 | 2017-07-04 18:28:11 +0900 | [diff] [blame] | 271 | lp.addRoute(new RouteInfo(new IpPrefix("192.0.2.0/24"))); |
Erik Kline | 7990aef | 2017-06-01 20:11:25 +0900 | [diff] [blame] | 272 | offload.setUpstreamLinkProperties(lp); |
Erik Kline | 5acb4e3 | 2017-07-04 18:28:11 +0900 | [diff] [blame] | 273 | // IPv4 prefixes and addresses on the upstream are simply left as whole |
| 274 | // prefixes (already passed in from UpstreamNetworkMonitor code). If a |
| 275 | // tethering client sends traffic to the IPv4 default router or other |
| 276 | // clients on the upstream this will not be hardware-forwarded, and that |
| 277 | // should be fine for now. Ergo: no change in local addresses, no call |
| 278 | // to setLocalPrefixes(). |
| 279 | inOrder.verify(mHardware, never()).setLocalPrefixes(mStringArrayCaptor.capture()); |
Erik Kline | 7990aef | 2017-06-01 20:11:25 +0900 | [diff] [blame] | 280 | inOrder.verify(mHardware, times(1)).setUpstreamParameters( |
Erik Kline | 664d208 | 2017-06-15 18:06:34 +0900 | [diff] [blame] | 281 | eq(testIfName), eq(ipv4Addr), eq(null), eq(null)); |
Lorenzo Colitti | 4ca0ba8 | 2017-07-12 15:48:07 +0900 | [diff] [blame] | 282 | inOrder.verify(mHardware, times(1)).getForwardedStats(eq(testIfName)); |
Lorenzo Colitti | ddce7ee | 2017-08-21 12:34:50 +0900 | [diff] [blame^] | 283 | inOrder.verify(mHardware, times(1)).setDataLimit(eq(testIfName), eq(Long.MAX_VALUE)); |
Erik Kline | 7990aef | 2017-06-01 20:11:25 +0900 | [diff] [blame] | 284 | inOrder.verifyNoMoreInteractions(); |
| 285 | |
| 286 | final String ipv4Gateway = "192.0.2.1"; |
| 287 | lp.addRoute(new RouteInfo(InetAddress.getByName(ipv4Gateway))); |
| 288 | offload.setUpstreamLinkProperties(lp); |
Erik Kline | 5acb4e3 | 2017-07-04 18:28:11 +0900 | [diff] [blame] | 289 | // No change in local addresses means no call to setLocalPrefixes(). |
| 290 | inOrder.verify(mHardware, never()).setLocalPrefixes(mStringArrayCaptor.capture()); |
Erik Kline | 7990aef | 2017-06-01 20:11:25 +0900 | [diff] [blame] | 291 | inOrder.verify(mHardware, times(1)).setUpstreamParameters( |
Erik Kline | 664d208 | 2017-06-15 18:06:34 +0900 | [diff] [blame] | 292 | eq(testIfName), eq(ipv4Addr), eq(ipv4Gateway), eq(null)); |
Lorenzo Colitti | 4ca0ba8 | 2017-07-12 15:48:07 +0900 | [diff] [blame] | 293 | inOrder.verify(mHardware, times(1)).getForwardedStats(eq(testIfName)); |
Lorenzo Colitti | ddce7ee | 2017-08-21 12:34:50 +0900 | [diff] [blame^] | 294 | inOrder.verify(mHardware, times(1)).setDataLimit(eq(testIfName), eq(Long.MAX_VALUE)); |
Erik Kline | 7990aef | 2017-06-01 20:11:25 +0900 | [diff] [blame] | 295 | inOrder.verifyNoMoreInteractions(); |
| 296 | |
| 297 | final String ipv6Gw1 = "fe80::cafe"; |
| 298 | lp.addRoute(new RouteInfo(InetAddress.getByName(ipv6Gw1))); |
| 299 | offload.setUpstreamLinkProperties(lp); |
Erik Kline | 5acb4e3 | 2017-07-04 18:28:11 +0900 | [diff] [blame] | 300 | // No change in local addresses means no call to setLocalPrefixes(). |
| 301 | inOrder.verify(mHardware, never()).setLocalPrefixes(mStringArrayCaptor.capture()); |
Erik Kline | 7990aef | 2017-06-01 20:11:25 +0900 | [diff] [blame] | 302 | inOrder.verify(mHardware, times(1)).setUpstreamParameters( |
| 303 | eq(testIfName), eq(ipv4Addr), eq(ipv4Gateway), mStringArrayCaptor.capture()); |
Lorenzo Colitti | 4ca0ba8 | 2017-07-12 15:48:07 +0900 | [diff] [blame] | 304 | inOrder.verify(mHardware, times(1)).getForwardedStats(eq(testIfName)); |
Erik Kline | 7990aef | 2017-06-01 20:11:25 +0900 | [diff] [blame] | 305 | ArrayList<String> v6gws = mStringArrayCaptor.getValue(); |
| 306 | assertEquals(1, v6gws.size()); |
| 307 | assertTrue(v6gws.contains(ipv6Gw1)); |
Lorenzo Colitti | ddce7ee | 2017-08-21 12:34:50 +0900 | [diff] [blame^] | 308 | inOrder.verify(mHardware, times(1)).setDataLimit(eq(testIfName), eq(Long.MAX_VALUE)); |
Erik Kline | 7990aef | 2017-06-01 20:11:25 +0900 | [diff] [blame] | 309 | inOrder.verifyNoMoreInteractions(); |
| 310 | |
| 311 | final String ipv6Gw2 = "fe80::d00d"; |
| 312 | lp.addRoute(new RouteInfo(InetAddress.getByName(ipv6Gw2))); |
| 313 | offload.setUpstreamLinkProperties(lp); |
Erik Kline | 5acb4e3 | 2017-07-04 18:28:11 +0900 | [diff] [blame] | 314 | // No change in local addresses means no call to setLocalPrefixes(). |
| 315 | inOrder.verify(mHardware, never()).setLocalPrefixes(mStringArrayCaptor.capture()); |
Erik Kline | 7990aef | 2017-06-01 20:11:25 +0900 | [diff] [blame] | 316 | inOrder.verify(mHardware, times(1)).setUpstreamParameters( |
| 317 | eq(testIfName), eq(ipv4Addr), eq(ipv4Gateway), mStringArrayCaptor.capture()); |
Lorenzo Colitti | 4ca0ba8 | 2017-07-12 15:48:07 +0900 | [diff] [blame] | 318 | inOrder.verify(mHardware, times(1)).getForwardedStats(eq(testIfName)); |
Erik Kline | 7990aef | 2017-06-01 20:11:25 +0900 | [diff] [blame] | 319 | v6gws = mStringArrayCaptor.getValue(); |
| 320 | assertEquals(2, v6gws.size()); |
| 321 | assertTrue(v6gws.contains(ipv6Gw1)); |
| 322 | assertTrue(v6gws.contains(ipv6Gw2)); |
Lorenzo Colitti | ddce7ee | 2017-08-21 12:34:50 +0900 | [diff] [blame^] | 323 | inOrder.verify(mHardware, times(1)).setDataLimit(eq(testIfName), eq(Long.MAX_VALUE)); |
Erik Kline | 7990aef | 2017-06-01 20:11:25 +0900 | [diff] [blame] | 324 | inOrder.verifyNoMoreInteractions(); |
| 325 | |
| 326 | final LinkProperties stacked = new LinkProperties(); |
| 327 | stacked.setInterfaceName("stacked"); |
| 328 | stacked.addLinkAddress(new LinkAddress("192.0.2.129/25")); |
| 329 | stacked.addRoute(new RouteInfo(InetAddress.getByName("192.0.2.254"))); |
| 330 | stacked.addRoute(new RouteInfo(InetAddress.getByName("fe80::bad:f00"))); |
| 331 | assertTrue(lp.addStackedLink(stacked)); |
| 332 | offload.setUpstreamLinkProperties(lp); |
Erik Kline | 5acb4e3 | 2017-07-04 18:28:11 +0900 | [diff] [blame] | 333 | // No change in local addresses means no call to setLocalPrefixes(). |
| 334 | inOrder.verify(mHardware, never()).setLocalPrefixes(mStringArrayCaptor.capture()); |
Erik Kline | 7990aef | 2017-06-01 20:11:25 +0900 | [diff] [blame] | 335 | inOrder.verify(mHardware, times(1)).setUpstreamParameters( |
| 336 | eq(testIfName), eq(ipv4Addr), eq(ipv4Gateway), mStringArrayCaptor.capture()); |
Lorenzo Colitti | 4ca0ba8 | 2017-07-12 15:48:07 +0900 | [diff] [blame] | 337 | inOrder.verify(mHardware, times(1)).getForwardedStats(eq(testIfName)); |
Erik Kline | 7990aef | 2017-06-01 20:11:25 +0900 | [diff] [blame] | 338 | v6gws = mStringArrayCaptor.getValue(); |
| 339 | assertEquals(2, v6gws.size()); |
| 340 | assertTrue(v6gws.contains(ipv6Gw1)); |
| 341 | assertTrue(v6gws.contains(ipv6Gw2)); |
Lorenzo Colitti | ddce7ee | 2017-08-21 12:34:50 +0900 | [diff] [blame^] | 342 | inOrder.verify(mHardware, times(1)).setDataLimit(eq(testIfName), eq(Long.MAX_VALUE)); |
Erik Kline | 7990aef | 2017-06-01 20:11:25 +0900 | [diff] [blame] | 343 | inOrder.verifyNoMoreInteractions(); |
Erik Kline | 5acb4e3 | 2017-07-04 18:28:11 +0900 | [diff] [blame] | 344 | |
| 345 | // Add in some IPv6 upstream info. When there is a tethered downstream |
| 346 | // making use of the IPv6 prefix we would expect to see the /64 route |
| 347 | // removed from "local prefixes" and /128s added for the upstream IPv6 |
| 348 | // addresses. This is not yet implemented, and for now we simply |
| 349 | // expect to see these /128s. |
| 350 | lp.addRoute(new RouteInfo(new IpPrefix("2001:db8::/64"))); |
| 351 | // "2001:db8::/64" plus "assigned" ASCII in hex |
| 352 | lp.addLinkAddress(new LinkAddress("2001:db8::6173:7369:676e:6564/64")); |
| 353 | // "2001:db8::/64" plus "random" ASCII in hex |
| 354 | lp.addLinkAddress(new LinkAddress("2001:db8::7261:6e64:6f6d/64")); |
| 355 | offload.setUpstreamLinkProperties(lp); |
| 356 | inOrder.verify(mHardware, times(1)).setLocalPrefixes(mStringArrayCaptor.capture()); |
| 357 | localPrefixes = mStringArrayCaptor.getValue(); |
| 358 | assertEquals(6, localPrefixes.size()); |
| 359 | assertTrue(localPrefixes.contains("127.0.0.0/8")); |
| 360 | assertTrue(localPrefixes.contains("192.0.2.0/24")); |
| 361 | assertTrue(localPrefixes.contains("fe80::/64")); |
| 362 | assertTrue(localPrefixes.contains("2001:db8::/64")); |
| 363 | assertTrue(localPrefixes.contains("2001:db8::6173:7369:676e:6564/128")); |
| 364 | assertTrue(localPrefixes.contains("2001:db8::7261:6e64:6f6d/128")); |
| 365 | // The relevant parts of the LinkProperties have not changed, but at the |
| 366 | // moment we do not de-dup upstream LinkProperties this carefully. |
| 367 | inOrder.verify(mHardware, times(1)).setUpstreamParameters( |
| 368 | eq(testIfName), eq(ipv4Addr), eq(ipv4Gateway), mStringArrayCaptor.capture()); |
| 369 | v6gws = mStringArrayCaptor.getValue(); |
| 370 | assertEquals(2, v6gws.size()); |
| 371 | assertTrue(v6gws.contains(ipv6Gw1)); |
| 372 | assertTrue(v6gws.contains(ipv6Gw2)); |
Lorenzo Colitti | 4ca0ba8 | 2017-07-12 15:48:07 +0900 | [diff] [blame] | 373 | inOrder.verify(mHardware, times(1)).getForwardedStats(eq(testIfName)); |
Lorenzo Colitti | ddce7ee | 2017-08-21 12:34:50 +0900 | [diff] [blame^] | 374 | inOrder.verify(mHardware, times(1)).setDataLimit(eq(testIfName), eq(Long.MAX_VALUE)); |
Erik Kline | 5acb4e3 | 2017-07-04 18:28:11 +0900 | [diff] [blame] | 375 | inOrder.verifyNoMoreInteractions(); |
| 376 | |
| 377 | // Completely identical LinkProperties updates are de-duped. |
| 378 | offload.setUpstreamLinkProperties(lp); |
| 379 | // This LinkProperties value does not differ from the default upstream. |
| 380 | // There should be no extraneous call to setUpstreamParameters(). |
| 381 | inOrder.verify(mHardware, never()).setUpstreamParameters( |
| 382 | anyObject(), anyObject(), anyObject(), anyObject()); |
| 383 | inOrder.verifyNoMoreInteractions(); |
Erik Kline | 7990aef | 2017-06-01 20:11:25 +0900 | [diff] [blame] | 384 | } |
Lorenzo Colitti | 4ca0ba8 | 2017-07-12 15:48:07 +0900 | [diff] [blame] | 385 | |
| 386 | private void assertNetworkStats(String iface, ForwardedStats stats, NetworkStats.Entry entry) { |
| 387 | assertEquals(iface, entry.iface); |
| 388 | assertEquals(stats.rxBytes, entry.rxBytes); |
| 389 | assertEquals(stats.txBytes, entry.txBytes); |
| 390 | assertEquals(SET_DEFAULT, entry.set); |
| 391 | assertEquals(TAG_NONE, entry.tag); |
Lorenzo Colitti | 4ca0ba8 | 2017-07-12 15:48:07 +0900 | [diff] [blame] | 392 | } |
| 393 | |
| 394 | @Test |
| 395 | public void testGetForwardedStats() throws Exception { |
| 396 | setupFunctioningHardwareInterface(); |
| 397 | enableOffload(); |
| 398 | |
| 399 | final OffloadController offload = makeOffloadController(); |
| 400 | offload.start(); |
| 401 | |
| 402 | final String ethernetIface = "eth1"; |
| 403 | final String mobileIface = "rmnet_data0"; |
| 404 | |
| 405 | ForwardedStats ethernetStats = new ForwardedStats(); |
| 406 | ethernetStats.rxBytes = 12345; |
| 407 | ethernetStats.txBytes = 54321; |
| 408 | |
| 409 | ForwardedStats mobileStats = new ForwardedStats(); |
| 410 | mobileStats.rxBytes = 999; |
| 411 | mobileStats.txBytes = 99999; |
| 412 | |
| 413 | when(mHardware.getForwardedStats(eq(ethernetIface))).thenReturn(ethernetStats); |
| 414 | when(mHardware.getForwardedStats(eq(mobileIface))).thenReturn(mobileStats); |
| 415 | |
Hugo Benichi | 752c128 | 2017-08-22 13:57:41 +0900 | [diff] [blame] | 416 | InOrder inOrder = inOrder(mHardware); |
| 417 | |
Lorenzo Colitti | 4ca0ba8 | 2017-07-12 15:48:07 +0900 | [diff] [blame] | 418 | final LinkProperties lp = new LinkProperties(); |
| 419 | lp.setInterfaceName(ethernetIface); |
| 420 | offload.setUpstreamLinkProperties(lp); |
Hugo Benichi | 752c128 | 2017-08-22 13:57:41 +0900 | [diff] [blame] | 421 | // Previous upstream was null, so no stats are fetched. |
| 422 | inOrder.verify(mHardware, never()).getForwardedStats(any()); |
Lorenzo Colitti | 4ca0ba8 | 2017-07-12 15:48:07 +0900 | [diff] [blame] | 423 | |
| 424 | lp.setInterfaceName(mobileIface); |
| 425 | offload.setUpstreamLinkProperties(lp); |
Hugo Benichi | 752c128 | 2017-08-22 13:57:41 +0900 | [diff] [blame] | 426 | // Expect that we fetch stats from the previous upstream. |
| 427 | inOrder.verify(mHardware, times(1)).getForwardedStats(eq(ethernetIface)); |
Lorenzo Colitti | 4ca0ba8 | 2017-07-12 15:48:07 +0900 | [diff] [blame] | 428 | |
| 429 | lp.setInterfaceName(ethernetIface); |
| 430 | offload.setUpstreamLinkProperties(lp); |
Hugo Benichi | 752c128 | 2017-08-22 13:57:41 +0900 | [diff] [blame] | 431 | // Expect that we fetch stats from the previous upstream. |
| 432 | inOrder.verify(mHardware, times(1)).getForwardedStats(eq(mobileIface)); |
Lorenzo Colitti | 4ca0ba8 | 2017-07-12 15:48:07 +0900 | [diff] [blame] | 433 | |
Hugo Benichi | 752c128 | 2017-08-22 13:57:41 +0900 | [diff] [blame] | 434 | ethernetStats = new ForwardedStats(); |
Lorenzo Colitti | 4ca0ba8 | 2017-07-12 15:48:07 +0900 | [diff] [blame] | 435 | ethernetStats.rxBytes = 100000; |
| 436 | ethernetStats.txBytes = 100000; |
Hugo Benichi | 752c128 | 2017-08-22 13:57:41 +0900 | [diff] [blame] | 437 | when(mHardware.getForwardedStats(eq(ethernetIface))).thenReturn(ethernetStats); |
Lorenzo Colitti | 4ca0ba8 | 2017-07-12 15:48:07 +0900 | [diff] [blame] | 438 | offload.setUpstreamLinkProperties(null); |
Hugo Benichi | 752c128 | 2017-08-22 13:57:41 +0900 | [diff] [blame] | 439 | // Expect that we fetch stats from the previous upstream. |
| 440 | inOrder.verify(mHardware, times(1)).getForwardedStats(eq(ethernetIface)); |
Lorenzo Colitti | 4ca0ba8 | 2017-07-12 15:48:07 +0900 | [diff] [blame] | 441 | |
Lorenzo Colitti | f1912ca | 2017-08-17 19:23:08 +0900 | [diff] [blame] | 442 | ITetheringStatsProvider provider = mTetherStatsProviderCaptor.getValue(); |
| 443 | NetworkStats stats = provider.getTetherStats(STATS_PER_IFACE); |
| 444 | NetworkStats perUidStats = provider.getTetherStats(STATS_PER_UID); |
Hugo Benichi | 752c128 | 2017-08-22 13:57:41 +0900 | [diff] [blame] | 445 | waitForIdle(); |
| 446 | // There is no current upstream, so no stats are fetched. |
| 447 | inOrder.verify(mHardware, never()).getForwardedStats(eq(ethernetIface)); |
| 448 | inOrder.verifyNoMoreInteractions(); |
Lorenzo Colitti | f1912ca | 2017-08-17 19:23:08 +0900 | [diff] [blame] | 449 | |
Lorenzo Colitti | 4ca0ba8 | 2017-07-12 15:48:07 +0900 | [diff] [blame] | 450 | assertEquals(2, stats.size()); |
Lorenzo Colitti | f1912ca | 2017-08-17 19:23:08 +0900 | [diff] [blame] | 451 | assertEquals(2, perUidStats.size()); |
Lorenzo Colitti | 4ca0ba8 | 2017-07-12 15:48:07 +0900 | [diff] [blame] | 452 | |
| 453 | NetworkStats.Entry entry = null; |
Lorenzo Colitti | f1912ca | 2017-08-17 19:23:08 +0900 | [diff] [blame] | 454 | for (int i = 0; i < stats.size(); i++) { |
| 455 | assertEquals(UID_ALL, stats.getValues(i, entry).uid); |
| 456 | assertEquals(UID_TETHERING, perUidStats.getValues(i, entry).uid); |
| 457 | } |
| 458 | |
Lorenzo Colitti | 4ca0ba8 | 2017-07-12 15:48:07 +0900 | [diff] [blame] | 459 | int ethernetPosition = ethernetIface.equals(stats.getValues(0, entry).iface) ? 0 : 1; |
| 460 | int mobilePosition = 1 - ethernetPosition; |
| 461 | |
| 462 | entry = stats.getValues(mobilePosition, entry); |
| 463 | assertNetworkStats(mobileIface, mobileStats, entry); |
Lorenzo Colitti | f1912ca | 2017-08-17 19:23:08 +0900 | [diff] [blame] | 464 | entry = perUidStats.getValues(mobilePosition, entry); |
| 465 | assertNetworkStats(mobileIface, mobileStats, entry); |
Lorenzo Colitti | 4ca0ba8 | 2017-07-12 15:48:07 +0900 | [diff] [blame] | 466 | |
| 467 | ethernetStats.rxBytes = 12345 + 100000; |
| 468 | ethernetStats.txBytes = 54321 + 100000; |
| 469 | entry = stats.getValues(ethernetPosition, entry); |
| 470 | assertNetworkStats(ethernetIface, ethernetStats, entry); |
Lorenzo Colitti | f1912ca | 2017-08-17 19:23:08 +0900 | [diff] [blame] | 471 | entry = perUidStats.getValues(ethernetPosition, entry); |
| 472 | assertNetworkStats(ethernetIface, ethernetStats, entry); |
Lorenzo Colitti | 4ca0ba8 | 2017-07-12 15:48:07 +0900 | [diff] [blame] | 473 | } |
Lorenzo Colitti | 50b60fc | 2017-08-11 13:47:49 +0900 | [diff] [blame] | 474 | |
| 475 | @Test |
| 476 | public void testSetInterfaceQuota() throws Exception { |
| 477 | setupFunctioningHardwareInterface(); |
| 478 | enableOffload(); |
| 479 | |
| 480 | final OffloadController offload = makeOffloadController(); |
| 481 | offload.start(); |
| 482 | |
| 483 | final String ethernetIface = "eth1"; |
| 484 | final String mobileIface = "rmnet_data0"; |
| 485 | final long ethernetLimit = 12345; |
| 486 | final long mobileLimit = 12345678; |
| 487 | |
| 488 | final LinkProperties lp = new LinkProperties(); |
| 489 | lp.setInterfaceName(ethernetIface); |
| 490 | offload.setUpstreamLinkProperties(lp); |
| 491 | |
| 492 | ITetheringStatsProvider provider = mTetherStatsProviderCaptor.getValue(); |
| 493 | final InOrder inOrder = inOrder(mHardware); |
| 494 | when(mHardware.setUpstreamParameters(any(), any(), any(), any())).thenReturn(true); |
| 495 | when(mHardware.setDataLimit(anyString(), anyLong())).thenReturn(true); |
| 496 | |
| 497 | // Applying an interface quota to the current upstream immediately sends it to the hardware. |
| 498 | provider.setInterfaceQuota(ethernetIface, ethernetLimit); |
| 499 | waitForIdle(); |
| 500 | inOrder.verify(mHardware).setDataLimit(ethernetIface, ethernetLimit); |
| 501 | inOrder.verifyNoMoreInteractions(); |
| 502 | |
| 503 | // Applying an interface quota to another upstream does not take any immediate action. |
| 504 | provider.setInterfaceQuota(mobileIface, mobileLimit); |
| 505 | waitForIdle(); |
| 506 | inOrder.verify(mHardware, never()).setDataLimit(anyString(), anyLong()); |
| 507 | |
| 508 | // Switching to that upstream causes the quota to be applied if the parameters were applied |
| 509 | // correctly. |
| 510 | lp.setInterfaceName(mobileIface); |
| 511 | offload.setUpstreamLinkProperties(lp); |
| 512 | waitForIdle(); |
| 513 | inOrder.verify(mHardware).setDataLimit(mobileIface, mobileLimit); |
| 514 | |
| 515 | // Setting a limit of ITetheringStatsProvider.QUOTA_UNLIMITED causes the limit to be set |
| 516 | // to Long.MAX_VALUE. |
| 517 | provider.setInterfaceQuota(mobileIface, ITetheringStatsProvider.QUOTA_UNLIMITED); |
| 518 | waitForIdle(); |
| 519 | inOrder.verify(mHardware).setDataLimit(mobileIface, Long.MAX_VALUE); |
| 520 | |
| 521 | // If setting upstream parameters fails, then the data limit is not set. |
| 522 | when(mHardware.setUpstreamParameters(any(), any(), any(), any())).thenReturn(false); |
| 523 | lp.setInterfaceName(ethernetIface); |
| 524 | offload.setUpstreamLinkProperties(lp); |
| 525 | provider.setInterfaceQuota(mobileIface, mobileLimit); |
| 526 | waitForIdle(); |
| 527 | inOrder.verify(mHardware, never()).setDataLimit(anyString(), anyLong()); |
| 528 | |
| 529 | // If setting the data limit fails while changing upstreams, offload is stopped. |
| 530 | when(mHardware.setUpstreamParameters(any(), any(), any(), any())).thenReturn(true); |
| 531 | when(mHardware.setDataLimit(anyString(), anyLong())).thenReturn(false); |
| 532 | lp.setInterfaceName(mobileIface); |
| 533 | offload.setUpstreamLinkProperties(lp); |
| 534 | provider.setInterfaceQuota(mobileIface, mobileLimit); |
| 535 | waitForIdle(); |
Lorenzo Colitti | ddce7ee | 2017-08-21 12:34:50 +0900 | [diff] [blame^] | 536 | inOrder.verify(mHardware).getForwardedStats(ethernetIface); |
Lorenzo Colitti | 50b60fc | 2017-08-11 13:47:49 +0900 | [diff] [blame] | 537 | inOrder.verify(mHardware).stopOffloadControl(); |
| 538 | } |
Lorenzo Colitti | 9f0baa9 | 2017-08-15 19:25:51 +0900 | [diff] [blame] | 539 | |
| 540 | @Test |
| 541 | public void testDataLimitCallback() throws Exception { |
| 542 | setupFunctioningHardwareInterface(); |
| 543 | enableOffload(); |
| 544 | |
| 545 | final OffloadController offload = makeOffloadController(); |
| 546 | offload.start(); |
| 547 | |
| 548 | OffloadHardwareInterface.ControlCallback callback = mControlCallbackCaptor.getValue(); |
| 549 | callback.onStoppedLimitReached(); |
| 550 | verify(mNMService, times(1)).tetherLimitReached(mTetherStatsProviderCaptor.getValue()); |
| 551 | } |
Erik Kline | 92c4db0 | 2017-05-31 10:21:32 +0900 | [diff] [blame] | 552 | } |