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