Benedict Wong | 80240ac | 2019-11-01 16:46:28 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package android.net; |
| 18 | |
Benedict Wong | a731991 | 2019-11-06 00:20:15 -0800 | [diff] [blame^] | 19 | import static org.junit.Assert.assertEquals; |
Benedict Wong | 79ea64f | 2019-12-12 23:38:36 -0800 | [diff] [blame] | 20 | import static org.junit.Assert.assertNotNull; |
| 21 | import static org.junit.Assert.assertNull; |
| 22 | import static org.mockito.Matchers.any; |
| 23 | import static org.mockito.Matchers.eq; |
Benedict Wong | 80240ac | 2019-11-01 16:46:28 -0700 | [diff] [blame] | 24 | import static org.mockito.Mockito.mock; |
Benedict Wong | 79ea64f | 2019-12-12 23:38:36 -0800 | [diff] [blame] | 25 | import static org.mockito.Mockito.verify; |
| 26 | import static org.mockito.Mockito.when; |
Benedict Wong | 80240ac | 2019-11-01 16:46:28 -0700 | [diff] [blame] | 27 | |
Benedict Wong | a731991 | 2019-11-06 00:20:15 -0800 | [diff] [blame^] | 28 | import android.content.ComponentName; |
| 29 | import android.content.Intent; |
Benedict Wong | 80240ac | 2019-11-01 16:46:28 -0700 | [diff] [blame] | 30 | import android.test.mock.MockContext; |
| 31 | |
| 32 | import androidx.test.filters.SmallTest; |
| 33 | import androidx.test.runner.AndroidJUnit4; |
| 34 | |
Benedict Wong | 79ea64f | 2019-12-12 23:38:36 -0800 | [diff] [blame] | 35 | import com.android.internal.net.VpnProfile; |
| 36 | |
Benedict Wong | 80240ac | 2019-11-01 16:46:28 -0700 | [diff] [blame] | 37 | import org.junit.Before; |
| 38 | import org.junit.Test; |
| 39 | import org.junit.runner.RunWith; |
| 40 | |
| 41 | /** Unit tests for {@link VpnManager}. */ |
| 42 | @SmallTest |
| 43 | @RunWith(AndroidJUnit4.class) |
| 44 | public class VpnManagerTest { |
Benedict Wong | 79ea64f | 2019-12-12 23:38:36 -0800 | [diff] [blame] | 45 | 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 Wong | 80240ac | 2019-11-01 16:46:28 -0700 | [diff] [blame] | 51 | |
| 52 | private IConnectivityManager mMockCs; |
| 53 | private VpnManager mVpnManager; |
| 54 | private final MockContext mMockContext = |
| 55 | new MockContext() { |
| 56 | @Override |
| 57 | public String getOpPackageName() { |
Benedict Wong | 79ea64f | 2019-12-12 23:38:36 -0800 | [diff] [blame] | 58 | return PKG_NAME; |
Benedict Wong | 80240ac | 2019-11-01 16:46:28 -0700 | [diff] [blame] | 59 | } |
| 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 Wong | 79ea64f | 2019-12-12 23:38:36 -0800 | [diff] [blame] | 69 | 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 Wong | a731991 | 2019-11-06 00:20:15 -0800 | [diff] [blame^] | 84 | 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 Wong | 79ea64f | 2019-12-12 23:38:36 -0800 | [diff] [blame] | 91 | verify(mMockCs).provisionVpnProfile(eq(profile.toVpnProfile()), eq(PKG_NAME)); |
Benedict Wong | 80240ac | 2019-11-01 16:46:28 -0700 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | @Test |
| 95 | public void testDeleteProvisionedVpnProfile() throws Exception { |
Benedict Wong | 79ea64f | 2019-12-12 23:38:36 -0800 | [diff] [blame] | 96 | mVpnManager.deleteProvisionedVpnProfile(); |
| 97 | verify(mMockCs).deleteVpnProfile(eq(PKG_NAME)); |
Benedict Wong | 80240ac | 2019-11-01 16:46:28 -0700 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | @Test |
| 101 | public void testStartProvisionedVpnProfile() throws Exception { |
Benedict Wong | 79ea64f | 2019-12-12 23:38:36 -0800 | [diff] [blame] | 102 | mVpnManager.startProvisionedVpnProfile(); |
| 103 | verify(mMockCs).startVpnProfile(eq(PKG_NAME)); |
Benedict Wong | 80240ac | 2019-11-01 16:46:28 -0700 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | @Test |
| 107 | public void testStopProvisionedVpnProfile() throws Exception { |
Benedict Wong | 79ea64f | 2019-12-12 23:38:36 -0800 | [diff] [blame] | 108 | 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 Wong | 80240ac | 2019-11-01 16:46:28 -0700 | [diff] [blame] | 119 | } |
| 120 | } |