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; |
abdelrahmani | 5bcbbc1 | 2023-06-13 19:56:07 +0000 | [diff] [blame] | 22 | import static org.junit.Assume.assumeFalse; |
Benedict Wong | 79ea64f | 2019-12-12 23:38:36 -0800 | [diff] [blame] | 23 | import static org.mockito.Matchers.any; |
| 24 | import static org.mockito.Matchers.eq; |
Benedict Wong | 80240ac | 2019-11-01 16:46:28 -0700 | [diff] [blame] | 25 | import static org.mockito.Mockito.mock; |
Benedict Wong | 79ea64f | 2019-12-12 23:38:36 -0800 | [diff] [blame] | 26 | import static org.mockito.Mockito.verify; |
| 27 | import static org.mockito.Mockito.when; |
Benedict Wong | 80240ac | 2019-11-01 16:46:28 -0700 | [diff] [blame] | 28 | |
Benedict Wong | a731991 | 2019-11-06 00:20:15 -0800 | [diff] [blame] | 29 | import android.content.ComponentName; |
| 30 | import android.content.Intent; |
abdelrahmani | 5bcbbc1 | 2023-06-13 19:56:07 +0000 | [diff] [blame] | 31 | import android.content.pm.PackageManager; |
Remi NGUYEN VAN | 154cf1d | 2021-06-29 17:16:28 +0900 | [diff] [blame] | 32 | import android.os.Build; |
Benedict Wong | 80240ac | 2019-11-01 16:46:28 -0700 | [diff] [blame] | 33 | import android.test.mock.MockContext; |
Ken Chen | 5b1f25a | 2021-05-12 17:13:46 +0800 | [diff] [blame] | 34 | import android.util.SparseArray; |
Benedict Wong | 80240ac | 2019-11-01 16:46:28 -0700 | [diff] [blame] | 35 | |
| 36 | import androidx.test.filters.SmallTest; |
abdelrahmani | 5bcbbc1 | 2023-06-13 19:56:07 +0000 | [diff] [blame] | 37 | import androidx.test.InstrumentationRegistry; |
Benedict Wong | 80240ac | 2019-11-01 16:46:28 -0700 | [diff] [blame] | 38 | |
Benedict Wong | 79ea64f | 2019-12-12 23:38:36 -0800 | [diff] [blame] | 39 | import com.android.internal.net.VpnProfile; |
Ken Chen | 5b1f25a | 2021-05-12 17:13:46 +0800 | [diff] [blame] | 40 | import com.android.internal.util.MessageUtils; |
Remi NGUYEN VAN | 154cf1d | 2021-06-29 17:16:28 +0900 | [diff] [blame] | 41 | import com.android.testutils.DevSdkIgnoreRule; |
| 42 | import com.android.testutils.DevSdkIgnoreRunner; |
Benedict Wong | 79ea64f | 2019-12-12 23:38:36 -0800 | [diff] [blame] | 43 | |
Benedict Wong | 80240ac | 2019-11-01 16:46:28 -0700 | [diff] [blame] | 44 | import org.junit.Before; |
| 45 | import org.junit.Test; |
| 46 | import org.junit.runner.RunWith; |
| 47 | |
| 48 | /** Unit tests for {@link VpnManager}. */ |
| 49 | @SmallTest |
Remi NGUYEN VAN | 154cf1d | 2021-06-29 17:16:28 +0900 | [diff] [blame] | 50 | @RunWith(DevSdkIgnoreRunner.class) |
| 51 | @DevSdkIgnoreRule.IgnoreUpTo(Build.VERSION_CODES.R) |
Benedict Wong | 80240ac | 2019-11-01 16:46:28 -0700 | [diff] [blame] | 52 | public class VpnManagerTest { |
abdelrahmani | 5bcbbc1 | 2023-06-13 19:56:07 +0000 | [diff] [blame] | 53 | |
Benedict Wong | 79ea64f | 2019-12-12 23:38:36 -0800 | [diff] [blame] | 54 | 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 Wong | 80240ac | 2019-11-01 16:46:28 -0700 | [diff] [blame] | 60 | |
Lorenzo Colitti | cd67529 | 2021-02-04 17:32:07 +0900 | [diff] [blame] | 61 | private IVpnManager mMockService; |
Benedict Wong | 80240ac | 2019-11-01 16:46:28 -0700 | [diff] [blame] | 62 | private VpnManager mVpnManager; |
| 63 | private final MockContext mMockContext = |
| 64 | new MockContext() { |
| 65 | @Override |
| 66 | public String getOpPackageName() { |
Benedict Wong | 79ea64f | 2019-12-12 23:38:36 -0800 | [diff] [blame] | 67 | return PKG_NAME; |
Benedict Wong | 80240ac | 2019-11-01 16:46:28 -0700 | [diff] [blame] | 68 | } |
| 69 | }; |
| 70 | |
| 71 | @Before |
| 72 | public void setUp() throws Exception { |
abdelrahmani | 5bcbbc1 | 2023-06-13 19:56:07 +0000 | [diff] [blame] | 73 | assumeFalse("Skipping test because watches don't support VPN", |
| 74 | InstrumentationRegistry.getContext().getPackageManager().hasSystemFeature( |
| 75 | PackageManager.FEATURE_WATCH)); |
Lorenzo Colitti | cd67529 | 2021-02-04 17:32:07 +0900 | [diff] [blame] | 76 | mMockService = mock(IVpnManager.class); |
| 77 | mVpnManager = new VpnManager(mMockContext, mMockService); |
Benedict Wong | 80240ac | 2019-11-01 16:46:28 -0700 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | @Test |
Benedict Wong | 79ea64f | 2019-12-12 23:38:36 -0800 | [diff] [blame] | 81 | public void testProvisionVpnProfilePreconsented() throws Exception { |
| 82 | final PlatformVpnProfile profile = getPlatformVpnProfile(); |
Lorenzo Colitti | cd67529 | 2021-02-04 17:32:07 +0900 | [diff] [blame] | 83 | when(mMockService.provisionVpnProfile(any(VpnProfile.class), eq(PKG_NAME))) |
| 84 | .thenReturn(true); |
Benedict Wong | 79ea64f | 2019-12-12 23:38:36 -0800 | [diff] [blame] | 85 | |
| 86 | // Expect there to be no intent returned, as consent has already been granted. |
| 87 | assertNull(mVpnManager.provisionVpnProfile(profile)); |
Lorenzo Colitti | cd67529 | 2021-02-04 17:32:07 +0900 | [diff] [blame] | 88 | verify(mMockService).provisionVpnProfile(eq(profile.toVpnProfile()), eq(PKG_NAME)); |
Benedict Wong | 79ea64f | 2019-12-12 23:38:36 -0800 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | @Test |
| 92 | public void testProvisionVpnProfileNeedsConsent() throws Exception { |
| 93 | final PlatformVpnProfile profile = getPlatformVpnProfile(); |
Lorenzo Colitti | cd67529 | 2021-02-04 17:32:07 +0900 | [diff] [blame] | 94 | when(mMockService.provisionVpnProfile(any(VpnProfile.class), eq(PKG_NAME))) |
| 95 | .thenReturn(false); |
Benedict Wong | 79ea64f | 2019-12-12 23:38:36 -0800 | [diff] [blame] | 96 | |
| 97 | // Expect intent to be returned, as consent has not already been granted. |
Benedict Wong | a731991 | 2019-11-06 00:20:15 -0800 | [diff] [blame] | 98 | 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 Colitti | cd67529 | 2021-02-04 17:32:07 +0900 | [diff] [blame] | 105 | verify(mMockService).provisionVpnProfile(eq(profile.toVpnProfile()), eq(PKG_NAME)); |
Benedict Wong | 80240ac | 2019-11-01 16:46:28 -0700 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | @Test |
| 109 | public void testDeleteProvisionedVpnProfile() throws Exception { |
Benedict Wong | 79ea64f | 2019-12-12 23:38:36 -0800 | [diff] [blame] | 110 | mVpnManager.deleteProvisionedVpnProfile(); |
Lorenzo Colitti | cd67529 | 2021-02-04 17:32:07 +0900 | [diff] [blame] | 111 | verify(mMockService).deleteVpnProfile(eq(PKG_NAME)); |
Benedict Wong | 80240ac | 2019-11-01 16:46:28 -0700 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | @Test |
| 115 | public void testStartProvisionedVpnProfile() throws Exception { |
Benedict Wong | 79ea64f | 2019-12-12 23:38:36 -0800 | [diff] [blame] | 116 | mVpnManager.startProvisionedVpnProfile(); |
Lorenzo Colitti | cd67529 | 2021-02-04 17:32:07 +0900 | [diff] [blame] | 117 | verify(mMockService).startVpnProfile(eq(PKG_NAME)); |
Benedict Wong | 80240ac | 2019-11-01 16:46:28 -0700 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | @Test |
| 121 | public void testStopProvisionedVpnProfile() throws Exception { |
Benedict Wong | 79ea64f | 2019-12-12 23:38:36 -0800 | [diff] [blame] | 122 | mVpnManager.stopProvisionedVpnProfile(); |
Lorenzo Colitti | cd67529 | 2021-02-04 17:32:07 +0900 | [diff] [blame] | 123 | verify(mMockService).stopVpnProfile(eq(PKG_NAME)); |
Benedict Wong | 79ea64f | 2019-12-12 23:38:36 -0800 | [diff] [blame] | 124 | } |
| 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 Wong | 80240ac | 2019-11-01 16:46:28 -0700 | [diff] [blame] | 133 | } |
Ken Chen | 5b1f25a | 2021-05-12 17:13:46 +0800 | [diff] [blame] | 134 | |
| 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 Wong | 80240ac | 2019-11-01 16:46:28 -0700 | [diff] [blame] | 148 | } |