From 827b44daeaec3298429d4e031839e55b864c4bb3 Mon Sep 17 00:00:00 2001 From: lucaslin Date: Fri, 22 Apr 2022 03:14:46 +0800 Subject: [PATCH] Test VpnManager event for CATEGORY_EVENT_IKE_ERROR Bug: 191413541 Test: atest FrameworksNetTests:VpnTest Change-Id: Iebc0ac281eb03fac9626c64e44cdd14be1462d89 --- .../android/server/connectivity/VpnTest.java | 42 +++++++++++++++---- 1 file changed, 33 insertions(+), 9 deletions(-) diff --git a/tests/unit/java/com/android/server/connectivity/VpnTest.java b/tests/unit/java/com/android/server/connectivity/VpnTest.java index 73363deccb..b3c25ac8e7 100644 --- a/tests/unit/java/com/android/server/connectivity/VpnTest.java +++ b/tests/unit/java/com/android/server/connectivity/VpnTest.java @@ -95,6 +95,7 @@ import android.net.VpnProfileState; import android.net.VpnService; import android.net.VpnTransportInfo; import android.net.ipsec.ike.IkeSessionCallback; +import android.net.ipsec.ike.exceptions.IkeException; import android.net.ipsec.ike.exceptions.IkeProtocolException; import android.os.Build.VERSION_CODES; import android.os.Bundle; @@ -1165,15 +1166,16 @@ public class VpnTest { config -> Arrays.asList(config.flags).contains(flag))); } - @Test - public void testStartPlatformVpnAuthenticationFailed() throws Exception { + private void setupPlatformVpnWithSpecificExceptionAndItsErrorCode( + IkeException exception, int errorType, int errorCode) throws Exception { final ArgumentCaptor captor = ArgumentCaptor.forClass(IkeSessionCallback.class); - final IkeProtocolException exception = mock(IkeProtocolException.class); - when(exception.getErrorType()) - .thenReturn(IkeProtocolException.ERROR_TYPE_AUTHENTICATION_FAILED); - final Vpn vpn = startLegacyVpn(createVpn(primaryUser.id), (mVpnProfile)); + final Vpn vpn = createVpnAndSetupUidChecks(AppOpsManager.OPSTR_ACTIVATE_PLATFORM_VPN); + when(mVpnProfileStore.get(vpn.getProfileNameForPackage(TEST_VPN_PKG))) + .thenReturn(mVpnProfile.encode()); + + final String sessionKey = vpn.startVpnProfile(TEST_VPN_PKG); final NetworkCallback cb = triggerOnAvailableAndGetCallback(); verifyInterfaceSetCfgWithFlags(IF_STATE_UP); @@ -1183,10 +1185,32 @@ public class VpnTest { verify(mIkev2SessionCreator, timeout(TEST_TIMEOUT_MS)) .createIkeSession(any(), any(), any(), any(), captor.capture(), any()); final IkeSessionCallback ikeCb = captor.getValue(); - ikeCb.onClosedExceptionally(exception); + ikeCb.onClosedWithException(exception); - verify(mConnectivityManager, timeout(TEST_TIMEOUT_MS)).unregisterNetworkCallback(eq(cb)); - assertEquals(LegacyVpnInfo.STATE_FAILED, vpn.getLegacyVpnInfo().state); + verifyVpnManagerEvent(sessionKey, VpnManager.CATEGORY_EVENT_IKE_ERROR, errorType, errorCode, + null /* profileState */); + if (errorType == VpnManager.ERROR_CLASS_NOT_RECOVERABLE) { + verify(mConnectivityManager, timeout(TEST_TIMEOUT_MS)) + .unregisterNetworkCallback(eq(cb)); + } + } + + @Test + public void testStartPlatformVpnAuthenticationFailed() throws Exception { + final IkeProtocolException exception = mock(IkeProtocolException.class); + final int errorCode = IkeProtocolException.ERROR_TYPE_AUTHENTICATION_FAILED; + when(exception.getErrorType()).thenReturn(errorCode); + setupPlatformVpnWithSpecificExceptionAndItsErrorCode(exception, + VpnManager.ERROR_CLASS_NOT_RECOVERABLE, errorCode); + } + + @Test + public void testStartPlatformVpnFailedWithRecoverableError() throws Exception { + final IkeProtocolException exception = mock(IkeProtocolException.class); + final int errorCode = IkeProtocolException.ERROR_TYPE_TEMPORARY_FAILURE; + when(exception.getErrorType()).thenReturn(errorCode); + setupPlatformVpnWithSpecificExceptionAndItsErrorCode(exception, + VpnManager.ERROR_CLASS_RECOVERABLE, errorCode); } @Test