Nathan Harold | 4f4459d | 2017-09-25 19:33:13 -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 android.net; |
| 18 | |
Chalard Jean | af71836 | 2019-05-30 17:11:14 +0900 | [diff] [blame^] | 19 | import static com.android.testutils.ParcelUtilsKt.assertParcelSane; |
| 20 | import static com.android.testutils.ParcelUtilsKt.assertParcelingIsLossless; |
Nathan Harold | 4f4459d | 2017-09-25 19:33:13 -0700 | [diff] [blame] | 21 | |
Chalard Jean | af71836 | 2019-05-30 17:11:14 +0900 | [diff] [blame^] | 22 | import static org.junit.Assert.assertEquals; |
| 23 | import static org.junit.Assert.assertNotSame; |
| 24 | import static org.junit.Assert.assertNull; |
Brett Chabot | 147f6cf | 2019-03-04 14:14:56 -0800 | [diff] [blame] | 25 | |
| 26 | import androidx.test.filters.SmallTest; |
Nathan Harold | 4f4459d | 2017-09-25 19:33:13 -0700 | [diff] [blame] | 27 | |
| 28 | import org.junit.Test; |
| 29 | import org.junit.runner.RunWith; |
| 30 | import org.junit.runners.JUnit4; |
| 31 | |
| 32 | /** Unit tests for {@link IpSecConfig}. */ |
| 33 | @SmallTest |
| 34 | @RunWith(JUnit4.class) |
| 35 | public class IpSecConfigTest { |
| 36 | |
| 37 | @Test |
| 38 | public void testDefaults() throws Exception { |
| 39 | IpSecConfig c = new IpSecConfig(); |
| 40 | assertEquals(IpSecTransform.MODE_TRANSPORT, c.getMode()); |
Nathan Harold | 3865a00 | 2018-01-05 19:25:13 -0800 | [diff] [blame] | 41 | assertEquals("", c.getSourceAddress()); |
| 42 | assertEquals("", c.getDestinationAddress()); |
Nathan Harold | 4f4459d | 2017-09-25 19:33:13 -0700 | [diff] [blame] | 43 | assertNull(c.getNetwork()); |
| 44 | assertEquals(IpSecTransform.ENCAP_NONE, c.getEncapType()); |
| 45 | assertEquals(IpSecManager.INVALID_RESOURCE_ID, c.getEncapSocketResourceId()); |
| 46 | assertEquals(0, c.getEncapRemotePort()); |
| 47 | assertEquals(0, c.getNattKeepaliveInterval()); |
Nathan Harold | 3865a00 | 2018-01-05 19:25:13 -0800 | [diff] [blame] | 48 | assertNull(c.getEncryption()); |
| 49 | assertNull(c.getAuthentication()); |
| 50 | assertEquals(IpSecManager.INVALID_RESOURCE_ID, c.getSpiResourceId()); |
Benedict Wong | 12b7056 | 2018-09-06 11:31:25 -0700 | [diff] [blame] | 51 | assertEquals(0, c.getXfrmInterfaceId()); |
Nathan Harold | 4f4459d | 2017-09-25 19:33:13 -0700 | [diff] [blame] | 52 | } |
| 53 | |
Benedict Wong | 159abb6 | 2018-02-06 20:43:21 -0800 | [diff] [blame] | 54 | private IpSecConfig getSampleConfig() { |
Nathan Harold | 4f4459d | 2017-09-25 19:33:13 -0700 | [diff] [blame] | 55 | IpSecConfig c = new IpSecConfig(); |
| 56 | c.setMode(IpSecTransform.MODE_TUNNEL); |
Nathan Harold | 3865a00 | 2018-01-05 19:25:13 -0800 | [diff] [blame] | 57 | c.setSourceAddress("0.0.0.0"); |
| 58 | c.setDestinationAddress("1.2.3.4"); |
Benedict Wong | 159abb6 | 2018-02-06 20:43:21 -0800 | [diff] [blame] | 59 | c.setSpiResourceId(1984); |
Nathan Harold | 4f4459d | 2017-09-25 19:33:13 -0700 | [diff] [blame] | 60 | c.setEncryption( |
Nathan Harold | 4f4459d | 2017-09-25 19:33:13 -0700 | [diff] [blame] | 61 | new IpSecAlgorithm( |
| 62 | IpSecAlgorithm.CRYPT_AES_CBC, |
| 63 | new byte[] {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0xA, 0xB, 0xC, 0xD, 0xE, 0xF})); |
| 64 | c.setAuthentication( |
Nathan Harold | 4f4459d | 2017-09-25 19:33:13 -0700 | [diff] [blame] | 65 | new IpSecAlgorithm( |
Nathan Harold | 82c3470 | 2017-11-09 16:49:33 -0800 | [diff] [blame] | 66 | IpSecAlgorithm.AUTH_HMAC_MD5, |
Benedict Wong | 5f38c02 | 2018-03-28 13:10:40 -0700 | [diff] [blame] | 67 | new byte[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 0xA, 0xB, 0xC, 0xD, 0xE, 0xF, 0}, |
| 68 | 128)); |
Benedict Wong | 159abb6 | 2018-02-06 20:43:21 -0800 | [diff] [blame] | 69 | c.setAuthenticatedEncryption( |
| 70 | new IpSecAlgorithm( |
| 71 | IpSecAlgorithm.AUTH_CRYPT_AES_GCM, |
| 72 | new byte[] { |
| 73 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 0xA, 0xB, 0xC, 0xD, 0xE, 0xF, 0, 1, 2, 3, 4 |
| 74 | }, |
| 75 | 128)); |
| 76 | c.setEncapType(android.system.OsConstants.UDP_ENCAP_ESPINUDP); |
| 77 | c.setEncapSocketResourceId(7); |
| 78 | c.setEncapRemotePort(22); |
| 79 | c.setNattKeepaliveInterval(42); |
| 80 | c.setMarkValue(12); |
| 81 | c.setMarkMask(23); |
Benedict Wong | 12b7056 | 2018-09-06 11:31:25 -0700 | [diff] [blame] | 82 | c.setXfrmInterfaceId(34); |
Benedict Wong | 159abb6 | 2018-02-06 20:43:21 -0800 | [diff] [blame] | 83 | |
| 84 | return c; |
| 85 | } |
| 86 | |
| 87 | @Test |
| 88 | public void testCopyConstructor() { |
| 89 | IpSecConfig original = getSampleConfig(); |
| 90 | IpSecConfig copy = new IpSecConfig(original); |
| 91 | |
Chalard Jean | af71836 | 2019-05-30 17:11:14 +0900 | [diff] [blame^] | 92 | assertEquals(original, copy); |
| 93 | assertNotSame(original, copy); |
Benedict Wong | 159abb6 | 2018-02-06 20:43:21 -0800 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | @Test |
Chalard Jean | af71836 | 2019-05-30 17:11:14 +0900 | [diff] [blame^] | 97 | public void testParcelUnparcel() { |
Benedict Wong | 159abb6 | 2018-02-06 20:43:21 -0800 | [diff] [blame] | 98 | assertParcelingIsLossless(new IpSecConfig()); |
| 99 | |
| 100 | IpSecConfig c = getSampleConfig(); |
Chalard Jean | af71836 | 2019-05-30 17:11:14 +0900 | [diff] [blame^] | 101 | assertParcelSane(c, 15); |
Nathan Harold | 4f4459d | 2017-09-25 19:33:13 -0700 | [diff] [blame] | 102 | } |
| 103 | } |