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;
18 
19 import static android.net.NetworkCapabilities.REDACT_FOR_NETWORK_SETTINGS;
20 import static android.net.NetworkCapabilities.REDACT_NONE;
21 
22 import static com.android.testutils.MiscAsserts.assertThrows;
23 import static com.android.testutils.ParcelUtils.assertParcelingIsLossless;
24 
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertFalse;
27 import static org.junit.Assert.assertNotEquals;
28 import static org.junit.Assert.assertTrue;
29 
30 import android.os.Build;
31 
32 import androidx.test.filters.SmallTest;
33 
34 import com.android.testutils.ConnectivityModuleTest;
35 import com.android.testutils.DevSdkIgnoreRule;
36 import com.android.testutils.DevSdkIgnoreRunner;
37 
38 import org.junit.Rule;
39 import org.junit.Test;
40 import org.junit.runner.RunWith;
41 
42 @RunWith(DevSdkIgnoreRunner.class)
43 @SmallTest
44 @DevSdkIgnoreRule.IgnoreUpTo(Build.VERSION_CODES.R)
45 @ConnectivityModuleTest
46 public class VpnTransportInfoTest {
47     @Rule
48     public final DevSdkIgnoreRule ignoreRule = new DevSdkIgnoreRule();
49 
50     @Test
testParceling()51     public void testParceling() {
52         final VpnTransportInfo v = new VpnTransportInfo(VpnManager.TYPE_VPN_PLATFORM, "12345");
53         assertParcelingIsLossless(v);
54 
55         final VpnTransportInfo v2 =
56                 new VpnTransportInfo(VpnManager.TYPE_VPN_PLATFORM, "12345", true, true);
57         assertParcelingIsLossless(v2);
58     }
59 
60     @Test
testEqualsAndHashCode()61     public void testEqualsAndHashCode() {
62         String session1 = "12345";
63         String session2 = "6789";
64         final VpnTransportInfo v11 = new VpnTransportInfo(VpnManager.TYPE_VPN_PLATFORM, session1);
65         final VpnTransportInfo v12 = new VpnTransportInfo(VpnManager.TYPE_VPN_SERVICE, session1);
66         final VpnTransportInfo v13 = new VpnTransportInfo(VpnManager.TYPE_VPN_PLATFORM, session1);
67         final VpnTransportInfo v14 = new VpnTransportInfo(VpnManager.TYPE_VPN_LEGACY, session1);
68         final VpnTransportInfo v15 = new VpnTransportInfo(VpnManager.TYPE_VPN_OEM, session1);
69         final VpnTransportInfo v16 = new VpnTransportInfo(
70                 VpnManager.TYPE_VPN_OEM, session1, true, true);
71         final VpnTransportInfo v17 = new VpnTransportInfo(
72                 VpnManager.TYPE_VPN_OEM, session1, true, true);
73         final VpnTransportInfo v21 = new VpnTransportInfo(VpnManager.TYPE_VPN_LEGACY, session2);
74 
75         final VpnTransportInfo v31 = v11.makeCopy(REDACT_FOR_NETWORK_SETTINGS);
76         final VpnTransportInfo v32 = v13.makeCopy(REDACT_FOR_NETWORK_SETTINGS);
77         final VpnTransportInfo v33 = v16.makeCopy(REDACT_FOR_NETWORK_SETTINGS);
78         final VpnTransportInfo v34 = v17.makeCopy(REDACT_FOR_NETWORK_SETTINGS);
79 
80         assertNotEquals(v11, v12);
81         assertNotEquals(v13, v14);
82         assertNotEquals(v14, v15);
83         assertNotEquals(v14, v21);
84         assertNotEquals(v15, v16);
85 
86         assertEquals(v11, v13);
87         assertEquals(v31, v32);
88         assertEquals(v33, v34);
89         assertEquals(v11.hashCode(), v13.hashCode());
90         assertEquals(v16.hashCode(), v17.hashCode());
91         assertEquals(REDACT_FOR_NETWORK_SETTINGS, v32.getApplicableRedactions());
92         assertEquals(session1, v15.makeCopy(REDACT_NONE).getSessionId());
93     }
94 
95     @DevSdkIgnoreRule.IgnoreAfter(Build.VERSION_CODES.TIRAMISU)
96     @Test
testIsBypassable_beforeU()97     public void testIsBypassable_beforeU() {
98         final VpnTransportInfo v = new VpnTransportInfo(VpnManager.TYPE_VPN_PLATFORM, "12345");
99         assertThrows(UnsupportedOperationException.class, () -> v.isBypassable());
100     }
101 
102     @DevSdkIgnoreRule.IgnoreUpTo(Build.VERSION_CODES.TIRAMISU)
103     @Test
testIsBypassable_afterU()104     public void testIsBypassable_afterU() {
105         final VpnTransportInfo v = new VpnTransportInfo(VpnManager.TYPE_VPN_PLATFORM, "12345");
106         assertFalse(v.isBypassable());
107 
108         final VpnTransportInfo v2 =
109                 new VpnTransportInfo(VpnManager.TYPE_VPN_PLATFORM, "12345", true, false);
110         assertTrue(v2.isBypassable());
111     }
112 
113     @DevSdkIgnoreRule.IgnoreUpTo(Build.VERSION_CODES.TIRAMISU)
114     @Test
testShouldLongLivedTcpExcluded()115     public void testShouldLongLivedTcpExcluded() {
116         final VpnTransportInfo v = new VpnTransportInfo(VpnManager.TYPE_VPN_PLATFORM, "12345");
117         assertFalse(v.areLongLivedTcpConnectionsExpensive());
118 
119         final VpnTransportInfo v2 = new VpnTransportInfo(
120                 VpnManager.TYPE_VPN_PLATFORM, "12345", true, true);
121         assertTrue(v2.areLongLivedTcpConnectionsExpensive());
122     }
123 }
124