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