• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2020 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;
18 
19 import static android.net.ipsec.ike.IkeSessionParams.IKE_OPTION_MOBIKE;
20 import static android.net.vcn.VcnGatewayConnectionConfig.DEFAULT_UNDERLYING_NETWORK_TEMPLATES;
21 import static android.net.vcn.VcnGatewayConnectionConfig.UNDERLYING_NETWORK_TEMPLATES_KEY;
22 
23 import static org.junit.Assert.assertArrayEquals;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertNotEquals;
26 import static org.junit.Assert.assertNotSame;
27 import static org.junit.Assert.assertTrue;
28 import static org.junit.Assert.fail;
29 
30 import android.net.NetworkCapabilities;
31 import android.net.ipsec.ike.IkeSessionParams;
32 import android.net.ipsec.ike.IkeTunnelConnectionParams;
33 import android.net.vcn.persistablebundleutils.IkeSessionParamsUtilsTest;
34 import android.net.vcn.persistablebundleutils.TunnelConnectionParamsUtilsTest;
35 import android.os.PersistableBundle;
36 
37 import androidx.test.filters.SmallTest;
38 import androidx.test.runner.AndroidJUnit4;
39 
40 import org.junit.Test;
41 import org.junit.runner.RunWith;
42 
43 import java.util.ArrayList;
44 import java.util.Arrays;
45 import java.util.List;
46 import java.util.concurrent.TimeUnit;
47 
48 @RunWith(AndroidJUnit4.class)
49 @SmallTest
50 public class VcnGatewayConnectionConfigTest {
51     // Public for use in VcnGatewayConnectionTest
52     public static final int[] EXPOSED_CAPS =
53             new int[] {
54                 NetworkCapabilities.NET_CAPABILITY_INTERNET, NetworkCapabilities.NET_CAPABILITY_MMS
55             };
56     public static final int[] UNDERLYING_CAPS = new int[] {NetworkCapabilities.NET_CAPABILITY_DUN};
57 
58     private static final List<VcnUnderlyingNetworkTemplate> UNDERLYING_NETWORK_TEMPLATES =
59             new ArrayList();
60 
61     static {
62         Arrays.sort(EXPOSED_CAPS);
63         Arrays.sort(UNDERLYING_CAPS);
64 
65         UNDERLYING_NETWORK_TEMPLATES.add(
VcnCellUnderlyingNetworkTemplateTest.getTestNetworkTemplate()66                 VcnCellUnderlyingNetworkTemplateTest.getTestNetworkTemplate());
67         UNDERLYING_NETWORK_TEMPLATES.add(
VcnWifiUnderlyingNetworkTemplateTest.getTestNetworkTemplate()68                 VcnWifiUnderlyingNetworkTemplateTest.getTestNetworkTemplate());
69     }
70 
71     public static final long[] RETRY_INTERVALS_MS =
72             new long[] {
73                 TimeUnit.SECONDS.toMillis(5),
74                 TimeUnit.SECONDS.toMillis(30),
75                 TimeUnit.MINUTES.toMillis(1),
76                 TimeUnit.MINUTES.toMillis(5),
77                 TimeUnit.MINUTES.toMillis(15),
78                 TimeUnit.MINUTES.toMillis(30)
79             };
80     public static final int MAX_MTU = 1360;
81 
82     public static final IkeTunnelConnectionParams TUNNEL_CONNECTION_PARAMS =
83             TunnelConnectionParamsUtilsTest.buildTestParams();
84 
85     public static final String GATEWAY_CONNECTION_NAME_PREFIX = "gatewayConnectionName-";
86     private static int sGatewayConnectionConfigCount = 0;
87 
buildTestConfig( String gatewayConnectionName, IkeTunnelConnectionParams tunnelConnectionParams)88     private static VcnGatewayConnectionConfig buildTestConfig(
89             String gatewayConnectionName, IkeTunnelConnectionParams tunnelConnectionParams) {
90         return buildTestConfigWithExposedCaps(
91                 new VcnGatewayConnectionConfig.Builder(
92                         gatewayConnectionName, tunnelConnectionParams),
93                 EXPOSED_CAPS);
94     }
95 
96     // Public for use in VcnGatewayConnectionTest
buildTestConfig()97     public static VcnGatewayConnectionConfig buildTestConfig() {
98         final VcnGatewayConnectionConfig.Builder builder =
99                 newBuilder().setVcnUnderlyingNetworkPriorities(UNDERLYING_NETWORK_TEMPLATES);
100 
101         return buildTestConfigWithExposedCaps(builder, EXPOSED_CAPS);
102     }
103 
newBuilder()104     private static VcnGatewayConnectionConfig.Builder newBuilder() {
105         // Append a unique identifier to the name prefix to guarantee that all created
106         // VcnGatewayConnectionConfigs have a unique name (required by VcnConfig).
107         return new VcnGatewayConnectionConfig.Builder(
108                 GATEWAY_CONNECTION_NAME_PREFIX + sGatewayConnectionConfigCount++,
109                 TUNNEL_CONNECTION_PARAMS);
110     }
111 
buildTestConfigWithExposedCaps( VcnGatewayConnectionConfig.Builder builder, int... exposedCaps)112     private static VcnGatewayConnectionConfig buildTestConfigWithExposedCaps(
113             VcnGatewayConnectionConfig.Builder builder, int... exposedCaps) {
114         builder.setRetryIntervalsMillis(RETRY_INTERVALS_MS).setMaxMtu(MAX_MTU);
115 
116         for (int caps : exposedCaps) {
117             builder.addExposedCapability(caps);
118         }
119 
120         return builder.build();
121     }
122 
123     // Public for use in VcnGatewayConnectionTest
buildTestConfigWithExposedCaps(int... exposedCaps)124     public static VcnGatewayConnectionConfig buildTestConfigWithExposedCaps(int... exposedCaps) {
125         return buildTestConfigWithExposedCaps(newBuilder(), exposedCaps);
126     }
127 
128     @Test
testBuilderRequiresNonNullGatewayConnectionName()129     public void testBuilderRequiresNonNullGatewayConnectionName() {
130         try {
131             new VcnGatewayConnectionConfig.Builder(
132                             null /* gatewayConnectionName */, TUNNEL_CONNECTION_PARAMS)
133                     .build();
134 
135             fail("Expected exception due to invalid gateway connection name");
136         } catch (NullPointerException e) {
137         }
138     }
139 
140     @Test
testBuilderRequiresNonNullTunnelConnectionParams()141     public void testBuilderRequiresNonNullTunnelConnectionParams() {
142         try {
143             new VcnGatewayConnectionConfig.Builder(
144                             GATEWAY_CONNECTION_NAME_PREFIX, null /* tunnelConnectionParams */)
145                     .build();
146 
147             fail("Expected exception due to the absence of tunnel connection parameters");
148         } catch (NullPointerException e) {
149         }
150     }
151 
152     @Test
testBuilderRequiresMobikeEnabled()153     public void testBuilderRequiresMobikeEnabled() {
154         try {
155             final IkeSessionParams ikeParams =
156                     IkeSessionParamsUtilsTest.createBuilderMinimum()
157                             .removeIkeOption(IKE_OPTION_MOBIKE)
158                             .build();
159             final IkeTunnelConnectionParams tunnelParams =
160                     TunnelConnectionParamsUtilsTest.buildTestParams(ikeParams);
161             new VcnGatewayConnectionConfig.Builder(GATEWAY_CONNECTION_NAME_PREFIX, tunnelParams);
162             fail("Expected exception due to MOBIKE not enabled");
163         } catch (IllegalArgumentException e) {
164         }
165     }
166 
167     @Test
testBuilderRequiresNonEmptyExposedCaps()168     public void testBuilderRequiresNonEmptyExposedCaps() {
169         try {
170             newBuilder().build();
171 
172             fail("Expected exception due to invalid exposed capabilities");
173         } catch (IllegalArgumentException e) {
174         }
175     }
176 
177     @Test
testBuilderRequiresNonNullNetworkTemplates()178     public void testBuilderRequiresNonNullNetworkTemplates() {
179         try {
180             newBuilder().setVcnUnderlyingNetworkPriorities(null);
181             fail("Expected exception due to invalid underlyingNetworkTemplates");
182         } catch (NullPointerException e) {
183         }
184     }
185 
186     @Test
testBuilderRequiresNonNullRetryInterval()187     public void testBuilderRequiresNonNullRetryInterval() {
188         try {
189             newBuilder().setRetryIntervalsMillis(null);
190             fail("Expected exception due to invalid retryIntervalMs");
191         } catch (IllegalArgumentException e) {
192         }
193     }
194 
195     @Test
testBuilderRequiresNonEmptyRetryInterval()196     public void testBuilderRequiresNonEmptyRetryInterval() {
197         try {
198             newBuilder().setRetryIntervalsMillis(new long[0]);
199             fail("Expected exception due to invalid retryIntervalMs");
200         } catch (IllegalArgumentException e) {
201         }
202     }
203 
204     @Test
testBuilderRequiresValidMtu()205     public void testBuilderRequiresValidMtu() {
206         try {
207             newBuilder().setMaxMtu(VcnGatewayConnectionConfig.MIN_MTU_V6 - 1);
208             fail("Expected exception due to invalid mtu");
209         } catch (IllegalArgumentException e) {
210         }
211     }
212 
213     @Test
testBuilderAndGetters()214     public void testBuilderAndGetters() {
215         final VcnGatewayConnectionConfig config = buildTestConfig();
216 
217         assertTrue(config.getGatewayConnectionName().startsWith(GATEWAY_CONNECTION_NAME_PREFIX));
218 
219         int[] exposedCaps = config.getExposedCapabilities();
220         Arrays.sort(exposedCaps);
221         assertArrayEquals(EXPOSED_CAPS, exposedCaps);
222 
223         assertEquals(UNDERLYING_NETWORK_TEMPLATES, config.getVcnUnderlyingNetworkPriorities());
224         assertEquals(TUNNEL_CONNECTION_PARAMS, config.getTunnelConnectionParams());
225 
226         assertArrayEquals(RETRY_INTERVALS_MS, config.getRetryIntervalsMillis());
227         assertEquals(MAX_MTU, config.getMaxMtu());
228     }
229 
230     @Test
testPersistableBundle()231     public void testPersistableBundle() {
232         final VcnGatewayConnectionConfig config = buildTestConfig();
233 
234         assertEquals(config, new VcnGatewayConnectionConfig(config.toPersistableBundle()));
235     }
236 
237     @Test
testParsePersistableBundleWithoutVcnUnderlyingNetworkTemplates()238     public void testParsePersistableBundleWithoutVcnUnderlyingNetworkTemplates() {
239         PersistableBundle configBundle = buildTestConfig().toPersistableBundle();
240         configBundle.putPersistableBundle(UNDERLYING_NETWORK_TEMPLATES_KEY, null);
241 
242         final VcnGatewayConnectionConfig config = new VcnGatewayConnectionConfig(configBundle);
243         assertEquals(
244                 DEFAULT_UNDERLYING_NETWORK_TEMPLATES, config.getVcnUnderlyingNetworkPriorities());
245     }
246 
buildTunnelConnectionParams(String ikePsk)247     private static IkeTunnelConnectionParams buildTunnelConnectionParams(String ikePsk) {
248         final IkeSessionParams ikeParams =
249                 IkeSessionParamsUtilsTest.createBuilderMinimum()
250                         .setAuthPsk(ikePsk.getBytes())
251                         .build();
252         return TunnelConnectionParamsUtilsTest.buildTestParams(ikeParams);
253     }
254 
255     @Test
testTunnelConnectionParamsEquals()256     public void testTunnelConnectionParamsEquals() throws Exception {
257         final String connectionName = "testTunnelConnectionParamsEquals.connectionName";
258         final String psk = "testTunnelConnectionParamsEquals.psk";
259 
260         final IkeTunnelConnectionParams tunnelParams = buildTunnelConnectionParams(psk);
261         final VcnGatewayConnectionConfig config = buildTestConfig(connectionName, tunnelParams);
262 
263         final IkeTunnelConnectionParams anotherTunnelParams = buildTunnelConnectionParams(psk);
264         final VcnGatewayConnectionConfig anotherConfig =
265                 buildTestConfig(connectionName, anotherTunnelParams);
266 
267         assertNotSame(tunnelParams, anotherTunnelParams);
268         assertEquals(tunnelParams, anotherTunnelParams);
269         assertEquals(config, anotherConfig);
270     }
271 
272     @Test
testTunnelConnectionParamsNotEquals()273     public void testTunnelConnectionParamsNotEquals() throws Exception {
274         final String connectionName = "testTunnelConnectionParamsNotEquals.connectionName";
275 
276         final IkeTunnelConnectionParams tunnelParams =
277                 buildTunnelConnectionParams("testTunnelConnectionParamsNotEquals.pskA");
278         final VcnGatewayConnectionConfig config = buildTestConfig(connectionName, tunnelParams);
279 
280         final IkeTunnelConnectionParams anotherTunnelParams =
281                 buildTunnelConnectionParams("testTunnelConnectionParamsNotEquals.pskB");
282         final VcnGatewayConnectionConfig anotherConfig =
283                 buildTestConfig(connectionName, anotherTunnelParams);
284 
285         assertNotEquals(tunnelParams, anotherTunnelParams);
286         assertNotEquals(config, anotherConfig);
287     }
288 
buildTestConfigWithVcnUnderlyingNetworkTemplates( List<VcnUnderlyingNetworkTemplate> networkTemplates)289     private static VcnGatewayConnectionConfig buildTestConfigWithVcnUnderlyingNetworkTemplates(
290             List<VcnUnderlyingNetworkTemplate> networkTemplates) {
291         return buildTestConfigWithExposedCaps(
292                 new VcnGatewayConnectionConfig.Builder(
293                                 "buildTestConfigWithVcnUnderlyingNetworkTemplates",
294                                 TUNNEL_CONNECTION_PARAMS)
295                         .setVcnUnderlyingNetworkPriorities(networkTemplates),
296                 EXPOSED_CAPS);
297     }
298 
299     @Test
testVcnUnderlyingNetworkTemplatesEquality()300     public void testVcnUnderlyingNetworkTemplatesEquality() throws Exception {
301         final VcnGatewayConnectionConfig config =
302                 buildTestConfigWithVcnUnderlyingNetworkTemplates(UNDERLYING_NETWORK_TEMPLATES);
303 
304         final List<VcnUnderlyingNetworkTemplate> networkTemplatesEqual = new ArrayList();
305         networkTemplatesEqual.add(VcnCellUnderlyingNetworkTemplateTest.getTestNetworkTemplate());
306         networkTemplatesEqual.add(VcnWifiUnderlyingNetworkTemplateTest.getTestNetworkTemplate());
307         final VcnGatewayConnectionConfig configEqual =
308                 buildTestConfigWithVcnUnderlyingNetworkTemplates(networkTemplatesEqual);
309 
310         final List<VcnUnderlyingNetworkTemplate> networkTemplatesNotEqual = new ArrayList();
311         networkTemplatesNotEqual.add(VcnWifiUnderlyingNetworkTemplateTest.getTestNetworkTemplate());
312         final VcnGatewayConnectionConfig configNotEqual =
313                 buildTestConfigWithVcnUnderlyingNetworkTemplates(networkTemplatesNotEqual);
314 
315         assertEquals(UNDERLYING_NETWORK_TEMPLATES, networkTemplatesEqual);
316         assertEquals(config, configEqual);
317 
318         assertNotEquals(UNDERLYING_NETWORK_TEMPLATES, networkTemplatesNotEqual);
319         assertNotEquals(config, configNotEqual);
320     }
321 }
322