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 | |
evitayan | e4259d3 | 2018-03-22 17:53:08 -0700 | [diff] [blame] | 19 | import static android.system.OsConstants.AF_INET; |
| 20 | import static android.system.OsConstants.AF_INET6; |
Nathan Harold | 2e9a520 | 2017-09-26 11:44:23 -0700 | [diff] [blame] | 21 | import static org.junit.Assert.assertEquals; |
Benedict Wong | ecc9f7c | 2018-03-01 18:53:07 -0800 | [diff] [blame] | 22 | import static org.junit.Assert.assertNotNull; |
Benedict Wong | 0febe5e | 2017-08-22 21:42:33 -0700 | [diff] [blame] | 23 | import static org.junit.Assert.fail; |
Nathan Harold | 2e9a520 | 2017-09-26 11:44:23 -0700 | [diff] [blame] | 24 | import static org.mockito.Matchers.anyInt; |
Nathan Harold | 2e9a520 | 2017-09-26 11:44:23 -0700 | [diff] [blame] | 25 | import static org.mockito.Matchers.anyString; |
| 26 | import static org.mockito.Matchers.eq; |
| 27 | import static org.mockito.Mockito.mock; |
Nathan Harold | 2279530 | 2018-02-27 19:19:40 -0800 | [diff] [blame] | 28 | import static org.mockito.Mockito.times; |
Nathan Harold | 2e9a520 | 2017-09-26 11:44:23 -0700 | [diff] [blame] | 29 | import static org.mockito.Mockito.verify; |
| 30 | import static org.mockito.Mockito.when; |
| 31 | |
Nathan Harold | 21208ee | 2018-03-15 18:06:06 -0700 | [diff] [blame] | 32 | import android.app.AppOpsManager; |
Nathan Harold | 2e9a520 | 2017-09-26 11:44:23 -0700 | [diff] [blame] | 33 | import android.content.Context; |
| 34 | import android.net.INetd; |
| 35 | import android.net.IpSecAlgorithm; |
| 36 | import android.net.IpSecConfig; |
| 37 | import android.net.IpSecManager; |
| 38 | import android.net.IpSecSpiResponse; |
evitayan | 0a7d3e3 | 2018-03-22 13:42:07 -0700 | [diff] [blame] | 39 | import android.net.IpSecTransform; |
Nathan Harold | 2e9a520 | 2017-09-26 11:44:23 -0700 | [diff] [blame] | 40 | import android.net.IpSecTransformResponse; |
Benedict Wong | ecc9f7c | 2018-03-01 18:53:07 -0800 | [diff] [blame] | 41 | import android.net.IpSecTunnelInterfaceResponse; |
evitayan | 0a7d3e3 | 2018-03-22 13:42:07 -0700 | [diff] [blame] | 42 | import android.net.IpSecUdpEncapResponse; |
Benedict Wong | ecc9f7c | 2018-03-01 18:53:07 -0800 | [diff] [blame] | 43 | import android.net.LinkAddress; |
| 44 | import android.net.Network; |
Nathan Harold | 2e9a520 | 2017-09-26 11:44:23 -0700 | [diff] [blame] | 45 | import android.net.NetworkUtils; |
| 46 | import android.os.Binder; |
| 47 | import android.os.ParcelFileDescriptor; |
| 48 | import android.support.test.filters.SmallTest; |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 49 | import android.system.Os; |
Benedict Wong | a098962 | 2018-07-25 13:06:29 -0700 | [diff] [blame] | 50 | import android.test.mock.MockContext; |
Nathan Harold | 2e9a520 | 2017-09-26 11:44:23 -0700 | [diff] [blame] | 51 | |
| 52 | import java.net.Socket; |
| 53 | import java.util.Arrays; |
| 54 | import java.util.Collection; |
| 55 | |
| 56 | import org.junit.Before; |
Nathan Harold | 8c69bcb | 2018-05-15 19:18:38 -0700 | [diff] [blame] | 57 | import org.junit.Ignore; |
Nathan Harold | 2e9a520 | 2017-09-26 11:44:23 -0700 | [diff] [blame] | 58 | import org.junit.Test; |
| 59 | import org.junit.runner.RunWith; |
| 60 | import org.junit.runners.Parameterized; |
| 61 | |
| 62 | /** Unit tests for {@link IpSecService}. */ |
| 63 | @SmallTest |
| 64 | @RunWith(Parameterized.class) |
| 65 | public class IpSecServiceParameterizedTest { |
| 66 | |
Nathan Harold | a252331 | 2018-01-05 19:25:13 -0800 | [diff] [blame] | 67 | private static final int TEST_SPI = 0xD1201D; |
Nathan Harold | 2e9a520 | 2017-09-26 11:44:23 -0700 | [diff] [blame] | 68 | |
Nathan Harold | 5676f5f | 2018-01-16 19:34:01 -0800 | [diff] [blame] | 69 | private final String mSourceAddr; |
evitayan | e4259d3 | 2018-03-22 17:53:08 -0700 | [diff] [blame] | 70 | private final String mDestinationAddr; |
Benedict Wong | ecc9f7c | 2018-03-01 18:53:07 -0800 | [diff] [blame] | 71 | private final LinkAddress mLocalInnerAddress; |
evitayan | e4259d3 | 2018-03-22 17:53:08 -0700 | [diff] [blame] | 72 | private final int mFamily; |
Nathan Harold | 2e9a520 | 2017-09-26 11:44:23 -0700 | [diff] [blame] | 73 | |
Benedict Wong | 781dae6 | 2018-09-06 11:31:25 -0700 | [diff] [blame] | 74 | private static final int[] ADDRESS_FAMILIES = |
| 75 | new int[] {AF_INET, AF_INET6}; |
| 76 | |
Nathan Harold | 2e9a520 | 2017-09-26 11:44:23 -0700 | [diff] [blame] | 77 | @Parameterized.Parameters |
| 78 | public static Collection ipSecConfigs() { |
Benedict Wong | ecc9f7c | 2018-03-01 18:53:07 -0800 | [diff] [blame] | 79 | return Arrays.asList( |
| 80 | new Object[][] { |
evitayan | e4259d3 | 2018-03-22 17:53:08 -0700 | [diff] [blame] | 81 | {"1.2.3.4", "8.8.4.4", "10.0.1.1/24", AF_INET}, |
| 82 | {"2601::2", "2601::10", "2001:db8::1/64", AF_INET6} |
Benedict Wong | ecc9f7c | 2018-03-01 18:53:07 -0800 | [diff] [blame] | 83 | }); |
Nathan Harold | 2e9a520 | 2017-09-26 11:44:23 -0700 | [diff] [blame] | 84 | } |
| 85 | |
Benedict Wong | 4ebc2c5 | 2017-11-01 17:14:25 -0700 | [diff] [blame] | 86 | private static final byte[] AEAD_KEY = { |
| 87 | 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, |
| 88 | 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, |
| 89 | 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, |
| 90 | 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, |
| 91 | 0x73, 0x61, 0x6C, 0x74 |
| 92 | }; |
Nathan Harold | 2e9a520 | 2017-09-26 11:44:23 -0700 | [diff] [blame] | 93 | private static final byte[] CRYPT_KEY = { |
| 94 | 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, |
| 95 | 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, |
| 96 | 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, |
| 97 | 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F |
| 98 | }; |
| 99 | private static final byte[] AUTH_KEY = { |
| 100 | 0x7A, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 101 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7F, |
| 102 | 0x7A, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 103 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7F |
| 104 | }; |
| 105 | |
Nathan Harold | 21208ee | 2018-03-15 18:06:06 -0700 | [diff] [blame] | 106 | AppOpsManager mMockAppOps = mock(AppOpsManager.class); |
| 107 | |
| 108 | MockContext mMockContext = new MockContext() { |
| 109 | @Override |
| 110 | public Object getSystemService(String name) { |
| 111 | switch(name) { |
| 112 | case Context.APP_OPS_SERVICE: |
| 113 | return mMockAppOps; |
| 114 | default: |
| 115 | return null; |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | @Override |
| 120 | public void enforceCallingOrSelfPermission(String permission, String message) { |
| 121 | if (permission == android.Manifest.permission.MANAGE_IPSEC_TUNNELS) { |
| 122 | return; |
| 123 | } |
| 124 | throw new SecurityException("Unavailable permission requested"); |
| 125 | } |
| 126 | }; |
| 127 | |
Nathan Harold | 2e9a520 | 2017-09-26 11:44:23 -0700 | [diff] [blame] | 128 | INetd mMockNetd; |
| 129 | IpSecService.IpSecServiceConfiguration mMockIpSecSrvConfig; |
| 130 | IpSecService mIpSecService; |
Benedict Wong | ecc9f7c | 2018-03-01 18:53:07 -0800 | [diff] [blame] | 131 | Network fakeNetwork = new Network(0xAB); |
Benedict Wong | a098962 | 2018-07-25 13:06:29 -0700 | [diff] [blame] | 132 | int mUid = Os.getuid(); |
Nathan Harold | 2e9a520 | 2017-09-26 11:44:23 -0700 | [diff] [blame] | 133 | |
Benedict Wong | 0febe5e | 2017-08-22 21:42:33 -0700 | [diff] [blame] | 134 | private static final IpSecAlgorithm AUTH_ALGO = |
| 135 | new IpSecAlgorithm(IpSecAlgorithm.AUTH_HMAC_SHA256, AUTH_KEY, AUTH_KEY.length * 4); |
| 136 | private static final IpSecAlgorithm CRYPT_ALGO = |
| 137 | new IpSecAlgorithm(IpSecAlgorithm.CRYPT_AES_CBC, CRYPT_KEY); |
| 138 | private static final IpSecAlgorithm AEAD_ALGO = |
Benedict Wong | 4ebc2c5 | 2017-11-01 17:14:25 -0700 | [diff] [blame] | 139 | new IpSecAlgorithm(IpSecAlgorithm.AUTH_CRYPT_AES_GCM, AEAD_KEY, 128); |
evitayan | 0a7d3e3 | 2018-03-22 13:42:07 -0700 | [diff] [blame] | 140 | private static final int REMOTE_ENCAP_PORT = 4500; |
Benedict Wong | 0febe5e | 2017-08-22 21:42:33 -0700 | [diff] [blame] | 141 | |
Benedict Wong | ecc9f7c | 2018-03-01 18:53:07 -0800 | [diff] [blame] | 142 | public IpSecServiceParameterizedTest( |
evitayan | e4259d3 | 2018-03-22 17:53:08 -0700 | [diff] [blame] | 143 | String sourceAddr, String destAddr, String localInnerAddr, int family) { |
Nathan Harold | 5676f5f | 2018-01-16 19:34:01 -0800 | [diff] [blame] | 144 | mSourceAddr = sourceAddr; |
| 145 | mDestinationAddr = destAddr; |
Benedict Wong | ecc9f7c | 2018-03-01 18:53:07 -0800 | [diff] [blame] | 146 | mLocalInnerAddress = new LinkAddress(localInnerAddr); |
evitayan | e4259d3 | 2018-03-22 17:53:08 -0700 | [diff] [blame] | 147 | mFamily = family; |
Nathan Harold | 2e9a520 | 2017-09-26 11:44:23 -0700 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | @Before |
| 151 | public void setUp() throws Exception { |
Nathan Harold | 2e9a520 | 2017-09-26 11:44:23 -0700 | [diff] [blame] | 152 | mMockNetd = mock(INetd.class); |
| 153 | mMockIpSecSrvConfig = mock(IpSecService.IpSecServiceConfiguration.class); |
| 154 | mIpSecService = new IpSecService(mMockContext, mMockIpSecSrvConfig); |
| 155 | |
| 156 | // Injecting mock netd |
| 157 | when(mMockIpSecSrvConfig.getNetdInstance()).thenReturn(mMockNetd); |
Nathan Harold | 21208ee | 2018-03-15 18:06:06 -0700 | [diff] [blame] | 158 | // A package granted the AppOp for MANAGE_IPSEC_TUNNELS will be MODE_ALLOWED. |
| 159 | when(mMockAppOps.noteOp(anyInt(), anyInt(), eq("blessedPackage"))) |
| 160 | .thenReturn(AppOpsManager.MODE_ALLOWED); |
| 161 | // A system package will not be granted the app op, so this should fall back to |
| 162 | // a permissions check, which should pass. |
| 163 | when(mMockAppOps.noteOp(anyInt(), anyInt(), eq("systemPackage"))) |
| 164 | .thenReturn(AppOpsManager.MODE_DEFAULT); |
| 165 | // A mismatch between the package name and the UID will return MODE_IGNORED. |
| 166 | when(mMockAppOps.noteOp(anyInt(), anyInt(), eq("badPackage"))) |
| 167 | .thenReturn(AppOpsManager.MODE_IGNORED); |
Nathan Harold | 2e9a520 | 2017-09-26 11:44:23 -0700 | [diff] [blame] | 168 | } |
| 169 | |
evitayan | 0a7d3e3 | 2018-03-22 13:42:07 -0700 | [diff] [blame] | 170 | //TODO: Add a test to verify SPI. |
| 171 | |
Nathan Harold | 2e9a520 | 2017-09-26 11:44:23 -0700 | [diff] [blame] | 172 | @Test |
| 173 | public void testIpSecServiceReserveSpi() throws Exception { |
Nathan Harold | a252331 | 2018-01-05 19:25:13 -0800 | [diff] [blame] | 174 | when(mMockNetd.ipSecAllocateSpi(anyInt(), anyString(), eq(mDestinationAddr), eq(TEST_SPI))) |
| 175 | .thenReturn(TEST_SPI); |
Nathan Harold | 2e9a520 | 2017-09-26 11:44:23 -0700 | [diff] [blame] | 176 | |
| 177 | IpSecSpiResponse spiResp = |
Jonathan Basseri | 5fb9290 | 2017-11-16 10:58:01 -0800 | [diff] [blame] | 178 | mIpSecService.allocateSecurityParameterIndex( |
Nathan Harold | a252331 | 2018-01-05 19:25:13 -0800 | [diff] [blame] | 179 | mDestinationAddr, TEST_SPI, new Binder()); |
Nathan Harold | 2e9a520 | 2017-09-26 11:44:23 -0700 | [diff] [blame] | 180 | assertEquals(IpSecManager.Status.OK, spiResp.status); |
Nathan Harold | a252331 | 2018-01-05 19:25:13 -0800 | [diff] [blame] | 181 | assertEquals(TEST_SPI, spiResp.spi); |
Nathan Harold | 2e9a520 | 2017-09-26 11:44:23 -0700 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | @Test |
| 185 | public void testReleaseSecurityParameterIndex() throws Exception { |
Nathan Harold | a252331 | 2018-01-05 19:25:13 -0800 | [diff] [blame] | 186 | when(mMockNetd.ipSecAllocateSpi(anyInt(), anyString(), eq(mDestinationAddr), eq(TEST_SPI))) |
| 187 | .thenReturn(TEST_SPI); |
Nathan Harold | 2e9a520 | 2017-09-26 11:44:23 -0700 | [diff] [blame] | 188 | |
| 189 | IpSecSpiResponse spiResp = |
Jonathan Basseri | 5fb9290 | 2017-11-16 10:58:01 -0800 | [diff] [blame] | 190 | mIpSecService.allocateSecurityParameterIndex( |
Nathan Harold | a252331 | 2018-01-05 19:25:13 -0800 | [diff] [blame] | 191 | mDestinationAddr, TEST_SPI, new Binder()); |
Nathan Harold | 2e9a520 | 2017-09-26 11:44:23 -0700 | [diff] [blame] | 192 | |
| 193 | mIpSecService.releaseSecurityParameterIndex(spiResp.resourceId); |
| 194 | |
| 195 | verify(mMockNetd) |
| 196 | .ipSecDeleteSecurityAssociation( |
Benedict Wong | a098962 | 2018-07-25 13:06:29 -0700 | [diff] [blame] | 197 | eq(mUid), |
Di Lu | 0b611f4 | 2018-01-11 11:35:25 -0800 | [diff] [blame] | 198 | anyString(), |
| 199 | anyString(), |
| 200 | eq(TEST_SPI), |
| 201 | anyInt(), |
Benedict Wong | 781dae6 | 2018-09-06 11:31:25 -0700 | [diff] [blame] | 202 | anyInt(), |
Di Lu | 0b611f4 | 2018-01-11 11:35:25 -0800 | [diff] [blame] | 203 | anyInt()); |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 204 | |
| 205 | // Verify quota and RefcountedResource objects cleaned up |
Benedict Wong | a098962 | 2018-07-25 13:06:29 -0700 | [diff] [blame] | 206 | IpSecService.UserRecord userRecord = mIpSecService.mUserResourceTracker.getUserRecord(mUid); |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 207 | assertEquals(0, userRecord.mSpiQuotaTracker.mCurrent); |
| 208 | try { |
| 209 | userRecord.mSpiRecords.getRefcountedResourceOrThrow(spiResp.resourceId); |
| 210 | fail("Expected IllegalArgumentException on attempt to access deleted resource"); |
| 211 | } catch (IllegalArgumentException expected) { |
| 212 | |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | @Test |
| 217 | public void testSecurityParameterIndexBinderDeath() throws Exception { |
Nathan Harold | a252331 | 2018-01-05 19:25:13 -0800 | [diff] [blame] | 218 | when(mMockNetd.ipSecAllocateSpi(anyInt(), anyString(), eq(mDestinationAddr), eq(TEST_SPI))) |
| 219 | .thenReturn(TEST_SPI); |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 220 | |
| 221 | IpSecSpiResponse spiResp = |
Nathan Harold | 660a335 | 2017-12-14 14:46:46 -0800 | [diff] [blame] | 222 | mIpSecService.allocateSecurityParameterIndex( |
Nathan Harold | a252331 | 2018-01-05 19:25:13 -0800 | [diff] [blame] | 223 | mDestinationAddr, TEST_SPI, new Binder()); |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 224 | |
Benedict Wong | a098962 | 2018-07-25 13:06:29 -0700 | [diff] [blame] | 225 | IpSecService.UserRecord userRecord = mIpSecService.mUserResourceTracker.getUserRecord(mUid); |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 226 | IpSecService.RefcountedResource refcountedRecord = |
| 227 | userRecord.mSpiRecords.getRefcountedResourceOrThrow(spiResp.resourceId); |
| 228 | |
| 229 | refcountedRecord.binderDied(); |
| 230 | |
| 231 | verify(mMockNetd) |
| 232 | .ipSecDeleteSecurityAssociation( |
Benedict Wong | a098962 | 2018-07-25 13:06:29 -0700 | [diff] [blame] | 233 | eq(mUid), |
Di Lu | 0b611f4 | 2018-01-11 11:35:25 -0800 | [diff] [blame] | 234 | anyString(), |
| 235 | anyString(), |
| 236 | eq(TEST_SPI), |
| 237 | anyInt(), |
Benedict Wong | 781dae6 | 2018-09-06 11:31:25 -0700 | [diff] [blame] | 238 | anyInt(), |
Di Lu | 0b611f4 | 2018-01-11 11:35:25 -0800 | [diff] [blame] | 239 | anyInt()); |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 240 | |
| 241 | // Verify quota and RefcountedResource objects cleaned up |
| 242 | assertEquals(0, userRecord.mSpiQuotaTracker.mCurrent); |
| 243 | try { |
| 244 | userRecord.mSpiRecords.getRefcountedResourceOrThrow(spiResp.resourceId); |
| 245 | fail("Expected IllegalArgumentException on attempt to access deleted resource"); |
| 246 | } catch (IllegalArgumentException expected) { |
| 247 | |
| 248 | } |
Nathan Harold | 2e9a520 | 2017-09-26 11:44:23 -0700 | [diff] [blame] | 249 | } |
| 250 | |
Nathan Harold | a252331 | 2018-01-05 19:25:13 -0800 | [diff] [blame] | 251 | private int getNewSpiResourceId(String remoteAddress, int returnSpi) throws Exception { |
| 252 | when(mMockNetd.ipSecAllocateSpi(anyInt(), anyString(), anyString(), anyInt())) |
Benedict Wong | 0febe5e | 2017-08-22 21:42:33 -0700 | [diff] [blame] | 253 | .thenReturn(returnSpi); |
Nathan Harold | 2e9a520 | 2017-09-26 11:44:23 -0700 | [diff] [blame] | 254 | |
Benedict Wong | 0febe5e | 2017-08-22 21:42:33 -0700 | [diff] [blame] | 255 | IpSecSpiResponse spi = |
Jonathan Basseri | 5fb9290 | 2017-11-16 10:58:01 -0800 | [diff] [blame] | 256 | mIpSecService.allocateSecurityParameterIndex( |
Benedict Wong | 0febe5e | 2017-08-22 21:42:33 -0700 | [diff] [blame] | 257 | NetworkUtils.numericToInetAddress(remoteAddress).getHostAddress(), |
| 258 | IpSecManager.INVALID_SECURITY_PARAMETER_INDEX, |
| 259 | new Binder()); |
| 260 | return spi.resourceId; |
| 261 | } |
Nathan Harold | 2e9a520 | 2017-09-26 11:44:23 -0700 | [diff] [blame] | 262 | |
Benedict Wong | 0febe5e | 2017-08-22 21:42:33 -0700 | [diff] [blame] | 263 | private void addDefaultSpisAndRemoteAddrToIpSecConfig(IpSecConfig config) throws Exception { |
Nathan Harold | a252331 | 2018-01-05 19:25:13 -0800 | [diff] [blame] | 264 | config.setSpiResourceId(getNewSpiResourceId(mDestinationAddr, TEST_SPI)); |
Nathan Harold | 5676f5f | 2018-01-16 19:34:01 -0800 | [diff] [blame] | 265 | config.setSourceAddress(mSourceAddr); |
Nathan Harold | a252331 | 2018-01-05 19:25:13 -0800 | [diff] [blame] | 266 | config.setDestinationAddress(mDestinationAddr); |
Benedict Wong | 0febe5e | 2017-08-22 21:42:33 -0700 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | private void addAuthAndCryptToIpSecConfig(IpSecConfig config) throws Exception { |
Nathan Harold | a252331 | 2018-01-05 19:25:13 -0800 | [diff] [blame] | 270 | config.setEncryption(CRYPT_ALGO); |
| 271 | config.setAuthentication(AUTH_ALGO); |
Nathan Harold | 2e9a520 | 2017-09-26 11:44:23 -0700 | [diff] [blame] | 272 | } |
| 273 | |
evitayan | 0a7d3e3 | 2018-03-22 13:42:07 -0700 | [diff] [blame] | 274 | private void addEncapSocketToIpSecConfig(int resourceId, IpSecConfig config) throws Exception { |
| 275 | config.setEncapType(IpSecTransform.ENCAP_ESPINUDP); |
| 276 | config.setEncapSocketResourceId(resourceId); |
| 277 | config.setEncapRemotePort(REMOTE_ENCAP_PORT); |
| 278 | } |
| 279 | |
| 280 | private void verifyTransformNetdCalledForCreatingSA( |
| 281 | IpSecConfig config, IpSecTransformResponse resp) throws Exception { |
| 282 | verifyTransformNetdCalledForCreatingSA(config, resp, 0); |
| 283 | } |
| 284 | |
| 285 | private void verifyTransformNetdCalledForCreatingSA( |
| 286 | IpSecConfig config, IpSecTransformResponse resp, int encapSocketPort) throws Exception { |
| 287 | IpSecAlgorithm auth = config.getAuthentication(); |
| 288 | IpSecAlgorithm crypt = config.getEncryption(); |
| 289 | IpSecAlgorithm authCrypt = config.getAuthenticatedEncryption(); |
| 290 | |
| 291 | verify(mMockNetd, times(1)) |
| 292 | .ipSecAddSecurityAssociation( |
| 293 | eq(mUid), |
| 294 | eq(config.getMode()), |
| 295 | eq(config.getSourceAddress()), |
| 296 | eq(config.getDestinationAddress()), |
| 297 | eq((config.getNetwork() != null) ? config.getNetwork().netId : 0), |
| 298 | eq(TEST_SPI), |
| 299 | eq(0), |
| 300 | eq(0), |
| 301 | eq((auth != null) ? auth.getName() : ""), |
| 302 | eq((auth != null) ? auth.getKey() : new byte[] {}), |
| 303 | eq((auth != null) ? auth.getTruncationLengthBits() : 0), |
| 304 | eq((crypt != null) ? crypt.getName() : ""), |
| 305 | eq((crypt != null) ? crypt.getKey() : new byte[] {}), |
| 306 | eq((crypt != null) ? crypt.getTruncationLengthBits() : 0), |
| 307 | eq((authCrypt != null) ? authCrypt.getName() : ""), |
| 308 | eq((authCrypt != null) ? authCrypt.getKey() : new byte[] {}), |
| 309 | eq((authCrypt != null) ? authCrypt.getTruncationLengthBits() : 0), |
| 310 | eq(config.getEncapType()), |
| 311 | eq(encapSocketPort), |
Benedict Wong | 781dae6 | 2018-09-06 11:31:25 -0700 | [diff] [blame] | 312 | eq(config.getEncapRemotePort()), |
| 313 | eq(config.getXfrmInterfaceId())); |
evitayan | 0a7d3e3 | 2018-03-22 13:42:07 -0700 | [diff] [blame] | 314 | } |
| 315 | |
Nathan Harold | 2e9a520 | 2017-09-26 11:44:23 -0700 | [diff] [blame] | 316 | @Test |
Benedict Wong | f33f0313 | 2018-01-18 14:38:16 -0800 | [diff] [blame] | 317 | public void testCreateTransform() throws Exception { |
Benedict Wong | 0febe5e | 2017-08-22 21:42:33 -0700 | [diff] [blame] | 318 | IpSecConfig ipSecConfig = new IpSecConfig(); |
| 319 | addDefaultSpisAndRemoteAddrToIpSecConfig(ipSecConfig); |
| 320 | addAuthAndCryptToIpSecConfig(ipSecConfig); |
Nathan Harold | 2e9a520 | 2017-09-26 11:44:23 -0700 | [diff] [blame] | 321 | |
| 322 | IpSecTransformResponse createTransformResp = |
Nathan Harold | 21208ee | 2018-03-15 18:06:06 -0700 | [diff] [blame] | 323 | mIpSecService.createTransform(ipSecConfig, new Binder(), "blessedPackage"); |
Nathan Harold | 2e9a520 | 2017-09-26 11:44:23 -0700 | [diff] [blame] | 324 | assertEquals(IpSecManager.Status.OK, createTransformResp.status); |
| 325 | |
evitayan | 0a7d3e3 | 2018-03-22 13:42:07 -0700 | [diff] [blame] | 326 | verifyTransformNetdCalledForCreatingSA(ipSecConfig, createTransformResp); |
Benedict Wong | 0febe5e | 2017-08-22 21:42:33 -0700 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | @Test |
Benedict Wong | f33f0313 | 2018-01-18 14:38:16 -0800 | [diff] [blame] | 330 | public void testCreateTransformAead() throws Exception { |
Benedict Wong | 0febe5e | 2017-08-22 21:42:33 -0700 | [diff] [blame] | 331 | IpSecConfig ipSecConfig = new IpSecConfig(); |
| 332 | addDefaultSpisAndRemoteAddrToIpSecConfig(ipSecConfig); |
| 333 | |
Nathan Harold | a252331 | 2018-01-05 19:25:13 -0800 | [diff] [blame] | 334 | ipSecConfig.setAuthenticatedEncryption(AEAD_ALGO); |
Benedict Wong | 0febe5e | 2017-08-22 21:42:33 -0700 | [diff] [blame] | 335 | |
| 336 | IpSecTransformResponse createTransformResp = |
Nathan Harold | 21208ee | 2018-03-15 18:06:06 -0700 | [diff] [blame] | 337 | mIpSecService.createTransform(ipSecConfig, new Binder(), "blessedPackage"); |
Benedict Wong | 0febe5e | 2017-08-22 21:42:33 -0700 | [diff] [blame] | 338 | assertEquals(IpSecManager.Status.OK, createTransformResp.status); |
| 339 | |
evitayan | 0a7d3e3 | 2018-03-22 13:42:07 -0700 | [diff] [blame] | 340 | verifyTransformNetdCalledForCreatingSA(ipSecConfig, createTransformResp); |
| 341 | } |
| 342 | |
| 343 | @Test |
| 344 | public void testCreateTransportModeTransformWithEncap() throws Exception { |
| 345 | IpSecUdpEncapResponse udpSock = mIpSecService.openUdpEncapsulationSocket(0, new Binder()); |
| 346 | |
| 347 | IpSecConfig ipSecConfig = new IpSecConfig(); |
| 348 | ipSecConfig.setMode(IpSecTransform.MODE_TRANSPORT); |
| 349 | addDefaultSpisAndRemoteAddrToIpSecConfig(ipSecConfig); |
| 350 | addAuthAndCryptToIpSecConfig(ipSecConfig); |
| 351 | addEncapSocketToIpSecConfig(udpSock.resourceId, ipSecConfig); |
| 352 | |
evitayan | e4259d3 | 2018-03-22 17:53:08 -0700 | [diff] [blame] | 353 | if (mFamily == AF_INET) { |
| 354 | IpSecTransformResponse createTransformResp = |
| 355 | mIpSecService.createTransform(ipSecConfig, new Binder(), "blessedPackage"); |
| 356 | assertEquals(IpSecManager.Status.OK, createTransformResp.status); |
evitayan | 0a7d3e3 | 2018-03-22 13:42:07 -0700 | [diff] [blame] | 357 | |
evitayan | e4259d3 | 2018-03-22 17:53:08 -0700 | [diff] [blame] | 358 | verifyTransformNetdCalledForCreatingSA(ipSecConfig, createTransformResp, udpSock.port); |
| 359 | } else { |
| 360 | try { |
| 361 | IpSecTransformResponse createTransformResp = |
| 362 | mIpSecService.createTransform(ipSecConfig, new Binder(), "blessedPackage"); |
| 363 | fail("Expected IllegalArgumentException on attempt to use UDP Encap in IPv6"); |
| 364 | } catch (IllegalArgumentException expected) { |
| 365 | } |
| 366 | } |
evitayan | 0a7d3e3 | 2018-03-22 13:42:07 -0700 | [diff] [blame] | 367 | } |
| 368 | |
| 369 | @Test |
| 370 | public void testCreateTunnelModeTransformWithEncap() throws Exception { |
| 371 | IpSecUdpEncapResponse udpSock = mIpSecService.openUdpEncapsulationSocket(0, new Binder()); |
| 372 | |
| 373 | IpSecConfig ipSecConfig = new IpSecConfig(); |
| 374 | ipSecConfig.setMode(IpSecTransform.MODE_TUNNEL); |
| 375 | addDefaultSpisAndRemoteAddrToIpSecConfig(ipSecConfig); |
| 376 | addAuthAndCryptToIpSecConfig(ipSecConfig); |
| 377 | addEncapSocketToIpSecConfig(udpSock.resourceId, ipSecConfig); |
| 378 | |
evitayan | e4259d3 | 2018-03-22 17:53:08 -0700 | [diff] [blame] | 379 | if (mFamily == AF_INET) { |
| 380 | IpSecTransformResponse createTransformResp = |
| 381 | mIpSecService.createTransform(ipSecConfig, new Binder(), "blessedPackage"); |
| 382 | assertEquals(IpSecManager.Status.OK, createTransformResp.status); |
evitayan | 0a7d3e3 | 2018-03-22 13:42:07 -0700 | [diff] [blame] | 383 | |
evitayan | e4259d3 | 2018-03-22 17:53:08 -0700 | [diff] [blame] | 384 | verifyTransformNetdCalledForCreatingSA(ipSecConfig, createTransformResp, udpSock.port); |
| 385 | } else { |
| 386 | try { |
| 387 | IpSecTransformResponse createTransformResp = |
| 388 | mIpSecService.createTransform(ipSecConfig, new Binder(), "blessedPackage"); |
| 389 | fail("Expected IllegalArgumentException on attempt to use UDP Encap in IPv6"); |
| 390 | } catch (IllegalArgumentException expected) { |
| 391 | } |
| 392 | } |
Nathan Harold | 2e9a520 | 2017-09-26 11:44:23 -0700 | [diff] [blame] | 393 | } |
| 394 | |
Andreas Gampe | a7b26b5 | 2018-02-26 08:06:30 -0800 | [diff] [blame] | 395 | @Test |
Benedict Wong | e6b4277 | 2017-12-13 18:26:40 -0800 | [diff] [blame] | 396 | public void testCreateTwoTransformsWithSameSpis() throws Exception { |
| 397 | IpSecConfig ipSecConfig = new IpSecConfig(); |
| 398 | addDefaultSpisAndRemoteAddrToIpSecConfig(ipSecConfig); |
| 399 | addAuthAndCryptToIpSecConfig(ipSecConfig); |
| 400 | |
| 401 | IpSecTransformResponse createTransformResp = |
Nathan Harold | 21208ee | 2018-03-15 18:06:06 -0700 | [diff] [blame] | 402 | mIpSecService.createTransform(ipSecConfig, new Binder(), "blessedPackage"); |
Benedict Wong | e6b4277 | 2017-12-13 18:26:40 -0800 | [diff] [blame] | 403 | assertEquals(IpSecManager.Status.OK, createTransformResp.status); |
| 404 | |
| 405 | // Attempting to create transform a second time with the same SPIs should throw an error... |
| 406 | try { |
Nathan Harold | 21208ee | 2018-03-15 18:06:06 -0700 | [diff] [blame] | 407 | mIpSecService.createTransform(ipSecConfig, new Binder(), "blessedPackage"); |
Benedict Wong | e6b4277 | 2017-12-13 18:26:40 -0800 | [diff] [blame] | 408 | fail("IpSecService should have thrown an error for reuse of SPI"); |
| 409 | } catch (IllegalStateException expected) { |
| 410 | } |
| 411 | |
| 412 | // ... even if the transform is deleted |
| 413 | mIpSecService.deleteTransform(createTransformResp.resourceId); |
| 414 | try { |
Nathan Harold | 21208ee | 2018-03-15 18:06:06 -0700 | [diff] [blame] | 415 | mIpSecService.createTransform(ipSecConfig, new Binder(), "blessedPackage"); |
Benedict Wong | e6b4277 | 2017-12-13 18:26:40 -0800 | [diff] [blame] | 416 | fail("IpSecService should have thrown an error for reuse of SPI"); |
| 417 | } catch (IllegalStateException expected) { |
| 418 | } |
| 419 | } |
| 420 | |
Nathan Harold | 2e9a520 | 2017-09-26 11:44:23 -0700 | [diff] [blame] | 421 | @Test |
Nathan Harold | 2279530 | 2018-02-27 19:19:40 -0800 | [diff] [blame] | 422 | public void testReleaseOwnedSpi() throws Exception { |
| 423 | IpSecConfig ipSecConfig = new IpSecConfig(); |
| 424 | addDefaultSpisAndRemoteAddrToIpSecConfig(ipSecConfig); |
| 425 | addAuthAndCryptToIpSecConfig(ipSecConfig); |
| 426 | |
| 427 | IpSecTransformResponse createTransformResp = |
Nathan Harold | 21208ee | 2018-03-15 18:06:06 -0700 | [diff] [blame] | 428 | mIpSecService.createTransform(ipSecConfig, new Binder(), "blessedPackage"); |
Benedict Wong | a098962 | 2018-07-25 13:06:29 -0700 | [diff] [blame] | 429 | IpSecService.UserRecord userRecord = mIpSecService.mUserResourceTracker.getUserRecord(mUid); |
Nathan Harold | 2279530 | 2018-02-27 19:19:40 -0800 | [diff] [blame] | 430 | assertEquals(1, userRecord.mSpiQuotaTracker.mCurrent); |
| 431 | mIpSecService.releaseSecurityParameterIndex(ipSecConfig.getSpiResourceId()); |
| 432 | verify(mMockNetd, times(0)) |
| 433 | .ipSecDeleteSecurityAssociation( |
Benedict Wong | a098962 | 2018-07-25 13:06:29 -0700 | [diff] [blame] | 434 | eq(mUid), |
Nathan Harold | 2279530 | 2018-02-27 19:19:40 -0800 | [diff] [blame] | 435 | anyString(), |
| 436 | anyString(), |
| 437 | eq(TEST_SPI), |
| 438 | anyInt(), |
Benedict Wong | 781dae6 | 2018-09-06 11:31:25 -0700 | [diff] [blame] | 439 | anyInt(), |
Nathan Harold | 2279530 | 2018-02-27 19:19:40 -0800 | [diff] [blame] | 440 | anyInt()); |
| 441 | // quota is not released until the SPI is released by the Transform |
| 442 | assertEquals(1, userRecord.mSpiQuotaTracker.mCurrent); |
| 443 | } |
| 444 | |
| 445 | @Test |
Benedict Wong | f33f0313 | 2018-01-18 14:38:16 -0800 | [diff] [blame] | 446 | public void testDeleteTransform() throws Exception { |
Benedict Wong | 0febe5e | 2017-08-22 21:42:33 -0700 | [diff] [blame] | 447 | IpSecConfig ipSecConfig = new IpSecConfig(); |
| 448 | addDefaultSpisAndRemoteAddrToIpSecConfig(ipSecConfig); |
| 449 | addAuthAndCryptToIpSecConfig(ipSecConfig); |
Nathan Harold | 2e9a520 | 2017-09-26 11:44:23 -0700 | [diff] [blame] | 450 | |
| 451 | IpSecTransformResponse createTransformResp = |
Nathan Harold | 21208ee | 2018-03-15 18:06:06 -0700 | [diff] [blame] | 452 | mIpSecService.createTransform(ipSecConfig, new Binder(), "blessedPackage"); |
Benedict Wong | f33f0313 | 2018-01-18 14:38:16 -0800 | [diff] [blame] | 453 | mIpSecService.deleteTransform(createTransformResp.resourceId); |
Nathan Harold | 2e9a520 | 2017-09-26 11:44:23 -0700 | [diff] [blame] | 454 | |
Nathan Harold | 2279530 | 2018-02-27 19:19:40 -0800 | [diff] [blame] | 455 | verify(mMockNetd, times(1)) |
Nathan Harold | 2e9a520 | 2017-09-26 11:44:23 -0700 | [diff] [blame] | 456 | .ipSecDeleteSecurityAssociation( |
Benedict Wong | a098962 | 2018-07-25 13:06:29 -0700 | [diff] [blame] | 457 | eq(mUid), |
Di Lu | 0b611f4 | 2018-01-11 11:35:25 -0800 | [diff] [blame] | 458 | anyString(), |
| 459 | anyString(), |
| 460 | eq(TEST_SPI), |
| 461 | anyInt(), |
Benedict Wong | 781dae6 | 2018-09-06 11:31:25 -0700 | [diff] [blame] | 462 | anyInt(), |
Di Lu | 0b611f4 | 2018-01-11 11:35:25 -0800 | [diff] [blame] | 463 | anyInt()); |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 464 | |
| 465 | // Verify quota and RefcountedResource objects cleaned up |
Benedict Wong | a098962 | 2018-07-25 13:06:29 -0700 | [diff] [blame] | 466 | IpSecService.UserRecord userRecord = mIpSecService.mUserResourceTracker.getUserRecord(mUid); |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 467 | assertEquals(0, userRecord.mTransformQuotaTracker.mCurrent); |
Nathan Harold | 2279530 | 2018-02-27 19:19:40 -0800 | [diff] [blame] | 468 | assertEquals(1, userRecord.mSpiQuotaTracker.mCurrent); |
| 469 | |
| 470 | mIpSecService.releaseSecurityParameterIndex(ipSecConfig.getSpiResourceId()); |
| 471 | // Verify that ipSecDeleteSa was not called when the SPI was released because the |
| 472 | // ownedByTransform property should prevent it; (note, the called count is cumulative). |
| 473 | verify(mMockNetd, times(1)) |
| 474 | .ipSecDeleteSecurityAssociation( |
| 475 | anyInt(), |
| 476 | anyString(), |
| 477 | anyString(), |
| 478 | anyInt(), |
| 479 | anyInt(), |
Benedict Wong | 781dae6 | 2018-09-06 11:31:25 -0700 | [diff] [blame] | 480 | anyInt(), |
Nathan Harold | 2279530 | 2018-02-27 19:19:40 -0800 | [diff] [blame] | 481 | anyInt()); |
| 482 | assertEquals(0, userRecord.mSpiQuotaTracker.mCurrent); |
| 483 | |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 484 | try { |
| 485 | userRecord.mTransformRecords.getRefcountedResourceOrThrow( |
| 486 | createTransformResp.resourceId); |
| 487 | fail("Expected IllegalArgumentException on attempt to access deleted resource"); |
| 488 | } catch (IllegalArgumentException expected) { |
| 489 | |
| 490 | } |
| 491 | } |
| 492 | |
| 493 | @Test |
| 494 | public void testTransportModeTransformBinderDeath() throws Exception { |
| 495 | IpSecConfig ipSecConfig = new IpSecConfig(); |
| 496 | addDefaultSpisAndRemoteAddrToIpSecConfig(ipSecConfig); |
| 497 | addAuthAndCryptToIpSecConfig(ipSecConfig); |
| 498 | |
| 499 | IpSecTransformResponse createTransformResp = |
Nathan Harold | 21208ee | 2018-03-15 18:06:06 -0700 | [diff] [blame] | 500 | mIpSecService.createTransform(ipSecConfig, new Binder(), "blessedPackage"); |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 501 | |
Benedict Wong | a098962 | 2018-07-25 13:06:29 -0700 | [diff] [blame] | 502 | IpSecService.UserRecord userRecord = mIpSecService.mUserResourceTracker.getUserRecord(mUid); |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 503 | IpSecService.RefcountedResource refcountedRecord = |
| 504 | userRecord.mTransformRecords.getRefcountedResourceOrThrow( |
| 505 | createTransformResp.resourceId); |
| 506 | |
| 507 | refcountedRecord.binderDied(); |
| 508 | |
| 509 | verify(mMockNetd) |
| 510 | .ipSecDeleteSecurityAssociation( |
Benedict Wong | a098962 | 2018-07-25 13:06:29 -0700 | [diff] [blame] | 511 | eq(mUid), |
Di Lu | 0b611f4 | 2018-01-11 11:35:25 -0800 | [diff] [blame] | 512 | anyString(), |
| 513 | anyString(), |
| 514 | eq(TEST_SPI), |
| 515 | anyInt(), |
Benedict Wong | 781dae6 | 2018-09-06 11:31:25 -0700 | [diff] [blame] | 516 | anyInt(), |
Di Lu | 0b611f4 | 2018-01-11 11:35:25 -0800 | [diff] [blame] | 517 | anyInt()); |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 518 | |
| 519 | // Verify quota and RefcountedResource objects cleaned up |
| 520 | assertEquals(0, userRecord.mTransformQuotaTracker.mCurrent); |
| 521 | try { |
| 522 | userRecord.mTransformRecords.getRefcountedResourceOrThrow( |
| 523 | createTransformResp.resourceId); |
| 524 | fail("Expected IllegalArgumentException on attempt to access deleted resource"); |
| 525 | } catch (IllegalArgumentException expected) { |
| 526 | |
| 527 | } |
Nathan Harold | 2e9a520 | 2017-09-26 11:44:23 -0700 | [diff] [blame] | 528 | } |
| 529 | |
| 530 | @Test |
| 531 | public void testApplyTransportModeTransform() throws Exception { |
Benedict Wong | 0febe5e | 2017-08-22 21:42:33 -0700 | [diff] [blame] | 532 | IpSecConfig ipSecConfig = new IpSecConfig(); |
| 533 | addDefaultSpisAndRemoteAddrToIpSecConfig(ipSecConfig); |
| 534 | addAuthAndCryptToIpSecConfig(ipSecConfig); |
Nathan Harold | 2e9a520 | 2017-09-26 11:44:23 -0700 | [diff] [blame] | 535 | |
| 536 | IpSecTransformResponse createTransformResp = |
Nathan Harold | 21208ee | 2018-03-15 18:06:06 -0700 | [diff] [blame] | 537 | mIpSecService.createTransform(ipSecConfig, new Binder(), "blessedPackage"); |
Nathan Harold | 2e9a520 | 2017-09-26 11:44:23 -0700 | [diff] [blame] | 538 | ParcelFileDescriptor pfd = ParcelFileDescriptor.fromSocket(new Socket()); |
| 539 | |
| 540 | int resourceId = createTransformResp.resourceId; |
Nathan Harold | a252331 | 2018-01-05 19:25:13 -0800 | [diff] [blame] | 541 | mIpSecService.applyTransportModeTransform(pfd, IpSecManager.DIRECTION_OUT, resourceId); |
Nathan Harold | 2e9a520 | 2017-09-26 11:44:23 -0700 | [diff] [blame] | 542 | |
| 543 | verify(mMockNetd) |
| 544 | .ipSecApplyTransportModeTransform( |
Luke Huang | e8e522b | 2018-11-23 12:01:41 +0800 | [diff] [blame^] | 545 | eq(pfd), |
Benedict Wong | a098962 | 2018-07-25 13:06:29 -0700 | [diff] [blame] | 546 | eq(mUid), |
Nathan Harold | a252331 | 2018-01-05 19:25:13 -0800 | [diff] [blame] | 547 | eq(IpSecManager.DIRECTION_OUT), |
Nathan Harold | 2e9a520 | 2017-09-26 11:44:23 -0700 | [diff] [blame] | 548 | anyString(), |
| 549 | anyString(), |
Nathan Harold | a252331 | 2018-01-05 19:25:13 -0800 | [diff] [blame] | 550 | eq(TEST_SPI)); |
Nathan Harold | 2e9a520 | 2017-09-26 11:44:23 -0700 | [diff] [blame] | 551 | } |
| 552 | |
| 553 | @Test |
| 554 | public void testRemoveTransportModeTransform() throws Exception { |
| 555 | ParcelFileDescriptor pfd = ParcelFileDescriptor.fromSocket(new Socket()); |
Nathan Harold | f73d252 | 2018-01-17 01:00:20 -0800 | [diff] [blame] | 556 | mIpSecService.removeTransportModeTransforms(pfd); |
Nathan Harold | 2e9a520 | 2017-09-26 11:44:23 -0700 | [diff] [blame] | 557 | |
Luke Huang | e8e522b | 2018-11-23 12:01:41 +0800 | [diff] [blame^] | 558 | verify(mMockNetd).ipSecRemoveTransportModeTransform(pfd); |
Nathan Harold | 2e9a520 | 2017-09-26 11:44:23 -0700 | [diff] [blame] | 559 | } |
Benedict Wong | ecc9f7c | 2018-03-01 18:53:07 -0800 | [diff] [blame] | 560 | |
| 561 | private IpSecTunnelInterfaceResponse createAndValidateTunnel( |
Nathan Harold | 21208ee | 2018-03-15 18:06:06 -0700 | [diff] [blame] | 562 | String localAddr, String remoteAddr, String pkgName) { |
Benedict Wong | ecc9f7c | 2018-03-01 18:53:07 -0800 | [diff] [blame] | 563 | IpSecTunnelInterfaceResponse createTunnelResp = |
| 564 | mIpSecService.createTunnelInterface( |
Nathan Harold | 21208ee | 2018-03-15 18:06:06 -0700 | [diff] [blame] | 565 | mSourceAddr, mDestinationAddr, fakeNetwork, new Binder(), pkgName); |
Benedict Wong | ecc9f7c | 2018-03-01 18:53:07 -0800 | [diff] [blame] | 566 | |
| 567 | assertNotNull(createTunnelResp); |
| 568 | assertEquals(IpSecManager.Status.OK, createTunnelResp.status); |
| 569 | return createTunnelResp; |
| 570 | } |
| 571 | |
| 572 | @Test |
| 573 | public void testCreateTunnelInterface() throws Exception { |
| 574 | IpSecTunnelInterfaceResponse createTunnelResp = |
Nathan Harold | 21208ee | 2018-03-15 18:06:06 -0700 | [diff] [blame] | 575 | createAndValidateTunnel(mSourceAddr, mDestinationAddr, "blessedPackage"); |
Benedict Wong | ecc9f7c | 2018-03-01 18:53:07 -0800 | [diff] [blame] | 576 | |
| 577 | // Check that we have stored the tracking object, and retrieve it |
Benedict Wong | a098962 | 2018-07-25 13:06:29 -0700 | [diff] [blame] | 578 | IpSecService.UserRecord userRecord = mIpSecService.mUserResourceTracker.getUserRecord(mUid); |
Benedict Wong | ecc9f7c | 2018-03-01 18:53:07 -0800 | [diff] [blame] | 579 | IpSecService.RefcountedResource refcountedRecord = |
| 580 | userRecord.mTunnelInterfaceRecords.getRefcountedResourceOrThrow( |
| 581 | createTunnelResp.resourceId); |
| 582 | |
| 583 | assertEquals(1, userRecord.mTunnelQuotaTracker.mCurrent); |
| 584 | verify(mMockNetd) |
Benedict Wong | 781dae6 | 2018-09-06 11:31:25 -0700 | [diff] [blame] | 585 | .ipSecAddTunnelInterface( |
Benedict Wong | ecc9f7c | 2018-03-01 18:53:07 -0800 | [diff] [blame] | 586 | eq(createTunnelResp.interfaceName), |
| 587 | eq(mSourceAddr), |
| 588 | eq(mDestinationAddr), |
| 589 | anyInt(), |
Benedict Wong | 781dae6 | 2018-09-06 11:31:25 -0700 | [diff] [blame] | 590 | anyInt(), |
Benedict Wong | ecc9f7c | 2018-03-01 18:53:07 -0800 | [diff] [blame] | 591 | anyInt()); |
| 592 | } |
| 593 | |
| 594 | @Test |
| 595 | public void testDeleteTunnelInterface() throws Exception { |
| 596 | IpSecTunnelInterfaceResponse createTunnelResp = |
Nathan Harold | 21208ee | 2018-03-15 18:06:06 -0700 | [diff] [blame] | 597 | createAndValidateTunnel(mSourceAddr, mDestinationAddr, "blessedPackage"); |
Benedict Wong | ecc9f7c | 2018-03-01 18:53:07 -0800 | [diff] [blame] | 598 | |
Benedict Wong | a098962 | 2018-07-25 13:06:29 -0700 | [diff] [blame] | 599 | IpSecService.UserRecord userRecord = mIpSecService.mUserResourceTracker.getUserRecord(mUid); |
Benedict Wong | ecc9f7c | 2018-03-01 18:53:07 -0800 | [diff] [blame] | 600 | |
Nathan Harold | 21208ee | 2018-03-15 18:06:06 -0700 | [diff] [blame] | 601 | mIpSecService.deleteTunnelInterface(createTunnelResp.resourceId, "blessedPackage"); |
Benedict Wong | ecc9f7c | 2018-03-01 18:53:07 -0800 | [diff] [blame] | 602 | |
| 603 | // Verify quota and RefcountedResource objects cleaned up |
| 604 | assertEquals(0, userRecord.mTunnelQuotaTracker.mCurrent); |
Benedict Wong | 781dae6 | 2018-09-06 11:31:25 -0700 | [diff] [blame] | 605 | verify(mMockNetd).ipSecRemoveTunnelInterface(eq(createTunnelResp.interfaceName)); |
Benedict Wong | ecc9f7c | 2018-03-01 18:53:07 -0800 | [diff] [blame] | 606 | try { |
| 607 | userRecord.mTunnelInterfaceRecords.getRefcountedResourceOrThrow( |
| 608 | createTunnelResp.resourceId); |
| 609 | fail("Expected IllegalArgumentException on attempt to access deleted resource"); |
| 610 | } catch (IllegalArgumentException expected) { |
| 611 | } |
| 612 | } |
| 613 | |
| 614 | @Test |
| 615 | public void testTunnelInterfaceBinderDeath() throws Exception { |
| 616 | IpSecTunnelInterfaceResponse createTunnelResp = |
Nathan Harold | 21208ee | 2018-03-15 18:06:06 -0700 | [diff] [blame] | 617 | createAndValidateTunnel(mSourceAddr, mDestinationAddr, "blessedPackage"); |
Benedict Wong | ecc9f7c | 2018-03-01 18:53:07 -0800 | [diff] [blame] | 618 | |
Benedict Wong | a098962 | 2018-07-25 13:06:29 -0700 | [diff] [blame] | 619 | IpSecService.UserRecord userRecord = mIpSecService.mUserResourceTracker.getUserRecord(mUid); |
Benedict Wong | ecc9f7c | 2018-03-01 18:53:07 -0800 | [diff] [blame] | 620 | IpSecService.RefcountedResource refcountedRecord = |
| 621 | userRecord.mTunnelInterfaceRecords.getRefcountedResourceOrThrow( |
| 622 | createTunnelResp.resourceId); |
| 623 | |
| 624 | refcountedRecord.binderDied(); |
| 625 | |
| 626 | // Verify quota and RefcountedResource objects cleaned up |
| 627 | assertEquals(0, userRecord.mTunnelQuotaTracker.mCurrent); |
Benedict Wong | 781dae6 | 2018-09-06 11:31:25 -0700 | [diff] [blame] | 628 | verify(mMockNetd).ipSecRemoveTunnelInterface(eq(createTunnelResp.interfaceName)); |
Benedict Wong | ecc9f7c | 2018-03-01 18:53:07 -0800 | [diff] [blame] | 629 | try { |
| 630 | userRecord.mTunnelInterfaceRecords.getRefcountedResourceOrThrow( |
| 631 | createTunnelResp.resourceId); |
| 632 | fail("Expected IllegalArgumentException on attempt to access deleted resource"); |
| 633 | } catch (IllegalArgumentException expected) { |
| 634 | } |
| 635 | } |
| 636 | |
| 637 | @Test |
Benedict Wong | 781dae6 | 2018-09-06 11:31:25 -0700 | [diff] [blame] | 638 | public void testApplyTunnelModeTransform() throws Exception { |
| 639 | IpSecConfig ipSecConfig = new IpSecConfig(); |
| 640 | ipSecConfig.setMode(IpSecTransform.MODE_TUNNEL); |
| 641 | addDefaultSpisAndRemoteAddrToIpSecConfig(ipSecConfig); |
| 642 | addAuthAndCryptToIpSecConfig(ipSecConfig); |
| 643 | |
| 644 | IpSecTransformResponse createTransformResp = |
| 645 | mIpSecService.createTransform(ipSecConfig, new Binder(), "blessedPackage"); |
| 646 | IpSecTunnelInterfaceResponse createTunnelResp = |
| 647 | createAndValidateTunnel(mSourceAddr, mDestinationAddr, "blessedPackage"); |
| 648 | |
| 649 | int transformResourceId = createTransformResp.resourceId; |
| 650 | int tunnelResourceId = createTunnelResp.resourceId; |
| 651 | mIpSecService.applyTunnelModeTransform(tunnelResourceId, IpSecManager.DIRECTION_OUT, |
| 652 | transformResourceId, "blessedPackage"); |
| 653 | |
| 654 | for (int selAddrFamily : ADDRESS_FAMILIES) { |
| 655 | verify(mMockNetd) |
| 656 | .ipSecUpdateSecurityPolicy( |
| 657 | eq(mUid), |
| 658 | eq(selAddrFamily), |
| 659 | eq(IpSecManager.DIRECTION_OUT), |
| 660 | anyString(), |
| 661 | anyString(), |
| 662 | eq(TEST_SPI), |
| 663 | anyInt(), // iKey/oKey |
| 664 | anyInt(), // mask |
| 665 | eq(tunnelResourceId)); |
| 666 | } |
| 667 | |
| 668 | ipSecConfig.setXfrmInterfaceId(tunnelResourceId); |
| 669 | verifyTransformNetdCalledForCreatingSA(ipSecConfig, createTransformResp); |
| 670 | } |
| 671 | |
| 672 | @Test |
Benedict Wong | ecc9f7c | 2018-03-01 18:53:07 -0800 | [diff] [blame] | 673 | public void testAddRemoveAddressFromTunnelInterface() throws Exception { |
Nathan Harold | 21208ee | 2018-03-15 18:06:06 -0700 | [diff] [blame] | 674 | for (String pkgName : new String[]{"blessedPackage", "systemPackage"}) { |
| 675 | IpSecTunnelInterfaceResponse createTunnelResp = |
| 676 | createAndValidateTunnel(mSourceAddr, mDestinationAddr, pkgName); |
| 677 | mIpSecService.addAddressToTunnelInterface( |
| 678 | createTunnelResp.resourceId, mLocalInnerAddress, pkgName); |
| 679 | verify(mMockNetd, times(1)) |
| 680 | .interfaceAddAddress( |
| 681 | eq(createTunnelResp.interfaceName), |
| 682 | eq(mLocalInnerAddress.getAddress().getHostAddress()), |
| 683 | eq(mLocalInnerAddress.getPrefixLength())); |
| 684 | mIpSecService.removeAddressFromTunnelInterface( |
| 685 | createTunnelResp.resourceId, mLocalInnerAddress, pkgName); |
| 686 | verify(mMockNetd, times(1)) |
| 687 | .interfaceDelAddress( |
| 688 | eq(createTunnelResp.interfaceName), |
| 689 | eq(mLocalInnerAddress.getAddress().getHostAddress()), |
| 690 | eq(mLocalInnerAddress.getPrefixLength())); |
| 691 | mIpSecService.deleteTunnelInterface(createTunnelResp.resourceId, pkgName); |
| 692 | } |
| 693 | } |
Benedict Wong | ecc9f7c | 2018-03-01 18:53:07 -0800 | [diff] [blame] | 694 | |
Nathan Harold | 8c69bcb | 2018-05-15 19:18:38 -0700 | [diff] [blame] | 695 | @Ignore |
Nathan Harold | 21208ee | 2018-03-15 18:06:06 -0700 | [diff] [blame] | 696 | @Test |
| 697 | public void testAddTunnelFailsForBadPackageName() throws Exception { |
| 698 | try { |
| 699 | IpSecTunnelInterfaceResponse createTunnelResp = |
| 700 | createAndValidateTunnel(mSourceAddr, mDestinationAddr, "badPackage"); |
| 701 | fail("Expected a SecurityException for badPackage."); |
| 702 | } catch (SecurityException expected) { |
| 703 | } |
Benedict Wong | ecc9f7c | 2018-03-01 18:53:07 -0800 | [diff] [blame] | 704 | } |
Nathan Harold | 2e9a520 | 2017-09-26 11:44:23 -0700 | [diff] [blame] | 705 | } |