blob: 95a794235a2ef38dfe10f58a7c1ea319183d8cd3 [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;
31
32import androidx.test.filters.SmallTest;
33import androidx.test.runner.AndroidJUnit4;
34
Benedict Wong79ea64f2019-12-12 23:38:36 -080035import com.android.internal.net.VpnProfile;
36
Benedict Wong80240ac2019-11-01 16:46:28 -070037import org.junit.Before;
38import org.junit.Test;
39import org.junit.runner.RunWith;
40
41/** Unit tests for {@link VpnManager}. */
42@SmallTest
43@RunWith(AndroidJUnit4.class)
44public class VpnManagerTest {
Benedict Wong79ea64f2019-12-12 23:38:36 -080045 private static final String PKG_NAME = "fooPackage";
46
47 private static final String SESSION_NAME_STRING = "testSession";
48 private static final String SERVER_ADDR_STRING = "1.2.3.4";
49 private static final String IDENTITY_STRING = "Identity";
50 private static final byte[] PSK_BYTES = "preSharedKey".getBytes();
Benedict Wong80240ac2019-11-01 16:46:28 -070051
52 private IConnectivityManager mMockCs;
53 private VpnManager mVpnManager;
54 private final MockContext mMockContext =
55 new MockContext() {
56 @Override
57 public String getOpPackageName() {
Benedict Wong79ea64f2019-12-12 23:38:36 -080058 return PKG_NAME;
Benedict Wong80240ac2019-11-01 16:46:28 -070059 }
60 };
61
62 @Before
63 public void setUp() throws Exception {
64 mMockCs = mock(IConnectivityManager.class);
65 mVpnManager = new VpnManager(mMockContext, mMockCs);
66 }
67
68 @Test
Benedict Wong79ea64f2019-12-12 23:38:36 -080069 public void testProvisionVpnProfilePreconsented() throws Exception {
70 final PlatformVpnProfile profile = getPlatformVpnProfile();
71 when(mMockCs.provisionVpnProfile(any(VpnProfile.class), eq(PKG_NAME))).thenReturn(true);
72
73 // Expect there to be no intent returned, as consent has already been granted.
74 assertNull(mVpnManager.provisionVpnProfile(profile));
75 verify(mMockCs).provisionVpnProfile(eq(profile.toVpnProfile()), eq(PKG_NAME));
76 }
77
78 @Test
79 public void testProvisionVpnProfileNeedsConsent() throws Exception {
80 final PlatformVpnProfile profile = getPlatformVpnProfile();
81 when(mMockCs.provisionVpnProfile(any(VpnProfile.class), eq(PKG_NAME))).thenReturn(false);
82
83 // Expect intent to be returned, as consent has not already been granted.
Benedict Wonga7319912019-11-06 00:20:15 -080084 final Intent intent = mVpnManager.provisionVpnProfile(profile);
85 assertNotNull(intent);
86
87 final ComponentName expectedComponentName =
88 ComponentName.unflattenFromString(
89 "com.android.vpndialogs/com.android.vpndialogs.PlatformVpnConfirmDialog");
90 assertEquals(expectedComponentName, intent.getComponent());
Benedict Wong79ea64f2019-12-12 23:38:36 -080091 verify(mMockCs).provisionVpnProfile(eq(profile.toVpnProfile()), eq(PKG_NAME));
Benedict Wong80240ac2019-11-01 16:46:28 -070092 }
93
94 @Test
95 public void testDeleteProvisionedVpnProfile() throws Exception {
Benedict Wong79ea64f2019-12-12 23:38:36 -080096 mVpnManager.deleteProvisionedVpnProfile();
97 verify(mMockCs).deleteVpnProfile(eq(PKG_NAME));
Benedict Wong80240ac2019-11-01 16:46:28 -070098 }
99
100 @Test
101 public void testStartProvisionedVpnProfile() throws Exception {
Benedict Wong79ea64f2019-12-12 23:38:36 -0800102 mVpnManager.startProvisionedVpnProfile();
103 verify(mMockCs).startVpnProfile(eq(PKG_NAME));
Benedict Wong80240ac2019-11-01 16:46:28 -0700104 }
105
106 @Test
107 public void testStopProvisionedVpnProfile() throws Exception {
Benedict Wong79ea64f2019-12-12 23:38:36 -0800108 mVpnManager.stopProvisionedVpnProfile();
109 verify(mMockCs).stopVpnProfile(eq(PKG_NAME));
110 }
111
112 private Ikev2VpnProfile getPlatformVpnProfile() throws Exception {
113 return new Ikev2VpnProfile.Builder(SERVER_ADDR_STRING, IDENTITY_STRING)
114 .setBypassable(true)
115 .setMaxMtu(1300)
116 .setMetered(true)
117 .setAuthPsk(PSK_BYTES)
118 .build();
Benedict Wong80240ac2019-11-01 16:46:28 -0700119 }
120}