|
| 1 | +package tech.httptoolkit.android.vpn |
| 2 | + |
| 3 | +import android.app.Application |
| 4 | +import com.google.common.truth.Truth.assertThat |
| 5 | +import org.junit.After |
| 6 | +import org.junit.Before |
| 7 | +import org.junit.Test |
| 8 | +import org.junit.runner.RunWith |
| 9 | +import org.robolectric.RobolectricTestRunner |
| 10 | +import org.robolectric.annotation.Config |
| 11 | +import tech.httptoolkit.android.vpn.transport.ip.IPAddress |
| 12 | +import tech.httptoolkit.android.vpn.transport.ip.IPHeader |
| 13 | +import tech.httptoolkit.android.vpn.transport.tcp.TCPHeader |
| 14 | +import java.net.DatagramPacket |
| 15 | +import java.net.DatagramSocket |
| 16 | +import java.net.InetAddress |
| 17 | +import java.net.ServerSocket |
| 18 | +import java.net.Socket |
| 19 | +import java.util.concurrent.Executors |
| 20 | +import java.util.concurrent.TimeUnit |
| 21 | + |
| 22 | +/** |
| 23 | + * End-to-end correctness checks on the engine's connection tracking, through the real |
| 24 | + * forwarding pipeline against loopback peers. Exercises both directions: |
| 25 | + * |
| 26 | + * - egress (packet intercepted from a local app): the engine creates/reuses the right |
| 27 | + * session per 5-tuple, and demultiplexes concurrent connections without crossing streams; |
| 28 | + * - ingress (data coming back from the LAN peer): each reply is routed back to the exact |
| 29 | + * client connection that originated it, and traffic for an unknown connection is rejected. |
| 30 | + */ |
| 31 | +@RunWith(RobolectricTestRunner::class) |
| 32 | +@Config(sdk = [34], application = Application::class) |
| 33 | +class ConnectionTrackingForwardingTest { |
| 34 | + |
| 35 | + private lateinit var harness: ForwardingTestHarness |
| 36 | + |
| 37 | + private val clientIp = "10.0.0.2" |
| 38 | + private val peerIp = "127.0.0.1" |
| 39 | + |
| 40 | + @Before |
| 41 | + fun setUp() { |
| 42 | + harness = ForwardingTestHarness() |
| 43 | + } |
| 44 | + |
| 45 | + @After |
| 46 | + fun tearDown() { |
| 47 | + harness.close() |
| 48 | + } |
| 49 | + |
| 50 | + // --- UDP ----------------------------------------------------------------- |
| 51 | + |
| 52 | + @Test |
| 53 | + fun `concurrent udp connections demultiplex replies back to the originating client`() { |
| 54 | + val peer = DatagramSocket(0, InetAddress.getByName(peerIp)).apply { soTimeout = 3000 } |
| 55 | + val peerPort = peer.localPort |
| 56 | + try { |
| 57 | + // Two client connections to the same peer, distinguished only by source port. |
| 58 | + harness.feed(TestPackets.udpPacket(clientIp, 40001, peerIp, peerPort, "one".toByteArray())) |
| 59 | + harness.feed(TestPackets.udpPacket(clientIp, 40002, peerIp, peerPort, "two".toByteArray())) |
| 60 | + |
| 61 | + // The peer sees two distinct source sockets; echo each payload straight back. |
| 62 | + repeat(2) { |
| 63 | + val rx = DatagramPacket(ByteArray(64), 64) |
| 64 | + peer.receive(rx) |
| 65 | + peer.send(DatagramPacket(rx.data, rx.length, rx.socketAddress)) |
| 66 | + } |
| 67 | + |
| 68 | + // Collect both TUN replies, then assert each landed on the correct client port: |
| 69 | + // "one" must return to 40001 and "two" to 40002 (no cross-talk). |
| 70 | + val byClientPort = buildMap { |
| 71 | + repeat(2) { |
| 72 | + val (_, udp, payload) = harness.parseUdp(harness.awaitTunPacket()) |
| 73 | + put(udp.destinationPort, String(payload)) |
| 74 | + } |
| 75 | + } |
| 76 | + assertThat(byClientPort).containsExactly(40001, "one", 40002, "two") |
| 77 | + } finally { |
| 78 | + peer.close() |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + @Test |
| 83 | + fun `repeated udp datagrams reuse one connection and accumulate egress`() { |
| 84 | + val peer = DatagramSocket(0, InetAddress.getByName(peerIp)).apply { soTimeout = 3000 } |
| 85 | + val peerPort = peer.localPort |
| 86 | + try { |
| 87 | + harness.feed(TestPackets.udpPacket(clientIp, 40003, peerIp, peerPort, "p1".toByteArray())) |
| 88 | + harness.feed(TestPackets.udpPacket(clientIp, 40003, peerIp, peerPort, "p2".toByteArray())) |
| 89 | + |
| 90 | + // Both datagrams reach the peer over the same upstream socket: the second reused |
| 91 | + // the connection rather than opening a new one. |
| 92 | + val first = DatagramPacket(ByteArray(64), 64).also { peer.receive(it) } |
| 93 | + val second = DatagramPacket(ByteArray(64), 64).also { peer.receive(it) } |
| 94 | + assertThat( |
| 95 | + setOf(String(first.data, 0, first.length), String(second.data, 0, second.length)) |
| 96 | + ).containsExactly("p1", "p2") |
| 97 | + assertThat(second.socketAddress).isEqualTo(first.socketAddress) |
| 98 | + |
| 99 | + // Exactly one session/flow was created; egress counters accumulate across both. |
| 100 | + val session = harness.await { |
| 101 | + harness.sessionByKey(udpKey(40003, peerPort)) |
| 102 | + } |
| 103 | + assertThat(harness.flowDao.countNotSyncedFlows()).isEqualTo(1) |
| 104 | + harness.await { session.flow.takeIf { it.packetCountEgress >= 2 } } |
| 105 | + } finally { |
| 106 | + peer.close() |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + @Test |
| 111 | + fun `back-to-back udp datagrams preserve message boundaries`() { |
| 112 | + // Regression test for datagram coalescing: two datagrams sent on the same connection |
| 113 | + // before the writer drains must NOT be merged into one upstream datagram. |
| 114 | + val peer = DatagramSocket(0, InetAddress.getByName(peerIp)).apply { soTimeout = 3000 } |
| 115 | + val peerPort = peer.localPort |
| 116 | + try { |
| 117 | + harness.feed(TestPackets.udpPacket(clientIp, 40004, peerIp, peerPort, "AAAA".toByteArray())) |
| 118 | + harness.feed(TestPackets.udpPacket(clientIp, 40004, peerIp, peerPort, "BBBB".toByteArray())) |
| 119 | + |
| 120 | + // The peer must receive two distinct 4-byte datagrams, not one merged "AAAABBBB". |
| 121 | + val first = DatagramPacket(ByteArray(64), 64).also { peer.receive(it) } |
| 122 | + val second = DatagramPacket(ByteArray(64), 64).also { peer.receive(it) } |
| 123 | + assertThat(first.length).isEqualTo(4) |
| 124 | + assertThat(second.length).isEqualTo(4) |
| 125 | + assertThat(listOf(String(first.data, 0, 4), String(second.data, 0, 4))) |
| 126 | + .containsExactly("AAAA", "BBBB").inOrder() |
| 127 | + } finally { |
| 128 | + peer.close() |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + // --- TCP ----------------------------------------------------------------- |
| 133 | + |
| 134 | + @Test |
| 135 | + fun `concurrent tcp connections are tracked independently`() { |
| 136 | + val server = ServerSocket(0, 50, InetAddress.getByName(peerIp)) |
| 137 | + val peerPort = server.localPort |
| 138 | + val executor = Executors.newFixedThreadPool(2) |
| 139 | + val isn1 = 1000L |
| 140 | + val isn2 = 5000L |
| 141 | + try { |
| 142 | + val accept1 = executor.submit<Socket> { server.accept() } |
| 143 | + val accept2 = executor.submit<Socket> { server.accept() } |
| 144 | + |
| 145 | + harness.feed(syn(40001, peerPort, isn1)) |
| 146 | + harness.feed(syn(40002, peerPort, isn2)) |
| 147 | + |
| 148 | + // Each SYN-ACK must be demultiplexed to its own client port and acknowledge that |
| 149 | + // connection's ISN — proving the two handshakes are not conflated. |
| 150 | + val synAcks = buildMap<Int, Pair<IPHeader, TCPHeader>> { |
| 151 | + repeat(2) { |
| 152 | + val pkt = harness.awaitTunPacketMatching { |
| 153 | + val (_, tcp) = harness.parseTcp(it); tcp.isSYN && tcp.isACK |
| 154 | + } |
| 155 | + val parsed = harness.parseTcp(pkt) |
| 156 | + put(parsed.second.destinationPort, parsed) |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + assertThat(synAcks.keys).containsExactly(40001, 40002) |
| 161 | + assertThat(synAcks.getValue(40001).second.ackNumber).isEqualTo(isn1 + 1) |
| 162 | + assertThat(synAcks.getValue(40002).second.ackNumber).isEqualTo(isn2 + 1) |
| 163 | + assertThat(synAcks.getValue(40001).first.destinationIP.toString()).isEqualTo(clientIp) |
| 164 | + assertThat(synAcks.getValue(40002).first.destinationIP.toString()).isEqualTo(clientIp) |
| 165 | + |
| 166 | + // Two distinct sessions and two recorded flows. |
| 167 | + assertThat(harness.sessionByKey(tcpKey(40001, peerPort))).isNotNull() |
| 168 | + assertThat(harness.sessionByKey(tcpKey(40002, peerPort))).isNotNull() |
| 169 | + assertThat(harness.flowDao.countNotSyncedFlows()).isEqualTo(2) |
| 170 | + |
| 171 | + accept1.get(3, TimeUnit.SECONDS) |
| 172 | + accept2.get(3, TimeUnit.SECONDS) |
| 173 | + } finally { |
| 174 | + executor.shutdownNow() |
| 175 | + server.close() |
| 176 | + } |
| 177 | + } |
| 178 | + |
| 179 | + @Test |
| 180 | + fun `tcp data for an unknown connection is rejected with RST and creates no session`() { |
| 181 | + // An ACK with payload but no preceding SYN has no tracked connection: the engine must |
| 182 | + // reject it with a RST rather than silently adopting it or crashing. |
| 183 | + harness.feed( |
| 184 | + TestPackets.tcpPacket( |
| 185 | + clientIp, 40009, peerIp, 9, |
| 186 | + seq = 42, ack = 99, flags = TestPackets.ACK, payload = "junk".toByteArray(), |
| 187 | + ) |
| 188 | + ) |
| 189 | + |
| 190 | + val rst = harness.awaitTunPacketMatching { harness.parseTcp(it).second.isRST } |
| 191 | + val (ip, tcp) = harness.parseTcp(rst) |
| 192 | + assertThat(tcp.isRST).isTrue() |
| 193 | + assertThat(ip.destinationIP.toString()).isEqualTo(clientIp) |
| 194 | + assertThat(tcp.destinationPort).isEqualTo(40009) |
| 195 | + |
| 196 | + assertThat(harness.sessionByKey(tcpKey(40009, 9))).isNull() |
| 197 | + assertThat(harness.flowDao.countNotSyncedFlows()).isEqualTo(0) |
| 198 | + } |
| 199 | + |
| 200 | + // --- helpers ------------------------------------------------------------- |
| 201 | + |
| 202 | + private fun syn(clientPort: Int, peerPort: Int, isn: Long): ByteArray = |
| 203 | + TestPackets.tcpPacket( |
| 204 | + clientIp, clientPort, peerIp, peerPort, |
| 205 | + seq = isn, ack = 0, flags = TestPackets.SYN, mss = 1460, |
| 206 | + ) |
| 207 | + |
| 208 | + private fun udpKey(clientPort: Int, peerPort: Int): String = Session.getSessionKey( |
| 209 | + SessionProtocol.UDP, |
| 210 | + IPAddress(TestPackets.ip(peerIp)), peerPort, |
| 211 | + IPAddress(TestPackets.ip(clientIp)), clientPort, |
| 212 | + ) |
| 213 | + |
| 214 | + private fun tcpKey(clientPort: Int, peerPort: Int): String = Session.getSessionKey( |
| 215 | + SessionProtocol.TCP, |
| 216 | + IPAddress(TestPackets.ip(peerIp)), peerPort, |
| 217 | + IPAddress(TestPackets.ip(clientIp)), clientPort, |
| 218 | + ) |
| 219 | +} |
0 commit comments