Christopher Wiley | 497c147 | 2016-10-11 13:26:03 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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; |
| 18 | |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 19 | import static org.junit.Assert.assertEquals; |
Christopher Wiley | 497c147 | 2016-10-11 13:26:03 -0700 | [diff] [blame] | 20 | import static org.junit.Assert.assertTrue; |
| 21 | import static org.mockito.Matchers.anyBoolean; |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 22 | import static org.mockito.Matchers.anyInt; |
| 23 | import static org.mockito.Matchers.anyString; |
Christopher Wiley | 497c147 | 2016-10-11 13:26:03 -0700 | [diff] [blame] | 24 | import static org.mockito.Matchers.eq; |
Erik Kline | 1fdc2e2 | 2017-05-08 17:56:35 +0900 | [diff] [blame^] | 25 | import static org.mockito.Mockito.any; |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 26 | import static org.mockito.Mockito.atLeastOnce; |
Erik Kline | 1fdc2e2 | 2017-05-08 17:56:35 +0900 | [diff] [blame^] | 27 | import static org.mockito.Mockito.doThrow; |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 28 | import static org.mockito.Mockito.times; |
| 29 | import static org.mockito.Mockito.verify; |
| 30 | import static org.mockito.Mockito.verifyNoMoreInteractions; |
Christopher Wiley | 497c147 | 2016-10-11 13:26:03 -0700 | [diff] [blame] | 31 | import static org.mockito.Mockito.when; |
| 32 | |
Erik Kline | 8351faa | 2017-04-17 16:47:23 +0900 | [diff] [blame] | 33 | import android.content.BroadcastReceiver; |
Christopher Wiley | 497c147 | 2016-10-11 13:26:03 -0700 | [diff] [blame] | 34 | import android.content.Context; |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 35 | import android.content.ContextWrapper; |
| 36 | import android.content.Intent; |
Erik Kline | 8351faa | 2017-04-17 16:47:23 +0900 | [diff] [blame] | 37 | import android.content.IntentFilter; |
Christopher Wiley | 497c147 | 2016-10-11 13:26:03 -0700 | [diff] [blame] | 38 | import android.content.res.Resources; |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 39 | import android.hardware.usb.UsbManager; |
| 40 | import android.net.ConnectivityManager; |
| 41 | import android.net.ConnectivityManager.NetworkCallback; |
Christopher Wiley | 497c147 | 2016-10-11 13:26:03 -0700 | [diff] [blame] | 42 | import android.net.INetworkPolicyManager; |
| 43 | import android.net.INetworkStatsService; |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 44 | import android.net.InterfaceConfiguration; |
| 45 | import android.net.NetworkRequest; |
| 46 | import android.net.wifi.WifiConfiguration; |
| 47 | import android.net.wifi.WifiManager; |
| 48 | import android.os.Handler; |
Christopher Wiley | 497c147 | 2016-10-11 13:26:03 -0700 | [diff] [blame] | 49 | import android.os.INetworkManagementService; |
| 50 | import android.os.PersistableBundle; |
Erik Kline | 1fdc2e2 | 2017-05-08 17:56:35 +0900 | [diff] [blame^] | 51 | import android.os.RemoteException; |
Christopher Wiley | 497c147 | 2016-10-11 13:26:03 -0700 | [diff] [blame] | 52 | import android.os.test.TestLooper; |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 53 | import android.os.UserHandle; |
Christopher Wiley | 497c147 | 2016-10-11 13:26:03 -0700 | [diff] [blame] | 54 | import android.support.test.filters.SmallTest; |
| 55 | import android.support.test.runner.AndroidJUnit4; |
| 56 | import android.telephony.CarrierConfigManager; |
| 57 | |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 58 | import com.android.internal.util.test.BroadcastInterceptingContext; |
| 59 | |
Erik Kline | 8351faa | 2017-04-17 16:47:23 +0900 | [diff] [blame] | 60 | import org.junit.After; |
Christopher Wiley | 497c147 | 2016-10-11 13:26:03 -0700 | [diff] [blame] | 61 | import org.junit.Before; |
| 62 | import org.junit.Test; |
| 63 | import org.junit.runner.RunWith; |
| 64 | import org.mockito.Mock; |
| 65 | import org.mockito.MockitoAnnotations; |
| 66 | |
Erik Kline | 8351faa | 2017-04-17 16:47:23 +0900 | [diff] [blame] | 67 | import java.util.ArrayList; |
| 68 | import java.util.Vector; |
| 69 | |
Christopher Wiley | 497c147 | 2016-10-11 13:26:03 -0700 | [diff] [blame] | 70 | @RunWith(AndroidJUnit4.class) |
| 71 | @SmallTest |
| 72 | public class TetheringTest { |
| 73 | private static final String[] PROVISIONING_APP_NAME = {"some", "app"}; |
| 74 | |
| 75 | @Mock private Context mContext; |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 76 | @Mock private ConnectivityManager mConnectivityManager; |
Christopher Wiley | 497c147 | 2016-10-11 13:26:03 -0700 | [diff] [blame] | 77 | @Mock private INetworkManagementService mNMService; |
| 78 | @Mock private INetworkStatsService mStatsService; |
| 79 | @Mock private INetworkPolicyManager mPolicyManager; |
| 80 | @Mock private MockableSystemProperties mSystemProperties; |
| 81 | @Mock private Resources mResources; |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 82 | @Mock private UsbManager mUsbManager; |
| 83 | @Mock private WifiManager mWifiManager; |
Christopher Wiley | 497c147 | 2016-10-11 13:26:03 -0700 | [diff] [blame] | 84 | @Mock private CarrierConfigManager mCarrierConfigManager; |
| 85 | |
| 86 | // Like so many Android system APIs, these cannot be mocked because it is marked final. |
| 87 | // We have to use the real versions. |
| 88 | private final PersistableBundle mCarrierConfig = new PersistableBundle(); |
| 89 | private final TestLooper mLooper = new TestLooper(); |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 90 | private final String mTestIfname = "test_wlan0"; |
Christopher Wiley | 497c147 | 2016-10-11 13:26:03 -0700 | [diff] [blame] | 91 | |
Erik Kline | 8351faa | 2017-04-17 16:47:23 +0900 | [diff] [blame] | 92 | private Vector<Intent> mIntents; |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 93 | private BroadcastInterceptingContext mServiceContext; |
Erik Kline | 8351faa | 2017-04-17 16:47:23 +0900 | [diff] [blame] | 94 | private BroadcastReceiver mBroadcastReceiver; |
Christopher Wiley | 497c147 | 2016-10-11 13:26:03 -0700 | [diff] [blame] | 95 | private Tethering mTethering; |
| 96 | |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 97 | private class MockContext extends BroadcastInterceptingContext { |
| 98 | MockContext(Context base) { |
| 99 | super(base); |
| 100 | } |
| 101 | |
| 102 | @Override |
| 103 | public Resources getResources() { return mResources; } |
| 104 | |
| 105 | @Override |
| 106 | public Object getSystemService(String name) { |
| 107 | if (Context.CONNECTIVITY_SERVICE.equals(name)) return mConnectivityManager; |
| 108 | if (Context.WIFI_SERVICE.equals(name)) return mWifiManager; |
| 109 | return super.getSystemService(name); |
| 110 | } |
| 111 | } |
| 112 | |
Erik Kline | 8351faa | 2017-04-17 16:47:23 +0900 | [diff] [blame] | 113 | @Before |
| 114 | public void setUp() throws Exception { |
Christopher Wiley | 497c147 | 2016-10-11 13:26:03 -0700 | [diff] [blame] | 115 | MockitoAnnotations.initMocks(this); |
Christopher Wiley | 497c147 | 2016-10-11 13:26:03 -0700 | [diff] [blame] | 116 | when(mResources.getStringArray(com.android.internal.R.array.config_tether_dhcp_range)) |
| 117 | .thenReturn(new String[0]); |
| 118 | when(mResources.getStringArray(com.android.internal.R.array.config_tether_usb_regexs)) |
| 119 | .thenReturn(new String[0]); |
| 120 | when(mResources.getStringArray(com.android.internal.R.array.config_tether_wifi_regexs)) |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 121 | .thenReturn(new String[]{ "test_wlan\\d" }); |
Christopher Wiley | 497c147 | 2016-10-11 13:26:03 -0700 | [diff] [blame] | 122 | when(mResources.getStringArray(com.android.internal.R.array.config_tether_bluetooth_regexs)) |
| 123 | .thenReturn(new String[0]); |
| 124 | when(mResources.getIntArray(com.android.internal.R.array.config_tether_upstream_types)) |
| 125 | .thenReturn(new int[0]); |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 126 | when(mNMService.listInterfaces()) |
| 127 | .thenReturn(new String[]{ "test_rmnet_data0", mTestIfname }); |
| 128 | when(mNMService.getInterfaceConfig(anyString())) |
| 129 | .thenReturn(new InterfaceConfiguration()); |
| 130 | |
| 131 | mServiceContext = new MockContext(mContext); |
Erik Kline | 8351faa | 2017-04-17 16:47:23 +0900 | [diff] [blame] | 132 | mIntents = new Vector<>(); |
| 133 | mBroadcastReceiver = new BroadcastReceiver() { |
| 134 | @Override |
| 135 | public void onReceive(Context context, Intent intent) { |
| 136 | mIntents.addElement(intent); |
| 137 | } |
| 138 | }; |
| 139 | mServiceContext.registerReceiver(mBroadcastReceiver, |
| 140 | new IntentFilter(ConnectivityManager.ACTION_TETHER_STATE_CHANGED)); |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 141 | mTethering = new Tethering(mServiceContext, mNMService, mStatsService, mPolicyManager, |
Christopher Wiley | 497c147 | 2016-10-11 13:26:03 -0700 | [diff] [blame] | 142 | mLooper.getLooper(), mSystemProperties); |
| 143 | } |
| 144 | |
Erik Kline | 8351faa | 2017-04-17 16:47:23 +0900 | [diff] [blame] | 145 | @After |
| 146 | public void tearDown() { |
| 147 | mServiceContext.unregisterReceiver(mBroadcastReceiver); |
| 148 | } |
| 149 | |
Christopher Wiley | 497c147 | 2016-10-11 13:26:03 -0700 | [diff] [blame] | 150 | private void setupForRequiredProvisioning() { |
| 151 | // Produce some acceptable looking provision app setting if requested. |
| 152 | when(mResources.getStringArray( |
| 153 | com.android.internal.R.array.config_mobile_hotspot_provision_app)) |
| 154 | .thenReturn(PROVISIONING_APP_NAME); |
| 155 | // Don't disable tethering provisioning unless requested. |
| 156 | when(mSystemProperties.getBoolean(eq(Tethering.DISABLE_PROVISIONING_SYSPROP_KEY), |
| 157 | anyBoolean())).thenReturn(false); |
| 158 | // Act like the CarrierConfigManager is present and ready unless told otherwise. |
| 159 | when(mContext.getSystemService(Context.CARRIER_CONFIG_SERVICE)) |
| 160 | .thenReturn(mCarrierConfigManager); |
| 161 | when(mCarrierConfigManager.getConfig()).thenReturn(mCarrierConfig); |
| 162 | mCarrierConfig.putBoolean(CarrierConfigManager.KEY_REQUIRE_ENTITLEMENT_CHECKS_BOOL, true); |
| 163 | } |
| 164 | |
| 165 | @Test |
| 166 | public void canRequireProvisioning() { |
| 167 | setupForRequiredProvisioning(); |
| 168 | assertTrue(mTethering.isTetherProvisioningRequired()); |
| 169 | } |
| 170 | |
| 171 | @Test |
| 172 | public void toleratesCarrierConfigManagerMissing() { |
| 173 | setupForRequiredProvisioning(); |
| 174 | when(mContext.getSystemService(Context.CARRIER_CONFIG_SERVICE)) |
| 175 | .thenReturn(null); |
| 176 | // Couldn't get the CarrierConfigManager, but still had a declared provisioning app. |
| 177 | // We therefore still require provisioning. |
| 178 | assertTrue(mTethering.isTetherProvisioningRequired()); |
| 179 | } |
| 180 | |
| 181 | @Test |
| 182 | public void toleratesCarrierConfigMissing() { |
| 183 | setupForRequiredProvisioning(); |
| 184 | when(mCarrierConfigManager.getConfig()).thenReturn(null); |
| 185 | // We still have a provisioning app configured, so still require provisioning. |
| 186 | assertTrue(mTethering.isTetherProvisioningRequired()); |
| 187 | } |
| 188 | |
| 189 | @Test |
| 190 | public void provisioningNotRequiredWhenAppNotFound() { |
| 191 | setupForRequiredProvisioning(); |
| 192 | when(mResources.getStringArray( |
| 193 | com.android.internal.R.array.config_mobile_hotspot_provision_app)) |
| 194 | .thenReturn(null); |
| 195 | assertTrue(!mTethering.isTetherProvisioningRequired()); |
| 196 | when(mResources.getStringArray( |
| 197 | com.android.internal.R.array.config_mobile_hotspot_provision_app)) |
| 198 | .thenReturn(new String[] {"malformedApp"}); |
| 199 | assertTrue(!mTethering.isTetherProvisioningRequired()); |
| 200 | } |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 201 | |
| 202 | private void sendWifiApStateChanged(int state) { |
| 203 | final Intent intent = new Intent(WifiManager.WIFI_AP_STATE_CHANGED_ACTION); |
| 204 | intent.putExtra(WifiManager.EXTRA_WIFI_AP_STATE, state); |
| 205 | mServiceContext.sendStickyBroadcastAsUser(intent, UserHandle.ALL); |
| 206 | } |
| 207 | |
Erik Kline | 8351faa | 2017-04-17 16:47:23 +0900 | [diff] [blame] | 208 | private void verifyInterfaceServingModeStarted() throws Exception { |
| 209 | verify(mNMService, times(1)).listInterfaces(); |
| 210 | verify(mNMService, times(1)).getInterfaceConfig(mTestIfname); |
| 211 | verify(mNMService, times(1)) |
| 212 | .setInterfaceConfig(eq(mTestIfname), any(InterfaceConfiguration.class)); |
| 213 | verify(mNMService, times(1)).tetherInterface(mTestIfname); |
| 214 | } |
| 215 | |
| 216 | private void verifyTetheringBroadcast(String ifname, String whichExtra) { |
| 217 | // Verify that ifname is in the whichExtra array of the tether state changed broadcast. |
| 218 | final Intent bcast = mIntents.get(0); |
| 219 | assertEquals(ConnectivityManager.ACTION_TETHER_STATE_CHANGED, bcast.getAction()); |
| 220 | final ArrayList<String> ifnames = bcast.getStringArrayListExtra(whichExtra); |
| 221 | assertTrue(ifnames.contains(ifname)); |
| 222 | mIntents.remove(bcast); |
| 223 | } |
| 224 | |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 225 | @Test |
| 226 | public void workingLocalOnlyHotspot() throws Exception { |
| 227 | when(mConnectivityManager.isTetheringSupported()).thenReturn(true); |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 228 | |
| 229 | // Emulate externally-visible WifiManager effects, causing the |
| 230 | // per-interface state machine to start up, and telling us that |
| 231 | // hotspot mode is to be started. |
| 232 | mTethering.interfaceStatusChanged(mTestIfname, true); |
| 233 | sendWifiApStateChanged(WifiManager.WIFI_AP_STATE_ENABLED); |
| 234 | mLooper.dispatchAll(); |
| 235 | |
Erik Kline | 8351faa | 2017-04-17 16:47:23 +0900 | [diff] [blame] | 236 | verifyInterfaceServingModeStarted(); |
| 237 | verifyTetheringBroadcast(mTestIfname, ConnectivityManager.EXTRA_AVAILABLE_TETHER); |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 238 | verify(mNMService, times(1)).setIpForwardingEnabled(true); |
| 239 | verify(mNMService, times(1)).startTethering(any(String[].class)); |
| 240 | verifyNoMoreInteractions(mNMService); |
Erik Kline | 216af6d | 2017-04-27 20:57:23 +0900 | [diff] [blame] | 241 | verify(mWifiManager).updateInterfaceIpState( |
| 242 | mTestIfname, WifiManager.IFACE_IP_MODE_LOCAL_ONLY); |
| 243 | verifyNoMoreInteractions(mWifiManager); |
Erik Kline | 8351faa | 2017-04-17 16:47:23 +0900 | [diff] [blame] | 244 | verifyTetheringBroadcast(mTestIfname, ConnectivityManager.EXTRA_ACTIVE_LOCAL_ONLY); |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 245 | // UpstreamNetworkMonitor will be started, and will register two callbacks: |
| 246 | // a "listen all" and a "track default". |
| 247 | verify(mConnectivityManager, times(1)).registerNetworkCallback( |
| 248 | any(NetworkRequest.class), any(NetworkCallback.class), any(Handler.class)); |
| 249 | verify(mConnectivityManager, times(1)).registerDefaultNetworkCallback( |
| 250 | any(NetworkCallback.class), any(Handler.class)); |
| 251 | // TODO: Figure out why this isn't exactly once, for sendTetherStateChangedBroadcast(). |
| 252 | verify(mConnectivityManager, atLeastOnce()).isTetheringSupported(); |
| 253 | verifyNoMoreInteractions(mConnectivityManager); |
| 254 | |
| 255 | // Emulate externally-visible WifiManager effects, when hotspot mode |
| 256 | // is being torn down. |
| 257 | sendWifiApStateChanged(WifiManager.WIFI_AP_STATE_DISABLED); |
| 258 | mTethering.interfaceRemoved(mTestIfname); |
| 259 | mLooper.dispatchAll(); |
| 260 | |
| 261 | verify(mNMService, times(1)).untetherInterface(mTestIfname); |
| 262 | // TODO: Why is {g,s}etInterfaceConfig() called more than once? |
| 263 | verify(mNMService, atLeastOnce()).getInterfaceConfig(mTestIfname); |
| 264 | verify(mNMService, atLeastOnce()) |
| 265 | .setInterfaceConfig(eq(mTestIfname), any(InterfaceConfiguration.class)); |
| 266 | verify(mNMService, times(1)).stopTethering(); |
| 267 | verify(mNMService, times(1)).setIpForwardingEnabled(false); |
| 268 | verifyNoMoreInteractions(mNMService); |
Erik Kline | 216af6d | 2017-04-27 20:57:23 +0900 | [diff] [blame] | 269 | verifyNoMoreInteractions(mWifiManager); |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 270 | // Asking for the last error after the per-interface state machine |
| 271 | // has been reaped yields an unknown interface error. |
| 272 | assertEquals(ConnectivityManager.TETHER_ERROR_UNKNOWN_IFACE, |
| 273 | mTethering.getLastTetherError(mTestIfname)); |
| 274 | } |
| 275 | |
| 276 | @Test |
| 277 | public void workingWifiTethering() throws Exception { |
| 278 | when(mConnectivityManager.isTetheringSupported()).thenReturn(true); |
Erik Kline | ceb54c6 | 2017-04-18 14:22:25 +0900 | [diff] [blame] | 279 | when(mWifiManager.startSoftAp(any(WifiConfiguration.class))).thenReturn(true); |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 280 | |
| 281 | // Emulate pressing the WiFi tethering button. |
| 282 | mTethering.startTethering(ConnectivityManager.TETHERING_WIFI, null, false); |
| 283 | mLooper.dispatchAll(); |
Erik Kline | ceb54c6 | 2017-04-18 14:22:25 +0900 | [diff] [blame] | 284 | verify(mWifiManager, times(1)).startSoftAp(null); |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 285 | verifyNoMoreInteractions(mWifiManager); |
| 286 | verifyNoMoreInteractions(mConnectivityManager); |
| 287 | verifyNoMoreInteractions(mNMService); |
| 288 | |
| 289 | // Emulate externally-visible WifiManager effects, causing the |
| 290 | // per-interface state machine to start up, and telling us that |
| 291 | // tethering mode is to be started. |
| 292 | mTethering.interfaceStatusChanged(mTestIfname, true); |
| 293 | sendWifiApStateChanged(WifiManager.WIFI_AP_STATE_ENABLED); |
| 294 | mLooper.dispatchAll(); |
| 295 | |
Erik Kline | 8351faa | 2017-04-17 16:47:23 +0900 | [diff] [blame] | 296 | verifyInterfaceServingModeStarted(); |
| 297 | verifyTetheringBroadcast(mTestIfname, ConnectivityManager.EXTRA_AVAILABLE_TETHER); |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 298 | verify(mNMService, times(1)).setIpForwardingEnabled(true); |
| 299 | verify(mNMService, times(1)).startTethering(any(String[].class)); |
| 300 | verifyNoMoreInteractions(mNMService); |
Erik Kline | 216af6d | 2017-04-27 20:57:23 +0900 | [diff] [blame] | 301 | verify(mWifiManager).updateInterfaceIpState( |
| 302 | mTestIfname, WifiManager.IFACE_IP_MODE_TETHERED); |
| 303 | verifyNoMoreInteractions(mWifiManager); |
Erik Kline | 8351faa | 2017-04-17 16:47:23 +0900 | [diff] [blame] | 304 | verifyTetheringBroadcast(mTestIfname, ConnectivityManager.EXTRA_ACTIVE_TETHER); |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 305 | // UpstreamNetworkMonitor will be started, and will register two callbacks: |
| 306 | // a "listen all" and a "track default". |
| 307 | verify(mConnectivityManager, times(1)).registerNetworkCallback( |
| 308 | any(NetworkRequest.class), any(NetworkCallback.class), any(Handler.class)); |
| 309 | verify(mConnectivityManager, times(1)).registerDefaultNetworkCallback( |
| 310 | any(NetworkCallback.class), any(Handler.class)); |
| 311 | // In tethering mode, in the default configuration, an explicit request |
| 312 | // for a mobile network is also made. |
| 313 | verify(mConnectivityManager, atLeastOnce()).getNetworkInfo(anyInt()); |
| 314 | verify(mConnectivityManager, times(1)).requestNetwork( |
| 315 | any(NetworkRequest.class), any(NetworkCallback.class), eq(0), anyInt(), |
| 316 | any(Handler.class)); |
| 317 | // TODO: Figure out why this isn't exactly once, for sendTetherStateChangedBroadcast(). |
| 318 | verify(mConnectivityManager, atLeastOnce()).isTetheringSupported(); |
| 319 | verifyNoMoreInteractions(mConnectivityManager); |
| 320 | |
| 321 | ///// |
| 322 | // We do not currently emulate any upstream being found. |
| 323 | // |
| 324 | // This is why there are no calls to verify mNMService.enableNat() or |
| 325 | // mNMService.startInterfaceForwarding(). |
| 326 | ///// |
| 327 | |
| 328 | // Emulate pressing the WiFi tethering button. |
| 329 | mTethering.stopTethering(ConnectivityManager.TETHERING_WIFI); |
| 330 | mLooper.dispatchAll(); |
Erik Kline | ceb54c6 | 2017-04-18 14:22:25 +0900 | [diff] [blame] | 331 | verify(mWifiManager, times(1)).stopSoftAp(); |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 332 | verifyNoMoreInteractions(mWifiManager); |
| 333 | verifyNoMoreInteractions(mConnectivityManager); |
| 334 | verifyNoMoreInteractions(mNMService); |
| 335 | |
| 336 | // Emulate externally-visible WifiManager effects, when tethering mode |
| 337 | // is being torn down. |
| 338 | sendWifiApStateChanged(WifiManager.WIFI_AP_STATE_DISABLED); |
| 339 | mTethering.interfaceRemoved(mTestIfname); |
| 340 | mLooper.dispatchAll(); |
| 341 | |
| 342 | verify(mNMService, times(1)).untetherInterface(mTestIfname); |
| 343 | // TODO: Why is {g,s}etInterfaceConfig() called more than once? |
| 344 | verify(mNMService, atLeastOnce()).getInterfaceConfig(mTestIfname); |
| 345 | verify(mNMService, atLeastOnce()) |
| 346 | .setInterfaceConfig(eq(mTestIfname), any(InterfaceConfiguration.class)); |
| 347 | verify(mNMService, times(1)).stopTethering(); |
| 348 | verify(mNMService, times(1)).setIpForwardingEnabled(false); |
| 349 | verifyNoMoreInteractions(mNMService); |
Erik Kline | 216af6d | 2017-04-27 20:57:23 +0900 | [diff] [blame] | 350 | verifyNoMoreInteractions(mWifiManager); |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 351 | // Asking for the last error after the per-interface state machine |
| 352 | // has been reaped yields an unknown interface error. |
| 353 | assertEquals(ConnectivityManager.TETHER_ERROR_UNKNOWN_IFACE, |
| 354 | mTethering.getLastTetherError(mTestIfname)); |
| 355 | } |
| 356 | |
Erik Kline | 1fdc2e2 | 2017-05-08 17:56:35 +0900 | [diff] [blame^] | 357 | @Test |
| 358 | public void failureEnablingIpForwarding() throws Exception { |
| 359 | when(mConnectivityManager.isTetheringSupported()).thenReturn(true); |
| 360 | when(mWifiManager.startSoftAp(any(WifiConfiguration.class))).thenReturn(true); |
| 361 | doThrow(new RemoteException()).when(mNMService).setIpForwardingEnabled(true); |
| 362 | |
| 363 | // Emulate pressing the WiFi tethering button. |
| 364 | mTethering.startTethering(ConnectivityManager.TETHERING_WIFI, null, false); |
| 365 | mLooper.dispatchAll(); |
| 366 | verify(mWifiManager, times(1)).startSoftAp(null); |
| 367 | verifyNoMoreInteractions(mWifiManager); |
| 368 | verifyNoMoreInteractions(mConnectivityManager); |
| 369 | verifyNoMoreInteractions(mNMService); |
| 370 | |
| 371 | // Emulate externally-visible WifiManager effects, causing the |
| 372 | // per-interface state machine to start up, and telling us that |
| 373 | // tethering mode is to be started. |
| 374 | mTethering.interfaceStatusChanged(mTestIfname, true); |
| 375 | sendWifiApStateChanged(WifiManager.WIFI_AP_STATE_ENABLED); |
| 376 | mLooper.dispatchAll(); |
| 377 | |
| 378 | // Activity caused by test_wlan0 becoming available. |
| 379 | verify(mNMService, times(1)).listInterfaces(); |
| 380 | // We verify get/set called twice here: once for setup and once during |
| 381 | // teardown because all events happen over the course of the single |
| 382 | // dispatchAll() above. |
| 383 | verify(mNMService, times(2)).getInterfaceConfig(mTestIfname); |
| 384 | verify(mNMService, times(2)) |
| 385 | .setInterfaceConfig(eq(mTestIfname), any(InterfaceConfiguration.class)); |
| 386 | verify(mNMService, times(1)).tetherInterface(mTestIfname); |
| 387 | verify(mWifiManager).updateInterfaceIpState( |
| 388 | mTestIfname, WifiManager.IFACE_IP_MODE_TETHERED); |
| 389 | verify(mConnectivityManager, atLeastOnce()).isTetheringSupported(); |
| 390 | verifyTetheringBroadcast(mTestIfname, ConnectivityManager.EXTRA_AVAILABLE_TETHER); |
| 391 | // This is called, but will throw. |
| 392 | verify(mNMService, times(1)).setIpForwardingEnabled(true); |
| 393 | // This never gets called because of the exception thrown above. |
| 394 | verify(mNMService, times(0)).startTethering(any(String[].class)); |
| 395 | // When the master state machine transitions to an error state it tells |
| 396 | // downstream interfaces, which causes us to tell Wi-Fi about the error |
| 397 | // so it can take down AP mode. |
| 398 | verify(mNMService, times(1)).untetherInterface(mTestIfname); |
| 399 | verify(mWifiManager).updateInterfaceIpState( |
| 400 | mTestIfname, WifiManager.IFACE_IP_MODE_CONFIGURATION_ERROR); |
| 401 | |
| 402 | verifyNoMoreInteractions(mWifiManager); |
| 403 | verifyNoMoreInteractions(mConnectivityManager); |
| 404 | verifyNoMoreInteractions(mNMService); |
| 405 | } |
| 406 | |
| 407 | // TODO: Test that a request for hotspot mode doesn't interfere with an |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 408 | // already operating tethering mode interface. |
Christopher Wiley | 497c147 | 2016-10-11 13:26:03 -0700 | [diff] [blame] | 409 | } |