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