Add VpnManager calls to ConnectivityService

This commit adds the relevant calls to ConnectivityService for the
VpnManager API to be functional

Bug: 144246837
Test: VpnManagerTest updated, FrameworksNetTests passing
Change-Id: I446a8595e3583a842a7f89c4f8d74526a85e311c
This commit is contained in:
Benedict Wong
2019-12-12 23:38:36 -08:00
parent df936cf1a7
commit 79ea64f963

View File

@@ -16,13 +16,21 @@
package android.net; package android.net;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.test.mock.MockContext; import android.test.mock.MockContext;
import androidx.test.filters.SmallTest; import androidx.test.filters.SmallTest;
import androidx.test.runner.AndroidJUnit4; import androidx.test.runner.AndroidJUnit4;
import com.android.internal.net.VpnProfile;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
@@ -31,7 +39,12 @@ import org.junit.runner.RunWith;
@SmallTest @SmallTest
@RunWith(AndroidJUnit4.class) @RunWith(AndroidJUnit4.class)
public class VpnManagerTest { public class VpnManagerTest {
private static final String VPN_PROFILE_KEY = "KEY"; private static final String PKG_NAME = "fooPackage";
private static final String SESSION_NAME_STRING = "testSession";
private static final String SERVER_ADDR_STRING = "1.2.3.4";
private static final String IDENTITY_STRING = "Identity";
private static final byte[] PSK_BYTES = "preSharedKey".getBytes();
private IConnectivityManager mMockCs; private IConnectivityManager mMockCs;
private VpnManager mVpnManager; private VpnManager mVpnManager;
@@ -39,7 +52,7 @@ public class VpnManagerTest {
new MockContext() { new MockContext() {
@Override @Override
public String getOpPackageName() { public String getOpPackageName() {
return "fooPackage"; return PKG_NAME;
} }
}; };
@@ -50,34 +63,49 @@ public class VpnManagerTest {
} }
@Test @Test
public void testProvisionVpnProfile() throws Exception { public void testProvisionVpnProfilePreconsented() throws Exception {
try { final PlatformVpnProfile profile = getPlatformVpnProfile();
mVpnManager.provisionVpnProfile(mock(PlatformVpnProfile.class)); when(mMockCs.provisionVpnProfile(any(VpnProfile.class), eq(PKG_NAME))).thenReturn(true);
} catch (UnsupportedOperationException expected) {
// Expect there to be no intent returned, as consent has already been granted.
assertNull(mVpnManager.provisionVpnProfile(profile));
verify(mMockCs).provisionVpnProfile(eq(profile.toVpnProfile()), eq(PKG_NAME));
} }
@Test
public void testProvisionVpnProfileNeedsConsent() throws Exception {
final PlatformVpnProfile profile = getPlatformVpnProfile();
when(mMockCs.provisionVpnProfile(any(VpnProfile.class), eq(PKG_NAME))).thenReturn(false);
// Expect intent to be returned, as consent has not already been granted.
assertNotNull(mVpnManager.provisionVpnProfile(profile));
verify(mMockCs).provisionVpnProfile(eq(profile.toVpnProfile()), eq(PKG_NAME));
} }
@Test @Test
public void testDeleteProvisionedVpnProfile() throws Exception { public void testDeleteProvisionedVpnProfile() throws Exception {
try {
mVpnManager.deleteProvisionedVpnProfile(); mVpnManager.deleteProvisionedVpnProfile();
} catch (UnsupportedOperationException expected) { verify(mMockCs).deleteVpnProfile(eq(PKG_NAME));
}
} }
@Test @Test
public void testStartProvisionedVpnProfile() throws Exception { public void testStartProvisionedVpnProfile() throws Exception {
try {
mVpnManager.startProvisionedVpnProfile(); mVpnManager.startProvisionedVpnProfile();
} catch (UnsupportedOperationException expected) { verify(mMockCs).startVpnProfile(eq(PKG_NAME));
}
} }
@Test @Test
public void testStopProvisionedVpnProfile() throws Exception { public void testStopProvisionedVpnProfile() throws Exception {
try {
mVpnManager.stopProvisionedVpnProfile(); mVpnManager.stopProvisionedVpnProfile();
} catch (UnsupportedOperationException expected) { verify(mMockCs).stopVpnProfile(eq(PKG_NAME));
} }
private Ikev2VpnProfile getPlatformVpnProfile() throws Exception {
return new Ikev2VpnProfile.Builder(SERVER_ADDR_STRING, IDENTITY_STRING)
.setBypassable(true)
.setMaxMtu(1300)
.setMetered(true)
.setAuthPsk(PSK_BYTES)
.build();
} }
} }