blob: 9f83036b205515b3d9739324eb880dfe069ee3c0 [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 VAN154cf1d2021-06-29 17:16:28 +090026import android.os.Build;
27
Brett Chabot147f6cf2019-03-04 14:14:56 -080028import androidx.test.filters.SmallTest;
Nathan Harold4f4459d2017-09-25 19:33:13 -070029
Remi NGUYEN VAN154cf1d2021-06-29 17:16:28 +090030import com.android.testutils.DevSdkIgnoreRule;
31import com.android.testutils.DevSdkIgnoreRunner;
32
Nathan Harold4f4459d2017-09-25 19:33:13 -070033import org.junit.Test;
34import org.junit.runner.RunWith;
Nathan Harold4f4459d2017-09-25 19:33:13 -070035
36/** Unit tests for {@link IpSecConfig}. */
37@SmallTest
Remi NGUYEN VAN154cf1d2021-06-29 17:16:28 +090038@RunWith(DevSdkIgnoreRunner.class)
Paul Hu516d5dc2022-05-26 12:57:01 +000039@DevSdkIgnoreRule.IgnoreUpTo(Build.VERSION_CODES.S_V2)
Nathan Harold4f4459d2017-09-25 19:33:13 -070040public class IpSecConfigTest {
41
42 @Test
43 public void testDefaults() throws Exception {
44 IpSecConfig c = new IpSecConfig();
45 assertEquals(IpSecTransform.MODE_TRANSPORT, c.getMode());
Nathan Harold3865a002018-01-05 19:25:13 -080046 assertEquals("", c.getSourceAddress());
47 assertEquals("", c.getDestinationAddress());
Nathan Harold4f4459d2017-09-25 19:33:13 -070048 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 Harold3865a002018-01-05 19:25:13 -080053 assertNull(c.getEncryption());
54 assertNull(c.getAuthentication());
55 assertEquals(IpSecManager.INVALID_RESOURCE_ID, c.getSpiResourceId());
Benedict Wong12b70562018-09-06 11:31:25 -070056 assertEquals(0, c.getXfrmInterfaceId());
Nathan Harold4f4459d2017-09-25 19:33:13 -070057 }
58
Benedict Wong159abb62018-02-06 20:43:21 -080059 private IpSecConfig getSampleConfig() {
Nathan Harold4f4459d2017-09-25 19:33:13 -070060 IpSecConfig c = new IpSecConfig();
61 c.setMode(IpSecTransform.MODE_TUNNEL);
Nathan Harold3865a002018-01-05 19:25:13 -080062 c.setSourceAddress("0.0.0.0");
63 c.setDestinationAddress("1.2.3.4");
Benedict Wong159abb62018-02-06 20:43:21 -080064 c.setSpiResourceId(1984);
Nathan Harold4f4459d2017-09-25 19:33:13 -070065 c.setEncryption(
Nathan Harold4f4459d2017-09-25 19:33:13 -070066 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 Harold4f4459d2017-09-25 19:33:13 -070070 new IpSecAlgorithm(
Nathan Harold82c34702017-11-09 16:49:33 -080071 IpSecAlgorithm.AUTH_HMAC_MD5,
Benedict Wong5f38c022018-03-28 13:10:40 -070072 new byte[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 0xA, 0xB, 0xC, 0xD, 0xE, 0xF, 0},
73 128));
Benedict Wong159abb62018-02-06 20:43:21 -080074 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 Wong12b70562018-09-06 11:31:25 -070087 c.setXfrmInterfaceId(34);
Benedict Wong159abb62018-02-06 20:43:21 -080088
89 return c;
90 }
91
92 @Test
93 public void testCopyConstructor() {
94 IpSecConfig original = getSampleConfig();
95 IpSecConfig copy = new IpSecConfig(original);
96
Chalard Jeanaf718362019-05-30 17:11:14 +090097 assertEquals(original, copy);
98 assertNotSame(original, copy);
Benedict Wong159abb62018-02-06 20:43:21 -080099 }
100
101 @Test
Chalard Jeanaf718362019-05-30 17:11:14 +0900102 public void testParcelUnparcel() {
Benedict Wong159abb62018-02-06 20:43:21 -0800103 assertParcelingIsLossless(new IpSecConfig());
104
105 IpSecConfig c = getSampleConfig();
Chalard Jeanaf718362019-05-30 17:11:14 +0900106 assertParcelSane(c, 15);
Nathan Harold4f4459d2017-09-25 19:33:13 -0700107 }
108}