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; |
Jimmy Chen | bcd86d0 | 2019-07-15 18:03:23 +0800 | [diff] [blame] | 28 | import static android.net.ConnectivityManager.TETHER_ERROR_NO_ERROR; |
markchien | b6eb2c2 | 2018-07-18 14:29:20 +0800 | [diff] [blame] | 29 | import static android.net.ConnectivityManager.TETHER_ERROR_UNKNOWN_IFACE; |
Erik Kline | 465ff3a | 2018-03-09 14:18:02 +0900 | [diff] [blame] | 30 | import static android.net.ConnectivityManager.TYPE_MOBILE; |
Jimmy Chen | bcd86d0 | 2019-07-15 18:03:23 +0800 | [diff] [blame] | 31 | import static android.net.ConnectivityManager.TYPE_WIFI_P2P; |
Remi NGUYEN VAN | 0e3d0923 | 2018-12-04 12:13:09 +0900 | [diff] [blame] | 32 | import static android.net.dhcp.IDhcpServer.STATUS_SUCCESS; |
Erik Kline | 2efb827 | 2017-05-31 15:53:53 +0900 | [diff] [blame] | 33 | import static android.net.wifi.WifiManager.EXTRA_WIFI_AP_INTERFACE_NAME; |
| 34 | import static android.net.wifi.WifiManager.EXTRA_WIFI_AP_MODE; |
| 35 | import static android.net.wifi.WifiManager.EXTRA_WIFI_AP_STATE; |
markchien | b6eb2c2 | 2018-07-18 14:29:20 +0800 | [diff] [blame] | 36 | import static android.net.wifi.WifiManager.IFACE_IP_MODE_LOCAL_ONLY; |
| 37 | import static android.net.wifi.WifiManager.IFACE_IP_MODE_TETHERED; |
Erik Kline | 2efb827 | 2017-05-31 15:53:53 +0900 | [diff] [blame] | 38 | import static android.net.wifi.WifiManager.WIFI_AP_STATE_ENABLED; |
Remi NGUYEN VAN | f5581f8 | 2018-09-28 11:34:15 +0900 | [diff] [blame] | 39 | import static android.provider.Settings.Global.TETHER_ENABLE_LEGACY_DHCP_SERVER; |
markchien | 0b59507 | 2019-01-08 23:52:21 +0800 | [diff] [blame] | 40 | import static android.telephony.SubscriptionManager.INVALID_SUBSCRIPTION_ID; |
Remi NGUYEN VAN | a911e84 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 41 | |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 42 | import static org.junit.Assert.assertEquals; |
Christopher Wiley | 497c147 | 2016-10-11 13:26:03 -0700 | [diff] [blame] | 43 | import static org.junit.Assert.assertTrue; |
Remi NGUYEN VAN | 0e3d0923 | 2018-12-04 12:13:09 +0900 | [diff] [blame] | 44 | import static org.junit.Assert.fail; |
Remi NGUYEN VAN | a911e84 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 45 | import static org.mockito.ArgumentMatchers.argThat; |
| 46 | import static org.mockito.ArgumentMatchers.notNull; |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 47 | import static org.mockito.Matchers.anyInt; |
| 48 | import static org.mockito.Matchers.anyString; |
Christopher Wiley | 497c147 | 2016-10-11 13:26:03 -0700 | [diff] [blame] | 49 | import static org.mockito.Matchers.eq; |
Erik Kline | 1fdc2e2 | 2017-05-08 17:56:35 +0900 | [diff] [blame] | 50 | import static org.mockito.Mockito.any; |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 51 | import static org.mockito.Mockito.atLeastOnce; |
Erik Kline | 1fdc2e2 | 2017-05-08 17:56:35 +0900 | [diff] [blame] | 52 | import static org.mockito.Mockito.doThrow; |
markchien | b6eb2c2 | 2018-07-18 14:29:20 +0800 | [diff] [blame] | 53 | import static org.mockito.Mockito.mock; |
Remi NGUYEN VAN | a911e84 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 54 | import static org.mockito.Mockito.never; |
Remi NGUYEN VAN | 0e3d0923 | 2018-12-04 12:13:09 +0900 | [diff] [blame] | 55 | import static org.mockito.Mockito.spy; |
| 56 | import static org.mockito.Mockito.timeout; |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 57 | import static org.mockito.Mockito.times; |
| 58 | import static org.mockito.Mockito.verify; |
| 59 | import static org.mockito.Mockito.verifyNoMoreInteractions; |
Christopher Wiley | 497c147 | 2016-10-11 13:26:03 -0700 | [diff] [blame] | 60 | import static org.mockito.Mockito.when; |
| 61 | |
Erik Kline | 8351faa | 2017-04-17 16:47:23 +0900 | [diff] [blame] | 62 | import android.content.BroadcastReceiver; |
Erik Kline | 92c4db0 | 2017-05-31 10:21:32 +0900 | [diff] [blame] | 63 | import android.content.ContentResolver; |
Christopher Wiley | 497c147 | 2016-10-11 13:26:03 -0700 | [diff] [blame] | 64 | import android.content.Context; |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 65 | import android.content.Intent; |
Erik Kline | 8351faa | 2017-04-17 16:47:23 +0900 | [diff] [blame] | 66 | import android.content.IntentFilter; |
Erik Kline | f3a08b4 | 2017-06-07 16:33:19 +0900 | [diff] [blame] | 67 | import android.content.pm.ApplicationInfo; |
Christopher Wiley | 497c147 | 2016-10-11 13:26:03 -0700 | [diff] [blame] | 68 | import android.content.res.Resources; |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 69 | import android.hardware.usb.UsbManager; |
Remi NGUYEN VAN | a911e84 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 70 | import android.net.INetd; |
Christopher Wiley | 497c147 | 2016-10-11 13:26:03 -0700 | [diff] [blame] | 71 | import android.net.INetworkPolicyManager; |
| 72 | import android.net.INetworkStatsService; |
markchien | 26299ed | 2019-02-27 14:56:11 +0800 | [diff] [blame] | 73 | import android.net.ITetheringEventCallback; |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 74 | import android.net.InterfaceConfiguration; |
Remi NGUYEN VAN | a911e84 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 75 | import android.net.IpPrefix; |
| 76 | import android.net.LinkAddress; |
| 77 | import android.net.LinkProperties; |
| 78 | import android.net.MacAddress; |
| 79 | import android.net.Network; |
| 80 | import android.net.NetworkCapabilities; |
| 81 | import android.net.NetworkInfo; |
| 82 | import android.net.NetworkState; |
| 83 | import android.net.NetworkUtils; |
| 84 | import android.net.RouteInfo; |
Remi NGUYEN VAN | 0e3d0923 | 2018-12-04 12:13:09 +0900 | [diff] [blame] | 85 | import android.net.dhcp.DhcpServerCallbacks; |
| 86 | import android.net.dhcp.DhcpServingParamsParcel; |
| 87 | import android.net.dhcp.IDhcpServer; |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 88 | import android.net.ip.IpServer; |
Remi NGUYEN VAN | a911e84 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 89 | import android.net.ip.RouterAdvertisementDaemon; |
| 90 | import android.net.util.InterfaceParams; |
| 91 | import android.net.util.NetworkConstants; |
Erik Kline | f4b6e34 | 2017-04-25 19:19:59 +0900 | [diff] [blame] | 92 | import android.net.util.SharedLog; |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 93 | import android.net.wifi.WifiConfiguration; |
| 94 | import android.net.wifi.WifiManager; |
Jimmy Chen | bcd86d0 | 2019-07-15 18:03:23 +0800 | [diff] [blame] | 95 | import android.net.wifi.p2p.WifiP2pGroup; |
| 96 | import android.net.wifi.p2p.WifiP2pInfo; |
| 97 | import android.net.wifi.p2p.WifiP2pManager; |
Alexandru-Andrei Rotaru | fa6d5c5 | 2017-07-18 16:49:22 +0100 | [diff] [blame] | 98 | import android.os.Bundle; |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 99 | import android.os.Handler; |
Christopher Wiley | 497c147 | 2016-10-11 13:26:03 -0700 | [diff] [blame] | 100 | import android.os.INetworkManagementService; |
| 101 | import android.os.PersistableBundle; |
Erik Kline | 1fdc2e2 | 2017-05-08 17:56:35 +0900 | [diff] [blame] | 102 | import android.os.RemoteException; |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 103 | import android.os.UserHandle; |
Alexandru-Andrei Rotaru | fa6d5c5 | 2017-07-18 16:49:22 +0100 | [diff] [blame] | 104 | import android.os.UserManager; |
markchien | b6eb2c2 | 2018-07-18 14:29:20 +0800 | [diff] [blame] | 105 | import android.os.test.TestLooper; |
Erik Kline | 92c4db0 | 2017-05-31 10:21:32 +0900 | [diff] [blame] | 106 | import android.provider.Settings; |
Christopher Wiley | 497c147 | 2016-10-11 13:26:03 -0700 | [diff] [blame] | 107 | import android.telephony.CarrierConfigManager; |
markchien | 04bdf87 | 2019-06-17 21:05:34 +0800 | [diff] [blame] | 108 | import android.telephony.PhoneStateListener; |
| 109 | import android.telephony.TelephonyManager; |
Erik Kline | 92c4db0 | 2017-05-31 10:21:32 +0900 | [diff] [blame] | 110 | import android.test.mock.MockContentResolver; |
Christopher Wiley | 497c147 | 2016-10-11 13:26:03 -0700 | [diff] [blame] | 111 | |
Brett Chabot | 1ae2aa6 | 2019-03-04 14:14:56 -0800 | [diff] [blame] | 112 | import androidx.test.filters.SmallTest; |
| 113 | import androidx.test.runner.AndroidJUnit4; |
| 114 | |
Remi NGUYEN VAN | a911e84 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 115 | import com.android.internal.util.ArrayUtils; |
| 116 | import com.android.internal.util.StateMachine; |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 117 | import com.android.internal.util.test.BroadcastInterceptingContext; |
Erik Kline | 92c4db0 | 2017-05-31 10:21:32 +0900 | [diff] [blame] | 118 | import com.android.internal.util.test.FakeSettingsProvider; |
Remi NGUYEN VAN | a911e84 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 119 | import com.android.server.connectivity.tethering.IPv6TetheringCoordinator; |
Erik Kline | 5a7c8a0 | 2017-04-30 19:36:15 +0900 | [diff] [blame] | 120 | import com.android.server.connectivity.tethering.OffloadHardwareInterface; |
markchien | 04bdf87 | 2019-06-17 21:05:34 +0800 | [diff] [blame] | 121 | import com.android.server.connectivity.tethering.TetheringConfiguration; |
Erik Kline | 5a7c8a0 | 2017-04-30 19:36:15 +0900 | [diff] [blame] | 122 | import com.android.server.connectivity.tethering.TetheringDependencies; |
Remi NGUYEN VAN | a911e84 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 123 | import com.android.server.connectivity.tethering.UpstreamNetworkMonitor; |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 124 | |
Erik Kline | 8351faa | 2017-04-17 16:47:23 +0900 | [diff] [blame] | 125 | import org.junit.After; |
Christopher Wiley | 497c147 | 2016-10-11 13:26:03 -0700 | [diff] [blame] | 126 | import org.junit.Before; |
| 127 | import org.junit.Test; |
| 128 | import org.junit.runner.RunWith; |
markchien | 04bdf87 | 2019-06-17 21:05:34 +0800 | [diff] [blame] | 129 | import org.mockito.ArgumentCaptor; |
Christopher Wiley | 497c147 | 2016-10-11 13:26:03 -0700 | [diff] [blame] | 130 | import org.mockito.Mock; |
| 131 | import org.mockito.MockitoAnnotations; |
| 132 | |
Remi NGUYEN VAN | a911e84 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 133 | import java.net.Inet4Address; |
| 134 | import java.net.Inet6Address; |
Erik Kline | 8351faa | 2017-04-17 16:47:23 +0900 | [diff] [blame] | 135 | import java.util.ArrayList; |
markchien | 26299ed | 2019-02-27 14:56:11 +0800 | [diff] [blame] | 136 | import java.util.Arrays; |
Erik Kline | 8351faa | 2017-04-17 16:47:23 +0900 | [diff] [blame] | 137 | import java.util.Vector; |
| 138 | |
Christopher Wiley | 497c147 | 2016-10-11 13:26:03 -0700 | [diff] [blame] | 139 | @RunWith(AndroidJUnit4.class) |
| 140 | @SmallTest |
| 141 | public class TetheringTest { |
Remi NGUYEN VAN | a911e84 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 142 | private static final int IFINDEX_OFFSET = 100; |
| 143 | |
Remi NGUYEN VAN | a911e84 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 144 | private static final String TEST_MOBILE_IFNAME = "test_rmnet_data0"; |
| 145 | private static final String TEST_XLAT_MOBILE_IFNAME = "v4-test_rmnet_data0"; |
| 146 | private static final String TEST_USB_IFNAME = "test_rndis0"; |
| 147 | private static final String TEST_WLAN_IFNAME = "test_wlan0"; |
Jimmy Chen | bcd86d0 | 2019-07-15 18:03:23 +0800 | [diff] [blame] | 148 | private static final String TEST_P2P_IFNAME = "test_p2p-p2p0-0"; |
Christopher Wiley | 497c147 | 2016-10-11 13:26:03 -0700 | [diff] [blame] | 149 | |
Remi NGUYEN VAN | 0e3d0923 | 2018-12-04 12:13:09 +0900 | [diff] [blame] | 150 | private static final int DHCPSERVER_START_TIMEOUT_MS = 1000; |
| 151 | |
Erik Kline | f3a08b4 | 2017-06-07 16:33:19 +0900 | [diff] [blame] | 152 | @Mock private ApplicationInfo mApplicationInfo; |
Christopher Wiley | 497c147 | 2016-10-11 13:26:03 -0700 | [diff] [blame] | 153 | @Mock private Context mContext; |
| 154 | @Mock private INetworkManagementService mNMService; |
| 155 | @Mock private INetworkStatsService mStatsService; |
| 156 | @Mock private INetworkPolicyManager mPolicyManager; |
| 157 | @Mock private MockableSystemProperties mSystemProperties; |
Erik Kline | 5a7c8a0 | 2017-04-30 19:36:15 +0900 | [diff] [blame] | 158 | @Mock private OffloadHardwareInterface mOffloadHardwareInterface; |
Christopher Wiley | 497c147 | 2016-10-11 13:26:03 -0700 | [diff] [blame] | 159 | @Mock private Resources mResources; |
markchien | 04bdf87 | 2019-06-17 21:05:34 +0800 | [diff] [blame] | 160 | @Mock private TelephonyManager mTelephonyManager; |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 161 | @Mock private UsbManager mUsbManager; |
| 162 | @Mock private WifiManager mWifiManager; |
Christopher Wiley | 497c147 | 2016-10-11 13:26:03 -0700 | [diff] [blame] | 163 | @Mock private CarrierConfigManager mCarrierConfigManager; |
Remi NGUYEN VAN | a911e84 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 164 | @Mock private UpstreamNetworkMonitor mUpstreamNetworkMonitor; |
| 165 | @Mock private IPv6TetheringCoordinator mIPv6TetheringCoordinator; |
| 166 | @Mock private RouterAdvertisementDaemon mRouterAdvertisementDaemon; |
Remi NGUYEN VAN | 0e3d0923 | 2018-12-04 12:13:09 +0900 | [diff] [blame] | 167 | @Mock private IDhcpServer mDhcpServer; |
Remi NGUYEN VAN | a911e84 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 168 | @Mock private INetd mNetd; |
| 169 | |
Remi NGUYEN VAN | 0e3d0923 | 2018-12-04 12:13:09 +0900 | [diff] [blame] | 170 | private final MockIpServerDependencies mIpServerDependencies = |
| 171 | spy(new MockIpServerDependencies()); |
Remi NGUYEN VAN | a911e84 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 172 | private final MockTetheringDependencies mTetheringDependencies = |
| 173 | new MockTetheringDependencies(); |
Christopher Wiley | 497c147 | 2016-10-11 13:26:03 -0700 | [diff] [blame] | 174 | |
| 175 | // Like so many Android system APIs, these cannot be mocked because it is marked final. |
| 176 | // We have to use the real versions. |
| 177 | private final PersistableBundle mCarrierConfig = new PersistableBundle(); |
| 178 | private final TestLooper mLooper = new TestLooper(); |
| 179 | |
Erik Kline | 8351faa | 2017-04-17 16:47:23 +0900 | [diff] [blame] | 180 | private Vector<Intent> mIntents; |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 181 | private BroadcastInterceptingContext mServiceContext; |
Erik Kline | 92c4db0 | 2017-05-31 10:21:32 +0900 | [diff] [blame] | 182 | private MockContentResolver mContentResolver; |
Erik Kline | 8351faa | 2017-04-17 16:47:23 +0900 | [diff] [blame] | 183 | private BroadcastReceiver mBroadcastReceiver; |
Christopher Wiley | 497c147 | 2016-10-11 13:26:03 -0700 | [diff] [blame] | 184 | private Tethering mTethering; |
markchien | 04bdf87 | 2019-06-17 21:05:34 +0800 | [diff] [blame] | 185 | private PhoneStateListener mPhoneStateListener; |
Christopher Wiley | 497c147 | 2016-10-11 13:26:03 -0700 | [diff] [blame] | 186 | |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 187 | private class MockContext extends BroadcastInterceptingContext { |
| 188 | MockContext(Context base) { |
| 189 | super(base); |
| 190 | } |
| 191 | |
| 192 | @Override |
Erik Kline | f3a08b4 | 2017-06-07 16:33:19 +0900 | [diff] [blame] | 193 | public ApplicationInfo getApplicationInfo() { return mApplicationInfo; } |
| 194 | |
| 195 | @Override |
Erik Kline | 92c4db0 | 2017-05-31 10:21:32 +0900 | [diff] [blame] | 196 | public ContentResolver getContentResolver() { return mContentResolver; } |
| 197 | |
| 198 | @Override |
| 199 | public String getPackageName() { return "TetheringTest"; } |
| 200 | |
| 201 | @Override |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 202 | public Resources getResources() { return mResources; } |
| 203 | |
| 204 | @Override |
| 205 | public Object getSystemService(String name) { |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 206 | if (Context.WIFI_SERVICE.equals(name)) return mWifiManager; |
Erik Kline | c438e30 | 2017-07-04 22:02:49 +0900 | [diff] [blame] | 207 | if (Context.USB_SERVICE.equals(name)) return mUsbManager; |
markchien | 04bdf87 | 2019-06-17 21:05:34 +0800 | [diff] [blame] | 208 | if (Context.TELEPHONY_SERVICE.equals(name)) return mTelephonyManager; |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 209 | return super.getSystemService(name); |
| 210 | } |
Jayachandran C | 43fa1be | 2019-11-15 09:58:04 -0800 | [diff] [blame] | 211 | |
| 212 | @Override |
| 213 | public String getSystemServiceName(Class<?> serviceClass) { |
| 214 | if (TelephonyManager.class.equals(serviceClass)) return Context.TELEPHONY_SERVICE; |
| 215 | return super.getSystemServiceName(serviceClass); |
| 216 | } |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 217 | } |
| 218 | |
Remi NGUYEN VAN | 0e3d0923 | 2018-12-04 12:13:09 +0900 | [diff] [blame] | 219 | public class MockIpServerDependencies extends IpServer.Dependencies { |
Remi NGUYEN VAN | 0e3d0923 | 2018-12-04 12:13:09 +0900 | [diff] [blame] | 220 | @Override |
| 221 | public RouterAdvertisementDaemon getRouterAdvertisementDaemon( |
| 222 | InterfaceParams ifParams) { |
| 223 | return mRouterAdvertisementDaemon; |
| 224 | } |
| 225 | |
| 226 | @Override |
| 227 | public InterfaceParams getInterfaceParams(String ifName) { |
| 228 | assertTrue("Non-mocked interface " + ifName, |
| 229 | ifName.equals(TEST_USB_IFNAME) |
| 230 | || ifName.equals(TEST_WLAN_IFNAME) |
Jimmy Chen | bcd86d0 | 2019-07-15 18:03:23 +0800 | [diff] [blame] | 231 | || ifName.equals(TEST_MOBILE_IFNAME) |
| 232 | || ifName.equals(TEST_P2P_IFNAME)); |
Remi NGUYEN VAN | 0e3d0923 | 2018-12-04 12:13:09 +0900 | [diff] [blame] | 233 | final String[] ifaces = new String[] { |
Jimmy Chen | bcd86d0 | 2019-07-15 18:03:23 +0800 | [diff] [blame] | 234 | TEST_USB_IFNAME, TEST_WLAN_IFNAME, TEST_MOBILE_IFNAME, TEST_P2P_IFNAME}; |
Remi NGUYEN VAN | 0e3d0923 | 2018-12-04 12:13:09 +0900 | [diff] [blame] | 235 | return new InterfaceParams(ifName, ArrayUtils.indexOf(ifaces, ifName) + IFINDEX_OFFSET, |
| 236 | MacAddress.ALL_ZEROS_ADDRESS); |
| 237 | } |
| 238 | |
| 239 | @Override |
| 240 | public INetd getNetdService() { |
| 241 | return mNetd; |
| 242 | } |
| 243 | |
| 244 | @Override |
| 245 | public void makeDhcpServer(String ifName, DhcpServingParamsParcel params, |
| 246 | DhcpServerCallbacks cb) { |
| 247 | new Thread(() -> { |
| 248 | try { |
| 249 | cb.onDhcpServerCreated(STATUS_SUCCESS, mDhcpServer); |
| 250 | } catch (RemoteException e) { |
| 251 | fail(e.getMessage()); |
| 252 | } |
| 253 | }).run(); |
| 254 | } |
| 255 | } |
| 256 | |
markchien | 04bdf87 | 2019-06-17 21:05:34 +0800 | [diff] [blame] | 257 | private class MockTetheringConfiguration extends TetheringConfiguration { |
| 258 | MockTetheringConfiguration(Context ctx, SharedLog log, int id) { |
| 259 | super(ctx, log, id); |
| 260 | } |
| 261 | |
| 262 | @Override |
| 263 | protected Resources getResourcesForSubIdWrapper(Context ctx, int subId) { |
| 264 | return mResources; |
| 265 | } |
| 266 | } |
| 267 | |
Remi NGUYEN VAN | a911e84 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 268 | public class MockTetheringDependencies extends TetheringDependencies { |
Erik Kline | 465ff3a | 2018-03-09 14:18:02 +0900 | [diff] [blame] | 269 | StateMachine upstreamNetworkMonitorMasterSM; |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 270 | ArrayList<IpServer> ipv6CoordinatorNotifyList; |
Erik Kline | 465ff3a | 2018-03-09 14:18:02 +0900 | [diff] [blame] | 271 | int isTetheringSupportedCalls; |
| 272 | |
| 273 | public void reset() { |
| 274 | upstreamNetworkMonitorMasterSM = null; |
| 275 | ipv6CoordinatorNotifyList = null; |
| 276 | isTetheringSupportedCalls = 0; |
| 277 | } |
Remi NGUYEN VAN | a911e84 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 278 | |
| 279 | @Override |
| 280 | public OffloadHardwareInterface getOffloadHardwareInterface(Handler h, SharedLog log) { |
| 281 | return mOffloadHardwareInterface; |
| 282 | } |
| 283 | |
| 284 | @Override |
| 285 | public UpstreamNetworkMonitor getUpstreamNetworkMonitor(Context ctx, |
| 286 | StateMachine target, SharedLog log, int what) { |
| 287 | upstreamNetworkMonitorMasterSM = target; |
| 288 | return mUpstreamNetworkMonitor; |
| 289 | } |
| 290 | |
| 291 | @Override |
| 292 | public IPv6TetheringCoordinator getIPv6TetheringCoordinator( |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 293 | ArrayList<IpServer> notifyList, SharedLog log) { |
Remi NGUYEN VAN | a911e84 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 294 | ipv6CoordinatorNotifyList = notifyList; |
| 295 | return mIPv6TetheringCoordinator; |
| 296 | } |
| 297 | |
| 298 | @Override |
Remi NGUYEN VAN | 5db454c | 2019-02-14 18:04:20 +0900 | [diff] [blame] | 299 | public IpServer.Dependencies getIpServerDependencies() { |
Remi NGUYEN VAN | 0e3d0923 | 2018-12-04 12:13:09 +0900 | [diff] [blame] | 300 | return mIpServerDependencies; |
Remi NGUYEN VAN | a911e84 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 301 | } |
Erik Kline | 465ff3a | 2018-03-09 14:18:02 +0900 | [diff] [blame] | 302 | |
| 303 | @Override |
| 304 | public boolean isTetheringSupported() { |
| 305 | isTetheringSupportedCalls++; |
| 306 | return true; |
| 307 | } |
markchien | 0b59507 | 2019-01-08 23:52:21 +0800 | [diff] [blame] | 308 | |
| 309 | @Override |
markchien | 04bdf87 | 2019-06-17 21:05:34 +0800 | [diff] [blame] | 310 | public TetheringConfiguration generateTetheringConfiguration(Context ctx, SharedLog log, |
| 311 | int subId) { |
| 312 | return new MockTetheringConfiguration(ctx, log, subId); |
markchien | 0b59507 | 2019-01-08 23:52:21 +0800 | [diff] [blame] | 313 | } |
Remi NGUYEN VAN | a911e84 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 314 | } |
| 315 | |
Remi NGUYEN VAN | 25a7e4f | 2018-03-09 14:07:18 +0900 | [diff] [blame] | 316 | private static NetworkState buildMobileUpstreamState(boolean withIPv4, boolean withIPv6, |
| 317 | boolean with464xlat) { |
Erik Kline | 465ff3a | 2018-03-09 14:18:02 +0900 | [diff] [blame] | 318 | final NetworkInfo info = new NetworkInfo(TYPE_MOBILE, 0, null, null); |
Remi NGUYEN VAN | a911e84 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 319 | info.setDetailedState(NetworkInfo.DetailedState.CONNECTED, null, null); |
| 320 | final LinkProperties prop = new LinkProperties(); |
| 321 | prop.setInterfaceName(TEST_MOBILE_IFNAME); |
| 322 | |
| 323 | if (withIPv4) { |
| 324 | prop.addRoute(new RouteInfo(new IpPrefix(Inet4Address.ANY, 0), |
| 325 | NetworkUtils.numericToInetAddress("10.0.0.1"), TEST_MOBILE_IFNAME)); |
| 326 | } |
| 327 | |
| 328 | if (withIPv6) { |
| 329 | prop.addDnsServer(NetworkUtils.numericToInetAddress("2001:db8::2")); |
| 330 | prop.addLinkAddress( |
| 331 | new LinkAddress(NetworkUtils.numericToInetAddress("2001:db8::"), |
| 332 | NetworkConstants.RFC7421_PREFIX_LENGTH)); |
| 333 | prop.addRoute(new RouteInfo(new IpPrefix(Inet6Address.ANY, 0), |
| 334 | NetworkUtils.numericToInetAddress("2001:db8::1"), TEST_MOBILE_IFNAME)); |
| 335 | } |
| 336 | |
Remi NGUYEN VAN | 25a7e4f | 2018-03-09 14:07:18 +0900 | [diff] [blame] | 337 | if (with464xlat) { |
| 338 | final LinkProperties stackedLink = new LinkProperties(); |
| 339 | stackedLink.setInterfaceName(TEST_XLAT_MOBILE_IFNAME); |
| 340 | stackedLink.addRoute(new RouteInfo(new IpPrefix(Inet4Address.ANY, 0), |
| 341 | NetworkUtils.numericToInetAddress("192.0.0.1"), TEST_XLAT_MOBILE_IFNAME)); |
| 342 | |
| 343 | prop.addStackedLink(stackedLink); |
| 344 | } |
| 345 | |
Remi NGUYEN VAN | a911e84 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 346 | |
| 347 | final NetworkCapabilities capabilities = new NetworkCapabilities() |
| 348 | .addTransportType(NetworkCapabilities.TRANSPORT_CELLULAR);; |
| 349 | return new NetworkState(info, prop, capabilities, new Network(100), null, "netid"); |
| 350 | } |
| 351 | |
| 352 | private static NetworkState buildMobileIPv4UpstreamState() { |
Remi NGUYEN VAN | 25a7e4f | 2018-03-09 14:07:18 +0900 | [diff] [blame] | 353 | return buildMobileUpstreamState(true, false, false); |
| 354 | } |
| 355 | |
| 356 | private static NetworkState buildMobileIPv6UpstreamState() { |
| 357 | return buildMobileUpstreamState(false, true, false); |
Remi NGUYEN VAN | a911e84 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 358 | } |
| 359 | |
| 360 | private static NetworkState buildMobileDualStackUpstreamState() { |
Remi NGUYEN VAN | 25a7e4f | 2018-03-09 14:07:18 +0900 | [diff] [blame] | 361 | return buildMobileUpstreamState(true, true, false); |
| 362 | } |
| 363 | |
| 364 | private static NetworkState buildMobile464xlatUpstreamState() { |
| 365 | return buildMobileUpstreamState(false, true, true); |
Remi NGUYEN VAN | a911e84 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 366 | } |
| 367 | |
Erik Kline | 8351faa | 2017-04-17 16:47:23 +0900 | [diff] [blame] | 368 | @Before |
| 369 | public void setUp() throws Exception { |
Christopher Wiley | 497c147 | 2016-10-11 13:26:03 -0700 | [diff] [blame] | 370 | MockitoAnnotations.initMocks(this); |
Christopher Wiley | 497c147 | 2016-10-11 13:26:03 -0700 | [diff] [blame] | 371 | when(mResources.getStringArray(com.android.internal.R.array.config_tether_dhcp_range)) |
| 372 | .thenReturn(new String[0]); |
| 373 | 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] | 374 | .thenReturn(new String[] { "test_rndis\\d" }); |
Christopher Wiley | 497c147 | 2016-10-11 13:26:03 -0700 | [diff] [blame] | 375 | 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] | 376 | .thenReturn(new String[]{ "test_wlan\\d" }); |
Jimmy Chen | bcd86d0 | 2019-07-15 18:03:23 +0800 | [diff] [blame] | 377 | when(mResources.getStringArray(com.android.internal.R.array.config_tether_wifi_p2p_regexs)) |
| 378 | .thenReturn(new String[]{ "test_p2p-p2p\\d-.*" }); |
Christopher Wiley | 497c147 | 2016-10-11 13:26:03 -0700 | [diff] [blame] | 379 | when(mResources.getStringArray(com.android.internal.R.array.config_tether_bluetooth_regexs)) |
| 380 | .thenReturn(new String[0]); |
| 381 | when(mResources.getIntArray(com.android.internal.R.array.config_tether_upstream_types)) |
| 382 | .thenReturn(new int[0]); |
Erik Kline | 7230290 | 2018-06-14 17:36:40 +0900 | [diff] [blame] | 383 | when(mResources.getBoolean(com.android.internal.R.bool.config_tether_upstream_automatic)) |
| 384 | .thenReturn(false); |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 385 | when(mNMService.listInterfaces()) |
Remi NGUYEN VAN | a911e84 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 386 | .thenReturn(new String[] { |
Jimmy Chen | bcd86d0 | 2019-07-15 18:03:23 +0800 | [diff] [blame] | 387 | TEST_MOBILE_IFNAME, TEST_WLAN_IFNAME, TEST_USB_IFNAME, TEST_P2P_IFNAME}); |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 388 | when(mNMService.getInterfaceConfig(anyString())) |
| 389 | .thenReturn(new InterfaceConfiguration()); |
Remi NGUYEN VAN | a911e84 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 390 | when(mRouterAdvertisementDaemon.start()) |
| 391 | .thenReturn(true); |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 392 | |
| 393 | mServiceContext = new MockContext(mContext); |
Erik Kline | 92c4db0 | 2017-05-31 10:21:32 +0900 | [diff] [blame] | 394 | mContentResolver = new MockContentResolver(mServiceContext); |
| 395 | mContentResolver.addProvider(Settings.AUTHORITY, new FakeSettingsProvider()); |
Remi NGUYEN VAN | f5581f8 | 2018-09-28 11:34:15 +0900 | [diff] [blame] | 396 | Settings.Global.putInt(mContentResolver, TETHER_ENABLE_LEGACY_DHCP_SERVER, 0); |
Erik Kline | 8351faa | 2017-04-17 16:47:23 +0900 | [diff] [blame] | 397 | mIntents = new Vector<>(); |
| 398 | mBroadcastReceiver = new BroadcastReceiver() { |
| 399 | @Override |
| 400 | public void onReceive(Context context, Intent intent) { |
| 401 | mIntents.addElement(intent); |
| 402 | } |
| 403 | }; |
| 404 | mServiceContext.registerReceiver(mBroadcastReceiver, |
Erik Kline | 465ff3a | 2018-03-09 14:18:02 +0900 | [diff] [blame] | 405 | new IntentFilter(ACTION_TETHER_STATE_CHANGED)); |
| 406 | mTetheringDependencies.reset(); |
Remi NGUYEN VAN | f5581f8 | 2018-09-28 11:34:15 +0900 | [diff] [blame] | 407 | mTethering = makeTethering(); |
Lorenzo Colitti | 5a7dea1 | 2017-07-12 15:48:07 +0900 | [diff] [blame] | 408 | verify(mNMService).registerTetheringStatsProvider(any(), anyString()); |
markchien | 04bdf87 | 2019-06-17 21:05:34 +0800 | [diff] [blame] | 409 | final ArgumentCaptor<PhoneStateListener> phoneListenerCaptor = |
| 410 | ArgumentCaptor.forClass(PhoneStateListener.class); |
| 411 | verify(mTelephonyManager).listen(phoneListenerCaptor.capture(), |
| 412 | eq(PhoneStateListener.LISTEN_ACTIVE_DATA_SUBSCRIPTION_ID_CHANGE)); |
| 413 | mPhoneStateListener = phoneListenerCaptor.getValue(); |
Christopher Wiley | 497c147 | 2016-10-11 13:26:03 -0700 | [diff] [blame] | 414 | } |
| 415 | |
Remi NGUYEN VAN | f5581f8 | 2018-09-28 11:34:15 +0900 | [diff] [blame] | 416 | private Tethering makeTethering() { |
| 417 | return new Tethering(mServiceContext, mNMService, mStatsService, mPolicyManager, |
| 418 | mLooper.getLooper(), mSystemProperties, |
| 419 | mTetheringDependencies); |
| 420 | } |
| 421 | |
Erik Kline | 8351faa | 2017-04-17 16:47:23 +0900 | [diff] [blame] | 422 | @After |
| 423 | public void tearDown() { |
| 424 | mServiceContext.unregisterReceiver(mBroadcastReceiver); |
| 425 | } |
| 426 | |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 427 | private void sendWifiApStateChanged(int state) { |
| 428 | final Intent intent = new Intent(WifiManager.WIFI_AP_STATE_CHANGED_ACTION); |
Erik Kline | 2efb827 | 2017-05-31 15:53:53 +0900 | [diff] [blame] | 429 | intent.putExtra(EXTRA_WIFI_AP_STATE, state); |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 430 | mServiceContext.sendStickyBroadcastAsUser(intent, UserHandle.ALL); |
| 431 | } |
| 432 | |
Erik Kline | 2efb827 | 2017-05-31 15:53:53 +0900 | [diff] [blame] | 433 | private void sendWifiApStateChanged(int state, String ifname, int ipmode) { |
| 434 | final Intent intent = new Intent(WifiManager.WIFI_AP_STATE_CHANGED_ACTION); |
| 435 | intent.putExtra(EXTRA_WIFI_AP_STATE, state); |
| 436 | intent.putExtra(EXTRA_WIFI_AP_INTERFACE_NAME, ifname); |
| 437 | intent.putExtra(EXTRA_WIFI_AP_MODE, ipmode); |
| 438 | mServiceContext.sendStickyBroadcastAsUser(intent, UserHandle.ALL); |
| 439 | } |
| 440 | |
Jimmy Chen | bcd86d0 | 2019-07-15 18:03:23 +0800 | [diff] [blame] | 441 | private static final String[] P2P_RECEIVER_PERMISSIONS_FOR_BROADCAST = { |
| 442 | android.Manifest.permission.ACCESS_FINE_LOCATION, |
| 443 | android.Manifest.permission.ACCESS_WIFI_STATE |
| 444 | }; |
| 445 | |
| 446 | private void sendWifiP2pConnectionChanged( |
| 447 | boolean isGroupFormed, boolean isGroupOwner, String ifname) { |
| 448 | WifiP2pInfo p2pInfo = new WifiP2pInfo(); |
| 449 | p2pInfo.groupFormed = isGroupFormed; |
| 450 | p2pInfo.isGroupOwner = isGroupOwner; |
| 451 | |
| 452 | NetworkInfo networkInfo = new NetworkInfo(TYPE_WIFI_P2P, 0, null, null); |
| 453 | |
| 454 | WifiP2pGroup group = new WifiP2pGroup(); |
| 455 | group.setIsGroupOwner(isGroupOwner); |
| 456 | group.setInterface(ifname); |
| 457 | |
| 458 | final Intent intent = new Intent(WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION); |
| 459 | intent.putExtra(WifiP2pManager.EXTRA_WIFI_P2P_INFO, p2pInfo); |
| 460 | intent.putExtra(WifiP2pManager.EXTRA_NETWORK_INFO, networkInfo); |
| 461 | intent.putExtra(WifiP2pManager.EXTRA_WIFI_P2P_GROUP, group); |
| 462 | mServiceContext.sendBroadcastAsUserMultiplePermissions(intent, UserHandle.ALL, |
| 463 | P2P_RECEIVER_PERMISSIONS_FOR_BROADCAST); |
| 464 | } |
| 465 | |
Erik Kline | c438e30 | 2017-07-04 22:02:49 +0900 | [diff] [blame] | 466 | private void sendUsbBroadcast(boolean connected, boolean configured, boolean rndisFunction) { |
| 467 | final Intent intent = new Intent(UsbManager.ACTION_USB_STATE); |
| 468 | intent.putExtra(USB_CONNECTED, connected); |
| 469 | intent.putExtra(USB_CONFIGURED, configured); |
| 470 | intent.putExtra(USB_FUNCTION_RNDIS, rndisFunction); |
| 471 | mServiceContext.sendStickyBroadcastAsUser(intent, UserHandle.ALL); |
| 472 | } |
| 473 | |
Erik Kline | 80b7a9f | 2018-02-28 15:01:35 +0900 | [diff] [blame] | 474 | private void sendConfigurationChanged() { |
| 475 | final Intent intent = new Intent(Intent.ACTION_CONFIGURATION_CHANGED); |
| 476 | mServiceContext.sendStickyBroadcastAsUser(intent, UserHandle.ALL); |
| 477 | } |
| 478 | |
Jimmy Chen | bcd86d0 | 2019-07-15 18:03:23 +0800 | [diff] [blame] | 479 | private void verifyInterfaceServingModeStarted(String ifname) throws Exception { |
| 480 | verify(mNMService, times(1)).getInterfaceConfig(ifname); |
Erik Kline | 8351faa | 2017-04-17 16:47:23 +0900 | [diff] [blame] | 481 | verify(mNMService, times(1)) |
Jimmy Chen | bcd86d0 | 2019-07-15 18:03:23 +0800 | [diff] [blame] | 482 | .setInterfaceConfig(eq(ifname), any(InterfaceConfiguration.class)); |
| 483 | verify(mNMService, times(1)).tetherInterface(ifname); |
Erik Kline | 8351faa | 2017-04-17 16:47:23 +0900 | [diff] [blame] | 484 | } |
| 485 | |
| 486 | private void verifyTetheringBroadcast(String ifname, String whichExtra) { |
| 487 | // Verify that ifname is in the whichExtra array of the tether state changed broadcast. |
| 488 | final Intent bcast = mIntents.get(0); |
Erik Kline | 465ff3a | 2018-03-09 14:18:02 +0900 | [diff] [blame] | 489 | assertEquals(ACTION_TETHER_STATE_CHANGED, bcast.getAction()); |
Erik Kline | 8351faa | 2017-04-17 16:47:23 +0900 | [diff] [blame] | 490 | final ArrayList<String> ifnames = bcast.getStringArrayListExtra(whichExtra); |
| 491 | assertTrue(ifnames.contains(ifname)); |
| 492 | mIntents.remove(bcast); |
| 493 | } |
| 494 | |
Erik Kline | a9cde8b | 2017-06-20 21:18:31 +0900 | [diff] [blame] | 495 | public void failingLocalOnlyHotspotLegacyApBroadcast( |
| 496 | boolean emulateInterfaceStatusChanged) throws Exception { |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 497 | // Emulate externally-visible WifiManager effects, causing the |
| 498 | // per-interface state machine to start up, and telling us that |
| 499 | // hotspot mode is to be started. |
Erik Kline | a9cde8b | 2017-06-20 21:18:31 +0900 | [diff] [blame] | 500 | if (emulateInterfaceStatusChanged) { |
Remi NGUYEN VAN | a911e84 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 501 | mTethering.interfaceStatusChanged(TEST_WLAN_IFNAME, true); |
Erik Kline | a9cde8b | 2017-06-20 21:18:31 +0900 | [diff] [blame] | 502 | } |
Erik Kline | 9e22554 | 2017-06-08 17:48:48 +0900 | [diff] [blame] | 503 | sendWifiApStateChanged(WIFI_AP_STATE_ENABLED); |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 504 | mLooper.dispatchAll(); |
| 505 | |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 506 | // If, and only if, Tethering received an interface status changed then |
| 507 | // it creates a IpServer and sends out a broadcast indicating that the |
| 508 | // interface is "available". |
Erik Kline | a9cde8b | 2017-06-20 21:18:31 +0900 | [diff] [blame] | 509 | if (emulateInterfaceStatusChanged) { |
Erik Kline | 465ff3a | 2018-03-09 14:18:02 +0900 | [diff] [blame] | 510 | assertEquals(1, mTetheringDependencies.isTetheringSupportedCalls); |
| 511 | verifyTetheringBroadcast(TEST_WLAN_IFNAME, EXTRA_AVAILABLE_TETHER); |
Jianpeng Li | a70feec | 2019-05-24 17:40:15 +0900 | [diff] [blame] | 512 | verify(mWifiManager).updateInterfaceIpState( |
| 513 | TEST_WLAN_IFNAME, WifiManager.IFACE_IP_MODE_UNSPECIFIED); |
Erik Kline | a9cde8b | 2017-06-20 21:18:31 +0900 | [diff] [blame] | 514 | } |
Erik Kline | 9e22554 | 2017-06-08 17:48:48 +0900 | [diff] [blame] | 515 | verifyNoMoreInteractions(mNMService); |
| 516 | verifyNoMoreInteractions(mWifiManager); |
| 517 | } |
| 518 | |
Remi NGUYEN VAN | a911e84 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 519 | private void prepareUsbTethering(NetworkState upstreamState) { |
Erik Kline | 7230290 | 2018-06-14 17:36:40 +0900 | [diff] [blame] | 520 | when(mUpstreamNetworkMonitor.getCurrentPreferredUpstream()).thenReturn(upstreamState); |
Remi NGUYEN VAN | a911e84 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 521 | when(mUpstreamNetworkMonitor.selectPreferredUpstreamType(any())) |
| 522 | .thenReturn(upstreamState); |
Erik Kline | c438e30 | 2017-07-04 22:02:49 +0900 | [diff] [blame] | 523 | |
| 524 | // Emulate pressing the USB tethering button in Settings UI. |
| 525 | mTethering.startTethering(TETHERING_USB, null, false); |
| 526 | mLooper.dispatchAll(); |
Jerry Zhang | 327b809 | 2018-01-09 17:53:04 -0800 | [diff] [blame] | 527 | verify(mUsbManager, times(1)).setCurrentFunctions(UsbManager.FUNCTION_RNDIS); |
Erik Kline | c438e30 | 2017-07-04 22:02:49 +0900 | [diff] [blame] | 528 | |
Remi NGUYEN VAN | a911e84 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 529 | mTethering.interfaceStatusChanged(TEST_USB_IFNAME, true); |
| 530 | } |
| 531 | |
| 532 | @Test |
| 533 | public void testUsbConfiguredBroadcastStartsTethering() throws Exception { |
| 534 | NetworkState upstreamState = buildMobileIPv4UpstreamState(); |
| 535 | prepareUsbTethering(upstreamState); |
| 536 | |
Erik Kline | c438e30 | 2017-07-04 22:02:49 +0900 | [diff] [blame] | 537 | // This should produce no activity of any kind. |
Erik Kline | c438e30 | 2017-07-04 22:02:49 +0900 | [diff] [blame] | 538 | verifyNoMoreInteractions(mNMService); |
| 539 | |
| 540 | // Pretend we then receive USB configured broadcast. |
| 541 | sendUsbBroadcast(true, true, true); |
| 542 | mLooper.dispatchAll(); |
| 543 | // Now we should see the start of tethering mechanics (in this case: |
| 544 | // tetherMatchingInterfaces() which starts by fetching all interfaces). |
| 545 | verify(mNMService, times(1)).listInterfaces(); |
Remi NGUYEN VAN | a911e84 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 546 | |
| 547 | // UpstreamNetworkMonitor should receive selected upstream |
| 548 | verify(mUpstreamNetworkMonitor, times(1)).selectPreferredUpstreamType(any()); |
| 549 | verify(mUpstreamNetworkMonitor, times(1)).setCurrentUpstream(upstreamState.network); |
Erik Kline | c438e30 | 2017-07-04 22:02:49 +0900 | [diff] [blame] | 550 | } |
| 551 | |
| 552 | @Test |
Erik Kline | a9cde8b | 2017-06-20 21:18:31 +0900 | [diff] [blame] | 553 | public void failingLocalOnlyHotspotLegacyApBroadcastWithIfaceStatusChanged() throws Exception { |
| 554 | failingLocalOnlyHotspotLegacyApBroadcast(true); |
| 555 | } |
| 556 | |
| 557 | @Test |
| 558 | public void failingLocalOnlyHotspotLegacyApBroadcastSansIfaceStatusChanged() throws Exception { |
| 559 | failingLocalOnlyHotspotLegacyApBroadcast(false); |
| 560 | } |
| 561 | |
| 562 | public void workingLocalOnlyHotspotEnrichedApBroadcast( |
| 563 | boolean emulateInterfaceStatusChanged) throws Exception { |
Erik Kline | 9e22554 | 2017-06-08 17:48:48 +0900 | [diff] [blame] | 564 | // Emulate externally-visible WifiManager effects, causing the |
| 565 | // per-interface state machine to start up, and telling us that |
| 566 | // hotspot mode is to be started. |
Erik Kline | a9cde8b | 2017-06-20 21:18:31 +0900 | [diff] [blame] | 567 | if (emulateInterfaceStatusChanged) { |
Remi NGUYEN VAN | a911e84 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 568 | mTethering.interfaceStatusChanged(TEST_WLAN_IFNAME, true); |
Erik Kline | a9cde8b | 2017-06-20 21:18:31 +0900 | [diff] [blame] | 569 | } |
Remi NGUYEN VAN | a911e84 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 570 | 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] | 571 | mLooper.dispatchAll(); |
| 572 | |
Jimmy Chen | bcd86d0 | 2019-07-15 18:03:23 +0800 | [diff] [blame] | 573 | verifyInterfaceServingModeStarted(TEST_WLAN_IFNAME); |
Erik Kline | 465ff3a | 2018-03-09 14:18:02 +0900 | [diff] [blame] | 574 | verifyTetheringBroadcast(TEST_WLAN_IFNAME, EXTRA_AVAILABLE_TETHER); |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 575 | verify(mNMService, times(1)).setIpForwardingEnabled(true); |
| 576 | verify(mNMService, times(1)).startTethering(any(String[].class)); |
| 577 | verifyNoMoreInteractions(mNMService); |
Erik Kline | 216af6d | 2017-04-27 20:57:23 +0900 | [diff] [blame] | 578 | verify(mWifiManager).updateInterfaceIpState( |
Jianpeng Li | a70feec | 2019-05-24 17:40:15 +0900 | [diff] [blame] | 579 | TEST_WLAN_IFNAME, WifiManager.IFACE_IP_MODE_UNSPECIFIED); |
| 580 | verify(mWifiManager).updateInterfaceIpState( |
Remi NGUYEN VAN | a911e84 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 581 | TEST_WLAN_IFNAME, WifiManager.IFACE_IP_MODE_LOCAL_ONLY); |
Erik Kline | 216af6d | 2017-04-27 20:57:23 +0900 | [diff] [blame] | 582 | verifyNoMoreInteractions(mWifiManager); |
Erik Kline | 465ff3a | 2018-03-09 14:18:02 +0900 | [diff] [blame] | 583 | verifyTetheringBroadcast(TEST_WLAN_IFNAME, EXTRA_ACTIVE_LOCAL_ONLY); |
markchien | a6c7287 | 2018-11-13 18:34:56 +0900 | [diff] [blame] | 584 | verify(mUpstreamNetworkMonitor, times(1)).startObserveAllNetworks(); |
Jimmy Chen | bcd86d0 | 2019-07-15 18:03:23 +0800 | [diff] [blame] | 585 | // This will be called twice, one is on entering IpServer.STATE_AVAILABLE, |
| 586 | // and another one is on IpServer.STATE_TETHERED/IpServer.STATE_LOCAL_ONLY. |
| 587 | assertEquals(2, mTetheringDependencies.isTetheringSupportedCalls); |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 588 | |
| 589 | // Emulate externally-visible WifiManager effects, when hotspot mode |
| 590 | // is being torn down. |
| 591 | sendWifiApStateChanged(WifiManager.WIFI_AP_STATE_DISABLED); |
Remi NGUYEN VAN | a911e84 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 592 | mTethering.interfaceRemoved(TEST_WLAN_IFNAME); |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 593 | mLooper.dispatchAll(); |
| 594 | |
Remi NGUYEN VAN | a911e84 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 595 | verify(mNMService, times(1)).untetherInterface(TEST_WLAN_IFNAME); |
Jimmy Chen | bcd86d0 | 2019-07-15 18:03:23 +0800 | [diff] [blame] | 596 | // {g,s}etInterfaceConfig() called twice for enabling and disabling IPv4. |
| 597 | verify(mNMService, times(2)).getInterfaceConfig(TEST_WLAN_IFNAME); |
| 598 | verify(mNMService, times(2)) |
Remi NGUYEN VAN | a911e84 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 599 | .setInterfaceConfig(eq(TEST_WLAN_IFNAME), any(InterfaceConfiguration.class)); |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 600 | verify(mNMService, times(1)).stopTethering(); |
| 601 | verify(mNMService, times(1)).setIpForwardingEnabled(false); |
Jianpeng Li | a70feec | 2019-05-24 17:40:15 +0900 | [diff] [blame] | 602 | verify(mWifiManager, times(3)).updateInterfaceIpState( |
| 603 | TEST_WLAN_IFNAME, WifiManager.IFACE_IP_MODE_UNSPECIFIED); |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 604 | verifyNoMoreInteractions(mNMService); |
Erik Kline | 216af6d | 2017-04-27 20:57:23 +0900 | [diff] [blame] | 605 | verifyNoMoreInteractions(mWifiManager); |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 606 | // Asking for the last error after the per-interface state machine |
| 607 | // has been reaped yields an unknown interface error. |
Erik Kline | 465ff3a | 2018-03-09 14:18:02 +0900 | [diff] [blame] | 608 | assertEquals(TETHER_ERROR_UNKNOWN_IFACE, mTethering.getLastTetherError(TEST_WLAN_IFNAME)); |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 609 | } |
| 610 | |
Remi NGUYEN VAN | a911e84 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 611 | /** |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 612 | * 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] | 613 | */ |
| 614 | private void sendIPv6TetherUpdates(NetworkState upstreamState) { |
| 615 | // IPv6TetheringCoordinator must have been notified of downstream |
| 616 | verify(mIPv6TetheringCoordinator, times(1)).addActiveDownstream( |
| 617 | argThat(sm -> sm.linkProperties().getInterfaceName().equals(TEST_USB_IFNAME)), |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 618 | eq(IpServer.STATE_TETHERED)); |
Remi NGUYEN VAN | a911e84 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 619 | |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 620 | for (IpServer ipSrv : |
Remi NGUYEN VAN | a911e84 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 621 | mTetheringDependencies.ipv6CoordinatorNotifyList) { |
Remi NGUYEN VAN | 25a7e4f | 2018-03-09 14:07:18 +0900 | [diff] [blame] | 622 | NetworkState ipv6OnlyState = buildMobileUpstreamState(false, true, false); |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 623 | ipSrv.sendMessage(IpServer.CMD_IPV6_TETHER_UPDATE, 0, 0, |
paulhu | d9736de | 2019-03-08 16:35:20 +0800 | [diff] [blame] | 624 | upstreamState.linkProperties.isIpv6Provisioned() |
Remi NGUYEN VAN | a911e84 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 625 | ? ipv6OnlyState.linkProperties |
| 626 | : null); |
| 627 | } |
| 628 | mLooper.dispatchAll(); |
| 629 | } |
| 630 | |
| 631 | private void runUsbTethering(NetworkState upstreamState) { |
| 632 | prepareUsbTethering(upstreamState); |
| 633 | sendUsbBroadcast(true, true, true); |
| 634 | mLooper.dispatchAll(); |
| 635 | } |
| 636 | |
| 637 | @Test |
| 638 | public void workingMobileUsbTethering_IPv4() throws Exception { |
| 639 | NetworkState upstreamState = buildMobileIPv4UpstreamState(); |
| 640 | runUsbTethering(upstreamState); |
| 641 | |
| 642 | verify(mNMService, times(1)).enableNat(TEST_USB_IFNAME, TEST_MOBILE_IFNAME); |
| 643 | verify(mNMService, times(1)).startInterfaceForwarding(TEST_USB_IFNAME, TEST_MOBILE_IFNAME); |
| 644 | |
| 645 | sendIPv6TetherUpdates(upstreamState); |
| 646 | verify(mRouterAdvertisementDaemon, never()).buildNewRa(any(), notNull()); |
Remi NGUYEN VAN | 0e3d0923 | 2018-12-04 12:13:09 +0900 | [diff] [blame] | 647 | verify(mDhcpServer, timeout(DHCPSERVER_START_TIMEOUT_MS).times(1)).start(any()); |
Remi NGUYEN VAN | f5581f8 | 2018-09-28 11:34:15 +0900 | [diff] [blame] | 648 | } |
| 649 | |
| 650 | @Test |
| 651 | public void workingMobileUsbTethering_IPv4LegacyDhcp() { |
| 652 | Settings.Global.putInt(mContentResolver, TETHER_ENABLE_LEGACY_DHCP_SERVER, 1); |
| 653 | mTethering = makeTethering(); |
| 654 | final NetworkState upstreamState = buildMobileIPv4UpstreamState(); |
| 655 | runUsbTethering(upstreamState); |
| 656 | sendIPv6TetherUpdates(upstreamState); |
| 657 | |
Remi NGUYEN VAN | 0e3d0923 | 2018-12-04 12:13:09 +0900 | [diff] [blame] | 658 | verify(mIpServerDependencies, never()).makeDhcpServer(any(), any(), any()); |
Remi NGUYEN VAN | a911e84 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 659 | } |
| 660 | |
| 661 | @Test |
Remi NGUYEN VAN | 25a7e4f | 2018-03-09 14:07:18 +0900 | [diff] [blame] | 662 | public void workingMobileUsbTethering_IPv6() throws Exception { |
| 663 | NetworkState upstreamState = buildMobileIPv6UpstreamState(); |
| 664 | runUsbTethering(upstreamState); |
| 665 | |
| 666 | verify(mNMService, times(1)).enableNat(TEST_USB_IFNAME, TEST_MOBILE_IFNAME); |
| 667 | verify(mNMService, times(1)).startInterfaceForwarding(TEST_USB_IFNAME, TEST_MOBILE_IFNAME); |
| 668 | |
| 669 | sendIPv6TetherUpdates(upstreamState); |
| 670 | verify(mRouterAdvertisementDaemon, times(1)).buildNewRa(any(), notNull()); |
| 671 | verify(mNetd, times(1)).tetherApplyDnsInterfaces(); |
| 672 | } |
| 673 | |
| 674 | @Test |
Remi NGUYEN VAN | a911e84 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 675 | public void workingMobileUsbTethering_DualStack() throws Exception { |
| 676 | NetworkState upstreamState = buildMobileDualStackUpstreamState(); |
| 677 | runUsbTethering(upstreamState); |
| 678 | |
| 679 | verify(mNMService, times(1)).enableNat(TEST_USB_IFNAME, TEST_MOBILE_IFNAME); |
| 680 | verify(mNMService, times(1)).startInterfaceForwarding(TEST_USB_IFNAME, TEST_MOBILE_IFNAME); |
| 681 | verify(mRouterAdvertisementDaemon, times(1)).start(); |
Remi NGUYEN VAN | 0e3d0923 | 2018-12-04 12:13:09 +0900 | [diff] [blame] | 682 | verify(mDhcpServer, timeout(DHCPSERVER_START_TIMEOUT_MS).times(1)).start(any()); |
Remi NGUYEN VAN | a911e84 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 683 | |
| 684 | sendIPv6TetherUpdates(upstreamState); |
| 685 | verify(mRouterAdvertisementDaemon, times(1)).buildNewRa(any(), notNull()); |
| 686 | verify(mNetd, times(1)).tetherApplyDnsInterfaces(); |
| 687 | } |
| 688 | |
Remi NGUYEN VAN | 25a7e4f | 2018-03-09 14:07:18 +0900 | [diff] [blame] | 689 | @Test |
| 690 | public void workingMobileUsbTethering_MultipleUpstreams() throws Exception { |
| 691 | NetworkState upstreamState = buildMobile464xlatUpstreamState(); |
| 692 | runUsbTethering(upstreamState); |
| 693 | |
| 694 | verify(mNMService, times(1)).enableNat(TEST_USB_IFNAME, TEST_XLAT_MOBILE_IFNAME); |
| 695 | verify(mNMService, times(1)).enableNat(TEST_USB_IFNAME, TEST_MOBILE_IFNAME); |
Remi NGUYEN VAN | 0e3d0923 | 2018-12-04 12:13:09 +0900 | [diff] [blame] | 696 | verify(mDhcpServer, timeout(DHCPSERVER_START_TIMEOUT_MS).times(1)).start(any()); |
Remi NGUYEN VAN | 25a7e4f | 2018-03-09 14:07:18 +0900 | [diff] [blame] | 697 | verify(mNMService, times(1)).startInterfaceForwarding(TEST_USB_IFNAME, TEST_MOBILE_IFNAME); |
| 698 | verify(mNMService, times(1)).startInterfaceForwarding(TEST_USB_IFNAME, |
| 699 | TEST_XLAT_MOBILE_IFNAME); |
| 700 | |
| 701 | sendIPv6TetherUpdates(upstreamState); |
| 702 | verify(mRouterAdvertisementDaemon, times(1)).buildNewRa(any(), notNull()); |
| 703 | verify(mNetd, times(1)).tetherApplyDnsInterfaces(); |
| 704 | } |
| 705 | |
| 706 | @Test |
| 707 | public void workingMobileUsbTethering_v6Then464xlat() throws Exception { |
| 708 | // Setup IPv6 |
| 709 | NetworkState upstreamState = buildMobileIPv6UpstreamState(); |
| 710 | runUsbTethering(upstreamState); |
| 711 | |
| 712 | verify(mNMService, times(1)).enableNat(TEST_USB_IFNAME, TEST_MOBILE_IFNAME); |
Remi NGUYEN VAN | 0e3d0923 | 2018-12-04 12:13:09 +0900 | [diff] [blame] | 713 | verify(mDhcpServer, timeout(DHCPSERVER_START_TIMEOUT_MS).times(1)).start(any()); |
Remi NGUYEN VAN | 25a7e4f | 2018-03-09 14:07:18 +0900 | [diff] [blame] | 714 | verify(mNMService, times(1)).startInterfaceForwarding(TEST_USB_IFNAME, TEST_MOBILE_IFNAME); |
| 715 | |
| 716 | // Then 464xlat comes up |
| 717 | upstreamState = buildMobile464xlatUpstreamState(); |
| 718 | when(mUpstreamNetworkMonitor.selectPreferredUpstreamType(any())) |
| 719 | .thenReturn(upstreamState); |
| 720 | |
| 721 | // Upstream LinkProperties changed: UpstreamNetworkMonitor sends EVENT_ON_LINKPROPERTIES. |
| 722 | mTetheringDependencies.upstreamNetworkMonitorMasterSM.sendMessage( |
| 723 | Tethering.TetherMasterSM.EVENT_UPSTREAM_CALLBACK, |
| 724 | UpstreamNetworkMonitor.EVENT_ON_LINKPROPERTIES, |
| 725 | 0, |
| 726 | upstreamState); |
| 727 | mLooper.dispatchAll(); |
| 728 | |
| 729 | // Forwarding is added for 464xlat |
| 730 | verify(mNMService, times(1)).enableNat(TEST_USB_IFNAME, TEST_XLAT_MOBILE_IFNAME); |
| 731 | verify(mNMService, times(1)).startInterfaceForwarding(TEST_USB_IFNAME, |
| 732 | TEST_XLAT_MOBILE_IFNAME); |
| 733 | // Forwarding was not re-added for v6 (still times(1)) |
| 734 | verify(mNMService, times(1)).enableNat(TEST_USB_IFNAME, TEST_MOBILE_IFNAME); |
| 735 | verify(mNMService, times(1)).startInterfaceForwarding(TEST_USB_IFNAME, TEST_MOBILE_IFNAME); |
Remi NGUYEN VAN | f5581f8 | 2018-09-28 11:34:15 +0900 | [diff] [blame] | 736 | // DHCP not restarted on downstream (still times(1)) |
Remi NGUYEN VAN | 0e3d0923 | 2018-12-04 12:13:09 +0900 | [diff] [blame] | 737 | verify(mDhcpServer, timeout(DHCPSERVER_START_TIMEOUT_MS).times(1)).start(any()); |
Remi NGUYEN VAN | 25a7e4f | 2018-03-09 14:07:18 +0900 | [diff] [blame] | 738 | } |
Remi NGUYEN VAN | a911e84 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 739 | |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 740 | @Test |
Erik Kline | 7230290 | 2018-06-14 17:36:40 +0900 | [diff] [blame] | 741 | public void configTetherUpstreamAutomaticIgnoresConfigTetherUpstreamTypes() throws Exception { |
| 742 | when(mResources.getBoolean(com.android.internal.R.bool.config_tether_upstream_automatic)) |
| 743 | .thenReturn(true); |
| 744 | sendConfigurationChanged(); |
| 745 | |
| 746 | // Setup IPv6 |
| 747 | final NetworkState upstreamState = buildMobileIPv6UpstreamState(); |
| 748 | runUsbTethering(upstreamState); |
| 749 | |
| 750 | // UpstreamNetworkMonitor should choose upstream automatically |
| 751 | // (in this specific case: choose the default network). |
| 752 | verify(mUpstreamNetworkMonitor, times(1)).getCurrentPreferredUpstream(); |
| 753 | verify(mUpstreamNetworkMonitor, never()).selectPreferredUpstreamType(any()); |
| 754 | |
| 755 | verify(mUpstreamNetworkMonitor, times(1)).setCurrentUpstream(upstreamState.network); |
| 756 | } |
| 757 | |
| 758 | @Test |
Erik Kline | a9cde8b | 2017-06-20 21:18:31 +0900 | [diff] [blame] | 759 | public void workingLocalOnlyHotspotEnrichedApBroadcastWithIfaceChanged() throws Exception { |
| 760 | workingLocalOnlyHotspotEnrichedApBroadcast(true); |
| 761 | } |
| 762 | |
| 763 | @Test |
| 764 | public void workingLocalOnlyHotspotEnrichedApBroadcastSansIfaceChanged() throws Exception { |
| 765 | workingLocalOnlyHotspotEnrichedApBroadcast(false); |
| 766 | } |
| 767 | |
| 768 | // TODO: Test with and without interfaceStatusChanged(). |
| 769 | @Test |
Erik Kline | 9e22554 | 2017-06-08 17:48:48 +0900 | [diff] [blame] | 770 | public void failingWifiTetheringLegacyApBroadcast() throws Exception { |
Erik Kline | ceb54c6 | 2017-04-18 14:22:25 +0900 | [diff] [blame] | 771 | when(mWifiManager.startSoftAp(any(WifiConfiguration.class))).thenReturn(true); |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 772 | |
| 773 | // Emulate pressing the WiFi tethering button. |
Erik Kline | c438e30 | 2017-07-04 22:02:49 +0900 | [diff] [blame] | 774 | mTethering.startTethering(TETHERING_WIFI, null, false); |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 775 | mLooper.dispatchAll(); |
Erik Kline | ceb54c6 | 2017-04-18 14:22:25 +0900 | [diff] [blame] | 776 | verify(mWifiManager, times(1)).startSoftAp(null); |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 777 | verifyNoMoreInteractions(mWifiManager); |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 778 | verifyNoMoreInteractions(mNMService); |
| 779 | |
| 780 | // Emulate externally-visible WifiManager effects, causing the |
| 781 | // per-interface state machine to start up, and telling us that |
| 782 | // tethering mode is to be started. |
Remi NGUYEN VAN | a911e84 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 783 | mTethering.interfaceStatusChanged(TEST_WLAN_IFNAME, true); |
Erik Kline | 9e22554 | 2017-06-08 17:48:48 +0900 | [diff] [blame] | 784 | sendWifiApStateChanged(WIFI_AP_STATE_ENABLED); |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 785 | mLooper.dispatchAll(); |
| 786 | |
Erik Kline | 465ff3a | 2018-03-09 14:18:02 +0900 | [diff] [blame] | 787 | assertEquals(1, mTetheringDependencies.isTetheringSupportedCalls); |
| 788 | verifyTetheringBroadcast(TEST_WLAN_IFNAME, EXTRA_AVAILABLE_TETHER); |
Jianpeng Li | a70feec | 2019-05-24 17:40:15 +0900 | [diff] [blame] | 789 | verify(mWifiManager).updateInterfaceIpState( |
| 790 | TEST_WLAN_IFNAME, WifiManager.IFACE_IP_MODE_UNSPECIFIED); |
Erik Kline | 9e22554 | 2017-06-08 17:48:48 +0900 | [diff] [blame] | 791 | verifyNoMoreInteractions(mNMService); |
| 792 | verifyNoMoreInteractions(mWifiManager); |
| 793 | } |
| 794 | |
Erik Kline | a9cde8b | 2017-06-20 21:18:31 +0900 | [diff] [blame] | 795 | // TODO: Test with and without interfaceStatusChanged(). |
Erik Kline | 9e22554 | 2017-06-08 17:48:48 +0900 | [diff] [blame] | 796 | @Test |
| 797 | public void workingWifiTetheringEnrichedApBroadcast() throws Exception { |
Erik Kline | 9e22554 | 2017-06-08 17:48:48 +0900 | [diff] [blame] | 798 | when(mWifiManager.startSoftAp(any(WifiConfiguration.class))).thenReturn(true); |
| 799 | |
| 800 | // Emulate pressing the WiFi tethering button. |
Erik Kline | c438e30 | 2017-07-04 22:02:49 +0900 | [diff] [blame] | 801 | mTethering.startTethering(TETHERING_WIFI, null, false); |
Erik Kline | 9e22554 | 2017-06-08 17:48:48 +0900 | [diff] [blame] | 802 | mLooper.dispatchAll(); |
| 803 | verify(mWifiManager, times(1)).startSoftAp(null); |
| 804 | verifyNoMoreInteractions(mWifiManager); |
Erik Kline | 9e22554 | 2017-06-08 17:48:48 +0900 | [diff] [blame] | 805 | verifyNoMoreInteractions(mNMService); |
| 806 | |
| 807 | // Emulate externally-visible WifiManager effects, causing the |
| 808 | // per-interface state machine to start up, and telling us that |
| 809 | // tethering mode is to be started. |
Remi NGUYEN VAN | a911e84 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 810 | mTethering.interfaceStatusChanged(TEST_WLAN_IFNAME, true); |
| 811 | sendWifiApStateChanged(WIFI_AP_STATE_ENABLED, TEST_WLAN_IFNAME, IFACE_IP_MODE_TETHERED); |
Erik Kline | 9e22554 | 2017-06-08 17:48:48 +0900 | [diff] [blame] | 812 | mLooper.dispatchAll(); |
| 813 | |
Jimmy Chen | bcd86d0 | 2019-07-15 18:03:23 +0800 | [diff] [blame] | 814 | verifyInterfaceServingModeStarted(TEST_WLAN_IFNAME); |
Erik Kline | 465ff3a | 2018-03-09 14:18:02 +0900 | [diff] [blame] | 815 | verifyTetheringBroadcast(TEST_WLAN_IFNAME, EXTRA_AVAILABLE_TETHER); |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 816 | verify(mNMService, times(1)).setIpForwardingEnabled(true); |
| 817 | verify(mNMService, times(1)).startTethering(any(String[].class)); |
| 818 | verifyNoMoreInteractions(mNMService); |
Erik Kline | 216af6d | 2017-04-27 20:57:23 +0900 | [diff] [blame] | 819 | verify(mWifiManager).updateInterfaceIpState( |
Jianpeng Li | a70feec | 2019-05-24 17:40:15 +0900 | [diff] [blame] | 820 | TEST_WLAN_IFNAME, WifiManager.IFACE_IP_MODE_UNSPECIFIED); |
| 821 | verify(mWifiManager).updateInterfaceIpState( |
Remi NGUYEN VAN | a911e84 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 822 | TEST_WLAN_IFNAME, WifiManager.IFACE_IP_MODE_TETHERED); |
Erik Kline | 216af6d | 2017-04-27 20:57:23 +0900 | [diff] [blame] | 823 | verifyNoMoreInteractions(mWifiManager); |
Erik Kline | 465ff3a | 2018-03-09 14:18:02 +0900 | [diff] [blame] | 824 | verifyTetheringBroadcast(TEST_WLAN_IFNAME, EXTRA_ACTIVE_TETHER); |
markchien | a6c7287 | 2018-11-13 18:34:56 +0900 | [diff] [blame] | 825 | verify(mUpstreamNetworkMonitor, times(1)).startObserveAllNetworks(); |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 826 | // In tethering mode, in the default configuration, an explicit request |
| 827 | // for a mobile network is also made. |
Remi NGUYEN VAN | a911e84 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 828 | verify(mUpstreamNetworkMonitor, times(1)).registerMobileNetworkRequest(); |
Jimmy Chen | bcd86d0 | 2019-07-15 18:03:23 +0800 | [diff] [blame] | 829 | // This will be called twice, one is on entering IpServer.STATE_AVAILABLE, |
| 830 | // and another one is on IpServer.STATE_TETHERED/IpServer.STATE_LOCAL_ONLY. |
| 831 | assertEquals(2, mTetheringDependencies.isTetheringSupportedCalls); |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 832 | |
| 833 | ///// |
| 834 | // We do not currently emulate any upstream being found. |
| 835 | // |
| 836 | // This is why there are no calls to verify mNMService.enableNat() or |
| 837 | // mNMService.startInterfaceForwarding(). |
| 838 | ///// |
| 839 | |
| 840 | // Emulate pressing the WiFi tethering button. |
Erik Kline | c438e30 | 2017-07-04 22:02:49 +0900 | [diff] [blame] | 841 | mTethering.stopTethering(TETHERING_WIFI); |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 842 | mLooper.dispatchAll(); |
Erik Kline | ceb54c6 | 2017-04-18 14:22:25 +0900 | [diff] [blame] | 843 | verify(mWifiManager, times(1)).stopSoftAp(); |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 844 | verifyNoMoreInteractions(mWifiManager); |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 845 | verifyNoMoreInteractions(mNMService); |
| 846 | |
| 847 | // Emulate externally-visible WifiManager effects, when tethering mode |
| 848 | // is being torn down. |
| 849 | sendWifiApStateChanged(WifiManager.WIFI_AP_STATE_DISABLED); |
Remi NGUYEN VAN | a911e84 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 850 | mTethering.interfaceRemoved(TEST_WLAN_IFNAME); |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 851 | mLooper.dispatchAll(); |
| 852 | |
Remi NGUYEN VAN | a911e84 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 853 | verify(mNMService, times(1)).untetherInterface(TEST_WLAN_IFNAME); |
Jimmy Chen | bcd86d0 | 2019-07-15 18:03:23 +0800 | [diff] [blame] | 854 | // {g,s}etInterfaceConfig() called twice for enabling and disabling IPv4. |
Remi NGUYEN VAN | a911e84 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 855 | verify(mNMService, atLeastOnce()).getInterfaceConfig(TEST_WLAN_IFNAME); |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 856 | verify(mNMService, atLeastOnce()) |
Remi NGUYEN VAN | a911e84 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 857 | .setInterfaceConfig(eq(TEST_WLAN_IFNAME), any(InterfaceConfiguration.class)); |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 858 | verify(mNMService, times(1)).stopTethering(); |
| 859 | verify(mNMService, times(1)).setIpForwardingEnabled(false); |
Jianpeng Li | a70feec | 2019-05-24 17:40:15 +0900 | [diff] [blame] | 860 | verify(mWifiManager, times(3)).updateInterfaceIpState( |
| 861 | TEST_WLAN_IFNAME, WifiManager.IFACE_IP_MODE_UNSPECIFIED); |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 862 | verifyNoMoreInteractions(mNMService); |
Erik Kline | 216af6d | 2017-04-27 20:57:23 +0900 | [diff] [blame] | 863 | verifyNoMoreInteractions(mWifiManager); |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 864 | // Asking for the last error after the per-interface state machine |
| 865 | // has been reaped yields an unknown interface error. |
Erik Kline | 465ff3a | 2018-03-09 14:18:02 +0900 | [diff] [blame] | 866 | assertEquals(TETHER_ERROR_UNKNOWN_IFACE, mTethering.getLastTetherError(TEST_WLAN_IFNAME)); |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 867 | } |
| 868 | |
Erik Kline | a9cde8b | 2017-06-20 21:18:31 +0900 | [diff] [blame] | 869 | // TODO: Test with and without interfaceStatusChanged(). |
Erik Kline | 1fdc2e2 | 2017-05-08 17:56:35 +0900 | [diff] [blame] | 870 | @Test |
| 871 | public void failureEnablingIpForwarding() throws Exception { |
Erik Kline | 1fdc2e2 | 2017-05-08 17:56:35 +0900 | [diff] [blame] | 872 | when(mWifiManager.startSoftAp(any(WifiConfiguration.class))).thenReturn(true); |
| 873 | doThrow(new RemoteException()).when(mNMService).setIpForwardingEnabled(true); |
| 874 | |
| 875 | // Emulate pressing the WiFi tethering button. |
Erik Kline | c438e30 | 2017-07-04 22:02:49 +0900 | [diff] [blame] | 876 | mTethering.startTethering(TETHERING_WIFI, null, false); |
Erik Kline | 1fdc2e2 | 2017-05-08 17:56:35 +0900 | [diff] [blame] | 877 | mLooper.dispatchAll(); |
| 878 | verify(mWifiManager, times(1)).startSoftAp(null); |
| 879 | verifyNoMoreInteractions(mWifiManager); |
Erik Kline | 1fdc2e2 | 2017-05-08 17:56:35 +0900 | [diff] [blame] | 880 | verifyNoMoreInteractions(mNMService); |
| 881 | |
| 882 | // Emulate externally-visible WifiManager effects, causing the |
| 883 | // per-interface state machine to start up, and telling us that |
| 884 | // tethering mode is to be started. |
Remi NGUYEN VAN | a911e84 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 885 | mTethering.interfaceStatusChanged(TEST_WLAN_IFNAME, true); |
| 886 | sendWifiApStateChanged(WIFI_AP_STATE_ENABLED, TEST_WLAN_IFNAME, IFACE_IP_MODE_TETHERED); |
Erik Kline | 1fdc2e2 | 2017-05-08 17:56:35 +0900 | [diff] [blame] | 887 | mLooper.dispatchAll(); |
| 888 | |
Remi NGUYEN VAN | d1b51a3 | 2019-01-22 16:13:57 +0900 | [diff] [blame] | 889 | // We verify get/set called thrice here: twice for setup (on NMService) and once during |
| 890 | // teardown (on Netd) because all events happen over the course of the single |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 891 | // dispatchAll() above. Note that once the IpServer IPv4 address config |
Erik Kline | 472276a | 2018-05-18 16:09:24 +0900 | [diff] [blame] | 892 | // 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] | 893 | verify(mNMService, times(2)).getInterfaceConfig(TEST_WLAN_IFNAME); |
Remi NGUYEN VAN | d1b51a3 | 2019-01-22 16:13:57 +0900 | [diff] [blame] | 894 | verify(mNMService, times(2)) |
Remi NGUYEN VAN | a911e84 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 895 | .setInterfaceConfig(eq(TEST_WLAN_IFNAME), any(InterfaceConfiguration.class)); |
Remi NGUYEN VAN | d1b51a3 | 2019-01-22 16:13:57 +0900 | [diff] [blame] | 896 | verify(mNetd, times(1)).interfaceSetCfg(argThat(p -> TEST_WLAN_IFNAME.equals(p.ifName))); |
Remi NGUYEN VAN | a911e84 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 897 | verify(mNMService, times(1)).tetherInterface(TEST_WLAN_IFNAME); |
Erik Kline | 1fdc2e2 | 2017-05-08 17:56:35 +0900 | [diff] [blame] | 898 | verify(mWifiManager).updateInterfaceIpState( |
Jianpeng Li | a70feec | 2019-05-24 17:40:15 +0900 | [diff] [blame] | 899 | TEST_WLAN_IFNAME, WifiManager.IFACE_IP_MODE_UNSPECIFIED); |
| 900 | verify(mWifiManager).updateInterfaceIpState( |
Remi NGUYEN VAN | a911e84 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 901 | TEST_WLAN_IFNAME, WifiManager.IFACE_IP_MODE_TETHERED); |
Jimmy Chen | bcd86d0 | 2019-07-15 18:03:23 +0800 | [diff] [blame] | 902 | // There are 3 state change event: |
| 903 | // AVAILABLE -> STATE_TETHERED -> STATE_AVAILABLE. |
| 904 | assertEquals(3, mTetheringDependencies.isTetheringSupportedCalls); |
Erik Kline | 465ff3a | 2018-03-09 14:18:02 +0900 | [diff] [blame] | 905 | verifyTetheringBroadcast(TEST_WLAN_IFNAME, EXTRA_AVAILABLE_TETHER); |
Erik Kline | 1fdc2e2 | 2017-05-08 17:56:35 +0900 | [diff] [blame] | 906 | // This is called, but will throw. |
| 907 | verify(mNMService, times(1)).setIpForwardingEnabled(true); |
| 908 | // This never gets called because of the exception thrown above. |
| 909 | verify(mNMService, times(0)).startTethering(any(String[].class)); |
| 910 | // When the master state machine transitions to an error state it tells |
| 911 | // downstream interfaces, which causes us to tell Wi-Fi about the error |
| 912 | // so it can take down AP mode. |
Remi NGUYEN VAN | a911e84 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 913 | verify(mNMService, times(1)).untetherInterface(TEST_WLAN_IFNAME); |
Erik Kline | 1fdc2e2 | 2017-05-08 17:56:35 +0900 | [diff] [blame] | 914 | verify(mWifiManager).updateInterfaceIpState( |
Remi NGUYEN VAN | a911e84 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 915 | TEST_WLAN_IFNAME, WifiManager.IFACE_IP_MODE_CONFIGURATION_ERROR); |
Erik Kline | 1fdc2e2 | 2017-05-08 17:56:35 +0900 | [diff] [blame] | 916 | |
| 917 | verifyNoMoreInteractions(mWifiManager); |
Erik Kline | 1fdc2e2 | 2017-05-08 17:56:35 +0900 | [diff] [blame] | 918 | verifyNoMoreInteractions(mNMService); |
| 919 | } |
| 920 | |
Alexandru-Andrei Rotaru | fa6d5c5 | 2017-07-18 16:49:22 +0100 | [diff] [blame] | 921 | private void userRestrictionsListenerBehaviour( |
| 922 | boolean currentDisallow, boolean nextDisallow, String[] activeTetheringIfacesList, |
| 923 | int expectedInteractionsWithShowNotification) throws Exception { |
| 924 | final int userId = 0; |
| 925 | final Bundle currRestrictions = new Bundle(); |
| 926 | final Bundle newRestrictions = new Bundle(); |
| 927 | Tethering tethering = mock(Tethering.class); |
| 928 | Tethering.TetheringUserRestrictionListener turl = |
| 929 | new Tethering.TetheringUserRestrictionListener(tethering); |
| 930 | |
| 931 | currRestrictions.putBoolean(UserManager.DISALLOW_CONFIG_TETHERING, currentDisallow); |
| 932 | newRestrictions.putBoolean(UserManager.DISALLOW_CONFIG_TETHERING, nextDisallow); |
| 933 | when(tethering.getTetheredIfaces()).thenReturn(activeTetheringIfacesList); |
| 934 | |
| 935 | turl.onUserRestrictionsChanged(userId, newRestrictions, currRestrictions); |
| 936 | |
| 937 | verify(tethering, times(expectedInteractionsWithShowNotification)) |
| 938 | .showTetheredNotification(anyInt(), eq(false)); |
| 939 | |
| 940 | verify(tethering, times(expectedInteractionsWithShowNotification)).untetherAll(); |
| 941 | } |
| 942 | |
| 943 | @Test |
| 944 | public void testDisallowTetheringWhenNoTetheringInterfaceIsActive() throws Exception { |
| 945 | final String[] emptyActiveIfacesList = new String[]{}; |
| 946 | final boolean currDisallow = false; |
| 947 | final boolean nextDisallow = true; |
| 948 | final int expectedInteractionsWithShowNotification = 0; |
| 949 | |
| 950 | userRestrictionsListenerBehaviour(currDisallow, nextDisallow, emptyActiveIfacesList, |
| 951 | expectedInteractionsWithShowNotification); |
| 952 | } |
| 953 | |
| 954 | @Test |
| 955 | public void testDisallowTetheringWhenAtLeastOneTetheringInterfaceIsActive() throws Exception { |
Remi NGUYEN VAN | a911e84 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 956 | final String[] nonEmptyActiveIfacesList = new String[]{TEST_WLAN_IFNAME}; |
Alexandru-Andrei Rotaru | fa6d5c5 | 2017-07-18 16:49:22 +0100 | [diff] [blame] | 957 | final boolean currDisallow = false; |
| 958 | final boolean nextDisallow = true; |
| 959 | final int expectedInteractionsWithShowNotification = 1; |
| 960 | |
| 961 | userRestrictionsListenerBehaviour(currDisallow, nextDisallow, nonEmptyActiveIfacesList, |
| 962 | expectedInteractionsWithShowNotification); |
| 963 | } |
| 964 | |
| 965 | @Test |
| 966 | public void testAllowTetheringWhenNoTetheringInterfaceIsActive() throws Exception { |
| 967 | final String[] nonEmptyActiveIfacesList = new String[]{}; |
| 968 | final boolean currDisallow = true; |
| 969 | final boolean nextDisallow = false; |
| 970 | final int expectedInteractionsWithShowNotification = 0; |
| 971 | |
| 972 | userRestrictionsListenerBehaviour(currDisallow, nextDisallow, nonEmptyActiveIfacesList, |
| 973 | expectedInteractionsWithShowNotification); |
| 974 | } |
| 975 | |
| 976 | @Test |
| 977 | public void testAllowTetheringWhenAtLeastOneTetheringInterfaceIsActive() throws Exception { |
Remi NGUYEN VAN | a911e84 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 978 | final String[] nonEmptyActiveIfacesList = new String[]{TEST_WLAN_IFNAME}; |
Alexandru-Andrei Rotaru | fa6d5c5 | 2017-07-18 16:49:22 +0100 | [diff] [blame] | 979 | final boolean currDisallow = true; |
| 980 | final boolean nextDisallow = false; |
| 981 | final int expectedInteractionsWithShowNotification = 0; |
| 982 | |
| 983 | userRestrictionsListenerBehaviour(currDisallow, nextDisallow, nonEmptyActiveIfacesList, |
| 984 | expectedInteractionsWithShowNotification); |
| 985 | } |
| 986 | |
| 987 | @Test |
| 988 | public void testDisallowTetheringUnchanged() throws Exception { |
Remi NGUYEN VAN | a911e84 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 989 | final String[] nonEmptyActiveIfacesList = new String[]{TEST_WLAN_IFNAME}; |
Alexandru-Andrei Rotaru | fa6d5c5 | 2017-07-18 16:49:22 +0100 | [diff] [blame] | 990 | final int expectedInteractionsWithShowNotification = 0; |
| 991 | boolean currDisallow = true; |
| 992 | boolean nextDisallow = true; |
| 993 | |
| 994 | userRestrictionsListenerBehaviour(currDisallow, nextDisallow, nonEmptyActiveIfacesList, |
| 995 | expectedInteractionsWithShowNotification); |
| 996 | |
| 997 | currDisallow = false; |
| 998 | nextDisallow = false; |
| 999 | |
| 1000 | userRestrictionsListenerBehaviour(currDisallow, nextDisallow, nonEmptyActiveIfacesList, |
| 1001 | expectedInteractionsWithShowNotification); |
| 1002 | } |
| 1003 | |
markchien | 26299ed | 2019-02-27 14:56:11 +0800 | [diff] [blame] | 1004 | private class TestTetheringEventCallback extends ITetheringEventCallback.Stub { |
| 1005 | private final ArrayList<Network> mActualUpstreams = new ArrayList<>(); |
| 1006 | |
| 1007 | public void expectUpstreamChanged(Network... networks) { |
| 1008 | final ArrayList<Network> expectedUpstreams = |
| 1009 | new ArrayList<Network>(Arrays.asList(networks)); |
| 1010 | for (Network upstream : expectedUpstreams) { |
| 1011 | // throws OOB if no expectations |
| 1012 | assertEquals(mActualUpstreams.remove(0), upstream); |
| 1013 | } |
| 1014 | assertNoCallback(); |
| 1015 | } |
| 1016 | |
| 1017 | @Override |
| 1018 | public void onUpstreamChanged(Network network) { |
| 1019 | mActualUpstreams.add(network); |
| 1020 | } |
| 1021 | |
| 1022 | public void assertNoCallback() { |
| 1023 | assertTrue(mActualUpstreams.isEmpty()); |
| 1024 | } |
| 1025 | } |
| 1026 | |
| 1027 | @Test |
| 1028 | public void testRegisterTetheringEventCallback() throws Exception { |
| 1029 | TestTetheringEventCallback callback1 = new TestTetheringEventCallback(); |
| 1030 | TestTetheringEventCallback callback2 = new TestTetheringEventCallback(); |
| 1031 | |
| 1032 | // 1. Register one callback and run usb tethering. |
| 1033 | mTethering.registerTetheringEventCallback(callback1); |
| 1034 | mLooper.dispatchAll(); |
| 1035 | callback1.expectUpstreamChanged(new Network[] {null}); |
| 1036 | NetworkState upstreamState = buildMobileDualStackUpstreamState(); |
| 1037 | runUsbTethering(upstreamState); |
| 1038 | callback1.expectUpstreamChanged(upstreamState.network); |
| 1039 | // 2. Register second callback. |
| 1040 | mTethering.registerTetheringEventCallback(callback2); |
| 1041 | mLooper.dispatchAll(); |
| 1042 | callback2.expectUpstreamChanged(upstreamState.network); |
| 1043 | // 3. Disable usb tethering. |
| 1044 | mTethering.stopTethering(TETHERING_USB); |
| 1045 | mLooper.dispatchAll(); |
| 1046 | sendUsbBroadcast(false, false, false); |
| 1047 | mLooper.dispatchAll(); |
| 1048 | callback1.expectUpstreamChanged(new Network[] {null}); |
| 1049 | callback2.expectUpstreamChanged(new Network[] {null}); |
| 1050 | // 4. Unregister first callback and run hotspot. |
| 1051 | mTethering.unregisterTetheringEventCallback(callback1); |
| 1052 | mLooper.dispatchAll(); |
| 1053 | when(mUpstreamNetworkMonitor.getCurrentPreferredUpstream()).thenReturn(upstreamState); |
| 1054 | when(mUpstreamNetworkMonitor.selectPreferredUpstreamType(any())) |
| 1055 | .thenReturn(upstreamState); |
| 1056 | when(mWifiManager.startSoftAp(any(WifiConfiguration.class))).thenReturn(true); |
| 1057 | mTethering.startTethering(TETHERING_WIFI, null, false); |
| 1058 | mLooper.dispatchAll(); |
| 1059 | mTethering.interfaceStatusChanged(TEST_WLAN_IFNAME, true); |
| 1060 | sendWifiApStateChanged(WIFI_AP_STATE_ENABLED, TEST_WLAN_IFNAME, IFACE_IP_MODE_TETHERED); |
| 1061 | mLooper.dispatchAll(); |
| 1062 | callback1.assertNoCallback(); |
| 1063 | callback2.expectUpstreamChanged(upstreamState.network); |
| 1064 | } |
Alexandru-Andrei Rotaru | fa6d5c5 | 2017-07-18 16:49:22 +0100 | [diff] [blame] | 1065 | |
markchien | 04bdf87 | 2019-06-17 21:05:34 +0800 | [diff] [blame] | 1066 | @Test |
| 1067 | public void testMultiSimAware() throws Exception { |
| 1068 | final TetheringConfiguration initailConfig = mTethering.getTetheringConfiguration(); |
| 1069 | assertEquals(INVALID_SUBSCRIPTION_ID, initailConfig.subId); |
| 1070 | |
| 1071 | final int fakeSubId = 1234; |
| 1072 | mPhoneStateListener.onActiveDataSubscriptionIdChanged(fakeSubId); |
| 1073 | final TetheringConfiguration newConfig = mTethering.getTetheringConfiguration(); |
| 1074 | assertEquals(fakeSubId, newConfig.subId); |
| 1075 | } |
| 1076 | |
Jimmy Chen | bcd86d0 | 2019-07-15 18:03:23 +0800 | [diff] [blame] | 1077 | private void workingWifiP2pGroupOwner( |
| 1078 | boolean emulateInterfaceStatusChanged) throws Exception { |
| 1079 | if (emulateInterfaceStatusChanged) { |
| 1080 | mTethering.interfaceStatusChanged(TEST_P2P_IFNAME, true); |
| 1081 | } |
| 1082 | sendWifiP2pConnectionChanged(true, true, TEST_P2P_IFNAME); |
| 1083 | mLooper.dispatchAll(); |
| 1084 | |
| 1085 | verifyInterfaceServingModeStarted(TEST_P2P_IFNAME); |
| 1086 | verifyTetheringBroadcast(TEST_P2P_IFNAME, EXTRA_AVAILABLE_TETHER); |
| 1087 | verify(mNMService, times(1)).setIpForwardingEnabled(true); |
| 1088 | verify(mNMService, times(1)).startTethering(any(String[].class)); |
| 1089 | verifyNoMoreInteractions(mNMService); |
| 1090 | verifyTetheringBroadcast(TEST_P2P_IFNAME, EXTRA_ACTIVE_LOCAL_ONLY); |
| 1091 | verify(mUpstreamNetworkMonitor, times(1)).startObserveAllNetworks(); |
| 1092 | // This will be called twice, one is on entering IpServer.STATE_AVAILABLE, |
| 1093 | // and another one is on IpServer.STATE_TETHERED/IpServer.STATE_LOCAL_ONLY. |
| 1094 | assertEquals(2, mTetheringDependencies.isTetheringSupportedCalls); |
| 1095 | |
| 1096 | assertEquals(TETHER_ERROR_NO_ERROR, mTethering.getLastTetherError(TEST_P2P_IFNAME)); |
| 1097 | |
| 1098 | // Emulate externally-visible WifiP2pManager effects, when wifi p2p group |
| 1099 | // is being removed. |
| 1100 | sendWifiP2pConnectionChanged(false, true, TEST_P2P_IFNAME); |
| 1101 | mTethering.interfaceRemoved(TEST_P2P_IFNAME); |
| 1102 | mLooper.dispatchAll(); |
| 1103 | |
| 1104 | verify(mNMService, times(1)).untetherInterface(TEST_P2P_IFNAME); |
| 1105 | // {g,s}etInterfaceConfig() called twice for enabling and disabling IPv4. |
| 1106 | verify(mNMService, times(2)).getInterfaceConfig(TEST_P2P_IFNAME); |
| 1107 | verify(mNMService, times(2)) |
| 1108 | .setInterfaceConfig(eq(TEST_P2P_IFNAME), any(InterfaceConfiguration.class)); |
| 1109 | verify(mNMService, times(1)).stopTethering(); |
| 1110 | verify(mNMService, times(1)).setIpForwardingEnabled(false); |
| 1111 | verify(mUpstreamNetworkMonitor, never()).getCurrentPreferredUpstream(); |
| 1112 | verify(mUpstreamNetworkMonitor, never()).selectPreferredUpstreamType(any()); |
| 1113 | verifyNoMoreInteractions(mNMService); |
| 1114 | // Asking for the last error after the per-interface state machine |
| 1115 | // has been reaped yields an unknown interface error. |
| 1116 | assertEquals(TETHER_ERROR_UNKNOWN_IFACE, mTethering.getLastTetherError(TEST_P2P_IFNAME)); |
| 1117 | } |
| 1118 | |
| 1119 | private void workingWifiP2pGroupClient( |
| 1120 | boolean emulateInterfaceStatusChanged) throws Exception { |
| 1121 | if (emulateInterfaceStatusChanged) { |
| 1122 | mTethering.interfaceStatusChanged(TEST_P2P_IFNAME, true); |
| 1123 | } |
| 1124 | sendWifiP2pConnectionChanged(true, false, TEST_P2P_IFNAME); |
| 1125 | mLooper.dispatchAll(); |
| 1126 | |
| 1127 | verify(mNMService, never()).getInterfaceConfig(TEST_P2P_IFNAME); |
| 1128 | verify(mNMService, never()) |
| 1129 | .setInterfaceConfig(eq(TEST_P2P_IFNAME), any(InterfaceConfiguration.class)); |
| 1130 | verify(mNMService, never()).tetherInterface(TEST_P2P_IFNAME); |
| 1131 | verify(mNMService, never()).setIpForwardingEnabled(true); |
| 1132 | verify(mNMService, never()).startTethering(any(String[].class)); |
| 1133 | |
| 1134 | // Emulate externally-visible WifiP2pManager effects, when wifi p2p group |
| 1135 | // is being removed. |
| 1136 | sendWifiP2pConnectionChanged(false, false, TEST_P2P_IFNAME); |
| 1137 | mTethering.interfaceRemoved(TEST_P2P_IFNAME); |
| 1138 | mLooper.dispatchAll(); |
| 1139 | |
| 1140 | verify(mNMService, never()).untetherInterface(TEST_P2P_IFNAME); |
| 1141 | verify(mNMService, never()).getInterfaceConfig(TEST_P2P_IFNAME); |
| 1142 | verify(mNMService, never()) |
| 1143 | .setInterfaceConfig(eq(TEST_P2P_IFNAME), any(InterfaceConfiguration.class)); |
| 1144 | verify(mNMService, never()).stopTethering(); |
| 1145 | verify(mNMService, never()).setIpForwardingEnabled(false); |
| 1146 | verifyNoMoreInteractions(mNMService); |
| 1147 | // Asking for the last error after the per-interface state machine |
| 1148 | // has been reaped yields an unknown interface error. |
| 1149 | assertEquals(TETHER_ERROR_UNKNOWN_IFACE, mTethering.getLastTetherError(TEST_P2P_IFNAME)); |
| 1150 | } |
| 1151 | |
| 1152 | @Test |
| 1153 | public void workingWifiP2pGroupOwnerWithIfaceChanged() throws Exception { |
| 1154 | workingWifiP2pGroupOwner(true); |
| 1155 | } |
| 1156 | |
| 1157 | @Test |
| 1158 | public void workingWifiP2pGroupOwnerSansIfaceChanged() throws Exception { |
| 1159 | workingWifiP2pGroupOwner(false); |
| 1160 | } |
| 1161 | |
| 1162 | private void workingWifiP2pGroupOwnerLegacyMode( |
| 1163 | boolean emulateInterfaceStatusChanged) throws Exception { |
| 1164 | // change to legacy mode and update tethering information by chaning SIM |
| 1165 | when(mResources.getStringArray(com.android.internal.R.array.config_tether_wifi_p2p_regexs)) |
| 1166 | .thenReturn(new String[]{}); |
| 1167 | final int fakeSubId = 1234; |
| 1168 | mPhoneStateListener.onActiveDataSubscriptionIdChanged(fakeSubId); |
| 1169 | |
| 1170 | if (emulateInterfaceStatusChanged) { |
| 1171 | mTethering.interfaceStatusChanged(TEST_P2P_IFNAME, true); |
| 1172 | } |
| 1173 | sendWifiP2pConnectionChanged(true, true, TEST_P2P_IFNAME); |
| 1174 | mLooper.dispatchAll(); |
| 1175 | |
| 1176 | verify(mNMService, never()).getInterfaceConfig(TEST_P2P_IFNAME); |
| 1177 | verify(mNMService, never()) |
| 1178 | .setInterfaceConfig(eq(TEST_P2P_IFNAME), any(InterfaceConfiguration.class)); |
| 1179 | verify(mNMService, never()).tetherInterface(TEST_P2P_IFNAME); |
| 1180 | verify(mNMService, never()).setIpForwardingEnabled(true); |
| 1181 | verify(mNMService, never()).startTethering(any(String[].class)); |
| 1182 | assertEquals(TETHER_ERROR_UNKNOWN_IFACE, mTethering.getLastTetherError(TEST_P2P_IFNAME)); |
| 1183 | } |
| 1184 | @Test |
| 1185 | public void workingWifiP2pGroupOwnerLegacyModeWithIfaceChanged() throws Exception { |
| 1186 | workingWifiP2pGroupOwnerLegacyMode(true); |
| 1187 | } |
| 1188 | |
| 1189 | @Test |
| 1190 | public void workingWifiP2pGroupOwnerLegacyModeSansIfaceChanged() throws Exception { |
| 1191 | workingWifiP2pGroupOwnerLegacyMode(false); |
| 1192 | } |
| 1193 | |
| 1194 | @Test |
| 1195 | public void workingWifiP2pGroupClientWithIfaceChanged() throws Exception { |
| 1196 | workingWifiP2pGroupClient(true); |
| 1197 | } |
| 1198 | |
| 1199 | @Test |
| 1200 | public void workingWifiP2pGroupClientSansIfaceChanged() throws Exception { |
| 1201 | workingWifiP2pGroupClient(false); |
| 1202 | } |
| 1203 | |
Erik Kline | 1fdc2e2 | 2017-05-08 17:56:35 +0900 | [diff] [blame] | 1204 | // 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] | 1205 | // already operating tethering mode interface. |
Christopher Wiley | 497c147 | 2016-10-11 13:26:03 -0700 | [diff] [blame] | 1206 | } |