Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / communicate-dev / src / test / java / com / vayunmathur / communicate / rcs / RcsSlice3E2ESupportTest.kt
RcsSlice3E2ESupportTest.kt160 lines · 7.3 KiBRaw
| 1 | package com.vayunmathur.communicate.rcs |
| 2 | |
| 3 | import com.vayunmathur.communicate.data.rcs.RcsSessionManager |
| 4 | import com.vayunmathur.communicate.data.rcs.RcsSession |
| 5 | import com.vayunmathur.communicate.data.rcs.e2e.RcsE2E |
| 6 | import com.vayunmathur.communicate.data.rcs.e2e.RcsKeyDirectory |
| 7 | import com.vayunmathur.communicate.data.rcs.e2e.RcsPeerKeys |
| 8 | import com.vayunmathur.communicate.data.rcs.e2e.RcsPendingGroups |
| 9 | import com.vayunmathur.communicate.data.rcs.e2e.e2eThreadFor |
| 10 | import com.vayunmathur.communicate.data.rcs.e2e.commitLockFor |
| 11 | import com.vayunmathur.communicate.data.rcs.e2e.hex |
| 12 | import com.vayunmathur.communicate.data.rcs.e2e.parseMembers |
| 13 | import com.vayunmathur.communicate.data.rcs.e2e.pruneMembers |
| 14 | import kotlin.test.Test |
| 15 | import kotlin.test.assertEquals |
| 16 | import kotlin.test.assertFalse |
| 17 | import kotlin.test.assertNull |
| 18 | import kotlin.test.assertTrue |
| 19 | import kotlinx.coroutines.test.runTest |
| 20 | import org.junit.runner.RunWith |
| 21 | import org.robolectric.RobolectricTestRunner |
| 22 | |
| 23 | /** |
| 24 | * Slice-3 tests for the E2EE support types that don't need the native |
| 25 | * library: [RcsPeerKeys] cache (store/get/consume/expiry), [RcsPendingGroups] |
| 26 | * readiness, key-directory parsing, member-map helpers, hex/thread helpers, |
| 27 | * and the gate-closed [RcsE2E] paths (native unavailable on this host or |
| 28 | * transport down → null/false, never throws). |
| 29 | */ |
| 30 | @RunWith(RobolectricTestRunner::class) |
| 31 | class RcsSlice3E2ESupportTest { |
| 32 | |
| 33 | @Test |
| 34 | fun peerKeysStoreGetConsume() { |
| 35 | RcsPeerKeys.clear() |
| 36 | val pkg = byteArrayOf(9, 8, 7, 6) |
| 37 | RcsPeerKeys.store("+15550006161", pkg) |
| 38 | assertTrue(RcsPeerKeys.get("+15550006161")!!.contentEquals(pkg)) |
| 39 | // Consume removes. |
| 40 | assertTrue(RcsPeerKeys.consume("+15550006161")!!.contentEquals(pkg)) |
| 41 | assertNull(RcsPeerKeys.get("+15550006161")) |
| 42 | assertNull(RcsPeerKeys.consume("+15550006161")) |
| 43 | } |
| 44 | |
| 45 | @Test |
| 46 | fun peerKeysRejectsBadInput() { |
| 47 | RcsPeerKeys.clear() |
| 48 | RcsPeerKeys.store("", byteArrayOf(1)) |
| 49 | RcsPeerKeys.store("+15550006262", ByteArray(0)) |
| 50 | assertNull(RcsPeerKeys.get("")) |
| 51 | assertNull(RcsPeerKeys.get("+15550006262")) |
| 52 | assertNull(RcsPeerKeys.get("+15550006363")) |
| 53 | } |
| 54 | |
| 55 | @Test |
| 56 | fun pendingGroupsReadiness() { |
| 57 | val conv = "slice3-pending-${System.nanoTime()}" |
| 58 | assertFalse(RcsPendingGroups.isPending(conv)) |
| 59 | RcsPendingGroups.add(conv, listOf("+15550007171", "+15550007272")) |
| 60 | assertTrue(RcsPendingGroups.isPending(conv)) |
| 61 | // Not ready: no packages cached. |
| 62 | RcsPeerKeys.clear() |
| 63 | assertTrue(RcsPendingGroups.readyFor("+15550007171").none { it.first == conv }) |
| 64 | // Cache both → ready. |
| 65 | RcsPeerKeys.store("+15550007171", byteArrayOf(1, 2)) |
| 66 | RcsPeerKeys.store("+15550007272", byteArrayOf(3, 4)) |
| 67 | val ready = RcsPendingGroups.readyFor("+15550007171").firstOrNull { it.first == conv } |
| 68 | assertTrue(ready != null) |
| 69 | assertEquals(setOf("+15550007171", "+15550007272"), ready.second.keys) |
| 70 | // Unrelated peer doesn't match. |
| 71 | assertTrue(RcsPendingGroups.readyFor("+15550009999").none { it.first == conv }) |
| 72 | RcsPendingGroups.remove(conv) |
| 73 | assertFalse(RcsPendingGroups.isPending(conv)) |
| 74 | // Bad adds are ignored. |
| 75 | RcsPendingGroups.add("", listOf("+1")) |
| 76 | RcsPendingGroups.add("slice3-blank", emptyList()) |
| 77 | assertFalse(RcsPendingGroups.isPending("")) |
| 78 | assertFalse(RcsPendingGroups.isPending("slice3-blank")) |
| 79 | RcsPeerKeys.clear() |
| 80 | } |
| 81 | |
| 82 | @Test |
| 83 | fun keyDirectoryParseVariants() { |
| 84 | val bytes = byteArrayOf(10, 20, 30, 40, 50) |
| 85 | val b64 = java.util.Base64.getEncoder().encodeToString(bytes) |
| 86 | val body = "Content-Type: application/x-rcs-keypackage\r\n\r\n$b64" |
| 87 | assertTrue(RcsKeyDirectory.parsePublished(body)!!.contentEquals(bytes)) |
| 88 | // LF-only separator also works. |
| 89 | assertTrue( |
| 90 | RcsKeyDirectory.parsePublished("Content-Type: application/x-rcs-keypackage\n\n$b64")!! |
| 91 | .contentEquals(bytes), |
| 92 | ) |
| 93 | assertNull(RcsKeyDirectory.parsePublished("plain body")) |
| 94 | assertNull(RcsKeyDirectory.parsePublished("Content-Type: application/x-rcs-keypackage\r\n\r\n")) |
| 95 | assertNull( |
| 96 | RcsKeyDirectory.parsePublished( |
| 97 | "Content-Type: application/x-rcs-keypackage\r\n\r\n!!!not-base64!!!", |
| 98 | ), |
| 99 | ) |
| 100 | assertNull(RcsKeyDirectory.lastPublishedFor("+15550008181")) |
| 101 | } |
| 102 | |
| 103 | @Test |
| 104 | fun memberMapHelpers() { |
| 105 | assertEquals(emptyMap(), RcsE2E.parseMembers("")) |
| 106 | assertEquals(emptyMap(), RcsE2E.parseMembers("garbage-no-equals")) |
| 107 | assertEquals(emptyMap(), RcsE2E.parseMembers("garbage-no-equals")) |
| 108 | assertEquals(mapOf("+1" to 3), RcsE2E.parseMembers("+1=3")) |
| 109 | assertEquals(mapOf("+1" to 1, "+2" to 2), RcsE2E.parseMembers("+1=1,+2=2")) |
| 110 | // Malformed entries skipped. |
| 111 | assertEquals(mapOf("+2" to 2), RcsE2E.parseMembers("bad,+2=2,alsobad=")) |
| 112 | // Prune keeps the rest. |
| 113 | assertEquals("+2=2", RcsE2E.pruneMembers("+1=1,+2=2", setOf(1))) |
| 114 | assertEquals("", RcsE2E.pruneMembers("+1=1", setOf(1))) |
| 115 | // Track: creator leaf starts at 1, dedupes, continues max+1. |
| 116 | val tracked = RcsE2E.trackAddedMembers("", listOf("+1", "+1", " ", "+2")) |
| 117 | assertEquals(mapOf("+1" to 1, "+2" to 2), RcsE2E.parseMembers(tracked)) |
| 118 | val grown = RcsE2E.trackAddedMembers(tracked, listOf("+3")) |
| 119 | assertEquals(3, RcsE2E.parseMembers(grown)["+3"]) |
| 120 | } |
| 121 | |
| 122 | @Test |
| 123 | fun hexAndThreadHelpers() { |
| 124 | assertEquals("00ff10", byteArrayOf(0, 0xFF.toByte(), 0x10).hex()) |
| 125 | assertEquals("e2e:+15550009191", e2eThreadFor("+15550009191")) |
| 126 | // Commit locks are per-conversation singletons. |
| 127 | assertTrue(RcsE2E.commitLockFor("slice3-lock-a") === RcsE2E.commitLockFor("slice3-lock-a")) |
| 128 | assertFalse(RcsE2E.commitLockFor("slice3-lock-a") === RcsE2E.commitLockFor("slice3-lock-b")) |
| 129 | } |
| 130 | |
| 131 | @Test |
| 132 | fun e2eGateClosedPathsNeverThrow() = runTest { |
| 133 | // Transport is down in tests, so these all fail closed. Only the |
| 134 | // Room-free paths are exercised here (peer-key cache + key directory |
| 135 | // are in-memory; the Room-backed identity paths need SQLCipher's |
| 136 | // AndroidKeyStore, covered on-device instead). |
| 137 | RcsPeerKeys.clear() |
| 138 | assertNull(RcsE2E.safetyFingerprint("+15550009292")) |
| 139 | assertNull(RcsE2E.mySafetyFingerprint("+15550009393")) |
| 140 | assertFalse(RcsE2E.requestKeyPackage("+15550009595")) |
| 141 | assertFalse(RcsE2E.isMlsPayload("plain text, short")) |
| 142 | // A cached package yields a stable safety fingerprint without Room. |
| 143 | RcsPeerKeys.store("+15550009292", byteArrayOf(7, 7, 7)) |
| 144 | val fp = RcsE2E.safetyFingerprint("+15550009292") |
| 145 | assertTrue(fp != null && fp.isNotBlank()) |
| 146 | assertEquals(fp, RcsE2E.safetyFingerprint("+15550009292")) |
| 147 | RcsPeerKeys.clear() |
| 148 | } |
| 149 | |
| 150 | @Test |
| 151 | fun e2eContentTypeDetectionWithoutNative() { |
| 152 | // Content-type match short-circuits before any native call; when the |
| 153 | // native lib is unavailable the gate returns false first. |
| 154 | val body = "Content-Type: application/x-rcs-mls\r\n\r\nQUJD" |
| 155 | val result = RcsE2E.isMlsPayload(body) |
| 156 | // Either true (native present + CT match) or false (gate closed) — |
| 157 | // both are honest; assert it doesn't throw and is deterministic. |
| 158 | assertEquals(result, RcsE2E.isMlsPayload(body)) |
| 159 | } |
| 160 | } |