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 | ecc9f7c | 2018-03-01 18:53:07 -0800 | [diff] [blame^] | 20 | import static org.junit.Assert.assertNotNull; |
Benedict Wong | 0febe5e | 2017-08-22 21:42:33 -0700 | [diff] [blame] | 21 | import static org.junit.Assert.fail; |
Nathan Harold | 2e9a520 | 2017-09-26 11:44:23 -0700 | [diff] [blame] | 22 | import static org.mockito.Matchers.anyInt; |
Nathan Harold | 2e9a520 | 2017-09-26 11:44:23 -0700 | [diff] [blame] | 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; |
Benedict Wong | ecc9f7c | 2018-03-01 18:53:07 -0800 | [diff] [blame^] | 36 | import android.net.IpSecTunnelInterfaceResponse; |
| 37 | import android.net.LinkAddress; |
| 38 | import android.net.Network; |
Nathan Harold | 2e9a520 | 2017-09-26 11:44:23 -0700 | [diff] [blame] | 39 | import android.net.NetworkUtils; |
| 40 | import android.os.Binder; |
| 41 | import android.os.ParcelFileDescriptor; |
| 42 | import android.support.test.filters.SmallTest; |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 43 | import android.system.Os; |
Nathan Harold | 2e9a520 | 2017-09-26 11:44:23 -0700 | [diff] [blame] | 44 | |
| 45 | import java.net.Socket; |
| 46 | import java.util.Arrays; |
| 47 | import java.util.Collection; |
| 48 | |
| 49 | import org.junit.Before; |
| 50 | import org.junit.Test; |
| 51 | import org.junit.runner.RunWith; |
| 52 | import org.junit.runners.Parameterized; |
| 53 | |
| 54 | /** Unit tests for {@link IpSecService}. */ |
| 55 | @SmallTest |
| 56 | @RunWith(Parameterized.class) |
| 57 | public class IpSecServiceParameterizedTest { |
| 58 | |
Nathan Harold | a252331 | 2018-01-05 19:25:13 -0800 | [diff] [blame] | 59 | private static final int TEST_SPI = 0xD1201D; |
Nathan Harold | 2e9a520 | 2017-09-26 11:44:23 -0700 | [diff] [blame] | 60 | |
Nathan Harold | a252331 | 2018-01-05 19:25:13 -0800 | [diff] [blame] | 61 | private final String mDestinationAddr; |
Nathan Harold | 5676f5f | 2018-01-16 19:34:01 -0800 | [diff] [blame] | 62 | private final String mSourceAddr; |
Benedict Wong | ecc9f7c | 2018-03-01 18:53:07 -0800 | [diff] [blame^] | 63 | private final LinkAddress mLocalInnerAddress; |
Nathan Harold | 2e9a520 | 2017-09-26 11:44:23 -0700 | [diff] [blame] | 64 | |
| 65 | @Parameterized.Parameters |
| 66 | public static Collection ipSecConfigs() { |
Benedict Wong | ecc9f7c | 2018-03-01 18:53:07 -0800 | [diff] [blame^] | 67 | 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 Harold | 2e9a520 | 2017-09-26 11:44:23 -0700 | [diff] [blame] | 72 | } |
| 73 | |
Benedict Wong | 4ebc2c5 | 2017-11-01 17:14:25 -0700 | [diff] [blame] | 74 | 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 Harold | 2e9a520 | 2017-09-26 11:44:23 -0700 | [diff] [blame] | 81 | 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 Wong | ecc9f7c | 2018-03-01 18:53:07 -0800 | [diff] [blame^] | 98 | Network fakeNetwork = new Network(0xAB); |
Nathan Harold | 2e9a520 | 2017-09-26 11:44:23 -0700 | [diff] [blame] | 99 | |
Benedict Wong | 0febe5e | 2017-08-22 21:42:33 -0700 | [diff] [blame] | 100 | 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 Wong | 4ebc2c5 | 2017-11-01 17:14:25 -0700 | [diff] [blame] | 105 | new IpSecAlgorithm(IpSecAlgorithm.AUTH_CRYPT_AES_GCM, AEAD_KEY, 128); |
Benedict Wong | 0febe5e | 2017-08-22 21:42:33 -0700 | [diff] [blame] | 106 | |
Benedict Wong | ecc9f7c | 2018-03-01 18:53:07 -0800 | [diff] [blame^] | 107 | public IpSecServiceParameterizedTest( |
| 108 | String sourceAddr, String destAddr, String localInnerAddr) { |
Nathan Harold | 5676f5f | 2018-01-16 19:34:01 -0800 | [diff] [blame] | 109 | mSourceAddr = sourceAddr; |
| 110 | mDestinationAddr = destAddr; |
Benedict Wong | ecc9f7c | 2018-03-01 18:53:07 -0800 | [diff] [blame^] | 111 | mLocalInnerAddress = new LinkAddress(localInnerAddr); |
Nathan Harold | 2e9a520 | 2017-09-26 11:44:23 -0700 | [diff] [blame] | 112 | } |
| 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 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 | assertEquals(IpSecManager.Status.OK, spiResp.status); |
Nathan Harold | a252331 | 2018-01-05 19:25:13 -0800 | [diff] [blame] | 134 | assertEquals(TEST_SPI, spiResp.spi); |
Nathan Harold | 2e9a520 | 2017-09-26 11:44:23 -0700 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | @Test |
| 138 | public void testReleaseSecurityParameterIndex() throws Exception { |
Nathan Harold | a252331 | 2018-01-05 19:25:13 -0800 | [diff] [blame] | 139 | when(mMockNetd.ipSecAllocateSpi(anyInt(), anyString(), eq(mDestinationAddr), eq(TEST_SPI))) |
| 140 | .thenReturn(TEST_SPI); |
Nathan Harold | 2e9a520 | 2017-09-26 11:44:23 -0700 | [diff] [blame] | 141 | |
| 142 | IpSecSpiResponse spiResp = |
Jonathan Basseri | 5fb9290 | 2017-11-16 10:58:01 -0800 | [diff] [blame] | 143 | mIpSecService.allocateSecurityParameterIndex( |
Nathan Harold | a252331 | 2018-01-05 19:25:13 -0800 | [diff] [blame] | 144 | mDestinationAddr, TEST_SPI, new Binder()); |
Nathan Harold | 2e9a520 | 2017-09-26 11:44:23 -0700 | [diff] [blame] | 145 | |
| 146 | mIpSecService.releaseSecurityParameterIndex(spiResp.resourceId); |
| 147 | |
| 148 | verify(mMockNetd) |
| 149 | .ipSecDeleteSecurityAssociation( |
Di Lu | 0b611f4 | 2018-01-11 11:35:25 -0800 | [diff] [blame] | 150 | eq(spiResp.resourceId), |
| 151 | anyString(), |
| 152 | anyString(), |
| 153 | eq(TEST_SPI), |
| 154 | anyInt(), |
| 155 | anyInt()); |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 156 | |
| 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 Harold | a252331 | 2018-01-05 19:25:13 -0800 | [diff] [blame] | 171 | when(mMockNetd.ipSecAllocateSpi(anyInt(), anyString(), eq(mDestinationAddr), eq(TEST_SPI))) |
| 172 | .thenReturn(TEST_SPI); |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 173 | |
| 174 | IpSecSpiResponse spiResp = |
Nathan Harold | 660a335 | 2017-12-14 14:46:46 -0800 | [diff] [blame] | 175 | mIpSecService.allocateSecurityParameterIndex( |
Nathan Harold | a252331 | 2018-01-05 19:25:13 -0800 | [diff] [blame] | 176 | mDestinationAddr, TEST_SPI, new Binder()); |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 177 | |
| 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 Lu | 0b611f4 | 2018-01-11 11:35:25 -0800 | [diff] [blame] | 187 | eq(spiResp.resourceId), |
| 188 | anyString(), |
| 189 | anyString(), |
| 190 | eq(TEST_SPI), |
| 191 | anyInt(), |
| 192 | anyInt()); |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 193 | |
| 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 Harold | 2e9a520 | 2017-09-26 11:44:23 -0700 | [diff] [blame] | 202 | } |
| 203 | |
Nathan Harold | a252331 | 2018-01-05 19:25:13 -0800 | [diff] [blame] | 204 | private int getNewSpiResourceId(String remoteAddress, int returnSpi) throws Exception { |
| 205 | when(mMockNetd.ipSecAllocateSpi(anyInt(), anyString(), anyString(), anyInt())) |
Benedict Wong | 0febe5e | 2017-08-22 21:42:33 -0700 | [diff] [blame] | 206 | .thenReturn(returnSpi); |
Nathan Harold | 2e9a520 | 2017-09-26 11:44:23 -0700 | [diff] [blame] | 207 | |
Benedict Wong | 0febe5e | 2017-08-22 21:42:33 -0700 | [diff] [blame] | 208 | IpSecSpiResponse spi = |
Jonathan Basseri | 5fb9290 | 2017-11-16 10:58:01 -0800 | [diff] [blame] | 209 | mIpSecService.allocateSecurityParameterIndex( |
Benedict Wong | 0febe5e | 2017-08-22 21:42:33 -0700 | [diff] [blame] | 210 | NetworkUtils.numericToInetAddress(remoteAddress).getHostAddress(), |
| 211 | IpSecManager.INVALID_SECURITY_PARAMETER_INDEX, |
| 212 | new Binder()); |
| 213 | return spi.resourceId; |
| 214 | } |
Nathan Harold | 2e9a520 | 2017-09-26 11:44:23 -0700 | [diff] [blame] | 215 | |
Benedict Wong | 0febe5e | 2017-08-22 21:42:33 -0700 | [diff] [blame] | 216 | private void addDefaultSpisAndRemoteAddrToIpSecConfig(IpSecConfig config) throws Exception { |
Nathan Harold | a252331 | 2018-01-05 19:25:13 -0800 | [diff] [blame] | 217 | config.setSpiResourceId(getNewSpiResourceId(mDestinationAddr, TEST_SPI)); |
Nathan Harold | 5676f5f | 2018-01-16 19:34:01 -0800 | [diff] [blame] | 218 | config.setSourceAddress(mSourceAddr); |
Nathan Harold | a252331 | 2018-01-05 19:25:13 -0800 | [diff] [blame] | 219 | config.setDestinationAddress(mDestinationAddr); |
Benedict Wong | 0febe5e | 2017-08-22 21:42:33 -0700 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | private void addAuthAndCryptToIpSecConfig(IpSecConfig config) throws Exception { |
Nathan Harold | a252331 | 2018-01-05 19:25:13 -0800 | [diff] [blame] | 223 | config.setEncryption(CRYPT_ALGO); |
| 224 | config.setAuthentication(AUTH_ALGO); |
Nathan Harold | 2e9a520 | 2017-09-26 11:44:23 -0700 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | @Test |
Benedict Wong | f33f0313 | 2018-01-18 14:38:16 -0800 | [diff] [blame] | 228 | public void testCreateTransform() throws Exception { |
Benedict Wong | 0febe5e | 2017-08-22 21:42:33 -0700 | [diff] [blame] | 229 | IpSecConfig ipSecConfig = new IpSecConfig(); |
| 230 | addDefaultSpisAndRemoteAddrToIpSecConfig(ipSecConfig); |
| 231 | addAuthAndCryptToIpSecConfig(ipSecConfig); |
Nathan Harold | 2e9a520 | 2017-09-26 11:44:23 -0700 | [diff] [blame] | 232 | |
| 233 | IpSecTransformResponse createTransformResp = |
Benedict Wong | f33f0313 | 2018-01-18 14:38:16 -0800 | [diff] [blame] | 234 | mIpSecService.createTransform(ipSecConfig, new Binder()); |
Nathan Harold | 2e9a520 | 2017-09-26 11:44:23 -0700 | [diff] [blame] | 235 | assertEquals(IpSecManager.Status.OK, createTransformResp.status); |
| 236 | |
| 237 | verify(mMockNetd) |
| 238 | .ipSecAddSecurityAssociation( |
| 239 | eq(createTransformResp.resourceId), |
| 240 | anyInt(), |
Nathan Harold | 2e9a520 | 2017-09-26 11:44:23 -0700 | [diff] [blame] | 241 | anyString(), |
| 242 | anyString(), |
Benedict Wong | 0fe58a9 | 2018-01-19 17:36:02 -0800 | [diff] [blame] | 243 | anyInt(), |
Nathan Harold | a252331 | 2018-01-05 19:25:13 -0800 | [diff] [blame] | 244 | eq(TEST_SPI), |
Di Lu | 0b611f4 | 2018-01-11 11:35:25 -0800 | [diff] [blame] | 245 | anyInt(), |
| 246 | anyInt(), |
Benedict Wong | 0febe5e | 2017-08-22 21:42:33 -0700 | [diff] [blame] | 247 | 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 Raj | fffa811 | 2017-10-26 11:49:02 -0700 | [diff] [blame] | 254 | eq(new byte[] {}), |
Benedict Wong | 0febe5e | 2017-08-22 21:42:33 -0700 | [diff] [blame] | 255 | eq(0), |
| 256 | anyInt(), |
| 257 | anyInt(), |
| 258 | anyInt()); |
| 259 | } |
| 260 | |
| 261 | @Test |
Benedict Wong | f33f0313 | 2018-01-18 14:38:16 -0800 | [diff] [blame] | 262 | public void testCreateTransformAead() throws Exception { |
Benedict Wong | 0febe5e | 2017-08-22 21:42:33 -0700 | [diff] [blame] | 263 | IpSecConfig ipSecConfig = new IpSecConfig(); |
| 264 | addDefaultSpisAndRemoteAddrToIpSecConfig(ipSecConfig); |
| 265 | |
Nathan Harold | a252331 | 2018-01-05 19:25:13 -0800 | [diff] [blame] | 266 | ipSecConfig.setAuthenticatedEncryption(AEAD_ALGO); |
Benedict Wong | 0febe5e | 2017-08-22 21:42:33 -0700 | [diff] [blame] | 267 | |
| 268 | IpSecTransformResponse createTransformResp = |
Benedict Wong | f33f0313 | 2018-01-18 14:38:16 -0800 | [diff] [blame] | 269 | mIpSecService.createTransform(ipSecConfig, new Binder()); |
Benedict Wong | 0febe5e | 2017-08-22 21:42:33 -0700 | [diff] [blame] | 270 | assertEquals(IpSecManager.Status.OK, createTransformResp.status); |
| 271 | |
| 272 | verify(mMockNetd) |
| 273 | .ipSecAddSecurityAssociation( |
| 274 | eq(createTransformResp.resourceId), |
| 275 | anyInt(), |
Benedict Wong | 0febe5e | 2017-08-22 21:42:33 -0700 | [diff] [blame] | 276 | anyString(), |
| 277 | anyString(), |
Benedict Wong | 0fe58a9 | 2018-01-19 17:36:02 -0800 | [diff] [blame] | 278 | anyInt(), |
Nathan Harold | a252331 | 2018-01-05 19:25:13 -0800 | [diff] [blame] | 279 | eq(TEST_SPI), |
Di Lu | 0b611f4 | 2018-01-11 11:35:25 -0800 | [diff] [blame] | 280 | anyInt(), |
| 281 | anyInt(), |
Benedict Wong | 0febe5e | 2017-08-22 21:42:33 -0700 | [diff] [blame] | 282 | eq(""), |
Manoj Boopathi Raj | fffa811 | 2017-10-26 11:49:02 -0700 | [diff] [blame] | 283 | eq(new byte[] {}), |
Benedict Wong | 0febe5e | 2017-08-22 21:42:33 -0700 | [diff] [blame] | 284 | eq(0), |
| 285 | eq(""), |
Manoj Boopathi Raj | fffa811 | 2017-10-26 11:49:02 -0700 | [diff] [blame] | 286 | eq(new byte[] {}), |
Benedict Wong | 0febe5e | 2017-08-22 21:42:33 -0700 | [diff] [blame] | 287 | eq(0), |
| 288 | eq(IpSecAlgorithm.AUTH_CRYPT_AES_GCM), |
Benedict Wong | 4ebc2c5 | 2017-11-01 17:14:25 -0700 | [diff] [blame] | 289 | eq(AEAD_KEY), |
Nathan Harold | 2e9a520 | 2017-09-26 11:44:23 -0700 | [diff] [blame] | 290 | anyInt(), |
| 291 | anyInt(), |
| 292 | anyInt(), |
| 293 | anyInt()); |
| 294 | } |
| 295 | |
Andreas Gampe | a7b26b5 | 2018-02-26 08:06:30 -0800 | [diff] [blame] | 296 | @Test |
Benedict Wong | e6b4277 | 2017-12-13 18:26:40 -0800 | [diff] [blame] | 297 | 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 Harold | 2e9a520 | 2017-09-26 11:44:23 -0700 | [diff] [blame] | 322 | @Test |
Benedict Wong | f33f0313 | 2018-01-18 14:38:16 -0800 | [diff] [blame] | 323 | public void testDeleteTransform() throws Exception { |
Benedict Wong | 0febe5e | 2017-08-22 21:42:33 -0700 | [diff] [blame] | 324 | IpSecConfig ipSecConfig = new IpSecConfig(); |
| 325 | addDefaultSpisAndRemoteAddrToIpSecConfig(ipSecConfig); |
| 326 | addAuthAndCryptToIpSecConfig(ipSecConfig); |
Nathan Harold | 2e9a520 | 2017-09-26 11:44:23 -0700 | [diff] [blame] | 327 | |
| 328 | IpSecTransformResponse createTransformResp = |
Benedict Wong | f33f0313 | 2018-01-18 14:38:16 -0800 | [diff] [blame] | 329 | mIpSecService.createTransform(ipSecConfig, new Binder()); |
| 330 | mIpSecService.deleteTransform(createTransformResp.resourceId); |
Nathan Harold | 2e9a520 | 2017-09-26 11:44:23 -0700 | [diff] [blame] | 331 | |
| 332 | verify(mMockNetd) |
| 333 | .ipSecDeleteSecurityAssociation( |
Di Lu | 0b611f4 | 2018-01-11 11:35:25 -0800 | [diff] [blame] | 334 | eq(createTransformResp.resourceId), |
| 335 | anyString(), |
| 336 | anyString(), |
| 337 | eq(TEST_SPI), |
| 338 | anyInt(), |
| 339 | anyInt()); |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 340 | |
| 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 Wong | f33f0313 | 2018-01-18 14:38:16 -0800 | [diff] [blame] | 361 | mIpSecService.createTransform(ipSecConfig, new Binder()); |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 362 | |
| 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 Lu | 0b611f4 | 2018-01-11 11:35:25 -0800 | [diff] [blame] | 373 | eq(createTransformResp.resourceId), |
| 374 | anyString(), |
| 375 | anyString(), |
| 376 | eq(TEST_SPI), |
| 377 | anyInt(), |
| 378 | anyInt()); |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 379 | |
| 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 Harold | 2e9a520 | 2017-09-26 11:44:23 -0700 | [diff] [blame] | 389 | } |
| 390 | |
| 391 | @Test |
| 392 | public void testApplyTransportModeTransform() throws Exception { |
Benedict Wong | 0febe5e | 2017-08-22 21:42:33 -0700 | [diff] [blame] | 393 | IpSecConfig ipSecConfig = new IpSecConfig(); |
| 394 | addDefaultSpisAndRemoteAddrToIpSecConfig(ipSecConfig); |
| 395 | addAuthAndCryptToIpSecConfig(ipSecConfig); |
Nathan Harold | 2e9a520 | 2017-09-26 11:44:23 -0700 | [diff] [blame] | 396 | |
| 397 | IpSecTransformResponse createTransformResp = |
Benedict Wong | f33f0313 | 2018-01-18 14:38:16 -0800 | [diff] [blame] | 398 | mIpSecService.createTransform(ipSecConfig, new Binder()); |
Nathan Harold | 2e9a520 | 2017-09-26 11:44:23 -0700 | [diff] [blame] | 399 | ParcelFileDescriptor pfd = ParcelFileDescriptor.fromSocket(new Socket()); |
| 400 | |
| 401 | int resourceId = createTransformResp.resourceId; |
Nathan Harold | a252331 | 2018-01-05 19:25:13 -0800 | [diff] [blame] | 402 | mIpSecService.applyTransportModeTransform(pfd, IpSecManager.DIRECTION_OUT, resourceId); |
Nathan Harold | 2e9a520 | 2017-09-26 11:44:23 -0700 | [diff] [blame] | 403 | |
| 404 | verify(mMockNetd) |
| 405 | .ipSecApplyTransportModeTransform( |
| 406 | eq(pfd.getFileDescriptor()), |
| 407 | eq(resourceId), |
Nathan Harold | a252331 | 2018-01-05 19:25:13 -0800 | [diff] [blame] | 408 | eq(IpSecManager.DIRECTION_OUT), |
Nathan Harold | 2e9a520 | 2017-09-26 11:44:23 -0700 | [diff] [blame] | 409 | anyString(), |
| 410 | anyString(), |
Nathan Harold | a252331 | 2018-01-05 19:25:13 -0800 | [diff] [blame] | 411 | eq(TEST_SPI)); |
Nathan Harold | 2e9a520 | 2017-09-26 11:44:23 -0700 | [diff] [blame] | 412 | } |
| 413 | |
| 414 | @Test |
| 415 | public void testRemoveTransportModeTransform() throws Exception { |
| 416 | ParcelFileDescriptor pfd = ParcelFileDescriptor.fromSocket(new Socket()); |
Nathan Harold | f73d252 | 2018-01-17 01:00:20 -0800 | [diff] [blame] | 417 | mIpSecService.removeTransportModeTransforms(pfd); |
Nathan Harold | 2e9a520 | 2017-09-26 11:44:23 -0700 | [diff] [blame] | 418 | |
| 419 | verify(mMockNetd).ipSecRemoveTransportModeTransform(pfd.getFileDescriptor()); |
| 420 | } |
Benedict Wong | ecc9f7c | 2018-03-01 18:53:07 -0800 | [diff] [blame^] | 421 | |
| 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 Harold | 2e9a520 | 2017-09-26 11:44:23 -0700 | [diff] [blame] | 520 | } |