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