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 | |
| 17 | package com.android.server.connectivity.tethering; |
| 18 | |
Erik Kline | dd4d582 | 2017-06-12 18:20:08 +0900 | [diff] [blame] | 19 | import static org.junit.Assert.assertFalse; |
| 20 | import static org.junit.Assert.assertTrue; |
Erik Kline | ab6439b | 2017-06-06 19:24:21 +0900 | [diff] [blame] | 21 | import static org.mockito.Matchers.any; |
Christopher Wiley | f972edc | 2016-05-23 16:17:30 -0700 | [diff] [blame] | 22 | import static org.mockito.Matchers.anyString; |
Erik Kline | ab6439b | 2017-06-06 19:24:21 +0900 | [diff] [blame] | 23 | import static org.mockito.Matchers.eq; |
Christopher Wiley | f972edc | 2016-05-23 16:17:30 -0700 | [diff] [blame] | 24 | import static org.mockito.Mockito.doThrow; |
Christopher Wiley | 279eca3 | 2016-05-20 13:23:10 -0700 | [diff] [blame] | 25 | import static org.mockito.Mockito.inOrder; |
| 26 | import static org.mockito.Mockito.reset; |
| 27 | import static org.mockito.Mockito.verify; |
Christopher Wiley | 08725a8 | 2016-05-18 16:32:44 -0700 | [diff] [blame] | 28 | import static org.mockito.Mockito.verifyNoMoreInteractions; |
Christopher Wiley | 279eca3 | 2016-05-20 13:23:10 -0700 | [diff] [blame] | 29 | import static org.mockito.Mockito.when; |
Christopher Wiley | 08725a8 | 2016-05-18 16:32:44 -0700 | [diff] [blame] | 30 | |
Christopher Wiley | e90e0a7 | 2016-05-31 10:44:35 -0700 | [diff] [blame] | 31 | import static android.net.ConnectivityManager.TETHER_ERROR_ENABLE_NAT_ERROR; |
| 32 | import static android.net.ConnectivityManager.TETHER_ERROR_NO_ERROR; |
| 33 | import static android.net.ConnectivityManager.TETHER_ERROR_TETHER_IFACE_ERROR; |
Erik Kline | a954be9 | 2017-02-13 17:12:02 +0900 | [diff] [blame] | 34 | import static android.net.ConnectivityManager.TETHERING_BLUETOOTH; |
| 35 | import static android.net.ConnectivityManager.TETHERING_USB; |
| 36 | import static android.net.ConnectivityManager.TETHERING_WIFI; |
Christopher Wiley | e90e0a7 | 2016-05-31 10:44:35 -0700 | [diff] [blame] | 37 | import static com.android.server.connectivity.tethering.IControlsTethering.STATE_AVAILABLE; |
| 38 | import static com.android.server.connectivity.tethering.IControlsTethering.STATE_TETHERED; |
| 39 | import static com.android.server.connectivity.tethering.IControlsTethering.STATE_UNAVAILABLE; |
| 40 | |
Christopher Wiley | 08725a8 | 2016-05-18 16:32:44 -0700 | [diff] [blame] | 41 | import android.net.INetworkStatsService; |
Christopher Wiley | 279eca3 | 2016-05-20 13:23:10 -0700 | [diff] [blame] | 42 | import android.net.InterfaceConfiguration; |
Erik Kline | dd4d582 | 2017-06-12 18:20:08 +0900 | [diff] [blame] | 43 | import android.net.LinkAddress; |
Erik Kline | ab6439b | 2017-06-06 19:24:21 +0900 | [diff] [blame] | 44 | import android.net.LinkProperties; |
Erik Kline | dd4d582 | 2017-06-12 18:20:08 +0900 | [diff] [blame] | 45 | import android.net.RouteInfo; |
Remi NGUYEN VAN | 25a7e4f | 2018-03-09 14:07:18 +0900 | [diff] [blame^] | 46 | import android.net.util.InterfaceSet; |
Erik Kline | 7747fd4 | 2017-05-12 16:52:48 +0900 | [diff] [blame] | 47 | import android.net.util.SharedLog; |
Christopher Wiley | 08725a8 | 2016-05-18 16:32:44 -0700 | [diff] [blame] | 48 | import android.os.INetworkManagementService; |
Christopher Wiley | 279eca3 | 2016-05-20 13:23:10 -0700 | [diff] [blame] | 49 | import android.os.RemoteException; |
Christopher Wiley | 08725a8 | 2016-05-18 16:32:44 -0700 | [diff] [blame] | 50 | import android.os.test.TestLooper; |
Christopher Wiley | 9bc0df2 | 2016-05-25 13:57:27 -0700 | [diff] [blame] | 51 | import android.support.test.filters.SmallTest; |
| 52 | import android.support.test.runner.AndroidJUnit4; |
Erik Kline | dd4d582 | 2017-06-12 18:20:08 +0900 | [diff] [blame] | 53 | import android.text.TextUtils; |
| 54 | |
| 55 | import java.net.Inet4Address; |
Christopher Wiley | 08725a8 | 2016-05-18 16:32:44 -0700 | [diff] [blame] | 56 | |
| 57 | import org.junit.Before; |
| 58 | import org.junit.Test; |
Christopher Wiley | 9bc0df2 | 2016-05-25 13:57:27 -0700 | [diff] [blame] | 59 | import org.junit.runner.RunWith; |
Erik Kline | dd4d582 | 2017-06-12 18:20:08 +0900 | [diff] [blame] | 60 | import org.mockito.ArgumentCaptor; |
Christopher Wiley | 279eca3 | 2016-05-20 13:23:10 -0700 | [diff] [blame] | 61 | import org.mockito.InOrder; |
Christopher Wiley | 08725a8 | 2016-05-18 16:32:44 -0700 | [diff] [blame] | 62 | import org.mockito.Mock; |
| 63 | import org.mockito.MockitoAnnotations; |
| 64 | |
Christopher Wiley | 9bc0df2 | 2016-05-25 13:57:27 -0700 | [diff] [blame] | 65 | @RunWith(AndroidJUnit4.class) |
| 66 | @SmallTest |
Mitchell Wills | 4622c2d | 2016-05-23 16:40:10 -0700 | [diff] [blame] | 67 | public class TetherInterfaceStateMachineTest { |
Christopher Wiley | 08725a8 | 2016-05-18 16:32:44 -0700 | [diff] [blame] | 68 | private static final String IFACE_NAME = "testnet1"; |
Christopher Wiley | 279eca3 | 2016-05-20 13:23:10 -0700 | [diff] [blame] | 69 | private static final String UPSTREAM_IFACE = "upstream0"; |
| 70 | private static final String UPSTREAM_IFACE2 = "upstream1"; |
Christopher Wiley | 08725a8 | 2016-05-18 16:32:44 -0700 | [diff] [blame] | 71 | |
| 72 | @Mock private INetworkManagementService mNMService; |
| 73 | @Mock private INetworkStatsService mStatsService; |
| 74 | @Mock private IControlsTethering mTetherHelper; |
Christopher Wiley | 279eca3 | 2016-05-20 13:23:10 -0700 | [diff] [blame] | 75 | @Mock private InterfaceConfiguration mInterfaceConfiguration; |
Erik Kline | 7747fd4 | 2017-05-12 16:52:48 +0900 | [diff] [blame] | 76 | @Mock private SharedLog mSharedLog; |
Remi NGUYEN VAN | a911e84 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 77 | @Mock private TetheringDependencies mTetheringDependencies; |
Christopher Wiley | 08725a8 | 2016-05-18 16:32:44 -0700 | [diff] [blame] | 78 | |
| 79 | private final TestLooper mLooper = new TestLooper(); |
Erik Kline | dd4d582 | 2017-06-12 18:20:08 +0900 | [diff] [blame] | 80 | private final ArgumentCaptor<LinkProperties> mLinkPropertiesCaptor = |
| 81 | ArgumentCaptor.forClass(LinkProperties.class); |
Mitchell Wills | 4622c2d | 2016-05-23 16:40:10 -0700 | [diff] [blame] | 82 | private TetherInterfaceStateMachine mTestedSm; |
Christopher Wiley | 08725a8 | 2016-05-18 16:32:44 -0700 | [diff] [blame] | 83 | |
Christopher Wiley | cd0cfbb | 2016-05-31 14:43:08 -0700 | [diff] [blame] | 84 | private void initStateMachine(int interfaceType) throws Exception { |
Erik Kline | 7747fd4 | 2017-05-12 16:52:48 +0900 | [diff] [blame] | 85 | mTestedSm = new TetherInterfaceStateMachine( |
| 86 | IFACE_NAME, mLooper.getLooper(), interfaceType, mSharedLog, |
Remi NGUYEN VAN | a911e84 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 87 | mNMService, mStatsService, mTetherHelper, mTetheringDependencies); |
Christopher Wiley | 279eca3 | 2016-05-20 13:23:10 -0700 | [diff] [blame] | 88 | mTestedSm.start(); |
| 89 | // Starting the state machine always puts us in a consistent state and notifies |
Erik Kline | dd4d582 | 2017-06-12 18:20:08 +0900 | [diff] [blame] | 90 | // the rest of the world that we've changed from an unknown to available state. |
Christopher Wiley | 279eca3 | 2016-05-20 13:23:10 -0700 | [diff] [blame] | 91 | mLooper.dispatchAll(); |
| 92 | reset(mNMService, mStatsService, mTetherHelper); |
Christopher Wiley | f972edc | 2016-05-23 16:17:30 -0700 | [diff] [blame] | 93 | when(mNMService.getInterfaceConfig(IFACE_NAME)).thenReturn(mInterfaceConfiguration); |
Christopher Wiley | 279eca3 | 2016-05-20 13:23:10 -0700 | [diff] [blame] | 94 | } |
| 95 | |
Christopher Wiley | cd0cfbb | 2016-05-31 14:43:08 -0700 | [diff] [blame] | 96 | private void initTetheredStateMachine(int interfaceType, String upstreamIface) throws Exception { |
| 97 | initStateMachine(interfaceType); |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 98 | dispatchCommand(TetherInterfaceStateMachine.CMD_TETHER_REQUESTED, STATE_TETHERED); |
Christopher Wiley | 279eca3 | 2016-05-20 13:23:10 -0700 | [diff] [blame] | 99 | if (upstreamIface != null) { |
| 100 | dispatchTetherConnectionChanged(upstreamIface); |
| 101 | } |
| 102 | reset(mNMService, mStatsService, mTetherHelper); |
Christopher Wiley | f972edc | 2016-05-23 16:17:30 -0700 | [diff] [blame] | 103 | when(mNMService.getInterfaceConfig(IFACE_NAME)).thenReturn(mInterfaceConfiguration); |
Christopher Wiley | 279eca3 | 2016-05-20 13:23:10 -0700 | [diff] [blame] | 104 | } |
| 105 | |
Christopher Wiley | e90e0a7 | 2016-05-31 10:44:35 -0700 | [diff] [blame] | 106 | @Before public void setUp() throws Exception { |
Christopher Wiley | 08725a8 | 2016-05-18 16:32:44 -0700 | [diff] [blame] | 107 | MockitoAnnotations.initMocks(this); |
Erik Kline | 7747fd4 | 2017-05-12 16:52:48 +0900 | [diff] [blame] | 108 | when(mSharedLog.forSubComponent(anyString())).thenReturn(mSharedLog); |
Christopher Wiley | 279eca3 | 2016-05-20 13:23:10 -0700 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | @Test |
| 112 | public void startsOutAvailable() { |
Christopher Wiley | cd0cfbb | 2016-05-31 14:43:08 -0700 | [diff] [blame] | 113 | mTestedSm = new TetherInterfaceStateMachine(IFACE_NAME, mLooper.getLooper(), |
Remi NGUYEN VAN | a911e84 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 114 | TETHERING_BLUETOOTH, mSharedLog, mNMService, mStatsService, mTetherHelper, |
| 115 | mTetheringDependencies); |
Christopher Wiley | 08725a8 | 2016-05-18 16:32:44 -0700 | [diff] [blame] | 116 | mTestedSm.start(); |
Christopher Wiley | 279eca3 | 2016-05-20 13:23:10 -0700 | [diff] [blame] | 117 | mLooper.dispatchAll(); |
Erik Kline | ab6439b | 2017-06-06 19:24:21 +0900 | [diff] [blame] | 118 | verify(mTetherHelper).updateInterfaceState( |
| 119 | mTestedSm, STATE_AVAILABLE, TETHER_ERROR_NO_ERROR); |
| 120 | verify(mTetherHelper).updateLinkProperties(eq(mTestedSm), any(LinkProperties.class)); |
Christopher Wiley | 279eca3 | 2016-05-20 13:23:10 -0700 | [diff] [blame] | 121 | verifyNoMoreInteractions(mTetherHelper, mNMService, mStatsService); |
Christopher Wiley | 08725a8 | 2016-05-18 16:32:44 -0700 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | @Test |
Christopher Wiley | f972edc | 2016-05-23 16:17:30 -0700 | [diff] [blame] | 125 | public void shouldDoNothingUntilRequested() throws Exception { |
Erik Kline | a954be9 | 2017-02-13 17:12:02 +0900 | [diff] [blame] | 126 | initStateMachine(TETHERING_BLUETOOTH); |
Christopher Wiley | 08725a8 | 2016-05-18 16:32:44 -0700 | [diff] [blame] | 127 | final int [] NOOP_COMMANDS = { |
Mitchell Wills | 4622c2d | 2016-05-23 16:40:10 -0700 | [diff] [blame] | 128 | TetherInterfaceStateMachine.CMD_TETHER_UNREQUESTED, |
| 129 | TetherInterfaceStateMachine.CMD_IP_FORWARDING_ENABLE_ERROR, |
| 130 | TetherInterfaceStateMachine.CMD_IP_FORWARDING_DISABLE_ERROR, |
| 131 | TetherInterfaceStateMachine.CMD_START_TETHERING_ERROR, |
| 132 | TetherInterfaceStateMachine.CMD_STOP_TETHERING_ERROR, |
| 133 | TetherInterfaceStateMachine.CMD_SET_DNS_FORWARDERS_ERROR, |
| 134 | TetherInterfaceStateMachine.CMD_TETHER_CONNECTION_CHANGED |
Christopher Wiley | 08725a8 | 2016-05-18 16:32:44 -0700 | [diff] [blame] | 135 | }; |
| 136 | for (int command : NOOP_COMMANDS) { |
Christopher Wiley | 279eca3 | 2016-05-20 13:23:10 -0700 | [diff] [blame] | 137 | // None of these commands should trigger us to request action from |
Christopher Wiley | 08725a8 | 2016-05-18 16:32:44 -0700 | [diff] [blame] | 138 | // the rest of the system. |
Christopher Wiley | 279eca3 | 2016-05-20 13:23:10 -0700 | [diff] [blame] | 139 | dispatchCommand(command); |
| 140 | verifyNoMoreInteractions(mNMService, mStatsService, mTetherHelper); |
Christopher Wiley | 08725a8 | 2016-05-18 16:32:44 -0700 | [diff] [blame] | 141 | } |
| 142 | } |
| 143 | |
Christopher Wiley | 279eca3 | 2016-05-20 13:23:10 -0700 | [diff] [blame] | 144 | @Test |
Christopher Wiley | f972edc | 2016-05-23 16:17:30 -0700 | [diff] [blame] | 145 | public void handlesImmediateInterfaceDown() throws Exception { |
Erik Kline | a954be9 | 2017-02-13 17:12:02 +0900 | [diff] [blame] | 146 | initStateMachine(TETHERING_BLUETOOTH); |
Christopher Wiley | e90e0a7 | 2016-05-31 10:44:35 -0700 | [diff] [blame] | 147 | |
Mitchell Wills | 4622c2d | 2016-05-23 16:40:10 -0700 | [diff] [blame] | 148 | dispatchCommand(TetherInterfaceStateMachine.CMD_INTERFACE_DOWN); |
Erik Kline | ab6439b | 2017-06-06 19:24:21 +0900 | [diff] [blame] | 149 | verify(mTetherHelper).updateInterfaceState( |
| 150 | mTestedSm, STATE_UNAVAILABLE, TETHER_ERROR_NO_ERROR); |
| 151 | verify(mTetherHelper).updateLinkProperties(eq(mTestedSm), any(LinkProperties.class)); |
Christopher Wiley | 279eca3 | 2016-05-20 13:23:10 -0700 | [diff] [blame] | 152 | verifyNoMoreInteractions(mNMService, mStatsService, mTetherHelper); |
Christopher Wiley | 279eca3 | 2016-05-20 13:23:10 -0700 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | @Test |
Christopher Wiley | f972edc | 2016-05-23 16:17:30 -0700 | [diff] [blame] | 156 | public void canBeTethered() throws Exception { |
Erik Kline | a954be9 | 2017-02-13 17:12:02 +0900 | [diff] [blame] | 157 | initStateMachine(TETHERING_BLUETOOTH); |
Christopher Wiley | e90e0a7 | 2016-05-31 10:44:35 -0700 | [diff] [blame] | 158 | |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 159 | dispatchCommand(TetherInterfaceStateMachine.CMD_TETHER_REQUESTED, STATE_TETHERED); |
Christopher Wiley | 279eca3 | 2016-05-20 13:23:10 -0700 | [diff] [blame] | 160 | InOrder inOrder = inOrder(mTetherHelper, mNMService); |
Christopher Wiley | 279eca3 | 2016-05-20 13:23:10 -0700 | [diff] [blame] | 161 | inOrder.verify(mNMService).tetherInterface(IFACE_NAME); |
Erik Kline | ab6439b | 2017-06-06 19:24:21 +0900 | [diff] [blame] | 162 | inOrder.verify(mTetherHelper).updateInterfaceState( |
| 163 | mTestedSm, STATE_TETHERED, TETHER_ERROR_NO_ERROR); |
| 164 | inOrder.verify(mTetherHelper).updateLinkProperties( |
| 165 | eq(mTestedSm), any(LinkProperties.class)); |
Christopher Wiley | 279eca3 | 2016-05-20 13:23:10 -0700 | [diff] [blame] | 166 | verifyNoMoreInteractions(mNMService, mStatsService, mTetherHelper); |
Christopher Wiley | 279eca3 | 2016-05-20 13:23:10 -0700 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | @Test |
| 170 | public void canUnrequestTethering() throws Exception { |
Erik Kline | a954be9 | 2017-02-13 17:12:02 +0900 | [diff] [blame] | 171 | initTetheredStateMachine(TETHERING_BLUETOOTH, null); |
Christopher Wiley | 279eca3 | 2016-05-20 13:23:10 -0700 | [diff] [blame] | 172 | |
Mitchell Wills | 4622c2d | 2016-05-23 16:40:10 -0700 | [diff] [blame] | 173 | dispatchCommand(TetherInterfaceStateMachine.CMD_TETHER_UNREQUESTED); |
Christopher Wiley | 279eca3 | 2016-05-20 13:23:10 -0700 | [diff] [blame] | 174 | InOrder inOrder = inOrder(mNMService, mStatsService, mTetherHelper); |
Christopher Wiley | f972edc | 2016-05-23 16:17:30 -0700 | [diff] [blame] | 175 | inOrder.verify(mNMService).untetherInterface(IFACE_NAME); |
Erik Kline | ab6439b | 2017-06-06 19:24:21 +0900 | [diff] [blame] | 176 | inOrder.verify(mTetherHelper).updateInterfaceState( |
| 177 | mTestedSm, STATE_AVAILABLE, TETHER_ERROR_NO_ERROR); |
| 178 | inOrder.verify(mTetherHelper).updateLinkProperties( |
| 179 | eq(mTestedSm), any(LinkProperties.class)); |
Christopher Wiley | 279eca3 | 2016-05-20 13:23:10 -0700 | [diff] [blame] | 180 | verifyNoMoreInteractions(mNMService, mStatsService, mTetherHelper); |
Christopher Wiley | 279eca3 | 2016-05-20 13:23:10 -0700 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | @Test |
Christopher Wiley | f972edc | 2016-05-23 16:17:30 -0700 | [diff] [blame] | 184 | public void canBeTetheredAsUsb() throws Exception { |
Erik Kline | a954be9 | 2017-02-13 17:12:02 +0900 | [diff] [blame] | 185 | initStateMachine(TETHERING_USB); |
Christopher Wiley | 279eca3 | 2016-05-20 13:23:10 -0700 | [diff] [blame] | 186 | |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 187 | dispatchCommand(TetherInterfaceStateMachine.CMD_TETHER_REQUESTED, STATE_TETHERED); |
Christopher Wiley | 279eca3 | 2016-05-20 13:23:10 -0700 | [diff] [blame] | 188 | InOrder inOrder = inOrder(mTetherHelper, mNMService); |
Christopher Wiley | 279eca3 | 2016-05-20 13:23:10 -0700 | [diff] [blame] | 189 | inOrder.verify(mNMService).getInterfaceConfig(IFACE_NAME); |
| 190 | inOrder.verify(mNMService).setInterfaceConfig(IFACE_NAME, mInterfaceConfiguration); |
Christopher Wiley | 279eca3 | 2016-05-20 13:23:10 -0700 | [diff] [blame] | 191 | inOrder.verify(mNMService).tetherInterface(IFACE_NAME); |
Erik Kline | ab6439b | 2017-06-06 19:24:21 +0900 | [diff] [blame] | 192 | inOrder.verify(mTetherHelper).updateInterfaceState( |
| 193 | mTestedSm, STATE_TETHERED, TETHER_ERROR_NO_ERROR); |
| 194 | inOrder.verify(mTetherHelper).updateLinkProperties( |
Erik Kline | dd4d582 | 2017-06-12 18:20:08 +0900 | [diff] [blame] | 195 | eq(mTestedSm), mLinkPropertiesCaptor.capture()); |
| 196 | assertIPv4AddressAndDirectlyConnectedRoute(mLinkPropertiesCaptor.getValue()); |
Christopher Wiley | 279eca3 | 2016-05-20 13:23:10 -0700 | [diff] [blame] | 197 | verifyNoMoreInteractions(mNMService, mStatsService, mTetherHelper); |
Christopher Wiley | 279eca3 | 2016-05-20 13:23:10 -0700 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | @Test |
| 201 | public void handlesFirstUpstreamChange() throws Exception { |
Erik Kline | a954be9 | 2017-02-13 17:12:02 +0900 | [diff] [blame] | 202 | initTetheredStateMachine(TETHERING_BLUETOOTH, null); |
Christopher Wiley | 279eca3 | 2016-05-20 13:23:10 -0700 | [diff] [blame] | 203 | |
Erik Kline | ab6439b | 2017-06-06 19:24:21 +0900 | [diff] [blame] | 204 | // Telling the state machine about its upstream interface triggers |
| 205 | // a little more configuration. |
Christopher Wiley | 279eca3 | 2016-05-20 13:23:10 -0700 | [diff] [blame] | 206 | dispatchTetherConnectionChanged(UPSTREAM_IFACE); |
| 207 | InOrder inOrder = inOrder(mNMService); |
| 208 | inOrder.verify(mNMService).enableNat(IFACE_NAME, UPSTREAM_IFACE); |
| 209 | inOrder.verify(mNMService).startInterfaceForwarding(IFACE_NAME, UPSTREAM_IFACE); |
| 210 | verifyNoMoreInteractions(mNMService, mStatsService, mTetherHelper); |
Christopher Wiley | 279eca3 | 2016-05-20 13:23:10 -0700 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | @Test |
| 214 | public void handlesChangingUpstream() throws Exception { |
Erik Kline | a954be9 | 2017-02-13 17:12:02 +0900 | [diff] [blame] | 215 | initTetheredStateMachine(TETHERING_BLUETOOTH, UPSTREAM_IFACE); |
Christopher Wiley | 279eca3 | 2016-05-20 13:23:10 -0700 | [diff] [blame] | 216 | |
| 217 | dispatchTetherConnectionChanged(UPSTREAM_IFACE2); |
| 218 | InOrder inOrder = inOrder(mNMService, mStatsService); |
| 219 | inOrder.verify(mStatsService).forceUpdate(); |
| 220 | inOrder.verify(mNMService).stopInterfaceForwarding(IFACE_NAME, UPSTREAM_IFACE); |
| 221 | inOrder.verify(mNMService).disableNat(IFACE_NAME, UPSTREAM_IFACE); |
| 222 | inOrder.verify(mNMService).enableNat(IFACE_NAME, UPSTREAM_IFACE2); |
| 223 | inOrder.verify(mNMService).startInterfaceForwarding(IFACE_NAME, UPSTREAM_IFACE2); |
| 224 | verifyNoMoreInteractions(mNMService, mStatsService, mTetherHelper); |
Christopher Wiley | 279eca3 | 2016-05-20 13:23:10 -0700 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | @Test |
Erik Kline | a954be9 | 2017-02-13 17:12:02 +0900 | [diff] [blame] | 228 | public void handlesChangingUpstreamNatFailure() throws Exception { |
| 229 | initTetheredStateMachine(TETHERING_WIFI, UPSTREAM_IFACE); |
| 230 | |
| 231 | doThrow(RemoteException.class).when(mNMService).enableNat(IFACE_NAME, UPSTREAM_IFACE2); |
| 232 | |
| 233 | dispatchTetherConnectionChanged(UPSTREAM_IFACE2); |
| 234 | InOrder inOrder = inOrder(mNMService, mStatsService); |
| 235 | inOrder.verify(mStatsService).forceUpdate(); |
| 236 | inOrder.verify(mNMService).stopInterfaceForwarding(IFACE_NAME, UPSTREAM_IFACE); |
| 237 | inOrder.verify(mNMService).disableNat(IFACE_NAME, UPSTREAM_IFACE); |
| 238 | inOrder.verify(mNMService).enableNat(IFACE_NAME, UPSTREAM_IFACE2); |
Erik Kline | 8ea4548 | 2017-02-13 17:28:53 +0900 | [diff] [blame] | 239 | inOrder.verify(mStatsService).forceUpdate(); |
| 240 | inOrder.verify(mNMService).stopInterfaceForwarding(IFACE_NAME, UPSTREAM_IFACE2); |
| 241 | inOrder.verify(mNMService).disableNat(IFACE_NAME, UPSTREAM_IFACE2); |
Erik Kline | a954be9 | 2017-02-13 17:12:02 +0900 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | @Test |
| 245 | public void handlesChangingUpstreamInterfaceForwardingFailure() throws Exception { |
| 246 | initTetheredStateMachine(TETHERING_WIFI, UPSTREAM_IFACE); |
| 247 | |
| 248 | doThrow(RemoteException.class).when(mNMService).startInterfaceForwarding( |
| 249 | IFACE_NAME, UPSTREAM_IFACE2); |
| 250 | |
| 251 | dispatchTetherConnectionChanged(UPSTREAM_IFACE2); |
| 252 | InOrder inOrder = inOrder(mNMService, mStatsService); |
| 253 | inOrder.verify(mStatsService).forceUpdate(); |
| 254 | inOrder.verify(mNMService).stopInterfaceForwarding(IFACE_NAME, UPSTREAM_IFACE); |
| 255 | inOrder.verify(mNMService).disableNat(IFACE_NAME, UPSTREAM_IFACE); |
| 256 | inOrder.verify(mNMService).enableNat(IFACE_NAME, UPSTREAM_IFACE2); |
| 257 | inOrder.verify(mNMService).startInterfaceForwarding(IFACE_NAME, UPSTREAM_IFACE2); |
Erik Kline | 8ea4548 | 2017-02-13 17:28:53 +0900 | [diff] [blame] | 258 | inOrder.verify(mStatsService).forceUpdate(); |
| 259 | inOrder.verify(mNMService).stopInterfaceForwarding(IFACE_NAME, UPSTREAM_IFACE2); |
| 260 | inOrder.verify(mNMService).disableNat(IFACE_NAME, UPSTREAM_IFACE2); |
Erik Kline | a954be9 | 2017-02-13 17:12:02 +0900 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | @Test |
Christopher Wiley | 279eca3 | 2016-05-20 13:23:10 -0700 | [diff] [blame] | 264 | public void canUnrequestTetheringWithUpstream() throws Exception { |
Erik Kline | a954be9 | 2017-02-13 17:12:02 +0900 | [diff] [blame] | 265 | initTetheredStateMachine(TETHERING_BLUETOOTH, UPSTREAM_IFACE); |
Christopher Wiley | 279eca3 | 2016-05-20 13:23:10 -0700 | [diff] [blame] | 266 | |
Mitchell Wills | 4622c2d | 2016-05-23 16:40:10 -0700 | [diff] [blame] | 267 | dispatchCommand(TetherInterfaceStateMachine.CMD_TETHER_UNREQUESTED); |
Christopher Wiley | 279eca3 | 2016-05-20 13:23:10 -0700 | [diff] [blame] | 268 | InOrder inOrder = inOrder(mNMService, mStatsService, mTetherHelper); |
| 269 | inOrder.verify(mStatsService).forceUpdate(); |
| 270 | inOrder.verify(mNMService).stopInterfaceForwarding(IFACE_NAME, UPSTREAM_IFACE); |
| 271 | inOrder.verify(mNMService).disableNat(IFACE_NAME, UPSTREAM_IFACE); |
| 272 | inOrder.verify(mNMService).untetherInterface(IFACE_NAME); |
Erik Kline | ab6439b | 2017-06-06 19:24:21 +0900 | [diff] [blame] | 273 | inOrder.verify(mTetherHelper).updateInterfaceState( |
| 274 | mTestedSm, STATE_AVAILABLE, TETHER_ERROR_NO_ERROR); |
| 275 | inOrder.verify(mTetherHelper).updateLinkProperties( |
| 276 | eq(mTestedSm), any(LinkProperties.class)); |
Christopher Wiley | 279eca3 | 2016-05-20 13:23:10 -0700 | [diff] [blame] | 277 | verifyNoMoreInteractions(mNMService, mStatsService, mTetherHelper); |
Christopher Wiley | 279eca3 | 2016-05-20 13:23:10 -0700 | [diff] [blame] | 278 | } |
| 279 | |
Christopher Wiley | f972edc | 2016-05-23 16:17:30 -0700 | [diff] [blame] | 280 | @Test |
| 281 | public void interfaceDownLeadsToUnavailable() throws Exception { |
| 282 | for (boolean shouldThrow : new boolean[]{true, false}) { |
Erik Kline | a954be9 | 2017-02-13 17:12:02 +0900 | [diff] [blame] | 283 | initTetheredStateMachine(TETHERING_USB, null); |
Christopher Wiley | f972edc | 2016-05-23 16:17:30 -0700 | [diff] [blame] | 284 | |
| 285 | if (shouldThrow) { |
| 286 | doThrow(RemoteException.class).when(mNMService).untetherInterface(IFACE_NAME); |
| 287 | } |
| 288 | dispatchCommand(TetherInterfaceStateMachine.CMD_INTERFACE_DOWN); |
Christopher Wiley | e90e0a7 | 2016-05-31 10:44:35 -0700 | [diff] [blame] | 289 | InOrder usbTeardownOrder = inOrder(mNMService, mInterfaceConfiguration, mTetherHelper); |
Christopher Wiley | f972edc | 2016-05-23 16:17:30 -0700 | [diff] [blame] | 290 | usbTeardownOrder.verify(mInterfaceConfiguration).setInterfaceDown(); |
| 291 | usbTeardownOrder.verify(mNMService).setInterfaceConfig( |
| 292 | IFACE_NAME, mInterfaceConfiguration); |
Erik Kline | ab6439b | 2017-06-06 19:24:21 +0900 | [diff] [blame] | 293 | usbTeardownOrder.verify(mTetherHelper).updateInterfaceState( |
| 294 | mTestedSm, STATE_UNAVAILABLE, TETHER_ERROR_NO_ERROR); |
| 295 | usbTeardownOrder.verify(mTetherHelper).updateLinkProperties( |
Erik Kline | dd4d582 | 2017-06-12 18:20:08 +0900 | [diff] [blame] | 296 | eq(mTestedSm), mLinkPropertiesCaptor.capture()); |
| 297 | assertNoAddressesNorRoutes(mLinkPropertiesCaptor.getValue()); |
Christopher Wiley | f972edc | 2016-05-23 16:17:30 -0700 | [diff] [blame] | 298 | } |
| 299 | } |
| 300 | |
| 301 | @Test |
| 302 | public void usbShouldBeTornDownOnTetherError() throws Exception { |
Erik Kline | a954be9 | 2017-02-13 17:12:02 +0900 | [diff] [blame] | 303 | initStateMachine(TETHERING_USB); |
Christopher Wiley | f972edc | 2016-05-23 16:17:30 -0700 | [diff] [blame] | 304 | |
| 305 | doThrow(RemoteException.class).when(mNMService).tetherInterface(IFACE_NAME); |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 306 | dispatchCommand(TetherInterfaceStateMachine.CMD_TETHER_REQUESTED, STATE_TETHERED); |
Christopher Wiley | e90e0a7 | 2016-05-31 10:44:35 -0700 | [diff] [blame] | 307 | InOrder usbTeardownOrder = inOrder(mNMService, mInterfaceConfiguration, mTetherHelper); |
Christopher Wiley | f972edc | 2016-05-23 16:17:30 -0700 | [diff] [blame] | 308 | usbTeardownOrder.verify(mInterfaceConfiguration).setInterfaceDown(); |
| 309 | usbTeardownOrder.verify(mNMService).setInterfaceConfig( |
| 310 | IFACE_NAME, mInterfaceConfiguration); |
Erik Kline | ab6439b | 2017-06-06 19:24:21 +0900 | [diff] [blame] | 311 | usbTeardownOrder.verify(mTetherHelper).updateInterfaceState( |
| 312 | mTestedSm, STATE_AVAILABLE, TETHER_ERROR_TETHER_IFACE_ERROR); |
| 313 | usbTeardownOrder.verify(mTetherHelper).updateLinkProperties( |
Erik Kline | dd4d582 | 2017-06-12 18:20:08 +0900 | [diff] [blame] | 314 | eq(mTestedSm), mLinkPropertiesCaptor.capture()); |
| 315 | assertNoAddressesNorRoutes(mLinkPropertiesCaptor.getValue()); |
Christopher Wiley | f972edc | 2016-05-23 16:17:30 -0700 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | @Test |
| 319 | public void shouldTearDownUsbOnUpstreamError() throws Exception { |
Erik Kline | a954be9 | 2017-02-13 17:12:02 +0900 | [diff] [blame] | 320 | initTetheredStateMachine(TETHERING_USB, null); |
Christopher Wiley | f972edc | 2016-05-23 16:17:30 -0700 | [diff] [blame] | 321 | |
| 322 | doThrow(RemoteException.class).when(mNMService).enableNat(anyString(), anyString()); |
| 323 | dispatchTetherConnectionChanged(UPSTREAM_IFACE); |
Christopher Wiley | e90e0a7 | 2016-05-31 10:44:35 -0700 | [diff] [blame] | 324 | InOrder usbTeardownOrder = inOrder(mNMService, mInterfaceConfiguration, mTetherHelper); |
Christopher Wiley | f972edc | 2016-05-23 16:17:30 -0700 | [diff] [blame] | 325 | usbTeardownOrder.verify(mInterfaceConfiguration).setInterfaceDown(); |
| 326 | usbTeardownOrder.verify(mNMService).setInterfaceConfig(IFACE_NAME, mInterfaceConfiguration); |
Erik Kline | ab6439b | 2017-06-06 19:24:21 +0900 | [diff] [blame] | 327 | usbTeardownOrder.verify(mTetherHelper).updateInterfaceState( |
| 328 | mTestedSm, STATE_AVAILABLE, TETHER_ERROR_ENABLE_NAT_ERROR); |
| 329 | usbTeardownOrder.verify(mTetherHelper).updateLinkProperties( |
Erik Kline | dd4d582 | 2017-06-12 18:20:08 +0900 | [diff] [blame] | 330 | eq(mTestedSm), mLinkPropertiesCaptor.capture()); |
| 331 | assertNoAddressesNorRoutes(mLinkPropertiesCaptor.getValue()); |
Christopher Wiley | f972edc | 2016-05-23 16:17:30 -0700 | [diff] [blame] | 332 | } |
Christopher Wiley | 279eca3 | 2016-05-20 13:23:10 -0700 | [diff] [blame] | 333 | |
Erik Kline | 624bf3d | 2017-02-14 15:55:00 +0900 | [diff] [blame] | 334 | @Test |
| 335 | public void ignoresDuplicateUpstreamNotifications() throws Exception { |
| 336 | initTetheredStateMachine(TETHERING_WIFI, UPSTREAM_IFACE); |
| 337 | |
| 338 | verifyNoMoreInteractions(mNMService, mStatsService, mTetherHelper); |
| 339 | |
| 340 | for (int i = 0; i < 5; i++) { |
| 341 | dispatchTetherConnectionChanged(UPSTREAM_IFACE); |
| 342 | verifyNoMoreInteractions(mNMService, mStatsService, mTetherHelper); |
| 343 | } |
| 344 | } |
| 345 | |
Christopher Wiley | 279eca3 | 2016-05-20 13:23:10 -0700 | [diff] [blame] | 346 | /** |
| 347 | * Send a command to the state machine under test, and run the event loop to idle. |
| 348 | * |
Mitchell Wills | 4622c2d | 2016-05-23 16:40:10 -0700 | [diff] [blame] | 349 | * @param command One of the TetherInterfaceStateMachine.CMD_* constants. |
Remi NGUYEN VAN | a911e84 | 2018-03-15 11:57:14 +0900 | [diff] [blame] | 350 | * @param arg1 An additional argument to pass. |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 351 | */ |
| 352 | private void dispatchCommand(int command, int arg1) { |
| 353 | mTestedSm.sendMessage(command, arg1); |
| 354 | mLooper.dispatchAll(); |
| 355 | } |
| 356 | |
| 357 | /** |
| 358 | * Send a command to the state machine under test, and run the event loop to idle. |
| 359 | * |
| 360 | * @param command One of the TetherInterfaceStateMachine.CMD_* constants. |
Christopher Wiley | 279eca3 | 2016-05-20 13:23:10 -0700 | [diff] [blame] | 361 | */ |
| 362 | private void dispatchCommand(int command) { |
| 363 | mTestedSm.sendMessage(command); |
| 364 | mLooper.dispatchAll(); |
| 365 | } |
| 366 | |
| 367 | /** |
| 368 | * Special override to tell the state machine that the upstream interface has changed. |
| 369 | * |
| 370 | * @see #dispatchCommand(int) |
| 371 | * @param upstreamIface String name of upstream interface (or null) |
| 372 | */ |
| 373 | private void dispatchTetherConnectionChanged(String upstreamIface) { |
Mitchell Wills | 4622c2d | 2016-05-23 16:40:10 -0700 | [diff] [blame] | 374 | mTestedSm.sendMessage(TetherInterfaceStateMachine.CMD_TETHER_CONNECTION_CHANGED, |
Remi NGUYEN VAN | 25a7e4f | 2018-03-09 14:07:18 +0900 | [diff] [blame^] | 375 | new InterfaceSet(upstreamIface)); |
Christopher Wiley | 279eca3 | 2016-05-20 13:23:10 -0700 | [diff] [blame] | 376 | mLooper.dispatchAll(); |
Christopher Wiley | 08725a8 | 2016-05-18 16:32:44 -0700 | [diff] [blame] | 377 | } |
Erik Kline | dd4d582 | 2017-06-12 18:20:08 +0900 | [diff] [blame] | 378 | |
| 379 | private void assertIPv4AddressAndDirectlyConnectedRoute(LinkProperties lp) { |
| 380 | // Find the first IPv4 LinkAddress. |
| 381 | LinkAddress addr4 = null; |
| 382 | for (LinkAddress addr : lp.getLinkAddresses()) { |
| 383 | if (!(addr.getAddress() instanceof Inet4Address)) continue; |
| 384 | addr4 = addr; |
| 385 | break; |
| 386 | } |
| 387 | assertTrue("missing IPv4 address", addr4 != null); |
| 388 | |
| 389 | // Assert the presence of the associated directly connected route. |
| 390 | final RouteInfo directlyConnected = new RouteInfo(addr4, null, lp.getInterfaceName()); |
| 391 | assertTrue("missing directly connected route: '" + directlyConnected.toString() + "'", |
| 392 | lp.getRoutes().contains(directlyConnected)); |
| 393 | } |
| 394 | |
| 395 | private void assertNoAddressesNorRoutes(LinkProperties lp) { |
| 396 | assertTrue(lp.getLinkAddresses().isEmpty()); |
| 397 | assertTrue(lp.getRoutes().isEmpty()); |
| 398 | // We also check that interface name is non-empty, because we should |
| 399 | // never see an empty interface name in any LinkProperties update. |
| 400 | assertFalse(TextUtils.isEmpty(lp.getInterfaceName())); |
| 401 | } |
Lorenzo Colitti | 5bce5a1 | 2016-10-28 17:45:55 +0900 | [diff] [blame] | 402 | } |