blob: 2ab4e4541220efed0eddf1b4848e58a9407f2961 [file] [log] [blame]
Benedict Wong80240ac2019-11-01 16:46:28 -07001/*
2 * Copyright (C) 2019 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
17package android.net;
18
Benedict Wonga7319912019-11-06 00:20:15 -080019import static org.junit.Assert.assertEquals;
Benedict Wong79ea64f2019-12-12 23:38:36 -080020import static org.junit.Assert.assertNotNull;
21import static org.junit.Assert.assertNull;
abdelrahmani5bcbbc12023-06-13 19:56:07 +000022import static org.junit.Assume.assumeFalse;
Benedict Wong79ea64f2019-12-12 23:38:36 -080023import static org.mockito.Matchers.any;
24import static org.mockito.Matchers.eq;
Benedict Wong80240ac2019-11-01 16:46:28 -070025import static org.mockito.Mockito.mock;
Benedict Wong79ea64f2019-12-12 23:38:36 -080026import static org.mockito.Mockito.verify;
27import static org.mockito.Mockito.when;
Benedict Wong80240ac2019-11-01 16:46:28 -070028
Benedict Wonga7319912019-11-06 00:20:15 -080029import android.content.ComponentName;
30import android.content.Intent;
abdelrahmani5bcbbc12023-06-13 19:56:07 +000031import android.content.pm.PackageManager;
Remi NGUYEN VAN154cf1d2021-06-29 17:16:28 +090032import android.os.Build;
Benedict Wong80240ac2019-11-01 16:46:28 -070033import android.test.mock.MockContext;
Ken Chen5b1f25a2021-05-12 17:13:46 +080034import android.util.SparseArray;
Benedict Wong80240ac2019-11-01 16:46:28 -070035
36import androidx.test.filters.SmallTest;
abdelrahmani5bcbbc12023-06-13 19:56:07 +000037import androidx.test.InstrumentationRegistry;
Benedict Wong80240ac2019-11-01 16:46:28 -070038
Benedict Wong79ea64f2019-12-12 23:38:36 -080039import com.android.internal.net.VpnProfile;
Ken Chen5b1f25a2021-05-12 17:13:46 +080040import com.android.internal.util.MessageUtils;
Remi NGUYEN VAN154cf1d2021-06-29 17:16:28 +090041import com.android.testutils.DevSdkIgnoreRule;
42import com.android.testutils.DevSdkIgnoreRunner;
Benedict Wong79ea64f2019-12-12 23:38:36 -080043
Benedict Wong80240ac2019-11-01 16:46:28 -070044import org.junit.Before;
45import org.junit.Test;
46import org.junit.runner.RunWith;
47
48/** Unit tests for {@link VpnManager}. */
49@SmallTest
Remi NGUYEN VAN154cf1d2021-06-29 17:16:28 +090050@RunWith(DevSdkIgnoreRunner.class)
51@DevSdkIgnoreRule.IgnoreUpTo(Build.VERSION_CODES.R)
Benedict Wong80240ac2019-11-01 16:46:28 -070052public class VpnManagerTest {
abdelrahmani5bcbbc12023-06-13 19:56:07 +000053
Benedict Wong79ea64f2019-12-12 23:38:36 -080054 private static final String PKG_NAME = "fooPackage";
55
56 private static final String SESSION_NAME_STRING = "testSession";
57 private static final String SERVER_ADDR_STRING = "1.2.3.4";
58 private static final String IDENTITY_STRING = "Identity";
59 private static final byte[] PSK_BYTES = "preSharedKey".getBytes();
Benedict Wong80240ac2019-11-01 16:46:28 -070060
Lorenzo Colitticd675292021-02-04 17:32:07 +090061 private IVpnManager mMockService;
Benedict Wong80240ac2019-11-01 16:46:28 -070062 private VpnManager mVpnManager;
63 private final MockContext mMockContext =
64 new MockContext() {
65 @Override
66 public String getOpPackageName() {
Benedict Wong79ea64f2019-12-12 23:38:36 -080067 return PKG_NAME;
Benedict Wong80240ac2019-11-01 16:46:28 -070068 }
69 };
70
71 @Before
72 public void setUp() throws Exception {
abdelrahmani5bcbbc12023-06-13 19:56:07 +000073 assumeFalse("Skipping test because watches don't support VPN",
74 InstrumentationRegistry.getContext().getPackageManager().hasSystemFeature(
75 PackageManager.FEATURE_WATCH));
Lorenzo Colitticd675292021-02-04 17:32:07 +090076 mMockService = mock(IVpnManager.class);
77 mVpnManager = new VpnManager(mMockContext, mMockService);
Benedict Wong80240ac2019-11-01 16:46:28 -070078 }
79
80 @Test
Benedict Wong79ea64f2019-12-12 23:38:36 -080081 public void testProvisionVpnProfilePreconsented() throws Exception {
82 final PlatformVpnProfile profile = getPlatformVpnProfile();
Lorenzo Colitticd675292021-02-04 17:32:07 +090083 when(mMockService.provisionVpnProfile(any(VpnProfile.class), eq(PKG_NAME)))
84 .thenReturn(true);
Benedict Wong79ea64f2019-12-12 23:38:36 -080085
86 // Expect there to be no intent returned, as consent has already been granted.
87 assertNull(mVpnManager.provisionVpnProfile(profile));
Lorenzo Colitticd675292021-02-04 17:32:07 +090088 verify(mMockService).provisionVpnProfile(eq(profile.toVpnProfile()), eq(PKG_NAME));
Benedict Wong79ea64f2019-12-12 23:38:36 -080089 }
90
91 @Test
92 public void testProvisionVpnProfileNeedsConsent() throws Exception {
93 final PlatformVpnProfile profile = getPlatformVpnProfile();
Lorenzo Colitticd675292021-02-04 17:32:07 +090094 when(mMockService.provisionVpnProfile(any(VpnProfile.class), eq(PKG_NAME)))
95 .thenReturn(false);
Benedict Wong79ea64f2019-12-12 23:38:36 -080096
97 // Expect intent to be returned, as consent has not already been granted.
Benedict Wonga7319912019-11-06 00:20:15 -080098 final Intent intent = mVpnManager.provisionVpnProfile(profile);
99 assertNotNull(intent);
100
101 final ComponentName expectedComponentName =
102 ComponentName.unflattenFromString(
103 "com.android.vpndialogs/com.android.vpndialogs.PlatformVpnConfirmDialog");
104 assertEquals(expectedComponentName, intent.getComponent());
Lorenzo Colitticd675292021-02-04 17:32:07 +0900105 verify(mMockService).provisionVpnProfile(eq(profile.toVpnProfile()), eq(PKG_NAME));
Benedict Wong80240ac2019-11-01 16:46:28 -0700106 }
107
108 @Test
109 public void testDeleteProvisionedVpnProfile() throws Exception {
Benedict Wong79ea64f2019-12-12 23:38:36 -0800110 mVpnManager.deleteProvisionedVpnProfile();
Lorenzo Colitticd675292021-02-04 17:32:07 +0900111 verify(mMockService).deleteVpnProfile(eq(PKG_NAME));
Benedict Wong80240ac2019-11-01 16:46:28 -0700112 }
113
114 @Test
115 public void testStartProvisionedVpnProfile() throws Exception {
Benedict Wong79ea64f2019-12-12 23:38:36 -0800116 mVpnManager.startProvisionedVpnProfile();
Lorenzo Colitticd675292021-02-04 17:32:07 +0900117 verify(mMockService).startVpnProfile(eq(PKG_NAME));
Benedict Wong80240ac2019-11-01 16:46:28 -0700118 }
119
120 @Test
121 public void testStopProvisionedVpnProfile() throws Exception {
Benedict Wong79ea64f2019-12-12 23:38:36 -0800122 mVpnManager.stopProvisionedVpnProfile();
Lorenzo Colitticd675292021-02-04 17:32:07 +0900123 verify(mMockService).stopVpnProfile(eq(PKG_NAME));
Benedict Wong79ea64f2019-12-12 23:38:36 -0800124 }
125
126 private Ikev2VpnProfile getPlatformVpnProfile() throws Exception {
127 return new Ikev2VpnProfile.Builder(SERVER_ADDR_STRING, IDENTITY_STRING)
128 .setBypassable(true)
129 .setMaxMtu(1300)
130 .setMetered(true)
131 .setAuthPsk(PSK_BYTES)
132 .build();
Benedict Wong80240ac2019-11-01 16:46:28 -0700133 }
Ken Chen5b1f25a2021-05-12 17:13:46 +0800134
135 @Test
136 public void testVpnTypesEqual() throws Exception {
137 SparseArray<String> vmVpnTypes = MessageUtils.findMessageNames(
138 new Class[] { VpnManager.class }, new String[]{ "TYPE_VPN_" });
139 SparseArray<String> nativeVpnType = MessageUtils.findMessageNames(
140 new Class[] { NativeVpnType.class }, new String[]{ "" });
141
142 // TYPE_VPN_NONE = -1 is only defined in VpnManager.
143 assertEquals(vmVpnTypes.size() - 1, nativeVpnType.size());
144 for (int i = VpnManager.TYPE_VPN_SERVICE; i < vmVpnTypes.size(); i++) {
145 assertEquals(vmVpnTypes.get(i), "TYPE_VPN_" + nativeVpnType.get(i));
146 }
147 }
Benedict Wong80240ac2019-11-01 16:46:28 -0700148}