Modern Apps git
git clone https://git.maproj.org/Modern-Apps.git
translate ▾

Modern-Apps.git / communicate-dev / src / test / java / com / vayunmathur / communicate / rcs / RcsSlice3SipResponseTest.kt

RcsSlice3SipResponseTest.kt175 lines · 6.8 KiBRaw
1package com.vayunmathur.communicate.rcs
2
3import com.vayunmathur.communicate.data.rcs.RcsImdn
4import com.vayunmathur.communicate.data.rcs.RcsSipResponse
5import com.vayunmathur.communicate.data.rcs.buildImdnBody
6import com.vayunmathur.communicate.data.rcs.ImdnDisposition
7import kotlin.test.Test
8import kotlin.test.assertEquals
9import kotlin.test.assertFalse
10import kotlin.test.assertNull
11import kotlin.test.assertTrue
12import kotlinx.coroutines.flow.first
13import kotlinx.coroutines.test.runTest
14import org.junit.runner.RunWith
15import org.robolectric.RobolectricTestRunner
16
17/**
18 * Slice-3 tests for [RcsSipResponse] (pure SIP response parsing over the
19 * framework `android.telephony.ims.SipMessage`, real under Robolectric) and
20 * [RcsImdn] (receipt tracker fed by parsed IMDN bodies).
21 *
22 * The framework class is absent from the unit-test android.jar stub AND from
23 * the compile-only aosp-stubs (which would shadow Robolectric's instrumented
24 * copy), so messages are constructed reflectively against the runtime class
25 * that Robolectric's android-all provides. The parse entry point takes
26 * `SipMessage`, so reflection builds the arg and the typed call is made via
27 * `RcsSipResponse.parse` through a reflective bridge.
28 */
29@RunWith(RobolectricTestRunner::class)
30class RcsSlice3SipResponseTest {
31
32 /** Construct a framework SipMessage reflectively (runtime class from android-all). */
33 private fun sipMessage(startLine: String, headers: String, body: ByteArray): Any {
34 val cls = Class.forName("android.telephony.ims.SipMessage")
35 val ctor = cls.getConstructor(String::class.java, String::class.java, ByteArray::class.java)
36 return ctor.newInstance(startLine, headers, body)
37 }
38
39 private val parseMethod: java.lang.reflect.Method by lazy {
40 val cls = Class.forName("android.telephony.ims.SipMessage")
41 RcsSipResponse::class.java.methods.first { it.name == "parse" && it.parameterTypes[0] == cls }
42 }
43
44 @Suppress("UNCHECKED_CAST")
45 private fun parse(msg: Any): RcsSipResponse.Parsed? =
46 parseMethod.invoke(RcsSipResponse, msg) as RcsSipResponse.Parsed?
47
48 private fun response(
49 startLine: String = "SIP/2.0 200 OK",
50 branch: String = "z9hG4bKabc123",
51 callId: String = "call-1@rcs",
52 toTag: String? = "remote-tag",
53 fromTag: String? = "local-tag",
54 cseq: String = "1 INVITE",
55 extraHeaders: String = "",
56 body: String = "",
57 ): Any {
58 val headers = buildString {
59 append("Via: SIP/2.0/TCP 10.0.0.1;branch=$branch\r\n")
60 append("To: <sip:peer@rcs>;tag=$toTag\r\n")
61 append("From: <sip:me@rcs>;tag=$fromTag\r\n")
62 append("Call-ID: $callId\r\n")
63 append("CSeq: $cseq\r\n")
64 if (extraHeaders.isNotEmpty()) append(extraHeaders)
65 }
66 return sipMessage(startLine, headers, body.toByteArray(Charsets.UTF_8))
67 }
68
69 @Test
70 fun parsesFullInviteResponse() {
71 val parsed = parse(response()) ?: error("expected parse")
72 assertEquals(200, parsed.statusCode)
73 assertEquals("OK", parsed.reason)
74 assertEquals("z9hG4bKabc123", parsed.branch)
75 assertEquals("call-1@rcs", parsed.callId)
76 assertEquals("remote-tag", parsed.toTag)
77 assertEquals("local-tag", parsed.fromTag)
78 assertEquals("INVITE", parsed.cseqMethod)
79 assertEquals(1L, parsed.cseqNumber)
80 }
81
82 @Test
83 fun parsesRequestAsNull() {
84 val invite = sipMessage(
85 "INVITE sip:peer@rcs SIP/2.0",
86 "Via: SIP/2.0/TCP x;branch=z9hG4bK1\r\nCall-ID: c\r\n",
87 ByteArray(0),
88 )
89 assertNull(parse(invite))
90 }
91
92 @Test
93 fun rejectsNonNumericStatus() {
94 val msg = response(startLine = "SIP/2.0 OKAY whatever")
95 assertNull(parse(msg))
96 }
97
98 @Test
99 fun requiresViaBranchAndCallId() {
100 // The framework constructor itself rejects a Via-less header section,
101 // so only the missing-Call-ID path reaches the parser (null).
102 val noCallId = sipMessage(
103 "SIP/2.0 200 OK",
104 "Via: SIP/2.0/TCP x;branch=z9hG4bK1\r\nCSeq: 1 MESSAGE\r\n",
105 ByteArray(0),
106 )
107 assertNull(parse(noCallId))
108 }
109
110 @Test
111 fun carriesBodyAndHeaders() {
112 val sdp = "v=0\r\nm=message 2855 TCP/MSRP *\r\n"
113 val parsed = parse(response(body = sdp)) ?: error("expected parse")
114 assertEquals(sdp, parsed.body)
115 assertTrue(parsed.headers.contains("Call-ID: call-1@rcs"))
116 }
117
118 @Test
119 fun parsesProvisionalAndFailureCodes() {
120 assertEquals(180, parse(response(startLine = "SIP/2.0 180 Ringing"))?.statusCode)
121 assertEquals(401, parse(response(startLine = "SIP/2.0 401 Unauthorized"))?.statusCode)
122 assertEquals(481, parse(response(startLine = "SIP/2.0 481 Call Leg Does Not Exist"))?.statusCode)
123 }
124
125 @Test
126 fun cseqMethodAndNumberForBye() {
127 val parsed = parse(response(cseq = "42 BYE")) ?: error("expected parse")
128 assertEquals("BYE", parsed.cseqMethod)
129 assertEquals(42L, parsed.cseqNumber)
130 }
131
132 @Test
133 fun missingTagsAreNull() {
134 val msg = sipMessage(
135 "SIP/2.0 200 OK",
136 "Via: SIP/2.0/TCP x;branch=z9hG4bK1\r\n" +
137 "To: <sip:peer@rcs>\r\n" +
138 "From: <sip:me@rcs>\r\n" +
139 "Call-ID: c@rcs\r\n" +
140 "CSeq: 1 MESSAGE\r\n",
141 ByteArray(0),
142 )
143 val parsed = parse(msg) ?: error("expected parse")
144 assertNull(parsed.toTag)
145 assertNull(parsed.fromTag)
146 }
147
148 @Test
149 fun isResponseDetectsStartLines() {
150 assertTrue(RcsSipResponse.isResponse("SIP/2.0 200 OK"))
151 assertTrue(RcsSipResponse.isResponse(" sip/2.0 404 Not Found"))
152 assertFalse(RcsSipResponse.isResponse("INVITE sip:peer@rcs SIP/2.0"))
153 assertFalse(RcsSipResponse.isResponse("MESSAGE sip:peer@rcs SIP/2.0"))
154 }
155
156 @Test
157 fun imdnReportsRoundTripThroughTracker() = runTest {
158 val body = buildImdnBody("slice3-msg-1", ImdnDisposition.Delivered)
159 RcsImdn.onReportReceived(body)
160 assertEquals(ImdnDisposition.Delivered, RcsImdn.statusFor("slice3-msg-1"))
161 assertEquals(ImdnDisposition.Delivered, RcsImdn.reports.first()["slice3-msg-1"])
162 }
163
164 @Test
165 fun imdnTrackerUpdatesAndIgnoresGarbage() = runTest {
166 RcsImdn.onReportReceived(buildImdnBody("slice3-msg-2", ImdnDisposition.Displayed))
167 assertEquals(ImdnDisposition.Displayed, RcsImdn.statusFor("slice3-msg-2"))
168 // Latest disposition wins.
169 RcsImdn.onReportReceived(buildImdnBody("slice3-msg-2", ImdnDisposition.Failed))
170 assertEquals(ImdnDisposition.Failed, RcsImdn.statusFor("slice3-msg-2"))
171 // Garbage is ignored, unknown ids are null.
172 RcsImdn.onReportReceived("not an imdn body")
173 assertNull(RcsImdn.statusFor("slice3-unknown-id"))
174 }
175}