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