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