blob: be1a45501bb1958572feeb4e83a0151da637f82b [file] [log] [blame]
Nathan Harold8ebb2682017-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
19import static org.junit.Assert.assertEquals;
Benedict Wong86e84772018-02-06 20:43:21 -080020import static org.junit.Assert.assertFalse;
Nathan Harold8ebb2682017-09-25 19:33:13 -070021import static org.junit.Assert.assertNull;
22import static org.junit.Assert.assertTrue;
23
24import android.os.Parcel;
25import android.support.test.filters.SmallTest;
26
27import org.junit.Test;
28import org.junit.runner.RunWith;
29import org.junit.runners.JUnit4;
30
31/** Unit tests for {@link IpSecConfig}. */
32@SmallTest
33@RunWith(JUnit4.class)
34public class IpSecConfigTest {
35
36 @Test
37 public void testDefaults() throws Exception {
38 IpSecConfig c = new IpSecConfig();
39 assertEquals(IpSecTransform.MODE_TRANSPORT, c.getMode());
Nathan Harolda47ac2b2018-01-05 19:25:13 -080040 assertEquals("", c.getSourceAddress());
41 assertEquals("", c.getDestinationAddress());
Nathan Harold8ebb2682017-09-25 19:33:13 -070042 assertNull(c.getNetwork());
43 assertEquals(IpSecTransform.ENCAP_NONE, c.getEncapType());
44 assertEquals(IpSecManager.INVALID_RESOURCE_ID, c.getEncapSocketResourceId());
45 assertEquals(0, c.getEncapRemotePort());
46 assertEquals(0, c.getNattKeepaliveInterval());
Nathan Harolda47ac2b2018-01-05 19:25:13 -080047 assertNull(c.getEncryption());
48 assertNull(c.getAuthentication());
49 assertEquals(IpSecManager.INVALID_RESOURCE_ID, c.getSpiResourceId());
Benedict Wong29ec3e92018-09-06 11:31:25 -070050 assertEquals(0, c.getXfrmInterfaceId());
Nathan Harold8ebb2682017-09-25 19:33:13 -070051 }
52
Benedict Wong86e84772018-02-06 20:43:21 -080053 private IpSecConfig getSampleConfig() {
Nathan Harold8ebb2682017-09-25 19:33:13 -070054 IpSecConfig c = new IpSecConfig();
55 c.setMode(IpSecTransform.MODE_TUNNEL);
Nathan Harolda47ac2b2018-01-05 19:25:13 -080056 c.setSourceAddress("0.0.0.0");
57 c.setDestinationAddress("1.2.3.4");
Benedict Wong86e84772018-02-06 20:43:21 -080058 c.setSpiResourceId(1984);
Nathan Harold8ebb2682017-09-25 19:33:13 -070059 c.setEncryption(
Nathan Harold8ebb2682017-09-25 19:33:13 -070060 new IpSecAlgorithm(
61 IpSecAlgorithm.CRYPT_AES_CBC,
62 new byte[] {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0xA, 0xB, 0xC, 0xD, 0xE, 0xF}));
63 c.setAuthentication(
Nathan Harold8ebb2682017-09-25 19:33:13 -070064 new IpSecAlgorithm(
Nathan Harold22ac4e52017-11-09 16:49:33 -080065 IpSecAlgorithm.AUTH_HMAC_MD5,
Benedict Wongb0571f52018-03-28 13:10:40 -070066 new byte[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 0xA, 0xB, 0xC, 0xD, 0xE, 0xF, 0},
67 128));
Benedict Wong86e84772018-02-06 20:43:21 -080068 c.setAuthenticatedEncryption(
69 new IpSecAlgorithm(
70 IpSecAlgorithm.AUTH_CRYPT_AES_GCM,
71 new byte[] {
72 1, 2, 3, 4, 5, 6, 7, 8, 9, 0xA, 0xB, 0xC, 0xD, 0xE, 0xF, 0, 1, 2, 3, 4
73 },
74 128));
75 c.setEncapType(android.system.OsConstants.UDP_ENCAP_ESPINUDP);
76 c.setEncapSocketResourceId(7);
77 c.setEncapRemotePort(22);
78 c.setNattKeepaliveInterval(42);
79 c.setMarkValue(12);
80 c.setMarkMask(23);
Benedict Wong29ec3e92018-09-06 11:31:25 -070081 c.setXfrmInterfaceId(34);
Benedict Wong86e84772018-02-06 20:43:21 -080082
83 return c;
84 }
85
86 @Test
87 public void testCopyConstructor() {
88 IpSecConfig original = getSampleConfig();
89 IpSecConfig copy = new IpSecConfig(original);
90
91 assertTrue(IpSecConfig.equals(original, copy));
92 assertFalse(original == copy);
93 }
94
95 @Test
96 public void testParcelUnparcel() throws Exception {
97 assertParcelingIsLossless(new IpSecConfig());
98
99 IpSecConfig c = getSampleConfig();
Nathan Harold8ebb2682017-09-25 19:33:13 -0700100 assertParcelingIsLossless(c);
101 }
102
103 private void assertParcelingIsLossless(IpSecConfig ci) throws Exception {
104 Parcel p = Parcel.obtain();
105 ci.writeToParcel(p, 0);
106 p.setDataPosition(0);
107 IpSecConfig co = IpSecConfig.CREATOR.createFromParcel(p);
108 assertTrue(IpSecConfig.equals(co, ci));
109 }
110}