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 | |
Christopher Wiley | f972edc | 2016-05-23 16:17:30 -0700 | [diff] [blame] | 19 | import static org.mockito.Matchers.anyString; |
| 20 | import static org.mockito.Mockito.doThrow; |
Christopher Wiley | 279eca3 | 2016-05-20 13:23:10 -0700 | [diff] [blame] | 21 | import static org.mockito.Mockito.inOrder; |
| 22 | import static org.mockito.Mockito.reset; |
| 23 | import static org.mockito.Mockito.verify; |
Christopher Wiley | 08725a8 | 2016-05-18 16:32:44 -0700 | [diff] [blame] | 24 | import static org.mockito.Mockito.verifyNoMoreInteractions; |
Christopher Wiley | 279eca3 | 2016-05-20 13:23:10 -0700 | [diff] [blame] | 25 | import static org.mockito.Mockito.when; |
Christopher Wiley | 08725a8 | 2016-05-18 16:32:44 -0700 | [diff] [blame] | 26 | |
Christopher Wiley | e90e0a7 | 2016-05-31 10:44:35 -0700 | [diff] [blame] | 27 | import static android.net.ConnectivityManager.TETHER_ERROR_ENABLE_NAT_ERROR; |
| 28 | import static android.net.ConnectivityManager.TETHER_ERROR_NO_ERROR; |
| 29 | import static android.net.ConnectivityManager.TETHER_ERROR_TETHER_IFACE_ERROR; |
| 30 | import static android.net.ConnectivityManager.TETHER_ERROR_UNTETHER_IFACE_ERROR; |
Erik Kline | a954be9 | 2017-02-13 17:12:02 +0900 | [diff] [blame] | 31 | import static android.net.ConnectivityManager.TETHERING_BLUETOOTH; |
| 32 | import static android.net.ConnectivityManager.TETHERING_USB; |
| 33 | import static android.net.ConnectivityManager.TETHERING_WIFI; |
Christopher Wiley | e90e0a7 | 2016-05-31 10:44:35 -0700 | [diff] [blame] | 34 | import static com.android.server.connectivity.tethering.IControlsTethering.STATE_AVAILABLE; |
| 35 | import static com.android.server.connectivity.tethering.IControlsTethering.STATE_TETHERED; |
| 36 | import static com.android.server.connectivity.tethering.IControlsTethering.STATE_UNAVAILABLE; |
| 37 | |
Christopher Wiley | cd0cfbb | 2016-05-31 14:43:08 -0700 | [diff] [blame] | 38 | import android.net.ConnectivityManager; |
Christopher Wiley | 08725a8 | 2016-05-18 16:32:44 -0700 | [diff] [blame] | 39 | import android.net.INetworkStatsService; |
Christopher Wiley | 279eca3 | 2016-05-20 13:23:10 -0700 | [diff] [blame] | 40 | import android.net.InterfaceConfiguration; |
Christopher Wiley | 08725a8 | 2016-05-18 16:32:44 -0700 | [diff] [blame] | 41 | import android.os.INetworkManagementService; |
Christopher Wiley | 279eca3 | 2016-05-20 13:23:10 -0700 | [diff] [blame] | 42 | import android.os.RemoteException; |
Christopher Wiley | 08725a8 | 2016-05-18 16:32:44 -0700 | [diff] [blame] | 43 | import android.os.test.TestLooper; |
Christopher Wiley | 9bc0df2 | 2016-05-25 13:57:27 -0700 | [diff] [blame] | 44 | import android.support.test.filters.SmallTest; |
| 45 | import android.support.test.runner.AndroidJUnit4; |
Christopher Wiley | 08725a8 | 2016-05-18 16:32:44 -0700 | [diff] [blame] | 46 | |
| 47 | import org.junit.Before; |
| 48 | import org.junit.Test; |
Christopher Wiley | 9bc0df2 | 2016-05-25 13:57:27 -0700 | [diff] [blame] | 49 | import org.junit.runner.RunWith; |
Christopher Wiley | 279eca3 | 2016-05-20 13:23:10 -0700 | [diff] [blame] | 50 | import org.mockito.InOrder; |
Christopher Wiley | 08725a8 | 2016-05-18 16:32:44 -0700 | [diff] [blame] | 51 | import org.mockito.Mock; |
| 52 | import org.mockito.MockitoAnnotations; |
| 53 | |
Christopher Wiley | 9bc0df2 | 2016-05-25 13:57:27 -0700 | [diff] [blame] | 54 | @RunWith(AndroidJUnit4.class) |
| 55 | @SmallTest |
Mitchell Wills | 4622c2d | 2016-05-23 16:40:10 -0700 | [diff] [blame] | 56 | public class TetherInterfaceStateMachineTest { |
Christopher Wiley | 08725a8 | 2016-05-18 16:32:44 -0700 | [diff] [blame] | 57 | private static final String IFACE_NAME = "testnet1"; |
Christopher Wiley | 279eca3 | 2016-05-20 13:23:10 -0700 | [diff] [blame] | 58 | private static final String UPSTREAM_IFACE = "upstream0"; |
| 59 | private static final String UPSTREAM_IFACE2 = "upstream1"; |
Christopher Wiley | 08725a8 | 2016-05-18 16:32:44 -0700 | [diff] [blame] | 60 | |
| 61 | @Mock private INetworkManagementService mNMService; |
| 62 | @Mock private INetworkStatsService mStatsService; |
| 63 | @Mock private IControlsTethering mTetherHelper; |
Christopher Wiley | 279eca3 | 2016-05-20 13:23:10 -0700 | [diff] [blame] | 64 | @Mock private InterfaceConfiguration mInterfaceConfiguration; |
Lorenzo Colitti | 5bce5a1 | 2016-10-28 17:45:55 +0900 | [diff] [blame] | 65 | @Mock private IPv6TetheringInterfaceServices mIPv6TetheringInterfaceServices; |
Christopher Wiley | 08725a8 | 2016-05-18 16:32:44 -0700 | [diff] [blame] | 66 | |
| 67 | private final TestLooper mLooper = new TestLooper(); |
Mitchell Wills | 4622c2d | 2016-05-23 16:40:10 -0700 | [diff] [blame] | 68 | private TetherInterfaceStateMachine mTestedSm; |
Christopher Wiley | 08725a8 | 2016-05-18 16:32:44 -0700 | [diff] [blame] | 69 | |
Christopher Wiley | cd0cfbb | 2016-05-31 14:43:08 -0700 | [diff] [blame] | 70 | private void initStateMachine(int interfaceType) throws Exception { |
| 71 | mTestedSm = new TetherInterfaceStateMachine(IFACE_NAME, mLooper.getLooper(), interfaceType, |
Lorenzo Colitti | 5bce5a1 | 2016-10-28 17:45:55 +0900 | [diff] [blame] | 72 | mNMService, mStatsService, mTetherHelper, mIPv6TetheringInterfaceServices); |
Christopher Wiley | 279eca3 | 2016-05-20 13:23:10 -0700 | [diff] [blame] | 73 | mTestedSm.start(); |
| 74 | // Starting the state machine always puts us in a consistent state and notifies |
| 75 | // the test of the world that we've changed from an unknown to available state. |
| 76 | mLooper.dispatchAll(); |
| 77 | reset(mNMService, mStatsService, mTetherHelper); |
Christopher Wiley | f972edc | 2016-05-23 16:17:30 -0700 | [diff] [blame] | 78 | when(mNMService.getInterfaceConfig(IFACE_NAME)).thenReturn(mInterfaceConfiguration); |
Christopher Wiley | 279eca3 | 2016-05-20 13:23:10 -0700 | [diff] [blame] | 79 | } |
| 80 | |
Christopher Wiley | cd0cfbb | 2016-05-31 14:43:08 -0700 | [diff] [blame] | 81 | private void initTetheredStateMachine(int interfaceType, String upstreamIface) throws Exception { |
| 82 | initStateMachine(interfaceType); |
Mitchell Wills | 4622c2d | 2016-05-23 16:40:10 -0700 | [diff] [blame] | 83 | dispatchCommand(TetherInterfaceStateMachine.CMD_TETHER_REQUESTED); |
Christopher Wiley | 279eca3 | 2016-05-20 13:23:10 -0700 | [diff] [blame] | 84 | if (upstreamIface != null) { |
| 85 | dispatchTetherConnectionChanged(upstreamIface); |
| 86 | } |
| 87 | reset(mNMService, mStatsService, mTetherHelper); |
Christopher Wiley | f972edc | 2016-05-23 16:17:30 -0700 | [diff] [blame] | 88 | when(mNMService.getInterfaceConfig(IFACE_NAME)).thenReturn(mInterfaceConfiguration); |
Christopher Wiley | 279eca3 | 2016-05-20 13:23:10 -0700 | [diff] [blame] | 89 | } |
| 90 | |
Christopher Wiley | e90e0a7 | 2016-05-31 10:44:35 -0700 | [diff] [blame] | 91 | @Before public void setUp() throws Exception { |
Christopher Wiley | 08725a8 | 2016-05-18 16:32:44 -0700 | [diff] [blame] | 92 | MockitoAnnotations.initMocks(this); |
Christopher Wiley | 279eca3 | 2016-05-20 13:23:10 -0700 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | @Test |
| 96 | public void startsOutAvailable() { |
Christopher Wiley | cd0cfbb | 2016-05-31 14:43:08 -0700 | [diff] [blame] | 97 | mTestedSm = new TetherInterfaceStateMachine(IFACE_NAME, mLooper.getLooper(), |
Erik Kline | a954be9 | 2017-02-13 17:12:02 +0900 | [diff] [blame] | 98 | TETHERING_BLUETOOTH, mNMService, mStatsService, mTetherHelper, |
Lorenzo Colitti | 5bce5a1 | 2016-10-28 17:45:55 +0900 | [diff] [blame] | 99 | mIPv6TetheringInterfaceServices); |
Christopher Wiley | 08725a8 | 2016-05-18 16:32:44 -0700 | [diff] [blame] | 100 | mTestedSm.start(); |
Christopher Wiley | 279eca3 | 2016-05-20 13:23:10 -0700 | [diff] [blame] | 101 | mLooper.dispatchAll(); |
Christopher Wiley | e90e0a7 | 2016-05-31 10:44:35 -0700 | [diff] [blame] | 102 | verify(mTetherHelper).notifyInterfaceStateChange( |
| 103 | IFACE_NAME, mTestedSm, STATE_AVAILABLE, TETHER_ERROR_NO_ERROR); |
Christopher Wiley | 279eca3 | 2016-05-20 13:23:10 -0700 | [diff] [blame] | 104 | verifyNoMoreInteractions(mTetherHelper, mNMService, mStatsService); |
Christopher Wiley | 08725a8 | 2016-05-18 16:32:44 -0700 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | @Test |
Christopher Wiley | f972edc | 2016-05-23 16:17:30 -0700 | [diff] [blame] | 108 | public void shouldDoNothingUntilRequested() throws Exception { |
Erik Kline | a954be9 | 2017-02-13 17:12:02 +0900 | [diff] [blame] | 109 | initStateMachine(TETHERING_BLUETOOTH); |
Christopher Wiley | 08725a8 | 2016-05-18 16:32:44 -0700 | [diff] [blame] | 110 | final int [] NOOP_COMMANDS = { |
Mitchell Wills | 4622c2d | 2016-05-23 16:40:10 -0700 | [diff] [blame] | 111 | TetherInterfaceStateMachine.CMD_TETHER_UNREQUESTED, |
| 112 | TetherInterfaceStateMachine.CMD_IP_FORWARDING_ENABLE_ERROR, |
| 113 | TetherInterfaceStateMachine.CMD_IP_FORWARDING_DISABLE_ERROR, |
| 114 | TetherInterfaceStateMachine.CMD_START_TETHERING_ERROR, |
| 115 | TetherInterfaceStateMachine.CMD_STOP_TETHERING_ERROR, |
| 116 | TetherInterfaceStateMachine.CMD_SET_DNS_FORWARDERS_ERROR, |
| 117 | TetherInterfaceStateMachine.CMD_TETHER_CONNECTION_CHANGED |
Christopher Wiley | 08725a8 | 2016-05-18 16:32:44 -0700 | [diff] [blame] | 118 | }; |
| 119 | for (int command : NOOP_COMMANDS) { |
Christopher Wiley | 279eca3 | 2016-05-20 13:23:10 -0700 | [diff] [blame] | 120 | // None of these commands should trigger us to request action from |
Christopher Wiley | 08725a8 | 2016-05-18 16:32:44 -0700 | [diff] [blame] | 121 | // the rest of the system. |
Christopher Wiley | 279eca3 | 2016-05-20 13:23:10 -0700 | [diff] [blame] | 122 | dispatchCommand(command); |
| 123 | verifyNoMoreInteractions(mNMService, mStatsService, mTetherHelper); |
Christopher Wiley | 08725a8 | 2016-05-18 16:32:44 -0700 | [diff] [blame] | 124 | } |
| 125 | } |
| 126 | |
Christopher Wiley | 279eca3 | 2016-05-20 13:23:10 -0700 | [diff] [blame] | 127 | @Test |
Christopher Wiley | f972edc | 2016-05-23 16:17:30 -0700 | [diff] [blame] | 128 | public void handlesImmediateInterfaceDown() throws Exception { |
Erik Kline | a954be9 | 2017-02-13 17:12:02 +0900 | [diff] [blame] | 129 | initStateMachine(TETHERING_BLUETOOTH); |
Christopher Wiley | e90e0a7 | 2016-05-31 10:44:35 -0700 | [diff] [blame] | 130 | |
Mitchell Wills | 4622c2d | 2016-05-23 16:40:10 -0700 | [diff] [blame] | 131 | dispatchCommand(TetherInterfaceStateMachine.CMD_INTERFACE_DOWN); |
Christopher Wiley | e90e0a7 | 2016-05-31 10:44:35 -0700 | [diff] [blame] | 132 | verify(mTetherHelper).notifyInterfaceStateChange( |
| 133 | IFACE_NAME, mTestedSm, STATE_UNAVAILABLE, TETHER_ERROR_NO_ERROR); |
Christopher Wiley | 279eca3 | 2016-05-20 13:23:10 -0700 | [diff] [blame] | 134 | verifyNoMoreInteractions(mNMService, mStatsService, mTetherHelper); |
Christopher Wiley | 279eca3 | 2016-05-20 13:23:10 -0700 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | @Test |
Christopher Wiley | f972edc | 2016-05-23 16:17:30 -0700 | [diff] [blame] | 138 | public void canBeTethered() throws Exception { |
Erik Kline | a954be9 | 2017-02-13 17:12:02 +0900 | [diff] [blame] | 139 | initStateMachine(TETHERING_BLUETOOTH); |
Christopher Wiley | e90e0a7 | 2016-05-31 10:44:35 -0700 | [diff] [blame] | 140 | |
Mitchell Wills | 4622c2d | 2016-05-23 16:40:10 -0700 | [diff] [blame] | 141 | dispatchCommand(TetherInterfaceStateMachine.CMD_TETHER_REQUESTED); |
Christopher Wiley | 279eca3 | 2016-05-20 13:23:10 -0700 | [diff] [blame] | 142 | InOrder inOrder = inOrder(mTetherHelper, mNMService); |
Christopher Wiley | 279eca3 | 2016-05-20 13:23:10 -0700 | [diff] [blame] | 143 | inOrder.verify(mNMService).tetherInterface(IFACE_NAME); |
Christopher Wiley | e90e0a7 | 2016-05-31 10:44:35 -0700 | [diff] [blame] | 144 | inOrder.verify(mTetherHelper).notifyInterfaceStateChange( |
| 145 | IFACE_NAME, mTestedSm, STATE_TETHERED, TETHER_ERROR_NO_ERROR); |
Christopher Wiley | 279eca3 | 2016-05-20 13:23:10 -0700 | [diff] [blame] | 146 | verifyNoMoreInteractions(mNMService, mStatsService, mTetherHelper); |
Christopher Wiley | 279eca3 | 2016-05-20 13:23:10 -0700 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | @Test |
| 150 | public void canUnrequestTethering() throws Exception { |
Erik Kline | a954be9 | 2017-02-13 17:12:02 +0900 | [diff] [blame] | 151 | initTetheredStateMachine(TETHERING_BLUETOOTH, null); |
Christopher Wiley | 279eca3 | 2016-05-20 13:23:10 -0700 | [diff] [blame] | 152 | |
Mitchell Wills | 4622c2d | 2016-05-23 16:40:10 -0700 | [diff] [blame] | 153 | dispatchCommand(TetherInterfaceStateMachine.CMD_TETHER_UNREQUESTED); |
Christopher Wiley | 279eca3 | 2016-05-20 13:23:10 -0700 | [diff] [blame] | 154 | InOrder inOrder = inOrder(mNMService, mStatsService, mTetherHelper); |
Christopher Wiley | f972edc | 2016-05-23 16:17:30 -0700 | [diff] [blame] | 155 | inOrder.verify(mNMService).untetherInterface(IFACE_NAME); |
Christopher Wiley | e90e0a7 | 2016-05-31 10:44:35 -0700 | [diff] [blame] | 156 | inOrder.verify(mTetherHelper).notifyInterfaceStateChange( |
| 157 | IFACE_NAME, mTestedSm, STATE_AVAILABLE, TETHER_ERROR_NO_ERROR); |
Christopher Wiley | 279eca3 | 2016-05-20 13:23:10 -0700 | [diff] [blame] | 158 | verifyNoMoreInteractions(mNMService, mStatsService, mTetherHelper); |
Christopher Wiley | 279eca3 | 2016-05-20 13:23:10 -0700 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | @Test |
Christopher Wiley | f972edc | 2016-05-23 16:17:30 -0700 | [diff] [blame] | 162 | public void canBeTetheredAsUsb() throws Exception { |
Erik Kline | a954be9 | 2017-02-13 17:12:02 +0900 | [diff] [blame] | 163 | initStateMachine(TETHERING_USB); |
Christopher Wiley | 279eca3 | 2016-05-20 13:23:10 -0700 | [diff] [blame] | 164 | |
Mitchell Wills | 4622c2d | 2016-05-23 16:40:10 -0700 | [diff] [blame] | 165 | dispatchCommand(TetherInterfaceStateMachine.CMD_TETHER_REQUESTED); |
Christopher Wiley | 279eca3 | 2016-05-20 13:23:10 -0700 | [diff] [blame] | 166 | InOrder inOrder = inOrder(mTetherHelper, mNMService); |
Christopher Wiley | 279eca3 | 2016-05-20 13:23:10 -0700 | [diff] [blame] | 167 | inOrder.verify(mNMService).getInterfaceConfig(IFACE_NAME); |
| 168 | inOrder.verify(mNMService).setInterfaceConfig(IFACE_NAME, mInterfaceConfiguration); |
Christopher Wiley | 279eca3 | 2016-05-20 13:23:10 -0700 | [diff] [blame] | 169 | inOrder.verify(mNMService).tetherInterface(IFACE_NAME); |
Christopher Wiley | e90e0a7 | 2016-05-31 10:44:35 -0700 | [diff] [blame] | 170 | inOrder.verify(mTetherHelper).notifyInterfaceStateChange( |
| 171 | IFACE_NAME, mTestedSm, STATE_TETHERED, TETHER_ERROR_NO_ERROR); |
Christopher Wiley | 279eca3 | 2016-05-20 13:23:10 -0700 | [diff] [blame] | 172 | verifyNoMoreInteractions(mNMService, mStatsService, mTetherHelper); |
Christopher Wiley | 279eca3 | 2016-05-20 13:23:10 -0700 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | @Test |
| 176 | public void handlesFirstUpstreamChange() throws Exception { |
Erik Kline | a954be9 | 2017-02-13 17:12:02 +0900 | [diff] [blame] | 177 | initTetheredStateMachine(TETHERING_BLUETOOTH, null); |
Christopher Wiley | 279eca3 | 2016-05-20 13:23:10 -0700 | [diff] [blame] | 178 | |
| 179 | // Telling the state machine about its upstream interface triggers a little more configuration. |
| 180 | dispatchTetherConnectionChanged(UPSTREAM_IFACE); |
| 181 | InOrder inOrder = inOrder(mNMService); |
| 182 | inOrder.verify(mNMService).enableNat(IFACE_NAME, UPSTREAM_IFACE); |
| 183 | inOrder.verify(mNMService).startInterfaceForwarding(IFACE_NAME, UPSTREAM_IFACE); |
| 184 | verifyNoMoreInteractions(mNMService, mStatsService, mTetherHelper); |
Christopher Wiley | 279eca3 | 2016-05-20 13:23:10 -0700 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | @Test |
| 188 | public void handlesChangingUpstream() throws Exception { |
Erik Kline | a954be9 | 2017-02-13 17:12:02 +0900 | [diff] [blame] | 189 | initTetheredStateMachine(TETHERING_BLUETOOTH, UPSTREAM_IFACE); |
Christopher Wiley | 279eca3 | 2016-05-20 13:23:10 -0700 | [diff] [blame] | 190 | |
| 191 | dispatchTetherConnectionChanged(UPSTREAM_IFACE2); |
| 192 | InOrder inOrder = inOrder(mNMService, mStatsService); |
| 193 | inOrder.verify(mStatsService).forceUpdate(); |
| 194 | inOrder.verify(mNMService).stopInterfaceForwarding(IFACE_NAME, UPSTREAM_IFACE); |
| 195 | inOrder.verify(mNMService).disableNat(IFACE_NAME, UPSTREAM_IFACE); |
| 196 | inOrder.verify(mNMService).enableNat(IFACE_NAME, UPSTREAM_IFACE2); |
| 197 | inOrder.verify(mNMService).startInterfaceForwarding(IFACE_NAME, UPSTREAM_IFACE2); |
| 198 | verifyNoMoreInteractions(mNMService, mStatsService, mTetherHelper); |
Christopher Wiley | 279eca3 | 2016-05-20 13:23:10 -0700 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | @Test |
Erik Kline | a954be9 | 2017-02-13 17:12:02 +0900 | [diff] [blame] | 202 | public void handlesChangingUpstreamNatFailure() throws Exception { |
| 203 | initTetheredStateMachine(TETHERING_WIFI, UPSTREAM_IFACE); |
| 204 | |
| 205 | doThrow(RemoteException.class).when(mNMService).enableNat(IFACE_NAME, UPSTREAM_IFACE2); |
| 206 | |
| 207 | dispatchTetherConnectionChanged(UPSTREAM_IFACE2); |
| 208 | InOrder inOrder = inOrder(mNMService, mStatsService); |
| 209 | inOrder.verify(mStatsService).forceUpdate(); |
| 210 | inOrder.verify(mNMService).stopInterfaceForwarding(IFACE_NAME, UPSTREAM_IFACE); |
| 211 | inOrder.verify(mNMService).disableNat(IFACE_NAME, UPSTREAM_IFACE); |
| 212 | inOrder.verify(mNMService).enableNat(IFACE_NAME, UPSTREAM_IFACE2); |
Erik Kline | 8ea4548 | 2017-02-13 17:28:53 +0900 | [diff] [blame^] | 213 | inOrder.verify(mStatsService).forceUpdate(); |
| 214 | inOrder.verify(mNMService).stopInterfaceForwarding(IFACE_NAME, UPSTREAM_IFACE2); |
| 215 | inOrder.verify(mNMService).disableNat(IFACE_NAME, UPSTREAM_IFACE2); |
Erik Kline | a954be9 | 2017-02-13 17:12:02 +0900 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | @Test |
| 219 | public void handlesChangingUpstreamInterfaceForwardingFailure() throws Exception { |
| 220 | initTetheredStateMachine(TETHERING_WIFI, UPSTREAM_IFACE); |
| 221 | |
| 222 | doThrow(RemoteException.class).when(mNMService).startInterfaceForwarding( |
| 223 | IFACE_NAME, UPSTREAM_IFACE2); |
| 224 | |
| 225 | dispatchTetherConnectionChanged(UPSTREAM_IFACE2); |
| 226 | InOrder inOrder = inOrder(mNMService, mStatsService); |
| 227 | inOrder.verify(mStatsService).forceUpdate(); |
| 228 | inOrder.verify(mNMService).stopInterfaceForwarding(IFACE_NAME, UPSTREAM_IFACE); |
| 229 | inOrder.verify(mNMService).disableNat(IFACE_NAME, UPSTREAM_IFACE); |
| 230 | inOrder.verify(mNMService).enableNat(IFACE_NAME, UPSTREAM_IFACE2); |
| 231 | inOrder.verify(mNMService).startInterfaceForwarding(IFACE_NAME, UPSTREAM_IFACE2); |
Erik Kline | 8ea4548 | 2017-02-13 17:28:53 +0900 | [diff] [blame^] | 232 | inOrder.verify(mStatsService).forceUpdate(); |
| 233 | inOrder.verify(mNMService).stopInterfaceForwarding(IFACE_NAME, UPSTREAM_IFACE2); |
| 234 | inOrder.verify(mNMService).disableNat(IFACE_NAME, UPSTREAM_IFACE2); |
Erik Kline | a954be9 | 2017-02-13 17:12:02 +0900 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | @Test |
Christopher Wiley | 279eca3 | 2016-05-20 13:23:10 -0700 | [diff] [blame] | 238 | public void canUnrequestTetheringWithUpstream() throws Exception { |
Erik Kline | a954be9 | 2017-02-13 17:12:02 +0900 | [diff] [blame] | 239 | initTetheredStateMachine(TETHERING_BLUETOOTH, UPSTREAM_IFACE); |
Christopher Wiley | 279eca3 | 2016-05-20 13:23:10 -0700 | [diff] [blame] | 240 | |
Mitchell Wills | 4622c2d | 2016-05-23 16:40:10 -0700 | [diff] [blame] | 241 | dispatchCommand(TetherInterfaceStateMachine.CMD_TETHER_UNREQUESTED); |
Christopher Wiley | 279eca3 | 2016-05-20 13:23:10 -0700 | [diff] [blame] | 242 | InOrder inOrder = inOrder(mNMService, mStatsService, mTetherHelper); |
| 243 | inOrder.verify(mStatsService).forceUpdate(); |
| 244 | inOrder.verify(mNMService).stopInterfaceForwarding(IFACE_NAME, UPSTREAM_IFACE); |
| 245 | inOrder.verify(mNMService).disableNat(IFACE_NAME, UPSTREAM_IFACE); |
| 246 | inOrder.verify(mNMService).untetherInterface(IFACE_NAME); |
Christopher Wiley | e90e0a7 | 2016-05-31 10:44:35 -0700 | [diff] [blame] | 247 | inOrder.verify(mTetherHelper).notifyInterfaceStateChange( |
| 248 | IFACE_NAME, mTestedSm, STATE_AVAILABLE, TETHER_ERROR_NO_ERROR); |
Christopher Wiley | 279eca3 | 2016-05-20 13:23:10 -0700 | [diff] [blame] | 249 | verifyNoMoreInteractions(mNMService, mStatsService, mTetherHelper); |
Christopher Wiley | 279eca3 | 2016-05-20 13:23:10 -0700 | [diff] [blame] | 250 | } |
| 251 | |
Christopher Wiley | f972edc | 2016-05-23 16:17:30 -0700 | [diff] [blame] | 252 | @Test |
| 253 | public void interfaceDownLeadsToUnavailable() throws Exception { |
| 254 | for (boolean shouldThrow : new boolean[]{true, false}) { |
Erik Kline | a954be9 | 2017-02-13 17:12:02 +0900 | [diff] [blame] | 255 | initTetheredStateMachine(TETHERING_USB, null); |
Christopher Wiley | f972edc | 2016-05-23 16:17:30 -0700 | [diff] [blame] | 256 | |
| 257 | if (shouldThrow) { |
| 258 | doThrow(RemoteException.class).when(mNMService).untetherInterface(IFACE_NAME); |
| 259 | } |
| 260 | dispatchCommand(TetherInterfaceStateMachine.CMD_INTERFACE_DOWN); |
Christopher Wiley | e90e0a7 | 2016-05-31 10:44:35 -0700 | [diff] [blame] | 261 | InOrder usbTeardownOrder = inOrder(mNMService, mInterfaceConfiguration, mTetherHelper); |
Christopher Wiley | f972edc | 2016-05-23 16:17:30 -0700 | [diff] [blame] | 262 | usbTeardownOrder.verify(mInterfaceConfiguration).setInterfaceDown(); |
| 263 | usbTeardownOrder.verify(mNMService).setInterfaceConfig( |
| 264 | IFACE_NAME, mInterfaceConfiguration); |
Christopher Wiley | e90e0a7 | 2016-05-31 10:44:35 -0700 | [diff] [blame] | 265 | usbTeardownOrder.verify(mTetherHelper).notifyInterfaceStateChange( |
| 266 | IFACE_NAME, mTestedSm, STATE_UNAVAILABLE, TETHER_ERROR_NO_ERROR); |
Christopher Wiley | f972edc | 2016-05-23 16:17:30 -0700 | [diff] [blame] | 267 | } |
| 268 | } |
| 269 | |
| 270 | @Test |
| 271 | public void usbShouldBeTornDownOnTetherError() throws Exception { |
Erik Kline | a954be9 | 2017-02-13 17:12:02 +0900 | [diff] [blame] | 272 | initStateMachine(TETHERING_USB); |
Christopher Wiley | f972edc | 2016-05-23 16:17:30 -0700 | [diff] [blame] | 273 | |
| 274 | doThrow(RemoteException.class).when(mNMService).tetherInterface(IFACE_NAME); |
| 275 | dispatchCommand(TetherInterfaceStateMachine.CMD_TETHER_REQUESTED); |
Christopher Wiley | e90e0a7 | 2016-05-31 10:44:35 -0700 | [diff] [blame] | 276 | InOrder usbTeardownOrder = inOrder(mNMService, mInterfaceConfiguration, mTetherHelper); |
Christopher Wiley | f972edc | 2016-05-23 16:17:30 -0700 | [diff] [blame] | 277 | usbTeardownOrder.verify(mInterfaceConfiguration).setInterfaceDown(); |
| 278 | usbTeardownOrder.verify(mNMService).setInterfaceConfig( |
| 279 | IFACE_NAME, mInterfaceConfiguration); |
Christopher Wiley | e90e0a7 | 2016-05-31 10:44:35 -0700 | [diff] [blame] | 280 | usbTeardownOrder.verify(mTetherHelper).notifyInterfaceStateChange( |
| 281 | IFACE_NAME, mTestedSm, STATE_AVAILABLE, TETHER_ERROR_TETHER_IFACE_ERROR); |
Christopher Wiley | f972edc | 2016-05-23 16:17:30 -0700 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | @Test |
| 285 | public void shouldTearDownUsbOnUpstreamError() throws Exception { |
Erik Kline | a954be9 | 2017-02-13 17:12:02 +0900 | [diff] [blame] | 286 | initTetheredStateMachine(TETHERING_USB, null); |
Christopher Wiley | f972edc | 2016-05-23 16:17:30 -0700 | [diff] [blame] | 287 | |
| 288 | doThrow(RemoteException.class).when(mNMService).enableNat(anyString(), anyString()); |
| 289 | dispatchTetherConnectionChanged(UPSTREAM_IFACE); |
Christopher Wiley | e90e0a7 | 2016-05-31 10:44:35 -0700 | [diff] [blame] | 290 | InOrder usbTeardownOrder = inOrder(mNMService, mInterfaceConfiguration, mTetherHelper); |
Christopher Wiley | f972edc | 2016-05-23 16:17:30 -0700 | [diff] [blame] | 291 | usbTeardownOrder.verify(mInterfaceConfiguration).setInterfaceDown(); |
| 292 | usbTeardownOrder.verify(mNMService).setInterfaceConfig(IFACE_NAME, mInterfaceConfiguration); |
Christopher Wiley | e90e0a7 | 2016-05-31 10:44:35 -0700 | [diff] [blame] | 293 | usbTeardownOrder.verify(mTetherHelper).notifyInterfaceStateChange( |
| 294 | IFACE_NAME, mTestedSm, STATE_AVAILABLE, TETHER_ERROR_ENABLE_NAT_ERROR); |
Christopher Wiley | f972edc | 2016-05-23 16:17:30 -0700 | [diff] [blame] | 295 | } |
Christopher Wiley | 279eca3 | 2016-05-20 13:23:10 -0700 | [diff] [blame] | 296 | |
| 297 | /** |
| 298 | * Send a command to the state machine under test, and run the event loop to idle. |
| 299 | * |
Mitchell Wills | 4622c2d | 2016-05-23 16:40:10 -0700 | [diff] [blame] | 300 | * @param command One of the TetherInterfaceStateMachine.CMD_* constants. |
Christopher Wiley | 279eca3 | 2016-05-20 13:23:10 -0700 | [diff] [blame] | 301 | */ |
| 302 | private void dispatchCommand(int command) { |
| 303 | mTestedSm.sendMessage(command); |
| 304 | mLooper.dispatchAll(); |
| 305 | } |
| 306 | |
| 307 | /** |
| 308 | * Special override to tell the state machine that the upstream interface has changed. |
| 309 | * |
| 310 | * @see #dispatchCommand(int) |
| 311 | * @param upstreamIface String name of upstream interface (or null) |
| 312 | */ |
| 313 | private void dispatchTetherConnectionChanged(String upstreamIface) { |
Mitchell Wills | 4622c2d | 2016-05-23 16:40:10 -0700 | [diff] [blame] | 314 | mTestedSm.sendMessage(TetherInterfaceStateMachine.CMD_TETHER_CONNECTION_CHANGED, |
| 315 | upstreamIface); |
Christopher Wiley | 279eca3 | 2016-05-20 13:23:10 -0700 | [diff] [blame] | 316 | mLooper.dispatchAll(); |
Christopher Wiley | 08725a8 | 2016-05-18 16:32:44 -0700 | [diff] [blame] | 317 | } |
Lorenzo Colitti | 5bce5a1 | 2016-10-28 17:45:55 +0900 | [diff] [blame] | 318 | } |