test: cover the local network classifier and gate state machine

Extract the on-link decision into a pure function, test it with the
Android 17 SDK through Robolectric's connectivity shadows, and exercise
the gate's grant, denial, cancellation and stale-check paths with a fake
checker. The SDK 37 shadow needs jdk.internal.access exported to the
test JVM.
This commit is contained in:
austinried
2026-11-14 12:08:14 +00:00
parent 94093d5e91
commit 63c41c24b3
6 changed files with 430 additions and 31 deletions
@@ -23,35 +23,24 @@ class LocalNetworkAccess(
private val deniedAddresses = mutableSetOf<String>()
suspend fun requiresPermission(address: String): Boolean {
if (Build.VERSION.SDK_INT < LOCAL_NETWORK_PERMISSION_SDK) return false
if (granted()) return false
val granted = granted()
if (Build.VERSION.SDK_INT < LOCAL_NETWORK_PERMISSION_SDK || granted) return false
val host = serverHost(address) ?: return false
return withContext(Dispatchers.IO) {
val resolved = if (isLocalHostname(host)) emptyList() else resolve(host)
val linkAddresses = currentLinkAddresses()
if (linkAddresses.isEmpty()) return@withContext false
if (isLocalHostname(host)) return@withContext true
val resolved = runCatching { InetAddress.getAllByName(host).toList() }.getOrDefault(emptyList())
if (resolved.isEmpty()) return@withContext false
val current = currentLinkAddresses()
resolved.any { candidate ->
!routedThroughVpn(candidate) && current.any { it.contains(candidate) }
}
needsPermission(
sdk = Build.VERSION.SDK_INT,
granted = granted,
host = host,
hasEligibleInterface = linkAddresses.isNotEmpty(),
resolved = resolved,
onLink = { candidate -> linkAddresses.any { it.contains(candidate) } },
vpnRouted = ::routedThroughVpn,
)
}
}
@RequiresApi(LOCAL_NETWORK_PERMISSION_SDK)
private fun routedThroughVpn(address: InetAddress): Boolean {
val manager = context.getSystemService(ConnectivityManager::class.java) ?: return false
val network = manager.activeNetwork ?: return false
val capabilities = manager.getNetworkCapabilities(network) ?: return false
if (!capabilities.hasTransport(NetworkCapabilities.TRANSPORT_VPN)) return false
return manager
.getLinkProperties(network)
?.routes
.orEmpty()
.any { route -> route.destination.contains(address) }
}
@Synchronized
fun markDenied(address: String) {
deniedAddresses += normalizeServerAddress(address)
@@ -63,6 +52,8 @@ class LocalNetworkAccess(
private fun granted(): Boolean =
ContextCompat.checkSelfPermission(context, LOCAL_NETWORK_PERMISSION) == PackageManager.PERMISSION_GRANTED
private fun resolve(host: String): List<InetAddress> = runCatching { InetAddress.getAllByName(host).toList() }.getOrDefault(emptyList())
@Suppress("DEPRECATION")
private fun currentLinkAddresses(): List<LinkAddress> {
val manager = context.getSystemService(ConnectivityManager::class.java) ?: return emptyList()
@@ -70,12 +61,31 @@ class LocalNetworkAccess(
.filter { network -> manager.getNetworkCapabilities(network)?.isLocalNetworkInterface == true }
.flatMap { network -> manager.getLinkProperties(network)?.linkAddresses.orEmpty() }
}
@RequiresApi(LOCAL_NETWORK_PERMISSION_SDK)
private fun routedThroughVpn(address: InetAddress): Boolean {
val manager = context.getSystemService(ConnectivityManager::class.java) ?: return false
val network = manager.activeNetwork ?: return false
val capabilities = manager.getNetworkCapabilities(network) ?: return false
if (!capabilities.hasTransport(NetworkCapabilities.TRANSPORT_VPN)) return false
val properties = manager.getLinkProperties(network) ?: return false
return properties.routes.any { route -> route.destination.contains(address) }
}
}
private val NetworkCapabilities.isLocalNetworkInterface: Boolean
get() =
!hasTransport(NetworkCapabilities.TRANSPORT_VPN) &&
!hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)
internal fun needsPermission(
sdk: Int,
granted: Boolean,
host: String?,
hasEligibleInterface: Boolean,
resolved: List<InetAddress>,
onLink: (InetAddress) -> Boolean,
vpnRouted: (InetAddress) -> Boolean,
): Boolean {
if (sdk < LOCAL_NETWORK_PERMISSION_SDK || granted || host == null || !hasEligibleInterface) return false
if (isLocalHostname(host)) return true
return resolved.any { candidate -> !vpnRouted(candidate) && onLink(candidate) }
}
internal fun isLocalHostname(host: String): Boolean {
val normalized = host.lowercase().trimEnd('.')
@@ -84,3 +94,8 @@ internal fun isLocalHostname(host: String): Boolean {
@RequiresApi(LOCAL_NETWORK_PERMISSION_SDK)
private fun LinkAddress.contains(address: InetAddress): Boolean = IpPrefix(this.address, prefixLength).contains(address)
private val NetworkCapabilities.isLocalNetworkInterface: Boolean
get() =
!hasTransport(NetworkCapabilities.TRANSPORT_VPN) &&
!hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)
@@ -33,7 +33,8 @@ import org.koin.compose.koinInject
class LocalNetworkGate internal constructor(
private val scope: CoroutineScope,
private val checker: LocalNetworkAccess,
private val requiresPermission: suspend (String) -> Boolean,
private val wasDenied: (String) -> Boolean,
private val requestPermission: (address: String, onGranted: () -> Unit, onDenied: () -> Unit) -> Unit,
private val clearPrompt: () -> Unit,
) {
@@ -57,7 +58,7 @@ class LocalNetworkGate internal constructor(
scope.launch {
val required =
try {
checker.requiresPermission(address)
requiresPermission(address)
} catch (cancellation: CancellationException) {
throw cancellation
} catch (_: Exception) {
@@ -65,7 +66,7 @@ class LocalNetworkGate internal constructor(
}
if (current != generation) return@launch
ensureActive()
if (required && !(automatic && checker.wasDenied(address))) {
if (required && !(automatic && wasDenied(address))) {
requestPermission(address, onGranted, onDenied)
} else {
onNotNeeded()
@@ -114,7 +115,8 @@ fun rememberLocalNetworkGate(): LocalNetworkGate {
remember(scope, checker) {
LocalNetworkGate(
scope = scope,
checker = checker,
requiresPermission = checker::requiresPermission,
wasDenied = checker::wasDenied,
requestPermission = { address, onGranted, onDenied ->
if (pending == null) pending = PendingRequest(address, onGranted, onDenied)
},