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