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