From ee21b714920e8f24920c1e6e49ffa121d0367bc1 Mon Sep 17 00:00:00 2001 From: Remi NGUYEN VAN Date: Tue, 7 Jan 2025 17:51:35 +0900 Subject: [PATCH] Wait for default route for wifi check Instead of only waiting for an IPv6 address, wait for global IPv6 addresses to be routable. This mirrors the check used for AI_ADDRCONFIG in DnsResolver. Without this, the later call to network.getAllByName may only return IPv4 addresses if IPv6 global addresses are not reachable. Test: atest Change-Id: I2cedd58431eb8abeb5ffd9fb8daedb061586373b --- .../ConnectivityCheckTest.kt | 29 ++++++++++++++----- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/staticlibs/testutils/app/connectivitychecker/src/com/android/testutils/connectivitypreparer/ConnectivityCheckTest.kt b/staticlibs/testutils/app/connectivitychecker/src/com/android/testutils/connectivitypreparer/ConnectivityCheckTest.kt index 8e27c627bc..c42d9e57fc 100644 --- a/staticlibs/testutils/app/connectivitychecker/src/com/android/testutils/connectivitypreparer/ConnectivityCheckTest.kt +++ b/staticlibs/testutils/app/connectivitychecker/src/com/android/testutils/connectivitypreparer/ConnectivityCheckTest.kt @@ -19,7 +19,7 @@ package com.android.testutils.connectivitypreparer import android.Manifest.permission.NETWORK_SETTINGS import android.content.pm.PackageManager.FEATURE_TELEPHONY import android.content.pm.PackageManager.FEATURE_WIFI -import android.net.LinkAddress +import android.net.InetAddresses.parseNumericAddress import android.net.Network import android.net.NetworkCapabilities.NET_CAPABILITY_INTERNET import android.net.NetworkCapabilities.TRANSPORT_WIFI @@ -66,7 +66,8 @@ class ConnectivityCheckTest { // Skip IPv6 checks on virtual devices which do not support it. Tests that require IPv6 will // still fail even if the preparer does not. private fun ipv6Unsupported(wifiSsid: String?) = ConnectUtil.VIRTUAL_SSIDS.contains( - WifiInfo.sanitizeSsid(wifiSsid)) + WifiInfo.sanitizeSsid(wifiSsid) + ) @Test fun testCheckWifiSetup() { @@ -89,13 +90,25 @@ class ConnectivityCheckTest { pos = 0, timeoutMs = 30_000L ) { - it is LinkPropertiesChanged && - it.network == network && - it.lp.allLinkAddresses.any(LinkAddress::isIpv4) && - (ipv6Unsupported(ssid) || it.lp.hasGlobalIpv6Address()) + if (it !is LinkPropertiesChanged || it.network != network) { + false + } else { + // Same check as used by DnsResolver for AI_ADDRCONFIG (have_ipv4) + val ipv4Reachable = it.lp.isReachable(parseNumericAddress("8.8.8.8")) + // Same check as used by DnsResolver for AI_ADDRCONFIG (have_ipv6) + val ipv6Reachable = it.lp.isReachable(parseNumericAddress("2000::")) + ipv4Reachable && (ipv6Unsupported(ssid) || ipv6Reachable) + } } - assertNotNull(lpChange, "Wifi network $network needs an IPv4 address" + - if (ipv6Unsupported(ssid)) "" else " and a global IPv6 address") + assertNotNull( + lpChange, + "Wifi network $network needs an IPv4 address and default route" + + if (ipv6Unsupported(ssid)) { + "" + } else { + " and a global IPv6 address and default route" + } + ) Pair(network, ssid) }