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; |
Nathan Harold | 2e9a520 | 2017-09-26 11:44:23 -0700 | [diff] [blame] | 22 | import static org.mockito.Matchers.anyString; |
| 23 | import static org.mockito.Matchers.eq; |
| 24 | import static org.mockito.Mockito.mock; |
| 25 | import static org.mockito.Mockito.verify; |
| 26 | import static org.mockito.Mockito.when; |
| 27 | |
| 28 | import android.content.Context; |
| 29 | import android.net.INetd; |
| 30 | import android.net.IpSecAlgorithm; |
| 31 | import android.net.IpSecConfig; |
| 32 | import android.net.IpSecManager; |
| 33 | import android.net.IpSecSpiResponse; |
Nathan Harold | 2e9a520 | 2017-09-26 11:44:23 -0700 | [diff] [blame] | 34 | import android.net.IpSecTransformResponse; |
| 35 | import android.net.NetworkUtils; |
| 36 | import android.os.Binder; |
| 37 | import android.os.ParcelFileDescriptor; |
| 38 | import android.support.test.filters.SmallTest; |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 39 | import android.system.Os; |
Nathan Harold | 2e9a520 | 2017-09-26 11:44:23 -0700 | [diff] [blame] | 40 | |
| 41 | import java.net.Socket; |
| 42 | import java.util.Arrays; |
| 43 | import java.util.Collection; |
| 44 | |
| 45 | import org.junit.Before; |
| 46 | import org.junit.Test; |
| 47 | import org.junit.runner.RunWith; |
| 48 | import org.junit.runners.Parameterized; |
| 49 | |
| 50 | /** Unit tests for {@link IpSecService}. */ |
| 51 | @SmallTest |
| 52 | @RunWith(Parameterized.class) |
| 53 | public class IpSecServiceParameterizedTest { |
| 54 | |
Nathan Harold | a252331 | 2018-01-05 19:25:13 -0800 | [diff] [blame] | 55 | private static final int TEST_SPI = 0xD1201D; |
Nathan Harold | 2e9a520 | 2017-09-26 11:44:23 -0700 | [diff] [blame] | 56 | |
Nathan Harold | a252331 | 2018-01-05 19:25:13 -0800 | [diff] [blame] | 57 | private final String mDestinationAddr; |
Nathan Harold | 5676f5f | 2018-01-16 19:34:01 -0800 | [diff] [blame] | 58 | private final String mSourceAddr; |
Nathan Harold | 2e9a520 | 2017-09-26 11:44:23 -0700 | [diff] [blame] | 59 | |
| 60 | @Parameterized.Parameters |
| 61 | public static Collection ipSecConfigs() { |
Nathan Harold | 5676f5f | 2018-01-16 19:34:01 -0800 | [diff] [blame] | 62 | 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] | 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 | 5676f5f | 2018-01-16 19:34:01 -0800 | [diff] [blame] | 97 | public IpSecServiceParameterizedTest(String sourceAddr, String destAddr) { |
| 98 | mSourceAddr = sourceAddr; |
| 99 | mDestinationAddr = destAddr; |
Nathan Harold | 2e9a520 | 2017-09-26 11:44:23 -0700 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | @Before |
| 103 | public void setUp() throws Exception { |
| 104 | mMockContext = mock(Context.class); |
| 105 | mMockNetd = mock(INetd.class); |
| 106 | mMockIpSecSrvConfig = mock(IpSecService.IpSecServiceConfiguration.class); |
| 107 | mIpSecService = new IpSecService(mMockContext, mMockIpSecSrvConfig); |
| 108 | |
| 109 | // Injecting mock netd |
| 110 | when(mMockIpSecSrvConfig.getNetdInstance()).thenReturn(mMockNetd); |
| 111 | } |
| 112 | |
| 113 | @Test |
| 114 | public void testIpSecServiceReserveSpi() throws Exception { |
Nathan Harold | a252331 | 2018-01-05 19:25:13 -0800 | [diff] [blame] | 115 | when(mMockNetd.ipSecAllocateSpi(anyInt(), anyString(), eq(mDestinationAddr), eq(TEST_SPI))) |
| 116 | .thenReturn(TEST_SPI); |
Nathan Harold | 2e9a520 | 2017-09-26 11:44:23 -0700 | [diff] [blame] | 117 | |
| 118 | IpSecSpiResponse spiResp = |
Jonathan Basseri | 5fb9290 | 2017-11-16 10:58:01 -0800 | [diff] [blame] | 119 | mIpSecService.allocateSecurityParameterIndex( |
Nathan Harold | a252331 | 2018-01-05 19:25:13 -0800 | [diff] [blame] | 120 | mDestinationAddr, TEST_SPI, new Binder()); |
Nathan Harold | 2e9a520 | 2017-09-26 11:44:23 -0700 | [diff] [blame] | 121 | assertEquals(IpSecManager.Status.OK, spiResp.status); |
Nathan Harold | a252331 | 2018-01-05 19:25:13 -0800 | [diff] [blame] | 122 | assertEquals(TEST_SPI, spiResp.spi); |
Nathan Harold | 2e9a520 | 2017-09-26 11:44:23 -0700 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | @Test |
| 126 | public void testReleaseSecurityParameterIndex() throws Exception { |
Nathan Harold | a252331 | 2018-01-05 19:25:13 -0800 | [diff] [blame] | 127 | when(mMockNetd.ipSecAllocateSpi(anyInt(), anyString(), eq(mDestinationAddr), eq(TEST_SPI))) |
| 128 | .thenReturn(TEST_SPI); |
Nathan Harold | 2e9a520 | 2017-09-26 11:44:23 -0700 | [diff] [blame] | 129 | |
| 130 | IpSecSpiResponse spiResp = |
Jonathan Basseri | 5fb9290 | 2017-11-16 10:58:01 -0800 | [diff] [blame] | 131 | mIpSecService.allocateSecurityParameterIndex( |
Nathan Harold | a252331 | 2018-01-05 19:25:13 -0800 | [diff] [blame] | 132 | mDestinationAddr, TEST_SPI, new Binder()); |
Nathan Harold | 2e9a520 | 2017-09-26 11:44:23 -0700 | [diff] [blame] | 133 | |
| 134 | mIpSecService.releaseSecurityParameterIndex(spiResp.resourceId); |
| 135 | |
| 136 | verify(mMockNetd) |
| 137 | .ipSecDeleteSecurityAssociation( |
Di Lu | 0b611f4 | 2018-01-11 11:35:25 -0800 | [diff] [blame] | 138 | eq(spiResp.resourceId), |
| 139 | anyString(), |
| 140 | anyString(), |
| 141 | eq(TEST_SPI), |
| 142 | anyInt(), |
| 143 | anyInt()); |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 144 | |
| 145 | // Verify quota and RefcountedResource objects cleaned up |
| 146 | IpSecService.UserRecord userRecord = |
| 147 | mIpSecService.mUserResourceTracker.getUserRecord(Os.getuid()); |
| 148 | assertEquals(0, userRecord.mSpiQuotaTracker.mCurrent); |
| 149 | try { |
| 150 | userRecord.mSpiRecords.getRefcountedResourceOrThrow(spiResp.resourceId); |
| 151 | fail("Expected IllegalArgumentException on attempt to access deleted resource"); |
| 152 | } catch (IllegalArgumentException expected) { |
| 153 | |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | @Test |
| 158 | public void testSecurityParameterIndexBinderDeath() throws Exception { |
Nathan Harold | a252331 | 2018-01-05 19:25:13 -0800 | [diff] [blame] | 159 | when(mMockNetd.ipSecAllocateSpi(anyInt(), anyString(), eq(mDestinationAddr), eq(TEST_SPI))) |
| 160 | .thenReturn(TEST_SPI); |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 161 | |
| 162 | IpSecSpiResponse spiResp = |
Nathan Harold | 660a335 | 2017-12-14 14:46:46 -0800 | [diff] [blame] | 163 | mIpSecService.allocateSecurityParameterIndex( |
Nathan Harold | a252331 | 2018-01-05 19:25:13 -0800 | [diff] [blame] | 164 | mDestinationAddr, TEST_SPI, new Binder()); |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 165 | |
| 166 | IpSecService.UserRecord userRecord = |
| 167 | mIpSecService.mUserResourceTracker.getUserRecord(Os.getuid()); |
| 168 | IpSecService.RefcountedResource refcountedRecord = |
| 169 | userRecord.mSpiRecords.getRefcountedResourceOrThrow(spiResp.resourceId); |
| 170 | |
| 171 | refcountedRecord.binderDied(); |
| 172 | |
| 173 | verify(mMockNetd) |
| 174 | .ipSecDeleteSecurityAssociation( |
Di Lu | 0b611f4 | 2018-01-11 11:35:25 -0800 | [diff] [blame] | 175 | eq(spiResp.resourceId), |
| 176 | anyString(), |
| 177 | anyString(), |
| 178 | eq(TEST_SPI), |
| 179 | anyInt(), |
| 180 | anyInt()); |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 181 | |
| 182 | // Verify quota and RefcountedResource objects cleaned up |
| 183 | assertEquals(0, userRecord.mSpiQuotaTracker.mCurrent); |
| 184 | try { |
| 185 | userRecord.mSpiRecords.getRefcountedResourceOrThrow(spiResp.resourceId); |
| 186 | fail("Expected IllegalArgumentException on attempt to access deleted resource"); |
| 187 | } catch (IllegalArgumentException expected) { |
| 188 | |
| 189 | } |
Nathan Harold | 2e9a520 | 2017-09-26 11:44:23 -0700 | [diff] [blame] | 190 | } |
| 191 | |
Nathan Harold | a252331 | 2018-01-05 19:25:13 -0800 | [diff] [blame] | 192 | private int getNewSpiResourceId(String remoteAddress, int returnSpi) throws Exception { |
| 193 | when(mMockNetd.ipSecAllocateSpi(anyInt(), anyString(), anyString(), anyInt())) |
Benedict Wong | 0febe5e | 2017-08-22 21:42:33 -0700 | [diff] [blame] | 194 | .thenReturn(returnSpi); |
Nathan Harold | 2e9a520 | 2017-09-26 11:44:23 -0700 | [diff] [blame] | 195 | |
Benedict Wong | 0febe5e | 2017-08-22 21:42:33 -0700 | [diff] [blame] | 196 | IpSecSpiResponse spi = |
Jonathan Basseri | 5fb9290 | 2017-11-16 10:58:01 -0800 | [diff] [blame] | 197 | mIpSecService.allocateSecurityParameterIndex( |
Benedict Wong | 0febe5e | 2017-08-22 21:42:33 -0700 | [diff] [blame] | 198 | NetworkUtils.numericToInetAddress(remoteAddress).getHostAddress(), |
| 199 | IpSecManager.INVALID_SECURITY_PARAMETER_INDEX, |
| 200 | new Binder()); |
| 201 | return spi.resourceId; |
| 202 | } |
Nathan Harold | 2e9a520 | 2017-09-26 11:44:23 -0700 | [diff] [blame] | 203 | |
Benedict Wong | 0febe5e | 2017-08-22 21:42:33 -0700 | [diff] [blame] | 204 | private void addDefaultSpisAndRemoteAddrToIpSecConfig(IpSecConfig config) throws Exception { |
Nathan Harold | a252331 | 2018-01-05 19:25:13 -0800 | [diff] [blame] | 205 | config.setSpiResourceId(getNewSpiResourceId(mDestinationAddr, TEST_SPI)); |
Nathan Harold | 5676f5f | 2018-01-16 19:34:01 -0800 | [diff] [blame] | 206 | config.setSourceAddress(mSourceAddr); |
Nathan Harold | a252331 | 2018-01-05 19:25:13 -0800 | [diff] [blame] | 207 | config.setDestinationAddress(mDestinationAddr); |
Benedict Wong | 0febe5e | 2017-08-22 21:42:33 -0700 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | private void addAuthAndCryptToIpSecConfig(IpSecConfig config) throws Exception { |
Nathan Harold | a252331 | 2018-01-05 19:25:13 -0800 | [diff] [blame] | 211 | config.setEncryption(CRYPT_ALGO); |
| 212 | config.setAuthentication(AUTH_ALGO); |
Nathan Harold | 2e9a520 | 2017-09-26 11:44:23 -0700 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | @Test |
Benedict Wong | f33f0313 | 2018-01-18 14:38:16 -0800 | [diff] [blame] | 216 | public void testCreateTransform() throws Exception { |
Benedict Wong | 0febe5e | 2017-08-22 21:42:33 -0700 | [diff] [blame] | 217 | IpSecConfig ipSecConfig = new IpSecConfig(); |
| 218 | addDefaultSpisAndRemoteAddrToIpSecConfig(ipSecConfig); |
| 219 | addAuthAndCryptToIpSecConfig(ipSecConfig); |
Nathan Harold | 2e9a520 | 2017-09-26 11:44:23 -0700 | [diff] [blame] | 220 | |
| 221 | IpSecTransformResponse createTransformResp = |
Benedict Wong | f33f0313 | 2018-01-18 14:38:16 -0800 | [diff] [blame] | 222 | mIpSecService.createTransform(ipSecConfig, new Binder()); |
Nathan Harold | 2e9a520 | 2017-09-26 11:44:23 -0700 | [diff] [blame] | 223 | assertEquals(IpSecManager.Status.OK, createTransformResp.status); |
| 224 | |
| 225 | verify(mMockNetd) |
| 226 | .ipSecAddSecurityAssociation( |
| 227 | eq(createTransformResp.resourceId), |
| 228 | anyInt(), |
Nathan Harold | 2e9a520 | 2017-09-26 11:44:23 -0700 | [diff] [blame] | 229 | anyString(), |
| 230 | anyString(), |
Benedict Wong | 0fe58a9 | 2018-01-19 17:36:02 -0800 | [diff] [blame] | 231 | anyInt(), |
Nathan Harold | a252331 | 2018-01-05 19:25:13 -0800 | [diff] [blame] | 232 | eq(TEST_SPI), |
Di Lu | 0b611f4 | 2018-01-11 11:35:25 -0800 | [diff] [blame] | 233 | anyInt(), |
| 234 | anyInt(), |
Benedict Wong | 0febe5e | 2017-08-22 21:42:33 -0700 | [diff] [blame] | 235 | eq(IpSecAlgorithm.AUTH_HMAC_SHA256), |
| 236 | eq(AUTH_KEY), |
| 237 | anyInt(), |
| 238 | eq(IpSecAlgorithm.CRYPT_AES_CBC), |
| 239 | eq(CRYPT_KEY), |
| 240 | anyInt(), |
| 241 | eq(""), |
Manoj Boopathi Raj | fffa811 | 2017-10-26 11:49:02 -0700 | [diff] [blame] | 242 | eq(new byte[] {}), |
Benedict Wong | 0febe5e | 2017-08-22 21:42:33 -0700 | [diff] [blame] | 243 | eq(0), |
| 244 | anyInt(), |
| 245 | anyInt(), |
| 246 | anyInt()); |
| 247 | } |
| 248 | |
| 249 | @Test |
Benedict Wong | f33f0313 | 2018-01-18 14:38:16 -0800 | [diff] [blame] | 250 | public void testCreateTransformAead() throws Exception { |
Benedict Wong | 0febe5e | 2017-08-22 21:42:33 -0700 | [diff] [blame] | 251 | IpSecConfig ipSecConfig = new IpSecConfig(); |
| 252 | addDefaultSpisAndRemoteAddrToIpSecConfig(ipSecConfig); |
| 253 | |
Nathan Harold | a252331 | 2018-01-05 19:25:13 -0800 | [diff] [blame] | 254 | ipSecConfig.setAuthenticatedEncryption(AEAD_ALGO); |
Benedict Wong | 0febe5e | 2017-08-22 21:42:33 -0700 | [diff] [blame] | 255 | |
| 256 | IpSecTransformResponse createTransformResp = |
Benedict Wong | f33f0313 | 2018-01-18 14:38:16 -0800 | [diff] [blame] | 257 | mIpSecService.createTransform(ipSecConfig, new Binder()); |
Benedict Wong | 0febe5e | 2017-08-22 21:42:33 -0700 | [diff] [blame] | 258 | assertEquals(IpSecManager.Status.OK, createTransformResp.status); |
| 259 | |
| 260 | verify(mMockNetd) |
| 261 | .ipSecAddSecurityAssociation( |
| 262 | eq(createTransformResp.resourceId), |
| 263 | anyInt(), |
Benedict Wong | 0febe5e | 2017-08-22 21:42:33 -0700 | [diff] [blame] | 264 | anyString(), |
| 265 | anyString(), |
Benedict Wong | 0fe58a9 | 2018-01-19 17:36:02 -0800 | [diff] [blame] | 266 | anyInt(), |
Nathan Harold | a252331 | 2018-01-05 19:25:13 -0800 | [diff] [blame] | 267 | eq(TEST_SPI), |
Di Lu | 0b611f4 | 2018-01-11 11:35:25 -0800 | [diff] [blame] | 268 | anyInt(), |
| 269 | anyInt(), |
Benedict Wong | 0febe5e | 2017-08-22 21:42:33 -0700 | [diff] [blame] | 270 | eq(""), |
Manoj Boopathi Raj | fffa811 | 2017-10-26 11:49:02 -0700 | [diff] [blame] | 271 | eq(new byte[] {}), |
Benedict Wong | 0febe5e | 2017-08-22 21:42:33 -0700 | [diff] [blame] | 272 | eq(0), |
| 273 | eq(""), |
Manoj Boopathi Raj | fffa811 | 2017-10-26 11:49:02 -0700 | [diff] [blame] | 274 | eq(new byte[] {}), |
Benedict Wong | 0febe5e | 2017-08-22 21:42:33 -0700 | [diff] [blame] | 275 | eq(0), |
| 276 | eq(IpSecAlgorithm.AUTH_CRYPT_AES_GCM), |
Benedict Wong | 4ebc2c5 | 2017-11-01 17:14:25 -0700 | [diff] [blame] | 277 | eq(AEAD_KEY), |
Nathan Harold | 2e9a520 | 2017-09-26 11:44:23 -0700 | [diff] [blame] | 278 | anyInt(), |
| 279 | anyInt(), |
| 280 | anyInt(), |
| 281 | anyInt()); |
| 282 | } |
| 283 | |
Andreas Gampe | a7b26b5 | 2018-02-26 08:06:30 -0800 | [diff] [blame^] | 284 | @Test |
Benedict Wong | e6b4277 | 2017-12-13 18:26:40 -0800 | [diff] [blame] | 285 | public void testCreateTwoTransformsWithSameSpis() throws Exception { |
| 286 | IpSecConfig ipSecConfig = new IpSecConfig(); |
| 287 | addDefaultSpisAndRemoteAddrToIpSecConfig(ipSecConfig); |
| 288 | addAuthAndCryptToIpSecConfig(ipSecConfig); |
| 289 | |
| 290 | IpSecTransformResponse createTransformResp = |
| 291 | mIpSecService.createTransform(ipSecConfig, new Binder()); |
| 292 | assertEquals(IpSecManager.Status.OK, createTransformResp.status); |
| 293 | |
| 294 | // Attempting to create transform a second time with the same SPIs should throw an error... |
| 295 | try { |
| 296 | mIpSecService.createTransform(ipSecConfig, new Binder()); |
| 297 | fail("IpSecService should have thrown an error for reuse of SPI"); |
| 298 | } catch (IllegalStateException expected) { |
| 299 | } |
| 300 | |
| 301 | // ... even if the transform is deleted |
| 302 | mIpSecService.deleteTransform(createTransformResp.resourceId); |
| 303 | try { |
| 304 | mIpSecService.createTransform(ipSecConfig, new Binder()); |
| 305 | fail("IpSecService should have thrown an error for reuse of SPI"); |
| 306 | } catch (IllegalStateException expected) { |
| 307 | } |
| 308 | } |
| 309 | |
Nathan Harold | 2e9a520 | 2017-09-26 11:44:23 -0700 | [diff] [blame] | 310 | @Test |
Benedict Wong | f33f0313 | 2018-01-18 14:38:16 -0800 | [diff] [blame] | 311 | public void testDeleteTransform() throws Exception { |
Benedict Wong | 0febe5e | 2017-08-22 21:42:33 -0700 | [diff] [blame] | 312 | IpSecConfig ipSecConfig = new IpSecConfig(); |
| 313 | addDefaultSpisAndRemoteAddrToIpSecConfig(ipSecConfig); |
| 314 | addAuthAndCryptToIpSecConfig(ipSecConfig); |
Nathan Harold | 2e9a520 | 2017-09-26 11:44:23 -0700 | [diff] [blame] | 315 | |
| 316 | IpSecTransformResponse createTransformResp = |
Benedict Wong | f33f0313 | 2018-01-18 14:38:16 -0800 | [diff] [blame] | 317 | mIpSecService.createTransform(ipSecConfig, new Binder()); |
| 318 | mIpSecService.deleteTransform(createTransformResp.resourceId); |
Nathan Harold | 2e9a520 | 2017-09-26 11:44:23 -0700 | [diff] [blame] | 319 | |
| 320 | verify(mMockNetd) |
| 321 | .ipSecDeleteSecurityAssociation( |
Di Lu | 0b611f4 | 2018-01-11 11:35:25 -0800 | [diff] [blame] | 322 | eq(createTransformResp.resourceId), |
| 323 | anyString(), |
| 324 | anyString(), |
| 325 | eq(TEST_SPI), |
| 326 | anyInt(), |
| 327 | anyInt()); |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 328 | |
| 329 | // Verify quota and RefcountedResource objects cleaned up |
| 330 | IpSecService.UserRecord userRecord = |
| 331 | mIpSecService.mUserResourceTracker.getUserRecord(Os.getuid()); |
| 332 | assertEquals(0, userRecord.mTransformQuotaTracker.mCurrent); |
| 333 | try { |
| 334 | userRecord.mTransformRecords.getRefcountedResourceOrThrow( |
| 335 | createTransformResp.resourceId); |
| 336 | fail("Expected IllegalArgumentException on attempt to access deleted resource"); |
| 337 | } catch (IllegalArgumentException expected) { |
| 338 | |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | @Test |
| 343 | public void testTransportModeTransformBinderDeath() throws Exception { |
| 344 | IpSecConfig ipSecConfig = new IpSecConfig(); |
| 345 | addDefaultSpisAndRemoteAddrToIpSecConfig(ipSecConfig); |
| 346 | addAuthAndCryptToIpSecConfig(ipSecConfig); |
| 347 | |
| 348 | IpSecTransformResponse createTransformResp = |
Benedict Wong | f33f0313 | 2018-01-18 14:38:16 -0800 | [diff] [blame] | 349 | mIpSecService.createTransform(ipSecConfig, new Binder()); |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 350 | |
| 351 | IpSecService.UserRecord userRecord = |
| 352 | mIpSecService.mUserResourceTracker.getUserRecord(Os.getuid()); |
| 353 | IpSecService.RefcountedResource refcountedRecord = |
| 354 | userRecord.mTransformRecords.getRefcountedResourceOrThrow( |
| 355 | createTransformResp.resourceId); |
| 356 | |
| 357 | refcountedRecord.binderDied(); |
| 358 | |
| 359 | verify(mMockNetd) |
| 360 | .ipSecDeleteSecurityAssociation( |
Di Lu | 0b611f4 | 2018-01-11 11:35:25 -0800 | [diff] [blame] | 361 | eq(createTransformResp.resourceId), |
| 362 | anyString(), |
| 363 | anyString(), |
| 364 | eq(TEST_SPI), |
| 365 | anyInt(), |
| 366 | anyInt()); |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 367 | |
| 368 | // Verify quota and RefcountedResource objects cleaned up |
| 369 | assertEquals(0, userRecord.mTransformQuotaTracker.mCurrent); |
| 370 | try { |
| 371 | userRecord.mTransformRecords.getRefcountedResourceOrThrow( |
| 372 | createTransformResp.resourceId); |
| 373 | fail("Expected IllegalArgumentException on attempt to access deleted resource"); |
| 374 | } catch (IllegalArgumentException expected) { |
| 375 | |
| 376 | } |
Nathan Harold | 2e9a520 | 2017-09-26 11:44:23 -0700 | [diff] [blame] | 377 | } |
| 378 | |
| 379 | @Test |
| 380 | public void testApplyTransportModeTransform() throws Exception { |
Benedict Wong | 0febe5e | 2017-08-22 21:42:33 -0700 | [diff] [blame] | 381 | IpSecConfig ipSecConfig = new IpSecConfig(); |
| 382 | addDefaultSpisAndRemoteAddrToIpSecConfig(ipSecConfig); |
| 383 | addAuthAndCryptToIpSecConfig(ipSecConfig); |
Nathan Harold | 2e9a520 | 2017-09-26 11:44:23 -0700 | [diff] [blame] | 384 | |
| 385 | IpSecTransformResponse createTransformResp = |
Benedict Wong | f33f0313 | 2018-01-18 14:38:16 -0800 | [diff] [blame] | 386 | mIpSecService.createTransform(ipSecConfig, new Binder()); |
Nathan Harold | 2e9a520 | 2017-09-26 11:44:23 -0700 | [diff] [blame] | 387 | ParcelFileDescriptor pfd = ParcelFileDescriptor.fromSocket(new Socket()); |
| 388 | |
| 389 | int resourceId = createTransformResp.resourceId; |
Nathan Harold | a252331 | 2018-01-05 19:25:13 -0800 | [diff] [blame] | 390 | mIpSecService.applyTransportModeTransform(pfd, IpSecManager.DIRECTION_OUT, resourceId); |
Nathan Harold | 2e9a520 | 2017-09-26 11:44:23 -0700 | [diff] [blame] | 391 | |
| 392 | verify(mMockNetd) |
| 393 | .ipSecApplyTransportModeTransform( |
| 394 | eq(pfd.getFileDescriptor()), |
| 395 | eq(resourceId), |
Nathan Harold | a252331 | 2018-01-05 19:25:13 -0800 | [diff] [blame] | 396 | eq(IpSecManager.DIRECTION_OUT), |
Nathan Harold | 2e9a520 | 2017-09-26 11:44:23 -0700 | [diff] [blame] | 397 | anyString(), |
| 398 | anyString(), |
Nathan Harold | a252331 | 2018-01-05 19:25:13 -0800 | [diff] [blame] | 399 | eq(TEST_SPI)); |
Nathan Harold | 2e9a520 | 2017-09-26 11:44:23 -0700 | [diff] [blame] | 400 | } |
| 401 | |
| 402 | @Test |
| 403 | public void testRemoveTransportModeTransform() throws Exception { |
| 404 | ParcelFileDescriptor pfd = ParcelFileDescriptor.fromSocket(new Socket()); |
Nathan Harold | f73d252 | 2018-01-17 01:00:20 -0800 | [diff] [blame] | 405 | mIpSecService.removeTransportModeTransforms(pfd); |
Nathan Harold | 2e9a520 | 2017-09-26 11:44:23 -0700 | [diff] [blame] | 406 | |
| 407 | verify(mMockNetd).ipSecRemoveTransportModeTransform(pfd.getFileDescriptor()); |
| 408 | } |
| 409 | } |