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