Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / communicate-dev / src / test / java / com / vayunmathur / communicate / whatsapp / W5Slice1PollVoteTest.kt
W5Slice1PollVoteTest.kt73 lines · 2.8 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.buildPollVoteMessage |
| 6 | import com.vayunmathur.communicate.data.whatsapp.decryptPollVote |
| 7 | import org.junit.runner.RunWith |
| 8 | import kotlin.test.Test |
| 9 | import kotlin.test.assertEquals |
| 10 | import kotlin.test.assertTrue |
| 11 | |
| 12 | /** |
| 13 | * Slice 1 (w5): poll-vote build/decrypt round-trip (WhatsAppProtocolCrypto.kt). |
| 14 | * Robolectric: the decrypt-failure path logs via Android Log, so the whole class |
| 15 | * runs under Robolectric. Happy paths are behavior-identical on JVM. |
| 16 | */ |
| 17 | @RunWith(AndroidJUnit4::class) |
| 18 | class W5Slice1PollVoteTest { |
| 19 | |
| 20 | @Test |
| 21 | fun pollVote_buildThenDecrypt_returnsOptionHashes() { |
| 22 | val secret = ByteArray(32) { (it + 9).toByte() } |
| 23 | val hashes = listOf( |
| 24 | WhatsAppProtocol.sha256("Alpha".toByteArray()), |
| 25 | WhatsAppProtocol.sha256("Beta".toByteArray()), |
| 26 | ) |
| 27 | val msg = WhatsAppProtocol.buildPollVoteMessage( |
| 28 | chatJid = "[email protected]", |
| 29 | pollMessageId = "POLL1", |
| 30 | pollCreatorJid = "[email protected]", |
| 31 | pollFromMe = false, |
| 32 | voterJid = "[email protected]", |
| 33 | optionHashes = hashes, |
| 34 | pollSecret = secret, |
| 35 | ) |
| 36 | assertTrue(msg.hasPollUpdateMessage()) |
| 37 | val update = msg.pollUpdateMessage |
| 38 | assertEquals("POLL1", update.pollCreationMessageKey.id) |
| 39 | assertEquals("[email protected]", update.pollCreationMessageKey.participant) |
| 40 | assertTrue(update.senderTimestampMS > 0) |
| 41 | |
| 42 | val back = WhatsAppProtocol.decryptPollVote(update, "POLL1", "[email protected]", "[email protected]", secret) |
| 43 | assertTrue(back != null) |
| 44 | assertEquals(2, back!!.size) |
| 45 | assertTrue(back[0].contentEquals(hashes[0])) |
| 46 | assertTrue(back[1].contentEquals(hashes[1])) |
| 47 | } |
| 48 | |
| 49 | @Test |
| 50 | fun pollVote_wrongSecret_returnsNull() { |
| 51 | val secret = ByteArray(32) { 1 } |
| 52 | val msg = WhatsAppProtocol.buildPollVoteMessage( |
| 53 | "[email protected]", "P1", "[email protected]", false, "[email protected]", |
| 54 | listOf(WhatsAppProtocol.sha256("A".toByteArray())), secret, |
| 55 | ) |
| 56 | assertEquals( |
| 57 | null, |
| 58 | WhatsAppProtocol.decryptPollVote( |
| 59 | msg.pollUpdateMessage, "P1", "[email protected]", "[email protected]", ByteArray(32) { 2 }, |
| 60 | ), |
| 61 | ) |
| 62 | } |
| 63 | |
| 64 | @Test |
| 65 | fun pollVote_fromMeGroup_omitsParticipant() { |
| 66 | val msg = WhatsAppProtocol.buildPollVoteMessage( |
| 67 | "[email protected]", "P2", "[email protected]", true, "[email protected]", |
| 68 | listOf(WhatsAppProtocol.sha256("A".toByteArray())), ByteArray(32) { 3 }, |
| 69 | ) |
| 70 | assertTrue(!msg.pollUpdateMessage.pollCreationMessageKey.hasParticipant()) |
| 71 | } |
| 72 | |
| 73 | } |