• 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.ipsec.ike.cts;
18 
19 import static org.junit.Assert.assertEquals;
20 import static org.junit.Assert.fail;
21 
22 import android.net.InetAddresses;
23 import android.net.ipsec.ike.IkeTrafficSelector;
24 
25 import java.net.Inet4Address;
26 import java.net.Inet6Address;
27 import java.net.InetAddress;
28 import java.util.HashMap;
29 import java.util.List;
30 import java.util.Map;
31 
32 /** Shared parameters and util methods for testing different components of IKE */
33 abstract class IkeTestBase {
34     static final int MIN_PORT = 0;
35     static final int MAX_PORT = 65535;
36     private static final int INBOUND_TS_START_PORT = MIN_PORT;
37     private static final int INBOUND_TS_END_PORT = 65520;
38     private static final int OUTBOUND_TS_START_PORT = 16;
39     private static final int OUTBOUND_TS_END_PORT = MAX_PORT;
40 
41     static final int IP4_ADDRESS_LEN = 4;
42     static final int IP6_ADDRESS_LEN = 16;
43     static final int IP4_PREFIX_LEN = 32;
44     static final int IP6_PREFIX_LEN = 64;
45 
46     static final byte[] IKE_PSK = "ikeAndroidPsk".getBytes();
47 
48     static final String LOCAL_HOSTNAME = "client.test.ike.android.net";
49     static final String REMOTE_HOSTNAME = "server.test.ike.android.net";
50     static final String LOCAL_ASN1_DN_STRING = "CN=client.test.ike.android.net, O=Android, C=US";
51     static final String LOCAL_RFC822_NAME = "client.test.ike@example.com";
52     static final byte[] LOCAL_KEY_ID = "Local Key ID".getBytes();
53 
54     static final int SUB_ID = 1;
55     static final byte[] EAP_IDENTITY = "test@android.net".getBytes();
56     static final String NETWORK_NAME = "android.net";
57     static final String EAP_MSCHAPV2_USERNAME = "mschapv2user";
58     static final String EAP_MSCHAPV2_PASSWORD = "password";
59 
60     static final Inet4Address IPV4_ADDRESS_LOCAL =
61             (Inet4Address) (InetAddresses.parseNumericAddress("192.0.2.100"));
62     static final Inet4Address IPV4_ADDRESS_REMOTE =
63             (Inet4Address) (InetAddresses.parseNumericAddress("198.51.100.100"));
64     static final Inet6Address IPV6_ADDRESS_LOCAL =
65             (Inet6Address) (InetAddresses.parseNumericAddress("2001:db8::100"));
66     static final Inet6Address IPV6_ADDRESS_REMOTE =
67             (Inet6Address) (InetAddresses.parseNumericAddress("2001:db8:255::100"));
68 
69     static final InetAddress PCSCF_IPV4_ADDRESS_1 = InetAddresses.parseNumericAddress("192.0.2.1");
70     static final InetAddress PCSCF_IPV4_ADDRESS_2 = InetAddresses.parseNumericAddress("192.0.2.2");
71     static final InetAddress PCSCF_IPV6_ADDRESS_1 =
72             InetAddresses.parseNumericAddress("2001:DB8::1");
73     static final InetAddress PCSCF_IPV6_ADDRESS_2 =
74             InetAddresses.parseNumericAddress("2001:DB8::2");
75 
76     static final IkeTrafficSelector DEFAULT_V4_TS =
77             new IkeTrafficSelector(
78                     MIN_PORT,
79                     MAX_PORT,
80                     InetAddresses.parseNumericAddress("0.0.0.0"),
81                     InetAddresses.parseNumericAddress("255.255.255.255"));
82     static final IkeTrafficSelector DEFAULT_V6_TS =
83             new IkeTrafficSelector(
84                     MIN_PORT,
85                     MAX_PORT,
86                     InetAddresses.parseNumericAddress("::"),
87                     InetAddresses.parseNumericAddress("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"));
88     static final IkeTrafficSelector INBOUND_V4_TS =
89             new IkeTrafficSelector(
90                     INBOUND_TS_START_PORT,
91                     INBOUND_TS_END_PORT,
92                     InetAddresses.parseNumericAddress("192.0.2.10"),
93                     InetAddresses.parseNumericAddress("192.0.2.20"));
94     static final IkeTrafficSelector OUTBOUND_V4_TS =
95             new IkeTrafficSelector(
96                     OUTBOUND_TS_START_PORT,
97                     OUTBOUND_TS_END_PORT,
98                     InetAddresses.parseNumericAddress("198.51.100.0"),
99                     InetAddresses.parseNumericAddress("198.51.100.255"));
100 
101     static final IkeTrafficSelector INBOUND_V6_TS =
102             new IkeTrafficSelector(
103                     INBOUND_TS_START_PORT,
104                     INBOUND_TS_END_PORT,
105                     InetAddresses.parseNumericAddress("2001:db8::10"),
106                     InetAddresses.parseNumericAddress("2001:db8::128"));
107     static final IkeTrafficSelector OUTBOUND_V6_TS =
108             new IkeTrafficSelector(
109                     OUTBOUND_TS_START_PORT,
110                     OUTBOUND_TS_END_PORT,
111                     InetAddresses.parseNumericAddress("2001:db8:255::64"),
112                     InetAddresses.parseNumericAddress("2001:db8:255::255"));
113 
114     // Verify Config requests in TunnelModeChildSessionParams and IkeSessionParams
verifyConfigRequestTypes( Map<Class<? extends T>, Integer> expectedReqCntMap, List<? extends T> resultReqList)115     <T> void verifyConfigRequestTypes(
116             Map<Class<? extends T>, Integer> expectedReqCntMap, List<? extends T> resultReqList) {
117         Map<Class<? extends T>, Integer> resultReqCntMap = new HashMap<>();
118 
119         // Verify that every config request type in resultReqList is expected, and build
120         // resultReqCntMap at the same time
121         for (T resultReq : resultReqList) {
122             boolean isResultReqExpected = false;
123 
124             for (Class<? extends T> expectedReqInterface : expectedReqCntMap.keySet()) {
125                 if (expectedReqInterface.isInstance(resultReq)) {
126                     isResultReqExpected = true;
127 
128                     resultReqCntMap.put(
129                             expectedReqInterface,
130                             resultReqCntMap.getOrDefault(expectedReqInterface, 0) + 1);
131                 }
132             }
133 
134             if (!isResultReqExpected) {
135                 fail("Failed due to unexpected config request " + resultReq);
136             }
137         }
138 
139         assertEquals(expectedReqCntMap, resultReqCntMap);
140 
141         // TODO: Think of a neat way to validate both counts and values in this method. Probably can
142         // build Runnables as validators for count and values.
143     }
144 }
145