|
| 1 | +// SPDX-License-Identifier: GPL-3.0-only |
| 2 | +// Copyright (C) 2026 The Somewhere Authors |
| 3 | + |
| 4 | +package eu.nodepass.somewhere.vpn |
| 5 | + |
| 6 | +import androidx.test.ext.junit.runners.AndroidJUnit4 |
| 7 | +import org.junit.After |
| 8 | +import org.junit.Assert.assertEquals |
| 9 | +import org.junit.Assert.assertTrue |
| 10 | +import org.junit.Before |
| 11 | +import org.junit.Test |
| 12 | +import org.junit.runner.RunWith |
| 13 | +import java.net.NetworkInterface |
| 14 | + |
| 15 | +/** |
| 16 | + * That the device accepted the TUN this client asked for. |
| 17 | + * |
| 18 | + * The configuration itself is checked by `TunConfigurationTest`, which needs no |
| 19 | + * device and checks the relationships between its parts. What that cannot check |
| 20 | + * is the part where Android is asked for it: `Builder.addAddress` and |
| 21 | + * `addRoute` validate what they are given and throw, `establish()` returns null |
| 22 | + * rather than explaining itself, and a family the platform declined is not an |
| 23 | + * exception — it is a tunnel that comes up carrying one family while the DNS |
| 24 | + * layer mints placeholders for two. |
| 25 | + * |
| 26 | + * That failure has a specific shape worth naming: every name still resolves, |
| 27 | + * IPv4 still works, and the only thing that breaks is whatever the device |
| 28 | + * decided to try over IPv6 first — intermittently, per app, and never in a way |
| 29 | + * that points here. |
| 30 | + * |
| 31 | + * **This is deliberately not a traffic test.** This client is forced out of its |
| 32 | + * own tunnel in every mode, so nothing this process sends enters the TUN. What |
| 33 | + * a test inside the app can honestly observe is the interface the platform |
| 34 | + * built, which is exactly what this reads. |
| 35 | + */ |
| 36 | +@RunWith(AndroidJUnit4::class) |
| 37 | +class TunFamiliesOnDeviceTest { |
| 38 | + @Before |
| 39 | + fun startTunnel() { |
| 40 | + E2eEnvironment.requireConsent(TunnelHarness.context) |
| 41 | + TunnelHarness.start() |
| 42 | + } |
| 43 | + |
| 44 | + @After |
| 45 | + fun stopTunnel() { |
| 46 | + runCatching { TunnelHarness.stop() } |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * Every address on an interface, as bytes. |
| 51 | + * |
| 52 | + * Bytes rather than the printed form: `getHostAddress` does not agree with |
| 53 | + * itself across implementations for IPv6 — one prints `fd66::2` and another |
| 54 | + * `fd66:0:0:0:0:0:0:2` — so a string comparison here would pass or fail for |
| 55 | + * reasons that have nothing to do with the tunnel. |
| 56 | + */ |
| 57 | + private fun addressesOf(candidate: NetworkInterface): List<ByteArray> = candidate.inetAddresses.toList().map { it.address } |
| 58 | + |
| 59 | + /** The interface carrying the TUN's own IPv4 address, or null. */ |
| 60 | + private fun tunInterface(): NetworkInterface? { |
| 61 | + val expected = TunConfiguration.parse(TunConfiguration.addresses.first { !it.isIpv6 }.address)!! |
| 62 | + return NetworkInterface |
| 63 | + .getNetworkInterfaces() |
| 64 | + .toList() |
| 65 | + .firstOrNull { candidate -> addressesOf(candidate).any { it.contentEquals(expected) } } |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + fun theTunnelInterfaceCarriesEveryFamilyThisClientDeclares() { |
| 70 | + val tun = |
| 71 | + tunInterface() ?: throw AssertionError( |
| 72 | + "no interface carries the TUN's own address; the tunnel reported Connected without one", |
| 73 | + ) |
| 74 | + |
| 75 | + val present = addressesOf(tun) |
| 76 | + val printed = present.joinToString { bytes -> bytes.joinToString("") { "%02x".format(it) } } |
| 77 | + TunConfiguration.addresses.forEach { declared -> |
| 78 | + val expected = TunConfiguration.parse(declared.address)!! |
| 79 | + assertTrue( |
| 80 | + "the platform did not give ${tun.name} the declared address ${declared.address}; it has $printed", |
| 81 | + present.any { it.contentEquals(expected) }, |
| 82 | + ) |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + @Test |
| 87 | + fun theInterfaceMtuIsTheOneTheConfigurationAsksFor() { |
| 88 | + // A platform that silently clamped the MTU would make every path-MTU |
| 89 | + // measurement in the device pass measure the clamp instead. |
| 90 | + val tun = tunInterface() ?: throw AssertionError("no interface carries the TUN's own address") |
| 91 | + assertEquals("the TUN's MTU is not what was asked for", TunConfiguration.MTU, tun.mtu) |
| 92 | + } |
| 93 | + |
| 94 | + @Test |
| 95 | + fun carryingIpv6AndSynthesisingItAreTheSameDecision() { |
| 96 | + // Restated on the device because the two live in different layers and |
| 97 | + // the failure is silent: an AAAA record pointing at an address nothing |
| 98 | + // routes fails at the device with no error anyone can act on. |
| 99 | + val tun = tunInterface() ?: throw AssertionError("no interface carries the TUN's own address") |
| 100 | + val hasIpv6 = |
| 101 | + tun.inetAddresses |
| 102 | + .toList() |
| 103 | + .any { it.address.size == 16 && !it.isLinkLocalAddress } |
| 104 | + assertEquals( |
| 105 | + "the tunnel carries IPv6=$hasIpv6 while the configuration says ${TunConfiguration.carriesIpv6}", |
| 106 | + TunConfiguration.carriesIpv6, |
| 107 | + hasIpv6, |
| 108 | + ) |
| 109 | + } |
| 110 | +} |
0 commit comments