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