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; |
Remi NGUYEN VAN | 154cf1d | 2021-06-29 17:16:28 +0900 | [diff] [blame^] | 30 | import android.os.Build; |
Benedict Wong | 80240ac | 2019-11-01 16:46:28 -0700 | [diff] [blame] | 31 | import android.test.mock.MockContext; |
Ken Chen | 5b1f25a | 2021-05-12 17:13:46 +0800 | [diff] [blame] | 32 | import android.util.SparseArray; |
Benedict Wong | 80240ac | 2019-11-01 16:46:28 -0700 | [diff] [blame] | 33 | |
| 34 | import androidx.test.filters.SmallTest; |
Benedict Wong | 80240ac | 2019-11-01 16:46:28 -0700 | [diff] [blame] | 35 | |
Benedict Wong | 79ea64f | 2019-12-12 23:38:36 -0800 | [diff] [blame] | 36 | import com.android.internal.net.VpnProfile; |
Ken Chen | 5b1f25a | 2021-05-12 17:13:46 +0800 | [diff] [blame] | 37 | import com.android.internal.util.MessageUtils; |
Remi NGUYEN VAN | 154cf1d | 2021-06-29 17:16:28 +0900 | [diff] [blame^] | 38 | import com.android.testutils.DevSdkIgnoreRule; |
| 39 | import com.android.testutils.DevSdkIgnoreRunner; |
Benedict Wong | 79ea64f | 2019-12-12 23:38:36 -0800 | [diff] [blame] | 40 | |
Benedict Wong | 80240ac | 2019-11-01 16:46:28 -0700 | [diff] [blame] | 41 | import org.junit.Before; |
| 42 | import org.junit.Test; |
| 43 | import org.junit.runner.RunWith; |
| 44 | |
| 45 | /** Unit tests for {@link VpnManager}. */ |
| 46 | @SmallTest |
Remi NGUYEN VAN | 154cf1d | 2021-06-29 17:16:28 +0900 | [diff] [blame^] | 47 | @RunWith(DevSdkIgnoreRunner.class) |
| 48 | @DevSdkIgnoreRule.IgnoreUpTo(Build.VERSION_CODES.R) |
Benedict Wong | 80240ac | 2019-11-01 16:46:28 -0700 | [diff] [blame] | 49 | public class VpnManagerTest { |
Benedict Wong | 79ea64f | 2019-12-12 23:38:36 -0800 | [diff] [blame] | 50 | 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 Wong | 80240ac | 2019-11-01 16:46:28 -0700 | [diff] [blame] | 56 | |
Lorenzo Colitti | cd67529 | 2021-02-04 17:32:07 +0900 | [diff] [blame] | 57 | private IVpnManager mMockService; |
Benedict Wong | 80240ac | 2019-11-01 16:46:28 -0700 | [diff] [blame] | 58 | private VpnManager mVpnManager; |
| 59 | private final MockContext mMockContext = |
| 60 | new MockContext() { |
| 61 | @Override |
| 62 | public String getOpPackageName() { |
Benedict Wong | 79ea64f | 2019-12-12 23:38:36 -0800 | [diff] [blame] | 63 | return PKG_NAME; |
Benedict Wong | 80240ac | 2019-11-01 16:46:28 -0700 | [diff] [blame] | 64 | } |
| 65 | }; |
| 66 | |
| 67 | @Before |
| 68 | public void setUp() throws Exception { |
Lorenzo Colitti | cd67529 | 2021-02-04 17:32:07 +0900 | [diff] [blame] | 69 | mMockService = mock(IVpnManager.class); |
| 70 | mVpnManager = new VpnManager(mMockContext, mMockService); |
Benedict Wong | 80240ac | 2019-11-01 16:46:28 -0700 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | @Test |
Benedict Wong | 79ea64f | 2019-12-12 23:38:36 -0800 | [diff] [blame] | 74 | public void testProvisionVpnProfilePreconsented() throws Exception { |
| 75 | final PlatformVpnProfile profile = getPlatformVpnProfile(); |
Lorenzo Colitti | cd67529 | 2021-02-04 17:32:07 +0900 | [diff] [blame] | 76 | when(mMockService.provisionVpnProfile(any(VpnProfile.class), eq(PKG_NAME))) |
| 77 | .thenReturn(true); |
Benedict Wong | 79ea64f | 2019-12-12 23:38:36 -0800 | [diff] [blame] | 78 | |
| 79 | // Expect there to be no intent returned, as consent has already been granted. |
| 80 | assertNull(mVpnManager.provisionVpnProfile(profile)); |
Lorenzo Colitti | cd67529 | 2021-02-04 17:32:07 +0900 | [diff] [blame] | 81 | verify(mMockService).provisionVpnProfile(eq(profile.toVpnProfile()), eq(PKG_NAME)); |
Benedict Wong | 79ea64f | 2019-12-12 23:38:36 -0800 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | @Test |
| 85 | public void testProvisionVpnProfileNeedsConsent() throws Exception { |
| 86 | final PlatformVpnProfile profile = getPlatformVpnProfile(); |
Lorenzo Colitti | cd67529 | 2021-02-04 17:32:07 +0900 | [diff] [blame] | 87 | when(mMockService.provisionVpnProfile(any(VpnProfile.class), eq(PKG_NAME))) |
| 88 | .thenReturn(false); |
Benedict Wong | 79ea64f | 2019-12-12 23:38:36 -0800 | [diff] [blame] | 89 | |
| 90 | // Expect intent to be returned, as consent has not already been granted. |
Benedict Wong | a731991 | 2019-11-06 00:20:15 -0800 | [diff] [blame] | 91 | 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 Colitti | cd67529 | 2021-02-04 17:32:07 +0900 | [diff] [blame] | 98 | verify(mMockService).provisionVpnProfile(eq(profile.toVpnProfile()), eq(PKG_NAME)); |
Benedict Wong | 80240ac | 2019-11-01 16:46:28 -0700 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | @Test |
| 102 | public void testDeleteProvisionedVpnProfile() throws Exception { |
Benedict Wong | 79ea64f | 2019-12-12 23:38:36 -0800 | [diff] [blame] | 103 | mVpnManager.deleteProvisionedVpnProfile(); |
Lorenzo Colitti | cd67529 | 2021-02-04 17:32:07 +0900 | [diff] [blame] | 104 | verify(mMockService).deleteVpnProfile(eq(PKG_NAME)); |
Benedict Wong | 80240ac | 2019-11-01 16:46:28 -0700 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | @Test |
| 108 | public void testStartProvisionedVpnProfile() throws Exception { |
Benedict Wong | 79ea64f | 2019-12-12 23:38:36 -0800 | [diff] [blame] | 109 | mVpnManager.startProvisionedVpnProfile(); |
Lorenzo Colitti | cd67529 | 2021-02-04 17:32:07 +0900 | [diff] [blame] | 110 | verify(mMockService).startVpnProfile(eq(PKG_NAME)); |
Benedict Wong | 80240ac | 2019-11-01 16:46:28 -0700 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | @Test |
| 114 | public void testStopProvisionedVpnProfile() throws Exception { |
Benedict Wong | 79ea64f | 2019-12-12 23:38:36 -0800 | [diff] [blame] | 115 | mVpnManager.stopProvisionedVpnProfile(); |
Lorenzo Colitti | cd67529 | 2021-02-04 17:32:07 +0900 | [diff] [blame] | 116 | verify(mMockService).stopVpnProfile(eq(PKG_NAME)); |
Benedict Wong | 79ea64f | 2019-12-12 23:38:36 -0800 | [diff] [blame] | 117 | } |
| 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 Wong | 80240ac | 2019-11-01 16:46:28 -0700 | [diff] [blame] | 126 | } |
Ken Chen | 5b1f25a | 2021-05-12 17:13:46 +0800 | [diff] [blame] | 127 | |
| 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 Wong | 80240ac | 2019-11-01 16:46:28 -0700 | [diff] [blame] | 141 | } |