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.os.Build; 31 import android.test.mock.MockContext; 32 import android.util.SparseArray; 33 34 import androidx.test.filters.SmallTest; 35 36 import com.android.internal.net.VpnProfile; 37 import com.android.internal.util.MessageUtils; 38 import com.android.testutils.DevSdkIgnoreRule; 39 import com.android.testutils.DevSdkIgnoreRunner; 40 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 47 @RunWith(DevSdkIgnoreRunner.class) 48 @DevSdkIgnoreRule.IgnoreUpTo(Build.VERSION_CODES.R) 49 public class VpnManagerTest { 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(); 56 57 private IVpnManager mMockService; 58 private VpnManager mVpnManager; 59 private final MockContext mMockContext = 60 new MockContext() { 61 @Override 62 public String getOpPackageName() { 63 return PKG_NAME; 64 } 65 }; 66 67 @Before setUp()68 public void setUp() throws Exception { 69 mMockService = mock(IVpnManager.class); 70 mVpnManager = new VpnManager(mMockContext, mMockService); 71 } 72 73 @Test testProvisionVpnProfilePreconsented()74 public void testProvisionVpnProfilePreconsented() throws Exception { 75 final PlatformVpnProfile profile = getPlatformVpnProfile(); 76 when(mMockService.provisionVpnProfile(any(VpnProfile.class), eq(PKG_NAME))) 77 .thenReturn(true); 78 79 // Expect there to be no intent returned, as consent has already been granted. 80 assertNull(mVpnManager.provisionVpnProfile(profile)); 81 verify(mMockService).provisionVpnProfile(eq(profile.toVpnProfile()), eq(PKG_NAME)); 82 } 83 84 @Test testProvisionVpnProfileNeedsConsent()85 public void testProvisionVpnProfileNeedsConsent() throws Exception { 86 final PlatformVpnProfile profile = getPlatformVpnProfile(); 87 when(mMockService.provisionVpnProfile(any(VpnProfile.class), eq(PKG_NAME))) 88 .thenReturn(false); 89 90 // Expect intent to be returned, as consent has not already been granted. 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()); 98 verify(mMockService).provisionVpnProfile(eq(profile.toVpnProfile()), eq(PKG_NAME)); 99 } 100 101 @Test testDeleteProvisionedVpnProfile()102 public void testDeleteProvisionedVpnProfile() throws Exception { 103 mVpnManager.deleteProvisionedVpnProfile(); 104 verify(mMockService).deleteVpnProfile(eq(PKG_NAME)); 105 } 106 107 @Test testStartProvisionedVpnProfile()108 public void testStartProvisionedVpnProfile() throws Exception { 109 mVpnManager.startProvisionedVpnProfile(); 110 verify(mMockService).startVpnProfile(eq(PKG_NAME)); 111 } 112 113 @Test testStopProvisionedVpnProfile()114 public void testStopProvisionedVpnProfile() throws Exception { 115 mVpnManager.stopProvisionedVpnProfile(); 116 verify(mMockService).stopVpnProfile(eq(PKG_NAME)); 117 } 118 getPlatformVpnProfile()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(); 126 } 127 128 @Test testVpnTypesEqual()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 } 141 } 142