From 7b2e5fe68ef60278242970aeec2ed4c2128e8037 Mon Sep 17 00:00:00 2001 From: Remi NGUYEN VAN Date: Wed, 11 Aug 2021 21:27:34 +0900 Subject: [PATCH] Fix ConnectivityDiagnosticsManagerTest for MTS When run as part of NetworkStack MTS, ConnectivityDiagnosticsManagerTest may fail as it verifies behavior that is only present will the latest tethering (connectivity) module installed. There is no guarantee that connectivity will be up-to-date for a given NetworkStack version, especially considering that on R->S device upgrade NetworkStack is likely to have been updated, but connectivity will be the factory version. Therefore NetworkStack tests need to cover devices that do not have an up-to-date connectivity module. Fix the test by observing that at least on T both module will have newer behavior. On S, accept both factory and updated behavior. Bug: 195727283 Test: atest ConnectivityDiagnosticsManagerTest Change-Id: Ifc0b09c884419d28817cfe619940c979ee1b0b9e --- .../ConnectivityDiagnosticsManagerTest.java | 54 ++++++++++++++----- 1 file changed, 41 insertions(+), 13 deletions(-) diff --git a/tests/cts/net/src/android/net/cts/ConnectivityDiagnosticsManagerTest.java b/tests/cts/net/src/android/net/cts/ConnectivityDiagnosticsManagerTest.java index a40c92d852..721ad82ff5 100644 --- a/tests/cts/net/src/android/net/cts/ConnectivityDiagnosticsManagerTest.java +++ b/tests/cts/net/src/android/net/cts/ConnectivityDiagnosticsManagerTest.java @@ -73,6 +73,7 @@ import android.platform.test.annotations.AppModeFull; import android.telephony.CarrierConfigManager; import android.telephony.SubscriptionManager; import android.telephony.TelephonyManager; +import android.util.ArraySet; import android.util.Pair; import androidx.test.InstrumentationRegistry; @@ -92,7 +93,9 @@ import org.junit.runner.RunWith; import java.security.MessageDigest; import java.util.ArrayList; +import java.util.Collections; import java.util.List; +import java.util.Set; import java.util.concurrent.CountDownLatch; import java.util.concurrent.Executor; import java.util.concurrent.TimeUnit; @@ -293,7 +296,7 @@ public class ConnectivityDiagnosticsManagerTest { final String interfaceName = mConnectivityManager.getLinkProperties(network).getInterfaceName(); - connDiagsCallback.expectOnConnectivityReportAvailable( + connDiagsCallback.maybeVerifyConnectivityReportAvailable( network, interfaceName, TRANSPORT_CELLULAR, NETWORK_VALIDATION_RESULT_VALID); connDiagsCallback.assertNoCallback(); } @@ -426,10 +429,15 @@ public class ConnectivityDiagnosticsManagerTest { cb.expectOnNetworkConnectivityReported(mTestNetwork, hasConnectivity); // All calls to #onNetworkConnectivityReported are expected to be accompanied by a call to - // #onConnectivityReportAvailable for S+ (for R, ConnectivityReports were only sent when the - // Network was re-validated - when reported connectivity != known connectivity). - if (SdkLevel.isAtLeastS() || !hasConnectivity) { + // #onConnectivityReportAvailable for T+ (for R, ConnectivityReports were only sent when the + // Network was re-validated - when reported connectivity != known connectivity). On S, + // recent module versions will have the callback, but not the earliest ones. + if (!hasConnectivity) { cb.expectOnConnectivityReportAvailable(mTestNetwork, interfaceName); + } else if (SdkLevel.isAtLeastS()) { + cb.maybeVerifyConnectivityReportAvailable(mTestNetwork, interfaceName, TRANSPORT_TEST, + getPossibleDiagnosticsValidationResults(), + SdkLevel.isAtLeastT() /* requireCallbackFired */); } cb.assertNoCallback(); @@ -485,18 +493,25 @@ public class ConnectivityDiagnosticsManagerTest { // Test Networks both do not require validation and are not tested for validation. This // results in the validation result being reported as SKIPPED for S+ (for R, the // platform marked these Networks as VALID). - final int expectedNetworkValidationResult = - SdkLevel.isAtLeastS() - ? NETWORK_VALIDATION_RESULT_SKIPPED - : NETWORK_VALIDATION_RESULT_VALID; - expectOnConnectivityReportAvailable( - network, interfaceName, TRANSPORT_TEST, expectedNetworkValidationResult); + + maybeVerifyConnectivityReportAvailable(network, interfaceName, TRANSPORT_TEST, + getPossibleDiagnosticsValidationResults(), true); } - public void expectOnConnectivityReportAvailable(@NonNull Network network, + public void maybeVerifyConnectivityReportAvailable(@NonNull Network network, @NonNull String interfaceName, int transportType, int expectedValidationResult) { + maybeVerifyConnectivityReportAvailable(network, interfaceName, transportType, + new ArraySet<>(Collections.singletonList(expectedValidationResult)), true); + } + + public void maybeVerifyConnectivityReportAvailable(@NonNull Network network, + @NonNull String interfaceName, int transportType, + Set possibleValidationResults, boolean requireCallbackFired) { final ConnectivityReport result = (ConnectivityReport) mHistory.poll(CALLBACK_TIMEOUT_MILLIS, x -> true); + if (!requireCallbackFired && result == null) { + return; + } assertEquals(network, result.getNetwork()); final NetworkCapabilities nc = result.getNetworkCapabilities(); @@ -508,8 +523,8 @@ public class ConnectivityDiagnosticsManagerTest { final PersistableBundle extras = result.getAdditionalInfo(); assertTrue(extras.containsKey(KEY_NETWORK_VALIDATION_RESULT)); final int actualValidationResult = extras.getInt(KEY_NETWORK_VALIDATION_RESULT); - assertEquals("Network validation result is incorrect", - expectedValidationResult, actualValidationResult); + assertTrue("Network validation result is incorrect: " + actualValidationResult, + possibleValidationResults.contains(actualValidationResult)); assertTrue(extras.containsKey(KEY_NETWORK_PROBES_SUCCEEDED_BITMASK)); final int probesSucceeded = extras.getInt(KEY_NETWORK_VALIDATION_RESULT); @@ -556,6 +571,19 @@ public class ConnectivityDiagnosticsManagerTest { } } + private static Set getPossibleDiagnosticsValidationResults() { + final Set possibleValidationResults = new ArraySet<>(); + possibleValidationResults.add(NETWORK_VALIDATION_RESULT_SKIPPED); + + // In S, some early module versions will return NETWORK_VALIDATION_RESULT_VALID. + // Starting from T, all module versions should only return SKIPPED. For platform < T, + // accept both values. + if (!SdkLevel.isAtLeastT()) { + possibleValidationResults.add(NETWORK_VALIDATION_RESULT_VALID); + } + return possibleValidationResults; + } + private class CarrierConfigReceiver extends BroadcastReceiver { // CountDownLatch used to wait for this BroadcastReceiver to be notified of a CarrierConfig // change. This latch will be counted down if a broadcast indicates this package has carrier