• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 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 com.google.android.iwlan.epdg;
18 
19 import static org.junit.Assert.assertEquals;
20 
21 import android.net.LinkAddress;
22 import android.telephony.data.ApnSetting;
23 import android.telephony.data.NetworkSliceInfo;
24 
25 import org.junit.Test;
26 
27 import java.net.InetAddress;
28 import java.util.ArrayList;
29 import java.util.List;
30 
31 public class TunnelLinkPropertiesTest {
32 
33     private static final String IP_ADDRESS = "192.0.2.1";
34     private static final String IPV6_ADDRESS = "2001:0db8:0000:0000:0000:ff00:0042:8329";
35     private static final String DNS_ADDRESS = "8.8.8.8";
36     private static final String PSCF_ADDRESS = "10.159.204.230";
37     private static final String INTERFACE_NAME = "ipsec6";
38     private static final NetworkSliceInfo SLICE_INFO =
39             NetworkSliceSelectionAssistanceInformation.getSliceInfo(new byte[] {1});
40 
createTestTunnelLinkProperties( @pnSetting.ProtocolType int protocolType)41     public static TunnelLinkProperties createTestTunnelLinkProperties(
42             @ApnSetting.ProtocolType int protocolType) throws Exception {
43         List<LinkAddress> mInternalAddressList = new ArrayList<>();
44         List<InetAddress> mDNSAddressList = new ArrayList<>();
45         List<InetAddress> mPCSFAddressList = new ArrayList<>();
46 
47         if (protocolType == ApnSetting.PROTOCOL_IP) {
48             mInternalAddressList.add(new LinkAddress(InetAddress.getByName(IP_ADDRESS), 3));
49         } else if (protocolType == ApnSetting.PROTOCOL_IPV6) {
50             mInternalAddressList.add(new LinkAddress(InetAddress.getByName(IPV6_ADDRESS), 3));
51         } else if (protocolType == ApnSetting.PROTOCOL_IPV4V6) {
52             mInternalAddressList.add(new LinkAddress(InetAddress.getByName(IP_ADDRESS), 3));
53             mInternalAddressList.add(new LinkAddress(InetAddress.getByName(IPV6_ADDRESS), 3));
54         }
55         mDNSAddressList.add(InetAddress.getByName(DNS_ADDRESS));
56         mPCSFAddressList.add(InetAddress.getByName(PSCF_ADDRESS));
57 
58         return TunnelLinkProperties.builder()
59                 .setInternalAddresses(mInternalAddressList)
60                 .setDnsAddresses(mDNSAddressList)
61                 .setPcscfAddresses(mPCSFAddressList)
62                 .setIfaceName(INTERFACE_NAME)
63                 .setSliceInfo(SLICE_INFO)
64                 .build();
65     }
66 
67     @Test
testGetProtocolType_emptyInternalAddresses_returnsUnknown()68     public void testGetProtocolType_emptyInternalAddresses_returnsUnknown() throws Exception {
69         List<InetAddress> mDNSAddressList = new ArrayList<>();
70         List<InetAddress> mPCSFAddressList = new ArrayList<>();
71         mDNSAddressList.add(InetAddress.getByName(DNS_ADDRESS));
72         mPCSFAddressList.add(InetAddress.getByName(PSCF_ADDRESS));
73         TunnelLinkProperties properties =
74                 TunnelLinkProperties.builder()
75                         .setInternalAddresses(new ArrayList<>())
76                         .setDnsAddresses(mDNSAddressList)
77                         .setPcscfAddresses(mPCSFAddressList)
78                         .setIfaceName(INTERFACE_NAME)
79                         .setSliceInfo(SLICE_INFO)
80                         .build();
81         assertEquals(ApnSetting.PROTOCOL_UNKNOWN, properties.getProtocolType());
82     }
83 
84     @Test
testGetProtocolType_onlyIpv4InternalAddresses_returnsIp()85     public void testGetProtocolType_onlyIpv4InternalAddresses_returnsIp() throws Exception {
86         TunnelLinkProperties properties = createTestTunnelLinkProperties(ApnSetting.PROTOCOL_IP);
87         assertEquals(ApnSetting.PROTOCOL_IP, properties.getProtocolType());
88     }
89 
90     @Test
testGetProtocolType_onlyIpv6InternalAddresses_returnsIpv6()91     public void testGetProtocolType_onlyIpv6InternalAddresses_returnsIpv6() throws Exception {
92         TunnelLinkProperties properties = createTestTunnelLinkProperties(ApnSetting.PROTOCOL_IPV6);
93         assertEquals(ApnSetting.PROTOCOL_IPV6, properties.getProtocolType());
94     }
95 
96     @Test
testGetProtocolType_bothIpv4AndIpv6InternalAddresses_returnsIpv4v6()97     public void testGetProtocolType_bothIpv4AndIpv6InternalAddresses_returnsIpv4v6()
98             throws Exception {
99         TunnelLinkProperties properties =
100                 createTestTunnelLinkProperties(ApnSetting.PROTOCOL_IPV4V6);
101         assertEquals(ApnSetting.PROTOCOL_IPV4V6, properties.getProtocolType());
102     }
103 }
104