Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / communicate-dev / src / test / java / com / vayunmathur / communicate / rcs / RcsMsrpLoopbackTest.kt
RcsMsrpLoopbackTest.kt126 lines · 4.6 KiBRaw
| 1 | package com.vayunmathur.communicate.rcs |
| 2 | |
| 3 | import com.vayunmathur.communicate.data.rcs.RcsMsrpFraming |
| 4 | import java.net.ServerSocket |
| 5 | import java.util.concurrent.CountDownLatch |
| 6 | import java.util.concurrent.TimeUnit |
| 7 | import java.util.concurrent.atomic.AtomicReference |
| 8 | import kotlin.test.Test |
| 9 | import kotlin.test.assertEquals |
| 10 | import kotlin.test.assertTrue |
| 11 | |
| 12 | /** |
| 13 | * MSRP loopback integration test (§8.4): a real `ServerSocket` on loopback |
| 14 | * plays the peer — accepts one TCP connection, reads one SEND, answers 200 — |
| 15 | * while the framing layer builds the chunk. No Android framework, no IMS PDN: |
| 16 | * exercises wire serialization + response matching end-to-end. |
| 17 | * |
| 18 | * Plaintext only; TLS-in-test needs cert generation + handshake orchestration |
| 19 | * that belongs in a Robolectric harness (see plan §8.4). |
| 20 | */ |
| 21 | class RcsMsrpLoopbackTest { |
| 22 | @Test |
| 23 | fun sendAckRoundTrip() { |
| 24 | val server = ServerSocket(0) |
| 25 | val port = server.localPort |
| 26 | val received = AtomicReference<String>() |
| 27 | val done = CountDownLatch(1) |
| 28 | val peer = startPeer(server, received, done) |
| 29 | peer.isDaemon = true |
| 30 | peer.start() |
| 31 | |
| 32 | // Client side: build one chunk via the framing layer, write it over a |
| 33 | // plain socket (loopback stands in for the IMS PDN here), await 200. |
| 34 | val chunks = RcsMsrpFraming.splitSend( |
| 35 | toPath = "msrp://127.0.0.1:$port/peer;tcp", |
| 36 | fromPath = "msrp://127.0.0.1:$port/self;tcp", |
| 37 | payload = "hello loopback".toByteArray(Charsets.UTF_8), |
| 38 | ) |
| 39 | assertEquals(1, chunks.size) |
| 40 | val chunk = chunks.single() |
| 41 | val acked = sendChunkAndAwaitAck(port, chunk) |
| 42 | assertTrue(done.await(15, TimeUnit.SECONDS), "peer thread finished") |
| 43 | val got = received.get() |
| 44 | assertTrue(got.contains("Byte-Range: 1-14/14"), "peer saw full range: $got") |
| 45 | assertTrue(acked, "client saw 200 OK") |
| 46 | } |
| 47 | |
| 48 | /** Loopback peer: read one SEND, reply 200 OK. */ |
| 49 | private fun startPeer( |
| 50 | server: ServerSocket, |
| 51 | received: AtomicReference<String>, |
| 52 | done: CountDownLatch, |
| 53 | ): Thread { |
| 54 | return Thread { |
| 55 | runCatching { |
| 56 | server.use { srv -> |
| 57 | srv.soTimeout = 15_000 |
| 58 | val socket = srv.accept() |
| 59 | socket.use { s -> |
| 60 | servePeer(s, received) |
| 61 | } |
| 62 | } |
| 63 | }.onFailure { received.set("ERROR: $it") } |
| 64 | done.countDown() |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | /** Read the SEND head + body, then reply 200 OK. */ |
| 69 | private fun servePeer(s: java.net.Socket, received: AtomicReference<String>) { |
| 70 | s.soTimeout = 15_000 |
| 71 | val input = s.getInputStream().bufferedReader(Charsets.UTF_8) |
| 72 | val headLines = mutableListOf<String>() |
| 73 | var line = input.readLine() |
| 74 | var guard = 0 |
| 75 | while (line != null && line.isNotEmpty() && guard++ < 32) { |
| 76 | headLines.add(line) |
| 77 | line = input.readLine() |
| 78 | } |
| 79 | val txid = headLines.firstOrNull() |
| 80 | ?.split(" ")?.getOrNull(1).orEmpty() |
| 81 | // Read body through the end marker. |
| 82 | val body = StringBuilder() |
| 83 | while (true) { |
| 84 | val l = input.readLine() |
| 85 | if (l == null || l.startsWith("-------$txid")) break |
| 86 | body.append(l).append("\r\n") |
| 87 | } |
| 88 | received.set(headLines.joinToString("\r\n") + "\n" + body.toString()) |
| 89 | val out = s.getOutputStream() |
| 90 | out.write( |
| 91 | RcsMsrpFraming.buildErrorResponse( |
| 92 | toPath = "", |
| 93 | txid = txid, |
| 94 | code = 200, |
| 95 | reason = "OK", |
| 96 | ), |
| 97 | ) |
| 98 | out.flush() |
| 99 | } |
| 100 | |
| 101 | /** Send one chunk and await the 200 OK. */ |
| 102 | private fun sendChunkAndAwaitAck(port: Int, chunk: RcsMsrpFraming.OutChunk): Boolean { |
| 103 | var acked = false |
| 104 | runCatching { |
| 105 | java.net.Socket().use { s -> |
| 106 | s.connect(java.net.InetSocketAddress("127.0.0.1", port), 10_000) |
| 107 | s.soTimeout = 10_000 |
| 108 | val out = s.getOutputStream() |
| 109 | out.write(RcsMsrpFraming.serializeChunk(chunk)) |
| 110 | out.flush() |
| 111 | val input = s.getInputStream().bufferedReader(Charsets.UTF_8) |
| 112 | var guard = 0 |
| 113 | var done = false |
| 114 | while (guard++ < 16 && !done) { |
| 115 | val reply = input.readLine() ?: break |
| 116 | val parsed = RcsMsrpFraming.parseResponseStart(reply) |
| 117 | if (parsed != null && parsed.first == chunk.txid) { |
| 118 | acked = parsed.second in 200..299 |
| 119 | done = true |
| 120 | } |
| 121 | } |
| 122 | } |
| 123 | } |
| 124 | return acked |
| 125 | } |
| 126 | } |