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 android.net; 18 19 import static com.android.testutils.ParcelUtils.assertParcelSane; 20 import static com.android.testutils.ParcelUtils.assertParcelingIsLossless; 21 22 import static org.junit.Assert.assertEquals; 23 import static org.junit.Assert.assertNotSame; 24 import static org.junit.Assert.assertNull; 25 26 import android.os.Build; 27 28 import androidx.test.filters.SmallTest; 29 30 import com.android.testutils.DevSdkIgnoreRule; 31 import com.android.testutils.DevSdkIgnoreRunner; 32 33 import org.junit.Test; 34 import org.junit.runner.RunWith; 35 36 /** Unit tests for {@link IpSecConfig}. */ 37 @SmallTest 38 @RunWith(DevSdkIgnoreRunner.class) 39 @DevSdkIgnoreRule.IgnoreUpTo(Build.VERSION_CODES.R) 40 public class IpSecConfigTest { 41 42 @Test testDefaults()43 public void testDefaults() throws Exception { 44 IpSecConfig c = new IpSecConfig(); 45 assertEquals(IpSecTransform.MODE_TRANSPORT, c.getMode()); 46 assertEquals("", c.getSourceAddress()); 47 assertEquals("", c.getDestinationAddress()); 48 assertNull(c.getNetwork()); 49 assertEquals(IpSecTransform.ENCAP_NONE, c.getEncapType()); 50 assertEquals(IpSecManager.INVALID_RESOURCE_ID, c.getEncapSocketResourceId()); 51 assertEquals(0, c.getEncapRemotePort()); 52 assertEquals(0, c.getNattKeepaliveInterval()); 53 assertNull(c.getEncryption()); 54 assertNull(c.getAuthentication()); 55 assertEquals(IpSecManager.INVALID_RESOURCE_ID, c.getSpiResourceId()); 56 assertEquals(0, c.getXfrmInterfaceId()); 57 } 58 getSampleConfig()59 private IpSecConfig getSampleConfig() { 60 IpSecConfig c = new IpSecConfig(); 61 c.setMode(IpSecTransform.MODE_TUNNEL); 62 c.setSourceAddress("0.0.0.0"); 63 c.setDestinationAddress("1.2.3.4"); 64 c.setSpiResourceId(1984); 65 c.setEncryption( 66 new IpSecAlgorithm( 67 IpSecAlgorithm.CRYPT_AES_CBC, 68 new byte[] {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0xA, 0xB, 0xC, 0xD, 0xE, 0xF})); 69 c.setAuthentication( 70 new IpSecAlgorithm( 71 IpSecAlgorithm.AUTH_HMAC_MD5, 72 new byte[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 0xA, 0xB, 0xC, 0xD, 0xE, 0xF, 0}, 73 128)); 74 c.setAuthenticatedEncryption( 75 new IpSecAlgorithm( 76 IpSecAlgorithm.AUTH_CRYPT_AES_GCM, 77 new byte[] { 78 1, 2, 3, 4, 5, 6, 7, 8, 9, 0xA, 0xB, 0xC, 0xD, 0xE, 0xF, 0, 1, 2, 3, 4 79 }, 80 128)); 81 c.setEncapType(android.system.OsConstants.UDP_ENCAP_ESPINUDP); 82 c.setEncapSocketResourceId(7); 83 c.setEncapRemotePort(22); 84 c.setNattKeepaliveInterval(42); 85 c.setMarkValue(12); 86 c.setMarkMask(23); 87 c.setXfrmInterfaceId(34); 88 89 return c; 90 } 91 92 @Test testCopyConstructor()93 public void testCopyConstructor() { 94 IpSecConfig original = getSampleConfig(); 95 IpSecConfig copy = new IpSecConfig(original); 96 97 assertEquals(original, copy); 98 assertNotSame(original, copy); 99 } 100 101 @Test testParcelUnparcel()102 public void testParcelUnparcel() { 103 assertParcelingIsLossless(new IpSecConfig()); 104 105 IpSecConfig c = getSampleConfig(); 106 assertParcelSane(c, 15); 107 } 108 } 109