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

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

RcsSlice3SessionManagerTest.kt355 lines · 15.3 KiBRaw
1package com.vayunmathur.communicate.rcs
2
3// FIXTURE RELATIVE-TIME EXCEPTION: timestamps are seeded relative to the wall clock the code under test reads internally, so the fresh/expired/relative-time branch under test keeps its meaning; a fixed past instant would silently change that branch.
4
5import com.vayunmathur.communicate.data.rcs.RcsSessionManager
6import com.vayunmathur.communicate.data.rcs.RcsSession
7import com.vayunmathur.communicate.data.rcs.buildInvite
8import com.vayunmathur.communicate.data.rcs.failSession
9import com.vayunmathur.communicate.data.rcs.withScheme
10import java.util.UUID
11import kotlin.test.Test
12import kotlin.test.assertEquals
13import kotlin.test.assertFalse
14import kotlin.test.assertNotNull
15import kotlin.test.assertNull
16import kotlin.test.assertTrue
17import kotlinx.coroutines.test.runTest
18import org.junit.runner.RunWith
19import org.robolectric.RobolectricTestRunner
20import org.robolectric.annotation.Config
21
22/**
23 * Slice-3 tests for [RcsSessionManager] dialog routing: inbound INVITE /
24 * MESSAGE / BYE / ACK handling, session establishment + teardown, backoff,
25 * sweeps, re-INVITEs and SDP answers. Transport sends fail closed (no
26 * delegate), so these assert pure state-machine behavior: sends return
27 * false/null while the session map still transitions correctly.
28 */
29@RunWith(RobolectricTestRunner::class)
30@Config(sdk = [34])
31class RcsSlice3SessionManagerTest {
32
33 private fun offerSdp(setup: String = "passive"): String =
34 "v=0\r\no=- 1 1 IN IP4 10.0.0.9\r\ns=-\r\nc=IN IP4 10.0.0.9\r\nt=0 0\r\n" +
35 "m=message 2855 TCP/MSRP *\r\na=path:msrp://10.0.0.9:2855/sess-9;tcp\r\na=setup:$setup\r\n"
36
37 @Test
38 fun inboundInviteCreatesIncomingSession() {
39 val callId = "slice3-in-${UUID.randomUUID()}"
40 val session = RcsSessionManager.onSipRequest(
41 method = "INVITE",
42 callId = callId,
43 fromUri = "sip:+15550001111@rcs",
44 contentType = "application/sdp",
45 body = offerSdp(),
46 fromTag = "peer-tag-1",
47 )
48 assertNotNull(session)
49 assertEquals(callId, session.callId)
50 assertEquals("peer-tag-1", session.remoteTag)
51 assertEquals("+15550001111", session.conversationId)
52 assertTrue(RcsSessionManager.isKnownDialog(callId))
53 // Cleanup.
54 RcsSessionManager.onSipRequest("BYE", callId, "sip:+15550001111@rcs", null, "")
55 assertFalse(RcsSessionManager.isKnownDialog(callId))
56 }
57
58 @Test
59 fun inviteConversationIdFallsBackToFullUri() {
60 val callId = "slice3-in-${UUID.randomUUID()}"
61 val session = RcsSessionManager.onSipRequest(
62 "INVITE", callId, "weird-uri-without-sip", "application/sdp", offerSdp(), "t1",
63 )
64 assertNotNull(session)
65 assertEquals("weird-uri-without-sip", session.conversationId)
66 RcsSessionManager.onSipRequest("BYE", callId, "weird-uri-without-sip", null, "")
67 }
68
69 @Test
70 fun inboundMessageImdnAndTyping() {
71 val imdnBody = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
72 "<imdn xmlns=\"urn:ietf:params:xml:ns:imdn\">\n" +
73 " <message-id>slice3-report-1</message-id>\n" +
74 " <status><positive-delivery/></status>\n" +
75 "</imdn>"
76 // IMDN report routes to the tracker without crashing.
77 assertNull(
78 RcsSessionManager.onSipRequest(
79 "MESSAGE", "c1", "sip:+15550002222@rcs", "message/imdn+xml", imdnBody,
80 ),
81 )
82 assertEquals(
83 com.vayunmathur.communicate.data.rcs.ImdnDisposition.Delivered,
84 com.vayunmathur.communicate.data.rcs.RcsImdn.statusFor("slice3-report-1"),
85 )
86 // Typing active then idle.
87 RcsSessionManager.onSipRequest(
88 "MESSAGE", "c2", "sip:+15550003333@rcs", "application/im-iscomposing+xml",
89 "<?xml version=\"1.0\"?><isComposing xmlns=\"urn:ietf:params:xml:ns:im-composing\">" +
90 "<state>active</state></isComposing>",
91 )
92 assertEquals(true, RcsSessionManager.typing.value["+15550003333"])
93 RcsSessionManager.onSipRequest(
94 "MESSAGE", "c3", "sip:+15550003333@rcs", null,
95 "<?xml version=\"1.0\"?><isComposing xmlns=\"urn:ietf:params:xml:ns:im-composing\">" +
96 "<state>idle</state></isComposing>",
97 )
98 assertEquals(false, RcsSessionManager.typing.value["+15550003333"])
99 }
100
101 @Test
102 fun ackConfirmsIncomingDialog() {
103 val callId = "slice3-ack-${UUID.randomUUID()}"
104 RcsSessionManager.onSipRequest("INVITE", callId, "sip:+15550004444@rcs", "application/sdp", offerSdp(), "t9")
105 val before = RcsSessionManager.sessionFor("+15550004444")
106 assertNotNull(before)
107 assertFalse(before.confirmed)
108 RcsSessionManager.onSipRequest("ACK", callId, "sip:+15550004444@rcs", null, "")
109 assertTrue(RcsSessionManager.sessionFor("+15550004444")?.confirmed == true)
110 RcsSessionManager.onSipRequest("BYE", callId, "sip:+15550004444@rcs", null, "")
111 }
112
113 @Test
114 fun unknownMethodReturnsNull() {
115 assertNull(RcsSessionManager.onSipRequest("OPTIONS", "c", "sip:x@rcs", null, ""))
116 assertNull(RcsSessionManager.onSipRequest("CANCEL", "c", "sip:x@rcs", null, ""))
117 }
118
119 @Test
120 fun sipResponseEstablishesAndAcks() = runTest {
121 val callId = "slice3-est-${UUID.randomUUID()}"
122 // Seed a pending outgoing session directly in the map.
123 RcsSessionManager.sessionsMutable.value = RcsSessionManager.sessionsMutable.value +
124 ("slice3-conv-est" to RcsSession(
125 dialogId = "$callId:lt",
126 callId = callId,
127 localTag = "lt",
128 remoteTag = "",
129 remoteUri = "sip:+15550005555@rcs",
130 conversationId = "slice3-conv-est",
131 ))
132 // 200 OK with a passive answer: dialog completes (ACK send fails
133 // closed without transport, but the dialog still confirms).
134 RcsSessionManager.onSipResponse(
135 statusCode = 200,
136 callId = callId,
137 remoteTag = "peer-tag-est",
138 sdpAnswer = offerSdp("passive"),
139 responseHeaders = "Contact: <sip:[email protected]>;expires=600\r\n",
140 )
141 val established = RcsSessionManager.sessionFor("slice3-conv-est")
142 assertNotNull(established)
143 assertEquals("peer-tag-est", established.remoteTag)
144 assertTrue(established.confirmed)
145 assertEquals("msrp://10.0.0.9:2855/sess-9;tcp", established.msrpRemotePath)
146 // Teardown of a confirmed dialog (BYE send fails closed, map still drops).
147 RcsSessionManager.terminateSession("slice3-conv-est")
148 assertNull(RcsSessionManager.sessionFor("slice3-conv-est"))
149 }
150
151 @Test
152 fun sipResponseFailureTearsDown() = runTest {
153 val callId = "slice3-fail-${UUID.randomUUID()}"
154 RcsSessionManager.sessionsMutable.value = RcsSessionManager.sessionsMutable.value +
155 ("slice3-conv-fail" to RcsSession(
156 dialogId = "$callId:lt",
157 callId = callId,
158 localTag = "lt",
159 remoteTag = "",
160 remoteUri = "sip:+15550006666@rcs",
161 conversationId = "slice3-conv-fail",
162 ))
163 RcsSessionManager.onSipResponse(486, callId, null, null)
164 assertNull(RcsSessionManager.sessionFor("slice3-conv-fail"))
165 }
166
167 @Test
168 fun dialogErrorBacksOffAndRecovers() {
169 val callId = "slice3-bo-${UUID.randomUUID()}"
170 RcsSessionManager.sessionsMutable.value = RcsSessionManager.sessionsMutable.value +
171 ("slice3-conv-bo" to RcsSession(
172 dialogId = "$callId:lt",
173 callId = callId,
174 localTag = "lt",
175 remoteTag = "rt",
176 remoteUri = "sip:+15550007777@rcs",
177 conversationId = "slice3-conv-bo",
178 ))
179 assertFalse(RcsSessionManager.inDialogBackoff("slice3-conv-bo"))
180 RcsSessionManager.onDialogError(callId, 481)
181 assertNull(RcsSessionManager.sessionFor("slice3-conv-bo"))
182 assertTrue(RcsSessionManager.inDialogBackoff("slice3-conv-bo"))
183 // Unknown call ids are safe no-ops.
184 RcsSessionManager.onDialogError("no-such-call", 408)
185 assertFalse(RcsSessionManager.inDialogBackoff("slice3-conv-unknown"))
186 }
187
188 @Test
189 fun sweepDropsStaleIncompleteSessions() {
190 val old = System.currentTimeMillis() - 60 * 60 * 1000L
191 RcsSessionManager.sessionsMutable.value = RcsSessionManager.sessionsMutable.value +
192 ("slice3-conv-stale" to RcsSession(
193 dialogId = "stale:lt",
194 callId = "slice3-stale-call",
195 localTag = "lt",
196 remoteTag = "",
197 remoteUri = "sip:+15550008888@rcs",
198 conversationId = "slice3-conv-stale",
199 createdAt = old,
200 ))
201 val dropped = RcsSessionManager.sweepStaleSessions()
202 assertTrue(dropped.contains("slice3-conv-stale"))
203 assertNull(RcsSessionManager.sessionFor("slice3-conv-stale"))
204 }
205
206 @Test
207 fun sweepKeepsFreshSessions() {
208 RcsSessionManager.sessionsMutable.value = RcsSessionManager.sessionsMutable.value +
209 ("slice3-conv-fresh" to RcsSession(
210 dialogId = "fresh:lt",
211 callId = "slice3-fresh-call",
212 localTag = "lt",
213 remoteTag = "",
214 remoteUri = "sip:+15550009999@rcs",
215 conversationId = "slice3-conv-fresh",
216 ))
217 val dropped = RcsSessionManager.sweepStaleSessions()
218 assertFalse(dropped.contains("slice3-conv-fresh"))
219 // Cleanup.
220 RcsSessionManager.onSipRequest("BYE", "slice3-fresh-call", "sip:+15550009999@rcs", null, "")
221 }
222
223 @Test
224 fun reInviteUpdatesDialog() {
225 val callId = "slice3-reinv-${UUID.randomUUID()}"
226 RcsSessionManager.sessionsMutable.value = RcsSessionManager.sessionsMutable.value +
227 ("slice3-conv-reinv" to RcsSession(
228 dialogId = "$callId:lt",
229 callId = callId,
230 localTag = "lt",
231 remoteTag = "rt",
232 remoteUri = "sip:+15550001010@rcs",
233 conversationId = "slice3-conv-reinv",
234 confirmed = true,
235 ))
236 RcsSessionManager.onReInvite(callId, offerSdp("active"))
237 val updated = RcsSessionManager.sessionFor("slice3-conv-reinv")
238 assertNotNull(updated)
239 assertEquals("msrp://10.0.0.9:2855/sess-9;tcp", updated.msrpRemotePath)
240 assertEquals(com.vayunmathur.communicate.data.rcs.MsrpSetup.ACTIVE, updated.msrpSetup)
241 assertFalse(updated.msrpSecure)
242 // Unknown re-INVITE is a no-op.
243 RcsSessionManager.onReInvite("no-such-call", offerSdp())
244 RcsSessionManager.onSipRequest("BYE", callId, "sip:+15550001010@rcs", null, "")
245 }
246
247 @Test
248 fun currentSdpRoundTripsBoundPath() {
249 val callId = "slice3-csdp-${UUID.randomUUID()}"
250 assertNull(RcsSessionManager.currentSdpFor("slice3-conv-nosession"))
251 RcsSessionManager.sessionsMutable.value = RcsSessionManager.sessionsMutable.value +
252 ("slice3-conv-csdp" to RcsSession(
253 dialogId = "$callId:lt",
254 callId = callId,
255 localTag = "lt",
256 remoteTag = "rt",
257 remoteUri = "sip:+15550001110@rcs",
258 conversationId = "slice3-conv-csdp",
259 confirmed = true,
260 msrpLocalPath = "msrp://10.0.0.1:2855/sess-local;tcp",
261 msrpSetup = com.vayunmathur.communicate.data.rcs.MsrpSetup.ACTIVE,
262 ))
263 val sdpBytes = RcsSessionManager.currentSdpFor("slice3-conv-csdp")
264 assertNotNull(sdpBytes)
265 val sdp = sdpBytes.toString(Charsets.UTF_8)
266 assertTrue(sdp.contains("a=path:msrp://10.0.0.1:2855/sess-local;tcp"))
267 assertTrue(sdp.contains("a=setup:passive"))
268 RcsSessionManager.onSipRequest("BYE", callId, "sip:+15550001110@rcs", null, "")
269 }
270
271 @Test
272 fun startSessionFailsClosedWithoutTransport() = runTest {
273 assertNull(RcsSessionManager.startSession("tel:+15550001212", "slice3-conv-noleg"))
274 assertNull(RcsSessionManager.startGroupSession("conf:[email protected]", "subj", "slice3-conv-noleg2"))
275 }
276
277 @Test
278 fun sendInDialogFailsClosedWithoutConfirmedSession() = runTest {
279 assertFalse(
280 RcsSessionManager.sendInDialogMessage("slice3-conv-nothing", "hi".toByteArray()),
281 )
282 // Unconfirmed session also fails closed.
283 RcsSessionManager.sessionsMutable.value = RcsSessionManager.sessionsMutable.value +
284 ("slice3-conv-unc" to RcsSession(
285 dialogId = "unc:lt",
286 callId = "slice3-unc-call",
287 localTag = "lt",
288 remoteTag = "rt",
289 remoteUri = "sip:+15550001313@rcs",
290 conversationId = "slice3-conv-unc",
291 confirmed = false,
292 ))
293 assertFalse(RcsSessionManager.sendInDialogMessage("slice3-conv-unc", "hi".toByteArray()))
294 RcsSessionManager.onSipRequest("BYE", "slice3-unc-call", "sip:+15550001313@rcs", null, "")
295 }
296
297 @Test
298 fun terminateUnknownSessionIsTrue() = runTest {
299 assertTrue(RcsSessionManager.terminateSession("slice3-conv-never-existed"))
300 }
301
302 @Test
303 fun failSessionDropsMapEntry() {
304 val session = RcsSession(
305 dialogId = "fs:lt",
306 callId = "slice3-fs-call",
307 localTag = "lt",
308 remoteTag = "",
309 remoteUri = "sip:+15550001414@rcs",
310 conversationId = "slice3-conv-fs",
311 )
312 RcsSessionManager.sessionsMutable.value = RcsSessionManager.sessionsMutable.value +
313 ("slice3-conv-fs" to session)
314 RcsSessionManager.failSession("slice3-conv-fs", session, "slice3-fs-call", 500)
315 assertNull(RcsSessionManager.sessionFor("slice3-conv-fs"))
316 }
317
318 @Test
319 fun inviteBuilderCarriesConversationHeaders() {
320 val (startLine, headers, content) = RcsSessionManager.buildInvite(
321 targetUri = "sip:+15550001515@rcs",
322 callId = "slice3-inv-call",
323 localTag = "ltag1234",
324 branch = "z9hG4bKslice3",
325 conversationId = "slice3-conv-inv",
326 subject = "hello group",
327 )
328 assertEquals("INVITE sip:+15550001515@rcs SIP/2.0", startLine)
329 assertTrue(headers.contains("Call-ID: slice3-inv-call\r\n"))
330 assertTrue(headers.contains("Conversation-ID: slice3-conv-inv\r\n"))
331 assertTrue(headers.contains("Subject: hello group\r\n"))
332 val body = content.toString(Charsets.UTF_8)
333 assertTrue(body.contains("m=message "))
334 assertTrue(body.contains("a=setup:active"))
335 }
336
337 @Test
338 fun contactHeaderCarriesInstanceAndCpmTag() {
339 val header = RcsSessionManager.contactHeader("sip:me@rcs", "12345")
340 assertTrue(header.startsWith("Contact: <sip:me@rcs>"))
341 assertTrue(header.contains("urn:gsma:imei:12345"))
342 assertTrue(header.contains("cpm.session"))
343 val unknown = RcsSessionManager.contactHeader("sip:me@rcs", null)
344 assertTrue(unknown.contains("urn:gsma:imei:unknown"))
345 }
346
347 @Test
348 fun withSchemeRewritesMsrpScheme() {
349 assertEquals(
350 "msrps://10.0.0.1:2855/x;tcp",
351 "msrp://10.0.0.1:2855/x;tcp".withScheme("msrps"),
352 )
353 assertEquals("noscheme", "noscheme".withScheme("msrps"))
354 }
355}