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 | |
| 19 | import static android.provider.Settings.Global.TETHER_OFFLOAD_DISABLED; |
Erik Kline | 7990aef | 2017-06-01 20:11:25 +0900 | [diff] [blame] | 20 | import static org.junit.Assert.assertEquals; |
| 21 | import static org.junit.Assert.assertTrue; |
Erik Kline | 92c4db0 | 2017-05-31 10:21:32 +0900 | [diff] [blame] | 22 | import static org.junit.Assert.fail; |
| 23 | import static org.mockito.Matchers.any; |
| 24 | import static org.mockito.Matchers.anyObject; |
Erik Kline | 7990aef | 2017-06-01 20:11:25 +0900 | [diff] [blame] | 25 | import static org.mockito.Matchers.eq; |
Erik Kline | 92c4db0 | 2017-05-31 10:21:32 +0900 | [diff] [blame] | 26 | import static org.mockito.Mockito.inOrder; |
| 27 | import static org.mockito.Mockito.never; |
Erik Kline | 7990aef | 2017-06-01 20:11:25 +0900 | [diff] [blame] | 28 | import static org.mockito.Mockito.reset; |
Erik Kline | 92c4db0 | 2017-05-31 10:21:32 +0900 | [diff] [blame] | 29 | import static org.mockito.Mockito.times; |
| 30 | import static org.mockito.Mockito.when; |
| 31 | |
| 32 | import android.content.Context; |
Erik Kline | f3a08b4 | 2017-06-07 16:33:19 +0900 | [diff] [blame] | 33 | import android.content.pm.ApplicationInfo; |
Erik Kline | 7990aef | 2017-06-01 20:11:25 +0900 | [diff] [blame] | 34 | import android.net.LinkAddress; |
| 35 | import android.net.LinkProperties; |
| 36 | import android.net.RouteInfo; |
Erik Kline | 92c4db0 | 2017-05-31 10:21:32 +0900 | [diff] [blame] | 37 | import android.net.util.SharedLog; |
| 38 | import android.provider.Settings; |
| 39 | import android.provider.Settings.SettingNotFoundException; |
| 40 | |
| 41 | import android.support.test.filters.SmallTest; |
| 42 | import android.support.test.runner.AndroidJUnit4; |
| 43 | import android.test.mock.MockContentResolver; |
| 44 | import com.android.internal.util.test.FakeSettingsProvider; |
| 45 | |
Erik Kline | 7990aef | 2017-06-01 20:11:25 +0900 | [diff] [blame] | 46 | import java.net.InetAddress; |
| 47 | import java.util.ArrayList; |
| 48 | |
Erik Kline | c87cd41 | 2017-07-07 17:38:30 +0900 | [diff] [blame^] | 49 | import org.junit.After; |
Erik Kline | 92c4db0 | 2017-05-31 10:21:32 +0900 | [diff] [blame] | 50 | import org.junit.Before; |
| 51 | import org.junit.runner.RunWith; |
| 52 | import org.junit.Test; |
Erik Kline | 7990aef | 2017-06-01 20:11:25 +0900 | [diff] [blame] | 53 | import org.mockito.ArgumentCaptor; |
Erik Kline | 92c4db0 | 2017-05-31 10:21:32 +0900 | [diff] [blame] | 54 | import org.mockito.InOrder; |
| 55 | import org.mockito.Mock; |
| 56 | import org.mockito.MockitoAnnotations; |
| 57 | |
| 58 | |
| 59 | @RunWith(AndroidJUnit4.class) |
| 60 | @SmallTest |
| 61 | public class OffloadControllerTest { |
| 62 | |
| 63 | @Mock private OffloadHardwareInterface mHardware; |
Erik Kline | f3a08b4 | 2017-06-07 16:33:19 +0900 | [diff] [blame] | 64 | @Mock private ApplicationInfo mApplicationInfo; |
Erik Kline | 92c4db0 | 2017-05-31 10:21:32 +0900 | [diff] [blame] | 65 | @Mock private Context mContext; |
Erik Kline | 7fd696c | 2017-06-12 18:20:08 +0900 | [diff] [blame] | 66 | private final ArgumentCaptor<ArrayList> mStringArrayCaptor = |
| 67 | ArgumentCaptor.forClass(ArrayList.class); |
Erik Kline | 92c4db0 | 2017-05-31 10:21:32 +0900 | [diff] [blame] | 68 | private MockContentResolver mContentResolver; |
| 69 | |
| 70 | @Before public void setUp() throws Exception { |
| 71 | MockitoAnnotations.initMocks(this); |
Erik Kline | f3a08b4 | 2017-06-07 16:33:19 +0900 | [diff] [blame] | 72 | when(mContext.getApplicationInfo()).thenReturn(mApplicationInfo); |
| 73 | when(mContext.getPackageName()).thenReturn("OffloadControllerTest"); |
Erik Kline | 92c4db0 | 2017-05-31 10:21:32 +0900 | [diff] [blame] | 74 | mContentResolver = new MockContentResolver(mContext); |
| 75 | mContentResolver.addProvider(Settings.AUTHORITY, new FakeSettingsProvider()); |
Erik Kline | 92c4db0 | 2017-05-31 10:21:32 +0900 | [diff] [blame] | 76 | when(mContext.getContentResolver()).thenReturn(mContentResolver); |
Erik Kline | c87cd41 | 2017-07-07 17:38:30 +0900 | [diff] [blame^] | 77 | FakeSettingsProvider.clearSettingsProvider(); |
| 78 | } |
| 79 | |
| 80 | @After public void tearDown() throws Exception { |
| 81 | FakeSettingsProvider.clearSettingsProvider(); |
Erik Kline | 92c4db0 | 2017-05-31 10:21:32 +0900 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | private void setupFunctioningHardwareInterface() { |
| 85 | when(mHardware.initOffloadConfig()).thenReturn(true); |
| 86 | when(mHardware.initOffloadControl(any(OffloadHardwareInterface.ControlCallback.class))) |
| 87 | .thenReturn(true); |
| 88 | } |
| 89 | |
Erik Kline | c87cd41 | 2017-07-07 17:38:30 +0900 | [diff] [blame^] | 90 | private void enableOffload() { |
| 91 | Settings.Global.putInt(mContentResolver, TETHER_OFFLOAD_DISABLED, 0); |
| 92 | } |
| 93 | |
Erik Kline | 92c4db0 | 2017-05-31 10:21:32 +0900 | [diff] [blame] | 94 | @Test |
Erik Kline | c87cd41 | 2017-07-07 17:38:30 +0900 | [diff] [blame^] | 95 | public void testNoSettingsValueDefaultDisabledDoesNotStart() { |
Erik Kline | 92c4db0 | 2017-05-31 10:21:32 +0900 | [diff] [blame] | 96 | setupFunctioningHardwareInterface(); |
Erik Kline | c87cd41 | 2017-07-07 17:38:30 +0900 | [diff] [blame^] | 97 | when(mHardware.getDefaultTetherOffloadDisabled()).thenReturn(1); |
Erik Kline | 92c4db0 | 2017-05-31 10:21:32 +0900 | [diff] [blame] | 98 | try { |
| 99 | Settings.Global.getInt(mContentResolver, TETHER_OFFLOAD_DISABLED); |
| 100 | fail(); |
| 101 | } catch (SettingNotFoundException expected) {} |
| 102 | |
| 103 | final OffloadController offload = |
| 104 | new OffloadController(null, mHardware, mContentResolver, new SharedLog("test")); |
| 105 | offload.start(); |
| 106 | |
| 107 | final InOrder inOrder = inOrder(mHardware); |
Erik Kline | c87cd41 | 2017-07-07 17:38:30 +0900 | [diff] [blame^] | 108 | inOrder.verify(mHardware, times(1)).getDefaultTetherOffloadDisabled(); |
| 109 | inOrder.verify(mHardware, never()).initOffloadConfig(); |
| 110 | inOrder.verify(mHardware, never()).initOffloadControl( |
| 111 | any(OffloadHardwareInterface.ControlCallback.class)); |
| 112 | inOrder.verifyNoMoreInteractions(); |
| 113 | } |
| 114 | |
| 115 | @Test |
| 116 | public void testNoSettingsValueDefaultEnabledDoesStart() { |
| 117 | setupFunctioningHardwareInterface(); |
| 118 | when(mHardware.getDefaultTetherOffloadDisabled()).thenReturn(0); |
| 119 | try { |
| 120 | Settings.Global.getInt(mContentResolver, TETHER_OFFLOAD_DISABLED); |
| 121 | fail(); |
| 122 | } catch (SettingNotFoundException expected) {} |
| 123 | |
| 124 | final OffloadController offload = |
| 125 | new OffloadController(null, mHardware, mContentResolver, new SharedLog("test")); |
| 126 | offload.start(); |
| 127 | |
| 128 | final InOrder inOrder = inOrder(mHardware); |
| 129 | inOrder.verify(mHardware, times(1)).getDefaultTetherOffloadDisabled(); |
Erik Kline | 92c4db0 | 2017-05-31 10:21:32 +0900 | [diff] [blame] | 130 | inOrder.verify(mHardware, times(1)).initOffloadConfig(); |
| 131 | inOrder.verify(mHardware, times(1)).initOffloadControl( |
| 132 | any(OffloadHardwareInterface.ControlCallback.class)); |
| 133 | inOrder.verifyNoMoreInteractions(); |
| 134 | } |
| 135 | |
| 136 | @Test |
| 137 | public void testSettingsAllowsStart() { |
| 138 | setupFunctioningHardwareInterface(); |
| 139 | Settings.Global.putInt(mContentResolver, TETHER_OFFLOAD_DISABLED, 0); |
| 140 | |
| 141 | final OffloadController offload = |
| 142 | new OffloadController(null, mHardware, mContentResolver, new SharedLog("test")); |
| 143 | offload.start(); |
| 144 | |
| 145 | final InOrder inOrder = inOrder(mHardware); |
Erik Kline | c87cd41 | 2017-07-07 17:38:30 +0900 | [diff] [blame^] | 146 | inOrder.verify(mHardware, times(1)).getDefaultTetherOffloadDisabled(); |
Erik Kline | 92c4db0 | 2017-05-31 10:21:32 +0900 | [diff] [blame] | 147 | inOrder.verify(mHardware, times(1)).initOffloadConfig(); |
| 148 | inOrder.verify(mHardware, times(1)).initOffloadControl( |
| 149 | any(OffloadHardwareInterface.ControlCallback.class)); |
| 150 | inOrder.verifyNoMoreInteractions(); |
| 151 | } |
| 152 | |
| 153 | @Test |
| 154 | public void testSettingsDisablesStart() { |
| 155 | setupFunctioningHardwareInterface(); |
| 156 | Settings.Global.putInt(mContentResolver, TETHER_OFFLOAD_DISABLED, 1); |
| 157 | |
| 158 | final OffloadController offload = |
| 159 | new OffloadController(null, mHardware, mContentResolver, new SharedLog("test")); |
| 160 | offload.start(); |
| 161 | |
| 162 | final InOrder inOrder = inOrder(mHardware); |
Erik Kline | c87cd41 | 2017-07-07 17:38:30 +0900 | [diff] [blame^] | 163 | inOrder.verify(mHardware, times(1)).getDefaultTetherOffloadDisabled(); |
Erik Kline | 92c4db0 | 2017-05-31 10:21:32 +0900 | [diff] [blame] | 164 | inOrder.verify(mHardware, never()).initOffloadConfig(); |
| 165 | inOrder.verify(mHardware, never()).initOffloadControl(anyObject()); |
| 166 | inOrder.verifyNoMoreInteractions(); |
| 167 | } |
Erik Kline | 7990aef | 2017-06-01 20:11:25 +0900 | [diff] [blame] | 168 | |
| 169 | @Test |
| 170 | public void testSetUpstreamLinkPropertiesWorking() throws Exception { |
| 171 | setupFunctioningHardwareInterface(); |
Erik Kline | c87cd41 | 2017-07-07 17:38:30 +0900 | [diff] [blame^] | 172 | enableOffload(); |
| 173 | |
Erik Kline | 7990aef | 2017-06-01 20:11:25 +0900 | [diff] [blame] | 174 | final OffloadController offload = |
| 175 | new OffloadController(null, mHardware, mContentResolver, new SharedLog("test")); |
| 176 | offload.start(); |
| 177 | |
| 178 | final InOrder inOrder = inOrder(mHardware); |
Erik Kline | c87cd41 | 2017-07-07 17:38:30 +0900 | [diff] [blame^] | 179 | inOrder.verify(mHardware, times(1)).getDefaultTetherOffloadDisabled(); |
Erik Kline | 7990aef | 2017-06-01 20:11:25 +0900 | [diff] [blame] | 180 | inOrder.verify(mHardware, times(1)).initOffloadConfig(); |
| 181 | inOrder.verify(mHardware, times(1)).initOffloadControl( |
| 182 | any(OffloadHardwareInterface.ControlCallback.class)); |
| 183 | inOrder.verifyNoMoreInteractions(); |
| 184 | |
| 185 | offload.setUpstreamLinkProperties(null); |
| 186 | inOrder.verify(mHardware, times(1)).setUpstreamParameters( |
| 187 | eq(null), eq(null), eq(null), eq(null)); |
| 188 | inOrder.verifyNoMoreInteractions(); |
| 189 | reset(mHardware); |
| 190 | |
| 191 | final LinkProperties lp = new LinkProperties(); |
| 192 | |
| 193 | final String testIfName = "rmnet_data17"; |
| 194 | lp.setInterfaceName(testIfName); |
| 195 | offload.setUpstreamLinkProperties(lp); |
| 196 | inOrder.verify(mHardware, times(1)).setUpstreamParameters( |
Erik Kline | 1185459 | 2017-06-15 18:06:34 +0900 | [diff] [blame] | 197 | eq(testIfName), eq(null), eq(null), eq(null)); |
Erik Kline | 7990aef | 2017-06-01 20:11:25 +0900 | [diff] [blame] | 198 | inOrder.verifyNoMoreInteractions(); |
| 199 | |
| 200 | final String ipv4Addr = "192.0.2.5"; |
| 201 | final String linkAddr = ipv4Addr + "/24"; |
| 202 | lp.addLinkAddress(new LinkAddress(linkAddr)); |
| 203 | offload.setUpstreamLinkProperties(lp); |
| 204 | inOrder.verify(mHardware, times(1)).setUpstreamParameters( |
Erik Kline | 1185459 | 2017-06-15 18:06:34 +0900 | [diff] [blame] | 205 | eq(testIfName), eq(ipv4Addr), eq(null), eq(null)); |
Erik Kline | 7990aef | 2017-06-01 20:11:25 +0900 | [diff] [blame] | 206 | inOrder.verifyNoMoreInteractions(); |
| 207 | |
| 208 | final String ipv4Gateway = "192.0.2.1"; |
| 209 | lp.addRoute(new RouteInfo(InetAddress.getByName(ipv4Gateway))); |
| 210 | offload.setUpstreamLinkProperties(lp); |
| 211 | inOrder.verify(mHardware, times(1)).setUpstreamParameters( |
Erik Kline | 1185459 | 2017-06-15 18:06:34 +0900 | [diff] [blame] | 212 | eq(testIfName), eq(ipv4Addr), eq(ipv4Gateway), eq(null)); |
Erik Kline | 7990aef | 2017-06-01 20:11:25 +0900 | [diff] [blame] | 213 | inOrder.verifyNoMoreInteractions(); |
| 214 | |
| 215 | final String ipv6Gw1 = "fe80::cafe"; |
| 216 | lp.addRoute(new RouteInfo(InetAddress.getByName(ipv6Gw1))); |
| 217 | offload.setUpstreamLinkProperties(lp); |
| 218 | inOrder.verify(mHardware, times(1)).setUpstreamParameters( |
| 219 | eq(testIfName), eq(ipv4Addr), eq(ipv4Gateway), mStringArrayCaptor.capture()); |
| 220 | ArrayList<String> v6gws = mStringArrayCaptor.getValue(); |
| 221 | assertEquals(1, v6gws.size()); |
| 222 | assertTrue(v6gws.contains(ipv6Gw1)); |
| 223 | inOrder.verifyNoMoreInteractions(); |
| 224 | |
| 225 | final String ipv6Gw2 = "fe80::d00d"; |
| 226 | lp.addRoute(new RouteInfo(InetAddress.getByName(ipv6Gw2))); |
| 227 | offload.setUpstreamLinkProperties(lp); |
| 228 | inOrder.verify(mHardware, times(1)).setUpstreamParameters( |
| 229 | eq(testIfName), eq(ipv4Addr), eq(ipv4Gateway), mStringArrayCaptor.capture()); |
| 230 | v6gws = mStringArrayCaptor.getValue(); |
| 231 | assertEquals(2, v6gws.size()); |
| 232 | assertTrue(v6gws.contains(ipv6Gw1)); |
| 233 | assertTrue(v6gws.contains(ipv6Gw2)); |
| 234 | inOrder.verifyNoMoreInteractions(); |
| 235 | |
| 236 | final LinkProperties stacked = new LinkProperties(); |
| 237 | stacked.setInterfaceName("stacked"); |
| 238 | stacked.addLinkAddress(new LinkAddress("192.0.2.129/25")); |
| 239 | stacked.addRoute(new RouteInfo(InetAddress.getByName("192.0.2.254"))); |
| 240 | stacked.addRoute(new RouteInfo(InetAddress.getByName("fe80::bad:f00"))); |
| 241 | assertTrue(lp.addStackedLink(stacked)); |
| 242 | offload.setUpstreamLinkProperties(lp); |
| 243 | inOrder.verify(mHardware, times(1)).setUpstreamParameters( |
| 244 | eq(testIfName), eq(ipv4Addr), eq(ipv4Gateway), mStringArrayCaptor.capture()); |
| 245 | v6gws = mStringArrayCaptor.getValue(); |
| 246 | assertEquals(2, v6gws.size()); |
| 247 | assertTrue(v6gws.contains(ipv6Gw1)); |
| 248 | assertTrue(v6gws.contains(ipv6Gw2)); |
| 249 | inOrder.verifyNoMoreInteractions(); |
| 250 | } |
Erik Kline | 92c4db0 | 2017-05-31 10:21:32 +0900 | [diff] [blame] | 251 | } |