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