Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / communicate-dev / src / test / java / com / vayunmathur / communicate / whatsapp / W5Slice1CallSignalingTest.kt
W5Slice1CallSignalingTest.kt131 lines · 6.0 KiBRaw
| 1 | package com.vayunmathur.communicate.whatsapp |
| 2 | |
| 3 | import androidx.test.ext.junit.runners.AndroidJUnit4 |
| 4 | import com.vayunmathur.communicate.data.whatsapp.WhatsAppProtocol |
| 5 | import com.vayunmathur.communicate.data.whatsapp.call.WhatsAppCallSignaling |
| 6 | import org.junit.runner.RunWith |
| 7 | import kotlin.test.Test |
| 8 | import kotlin.test.assertEquals |
| 9 | import kotlin.test.assertNull |
| 10 | import kotlin.test.assertTrue |
| 11 | |
| 12 | /** |
| 13 | * Slice 1 (w5): call signaling builders/parsers (WhatsAppCallSignaling.kt). |
| 14 | * Robolectric: uses android.util.Base64 for the webrtc SDP/ICE extension. |
| 15 | */ |
| 16 | @RunWith(AndroidJUnit4::class) |
| 17 | class W5Slice1CallSignalingTest { |
| 18 | |
| 19 | @Test |
| 20 | fun offer_videoWithSdpAndCandidates_shape() { |
| 21 | val encs = listOf(WhatsAppProtocol.ParticipantEnc("1:[email protected]", "msg", byteArrayOf(7))) |
| 22 | val node = WhatsAppCallSignaling.buildOffer( |
| 23 | to = "[email protected]", callId = "CALL1", callCreator = "[email protected]", |
| 24 | video = true, encs = encs, stanzaId = "S1", |
| 25 | sdp = "v=0 fake sdp", candidates = listOf("cand-a", "cand-b"), |
| 26 | ) |
| 27 | assertEquals("call", node.tag) |
| 28 | assertEquals("S1", node.attrs["id"]) |
| 29 | val offer = node.getChildByTag("offer")!! |
| 30 | assertEquals("CALL1", offer.attrs["call-id"]) |
| 31 | assertTrue(offer.getChildByTag("video") != null) |
| 32 | val webrtc = offer.getChildByTag("webrtc")!! |
| 33 | assertEquals("v=0 fake sdp", WhatsAppCallSignaling.extractSdp(webrtc)) |
| 34 | assertEquals(listOf("cand-a", "cand-b"), WhatsAppCallSignaling.extractCandidates(webrtc)) |
| 35 | // one <to> destination per enc |
| 36 | assertEquals(1, offer.content.count { (it as WhatsAppProtocol.Node).tag == "to" }) |
| 37 | } |
| 38 | |
| 39 | @Test |
| 40 | fun offer_audioNoSdp_noWebrtcChild() { |
| 41 | val node = WhatsAppCallSignaling.buildOffer( |
| 42 | "[email protected]", "C2", "[email protected]", false, emptyList(), "S2", |
| 43 | ) |
| 44 | val offer = node.getChildByTag("offer")!! |
| 45 | val audio = offer.getChildByTag("audio")!! |
| 46 | assertEquals("opus", audio.attrs["enc"]) |
| 47 | assertNull(offer.getChildByTag("webrtc")) |
| 48 | } |
| 49 | |
| 50 | @Test |
| 51 | fun preAccept_accept_reject_terminate_shapes() { |
| 52 | val pre = WhatsAppCallSignaling.buildPreAccept("[email protected]", "C3", "[email protected]", true, "S3", sdp = "sdp-a") |
| 53 | assertTrue(pre.getChildByTag("preaccept")!!.getChildByTag("video") != null) |
| 54 | assertEquals("sdp-a", WhatsAppCallSignaling.extractSdp(pre.getChildByTag("preaccept")!!.getChildByTag("webrtc")!!)) |
| 55 | |
| 56 | val acc = WhatsAppCallSignaling.buildAccept("[email protected]", "C4", "[email protected]", "S4") |
| 57 | assertTrue(acc.getChildByTag("accept") != null) |
| 58 | |
| 59 | val rej = WhatsAppCallSignaling.buildReject("[email protected]", "C5", "[email protected]", "S5") |
| 60 | assertEquals("C5", rej.getChildByTag("reject")!!.attrs["call-id"]) |
| 61 | |
| 62 | val term = WhatsAppCallSignaling.buildTerminate("[email protected]", "C6", "[email protected]", "timeout", "S6") |
| 63 | assertEquals("timeout", term.getChildByTag("terminate")!!.attrs["reason"]) |
| 64 | } |
| 65 | |
| 66 | @Test |
| 67 | fun iceUpdate_carriesCandidates() { |
| 68 | val node = WhatsAppCallSignaling.buildIceUpdate("[email protected]", "C7", "[email protected]", "S7", listOf("c1")) |
| 69 | val relay = node.getChildByTag("relay")!! |
| 70 | assertEquals(listOf("c1"), WhatsAppCallSignaling.extractCandidates(relay.getChildByTag("webrtc")!!)) |
| 71 | } |
| 72 | |
| 73 | @Test |
| 74 | fun webrtcChild_empty_returnsNull() { |
| 75 | assertNull(WhatsAppCallSignaling.webrtcChild(null, emptyList())) |
| 76 | assertEquals(null, WhatsAppCallSignaling.extractSdp(WhatsAppProtocol.Node(tag = "webrtc"))) |
| 77 | assertEquals(emptyList(), WhatsAppCallSignaling.extractCandidates(WhatsAppProtocol.Node(tag = "webrtc"))) |
| 78 | } |
| 79 | |
| 80 | @Test |
| 81 | fun parse_offerRoundTrip() { |
| 82 | val encs = listOf(WhatsAppProtocol.ParticipantEnc("1:[email protected]", "pkmsg", byteArrayOf(1, 2))) |
| 83 | val built = WhatsAppCallSignaling.buildOffer( |
| 84 | "[email protected]", "CALL9", "[email protected]", false, encs, "ST9", |
| 85 | sdp = "v=0 sdp", candidates = listOf("cc"), |
| 86 | ) |
| 87 | // inbound stanzas carry from= |
| 88 | val inbound = WhatsAppProtocol.Node( |
| 89 | tag = "call", |
| 90 | attrs = built.attrs + mapOf("from" to "[email protected]"), |
| 91 | content = built.content, |
| 92 | ) |
| 93 | val parsed = WhatsAppCallSignaling.parse(inbound)!! |
| 94 | assertEquals(WhatsAppCallSignaling.InboundCall.Kind.OFFER, parsed.kind) |
| 95 | assertEquals("CALL9", parsed.callId) |
| 96 | assertEquals("[email protected]", parsed.from) |
| 97 | assertEquals("[email protected]", parsed.creator) |
| 98 | assertEquals(false, parsed.isVideo) |
| 99 | assertEquals("v=0 sdp", parsed.sdp) |
| 100 | assertEquals(listOf("cc"), parsed.candidates) |
| 101 | assertEquals("ST9", parsed.stanzaId) |
| 102 | } |
| 103 | |
| 104 | @Test |
| 105 | fun parse_nonCallOrMissingFrom_returnsNull() { |
| 106 | assertNull(WhatsAppCallSignaling.parse(WhatsAppProtocol.Node(tag = "iq"))) |
| 107 | assertNull(WhatsAppCallSignaling.parse(WhatsAppProtocol.Node(tag = "call", content = listOf(WhatsAppProtocol.Node(tag = "offer"))))) |
| 108 | assertNull(WhatsAppCallSignaling.parse(WhatsAppProtocol.Node(tag = "call", attrs = mapOf("from" to "a")))) |
| 109 | } |
| 110 | |
| 111 | @Test |
| 112 | fun parse_unknownKind_mapsToUnknown() { |
| 113 | val node = WhatsAppProtocol.Node( |
| 114 | tag = "call", |
| 115 | attrs = mapOf("from" to "[email protected]", "id" to "Z1"), |
| 116 | content = listOf(WhatsAppProtocol.Node(tag = "weird", attrs = mapOf("call-id" to "CZ"))), |
| 117 | ) |
| 118 | val parsed = WhatsAppCallSignaling.parse(node)!! |
| 119 | assertEquals(WhatsAppCallSignaling.InboundCall.Kind.UNKNOWN, parsed.kind) |
| 120 | assertEquals("CZ", parsed.callId) |
| 121 | } |
| 122 | |
| 123 | @Test |
| 124 | fun parse_videoChild_setsIsVideo() { |
| 125 | val built = WhatsAppCallSignaling.buildOffer("[email protected]", "CV", "[email protected]", true, emptyList(), "SV") |
| 126 | val inbound = WhatsAppProtocol.Node( |
| 127 | tag = "call", attrs = built.attrs + mapOf("from" to "[email protected]"), content = built.content, |
| 128 | ) |
| 129 | assertEquals(true, WhatsAppCallSignaling.parse(inbound)!!.isVideo) |
| 130 | } |
| 131 | } |