Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / communicate-dev / src / test / java / com / vayunmathur / communicate / signal / SignalCallMessageTest.kt
SignalCallMessageTest.kt104 lines · 4.2 KiBRaw
| 1 | package com.vayunmathur.communicate.signal |
| 2 | |
| 3 | import com.vayunmathur.communicate.data.signal.call.SignalCallMessage |
| 4 | import com.vayunmathur.communicate.data.signal.call.SignalRemote |
| 5 | import com.vayunmathur.communicate.data.signal.call.toContent |
| 6 | import com.vayunmathur.communicate.data.signal.call.toProto |
| 7 | import com.vayunmathur.communicate.data.signal.call.toRingRtc |
| 8 | import org.signal.ringrtc.CallManager |
| 9 | import org.whispersystems.signalservice.internal.push.SignalServiceProtos |
| 10 | import kotlin.test.Test |
| 11 | import kotlin.test.assertContentEquals |
| 12 | import kotlin.test.assertEquals |
| 13 | import kotlin.test.assertFalse |
| 14 | import kotlin.test.assertTrue |
| 15 | |
| 16 | /** |
| 17 | * Wire vectors for 1:1 call signaling. The details that matter: offers, answers and ICE updates carry |
| 18 | * **opaque** V4 protobuf bytes and never SDP (those fields are `reserved`), a batch of ICE candidates |
| 19 | * shares one call id, and `destinationDeviceId` is omitted for broadcast messages. |
| 20 | */ |
| 21 | class SignalCallMessageTest { |
| 22 | private val opaque = byteArrayOf(1, 2, 3, 4) |
| 23 | |
| 24 | @Test |
| 25 | fun offerCarriesOpaqueAndMediaType() { |
| 26 | val content = SignalCallMessage.Offer(callId = 42L, opaque = opaque, video = true).toContent(null) |
| 27 | val offer = content.callMessage.offer |
| 28 | assertEquals(42L, offer.id) |
| 29 | assertEquals(SignalServiceProtos.CallMessage.Offer.Type.OFFER_VIDEO_CALL, offer.type) |
| 30 | assertContentEquals(opaque, offer.opaque.toByteArray()) |
| 31 | assertTrue(offer.hasOpaque()) |
| 32 | } |
| 33 | |
| 34 | @Test |
| 35 | fun audioOfferUsesTheAudioType() { |
| 36 | val content = SignalCallMessage.Offer(callId = 1L, opaque = opaque, video = false).toContent(null) |
| 37 | assertEquals( |
| 38 | SignalServiceProtos.CallMessage.Offer.Type.OFFER_AUDIO_CALL, |
| 39 | content.callMessage.offer.type, |
| 40 | ) |
| 41 | } |
| 42 | |
| 43 | @Test |
| 44 | fun answerCarriesOpaque() { |
| 45 | val content = SignalCallMessage.Answer(callId = 7L, opaque = opaque).toContent(2) |
| 46 | assertEquals(7L, content.callMessage.answer.id) |
| 47 | assertContentEquals(opaque, content.callMessage.answer.opaque.toByteArray()) |
| 48 | } |
| 49 | |
| 50 | @Test |
| 51 | fun iceBatchSharesOneCallIdAndKeepsEveryCandidate() { |
| 52 | val candidates = listOf(byteArrayOf(1), byteArrayOf(2), byteArrayOf(3)) |
| 53 | val content = SignalCallMessage.Ice(callId = 9L, candidates = candidates).toContent(null) |
| 54 | val updates = content.callMessage.iceUpdateList |
| 55 | assertEquals(3, updates.size) |
| 56 | assertTrue(updates.all { it.id == 9L }) |
| 57 | assertContentEquals(candidates.map { it.first() }, updates.map { it.opaque.toByteArray().first() }) |
| 58 | } |
| 59 | |
| 60 | @Test |
| 61 | fun hangupCarriesTypeAndExcludedDevice() { |
| 62 | val content = SignalCallMessage.Hangup( |
| 63 | callId = 5L, |
| 64 | type = CallManager.HangupType.DECLINED, |
| 65 | deviceId = 3, |
| 66 | ).toContent(null) |
| 67 | val hangup = content.callMessage.hangup |
| 68 | assertEquals(5L, hangup.id) |
| 69 | assertEquals(SignalServiceProtos.CallMessage.Hangup.Type.HANGUP_DECLINED, hangup.type) |
| 70 | assertEquals(3, hangup.deviceId) |
| 71 | } |
| 72 | |
| 73 | @Test |
| 74 | fun busyCarriesOnlyTheCallId() { |
| 75 | val content = SignalCallMessage.Busy(callId = 11L).toContent(null) |
| 76 | assertEquals(11L, content.callMessage.busy.id) |
| 77 | } |
| 78 | |
| 79 | @Test |
| 80 | fun destinationDeviceIdIsOmittedForBroadcast() { |
| 81 | // Broadcast means every device; setting the field would make recipients drop it. |
| 82 | val broadcast = SignalCallMessage.Offer(1L, opaque, false).toContent(null) |
| 83 | assertFalse(broadcast.callMessage.hasDestinationDeviceId()) |
| 84 | |
| 85 | val targeted = SignalCallMessage.Answer(1L, opaque).toContent(4) |
| 86 | assertTrue(targeted.callMessage.hasDestinationDeviceId()) |
| 87 | assertEquals(4, targeted.callMessage.destinationDeviceId) |
| 88 | } |
| 89 | |
| 90 | @Test |
| 91 | fun hangupTypesRoundTripThroughTheProto() { |
| 92 | for (type in CallManager.HangupType.values()) { |
| 93 | assertEquals(type, type.toProto().toRingRtc(), "round-trip failed for $type") |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | @Test |
| 98 | fun remoteComparesByAciCaseInsensitively() { |
| 99 | val a = SignalRemote("3F0E5A2C-1111-2222-3333-444455556666") |
| 100 | val b = SignalRemote("3f0e5a2c-1111-2222-3333-444455556666") |
| 101 | assertTrue(a.recipientEquals(b)) |
| 102 | assertFalse(a.recipientEquals(SignalRemote("7c1d9b4e-aaaa-bbbb-cccc-ddddeeeeffff"))) |
| 103 | } |
| 104 | } |