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