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(); |
| 358 | assertTrue(mTethering.isTetherProvisioningRequired()); |
| 359 | } |
| 360 | |
| 361 | @Test |
| 362 | public void toleratesCarrierConfigManagerMissing() { |
| 363 | setupForRequiredProvisioning(); |
| 364 | when(mContext.getSystemService(Context.CARRIER_CONFIG_SERVICE)) |
| 365 | .thenReturn(null); |
| 366 | // Couldn't get the CarrierConfigManager, but still had a declared provisioning app. |
| 367 | // We therefore still require provisioning. |
| 368 | assertTrue(mTethering.isTetherProvisioningRequired()); |
| 369 | } |
| 370 | |
| 371 | @Test |
| 372 | public void toleratesCarrierConfigMissing() { |
| 373 | setupForRequiredProvisioning(); |
| 374 | when(mCarrierConfigManager.getConfig()).thenReturn(null); |
| 375 | // We still have a provisioning app configured, so still require provisioning. |
| 376 | assertTrue(mTethering.isTetherProvisioningRequired()); |
| 377 | } |
| 378 | |
| 379 | @Test |
| 380 | public void provisioningNotRequiredWhenAppNotFound() { |
| 381 | setupForRequiredProvisioning(); |
| 382 | when(mResources.getStringArray( |
| 383 | com.android.internal.R.array.config_mobile_hotspot_provision_app)) |
| 384 | .thenReturn(null); |
| 385 | assertTrue(!mTethering.isTetherProvisioningRequired()); |
| 386 | when(mResources.getStringArray( |
| 387 | com.android.internal.R.array.config_mobile_hotspot_provision_app)) |
| 388 | .thenReturn(new String[] {"malformedApp"}); |
| 389 | assertTrue(!mTethering.isTetherProvisioningRequired()); |
| 390 | } |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 391 | |
| 392 | private void sendWifiApStateChanged(int state) { |
| 393 | final Intent intent = new Intent(WifiManager.WIFI_AP_STATE_CHANGED_ACTION); |
Erik Kline | 2efb827 | 2017-05-31 15:53:53 +0900 | [diff] [blame] | 394 | intent.putExtra(EXTRA_WIFI_AP_STATE, state); |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 395 | mServiceContext.sendStickyBroadcastAsUser(intent, UserHandle.ALL); |
| 396 | } |
| 397 | |
Erik Kline | 2efb827 | 2017-05-31 15:53:53 +0900 | [diff] [blame] | 398 | private void sendWifiApStateChanged(int state, String ifname, int ipmode) { |
| 399 | final Intent intent = new Intent(WifiManager.WIFI_AP_STATE_CHANGED_ACTION); |
| 400 | intent.putExtra(EXTRA_WIFI_AP_STATE, state); |
| 401 | intent.putExtra(EXTRA_WIFI_AP_INTERFACE_NAME, ifname); |
| 402 | intent.putExtra(EXTRA_WIFI_AP_MODE, ipmode); |
| 403 | mServiceContext.sendStickyBroadcastAsUser(intent, UserHandle.ALL); |
| 404 | } |
| 405 | |
Erik Kline | c438e30 | 2017-07-04 22:02:49 +0900 | [diff] [blame] | 406 | private void sendUsbBroadcast(boolean connected, boolean configured, boolean rndisFunction) { |
| 407 | final Intent intent = new Intent(UsbManager.ACTION_USB_STATE); |
| 408 | intent.putExtra(USB_CONNECTED, connected); |
| 409 | intent.putExtra(USB_CONFIGURED, configured); |
| 410 | intent.putExtra(USB_FUNCTION_RNDIS, rndisFunction); |
| 411 | mServiceContext.sendStickyBroadcastAsUser(intent, UserHandle.ALL); |
| 412 | } |
| 413 | |
Erik Kline | 9e22554 | 2017-06-08 17:48:48 +0900 | [diff] [blame] | 414 | private void verifyInterfaceServingModeStarted() throws Exception { |
Remi NGUYEN VAN | 5d0dc45 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 415 | verify(mNMService, times(1)).getInterfaceConfig(TEST_WLAN_IFNAME); |
Erik Kline | 8351faa | 2017-04-17 16:47:23 +0900 | [diff] [blame] | 416 | verify(mNMService, times(1)) |
Remi NGUYEN VAN | 5d0dc45 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 417 | .setInterfaceConfig(eq(TEST_WLAN_IFNAME), any(InterfaceConfiguration.class)); |
| 418 | verify(mNMService, times(1)).tetherInterface(TEST_WLAN_IFNAME); |
Erik Kline | 8351faa | 2017-04-17 16:47:23 +0900 | [diff] [blame] | 419 | } |
| 420 | |
| 421 | private void verifyTetheringBroadcast(String ifname, String whichExtra) { |
| 422 | // Verify that ifname is in the whichExtra array of the tether state changed broadcast. |
| 423 | final Intent bcast = mIntents.get(0); |
Erik Kline | 7a26ba3 | 2018-03-09 14:18:02 +0900 | [diff] [blame] | 424 | assertEquals(ACTION_TETHER_STATE_CHANGED, bcast.getAction()); |
Erik Kline | 8351faa | 2017-04-17 16:47:23 +0900 | [diff] [blame] | 425 | final ArrayList<String> ifnames = bcast.getStringArrayListExtra(whichExtra); |
| 426 | assertTrue(ifnames.contains(ifname)); |
| 427 | mIntents.remove(bcast); |
| 428 | } |
| 429 | |
Erik Kline | a9cde8b | 2017-06-20 21:18:31 +0900 | [diff] [blame] | 430 | public void failingLocalOnlyHotspotLegacyApBroadcast( |
| 431 | boolean emulateInterfaceStatusChanged) throws Exception { |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 432 | // Emulate externally-visible WifiManager effects, causing the |
| 433 | // per-interface state machine to start up, and telling us that |
| 434 | // hotspot mode is to be started. |
Erik Kline | a9cde8b | 2017-06-20 21:18:31 +0900 | [diff] [blame] | 435 | if (emulateInterfaceStatusChanged) { |
Remi NGUYEN VAN | 5d0dc45 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 436 | mTethering.interfaceStatusChanged(TEST_WLAN_IFNAME, true); |
Erik Kline | a9cde8b | 2017-06-20 21:18:31 +0900 | [diff] [blame] | 437 | } |
Erik Kline | 9e22554 | 2017-06-08 17:48:48 +0900 | [diff] [blame] | 438 | sendWifiApStateChanged(WIFI_AP_STATE_ENABLED); |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 439 | mLooper.dispatchAll(); |
| 440 | |
Erik Kline | a9cde8b | 2017-06-20 21:18:31 +0900 | [diff] [blame] | 441 | // If, and only if, Tethering received an interface status changed |
| 442 | // then it creates a TetherInterfaceStateMachine and sends out a |
| 443 | // broadcast indicating that the interface is "available". |
| 444 | if (emulateInterfaceStatusChanged) { |
Erik Kline | 7a26ba3 | 2018-03-09 14:18:02 +0900 | [diff] [blame] | 445 | assertEquals(1, mTetheringDependencies.isTetheringSupportedCalls); |
| 446 | verifyTetheringBroadcast(TEST_WLAN_IFNAME, EXTRA_AVAILABLE_TETHER); |
Erik Kline | a9cde8b | 2017-06-20 21:18:31 +0900 | [diff] [blame] | 447 | } |
Erik Kline | 9e22554 | 2017-06-08 17:48:48 +0900 | [diff] [blame] | 448 | verifyNoMoreInteractions(mNMService); |
| 449 | verifyNoMoreInteractions(mWifiManager); |
| 450 | } |
| 451 | |
Remi NGUYEN VAN | 5d0dc45 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 452 | private void prepareUsbTethering(NetworkState upstreamState) { |
Remi NGUYEN VAN | 5d0dc45 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 453 | when(mUpstreamNetworkMonitor.selectPreferredUpstreamType(any())) |
| 454 | .thenReturn(upstreamState); |
Erik Kline | c438e30 | 2017-07-04 22:02:49 +0900 | [diff] [blame] | 455 | |
| 456 | // Emulate pressing the USB tethering button in Settings UI. |
| 457 | mTethering.startTethering(TETHERING_USB, null, false); |
| 458 | mLooper.dispatchAll(); |
Jerry Zhang | 327b809 | 2018-01-09 17:53:04 -0800 | [diff] [blame] | 459 | verify(mUsbManager, times(1)).setCurrentFunctions(UsbManager.FUNCTION_RNDIS); |
Erik Kline | c438e30 | 2017-07-04 22:02:49 +0900 | [diff] [blame] | 460 | |
Remi NGUYEN VAN | 5d0dc45 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 461 | mTethering.interfaceStatusChanged(TEST_USB_IFNAME, true); |
| 462 | } |
| 463 | |
| 464 | @Test |
| 465 | public void testUsbConfiguredBroadcastStartsTethering() throws Exception { |
| 466 | NetworkState upstreamState = buildMobileIPv4UpstreamState(); |
| 467 | prepareUsbTethering(upstreamState); |
| 468 | |
Erik Kline | c438e30 | 2017-07-04 22:02:49 +0900 | [diff] [blame] | 469 | // This should produce no activity of any kind. |
Erik Kline | c438e30 | 2017-07-04 22:02:49 +0900 | [diff] [blame] | 470 | verifyNoMoreInteractions(mNMService); |
| 471 | |
| 472 | // Pretend we then receive USB configured broadcast. |
| 473 | sendUsbBroadcast(true, true, true); |
| 474 | mLooper.dispatchAll(); |
| 475 | // Now we should see the start of tethering mechanics (in this case: |
| 476 | // tetherMatchingInterfaces() which starts by fetching all interfaces). |
| 477 | verify(mNMService, times(1)).listInterfaces(); |
Remi NGUYEN VAN | 5d0dc45 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 478 | |
| 479 | // UpstreamNetworkMonitor should receive selected upstream |
| 480 | verify(mUpstreamNetworkMonitor, times(1)).selectPreferredUpstreamType(any()); |
| 481 | verify(mUpstreamNetworkMonitor, times(1)).setCurrentUpstream(upstreamState.network); |
Erik Kline | c438e30 | 2017-07-04 22:02:49 +0900 | [diff] [blame] | 482 | } |
| 483 | |
| 484 | @Test |
Erik Kline | a9cde8b | 2017-06-20 21:18:31 +0900 | [diff] [blame] | 485 | public void failingLocalOnlyHotspotLegacyApBroadcastWithIfaceStatusChanged() throws Exception { |
| 486 | failingLocalOnlyHotspotLegacyApBroadcast(true); |
| 487 | } |
| 488 | |
| 489 | @Test |
| 490 | public void failingLocalOnlyHotspotLegacyApBroadcastSansIfaceStatusChanged() throws Exception { |
| 491 | failingLocalOnlyHotspotLegacyApBroadcast(false); |
| 492 | } |
| 493 | |
| 494 | public void workingLocalOnlyHotspotEnrichedApBroadcast( |
| 495 | boolean emulateInterfaceStatusChanged) throws Exception { |
Erik Kline | 9e22554 | 2017-06-08 17:48:48 +0900 | [diff] [blame] | 496 | // Emulate externally-visible WifiManager effects, causing the |
| 497 | // per-interface state machine to start up, and telling us that |
| 498 | // hotspot mode is to be started. |
Erik Kline | a9cde8b | 2017-06-20 21:18:31 +0900 | [diff] [blame] | 499 | if (emulateInterfaceStatusChanged) { |
Remi NGUYEN VAN | 5d0dc45 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 500 | mTethering.interfaceStatusChanged(TEST_WLAN_IFNAME, true); |
Erik Kline | a9cde8b | 2017-06-20 21:18:31 +0900 | [diff] [blame] | 501 | } |
Remi NGUYEN VAN | 5d0dc45 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 502 | 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] | 503 | mLooper.dispatchAll(); |
| 504 | |
| 505 | verifyInterfaceServingModeStarted(); |
Erik Kline | 7a26ba3 | 2018-03-09 14:18:02 +0900 | [diff] [blame] | 506 | verifyTetheringBroadcast(TEST_WLAN_IFNAME, EXTRA_AVAILABLE_TETHER); |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 507 | verify(mNMService, times(1)).setIpForwardingEnabled(true); |
| 508 | verify(mNMService, times(1)).startTethering(any(String[].class)); |
| 509 | verifyNoMoreInteractions(mNMService); |
Erik Kline | 216af6d | 2017-04-27 20:57:23 +0900 | [diff] [blame] | 510 | verify(mWifiManager).updateInterfaceIpState( |
Remi NGUYEN VAN | 5d0dc45 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 511 | TEST_WLAN_IFNAME, WifiManager.IFACE_IP_MODE_LOCAL_ONLY); |
Erik Kline | 216af6d | 2017-04-27 20:57:23 +0900 | [diff] [blame] | 512 | verifyNoMoreInteractions(mWifiManager); |
Erik Kline | 7a26ba3 | 2018-03-09 14:18:02 +0900 | [diff] [blame] | 513 | verifyTetheringBroadcast(TEST_WLAN_IFNAME, EXTRA_ACTIVE_LOCAL_ONLY); |
Remi NGUYEN VAN | 5d0dc45 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 514 | verify(mUpstreamNetworkMonitor, times(1)).start(); |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 515 | // TODO: Figure out why this isn't exactly once, for sendTetherStateChangedBroadcast(). |
Erik Kline | 7a26ba3 | 2018-03-09 14:18:02 +0900 | [diff] [blame] | 516 | assertTrue(1 <= mTetheringDependencies.isTetheringSupportedCalls); |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 517 | |
| 518 | // Emulate externally-visible WifiManager effects, when hotspot mode |
| 519 | // is being torn down. |
| 520 | sendWifiApStateChanged(WifiManager.WIFI_AP_STATE_DISABLED); |
Remi NGUYEN VAN | 5d0dc45 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 521 | mTethering.interfaceRemoved(TEST_WLAN_IFNAME); |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 522 | mLooper.dispatchAll(); |
| 523 | |
Remi NGUYEN VAN | 5d0dc45 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 524 | verify(mNMService, times(1)).untetherInterface(TEST_WLAN_IFNAME); |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 525 | // TODO: Why is {g,s}etInterfaceConfig() called more than once? |
Remi NGUYEN VAN | 5d0dc45 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 526 | verify(mNMService, atLeastOnce()).getInterfaceConfig(TEST_WLAN_IFNAME); |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 527 | verify(mNMService, atLeastOnce()) |
Remi NGUYEN VAN | 5d0dc45 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 528 | .setInterfaceConfig(eq(TEST_WLAN_IFNAME), any(InterfaceConfiguration.class)); |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 529 | verify(mNMService, times(1)).stopTethering(); |
| 530 | verify(mNMService, times(1)).setIpForwardingEnabled(false); |
| 531 | verifyNoMoreInteractions(mNMService); |
Erik Kline | 216af6d | 2017-04-27 20:57:23 +0900 | [diff] [blame] | 532 | verifyNoMoreInteractions(mWifiManager); |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 533 | // Asking for the last error after the per-interface state machine |
| 534 | // has been reaped yields an unknown interface error. |
Erik Kline | 7a26ba3 | 2018-03-09 14:18:02 +0900 | [diff] [blame] | 535 | assertEquals(TETHER_ERROR_UNKNOWN_IFACE, mTethering.getLastTetherError(TEST_WLAN_IFNAME)); |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 536 | } |
| 537 | |
Remi NGUYEN VAN | 5d0dc45 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 538 | /** |
| 539 | * Send CMD_IPV6_TETHER_UPDATE to TISMs as would be done by IPv6TetheringCoordinator. |
| 540 | */ |
| 541 | private void sendIPv6TetherUpdates(NetworkState upstreamState) { |
| 542 | // IPv6TetheringCoordinator must have been notified of downstream |
| 543 | verify(mIPv6TetheringCoordinator, times(1)).addActiveDownstream( |
| 544 | argThat(sm -> sm.linkProperties().getInterfaceName().equals(TEST_USB_IFNAME)), |
| 545 | eq(IControlsTethering.STATE_TETHERED)); |
| 546 | |
| 547 | for (TetherInterfaceStateMachine tism : |
| 548 | mTetheringDependencies.ipv6CoordinatorNotifyList) { |
Remi NGUYEN VAN | 6c02f99 | 2018-03-09 14:07:18 +0900 | [diff] [blame] | 549 | NetworkState ipv6OnlyState = buildMobileUpstreamState(false, true, false); |
Remi NGUYEN VAN | 5d0dc45 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 550 | tism.sendMessage(TetherInterfaceStateMachine.CMD_IPV6_TETHER_UPDATE, 0, 0, |
| 551 | upstreamState.linkProperties.isIPv6Provisioned() |
| 552 | ? ipv6OnlyState.linkProperties |
| 553 | : null); |
| 554 | } |
| 555 | mLooper.dispatchAll(); |
| 556 | } |
| 557 | |
| 558 | private void runUsbTethering(NetworkState upstreamState) { |
| 559 | prepareUsbTethering(upstreamState); |
| 560 | sendUsbBroadcast(true, true, true); |
| 561 | mLooper.dispatchAll(); |
| 562 | } |
| 563 | |
| 564 | @Test |
| 565 | public void workingMobileUsbTethering_IPv4() throws Exception { |
| 566 | NetworkState upstreamState = buildMobileIPv4UpstreamState(); |
| 567 | runUsbTethering(upstreamState); |
| 568 | |
| 569 | verify(mNMService, times(1)).enableNat(TEST_USB_IFNAME, TEST_MOBILE_IFNAME); |
| 570 | verify(mNMService, times(1)).startInterfaceForwarding(TEST_USB_IFNAME, TEST_MOBILE_IFNAME); |
| 571 | |
| 572 | sendIPv6TetherUpdates(upstreamState); |
| 573 | verify(mRouterAdvertisementDaemon, never()).buildNewRa(any(), notNull()); |
| 574 | } |
| 575 | |
| 576 | @Test |
Remi NGUYEN VAN | 6c02f99 | 2018-03-09 14:07:18 +0900 | [diff] [blame] | 577 | public void workingMobileUsbTethering_IPv6() throws Exception { |
| 578 | NetworkState upstreamState = buildMobileIPv6UpstreamState(); |
| 579 | runUsbTethering(upstreamState); |
| 580 | |
| 581 | verify(mNMService, times(1)).enableNat(TEST_USB_IFNAME, TEST_MOBILE_IFNAME); |
| 582 | verify(mNMService, times(1)).startInterfaceForwarding(TEST_USB_IFNAME, TEST_MOBILE_IFNAME); |
| 583 | |
| 584 | sendIPv6TetherUpdates(upstreamState); |
| 585 | verify(mRouterAdvertisementDaemon, times(1)).buildNewRa(any(), notNull()); |
| 586 | verify(mNetd, times(1)).tetherApplyDnsInterfaces(); |
| 587 | } |
| 588 | |
| 589 | @Test |
Remi NGUYEN VAN | 5d0dc45 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 590 | public void workingMobileUsbTethering_DualStack() throws Exception { |
| 591 | NetworkState upstreamState = buildMobileDualStackUpstreamState(); |
| 592 | runUsbTethering(upstreamState); |
| 593 | |
| 594 | verify(mNMService, times(1)).enableNat(TEST_USB_IFNAME, TEST_MOBILE_IFNAME); |
| 595 | verify(mNMService, times(1)).startInterfaceForwarding(TEST_USB_IFNAME, TEST_MOBILE_IFNAME); |
| 596 | verify(mRouterAdvertisementDaemon, times(1)).start(); |
| 597 | |
| 598 | sendIPv6TetherUpdates(upstreamState); |
| 599 | verify(mRouterAdvertisementDaemon, times(1)).buildNewRa(any(), notNull()); |
| 600 | verify(mNetd, times(1)).tetherApplyDnsInterfaces(); |
| 601 | } |
| 602 | |
Remi NGUYEN VAN | 6c02f99 | 2018-03-09 14:07:18 +0900 | [diff] [blame] | 603 | @Test |
| 604 | public void workingMobileUsbTethering_MultipleUpstreams() throws Exception { |
| 605 | NetworkState upstreamState = buildMobile464xlatUpstreamState(); |
| 606 | runUsbTethering(upstreamState); |
| 607 | |
| 608 | verify(mNMService, times(1)).enableNat(TEST_USB_IFNAME, TEST_XLAT_MOBILE_IFNAME); |
| 609 | verify(mNMService, times(1)).enableNat(TEST_USB_IFNAME, TEST_MOBILE_IFNAME); |
| 610 | verify(mNMService, times(1)).startInterfaceForwarding(TEST_USB_IFNAME, TEST_MOBILE_IFNAME); |
| 611 | verify(mNMService, times(1)).startInterfaceForwarding(TEST_USB_IFNAME, |
| 612 | TEST_XLAT_MOBILE_IFNAME); |
| 613 | |
| 614 | sendIPv6TetherUpdates(upstreamState); |
| 615 | verify(mRouterAdvertisementDaemon, times(1)).buildNewRa(any(), notNull()); |
| 616 | verify(mNetd, times(1)).tetherApplyDnsInterfaces(); |
| 617 | } |
| 618 | |
| 619 | @Test |
| 620 | public void workingMobileUsbTethering_v6Then464xlat() throws Exception { |
| 621 | // Setup IPv6 |
| 622 | NetworkState upstreamState = buildMobileIPv6UpstreamState(); |
| 623 | runUsbTethering(upstreamState); |
| 624 | |
| 625 | // Then 464xlat comes up |
| 626 | upstreamState = buildMobile464xlatUpstreamState(); |
| 627 | when(mUpstreamNetworkMonitor.selectPreferredUpstreamType(any())) |
| 628 | .thenReturn(upstreamState); |
| 629 | |
| 630 | // Upstream LinkProperties changed: UpstreamNetworkMonitor sends EVENT_ON_LINKPROPERTIES. |
| 631 | mTetheringDependencies.upstreamNetworkMonitorMasterSM.sendMessage( |
| 632 | Tethering.TetherMasterSM.EVENT_UPSTREAM_CALLBACK, |
| 633 | UpstreamNetworkMonitor.EVENT_ON_LINKPROPERTIES, |
| 634 | 0, |
| 635 | upstreamState); |
| 636 | mLooper.dispatchAll(); |
| 637 | |
| 638 | // Forwarding is added for 464xlat, and was still added only once for v6 |
| 639 | verify(mNMService, times(1)).enableNat(TEST_USB_IFNAME, TEST_XLAT_MOBILE_IFNAME); |
| 640 | verify(mNMService, times(1)).enableNat(TEST_USB_IFNAME, TEST_MOBILE_IFNAME); |
| 641 | verify(mNMService, times(1)).startInterfaceForwarding(TEST_USB_IFNAME, TEST_MOBILE_IFNAME); |
| 642 | verify(mNMService, times(1)).startInterfaceForwarding(TEST_USB_IFNAME, |
| 643 | TEST_XLAT_MOBILE_IFNAME); |
| 644 | } |
Remi NGUYEN VAN | 5d0dc45 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 645 | |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 646 | @Test |
Erik Kline | a9cde8b | 2017-06-20 21:18:31 +0900 | [diff] [blame] | 647 | public void workingLocalOnlyHotspotEnrichedApBroadcastWithIfaceChanged() throws Exception { |
| 648 | workingLocalOnlyHotspotEnrichedApBroadcast(true); |
| 649 | } |
| 650 | |
| 651 | @Test |
| 652 | public void workingLocalOnlyHotspotEnrichedApBroadcastSansIfaceChanged() throws Exception { |
| 653 | workingLocalOnlyHotspotEnrichedApBroadcast(false); |
| 654 | } |
| 655 | |
| 656 | // TODO: Test with and without interfaceStatusChanged(). |
| 657 | @Test |
Erik Kline | 9e22554 | 2017-06-08 17:48:48 +0900 | [diff] [blame] | 658 | public void failingWifiTetheringLegacyApBroadcast() throws Exception { |
Erik Kline | ceb54c6 | 2017-04-18 14:22:25 +0900 | [diff] [blame] | 659 | when(mWifiManager.startSoftAp(any(WifiConfiguration.class))).thenReturn(true); |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 660 | |
| 661 | // Emulate pressing the WiFi tethering button. |
Erik Kline | c438e30 | 2017-07-04 22:02:49 +0900 | [diff] [blame] | 662 | mTethering.startTethering(TETHERING_WIFI, null, false); |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 663 | mLooper.dispatchAll(); |
Erik Kline | ceb54c6 | 2017-04-18 14:22:25 +0900 | [diff] [blame] | 664 | verify(mWifiManager, times(1)).startSoftAp(null); |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 665 | verifyNoMoreInteractions(mWifiManager); |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 666 | verifyNoMoreInteractions(mNMService); |
| 667 | |
| 668 | // Emulate externally-visible WifiManager effects, causing the |
| 669 | // per-interface state machine to start up, and telling us that |
| 670 | // tethering mode is to be started. |
Remi NGUYEN VAN | 5d0dc45 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 671 | mTethering.interfaceStatusChanged(TEST_WLAN_IFNAME, true); |
Erik Kline | 9e22554 | 2017-06-08 17:48:48 +0900 | [diff] [blame] | 672 | sendWifiApStateChanged(WIFI_AP_STATE_ENABLED); |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 673 | mLooper.dispatchAll(); |
| 674 | |
Erik Kline | 7a26ba3 | 2018-03-09 14:18:02 +0900 | [diff] [blame] | 675 | assertEquals(1, mTetheringDependencies.isTetheringSupportedCalls); |
| 676 | verifyTetheringBroadcast(TEST_WLAN_IFNAME, EXTRA_AVAILABLE_TETHER); |
Erik Kline | 9e22554 | 2017-06-08 17:48:48 +0900 | [diff] [blame] | 677 | verifyNoMoreInteractions(mNMService); |
| 678 | verifyNoMoreInteractions(mWifiManager); |
| 679 | } |
| 680 | |
Erik Kline | a9cde8b | 2017-06-20 21:18:31 +0900 | [diff] [blame] | 681 | // TODO: Test with and without interfaceStatusChanged(). |
Erik Kline | 9e22554 | 2017-06-08 17:48:48 +0900 | [diff] [blame] | 682 | @Test |
| 683 | public void workingWifiTetheringEnrichedApBroadcast() throws Exception { |
Erik Kline | 9e22554 | 2017-06-08 17:48:48 +0900 | [diff] [blame] | 684 | when(mWifiManager.startSoftAp(any(WifiConfiguration.class))).thenReturn(true); |
| 685 | |
| 686 | // Emulate pressing the WiFi tethering button. |
Erik Kline | c438e30 | 2017-07-04 22:02:49 +0900 | [diff] [blame] | 687 | mTethering.startTethering(TETHERING_WIFI, null, false); |
Erik Kline | 9e22554 | 2017-06-08 17:48:48 +0900 | [diff] [blame] | 688 | mLooper.dispatchAll(); |
| 689 | verify(mWifiManager, times(1)).startSoftAp(null); |
| 690 | verifyNoMoreInteractions(mWifiManager); |
Erik Kline | 9e22554 | 2017-06-08 17:48:48 +0900 | [diff] [blame] | 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 | 5d0dc45 | 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 | 9e22554 | 2017-06-08 17:48:48 +0900 | [diff] [blame] | 698 | mLooper.dispatchAll(); |
| 699 | |
| 700 | verifyInterfaceServingModeStarted(); |
Erik Kline | 7a26ba3 | 2018-03-09 14:18:02 +0900 | [diff] [blame] | 701 | verifyTetheringBroadcast(TEST_WLAN_IFNAME, EXTRA_AVAILABLE_TETHER); |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 702 | verify(mNMService, times(1)).setIpForwardingEnabled(true); |
| 703 | verify(mNMService, times(1)).startTethering(any(String[].class)); |
| 704 | verifyNoMoreInteractions(mNMService); |
Erik Kline | 216af6d | 2017-04-27 20:57:23 +0900 | [diff] [blame] | 705 | verify(mWifiManager).updateInterfaceIpState( |
Remi NGUYEN VAN | 5d0dc45 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 706 | TEST_WLAN_IFNAME, WifiManager.IFACE_IP_MODE_TETHERED); |
Erik Kline | 216af6d | 2017-04-27 20:57:23 +0900 | [diff] [blame] | 707 | verifyNoMoreInteractions(mWifiManager); |
Erik Kline | 7a26ba3 | 2018-03-09 14:18:02 +0900 | [diff] [blame] | 708 | verifyTetheringBroadcast(TEST_WLAN_IFNAME, EXTRA_ACTIVE_TETHER); |
Remi NGUYEN VAN | 5d0dc45 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 709 | verify(mUpstreamNetworkMonitor, times(1)).start(); |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 710 | // In tethering mode, in the default configuration, an explicit request |
| 711 | // for a mobile network is also made. |
Remi NGUYEN VAN | 5d0dc45 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 712 | verify(mUpstreamNetworkMonitor, times(1)).registerMobileNetworkRequest(); |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 713 | // TODO: Figure out why this isn't exactly once, for sendTetherStateChangedBroadcast(). |
Erik Kline | 7a26ba3 | 2018-03-09 14:18:02 +0900 | [diff] [blame] | 714 | assertTrue(1 <= mTetheringDependencies.isTetheringSupportedCalls); |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 715 | |
| 716 | ///// |
| 717 | // We do not currently emulate any upstream being found. |
| 718 | // |
| 719 | // This is why there are no calls to verify mNMService.enableNat() or |
| 720 | // mNMService.startInterfaceForwarding(). |
| 721 | ///// |
| 722 | |
| 723 | // Emulate pressing the WiFi tethering button. |
Erik Kline | c438e30 | 2017-07-04 22:02:49 +0900 | [diff] [blame] | 724 | mTethering.stopTethering(TETHERING_WIFI); |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 725 | mLooper.dispatchAll(); |
Erik Kline | ceb54c6 | 2017-04-18 14:22:25 +0900 | [diff] [blame] | 726 | verify(mWifiManager, times(1)).stopSoftAp(); |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 727 | verifyNoMoreInteractions(mWifiManager); |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 728 | verifyNoMoreInteractions(mNMService); |
| 729 | |
| 730 | // Emulate externally-visible WifiManager effects, when tethering mode |
| 731 | // is being torn down. |
| 732 | sendWifiApStateChanged(WifiManager.WIFI_AP_STATE_DISABLED); |
Remi NGUYEN VAN | 5d0dc45 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 733 | mTethering.interfaceRemoved(TEST_WLAN_IFNAME); |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 734 | mLooper.dispatchAll(); |
| 735 | |
Remi NGUYEN VAN | 5d0dc45 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 736 | verify(mNMService, times(1)).untetherInterface(TEST_WLAN_IFNAME); |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 737 | // TODO: Why is {g,s}etInterfaceConfig() called more than once? |
Remi NGUYEN VAN | 5d0dc45 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 738 | verify(mNMService, atLeastOnce()).getInterfaceConfig(TEST_WLAN_IFNAME); |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 739 | verify(mNMService, atLeastOnce()) |
Remi NGUYEN VAN | 5d0dc45 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 740 | .setInterfaceConfig(eq(TEST_WLAN_IFNAME), any(InterfaceConfiguration.class)); |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 741 | verify(mNMService, times(1)).stopTethering(); |
| 742 | verify(mNMService, times(1)).setIpForwardingEnabled(false); |
| 743 | verifyNoMoreInteractions(mNMService); |
Erik Kline | 216af6d | 2017-04-27 20:57:23 +0900 | [diff] [blame] | 744 | verifyNoMoreInteractions(mWifiManager); |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 745 | // Asking for the last error after the per-interface state machine |
| 746 | // has been reaped yields an unknown interface error. |
Erik Kline | 7a26ba3 | 2018-03-09 14:18:02 +0900 | [diff] [blame] | 747 | assertEquals(TETHER_ERROR_UNKNOWN_IFACE, mTethering.getLastTetherError(TEST_WLAN_IFNAME)); |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 748 | } |
| 749 | |
Erik Kline | a9cde8b | 2017-06-20 21:18:31 +0900 | [diff] [blame] | 750 | // TODO: Test with and without interfaceStatusChanged(). |
Erik Kline | 1fdc2e2 | 2017-05-08 17:56:35 +0900 | [diff] [blame] | 751 | @Test |
| 752 | public void failureEnablingIpForwarding() throws Exception { |
Erik Kline | 1fdc2e2 | 2017-05-08 17:56:35 +0900 | [diff] [blame] | 753 | when(mWifiManager.startSoftAp(any(WifiConfiguration.class))).thenReturn(true); |
| 754 | doThrow(new RemoteException()).when(mNMService).setIpForwardingEnabled(true); |
| 755 | |
| 756 | // Emulate pressing the WiFi tethering button. |
Erik Kline | c438e30 | 2017-07-04 22:02:49 +0900 | [diff] [blame] | 757 | mTethering.startTethering(TETHERING_WIFI, null, false); |
Erik Kline | 1fdc2e2 | 2017-05-08 17:56:35 +0900 | [diff] [blame] | 758 | mLooper.dispatchAll(); |
| 759 | verify(mWifiManager, times(1)).startSoftAp(null); |
| 760 | verifyNoMoreInteractions(mWifiManager); |
Erik Kline | 1fdc2e2 | 2017-05-08 17:56:35 +0900 | [diff] [blame] | 761 | verifyNoMoreInteractions(mNMService); |
| 762 | |
| 763 | // Emulate externally-visible WifiManager effects, causing the |
| 764 | // per-interface state machine to start up, and telling us that |
| 765 | // tethering mode is to be started. |
Remi NGUYEN VAN | 5d0dc45 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 766 | mTethering.interfaceStatusChanged(TEST_WLAN_IFNAME, true); |
| 767 | sendWifiApStateChanged(WIFI_AP_STATE_ENABLED, TEST_WLAN_IFNAME, IFACE_IP_MODE_TETHERED); |
Erik Kline | 1fdc2e2 | 2017-05-08 17:56:35 +0900 | [diff] [blame] | 768 | mLooper.dispatchAll(); |
| 769 | |
Erik Kline | 1fdc2e2 | 2017-05-08 17:56:35 +0900 | [diff] [blame] | 770 | // We verify get/set called twice here: once for setup and once during |
| 771 | // teardown because all events happen over the course of the single |
| 772 | // dispatchAll() above. |
Remi NGUYEN VAN | 5d0dc45 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 773 | verify(mNMService, times(2)).getInterfaceConfig(TEST_WLAN_IFNAME); |
Erik Kline | 1fdc2e2 | 2017-05-08 17:56:35 +0900 | [diff] [blame] | 774 | verify(mNMService, times(2)) |
Remi NGUYEN VAN | 5d0dc45 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 775 | .setInterfaceConfig(eq(TEST_WLAN_IFNAME), any(InterfaceConfiguration.class)); |
| 776 | verify(mNMService, times(1)).tetherInterface(TEST_WLAN_IFNAME); |
Erik Kline | 1fdc2e2 | 2017-05-08 17:56:35 +0900 | [diff] [blame] | 777 | verify(mWifiManager).updateInterfaceIpState( |
Remi NGUYEN VAN | 5d0dc45 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 778 | TEST_WLAN_IFNAME, WifiManager.IFACE_IP_MODE_TETHERED); |
Erik Kline | 7a26ba3 | 2018-03-09 14:18:02 +0900 | [diff] [blame] | 779 | // TODO: Figure out why this isn't exactly once, for sendTetherStateChangedBroadcast(). |
| 780 | assertTrue(1 <= mTetheringDependencies.isTetheringSupportedCalls); |
| 781 | verifyTetheringBroadcast(TEST_WLAN_IFNAME, EXTRA_AVAILABLE_TETHER); |
Erik Kline | 1fdc2e2 | 2017-05-08 17:56:35 +0900 | [diff] [blame] | 782 | // This is called, but will throw. |
| 783 | verify(mNMService, times(1)).setIpForwardingEnabled(true); |
| 784 | // This never gets called because of the exception thrown above. |
| 785 | verify(mNMService, times(0)).startTethering(any(String[].class)); |
| 786 | // When the master state machine transitions to an error state it tells |
| 787 | // downstream interfaces, which causes us to tell Wi-Fi about the error |
| 788 | // so it can take down AP mode. |
Remi NGUYEN VAN | 5d0dc45 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 789 | verify(mNMService, times(1)).untetherInterface(TEST_WLAN_IFNAME); |
Erik Kline | 1fdc2e2 | 2017-05-08 17:56:35 +0900 | [diff] [blame] | 790 | verify(mWifiManager).updateInterfaceIpState( |
Remi NGUYEN VAN | 5d0dc45 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 791 | TEST_WLAN_IFNAME, WifiManager.IFACE_IP_MODE_CONFIGURATION_ERROR); |
Erik Kline | 1fdc2e2 | 2017-05-08 17:56:35 +0900 | [diff] [blame] | 792 | |
| 793 | verifyNoMoreInteractions(mWifiManager); |
Erik Kline | 1fdc2e2 | 2017-05-08 17:56:35 +0900 | [diff] [blame] | 794 | verifyNoMoreInteractions(mNMService); |
| 795 | } |
| 796 | |
Alexandru-Andrei Rotaru | fa6d5c5 | 2017-07-18 16:49:22 +0100 | [diff] [blame] | 797 | private void userRestrictionsListenerBehaviour( |
| 798 | boolean currentDisallow, boolean nextDisallow, String[] activeTetheringIfacesList, |
| 799 | int expectedInteractionsWithShowNotification) throws Exception { |
| 800 | final int userId = 0; |
| 801 | final Bundle currRestrictions = new Bundle(); |
| 802 | final Bundle newRestrictions = new Bundle(); |
| 803 | Tethering tethering = mock(Tethering.class); |
| 804 | Tethering.TetheringUserRestrictionListener turl = |
| 805 | new Tethering.TetheringUserRestrictionListener(tethering); |
| 806 | |
| 807 | currRestrictions.putBoolean(UserManager.DISALLOW_CONFIG_TETHERING, currentDisallow); |
| 808 | newRestrictions.putBoolean(UserManager.DISALLOW_CONFIG_TETHERING, nextDisallow); |
| 809 | when(tethering.getTetheredIfaces()).thenReturn(activeTetheringIfacesList); |
| 810 | |
| 811 | turl.onUserRestrictionsChanged(userId, newRestrictions, currRestrictions); |
| 812 | |
| 813 | verify(tethering, times(expectedInteractionsWithShowNotification)) |
| 814 | .showTetheredNotification(anyInt(), eq(false)); |
| 815 | |
| 816 | verify(tethering, times(expectedInteractionsWithShowNotification)).untetherAll(); |
| 817 | } |
| 818 | |
| 819 | @Test |
| 820 | public void testDisallowTetheringWhenNoTetheringInterfaceIsActive() throws Exception { |
| 821 | final String[] emptyActiveIfacesList = new String[]{}; |
| 822 | final boolean currDisallow = false; |
| 823 | final boolean nextDisallow = true; |
| 824 | final int expectedInteractionsWithShowNotification = 0; |
| 825 | |
| 826 | userRestrictionsListenerBehaviour(currDisallow, nextDisallow, emptyActiveIfacesList, |
| 827 | expectedInteractionsWithShowNotification); |
| 828 | } |
| 829 | |
| 830 | @Test |
| 831 | public void testDisallowTetheringWhenAtLeastOneTetheringInterfaceIsActive() throws Exception { |
Remi NGUYEN VAN | 5d0dc45 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 832 | final String[] nonEmptyActiveIfacesList = new String[]{TEST_WLAN_IFNAME}; |
Alexandru-Andrei Rotaru | fa6d5c5 | 2017-07-18 16:49:22 +0100 | [diff] [blame] | 833 | final boolean currDisallow = false; |
| 834 | final boolean nextDisallow = true; |
| 835 | final int expectedInteractionsWithShowNotification = 1; |
| 836 | |
| 837 | userRestrictionsListenerBehaviour(currDisallow, nextDisallow, nonEmptyActiveIfacesList, |
| 838 | expectedInteractionsWithShowNotification); |
| 839 | } |
| 840 | |
| 841 | @Test |
| 842 | public void testAllowTetheringWhenNoTetheringInterfaceIsActive() throws Exception { |
| 843 | final String[] nonEmptyActiveIfacesList = new String[]{}; |
| 844 | final boolean currDisallow = true; |
| 845 | final boolean nextDisallow = false; |
| 846 | final int expectedInteractionsWithShowNotification = 0; |
| 847 | |
| 848 | userRestrictionsListenerBehaviour(currDisallow, nextDisallow, nonEmptyActiveIfacesList, |
| 849 | expectedInteractionsWithShowNotification); |
| 850 | } |
| 851 | |
| 852 | @Test |
| 853 | public void testAllowTetheringWhenAtLeastOneTetheringInterfaceIsActive() throws Exception { |
Remi NGUYEN VAN | 5d0dc45 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 854 | final String[] nonEmptyActiveIfacesList = new String[]{TEST_WLAN_IFNAME}; |
Alexandru-Andrei Rotaru | fa6d5c5 | 2017-07-18 16:49:22 +0100 | [diff] [blame] | 855 | final boolean currDisallow = true; |
| 856 | final boolean nextDisallow = false; |
| 857 | final int expectedInteractionsWithShowNotification = 0; |
| 858 | |
| 859 | userRestrictionsListenerBehaviour(currDisallow, nextDisallow, nonEmptyActiveIfacesList, |
| 860 | expectedInteractionsWithShowNotification); |
| 861 | } |
| 862 | |
| 863 | @Test |
| 864 | public void testDisallowTetheringUnchanged() throws Exception { |
Remi NGUYEN VAN | 5d0dc45 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 865 | final String[] nonEmptyActiveIfacesList = new String[]{TEST_WLAN_IFNAME}; |
Alexandru-Andrei Rotaru | fa6d5c5 | 2017-07-18 16:49:22 +0100 | [diff] [blame] | 866 | final int expectedInteractionsWithShowNotification = 0; |
| 867 | boolean currDisallow = true; |
| 868 | boolean nextDisallow = true; |
| 869 | |
| 870 | userRestrictionsListenerBehaviour(currDisallow, nextDisallow, nonEmptyActiveIfacesList, |
| 871 | expectedInteractionsWithShowNotification); |
| 872 | |
| 873 | currDisallow = false; |
| 874 | nextDisallow = false; |
| 875 | |
| 876 | userRestrictionsListenerBehaviour(currDisallow, nextDisallow, nonEmptyActiveIfacesList, |
| 877 | expectedInteractionsWithShowNotification); |
| 878 | } |
| 879 | |
| 880 | |
Erik Kline | 1fdc2e2 | 2017-05-08 17:56:35 +0900 | [diff] [blame] | 881 | // 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] | 882 | // already operating tethering mode interface. |
Christopher Wiley | 497c147 | 2016-10-11 13:26:03 -0700 | [diff] [blame] | 883 | } |