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.cts;
18 
19 import static android.net.NetworkCapabilities.NET_CAPABILITY_DUN;
20 import static android.net.NetworkCapabilities.NET_CAPABILITY_INTERNET;
21 import static android.net.ipsec.ike.IkeSessionParams.IKE_OPTION_MOBIKE;
22 import static android.net.vcn.VcnGatewayConnectionConfig.VCN_GATEWAY_OPTION_ENABLE_DATA_STALL_RECOVERY_WITH_MOBILITY;
23 
24 import static org.junit.Assert.assertArrayEquals;
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertFalse;
27 import static org.junit.Assert.assertTrue;
28 import static org.junit.Assert.fail;
29 
30 import android.net.ipsec.ike.IkeSessionParams;
31 import android.net.ipsec.ike.IkeTunnelConnectionParams;
32 import android.net.vcn.Flags;
33 import android.net.vcn.VcnGatewayConnectionConfig;
34 import android.net.vcn.VcnUnderlyingNetworkTemplate;
35 import android.platform.test.annotations.RequiresFlagsEnabled;
36 import android.platform.test.flag.junit.CheckFlagsRule;
37 import android.platform.test.flag.junit.DeviceFlagsValueProvider;
38 
39 import androidx.test.ext.junit.runners.AndroidJUnit4;
40 
41 import org.junit.Rule;
42 import org.junit.Test;
43 import org.junit.runner.RunWith;
44 
45 import java.util.ArrayList;
46 import java.util.Collections;
47 import java.util.List;
48 import java.util.Set;
49 import java.util.concurrent.TimeUnit;
50 
51 @RunWith(AndroidJUnit4.class)
52 public class VcnGatewayConnectionConfigTest extends VcnTestBase {
53     @Rule
54     public final CheckFlagsRule mCheckFlagsRule = DeviceFlagsValueProvider.createCheckFlagsRule();
55 
56     private static final String VCN_GATEWAY_CONNECTION_NAME = "test-vcn-gateway-connection";
57     private static final long[] RETRY_INTERNAL_MILLIS =
58             new long[] {
59                 TimeUnit.SECONDS.toMillis(1),
60                 TimeUnit.MINUTES.toMillis(1),
61                 TimeUnit.HOURS.toMillis(1)
62             };
63     private static final int MAX_MTU = 1360;
64 
65     private static final List<VcnUnderlyingNetworkTemplate> UNDERLYING_NETWORK_TEMPLATES;
66 
67     static {
68         List<VcnUnderlyingNetworkTemplate> nwTemplates = new ArrayList<>();
VcnCellUnderlyingNetworkTemplateTest.getTestNetworkTemplate()69         nwTemplates.add(VcnCellUnderlyingNetworkTemplateTest.getTestNetworkTemplate());
VcnWifiUnderlyingNetworkTemplateTest.getTestNetworkTemplate()70         nwTemplates.add(VcnWifiUnderlyingNetworkTemplateTest.getTestNetworkTemplate());
71         UNDERLYING_NETWORK_TEMPLATES = Collections.unmodifiableList(nwTemplates);
72     }
73 
74     private static final Set<Integer> GATEWAY_OPTIONS =
75             Collections.singleton(VCN_GATEWAY_OPTION_ENABLE_DATA_STALL_RECOVERY_WITH_MOBILITY);
76 
buildVcnGatewayConnectionConfigBase()77     public static VcnGatewayConnectionConfig.Builder buildVcnGatewayConnectionConfigBase() {
78         return new VcnGatewayConnectionConfig.Builder(
79                         VCN_GATEWAY_CONNECTION_NAME, buildTunnelConnectionParams())
80                 .addExposedCapability(NET_CAPABILITY_INTERNET)
81                 .setRetryIntervalsMillis(RETRY_INTERNAL_MILLIS)
82                 .setMaxMtu(MAX_MTU)
83                 .setVcnUnderlyingNetworkPriorities(UNDERLYING_NETWORK_TEMPLATES);
84     }
85 
86     // Package private for use in VcnConfigTest
buildVcnGatewayConnectionConfig()87     static VcnGatewayConnectionConfig buildVcnGatewayConnectionConfig() {
88         return buildVcnGatewayConnectionConfigBase().build();
89     }
90 
91     @Test
testBuildVcnGatewayConnectionConfig()92     public void testBuildVcnGatewayConnectionConfig() throws Exception {
93         final VcnGatewayConnectionConfig gatewayConnConfig = buildVcnGatewayConnectionConfig();
94 
95         assertEquals(VCN_GATEWAY_CONNECTION_NAME, gatewayConnConfig.getGatewayConnectionName());
96         assertEquals(buildTunnelConnectionParams(), gatewayConnConfig.getTunnelConnectionParams());
97         assertArrayEquals(
98                 new int[] {NET_CAPABILITY_INTERNET}, gatewayConnConfig.getExposedCapabilities());
99         assertEquals(
100                 UNDERLYING_NETWORK_TEMPLATES,
101                 gatewayConnConfig.getVcnUnderlyingNetworkPriorities());
102         assertArrayEquals(RETRY_INTERNAL_MILLIS, gatewayConnConfig.getRetryIntervalsMillis());
103     }
104 
105     @Test
testBuilderAddRemove()106     public void testBuilderAddRemove() throws Exception {
107         final VcnGatewayConnectionConfig gatewayConnConfig =
108                 buildVcnGatewayConnectionConfigBase()
109                         .addExposedCapability(NET_CAPABILITY_DUN)
110                         .removeExposedCapability(NET_CAPABILITY_DUN)
111                         .build();
112 
113         assertArrayEquals(
114                 new int[] {NET_CAPABILITY_INTERNET}, gatewayConnConfig.getExposedCapabilities());
115     }
116 
117     @Test
testBuildWithoutMobikeEnabled()118     public void testBuildWithoutMobikeEnabled() {
119         final IkeSessionParams ikeParams =
120                 getIkeSessionParamsBase().removeIkeOption(IKE_OPTION_MOBIKE).build();
121         final IkeTunnelConnectionParams tunnelParams = buildTunnelConnectionParams(ikeParams);
122 
123         try {
124             new VcnGatewayConnectionConfig.Builder(VCN_GATEWAY_CONNECTION_NAME, tunnelParams);
125             fail("Expected exception if MOBIKE not configured");
126         } catch (IllegalArgumentException e) {
127         }
128     }
129 
130     @Test
testBuilderAddGatewayOptions()131     public void testBuilderAddGatewayOptions() throws Exception {
132         final VcnGatewayConnectionConfig.Builder builder = buildVcnGatewayConnectionConfigBase();
133 
134         for (int option : GATEWAY_OPTIONS) {
135             builder.addGatewayOption(option);
136         }
137 
138         final VcnGatewayConnectionConfig gatewayConnConfig = builder.build();
139         for (int option : GATEWAY_OPTIONS) {
140             assertTrue(gatewayConnConfig.hasGatewayOption(option));
141         }
142     }
143 
144     @Test
testBuilderAddRemoveGatewayOptions()145     public void testBuilderAddRemoveGatewayOptions() throws Exception {
146         final VcnGatewayConnectionConfig.Builder builder = buildVcnGatewayConnectionConfigBase();
147 
148         for (int option : GATEWAY_OPTIONS) {
149             builder.addGatewayOption(option);
150         }
151 
152         for (int option : GATEWAY_OPTIONS) {
153             builder.removeGatewayOption(option);
154         }
155 
156         final VcnGatewayConnectionConfig gatewayConnConfig = builder.build();
157         for (int option : GATEWAY_OPTIONS) {
158             assertFalse(gatewayConnConfig.hasGatewayOption(option));
159         }
160     }
161 
162     @Test
testBuilderRequiresValidOption()163     public void testBuilderRequiresValidOption() {
164         try {
165             buildVcnGatewayConnectionConfigBase().addGatewayOption(-1);
166             fail("Expected exception due to the invalid VCN gateway option");
167         } catch (IllegalArgumentException e) {
168         }
169     }
170 
171     @Test
testBuilderSetMinUdpPort4500NatTimeout()172     public void testBuilderSetMinUdpPort4500NatTimeout() {
173         final int natTimeoutSeconds = 600;
174         final VcnGatewayConnectionConfig gatewayConnConfig = buildVcnGatewayConnectionConfigBase()
175                 .setMinUdpPort4500NatTimeoutSeconds(600).build();
176         assertEquals(natTimeoutSeconds, gatewayConnConfig.getMinUdpPort4500NatTimeoutSeconds());
177     }
178 
179     @Test
testBuilderSetMinUdpPort4500NatTimeout_invalidValues()180     public void testBuilderSetMinUdpPort4500NatTimeout_invalidValues() {
181         try {
182             buildVcnGatewayConnectionConfigBase().setMinUdpPort4500NatTimeoutSeconds(-1);
183             fail("Expected exception due to invalid timeout range");
184         } catch (IllegalArgumentException e) {
185         }
186 
187         try {
188             buildVcnGatewayConnectionConfigBase().setMinUdpPort4500NatTimeoutSeconds(119);
189             fail("Expected exception due to invalid timeout range");
190         } catch (IllegalArgumentException e) {
191         }
192     }
193 
194     @RequiresFlagsEnabled(Flags.FLAG_SAFE_MODE_CONFIG)
195     @Test
testBuilderSetSafeModeEnabled()196     public void testBuilderSetSafeModeEnabled() {
197         final VcnGatewayConnectionConfig gatewayConnConfig =
198                 buildVcnGatewayConnectionConfigBase().setSafeModeEnabled(false).build();
199         assertFalse(gatewayConnConfig.isSafeModeEnabled());
200     }
201 
202     @RequiresFlagsEnabled(Flags.FLAG_SAFE_MODE_CONFIG)
203     @Test
testGetSafeModeEnabledDefault()204     public void testGetSafeModeEnabledDefault() {
205         final VcnGatewayConnectionConfig gatewayConnConfig = buildVcnGatewayConnectionConfig();
206         assertTrue(gatewayConnConfig.isSafeModeEnabled());
207     }
208 }
209