blob: 457c92372c72e8431ed0857d9c24378a6ddd39ed [file] [log] [blame]
Nathan Harold4f4459d2017-09-25 19:33:13 -07001/*
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
17package android.net;
18
Chalard Jean39175f22020-06-26 00:41:26 +090019import static com.android.testutils.ParcelUtils.assertParcelSane;
20import static com.android.testutils.ParcelUtils.assertParcelingIsLossless;
Nathan Harold4f4459d2017-09-25 19:33:13 -070021
Chalard Jeanaf718362019-05-30 17:11:14 +090022import static org.junit.Assert.assertEquals;
23import static org.junit.Assert.assertNotSame;
24import static org.junit.Assert.assertNull;
Brett Chabot147f6cf2019-03-04 14:14:56 -080025
Remi NGUYEN VAN05dd1ba2021-06-17 15:34:25 +090026import android.os.Build;
27
28import androidx.test.filters.SdkSuppress;
Brett Chabot147f6cf2019-03-04 14:14:56 -080029import androidx.test.filters.SmallTest;
Nathan Harold4f4459d2017-09-25 19:33:13 -070030
31import org.junit.Test;
32import org.junit.runner.RunWith;
33import org.junit.runners.JUnit4;
34
35/** Unit tests for {@link IpSecConfig}. */
36@SmallTest
37@RunWith(JUnit4.class)
Remi NGUYEN VAN05dd1ba2021-06-17 15:34:25 +090038@SdkSuppress(minSdkVersion = Build.VERSION_CODES.S, codeName = "S")
Nathan Harold4f4459d2017-09-25 19:33:13 -070039public class IpSecConfigTest {
40
41 @Test
42 public void testDefaults() throws Exception {
43 IpSecConfig c = new IpSecConfig();
44 assertEquals(IpSecTransform.MODE_TRANSPORT, c.getMode());
Nathan Harold3865a002018-01-05 19:25:13 -080045 assertEquals("", c.getSourceAddress());
46 assertEquals("", c.getDestinationAddress());
Nathan Harold4f4459d2017-09-25 19:33:13 -070047 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 Harold3865a002018-01-05 19:25:13 -080052 assertNull(c.getEncryption());
53 assertNull(c.getAuthentication());
54 assertEquals(IpSecManager.INVALID_RESOURCE_ID, c.getSpiResourceId());
Benedict Wong12b70562018-09-06 11:31:25 -070055 assertEquals(0, c.getXfrmInterfaceId());
Nathan Harold4f4459d2017-09-25 19:33:13 -070056 }
57
Benedict Wong159abb62018-02-06 20:43:21 -080058 private IpSecConfig getSampleConfig() {
Nathan Harold4f4459d2017-09-25 19:33:13 -070059 IpSecConfig c = new IpSecConfig();
60 c.setMode(IpSecTransform.MODE_TUNNEL);
Nathan Harold3865a002018-01-05 19:25:13 -080061 c.setSourceAddress("0.0.0.0");
62 c.setDestinationAddress("1.2.3.4");
Benedict Wong159abb62018-02-06 20:43:21 -080063 c.setSpiResourceId(1984);
Nathan Harold4f4459d2017-09-25 19:33:13 -070064 c.setEncryption(
Nathan Harold4f4459d2017-09-25 19:33:13 -070065 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 Harold4f4459d2017-09-25 19:33:13 -070069 new IpSecAlgorithm(
Nathan Harold82c34702017-11-09 16:49:33 -080070 IpSecAlgorithm.AUTH_HMAC_MD5,
Benedict Wong5f38c022018-03-28 13:10:40 -070071 new byte[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 0xA, 0xB, 0xC, 0xD, 0xE, 0xF, 0},
72 128));
Benedict Wong159abb62018-02-06 20:43:21 -080073 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 Wong12b70562018-09-06 11:31:25 -070086 c.setXfrmInterfaceId(34);
Benedict Wong159abb62018-02-06 20:43:21 -080087
88 return c;
89 }
90
91 @Test
92 public void testCopyConstructor() {
93 IpSecConfig original = getSampleConfig();
94 IpSecConfig copy = new IpSecConfig(original);
95
Chalard Jeanaf718362019-05-30 17:11:14 +090096 assertEquals(original, copy);
97 assertNotSame(original, copy);
Benedict Wong159abb62018-02-06 20:43:21 -080098 }
99
100 @Test
Chalard Jeanaf718362019-05-30 17:11:14 +0900101 public void testParcelUnparcel() {
Benedict Wong159abb62018-02-06 20:43:21 -0800102 assertParcelingIsLossless(new IpSecConfig());
103
104 IpSecConfig c = getSampleConfig();
Chalard Jeanaf718362019-05-30 17:11:14 +0900105 assertParcelSane(c, 15);
Nathan Harold4f4459d2017-09-25 19:33:13 -0700106 }
107}