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 | |
Lorenzo Colitti | 6998fa8 | 2019-01-08 10:04:25 +0900 | [diff] [blame] | 19 | import static org.junit.Assert.assertEquals; |
Hugo Benichi | ef50288 | 2017-09-01 01:23:32 +0000 | [diff] [blame] | 20 | import static org.junit.Assert.assertFalse; |
Hugo Benichi | ef50288 | 2017-09-01 01:23:32 +0000 | [diff] [blame] | 21 | import static org.junit.Assert.assertTrue; |
Hugo Benichi | ef50288 | 2017-09-01 01:23:32 +0000 | [diff] [blame] | 22 | import static org.mockito.Mockito.eq; |
Lorenzo Colitti | df59563 | 2019-01-08 14:43:37 +0900 | [diff] [blame] | 23 | import static org.mockito.Mockito.inOrder; |
Hugo Benichi | ef50288 | 2017-09-01 01:23:32 +0000 | [diff] [blame] | 24 | import static org.mockito.Mockito.times; |
| 25 | import static org.mockito.Mockito.verify; |
| 26 | import static org.mockito.Mockito.verifyNoMoreInteractions; |
| 27 | import static org.mockito.Mockito.when; |
| 28 | |
| 29 | import android.net.ConnectivityManager; |
Lorenzo Colitti | 9307ca2 | 2019-01-12 01:54:23 +0900 | [diff] [blame] | 30 | import android.net.INetd; |
Hugo Benichi | ef50288 | 2017-09-01 01:23:32 +0000 | [diff] [blame] | 31 | import android.net.InterfaceConfiguration; |
Lorenzo Colitti | 6998fa8 | 2019-01-08 10:04:25 +0900 | [diff] [blame] | 32 | import android.net.IpPrefix; |
Hugo Benichi | ef50288 | 2017-09-01 01:23:32 +0000 | [diff] [blame] | 33 | import android.net.LinkAddress; |
| 34 | import android.net.LinkProperties; |
| 35 | import android.net.NetworkInfo; |
soma, kawata | 88b8f63 | 2018-10-23 21:10:02 +0900 | [diff] [blame] | 36 | import android.net.NetworkMisc; |
Hugo Benichi | ef50288 | 2017-09-01 01:23:32 +0000 | [diff] [blame] | 37 | import android.os.Handler; |
| 38 | import android.os.INetworkManagementService; |
| 39 | import android.os.test.TestLooper; |
Brett Chabot | 1ae2aa6 | 2019-03-04 14:14:56 -0800 | [diff] [blame] | 40 | |
| 41 | import androidx.test.filters.SmallTest; |
| 42 | import androidx.test.runner.AndroidJUnit4; |
Hugo Benichi | ef50288 | 2017-09-01 01:23:32 +0000 | [diff] [blame] | 43 | |
| 44 | import com.android.server.ConnectivityService; |
| 45 | |
| 46 | import org.junit.Before; |
| 47 | import org.junit.Test; |
| 48 | import org.junit.runner.RunWith; |
| 49 | import org.mockito.ArgumentCaptor; |
Lorenzo Colitti | df59563 | 2019-01-08 14:43:37 +0900 | [diff] [blame] | 50 | import org.mockito.InOrder; |
Hugo Benichi | ef50288 | 2017-09-01 01:23:32 +0000 | [diff] [blame] | 51 | import org.mockito.Mock; |
| 52 | import org.mockito.MockitoAnnotations; |
| 53 | |
| 54 | @RunWith(AndroidJUnit4.class) |
| 55 | @SmallTest |
| 56 | public class Nat464XlatTest { |
| 57 | |
| 58 | static final String BASE_IFACE = "test0"; |
| 59 | static final String STACKED_IFACE = "v4-test0"; |
| 60 | static final LinkAddress ADDR = new LinkAddress("192.0.2.5/29"); |
Lorenzo Colitti | 6998fa8 | 2019-01-08 10:04:25 +0900 | [diff] [blame] | 61 | static final String NAT64_PREFIX = "64:ff9b::/96"; |
Lorenzo Colitti | d593e29 | 2019-02-19 13:21:56 +0900 | [diff] [blame] | 62 | static final int NETID = 42; |
Hugo Benichi | ef50288 | 2017-09-01 01:23:32 +0000 | [diff] [blame] | 63 | |
| 64 | @Mock ConnectivityService mConnectivity; |
soma, kawata | 88b8f63 | 2018-10-23 21:10:02 +0900 | [diff] [blame] | 65 | @Mock NetworkMisc mMisc; |
Lorenzo Colitti | 9307ca2 | 2019-01-12 01:54:23 +0900 | [diff] [blame] | 66 | @Mock INetd mNetd; |
Hugo Benichi | ef50288 | 2017-09-01 01:23:32 +0000 | [diff] [blame] | 67 | @Mock INetworkManagementService mNms; |
| 68 | @Mock InterfaceConfiguration mConfig; |
| 69 | @Mock NetworkAgentInfo mNai; |
| 70 | |
| 71 | TestLooper mLooper; |
| 72 | Handler mHandler; |
| 73 | |
| 74 | Nat464Xlat makeNat464Xlat() { |
Lorenzo Colitti | d593e29 | 2019-02-19 13:21:56 +0900 | [diff] [blame] | 75 | return new Nat464Xlat(mNai, mNetd, mNms) { |
| 76 | @Override protected int getNetId() { |
| 77 | return NETID; |
| 78 | } |
| 79 | }; |
Hugo Benichi | ef50288 | 2017-09-01 01:23:32 +0000 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | @Before |
| 83 | public void setUp() throws Exception { |
| 84 | mLooper = new TestLooper(); |
| 85 | mHandler = new Handler(mLooper.getLooper()); |
| 86 | |
| 87 | MockitoAnnotations.initMocks(this); |
| 88 | |
| 89 | mNai.linkProperties = new LinkProperties(); |
| 90 | mNai.linkProperties.setInterfaceName(BASE_IFACE); |
| 91 | mNai.networkInfo = new NetworkInfo(null); |
| 92 | mNai.networkInfo.setType(ConnectivityManager.TYPE_WIFI); |
| 93 | when(mNai.connService()).thenReturn(mConnectivity); |
soma, kawata | 88b8f63 | 2018-10-23 21:10:02 +0900 | [diff] [blame] | 94 | when(mNai.netMisc()).thenReturn(mMisc); |
Hugo Benichi | ef50288 | 2017-09-01 01:23:32 +0000 | [diff] [blame] | 95 | when(mNai.handler()).thenReturn(mHandler); |
| 96 | |
| 97 | when(mNms.getInterfaceConfig(eq(STACKED_IFACE))).thenReturn(mConfig); |
| 98 | when(mConfig.getLinkAddress()).thenReturn(ADDR); |
| 99 | } |
| 100 | |
Lorenzo Colitti | 6998fa8 | 2019-01-08 10:04:25 +0900 | [diff] [blame] | 101 | private void assertRequiresClat(boolean expected, NetworkAgentInfo nai) { |
Lorenzo Colitti | d593e29 | 2019-02-19 13:21:56 +0900 | [diff] [blame] | 102 | String msg = String.format("requiresClat expected %b for type=%d state=%s skip=%b " |
Lorenzo Colitti | 6998fa8 | 2019-01-08 10:04:25 +0900 | [diff] [blame] | 103 | + "nat64Prefix=%s addresses=%s", expected, nai.networkInfo.getType(), |
| 104 | nai.networkInfo.getDetailedState(), |
| 105 | mMisc.skip464xlat, nai.linkProperties.getNat64Prefix(), |
| 106 | nai.linkProperties.getLinkAddresses()); |
| 107 | assertEquals(msg, expected, Nat464Xlat.requiresClat(nai)); |
| 108 | } |
| 109 | |
Lorenzo Colitti | d593e29 | 2019-02-19 13:21:56 +0900 | [diff] [blame] | 110 | private void assertShouldStartClat(boolean expected, NetworkAgentInfo nai) { |
| 111 | String msg = String.format("shouldStartClat expected %b for type=%d state=%s skip=%b " |
| 112 | + "nat64Prefix=%s addresses=%s", expected, nai.networkInfo.getType(), |
| 113 | nai.networkInfo.getDetailedState(), |
| 114 | mMisc.skip464xlat, nai.linkProperties.getNat64Prefix(), |
| 115 | nai.linkProperties.getLinkAddresses()); |
| 116 | assertEquals(msg, expected, Nat464Xlat.shouldStartClat(nai)); |
| 117 | } |
| 118 | |
Hugo Benichi | ef50288 | 2017-09-01 01:23:32 +0000 | [diff] [blame] | 119 | @Test |
Erik Kline | 3c18216 | 2017-09-21 17:28:10 +0900 | [diff] [blame] | 120 | public void testRequiresClat() throws Exception { |
| 121 | final int[] supportedTypes = { |
| 122 | ConnectivityManager.TYPE_MOBILE, |
| 123 | ConnectivityManager.TYPE_WIFI, |
| 124 | ConnectivityManager.TYPE_ETHERNET, |
| 125 | }; |
| 126 | |
| 127 | // NetworkInfo doesn't allow setting the State directly, but rather |
| 128 | // requires setting DetailedState in order set State as a side-effect. |
| 129 | final NetworkInfo.DetailedState[] supportedDetailedStates = { |
| 130 | NetworkInfo.DetailedState.CONNECTED, |
| 131 | NetworkInfo.DetailedState.SUSPENDED, |
| 132 | }; |
| 133 | |
Lorenzo Colitti | 6998fa8 | 2019-01-08 10:04:25 +0900 | [diff] [blame] | 134 | LinkProperties oldLp = new LinkProperties(mNai.linkProperties); |
Erik Kline | 3c18216 | 2017-09-21 17:28:10 +0900 | [diff] [blame] | 135 | for (int type : supportedTypes) { |
| 136 | mNai.networkInfo.setType(type); |
| 137 | for (NetworkInfo.DetailedState state : supportedDetailedStates) { |
| 138 | mNai.networkInfo.setDetailedState(state, "reason", "extraInfo"); |
Lorenzo Colitti | 6998fa8 | 2019-01-08 10:04:25 +0900 | [diff] [blame] | 139 | |
| 140 | mNai.linkProperties.setNat64Prefix(new IpPrefix("2001:db8:0:64::/96")); |
| 141 | assertRequiresClat(false, mNai); |
Lorenzo Colitti | d593e29 | 2019-02-19 13:21:56 +0900 | [diff] [blame] | 142 | assertShouldStartClat(false, mNai); |
Lorenzo Colitti | 6998fa8 | 2019-01-08 10:04:25 +0900 | [diff] [blame] | 143 | |
| 144 | mNai.linkProperties.addLinkAddress(new LinkAddress("fc00::1/64")); |
| 145 | assertRequiresClat(false, mNai); |
Lorenzo Colitti | d593e29 | 2019-02-19 13:21:56 +0900 | [diff] [blame] | 146 | assertShouldStartClat(false, mNai); |
Lorenzo Colitti | 6998fa8 | 2019-01-08 10:04:25 +0900 | [diff] [blame] | 147 | |
| 148 | mNai.linkProperties.addLinkAddress(new LinkAddress("2001:db8::1/64")); |
| 149 | assertRequiresClat(true, mNai); |
Lorenzo Colitti | d593e29 | 2019-02-19 13:21:56 +0900 | [diff] [blame] | 150 | assertShouldStartClat(true, mNai); |
soma, kawata | 88b8f63 | 2018-10-23 21:10:02 +0900 | [diff] [blame] | 151 | |
| 152 | mMisc.skip464xlat = true; |
Lorenzo Colitti | 6998fa8 | 2019-01-08 10:04:25 +0900 | [diff] [blame] | 153 | assertRequiresClat(false, mNai); |
Lorenzo Colitti | d593e29 | 2019-02-19 13:21:56 +0900 | [diff] [blame] | 154 | assertShouldStartClat(false, mNai); |
soma, kawata | 88b8f63 | 2018-10-23 21:10:02 +0900 | [diff] [blame] | 155 | |
| 156 | mMisc.skip464xlat = false; |
Lorenzo Colitti | 6998fa8 | 2019-01-08 10:04:25 +0900 | [diff] [blame] | 157 | assertRequiresClat(true, mNai); |
Lorenzo Colitti | d593e29 | 2019-02-19 13:21:56 +0900 | [diff] [blame] | 158 | assertShouldStartClat(true, mNai); |
Lorenzo Colitti | 6998fa8 | 2019-01-08 10:04:25 +0900 | [diff] [blame] | 159 | |
| 160 | mNai.linkProperties.addLinkAddress(new LinkAddress("192.0.2.2/24")); |
| 161 | assertRequiresClat(false, mNai); |
Lorenzo Colitti | d593e29 | 2019-02-19 13:21:56 +0900 | [diff] [blame] | 162 | assertShouldStartClat(false, mNai); |
| 163 | |
| 164 | mNai.linkProperties.removeLinkAddress(new LinkAddress("192.0.2.2/24")); |
| 165 | assertRequiresClat(true, mNai); |
| 166 | assertShouldStartClat(true, mNai); |
| 167 | |
| 168 | mNai.linkProperties.setNat64Prefix(null); |
| 169 | assertRequiresClat(true, mNai); |
| 170 | assertShouldStartClat(false, mNai); |
Lorenzo Colitti | 6998fa8 | 2019-01-08 10:04:25 +0900 | [diff] [blame] | 171 | |
| 172 | mNai.linkProperties = new LinkProperties(oldLp); |
Erik Kline | 3c18216 | 2017-09-21 17:28:10 +0900 | [diff] [blame] | 173 | } |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | @Test |
Hugo Benichi | ef50288 | 2017-09-01 01:23:32 +0000 | [diff] [blame] | 178 | public void testNormalStartAndStop() throws Exception { |
| 179 | Nat464Xlat nat = makeNat464Xlat(); |
| 180 | ArgumentCaptor<LinkProperties> c = ArgumentCaptor.forClass(LinkProperties.class); |
| 181 | |
Lorenzo Colitti | 6998fa8 | 2019-01-08 10:04:25 +0900 | [diff] [blame] | 182 | nat.setNat64Prefix(new IpPrefix(NAT64_PREFIX)); |
| 183 | |
Lorenzo Colitti | d593e29 | 2019-02-19 13:21:56 +0900 | [diff] [blame] | 184 | // Start clat. |
Hugo Benichi | ef50288 | 2017-09-01 01:23:32 +0000 | [diff] [blame] | 185 | nat.start(); |
| 186 | |
| 187 | verify(mNms).registerObserver(eq(nat)); |
Lorenzo Colitti | 6998fa8 | 2019-01-08 10:04:25 +0900 | [diff] [blame] | 188 | verify(mNetd).clatdStart(eq(BASE_IFACE), eq(NAT64_PREFIX)); |
Hugo Benichi | ef50288 | 2017-09-01 01:23:32 +0000 | [diff] [blame] | 189 | |
| 190 | // Stacked interface up notification arrives. |
| 191 | nat.interfaceLinkStateChanged(STACKED_IFACE, true); |
| 192 | mLooper.dispatchNext(); |
| 193 | |
| 194 | verify(mNms).getInterfaceConfig(eq(STACKED_IFACE)); |
Hugo Benichi | ef50288 | 2017-09-01 01:23:32 +0000 | [diff] [blame] | 195 | verify(mConnectivity).handleUpdateLinkProperties(eq(mNai), c.capture()); |
| 196 | assertFalse(c.getValue().getStackedLinks().isEmpty()); |
| 197 | assertTrue(c.getValue().getAllInterfaceNames().contains(STACKED_IFACE)); |
| 198 | assertRunning(nat); |
| 199 | |
Lorenzo Colitti | d593e29 | 2019-02-19 13:21:56 +0900 | [diff] [blame] | 200 | // Stop clat (Network disconnects, IPv4 addr appears, ...). |
Hugo Benichi | ef50288 | 2017-09-01 01:23:32 +0000 | [diff] [blame] | 201 | nat.stop(); |
| 202 | |
Lorenzo Colitti | 9307ca2 | 2019-01-12 01:54:23 +0900 | [diff] [blame] | 203 | verify(mNetd).clatdStop(eq(BASE_IFACE)); |
Hugo Benichi | ef50288 | 2017-09-01 01:23:32 +0000 | [diff] [blame] | 204 | verify(mConnectivity, times(2)).handleUpdateLinkProperties(eq(mNai), c.capture()); |
Lorenzo Colitti | df59563 | 2019-01-08 14:43:37 +0900 | [diff] [blame] | 205 | verify(mNms).unregisterObserver(eq(nat)); |
Hugo Benichi | ef50288 | 2017-09-01 01:23:32 +0000 | [diff] [blame] | 206 | assertTrue(c.getValue().getStackedLinks().isEmpty()); |
| 207 | assertFalse(c.getValue().getAllInterfaceNames().contains(STACKED_IFACE)); |
Lorenzo Colitti | d593e29 | 2019-02-19 13:21:56 +0900 | [diff] [blame] | 208 | verify(mNetd).resolverStopPrefix64Discovery(eq(NETID)); |
Hugo Benichi | ef50288 | 2017-09-01 01:23:32 +0000 | [diff] [blame] | 209 | assertIdle(nat); |
| 210 | |
Lorenzo Colitti | df59563 | 2019-01-08 14:43:37 +0900 | [diff] [blame] | 211 | // Stacked interface removed notification arrives and is ignored. |
| 212 | nat.interfaceRemoved(STACKED_IFACE); |
| 213 | mLooper.dispatchNext(); |
| 214 | |
Lorenzo Colitti | 9307ca2 | 2019-01-12 01:54:23 +0900 | [diff] [blame] | 215 | verifyNoMoreInteractions(mNetd, mNms, mConnectivity); |
Hugo Benichi | ef50288 | 2017-09-01 01:23:32 +0000 | [diff] [blame] | 216 | } |
| 217 | |
Lorenzo Colitti | df59563 | 2019-01-08 14:43:37 +0900 | [diff] [blame] | 218 | private void checkStartStopStart(boolean interfaceRemovedFirst) throws Exception { |
| 219 | Nat464Xlat nat = makeNat464Xlat(); |
| 220 | ArgumentCaptor<LinkProperties> c = ArgumentCaptor.forClass(LinkProperties.class); |
| 221 | InOrder inOrder = inOrder(mNetd, mConnectivity); |
| 222 | |
Lorenzo Colitti | 6998fa8 | 2019-01-08 10:04:25 +0900 | [diff] [blame] | 223 | nat.setNat64Prefix(new IpPrefix(NAT64_PREFIX)); |
| 224 | |
Lorenzo Colitti | df59563 | 2019-01-08 14:43:37 +0900 | [diff] [blame] | 225 | nat.start(); |
| 226 | |
Lorenzo Colitti | 6998fa8 | 2019-01-08 10:04:25 +0900 | [diff] [blame] | 227 | inOrder.verify(mNetd).clatdStart(eq(BASE_IFACE), eq(NAT64_PREFIX)); |
Lorenzo Colitti | df59563 | 2019-01-08 14:43:37 +0900 | [diff] [blame] | 228 | |
| 229 | // Stacked interface up notification arrives. |
| 230 | nat.interfaceLinkStateChanged(STACKED_IFACE, true); |
| 231 | mLooper.dispatchNext(); |
| 232 | |
| 233 | inOrder.verify(mConnectivity).handleUpdateLinkProperties(eq(mNai), c.capture()); |
| 234 | assertFalse(c.getValue().getStackedLinks().isEmpty()); |
| 235 | assertTrue(c.getValue().getAllInterfaceNames().contains(STACKED_IFACE)); |
| 236 | assertRunning(nat); |
| 237 | |
| 238 | // ConnectivityService stops clat (Network disconnects, IPv4 addr appears, ...). |
| 239 | nat.stop(); |
| 240 | |
| 241 | inOrder.verify(mNetd).clatdStop(eq(BASE_IFACE)); |
| 242 | |
| 243 | inOrder.verify(mConnectivity, times(1)).handleUpdateLinkProperties(eq(mNai), c.capture()); |
| 244 | assertTrue(c.getValue().getStackedLinks().isEmpty()); |
| 245 | assertFalse(c.getValue().getAllInterfaceNames().contains(STACKED_IFACE)); |
| 246 | assertIdle(nat); |
| 247 | |
| 248 | if (interfaceRemovedFirst) { |
| 249 | // Stacked interface removed notification arrives and is ignored. |
| 250 | nat.interfaceRemoved(STACKED_IFACE); |
| 251 | mLooper.dispatchNext(); |
| 252 | nat.interfaceLinkStateChanged(STACKED_IFACE, false); |
| 253 | mLooper.dispatchNext(); |
| 254 | } |
| 255 | |
| 256 | assertTrue(c.getValue().getStackedLinks().isEmpty()); |
| 257 | assertFalse(c.getValue().getAllInterfaceNames().contains(STACKED_IFACE)); |
| 258 | assertIdle(nat); |
| 259 | inOrder.verifyNoMoreInteractions(); |
| 260 | |
Lorenzo Colitti | df59563 | 2019-01-08 14:43:37 +0900 | [diff] [blame] | 261 | nat.start(); |
| 262 | |
Lorenzo Colitti | 6998fa8 | 2019-01-08 10:04:25 +0900 | [diff] [blame] | 263 | inOrder.verify(mNetd).clatdStart(eq(BASE_IFACE), eq(NAT64_PREFIX)); |
Lorenzo Colitti | df59563 | 2019-01-08 14:43:37 +0900 | [diff] [blame] | 264 | |
| 265 | if (!interfaceRemovedFirst) { |
| 266 | // Stacked interface removed notification arrives and is ignored. |
| 267 | nat.interfaceRemoved(STACKED_IFACE); |
| 268 | mLooper.dispatchNext(); |
| 269 | nat.interfaceLinkStateChanged(STACKED_IFACE, false); |
| 270 | mLooper.dispatchNext(); |
| 271 | } |
| 272 | |
| 273 | // Stacked interface up notification arrives. |
| 274 | nat.interfaceLinkStateChanged(STACKED_IFACE, true); |
| 275 | mLooper.dispatchNext(); |
| 276 | |
| 277 | inOrder.verify(mConnectivity).handleUpdateLinkProperties(eq(mNai), c.capture()); |
| 278 | assertFalse(c.getValue().getStackedLinks().isEmpty()); |
| 279 | assertTrue(c.getValue().getAllInterfaceNames().contains(STACKED_IFACE)); |
| 280 | assertRunning(nat); |
| 281 | |
| 282 | // ConnectivityService stops clat again. |
| 283 | nat.stop(); |
| 284 | |
| 285 | inOrder.verify(mNetd).clatdStop(eq(BASE_IFACE)); |
| 286 | |
| 287 | inOrder.verify(mConnectivity, times(1)).handleUpdateLinkProperties(eq(mNai), c.capture()); |
| 288 | assertTrue(c.getValue().getStackedLinks().isEmpty()); |
| 289 | assertFalse(c.getValue().getAllInterfaceNames().contains(STACKED_IFACE)); |
| 290 | assertIdle(nat); |
| 291 | |
| 292 | inOrder.verifyNoMoreInteractions(); |
| 293 | } |
| 294 | |
| 295 | @Test |
| 296 | public void testStartStopStart() throws Exception { |
| 297 | checkStartStopStart(true); |
| 298 | } |
| 299 | |
| 300 | @Test |
| 301 | public void testStartStopStartBeforeInterfaceRemoved() throws Exception { |
| 302 | checkStartStopStart(false); |
| 303 | } |
| 304 | |
Hugo Benichi | ef50288 | 2017-09-01 01:23:32 +0000 | [diff] [blame] | 305 | @Test |
| 306 | public void testClatdCrashWhileRunning() throws Exception { |
| 307 | Nat464Xlat nat = makeNat464Xlat(); |
| 308 | ArgumentCaptor<LinkProperties> c = ArgumentCaptor.forClass(LinkProperties.class); |
| 309 | |
Lorenzo Colitti | 6998fa8 | 2019-01-08 10:04:25 +0900 | [diff] [blame] | 310 | nat.setNat64Prefix(new IpPrefix(NAT64_PREFIX)); |
| 311 | |
Hugo Benichi | ef50288 | 2017-09-01 01:23:32 +0000 | [diff] [blame] | 312 | nat.start(); |
| 313 | |
| 314 | verify(mNms).registerObserver(eq(nat)); |
Lorenzo Colitti | 6998fa8 | 2019-01-08 10:04:25 +0900 | [diff] [blame] | 315 | verify(mNetd).clatdStart(eq(BASE_IFACE), eq(NAT64_PREFIX)); |
Hugo Benichi | ef50288 | 2017-09-01 01:23:32 +0000 | [diff] [blame] | 316 | |
| 317 | // Stacked interface up notification arrives. |
| 318 | nat.interfaceLinkStateChanged(STACKED_IFACE, true); |
| 319 | mLooper.dispatchNext(); |
| 320 | |
| 321 | verify(mNms).getInterfaceConfig(eq(STACKED_IFACE)); |
Hugo Benichi | ef50288 | 2017-09-01 01:23:32 +0000 | [diff] [blame] | 322 | verify(mConnectivity, times(1)).handleUpdateLinkProperties(eq(mNai), c.capture()); |
| 323 | assertFalse(c.getValue().getStackedLinks().isEmpty()); |
| 324 | assertTrue(c.getValue().getAllInterfaceNames().contains(STACKED_IFACE)); |
| 325 | assertRunning(nat); |
| 326 | |
| 327 | // Stacked interface removed notification arrives (clatd crashed, ...). |
| 328 | nat.interfaceRemoved(STACKED_IFACE); |
| 329 | mLooper.dispatchNext(); |
| 330 | |
Lorenzo Colitti | 9307ca2 | 2019-01-12 01:54:23 +0900 | [diff] [blame] | 331 | verify(mNetd).clatdStop(eq(BASE_IFACE)); |
Hugo Benichi | ef50288 | 2017-09-01 01:23:32 +0000 | [diff] [blame] | 332 | verify(mConnectivity, times(2)).handleUpdateLinkProperties(eq(mNai), c.capture()); |
Lorenzo Colitti | df59563 | 2019-01-08 14:43:37 +0900 | [diff] [blame] | 333 | verify(mNms).unregisterObserver(eq(nat)); |
Lorenzo Colitti | d593e29 | 2019-02-19 13:21:56 +0900 | [diff] [blame] | 334 | verify(mNetd).resolverStopPrefix64Discovery(eq(NETID)); |
Hugo Benichi | ef50288 | 2017-09-01 01:23:32 +0000 | [diff] [blame] | 335 | assertTrue(c.getValue().getStackedLinks().isEmpty()); |
| 336 | assertFalse(c.getValue().getAllInterfaceNames().contains(STACKED_IFACE)); |
| 337 | assertIdle(nat); |
| 338 | |
| 339 | // ConnectivityService stops clat: no-op. |
| 340 | nat.stop(); |
| 341 | |
Lorenzo Colitti | 9307ca2 | 2019-01-12 01:54:23 +0900 | [diff] [blame] | 342 | verifyNoMoreInteractions(mNetd, mNms, mConnectivity); |
Hugo Benichi | ef50288 | 2017-09-01 01:23:32 +0000 | [diff] [blame] | 343 | } |
| 344 | |
| 345 | @Test |
| 346 | public void testStopBeforeClatdStarts() throws Exception { |
| 347 | Nat464Xlat nat = makeNat464Xlat(); |
| 348 | |
Lorenzo Colitti | 6998fa8 | 2019-01-08 10:04:25 +0900 | [diff] [blame] | 349 | nat.setNat64Prefix(new IpPrefix(NAT64_PREFIX)); |
| 350 | |
Hugo Benichi | ef50288 | 2017-09-01 01:23:32 +0000 | [diff] [blame] | 351 | nat.start(); |
| 352 | |
| 353 | verify(mNms).registerObserver(eq(nat)); |
Lorenzo Colitti | 6998fa8 | 2019-01-08 10:04:25 +0900 | [diff] [blame] | 354 | verify(mNetd).clatdStart(eq(BASE_IFACE), eq(NAT64_PREFIX)); |
Hugo Benichi | ef50288 | 2017-09-01 01:23:32 +0000 | [diff] [blame] | 355 | |
| 356 | // ConnectivityService immediately stops clat (Network disconnects, IPv4 addr appears, ...) |
| 357 | nat.stop(); |
| 358 | |
Lorenzo Colitti | 9307ca2 | 2019-01-12 01:54:23 +0900 | [diff] [blame] | 359 | verify(mNetd).clatdStop(eq(BASE_IFACE)); |
Lorenzo Colitti | df59563 | 2019-01-08 14:43:37 +0900 | [diff] [blame] | 360 | verify(mNms).unregisterObserver(eq(nat)); |
Lorenzo Colitti | d593e29 | 2019-02-19 13:21:56 +0900 | [diff] [blame] | 361 | verify(mNetd).resolverStopPrefix64Discovery(eq(NETID)); |
Hugo Benichi | ef50288 | 2017-09-01 01:23:32 +0000 | [diff] [blame] | 362 | assertIdle(nat); |
| 363 | |
| 364 | // In-flight interface up notification arrives: no-op |
| 365 | nat.interfaceLinkStateChanged(STACKED_IFACE, true); |
| 366 | mLooper.dispatchNext(); |
| 367 | |
Hugo Benichi | ef50288 | 2017-09-01 01:23:32 +0000 | [diff] [blame] | 368 | // Interface removed notification arrives after stopClatd() takes effect: no-op. |
| 369 | nat.interfaceRemoved(STACKED_IFACE); |
| 370 | mLooper.dispatchNext(); |
| 371 | |
| 372 | assertIdle(nat); |
| 373 | |
Lorenzo Colitti | 9307ca2 | 2019-01-12 01:54:23 +0900 | [diff] [blame] | 374 | verifyNoMoreInteractions(mNetd, mNms, mConnectivity); |
Hugo Benichi | ef50288 | 2017-09-01 01:23:32 +0000 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | @Test |
| 378 | public void testStopAndClatdNeverStarts() throws Exception { |
| 379 | Nat464Xlat nat = makeNat464Xlat(); |
| 380 | |
Lorenzo Colitti | 6998fa8 | 2019-01-08 10:04:25 +0900 | [diff] [blame] | 381 | nat.setNat64Prefix(new IpPrefix(NAT64_PREFIX)); |
| 382 | |
Hugo Benichi | ef50288 | 2017-09-01 01:23:32 +0000 | [diff] [blame] | 383 | nat.start(); |
| 384 | |
| 385 | verify(mNms).registerObserver(eq(nat)); |
Lorenzo Colitti | 6998fa8 | 2019-01-08 10:04:25 +0900 | [diff] [blame] | 386 | verify(mNetd).clatdStart(eq(BASE_IFACE), eq(NAT64_PREFIX)); |
Hugo Benichi | ef50288 | 2017-09-01 01:23:32 +0000 | [diff] [blame] | 387 | |
| 388 | // ConnectivityService immediately stops clat (Network disconnects, IPv4 addr appears, ...) |
| 389 | nat.stop(); |
| 390 | |
Lorenzo Colitti | 9307ca2 | 2019-01-12 01:54:23 +0900 | [diff] [blame] | 391 | verify(mNetd).clatdStop(eq(BASE_IFACE)); |
Lorenzo Colitti | df59563 | 2019-01-08 14:43:37 +0900 | [diff] [blame] | 392 | verify(mNms).unregisterObserver(eq(nat)); |
Lorenzo Colitti | d593e29 | 2019-02-19 13:21:56 +0900 | [diff] [blame] | 393 | verify(mNetd).resolverStopPrefix64Discovery(eq(NETID)); |
Hugo Benichi | ef50288 | 2017-09-01 01:23:32 +0000 | [diff] [blame] | 394 | assertIdle(nat); |
| 395 | |
Lorenzo Colitti | 9307ca2 | 2019-01-12 01:54:23 +0900 | [diff] [blame] | 396 | verifyNoMoreInteractions(mNetd, mNms, mConnectivity); |
Hugo Benichi | ef50288 | 2017-09-01 01:23:32 +0000 | [diff] [blame] | 397 | } |
| 398 | |
| 399 | static void assertIdle(Nat464Xlat nat) { |
| 400 | assertTrue("Nat464Xlat was not IDLE", !nat.isStarted()); |
| 401 | } |
| 402 | |
| 403 | static void assertRunning(Nat464Xlat nat) { |
| 404 | assertTrue("Nat464Xlat was not RUNNING", nat.isRunning()); |
| 405 | } |
| 406 | } |