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