1 /*
2  * Copyright (C) 2021 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.vcn.persistablebundleutils;
18 
19 import static android.system.OsConstants.AF_INET;
20 import static android.system.OsConstants.AF_INET6;
21 
22 import static org.junit.Assert.assertEquals;
23 
24 import android.net.InetAddresses;
25 import android.net.ipsec.ike.ChildSaProposal;
26 import android.net.ipsec.ike.IkeTrafficSelector;
27 import android.net.ipsec.ike.TunnelModeChildSessionParams;
28 import android.os.PersistableBundle;
29 
30 import androidx.test.filters.SmallTest;
31 import androidx.test.runner.AndroidJUnit4;
32 
33 import org.junit.Test;
34 import org.junit.runner.RunWith;
35 
36 import java.net.Inet4Address;
37 import java.net.Inet6Address;
38 import java.util.concurrent.TimeUnit;
39 
40 @RunWith(AndroidJUnit4.class)
41 @SmallTest
42 public class TunnelModeChildSessionParamsUtilsTest {
43     // Package private for use in EncryptedTunnelParamsUtilsTest
createBuilderMinimum()44     static TunnelModeChildSessionParams.Builder createBuilderMinimum() {
45         final ChildSaProposal saProposal = SaProposalUtilsTest.buildTestChildSaProposal();
46         return new TunnelModeChildSessionParams.Builder().addSaProposal(saProposal);
47     }
48 
verifyPersistableBundleEncodeDecodeIsLossless( TunnelModeChildSessionParams params)49     private static void verifyPersistableBundleEncodeDecodeIsLossless(
50             TunnelModeChildSessionParams params) {
51         final PersistableBundle bundle =
52                 TunnelModeChildSessionParamsUtils.toPersistableBundle(params);
53         final TunnelModeChildSessionParams result =
54                 TunnelModeChildSessionParamsUtils.fromPersistableBundle(bundle);
55 
56         assertEquals(params, result);
57     }
58 
59     @Test
testMinimumParamsEncodeDecodeIsLossless()60     public void testMinimumParamsEncodeDecodeIsLossless() throws Exception {
61         final TunnelModeChildSessionParams sessionParams = createBuilderMinimum().build();
62         verifyPersistableBundleEncodeDecodeIsLossless(sessionParams);
63     }
64 
65     @Test
testSetTsEncodeDecodeIsLossless()66     public void testSetTsEncodeDecodeIsLossless() throws Exception {
67         final IkeTrafficSelector tsInbound =
68                 new IkeTrafficSelector(
69                         16,
70                         65520,
71                         InetAddresses.parseNumericAddress("192.0.2.100"),
72                         InetAddresses.parseNumericAddress("192.0.2.101"));
73         final IkeTrafficSelector tsOutbound =
74                 new IkeTrafficSelector(
75                         32,
76                         256,
77                         InetAddresses.parseNumericAddress("192.0.2.200"),
78                         InetAddresses.parseNumericAddress("192.0.2.255"));
79 
80         final TunnelModeChildSessionParams sessionParams =
81                 createBuilderMinimum()
82                         .addInboundTrafficSelectors(tsInbound)
83                         .addOutboundTrafficSelectors(tsOutbound)
84                         .build();
85         verifyPersistableBundleEncodeDecodeIsLossless(sessionParams);
86     }
87 
88     @Test
testSetLifetimesEncodeDecodeIsLossless()89     public void testSetLifetimesEncodeDecodeIsLossless() throws Exception {
90         final int hardLifetime = (int) TimeUnit.HOURS.toSeconds(3L);
91         final int softLifetime = (int) TimeUnit.HOURS.toSeconds(1L);
92 
93         final TunnelModeChildSessionParams sessionParams =
94                 createBuilderMinimum().setLifetimeSeconds(hardLifetime, softLifetime).build();
95         verifyPersistableBundleEncodeDecodeIsLossless(sessionParams);
96     }
97 
98     @Test
testSetConfigRequestsEncodeDecodeIsLossless()99     public void testSetConfigRequestsEncodeDecodeIsLossless() throws Exception {
100         final int ipv6PrefixLen = 64;
101         final Inet4Address ipv4Address =
102                 (Inet4Address) InetAddresses.parseNumericAddress("192.0.2.100");
103         final Inet6Address ipv6Address =
104                 (Inet6Address) InetAddresses.parseNumericAddress("2001:db8::1");
105 
106         final TunnelModeChildSessionParams sessionParams =
107                 createBuilderMinimum()
108                         .addInternalAddressRequest(AF_INET)
109                         .addInternalAddressRequest(AF_INET6)
110                         .addInternalAddressRequest(ipv4Address)
111                         .addInternalAddressRequest(ipv6Address, ipv6PrefixLen)
112                         .addInternalDnsServerRequest(AF_INET)
113                         .addInternalDnsServerRequest(AF_INET6)
114                         .addInternalDhcpServerRequest(AF_INET)
115                         .build();
116         verifyPersistableBundleEncodeDecodeIsLossless(sessionParams);
117     }
118 }
119