blob: 25e225ef303ac6d747764439e333e51bba4d07f3 [file] [log] [blame]
Remi NGUYEN VAN678277c2021-05-11 13:37:06 +00001/*
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
19import static com.android.testutils.ParcelUtils.assertParcelSane;
20import static com.android.testutils.ParcelUtils.assertParcelingIsLossless;
21
22import static org.junit.Assert.assertEquals;
23import static org.junit.Assert.assertNotSame;
24import static org.junit.Assert.assertNull;
25
26import androidx.test.filters.SmallTest;
27
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());
41 assertEquals("", c.getSourceAddress());
42 assertEquals("", c.getDestinationAddress());
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());
48 assertNull(c.getEncryption());
49 assertNull(c.getAuthentication());
50 assertEquals(IpSecManager.INVALID_RESOURCE_ID, c.getSpiResourceId());
51 assertEquals(0, c.getXfrmInterfaceId());
52 }
53
54 private IpSecConfig getSampleConfig() {
55 IpSecConfig c = new IpSecConfig();
56 c.setMode(IpSecTransform.MODE_TUNNEL);
57 c.setSourceAddress("0.0.0.0");
58 c.setDestinationAddress("1.2.3.4");
59 c.setSpiResourceId(1984);
60 c.setEncryption(
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(
65 new IpSecAlgorithm(
66 IpSecAlgorithm.AUTH_HMAC_MD5,
67 new byte[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 0xA, 0xB, 0xC, 0xD, 0xE, 0xF, 0},
68 128));
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);
82 c.setXfrmInterfaceId(34);
83
84 return c;
85 }
86
87 @Test
88 public void testCopyConstructor() {
89 IpSecConfig original = getSampleConfig();
90 IpSecConfig copy = new IpSecConfig(original);
91
92 assertEquals(original, copy);
93 assertNotSame(original, copy);
94 }
95
96 @Test
97 public void testParcelUnparcel() {
98 assertParcelingIsLossless(new IpSecConfig());
99
100 IpSecConfig c = getSampleConfig();
101 assertParcelSane(c, 15);
102 }
103}