Remi NGUYEN VAN | 31022d6 | 2021-05-11 13:37:06 +0000 | [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 | |
| 19 | import static org.junit.Assert.assertEquals; |
| 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; |
| 24 | import static org.mockito.Mockito.mock; |
| 25 | import static org.mockito.Mockito.verify; |
| 26 | import static org.mockito.Mockito.when; |
| 27 | |
| 28 | import android.content.ComponentName; |
| 29 | import android.content.Intent; |
| 30 | import android.test.mock.MockContext; |
| 31 | import android.util.SparseArray; |
| 32 | |
| 33 | import androidx.test.filters.SmallTest; |
| 34 | import androidx.test.runner.AndroidJUnit4; |
| 35 | |
| 36 | import com.android.internal.net.VpnProfile; |
| 37 | import com.android.internal.util.MessageUtils; |
| 38 | |
| 39 | import org.junit.Before; |
| 40 | import org.junit.Test; |
| 41 | import org.junit.runner.RunWith; |
| 42 | |
| 43 | /** Unit tests for {@link VpnManager}. */ |
| 44 | @SmallTest |
| 45 | @RunWith(AndroidJUnit4.class) |
| 46 | public class VpnManagerTest { |
| 47 | 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(); |
| 53 | |
| 54 | private IVpnManager mMockService; |
| 55 | private VpnManager mVpnManager; |
| 56 | private final MockContext mMockContext = |
| 57 | new MockContext() { |
| 58 | @Override |
| 59 | public String getOpPackageName() { |
| 60 | return PKG_NAME; |
| 61 | } |
| 62 | }; |
| 63 | |
| 64 | @Before |
| 65 | public void setUp() throws Exception { |
| 66 | mMockService = mock(IVpnManager.class); |
| 67 | mVpnManager = new VpnManager(mMockContext, mMockService); |
| 68 | } |
| 69 | |
| 70 | @Test |
| 71 | public void testProvisionVpnProfilePreconsented() throws Exception { |
| 72 | final PlatformVpnProfile profile = getPlatformVpnProfile(); |
| 73 | when(mMockService.provisionVpnProfile(any(VpnProfile.class), eq(PKG_NAME))) |
| 74 | .thenReturn(true); |
| 75 | |
| 76 | // Expect there to be no intent returned, as consent has already been granted. |
| 77 | assertNull(mVpnManager.provisionVpnProfile(profile)); |
| 78 | verify(mMockService).provisionVpnProfile(eq(profile.toVpnProfile()), eq(PKG_NAME)); |
| 79 | } |
| 80 | |
| 81 | @Test |
| 82 | public void testProvisionVpnProfileNeedsConsent() throws Exception { |
| 83 | final PlatformVpnProfile profile = getPlatformVpnProfile(); |
| 84 | when(mMockService.provisionVpnProfile(any(VpnProfile.class), eq(PKG_NAME))) |
| 85 | .thenReturn(false); |
| 86 | |
| 87 | // Expect intent to be returned, as consent has not already been granted. |
| 88 | 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()); |
| 95 | verify(mMockService).provisionVpnProfile(eq(profile.toVpnProfile()), eq(PKG_NAME)); |
| 96 | } |
| 97 | |
| 98 | @Test |
| 99 | public void testDeleteProvisionedVpnProfile() throws Exception { |
| 100 | mVpnManager.deleteProvisionedVpnProfile(); |
| 101 | verify(mMockService).deleteVpnProfile(eq(PKG_NAME)); |
| 102 | } |
| 103 | |
| 104 | @Test |
| 105 | public void testStartProvisionedVpnProfile() throws Exception { |
| 106 | mVpnManager.startProvisionedVpnProfile(); |
| 107 | verify(mMockService).startVpnProfile(eq(PKG_NAME)); |
| 108 | } |
| 109 | |
| 110 | @Test |
| 111 | public void testStopProvisionedVpnProfile() throws Exception { |
| 112 | mVpnManager.stopProvisionedVpnProfile(); |
| 113 | verify(mMockService).stopVpnProfile(eq(PKG_NAME)); |
| 114 | } |
| 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(); |
| 123 | } |
| 124 | |
| 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 | } |
| 138 | } |