package com.vayunmathur.communicate.rcs import com.vayunmathur.communicate.data.rcs.RcsMsrpFraming import java.net.ServerSocket import java.util.concurrent.CountDownLatch import java.util.concurrent.TimeUnit import java.util.concurrent.atomic.AtomicReference import kotlin.test.Test import kotlin.test.assertEquals import kotlin.test.assertTrue /** * MSRP loopback integration test (§8.4): a real `ServerSocket` on loopback * plays the peer — accepts one TCP connection, reads one SEND, answers 200 — * while the framing layer builds the chunk. No Android framework, no IMS PDN: * exercises wire serialization + response matching end-to-end. * * Plaintext only; TLS-in-test needs cert generation + handshake orchestration * that belongs in a Robolectric harness (see plan §8.4). */ class RcsMsrpLoopbackTest { @Test fun sendAckRoundTrip() { val server = ServerSocket(0) val port = server.localPort val received = AtomicReference() val done = CountDownLatch(1) val peer = startPeer(server, received, done) peer.isDaemon = true peer.start() // Client side: build one chunk via the framing layer, write it over a // plain socket (loopback stands in for the IMS PDN here), await 200. val chunks = RcsMsrpFraming.splitSend( toPath = "msrp://127.0.0.1:$port/peer;tcp", fromPath = "msrp://127.0.0.1:$port/self;tcp", payload = "hello loopback".toByteArray(Charsets.UTF_8), ) assertEquals(1, chunks.size) val chunk = chunks.single() val acked = sendChunkAndAwaitAck(port, chunk) assertTrue(done.await(15, TimeUnit.SECONDS), "peer thread finished") val got = received.get() assertTrue(got.contains("Byte-Range: 1-14/14"), "peer saw full range: $got") assertTrue(acked, "client saw 200 OK") } /** Loopback peer: read one SEND, reply 200 OK. */ private fun startPeer( server: ServerSocket, received: AtomicReference, done: CountDownLatch, ): Thread { return Thread { runCatching { server.use { srv -> srv.soTimeout = 15_000 val socket = srv.accept() socket.use { s -> servePeer(s, received) } } }.onFailure { received.set("ERROR: $it") } done.countDown() } } /** Read the SEND head + body, then reply 200 OK. */ private fun servePeer(s: java.net.Socket, received: AtomicReference) { s.soTimeout = 15_000 val input = s.getInputStream().bufferedReader(Charsets.UTF_8) val headLines = mutableListOf() var line = input.readLine() var guard = 0 while (line != null && line.isNotEmpty() && guard++ < 32) { headLines.add(line) line = input.readLine() } val txid = headLines.firstOrNull() ?.split(" ")?.getOrNull(1).orEmpty() // Read body through the end marker. val body = StringBuilder() while (true) { val l = input.readLine() if (l == null || l.startsWith("-------$txid")) break body.append(l).append("\r\n") } received.set(headLines.joinToString("\r\n") + "\n" + body.toString()) val out = s.getOutputStream() out.write( RcsMsrpFraming.buildErrorResponse( toPath = "", txid = txid, code = 200, reason = "OK", ), ) out.flush() } /** Send one chunk and await the 200 OK. */ private fun sendChunkAndAwaitAck(port: Int, chunk: RcsMsrpFraming.OutChunk): Boolean { var acked = false runCatching { java.net.Socket().use { s -> s.connect(java.net.InetSocketAddress("127.0.0.1", port), 10_000) s.soTimeout = 10_000 val out = s.getOutputStream() out.write(RcsMsrpFraming.serializeChunk(chunk)) out.flush() val input = s.getInputStream().bufferedReader(Charsets.UTF_8) var guard = 0 var done = false while (guard++ < 16 && !done) { val reply = input.readLine() ?: break val parsed = RcsMsrpFraming.parseResponseStart(reply) if (parsed != null && parsed.first == chunk.txid) { acked = parsed.second in 200..299 done = true } } } } return acked } }