1 /*
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 
17 package android.net;
18 
19 import static org.junit.Assert.assertEquals;
20 import static org.junit.Assert.assertFalse;
21 import static org.junit.Assert.assertNull;
22 import static org.junit.Assert.assertTrue;
23 
24 import android.os.Parcel;
25 
26 import androidx.test.filters.SmallTest;
27 
28 import org.junit.Test;
29 import org.junit.runner.RunWith;
30 import org.junit.runners.JUnit4;
31 
32 /** Unit tests for {@link IpSecConfig}. */
33 @SmallTest
34 @RunWith(JUnit4.class)
35 public class IpSecConfigTest {
36 
37     @Test
testDefaults()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 
getSampleConfig()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
testCopyConstructor()88     public void testCopyConstructor() {
89         IpSecConfig original = getSampleConfig();
90         IpSecConfig copy = new IpSecConfig(original);
91 
92         assertTrue(IpSecConfig.equals(original, copy));
93         assertFalse(original == copy);
94     }
95 
96     @Test
testParcelUnparcel()97     public void testParcelUnparcel() throws Exception {
98         assertParcelingIsLossless(new IpSecConfig());
99 
100         IpSecConfig c = getSampleConfig();
101         assertParcelingIsLossless(c);
102     }
103 
assertParcelingIsLossless(IpSecConfig ci)104     private void assertParcelingIsLossless(IpSecConfig ci) throws Exception {
105         Parcel p = Parcel.obtain();
106         ci.writeToParcel(p, 0);
107         p.setDataPosition(0);
108         IpSecConfig co = IpSecConfig.CREATOR.createFromParcel(p);
109         assertTrue(IpSecConfig.equals(co, ci));
110     }
111 }
112