Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / communicate-dev / src / test / java / com / vayunmathur / communicate / rcs / RcsSlice3Pass2SessionTest.kt
RcsSlice3Pass2SessionTest.kt318 lines · 14.8 KiBRaw
| 1 | package com.vayunmathur.communicate.rcs |
| 2 | |
| 3 | import com.vayunmathur.communicate.data.rcs.MsrpSetup |
| 4 | import com.vayunmathur.communicate.data.rcs.RcsSession |
| 5 | import com.vayunmathur.communicate.data.rcs.RcsSessionManager |
| 6 | import com.vayunmathur.communicate.data.rcs.buildSdpAnswer |
| 7 | import com.vayunmathur.communicate.data.rcs.buildSdpOffer |
| 8 | import com.vayunmathur.communicate.data.rcs.hostedFocusFor |
| 9 | import com.vayunmathur.communicate.data.rcs.noteFocusJoin |
| 10 | import com.vayunmathur.communicate.data.rcs.noteFocusLeave |
| 11 | import com.vayunmathur.communicate.data.rcs.destroyHostedFocus |
| 12 | import com.vayunmathur.communicate.data.rcs.dropFocusSubscriber |
| 13 | import com.vayunmathur.communicate.data.rcs.focusMembers |
| 14 | import com.vayunmathur.communicate.data.rcs.hostGroupFocus |
| 15 | import com.vayunmathur.communicate.data.rcs.inviteFocusMember |
| 16 | import com.vayunmathur.communicate.data.rcs.noteFocusSubscriber |
| 17 | import com.vayunmathur.communicate.data.rcs.onConferenceNotify |
| 18 | import com.vayunmathur.communicate.data.rcs.publishConferenceInfo |
| 19 | import com.vayunmathur.communicate.data.rcs.refreshEventSubscriptions |
| 20 | import com.vayunmathur.communicate.data.rcs.removeFocusMember |
| 21 | import com.vayunmathur.communicate.data.rcs.subscribeConferenceEvents |
| 22 | import java.util.UUID |
| 23 | import kotlinx.coroutines.test.runTest |
| 24 | import org.junit.runner.RunWith |
| 25 | import org.robolectric.RobolectricTestRunner |
| 26 | import kotlin.test.Test |
| 27 | import kotlin.test.assertEquals |
| 28 | import kotlin.test.assertFalse |
| 29 | import kotlin.test.assertNotNull |
| 30 | import kotlin.test.assertNull |
| 31 | import kotlin.test.assertTrue |
| 32 | |
| 33 | /** |
| 34 | * Slice-3 pass 2: session-manager edges not reached in pass 1 — SDP |
| 35 | * offer/answer matrices, re-INVITE application, current-SDP emission, |
| 36 | * error backoff, focus bookkeeping on a hosted session, and the |
| 37 | * transport-gated entry points. |
| 38 | */ |
| 39 | @RunWith(RobolectricTestRunner::class) |
| 40 | class RcsSlice3Pass2SessionTest { |
| 41 | |
| 42 | private fun seedSession( |
| 43 | conversationId: String, |
| 44 | callId: String = "pass2-${UUID.randomUUID()}", |
| 45 | session: RcsSession = RcsSession( |
| 46 | dialogId = "$callId:d", callId = callId, localTag = "lt", remoteTag = "rt", |
| 47 | remoteUri = "sip:peer@rcs", conversationId = conversationId, |
| 48 | ), |
| 49 | ): RcsSession { |
| 50 | RcsSessionManager.sessionsMutable.value = |
| 51 | RcsSessionManager.sessionsMutable.value + (conversationId to session) |
| 52 | return session |
| 53 | } |
| 54 | |
| 55 | private fun drop(conversationId: String) { |
| 56 | RcsSessionManager.sessionsMutable.value = |
| 57 | RcsSessionManager.sessionsMutable.value - conversationId |
| 58 | } |
| 59 | |
| 60 | @Test |
| 61 | fun sdpOfferAnswerMatrices() { |
| 62 | // Plain offer without a listen path: active connect-out, placeholder host. |
| 63 | val plain = with(RcsSessionManager) { buildSdpOffer(null, null) } |
| 64 | assertTrue(plain.contains("m=message 2855 TCP/MSRP *")) |
| 65 | assertTrue(plain.contains("a=setup:active")) |
| 66 | assertTrue(plain.contains("a=path:msrp://")) |
| 67 | assertFalse(plain.contains("a=fingerprint:")) |
| 68 | // With a listen path: actpass + our path, explicit IP. |
| 69 | val listened = with(RcsSessionManager) { |
| 70 | buildSdpOffer("10.0.0.5", "msrp://10.0.0.5:2855/sess-1;tcp") |
| 71 | } |
| 72 | assertTrue(listened.contains("c=IN IP4 10.0.0.5")) |
| 73 | assertTrue(listened.contains("a=setup:actpass")) |
| 74 | assertTrue(listened.contains("a=path:msrp://10.0.0.5:2855/sess-1;tcp")) |
| 75 | // TLS offer: TLS proto, msrps path rewrite, fingerprint line. |
| 76 | val secure = with(RcsSessionManager) { |
| 77 | buildSdpOffer("10.0.0.5", "msrp://10.0.0.5:2855/sess-1;tcp", "AA:BB:CC") |
| 78 | } |
| 79 | assertTrue(secure.contains("TCP/TLS/MSRP")) |
| 80 | assertTrue(secure.contains("a=path:msrps://10.0.0.5:2855/sess-1;tcp")) |
| 81 | assertTrue(secure.contains("a=fingerprint:SHA-256 AA:BB:CC")) |
| 82 | // Blank IP falls back to 0.0.0.0. |
| 83 | val blankIp = with(RcsSessionManager) { buildSdpOffer(" ", null) } |
| 84 | assertTrue(blankIp.contains("c=IN IP4 0.0.0.0")) |
| 85 | |
| 86 | // Answers mirror the offerer's security only with a fingerprint. |
| 87 | val answer = with(RcsSessionManager) { buildSdpAnswer(null, null) } |
| 88 | assertTrue(answer.contains("a=setup:active")) |
| 89 | assertTrue(answer.contains("TCP/MSRP")) |
| 90 | val passive = with(RcsSessionManager) { |
| 91 | buildSdpAnswer("10.0.0.6", "msrp://10.0.0.6:2855/sess-2;tcp") |
| 92 | } |
| 93 | assertTrue(passive.contains("a=setup:passive")) |
| 94 | val tlsAnswer = with(RcsSessionManager) { |
| 95 | buildSdpAnswer("10.0.0.6", "msrp://10.0.0.6:2855/sess-2;tcp", secure = true, tlsFingerprint = "DD:EE") |
| 96 | } |
| 97 | assertTrue(tlsAnswer.contains("TCP/TLS/MSRP")) |
| 98 | assertTrue(tlsAnswer.contains("a=fingerprint:SHA-256 DD:EE")) |
| 99 | // Secure requested but no fingerprint: downgraded answer. |
| 100 | val downgraded = with(RcsSessionManager) { |
| 101 | buildSdpAnswer("10.0.0.6", "msrp://10.0.0.6:2855/sess-2;tcp", secure = true, tlsFingerprint = null) |
| 102 | } |
| 103 | assertTrue(downgraded.contains("m=message 2855 TCP/MSRP *")) |
| 104 | assertFalse(downgraded.contains("a=fingerprint:")) |
| 105 | } |
| 106 | |
| 107 | @Test |
| 108 | fun currentSdpForSeededSession() { |
| 109 | val conv = "pass2-curr-${UUID.randomUUID()}" |
| 110 | assertNull(RcsSessionManager.currentSdpFor(conv)) |
| 111 | // No local path yet: no SDP. |
| 112 | seedSession(conv) |
| 113 | assertNull(RcsSessionManager.currentSdpFor(conv)) |
| 114 | // Bind a plaintext passive-side path (peer active => we are passive). |
| 115 | seedSession( |
| 116 | conv, |
| 117 | session = RcsSession( |
| 118 | dialogId = "d2", callId = "pass2-curr-call", localTag = "lt", remoteTag = "rt", |
| 119 | remoteUri = "sip:peer@rcs", conversationId = conv, |
| 120 | msrpLocalPath = "msrp://10.0.0.7:9999/self;tcp", |
| 121 | msrpRemotePath = "msrp://10.0.0.9:2855/peer;tcp", |
| 122 | msrpSetup = MsrpSetup.ACTIVE, |
| 123 | ), |
| 124 | ) |
| 125 | val sdp = RcsSessionManager.currentSdpFor(conv)?.toString(Charsets.UTF_8) |
| 126 | assertNotNull(sdp) |
| 127 | assertTrue(sdp.contains("m=message 9999 TCP/MSRP *")) |
| 128 | assertTrue(sdp.contains("a=path:msrp://10.0.0.7:9999/self;tcp")) |
| 129 | assertTrue(sdp.contains("a=setup:passive")) |
| 130 | // Secure + non-active peer: TLS proto, active role. |
| 131 | seedSession( |
| 132 | conv, |
| 133 | session = RcsSession( |
| 134 | dialogId = "d3", callId = "pass2-curr-call", localTag = "lt", remoteTag = "rt", |
| 135 | remoteUri = "sip:peer@rcs", conversationId = conv, |
| 136 | msrpLocalPath = "msrps://10.0.0.7:9999/self;tcp", |
| 137 | msrpRemotePath = "msrps://10.0.0.9:2855/peer;tcp", |
| 138 | msrpSetup = MsrpSetup.PASSIVE, msrpSecure = true, |
| 139 | ), |
| 140 | ) |
| 141 | val secureSdp = RcsSessionManager.currentSdpFor(conv)?.toString(Charsets.UTF_8) |
| 142 | assertNotNull(secureSdp) |
| 143 | assertTrue(secureSdp.contains("TCP/TLS/MSRP")) |
| 144 | assertTrue(secureSdp.contains("a=setup:active")) |
| 145 | drop(conv) |
| 146 | } |
| 147 | |
| 148 | @Test |
| 149 | fun reInviteAppliesNewSdp() { |
| 150 | val conv = "pass2-reinv-${UUID.randomUUID()}" |
| 151 | val callId = "pass2-reinv-call-${UUID.randomUUID()}" |
| 152 | seedSession(conv, callId) |
| 153 | assertNull(RcsSessionManager.peerFingerprint(callId)) |
| 154 | val sdp = "v=0\r\nm=message 2855 TCP/TLS/MSRP *\r\n" + |
| 155 | "a=path:msrps://10.0.0.9:2855/new;tcp\r\na=setup:passive\r\n" + |
| 156 | "a=fingerprint:SHA-256 11:22:33\r\n" |
| 157 | RcsSessionManager.onReInvite(callId, sdp) |
| 158 | val updated = RcsSessionManager.sessionFor(conv) |
| 159 | assertNotNull(updated) |
| 160 | assertEquals("msrps://10.0.0.9:2855/new;tcp", updated.msrpRemotePath) |
| 161 | assertEquals(MsrpSetup.PASSIVE, updated.msrpSetup) |
| 162 | assertTrue(updated.msrpSecure) |
| 163 | assertEquals("11:22:33", RcsSessionManager.peerFingerprint(callId)) |
| 164 | // Unknown call id is a no-op. |
| 165 | RcsSessionManager.onReInvite("no-such-call", sdp) |
| 166 | drop(conv) |
| 167 | } |
| 168 | |
| 169 | @Test |
| 170 | fun dialogErrorSetsBackoffAndDropsSession() { |
| 171 | val conv = "pass2-err-${UUID.randomUUID()}" |
| 172 | val callId = "pass2-err-call-${UUID.randomUUID()}" |
| 173 | assertFalse(RcsSessionManager.inDialogBackoff(conv)) |
| 174 | seedSession(conv, callId) |
| 175 | assertTrue(RcsSessionManager.isKnownDialog(callId)) |
| 176 | RcsSessionManager.onDialogError(callId, 481) |
| 177 | assertNull(RcsSessionManager.sessionFor(conv)) |
| 178 | assertFalse(RcsSessionManager.isKnownDialog(callId)) |
| 179 | assertTrue(RcsSessionManager.inDialogBackoff(conv)) |
| 180 | // Errors for unknown calls do not create backoff for other ids. |
| 181 | RcsSessionManager.onDialogError("unknown-call", 408) |
| 182 | assertFalse(RcsSessionManager.inDialogBackoff("pass2-other")) |
| 183 | } |
| 184 | |
| 185 | @Test |
| 186 | fun focusBookkeepingOnSeededFocusSession() = runTest { |
| 187 | val conv = "pass2-focus-${UUID.randomUUID()}" |
| 188 | val focusUri = "conf:[email protected]" |
| 189 | seedSession( |
| 190 | conv, |
| 191 | session = RcsSession( |
| 192 | dialogId = "focus:$focusUri", callId = "focus-call", localTag = "lt", |
| 193 | remoteTag = "", remoteUri = focusUri, conversationId = conv, |
| 194 | isGroup = true, isFocus = true, |
| 195 | ), |
| 196 | ) |
| 197 | assertEquals(focusUri, RcsSessionManager.hostedFocusFor(conv)) |
| 198 | assertNull(RcsSessionManager.hostedFocusFor("no-such-conv")) |
| 199 | assertNull(RcsSessionManager.focusMembers(focusUri)) |
| 200 | // Hosting lookups by target URI (INVITE-to-focus routing). |
| 201 | assertNotNull(RcsSessionManager.hostedConversationFor("INVITE $focusUri SIP/2.0")) |
| 202 | assertNull(RcsSessionManager.hostedConversationFor("")) |
| 203 | assertNull(RcsSessionManager.hostedConversationFor("tel:+1555")) |
| 204 | // invite/remove need transport; they fail closed. |
| 205 | assertFalse(RcsSessionManager.inviteFocusMember(conv, "+1555")) |
| 206 | drop(conv) |
| 207 | // Non-focus sessions are not hosted foci. |
| 208 | val conv2 = "pass2-plain-${UUID.randomUUID()}" |
| 209 | seedSession(conv2) |
| 210 | assertNull(RcsSessionManager.hostedFocusFor(conv2)) |
| 211 | assertFalse(RcsSessionManager.removeFocusMember(conv2, "+1555")) |
| 212 | drop(conv2) |
| 213 | } |
| 214 | |
| 215 | @Test |
| 216 | fun noteJoinLeaveOnUnknownFocusAreSafe() { |
| 217 | RcsSessionManager.noteFocusJoin("conf:[email protected]", " +1555 ") |
| 218 | RcsSessionManager.noteFocusLeave("conf:[email protected]", "+1555") |
| 219 | assertNull(RcsSessionManager.focusMembers("conf:[email protected]")) |
| 220 | } |
| 221 | |
| 222 | @Test |
| 223 | fun transportGatedEntryPointsFailClosed() = runTest { |
| 224 | val conv = "pass2-gated-${UUID.randomUUID()}" |
| 225 | assertNull(RcsSessionManager.startSession("tel:+1555", conv)) |
| 226 | assertNull(RcsSessionManager.startGroupSession("sip:focus@rcs", "subject", conv)) |
| 227 | assertFalse(RcsSessionManager.acceptIncoming(conv)) |
| 228 | assertFalse(RcsSessionManager.declineIncoming(conv)) |
| 229 | assertFalse(RcsSessionManager.sendInDialogMessage(conv, byteArrayOf(1))) |
| 230 | // Terminating an unknown conversation is trivially successful. |
| 231 | assertTrue(RcsSessionManager.terminateSession(conv)) |
| 232 | assertNull(RcsSessionManager.sessionFor(conv)) |
| 233 | // Group focus hosting also needs the transport. |
| 234 | assertNull(RcsSessionManager.hostGroupFocus(conv, "s", listOf("+1555"))) |
| 235 | assertFalse(RcsSessionManager.subscribeConferenceEvents("sip:focus@rcs", conv)) |
| 236 | RcsSessionManager.refreshEventSubscriptions() |
| 237 | // No hosted session exists yet, so destroying is trivially done. |
| 238 | assertTrue(RcsSessionManager.destroyHostedFocus(conv)) |
| 239 | } |
| 240 | |
| 241 | @Test |
| 242 | fun terminateSeededUnconfirmedSession() = runTest { |
| 243 | val conv = "pass2-term-${UUID.randomUUID()}" |
| 244 | seedSession(conv) |
| 245 | // The BYE send fails (no transport) but the session is dropped. |
| 246 | val ok = RcsSessionManager.terminateSession(conv) |
| 247 | assertFalse(ok) |
| 248 | assertNull(RcsSessionManager.sessionFor(conv)) |
| 249 | } |
| 250 | |
| 251 | @Test |
| 252 | fun destroyHostedFocusOnNonFocusDelegatesToTerminate() = runTest { |
| 253 | val conv = "pass2-destroy-${UUID.randomUUID()}" |
| 254 | seedSession(conv) |
| 255 | assertFalse(RcsSessionManager.destroyHostedFocus(conv)) |
| 256 | assertNull(RcsSessionManager.sessionFor(conv)) |
| 257 | // Unknown conversation: destroys trivially. |
| 258 | assertTrue(RcsSessionManager.destroyHostedFocus("pass2-unknown-${UUID.randomUUID()}")) |
| 259 | } |
| 260 | |
| 261 | @Test |
| 262 | fun sessionResponsesIgnoreUnknownAndProvisional() = runTest { |
| 263 | // Unknown call ids and dialog-less codes are absorbed. |
| 264 | RcsSessionManager.onSipResponse(200, "unknown-call", "tag", null) |
| 265 | RcsSessionManager.onSipResponse(100, "unknown-call", null, null) |
| 266 | val conv = "pass2-resp-${UUID.randomUUID()}" |
| 267 | val callId = "pass2-resp-call-${UUID.randomUUID()}" |
| 268 | seedSession(conv, callId) |
| 269 | // Provisional responses (< 300 without success handling) leave the session. |
| 270 | RcsSessionManager.onSipResponse(180, callId, null, null) |
| 271 | assertNotNull(RcsSessionManager.sessionFor(conv)) |
| 272 | // 2xx without a remote tag does not establish. |
| 273 | RcsSessionManager.onSipResponse(200, callId, null, null) |
| 274 | assertNotNull(RcsSessionManager.sessionFor(conv)) |
| 275 | assertFalse(RcsSessionManager.sessionFor(conv)!!.confirmed) |
| 276 | drop(conv) |
| 277 | } |
| 278 | |
| 279 | @Test |
| 280 | fun establishedPeerActiveWithoutListenPathIsUnusableButConfirmed() = runTest { |
| 281 | // With a fake transport, establish a session whose peer answers |
| 282 | // active-only: media cannot bind, so the remote path is dropped |
| 283 | // but the dialog still confirms. |
| 284 | com.vayunmathur.communicate.data.rcs.RcsSipTransport.fakeSenderForTest = { _, _, _ -> true } |
| 285 | try { |
| 286 | val conv = "pass2-unusable-${UUID.randomUUID()}" |
| 287 | val dialog = RcsSessionManager.startSession("tel:+1555", conv) |
| 288 | assertNotNull(dialog) |
| 289 | val session = RcsSessionManager.sessionFor(conv) |
| 290 | assertNotNull(session) |
| 291 | RcsSessionManager.onSipResponse( |
| 292 | 200, session.callId, "rtag", |
| 293 | "v=0\r\nm=message 2855 TCP/MSRP *\r\na=path:msrp://10.0.0.9:2855/p;tcp\r\na=setup:active\r\n", |
| 294 | ) |
| 295 | val established = RcsSessionManager.sessionFor(conv) |
| 296 | assertNotNull(established) |
| 297 | assertTrue(established.confirmed) |
| 298 | assertNull(established.msrpRemotePath) |
| 299 | assertTrue(RcsSessionManager.terminateSession(conv)) |
| 300 | } finally { |
| 301 | com.vayunmathur.communicate.data.rcs.RcsSipTransport.fakeSenderForTest = null |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | @Test |
| 306 | fun subscribeAndPublishConferenceWithoutTransport() = runTest { |
| 307 | val conv = "pass2-conf-${UUID.randomUUID()}" |
| 308 | RcsSessionManager.noteFocusSubscriber("sub-call-1", "conf:[email protected]") |
| 309 | RcsSessionManager.publishConferenceInfo("conf:[email protected]") |
| 310 | RcsSessionManager.dropFocusSubscriber("sub-call-1") |
| 311 | RcsSessionManager.noteFocusSubscriber("sub-call-1", "conf:[email protected]") |
| 312 | RcsSessionManager.dropFocusSubscriber("sub-call-1") |
| 313 | // Notify routing for an unparseable body reconciles nothing. |
| 314 | val (added, removed) = RcsSessionManager.onConferenceNotify(conv, "not a conference doc") |
| 315 | assertEquals(emptyList(), added) |
| 316 | assertEquals(emptyList(), removed) |
| 317 | } |
| 318 | } |