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