Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / communicate-dev / src / test / java / com / vayunmathur / communicate / rcs / HiddenApiRcsPass3Test.kt
HiddenApiRcsPass3Test.kt158 lines · 7.2 KiBRaw
| 1 | package com.vayunmathur.communicate.rcs |
| 2 | |
| 3 | import android.content.Context |
| 4 | import android.net.Uri |
| 5 | import android.telephony.ims.RcsClientConfiguration |
| 6 | import android.telephony.ims.`ProvisioningManager$RcsProvisioningCallback` |
| 7 | import androidx.test.core.app.ApplicationProvider |
| 8 | import com.vayunmathur.communicate.data.rcs.RcsHiddenApi |
| 9 | import com.vayunmathur.communicate.data.rcs.RcsMsrp |
| 10 | import com.vayunmathur.communicate.data.rcs.RcsSession |
| 11 | import java.net.ServerSocket |
| 12 | import java.net.Socket |
| 13 | import java.util.concurrent.CopyOnWriteArrayList |
| 14 | import java.util.concurrent.Executors |
| 15 | import java.util.concurrent.TimeUnit |
| 16 | import kotlinx.coroutines.runBlocking |
| 17 | import org.junit.runner.RunWith |
| 18 | import org.robolectric.RobolectricTestRunner |
| 19 | import kotlin.test.Test |
| 20 | import kotlin.test.assertEquals |
| 21 | import kotlin.test.assertFailsWith |
| 22 | import kotlin.test.assertFalse |
| 23 | import kotlin.test.assertNotNull |
| 24 | import kotlin.test.assertNull |
| 25 | import kotlin.test.assertTrue |
| 26 | |
| 27 | /** |
| 28 | * Pass 3: the hidden-API reflection bridge against hand-written fake |
| 29 | * framework objects (UCE capability callbacks via dynamic proxy, |
| 30 | * provisioning callback via the concrete stub subclass), plus MSRP |
| 31 | * connection failure edges over loopback sockets. |
| 32 | */ |
| 33 | @RunWith(RobolectricTestRunner::class) |
| 34 | class HiddenApiRcsPass3Test { |
| 35 | private val context: Context get() = ApplicationProvider.getApplicationContext() |
| 36 | private val executor = Executors.newSingleThreadExecutor() |
| 37 | |
| 38 | @Test fun uceBridgeFailsClosedWithoutAdapterMethods() { |
| 39 | val callback = object : RcsHiddenApi.UceCallback { |
| 40 | override fun onCapabilitiesReceived(caps: List<*>) {} |
| 41 | override fun onComplete() {} |
| 42 | override fun onError(errorCode: Int, retryAfterMillis: Long) {} |
| 43 | } |
| 44 | // No reflective requestCapabilities/requestAvailability methods here. |
| 45 | assertFalse(RcsHiddenApi.requestUceCapabilities(Any(), emptyList(), executor, callback)) |
| 46 | assertFalse( |
| 47 | RcsHiddenApi.requestUceAvailability(Any(), Uri.parse("tel:+15550007002"), executor, callback), |
| 48 | ) |
| 49 | } |
| 50 | |
| 51 | /** Stands in for ProvisioningManager (matched reflectively). */ |
| 52 | class FakeProvisioningManager { |
| 53 | var config: RcsClientConfiguration? = null |
| 54 | var callback: `ProvisioningManager$RcsProvisioningCallback`? = null |
| 55 | var unregisteredWith: Any? = null |
| 56 | fun setRcsClientConfiguration(config: RcsClientConfiguration) { this.config = config } |
| 57 | fun registerRcsProvisioningCallback( |
| 58 | // Mirrors the framework signature; the fake invokes the callback directly. |
| 59 | @Suppress("UnusedParameter") executor: java.util.concurrent.Executor, |
| 60 | callback: `ProvisioningManager$RcsProvisioningCallback`, |
| 61 | ) { this.callback = callback } |
| 62 | fun unregisterRcsProvisioningCallback(callback: `ProvisioningManager$RcsProvisioningCallback`) { |
| 63 | unregisteredWith = callback |
| 64 | } |
| 65 | // Mirrors the framework capability query; the fake always reports single-registration capable. |
| 66 | @Suppress("FunctionOnlyReturningConstant") |
| 67 | fun isRcsVolteSingleRegistrationCapable(): Boolean = true |
| 68 | } |
| 69 | |
| 70 | class ThrowingProvisioningManager { |
| 71 | fun isRcsVolteSingleRegistrationCapable(): Boolean = |
| 72 | throw IllegalStateException("dead") |
| 73 | } |
| 74 | |
| 75 | @Test fun provisioningCallbackSubclassForwardsEvents() { |
| 76 | val pm = FakeProvisioningManager() |
| 77 | var changed: ByteArray? = null |
| 78 | var resets = 0 |
| 79 | var removed = 0 |
| 80 | val callback = object : RcsHiddenApi.ProvisioningCallback { |
| 81 | override fun onConfigurationChanged(configXml: ByteArray) { changed = configXml } |
| 82 | override fun onConfigurationReset() { resets++ } |
| 83 | override fun onRemoved() { removed++ } |
| 84 | } |
| 85 | assertTrue(RcsHiddenApi.registerProvisioningCallback(pm, executor, "6.0", "UP_2.0", callback)) |
| 86 | assertNotNull(pm.config) |
| 87 | val subclass = pm.callback |
| 88 | assertNotNull(subclass) |
| 89 | subclass.onConfigurationChanged("<rcs/>".toByteArray()) |
| 90 | assertEquals("<rcs/>", changed?.toString(Charsets.UTF_8)) |
| 91 | subclass.onConfigurationReset() |
| 92 | subclass.onRemoved() |
| 93 | assertEquals(1, resets) |
| 94 | assertEquals(1, removed) |
| 95 | // Unregister without a proxy is a no-op; with one it dispatches. |
| 96 | RcsHiddenApi.unregisterProvisioningCallback(pm) |
| 97 | RcsHiddenApi.unregisterProvisioningCallback(pm, subclass) |
| 98 | // Single-reg tri-state. |
| 99 | assertEquals(true, RcsHiddenApi.isSingleRegCapable(pm)) |
| 100 | assertNull(RcsHiddenApi.isSingleRegCapable(ThrowingProvisioningManager())) |
| 101 | assertNull(RcsHiddenApi.isSingleRegCapable(Any())) |
| 102 | // The reflective delegate-manager lookup resolves under Robolectric. |
| 103 | assertNotNull(RcsHiddenApi.sipDelegateManager(context, 1)) |
| 104 | } |
| 105 | |
| 106 | @Test fun msrpConnectRefusedAndSendEdges() = runBlocking { |
| 107 | // No listener: connect fails closed. |
| 108 | val dead = RcsSession( |
| 109 | dialogId = "d-ms", callId = "ms-1", localTag = "lt", remoteTag = "rt", |
| 110 | remoteUri = "sip:+15550007009@rcs", conversationId = "ms-conv", |
| 111 | msrpRemotePath = "msrp://127.0.0.1:1/dead;tcp", |
| 112 | msrpLocalPath = "msrp://127.0.0.1/local;tcp", |
| 113 | ) |
| 114 | assertNull(RcsMsrp.connect(dead, context) { _, _ -> }) |
| 115 | // No remote path at all. |
| 116 | assertNull(RcsMsrp.connect(dead.copy(msrpRemotePath = null), context) { _, _ -> }) |
| 117 | |
| 118 | // Loopback pair: SEND round trip in both directions. |
| 119 | val server = ServerSocket(0) |
| 120 | try { |
| 121 | val port = server.localPort |
| 122 | val clientSession = dead.copy( |
| 123 | dialogId = "d-ms2", callId = "ms-2", |
| 124 | msrpRemotePath = "msrp://127.0.0.1:$port/peer;tcp", |
| 125 | ) |
| 126 | val receivedByServer = CopyOnWriteArrayList<ByteArray>() |
| 127 | val acceptedRef = java.util.concurrent.atomic.AtomicReference<RcsMsrp.MsrpConnection>() |
| 128 | val acceptThread = Thread { |
| 129 | runCatching { |
| 130 | val socket: Socket = server.accept() |
| 131 | val conn = RcsMsrp.wrapAccepted( |
| 132 | socket, |
| 133 | com.vayunmathur.communicate.data.rcs.RcsMsrpListen.AcceptedHead(emptyList()), |
| 134 | "msrp://127.0.0.1:$port/peer;tcp", |
| 135 | "msrp://127.0.0.1/local;tcp", |
| 136 | ) { _, body -> receivedByServer += body } |
| 137 | acceptedRef.set(conn) |
| 138 | } |
| 139 | }.apply { isDaemon = true; start() } |
| 140 | |
| 141 | val conn = RcsMsrp.connect(clientSession, context) { _, _ -> } |
| 142 | assertNotNull(conn) |
| 143 | assertTrue(conn.sendCpim("hello msrp".toByteArray())) |
| 144 | val deadline = System.currentTimeMillis() + 10_000 |
| 145 | while (receivedByServer.isEmpty() && System.currentTimeMillis() < deadline) Thread.sleep(10) |
| 146 | assertEquals("hello msrp", receivedByServer.firstOrNull()?.toString(Charsets.UTF_8)) |
| 147 | // Empty payload and closed connection refuse to send. |
| 148 | assertFalse(conn.sendCpim(ByteArray(0))) |
| 149 | conn.close() |
| 150 | assertTrue(conn.isClosed()) |
| 151 | assertFalse(conn.sendCpim("late".toByteArray())) |
| 152 | acceptedRef.get()?.close() |
| 153 | acceptThread.join(TimeUnit.SECONDS.toMillis(5)) |
| 154 | } finally { |
| 155 | server.close() |
| 156 | } |
| 157 | } |
| 158 | } |