Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / communicate-dev / src / test / java / com / vayunmathur / communicate / signal / SignalSlice2MessagingOfflineTest.kt
SignalSlice2MessagingOfflineTest.kt402 lines · 16.7 KiBRaw
| 1 | package com.vayunmathur.communicate.signal |
| 2 | |
| 3 | import androidx.room3.Room |
| 4 | import androidx.sqlite.driver.bundled.BundledSQLiteDriver |
| 5 | import androidx.test.core.app.ApplicationProvider |
| 6 | import com.vayunmathur.communicate.data.signal.SignalAuthData |
| 7 | import com.vayunmathur.communicate.data.signal.SignalCachedMessage |
| 8 | import com.vayunmathur.communicate.data.signal.SignalClient |
| 9 | import com.vayunmathur.communicate.data.signal.SignalContact |
| 10 | import com.vayunmathur.communicate.data.signal.SignalConversation |
| 11 | import com.vayunmathur.communicate.data.signal.SignalDatabase |
| 12 | import com.vayunmathur.communicate.data.signal.SignalEvent |
| 13 | import com.vayunmathur.communicate.data.signal.SignalGroups |
| 14 | import com.vayunmathur.communicate.data.signal.SignalProtocol |
| 15 | import com.vayunmathur.communicate.data.signal.e2e.SignalE2E |
| 16 | import com.vayunmathur.communicate.data.signal.acceptIdentityChange |
| 17 | import com.vayunmathur.communicate.data.signal.conversationIdFor |
| 18 | import com.vayunmathur.communicate.data.signal.displayNameFor |
| 19 | import com.vayunmathur.communicate.data.signal.editMessage |
| 20 | import com.vayunmathur.communicate.data.signal.markRead |
| 21 | import com.vayunmathur.communicate.data.signal.pendingIdentityChange |
| 22 | import com.vayunmathur.communicate.data.signal.poll |
| 23 | import com.vayunmathur.communicate.data.signal.readReceipt |
| 24 | import com.vayunmathur.communicate.data.signal.refreshPresence |
| 25 | import com.vayunmathur.communicate.data.signal.rejectCall |
| 26 | import com.vayunmathur.communicate.data.signal.removeReaction |
| 27 | import com.vayunmathur.communicate.data.signal.reportIdentityChange |
| 28 | import com.vayunmathur.communicate.data.signal.revoke |
| 29 | import com.vayunmathur.communicate.data.signal.safetyNumber |
| 30 | import com.vayunmathur.communicate.data.signal.sendContactCard |
| 31 | import com.vayunmathur.communicate.data.signal.sendMedia |
| 32 | import com.vayunmathur.communicate.data.signal.sendPollVote |
| 33 | import com.vayunmathur.communicate.data.signal.sendReaction |
| 34 | import com.vayunmathur.communicate.data.signal.sendTyping |
| 35 | import kotlin.test.AfterTest |
| 36 | import kotlin.test.BeforeTest |
| 37 | import kotlin.test.Test |
| 38 | import kotlin.test.assertEquals |
| 39 | import kotlin.test.assertFalse |
| 40 | import kotlin.test.assertNotNull |
| 41 | import kotlin.test.assertNull |
| 42 | import kotlin.test.assertTrue |
| 43 | import kotlinx.coroutines.async |
| 44 | import kotlinx.coroutines.flow.first |
| 45 | import kotlinx.coroutines.launch |
| 46 | import kotlinx.coroutines.test.runTest |
| 47 | import kotlinx.coroutines.withTimeoutOrNull |
| 48 | import org.junit.runner.RunWith |
| 49 | import org.robolectric.RobolectricTestRunner |
| 50 | |
| 51 | /** |
| 52 | * Wave 10 (w6 slice 2/6): [SignalClient] messaging offline paths — reactions, edits, revokes, |
| 53 | * polls, receipts, typing, presence, contact cards, media guard, identity bookkeeping, and |
| 54 | * contact/display resolution. `e2e` stays null so every [sendContent]-backed path fails closed |
| 55 | * offline; the event and DB effects are what is asserted. |
| 56 | */ |
| 57 | @RunWith(RobolectricTestRunner::class) |
| 58 | class SignalSlice2MessagingOfflineTest { |
| 59 | |
| 60 | private lateinit var db: SignalDatabase |
| 61 | private val peer = "3f0e5a2c-1111-2222-3333-444455556666" |
| 62 | |
| 63 | @BeforeTest |
| 64 | fun open() { |
| 65 | val ctx = ApplicationProvider.getApplicationContext<android.content.Context>() |
| 66 | db = Room.inMemoryDatabaseBuilder(ctx, SignalDatabase::class.java) |
| 67 | .setDriver(BundledSQLiteDriver()).allowMainThreadQueries().build() |
| 68 | SignalClient.db = db |
| 69 | SignalClient.e2e = null |
| 70 | SignalClient.authData = SignalAuthData( |
| 71 | phoneNumber = "+14155552671", |
| 72 | aci = "aaaaaaaa-1111-2222-3333-444455556666", |
| 73 | password = "secret", |
| 74 | ) |
| 75 | SignalClient.socket = null |
| 76 | SignalClient.unauthSocket = null |
| 77 | SignalClient.pendingIdentityChanges.clear() |
| 78 | } |
| 79 | |
| 80 | @AfterTest |
| 81 | fun close() { |
| 82 | SignalClient.db = null |
| 83 | SignalClient.authData = null |
| 84 | SignalClient.e2e = null |
| 85 | SignalClient.pendingIdentityChanges.clear() |
| 86 | db.close() |
| 87 | } |
| 88 | |
| 89 | // ---- reactions ---- |
| 90 | |
| 91 | @Test |
| 92 | fun sendReaction_emitsReceivedWithoutNetwork() = runTest { |
| 93 | val seen = mutableListOf<SignalEvent>() |
| 94 | val job = launch(kotlinx.coroutines.Dispatchers.Unconfined) { SignalClient.events.collect { seen.add(it) } } |
| 95 | val ok = SignalClient.sendReaction(peer, "msg-1", "\u2764\uFE0F") |
| 96 | assertTrue(ok) // always true: send failures are surfaced via events, not the return |
| 97 | job.cancel() |
| 98 | val received = seen.filterIsInstance<SignalEvent.ReactionReceived>() |
| 99 | assertEquals(1, received.size) |
| 100 | assertEquals(peer, received[0].conversationId) |
| 101 | assertEquals("msg-1", received[0].messageId) |
| 102 | assertEquals("\u2764\uFE0F", received[0].emoji) |
| 103 | } |
| 104 | |
| 105 | @Test |
| 106 | fun sendReaction_usesCachedTimestampAndAuthor() = runTest { |
| 107 | db.cachedMessageDao().upsert( |
| 108 | SignalCachedMessage( |
| 109 | messageId = "msg-9", conversationId = peer, body = "hi", |
| 110 | timestamp = 424242L, outgoing = false, senderId = peer, |
| 111 | ), |
| 112 | ) |
| 113 | val seen = mutableListOf<SignalEvent>() |
| 114 | val job = launch(kotlinx.coroutines.Dispatchers.Unconfined) { SignalClient.events.collect { seen.add(it) } } |
| 115 | SignalClient.sendReaction(peer, "msg-9", "\uD83D\uDC4D") |
| 116 | job.cancel() |
| 117 | assertEquals(1, seen.filterIsInstance<SignalEvent.ReactionReceived>().size) |
| 118 | } |
| 119 | |
| 120 | @Test |
| 121 | fun removeReaction_emitsRemoved() = runTest { |
| 122 | val seen = mutableListOf<SignalEvent>() |
| 123 | val job = launch(kotlinx.coroutines.Dispatchers.Unconfined) { SignalClient.events.collect { seen.add(it) } } |
| 124 | assertTrue(SignalClient.removeReaction(peer, "msg-2")) |
| 125 | job.cancel() |
| 126 | val removed = seen.filterIsInstance<SignalEvent.ReactionRemoved>() |
| 127 | assertEquals(1, removed.size) |
| 128 | assertEquals("msg-2", removed[0].messageId) |
| 129 | assertEquals(peer, removed[0].conversationId) |
| 130 | } |
| 131 | |
| 132 | // ---- edits / revokes ---- |
| 133 | |
| 134 | @Test |
| 135 | fun editMessage_marksEditedAndEmits() = runTest { |
| 136 | db.cachedMessageDao().upsert( |
| 137 | SignalCachedMessage( |
| 138 | messageId = "edit-1", conversationId = peer, body = "typo", |
| 139 | timestamp = 1000L, outgoing = true, senderId = "me", |
| 140 | ), |
| 141 | ) |
| 142 | val seen = mutableListOf<SignalEvent>() |
| 143 | val job = launch(kotlinx.coroutines.Dispatchers.Unconfined) { SignalClient.events.collect { seen.add(it) } } |
| 144 | assertTrue(SignalClient.editMessage(peer, "edit-1", "fixed")) |
| 145 | job.cancel() |
| 146 | assertEquals("fixed", db.cachedMessageDao().get("edit-1")?.body) |
| 147 | val edited = seen.filterIsInstance<SignalEvent.MessageEdited>() |
| 148 | assertEquals(1, edited.size) |
| 149 | assertEquals("fixed", edited[0].newBody) |
| 150 | } |
| 151 | |
| 152 | @Test |
| 153 | fun revoke_marksRevokedAndEmits() = runTest { |
| 154 | db.cachedMessageDao().upsert( |
| 155 | SignalCachedMessage( |
| 156 | messageId = "rev-1", conversationId = peer, body = "oops", |
| 157 | timestamp = 2000L, outgoing = true, senderId = "me", |
| 158 | ), |
| 159 | ) |
| 160 | val seen = mutableListOf<SignalEvent>() |
| 161 | val job = launch(kotlinx.coroutines.Dispatchers.Unconfined) { SignalClient.events.collect { seen.add(it) } } |
| 162 | assertTrue(SignalClient.revoke(peer, "rev-1")) |
| 163 | job.cancel() |
| 164 | assertTrue(db.cachedMessageDao().get("rev-1")?.isRevoked == true) |
| 165 | assertEquals(1, seen.filterIsInstance<SignalEvent.MessageDeleted>().size) |
| 166 | } |
| 167 | |
| 168 | // ---- polls ---- |
| 169 | |
| 170 | @Test |
| 171 | fun poll_cachesQuestionAndEmitsUpdate() = runTest { |
| 172 | val seen = mutableListOf<SignalEvent>() |
| 173 | val job = launch(kotlinx.coroutines.Dispatchers.Unconfined) { SignalClient.events.collect { seen.add(it) } } |
| 174 | val id = SignalClient.poll(peer, "lunch?", listOf("pizza", "sushi")) |
| 175 | assertNotNull(id) |
| 176 | job.cancel() |
| 177 | val cached = db.cachedMessageDao().get(id) |
| 178 | assertNotNull(cached) |
| 179 | assertEquals("lunch?", cached.body) |
| 180 | assertTrue(seen.filterIsInstance<SignalEvent.MessageUpdate>().any { it.messageId == id }) |
| 181 | } |
| 182 | |
| 183 | @Test |
| 184 | fun sendPollVote_emitsVoteEvenOffline() = runTest { |
| 185 | val id = SignalClient.poll(peer, "lunch?", listOf("pizza", "sushi")) |
| 186 | assertNotNull(id) |
| 187 | val seen = mutableListOf<SignalEvent>() |
| 188 | val job = launch(kotlinx.coroutines.Dispatchers.Unconfined) { SignalClient.events.collect { seen.add(it) } } |
| 189 | assertTrue(SignalClient.sendPollVote(peer, id, listOf("pizza"))) |
| 190 | job.cancel() |
| 191 | val votes = seen.filterIsInstance<SignalEvent.PollVote>() |
| 192 | assertEquals(1, votes.size) |
| 193 | assertEquals(listOf("pizza"), votes[0].optionNames) |
| 194 | } |
| 195 | |
| 196 | @Test |
| 197 | fun sendPollVote_unknownPollFallsBackToIndexes() = runTest { |
| 198 | val seen = mutableListOf<SignalEvent>() |
| 199 | val job = launch(kotlinx.coroutines.Dispatchers.Unconfined) { SignalClient.events.collect { seen.add(it) } } |
| 200 | assertTrue(SignalClient.sendPollVote(peer, "no-such-poll", listOf("a", "b"))) |
| 201 | job.cancel() |
| 202 | assertEquals(1, seen.filterIsInstance<SignalEvent.PollVote>().size) |
| 203 | } |
| 204 | |
| 205 | // ---- receipts / typing / presence ---- |
| 206 | |
| 207 | @Test |
| 208 | fun readReceipt_emitsWithCachedTimestamp() = runTest { |
| 209 | db.cachedMessageDao().upsert( |
| 210 | SignalCachedMessage( |
| 211 | messageId = "rr-1", conversationId = peer, body = "yo", |
| 212 | timestamp = 777L, outgoing = false, senderId = peer, |
| 213 | ), |
| 214 | ) |
| 215 | val seen = mutableListOf<SignalEvent>() |
| 216 | val job = launch(kotlinx.coroutines.Dispatchers.Unconfined) { SignalClient.events.collect { seen.add(it) } } |
| 217 | assertTrue(SignalClient.readReceipt(peer, "rr-1", 0L)) |
| 218 | job.cancel() |
| 219 | val receipts = seen.filterIsInstance<SignalEvent.ReadReceipt>() |
| 220 | assertEquals(1, receipts.size) |
| 221 | assertEquals(777L, receipts[0].timestampMs) |
| 222 | } |
| 223 | |
| 224 | @Test |
| 225 | fun readReceipt_fallsBackToLastTimestamp() = runTest { |
| 226 | val seen = mutableListOf<SignalEvent>() |
| 227 | val job = launch(kotlinx.coroutines.Dispatchers.Unconfined) { SignalClient.events.collect { seen.add(it) } } |
| 228 | assertTrue(SignalClient.readReceipt(peer, null, 999L)) |
| 229 | job.cancel() |
| 230 | val receipts = seen.filterIsInstance<SignalEvent.ReadReceipt>() |
| 231 | assertEquals(1, receipts.size) |
| 232 | assertEquals(999L, receipts[0].timestampMs) |
| 233 | } |
| 234 | |
| 235 | @Test |
| 236 | fun markRead_marksDbAndEmitsPerMessage() = runTest { |
| 237 | db.cachedMessageDao().upsert( |
| 238 | SignalCachedMessage( |
| 239 | messageId = "mr-1", conversationId = peer, body = "a", |
| 240 | timestamp = 10L, outgoing = false, senderId = peer, |
| 241 | ), |
| 242 | ) |
| 243 | db.cachedMessageDao().upsert( |
| 244 | SignalCachedMessage( |
| 245 | messageId = "mr-2", conversationId = peer, body = "b", |
| 246 | timestamp = 20L, outgoing = false, senderId = peer, |
| 247 | ), |
| 248 | ) |
| 249 | val seen = mutableListOf<SignalEvent>() |
| 250 | val job = launch(kotlinx.coroutines.Dispatchers.Unconfined) { SignalClient.events.collect { seen.add(it) } } |
| 251 | SignalClient.markRead(peer, listOf("mr-1", "mr-2")) |
| 252 | job.cancel() |
| 253 | val receipts = seen.filterIsInstance<SignalEvent.ReadReceipt>() |
| 254 | assertEquals(2, receipts.size) |
| 255 | assertEquals(setOf("mr-1", "mr-2"), receipts.map { it.messageId }.toSet()) |
| 256 | } |
| 257 | |
| 258 | @Test |
| 259 | fun sendTyping_emitsIndicator() = runTest { |
| 260 | val seen = mutableListOf<SignalEvent>() |
| 261 | val job = launch(kotlinx.coroutines.Dispatchers.Unconfined) { SignalClient.events.collect { seen.add(it) } } |
| 262 | SignalClient.sendTyping(peer, true) |
| 263 | SignalClient.sendTyping(peer, false) |
| 264 | job.cancel() |
| 265 | val typing = seen.filterIsInstance<SignalEvent.TypingIndicator>() |
| 266 | assertEquals(2, typing.size) |
| 267 | assertTrue(typing[0].isTyping) |
| 268 | assertFalse(typing[1].isTyping) |
| 269 | } |
| 270 | |
| 271 | @Test |
| 272 | fun refreshPresence_emitsOfflineUpdate() = runTest { |
| 273 | // Signal has no presence REST: always an offline no-op update, never a network call. |
| 274 | val first = kotlinx.coroutines.CoroutineScope(kotlinx.coroutines.Dispatchers.Unconfined).async { |
| 275 | SignalClient.events.first() |
| 276 | } |
| 277 | SignalClient.refreshPresence(peer) |
| 278 | val event = withTimeoutOrNull(5_000) { first.await() } |
| 279 | assertTrue(event is SignalEvent.PresenceUpdate, "got $event") |
| 280 | assertFalse((event as SignalEvent.PresenceUpdate).isOnline) |
| 281 | } |
| 282 | |
| 283 | // ---- contact cards ---- |
| 284 | |
| 285 | @Test |
| 286 | fun sendContactCard_failsClosedWithoutE2e() = runTest { |
| 287 | // e2e is null so sendContent returns false; the card still builds offline. |
| 288 | assertFalse( |
| 289 | SignalClient.sendContactCard( |
| 290 | peer, "Ada", "Lovelace", |
| 291 | listOf("+14155552671" to "mobile"), listOf("[email protected]"), |
| 292 | ), |
| 293 | ) |
| 294 | } |
| 295 | |
| 296 | // ---- media guard ---- |
| 297 | |
| 298 | @Test |
| 299 | fun sendMedia_emitsSendFailedWhenFormUnreachable() = runTest { |
| 300 | // fetchForm hits the network; offline it returns null and a SendFailed is emitted. |
| 301 | SignalClient.e2e = null |
| 302 | val first = kotlinx.coroutines.CoroutineScope(kotlinx.coroutines.Dispatchers.Unconfined).async { |
| 303 | SignalClient.events.first() |
| 304 | } |
| 305 | val id = SignalClient.sendMedia(peer, byteArrayOf(1, 2, 3), "image/png", "pic.png") |
| 306 | assertNull(id) |
| 307 | val event = withTimeoutOrNull(15_000) { first.await() } |
| 308 | assertTrue(event is SignalEvent.SendFailed, "got $event") |
| 309 | } |
| 310 | |
| 311 | // ---- identity bookkeeping ---- |
| 312 | |
| 313 | @Test |
| 314 | fun reportIdentityChange_storesPendingAndEmits() = runTest { |
| 315 | val key = ByteArray(33) { it.toByte() } |
| 316 | val first = kotlinx.coroutines.CoroutineScope(kotlinx.coroutines.Dispatchers.Unconfined).async { |
| 317 | SignalClient.events.first() |
| 318 | } |
| 319 | SignalClient.reportIdentityChange(peer, key) |
| 320 | assertTrue(SignalClient.pendingIdentityChange(peer)?.contentEquals(key) == true) |
| 321 | // The event is produced off the test scheduler; withTimeoutOrNull |
| 322 | // inside runTest burns virtual time instantly and returned null |
| 323 | // under load. Await the same signal on a real dispatcher instead. |
| 324 | val event = kotlinx.coroutines.withContext(kotlinx.coroutines.Dispatchers.Default) { |
| 325 | withTimeoutOrNull(60_000) { first.await() } |
| 326 | } |
| 327 | assertTrue(event is SignalEvent.IdentityKeyChanged, "got $event") |
| 328 | assertEquals(peer, (event as SignalEvent.IdentityKeyChanged).peerAci) |
| 329 | } |
| 330 | |
| 331 | @Test |
| 332 | fun acceptIdentityChange_rejectsWithoutE2e() = runTest { |
| 333 | SignalClient.reportIdentityChange(peer, ByteArray(33) { 7 }) |
| 334 | // e2e is null: cannot accept, pending stays. |
| 335 | assertFalse(SignalClient.acceptIdentityChange(peer, "ff".repeat(33))) |
| 336 | assertNotNull(SignalClient.pendingIdentityChange(peer)) |
| 337 | } |
| 338 | |
| 339 | @Test |
| 340 | fun acceptIdentityChange_rejectsMissingPending() = runTest { |
| 341 | assertFalse(SignalClient.acceptIdentityChange("nobody", "00")) |
| 342 | } |
| 343 | |
| 344 | @Test |
| 345 | fun acceptIdentityChange_rejectsWrongHex() = runTest { |
| 346 | SignalClient.reportIdentityChange(peer, ByteArray(33) { 7 }) |
| 347 | SignalClient.e2e = SignalE2E(db, SignalClient.authData!!) |
| 348 | assertFalse(SignalClient.acceptIdentityChange(peer, "00")) |
| 349 | } |
| 350 | |
| 351 | @Test |
| 352 | fun safetyNumber_nullWithoutE2e() = runTest { |
| 353 | assertNull(SignalClient.safetyNumber(peer)) |
| 354 | } |
| 355 | |
| 356 | // ---- contact / display resolution ---- |
| 357 | |
| 358 | @Test |
| 359 | fun displayNameFor_prefersContactName() = runTest { |
| 360 | assertEquals(peer, SignalClient.displayNameFor(peer)) |
| 361 | db.contactDao().upsert( |
| 362 | SignalContact(aci = peer, phoneE164 = "+14155552671", displayName = "Grace"), |
| 363 | ) |
| 364 | assertEquals("Grace", SignalClient.displayNameFor(peer)) |
| 365 | } |
| 366 | |
| 367 | @Test |
| 368 | fun displayNameFor_fallsBackToPhoneThenId() = runTest { |
| 369 | db.contactDao().upsert( |
| 370 | SignalContact(aci = "x-1", phoneE164 = "+14155550000", displayName = ""), |
| 371 | ) |
| 372 | assertEquals("+14155550000", SignalClient.displayNameFor("x-1")) |
| 373 | } |
| 374 | |
| 375 | @Test |
| 376 | fun conversationIdFor_anchorsOnPhone() = runTest { |
| 377 | assertEquals(peer, SignalClient.conversationIdFor(peer)) |
| 378 | db.contactDao().upsert( |
| 379 | SignalContact(aci = peer, phoneE164 = "+14155552671", displayName = "Grace"), |
| 380 | ) |
| 381 | assertEquals("+14155552671", SignalClient.conversationIdFor(peer)) |
| 382 | } |
| 383 | |
| 384 | @Test |
| 385 | fun groupHelpers_roundTrip() = runTest { |
| 386 | val (masterKey, _) = SignalGroups.generateMasterKeyAndSecretParams() |
| 387 | val convId = SignalProtocol.toConversationId("", masterKey) |
| 388 | assertTrue(SignalProtocol.isGroupConversation(convId)) |
| 389 | assertTrue(SignalGroups.groupIdentifierBytes(masterKey).size == 32) |
| 390 | } |
| 391 | |
| 392 | @Test |
| 393 | fun rejectCall_withoutManagerEmitsAndReturnsTrue() = runTest { |
| 394 | val seen = mutableListOf<SignalEvent>() |
| 395 | val job = launch(kotlinx.coroutines.Dispatchers.Unconfined) { SignalClient.events.collect { seen.add(it) } } |
| 396 | assertTrue(SignalClient.rejectCall("call-1")) |
| 397 | job.cancel() |
| 398 | val ended = seen.filterIsInstance<SignalEvent.CallEnded>() |
| 399 | assertEquals(1, ended.size) |
| 400 | assertEquals("rejected", ended[0].reason) |
| 401 | } |
| 402 | } |