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 | 39175f2 | 2020-06-26 00:41:26 +0900 | [diff] [blame] | 19 | import static com.android.testutils.ParcelUtils.assertParcelSane; |
| 20 | import static com.android.testutils.ParcelUtils.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 | |
Remi NGUYEN VAN | 154cf1d | 2021-06-29 17:16:28 +0900 | [diff] [blame] | 26 | import android.os.Build; |
| 27 | |
Brett Chabot | 147f6cf | 2019-03-04 14:14:56 -0800 | [diff] [blame] | 28 | import androidx.test.filters.SmallTest; |
Nathan Harold | 4f4459d | 2017-09-25 19:33:13 -0700 | [diff] [blame] | 29 | |
Remi NGUYEN VAN | 154cf1d | 2021-06-29 17:16:28 +0900 | [diff] [blame] | 30 | import com.android.testutils.DevSdkIgnoreRule; |
| 31 | import com.android.testutils.DevSdkIgnoreRunner; |
| 32 | |
Nathan Harold | 4f4459d | 2017-09-25 19:33:13 -0700 | [diff] [blame] | 33 | import org.junit.Test; |
| 34 | import org.junit.runner.RunWith; |
Nathan Harold | 4f4459d | 2017-09-25 19:33:13 -0700 | [diff] [blame] | 35 | |
| 36 | /** Unit tests for {@link IpSecConfig}. */ |
| 37 | @SmallTest |
Remi NGUYEN VAN | 154cf1d | 2021-06-29 17:16:28 +0900 | [diff] [blame] | 38 | @RunWith(DevSdkIgnoreRunner.class) |
Paul Hu | 516d5dc | 2022-05-26 12:57:01 +0000 | [diff] [blame] | 39 | @DevSdkIgnoreRule.IgnoreUpTo(Build.VERSION_CODES.S_V2) |
Nathan Harold | 4f4459d | 2017-09-25 19:33:13 -0700 | [diff] [blame] | 40 | public class IpSecConfigTest { |
| 41 | |
| 42 | @Test |
| 43 | public void testDefaults() throws Exception { |
| 44 | IpSecConfig c = new IpSecConfig(); |
| 45 | assertEquals(IpSecTransform.MODE_TRANSPORT, c.getMode()); |
Nathan Harold | 3865a00 | 2018-01-05 19:25:13 -0800 | [diff] [blame] | 46 | assertEquals("", c.getSourceAddress()); |
| 47 | assertEquals("", c.getDestinationAddress()); |
Nathan Harold | 4f4459d | 2017-09-25 19:33:13 -0700 | [diff] [blame] | 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()); |
Nathan Harold | 3865a00 | 2018-01-05 19:25:13 -0800 | [diff] [blame] | 53 | assertNull(c.getEncryption()); |
| 54 | assertNull(c.getAuthentication()); |
| 55 | assertEquals(IpSecManager.INVALID_RESOURCE_ID, c.getSpiResourceId()); |
Benedict Wong | 12b7056 | 2018-09-06 11:31:25 -0700 | [diff] [blame] | 56 | assertEquals(0, c.getXfrmInterfaceId()); |
Nathan Harold | 4f4459d | 2017-09-25 19:33:13 -0700 | [diff] [blame] | 57 | } |
| 58 | |
Benedict Wong | 159abb6 | 2018-02-06 20:43:21 -0800 | [diff] [blame] | 59 | private IpSecConfig getSampleConfig() { |
Nathan Harold | 4f4459d | 2017-09-25 19:33:13 -0700 | [diff] [blame] | 60 | IpSecConfig c = new IpSecConfig(); |
| 61 | c.setMode(IpSecTransform.MODE_TUNNEL); |
Nathan Harold | 3865a00 | 2018-01-05 19:25:13 -0800 | [diff] [blame] | 62 | c.setSourceAddress("0.0.0.0"); |
| 63 | c.setDestinationAddress("1.2.3.4"); |
Benedict Wong | 159abb6 | 2018-02-06 20:43:21 -0800 | [diff] [blame] | 64 | c.setSpiResourceId(1984); |
Nathan Harold | 4f4459d | 2017-09-25 19:33:13 -0700 | [diff] [blame] | 65 | c.setEncryption( |
Nathan Harold | 4f4459d | 2017-09-25 19:33:13 -0700 | [diff] [blame] | 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( |
Nathan Harold | 4f4459d | 2017-09-25 19:33:13 -0700 | [diff] [blame] | 70 | new IpSecAlgorithm( |
Nathan Harold | 82c3470 | 2017-11-09 16:49:33 -0800 | [diff] [blame] | 71 | IpSecAlgorithm.AUTH_HMAC_MD5, |
Benedict Wong | 5f38c02 | 2018-03-28 13:10:40 -0700 | [diff] [blame] | 72 | new byte[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 0xA, 0xB, 0xC, 0xD, 0xE, 0xF, 0}, |
| 73 | 128)); |
Benedict Wong | 159abb6 | 2018-02-06 20:43:21 -0800 | [diff] [blame] | 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); |
Benedict Wong | 12b7056 | 2018-09-06 11:31:25 -0700 | [diff] [blame] | 87 | c.setXfrmInterfaceId(34); |
Benedict Wong | 159abb6 | 2018-02-06 20:43:21 -0800 | [diff] [blame] | 88 | |
| 89 | return c; |
| 90 | } |
| 91 | |
| 92 | @Test |
| 93 | public void testCopyConstructor() { |
| 94 | IpSecConfig original = getSampleConfig(); |
| 95 | IpSecConfig copy = new IpSecConfig(original); |
| 96 | |
Chalard Jean | af71836 | 2019-05-30 17:11:14 +0900 | [diff] [blame] | 97 | assertEquals(original, copy); |
| 98 | assertNotSame(original, copy); |
Benedict Wong | 159abb6 | 2018-02-06 20:43:21 -0800 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | @Test |
Chalard Jean | af71836 | 2019-05-30 17:11:14 +0900 | [diff] [blame] | 102 | public void testParcelUnparcel() { |
Benedict Wong | 159abb6 | 2018-02-06 20:43:21 -0800 | [diff] [blame] | 103 | assertParcelingIsLossless(new IpSecConfig()); |
| 104 | |
| 105 | IpSecConfig c = getSampleConfig(); |
Chalard Jean | af71836 | 2019-05-30 17:11:14 +0900 | [diff] [blame] | 106 | assertParcelSane(c, 15); |
Nathan Harold | 4f4459d | 2017-09-25 19:33:13 -0700 | [diff] [blame] | 107 | } |
| 108 | } |