blob: 3135062138ac580c08c18f1672487d04ac2db36b [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;
Benedict Wong80240ac2019-11-01 16:46:28 -070030import android.test.mock.MockContext;
Ken Chen5b1f25a2021-05-12 17:13:46 +080031import android.util.SparseArray;
Benedict Wong80240ac2019-11-01 16:46:28 -070032
33import androidx.test.filters.SmallTest;
34import androidx.test.runner.AndroidJUnit4;
35
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;
Benedict Wong79ea64f2019-12-12 23:38:36 -080038
Benedict Wong80240ac2019-11-01 16:46:28 -070039import org.junit.Before;
40import org.junit.Test;
41import org.junit.runner.RunWith;
42
43/** Unit tests for {@link VpnManager}. */
44@SmallTest
45@RunWith(AndroidJUnit4.class)
46public class VpnManagerTest {
Benedict Wong79ea64f2019-12-12 23:38:36 -080047 private static final String PKG_NAME = "fooPackage";
48
49 private static final String SESSION_NAME_STRING = "testSession";
50 private static final String SERVER_ADDR_STRING = "1.2.3.4";
51 private static final String IDENTITY_STRING = "Identity";
52 private static final byte[] PSK_BYTES = "preSharedKey".getBytes();
Benedict Wong80240ac2019-11-01 16:46:28 -070053
Lorenzo Colitticd675292021-02-04 17:32:07 +090054 private IVpnManager mMockService;
Benedict Wong80240ac2019-11-01 16:46:28 -070055 private VpnManager mVpnManager;
56 private final MockContext mMockContext =
57 new MockContext() {
58 @Override
59 public String getOpPackageName() {
Benedict Wong79ea64f2019-12-12 23:38:36 -080060 return PKG_NAME;
Benedict Wong80240ac2019-11-01 16:46:28 -070061 }
62 };
63
64 @Before
65 public void setUp() throws Exception {
Lorenzo Colitticd675292021-02-04 17:32:07 +090066 mMockService = mock(IVpnManager.class);
67 mVpnManager = new VpnManager(mMockContext, mMockService);
Benedict Wong80240ac2019-11-01 16:46:28 -070068 }
69
70 @Test
Benedict Wong79ea64f2019-12-12 23:38:36 -080071 public void testProvisionVpnProfilePreconsented() throws Exception {
72 final PlatformVpnProfile profile = getPlatformVpnProfile();
Lorenzo Colitticd675292021-02-04 17:32:07 +090073 when(mMockService.provisionVpnProfile(any(VpnProfile.class), eq(PKG_NAME)))
74 .thenReturn(true);
Benedict Wong79ea64f2019-12-12 23:38:36 -080075
76 // Expect there to be no intent returned, as consent has already been granted.
77 assertNull(mVpnManager.provisionVpnProfile(profile));
Lorenzo Colitticd675292021-02-04 17:32:07 +090078 verify(mMockService).provisionVpnProfile(eq(profile.toVpnProfile()), eq(PKG_NAME));
Benedict Wong79ea64f2019-12-12 23:38:36 -080079 }
80
81 @Test
82 public void testProvisionVpnProfileNeedsConsent() throws Exception {
83 final PlatformVpnProfile profile = getPlatformVpnProfile();
Lorenzo Colitticd675292021-02-04 17:32:07 +090084 when(mMockService.provisionVpnProfile(any(VpnProfile.class), eq(PKG_NAME)))
85 .thenReturn(false);
Benedict Wong79ea64f2019-12-12 23:38:36 -080086
87 // Expect intent to be returned, as consent has not already been granted.
Benedict Wonga7319912019-11-06 00:20:15 -080088 final Intent intent = mVpnManager.provisionVpnProfile(profile);
89 assertNotNull(intent);
90
91 final ComponentName expectedComponentName =
92 ComponentName.unflattenFromString(
93 "com.android.vpndialogs/com.android.vpndialogs.PlatformVpnConfirmDialog");
94 assertEquals(expectedComponentName, intent.getComponent());
Lorenzo Colitticd675292021-02-04 17:32:07 +090095 verify(mMockService).provisionVpnProfile(eq(profile.toVpnProfile()), eq(PKG_NAME));
Benedict Wong80240ac2019-11-01 16:46:28 -070096 }
97
98 @Test
99 public void testDeleteProvisionedVpnProfile() throws Exception {
Benedict Wong79ea64f2019-12-12 23:38:36 -0800100 mVpnManager.deleteProvisionedVpnProfile();
Lorenzo Colitticd675292021-02-04 17:32:07 +0900101 verify(mMockService).deleteVpnProfile(eq(PKG_NAME));
Benedict Wong80240ac2019-11-01 16:46:28 -0700102 }
103
104 @Test
105 public void testStartProvisionedVpnProfile() throws Exception {
Benedict Wong79ea64f2019-12-12 23:38:36 -0800106 mVpnManager.startProvisionedVpnProfile();
Lorenzo Colitticd675292021-02-04 17:32:07 +0900107 verify(mMockService).startVpnProfile(eq(PKG_NAME));
Benedict Wong80240ac2019-11-01 16:46:28 -0700108 }
109
110 @Test
111 public void testStopProvisionedVpnProfile() throws Exception {
Benedict Wong79ea64f2019-12-12 23:38:36 -0800112 mVpnManager.stopProvisionedVpnProfile();
Lorenzo Colitticd675292021-02-04 17:32:07 +0900113 verify(mMockService).stopVpnProfile(eq(PKG_NAME));
Benedict Wong79ea64f2019-12-12 23:38:36 -0800114 }
115
116 private Ikev2VpnProfile getPlatformVpnProfile() throws Exception {
117 return new Ikev2VpnProfile.Builder(SERVER_ADDR_STRING, IDENTITY_STRING)
118 .setBypassable(true)
119 .setMaxMtu(1300)
120 .setMetered(true)
121 .setAuthPsk(PSK_BYTES)
122 .build();
Benedict Wong80240ac2019-11-01 16:46:28 -0700123 }
Ken Chen5b1f25a2021-05-12 17:13:46 +0800124
125 @Test
126 public void testVpnTypesEqual() throws Exception {
127 SparseArray<String> vmVpnTypes = MessageUtils.findMessageNames(
128 new Class[] { VpnManager.class }, new String[]{ "TYPE_VPN_" });
129 SparseArray<String> nativeVpnType = MessageUtils.findMessageNames(
130 new Class[] { NativeVpnType.class }, new String[]{ "" });
131
132 // TYPE_VPN_NONE = -1 is only defined in VpnManager.
133 assertEquals(vmVpnTypes.size() - 1, nativeVpnType.size());
134 for (int i = VpnManager.TYPE_VPN_SERVICE; i < vmVpnTypes.size(); i++) {
135 assertEquals(vmVpnTypes.get(i), "TYPE_VPN_" + nativeVpnType.get(i));
136 }
137 }
Benedict Wong80240ac2019-11-01 16:46:28 -0700138}