Hugo Benichi | ef50288 | 2017-09-01 01:23:32 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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; |
| 18 | |
| 19 | import static org.junit.Assert.assertFalse; |
Hugo Benichi | ef50288 | 2017-09-01 01:23:32 +0000 | [diff] [blame] | 20 | import static org.junit.Assert.assertTrue; |
Hugo Benichi | ef50288 | 2017-09-01 01:23:32 +0000 | [diff] [blame] | 21 | import static org.mockito.Mockito.eq; |
| 22 | import static org.mockito.Mockito.times; |
| 23 | import static org.mockito.Mockito.verify; |
| 24 | import static org.mockito.Mockito.verifyNoMoreInteractions; |
| 25 | import static org.mockito.Mockito.when; |
| 26 | |
| 27 | import android.net.ConnectivityManager; |
Lorenzo Colitti | 9307ca2 | 2019-01-12 01:54:23 +0900 | [diff] [blame] | 28 | import android.net.INetd; |
Hugo Benichi | ef50288 | 2017-09-01 01:23:32 +0000 | [diff] [blame] | 29 | import android.net.InterfaceConfiguration; |
| 30 | import android.net.LinkAddress; |
| 31 | import android.net.LinkProperties; |
| 32 | import android.net.NetworkInfo; |
soma, kawata | 88b8f63 | 2018-10-23 21:10:02 +0900 | [diff] [blame] | 33 | import android.net.NetworkMisc; |
Hugo Benichi | ef50288 | 2017-09-01 01:23:32 +0000 | [diff] [blame] | 34 | import android.os.Handler; |
| 35 | import android.os.INetworkManagementService; |
| 36 | import android.os.test.TestLooper; |
| 37 | import android.support.test.filters.SmallTest; |
| 38 | import android.support.test.runner.AndroidJUnit4; |
| 39 | |
| 40 | import com.android.server.ConnectivityService; |
| 41 | |
| 42 | import org.junit.Before; |
| 43 | import org.junit.Test; |
| 44 | import org.junit.runner.RunWith; |
| 45 | import org.mockito.ArgumentCaptor; |
| 46 | import org.mockito.Mock; |
| 47 | import org.mockito.MockitoAnnotations; |
| 48 | |
| 49 | @RunWith(AndroidJUnit4.class) |
| 50 | @SmallTest |
| 51 | public class Nat464XlatTest { |
| 52 | |
| 53 | static final String BASE_IFACE = "test0"; |
| 54 | static final String STACKED_IFACE = "v4-test0"; |
| 55 | static final LinkAddress ADDR = new LinkAddress("192.0.2.5/29"); |
| 56 | |
| 57 | @Mock ConnectivityService mConnectivity; |
soma, kawata | 88b8f63 | 2018-10-23 21:10:02 +0900 | [diff] [blame] | 58 | @Mock NetworkMisc mMisc; |
Lorenzo Colitti | 9307ca2 | 2019-01-12 01:54:23 +0900 | [diff] [blame] | 59 | @Mock INetd mNetd; |
Hugo Benichi | ef50288 | 2017-09-01 01:23:32 +0000 | [diff] [blame] | 60 | @Mock INetworkManagementService mNms; |
| 61 | @Mock InterfaceConfiguration mConfig; |
| 62 | @Mock NetworkAgentInfo mNai; |
| 63 | |
| 64 | TestLooper mLooper; |
| 65 | Handler mHandler; |
| 66 | |
| 67 | Nat464Xlat makeNat464Xlat() { |
Lorenzo Colitti | 9307ca2 | 2019-01-12 01:54:23 +0900 | [diff] [blame] | 68 | return new Nat464Xlat(mNai, mNetd, mNms); |
Hugo Benichi | ef50288 | 2017-09-01 01:23:32 +0000 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | @Before |
| 72 | public void setUp() throws Exception { |
| 73 | mLooper = new TestLooper(); |
| 74 | mHandler = new Handler(mLooper.getLooper()); |
| 75 | |
| 76 | MockitoAnnotations.initMocks(this); |
| 77 | |
| 78 | mNai.linkProperties = new LinkProperties(); |
| 79 | mNai.linkProperties.setInterfaceName(BASE_IFACE); |
| 80 | mNai.networkInfo = new NetworkInfo(null); |
| 81 | mNai.networkInfo.setType(ConnectivityManager.TYPE_WIFI); |
| 82 | when(mNai.connService()).thenReturn(mConnectivity); |
soma, kawata | 88b8f63 | 2018-10-23 21:10:02 +0900 | [diff] [blame] | 83 | when(mNai.netMisc()).thenReturn(mMisc); |
Hugo Benichi | ef50288 | 2017-09-01 01:23:32 +0000 | [diff] [blame] | 84 | when(mNai.handler()).thenReturn(mHandler); |
| 85 | |
| 86 | when(mNms.getInterfaceConfig(eq(STACKED_IFACE))).thenReturn(mConfig); |
| 87 | when(mConfig.getLinkAddress()).thenReturn(ADDR); |
| 88 | } |
| 89 | |
| 90 | @Test |
Erik Kline | 3c18216 | 2017-09-21 17:28:10 +0900 | [diff] [blame] | 91 | public void testRequiresClat() throws Exception { |
| 92 | final int[] supportedTypes = { |
| 93 | ConnectivityManager.TYPE_MOBILE, |
| 94 | ConnectivityManager.TYPE_WIFI, |
| 95 | ConnectivityManager.TYPE_ETHERNET, |
| 96 | }; |
| 97 | |
| 98 | // NetworkInfo doesn't allow setting the State directly, but rather |
| 99 | // requires setting DetailedState in order set State as a side-effect. |
| 100 | final NetworkInfo.DetailedState[] supportedDetailedStates = { |
| 101 | NetworkInfo.DetailedState.CONNECTED, |
| 102 | NetworkInfo.DetailedState.SUSPENDED, |
| 103 | }; |
| 104 | |
| 105 | for (int type : supportedTypes) { |
| 106 | mNai.networkInfo.setType(type); |
| 107 | for (NetworkInfo.DetailedState state : supportedDetailedStates) { |
| 108 | mNai.networkInfo.setDetailedState(state, "reason", "extraInfo"); |
soma, kawata | 88b8f63 | 2018-10-23 21:10:02 +0900 | [diff] [blame] | 109 | String msg = String.format("requiresClat expected for type=%d state=%s", |
| 110 | type, state); |
| 111 | |
| 112 | mMisc.skip464xlat = true; |
| 113 | String errorMsg = msg + String.format(" skip464xlat=%b", mMisc.skip464xlat); |
| 114 | assertFalse(errorMsg, Nat464Xlat.requiresClat(mNai)); |
| 115 | |
| 116 | mMisc.skip464xlat = false; |
| 117 | errorMsg = msg + String.format(" skip464xlat=%b", mMisc.skip464xlat); |
| 118 | assertTrue(errorMsg, Nat464Xlat.requiresClat(mNai)); |
Erik Kline | 3c18216 | 2017-09-21 17:28:10 +0900 | [diff] [blame] | 119 | } |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | @Test |
Hugo Benichi | ef50288 | 2017-09-01 01:23:32 +0000 | [diff] [blame] | 124 | public void testNormalStartAndStop() throws Exception { |
| 125 | Nat464Xlat nat = makeNat464Xlat(); |
| 126 | ArgumentCaptor<LinkProperties> c = ArgumentCaptor.forClass(LinkProperties.class); |
| 127 | |
| 128 | // ConnectivityService starts clat. |
| 129 | nat.start(); |
| 130 | |
| 131 | verify(mNms).registerObserver(eq(nat)); |
Lorenzo Colitti | 9307ca2 | 2019-01-12 01:54:23 +0900 | [diff] [blame] | 132 | verify(mNetd).clatdStart(eq(BASE_IFACE)); |
Hugo Benichi | ef50288 | 2017-09-01 01:23:32 +0000 | [diff] [blame] | 133 | |
| 134 | // Stacked interface up notification arrives. |
| 135 | nat.interfaceLinkStateChanged(STACKED_IFACE, true); |
| 136 | mLooper.dispatchNext(); |
| 137 | |
| 138 | verify(mNms).getInterfaceConfig(eq(STACKED_IFACE)); |
Hugo Benichi | ef50288 | 2017-09-01 01:23:32 +0000 | [diff] [blame] | 139 | verify(mConnectivity).handleUpdateLinkProperties(eq(mNai), c.capture()); |
| 140 | assertFalse(c.getValue().getStackedLinks().isEmpty()); |
| 141 | assertTrue(c.getValue().getAllInterfaceNames().contains(STACKED_IFACE)); |
| 142 | assertRunning(nat); |
| 143 | |
| 144 | // ConnectivityService stops clat (Network disconnects, IPv4 addr appears, ...). |
| 145 | nat.stop(); |
| 146 | |
Lorenzo Colitti | 9307ca2 | 2019-01-12 01:54:23 +0900 | [diff] [blame] | 147 | verify(mNetd).clatdStop(eq(BASE_IFACE)); |
Hugo Benichi | ef50288 | 2017-09-01 01:23:32 +0000 | [diff] [blame] | 148 | |
| 149 | // Stacked interface removed notification arrives. |
| 150 | nat.interfaceRemoved(STACKED_IFACE); |
| 151 | mLooper.dispatchNext(); |
| 152 | |
| 153 | verify(mNms).unregisterObserver(eq(nat)); |
| 154 | verify(mConnectivity, times(2)).handleUpdateLinkProperties(eq(mNai), c.capture()); |
| 155 | assertTrue(c.getValue().getStackedLinks().isEmpty()); |
| 156 | assertFalse(c.getValue().getAllInterfaceNames().contains(STACKED_IFACE)); |
| 157 | assertIdle(nat); |
| 158 | |
Lorenzo Colitti | 9307ca2 | 2019-01-12 01:54:23 +0900 | [diff] [blame] | 159 | verifyNoMoreInteractions(mNetd, mNms, mConnectivity); |
Hugo Benichi | ef50288 | 2017-09-01 01:23:32 +0000 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | @Test |
| 163 | public void testClatdCrashWhileRunning() throws Exception { |
| 164 | Nat464Xlat nat = makeNat464Xlat(); |
| 165 | ArgumentCaptor<LinkProperties> c = ArgumentCaptor.forClass(LinkProperties.class); |
| 166 | |
| 167 | // ConnectivityService starts clat. |
| 168 | nat.start(); |
| 169 | |
| 170 | verify(mNms).registerObserver(eq(nat)); |
Lorenzo Colitti | 9307ca2 | 2019-01-12 01:54:23 +0900 | [diff] [blame] | 171 | verify(mNetd).clatdStart(eq(BASE_IFACE)); |
Hugo Benichi | ef50288 | 2017-09-01 01:23:32 +0000 | [diff] [blame] | 172 | |
| 173 | // Stacked interface up notification arrives. |
| 174 | nat.interfaceLinkStateChanged(STACKED_IFACE, true); |
| 175 | mLooper.dispatchNext(); |
| 176 | |
| 177 | verify(mNms).getInterfaceConfig(eq(STACKED_IFACE)); |
Hugo Benichi | ef50288 | 2017-09-01 01:23:32 +0000 | [diff] [blame] | 178 | verify(mConnectivity, times(1)).handleUpdateLinkProperties(eq(mNai), c.capture()); |
| 179 | assertFalse(c.getValue().getStackedLinks().isEmpty()); |
| 180 | assertTrue(c.getValue().getAllInterfaceNames().contains(STACKED_IFACE)); |
| 181 | assertRunning(nat); |
| 182 | |
| 183 | // Stacked interface removed notification arrives (clatd crashed, ...). |
| 184 | nat.interfaceRemoved(STACKED_IFACE); |
| 185 | mLooper.dispatchNext(); |
| 186 | |
| 187 | verify(mNms).unregisterObserver(eq(nat)); |
Lorenzo Colitti | 9307ca2 | 2019-01-12 01:54:23 +0900 | [diff] [blame] | 188 | verify(mNetd).clatdStop(eq(BASE_IFACE)); |
Hugo Benichi | ef50288 | 2017-09-01 01:23:32 +0000 | [diff] [blame] | 189 | verify(mConnectivity, times(2)).handleUpdateLinkProperties(eq(mNai), c.capture()); |
| 190 | assertTrue(c.getValue().getStackedLinks().isEmpty()); |
| 191 | assertFalse(c.getValue().getAllInterfaceNames().contains(STACKED_IFACE)); |
| 192 | assertIdle(nat); |
| 193 | |
| 194 | // ConnectivityService stops clat: no-op. |
| 195 | nat.stop(); |
| 196 | |
Lorenzo Colitti | 9307ca2 | 2019-01-12 01:54:23 +0900 | [diff] [blame] | 197 | verifyNoMoreInteractions(mNetd, mNms, mConnectivity); |
Hugo Benichi | ef50288 | 2017-09-01 01:23:32 +0000 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | @Test |
| 201 | public void testStopBeforeClatdStarts() throws Exception { |
| 202 | Nat464Xlat nat = makeNat464Xlat(); |
| 203 | |
| 204 | // ConnectivityService starts clat. |
| 205 | nat.start(); |
| 206 | |
| 207 | verify(mNms).registerObserver(eq(nat)); |
Lorenzo Colitti | 9307ca2 | 2019-01-12 01:54:23 +0900 | [diff] [blame] | 208 | verify(mNetd).clatdStart(eq(BASE_IFACE)); |
Hugo Benichi | ef50288 | 2017-09-01 01:23:32 +0000 | [diff] [blame] | 209 | |
| 210 | // ConnectivityService immediately stops clat (Network disconnects, IPv4 addr appears, ...) |
| 211 | nat.stop(); |
| 212 | |
| 213 | verify(mNms).unregisterObserver(eq(nat)); |
Lorenzo Colitti | 9307ca2 | 2019-01-12 01:54:23 +0900 | [diff] [blame] | 214 | verify(mNetd).clatdStop(eq(BASE_IFACE)); |
Hugo Benichi | ef50288 | 2017-09-01 01:23:32 +0000 | [diff] [blame] | 215 | assertIdle(nat); |
| 216 | |
| 217 | // In-flight interface up notification arrives: no-op |
| 218 | nat.interfaceLinkStateChanged(STACKED_IFACE, true); |
| 219 | mLooper.dispatchNext(); |
| 220 | |
| 221 | |
| 222 | // Interface removed notification arrives after stopClatd() takes effect: no-op. |
| 223 | nat.interfaceRemoved(STACKED_IFACE); |
| 224 | mLooper.dispatchNext(); |
| 225 | |
| 226 | assertIdle(nat); |
| 227 | |
Lorenzo Colitti | 9307ca2 | 2019-01-12 01:54:23 +0900 | [diff] [blame] | 228 | verifyNoMoreInteractions(mNetd, mNms, mConnectivity); |
Hugo Benichi | ef50288 | 2017-09-01 01:23:32 +0000 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | @Test |
| 232 | public void testStopAndClatdNeverStarts() throws Exception { |
| 233 | Nat464Xlat nat = makeNat464Xlat(); |
| 234 | |
| 235 | // ConnectivityService starts clat. |
| 236 | nat.start(); |
| 237 | |
| 238 | verify(mNms).registerObserver(eq(nat)); |
Lorenzo Colitti | 9307ca2 | 2019-01-12 01:54:23 +0900 | [diff] [blame] | 239 | verify(mNetd).clatdStart(eq(BASE_IFACE)); |
Hugo Benichi | ef50288 | 2017-09-01 01:23:32 +0000 | [diff] [blame] | 240 | |
| 241 | // ConnectivityService immediately stops clat (Network disconnects, IPv4 addr appears, ...) |
| 242 | nat.stop(); |
| 243 | |
| 244 | verify(mNms).unregisterObserver(eq(nat)); |
Lorenzo Colitti | 9307ca2 | 2019-01-12 01:54:23 +0900 | [diff] [blame] | 245 | verify(mNetd).clatdStop(eq(BASE_IFACE)); |
Hugo Benichi | ef50288 | 2017-09-01 01:23:32 +0000 | [diff] [blame] | 246 | assertIdle(nat); |
| 247 | |
Lorenzo Colitti | 9307ca2 | 2019-01-12 01:54:23 +0900 | [diff] [blame] | 248 | verifyNoMoreInteractions(mNetd, mNms, mConnectivity); |
Hugo Benichi | ef50288 | 2017-09-01 01:23:32 +0000 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | static void assertIdle(Nat464Xlat nat) { |
| 252 | assertTrue("Nat464Xlat was not IDLE", !nat.isStarted()); |
| 253 | } |
| 254 | |
| 255 | static void assertRunning(Nat464Xlat nat) { |
| 256 | assertTrue("Nat464Xlat was not RUNNING", nat.isRunning()); |
| 257 | } |
| 258 | } |