blob: acf9e8f71718a28c4a563ce21a4ed18cc40c94e4 [file] [log] [blame]
Christopher Wiley497c1472016-10-11 13:26:03 -07001/*
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
17package com.android.server.connectivity;
18
Erik Klinec438e302017-07-04 22:02:49 +090019import static android.hardware.usb.UsbManager.USB_CONFIGURED;
20import static android.hardware.usb.UsbManager.USB_CONNECTED;
21import static android.hardware.usb.UsbManager.USB_FUNCTION_RNDIS;
22import static android.net.ConnectivityManager.TETHERING_WIFI;
23import static android.net.ConnectivityManager.TETHERING_USB;
Erik Kline2efb8272017-05-31 15:53:53 +090024import static android.net.wifi.WifiManager.IFACE_IP_MODE_LOCAL_ONLY;
25import static android.net.wifi.WifiManager.IFACE_IP_MODE_TETHERED;
26import static android.net.wifi.WifiManager.EXTRA_WIFI_AP_INTERFACE_NAME;
27import static android.net.wifi.WifiManager.EXTRA_WIFI_AP_MODE;
28import static android.net.wifi.WifiManager.EXTRA_WIFI_AP_STATE;
29import static android.net.wifi.WifiManager.WIFI_AP_STATE_ENABLED;
Erik Klineea9cc482017-03-10 19:35:34 +090030import static org.junit.Assert.assertEquals;
Christopher Wiley497c1472016-10-11 13:26:03 -070031import static org.junit.Assert.assertTrue;
32import static org.mockito.Matchers.anyBoolean;
Erik Klineea9cc482017-03-10 19:35:34 +090033import static org.mockito.Matchers.anyInt;
34import static org.mockito.Matchers.anyString;
Christopher Wiley497c1472016-10-11 13:26:03 -070035import static org.mockito.Matchers.eq;
Erik Kline1fdc2e22017-05-08 17:56:35 +090036import static org.mockito.Mockito.any;
Erik Klineea9cc482017-03-10 19:35:34 +090037import static org.mockito.Mockito.atLeastOnce;
Erik Kline1fdc2e22017-05-08 17:56:35 +090038import static org.mockito.Mockito.doThrow;
Erik Klineea9cc482017-03-10 19:35:34 +090039import static org.mockito.Mockito.times;
40import static org.mockito.Mockito.verify;
41import static org.mockito.Mockito.verifyNoMoreInteractions;
Christopher Wiley497c1472016-10-11 13:26:03 -070042import static org.mockito.Mockito.when;
43
Erik Kline8351faa2017-04-17 16:47:23 +090044import android.content.BroadcastReceiver;
Erik Kline92c4db02017-05-31 10:21:32 +090045import android.content.ContentResolver;
Christopher Wiley497c1472016-10-11 13:26:03 -070046import android.content.Context;
Erik Klineea9cc482017-03-10 19:35:34 +090047import android.content.ContextWrapper;
48import android.content.Intent;
Erik Kline8351faa2017-04-17 16:47:23 +090049import android.content.IntentFilter;
Erik Klinef3a08b42017-06-07 16:33:19 +090050import android.content.pm.ApplicationInfo;
Christopher Wiley497c1472016-10-11 13:26:03 -070051import android.content.res.Resources;
Erik Klineea9cc482017-03-10 19:35:34 +090052import android.hardware.usb.UsbManager;
53import android.net.ConnectivityManager;
54import android.net.ConnectivityManager.NetworkCallback;
Christopher Wiley497c1472016-10-11 13:26:03 -070055import android.net.INetworkPolicyManager;
56import android.net.INetworkStatsService;
Erik Klineea9cc482017-03-10 19:35:34 +090057import android.net.InterfaceConfiguration;
58import android.net.NetworkRequest;
Erik Klinef4b6e342017-04-25 19:19:59 +090059import android.net.util.SharedLog;
Erik Klineea9cc482017-03-10 19:35:34 +090060import android.net.wifi.WifiConfiguration;
61import android.net.wifi.WifiManager;
62import android.os.Handler;
Christopher Wiley497c1472016-10-11 13:26:03 -070063import android.os.INetworkManagementService;
64import android.os.PersistableBundle;
Erik Kline1fdc2e22017-05-08 17:56:35 +090065import android.os.RemoteException;
Christopher Wiley497c1472016-10-11 13:26:03 -070066import android.os.test.TestLooper;
Erik Klineea9cc482017-03-10 19:35:34 +090067import android.os.UserHandle;
Erik Kline92c4db02017-05-31 10:21:32 +090068import android.provider.Settings;
Christopher Wiley497c1472016-10-11 13:26:03 -070069import android.support.test.filters.SmallTest;
70import android.support.test.runner.AndroidJUnit4;
71import android.telephony.CarrierConfigManager;
Erik Kline92c4db02017-05-31 10:21:32 +090072import android.test.mock.MockContentResolver;
Christopher Wiley497c1472016-10-11 13:26:03 -070073
Erik Klineea9cc482017-03-10 19:35:34 +090074import com.android.internal.util.test.BroadcastInterceptingContext;
Erik Kline92c4db02017-05-31 10:21:32 +090075import com.android.internal.util.test.FakeSettingsProvider;
Erik Kline5a7c8a02017-04-30 19:36:15 +090076import com.android.server.connectivity.tethering.OffloadHardwareInterface;
77import com.android.server.connectivity.tethering.TetheringDependencies;
Erik Klineea9cc482017-03-10 19:35:34 +090078
Erik Kline8351faa2017-04-17 16:47:23 +090079import org.junit.After;
Christopher Wiley497c1472016-10-11 13:26:03 -070080import org.junit.Before;
81import org.junit.Test;
82import org.junit.runner.RunWith;
83import org.mockito.Mock;
84import org.mockito.MockitoAnnotations;
85
Erik Kline8351faa2017-04-17 16:47:23 +090086import java.util.ArrayList;
87import java.util.Vector;
88
Christopher Wiley497c1472016-10-11 13:26:03 -070089@RunWith(AndroidJUnit4.class)
90@SmallTest
91public class TetheringTest {
92 private static final String[] PROVISIONING_APP_NAME = {"some", "app"};
93
Erik Klinef3a08b42017-06-07 16:33:19 +090094 @Mock private ApplicationInfo mApplicationInfo;
Christopher Wiley497c1472016-10-11 13:26:03 -070095 @Mock private Context mContext;
Erik Klineea9cc482017-03-10 19:35:34 +090096 @Mock private ConnectivityManager mConnectivityManager;
Christopher Wiley497c1472016-10-11 13:26:03 -070097 @Mock private INetworkManagementService mNMService;
98 @Mock private INetworkStatsService mStatsService;
99 @Mock private INetworkPolicyManager mPolicyManager;
100 @Mock private MockableSystemProperties mSystemProperties;
Erik Kline5a7c8a02017-04-30 19:36:15 +0900101 @Mock private OffloadHardwareInterface mOffloadHardwareInterface;
Christopher Wiley497c1472016-10-11 13:26:03 -0700102 @Mock private Resources mResources;
Erik Kline5a7c8a02017-04-30 19:36:15 +0900103 @Mock private TetheringDependencies mTetheringDependencies;
Erik Klineea9cc482017-03-10 19:35:34 +0900104 @Mock private UsbManager mUsbManager;
105 @Mock private WifiManager mWifiManager;
Christopher Wiley497c1472016-10-11 13:26:03 -0700106 @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 Klineea9cc482017-03-10 19:35:34 +0900112 private final String mTestIfname = "test_wlan0";
Christopher Wiley497c1472016-10-11 13:26:03 -0700113
Erik Kline8351faa2017-04-17 16:47:23 +0900114 private Vector<Intent> mIntents;
Erik Klineea9cc482017-03-10 19:35:34 +0900115 private BroadcastInterceptingContext mServiceContext;
Erik Kline92c4db02017-05-31 10:21:32 +0900116 private MockContentResolver mContentResolver;
Erik Kline8351faa2017-04-17 16:47:23 +0900117 private BroadcastReceiver mBroadcastReceiver;
Christopher Wiley497c1472016-10-11 13:26:03 -0700118 private Tethering mTethering;
119
Erik Klineea9cc482017-03-10 19:35:34 +0900120 private class MockContext extends BroadcastInterceptingContext {
121 MockContext(Context base) {
122 super(base);
123 }
124
125 @Override
Erik Klinef3a08b42017-06-07 16:33:19 +0900126 public ApplicationInfo getApplicationInfo() { return mApplicationInfo; }
127
128 @Override
Erik Kline92c4db02017-05-31 10:21:32 +0900129 public ContentResolver getContentResolver() { return mContentResolver; }
130
131 @Override
132 public String getPackageName() { return "TetheringTest"; }
133
134 @Override
Erik Klineea9cc482017-03-10 19:35:34 +0900135 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 Klinec438e302017-07-04 22:02:49 +0900141 if (Context.USB_SERVICE.equals(name)) return mUsbManager;
Erik Klineea9cc482017-03-10 19:35:34 +0900142 return super.getSystemService(name);
143 }
144 }
145
Erik Kline8351faa2017-04-17 16:47:23 +0900146 @Before
147 public void setUp() throws Exception {
Christopher Wiley497c1472016-10-11 13:26:03 -0700148 MockitoAnnotations.initMocks(this);
Christopher Wiley497c1472016-10-11 13:26:03 -0700149 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 Klinec438e302017-07-04 22:02:49 +0900154 .thenReturn(new String[]{ "test_wlan\\d", "test_rndis\\d" });
Christopher Wiley497c1472016-10-11 13:26:03 -0700155 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 Klineea9cc482017-03-10 19:35:34 +0900159 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 Kline92c4db02017-05-31 10:21:32 +0900165 mContentResolver = new MockContentResolver(mServiceContext);
166 mContentResolver.addProvider(Settings.AUTHORITY, new FakeSettingsProvider());
Erik Kline8351faa2017-04-17 16:47:23 +0900167 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 Klinef4b6e342017-04-25 19:19:59 +0900176 when(mTetheringDependencies.getOffloadHardwareInterface(
177 any(Handler.class), any(SharedLog.class))).thenReturn(mOffloadHardwareInterface);
Erik Klineea9cc482017-03-10 19:35:34 +0900178 mTethering = new Tethering(mServiceContext, mNMService, mStatsService, mPolicyManager,
Erik Kline5a7c8a02017-04-30 19:36:15 +0900179 mLooper.getLooper(), mSystemProperties,
180 mTetheringDependencies);
Christopher Wiley497c1472016-10-11 13:26:03 -0700181 }
182
Erik Kline8351faa2017-04-17 16:47:23 +0900183 @After
184 public void tearDown() {
185 mServiceContext.unregisterReceiver(mBroadcastReceiver);
186 }
187
Christopher Wiley497c1472016-10-11 13:26:03 -0700188 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 Klineea9cc482017-03-10 19:35:34 +0900239
240 private void sendWifiApStateChanged(int state) {
241 final Intent intent = new Intent(WifiManager.WIFI_AP_STATE_CHANGED_ACTION);
Erik Kline2efb8272017-05-31 15:53:53 +0900242 intent.putExtra(EXTRA_WIFI_AP_STATE, state);
Erik Klineea9cc482017-03-10 19:35:34 +0900243 mServiceContext.sendStickyBroadcastAsUser(intent, UserHandle.ALL);
244 }
245
Erik Kline2efb8272017-05-31 15:53:53 +0900246 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 Klinec438e302017-07-04 22:02:49 +0900254 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 Kline9e225542017-06-08 17:48:48 +0900262 private void verifyInterfaceServingModeStarted() throws Exception {
Erik Kline8351faa2017-04-17 16:47:23 +0900263 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 Klinea9cde8b2017-06-20 21:18:31 +0900278 public void failingLocalOnlyHotspotLegacyApBroadcast(
279 boolean emulateInterfaceStatusChanged) throws Exception {
Erik Klineea9cc482017-03-10 19:35:34 +0900280 when(mConnectivityManager.isTetheringSupported()).thenReturn(true);
Erik Klineea9cc482017-03-10 19:35:34 +0900281
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 Klinea9cde8b2017-06-20 21:18:31 +0900285 if (emulateInterfaceStatusChanged) {
286 mTethering.interfaceStatusChanged(mTestIfname, true);
287 }
Erik Kline9e225542017-06-08 17:48:48 +0900288 sendWifiApStateChanged(WIFI_AP_STATE_ENABLED);
Erik Klineea9cc482017-03-10 19:35:34 +0900289 mLooper.dispatchAll();
290
Erik Klinea9cde8b2017-06-20 21:18:31 +0900291 // 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 Kline9e225542017-06-08 17:48:48 +0900298 verifyNoMoreInteractions(mConnectivityManager);
299 verifyNoMoreInteractions(mNMService);
300 verifyNoMoreInteractions(mWifiManager);
301 }
302
303 @Test
Erik Klinec438e302017-07-04 22:02:49 +0900304 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 Klinea9cde8b2017-06-20 21:18:31 +0900330 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 Kline9e225542017-06-08 17:48:48 +0900341 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 Klinea9cde8b2017-06-20 21:18:31 +0900346 if (emulateInterfaceStatusChanged) {
347 mTethering.interfaceStatusChanged(mTestIfname, true);
348 }
Erik Kline9e225542017-06-08 17:48:48 +0900349 sendWifiApStateChanged(WIFI_AP_STATE_ENABLED, mTestIfname, IFACE_IP_MODE_LOCAL_ONLY);
350 mLooper.dispatchAll();
351
352 verifyInterfaceServingModeStarted();
Erik Kline8351faa2017-04-17 16:47:23 +0900353 verifyTetheringBroadcast(mTestIfname, ConnectivityManager.EXTRA_AVAILABLE_TETHER);
Erik Klineea9cc482017-03-10 19:35:34 +0900354 verify(mNMService, times(1)).setIpForwardingEnabled(true);
355 verify(mNMService, times(1)).startTethering(any(String[].class));
356 verifyNoMoreInteractions(mNMService);
Erik Kline216af6d2017-04-27 20:57:23 +0900357 verify(mWifiManager).updateInterfaceIpState(
358 mTestIfname, WifiManager.IFACE_IP_MODE_LOCAL_ONLY);
359 verifyNoMoreInteractions(mWifiManager);
Erik Kline8351faa2017-04-17 16:47:23 +0900360 verifyTetheringBroadcast(mTestIfname, ConnectivityManager.EXTRA_ACTIVE_LOCAL_ONLY);
Erik Klineea9cc482017-03-10 19:35:34 +0900361 // 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 Kline216af6d2017-04-27 20:57:23 +0900385 verifyNoMoreInteractions(mWifiManager);
Erik Klineea9cc482017-03-10 19:35:34 +0900386 // 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 Klinea9cde8b2017-06-20 21:18:31 +0900393 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 Kline9e225542017-06-08 17:48:48 +0900404 public void failingWifiTetheringLegacyApBroadcast() throws Exception {
Erik Klineea9cc482017-03-10 19:35:34 +0900405 when(mConnectivityManager.isTetheringSupported()).thenReturn(true);
Erik Klineceb54c62017-04-18 14:22:25 +0900406 when(mWifiManager.startSoftAp(any(WifiConfiguration.class))).thenReturn(true);
Erik Klineea9cc482017-03-10 19:35:34 +0900407
408 // Emulate pressing the WiFi tethering button.
Erik Klinec438e302017-07-04 22:02:49 +0900409 mTethering.startTethering(TETHERING_WIFI, null, false);
Erik Klineea9cc482017-03-10 19:35:34 +0900410 mLooper.dispatchAll();
Erik Klineceb54c62017-04-18 14:22:25 +0900411 verify(mWifiManager, times(1)).startSoftAp(null);
Erik Klineea9cc482017-03-10 19:35:34 +0900412 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 Kline9e225542017-06-08 17:48:48 +0900420 sendWifiApStateChanged(WIFI_AP_STATE_ENABLED);
Erik Klineea9cc482017-03-10 19:35:34 +0900421 mLooper.dispatchAll();
422
Erik Kline9e225542017-06-08 17:48:48 +0900423 verify(mConnectivityManager, atLeastOnce()).isTetheringSupported();
424 verifyTetheringBroadcast(mTestIfname, ConnectivityManager.EXTRA_AVAILABLE_TETHER);
425 verifyNoMoreInteractions(mConnectivityManager);
426 verifyNoMoreInteractions(mNMService);
427 verifyNoMoreInteractions(mWifiManager);
428 }
429
Erik Klinea9cde8b2017-06-20 21:18:31 +0900430 // TODO: Test with and without interfaceStatusChanged().
Erik Kline9e225542017-06-08 17:48:48 +0900431 @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 Klinec438e302017-07-04 22:02:49 +0900437 mTethering.startTethering(TETHERING_WIFI, null, false);
Erik Kline9e225542017-06-08 17:48:48 +0900438 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 Kline8351faa2017-04-17 16:47:23 +0900452 verifyTetheringBroadcast(mTestIfname, ConnectivityManager.EXTRA_AVAILABLE_TETHER);
Erik Klineea9cc482017-03-10 19:35:34 +0900453 verify(mNMService, times(1)).setIpForwardingEnabled(true);
454 verify(mNMService, times(1)).startTethering(any(String[].class));
455 verifyNoMoreInteractions(mNMService);
Erik Kline216af6d2017-04-27 20:57:23 +0900456 verify(mWifiManager).updateInterfaceIpState(
457 mTestIfname, WifiManager.IFACE_IP_MODE_TETHERED);
458 verifyNoMoreInteractions(mWifiManager);
Erik Kline8351faa2017-04-17 16:47:23 +0900459 verifyTetheringBroadcast(mTestIfname, ConnectivityManager.EXTRA_ACTIVE_TETHER);
Erik Klineea9cc482017-03-10 19:35:34 +0900460 // 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 Klineea9cc482017-03-10 19:35:34 +0900468 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 Klinec438e302017-07-04 22:02:49 +0900483 mTethering.stopTethering(TETHERING_WIFI);
Erik Klineea9cc482017-03-10 19:35:34 +0900484 mLooper.dispatchAll();
Erik Klineceb54c62017-04-18 14:22:25 +0900485 verify(mWifiManager, times(1)).stopSoftAp();
Erik Klineea9cc482017-03-10 19:35:34 +0900486 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 Kline216af6d2017-04-27 20:57:23 +0900504 verifyNoMoreInteractions(mWifiManager);
Erik Klineea9cc482017-03-10 19:35:34 +0900505 // 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 Klinea9cde8b2017-06-20 21:18:31 +0900511 // TODO: Test with and without interfaceStatusChanged().
Erik Kline1fdc2e22017-05-08 17:56:35 +0900512 @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 Klinec438e302017-07-04 22:02:49 +0900519 mTethering.startTethering(TETHERING_WIFI, null, false);
Erik Kline1fdc2e22017-05-08 17:56:35 +0900520 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 Kline9e225542017-06-08 17:48:48 +0900530 sendWifiApStateChanged(WIFI_AP_STATE_ENABLED, mTestIfname, IFACE_IP_MODE_TETHERED);
Erik Kline1fdc2e22017-05-08 17:56:35 +0900531 mLooper.dispatchAll();
532
Erik Kline1fdc2e22017-05-08 17:56:35 +0900533 // 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 Klineea9cc482017-03-10 19:35:34 +0900561 // already operating tethering mode interface.
Christopher Wiley497c1472016-10-11 13:26:03 -0700562}