package com.vayunmathur.communicate.rcs import android.content.Context import androidx.test.core.app.ApplicationProvider import com.vayunmathur.communicate.data.rcs.RcsDirectSip import java.net.ServerSocket import java.util.concurrent.CopyOnWriteArrayList import java.util.concurrent.CountDownLatch import java.util.concurrent.TimeUnit import kotlinx.coroutines.runBlocking import org.junit.runner.RunWith import org.robolectric.RobolectricTestRunner import org.robolectric.annotation.Config import kotlin.test.AfterTest import kotlin.test.Test import kotlin.test.assertFalse import kotlin.test.assertTrue /** * Slice-3 pass 2: direct-SIP AKA registration — 401 challenge, digest * REGISTER built from the ISIM response (shadow TelephonyManager), 200 OK, * deregistration, and a full restart after stop (regression coverage for * the scope-cancellation restart bug). */ @RunWith(RobolectricTestRunner::class) @Config( sdk = [34], shadows = [RcsSlice3Pass2ImsShadow::class, RcsSlice3Pass2TelephonyShadow::class], ) class RcsSlice3Pass2DirectSipAuthTest { private val context: Context get() = ApplicationProvider.getApplicationContext() @AfterTest fun tearDown() { RcsDirectSip.stop() } /** * The fake P-CSCF must listen on 5060 (the shadow advertises it and * production dials that port). Three test classes bind it, and Gradle * workers pull classes dynamically, so any two can listen at once: * SO_REUSEADDR covers TIME_WAIT from earlier registrations, and the * bounded retry waits out another class's active listen (bounded by * that class's method runtime). Without this, 5060 binds flake with * BindException under load (both directions observed). */ private fun pcscfSocket(): ServerSocket { // Attempt-counted, never clock-bounded: Robolectric substitutes // System.currentTimeMillis with its paused scheduler clock, which // can leap past any wall-clock deadline instantly. var attempts = 0 while (true) { try { return ServerSocket().apply { reuseAddress = true bind(java.net.InetSocketAddress(5060)) } } catch (e: java.net.BindException) { if (++attempts >= 600) throw e Thread.sleep(100) } } } private class PcscfScript(val challengeFirst: Boolean) { val requests = CopyOnWriteArrayList() val done = CountDownLatch(1) val error = java.util.concurrent.atomic.AtomicReference() fun serve(server: ServerSocket) { Thread { try { server.accept().use { s -> s.soTimeout = 20_000 val input = s.getInputStream().bufferedReader(Charsets.UTF_8) val out = s.getOutputStream() var first = true var request = readRequest(input) while (request.isNotEmpty()) { requests += request.joinToString("\n") val finished = if (first && challengeFirst) { first = false respond(out, request, 401, "Unauthorized", "WWW-Authenticate: Digest realm=\"carrier.example\", " + "nonce=\"bm9uY2UxMjM0NTY=\", qop=auth, algorithm=MD5\r\n") false } else { respond(out, request, 200, "OK") request.joinToString("\n").contains("expires=0") } request = if (finished) emptyList() else readRequest(input) } } } catch (e: Throwable) { error.set(e.toString()) } done.countDown() }.apply { isDaemon = true; start() } } /** Read one SIP request head; empty at end of stream. */ private fun readRequest(input: java.io.BufferedReader): List { val lines = mutableListOf() var line = input.readLine() ?: return lines while (line.isNotEmpty()) { lines += line line = input.readLine() ?: return lines } return lines } /** Write a SIP response echoing Via branch / Call-ID / CSeq. */ private fun respond( out: java.io.OutputStream, lines: List, code: Int, reason: String, extra: String = "", ) { val branch = lines.firstOrNull { it.startsWith("Via:", true) } ?.substringAfter("branch=")?.substringBefore(";")?.trim().orEmpty() val callId = lines.firstOrNull { it.startsWith("Call-ID:", true) }.orEmpty() val cseq = lines.firstOrNull { it.startsWith("CSeq:", true) } ?.substringAfter(":")?.trim().orEmpty() out.write( ("SIP/2.0 $code $reason\r\nVia: SIP/2.0/TCP 127.0.0.1;branch=$branch\r\n" + "$callId\r\nCSeq: $cseq\r\n$extra" + "Contact: ;expires=600\r\n" + "Expires: 600\r\nContent-Length: 0\r\n\r\n").toByteArray(Charsets.UTF_8), ) out.flush() } } @Test fun akaChallengeRegistrationAndRestart() = runBlocking { // --- First start: 401 -> AKA digest REGISTER -> 200. --- val server1 = pcscfSocket() val script1 = PcscfScript(challengeFirst = true) script1.serve(server1) assertTrue(RcsDirectSip.start(context, 1), "start failed: ${script1.requests}") assertTrue(RcsDirectSip.isRegistered()) RcsDirectSip.stop() assertFalse(RcsDirectSip.isRegistered()) assertTrue(script1.done.await(20, TimeUnit.SECONDS), "script1: ${script1.error.get()}") server1.close() assertEqualsRequests(script1) // --- Restart after stop: the stack must come back (scope fix). --- val server2 = pcscfSocket() val script2 = PcscfScript(challengeFirst = false) script2.serve(server2) assertTrue(RcsDirectSip.start(context, 1), "restart failed: ${script2.requests}") assertTrue(RcsDirectSip.isRegistered()) RcsDirectSip.stop() assertTrue(script2.done.await(20, TimeUnit.SECONDS), "script2: ${script2.error.get()}") server2.close() assertTrue(script2.requests.first().contains("REGISTER sip:127.0.0.1")) assertTrue(script2.requests.last().contains("expires=0")) Unit } private fun assertEqualsRequests(script: PcscfScript) { val all = script.requests assertTrue(all.size >= 3, "requests=$all") assertTrue(all[0].contains("CSeq: 1 REGISTER"), all[0]) val authed = all[1] assertTrue(authed.contains("Authorization: Digest "), authed) assertTrue(authed.contains("username="), authed) assertTrue(authed.contains("nonce=\"bm9uY2UxMjM0NTY=\""), authed) assertTrue(all.last().contains("expires=0"), all.last()) } }