Christopher Wiley | 08725a8 | 2016-05-18 16:32:44 -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 | |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 17 | package android.net.ip; |
Christopher Wiley | 08725a8 | 2016-05-18 16:32:44 -0700 | [diff] [blame] | 18 | |
Remi NGUYEN VAN | 73105e11 | 2018-12-21 16:17:09 +0900 | [diff] [blame] | 19 | import static android.net.ConnectivityManager.TETHERING_BLUETOOTH; |
| 20 | import static android.net.ConnectivityManager.TETHERING_USB; |
| 21 | import static android.net.ConnectivityManager.TETHERING_WIFI; |
Jimmy Chen | bcd86d0 | 2019-07-15 18:03:23 +0800 | [diff] [blame] | 22 | import static android.net.ConnectivityManager.TETHERING_WIFI_P2P; |
Remi NGUYEN VAN | 73105e11 | 2018-12-21 16:17:09 +0900 | [diff] [blame] | 23 | import static android.net.ConnectivityManager.TETHER_ERROR_ENABLE_NAT_ERROR; |
| 24 | import static android.net.ConnectivityManager.TETHER_ERROR_NO_ERROR; |
| 25 | import static android.net.ConnectivityManager.TETHER_ERROR_TETHER_IFACE_ERROR; |
Remi NGUYEN VAN | 0e3d0923 | 2018-12-04 12:13:09 +0900 | [diff] [blame] | 26 | import static android.net.dhcp.IDhcpServer.STATUS_SUCCESS; |
Remi NGUYEN VAN | 73105e11 | 2018-12-21 16:17:09 +0900 | [diff] [blame] | 27 | import static android.net.ip.IpServer.STATE_AVAILABLE; |
Jimmy Chen | bcd86d0 | 2019-07-15 18:03:23 +0800 | [diff] [blame] | 28 | import static android.net.ip.IpServer.STATE_LOCAL_ONLY; |
Remi NGUYEN VAN | 73105e11 | 2018-12-21 16:17:09 +0900 | [diff] [blame] | 29 | import static android.net.ip.IpServer.STATE_TETHERED; |
| 30 | import static android.net.ip.IpServer.STATE_UNAVAILABLE; |
Remi NGUYEN VAN | e88516f | 2019-01-20 09:35:10 +0900 | [diff] [blame] | 31 | import static android.net.shared.Inet4AddressUtils.intToInet4AddressHTH; |
Remi NGUYEN VAN | 73105e11 | 2018-12-21 16:17:09 +0900 | [diff] [blame] | 32 | |
Remi NGUYEN VAN | e3bb5c5 | 2018-06-12 15:57:04 +0900 | [diff] [blame] | 33 | import static org.junit.Assert.assertEquals; |
Erik Kline | dd4d582 | 2017-06-12 18:20:08 +0900 | [diff] [blame] | 34 | import static org.junit.Assert.assertFalse; |
Remi NGUYEN VAN | 0e3d0923 | 2018-12-04 12:13:09 +0900 | [diff] [blame] | 35 | import static org.junit.Assert.assertNotNull; |
Erik Kline | dd4d582 | 2017-06-12 18:20:08 +0900 | [diff] [blame] | 36 | import static org.junit.Assert.assertTrue; |
Remi NGUYEN VAN | 0e3d0923 | 2018-12-04 12:13:09 +0900 | [diff] [blame] | 37 | import static org.junit.Assert.fail; |
Remi NGUYEN VAN | d1b51a3 | 2019-01-22 16:13:57 +0900 | [diff] [blame] | 38 | import static org.mockito.ArgumentMatchers.argThat; |
Erik Kline | ab6439b | 2017-06-06 19:24:21 +0900 | [diff] [blame] | 39 | import static org.mockito.Matchers.any; |
Christopher Wiley | f972edc | 2016-05-23 16:17:30 -0700 | [diff] [blame] | 40 | import static org.mockito.Matchers.anyString; |
Erik Kline | ab6439b | 2017-06-06 19:24:21 +0900 | [diff] [blame] | 41 | import static org.mockito.Matchers.eq; |
Remi NGUYEN VAN | 0e3d0923 | 2018-12-04 12:13:09 +0900 | [diff] [blame] | 42 | import static org.mockito.Mockito.doAnswer; |
Christopher Wiley | f972edc | 2016-05-23 16:17:30 -0700 | [diff] [blame] | 43 | import static org.mockito.Mockito.doThrow; |
Christopher Wiley | 279eca3 | 2016-05-20 13:23:10 -0700 | [diff] [blame] | 44 | import static org.mockito.Mockito.inOrder; |
Remi NGUYEN VAN | e3bb5c5 | 2018-06-12 15:57:04 +0900 | [diff] [blame] | 45 | import static org.mockito.Mockito.never; |
Christopher Wiley | 279eca3 | 2016-05-20 13:23:10 -0700 | [diff] [blame] | 46 | import static org.mockito.Mockito.reset; |
Remi NGUYEN VAN | 0e3d0923 | 2018-12-04 12:13:09 +0900 | [diff] [blame] | 47 | import static org.mockito.Mockito.timeout; |
Remi NGUYEN VAN | e3bb5c5 | 2018-06-12 15:57:04 +0900 | [diff] [blame] | 48 | import static org.mockito.Mockito.times; |
Christopher Wiley | 279eca3 | 2016-05-20 13:23:10 -0700 | [diff] [blame] | 49 | import static org.mockito.Mockito.verify; |
Christopher Wiley | 08725a8 | 2016-05-18 16:32:44 -0700 | [diff] [blame] | 50 | import static org.mockito.Mockito.verifyNoMoreInteractions; |
Christopher Wiley | 279eca3 | 2016-05-20 13:23:10 -0700 | [diff] [blame] | 51 | import static org.mockito.Mockito.when; |
Christopher Wiley | 08725a8 | 2016-05-18 16:32:44 -0700 | [diff] [blame] | 52 | |
Remi NGUYEN VAN | d1b51a3 | 2019-01-22 16:13:57 +0900 | [diff] [blame] | 53 | import android.net.INetd; |
Christopher Wiley | 08725a8 | 2016-05-18 16:32:44 -0700 | [diff] [blame] | 54 | import android.net.INetworkStatsService; |
Christopher Wiley | 279eca3 | 2016-05-20 13:23:10 -0700 | [diff] [blame] | 55 | import android.net.InterfaceConfiguration; |
Remi NGUYEN VAN | e3bb5c5 | 2018-06-12 15:57:04 +0900 | [diff] [blame] | 56 | import android.net.IpPrefix; |
Erik Kline | dd4d582 | 2017-06-12 18:20:08 +0900 | [diff] [blame] | 57 | import android.net.LinkAddress; |
Erik Kline | ab6439b | 2017-06-06 19:24:21 +0900 | [diff] [blame] | 58 | import android.net.LinkProperties; |
Remi NGUYEN VAN | e3bb5c5 | 2018-06-12 15:57:04 +0900 | [diff] [blame] | 59 | import android.net.MacAddress; |
Erik Kline | dd4d582 | 2017-06-12 18:20:08 +0900 | [diff] [blame] | 60 | import android.net.RouteInfo; |
Remi NGUYEN VAN | 0e3d0923 | 2018-12-04 12:13:09 +0900 | [diff] [blame] | 61 | import android.net.dhcp.DhcpServingParamsParcel; |
| 62 | import android.net.dhcp.IDhcpServer; |
| 63 | import android.net.dhcp.IDhcpServerCallbacks; |
Remi NGUYEN VAN | e3bb5c5 | 2018-06-12 15:57:04 +0900 | [diff] [blame] | 64 | import android.net.util.InterfaceParams; |
Remi NGUYEN VAN | 25a7e4f | 2018-03-09 14:07:18 +0900 | [diff] [blame] | 65 | import android.net.util.InterfaceSet; |
Erik Kline | 7747fd4 | 2017-05-12 16:52:48 +0900 | [diff] [blame] | 66 | import android.net.util.SharedLog; |
Christopher Wiley | 08725a8 | 2016-05-18 16:32:44 -0700 | [diff] [blame] | 67 | import android.os.INetworkManagementService; |
Christopher Wiley | 279eca3 | 2016-05-20 13:23:10 -0700 | [diff] [blame] | 68 | import android.os.RemoteException; |
Christopher Wiley | 08725a8 | 2016-05-18 16:32:44 -0700 | [diff] [blame] | 69 | import android.os.test.TestLooper; |
Erik Kline | dd4d582 | 2017-06-12 18:20:08 +0900 | [diff] [blame] | 70 | import android.text.TextUtils; |
| 71 | |
Brett Chabot | 1ae2aa6 | 2019-03-04 14:14:56 -0800 | [diff] [blame] | 72 | import androidx.test.filters.SmallTest; |
| 73 | import androidx.test.runner.AndroidJUnit4; |
| 74 | |
Christopher Wiley | 08725a8 | 2016-05-18 16:32:44 -0700 | [diff] [blame] | 75 | import org.junit.Before; |
| 76 | import org.junit.Test; |
Christopher Wiley | 9bc0df2 | 2016-05-25 13:57:27 -0700 | [diff] [blame] | 77 | import org.junit.runner.RunWith; |
Erik Kline | dd4d582 | 2017-06-12 18:20:08 +0900 | [diff] [blame] | 78 | import org.mockito.ArgumentCaptor; |
Remi NGUYEN VAN | e3bb5c5 | 2018-06-12 15:57:04 +0900 | [diff] [blame] | 79 | import org.mockito.Captor; |
Christopher Wiley | 279eca3 | 2016-05-20 13:23:10 -0700 | [diff] [blame] | 80 | import org.mockito.InOrder; |
Christopher Wiley | 08725a8 | 2016-05-18 16:32:44 -0700 | [diff] [blame] | 81 | import org.mockito.Mock; |
| 82 | import org.mockito.MockitoAnnotations; |
| 83 | |
Remi NGUYEN VAN | 73105e11 | 2018-12-21 16:17:09 +0900 | [diff] [blame] | 84 | import java.net.Inet4Address; |
| 85 | |
Christopher Wiley | 9bc0df2 | 2016-05-25 13:57:27 -0700 | [diff] [blame] | 86 | @RunWith(AndroidJUnit4.class) |
| 87 | @SmallTest |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 88 | public class IpServerTest { |
Christopher Wiley | 08725a8 | 2016-05-18 16:32:44 -0700 | [diff] [blame] | 89 | private static final String IFACE_NAME = "testnet1"; |
Christopher Wiley | 279eca3 | 2016-05-20 13:23:10 -0700 | [diff] [blame] | 90 | private static final String UPSTREAM_IFACE = "upstream0"; |
| 91 | private static final String UPSTREAM_IFACE2 = "upstream1"; |
Remi NGUYEN VAN | e3bb5c5 | 2018-06-12 15:57:04 +0900 | [diff] [blame] | 92 | private static final int DHCP_LEASE_TIME_SECS = 3600; |
| 93 | |
| 94 | private static final InterfaceParams TEST_IFACE_PARAMS = new InterfaceParams( |
| 95 | IFACE_NAME, 42 /* index */, MacAddress.ALL_ZEROS_ADDRESS, 1500 /* defaultMtu */); |
Christopher Wiley | 08725a8 | 2016-05-18 16:32:44 -0700 | [diff] [blame] | 96 | |
Remi NGUYEN VAN | 0e3d0923 | 2018-12-04 12:13:09 +0900 | [diff] [blame] | 97 | private static final int MAKE_DHCPSERVER_TIMEOUT_MS = 1000; |
| 98 | |
Christopher Wiley | 08725a8 | 2016-05-18 16:32:44 -0700 | [diff] [blame] | 99 | @Mock private INetworkManagementService mNMService; |
Remi NGUYEN VAN | d1b51a3 | 2019-01-22 16:13:57 +0900 | [diff] [blame] | 100 | @Mock private INetd mNetd; |
Christopher Wiley | 08725a8 | 2016-05-18 16:32:44 -0700 | [diff] [blame] | 101 | @Mock private INetworkStatsService mStatsService; |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 102 | @Mock private IpServer.Callback mCallback; |
Christopher Wiley | 279eca3 | 2016-05-20 13:23:10 -0700 | [diff] [blame] | 103 | @Mock private InterfaceConfiguration mInterfaceConfiguration; |
Erik Kline | 7747fd4 | 2017-05-12 16:52:48 +0900 | [diff] [blame] | 104 | @Mock private SharedLog mSharedLog; |
Remi NGUYEN VAN | 0e3d0923 | 2018-12-04 12:13:09 +0900 | [diff] [blame] | 105 | @Mock private IDhcpServer mDhcpServer; |
Remi NGUYEN VAN | e3bb5c5 | 2018-06-12 15:57:04 +0900 | [diff] [blame] | 106 | @Mock private RouterAdvertisementDaemon mRaDaemon; |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 107 | @Mock private IpServer.Dependencies mDependencies; |
Christopher Wiley | 08725a8 | 2016-05-18 16:32:44 -0700 | [diff] [blame] | 108 | |
Remi NGUYEN VAN | 0e3d0923 | 2018-12-04 12:13:09 +0900 | [diff] [blame] | 109 | @Captor private ArgumentCaptor<DhcpServingParamsParcel> mDhcpParamsCaptor; |
Remi NGUYEN VAN | e3bb5c5 | 2018-06-12 15:57:04 +0900 | [diff] [blame] | 110 | |
Christopher Wiley | 08725a8 | 2016-05-18 16:32:44 -0700 | [diff] [blame] | 111 | private final TestLooper mLooper = new TestLooper(); |
Erik Kline | dd4d582 | 2017-06-12 18:20:08 +0900 | [diff] [blame] | 112 | private final ArgumentCaptor<LinkProperties> mLinkPropertiesCaptor = |
| 113 | ArgumentCaptor.forClass(LinkProperties.class); |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 114 | private IpServer mIpServer; |
Christopher Wiley | 08725a8 | 2016-05-18 16:32:44 -0700 | [diff] [blame] | 115 | |
Christopher Wiley | cd0cfbb | 2016-05-31 14:43:08 -0700 | [diff] [blame] | 116 | private void initStateMachine(int interfaceType) throws Exception { |
Remi NGUYEN VAN | e3bb5c5 | 2018-06-12 15:57:04 +0900 | [diff] [blame] | 117 | initStateMachine(interfaceType, false /* usingLegacyDhcp */); |
| 118 | } |
| 119 | |
| 120 | private void initStateMachine(int interfaceType, boolean usingLegacyDhcp) throws Exception { |
Remi NGUYEN VAN | 0e3d0923 | 2018-12-04 12:13:09 +0900 | [diff] [blame] | 121 | doAnswer(inv -> { |
| 122 | final IDhcpServerCallbacks cb = inv.getArgument(2); |
| 123 | new Thread(() -> { |
| 124 | try { |
| 125 | cb.onDhcpServerCreated(STATUS_SUCCESS, mDhcpServer); |
| 126 | } catch (RemoteException e) { |
| 127 | fail(e.getMessage()); |
| 128 | } |
| 129 | }).run(); |
| 130 | return null; |
| 131 | }).when(mDependencies).makeDhcpServer(any(), mDhcpParamsCaptor.capture(), any()); |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 132 | when(mDependencies.getRouterAdvertisementDaemon(any())).thenReturn(mRaDaemon); |
| 133 | when(mDependencies.getInterfaceParams(IFACE_NAME)).thenReturn(TEST_IFACE_PARAMS); |
Remi NGUYEN VAN | d1b51a3 | 2019-01-22 16:13:57 +0900 | [diff] [blame] | 134 | when(mDependencies.getNetdService()).thenReturn(mNetd); |
| 135 | |
| 136 | mIpServer = new IpServer( |
| 137 | IFACE_NAME, mLooper.getLooper(), interfaceType, mSharedLog, |
| 138 | mNMService, mStatsService, mCallback, usingLegacyDhcp, mDependencies); |
| 139 | mIpServer.start(); |
| 140 | // Starting the state machine always puts us in a consistent state and notifies |
| 141 | // the rest of the world that we've changed from an unknown to available state. |
| 142 | mLooper.dispatchAll(); |
| 143 | reset(mNMService, mStatsService, mCallback); |
| 144 | when(mNMService.getInterfaceConfig(IFACE_NAME)).thenReturn(mInterfaceConfiguration); |
Remi NGUYEN VAN | e3bb5c5 | 2018-06-12 15:57:04 +0900 | [diff] [blame] | 145 | |
| 146 | when(mRaDaemon.start()).thenReturn(true); |
Christopher Wiley | 279eca3 | 2016-05-20 13:23:10 -0700 | [diff] [blame] | 147 | } |
| 148 | |
Remi NGUYEN VAN | e3bb5c5 | 2018-06-12 15:57:04 +0900 | [diff] [blame] | 149 | private void initTetheredStateMachine(int interfaceType, String upstreamIface) |
| 150 | throws Exception { |
| 151 | initTetheredStateMachine(interfaceType, upstreamIface, false); |
| 152 | } |
| 153 | |
| 154 | private void initTetheredStateMachine(int interfaceType, String upstreamIface, |
| 155 | boolean usingLegacyDhcp) throws Exception { |
| 156 | initStateMachine(interfaceType, usingLegacyDhcp); |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 157 | dispatchCommand(IpServer.CMD_TETHER_REQUESTED, STATE_TETHERED); |
Christopher Wiley | 279eca3 | 2016-05-20 13:23:10 -0700 | [diff] [blame] | 158 | if (upstreamIface != null) { |
| 159 | dispatchTetherConnectionChanged(upstreamIface); |
| 160 | } |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 161 | reset(mNMService, mStatsService, mCallback); |
Christopher Wiley | f972edc | 2016-05-23 16:17:30 -0700 | [diff] [blame] | 162 | when(mNMService.getInterfaceConfig(IFACE_NAME)).thenReturn(mInterfaceConfiguration); |
Christopher Wiley | 279eca3 | 2016-05-20 13:23:10 -0700 | [diff] [blame] | 163 | } |
| 164 | |
Christopher Wiley | e90e0a7 | 2016-05-31 10:44:35 -0700 | [diff] [blame] | 165 | @Before public void setUp() throws Exception { |
Christopher Wiley | 08725a8 | 2016-05-18 16:32:44 -0700 | [diff] [blame] | 166 | MockitoAnnotations.initMocks(this); |
Erik Kline | 7747fd4 | 2017-05-12 16:52:48 +0900 | [diff] [blame] | 167 | when(mSharedLog.forSubComponent(anyString())).thenReturn(mSharedLog); |
Christopher Wiley | 279eca3 | 2016-05-20 13:23:10 -0700 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | @Test |
| 171 | public void startsOutAvailable() { |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 172 | mIpServer = new IpServer(IFACE_NAME, mLooper.getLooper(), |
| 173 | TETHERING_BLUETOOTH, mSharedLog, mNMService, mStatsService, mCallback, |
| 174 | false /* usingLegacyDhcp */, mDependencies); |
| 175 | mIpServer.start(); |
Christopher Wiley | 279eca3 | 2016-05-20 13:23:10 -0700 | [diff] [blame] | 176 | mLooper.dispatchAll(); |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 177 | verify(mCallback).updateInterfaceState( |
| 178 | mIpServer, STATE_AVAILABLE, TETHER_ERROR_NO_ERROR); |
| 179 | verify(mCallback).updateLinkProperties(eq(mIpServer), any(LinkProperties.class)); |
| 180 | verifyNoMoreInteractions(mCallback, mNMService, mStatsService); |
Christopher Wiley | 08725a8 | 2016-05-18 16:32:44 -0700 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | @Test |
Christopher Wiley | f972edc | 2016-05-23 16:17:30 -0700 | [diff] [blame] | 184 | public void shouldDoNothingUntilRequested() throws Exception { |
Erik Kline | a954be9 | 2017-02-13 17:12:02 +0900 | [diff] [blame] | 185 | initStateMachine(TETHERING_BLUETOOTH); |
markchien | 08cc030 | 2019-09-09 20:50:49 +0800 | [diff] [blame] | 186 | final int [] noOp_commands = { |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 187 | IpServer.CMD_TETHER_UNREQUESTED, |
| 188 | IpServer.CMD_IP_FORWARDING_ENABLE_ERROR, |
| 189 | IpServer.CMD_IP_FORWARDING_DISABLE_ERROR, |
| 190 | IpServer.CMD_START_TETHERING_ERROR, |
| 191 | IpServer.CMD_STOP_TETHERING_ERROR, |
| 192 | IpServer.CMD_SET_DNS_FORWARDERS_ERROR, |
| 193 | IpServer.CMD_TETHER_CONNECTION_CHANGED |
Christopher Wiley | 08725a8 | 2016-05-18 16:32:44 -0700 | [diff] [blame] | 194 | }; |
markchien | 08cc030 | 2019-09-09 20:50:49 +0800 | [diff] [blame] | 195 | for (int command : noOp_commands) { |
Christopher Wiley | 279eca3 | 2016-05-20 13:23:10 -0700 | [diff] [blame] | 196 | // None of these commands should trigger us to request action from |
Christopher Wiley | 08725a8 | 2016-05-18 16:32:44 -0700 | [diff] [blame] | 197 | // the rest of the system. |
Christopher Wiley | 279eca3 | 2016-05-20 13:23:10 -0700 | [diff] [blame] | 198 | dispatchCommand(command); |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 199 | verifyNoMoreInteractions(mNMService, mStatsService, mCallback); |
Christopher Wiley | 08725a8 | 2016-05-18 16:32:44 -0700 | [diff] [blame] | 200 | } |
| 201 | } |
| 202 | |
Christopher Wiley | 279eca3 | 2016-05-20 13:23:10 -0700 | [diff] [blame] | 203 | @Test |
Christopher Wiley | f972edc | 2016-05-23 16:17:30 -0700 | [diff] [blame] | 204 | public void handlesImmediateInterfaceDown() throws Exception { |
Erik Kline | a954be9 | 2017-02-13 17:12:02 +0900 | [diff] [blame] | 205 | initStateMachine(TETHERING_BLUETOOTH); |
Christopher Wiley | e90e0a7 | 2016-05-31 10:44:35 -0700 | [diff] [blame] | 206 | |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 207 | dispatchCommand(IpServer.CMD_INTERFACE_DOWN); |
| 208 | verify(mCallback).updateInterfaceState( |
| 209 | mIpServer, STATE_UNAVAILABLE, TETHER_ERROR_NO_ERROR); |
| 210 | verify(mCallback).updateLinkProperties(eq(mIpServer), any(LinkProperties.class)); |
| 211 | verifyNoMoreInteractions(mNMService, mStatsService, mCallback); |
Christopher Wiley | 279eca3 | 2016-05-20 13:23:10 -0700 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | @Test |
Christopher Wiley | f972edc | 2016-05-23 16:17:30 -0700 | [diff] [blame] | 215 | public void canBeTethered() throws Exception { |
Erik Kline | a954be9 | 2017-02-13 17:12:02 +0900 | [diff] [blame] | 216 | initStateMachine(TETHERING_BLUETOOTH); |
Christopher Wiley | e90e0a7 | 2016-05-31 10:44:35 -0700 | [diff] [blame] | 217 | |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 218 | dispatchCommand(IpServer.CMD_TETHER_REQUESTED, STATE_TETHERED); |
| 219 | InOrder inOrder = inOrder(mCallback, mNMService); |
Christopher Wiley | 279eca3 | 2016-05-20 13:23:10 -0700 | [diff] [blame] | 220 | inOrder.verify(mNMService).tetherInterface(IFACE_NAME); |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 221 | inOrder.verify(mCallback).updateInterfaceState( |
| 222 | mIpServer, STATE_TETHERED, TETHER_ERROR_NO_ERROR); |
| 223 | inOrder.verify(mCallback).updateLinkProperties( |
| 224 | eq(mIpServer), any(LinkProperties.class)); |
| 225 | verifyNoMoreInteractions(mNMService, mStatsService, mCallback); |
Christopher Wiley | 279eca3 | 2016-05-20 13:23:10 -0700 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | @Test |
| 229 | public void canUnrequestTethering() throws Exception { |
Erik Kline | a954be9 | 2017-02-13 17:12:02 +0900 | [diff] [blame] | 230 | initTetheredStateMachine(TETHERING_BLUETOOTH, null); |
Christopher Wiley | 279eca3 | 2016-05-20 13:23:10 -0700 | [diff] [blame] | 231 | |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 232 | dispatchCommand(IpServer.CMD_TETHER_UNREQUESTED); |
Remi NGUYEN VAN | d1b51a3 | 2019-01-22 16:13:57 +0900 | [diff] [blame] | 233 | InOrder inOrder = inOrder(mNMService, mNetd, mStatsService, mCallback); |
Christopher Wiley | f972edc | 2016-05-23 16:17:30 -0700 | [diff] [blame] | 234 | inOrder.verify(mNMService).untetherInterface(IFACE_NAME); |
Remi NGUYEN VAN | d1b51a3 | 2019-01-22 16:13:57 +0900 | [diff] [blame] | 235 | inOrder.verify(mNetd).interfaceSetCfg(argThat(cfg -> IFACE_NAME.equals(cfg.ifName))); |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 236 | inOrder.verify(mCallback).updateInterfaceState( |
| 237 | mIpServer, STATE_AVAILABLE, TETHER_ERROR_NO_ERROR); |
| 238 | inOrder.verify(mCallback).updateLinkProperties( |
| 239 | eq(mIpServer), any(LinkProperties.class)); |
| 240 | verifyNoMoreInteractions(mNMService, mStatsService, mCallback); |
Christopher Wiley | 279eca3 | 2016-05-20 13:23:10 -0700 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | @Test |
Christopher Wiley | f972edc | 2016-05-23 16:17:30 -0700 | [diff] [blame] | 244 | public void canBeTetheredAsUsb() throws Exception { |
Erik Kline | a954be9 | 2017-02-13 17:12:02 +0900 | [diff] [blame] | 245 | initStateMachine(TETHERING_USB); |
Christopher Wiley | 279eca3 | 2016-05-20 13:23:10 -0700 | [diff] [blame] | 246 | |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 247 | dispatchCommand(IpServer.CMD_TETHER_REQUESTED, STATE_TETHERED); |
| 248 | InOrder inOrder = inOrder(mCallback, mNMService); |
Christopher Wiley | 279eca3 | 2016-05-20 13:23:10 -0700 | [diff] [blame] | 249 | inOrder.verify(mNMService).getInterfaceConfig(IFACE_NAME); |
| 250 | inOrder.verify(mNMService).setInterfaceConfig(IFACE_NAME, mInterfaceConfiguration); |
Christopher Wiley | 279eca3 | 2016-05-20 13:23:10 -0700 | [diff] [blame] | 251 | inOrder.verify(mNMService).tetherInterface(IFACE_NAME); |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 252 | inOrder.verify(mCallback).updateInterfaceState( |
| 253 | mIpServer, STATE_TETHERED, TETHER_ERROR_NO_ERROR); |
| 254 | inOrder.verify(mCallback).updateLinkProperties( |
| 255 | eq(mIpServer), mLinkPropertiesCaptor.capture()); |
Erik Kline | dd4d582 | 2017-06-12 18:20:08 +0900 | [diff] [blame] | 256 | assertIPv4AddressAndDirectlyConnectedRoute(mLinkPropertiesCaptor.getValue()); |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 257 | verifyNoMoreInteractions(mNMService, mStatsService, mCallback); |
Christopher Wiley | 279eca3 | 2016-05-20 13:23:10 -0700 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | @Test |
Jimmy Chen | bcd86d0 | 2019-07-15 18:03:23 +0800 | [diff] [blame] | 261 | public void canBeTetheredAsWifiP2p() throws Exception { |
| 262 | initStateMachine(TETHERING_WIFI_P2P); |
| 263 | |
| 264 | dispatchCommand(IpServer.CMD_TETHER_REQUESTED, STATE_LOCAL_ONLY); |
| 265 | InOrder inOrder = inOrder(mCallback, mNMService); |
| 266 | inOrder.verify(mNMService).getInterfaceConfig(IFACE_NAME); |
| 267 | inOrder.verify(mNMService).setInterfaceConfig(IFACE_NAME, mInterfaceConfiguration); |
| 268 | inOrder.verify(mNMService).tetherInterface(IFACE_NAME); |
| 269 | inOrder.verify(mCallback).updateInterfaceState( |
| 270 | mIpServer, STATE_LOCAL_ONLY, TETHER_ERROR_NO_ERROR); |
| 271 | inOrder.verify(mCallback).updateLinkProperties( |
| 272 | eq(mIpServer), mLinkPropertiesCaptor.capture()); |
| 273 | assertIPv4AddressAndDirectlyConnectedRoute(mLinkPropertiesCaptor.getValue()); |
| 274 | verifyNoMoreInteractions(mNMService, mStatsService, mCallback); |
| 275 | } |
| 276 | |
| 277 | @Test |
Christopher Wiley | 279eca3 | 2016-05-20 13:23:10 -0700 | [diff] [blame] | 278 | public void handlesFirstUpstreamChange() throws Exception { |
Erik Kline | a954be9 | 2017-02-13 17:12:02 +0900 | [diff] [blame] | 279 | initTetheredStateMachine(TETHERING_BLUETOOTH, null); |
Christopher Wiley | 279eca3 | 2016-05-20 13:23:10 -0700 | [diff] [blame] | 280 | |
Erik Kline | ab6439b | 2017-06-06 19:24:21 +0900 | [diff] [blame] | 281 | // Telling the state machine about its upstream interface triggers |
| 282 | // a little more configuration. |
Christopher Wiley | 279eca3 | 2016-05-20 13:23:10 -0700 | [diff] [blame] | 283 | dispatchTetherConnectionChanged(UPSTREAM_IFACE); |
| 284 | InOrder inOrder = inOrder(mNMService); |
| 285 | inOrder.verify(mNMService).enableNat(IFACE_NAME, UPSTREAM_IFACE); |
| 286 | inOrder.verify(mNMService).startInterfaceForwarding(IFACE_NAME, UPSTREAM_IFACE); |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 287 | verifyNoMoreInteractions(mNMService, mStatsService, mCallback); |
Christopher Wiley | 279eca3 | 2016-05-20 13:23:10 -0700 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | @Test |
| 291 | public void handlesChangingUpstream() throws Exception { |
Erik Kline | a954be9 | 2017-02-13 17:12:02 +0900 | [diff] [blame] | 292 | initTetheredStateMachine(TETHERING_BLUETOOTH, UPSTREAM_IFACE); |
Christopher Wiley | 279eca3 | 2016-05-20 13:23:10 -0700 | [diff] [blame] | 293 | |
| 294 | dispatchTetherConnectionChanged(UPSTREAM_IFACE2); |
| 295 | InOrder inOrder = inOrder(mNMService, mStatsService); |
| 296 | inOrder.verify(mStatsService).forceUpdate(); |
| 297 | inOrder.verify(mNMService).stopInterfaceForwarding(IFACE_NAME, UPSTREAM_IFACE); |
| 298 | inOrder.verify(mNMService).disableNat(IFACE_NAME, UPSTREAM_IFACE); |
| 299 | inOrder.verify(mNMService).enableNat(IFACE_NAME, UPSTREAM_IFACE2); |
| 300 | inOrder.verify(mNMService).startInterfaceForwarding(IFACE_NAME, UPSTREAM_IFACE2); |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 301 | verifyNoMoreInteractions(mNMService, mStatsService, mCallback); |
Christopher Wiley | 279eca3 | 2016-05-20 13:23:10 -0700 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | @Test |
Erik Kline | a954be9 | 2017-02-13 17:12:02 +0900 | [diff] [blame] | 305 | public void handlesChangingUpstreamNatFailure() throws Exception { |
| 306 | initTetheredStateMachine(TETHERING_WIFI, UPSTREAM_IFACE); |
| 307 | |
| 308 | doThrow(RemoteException.class).when(mNMService).enableNat(IFACE_NAME, UPSTREAM_IFACE2); |
| 309 | |
| 310 | dispatchTetherConnectionChanged(UPSTREAM_IFACE2); |
| 311 | InOrder inOrder = inOrder(mNMService, mStatsService); |
| 312 | inOrder.verify(mStatsService).forceUpdate(); |
| 313 | inOrder.verify(mNMService).stopInterfaceForwarding(IFACE_NAME, UPSTREAM_IFACE); |
| 314 | inOrder.verify(mNMService).disableNat(IFACE_NAME, UPSTREAM_IFACE); |
| 315 | inOrder.verify(mNMService).enableNat(IFACE_NAME, UPSTREAM_IFACE2); |
Erik Kline | 8ea4548 | 2017-02-13 17:28:53 +0900 | [diff] [blame] | 316 | inOrder.verify(mStatsService).forceUpdate(); |
| 317 | inOrder.verify(mNMService).stopInterfaceForwarding(IFACE_NAME, UPSTREAM_IFACE2); |
| 318 | inOrder.verify(mNMService).disableNat(IFACE_NAME, UPSTREAM_IFACE2); |
Erik Kline | a954be9 | 2017-02-13 17:12:02 +0900 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | @Test |
| 322 | public void handlesChangingUpstreamInterfaceForwardingFailure() throws Exception { |
| 323 | initTetheredStateMachine(TETHERING_WIFI, UPSTREAM_IFACE); |
| 324 | |
| 325 | doThrow(RemoteException.class).when(mNMService).startInterfaceForwarding( |
| 326 | IFACE_NAME, UPSTREAM_IFACE2); |
| 327 | |
| 328 | dispatchTetherConnectionChanged(UPSTREAM_IFACE2); |
| 329 | InOrder inOrder = inOrder(mNMService, mStatsService); |
| 330 | inOrder.verify(mStatsService).forceUpdate(); |
| 331 | inOrder.verify(mNMService).stopInterfaceForwarding(IFACE_NAME, UPSTREAM_IFACE); |
| 332 | inOrder.verify(mNMService).disableNat(IFACE_NAME, UPSTREAM_IFACE); |
| 333 | inOrder.verify(mNMService).enableNat(IFACE_NAME, UPSTREAM_IFACE2); |
| 334 | inOrder.verify(mNMService).startInterfaceForwarding(IFACE_NAME, UPSTREAM_IFACE2); |
Erik Kline | 8ea4548 | 2017-02-13 17:28:53 +0900 | [diff] [blame] | 335 | inOrder.verify(mStatsService).forceUpdate(); |
| 336 | inOrder.verify(mNMService).stopInterfaceForwarding(IFACE_NAME, UPSTREAM_IFACE2); |
| 337 | inOrder.verify(mNMService).disableNat(IFACE_NAME, UPSTREAM_IFACE2); |
Erik Kline | a954be9 | 2017-02-13 17:12:02 +0900 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | @Test |
Christopher Wiley | 279eca3 | 2016-05-20 13:23:10 -0700 | [diff] [blame] | 341 | public void canUnrequestTetheringWithUpstream() throws Exception { |
Erik Kline | a954be9 | 2017-02-13 17:12:02 +0900 | [diff] [blame] | 342 | initTetheredStateMachine(TETHERING_BLUETOOTH, UPSTREAM_IFACE); |
Christopher Wiley | 279eca3 | 2016-05-20 13:23:10 -0700 | [diff] [blame] | 343 | |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 344 | dispatchCommand(IpServer.CMD_TETHER_UNREQUESTED); |
Remi NGUYEN VAN | d1b51a3 | 2019-01-22 16:13:57 +0900 | [diff] [blame] | 345 | InOrder inOrder = inOrder(mNMService, mNetd, mStatsService, mCallback); |
Christopher Wiley | 279eca3 | 2016-05-20 13:23:10 -0700 | [diff] [blame] | 346 | inOrder.verify(mStatsService).forceUpdate(); |
| 347 | inOrder.verify(mNMService).stopInterfaceForwarding(IFACE_NAME, UPSTREAM_IFACE); |
| 348 | inOrder.verify(mNMService).disableNat(IFACE_NAME, UPSTREAM_IFACE); |
| 349 | inOrder.verify(mNMService).untetherInterface(IFACE_NAME); |
Remi NGUYEN VAN | d1b51a3 | 2019-01-22 16:13:57 +0900 | [diff] [blame] | 350 | inOrder.verify(mNetd).interfaceSetCfg(argThat(cfg -> IFACE_NAME.equals(cfg.ifName))); |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 351 | inOrder.verify(mCallback).updateInterfaceState( |
| 352 | mIpServer, STATE_AVAILABLE, TETHER_ERROR_NO_ERROR); |
| 353 | inOrder.verify(mCallback).updateLinkProperties( |
| 354 | eq(mIpServer), any(LinkProperties.class)); |
| 355 | verifyNoMoreInteractions(mNMService, mStatsService, mCallback); |
Christopher Wiley | 279eca3 | 2016-05-20 13:23:10 -0700 | [diff] [blame] | 356 | } |
| 357 | |
Christopher Wiley | f972edc | 2016-05-23 16:17:30 -0700 | [diff] [blame] | 358 | @Test |
| 359 | public void interfaceDownLeadsToUnavailable() throws Exception { |
| 360 | for (boolean shouldThrow : new boolean[]{true, false}) { |
Erik Kline | a954be9 | 2017-02-13 17:12:02 +0900 | [diff] [blame] | 361 | initTetheredStateMachine(TETHERING_USB, null); |
Christopher Wiley | f972edc | 2016-05-23 16:17:30 -0700 | [diff] [blame] | 362 | |
| 363 | if (shouldThrow) { |
| 364 | doThrow(RemoteException.class).when(mNMService).untetherInterface(IFACE_NAME); |
| 365 | } |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 366 | dispatchCommand(IpServer.CMD_INTERFACE_DOWN); |
| 367 | InOrder usbTeardownOrder = inOrder(mNMService, mInterfaceConfiguration, mCallback); |
Christopher Wiley | f972edc | 2016-05-23 16:17:30 -0700 | [diff] [blame] | 368 | usbTeardownOrder.verify(mInterfaceConfiguration).setInterfaceDown(); |
| 369 | usbTeardownOrder.verify(mNMService).setInterfaceConfig( |
| 370 | IFACE_NAME, mInterfaceConfiguration); |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 371 | usbTeardownOrder.verify(mCallback).updateInterfaceState( |
| 372 | mIpServer, STATE_UNAVAILABLE, TETHER_ERROR_NO_ERROR); |
| 373 | usbTeardownOrder.verify(mCallback).updateLinkProperties( |
| 374 | eq(mIpServer), mLinkPropertiesCaptor.capture()); |
Erik Kline | dd4d582 | 2017-06-12 18:20:08 +0900 | [diff] [blame] | 375 | assertNoAddressesNorRoutes(mLinkPropertiesCaptor.getValue()); |
Christopher Wiley | f972edc | 2016-05-23 16:17:30 -0700 | [diff] [blame] | 376 | } |
| 377 | } |
| 378 | |
| 379 | @Test |
| 380 | public void usbShouldBeTornDownOnTetherError() throws Exception { |
Erik Kline | a954be9 | 2017-02-13 17:12:02 +0900 | [diff] [blame] | 381 | initStateMachine(TETHERING_USB); |
Christopher Wiley | f972edc | 2016-05-23 16:17:30 -0700 | [diff] [blame] | 382 | |
| 383 | doThrow(RemoteException.class).when(mNMService).tetherInterface(IFACE_NAME); |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 384 | dispatchCommand(IpServer.CMD_TETHER_REQUESTED, STATE_TETHERED); |
| 385 | InOrder usbTeardownOrder = inOrder(mNMService, mInterfaceConfiguration, mCallback); |
Christopher Wiley | f972edc | 2016-05-23 16:17:30 -0700 | [diff] [blame] | 386 | usbTeardownOrder.verify(mInterfaceConfiguration).setInterfaceDown(); |
| 387 | usbTeardownOrder.verify(mNMService).setInterfaceConfig( |
| 388 | IFACE_NAME, mInterfaceConfiguration); |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 389 | usbTeardownOrder.verify(mCallback).updateInterfaceState( |
| 390 | mIpServer, STATE_AVAILABLE, TETHER_ERROR_TETHER_IFACE_ERROR); |
| 391 | usbTeardownOrder.verify(mCallback).updateLinkProperties( |
| 392 | eq(mIpServer), mLinkPropertiesCaptor.capture()); |
Erik Kline | dd4d582 | 2017-06-12 18:20:08 +0900 | [diff] [blame] | 393 | assertNoAddressesNorRoutes(mLinkPropertiesCaptor.getValue()); |
Christopher Wiley | f972edc | 2016-05-23 16:17:30 -0700 | [diff] [blame] | 394 | } |
| 395 | |
| 396 | @Test |
| 397 | public void shouldTearDownUsbOnUpstreamError() throws Exception { |
Erik Kline | a954be9 | 2017-02-13 17:12:02 +0900 | [diff] [blame] | 398 | initTetheredStateMachine(TETHERING_USB, null); |
Christopher Wiley | f972edc | 2016-05-23 16:17:30 -0700 | [diff] [blame] | 399 | |
| 400 | doThrow(RemoteException.class).when(mNMService).enableNat(anyString(), anyString()); |
| 401 | dispatchTetherConnectionChanged(UPSTREAM_IFACE); |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 402 | InOrder usbTeardownOrder = inOrder(mNMService, mInterfaceConfiguration, mCallback); |
Christopher Wiley | f972edc | 2016-05-23 16:17:30 -0700 | [diff] [blame] | 403 | usbTeardownOrder.verify(mInterfaceConfiguration).setInterfaceDown(); |
| 404 | usbTeardownOrder.verify(mNMService).setInterfaceConfig(IFACE_NAME, mInterfaceConfiguration); |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 405 | usbTeardownOrder.verify(mCallback).updateInterfaceState( |
| 406 | mIpServer, STATE_AVAILABLE, TETHER_ERROR_ENABLE_NAT_ERROR); |
| 407 | usbTeardownOrder.verify(mCallback).updateLinkProperties( |
| 408 | eq(mIpServer), mLinkPropertiesCaptor.capture()); |
Erik Kline | dd4d582 | 2017-06-12 18:20:08 +0900 | [diff] [blame] | 409 | assertNoAddressesNorRoutes(mLinkPropertiesCaptor.getValue()); |
Christopher Wiley | f972edc | 2016-05-23 16:17:30 -0700 | [diff] [blame] | 410 | } |
Christopher Wiley | 279eca3 | 2016-05-20 13:23:10 -0700 | [diff] [blame] | 411 | |
Erik Kline | 624bf3d | 2017-02-14 15:55:00 +0900 | [diff] [blame] | 412 | @Test |
| 413 | public void ignoresDuplicateUpstreamNotifications() throws Exception { |
| 414 | initTetheredStateMachine(TETHERING_WIFI, UPSTREAM_IFACE); |
| 415 | |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 416 | verifyNoMoreInteractions(mNMService, mStatsService, mCallback); |
Erik Kline | 624bf3d | 2017-02-14 15:55:00 +0900 | [diff] [blame] | 417 | |
| 418 | for (int i = 0; i < 5; i++) { |
| 419 | dispatchTetherConnectionChanged(UPSTREAM_IFACE); |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 420 | verifyNoMoreInteractions(mNMService, mStatsService, mCallback); |
Erik Kline | 624bf3d | 2017-02-14 15:55:00 +0900 | [diff] [blame] | 421 | } |
| 422 | } |
| 423 | |
Remi NGUYEN VAN | e3bb5c5 | 2018-06-12 15:57:04 +0900 | [diff] [blame] | 424 | @Test |
| 425 | public void startsDhcpServer() throws Exception { |
| 426 | initTetheredStateMachine(TETHERING_WIFI, UPSTREAM_IFACE); |
| 427 | dispatchTetherConnectionChanged(UPSTREAM_IFACE); |
| 428 | |
| 429 | assertDhcpStarted(new IpPrefix("192.168.43.0/24")); |
| 430 | } |
| 431 | |
| 432 | @Test |
| 433 | public void startsDhcpServerOnBluetooth() throws Exception { |
| 434 | initTetheredStateMachine(TETHERING_BLUETOOTH, UPSTREAM_IFACE); |
| 435 | dispatchTetherConnectionChanged(UPSTREAM_IFACE); |
| 436 | |
| 437 | assertDhcpStarted(new IpPrefix("192.168.44.0/24")); |
| 438 | } |
| 439 | |
| 440 | @Test |
Jimmy Chen | bcd86d0 | 2019-07-15 18:03:23 +0800 | [diff] [blame] | 441 | public void startsDhcpServerOnWifiP2p() throws Exception { |
| 442 | initTetheredStateMachine(TETHERING_WIFI_P2P, UPSTREAM_IFACE); |
| 443 | dispatchTetherConnectionChanged(UPSTREAM_IFACE); |
| 444 | |
| 445 | assertDhcpStarted(new IpPrefix("192.168.49.0/24")); |
| 446 | } |
| 447 | |
| 448 | @Test |
Remi NGUYEN VAN | e3bb5c5 | 2018-06-12 15:57:04 +0900 | [diff] [blame] | 449 | public void doesNotStartDhcpServerIfDisabled() throws Exception { |
| 450 | initTetheredStateMachine(TETHERING_WIFI, UPSTREAM_IFACE, true /* usingLegacyDhcp */); |
| 451 | dispatchTetherConnectionChanged(UPSTREAM_IFACE); |
| 452 | |
Remi NGUYEN VAN | 0e3d0923 | 2018-12-04 12:13:09 +0900 | [diff] [blame] | 453 | verify(mDependencies, never()).makeDhcpServer(any(), any(), any()); |
Remi NGUYEN VAN | e3bb5c5 | 2018-06-12 15:57:04 +0900 | [diff] [blame] | 454 | } |
| 455 | |
Remi NGUYEN VAN | 0e3d0923 | 2018-12-04 12:13:09 +0900 | [diff] [blame] | 456 | private void assertDhcpStarted(IpPrefix expectedPrefix) throws Exception { |
| 457 | verify(mDependencies, times(1)).makeDhcpServer(eq(IFACE_NAME), any(), any()); |
| 458 | verify(mDhcpServer, timeout(MAKE_DHCPSERVER_TIMEOUT_MS).times(1)).start(any()); |
| 459 | final DhcpServingParamsParcel params = mDhcpParamsCaptor.getValue(); |
Remi NGUYEN VAN | e3bb5c5 | 2018-06-12 15:57:04 +0900 | [diff] [blame] | 460 | // Last address byte is random |
Remi NGUYEN VAN | 0e3d0923 | 2018-12-04 12:13:09 +0900 | [diff] [blame] | 461 | assertTrue(expectedPrefix.contains(intToInet4AddressHTH(params.serverAddr))); |
| 462 | assertEquals(expectedPrefix.getPrefixLength(), params.serverAddrPrefixLength); |
| 463 | assertEquals(1, params.defaultRouters.length); |
| 464 | assertEquals(params.serverAddr, params.defaultRouters[0]); |
| 465 | assertEquals(1, params.dnsServers.length); |
| 466 | assertEquals(params.serverAddr, params.dnsServers[0]); |
Remi NGUYEN VAN | e3bb5c5 | 2018-06-12 15:57:04 +0900 | [diff] [blame] | 467 | assertEquals(DHCP_LEASE_TIME_SECS, params.dhcpLeaseTimeSecs); |
| 468 | } |
| 469 | |
Christopher Wiley | 279eca3 | 2016-05-20 13:23:10 -0700 | [diff] [blame] | 470 | /** |
| 471 | * Send a command to the state machine under test, and run the event loop to idle. |
| 472 | * |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 473 | * @param command One of the IpServer.CMD_* constants. |
Remi NGUYEN VAN | a911e84 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 474 | * @param arg1 An additional argument to pass. |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 475 | */ |
| 476 | private void dispatchCommand(int command, int arg1) { |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 477 | mIpServer.sendMessage(command, arg1); |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 478 | mLooper.dispatchAll(); |
| 479 | } |
| 480 | |
| 481 | /** |
| 482 | * Send a command to the state machine under test, and run the event loop to idle. |
| 483 | * |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 484 | * @param command One of the IpServer.CMD_* constants. |
Christopher Wiley | 279eca3 | 2016-05-20 13:23:10 -0700 | [diff] [blame] | 485 | */ |
| 486 | private void dispatchCommand(int command) { |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 487 | mIpServer.sendMessage(command); |
Christopher Wiley | 279eca3 | 2016-05-20 13:23:10 -0700 | [diff] [blame] | 488 | mLooper.dispatchAll(); |
| 489 | } |
| 490 | |
| 491 | /** |
| 492 | * Special override to tell the state machine that the upstream interface has changed. |
| 493 | * |
| 494 | * @see #dispatchCommand(int) |
| 495 | * @param upstreamIface String name of upstream interface (or null) |
| 496 | */ |
| 497 | private void dispatchTetherConnectionChanged(String upstreamIface) { |
Erik Kline | 7a4ccc6 | 2018-08-27 17:26:47 +0900 | [diff] [blame] | 498 | mIpServer.sendMessage(IpServer.CMD_TETHER_CONNECTION_CHANGED, |
Remi NGUYEN VAN | 25a7e4f | 2018-03-09 14:07:18 +0900 | [diff] [blame] | 499 | new InterfaceSet(upstreamIface)); |
Christopher Wiley | 279eca3 | 2016-05-20 13:23:10 -0700 | [diff] [blame] | 500 | mLooper.dispatchAll(); |
Christopher Wiley | 08725a8 | 2016-05-18 16:32:44 -0700 | [diff] [blame] | 501 | } |
Erik Kline | dd4d582 | 2017-06-12 18:20:08 +0900 | [diff] [blame] | 502 | |
| 503 | private void assertIPv4AddressAndDirectlyConnectedRoute(LinkProperties lp) { |
| 504 | // Find the first IPv4 LinkAddress. |
| 505 | LinkAddress addr4 = null; |
| 506 | for (LinkAddress addr : lp.getLinkAddresses()) { |
| 507 | if (!(addr.getAddress() instanceof Inet4Address)) continue; |
| 508 | addr4 = addr; |
| 509 | break; |
| 510 | } |
Remi NGUYEN VAN | 0e3d0923 | 2018-12-04 12:13:09 +0900 | [diff] [blame] | 511 | assertNotNull("missing IPv4 address", addr4); |
Erik Kline | dd4d582 | 2017-06-12 18:20:08 +0900 | [diff] [blame] | 512 | |
markchien | 986750b | 2019-12-06 15:24:53 +0800 | [diff] [blame^] | 513 | final IpPrefix destination = new IpPrefix(addr4.getAddress(), addr4.getPrefixLength()); |
Erik Kline | dd4d582 | 2017-06-12 18:20:08 +0900 | [diff] [blame] | 514 | // Assert the presence of the associated directly connected route. |
markchien | 986750b | 2019-12-06 15:24:53 +0800 | [diff] [blame^] | 515 | final RouteInfo directlyConnected = new RouteInfo(destination, null, lp.getInterfaceName(), |
| 516 | RouteInfo.RTN_UNICAST); |
Erik Kline | dd4d582 | 2017-06-12 18:20:08 +0900 | [diff] [blame] | 517 | assertTrue("missing directly connected route: '" + directlyConnected.toString() + "'", |
| 518 | lp.getRoutes().contains(directlyConnected)); |
| 519 | } |
| 520 | |
| 521 | private void assertNoAddressesNorRoutes(LinkProperties lp) { |
| 522 | assertTrue(lp.getLinkAddresses().isEmpty()); |
| 523 | assertTrue(lp.getRoutes().isEmpty()); |
| 524 | // We also check that interface name is non-empty, because we should |
| 525 | // never see an empty interface name in any LinkProperties update. |
| 526 | assertFalse(TextUtils.isEmpty(lp.getInterfaceName())); |
| 527 | } |
Lorenzo Colitti | 5bce5a1 | 2016-10-28 17:45:55 +0900 | [diff] [blame] | 528 | } |