blob: c9888b24b6da223aa6785d6e3ac6aec580fbe4df [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 Jeanaf718362019-05-30 17:11:14 +090019import static com.android.testutils.ParcelUtilsKt.assertParcelSane;
20import static com.android.testutils.ParcelUtilsKt.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
26import androidx.test.filters.SmallTest;
Nathan Harold4f4459d2017-09-25 19:33:13 -070027
28import org.junit.Test;
29import org.junit.runner.RunWith;
30import org.junit.runners.JUnit4;
31
32/** Unit tests for {@link IpSecConfig}. */
33@SmallTest
34@RunWith(JUnit4.class)
35public class IpSecConfigTest {
36
37 @Test
38 public void testDefaults() throws Exception {
39 IpSecConfig c = new IpSecConfig();
40 assertEquals(IpSecTransform.MODE_TRANSPORT, c.getMode());
Nathan Harold3865a002018-01-05 19:25:13 -080041 assertEquals("", c.getSourceAddress());
42 assertEquals("", c.getDestinationAddress());
Nathan Harold4f4459d2017-09-25 19:33:13 -070043 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 Harold3865a002018-01-05 19:25:13 -080048 assertNull(c.getEncryption());
49 assertNull(c.getAuthentication());
50 assertEquals(IpSecManager.INVALID_RESOURCE_ID, c.getSpiResourceId());
Benedict Wong12b70562018-09-06 11:31:25 -070051 assertEquals(0, c.getXfrmInterfaceId());
Nathan Harold4f4459d2017-09-25 19:33:13 -070052 }
53
Benedict Wong159abb62018-02-06 20:43:21 -080054 private IpSecConfig getSampleConfig() {
Nathan Harold4f4459d2017-09-25 19:33:13 -070055 IpSecConfig c = new IpSecConfig();
56 c.setMode(IpSecTransform.MODE_TUNNEL);
Nathan Harold3865a002018-01-05 19:25:13 -080057 c.setSourceAddress("0.0.0.0");
58 c.setDestinationAddress("1.2.3.4");
Benedict Wong159abb62018-02-06 20:43:21 -080059 c.setSpiResourceId(1984);
Nathan Harold4f4459d2017-09-25 19:33:13 -070060 c.setEncryption(
Nathan Harold4f4459d2017-09-25 19:33:13 -070061 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 Harold4f4459d2017-09-25 19:33:13 -070065 new IpSecAlgorithm(
Nathan Harold82c34702017-11-09 16:49:33 -080066 IpSecAlgorithm.AUTH_HMAC_MD5,
Benedict Wong5f38c022018-03-28 13:10:40 -070067 new byte[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 0xA, 0xB, 0xC, 0xD, 0xE, 0xF, 0},
68 128));
Benedict Wong159abb62018-02-06 20:43:21 -080069 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 Wong12b70562018-09-06 11:31:25 -070082 c.setXfrmInterfaceId(34);
Benedict Wong159abb62018-02-06 20:43:21 -080083
84 return c;
85 }
86
87 @Test
88 public void testCopyConstructor() {
89 IpSecConfig original = getSampleConfig();
90 IpSecConfig copy = new IpSecConfig(original);
91
Chalard Jeanaf718362019-05-30 17:11:14 +090092 assertEquals(original, copy);
93 assertNotSame(original, copy);
Benedict Wong159abb62018-02-06 20:43:21 -080094 }
95
96 @Test
Chalard Jeanaf718362019-05-30 17:11:14 +090097 public void testParcelUnparcel() {
Benedict Wong159abb62018-02-06 20:43:21 -080098 assertParcelingIsLossless(new IpSecConfig());
99
100 IpSecConfig c = getSampleConfig();
Chalard Jeanaf718362019-05-30 17:11:14 +0900101 assertParcelSane(c, 15);
Nathan Harold4f4459d2017-09-25 19:33:13 -0700102 }
103}