blob: a5c55e8d844e9bfa5dfd333cf39fb8df1c1decfa [file] [log] [blame]
Nathan Harold2e9a5202017-09-26 11:44:23 -07001/*
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
17package com.android.server;
18
19import static org.junit.Assert.assertEquals;
Benedict Wongecc9f7c2018-03-01 18:53:07 -080020import static org.junit.Assert.assertNotNull;
Benedict Wong0febe5e2017-08-22 21:42:33 -070021import static org.junit.Assert.fail;
Nathan Harold2e9a5202017-09-26 11:44:23 -070022import static org.mockito.Matchers.anyInt;
Nathan Harold2e9a5202017-09-26 11:44:23 -070023import static org.mockito.Matchers.anyString;
24import static org.mockito.Matchers.eq;
25import static org.mockito.Mockito.mock;
26import static org.mockito.Mockito.verify;
27import static org.mockito.Mockito.when;
28
29import android.content.Context;
30import android.net.INetd;
31import android.net.IpSecAlgorithm;
32import android.net.IpSecConfig;
33import android.net.IpSecManager;
34import android.net.IpSecSpiResponse;
Nathan Harold2e9a5202017-09-26 11:44:23 -070035import android.net.IpSecTransformResponse;
Benedict Wongecc9f7c2018-03-01 18:53:07 -080036import android.net.IpSecTunnelInterfaceResponse;
37import android.net.LinkAddress;
38import android.net.Network;
Nathan Harold2e9a5202017-09-26 11:44:23 -070039import android.net.NetworkUtils;
40import android.os.Binder;
41import android.os.ParcelFileDescriptor;
42import android.support.test.filters.SmallTest;
Benedict Wong344bd622017-11-16 15:27:22 -080043import android.system.Os;
Nathan Harold2e9a5202017-09-26 11:44:23 -070044
45import java.net.Socket;
46import java.util.Arrays;
47import java.util.Collection;
48
49import org.junit.Before;
50import org.junit.Test;
51import org.junit.runner.RunWith;
52import org.junit.runners.Parameterized;
53
54/** Unit tests for {@link IpSecService}. */
55@SmallTest
56@RunWith(Parameterized.class)
57public class IpSecServiceParameterizedTest {
58
Nathan Harolda2523312018-01-05 19:25:13 -080059 private static final int TEST_SPI = 0xD1201D;
Nathan Harold2e9a5202017-09-26 11:44:23 -070060
Nathan Harolda2523312018-01-05 19:25:13 -080061 private final String mDestinationAddr;
Nathan Harold5676f5f2018-01-16 19:34:01 -080062 private final String mSourceAddr;
Benedict Wongecc9f7c2018-03-01 18:53:07 -080063 private final LinkAddress mLocalInnerAddress;
Nathan Harold2e9a5202017-09-26 11:44:23 -070064
65 @Parameterized.Parameters
66 public static Collection ipSecConfigs() {
Benedict Wongecc9f7c2018-03-01 18:53:07 -080067 return Arrays.asList(
68 new Object[][] {
69 {"1.2.3.4", "8.8.4.4", "10.0.1.1/24"},
70 {"2601::2", "2601::10", "2001:db8::1/64"}
71 });
Nathan Harold2e9a5202017-09-26 11:44:23 -070072 }
73
Benedict Wong4ebc2c52017-11-01 17:14:25 -070074 private static final byte[] AEAD_KEY = {
75 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
76 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
77 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
78 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F,
79 0x73, 0x61, 0x6C, 0x74
80 };
Nathan Harold2e9a5202017-09-26 11:44:23 -070081 private static final byte[] CRYPT_KEY = {
82 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
83 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
84 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
85 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F
86 };
87 private static final byte[] AUTH_KEY = {
88 0x7A, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
89 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7F,
90 0x7A, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
91 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7F
92 };
93
94 Context mMockContext;
95 INetd mMockNetd;
96 IpSecService.IpSecServiceConfiguration mMockIpSecSrvConfig;
97 IpSecService mIpSecService;
Benedict Wongecc9f7c2018-03-01 18:53:07 -080098 Network fakeNetwork = new Network(0xAB);
Nathan Harold2e9a5202017-09-26 11:44:23 -070099
Benedict Wong0febe5e2017-08-22 21:42:33 -0700100 private static final IpSecAlgorithm AUTH_ALGO =
101 new IpSecAlgorithm(IpSecAlgorithm.AUTH_HMAC_SHA256, AUTH_KEY, AUTH_KEY.length * 4);
102 private static final IpSecAlgorithm CRYPT_ALGO =
103 new IpSecAlgorithm(IpSecAlgorithm.CRYPT_AES_CBC, CRYPT_KEY);
104 private static final IpSecAlgorithm AEAD_ALGO =
Benedict Wong4ebc2c52017-11-01 17:14:25 -0700105 new IpSecAlgorithm(IpSecAlgorithm.AUTH_CRYPT_AES_GCM, AEAD_KEY, 128);
Benedict Wong0febe5e2017-08-22 21:42:33 -0700106
Benedict Wongecc9f7c2018-03-01 18:53:07 -0800107 public IpSecServiceParameterizedTest(
108 String sourceAddr, String destAddr, String localInnerAddr) {
Nathan Harold5676f5f2018-01-16 19:34:01 -0800109 mSourceAddr = sourceAddr;
110 mDestinationAddr = destAddr;
Benedict Wongecc9f7c2018-03-01 18:53:07 -0800111 mLocalInnerAddress = new LinkAddress(localInnerAddr);
Nathan Harold2e9a5202017-09-26 11:44:23 -0700112 }
113
114 @Before
115 public void setUp() throws Exception {
116 mMockContext = mock(Context.class);
117 mMockNetd = mock(INetd.class);
118 mMockIpSecSrvConfig = mock(IpSecService.IpSecServiceConfiguration.class);
119 mIpSecService = new IpSecService(mMockContext, mMockIpSecSrvConfig);
120
121 // Injecting mock netd
122 when(mMockIpSecSrvConfig.getNetdInstance()).thenReturn(mMockNetd);
123 }
124
125 @Test
126 public void testIpSecServiceReserveSpi() throws Exception {
Nathan Harolda2523312018-01-05 19:25:13 -0800127 when(mMockNetd.ipSecAllocateSpi(anyInt(), anyString(), eq(mDestinationAddr), eq(TEST_SPI)))
128 .thenReturn(TEST_SPI);
Nathan Harold2e9a5202017-09-26 11:44:23 -0700129
130 IpSecSpiResponse spiResp =
Jonathan Basseri5fb92902017-11-16 10:58:01 -0800131 mIpSecService.allocateSecurityParameterIndex(
Nathan Harolda2523312018-01-05 19:25:13 -0800132 mDestinationAddr, TEST_SPI, new Binder());
Nathan Harold2e9a5202017-09-26 11:44:23 -0700133 assertEquals(IpSecManager.Status.OK, spiResp.status);
Nathan Harolda2523312018-01-05 19:25:13 -0800134 assertEquals(TEST_SPI, spiResp.spi);
Nathan Harold2e9a5202017-09-26 11:44:23 -0700135 }
136
137 @Test
138 public void testReleaseSecurityParameterIndex() throws Exception {
Nathan Harolda2523312018-01-05 19:25:13 -0800139 when(mMockNetd.ipSecAllocateSpi(anyInt(), anyString(), eq(mDestinationAddr), eq(TEST_SPI)))
140 .thenReturn(TEST_SPI);
Nathan Harold2e9a5202017-09-26 11:44:23 -0700141
142 IpSecSpiResponse spiResp =
Jonathan Basseri5fb92902017-11-16 10:58:01 -0800143 mIpSecService.allocateSecurityParameterIndex(
Nathan Harolda2523312018-01-05 19:25:13 -0800144 mDestinationAddr, TEST_SPI, new Binder());
Nathan Harold2e9a5202017-09-26 11:44:23 -0700145
146 mIpSecService.releaseSecurityParameterIndex(spiResp.resourceId);
147
148 verify(mMockNetd)
149 .ipSecDeleteSecurityAssociation(
Di Lu0b611f42018-01-11 11:35:25 -0800150 eq(spiResp.resourceId),
151 anyString(),
152 anyString(),
153 eq(TEST_SPI),
154 anyInt(),
155 anyInt());
Benedict Wong344bd622017-11-16 15:27:22 -0800156
157 // Verify quota and RefcountedResource objects cleaned up
158 IpSecService.UserRecord userRecord =
159 mIpSecService.mUserResourceTracker.getUserRecord(Os.getuid());
160 assertEquals(0, userRecord.mSpiQuotaTracker.mCurrent);
161 try {
162 userRecord.mSpiRecords.getRefcountedResourceOrThrow(spiResp.resourceId);
163 fail("Expected IllegalArgumentException on attempt to access deleted resource");
164 } catch (IllegalArgumentException expected) {
165
166 }
167 }
168
169 @Test
170 public void testSecurityParameterIndexBinderDeath() throws Exception {
Nathan Harolda2523312018-01-05 19:25:13 -0800171 when(mMockNetd.ipSecAllocateSpi(anyInt(), anyString(), eq(mDestinationAddr), eq(TEST_SPI)))
172 .thenReturn(TEST_SPI);
Benedict Wong344bd622017-11-16 15:27:22 -0800173
174 IpSecSpiResponse spiResp =
Nathan Harold660a3352017-12-14 14:46:46 -0800175 mIpSecService.allocateSecurityParameterIndex(
Nathan Harolda2523312018-01-05 19:25:13 -0800176 mDestinationAddr, TEST_SPI, new Binder());
Benedict Wong344bd622017-11-16 15:27:22 -0800177
178 IpSecService.UserRecord userRecord =
179 mIpSecService.mUserResourceTracker.getUserRecord(Os.getuid());
180 IpSecService.RefcountedResource refcountedRecord =
181 userRecord.mSpiRecords.getRefcountedResourceOrThrow(spiResp.resourceId);
182
183 refcountedRecord.binderDied();
184
185 verify(mMockNetd)
186 .ipSecDeleteSecurityAssociation(
Di Lu0b611f42018-01-11 11:35:25 -0800187 eq(spiResp.resourceId),
188 anyString(),
189 anyString(),
190 eq(TEST_SPI),
191 anyInt(),
192 anyInt());
Benedict Wong344bd622017-11-16 15:27:22 -0800193
194 // Verify quota and RefcountedResource objects cleaned up
195 assertEquals(0, userRecord.mSpiQuotaTracker.mCurrent);
196 try {
197 userRecord.mSpiRecords.getRefcountedResourceOrThrow(spiResp.resourceId);
198 fail("Expected IllegalArgumentException on attempt to access deleted resource");
199 } catch (IllegalArgumentException expected) {
200
201 }
Nathan Harold2e9a5202017-09-26 11:44:23 -0700202 }
203
Nathan Harolda2523312018-01-05 19:25:13 -0800204 private int getNewSpiResourceId(String remoteAddress, int returnSpi) throws Exception {
205 when(mMockNetd.ipSecAllocateSpi(anyInt(), anyString(), anyString(), anyInt()))
Benedict Wong0febe5e2017-08-22 21:42:33 -0700206 .thenReturn(returnSpi);
Nathan Harold2e9a5202017-09-26 11:44:23 -0700207
Benedict Wong0febe5e2017-08-22 21:42:33 -0700208 IpSecSpiResponse spi =
Jonathan Basseri5fb92902017-11-16 10:58:01 -0800209 mIpSecService.allocateSecurityParameterIndex(
Benedict Wong0febe5e2017-08-22 21:42:33 -0700210 NetworkUtils.numericToInetAddress(remoteAddress).getHostAddress(),
211 IpSecManager.INVALID_SECURITY_PARAMETER_INDEX,
212 new Binder());
213 return spi.resourceId;
214 }
Nathan Harold2e9a5202017-09-26 11:44:23 -0700215
Benedict Wong0febe5e2017-08-22 21:42:33 -0700216 private void addDefaultSpisAndRemoteAddrToIpSecConfig(IpSecConfig config) throws Exception {
Nathan Harolda2523312018-01-05 19:25:13 -0800217 config.setSpiResourceId(getNewSpiResourceId(mDestinationAddr, TEST_SPI));
Nathan Harold5676f5f2018-01-16 19:34:01 -0800218 config.setSourceAddress(mSourceAddr);
Nathan Harolda2523312018-01-05 19:25:13 -0800219 config.setDestinationAddress(mDestinationAddr);
Benedict Wong0febe5e2017-08-22 21:42:33 -0700220 }
221
222 private void addAuthAndCryptToIpSecConfig(IpSecConfig config) throws Exception {
Nathan Harolda2523312018-01-05 19:25:13 -0800223 config.setEncryption(CRYPT_ALGO);
224 config.setAuthentication(AUTH_ALGO);
Nathan Harold2e9a5202017-09-26 11:44:23 -0700225 }
226
227 @Test
Benedict Wongf33f03132018-01-18 14:38:16 -0800228 public void testCreateTransform() throws Exception {
Benedict Wong0febe5e2017-08-22 21:42:33 -0700229 IpSecConfig ipSecConfig = new IpSecConfig();
230 addDefaultSpisAndRemoteAddrToIpSecConfig(ipSecConfig);
231 addAuthAndCryptToIpSecConfig(ipSecConfig);
Nathan Harold2e9a5202017-09-26 11:44:23 -0700232
233 IpSecTransformResponse createTransformResp =
Benedict Wongf33f03132018-01-18 14:38:16 -0800234 mIpSecService.createTransform(ipSecConfig, new Binder());
Nathan Harold2e9a5202017-09-26 11:44:23 -0700235 assertEquals(IpSecManager.Status.OK, createTransformResp.status);
236
237 verify(mMockNetd)
238 .ipSecAddSecurityAssociation(
239 eq(createTransformResp.resourceId),
240 anyInt(),
Nathan Harold2e9a5202017-09-26 11:44:23 -0700241 anyString(),
242 anyString(),
Benedict Wong0fe58a92018-01-19 17:36:02 -0800243 anyInt(),
Nathan Harolda2523312018-01-05 19:25:13 -0800244 eq(TEST_SPI),
Di Lu0b611f42018-01-11 11:35:25 -0800245 anyInt(),
246 anyInt(),
Benedict Wong0febe5e2017-08-22 21:42:33 -0700247 eq(IpSecAlgorithm.AUTH_HMAC_SHA256),
248 eq(AUTH_KEY),
249 anyInt(),
250 eq(IpSecAlgorithm.CRYPT_AES_CBC),
251 eq(CRYPT_KEY),
252 anyInt(),
253 eq(""),
Manoj Boopathi Rajfffa8112017-10-26 11:49:02 -0700254 eq(new byte[] {}),
Benedict Wong0febe5e2017-08-22 21:42:33 -0700255 eq(0),
256 anyInt(),
257 anyInt(),
258 anyInt());
259 }
260
261 @Test
Benedict Wongf33f03132018-01-18 14:38:16 -0800262 public void testCreateTransformAead() throws Exception {
Benedict Wong0febe5e2017-08-22 21:42:33 -0700263 IpSecConfig ipSecConfig = new IpSecConfig();
264 addDefaultSpisAndRemoteAddrToIpSecConfig(ipSecConfig);
265
Nathan Harolda2523312018-01-05 19:25:13 -0800266 ipSecConfig.setAuthenticatedEncryption(AEAD_ALGO);
Benedict Wong0febe5e2017-08-22 21:42:33 -0700267
268 IpSecTransformResponse createTransformResp =
Benedict Wongf33f03132018-01-18 14:38:16 -0800269 mIpSecService.createTransform(ipSecConfig, new Binder());
Benedict Wong0febe5e2017-08-22 21:42:33 -0700270 assertEquals(IpSecManager.Status.OK, createTransformResp.status);
271
272 verify(mMockNetd)
273 .ipSecAddSecurityAssociation(
274 eq(createTransformResp.resourceId),
275 anyInt(),
Benedict Wong0febe5e2017-08-22 21:42:33 -0700276 anyString(),
277 anyString(),
Benedict Wong0fe58a92018-01-19 17:36:02 -0800278 anyInt(),
Nathan Harolda2523312018-01-05 19:25:13 -0800279 eq(TEST_SPI),
Di Lu0b611f42018-01-11 11:35:25 -0800280 anyInt(),
281 anyInt(),
Benedict Wong0febe5e2017-08-22 21:42:33 -0700282 eq(""),
Manoj Boopathi Rajfffa8112017-10-26 11:49:02 -0700283 eq(new byte[] {}),
Benedict Wong0febe5e2017-08-22 21:42:33 -0700284 eq(0),
285 eq(""),
Manoj Boopathi Rajfffa8112017-10-26 11:49:02 -0700286 eq(new byte[] {}),
Benedict Wong0febe5e2017-08-22 21:42:33 -0700287 eq(0),
288 eq(IpSecAlgorithm.AUTH_CRYPT_AES_GCM),
Benedict Wong4ebc2c52017-11-01 17:14:25 -0700289 eq(AEAD_KEY),
Nathan Harold2e9a5202017-09-26 11:44:23 -0700290 anyInt(),
291 anyInt(),
292 anyInt(),
293 anyInt());
294 }
295
Andreas Gampea7b26b52018-02-26 08:06:30 -0800296 @Test
Benedict Wonge6b42772017-12-13 18:26:40 -0800297 public void testCreateTwoTransformsWithSameSpis() throws Exception {
298 IpSecConfig ipSecConfig = new IpSecConfig();
299 addDefaultSpisAndRemoteAddrToIpSecConfig(ipSecConfig);
300 addAuthAndCryptToIpSecConfig(ipSecConfig);
301
302 IpSecTransformResponse createTransformResp =
303 mIpSecService.createTransform(ipSecConfig, new Binder());
304 assertEquals(IpSecManager.Status.OK, createTransformResp.status);
305
306 // Attempting to create transform a second time with the same SPIs should throw an error...
307 try {
308 mIpSecService.createTransform(ipSecConfig, new Binder());
309 fail("IpSecService should have thrown an error for reuse of SPI");
310 } catch (IllegalStateException expected) {
311 }
312
313 // ... even if the transform is deleted
314 mIpSecService.deleteTransform(createTransformResp.resourceId);
315 try {
316 mIpSecService.createTransform(ipSecConfig, new Binder());
317 fail("IpSecService should have thrown an error for reuse of SPI");
318 } catch (IllegalStateException expected) {
319 }
320 }
321
Nathan Harold2e9a5202017-09-26 11:44:23 -0700322 @Test
Benedict Wongf33f03132018-01-18 14:38:16 -0800323 public void testDeleteTransform() throws Exception {
Benedict Wong0febe5e2017-08-22 21:42:33 -0700324 IpSecConfig ipSecConfig = new IpSecConfig();
325 addDefaultSpisAndRemoteAddrToIpSecConfig(ipSecConfig);
326 addAuthAndCryptToIpSecConfig(ipSecConfig);
Nathan Harold2e9a5202017-09-26 11:44:23 -0700327
328 IpSecTransformResponse createTransformResp =
Benedict Wongf33f03132018-01-18 14:38:16 -0800329 mIpSecService.createTransform(ipSecConfig, new Binder());
330 mIpSecService.deleteTransform(createTransformResp.resourceId);
Nathan Harold2e9a5202017-09-26 11:44:23 -0700331
332 verify(mMockNetd)
333 .ipSecDeleteSecurityAssociation(
Di Lu0b611f42018-01-11 11:35:25 -0800334 eq(createTransformResp.resourceId),
335 anyString(),
336 anyString(),
337 eq(TEST_SPI),
338 anyInt(),
339 anyInt());
Benedict Wong344bd622017-11-16 15:27:22 -0800340
341 // Verify quota and RefcountedResource objects cleaned up
342 IpSecService.UserRecord userRecord =
343 mIpSecService.mUserResourceTracker.getUserRecord(Os.getuid());
344 assertEquals(0, userRecord.mTransformQuotaTracker.mCurrent);
345 try {
346 userRecord.mTransformRecords.getRefcountedResourceOrThrow(
347 createTransformResp.resourceId);
348 fail("Expected IllegalArgumentException on attempt to access deleted resource");
349 } catch (IllegalArgumentException expected) {
350
351 }
352 }
353
354 @Test
355 public void testTransportModeTransformBinderDeath() throws Exception {
356 IpSecConfig ipSecConfig = new IpSecConfig();
357 addDefaultSpisAndRemoteAddrToIpSecConfig(ipSecConfig);
358 addAuthAndCryptToIpSecConfig(ipSecConfig);
359
360 IpSecTransformResponse createTransformResp =
Benedict Wongf33f03132018-01-18 14:38:16 -0800361 mIpSecService.createTransform(ipSecConfig, new Binder());
Benedict Wong344bd622017-11-16 15:27:22 -0800362
363 IpSecService.UserRecord userRecord =
364 mIpSecService.mUserResourceTracker.getUserRecord(Os.getuid());
365 IpSecService.RefcountedResource refcountedRecord =
366 userRecord.mTransformRecords.getRefcountedResourceOrThrow(
367 createTransformResp.resourceId);
368
369 refcountedRecord.binderDied();
370
371 verify(mMockNetd)
372 .ipSecDeleteSecurityAssociation(
Di Lu0b611f42018-01-11 11:35:25 -0800373 eq(createTransformResp.resourceId),
374 anyString(),
375 anyString(),
376 eq(TEST_SPI),
377 anyInt(),
378 anyInt());
Benedict Wong344bd622017-11-16 15:27:22 -0800379
380 // Verify quota and RefcountedResource objects cleaned up
381 assertEquals(0, userRecord.mTransformQuotaTracker.mCurrent);
382 try {
383 userRecord.mTransformRecords.getRefcountedResourceOrThrow(
384 createTransformResp.resourceId);
385 fail("Expected IllegalArgumentException on attempt to access deleted resource");
386 } catch (IllegalArgumentException expected) {
387
388 }
Nathan Harold2e9a5202017-09-26 11:44:23 -0700389 }
390
391 @Test
392 public void testApplyTransportModeTransform() throws Exception {
Benedict Wong0febe5e2017-08-22 21:42:33 -0700393 IpSecConfig ipSecConfig = new IpSecConfig();
394 addDefaultSpisAndRemoteAddrToIpSecConfig(ipSecConfig);
395 addAuthAndCryptToIpSecConfig(ipSecConfig);
Nathan Harold2e9a5202017-09-26 11:44:23 -0700396
397 IpSecTransformResponse createTransformResp =
Benedict Wongf33f03132018-01-18 14:38:16 -0800398 mIpSecService.createTransform(ipSecConfig, new Binder());
Nathan Harold2e9a5202017-09-26 11:44:23 -0700399 ParcelFileDescriptor pfd = ParcelFileDescriptor.fromSocket(new Socket());
400
401 int resourceId = createTransformResp.resourceId;
Nathan Harolda2523312018-01-05 19:25:13 -0800402 mIpSecService.applyTransportModeTransform(pfd, IpSecManager.DIRECTION_OUT, resourceId);
Nathan Harold2e9a5202017-09-26 11:44:23 -0700403
404 verify(mMockNetd)
405 .ipSecApplyTransportModeTransform(
406 eq(pfd.getFileDescriptor()),
407 eq(resourceId),
Nathan Harolda2523312018-01-05 19:25:13 -0800408 eq(IpSecManager.DIRECTION_OUT),
Nathan Harold2e9a5202017-09-26 11:44:23 -0700409 anyString(),
410 anyString(),
Nathan Harolda2523312018-01-05 19:25:13 -0800411 eq(TEST_SPI));
Nathan Harold2e9a5202017-09-26 11:44:23 -0700412 }
413
414 @Test
415 public void testRemoveTransportModeTransform() throws Exception {
416 ParcelFileDescriptor pfd = ParcelFileDescriptor.fromSocket(new Socket());
Nathan Haroldf73d2522018-01-17 01:00:20 -0800417 mIpSecService.removeTransportModeTransforms(pfd);
Nathan Harold2e9a5202017-09-26 11:44:23 -0700418
419 verify(mMockNetd).ipSecRemoveTransportModeTransform(pfd.getFileDescriptor());
420 }
Benedict Wongecc9f7c2018-03-01 18:53:07 -0800421
422 private IpSecTunnelInterfaceResponse createAndValidateTunnel(
423 String localAddr, String remoteAddr) {
424 IpSecTunnelInterfaceResponse createTunnelResp =
425 mIpSecService.createTunnelInterface(
426 mSourceAddr, mDestinationAddr, fakeNetwork, new Binder());
427
428 assertNotNull(createTunnelResp);
429 assertEquals(IpSecManager.Status.OK, createTunnelResp.status);
430 return createTunnelResp;
431 }
432
433 @Test
434 public void testCreateTunnelInterface() throws Exception {
435 IpSecTunnelInterfaceResponse createTunnelResp =
436 createAndValidateTunnel(mSourceAddr, mDestinationAddr);
437
438 // Check that we have stored the tracking object, and retrieve it
439 IpSecService.UserRecord userRecord =
440 mIpSecService.mUserResourceTracker.getUserRecord(Os.getuid());
441 IpSecService.RefcountedResource refcountedRecord =
442 userRecord.mTunnelInterfaceRecords.getRefcountedResourceOrThrow(
443 createTunnelResp.resourceId);
444
445 assertEquals(1, userRecord.mTunnelQuotaTracker.mCurrent);
446 verify(mMockNetd)
447 .addVirtualTunnelInterface(
448 eq(createTunnelResp.interfaceName),
449 eq(mSourceAddr),
450 eq(mDestinationAddr),
451 anyInt(),
452 anyInt());
453 }
454
455 @Test
456 public void testDeleteTunnelInterface() throws Exception {
457 IpSecTunnelInterfaceResponse createTunnelResp =
458 createAndValidateTunnel(mSourceAddr, mDestinationAddr);
459
460 IpSecService.UserRecord userRecord =
461 mIpSecService.mUserResourceTracker.getUserRecord(Os.getuid());
462
463 mIpSecService.deleteTunnelInterface(createTunnelResp.resourceId);
464
465 // Verify quota and RefcountedResource objects cleaned up
466 assertEquals(0, userRecord.mTunnelQuotaTracker.mCurrent);
467 verify(mMockNetd).removeVirtualTunnelInterface(eq(createTunnelResp.interfaceName));
468 try {
469 userRecord.mTunnelInterfaceRecords.getRefcountedResourceOrThrow(
470 createTunnelResp.resourceId);
471 fail("Expected IllegalArgumentException on attempt to access deleted resource");
472 } catch (IllegalArgumentException expected) {
473 }
474 }
475
476 @Test
477 public void testTunnelInterfaceBinderDeath() throws Exception {
478 IpSecTunnelInterfaceResponse createTunnelResp =
479 createAndValidateTunnel(mSourceAddr, mDestinationAddr);
480
481 IpSecService.UserRecord userRecord =
482 mIpSecService.mUserResourceTracker.getUserRecord(Os.getuid());
483 IpSecService.RefcountedResource refcountedRecord =
484 userRecord.mTunnelInterfaceRecords.getRefcountedResourceOrThrow(
485 createTunnelResp.resourceId);
486
487 refcountedRecord.binderDied();
488
489 // Verify quota and RefcountedResource objects cleaned up
490 assertEquals(0, userRecord.mTunnelQuotaTracker.mCurrent);
491 verify(mMockNetd).removeVirtualTunnelInterface(eq(createTunnelResp.interfaceName));
492 try {
493 userRecord.mTunnelInterfaceRecords.getRefcountedResourceOrThrow(
494 createTunnelResp.resourceId);
495 fail("Expected IllegalArgumentException on attempt to access deleted resource");
496 } catch (IllegalArgumentException expected) {
497 }
498 }
499
500 @Test
501 public void testAddRemoveAddressFromTunnelInterface() throws Exception {
502 IpSecTunnelInterfaceResponse createTunnelResp =
503 createAndValidateTunnel(mSourceAddr, mDestinationAddr);
504
505 mIpSecService.addAddressToTunnelInterface(createTunnelResp.resourceId, mLocalInnerAddress);
506 verify(mMockNetd)
507 .interfaceAddAddress(
508 eq(createTunnelResp.interfaceName),
509 eq(mLocalInnerAddress.getAddress().getHostAddress()),
510 eq(mLocalInnerAddress.getPrefixLength()));
511
512 mIpSecService.removeAddressFromTunnelInterface(
513 createTunnelResp.resourceId, mLocalInnerAddress);
514 verify(mMockNetd)
515 .interfaceDelAddress(
516 eq(createTunnelResp.interfaceName),
517 eq(mLocalInnerAddress.getAddress().getHostAddress()),
518 eq(mLocalInnerAddress.getPrefixLength()));
519 }
Nathan Harold2e9a5202017-09-26 11:44:23 -0700520}