blob: a30b3629d5c43c35dbdae21231675c257847153c [file] [log] [blame]
Christopher Wiley1ff75bd2016-05-18 16:32:44 -07001/*
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
17package com.android.server.connectivity.tethering;
18
Christopher Wileye10bfc02016-05-23 16:17:30 -070019import static org.mockito.Matchers.anyString;
20import static org.mockito.Mockito.doThrow;
Christopher Wiley7b61d712016-05-20 13:23:10 -070021import static org.mockito.Mockito.inOrder;
22import static org.mockito.Mockito.reset;
23import static org.mockito.Mockito.verify;
Christopher Wiley1ff75bd2016-05-18 16:32:44 -070024import static org.mockito.Mockito.verifyNoMoreInteractions;
Christopher Wiley7b61d712016-05-20 13:23:10 -070025import static org.mockito.Mockito.when;
Christopher Wiley1ff75bd2016-05-18 16:32:44 -070026
Christopher Wileyd985dde2016-05-31 10:44:35 -070027import static android.net.ConnectivityManager.TETHER_ERROR_ENABLE_NAT_ERROR;
28import static android.net.ConnectivityManager.TETHER_ERROR_NO_ERROR;
29import static android.net.ConnectivityManager.TETHER_ERROR_TETHER_IFACE_ERROR;
30import static android.net.ConnectivityManager.TETHER_ERROR_UNTETHER_IFACE_ERROR;
31import static com.android.server.connectivity.tethering.IControlsTethering.STATE_AVAILABLE;
32import static com.android.server.connectivity.tethering.IControlsTethering.STATE_TETHERED;
33import static com.android.server.connectivity.tethering.IControlsTethering.STATE_UNAVAILABLE;
34
Christopher Wiley5c0b10a2016-05-31 14:43:08 -070035import android.net.ConnectivityManager;
Christopher Wiley1ff75bd2016-05-18 16:32:44 -070036import android.net.INetworkStatsService;
Christopher Wiley7b61d712016-05-20 13:23:10 -070037import android.net.InterfaceConfiguration;
Christopher Wiley1ff75bd2016-05-18 16:32:44 -070038import android.os.INetworkManagementService;
Christopher Wiley7b61d712016-05-20 13:23:10 -070039import android.os.RemoteException;
Christopher Wiley1ff75bd2016-05-18 16:32:44 -070040import android.os.test.TestLooper;
Christopher Wiley0ab0dd32016-05-25 13:57:27 -070041import android.support.test.filters.SmallTest;
42import android.support.test.runner.AndroidJUnit4;
Christopher Wiley1ff75bd2016-05-18 16:32:44 -070043
44import org.junit.Before;
45import org.junit.Test;
Christopher Wiley0ab0dd32016-05-25 13:57:27 -070046import org.junit.runner.RunWith;
Christopher Wiley7b61d712016-05-20 13:23:10 -070047import org.mockito.InOrder;
Christopher Wiley1ff75bd2016-05-18 16:32:44 -070048import org.mockito.Mock;
49import org.mockito.MockitoAnnotations;
50
Christopher Wiley0ab0dd32016-05-25 13:57:27 -070051@RunWith(AndroidJUnit4.class)
52@SmallTest
Mitchell Wills7040b4e2016-05-23 16:40:10 -070053public class TetherInterfaceStateMachineTest {
Christopher Wiley1ff75bd2016-05-18 16:32:44 -070054 private static final String IFACE_NAME = "testnet1";
Christopher Wiley7b61d712016-05-20 13:23:10 -070055 private static final String UPSTREAM_IFACE = "upstream0";
56 private static final String UPSTREAM_IFACE2 = "upstream1";
Christopher Wiley1ff75bd2016-05-18 16:32:44 -070057
58 @Mock private INetworkManagementService mNMService;
59 @Mock private INetworkStatsService mStatsService;
60 @Mock private IControlsTethering mTetherHelper;
Christopher Wiley7b61d712016-05-20 13:23:10 -070061 @Mock private InterfaceConfiguration mInterfaceConfiguration;
Christopher Wiley1ff75bd2016-05-18 16:32:44 -070062
63 private final TestLooper mLooper = new TestLooper();
Mitchell Wills7040b4e2016-05-23 16:40:10 -070064 private TetherInterfaceStateMachine mTestedSm;
Christopher Wiley1ff75bd2016-05-18 16:32:44 -070065
Christopher Wiley5c0b10a2016-05-31 14:43:08 -070066 private void initStateMachine(int interfaceType) throws Exception {
67 mTestedSm = new TetherInterfaceStateMachine(IFACE_NAME, mLooper.getLooper(), interfaceType,
Christopher Wiley7b61d712016-05-20 13:23:10 -070068 mNMService, mStatsService, mTetherHelper);
69 mTestedSm.start();
70 // Starting the state machine always puts us in a consistent state and notifies
71 // the test of the world that we've changed from an unknown to available state.
72 mLooper.dispatchAll();
73 reset(mNMService, mStatsService, mTetherHelper);
Christopher Wileye10bfc02016-05-23 16:17:30 -070074 when(mNMService.getInterfaceConfig(IFACE_NAME)).thenReturn(mInterfaceConfiguration);
Christopher Wiley7b61d712016-05-20 13:23:10 -070075 }
76
Christopher Wiley5c0b10a2016-05-31 14:43:08 -070077 private void initTetheredStateMachine(int interfaceType, String upstreamIface) throws Exception {
78 initStateMachine(interfaceType);
Mitchell Wills7040b4e2016-05-23 16:40:10 -070079 dispatchCommand(TetherInterfaceStateMachine.CMD_TETHER_REQUESTED);
Christopher Wiley7b61d712016-05-20 13:23:10 -070080 if (upstreamIface != null) {
81 dispatchTetherConnectionChanged(upstreamIface);
82 }
83 reset(mNMService, mStatsService, mTetherHelper);
Christopher Wileye10bfc02016-05-23 16:17:30 -070084 when(mNMService.getInterfaceConfig(IFACE_NAME)).thenReturn(mInterfaceConfiguration);
Christopher Wiley7b61d712016-05-20 13:23:10 -070085 }
86
Christopher Wileyd985dde2016-05-31 10:44:35 -070087 @Before public void setUp() throws Exception {
Christopher Wiley1ff75bd2016-05-18 16:32:44 -070088 MockitoAnnotations.initMocks(this);
Christopher Wiley7b61d712016-05-20 13:23:10 -070089 }
90
91 @Test
92 public void startsOutAvailable() {
Christopher Wiley5c0b10a2016-05-31 14:43:08 -070093 mTestedSm = new TetherInterfaceStateMachine(IFACE_NAME, mLooper.getLooper(),
94 ConnectivityManager.TETHERING_BLUETOOTH, mNMService, mStatsService, mTetherHelper);
Christopher Wiley1ff75bd2016-05-18 16:32:44 -070095 mTestedSm.start();
Christopher Wiley7b61d712016-05-20 13:23:10 -070096 mLooper.dispatchAll();
Christopher Wileyd985dde2016-05-31 10:44:35 -070097 verify(mTetherHelper).notifyInterfaceStateChange(
98 IFACE_NAME, mTestedSm, STATE_AVAILABLE, TETHER_ERROR_NO_ERROR);
Christopher Wiley7b61d712016-05-20 13:23:10 -070099 verifyNoMoreInteractions(mTetherHelper, mNMService, mStatsService);
Christopher Wiley1ff75bd2016-05-18 16:32:44 -0700100 }
101
102 @Test
Christopher Wileye10bfc02016-05-23 16:17:30 -0700103 public void shouldDoNothingUntilRequested() throws Exception {
Christopher Wiley5c0b10a2016-05-31 14:43:08 -0700104 initStateMachine(ConnectivityManager.TETHERING_BLUETOOTH);
Christopher Wiley1ff75bd2016-05-18 16:32:44 -0700105 final int [] NOOP_COMMANDS = {
Mitchell Wills7040b4e2016-05-23 16:40:10 -0700106 TetherInterfaceStateMachine.CMD_TETHER_UNREQUESTED,
107 TetherInterfaceStateMachine.CMD_IP_FORWARDING_ENABLE_ERROR,
108 TetherInterfaceStateMachine.CMD_IP_FORWARDING_DISABLE_ERROR,
109 TetherInterfaceStateMachine.CMD_START_TETHERING_ERROR,
110 TetherInterfaceStateMachine.CMD_STOP_TETHERING_ERROR,
111 TetherInterfaceStateMachine.CMD_SET_DNS_FORWARDERS_ERROR,
112 TetherInterfaceStateMachine.CMD_TETHER_CONNECTION_CHANGED
Christopher Wiley1ff75bd2016-05-18 16:32:44 -0700113 };
114 for (int command : NOOP_COMMANDS) {
Christopher Wiley7b61d712016-05-20 13:23:10 -0700115 // None of these commands should trigger us to request action from
Christopher Wiley1ff75bd2016-05-18 16:32:44 -0700116 // the rest of the system.
Christopher Wiley7b61d712016-05-20 13:23:10 -0700117 dispatchCommand(command);
118 verifyNoMoreInteractions(mNMService, mStatsService, mTetherHelper);
Christopher Wiley1ff75bd2016-05-18 16:32:44 -0700119 }
120 }
121
Christopher Wiley7b61d712016-05-20 13:23:10 -0700122 @Test
Christopher Wileye10bfc02016-05-23 16:17:30 -0700123 public void handlesImmediateInterfaceDown() throws Exception {
Christopher Wiley5c0b10a2016-05-31 14:43:08 -0700124 initStateMachine(ConnectivityManager.TETHERING_BLUETOOTH);
Christopher Wileyd985dde2016-05-31 10:44:35 -0700125
Mitchell Wills7040b4e2016-05-23 16:40:10 -0700126 dispatchCommand(TetherInterfaceStateMachine.CMD_INTERFACE_DOWN);
Christopher Wileyd985dde2016-05-31 10:44:35 -0700127 verify(mTetherHelper).notifyInterfaceStateChange(
128 IFACE_NAME, mTestedSm, STATE_UNAVAILABLE, TETHER_ERROR_NO_ERROR);
Christopher Wiley7b61d712016-05-20 13:23:10 -0700129 verifyNoMoreInteractions(mNMService, mStatsService, mTetherHelper);
Christopher Wiley7b61d712016-05-20 13:23:10 -0700130 }
131
132 @Test
Christopher Wileye10bfc02016-05-23 16:17:30 -0700133 public void canBeTethered() throws Exception {
Christopher Wiley5c0b10a2016-05-31 14:43:08 -0700134 initStateMachine(ConnectivityManager.TETHERING_BLUETOOTH);
Christopher Wileyd985dde2016-05-31 10:44:35 -0700135
Mitchell Wills7040b4e2016-05-23 16:40:10 -0700136 dispatchCommand(TetherInterfaceStateMachine.CMD_TETHER_REQUESTED);
Christopher Wiley7b61d712016-05-20 13:23:10 -0700137 InOrder inOrder = inOrder(mTetherHelper, mNMService);
Christopher Wiley7b61d712016-05-20 13:23:10 -0700138 inOrder.verify(mNMService).tetherInterface(IFACE_NAME);
Christopher Wileyd985dde2016-05-31 10:44:35 -0700139 inOrder.verify(mTetherHelper).notifyInterfaceStateChange(
140 IFACE_NAME, mTestedSm, STATE_TETHERED, TETHER_ERROR_NO_ERROR);
Christopher Wiley7b61d712016-05-20 13:23:10 -0700141 verifyNoMoreInteractions(mNMService, mStatsService, mTetherHelper);
Christopher Wiley7b61d712016-05-20 13:23:10 -0700142 }
143
144 @Test
145 public void canUnrequestTethering() throws Exception {
Christopher Wiley5c0b10a2016-05-31 14:43:08 -0700146 initTetheredStateMachine(ConnectivityManager.TETHERING_BLUETOOTH, null);
Christopher Wiley7b61d712016-05-20 13:23:10 -0700147
Mitchell Wills7040b4e2016-05-23 16:40:10 -0700148 dispatchCommand(TetherInterfaceStateMachine.CMD_TETHER_UNREQUESTED);
Christopher Wiley7b61d712016-05-20 13:23:10 -0700149 InOrder inOrder = inOrder(mNMService, mStatsService, mTetherHelper);
Christopher Wileye10bfc02016-05-23 16:17:30 -0700150 inOrder.verify(mNMService).untetherInterface(IFACE_NAME);
Christopher Wileyd985dde2016-05-31 10:44:35 -0700151 inOrder.verify(mTetherHelper).notifyInterfaceStateChange(
152 IFACE_NAME, mTestedSm, STATE_AVAILABLE, TETHER_ERROR_NO_ERROR);
Christopher Wiley7b61d712016-05-20 13:23:10 -0700153 verifyNoMoreInteractions(mNMService, mStatsService, mTetherHelper);
Christopher Wiley7b61d712016-05-20 13:23:10 -0700154 }
155
156 @Test
Christopher Wileye10bfc02016-05-23 16:17:30 -0700157 public void canBeTetheredAsUsb() throws Exception {
Christopher Wiley5c0b10a2016-05-31 14:43:08 -0700158 initStateMachine(ConnectivityManager.TETHERING_USB);
Christopher Wiley7b61d712016-05-20 13:23:10 -0700159
Mitchell Wills7040b4e2016-05-23 16:40:10 -0700160 dispatchCommand(TetherInterfaceStateMachine.CMD_TETHER_REQUESTED);
Christopher Wiley7b61d712016-05-20 13:23:10 -0700161 InOrder inOrder = inOrder(mTetherHelper, mNMService);
Christopher Wiley7b61d712016-05-20 13:23:10 -0700162 inOrder.verify(mNMService).getInterfaceConfig(IFACE_NAME);
163 inOrder.verify(mNMService).setInterfaceConfig(IFACE_NAME, mInterfaceConfiguration);
Christopher Wiley7b61d712016-05-20 13:23:10 -0700164 inOrder.verify(mNMService).tetherInterface(IFACE_NAME);
Christopher Wileyd985dde2016-05-31 10:44:35 -0700165 inOrder.verify(mTetherHelper).notifyInterfaceStateChange(
166 IFACE_NAME, mTestedSm, STATE_TETHERED, TETHER_ERROR_NO_ERROR);
Christopher Wiley7b61d712016-05-20 13:23:10 -0700167 verifyNoMoreInteractions(mNMService, mStatsService, mTetherHelper);
Christopher Wiley7b61d712016-05-20 13:23:10 -0700168 }
169
170 @Test
171 public void handlesFirstUpstreamChange() throws Exception {
Christopher Wiley5c0b10a2016-05-31 14:43:08 -0700172 initTetheredStateMachine(ConnectivityManager.TETHERING_BLUETOOTH, null);
Christopher Wiley7b61d712016-05-20 13:23:10 -0700173
174 // Telling the state machine about its upstream interface triggers a little more configuration.
175 dispatchTetherConnectionChanged(UPSTREAM_IFACE);
176 InOrder inOrder = inOrder(mNMService);
177 inOrder.verify(mNMService).enableNat(IFACE_NAME, UPSTREAM_IFACE);
178 inOrder.verify(mNMService).startInterfaceForwarding(IFACE_NAME, UPSTREAM_IFACE);
179 verifyNoMoreInteractions(mNMService, mStatsService, mTetherHelper);
Christopher Wiley7b61d712016-05-20 13:23:10 -0700180 }
181
182 @Test
183 public void handlesChangingUpstream() throws Exception {
Christopher Wiley5c0b10a2016-05-31 14:43:08 -0700184 initTetheredStateMachine(ConnectivityManager.TETHERING_BLUETOOTH, UPSTREAM_IFACE);
Christopher Wiley7b61d712016-05-20 13:23:10 -0700185
186 dispatchTetherConnectionChanged(UPSTREAM_IFACE2);
187 InOrder inOrder = inOrder(mNMService, mStatsService);
188 inOrder.verify(mStatsService).forceUpdate();
189 inOrder.verify(mNMService).stopInterfaceForwarding(IFACE_NAME, UPSTREAM_IFACE);
190 inOrder.verify(mNMService).disableNat(IFACE_NAME, UPSTREAM_IFACE);
191 inOrder.verify(mNMService).enableNat(IFACE_NAME, UPSTREAM_IFACE2);
192 inOrder.verify(mNMService).startInterfaceForwarding(IFACE_NAME, UPSTREAM_IFACE2);
193 verifyNoMoreInteractions(mNMService, mStatsService, mTetherHelper);
Christopher Wiley7b61d712016-05-20 13:23:10 -0700194 }
195
196 @Test
197 public void canUnrequestTetheringWithUpstream() throws Exception {
Christopher Wiley5c0b10a2016-05-31 14:43:08 -0700198 initTetheredStateMachine(ConnectivityManager.TETHERING_BLUETOOTH, UPSTREAM_IFACE);
Christopher Wiley7b61d712016-05-20 13:23:10 -0700199
Mitchell Wills7040b4e2016-05-23 16:40:10 -0700200 dispatchCommand(TetherInterfaceStateMachine.CMD_TETHER_UNREQUESTED);
Christopher Wiley7b61d712016-05-20 13:23:10 -0700201 InOrder inOrder = inOrder(mNMService, mStatsService, mTetherHelper);
202 inOrder.verify(mStatsService).forceUpdate();
203 inOrder.verify(mNMService).stopInterfaceForwarding(IFACE_NAME, UPSTREAM_IFACE);
204 inOrder.verify(mNMService).disableNat(IFACE_NAME, UPSTREAM_IFACE);
205 inOrder.verify(mNMService).untetherInterface(IFACE_NAME);
Christopher Wileyd985dde2016-05-31 10:44:35 -0700206 inOrder.verify(mTetherHelper).notifyInterfaceStateChange(
207 IFACE_NAME, mTestedSm, STATE_AVAILABLE, TETHER_ERROR_NO_ERROR);
Christopher Wiley7b61d712016-05-20 13:23:10 -0700208 verifyNoMoreInteractions(mNMService, mStatsService, mTetherHelper);
Christopher Wiley7b61d712016-05-20 13:23:10 -0700209 }
210
Christopher Wileye10bfc02016-05-23 16:17:30 -0700211 @Test
212 public void interfaceDownLeadsToUnavailable() throws Exception {
213 for (boolean shouldThrow : new boolean[]{true, false}) {
Christopher Wiley5c0b10a2016-05-31 14:43:08 -0700214 initTetheredStateMachine(ConnectivityManager.TETHERING_USB, null);
Christopher Wileye10bfc02016-05-23 16:17:30 -0700215
216 if (shouldThrow) {
217 doThrow(RemoteException.class).when(mNMService).untetherInterface(IFACE_NAME);
218 }
219 dispatchCommand(TetherInterfaceStateMachine.CMD_INTERFACE_DOWN);
Christopher Wileyd985dde2016-05-31 10:44:35 -0700220 InOrder usbTeardownOrder = inOrder(mNMService, mInterfaceConfiguration, mTetherHelper);
Christopher Wileye10bfc02016-05-23 16:17:30 -0700221 usbTeardownOrder.verify(mInterfaceConfiguration).setInterfaceDown();
222 usbTeardownOrder.verify(mNMService).setInterfaceConfig(
223 IFACE_NAME, mInterfaceConfiguration);
Christopher Wileyd985dde2016-05-31 10:44:35 -0700224 usbTeardownOrder.verify(mTetherHelper).notifyInterfaceStateChange(
225 IFACE_NAME, mTestedSm, STATE_UNAVAILABLE, TETHER_ERROR_NO_ERROR);
Christopher Wileye10bfc02016-05-23 16:17:30 -0700226 }
227 }
228
229 @Test
230 public void usbShouldBeTornDownOnTetherError() throws Exception {
Christopher Wiley5c0b10a2016-05-31 14:43:08 -0700231 initStateMachine(ConnectivityManager.TETHERING_USB);
Christopher Wileye10bfc02016-05-23 16:17:30 -0700232
233 doThrow(RemoteException.class).when(mNMService).tetherInterface(IFACE_NAME);
234 dispatchCommand(TetherInterfaceStateMachine.CMD_TETHER_REQUESTED);
Christopher Wileyd985dde2016-05-31 10:44:35 -0700235 InOrder usbTeardownOrder = inOrder(mNMService, mInterfaceConfiguration, mTetherHelper);
Christopher Wileye10bfc02016-05-23 16:17:30 -0700236 usbTeardownOrder.verify(mInterfaceConfiguration).setInterfaceDown();
237 usbTeardownOrder.verify(mNMService).setInterfaceConfig(
238 IFACE_NAME, mInterfaceConfiguration);
Christopher Wileyd985dde2016-05-31 10:44:35 -0700239 usbTeardownOrder.verify(mTetherHelper).notifyInterfaceStateChange(
240 IFACE_NAME, mTestedSm, STATE_AVAILABLE, TETHER_ERROR_TETHER_IFACE_ERROR);
Christopher Wileye10bfc02016-05-23 16:17:30 -0700241 }
242
243 @Test
244 public void shouldTearDownUsbOnUpstreamError() throws Exception {
Christopher Wiley5c0b10a2016-05-31 14:43:08 -0700245 initTetheredStateMachine(ConnectivityManager.TETHERING_USB, null);
Christopher Wileye10bfc02016-05-23 16:17:30 -0700246
247 doThrow(RemoteException.class).when(mNMService).enableNat(anyString(), anyString());
248 dispatchTetherConnectionChanged(UPSTREAM_IFACE);
Christopher Wileyd985dde2016-05-31 10:44:35 -0700249 InOrder usbTeardownOrder = inOrder(mNMService, mInterfaceConfiguration, mTetherHelper);
Christopher Wileye10bfc02016-05-23 16:17:30 -0700250 usbTeardownOrder.verify(mInterfaceConfiguration).setInterfaceDown();
251 usbTeardownOrder.verify(mNMService).setInterfaceConfig(IFACE_NAME, mInterfaceConfiguration);
Christopher Wileyd985dde2016-05-31 10:44:35 -0700252 usbTeardownOrder.verify(mTetherHelper).notifyInterfaceStateChange(
253 IFACE_NAME, mTestedSm, STATE_AVAILABLE, TETHER_ERROR_ENABLE_NAT_ERROR);
Christopher Wileye10bfc02016-05-23 16:17:30 -0700254 }
Christopher Wiley7b61d712016-05-20 13:23:10 -0700255
256 /**
257 * Send a command to the state machine under test, and run the event loop to idle.
258 *
Mitchell Wills7040b4e2016-05-23 16:40:10 -0700259 * @param command One of the TetherInterfaceStateMachine.CMD_* constants.
Christopher Wiley7b61d712016-05-20 13:23:10 -0700260 */
261 private void dispatchCommand(int command) {
262 mTestedSm.sendMessage(command);
263 mLooper.dispatchAll();
264 }
265
266 /**
267 * Special override to tell the state machine that the upstream interface has changed.
268 *
269 * @see #dispatchCommand(int)
270 * @param upstreamIface String name of upstream interface (or null)
271 */
272 private void dispatchTetherConnectionChanged(String upstreamIface) {
Mitchell Wills7040b4e2016-05-23 16:40:10 -0700273 mTestedSm.sendMessage(TetherInterfaceStateMachine.CMD_TETHER_CONNECTION_CHANGED,
274 upstreamIface);
Christopher Wiley7b61d712016-05-20 13:23:10 -0700275 mLooper.dispatchAll();
Christopher Wiley1ff75bd2016-05-18 16:32:44 -0700276 }
Christopher Wiley0ab0dd32016-05-25 13:57:27 -0700277}