Merge "Correct the test design for network lost case"

This commit is contained in:
Lucas Lin
2022-12-08 03:25:05 +00:00
committed by Gerrit Code Review

View File

@@ -1498,8 +1498,18 @@ public class VpnTest extends VpnTestBase {
verify(mIkev2SessionCreator, timeout(TEST_TIMEOUT_MS)) verify(mIkev2SessionCreator, timeout(TEST_TIMEOUT_MS))
.createIkeSession(any(), any(), any(), any(), captor.capture(), any()); .createIkeSession(any(), any(), any(), any(), captor.capture(), any());
reset(mIkev2SessionCreator); reset(mIkev2SessionCreator);
// For network lost case, the process should be triggered by calling onLost(), which is the
// same process with the real case.
if (errorCode == VpnManager.ERROR_CODE_NETWORK_LOST) {
cb.onLost(TEST_NETWORK);
final ArgumentCaptor<Runnable> runnableCaptor =
ArgumentCaptor.forClass(Runnable.class);
verify(mExecutor).schedule(runnableCaptor.capture(), anyLong(), any());
runnableCaptor.getValue().run();
} else {
final IkeSessionCallback ikeCb = captor.getValue(); final IkeSessionCallback ikeCb = captor.getValue();
ikeCb.onClosedWithException(exception); ikeCb.onClosedWithException(exception);
}
verifyPowerSaveTempWhitelistApp(TEST_VPN_PKG); verifyPowerSaveTempWhitelistApp(TEST_VPN_PKG);
reset(mDeviceIdleInternal); reset(mDeviceIdleInternal);
@@ -1510,14 +1520,16 @@ public class VpnTest extends VpnTestBase {
.unregisterNetworkCallback(eq(cb)); .unregisterNetworkCallback(eq(cb));
} else if (errorType == VpnManager.ERROR_CLASS_RECOVERABLE) { } else if (errorType == VpnManager.ERROR_CLASS_RECOVERABLE) {
int retryIndex = 0; int retryIndex = 0;
final IkeSessionCallback ikeCb2 = verifyRetryAndGetNewIkeCb(retryIndex++); final IkeSessionCallback ikeCb2 = verifyRetryAndGetNewIkeCb(retryIndex++, errorCode);
if (ikeCb2 != null) {
ikeCb2.onClosedWithException(exception); ikeCb2.onClosedWithException(exception);
verifyRetryAndGetNewIkeCb(retryIndex++); verifyRetryAndGetNewIkeCb(retryIndex++, errorCode);
}
} }
} }
private IkeSessionCallback verifyRetryAndGetNewIkeCb(int retryIndex) { private IkeSessionCallback verifyRetryAndGetNewIkeCb(int retryIndex, int errorCode) {
final ArgumentCaptor<Runnable> runnableCaptor = final ArgumentCaptor<Runnable> runnableCaptor =
ArgumentCaptor.forClass(Runnable.class); ArgumentCaptor.forClass(Runnable.class);
final ArgumentCaptor<IkeSessionCallback> ikeCbCaptor = final ArgumentCaptor<IkeSessionCallback> ikeCbCaptor =
@@ -1530,15 +1542,21 @@ public class VpnTest extends VpnTestBase {
// Mock the event of firing the retry task // Mock the event of firing the retry task
runnableCaptor.getValue().run(); runnableCaptor.getValue().run();
// Vpn won't retry when there is no usable underlying network.
if (errorCode == VpnManager.ERROR_CODE_NETWORK_LOST) {
verify(mIkev2SessionCreator, never())
.createIkeSession(any(), any(), any(), any(), ikeCbCaptor.capture(), any());
} else {
verify(mIkev2SessionCreator) verify(mIkev2SessionCreator)
.createIkeSession(any(), any(), any(), any(), ikeCbCaptor.capture(), any()); .createIkeSession(any(), any(), any(), any(), ikeCbCaptor.capture(), any());
}
// Forget the mIkev2SessionCreator#createIkeSession call and mExecutor#schedule call // Forget the mIkev2SessionCreator#createIkeSession call and mExecutor#schedule call
// for the next retry verification // for the next retry verification
resetIkev2SessionCreator(mIkeSessionWrapper); resetIkev2SessionCreator(mIkeSessionWrapper);
resetExecutor(mScheduledFuture); resetExecutor(mScheduledFuture);
return ikeCbCaptor.getValue(); return (ikeCbCaptor.getAllValues().size() == 0) ? null : ikeCbCaptor.getValue();
} }
@Test @Test