Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / communicate-dev / src / test / java / com / vayunmathur / communicate / rcs / RcsSlice3Pass2DelegateTest.kt
RcsSlice3Pass2DelegateTest.kt466 lines · 22.2 KiBRaw
| 1 | package com.vayunmathur.communicate.rcs |
| 2 | |
| 3 | import com.vayunmathur.communicate.data.rcs.buildInvite |
| 4 | |
| 5 | import android.content.Context |
| 6 | import androidx.test.core.app.ApplicationProvider |
| 7 | import com.vayunmathur.communicate.data.rcs.RcsRegistrationState |
| 8 | import com.vayunmathur.communicate.data.rcs.RcsSipTransport |
| 9 | import com.vayunmathur.communicate.data.rcs.RcsUnavailableReason |
| 10 | import java.lang.reflect.InvocationHandler |
| 11 | import java.lang.reflect.Proxy |
| 12 | import java.net.InetSocketAddress |
| 13 | import java.util.concurrent.CopyOnWriteArrayList |
| 14 | import kotlin.coroutines.Continuation |
| 15 | import kotlin.coroutines.intrinsics.COROUTINE_SUSPENDED |
| 16 | import kotlin.coroutines.suspendCoroutine |
| 17 | import kotlinx.coroutines.async |
| 18 | import kotlinx.coroutines.test.runTest |
| 19 | import org.junit.runner.RunWith |
| 20 | import org.robolectric.RobolectricTestRunner |
| 21 | import org.robolectric.annotation.Config |
| 22 | import kotlin.test.AfterTest |
| 23 | import kotlin.test.Test |
| 24 | import kotlin.test.assertEquals |
| 25 | import kotlin.test.assertFalse |
| 26 | import kotlin.test.assertNotNull |
| 27 | import kotlin.test.assertNull |
| 28 | import kotlin.test.assertTrue |
| 29 | |
| 30 | /** |
| 31 | * Slice-3 pass 2: the SipDelegate transport with a fake framework |
| 32 | * connection installed through the test seam — delegate callbacks, |
| 33 | * pending-send acks, digest-auth retry, inbound routing, teardown. |
| 34 | * Framework types are built reflectively (they are hidden APIs). |
| 35 | */ |
| 36 | @RunWith(RobolectricTestRunner::class) |
| 37 | @Config(sdk = [34], shadows = [RcsSlice3Pass2ProvisioningShadow::class]) |
| 38 | class RcsSlice3Pass2DelegateTest { |
| 39 | |
| 40 | private val context: Context get() = ApplicationProvider.getApplicationContext() |
| 41 | |
| 42 | private val sipMessageClass: Class<*> = Class.forName("android.telephony.ims.SipMessage") |
| 43 | private val connectionClass: Class<*> = Class.forName("android.telephony.ims.SipDelegateConnection") |
| 44 | |
| 45 | private class FakeConnection { |
| 46 | data class SentMessage(val message: Any, val version: Long, val branch: String) |
| 47 | val sentMessages = CopyOnWriteArrayList<SentMessage>() |
| 48 | val cleanedSessions = CopyOnWriteArrayList<String>() |
| 49 | val notifiedReceived = CopyOnWriteArrayList<String>() |
| 50 | @Volatile var failSend = false |
| 51 | |
| 52 | val proxy: Any |
| 53 | init { |
| 54 | val connectionClass = Class.forName("android.telephony.ims.SipDelegateConnection") |
| 55 | val sipMessageClass = Class.forName("android.telephony.ims.SipMessage") |
| 56 | val branchMethod = sipMessageClass.getMethod("getViaBranchParameter") |
| 57 | val handler = InvocationHandler { _, method, args -> |
| 58 | when (method.name) { |
| 59 | "sendMessage" -> { |
| 60 | if (failSend) throw IllegalStateException("fake send failure") |
| 61 | val message = args[0] |
| 62 | sentMessages += SentMessage( |
| 63 | message, args[1] as Long, branchMethod.invoke(message) as String, |
| 64 | ) |
| 65 | null |
| 66 | } |
| 67 | "cleanupSession" -> { cleanedSessions += args[0] as String; null } |
| 68 | "notifyMessageReceived" -> { notifiedReceived += args[0] as String; null } |
| 69 | else -> null |
| 70 | } |
| 71 | } |
| 72 | proxy = Proxy.newProxyInstance(connectionClass.classLoader, arrayOf(connectionClass), handler) |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | private fun sipMessage(startLine: String, headers: String, body: ByteArray = ByteArray(0)): Any = |
| 77 | sipMessageClass.getConstructor(String::class.java, String::class.java, ByteArray::class.java) |
| 78 | .newInstance(startLine, headers, body) |
| 79 | |
| 80 | private fun config(version: Long = 7L): Any { |
| 81 | val builderClass = Class.forName("android.telephony.ims.SipDelegateConfiguration\$Builder") |
| 82 | val builder = builderClass |
| 83 | .getConstructor(Long::class.javaPrimitiveType, Int::class.javaPrimitiveType, InetSocketAddress::class.java, InetSocketAddress::class.java) |
| 84 | .newInstance(version, 1, InetSocketAddress("10.0.0.2", 5060), InetSocketAddress("10.0.0.1", 5060)) |
| 85 | fun set(name: String, value: String) { |
| 86 | builderClass.getMethod(name, String::class.java).invoke(builder, value) |
| 87 | } |
| 88 | set("setPublicUserIdentifier", "sip:[email protected]") |
| 89 | set("setHomeDomain", "carrier.example") |
| 90 | set("setImei", "imei-123") |
| 91 | set("setSipServiceRouteHeader", "<sip:proxy.carrier.example;lr>") |
| 92 | set("setSipPaniHeader", "3GPP-E-UTRAN-FDD;utran-cell-id-3gpp=abc") |
| 93 | set("setSipUserAgentHeader", "CarrierUA/1.0") |
| 94 | return builderClass.getMethod("build").invoke(builder) |
| 95 | } |
| 96 | |
| 97 | private object RcsSessionManagerForInvite { |
| 98 | fun build(): Triple<String, String, ByteArray> = |
| 99 | with(com.vayunmathur.communicate.data.rcs.RcsSessionManager) { |
| 100 | buildInvite( |
| 101 | targetUri = "sip:[email protected]", |
| 102 | callId = "invite-call@rcs", |
| 103 | localTag = "ltag1234", |
| 104 | branch = "z9hG4bKinvite", |
| 105 | conversationId = "conv-invite", |
| 106 | subject = "hello subject", |
| 107 | ) |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | private fun seam(name: String): java.lang.reflect.Method = |
| 112 | RcsSipTransport::class.java.methods.first { it.name.startsWith(name) } |
| 113 | |
| 114 | private fun install(fake: FakeConnection?, version: Long = 7L, config: Any? = if (fake != null) config(version) else null) { |
| 115 | seam("installConnectionForTest").invoke(RcsSipTransport, fake?.proxy, version, config) |
| 116 | } |
| 117 | |
| 118 | private fun callbacks(): Pair<Any, Any> { |
| 119 | @Suppress("UNCHECKED_CAST") |
| 120 | return seam("callbacksForTest").invoke(RcsSipTransport) as Pair<Any, Any> |
| 121 | } |
| 122 | |
| 123 | private fun invokeCallback(callback: Any, methodName: String, vararg args: Any?) { |
| 124 | val method = callback.javaClass.methods.first { |
| 125 | it.name == methodName && it.parameterCount == args.size |
| 126 | } |
| 127 | method.invoke(callback, *args) |
| 128 | } |
| 129 | |
| 130 | private fun responseMessage( |
| 131 | startLine: String, |
| 132 | branch: String, |
| 133 | callId: String = "dlg-call@rcs", |
| 134 | cseq: String = "1 MESSAGE", |
| 135 | extra: String = "", |
| 136 | body: String = "", |
| 137 | ): Any { |
| 138 | val headers = buildString { |
| 139 | append("Via: SIP/2.0/TCP 10.0.0.1;branch=$branch\r\n") |
| 140 | append("To: <sip:peer@rcs>;tag=remote\r\n") |
| 141 | append("From: <sip:[email protected]>;tag=local\r\n") |
| 142 | append("Call-ID: $callId\r\n") |
| 143 | append("CSeq: $cseq\r\n") |
| 144 | append(extra) |
| 145 | } |
| 146 | return sipMessage(startLine, headers, body.toByteArray(Charsets.UTF_8)) |
| 147 | } |
| 148 | |
| 149 | private suspend fun invokeSuspend(methodName: String, vararg args: Any?): Any? { |
| 150 | val method = RcsSipTransport::class.java.methods.first { |
| 151 | it.name == methodName && it.parameterTypes.size == args.size + 1 |
| 152 | } |
| 153 | return suspendCoroutine { cont -> |
| 154 | @Suppress("UNCHECKED_CAST") |
| 155 | val result = method.invoke(RcsSipTransport, *args, cont as Continuation<Any?>) |
| 156 | if (result !== COROUTINE_SUSPENDED) cont.resumeWith(Result.success(result)) |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | private fun setInboundListener(listener: ((Any) -> Unit)?) { |
| 161 | // Reflective: the property type is the hidden SipMessage class. |
| 162 | val field = RcsSipTransport::class.java.getDeclaredField("onInboundMessage") |
| 163 | field.isAccessible = true |
| 164 | field.set(RcsSipTransport, listener) |
| 165 | } |
| 166 | |
| 167 | @AfterTest |
| 168 | fun tearDown() { |
| 169 | seam("installConnectionForTest").invoke(RcsSipTransport, null, -1L, null) |
| 170 | setInboundListener(null) |
| 171 | RcsSipTransport.tearDown(context) |
| 172 | } |
| 173 | |
| 174 | @Test |
| 175 | fun delegateLifecycleCallbacksDriveState() { |
| 176 | val fake = FakeConnection() |
| 177 | val (stateCallback, _) = callbacks() |
| 178 | // onCreated installs the connection and flips state to Available. |
| 179 | invokeCallback(stateCallback, "onCreated", fake.proxy) |
| 180 | assertEquals(RcsRegistrationState.Available, RcsSipTransport.state.value) |
| 181 | assertEquals("single-reg delegate", RcsSipTransport.activeLeg) |
| 182 | // onConfigurationChanged records the snapshot used by SIP builders. |
| 183 | invokeCallback(stateCallback, "onConfigurationChanged", config()) |
| 184 | val snapshot = RcsSipTransport.lastConfigSnapshot() |
| 185 | assertNotNull(snapshot) |
| 186 | assertEquals(7L, snapshot.version) |
| 187 | assertEquals("sip:[email protected]", snapshot.publicUserId) |
| 188 | assertEquals("carrier.example", snapshot.homeDomain) |
| 189 | assertEquals("<sip:proxy.carrier.example;lr>", snapshot.serviceRoute) |
| 190 | assertEquals("imei-123", snapshot.imei) |
| 191 | // Chat builders now use the configured identity as From. |
| 192 | val (_, headers, _) = RcsSipTransport.buildChatMessage("sip:fallback@rcs", "sip:peer@rcs", "c1", "hi") |
| 193 | assertTrue(headers.contains("From: <sip:[email protected]>")) |
| 194 | |
| 195 | // Denied feature tags flip to Unavailable(TransportDenied). |
| 196 | val regStateClass = Class.forName("android.telephony.ims.DelegateRegistrationState") |
| 197 | val regState = regStateClass.getDeclaredConstructor().apply { isAccessible = true }.newInstance() |
| 198 | val tagClass = Class.forName("android.telephony.ims.FeatureTagState") |
| 199 | val denied = tagClass.getConstructor(String::class.java, Int::class.javaPrimitiveType) |
| 200 | .newInstance("+g.3gpp.icsi-ref=\"urn:x\"", 2) |
| 201 | invokeCallback(stateCallback, "onFeatureTagStatusChanged", regState, setOf(denied)) |
| 202 | assertEquals( |
| 203 | RcsRegistrationState.Unavailable(RcsUnavailableReason.TransportDenied), |
| 204 | RcsSipTransport.state.value, |
| 205 | ) |
| 206 | // An empty denial set leaves state alone. |
| 207 | invokeCallback(stateCallback, "onFeatureTagStatusChanged", regState, emptySet<Any>()) |
| 208 | assertEquals( |
| 209 | RcsRegistrationState.Unavailable(RcsUnavailableReason.TransportDenied), |
| 210 | RcsSipTransport.state.value, |
| 211 | ) |
| 212 | |
| 213 | // onDestroyed clears the connection and reports ServiceUnavailable |
| 214 | // only when it had been Available; restore Available first. |
| 215 | install(fake) |
| 216 | assertEquals(RcsRegistrationState.Available, RcsSipTransport.state.value) |
| 217 | invokeCallback(stateCallback, "onDestroyed", 1) |
| 218 | assertFalse(RcsSipTransport.canSend()) |
| 219 | assertEquals( |
| 220 | RcsRegistrationState.Unavailable(RcsUnavailableReason.ServiceUnavailable), |
| 221 | RcsSipTransport.state.value, |
| 222 | ) |
| 223 | } |
| 224 | |
| 225 | @Test |
| 226 | fun sendSipMessageAckAndFailureFlows() = runTest { |
| 227 | val fake = FakeConnection() |
| 228 | install(fake) |
| 229 | assertTrue(RcsSipTransport.canSend()) |
| 230 | |
| 231 | // Happy path: send is parked, the sent-ack completes it true. |
| 232 | val (_, _, _) = Triple(0, 0, 0) |
| 233 | val pending = async(kotlinx.coroutines.Dispatchers.Default) { |
| 234 | @Suppress("UNCHECKED_CAST") |
| 235 | invokeSuspend("sendSipMessage", "MESSAGE sip:peer@rcs SIP/2.0", "Via: SIP/2.0/TCP x;branch=placeholder\r\n", "body".toByteArray()) as Boolean |
| 236 | } |
| 237 | // Wait for the fake to see the send. |
| 238 | val deadline = System.currentTimeMillis() + 10_000 |
| 239 | while (System.currentTimeMillis() < deadline && fake.sentMessages.isEmpty()) Thread.sleep(10) |
| 240 | assertEquals(1, fake.sentMessages.size) |
| 241 | assertEquals(7L, fake.sentMessages.first().version) |
| 242 | val branch = fake.sentMessages.first().branch |
| 243 | assertTrue(branch.startsWith("z9hG4bK") || branch.isNotEmpty(), branch) |
| 244 | val (_, messageCallback) = callbacks() |
| 245 | invokeCallback(messageCallback, "onMessageSent", branch) |
| 246 | assertTrue(pending.await()) |
| 247 | |
| 248 | // Failure callback completes a send false. |
| 249 | fake.sentMessages.clear() |
| 250 | val pending2 = async(kotlinx.coroutines.Dispatchers.Default) { |
| 251 | @Suppress("UNCHECKED_CAST") |
| 252 | invokeSuspend("sendSipMessage", "MESSAGE sip:peer@rcs SIP/2.0", "Via: SIP/2.0/TCP x;branch=p2\r\n", ByteArray(0)) as Boolean |
| 253 | } |
| 254 | val deadline2 = System.currentTimeMillis() + 10_000 |
| 255 | while (System.currentTimeMillis() < deadline2 && fake.sentMessages.isEmpty()) Thread.sleep(10) |
| 256 | val branch2 = fake.sentMessages.first().branch |
| 257 | invokeCallback(messageCallback, "onMessageSendFailure", branch2, 3) |
| 258 | assertFalse(pending2.await()) |
| 259 | |
| 260 | // A throwing connection send fails the call immediately. |
| 261 | fake.failSend = true |
| 262 | val failed = invokeSuspend("sendSipMessage", "MESSAGE sip:peer@rcs SIP/2.0", "Via: x\r\n", ByteArray(0)) as Boolean |
| 263 | assertFalse(failed) |
| 264 | fake.failSend = false |
| 265 | } |
| 266 | |
| 267 | @Test |
| 268 | fun inboundMessageCallbackRoutesToListener() { |
| 269 | val fake = FakeConnection() |
| 270 | install(fake) |
| 271 | val received = CopyOnWriteArrayList<Any>() |
| 272 | setInboundListener { received += it } |
| 273 | val (_, messageCallback) = callbacks() |
| 274 | val inbound = sipMessage( |
| 275 | "MESSAGE sip:me@rcs SIP/2.0", |
| 276 | "Via: SIP/2.0/TCP 10.0.0.1;branch=z9hG4bKinbound1\r\nCall-ID: in-1\r\n", |
| 277 | "hello".toByteArray(), |
| 278 | ) |
| 279 | invokeCallback(messageCallback, "onMessageReceived", inbound) |
| 280 | assertEquals(1, received.size) |
| 281 | // The delegate was told we received it (branch ack). |
| 282 | assertEquals(listOf("z9hG4bKinbound1"), fake.notifiedReceived.toList()) |
| 283 | setInboundListener(null) |
| 284 | } |
| 285 | |
| 286 | @Test |
| 287 | fun digestAuthRetryOn401ThenSuccess() = runTest { |
| 288 | val fake = FakeConnection() |
| 289 | install(fake) |
| 290 | val pending = async(kotlinx.coroutines.Dispatchers.Default) { |
| 291 | @Suppress("UNCHECKED_CAST") |
| 292 | invokeSuspend("sendSipMessage", "MESSAGE sip:peer@rcs SIP/2.0", "Via: SIP/2.0/TCP host;branch=origbranch\r\nContent-Length: 4\r\n", "body".toByteArray()) as Boolean |
| 293 | } |
| 294 | val deadline = System.currentTimeMillis() + 10_000 |
| 295 | while (System.currentTimeMillis() < deadline && fake.sentMessages.isEmpty()) Thread.sleep(10) |
| 296 | assertEquals(1, fake.sentMessages.size) |
| 297 | val challenge = "WWW-Authenticate: Digest realm=\"carrier.example\", nonce=\"nonce-42\", qop=auth\r\n" |
| 298 | val response401 = responseMessage("SIP/2.0 401 Unauthorized", fake.sentMessages.first().branch, extra = challenge) |
| 299 | val consumed = invokeSuspend("onSipResponseMessage", response401) as Boolean |
| 300 | assertTrue(consumed) |
| 301 | // The request was re-sent once, with a digest Authorization header |
| 302 | // and a fresh branch. |
| 303 | assertEquals(2, fake.sentMessages.size) |
| 304 | val retried = fake.sentMessages[1] |
| 305 | assertFalse(retried.branch == fake.sentMessages[0].branch) |
| 306 | // Ack the retried transaction. |
| 307 | val (_, messageCallback) = callbacks() |
| 308 | invokeCallback(messageCallback, "onMessageSent", retried.branch) |
| 309 | assertTrue(pending.await()) |
| 310 | } |
| 311 | |
| 312 | @Test |
| 313 | fun second401FailsTheSend() = runTest { |
| 314 | val fake = FakeConnection() |
| 315 | install(fake) |
| 316 | val pending = async(kotlinx.coroutines.Dispatchers.Default) { |
| 317 | @Suppress("UNCHECKED_CAST") |
| 318 | invokeSuspend("sendSipMessage", "MESSAGE sip:peer@rcs SIP/2.0", "Via: SIP/2.0/TCP host;branch=b1\r\n", "body".toByteArray()) as Boolean |
| 319 | } |
| 320 | val deadline = System.currentTimeMillis() + 10_000 |
| 321 | while (System.currentTimeMillis() < deadline && fake.sentMessages.isEmpty()) Thread.sleep(10) |
| 322 | val challenge = "WWW-Authenticate: Digest realm=\"carrier.example\", nonce=\"n1\", qop=auth\r\n" |
| 323 | assertTrue(invokeSuspend("onSipResponseMessage", responseMessage("SIP/2.0 401 Unauthorized", fake.sentMessages[0].branch, extra = challenge)) as Boolean) |
| 324 | assertEquals(2, fake.sentMessages.size) |
| 325 | // Second challenge for the retried branch: no more retries, send fails. |
| 326 | assertTrue(invokeSuspend("onSipResponseMessage", responseMessage("SIP/2.0 401 Unauthorized", fake.sentMessages[1].branch, extra = challenge)) as Boolean) |
| 327 | assertFalse(pending.await()) |
| 328 | assertEquals(2, fake.sentMessages.size) |
| 329 | } |
| 330 | |
| 331 | @Test |
| 332 | fun pendingResponseResolvesThroughResponseRouter() = runTest { |
| 333 | val fake = FakeConnection() |
| 334 | install(fake) |
| 335 | val pending = async(kotlinx.coroutines.Dispatchers.Default) { |
| 336 | @Suppress("UNCHECKED_CAST") |
| 337 | invokeSuspend("sendSipMessage", "MESSAGE sip:peer@rcs SIP/2.0", "Via: SIP/2.0/TCP host;branch=b9\r\n", "body".toByteArray()) as Boolean |
| 338 | } |
| 339 | val deadline = System.currentTimeMillis() + 10_000 |
| 340 | while (System.currentTimeMillis() < deadline && fake.sentMessages.isEmpty()) Thread.sleep(10) |
| 341 | val branch = fake.sentMessages.first().branch |
| 342 | // Provisional is absorbed without completing the send. |
| 343 | assertTrue(invokeSuspend("onSipResponseMessage", responseMessage("SIP/2.0 100 Trying", branch)) as Boolean) |
| 344 | assertFalse(pending.isCompleted) |
| 345 | // Final 2xx (non-INVITE) completes it successfully. |
| 346 | assertTrue(invokeSuspend("onSipResponseMessage", responseMessage("SIP/2.0 200 OK", branch)) as Boolean) |
| 347 | assertTrue(pending.await()) |
| 348 | // A failure final on a fresh send fails it (and routes dialog teardown). |
| 349 | val pending2 = async(kotlinx.coroutines.Dispatchers.Default) { |
| 350 | @Suppress("UNCHECKED_CAST") |
| 351 | invokeSuspend("sendSipMessage", "MESSAGE sip:peer@rcs SIP/2.0", "Via: SIP/2.0/TCP host;branch=b10\r\n", "body".toByteArray()) as Boolean |
| 352 | } |
| 353 | val deadline2 = System.currentTimeMillis() + 10_000 |
| 354 | while (System.currentTimeMillis() < deadline2 && fake.sentMessages.size < 2) Thread.sleep(10) |
| 355 | val branch2 = fake.sentMessages[1].branch |
| 356 | assertTrue(invokeSuspend("onSipResponseMessage", responseMessage("SIP/2.0 603 Decline", branch2)) as Boolean) |
| 357 | assertFalse(pending2.await()) |
| 358 | } |
| 359 | |
| 360 | private fun awaitTransportState(timeoutMs: Long = 10_000): RcsRegistrationState { |
| 361 | val deadline = System.currentTimeMillis() + timeoutMs |
| 362 | var state = RcsSipTransport.state.value |
| 363 | while (System.currentTimeMillis() < deadline) { |
| 364 | state = RcsSipTransport.state.value |
| 365 | if (state !is RcsRegistrationState.Unknown && state !is RcsRegistrationState.Provisioning) break |
| 366 | Thread.sleep(25) |
| 367 | } |
| 368 | return state |
| 369 | } |
| 370 | |
| 371 | private fun configureProbe(url: String?, singleRegFeature: Boolean) { |
| 372 | org.robolectric.shadows.ShadowSubscriptionManager.setDefaultSmsSubscriptionId(5) |
| 373 | val ccm = context.getSystemService(android.telephony.CarrierConfigManager::class.java) |
| 374 | val bundle = android.os.PersistableBundle() |
| 375 | if (url != null) { |
| 376 | bundle.putString(android.telephony.CarrierConfigManager.KEY_RCS_CONFIG_SERVER_URL_STRING, url) |
| 377 | } |
| 378 | org.robolectric.Shadows.shadowOf(ccm).setConfigForSubId(5, bundle) |
| 379 | org.robolectric.Shadows.shadowOf(context.packageManager) |
| 380 | .setSystemFeature("android.hardware.telephony.ims.singlereg", singleRegFeature) |
| 381 | } |
| 382 | |
| 383 | @Test |
| 384 | fun ensureRegisteredNotSupportedWithoutSingleRegFeature() { |
| 385 | RcsSipTransport.tearDown(context) |
| 386 | RcsSlice3Pass2ProvisioningShadow.reset() |
| 387 | RcsSlice3Pass2ProvisioningShadow.capable = false |
| 388 | try { |
| 389 | configureProbe("https://config.example/rcs", singleRegFeature = false) |
| 390 | RcsSipTransport.ensureRegistered(context) |
| 391 | val state = awaitTransportState() |
| 392 | // Probe says the framework is not single-reg capable and we |
| 393 | // hold no carrier privilege; the device gate is off too, so |
| 394 | // the direct-SIP fallback is not attempted and the probe stands. |
| 395 | assertEquals( |
| 396 | RcsRegistrationState.Unavailable(RcsUnavailableReason.NoCarrierPrivilege), state, |
| 397 | ) |
| 398 | } finally { |
| 399 | RcsSlice3Pass2ProvisioningShadow.reset() |
| 400 | RcsSipTransport.tearDown(context) |
| 401 | } |
| 402 | } |
| 403 | |
| 404 | @Test |
| 405 | fun ensureRegisteredAttemptsDelegateWhenProvisioned() { |
| 406 | RcsSipTransport.tearDown(context) |
| 407 | RcsSlice3Pass2ProvisioningShadow.reset() |
| 408 | try { |
| 409 | configureProbe("https://config.example/rcs", singleRegFeature = true) |
| 410 | RcsSipTransport.ensureRegistered(context) |
| 411 | // The probe itself completes Available on its own flow. |
| 412 | val deadline = System.currentTimeMillis() + 10_000 |
| 413 | while (System.currentTimeMillis() < deadline && |
| 414 | com.vayunmathur.communicate.data.rcs.RcsProvisioning.state.value != |
| 415 | RcsRegistrationState.Available |
| 416 | ) { |
| 417 | Thread.sleep(25) |
| 418 | } |
| 419 | assertEquals( |
| 420 | RcsRegistrationState.Available, |
| 421 | com.vayunmathur.communicate.data.rcs.RcsProvisioning.state.value, |
| 422 | ) |
| 423 | // The delegate attempt resolves on the IO scope to some terminal |
| 424 | // state (no privilege on the JVM); Unknown/Disabled/other would |
| 425 | // mean ensureRegistered never ran. |
| 426 | val state = awaitTransportState() |
| 427 | assertTrue( |
| 428 | state !is RcsRegistrationState.Disabled, |
| 429 | "transport state never engaged: $state", |
| 430 | ) |
| 431 | } finally { |
| 432 | RcsSlice3Pass2ProvisioningShadow.reset() |
| 433 | RcsSipTransport.tearDown(context) |
| 434 | } |
| 435 | } |
| 436 | |
| 437 | @Test |
| 438 | fun inviteBuilderUsesDelegateConfig() { |
| 439 | val fake = FakeConnection() |
| 440 | install(fake) |
| 441 | val invite = with(RcsSessionManagerForInvite) { build() } |
| 442 | assertTrue(invite.first.startsWith("INVITE sip:[email protected] SIP/2.0"), invite.first) |
| 443 | assertTrue(invite.second.contains("Route: <<sip:proxy.carrier.example;lr>>") || |
| 444 | invite.second.contains("Route: <sip:proxy.carrier.example;lr>"), invite.second) |
| 445 | assertTrue(invite.second.contains("P-Access-Network-Info: 3GPP-E-UTRAN-FDD"), invite.second) |
| 446 | assertTrue(invite.second.contains("User-Agent: CarrierUA/1.0"), invite.second) |
| 447 | assertTrue(invite.second.contains("Contact: <sip:[email protected]>"), invite.second) |
| 448 | val sdp = invite.third.toString(Charsets.UTF_8) |
| 449 | assertTrue(sdp.contains("a=accept-types:message/cpim"), sdp) |
| 450 | } |
| 451 | |
| 452 | @Test |
| 453 | fun cleanupAndTeardownDropConnection() { |
| 454 | val fake = FakeConnection() |
| 455 | install(fake) |
| 456 | RcsSipTransport.cleanupSession("call-42") |
| 457 | RcsSipTransport.cleanupSession("") |
| 458 | assertEquals(listOf("call-42"), fake.cleanedSessions.toList()) |
| 459 | RcsSipTransport.tearDown(context) |
| 460 | assertFalse(RcsSipTransport.canSend()) |
| 461 | assertEquals("none", RcsSipTransport.activeLeg) |
| 462 | // teardown drops the connection; the last config snapshot is kept |
| 463 | // (only onDestroyed clears it on device). |
| 464 | assertEquals(7L, RcsSipTransport.lastConfigSnapshot()?.version) |
| 465 | } |
| 466 | } |