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