From 0366028f64ac9e7512c5217cacec0307c09f3e5b Mon Sep 17 00:00:00 2001 From: chiachangwang Date: Tue, 19 Apr 2022 18:57:19 +0800 Subject: [PATCH] Deflake testStartStopVpnProfileV6 and testStartStopVpnProfileV4 The tests expect to receive network validated callback on VPN network in S and below. However, there is a platform bug that VPN may misuse VPN network itself as the underlying network, and cause VPN reconnect. If this race happens before validation completed and callback sent, the test will fail to get the validated network callback. Restrict the validated capabilities check in T+ only devices which contain the necessary fixes. Also, unreigster the callback at the end of the test. Bug: 228790797 Test: atest android.net.cts.Ikev2VpnTest Change-Id: Id0ae0f983f989bfde5df2481e0d529bc5a140a7d --- .../net/src/android/net/cts/Ikev2VpnTest.java | 21 +++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/tests/cts/net/src/android/net/cts/Ikev2VpnTest.java b/tests/cts/net/src/android/net/cts/Ikev2VpnTest.java index 9590f88207..04843f90a0 100644 --- a/tests/cts/net/src/android/net/cts/Ikev2VpnTest.java +++ b/tests/cts/net/src/android/net/cts/Ikev2VpnTest.java @@ -87,6 +87,7 @@ import java.security.KeyPair; import java.security.KeyPairGenerator; import java.security.PrivateKey; import java.security.cert.X509Certificate; +import java.util.ArrayList; import java.util.Arrays; import java.util.Date; import java.util.List; @@ -196,6 +197,7 @@ public class Ikev2VpnTest { private final X509Certificate mServerRootCa; private final CertificateAndKey mUserCertKey; + private final List mCallbacksToUnregister = new ArrayList<>(); public Ikev2VpnTest() throws Exception { // Build certificates @@ -205,6 +207,9 @@ public class Ikev2VpnTest { @After public void tearDown() { + for (TestableNetworkCallback callback : mCallbacksToUnregister) { + sCM.unregisterNetworkCallback(callback); + } setAppop(AppOpsManager.OP_ACTIVATE_VPN, false); setAppop(AppOpsManager.OP_ACTIVATE_PLATFORM_VPN, false); } @@ -484,7 +489,7 @@ public class Ikev2VpnTest { final TestableNetworkCallback cb = new TestableNetworkCallback(TIMEOUT_MS); final NetworkRequest nr = new NetworkRequest.Builder() .clearCapabilities().addTransportType(TRANSPORT_VPN).build(); - sCM.registerNetworkCallback(nr, cb); + registerNetworkCallback(nr, cb); if (testSessionKey) { // testSessionKey will never be true if running on caps.hasTransport(TRANSPORT_VPN) + cb.expectCapabilitiesThat(vpnNetwork, TIMEOUT_MS, + caps -> caps.hasTransport(TRANSPORT_VPN) && caps.hasCapability(NET_CAPABILITY_INTERNET) && !caps.hasCapability(NET_CAPABILITY_VALIDATED) && Process.myUid() == caps.getOwnerUid()); @@ -533,8 +539,10 @@ public class Ikev2VpnTest { // but unexpectedly sends this callback, expecting LOST below will fail because the next // callback will be the validated capabilities instead. // In S and below, |requiresValidation| is ignored, so this callback is always sent - // regardless of its value. - if (!requiresValidation || !TestUtils.shouldTestTApis()) { + // regardless of its value. However, there is a race in Vpn(see b/228574221) that VPN may + // misuse VPN network itself as the underlying network. The fix is not available without + // SDK > T platform. Thus, verify this only on T+ platform. + if (!requiresValidation && TestUtils.shouldTestTApis()) { cb.eventuallyExpect(CallbackEntry.NETWORK_CAPS_UPDATED, TIMEOUT_MS, entry -> ((CallbackEntry.CapabilitiesChanged) entry).getCaps() .hasCapability(NET_CAPABILITY_VALIDATED)); @@ -547,6 +555,11 @@ public class Ikev2VpnTest { lost -> vpnNetwork.equals(lost.getNetwork())); } + private void registerNetworkCallback(NetworkRequest request, TestableNetworkCallback callback) { + sCM.registerNetworkCallback(request, callback); + mCallbacksToUnregister.add(callback); + } + private class VerifyStartStopVpnProfileTest implements TestNetworkRunnable.Test { private final boolean mTestIpv6Only; private final boolean mRequiresValidation;