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; |
Erik Kline | 465ff3a | 2018-03-09 14:18:02 +0900 | [diff] [blame] | 22 | import static android.net.ConnectivityManager.ACTION_TETHER_STATE_CHANGED; |
| 23 | import static android.net.ConnectivityManager.EXTRA_ACTIVE_LOCAL_ONLY; |
| 24 | import static android.net.ConnectivityManager.EXTRA_ACTIVE_TETHER; |
| 25 | import static android.net.ConnectivityManager.EXTRA_AVAILABLE_TETHER; |
Erik Kline | c438e30 | 2017-07-04 22:02:49 +0900 | [diff] [blame] | 26 | import static android.net.ConnectivityManager.TETHERING_USB; |
markchien | b6eb2c2 | 2018-07-18 14:29:20 +0800 | [diff] [blame] | 27 | import static android.net.ConnectivityManager.TETHERING_WIFI; |
| 28 | import static android.net.ConnectivityManager.TETHER_ERROR_UNKNOWN_IFACE; |
Erik Kline | 465ff3a | 2018-03-09 14:18:02 +0900 | [diff] [blame] | 29 | import static android.net.ConnectivityManager.TYPE_MOBILE; |
Remi NGUYEN VAN | 0e3d0923 | 2018-12-04 12:13:09 +0900 | [diff] [blame] | 30 | import static android.net.dhcp.IDhcpServer.STATUS_SUCCESS; |
Erik Kline | 2efb827 | 2017-05-31 15:53:53 +0900 | [diff] [blame] | 31 | import static android.net.wifi.WifiManager.EXTRA_WIFI_AP_INTERFACE_NAME; |
| 32 | import static android.net.wifi.WifiManager.EXTRA_WIFI_AP_MODE; |
| 33 | import static android.net.wifi.WifiManager.EXTRA_WIFI_AP_STATE; |
markchien | b6eb2c2 | 2018-07-18 14:29:20 +0800 | [diff] [blame] | 34 | import static android.net.wifi.WifiManager.IFACE_IP_MODE_LOCAL_ONLY; |
| 35 | import static android.net.wifi.WifiManager.IFACE_IP_MODE_TETHERED; |
Erik Kline | 2efb827 | 2017-05-31 15:53:53 +0900 | [diff] [blame] | 36 | import static android.net.wifi.WifiManager.WIFI_AP_STATE_ENABLED; |
Remi NGUYEN VAN | f5581f8 | 2018-09-28 11:34:15 +0900 | [diff] [blame] | 37 | import static android.provider.Settings.Global.TETHER_ENABLE_LEGACY_DHCP_SERVER; |
Remi NGUYEN VAN | a911e84 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 38 | |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 39 | import static org.junit.Assert.assertEquals; |
Christopher Wiley | 497c147 | 2016-10-11 13:26:03 -0700 | [diff] [blame] | 40 | import static org.junit.Assert.assertTrue; |
Remi NGUYEN VAN | 0e3d0923 | 2018-12-04 12:13:09 +0900 | [diff] [blame] | 41 | import static org.junit.Assert.fail; |
Remi NGUYEN VAN | a911e84 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 42 | import static org.mockito.ArgumentMatchers.argThat; |
| 43 | import static org.mockito.ArgumentMatchers.notNull; |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 44 | import static org.mockito.Matchers.anyInt; |
| 45 | import static org.mockito.Matchers.anyString; |
Christopher Wiley | 497c147 | 2016-10-11 13:26:03 -0700 | [diff] [blame] | 46 | import static org.mockito.Matchers.eq; |
Erik Kline | 1fdc2e2 | 2017-05-08 17:56:35 +0900 | [diff] [blame] | 47 | import static org.mockito.Mockito.any; |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 48 | import static org.mockito.Mockito.atLeastOnce; |
Erik Kline | 1fdc2e2 | 2017-05-08 17:56:35 +0900 | [diff] [blame] | 49 | import static org.mockito.Mockito.doThrow; |
markchien | b6eb2c2 | 2018-07-18 14:29:20 +0800 | [diff] [blame] | 50 | import static org.mockito.Mockito.mock; |
Remi NGUYEN VAN | a911e84 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 51 | import static org.mockito.Mockito.never; |
Remi NGUYEN VAN | 0e3d0923 | 2018-12-04 12:13:09 +0900 | [diff] [blame] | 52 | import static org.mockito.Mockito.spy; |
| 53 | import static org.mockito.Mockito.timeout; |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 54 | import static org.mockito.Mockito.times; |
| 55 | import static org.mockito.Mockito.verify; |
| 56 | import static org.mockito.Mockito.verifyNoMoreInteractions; |
Christopher Wiley | 497c147 | 2016-10-11 13:26:03 -0700 | [diff] [blame] | 57 | import static org.mockito.Mockito.when; |
| 58 | |
Erik Kline | 8351faa | 2017-04-17 16:47:23 +0900 | [diff] [blame] | 59 | import android.content.BroadcastReceiver; |
Erik Kline | 92c4db0 | 2017-05-31 10:21:32 +0900 | [diff] [blame] | 60 | import android.content.ContentResolver; |
Christopher Wiley | 497c147 | 2016-10-11 13:26:03 -0700 | [diff] [blame] | 61 | import android.content.Context; |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 62 | import android.content.Intent; |
Erik Kline | 8351faa | 2017-04-17 16:47:23 +0900 | [diff] [blame] | 63 | import android.content.IntentFilter; |
Erik Kline | f3a08b4 | 2017-06-07 16:33:19 +0900 | [diff] [blame] | 64 | import android.content.pm.ApplicationInfo; |
Christopher Wiley | 497c147 | 2016-10-11 13:26:03 -0700 | [diff] [blame] | 65 | import android.content.res.Resources; |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 66 | import android.hardware.usb.UsbManager; |
Remi NGUYEN VAN | a911e84 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 67 | import android.net.INetd; |
Christopher Wiley | 497c147 | 2016-10-11 13:26:03 -0700 | [diff] [blame] | 68 | import android.net.INetworkPolicyManager; |
| 69 | import android.net.INetworkStatsService; |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 70 | import android.net.InterfaceConfiguration; |
Remi NGUYEN VAN | a911e84 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 71 | import android.net.IpPrefix; |
| 72 | import android.net.LinkAddress; |
| 73 | import android.net.LinkProperties; |
| 74 | import android.net.MacAddress; |
| 75 | import android.net.Network; |
| 76 | import android.net.NetworkCapabilities; |
| 77 | import android.net.NetworkInfo; |
| 78 | import android.net.NetworkState; |
| 79 | import android.net.NetworkUtils; |
| 80 | import android.net.RouteInfo; |
Remi NGUYEN VAN | 0e3d0923 | 2018-12-04 12:13:09 +0900 | [diff] [blame] | 81 | import android.net.dhcp.DhcpServerCallbacks; |
| 82 | import android.net.dhcp.DhcpServingParamsParcel; |
| 83 | import android.net.dhcp.IDhcpServer; |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 84 | import android.net.ip.IpServer; |
Remi NGUYEN VAN | a911e84 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 85 | import android.net.ip.RouterAdvertisementDaemon; |
| 86 | import android.net.util.InterfaceParams; |
| 87 | import android.net.util.NetworkConstants; |
Erik Kline | f4b6e34 | 2017-04-25 19:19:59 +0900 | [diff] [blame] | 88 | import android.net.util.SharedLog; |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 89 | import android.net.wifi.WifiConfiguration; |
| 90 | import android.net.wifi.WifiManager; |
Alexandru-Andrei Rotaru | fa6d5c5 | 2017-07-18 16:49:22 +0100 | [diff] [blame] | 91 | import android.os.Bundle; |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 92 | import android.os.Handler; |
Christopher Wiley | 497c147 | 2016-10-11 13:26:03 -0700 | [diff] [blame] | 93 | import android.os.INetworkManagementService; |
| 94 | import android.os.PersistableBundle; |
Erik Kline | 1fdc2e2 | 2017-05-08 17:56:35 +0900 | [diff] [blame] | 95 | import android.os.RemoteException; |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 96 | import android.os.UserHandle; |
Alexandru-Andrei Rotaru | fa6d5c5 | 2017-07-18 16:49:22 +0100 | [diff] [blame] | 97 | import android.os.UserManager; |
markchien | b6eb2c2 | 2018-07-18 14:29:20 +0800 | [diff] [blame] | 98 | import android.os.test.TestLooper; |
Erik Kline | 92c4db0 | 2017-05-31 10:21:32 +0900 | [diff] [blame] | 99 | import android.provider.Settings; |
Christopher Wiley | 497c147 | 2016-10-11 13:26:03 -0700 | [diff] [blame] | 100 | import android.support.test.filters.SmallTest; |
| 101 | import android.support.test.runner.AndroidJUnit4; |
| 102 | import android.telephony.CarrierConfigManager; |
Erik Kline | 92c4db0 | 2017-05-31 10:21:32 +0900 | [diff] [blame] | 103 | import android.test.mock.MockContentResolver; |
Christopher Wiley | 497c147 | 2016-10-11 13:26:03 -0700 | [diff] [blame] | 104 | |
Remi NGUYEN VAN | a911e84 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 105 | import com.android.internal.util.ArrayUtils; |
| 106 | import com.android.internal.util.StateMachine; |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 107 | import com.android.internal.util.test.BroadcastInterceptingContext; |
Erik Kline | 92c4db0 | 2017-05-31 10:21:32 +0900 | [diff] [blame] | 108 | import com.android.internal.util.test.FakeSettingsProvider; |
Remi NGUYEN VAN | a911e84 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 109 | import com.android.server.connectivity.tethering.IPv6TetheringCoordinator; |
Erik Kline | 5a7c8a0 | 2017-04-30 19:36:15 +0900 | [diff] [blame] | 110 | import com.android.server.connectivity.tethering.OffloadHardwareInterface; |
| 111 | import com.android.server.connectivity.tethering.TetheringDependencies; |
Remi NGUYEN VAN | a911e84 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 112 | import com.android.server.connectivity.tethering.UpstreamNetworkMonitor; |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 113 | |
Erik Kline | 8351faa | 2017-04-17 16:47:23 +0900 | [diff] [blame] | 114 | import org.junit.After; |
Christopher Wiley | 497c147 | 2016-10-11 13:26:03 -0700 | [diff] [blame] | 115 | import org.junit.Before; |
| 116 | import org.junit.Test; |
| 117 | import org.junit.runner.RunWith; |
| 118 | import org.mockito.Mock; |
| 119 | import org.mockito.MockitoAnnotations; |
| 120 | |
Remi NGUYEN VAN | a911e84 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 121 | import java.net.Inet4Address; |
| 122 | import java.net.Inet6Address; |
Erik Kline | 8351faa | 2017-04-17 16:47:23 +0900 | [diff] [blame] | 123 | import java.util.ArrayList; |
| 124 | import java.util.Vector; |
| 125 | |
Christopher Wiley | 497c147 | 2016-10-11 13:26:03 -0700 | [diff] [blame] | 126 | @RunWith(AndroidJUnit4.class) |
| 127 | @SmallTest |
| 128 | public class TetheringTest { |
Remi NGUYEN VAN | a911e84 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 129 | private static final int IFINDEX_OFFSET = 100; |
| 130 | |
Remi NGUYEN VAN | a911e84 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 131 | private static final String TEST_MOBILE_IFNAME = "test_rmnet_data0"; |
| 132 | private static final String TEST_XLAT_MOBILE_IFNAME = "v4-test_rmnet_data0"; |
| 133 | private static final String TEST_USB_IFNAME = "test_rndis0"; |
| 134 | private static final String TEST_WLAN_IFNAME = "test_wlan0"; |
Christopher Wiley | 497c147 | 2016-10-11 13:26:03 -0700 | [diff] [blame] | 135 | |
Remi NGUYEN VAN | 0e3d0923 | 2018-12-04 12:13:09 +0900 | [diff] [blame] | 136 | private static final int DHCPSERVER_START_TIMEOUT_MS = 1000; |
| 137 | |
Erik Kline | f3a08b4 | 2017-06-07 16:33:19 +0900 | [diff] [blame] | 138 | @Mock private ApplicationInfo mApplicationInfo; |
Christopher Wiley | 497c147 | 2016-10-11 13:26:03 -0700 | [diff] [blame] | 139 | @Mock private Context mContext; |
| 140 | @Mock private INetworkManagementService mNMService; |
| 141 | @Mock private INetworkStatsService mStatsService; |
| 142 | @Mock private INetworkPolicyManager mPolicyManager; |
| 143 | @Mock private MockableSystemProperties mSystemProperties; |
Erik Kline | 5a7c8a0 | 2017-04-30 19:36:15 +0900 | [diff] [blame] | 144 | @Mock private OffloadHardwareInterface mOffloadHardwareInterface; |
Christopher Wiley | 497c147 | 2016-10-11 13:26:03 -0700 | [diff] [blame] | 145 | @Mock private Resources mResources; |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 146 | @Mock private UsbManager mUsbManager; |
| 147 | @Mock private WifiManager mWifiManager; |
Christopher Wiley | 497c147 | 2016-10-11 13:26:03 -0700 | [diff] [blame] | 148 | @Mock private CarrierConfigManager mCarrierConfigManager; |
Remi NGUYEN VAN | a911e84 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 149 | @Mock private UpstreamNetworkMonitor mUpstreamNetworkMonitor; |
| 150 | @Mock private IPv6TetheringCoordinator mIPv6TetheringCoordinator; |
| 151 | @Mock private RouterAdvertisementDaemon mRouterAdvertisementDaemon; |
Remi NGUYEN VAN | 0e3d0923 | 2018-12-04 12:13:09 +0900 | [diff] [blame] | 152 | @Mock private IDhcpServer mDhcpServer; |
Remi NGUYEN VAN | a911e84 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 153 | @Mock private INetd mNetd; |
| 154 | |
Remi NGUYEN VAN | 0e3d0923 | 2018-12-04 12:13:09 +0900 | [diff] [blame] | 155 | private final MockIpServerDependencies mIpServerDependencies = |
| 156 | spy(new MockIpServerDependencies()); |
Remi NGUYEN VAN | a911e84 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 157 | private final MockTetheringDependencies mTetheringDependencies = |
| 158 | new MockTetheringDependencies(); |
Christopher Wiley | 497c147 | 2016-10-11 13:26:03 -0700 | [diff] [blame] | 159 | |
| 160 | // Like so many Android system APIs, these cannot be mocked because it is marked final. |
| 161 | // We have to use the real versions. |
| 162 | private final PersistableBundle mCarrierConfig = new PersistableBundle(); |
| 163 | private final TestLooper mLooper = new TestLooper(); |
| 164 | |
Erik Kline | 8351faa | 2017-04-17 16:47:23 +0900 | [diff] [blame] | 165 | private Vector<Intent> mIntents; |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 166 | private BroadcastInterceptingContext mServiceContext; |
Erik Kline | 92c4db0 | 2017-05-31 10:21:32 +0900 | [diff] [blame] | 167 | private MockContentResolver mContentResolver; |
Erik Kline | 8351faa | 2017-04-17 16:47:23 +0900 | [diff] [blame] | 168 | private BroadcastReceiver mBroadcastReceiver; |
Christopher Wiley | 497c147 | 2016-10-11 13:26:03 -0700 | [diff] [blame] | 169 | private Tethering mTethering; |
| 170 | |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 171 | private class MockContext extends BroadcastInterceptingContext { |
| 172 | MockContext(Context base) { |
| 173 | super(base); |
| 174 | } |
| 175 | |
| 176 | @Override |
Erik Kline | f3a08b4 | 2017-06-07 16:33:19 +0900 | [diff] [blame] | 177 | public ApplicationInfo getApplicationInfo() { return mApplicationInfo; } |
| 178 | |
| 179 | @Override |
Erik Kline | 92c4db0 | 2017-05-31 10:21:32 +0900 | [diff] [blame] | 180 | public ContentResolver getContentResolver() { return mContentResolver; } |
| 181 | |
| 182 | @Override |
| 183 | public String getPackageName() { return "TetheringTest"; } |
| 184 | |
| 185 | @Override |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 186 | public Resources getResources() { return mResources; } |
| 187 | |
| 188 | @Override |
| 189 | public Object getSystemService(String name) { |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 190 | if (Context.WIFI_SERVICE.equals(name)) return mWifiManager; |
Erik Kline | c438e30 | 2017-07-04 22:02:49 +0900 | [diff] [blame] | 191 | if (Context.USB_SERVICE.equals(name)) return mUsbManager; |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 192 | return super.getSystemService(name); |
| 193 | } |
| 194 | } |
| 195 | |
Remi NGUYEN VAN | 0e3d0923 | 2018-12-04 12:13:09 +0900 | [diff] [blame] | 196 | public class MockIpServerDependencies extends IpServer.Dependencies { |
| 197 | MockIpServerDependencies() { |
| 198 | super(null); |
| 199 | } |
| 200 | |
| 201 | @Override |
| 202 | public RouterAdvertisementDaemon getRouterAdvertisementDaemon( |
| 203 | InterfaceParams ifParams) { |
| 204 | return mRouterAdvertisementDaemon; |
| 205 | } |
| 206 | |
| 207 | @Override |
| 208 | public InterfaceParams getInterfaceParams(String ifName) { |
| 209 | assertTrue("Non-mocked interface " + ifName, |
| 210 | ifName.equals(TEST_USB_IFNAME) |
| 211 | || ifName.equals(TEST_WLAN_IFNAME) |
| 212 | || ifName.equals(TEST_MOBILE_IFNAME)); |
| 213 | final String[] ifaces = new String[] { |
| 214 | TEST_USB_IFNAME, TEST_WLAN_IFNAME, TEST_MOBILE_IFNAME }; |
| 215 | return new InterfaceParams(ifName, ArrayUtils.indexOf(ifaces, ifName) + IFINDEX_OFFSET, |
| 216 | MacAddress.ALL_ZEROS_ADDRESS); |
| 217 | } |
| 218 | |
| 219 | @Override |
| 220 | public INetd getNetdService() { |
| 221 | return mNetd; |
| 222 | } |
| 223 | |
| 224 | @Override |
| 225 | public void makeDhcpServer(String ifName, DhcpServingParamsParcel params, |
| 226 | DhcpServerCallbacks cb) { |
| 227 | new Thread(() -> { |
| 228 | try { |
| 229 | cb.onDhcpServerCreated(STATUS_SUCCESS, mDhcpServer); |
| 230 | } catch (RemoteException e) { |
| 231 | fail(e.getMessage()); |
| 232 | } |
| 233 | }).run(); |
| 234 | } |
| 235 | } |
| 236 | |
Remi NGUYEN VAN | a911e84 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 237 | public class MockTetheringDependencies extends TetheringDependencies { |
Erik Kline | 465ff3a | 2018-03-09 14:18:02 +0900 | [diff] [blame] | 238 | StateMachine upstreamNetworkMonitorMasterSM; |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 239 | ArrayList<IpServer> ipv6CoordinatorNotifyList; |
Erik Kline | 465ff3a | 2018-03-09 14:18:02 +0900 | [diff] [blame] | 240 | int isTetheringSupportedCalls; |
| 241 | |
| 242 | public void reset() { |
| 243 | upstreamNetworkMonitorMasterSM = null; |
| 244 | ipv6CoordinatorNotifyList = null; |
| 245 | isTetheringSupportedCalls = 0; |
| 246 | } |
Remi NGUYEN VAN | a911e84 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 247 | |
| 248 | @Override |
| 249 | public OffloadHardwareInterface getOffloadHardwareInterface(Handler h, SharedLog log) { |
| 250 | return mOffloadHardwareInterface; |
| 251 | } |
| 252 | |
| 253 | @Override |
| 254 | public UpstreamNetworkMonitor getUpstreamNetworkMonitor(Context ctx, |
| 255 | StateMachine target, SharedLog log, int what) { |
| 256 | upstreamNetworkMonitorMasterSM = target; |
| 257 | return mUpstreamNetworkMonitor; |
| 258 | } |
| 259 | |
| 260 | @Override |
| 261 | public IPv6TetheringCoordinator getIPv6TetheringCoordinator( |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 262 | ArrayList<IpServer> notifyList, SharedLog log) { |
Remi NGUYEN VAN | a911e84 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 263 | ipv6CoordinatorNotifyList = notifyList; |
| 264 | return mIPv6TetheringCoordinator; |
| 265 | } |
| 266 | |
| 267 | @Override |
Remi NGUYEN VAN | 0e3d0923 | 2018-12-04 12:13:09 +0900 | [diff] [blame] | 268 | public IpServer.Dependencies getIpServerDependencies(Context context) { |
| 269 | return mIpServerDependencies; |
Remi NGUYEN VAN | a911e84 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 270 | } |
Erik Kline | 465ff3a | 2018-03-09 14:18:02 +0900 | [diff] [blame] | 271 | |
| 272 | @Override |
| 273 | public boolean isTetheringSupported() { |
| 274 | isTetheringSupportedCalls++; |
| 275 | return true; |
| 276 | } |
Remi NGUYEN VAN | a911e84 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 277 | } |
| 278 | |
Remi NGUYEN VAN | 25a7e4f | 2018-03-09 14:07:18 +0900 | [diff] [blame] | 279 | private static NetworkState buildMobileUpstreamState(boolean withIPv4, boolean withIPv6, |
| 280 | boolean with464xlat) { |
Erik Kline | 465ff3a | 2018-03-09 14:18:02 +0900 | [diff] [blame] | 281 | final NetworkInfo info = new NetworkInfo(TYPE_MOBILE, 0, null, null); |
Remi NGUYEN VAN | a911e84 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 282 | info.setDetailedState(NetworkInfo.DetailedState.CONNECTED, null, null); |
| 283 | final LinkProperties prop = new LinkProperties(); |
| 284 | prop.setInterfaceName(TEST_MOBILE_IFNAME); |
| 285 | |
| 286 | if (withIPv4) { |
| 287 | prop.addRoute(new RouteInfo(new IpPrefix(Inet4Address.ANY, 0), |
| 288 | NetworkUtils.numericToInetAddress("10.0.0.1"), TEST_MOBILE_IFNAME)); |
| 289 | } |
| 290 | |
| 291 | if (withIPv6) { |
| 292 | prop.addDnsServer(NetworkUtils.numericToInetAddress("2001:db8::2")); |
| 293 | prop.addLinkAddress( |
| 294 | new LinkAddress(NetworkUtils.numericToInetAddress("2001:db8::"), |
| 295 | NetworkConstants.RFC7421_PREFIX_LENGTH)); |
| 296 | prop.addRoute(new RouteInfo(new IpPrefix(Inet6Address.ANY, 0), |
| 297 | NetworkUtils.numericToInetAddress("2001:db8::1"), TEST_MOBILE_IFNAME)); |
| 298 | } |
| 299 | |
Remi NGUYEN VAN | 25a7e4f | 2018-03-09 14:07:18 +0900 | [diff] [blame] | 300 | if (with464xlat) { |
| 301 | final LinkProperties stackedLink = new LinkProperties(); |
| 302 | stackedLink.setInterfaceName(TEST_XLAT_MOBILE_IFNAME); |
| 303 | stackedLink.addRoute(new RouteInfo(new IpPrefix(Inet4Address.ANY, 0), |
| 304 | NetworkUtils.numericToInetAddress("192.0.0.1"), TEST_XLAT_MOBILE_IFNAME)); |
| 305 | |
| 306 | prop.addStackedLink(stackedLink); |
| 307 | } |
| 308 | |
Remi NGUYEN VAN | a911e84 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 309 | |
| 310 | final NetworkCapabilities capabilities = new NetworkCapabilities() |
| 311 | .addTransportType(NetworkCapabilities.TRANSPORT_CELLULAR);; |
| 312 | return new NetworkState(info, prop, capabilities, new Network(100), null, "netid"); |
| 313 | } |
| 314 | |
| 315 | private static NetworkState buildMobileIPv4UpstreamState() { |
Remi NGUYEN VAN | 25a7e4f | 2018-03-09 14:07:18 +0900 | [diff] [blame] | 316 | return buildMobileUpstreamState(true, false, false); |
| 317 | } |
| 318 | |
| 319 | private static NetworkState buildMobileIPv6UpstreamState() { |
| 320 | return buildMobileUpstreamState(false, true, false); |
Remi NGUYEN VAN | a911e84 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | private static NetworkState buildMobileDualStackUpstreamState() { |
Remi NGUYEN VAN | 25a7e4f | 2018-03-09 14:07:18 +0900 | [diff] [blame] | 324 | return buildMobileUpstreamState(true, true, false); |
| 325 | } |
| 326 | |
| 327 | private static NetworkState buildMobile464xlatUpstreamState() { |
| 328 | return buildMobileUpstreamState(false, true, true); |
Remi NGUYEN VAN | a911e84 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 329 | } |
| 330 | |
Erik Kline | 8351faa | 2017-04-17 16:47:23 +0900 | [diff] [blame] | 331 | @Before |
| 332 | public void setUp() throws Exception { |
Christopher Wiley | 497c147 | 2016-10-11 13:26:03 -0700 | [diff] [blame] | 333 | MockitoAnnotations.initMocks(this); |
Christopher Wiley | 497c147 | 2016-10-11 13:26:03 -0700 | [diff] [blame] | 334 | when(mResources.getStringArray(com.android.internal.R.array.config_tether_dhcp_range)) |
| 335 | .thenReturn(new String[0]); |
| 336 | when(mResources.getStringArray(com.android.internal.R.array.config_tether_usb_regexs)) |
Remi NGUYEN VAN | a911e84 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 337 | .thenReturn(new String[] { "test_rndis\\d" }); |
Christopher Wiley | 497c147 | 2016-10-11 13:26:03 -0700 | [diff] [blame] | 338 | when(mResources.getStringArray(com.android.internal.R.array.config_tether_wifi_regexs)) |
Remi NGUYEN VAN | a911e84 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 339 | .thenReturn(new String[]{ "test_wlan\\d" }); |
Christopher Wiley | 497c147 | 2016-10-11 13:26:03 -0700 | [diff] [blame] | 340 | when(mResources.getStringArray(com.android.internal.R.array.config_tether_bluetooth_regexs)) |
| 341 | .thenReturn(new String[0]); |
| 342 | when(mResources.getIntArray(com.android.internal.R.array.config_tether_upstream_types)) |
| 343 | .thenReturn(new int[0]); |
Erik Kline | 7230290 | 2018-06-14 17:36:40 +0900 | [diff] [blame] | 344 | when(mResources.getBoolean(com.android.internal.R.bool.config_tether_upstream_automatic)) |
| 345 | .thenReturn(false); |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 346 | when(mNMService.listInterfaces()) |
Remi NGUYEN VAN | a911e84 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 347 | .thenReturn(new String[] { |
| 348 | TEST_MOBILE_IFNAME, TEST_WLAN_IFNAME, TEST_USB_IFNAME}); |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 349 | when(mNMService.getInterfaceConfig(anyString())) |
| 350 | .thenReturn(new InterfaceConfiguration()); |
Remi NGUYEN VAN | a911e84 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 351 | when(mRouterAdvertisementDaemon.start()) |
| 352 | .thenReturn(true); |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 353 | |
| 354 | mServiceContext = new MockContext(mContext); |
Erik Kline | 92c4db0 | 2017-05-31 10:21:32 +0900 | [diff] [blame] | 355 | mContentResolver = new MockContentResolver(mServiceContext); |
| 356 | mContentResolver.addProvider(Settings.AUTHORITY, new FakeSettingsProvider()); |
Remi NGUYEN VAN | f5581f8 | 2018-09-28 11:34:15 +0900 | [diff] [blame] | 357 | Settings.Global.putInt(mContentResolver, TETHER_ENABLE_LEGACY_DHCP_SERVER, 0); |
Erik Kline | 8351faa | 2017-04-17 16:47:23 +0900 | [diff] [blame] | 358 | mIntents = new Vector<>(); |
| 359 | mBroadcastReceiver = new BroadcastReceiver() { |
| 360 | @Override |
| 361 | public void onReceive(Context context, Intent intent) { |
| 362 | mIntents.addElement(intent); |
| 363 | } |
| 364 | }; |
| 365 | mServiceContext.registerReceiver(mBroadcastReceiver, |
Erik Kline | 465ff3a | 2018-03-09 14:18:02 +0900 | [diff] [blame] | 366 | new IntentFilter(ACTION_TETHER_STATE_CHANGED)); |
| 367 | mTetheringDependencies.reset(); |
Remi NGUYEN VAN | f5581f8 | 2018-09-28 11:34:15 +0900 | [diff] [blame] | 368 | mTethering = makeTethering(); |
Lorenzo Colitti | 5a7dea1 | 2017-07-12 15:48:07 +0900 | [diff] [blame] | 369 | verify(mNMService).registerTetheringStatsProvider(any(), anyString()); |
Christopher Wiley | 497c147 | 2016-10-11 13:26:03 -0700 | [diff] [blame] | 370 | } |
| 371 | |
Remi NGUYEN VAN | f5581f8 | 2018-09-28 11:34:15 +0900 | [diff] [blame] | 372 | private Tethering makeTethering() { |
| 373 | return new Tethering(mServiceContext, mNMService, mStatsService, mPolicyManager, |
| 374 | mLooper.getLooper(), mSystemProperties, |
| 375 | mTetheringDependencies); |
| 376 | } |
| 377 | |
Erik Kline | 8351faa | 2017-04-17 16:47:23 +0900 | [diff] [blame] | 378 | @After |
| 379 | public void tearDown() { |
| 380 | mServiceContext.unregisterReceiver(mBroadcastReceiver); |
| 381 | } |
| 382 | |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 383 | private void sendWifiApStateChanged(int state) { |
| 384 | final Intent intent = new Intent(WifiManager.WIFI_AP_STATE_CHANGED_ACTION); |
Erik Kline | 2efb827 | 2017-05-31 15:53:53 +0900 | [diff] [blame] | 385 | intent.putExtra(EXTRA_WIFI_AP_STATE, state); |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 386 | mServiceContext.sendStickyBroadcastAsUser(intent, UserHandle.ALL); |
| 387 | } |
| 388 | |
Erik Kline | 2efb827 | 2017-05-31 15:53:53 +0900 | [diff] [blame] | 389 | private void sendWifiApStateChanged(int state, String ifname, int ipmode) { |
| 390 | final Intent intent = new Intent(WifiManager.WIFI_AP_STATE_CHANGED_ACTION); |
| 391 | intent.putExtra(EXTRA_WIFI_AP_STATE, state); |
| 392 | intent.putExtra(EXTRA_WIFI_AP_INTERFACE_NAME, ifname); |
| 393 | intent.putExtra(EXTRA_WIFI_AP_MODE, ipmode); |
| 394 | mServiceContext.sendStickyBroadcastAsUser(intent, UserHandle.ALL); |
| 395 | } |
| 396 | |
Erik Kline | c438e30 | 2017-07-04 22:02:49 +0900 | [diff] [blame] | 397 | private void sendUsbBroadcast(boolean connected, boolean configured, boolean rndisFunction) { |
| 398 | final Intent intent = new Intent(UsbManager.ACTION_USB_STATE); |
| 399 | intent.putExtra(USB_CONNECTED, connected); |
| 400 | intent.putExtra(USB_CONFIGURED, configured); |
| 401 | intent.putExtra(USB_FUNCTION_RNDIS, rndisFunction); |
| 402 | mServiceContext.sendStickyBroadcastAsUser(intent, UserHandle.ALL); |
| 403 | } |
| 404 | |
Erik Kline | 80b7a9f | 2018-02-28 15:01:35 +0900 | [diff] [blame] | 405 | private void sendConfigurationChanged() { |
| 406 | final Intent intent = new Intent(Intent.ACTION_CONFIGURATION_CHANGED); |
| 407 | mServiceContext.sendStickyBroadcastAsUser(intent, UserHandle.ALL); |
| 408 | } |
| 409 | |
Erik Kline | 9e22554 | 2017-06-08 17:48:48 +0900 | [diff] [blame] | 410 | private void verifyInterfaceServingModeStarted() throws Exception { |
Remi NGUYEN VAN | a911e84 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 411 | verify(mNMService, times(1)).getInterfaceConfig(TEST_WLAN_IFNAME); |
Erik Kline | 8351faa | 2017-04-17 16:47:23 +0900 | [diff] [blame] | 412 | verify(mNMService, times(1)) |
Remi NGUYEN VAN | a911e84 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 413 | .setInterfaceConfig(eq(TEST_WLAN_IFNAME), any(InterfaceConfiguration.class)); |
| 414 | verify(mNMService, times(1)).tetherInterface(TEST_WLAN_IFNAME); |
Erik Kline | 8351faa | 2017-04-17 16:47:23 +0900 | [diff] [blame] | 415 | } |
| 416 | |
| 417 | private void verifyTetheringBroadcast(String ifname, String whichExtra) { |
| 418 | // Verify that ifname is in the whichExtra array of the tether state changed broadcast. |
| 419 | final Intent bcast = mIntents.get(0); |
Erik Kline | 465ff3a | 2018-03-09 14:18:02 +0900 | [diff] [blame] | 420 | assertEquals(ACTION_TETHER_STATE_CHANGED, bcast.getAction()); |
Erik Kline | 8351faa | 2017-04-17 16:47:23 +0900 | [diff] [blame] | 421 | final ArrayList<String> ifnames = bcast.getStringArrayListExtra(whichExtra); |
| 422 | assertTrue(ifnames.contains(ifname)); |
| 423 | mIntents.remove(bcast); |
| 424 | } |
| 425 | |
Erik Kline | a9cde8b | 2017-06-20 21:18:31 +0900 | [diff] [blame] | 426 | public void failingLocalOnlyHotspotLegacyApBroadcast( |
| 427 | boolean emulateInterfaceStatusChanged) throws Exception { |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 428 | // Emulate externally-visible WifiManager effects, causing the |
| 429 | // per-interface state machine to start up, and telling us that |
| 430 | // hotspot mode is to be started. |
Erik Kline | a9cde8b | 2017-06-20 21:18:31 +0900 | [diff] [blame] | 431 | if (emulateInterfaceStatusChanged) { |
Remi NGUYEN VAN | a911e84 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 432 | mTethering.interfaceStatusChanged(TEST_WLAN_IFNAME, true); |
Erik Kline | a9cde8b | 2017-06-20 21:18:31 +0900 | [diff] [blame] | 433 | } |
Erik Kline | 9e22554 | 2017-06-08 17:48:48 +0900 | [diff] [blame] | 434 | sendWifiApStateChanged(WIFI_AP_STATE_ENABLED); |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 435 | mLooper.dispatchAll(); |
| 436 | |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 437 | // If, and only if, Tethering received an interface status changed then |
| 438 | // it creates a IpServer and sends out a broadcast indicating that the |
| 439 | // interface is "available". |
Erik Kline | a9cde8b | 2017-06-20 21:18:31 +0900 | [diff] [blame] | 440 | if (emulateInterfaceStatusChanged) { |
Erik Kline | 465ff3a | 2018-03-09 14:18:02 +0900 | [diff] [blame] | 441 | assertEquals(1, mTetheringDependencies.isTetheringSupportedCalls); |
| 442 | verifyTetheringBroadcast(TEST_WLAN_IFNAME, EXTRA_AVAILABLE_TETHER); |
Erik Kline | a9cde8b | 2017-06-20 21:18:31 +0900 | [diff] [blame] | 443 | } |
Erik Kline | 9e22554 | 2017-06-08 17:48:48 +0900 | [diff] [blame] | 444 | verifyNoMoreInteractions(mNMService); |
| 445 | verifyNoMoreInteractions(mWifiManager); |
| 446 | } |
| 447 | |
Remi NGUYEN VAN | a911e84 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 448 | private void prepareUsbTethering(NetworkState upstreamState) { |
Erik Kline | 7230290 | 2018-06-14 17:36:40 +0900 | [diff] [blame] | 449 | when(mUpstreamNetworkMonitor.getCurrentPreferredUpstream()).thenReturn(upstreamState); |
Remi NGUYEN VAN | a911e84 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 450 | when(mUpstreamNetworkMonitor.selectPreferredUpstreamType(any())) |
| 451 | .thenReturn(upstreamState); |
Erik Kline | c438e30 | 2017-07-04 22:02:49 +0900 | [diff] [blame] | 452 | |
| 453 | // Emulate pressing the USB tethering button in Settings UI. |
| 454 | mTethering.startTethering(TETHERING_USB, null, false); |
| 455 | mLooper.dispatchAll(); |
Jerry Zhang | 327b809 | 2018-01-09 17:53:04 -0800 | [diff] [blame] | 456 | verify(mUsbManager, times(1)).setCurrentFunctions(UsbManager.FUNCTION_RNDIS); |
Erik Kline | c438e30 | 2017-07-04 22:02:49 +0900 | [diff] [blame] | 457 | |
Remi NGUYEN VAN | a911e84 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 458 | mTethering.interfaceStatusChanged(TEST_USB_IFNAME, true); |
| 459 | } |
| 460 | |
| 461 | @Test |
| 462 | public void testUsbConfiguredBroadcastStartsTethering() throws Exception { |
| 463 | NetworkState upstreamState = buildMobileIPv4UpstreamState(); |
| 464 | prepareUsbTethering(upstreamState); |
| 465 | |
Erik Kline | c438e30 | 2017-07-04 22:02:49 +0900 | [diff] [blame] | 466 | // This should produce no activity of any kind. |
Erik Kline | c438e30 | 2017-07-04 22:02:49 +0900 | [diff] [blame] | 467 | verifyNoMoreInteractions(mNMService); |
| 468 | |
| 469 | // Pretend we then receive USB configured broadcast. |
| 470 | sendUsbBroadcast(true, true, true); |
| 471 | mLooper.dispatchAll(); |
| 472 | // Now we should see the start of tethering mechanics (in this case: |
| 473 | // tetherMatchingInterfaces() which starts by fetching all interfaces). |
| 474 | verify(mNMService, times(1)).listInterfaces(); |
Remi NGUYEN VAN | a911e84 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 475 | |
| 476 | // UpstreamNetworkMonitor should receive selected upstream |
| 477 | verify(mUpstreamNetworkMonitor, times(1)).selectPreferredUpstreamType(any()); |
| 478 | verify(mUpstreamNetworkMonitor, times(1)).setCurrentUpstream(upstreamState.network); |
Erik Kline | c438e30 | 2017-07-04 22:02:49 +0900 | [diff] [blame] | 479 | } |
| 480 | |
| 481 | @Test |
Erik Kline | a9cde8b | 2017-06-20 21:18:31 +0900 | [diff] [blame] | 482 | public void failingLocalOnlyHotspotLegacyApBroadcastWithIfaceStatusChanged() throws Exception { |
| 483 | failingLocalOnlyHotspotLegacyApBroadcast(true); |
| 484 | } |
| 485 | |
| 486 | @Test |
| 487 | public void failingLocalOnlyHotspotLegacyApBroadcastSansIfaceStatusChanged() throws Exception { |
| 488 | failingLocalOnlyHotspotLegacyApBroadcast(false); |
| 489 | } |
| 490 | |
| 491 | public void workingLocalOnlyHotspotEnrichedApBroadcast( |
| 492 | boolean emulateInterfaceStatusChanged) throws Exception { |
Erik Kline | 9e22554 | 2017-06-08 17:48:48 +0900 | [diff] [blame] | 493 | // Emulate externally-visible WifiManager effects, causing the |
| 494 | // per-interface state machine to start up, and telling us that |
| 495 | // hotspot mode is to be started. |
Erik Kline | a9cde8b | 2017-06-20 21:18:31 +0900 | [diff] [blame] | 496 | if (emulateInterfaceStatusChanged) { |
Remi NGUYEN VAN | a911e84 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 497 | mTethering.interfaceStatusChanged(TEST_WLAN_IFNAME, true); |
Erik Kline | a9cde8b | 2017-06-20 21:18:31 +0900 | [diff] [blame] | 498 | } |
Remi NGUYEN VAN | a911e84 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 499 | sendWifiApStateChanged(WIFI_AP_STATE_ENABLED, TEST_WLAN_IFNAME, IFACE_IP_MODE_LOCAL_ONLY); |
Erik Kline | 9e22554 | 2017-06-08 17:48:48 +0900 | [diff] [blame] | 500 | mLooper.dispatchAll(); |
| 501 | |
| 502 | verifyInterfaceServingModeStarted(); |
Erik Kline | 465ff3a | 2018-03-09 14:18:02 +0900 | [diff] [blame] | 503 | verifyTetheringBroadcast(TEST_WLAN_IFNAME, EXTRA_AVAILABLE_TETHER); |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 504 | verify(mNMService, times(1)).setIpForwardingEnabled(true); |
| 505 | verify(mNMService, times(1)).startTethering(any(String[].class)); |
| 506 | verifyNoMoreInteractions(mNMService); |
Erik Kline | 216af6d | 2017-04-27 20:57:23 +0900 | [diff] [blame] | 507 | verify(mWifiManager).updateInterfaceIpState( |
Remi NGUYEN VAN | a911e84 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 508 | TEST_WLAN_IFNAME, WifiManager.IFACE_IP_MODE_LOCAL_ONLY); |
Erik Kline | 216af6d | 2017-04-27 20:57:23 +0900 | [diff] [blame] | 509 | verifyNoMoreInteractions(mWifiManager); |
Erik Kline | 465ff3a | 2018-03-09 14:18:02 +0900 | [diff] [blame] | 510 | verifyTetheringBroadcast(TEST_WLAN_IFNAME, EXTRA_ACTIVE_LOCAL_ONLY); |
markchien | a6c7287 | 2018-11-13 18:34:56 +0900 | [diff] [blame] | 511 | verify(mUpstreamNetworkMonitor, times(1)).startObserveAllNetworks(); |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 512 | // TODO: Figure out why this isn't exactly once, for sendTetherStateChangedBroadcast(). |
Erik Kline | 465ff3a | 2018-03-09 14:18:02 +0900 | [diff] [blame] | 513 | assertTrue(1 <= mTetheringDependencies.isTetheringSupportedCalls); |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 514 | |
| 515 | // Emulate externally-visible WifiManager effects, when hotspot mode |
| 516 | // is being torn down. |
| 517 | sendWifiApStateChanged(WifiManager.WIFI_AP_STATE_DISABLED); |
Remi NGUYEN VAN | a911e84 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 518 | mTethering.interfaceRemoved(TEST_WLAN_IFNAME); |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 519 | mLooper.dispatchAll(); |
| 520 | |
Remi NGUYEN VAN | a911e84 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 521 | verify(mNMService, times(1)).untetherInterface(TEST_WLAN_IFNAME); |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 522 | // TODO: Why is {g,s}etInterfaceConfig() called more than once? |
Remi NGUYEN VAN | a911e84 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 523 | verify(mNMService, atLeastOnce()).getInterfaceConfig(TEST_WLAN_IFNAME); |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 524 | verify(mNMService, atLeastOnce()) |
Remi NGUYEN VAN | a911e84 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 525 | .setInterfaceConfig(eq(TEST_WLAN_IFNAME), any(InterfaceConfiguration.class)); |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 526 | verify(mNMService, times(1)).stopTethering(); |
| 527 | verify(mNMService, times(1)).setIpForwardingEnabled(false); |
| 528 | verifyNoMoreInteractions(mNMService); |
Erik Kline | 216af6d | 2017-04-27 20:57:23 +0900 | [diff] [blame] | 529 | verifyNoMoreInteractions(mWifiManager); |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 530 | // Asking for the last error after the per-interface state machine |
| 531 | // has been reaped yields an unknown interface error. |
Erik Kline | 465ff3a | 2018-03-09 14:18:02 +0900 | [diff] [blame] | 532 | assertEquals(TETHER_ERROR_UNKNOWN_IFACE, mTethering.getLastTetherError(TEST_WLAN_IFNAME)); |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 533 | } |
| 534 | |
Remi NGUYEN VAN | a911e84 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 535 | /** |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 536 | * Send CMD_IPV6_TETHER_UPDATE to IpServers as would be done by IPv6TetheringCoordinator. |
Remi NGUYEN VAN | a911e84 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 537 | */ |
| 538 | private void sendIPv6TetherUpdates(NetworkState upstreamState) { |
| 539 | // IPv6TetheringCoordinator must have been notified of downstream |
| 540 | verify(mIPv6TetheringCoordinator, times(1)).addActiveDownstream( |
| 541 | argThat(sm -> sm.linkProperties().getInterfaceName().equals(TEST_USB_IFNAME)), |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 542 | eq(IpServer.STATE_TETHERED)); |
Remi NGUYEN VAN | a911e84 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 543 | |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 544 | for (IpServer ipSrv : |
Remi NGUYEN VAN | a911e84 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 545 | mTetheringDependencies.ipv6CoordinatorNotifyList) { |
Remi NGUYEN VAN | 25a7e4f | 2018-03-09 14:07:18 +0900 | [diff] [blame] | 546 | NetworkState ipv6OnlyState = buildMobileUpstreamState(false, true, false); |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 547 | ipSrv.sendMessage(IpServer.CMD_IPV6_TETHER_UPDATE, 0, 0, |
Remi NGUYEN VAN | a911e84 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 548 | upstreamState.linkProperties.isIPv6Provisioned() |
| 549 | ? ipv6OnlyState.linkProperties |
| 550 | : null); |
| 551 | } |
| 552 | mLooper.dispatchAll(); |
| 553 | } |
| 554 | |
| 555 | private void runUsbTethering(NetworkState upstreamState) { |
| 556 | prepareUsbTethering(upstreamState); |
| 557 | sendUsbBroadcast(true, true, true); |
| 558 | mLooper.dispatchAll(); |
| 559 | } |
| 560 | |
| 561 | @Test |
| 562 | public void workingMobileUsbTethering_IPv4() throws Exception { |
| 563 | NetworkState upstreamState = buildMobileIPv4UpstreamState(); |
| 564 | runUsbTethering(upstreamState); |
| 565 | |
| 566 | verify(mNMService, times(1)).enableNat(TEST_USB_IFNAME, TEST_MOBILE_IFNAME); |
| 567 | verify(mNMService, times(1)).startInterfaceForwarding(TEST_USB_IFNAME, TEST_MOBILE_IFNAME); |
| 568 | |
| 569 | sendIPv6TetherUpdates(upstreamState); |
| 570 | verify(mRouterAdvertisementDaemon, never()).buildNewRa(any(), notNull()); |
Remi NGUYEN VAN | 0e3d0923 | 2018-12-04 12:13:09 +0900 | [diff] [blame] | 571 | verify(mDhcpServer, timeout(DHCPSERVER_START_TIMEOUT_MS).times(1)).start(any()); |
Remi NGUYEN VAN | f5581f8 | 2018-09-28 11:34:15 +0900 | [diff] [blame] | 572 | } |
| 573 | |
| 574 | @Test |
| 575 | public void workingMobileUsbTethering_IPv4LegacyDhcp() { |
| 576 | Settings.Global.putInt(mContentResolver, TETHER_ENABLE_LEGACY_DHCP_SERVER, 1); |
| 577 | mTethering = makeTethering(); |
| 578 | final NetworkState upstreamState = buildMobileIPv4UpstreamState(); |
| 579 | runUsbTethering(upstreamState); |
| 580 | sendIPv6TetherUpdates(upstreamState); |
| 581 | |
Remi NGUYEN VAN | 0e3d0923 | 2018-12-04 12:13:09 +0900 | [diff] [blame] | 582 | verify(mIpServerDependencies, never()).makeDhcpServer(any(), any(), any()); |
Remi NGUYEN VAN | a911e84 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 583 | } |
| 584 | |
| 585 | @Test |
Remi NGUYEN VAN | 25a7e4f | 2018-03-09 14:07:18 +0900 | [diff] [blame] | 586 | public void workingMobileUsbTethering_IPv6() throws Exception { |
| 587 | NetworkState upstreamState = buildMobileIPv6UpstreamState(); |
| 588 | runUsbTethering(upstreamState); |
| 589 | |
| 590 | verify(mNMService, times(1)).enableNat(TEST_USB_IFNAME, TEST_MOBILE_IFNAME); |
| 591 | verify(mNMService, times(1)).startInterfaceForwarding(TEST_USB_IFNAME, TEST_MOBILE_IFNAME); |
| 592 | |
| 593 | sendIPv6TetherUpdates(upstreamState); |
| 594 | verify(mRouterAdvertisementDaemon, times(1)).buildNewRa(any(), notNull()); |
| 595 | verify(mNetd, times(1)).tetherApplyDnsInterfaces(); |
| 596 | } |
| 597 | |
| 598 | @Test |
Remi NGUYEN VAN | a911e84 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 599 | public void workingMobileUsbTethering_DualStack() throws Exception { |
| 600 | NetworkState upstreamState = buildMobileDualStackUpstreamState(); |
| 601 | runUsbTethering(upstreamState); |
| 602 | |
| 603 | verify(mNMService, times(1)).enableNat(TEST_USB_IFNAME, TEST_MOBILE_IFNAME); |
| 604 | verify(mNMService, times(1)).startInterfaceForwarding(TEST_USB_IFNAME, TEST_MOBILE_IFNAME); |
| 605 | verify(mRouterAdvertisementDaemon, times(1)).start(); |
Remi NGUYEN VAN | 0e3d0923 | 2018-12-04 12:13:09 +0900 | [diff] [blame] | 606 | verify(mDhcpServer, timeout(DHCPSERVER_START_TIMEOUT_MS).times(1)).start(any()); |
Remi NGUYEN VAN | a911e84 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 607 | |
| 608 | sendIPv6TetherUpdates(upstreamState); |
| 609 | verify(mRouterAdvertisementDaemon, times(1)).buildNewRa(any(), notNull()); |
| 610 | verify(mNetd, times(1)).tetherApplyDnsInterfaces(); |
| 611 | } |
| 612 | |
Remi NGUYEN VAN | 25a7e4f | 2018-03-09 14:07:18 +0900 | [diff] [blame] | 613 | @Test |
| 614 | public void workingMobileUsbTethering_MultipleUpstreams() throws Exception { |
| 615 | NetworkState upstreamState = buildMobile464xlatUpstreamState(); |
| 616 | runUsbTethering(upstreamState); |
| 617 | |
| 618 | verify(mNMService, times(1)).enableNat(TEST_USB_IFNAME, TEST_XLAT_MOBILE_IFNAME); |
| 619 | verify(mNMService, times(1)).enableNat(TEST_USB_IFNAME, TEST_MOBILE_IFNAME); |
Remi NGUYEN VAN | 0e3d0923 | 2018-12-04 12:13:09 +0900 | [diff] [blame] | 620 | verify(mDhcpServer, timeout(DHCPSERVER_START_TIMEOUT_MS).times(1)).start(any()); |
Remi NGUYEN VAN | 25a7e4f | 2018-03-09 14:07:18 +0900 | [diff] [blame] | 621 | verify(mNMService, times(1)).startInterfaceForwarding(TEST_USB_IFNAME, TEST_MOBILE_IFNAME); |
| 622 | verify(mNMService, times(1)).startInterfaceForwarding(TEST_USB_IFNAME, |
| 623 | TEST_XLAT_MOBILE_IFNAME); |
| 624 | |
| 625 | sendIPv6TetherUpdates(upstreamState); |
| 626 | verify(mRouterAdvertisementDaemon, times(1)).buildNewRa(any(), notNull()); |
| 627 | verify(mNetd, times(1)).tetherApplyDnsInterfaces(); |
| 628 | } |
| 629 | |
| 630 | @Test |
| 631 | public void workingMobileUsbTethering_v6Then464xlat() throws Exception { |
| 632 | // Setup IPv6 |
| 633 | NetworkState upstreamState = buildMobileIPv6UpstreamState(); |
| 634 | runUsbTethering(upstreamState); |
| 635 | |
| 636 | verify(mNMService, times(1)).enableNat(TEST_USB_IFNAME, TEST_MOBILE_IFNAME); |
Remi NGUYEN VAN | 0e3d0923 | 2018-12-04 12:13:09 +0900 | [diff] [blame] | 637 | verify(mDhcpServer, timeout(DHCPSERVER_START_TIMEOUT_MS).times(1)).start(any()); |
Remi NGUYEN VAN | 25a7e4f | 2018-03-09 14:07:18 +0900 | [diff] [blame] | 638 | verify(mNMService, times(1)).startInterfaceForwarding(TEST_USB_IFNAME, TEST_MOBILE_IFNAME); |
| 639 | |
| 640 | // Then 464xlat comes up |
| 641 | upstreamState = buildMobile464xlatUpstreamState(); |
| 642 | when(mUpstreamNetworkMonitor.selectPreferredUpstreamType(any())) |
| 643 | .thenReturn(upstreamState); |
| 644 | |
| 645 | // Upstream LinkProperties changed: UpstreamNetworkMonitor sends EVENT_ON_LINKPROPERTIES. |
| 646 | mTetheringDependencies.upstreamNetworkMonitorMasterSM.sendMessage( |
| 647 | Tethering.TetherMasterSM.EVENT_UPSTREAM_CALLBACK, |
| 648 | UpstreamNetworkMonitor.EVENT_ON_LINKPROPERTIES, |
| 649 | 0, |
| 650 | upstreamState); |
| 651 | mLooper.dispatchAll(); |
| 652 | |
| 653 | // Forwarding is added for 464xlat |
| 654 | verify(mNMService, times(1)).enableNat(TEST_USB_IFNAME, TEST_XLAT_MOBILE_IFNAME); |
| 655 | verify(mNMService, times(1)).startInterfaceForwarding(TEST_USB_IFNAME, |
| 656 | TEST_XLAT_MOBILE_IFNAME); |
| 657 | // Forwarding was not re-added for v6 (still times(1)) |
| 658 | verify(mNMService, times(1)).enableNat(TEST_USB_IFNAME, TEST_MOBILE_IFNAME); |
| 659 | verify(mNMService, times(1)).startInterfaceForwarding(TEST_USB_IFNAME, TEST_MOBILE_IFNAME); |
Remi NGUYEN VAN | f5581f8 | 2018-09-28 11:34:15 +0900 | [diff] [blame] | 660 | // DHCP not restarted on downstream (still times(1)) |
Remi NGUYEN VAN | 0e3d0923 | 2018-12-04 12:13:09 +0900 | [diff] [blame] | 661 | verify(mDhcpServer, timeout(DHCPSERVER_START_TIMEOUT_MS).times(1)).start(any()); |
Remi NGUYEN VAN | 25a7e4f | 2018-03-09 14:07:18 +0900 | [diff] [blame] | 662 | } |
Remi NGUYEN VAN | a911e84 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 663 | |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 664 | @Test |
Erik Kline | 7230290 | 2018-06-14 17:36:40 +0900 | [diff] [blame] | 665 | public void configTetherUpstreamAutomaticIgnoresConfigTetherUpstreamTypes() throws Exception { |
| 666 | when(mResources.getBoolean(com.android.internal.R.bool.config_tether_upstream_automatic)) |
| 667 | .thenReturn(true); |
| 668 | sendConfigurationChanged(); |
| 669 | |
| 670 | // Setup IPv6 |
| 671 | final NetworkState upstreamState = buildMobileIPv6UpstreamState(); |
| 672 | runUsbTethering(upstreamState); |
| 673 | |
| 674 | // UpstreamNetworkMonitor should choose upstream automatically |
| 675 | // (in this specific case: choose the default network). |
| 676 | verify(mUpstreamNetworkMonitor, times(1)).getCurrentPreferredUpstream(); |
| 677 | verify(mUpstreamNetworkMonitor, never()).selectPreferredUpstreamType(any()); |
| 678 | |
| 679 | verify(mUpstreamNetworkMonitor, times(1)).setCurrentUpstream(upstreamState.network); |
| 680 | } |
| 681 | |
| 682 | @Test |
Erik Kline | a9cde8b | 2017-06-20 21:18:31 +0900 | [diff] [blame] | 683 | public void workingLocalOnlyHotspotEnrichedApBroadcastWithIfaceChanged() throws Exception { |
| 684 | workingLocalOnlyHotspotEnrichedApBroadcast(true); |
| 685 | } |
| 686 | |
| 687 | @Test |
| 688 | public void workingLocalOnlyHotspotEnrichedApBroadcastSansIfaceChanged() throws Exception { |
| 689 | workingLocalOnlyHotspotEnrichedApBroadcast(false); |
| 690 | } |
| 691 | |
| 692 | // TODO: Test with and without interfaceStatusChanged(). |
| 693 | @Test |
Erik Kline | 9e22554 | 2017-06-08 17:48:48 +0900 | [diff] [blame] | 694 | public void failingWifiTetheringLegacyApBroadcast() throws Exception { |
Erik Kline | ceb54c6 | 2017-04-18 14:22:25 +0900 | [diff] [blame] | 695 | when(mWifiManager.startSoftAp(any(WifiConfiguration.class))).thenReturn(true); |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 696 | |
| 697 | // Emulate pressing the WiFi tethering button. |
Erik Kline | c438e30 | 2017-07-04 22:02:49 +0900 | [diff] [blame] | 698 | mTethering.startTethering(TETHERING_WIFI, null, false); |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 699 | mLooper.dispatchAll(); |
Erik Kline | ceb54c6 | 2017-04-18 14:22:25 +0900 | [diff] [blame] | 700 | verify(mWifiManager, times(1)).startSoftAp(null); |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 701 | verifyNoMoreInteractions(mWifiManager); |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 702 | verifyNoMoreInteractions(mNMService); |
| 703 | |
| 704 | // Emulate externally-visible WifiManager effects, causing the |
| 705 | // per-interface state machine to start up, and telling us that |
| 706 | // tethering mode is to be started. |
Remi NGUYEN VAN | a911e84 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 707 | mTethering.interfaceStatusChanged(TEST_WLAN_IFNAME, true); |
Erik Kline | 9e22554 | 2017-06-08 17:48:48 +0900 | [diff] [blame] | 708 | sendWifiApStateChanged(WIFI_AP_STATE_ENABLED); |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 709 | mLooper.dispatchAll(); |
| 710 | |
Erik Kline | 465ff3a | 2018-03-09 14:18:02 +0900 | [diff] [blame] | 711 | assertEquals(1, mTetheringDependencies.isTetheringSupportedCalls); |
| 712 | verifyTetheringBroadcast(TEST_WLAN_IFNAME, EXTRA_AVAILABLE_TETHER); |
Erik Kline | 9e22554 | 2017-06-08 17:48:48 +0900 | [diff] [blame] | 713 | verifyNoMoreInteractions(mNMService); |
| 714 | verifyNoMoreInteractions(mWifiManager); |
| 715 | } |
| 716 | |
Erik Kline | a9cde8b | 2017-06-20 21:18:31 +0900 | [diff] [blame] | 717 | // TODO: Test with and without interfaceStatusChanged(). |
Erik Kline | 9e22554 | 2017-06-08 17:48:48 +0900 | [diff] [blame] | 718 | @Test |
| 719 | public void workingWifiTetheringEnrichedApBroadcast() throws Exception { |
Erik Kline | 9e22554 | 2017-06-08 17:48:48 +0900 | [diff] [blame] | 720 | when(mWifiManager.startSoftAp(any(WifiConfiguration.class))).thenReturn(true); |
| 721 | |
| 722 | // Emulate pressing the WiFi tethering button. |
Erik Kline | c438e30 | 2017-07-04 22:02:49 +0900 | [diff] [blame] | 723 | mTethering.startTethering(TETHERING_WIFI, null, false); |
Erik Kline | 9e22554 | 2017-06-08 17:48:48 +0900 | [diff] [blame] | 724 | mLooper.dispatchAll(); |
| 725 | verify(mWifiManager, times(1)).startSoftAp(null); |
| 726 | verifyNoMoreInteractions(mWifiManager); |
Erik Kline | 9e22554 | 2017-06-08 17:48:48 +0900 | [diff] [blame] | 727 | verifyNoMoreInteractions(mNMService); |
| 728 | |
| 729 | // Emulate externally-visible WifiManager effects, causing the |
| 730 | // per-interface state machine to start up, and telling us that |
| 731 | // tethering mode is to be started. |
Remi NGUYEN VAN | a911e84 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 732 | mTethering.interfaceStatusChanged(TEST_WLAN_IFNAME, true); |
| 733 | sendWifiApStateChanged(WIFI_AP_STATE_ENABLED, TEST_WLAN_IFNAME, IFACE_IP_MODE_TETHERED); |
Erik Kline | 9e22554 | 2017-06-08 17:48:48 +0900 | [diff] [blame] | 734 | mLooper.dispatchAll(); |
| 735 | |
| 736 | verifyInterfaceServingModeStarted(); |
Erik Kline | 465ff3a | 2018-03-09 14:18:02 +0900 | [diff] [blame] | 737 | verifyTetheringBroadcast(TEST_WLAN_IFNAME, EXTRA_AVAILABLE_TETHER); |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 738 | verify(mNMService, times(1)).setIpForwardingEnabled(true); |
| 739 | verify(mNMService, times(1)).startTethering(any(String[].class)); |
| 740 | verifyNoMoreInteractions(mNMService); |
Erik Kline | 216af6d | 2017-04-27 20:57:23 +0900 | [diff] [blame] | 741 | verify(mWifiManager).updateInterfaceIpState( |
Remi NGUYEN VAN | a911e84 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 742 | TEST_WLAN_IFNAME, WifiManager.IFACE_IP_MODE_TETHERED); |
Erik Kline | 216af6d | 2017-04-27 20:57:23 +0900 | [diff] [blame] | 743 | verifyNoMoreInteractions(mWifiManager); |
Erik Kline | 465ff3a | 2018-03-09 14:18:02 +0900 | [diff] [blame] | 744 | verifyTetheringBroadcast(TEST_WLAN_IFNAME, EXTRA_ACTIVE_TETHER); |
markchien | a6c7287 | 2018-11-13 18:34:56 +0900 | [diff] [blame] | 745 | verify(mUpstreamNetworkMonitor, times(1)).startObserveAllNetworks(); |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 746 | // In tethering mode, in the default configuration, an explicit request |
| 747 | // for a mobile network is also made. |
Remi NGUYEN VAN | a911e84 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 748 | verify(mUpstreamNetworkMonitor, times(1)).registerMobileNetworkRequest(); |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 749 | // TODO: Figure out why this isn't exactly once, for sendTetherStateChangedBroadcast(). |
Erik Kline | 465ff3a | 2018-03-09 14:18:02 +0900 | [diff] [blame] | 750 | assertTrue(1 <= mTetheringDependencies.isTetheringSupportedCalls); |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 751 | |
| 752 | ///// |
| 753 | // We do not currently emulate any upstream being found. |
| 754 | // |
| 755 | // This is why there are no calls to verify mNMService.enableNat() or |
| 756 | // mNMService.startInterfaceForwarding(). |
| 757 | ///// |
| 758 | |
| 759 | // Emulate pressing the WiFi tethering button. |
Erik Kline | c438e30 | 2017-07-04 22:02:49 +0900 | [diff] [blame] | 760 | mTethering.stopTethering(TETHERING_WIFI); |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 761 | mLooper.dispatchAll(); |
Erik Kline | ceb54c6 | 2017-04-18 14:22:25 +0900 | [diff] [blame] | 762 | verify(mWifiManager, times(1)).stopSoftAp(); |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 763 | verifyNoMoreInteractions(mWifiManager); |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 764 | verifyNoMoreInteractions(mNMService); |
| 765 | |
| 766 | // Emulate externally-visible WifiManager effects, when tethering mode |
| 767 | // is being torn down. |
| 768 | sendWifiApStateChanged(WifiManager.WIFI_AP_STATE_DISABLED); |
Remi NGUYEN VAN | a911e84 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 769 | mTethering.interfaceRemoved(TEST_WLAN_IFNAME); |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 770 | mLooper.dispatchAll(); |
| 771 | |
Remi NGUYEN VAN | a911e84 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 772 | verify(mNMService, times(1)).untetherInterface(TEST_WLAN_IFNAME); |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 773 | // TODO: Why is {g,s}etInterfaceConfig() called more than once? |
Remi NGUYEN VAN | a911e84 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 774 | verify(mNMService, atLeastOnce()).getInterfaceConfig(TEST_WLAN_IFNAME); |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 775 | verify(mNMService, atLeastOnce()) |
Remi NGUYEN VAN | a911e84 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 776 | .setInterfaceConfig(eq(TEST_WLAN_IFNAME), any(InterfaceConfiguration.class)); |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 777 | verify(mNMService, times(1)).stopTethering(); |
| 778 | verify(mNMService, times(1)).setIpForwardingEnabled(false); |
| 779 | verifyNoMoreInteractions(mNMService); |
Erik Kline | 216af6d | 2017-04-27 20:57:23 +0900 | [diff] [blame] | 780 | verifyNoMoreInteractions(mWifiManager); |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 781 | // Asking for the last error after the per-interface state machine |
| 782 | // has been reaped yields an unknown interface error. |
Erik Kline | 465ff3a | 2018-03-09 14:18:02 +0900 | [diff] [blame] | 783 | assertEquals(TETHER_ERROR_UNKNOWN_IFACE, mTethering.getLastTetherError(TEST_WLAN_IFNAME)); |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 784 | } |
| 785 | |
Erik Kline | a9cde8b | 2017-06-20 21:18:31 +0900 | [diff] [blame] | 786 | // TODO: Test with and without interfaceStatusChanged(). |
Erik Kline | 1fdc2e2 | 2017-05-08 17:56:35 +0900 | [diff] [blame] | 787 | @Test |
| 788 | public void failureEnablingIpForwarding() throws Exception { |
Erik Kline | 1fdc2e2 | 2017-05-08 17:56:35 +0900 | [diff] [blame] | 789 | when(mWifiManager.startSoftAp(any(WifiConfiguration.class))).thenReturn(true); |
| 790 | doThrow(new RemoteException()).when(mNMService).setIpForwardingEnabled(true); |
| 791 | |
| 792 | // Emulate pressing the WiFi tethering button. |
Erik Kline | c438e30 | 2017-07-04 22:02:49 +0900 | [diff] [blame] | 793 | mTethering.startTethering(TETHERING_WIFI, null, false); |
Erik Kline | 1fdc2e2 | 2017-05-08 17:56:35 +0900 | [diff] [blame] | 794 | mLooper.dispatchAll(); |
| 795 | verify(mWifiManager, times(1)).startSoftAp(null); |
| 796 | verifyNoMoreInteractions(mWifiManager); |
Erik Kline | 1fdc2e2 | 2017-05-08 17:56:35 +0900 | [diff] [blame] | 797 | verifyNoMoreInteractions(mNMService); |
| 798 | |
| 799 | // Emulate externally-visible WifiManager effects, causing the |
| 800 | // per-interface state machine to start up, and telling us that |
| 801 | // tethering mode is to be started. |
Remi NGUYEN VAN | a911e84 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 802 | mTethering.interfaceStatusChanged(TEST_WLAN_IFNAME, true); |
| 803 | sendWifiApStateChanged(WIFI_AP_STATE_ENABLED, TEST_WLAN_IFNAME, IFACE_IP_MODE_TETHERED); |
Erik Kline | 1fdc2e2 | 2017-05-08 17:56:35 +0900 | [diff] [blame] | 804 | mLooper.dispatchAll(); |
| 805 | |
Remi NGUYEN VAN | d1b51a3 | 2019-01-22 16:13:57 +0900 | [diff] [blame^] | 806 | // We verify get/set called thrice here: twice for setup (on NMService) and once during |
| 807 | // teardown (on Netd) because all events happen over the course of the single |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 808 | // dispatchAll() above. Note that once the IpServer IPv4 address config |
Erik Kline | 472276a | 2018-05-18 16:09:24 +0900 | [diff] [blame] | 809 | // code is refactored the two calls during shutdown will revert to one. |
Remi NGUYEN VAN | a911e84 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 810 | verify(mNMService, times(2)).getInterfaceConfig(TEST_WLAN_IFNAME); |
Remi NGUYEN VAN | d1b51a3 | 2019-01-22 16:13:57 +0900 | [diff] [blame^] | 811 | verify(mNMService, times(2)) |
Remi NGUYEN VAN | a911e84 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 812 | .setInterfaceConfig(eq(TEST_WLAN_IFNAME), any(InterfaceConfiguration.class)); |
Remi NGUYEN VAN | d1b51a3 | 2019-01-22 16:13:57 +0900 | [diff] [blame^] | 813 | verify(mNetd, times(1)).interfaceSetCfg(argThat(p -> TEST_WLAN_IFNAME.equals(p.ifName))); |
Remi NGUYEN VAN | a911e84 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 814 | verify(mNMService, times(1)).tetherInterface(TEST_WLAN_IFNAME); |
Erik Kline | 1fdc2e2 | 2017-05-08 17:56:35 +0900 | [diff] [blame] | 815 | verify(mWifiManager).updateInterfaceIpState( |
Remi NGUYEN VAN | a911e84 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 816 | TEST_WLAN_IFNAME, WifiManager.IFACE_IP_MODE_TETHERED); |
Erik Kline | 465ff3a | 2018-03-09 14:18:02 +0900 | [diff] [blame] | 817 | // TODO: Figure out why this isn't exactly once, for sendTetherStateChangedBroadcast(). |
| 818 | assertTrue(1 <= mTetheringDependencies.isTetheringSupportedCalls); |
| 819 | verifyTetheringBroadcast(TEST_WLAN_IFNAME, EXTRA_AVAILABLE_TETHER); |
Erik Kline | 1fdc2e2 | 2017-05-08 17:56:35 +0900 | [diff] [blame] | 820 | // This is called, but will throw. |
| 821 | verify(mNMService, times(1)).setIpForwardingEnabled(true); |
| 822 | // This never gets called because of the exception thrown above. |
| 823 | verify(mNMService, times(0)).startTethering(any(String[].class)); |
| 824 | // When the master state machine transitions to an error state it tells |
| 825 | // downstream interfaces, which causes us to tell Wi-Fi about the error |
| 826 | // so it can take down AP mode. |
Remi NGUYEN VAN | a911e84 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 827 | verify(mNMService, times(1)).untetherInterface(TEST_WLAN_IFNAME); |
Erik Kline | 1fdc2e2 | 2017-05-08 17:56:35 +0900 | [diff] [blame] | 828 | verify(mWifiManager).updateInterfaceIpState( |
Remi NGUYEN VAN | a911e84 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 829 | TEST_WLAN_IFNAME, WifiManager.IFACE_IP_MODE_CONFIGURATION_ERROR); |
Erik Kline | 1fdc2e2 | 2017-05-08 17:56:35 +0900 | [diff] [blame] | 830 | |
| 831 | verifyNoMoreInteractions(mWifiManager); |
Erik Kline | 1fdc2e2 | 2017-05-08 17:56:35 +0900 | [diff] [blame] | 832 | verifyNoMoreInteractions(mNMService); |
| 833 | } |
| 834 | |
Alexandru-Andrei Rotaru | fa6d5c5 | 2017-07-18 16:49:22 +0100 | [diff] [blame] | 835 | private void userRestrictionsListenerBehaviour( |
| 836 | boolean currentDisallow, boolean nextDisallow, String[] activeTetheringIfacesList, |
| 837 | int expectedInteractionsWithShowNotification) throws Exception { |
| 838 | final int userId = 0; |
| 839 | final Bundle currRestrictions = new Bundle(); |
| 840 | final Bundle newRestrictions = new Bundle(); |
| 841 | Tethering tethering = mock(Tethering.class); |
| 842 | Tethering.TetheringUserRestrictionListener turl = |
| 843 | new Tethering.TetheringUserRestrictionListener(tethering); |
| 844 | |
| 845 | currRestrictions.putBoolean(UserManager.DISALLOW_CONFIG_TETHERING, currentDisallow); |
| 846 | newRestrictions.putBoolean(UserManager.DISALLOW_CONFIG_TETHERING, nextDisallow); |
| 847 | when(tethering.getTetheredIfaces()).thenReturn(activeTetheringIfacesList); |
| 848 | |
| 849 | turl.onUserRestrictionsChanged(userId, newRestrictions, currRestrictions); |
| 850 | |
| 851 | verify(tethering, times(expectedInteractionsWithShowNotification)) |
| 852 | .showTetheredNotification(anyInt(), eq(false)); |
| 853 | |
| 854 | verify(tethering, times(expectedInteractionsWithShowNotification)).untetherAll(); |
| 855 | } |
| 856 | |
| 857 | @Test |
| 858 | public void testDisallowTetheringWhenNoTetheringInterfaceIsActive() throws Exception { |
| 859 | final String[] emptyActiveIfacesList = new String[]{}; |
| 860 | final boolean currDisallow = false; |
| 861 | final boolean nextDisallow = true; |
| 862 | final int expectedInteractionsWithShowNotification = 0; |
| 863 | |
| 864 | userRestrictionsListenerBehaviour(currDisallow, nextDisallow, emptyActiveIfacesList, |
| 865 | expectedInteractionsWithShowNotification); |
| 866 | } |
| 867 | |
| 868 | @Test |
| 869 | public void testDisallowTetheringWhenAtLeastOneTetheringInterfaceIsActive() throws Exception { |
Remi NGUYEN VAN | a911e84 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 870 | final String[] nonEmptyActiveIfacesList = new String[]{TEST_WLAN_IFNAME}; |
Alexandru-Andrei Rotaru | fa6d5c5 | 2017-07-18 16:49:22 +0100 | [diff] [blame] | 871 | final boolean currDisallow = false; |
| 872 | final boolean nextDisallow = true; |
| 873 | final int expectedInteractionsWithShowNotification = 1; |
| 874 | |
| 875 | userRestrictionsListenerBehaviour(currDisallow, nextDisallow, nonEmptyActiveIfacesList, |
| 876 | expectedInteractionsWithShowNotification); |
| 877 | } |
| 878 | |
| 879 | @Test |
| 880 | public void testAllowTetheringWhenNoTetheringInterfaceIsActive() throws Exception { |
| 881 | final String[] nonEmptyActiveIfacesList = new String[]{}; |
| 882 | final boolean currDisallow = true; |
| 883 | final boolean nextDisallow = false; |
| 884 | final int expectedInteractionsWithShowNotification = 0; |
| 885 | |
| 886 | userRestrictionsListenerBehaviour(currDisallow, nextDisallow, nonEmptyActiveIfacesList, |
| 887 | expectedInteractionsWithShowNotification); |
| 888 | } |
| 889 | |
| 890 | @Test |
| 891 | public void testAllowTetheringWhenAtLeastOneTetheringInterfaceIsActive() throws Exception { |
Remi NGUYEN VAN | a911e84 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 892 | final String[] nonEmptyActiveIfacesList = new String[]{TEST_WLAN_IFNAME}; |
Alexandru-Andrei Rotaru | fa6d5c5 | 2017-07-18 16:49:22 +0100 | [diff] [blame] | 893 | final boolean currDisallow = true; |
| 894 | final boolean nextDisallow = false; |
| 895 | final int expectedInteractionsWithShowNotification = 0; |
| 896 | |
| 897 | userRestrictionsListenerBehaviour(currDisallow, nextDisallow, nonEmptyActiveIfacesList, |
| 898 | expectedInteractionsWithShowNotification); |
| 899 | } |
| 900 | |
| 901 | @Test |
| 902 | public void testDisallowTetheringUnchanged() throws Exception { |
Remi NGUYEN VAN | a911e84 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 903 | final String[] nonEmptyActiveIfacesList = new String[]{TEST_WLAN_IFNAME}; |
Alexandru-Andrei Rotaru | fa6d5c5 | 2017-07-18 16:49:22 +0100 | [diff] [blame] | 904 | final int expectedInteractionsWithShowNotification = 0; |
| 905 | boolean currDisallow = true; |
| 906 | boolean nextDisallow = true; |
| 907 | |
| 908 | userRestrictionsListenerBehaviour(currDisallow, nextDisallow, nonEmptyActiveIfacesList, |
| 909 | expectedInteractionsWithShowNotification); |
| 910 | |
| 911 | currDisallow = false; |
| 912 | nextDisallow = false; |
| 913 | |
| 914 | userRestrictionsListenerBehaviour(currDisallow, nextDisallow, nonEmptyActiveIfacesList, |
| 915 | expectedInteractionsWithShowNotification); |
| 916 | } |
| 917 | |
| 918 | |
Erik Kline | 1fdc2e2 | 2017-05-08 17:56:35 +0900 | [diff] [blame] | 919 | // 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] | 920 | // already operating tethering mode interface. |
Christopher Wiley | 497c147 | 2016-10-11 13:26:03 -0700 | [diff] [blame] | 921 | } |