From 3c46660097474de3fbdf07e9cfc788c122e8d874 Mon Sep 17 00:00:00 2001 From: Junyu Lai Date: Thu, 27 Oct 2022 16:53:02 +0800 Subject: [PATCH] Verifying equality of LinkProperties with callbacks During the test, content of LinkProperties might be changed according to network conditions, e.g. IPv6 RA, DNS64, etc. Thus, verify equality of two LinkProperties got in different time is not reliable and would cause flaky. Change it to verify with the LinkProperties received from callbacks. Test: atest CtsNetTestCases:android.net.cts.ConnectivityManagerTest Fix: 254188130 Change-Id: I845940ff6c6defb1887bfda4bcb47f01382b40bc --- .../net/cts/ConnectivityManagerTest.java | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/tests/cts/net/src/android/net/cts/ConnectivityManagerTest.java b/tests/cts/net/src/android/net/cts/ConnectivityManagerTest.java index 8dbcc0032d..5ef37948c0 100644 --- a/tests/cts/net/src/android/net/cts/ConnectivityManagerTest.java +++ b/tests/cts/net/src/android/net/cts/ConnectivityManagerTest.java @@ -556,6 +556,11 @@ public class ConnectivityManagerTest { // got from other APIs. final Network[] networks = mCm.getAllNetworks(); assertGreaterOrEqual(networks.length, 1); + final TestableNetworkCallback allNetworkLinkPropertiesListener = + new TestableNetworkCallback(); + mCm.registerNetworkCallback(new NetworkRequest.Builder().clearCapabilities().build(), + allNetworkLinkPropertiesListener); + final List snapshots = runWithShellPermissionIdentity( () -> mCm.getAllNetworkStateSnapshots(), NETWORK_SETTINGS); assertEquals(networks.length, snapshots.size()); @@ -581,7 +586,18 @@ public class ConnectivityManagerTest { assertEquals("", caps.describeImmutableDifferences( snapshot.getNetworkCapabilities() .setNetworkSpecifier(redactedSnapshotCapSpecifier))); - assertEquals(mCm.getLinkProperties(network), snapshot.getLinkProperties()); + + // Don't check that the mutable fields are the same with synchronous calls, as + // the device may add or remove content of these fields in the middle of the test. + // Instead, search the target LinkProperties from received LinkPropertiesChanged + // callbacks. This is guaranteed to succeed because the callback is registered + // before getAllNetworkStateSnapshots is called. + final LinkProperties lpFromSnapshot = snapshot.getLinkProperties(); + allNetworkLinkPropertiesListener.eventuallyExpect(CallbackEntry.LINK_PROPERTIES_CHANGED, + NETWORK_CALLBACK_TIMEOUT_MS, 0 /* mark */, entry -> + entry.getNetwork().equals(network) + && entry.getLp().equals(lpFromSnapshot)); + assertEquals(mCm.getNetworkInfo(network).getType(), snapshot.getLegacyType()); if (network.equals(cellNetwork)) {