blob: 532081a946b72c52b6824fc2d68cd18407182481 [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;
22import static org.mockito.Matchers.any;
23import static org.mockito.Matchers.eq;
Benedict Wong80240ac2019-11-01 16:46:28 -070024import static org.mockito.Mockito.mock;
Benedict Wong79ea64f2019-12-12 23:38:36 -080025import static org.mockito.Mockito.verify;
26import static org.mockito.Mockito.when;
Benedict Wong80240ac2019-11-01 16:46:28 -070027
Benedict Wonga7319912019-11-06 00:20:15 -080028import android.content.ComponentName;
29import android.content.Intent;
Remi NGUYEN VAN154cf1d2021-06-29 17:16:28 +090030import android.os.Build;
Benedict Wong80240ac2019-11-01 16:46:28 -070031import android.test.mock.MockContext;
Ken Chen5b1f25a2021-05-12 17:13:46 +080032import android.util.SparseArray;
Benedict Wong80240ac2019-11-01 16:46:28 -070033
34import androidx.test.filters.SmallTest;
Benedict Wong80240ac2019-11-01 16:46:28 -070035
Benedict Wong79ea64f2019-12-12 23:38:36 -080036import com.android.internal.net.VpnProfile;
Ken Chen5b1f25a2021-05-12 17:13:46 +080037import com.android.internal.util.MessageUtils;
Remi NGUYEN VAN154cf1d2021-06-29 17:16:28 +090038import com.android.testutils.DevSdkIgnoreRule;
39import com.android.testutils.DevSdkIgnoreRunner;
Benedict Wong79ea64f2019-12-12 23:38:36 -080040
Benedict Wong80240ac2019-11-01 16:46:28 -070041import org.junit.Before;
42import org.junit.Test;
43import org.junit.runner.RunWith;
44
45/** Unit tests for {@link VpnManager}. */
46@SmallTest
Remi NGUYEN VAN154cf1d2021-06-29 17:16:28 +090047@RunWith(DevSdkIgnoreRunner.class)
48@DevSdkIgnoreRule.IgnoreUpTo(Build.VERSION_CODES.R)
Benedict Wong80240ac2019-11-01 16:46:28 -070049public class VpnManagerTest {
Benedict Wong79ea64f2019-12-12 23:38:36 -080050 private static final String PKG_NAME = "fooPackage";
51
52 private static final String SESSION_NAME_STRING = "testSession";
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();
Benedict Wong80240ac2019-11-01 16:46:28 -070056
Lorenzo Colitticd675292021-02-04 17:32:07 +090057 private IVpnManager mMockService;
Benedict Wong80240ac2019-11-01 16:46:28 -070058 private VpnManager mVpnManager;
59 private final MockContext mMockContext =
60 new MockContext() {
61 @Override
62 public String getOpPackageName() {
Benedict Wong79ea64f2019-12-12 23:38:36 -080063 return PKG_NAME;
Benedict Wong80240ac2019-11-01 16:46:28 -070064 }
65 };
66
67 @Before
68 public void setUp() throws Exception {
Lorenzo Colitticd675292021-02-04 17:32:07 +090069 mMockService = mock(IVpnManager.class);
70 mVpnManager = new VpnManager(mMockContext, mMockService);
Benedict Wong80240ac2019-11-01 16:46:28 -070071 }
72
73 @Test
Benedict Wong79ea64f2019-12-12 23:38:36 -080074 public void testProvisionVpnProfilePreconsented() throws Exception {
75 final PlatformVpnProfile profile = getPlatformVpnProfile();
Lorenzo Colitticd675292021-02-04 17:32:07 +090076 when(mMockService.provisionVpnProfile(any(VpnProfile.class), eq(PKG_NAME)))
77 .thenReturn(true);
Benedict Wong79ea64f2019-12-12 23:38:36 -080078
79 // Expect there to be no intent returned, as consent has already been granted.
80 assertNull(mVpnManager.provisionVpnProfile(profile));
Lorenzo Colitticd675292021-02-04 17:32:07 +090081 verify(mMockService).provisionVpnProfile(eq(profile.toVpnProfile()), eq(PKG_NAME));
Benedict Wong79ea64f2019-12-12 23:38:36 -080082 }
83
84 @Test
85 public void testProvisionVpnProfileNeedsConsent() throws Exception {
86 final PlatformVpnProfile profile = getPlatformVpnProfile();
Lorenzo Colitticd675292021-02-04 17:32:07 +090087 when(mMockService.provisionVpnProfile(any(VpnProfile.class), eq(PKG_NAME)))
88 .thenReturn(false);
Benedict Wong79ea64f2019-12-12 23:38:36 -080089
90 // Expect intent to be returned, as consent has not already been granted.
Benedict Wonga7319912019-11-06 00:20:15 -080091 final Intent intent = mVpnManager.provisionVpnProfile(profile);
92 assertNotNull(intent);
93
94 final ComponentName expectedComponentName =
95 ComponentName.unflattenFromString(
96 "com.android.vpndialogs/com.android.vpndialogs.PlatformVpnConfirmDialog");
97 assertEquals(expectedComponentName, intent.getComponent());
Lorenzo Colitticd675292021-02-04 17:32:07 +090098 verify(mMockService).provisionVpnProfile(eq(profile.toVpnProfile()), eq(PKG_NAME));
Benedict Wong80240ac2019-11-01 16:46:28 -070099 }
100
101 @Test
102 public void testDeleteProvisionedVpnProfile() throws Exception {
Benedict Wong79ea64f2019-12-12 23:38:36 -0800103 mVpnManager.deleteProvisionedVpnProfile();
Lorenzo Colitticd675292021-02-04 17:32:07 +0900104 verify(mMockService).deleteVpnProfile(eq(PKG_NAME));
Benedict Wong80240ac2019-11-01 16:46:28 -0700105 }
106
107 @Test
108 public void testStartProvisionedVpnProfile() throws Exception {
Benedict Wong79ea64f2019-12-12 23:38:36 -0800109 mVpnManager.startProvisionedVpnProfile();
Lorenzo Colitticd675292021-02-04 17:32:07 +0900110 verify(mMockService).startVpnProfile(eq(PKG_NAME));
Benedict Wong80240ac2019-11-01 16:46:28 -0700111 }
112
113 @Test
114 public void testStopProvisionedVpnProfile() throws Exception {
Benedict Wong79ea64f2019-12-12 23:38:36 -0800115 mVpnManager.stopProvisionedVpnProfile();
Lorenzo Colitticd675292021-02-04 17:32:07 +0900116 verify(mMockService).stopVpnProfile(eq(PKG_NAME));
Benedict Wong79ea64f2019-12-12 23:38:36 -0800117 }
118
119 private Ikev2VpnProfile getPlatformVpnProfile() throws Exception {
120 return new Ikev2VpnProfile.Builder(SERVER_ADDR_STRING, IDENTITY_STRING)
121 .setBypassable(true)
122 .setMaxMtu(1300)
123 .setMetered(true)
124 .setAuthPsk(PSK_BYTES)
125 .build();
Benedict Wong80240ac2019-11-01 16:46:28 -0700126 }
Ken Chen5b1f25a2021-05-12 17:13:46 +0800127
128 @Test
129 public void testVpnTypesEqual() throws Exception {
130 SparseArray<String> vmVpnTypes = MessageUtils.findMessageNames(
131 new Class[] { VpnManager.class }, new String[]{ "TYPE_VPN_" });
132 SparseArray<String> nativeVpnType = MessageUtils.findMessageNames(
133 new Class[] { NativeVpnType.class }, new String[]{ "" });
134
135 // TYPE_VPN_NONE = -1 is only defined in VpnManager.
136 assertEquals(vmVpnTypes.size() - 1, nativeVpnType.size());
137 for (int i = VpnManager.TYPE_VPN_SERVICE; i < vmVpnTypes.size(); i++) {
138 assertEquals(vmVpnTypes.get(i), "TYPE_VPN_" + nativeVpnType.get(i));
139 }
140 }
Benedict Wong80240ac2019-11-01 16:46:28 -0700141}