Nathan Harold | 2e9a520 | 2017-09-26 11:44:23 -0700 | [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; |
| 18 | |
| 19 | import static org.junit.Assert.assertEquals; |
Benedict Wong | 0febe5e | 2017-08-22 21:42:33 -0700 | [diff] [blame] | 20 | import static org.junit.Assert.fail; |
Nathan Harold | 2e9a520 | 2017-09-26 11:44:23 -0700 | [diff] [blame] | 21 | import static org.mockito.Matchers.anyInt; |
| 22 | import static org.mockito.Matchers.anyLong; |
| 23 | import static org.mockito.Matchers.anyString; |
| 24 | import static org.mockito.Matchers.eq; |
| 25 | import static org.mockito.Mockito.mock; |
| 26 | import static org.mockito.Mockito.verify; |
| 27 | import static org.mockito.Mockito.when; |
| 28 | |
| 29 | import android.content.Context; |
| 30 | import android.net.INetd; |
| 31 | import android.net.IpSecAlgorithm; |
| 32 | import android.net.IpSecConfig; |
| 33 | import android.net.IpSecManager; |
| 34 | import android.net.IpSecSpiResponse; |
Nathan Harold | 2e9a520 | 2017-09-26 11:44:23 -0700 | [diff] [blame] | 35 | import android.net.IpSecTransformResponse; |
| 36 | import android.net.NetworkUtils; |
| 37 | import android.os.Binder; |
| 38 | import android.os.ParcelFileDescriptor; |
| 39 | import android.support.test.filters.SmallTest; |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 40 | import android.system.Os; |
Nathan Harold | 2e9a520 | 2017-09-26 11:44:23 -0700 | [diff] [blame] | 41 | |
| 42 | import java.net.Socket; |
| 43 | import java.util.Arrays; |
| 44 | import java.util.Collection; |
| 45 | |
| 46 | import org.junit.Before; |
| 47 | import org.junit.Test; |
| 48 | import org.junit.runner.RunWith; |
| 49 | import org.junit.runners.Parameterized; |
| 50 | |
| 51 | /** Unit tests for {@link IpSecService}. */ |
| 52 | @SmallTest |
| 53 | @RunWith(Parameterized.class) |
| 54 | public class IpSecServiceParameterizedTest { |
| 55 | |
Nathan Harold | a252331 | 2018-01-05 19:25:13 -0800 | [diff] [blame^] | 56 | private static final int TEST_SPI = 0xD1201D; |
Nathan Harold | 2e9a520 | 2017-09-26 11:44:23 -0700 | [diff] [blame] | 57 | |
Nathan Harold | a252331 | 2018-01-05 19:25:13 -0800 | [diff] [blame^] | 58 | private final String mDestinationAddr; |
Nathan Harold | 2e9a520 | 2017-09-26 11:44:23 -0700 | [diff] [blame] | 59 | |
| 60 | @Parameterized.Parameters |
| 61 | public static Collection ipSecConfigs() { |
| 62 | return Arrays.asList(new Object[][] {{"8.8.4.4"}, {"2601::10"}}); |
| 63 | } |
| 64 | |
Benedict Wong | 4ebc2c5 | 2017-11-01 17:14:25 -0700 | [diff] [blame] | 65 | private static final byte[] AEAD_KEY = { |
| 66 | 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, |
| 67 | 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, |
| 68 | 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, |
| 69 | 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, |
| 70 | 0x73, 0x61, 0x6C, 0x74 |
| 71 | }; |
Nathan Harold | 2e9a520 | 2017-09-26 11:44:23 -0700 | [diff] [blame] | 72 | private static final byte[] CRYPT_KEY = { |
| 73 | 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, |
| 74 | 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, |
| 75 | 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, |
| 76 | 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F |
| 77 | }; |
| 78 | private static final byte[] AUTH_KEY = { |
| 79 | 0x7A, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 80 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7F, |
| 81 | 0x7A, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 82 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7F |
| 83 | }; |
| 84 | |
| 85 | Context mMockContext; |
| 86 | INetd mMockNetd; |
| 87 | IpSecService.IpSecServiceConfiguration mMockIpSecSrvConfig; |
| 88 | IpSecService mIpSecService; |
| 89 | |
Benedict Wong | 0febe5e | 2017-08-22 21:42:33 -0700 | [diff] [blame] | 90 | private static final IpSecAlgorithm AUTH_ALGO = |
| 91 | new IpSecAlgorithm(IpSecAlgorithm.AUTH_HMAC_SHA256, AUTH_KEY, AUTH_KEY.length * 4); |
| 92 | private static final IpSecAlgorithm CRYPT_ALGO = |
| 93 | new IpSecAlgorithm(IpSecAlgorithm.CRYPT_AES_CBC, CRYPT_KEY); |
| 94 | private static final IpSecAlgorithm AEAD_ALGO = |
Benedict Wong | 4ebc2c5 | 2017-11-01 17:14:25 -0700 | [diff] [blame] | 95 | new IpSecAlgorithm(IpSecAlgorithm.AUTH_CRYPT_AES_GCM, AEAD_KEY, 128); |
Benedict Wong | 0febe5e | 2017-08-22 21:42:33 -0700 | [diff] [blame] | 96 | |
Nathan Harold | 2e9a520 | 2017-09-26 11:44:23 -0700 | [diff] [blame] | 97 | public IpSecServiceParameterizedTest(String remoteAddr) { |
Nathan Harold | a252331 | 2018-01-05 19:25:13 -0800 | [diff] [blame^] | 98 | mDestinationAddr = remoteAddr; |
Nathan Harold | 2e9a520 | 2017-09-26 11:44:23 -0700 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | @Before |
| 102 | public void setUp() throws Exception { |
| 103 | mMockContext = mock(Context.class); |
| 104 | mMockNetd = mock(INetd.class); |
| 105 | mMockIpSecSrvConfig = mock(IpSecService.IpSecServiceConfiguration.class); |
| 106 | mIpSecService = new IpSecService(mMockContext, mMockIpSecSrvConfig); |
| 107 | |
| 108 | // Injecting mock netd |
| 109 | when(mMockIpSecSrvConfig.getNetdInstance()).thenReturn(mMockNetd); |
| 110 | } |
| 111 | |
| 112 | @Test |
| 113 | public void testIpSecServiceReserveSpi() throws Exception { |
Nathan Harold | a252331 | 2018-01-05 19:25:13 -0800 | [diff] [blame^] | 114 | when(mMockNetd.ipSecAllocateSpi(anyInt(), anyString(), eq(mDestinationAddr), eq(TEST_SPI))) |
| 115 | .thenReturn(TEST_SPI); |
Nathan Harold | 2e9a520 | 2017-09-26 11:44:23 -0700 | [diff] [blame] | 116 | |
| 117 | IpSecSpiResponse spiResp = |
Jonathan Basseri | 5fb9290 | 2017-11-16 10:58:01 -0800 | [diff] [blame] | 118 | mIpSecService.allocateSecurityParameterIndex( |
Nathan Harold | a252331 | 2018-01-05 19:25:13 -0800 | [diff] [blame^] | 119 | mDestinationAddr, TEST_SPI, new Binder()); |
Nathan Harold | 2e9a520 | 2017-09-26 11:44:23 -0700 | [diff] [blame] | 120 | assertEquals(IpSecManager.Status.OK, spiResp.status); |
Nathan Harold | a252331 | 2018-01-05 19:25:13 -0800 | [diff] [blame^] | 121 | assertEquals(TEST_SPI, spiResp.spi); |
Nathan Harold | 2e9a520 | 2017-09-26 11:44:23 -0700 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | @Test |
| 125 | public void testReleaseSecurityParameterIndex() throws Exception { |
Nathan Harold | a252331 | 2018-01-05 19:25:13 -0800 | [diff] [blame^] | 126 | when(mMockNetd.ipSecAllocateSpi(anyInt(), anyString(), eq(mDestinationAddr), eq(TEST_SPI))) |
| 127 | .thenReturn(TEST_SPI); |
Nathan Harold | 2e9a520 | 2017-09-26 11:44:23 -0700 | [diff] [blame] | 128 | |
| 129 | IpSecSpiResponse spiResp = |
Jonathan Basseri | 5fb9290 | 2017-11-16 10:58:01 -0800 | [diff] [blame] | 130 | mIpSecService.allocateSecurityParameterIndex( |
Nathan Harold | a252331 | 2018-01-05 19:25:13 -0800 | [diff] [blame^] | 131 | mDestinationAddr, TEST_SPI, new Binder()); |
Nathan Harold | 2e9a520 | 2017-09-26 11:44:23 -0700 | [diff] [blame] | 132 | |
| 133 | mIpSecService.releaseSecurityParameterIndex(spiResp.resourceId); |
| 134 | |
| 135 | verify(mMockNetd) |
| 136 | .ipSecDeleteSecurityAssociation( |
Nathan Harold | a252331 | 2018-01-05 19:25:13 -0800 | [diff] [blame^] | 137 | eq(spiResp.resourceId), anyString(), anyString(), eq(TEST_SPI)); |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 138 | |
| 139 | // Verify quota and RefcountedResource objects cleaned up |
| 140 | IpSecService.UserRecord userRecord = |
| 141 | mIpSecService.mUserResourceTracker.getUserRecord(Os.getuid()); |
| 142 | assertEquals(0, userRecord.mSpiQuotaTracker.mCurrent); |
| 143 | try { |
| 144 | userRecord.mSpiRecords.getRefcountedResourceOrThrow(spiResp.resourceId); |
| 145 | fail("Expected IllegalArgumentException on attempt to access deleted resource"); |
| 146 | } catch (IllegalArgumentException expected) { |
| 147 | |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | @Test |
| 152 | public void testSecurityParameterIndexBinderDeath() throws Exception { |
Nathan Harold | a252331 | 2018-01-05 19:25:13 -0800 | [diff] [blame^] | 153 | when(mMockNetd.ipSecAllocateSpi(anyInt(), anyString(), eq(mDestinationAddr), eq(TEST_SPI))) |
| 154 | .thenReturn(TEST_SPI); |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 155 | |
| 156 | IpSecSpiResponse spiResp = |
Nathan Harold | 660a335 | 2017-12-14 14:46:46 -0800 | [diff] [blame] | 157 | mIpSecService.allocateSecurityParameterIndex( |
Nathan Harold | a252331 | 2018-01-05 19:25:13 -0800 | [diff] [blame^] | 158 | mDestinationAddr, TEST_SPI, new Binder()); |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 159 | |
| 160 | IpSecService.UserRecord userRecord = |
| 161 | mIpSecService.mUserResourceTracker.getUserRecord(Os.getuid()); |
| 162 | IpSecService.RefcountedResource refcountedRecord = |
| 163 | userRecord.mSpiRecords.getRefcountedResourceOrThrow(spiResp.resourceId); |
| 164 | |
| 165 | refcountedRecord.binderDied(); |
| 166 | |
| 167 | verify(mMockNetd) |
| 168 | .ipSecDeleteSecurityAssociation( |
Nathan Harold | a252331 | 2018-01-05 19:25:13 -0800 | [diff] [blame^] | 169 | eq(spiResp.resourceId), anyString(), anyString(), eq(TEST_SPI)); |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 170 | |
| 171 | // Verify quota and RefcountedResource objects cleaned up |
| 172 | assertEquals(0, userRecord.mSpiQuotaTracker.mCurrent); |
| 173 | try { |
| 174 | userRecord.mSpiRecords.getRefcountedResourceOrThrow(spiResp.resourceId); |
| 175 | fail("Expected IllegalArgumentException on attempt to access deleted resource"); |
| 176 | } catch (IllegalArgumentException expected) { |
| 177 | |
| 178 | } |
Nathan Harold | 2e9a520 | 2017-09-26 11:44:23 -0700 | [diff] [blame] | 179 | } |
| 180 | |
Nathan Harold | a252331 | 2018-01-05 19:25:13 -0800 | [diff] [blame^] | 181 | private int getNewSpiResourceId(String remoteAddress, int returnSpi) throws Exception { |
| 182 | when(mMockNetd.ipSecAllocateSpi(anyInt(), anyString(), anyString(), anyInt())) |
Benedict Wong | 0febe5e | 2017-08-22 21:42:33 -0700 | [diff] [blame] | 183 | .thenReturn(returnSpi); |
Nathan Harold | 2e9a520 | 2017-09-26 11:44:23 -0700 | [diff] [blame] | 184 | |
Benedict Wong | 0febe5e | 2017-08-22 21:42:33 -0700 | [diff] [blame] | 185 | IpSecSpiResponse spi = |
Jonathan Basseri | 5fb9290 | 2017-11-16 10:58:01 -0800 | [diff] [blame] | 186 | mIpSecService.allocateSecurityParameterIndex( |
Benedict Wong | 0febe5e | 2017-08-22 21:42:33 -0700 | [diff] [blame] | 187 | NetworkUtils.numericToInetAddress(remoteAddress).getHostAddress(), |
| 188 | IpSecManager.INVALID_SECURITY_PARAMETER_INDEX, |
| 189 | new Binder()); |
| 190 | return spi.resourceId; |
| 191 | } |
Nathan Harold | 2e9a520 | 2017-09-26 11:44:23 -0700 | [diff] [blame] | 192 | |
Benedict Wong | 0febe5e | 2017-08-22 21:42:33 -0700 | [diff] [blame] | 193 | private void addDefaultSpisAndRemoteAddrToIpSecConfig(IpSecConfig config) throws Exception { |
Nathan Harold | a252331 | 2018-01-05 19:25:13 -0800 | [diff] [blame^] | 194 | config.setSpiResourceId(getNewSpiResourceId(mDestinationAddr, TEST_SPI)); |
| 195 | config.setDestinationAddress(mDestinationAddr); |
Benedict Wong | 0febe5e | 2017-08-22 21:42:33 -0700 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | private void addAuthAndCryptToIpSecConfig(IpSecConfig config) throws Exception { |
Nathan Harold | a252331 | 2018-01-05 19:25:13 -0800 | [diff] [blame^] | 199 | config.setEncryption(CRYPT_ALGO); |
| 200 | config.setAuthentication(AUTH_ALGO); |
Nathan Harold | 2e9a520 | 2017-09-26 11:44:23 -0700 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | @Test |
| 204 | public void testCreateTransportModeTransform() throws Exception { |
Benedict Wong | 0febe5e | 2017-08-22 21:42:33 -0700 | [diff] [blame] | 205 | IpSecConfig ipSecConfig = new IpSecConfig(); |
| 206 | addDefaultSpisAndRemoteAddrToIpSecConfig(ipSecConfig); |
| 207 | addAuthAndCryptToIpSecConfig(ipSecConfig); |
Nathan Harold | 2e9a520 | 2017-09-26 11:44:23 -0700 | [diff] [blame] | 208 | |
| 209 | IpSecTransformResponse createTransformResp = |
| 210 | mIpSecService.createTransportModeTransform(ipSecConfig, new Binder()); |
| 211 | assertEquals(IpSecManager.Status.OK, createTransformResp.status); |
| 212 | |
| 213 | verify(mMockNetd) |
| 214 | .ipSecAddSecurityAssociation( |
| 215 | eq(createTransformResp.resourceId), |
| 216 | anyInt(), |
Nathan Harold | 2e9a520 | 2017-09-26 11:44:23 -0700 | [diff] [blame] | 217 | anyString(), |
| 218 | anyString(), |
| 219 | anyLong(), |
Nathan Harold | a252331 | 2018-01-05 19:25:13 -0800 | [diff] [blame^] | 220 | eq(TEST_SPI), |
Benedict Wong | 0febe5e | 2017-08-22 21:42:33 -0700 | [diff] [blame] | 221 | eq(IpSecAlgorithm.AUTH_HMAC_SHA256), |
| 222 | eq(AUTH_KEY), |
| 223 | anyInt(), |
| 224 | eq(IpSecAlgorithm.CRYPT_AES_CBC), |
| 225 | eq(CRYPT_KEY), |
| 226 | anyInt(), |
| 227 | eq(""), |
Manoj Boopathi Raj | fffa811 | 2017-10-26 11:49:02 -0700 | [diff] [blame] | 228 | eq(new byte[] {}), |
Benedict Wong | 0febe5e | 2017-08-22 21:42:33 -0700 | [diff] [blame] | 229 | eq(0), |
| 230 | anyInt(), |
| 231 | anyInt(), |
| 232 | anyInt()); |
| 233 | } |
| 234 | |
| 235 | @Test |
| 236 | public void testCreateTransportModeTransformAead() throws Exception { |
| 237 | IpSecConfig ipSecConfig = new IpSecConfig(); |
| 238 | addDefaultSpisAndRemoteAddrToIpSecConfig(ipSecConfig); |
| 239 | |
Nathan Harold | a252331 | 2018-01-05 19:25:13 -0800 | [diff] [blame^] | 240 | ipSecConfig.setAuthenticatedEncryption(AEAD_ALGO); |
Benedict Wong | 0febe5e | 2017-08-22 21:42:33 -0700 | [diff] [blame] | 241 | |
| 242 | IpSecTransformResponse createTransformResp = |
| 243 | mIpSecService.createTransportModeTransform(ipSecConfig, new Binder()); |
| 244 | assertEquals(IpSecManager.Status.OK, createTransformResp.status); |
| 245 | |
| 246 | verify(mMockNetd) |
| 247 | .ipSecAddSecurityAssociation( |
| 248 | eq(createTransformResp.resourceId), |
| 249 | anyInt(), |
Benedict Wong | 0febe5e | 2017-08-22 21:42:33 -0700 | [diff] [blame] | 250 | anyString(), |
| 251 | anyString(), |
| 252 | anyLong(), |
Nathan Harold | a252331 | 2018-01-05 19:25:13 -0800 | [diff] [blame^] | 253 | eq(TEST_SPI), |
Benedict Wong | 0febe5e | 2017-08-22 21:42:33 -0700 | [diff] [blame] | 254 | eq(""), |
Manoj Boopathi Raj | fffa811 | 2017-10-26 11:49:02 -0700 | [diff] [blame] | 255 | eq(new byte[] {}), |
Benedict Wong | 0febe5e | 2017-08-22 21:42:33 -0700 | [diff] [blame] | 256 | eq(0), |
| 257 | eq(""), |
Manoj Boopathi Raj | fffa811 | 2017-10-26 11:49:02 -0700 | [diff] [blame] | 258 | eq(new byte[] {}), |
Benedict Wong | 0febe5e | 2017-08-22 21:42:33 -0700 | [diff] [blame] | 259 | eq(0), |
| 260 | eq(IpSecAlgorithm.AUTH_CRYPT_AES_GCM), |
Benedict Wong | 4ebc2c5 | 2017-11-01 17:14:25 -0700 | [diff] [blame] | 261 | eq(AEAD_KEY), |
Nathan Harold | 2e9a520 | 2017-09-26 11:44:23 -0700 | [diff] [blame] | 262 | anyInt(), |
| 263 | anyInt(), |
| 264 | anyInt(), |
| 265 | anyInt()); |
| 266 | } |
| 267 | |
| 268 | @Test |
| 269 | public void testDeleteTransportModeTransform() throws Exception { |
Benedict Wong | 0febe5e | 2017-08-22 21:42:33 -0700 | [diff] [blame] | 270 | IpSecConfig ipSecConfig = new IpSecConfig(); |
| 271 | addDefaultSpisAndRemoteAddrToIpSecConfig(ipSecConfig); |
| 272 | addAuthAndCryptToIpSecConfig(ipSecConfig); |
Nathan Harold | 2e9a520 | 2017-09-26 11:44:23 -0700 | [diff] [blame] | 273 | |
| 274 | IpSecTransformResponse createTransformResp = |
| 275 | mIpSecService.createTransportModeTransform(ipSecConfig, new Binder()); |
| 276 | mIpSecService.deleteTransportModeTransform(createTransformResp.resourceId); |
| 277 | |
| 278 | verify(mMockNetd) |
| 279 | .ipSecDeleteSecurityAssociation( |
Nathan Harold | a252331 | 2018-01-05 19:25:13 -0800 | [diff] [blame^] | 280 | eq(createTransformResp.resourceId), anyString(), anyString(), eq(TEST_SPI)); |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 281 | |
| 282 | // Verify quota and RefcountedResource objects cleaned up |
| 283 | IpSecService.UserRecord userRecord = |
| 284 | mIpSecService.mUserResourceTracker.getUserRecord(Os.getuid()); |
| 285 | assertEquals(0, userRecord.mTransformQuotaTracker.mCurrent); |
| 286 | try { |
| 287 | userRecord.mTransformRecords.getRefcountedResourceOrThrow( |
| 288 | createTransformResp.resourceId); |
| 289 | fail("Expected IllegalArgumentException on attempt to access deleted resource"); |
| 290 | } catch (IllegalArgumentException expected) { |
| 291 | |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | @Test |
| 296 | public void testTransportModeTransformBinderDeath() throws Exception { |
| 297 | IpSecConfig ipSecConfig = new IpSecConfig(); |
| 298 | addDefaultSpisAndRemoteAddrToIpSecConfig(ipSecConfig); |
| 299 | addAuthAndCryptToIpSecConfig(ipSecConfig); |
| 300 | |
| 301 | IpSecTransformResponse createTransformResp = |
| 302 | mIpSecService.createTransportModeTransform(ipSecConfig, new Binder()); |
| 303 | |
| 304 | IpSecService.UserRecord userRecord = |
| 305 | mIpSecService.mUserResourceTracker.getUserRecord(Os.getuid()); |
| 306 | IpSecService.RefcountedResource refcountedRecord = |
| 307 | userRecord.mTransformRecords.getRefcountedResourceOrThrow( |
| 308 | createTransformResp.resourceId); |
| 309 | |
| 310 | refcountedRecord.binderDied(); |
| 311 | |
| 312 | verify(mMockNetd) |
| 313 | .ipSecDeleteSecurityAssociation( |
Nathan Harold | a252331 | 2018-01-05 19:25:13 -0800 | [diff] [blame^] | 314 | eq(createTransformResp.resourceId), anyString(), anyString(), eq(TEST_SPI)); |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 315 | |
| 316 | // Verify quota and RefcountedResource objects cleaned up |
| 317 | assertEquals(0, userRecord.mTransformQuotaTracker.mCurrent); |
| 318 | try { |
| 319 | userRecord.mTransformRecords.getRefcountedResourceOrThrow( |
| 320 | createTransformResp.resourceId); |
| 321 | fail("Expected IllegalArgumentException on attempt to access deleted resource"); |
| 322 | } catch (IllegalArgumentException expected) { |
| 323 | |
| 324 | } |
Nathan Harold | 2e9a520 | 2017-09-26 11:44:23 -0700 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | @Test |
| 328 | public void testApplyTransportModeTransform() throws Exception { |
Benedict Wong | 0febe5e | 2017-08-22 21:42:33 -0700 | [diff] [blame] | 329 | IpSecConfig ipSecConfig = new IpSecConfig(); |
| 330 | addDefaultSpisAndRemoteAddrToIpSecConfig(ipSecConfig); |
| 331 | addAuthAndCryptToIpSecConfig(ipSecConfig); |
Nathan Harold | 2e9a520 | 2017-09-26 11:44:23 -0700 | [diff] [blame] | 332 | |
| 333 | IpSecTransformResponse createTransformResp = |
| 334 | mIpSecService.createTransportModeTransform(ipSecConfig, new Binder()); |
| 335 | ParcelFileDescriptor pfd = ParcelFileDescriptor.fromSocket(new Socket()); |
| 336 | |
| 337 | int resourceId = createTransformResp.resourceId; |
Nathan Harold | a252331 | 2018-01-05 19:25:13 -0800 | [diff] [blame^] | 338 | mIpSecService.applyTransportModeTransform(pfd, IpSecManager.DIRECTION_OUT, resourceId); |
Nathan Harold | 2e9a520 | 2017-09-26 11:44:23 -0700 | [diff] [blame] | 339 | |
| 340 | verify(mMockNetd) |
| 341 | .ipSecApplyTransportModeTransform( |
| 342 | eq(pfd.getFileDescriptor()), |
| 343 | eq(resourceId), |
Nathan Harold | a252331 | 2018-01-05 19:25:13 -0800 | [diff] [blame^] | 344 | eq(IpSecManager.DIRECTION_OUT), |
Nathan Harold | 2e9a520 | 2017-09-26 11:44:23 -0700 | [diff] [blame] | 345 | anyString(), |
| 346 | anyString(), |
Nathan Harold | a252331 | 2018-01-05 19:25:13 -0800 | [diff] [blame^] | 347 | eq(TEST_SPI)); |
Nathan Harold | 2e9a520 | 2017-09-26 11:44:23 -0700 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | @Test |
| 351 | public void testRemoveTransportModeTransform() throws Exception { |
| 352 | ParcelFileDescriptor pfd = ParcelFileDescriptor.fromSocket(new Socket()); |
Nathan Harold | a252331 | 2018-01-05 19:25:13 -0800 | [diff] [blame^] | 353 | mIpSecService.removeTransportModeTransforms(pfd, 1); |
Nathan Harold | 2e9a520 | 2017-09-26 11:44:23 -0700 | [diff] [blame] | 354 | |
| 355 | verify(mMockNetd).ipSecRemoveTransportModeTransform(pfd.getFileDescriptor()); |
| 356 | } |
| 357 | } |