1 /*
2  * Copyright (C) 2024 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 org.junit.Assert.assertEquals;
20 import static org.junit.Assert.assertNotNull;
21 import static org.junit.Assert.assertNull;
22 import static org.junit.Assume.assumeFalse;
23 import static org.mockito.ArgumentMatchers.any;
24 import static org.mockito.ArgumentMatchers.eq;
25 import static org.mockito.Mockito.mock;
26 import static org.mockito.Mockito.verify;
27 import static org.mockito.Mockito.when;
28 
29 import android.content.ComponentName;
30 import android.content.Intent;
31 import android.content.pm.PackageManager;
32 import android.test.mock.MockContext;
33 import android.util.SparseArray;
34 
35 import androidx.test.InstrumentationRegistry;
36 import androidx.test.ext.junit.runners.AndroidJUnit4;
37 import androidx.test.filters.SmallTest;
38 
39 import com.android.internal.net.VpnProfile;
40 import com.android.internal.util.MessageUtils;
41 
42 import org.junit.Before;
43 import org.junit.Test;
44 import org.junit.runner.RunWith;
45 
46 /** Unit tests for {@link VpnManager}. */
47 @SmallTest
48 @RunWith(AndroidJUnit4.class)
49 public class VpnManagerTest {
50 
51     private static final String PKG_NAME = "fooPackage";
52 
53     private static final String SERVER_ADDR_STRING = "1.2.3.4";
54     private static final String IDENTITY_STRING = "Identity";
55     private static final byte[] PSK_BYTES = "preSharedKey".getBytes();
56 
57     private IVpnManager mMockService;
58     private VpnManager mVpnManager;
59     private final MockContext mMockContext =
60             new MockContext() {
61                 @Override
62                 public String getOpPackageName() {
63                     return PKG_NAME;
64                 }
65             };
66 
67     @Before
setUp()68     public void setUp() throws Exception {
69         assumeFalse("Skipping test because watches don't support VPN",
70                 InstrumentationRegistry.getContext().getPackageManager().hasSystemFeature(
71                 PackageManager.FEATURE_WATCH));
72         mMockService = mock(IVpnManager.class);
73         mVpnManager = new VpnManager(mMockContext, mMockService);
74     }
75 
76     @Test
testProvisionVpnProfilePreconsented()77     public void testProvisionVpnProfilePreconsented() throws Exception {
78         final PlatformVpnProfile profile = getPlatformVpnProfile();
79         when(mMockService.provisionVpnProfile(any(VpnProfile.class), eq(PKG_NAME)))
80                 .thenReturn(true);
81 
82         // Expect there to be no intent returned, as consent has already been granted.
83         assertNull(mVpnManager.provisionVpnProfile(profile));
84         verify(mMockService).provisionVpnProfile(eq(profile.toVpnProfile()), eq(PKG_NAME));
85     }
86 
87     @Test
testProvisionVpnProfileNeedsConsent()88     public void testProvisionVpnProfileNeedsConsent() throws Exception {
89         final PlatformVpnProfile profile = getPlatformVpnProfile();
90         when(mMockService.provisionVpnProfile(any(VpnProfile.class), eq(PKG_NAME)))
91                 .thenReturn(false);
92 
93         // Expect intent to be returned, as consent has not already been granted.
94         final Intent intent = mVpnManager.provisionVpnProfile(profile);
95         assertNotNull(intent);
96 
97         final ComponentName expectedComponentName =
98                 ComponentName.unflattenFromString(
99                         "com.android.vpndialogs/com.android.vpndialogs.PlatformVpnConfirmDialog");
100         assertEquals(expectedComponentName, intent.getComponent());
101         verify(mMockService).provisionVpnProfile(eq(profile.toVpnProfile()), eq(PKG_NAME));
102     }
103 
104     @Test
testDeleteProvisionedVpnProfile()105     public void testDeleteProvisionedVpnProfile() throws Exception {
106         mVpnManager.deleteProvisionedVpnProfile();
107         verify(mMockService).deleteVpnProfile(eq(PKG_NAME));
108     }
109 
110     @Test
testStartProvisionedVpnProfile()111     public void testStartProvisionedVpnProfile() throws Exception {
112         mVpnManager.startProvisionedVpnProfile();
113         verify(mMockService).startVpnProfile(eq(PKG_NAME));
114     }
115 
116     @Test
testStopProvisionedVpnProfile()117     public void testStopProvisionedVpnProfile() throws Exception {
118         mVpnManager.stopProvisionedVpnProfile();
119         verify(mMockService).stopVpnProfile(eq(PKG_NAME));
120     }
121 
getPlatformVpnProfile()122     private Ikev2VpnProfile getPlatformVpnProfile() throws Exception {
123         return new Ikev2VpnProfile.Builder(SERVER_ADDR_STRING, IDENTITY_STRING)
124                 .setBypassable(true)
125                 .setMaxMtu(1300)
126                 .setMetered(true)
127                 .setAuthPsk(PSK_BYTES)
128                 .build();
129     }
130 
131     @Test
testVpnTypesEqual()132     public void testVpnTypesEqual() throws Exception {
133         SparseArray<String> vmVpnTypes = MessageUtils.findMessageNames(
134                 new Class[] { VpnManager.class }, new String[]{ "TYPE_VPN_" });
135         SparseArray<String> nativeVpnType = MessageUtils.findMessageNames(
136                 new Class[] { NativeVpnType.class }, new String[]{ "" });
137 
138         // TYPE_VPN_NONE = -1 is only defined in VpnManager.
139         assertEquals(vmVpnTypes.size() - 1, nativeVpnType.size());
140         for (int i = VpnManager.TYPE_VPN_SERVICE; i < vmVpnTypes.size(); i++) {
141             assertEquals(vmVpnTypes.get(i), "TYPE_VPN_" + nativeVpnType.get(i));
142         }
143     }
144 }
145