Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / communicate-dev / src / test / java / com / vayunmathur / communicate / signal / SignalAuthAndServiceDataTest.kt
SignalAuthAndServiceDataTest.kt199 lines · 8.9 KiBRaw
| 1 | package com.vayunmathur.communicate.signal |
| 2 | |
| 3 | import com.vayunmathur.communicate.data.signal.SignalAuthData |
| 4 | import com.vayunmathur.communicate.data.signal.SignalPollOptionData |
| 5 | import com.vayunmathur.communicate.data.signal.SignalReaction |
| 6 | import com.vayunmathur.communicate.data.signal.SignalServiceData |
| 7 | import com.vayunmathur.communicate.data.signal.registration.SignalPqPreKey |
| 8 | import com.vayunmathur.communicate.data.signal.registration.SignalRegistrationKeys |
| 9 | import org.signal.libsignal.protocol.ecc.ECPrivateKey |
| 10 | import org.junit.runner.RunWith |
| 11 | import org.robolectric.RobolectricTestRunner |
| 12 | import kotlin.test.Test |
| 13 | import kotlin.test.assertEquals |
| 14 | import kotlin.test.assertFalse |
| 15 | import kotlin.test.assertNotNull |
| 16 | import kotlin.test.assertNull |
| 17 | import kotlin.test.assertTrue |
| 18 | |
| 19 | /** |
| 20 | * Wave 1 (w6 slice 2/6): [SignalAuthData] effective-getters, [SignalServiceData] round-trip, |
| 21 | * [SignalPqPreKey] wire shape, [SignalRegistrationKeys] scaffold. Pure JVM. |
| 22 | */ |
| 23 | @RunWith(RobolectricTestRunner::class) |
| 24 | class SignalAuthAndServiceDataTest { |
| 25 | |
| 26 | // ---- SignalAuthData effective getters ---- |
| 27 | |
| 28 | @Test |
| 29 | fun effectiveGetters_preferDualIdentityFields() { |
| 30 | val auth = SignalAuthData( |
| 31 | phoneNumber = "+15551234567", |
| 32 | identityPrivateKey = "legacy-priv", identityPublicKey = "legacy-pub", |
| 33 | registrationId = 1, signedPreKeyId = 5, |
| 34 | signedPreKeyPublic = "legacy-spk-pub", signedPreKeyPrivate = "legacy-spk-priv", |
| 35 | signedPreKeySignature = "legacy-spk-sig", |
| 36 | pqLastResortKeyId = 9, pqLastResortPublic = "legacy-pq-pub", |
| 37 | pqLastResortSecret = "legacy-pq-sec", pqLastResortSignature = "legacy-pq-sig", |
| 38 | aciIdentityPrivateKey = "aci-priv", aciIdentityPublicKey = "aci-pub", |
| 39 | aciRegistrationId = 11, aciSignedPreKeyId = 12, |
| 40 | aciSignedPreKeyPublic = "aci-spk-pub", aciSignedPreKeyPrivate = "aci-spk-priv", |
| 41 | aciSignedPreKeySignature = "aci-spk-sig", |
| 42 | aciPqLastResortKeyId = 13, aciPqLastResortPublic = "aci-pq-pub", |
| 43 | aciPqLastResortSecret = "aci-pq-sec", aciPqLastResortSignature = "aci-pq-sig", |
| 44 | ) |
| 45 | assertEquals("aci-priv", auth.effectiveAciPrivate()) |
| 46 | assertEquals("aci-pub", auth.effectiveAciPublic()) |
| 47 | assertEquals(11, auth.effectiveAciRegId()) |
| 48 | assertEquals(12, auth.effectiveAciSignedId()) |
| 49 | assertEquals("aci-spk-pub", auth.effectiveAciSignedPub()) |
| 50 | assertEquals("aci-spk-priv", auth.effectiveAciSignedPriv()) |
| 51 | assertEquals("aci-spk-sig", auth.effectiveAciSignedSig()) |
| 52 | assertEquals(13, auth.effectiveAciPqId()) |
| 53 | assertEquals("aci-pq-pub", auth.effectiveAciPqPub()) |
| 54 | assertEquals("aci-pq-sec", auth.effectiveAciPqSec()) |
| 55 | assertEquals("aci-pq-sig", auth.effectiveAciPqSig()) |
| 56 | } |
| 57 | |
| 58 | @Test |
| 59 | fun effectiveGetters_fallBackToLegacyFields() { |
| 60 | val auth = SignalAuthData( |
| 61 | phoneNumber = "+1", |
| 62 | identityPrivateKey = "legacy-priv", identityPublicKey = "legacy-pub", |
| 63 | registrationId = 3, signedPreKeyId = 4, |
| 64 | signedPreKeyPublic = "spk-pub", signedPreKeyPrivate = "spk-priv", |
| 65 | signedPreKeySignature = "spk-sig", |
| 66 | pqLastResortKeyId = 8, pqLastResortPublic = "pq-pub", |
| 67 | pqLastResortSecret = "pq-sec", pqLastResortSignature = "pq-sig", |
| 68 | ) |
| 69 | assertEquals("legacy-priv", auth.effectiveAciPrivate()) |
| 70 | assertEquals("legacy-pub", auth.effectiveAciPublic()) |
| 71 | assertEquals(3, auth.effectiveAciRegId()) |
| 72 | assertEquals(4, auth.effectiveAciSignedId()) |
| 73 | assertEquals("spk-pub", auth.effectiveAciSignedPub()) |
| 74 | assertEquals("spk-priv", auth.effectiveAciSignedPriv()) |
| 75 | assertEquals("spk-sig", auth.effectiveAciSignedSig()) |
| 76 | assertEquals(8, auth.effectiveAciPqId()) |
| 77 | assertEquals("pq-pub", auth.effectiveAciPqPub()) |
| 78 | assertEquals("pq-sec", auth.effectiveAciPqSec()) |
| 79 | assertEquals("pq-sig", auth.effectiveAciPqSig()) |
| 80 | } |
| 81 | |
| 82 | // ---- SignalServiceData ---- |
| 83 | |
| 84 | @Test |
| 85 | fun serviceData_roundTripsAllFields() { |
| 86 | val sd = SignalServiceData( |
| 87 | senderName = "Ada", senderId = "aci-1", |
| 88 | reactions = listOf(SignalReaction("👍", 2)), |
| 89 | isEdited = true, isRevoked = false, |
| 90 | pollQuestion = "lunch?", pollOptions = listOf(SignalPollOptionData("a", 1, listOf("v"))), |
| 91 | quotedMessageId = "m1", quotedBody = "qb", quotedSender = "qs", |
| 92 | isGroup = true, groupParticipantCount = 3, |
| 93 | mediaUrl = "https://x", mediaMime = "image/png", mediaName = "n.png", |
| 94 | ) |
| 95 | val parsed = SignalServiceData.parse(sd.serialize())!! |
| 96 | assertEquals(sd, parsed) |
| 97 | } |
| 98 | |
| 99 | @Test |
| 100 | fun serviceData_parseRejectsBlankAndGarbage() { |
| 101 | assertNull(SignalServiceData.parse(null)) |
| 102 | assertNull(SignalServiceData.parse("")) |
| 103 | assertNull(SignalServiceData.parse(" ")) |
| 104 | assertNull(SignalServiceData.parse("{not json")) |
| 105 | } |
| 106 | |
| 107 | @Test |
| 108 | fun serviceData_defaultsAreEmpty() { |
| 109 | val parsed = SignalServiceData.parse(SignalServiceData().serialize())!! |
| 110 | assertEquals(SignalServiceData(), parsed) |
| 111 | assertFalse(parsed.isEdited) |
| 112 | assertTrue(parsed.reactions.isEmpty()) |
| 113 | } |
| 114 | |
| 115 | // ---- SignalPqPreKey ---- |
| 116 | |
| 117 | @Test |
| 118 | fun pqSigningInput_isIdentity() { |
| 119 | val buf = byteArrayOf(1, 2, 3) |
| 120 | assertTrue(SignalPqPreKey.signingInput(buf).contentEquals(buf)) |
| 121 | } |
| 122 | |
| 123 | @Test |
| 124 | fun pqGenerate_wireShapeAndSignature() { |
| 125 | val ikp = org.signal.libsignal.protocol.IdentityKeyPair.generate() |
| 126 | val gen = SignalPqPreKey.generateWithECPrivate(ikp.privateKey, keyId = 7) |
| 127 | assertEquals(7, gen.keyId) |
| 128 | // 1569B = 0x08 tag || 1568 raw Kyber1024 public. |
| 129 | assertEquals(1569, gen.publicKey.size) |
| 130 | assertEquals(0x08.toByte(), gen.publicKey[0]) |
| 131 | assertTrue(gen.secretKey.isNotEmpty()) |
| 132 | // Signature is over the full 1569B including the tag, and verifies |
| 133 | // under the identity key. |
| 134 | assertEquals(64, gen.signature.size) |
| 135 | assertTrue(ikp.publicKey.publicKey.verifySignature(gen.publicKey, gen.signature)) |
| 136 | // The ByteArray overload agrees on shape. |
| 137 | val gen2 = SignalPqPreKey.generate(ikp.privateKey.serialize(), keyId = 7) |
| 138 | assertEquals(1569, gen2.publicKey.size) |
| 139 | assertEquals(0x08.toByte(), gen2.publicKey[0]) |
| 140 | } |
| 141 | |
| 142 | @Test |
| 143 | fun pqGenerateWithECPrivate_matchesGenerate() { |
| 144 | val identity = ECPrivateKey.generate() |
| 145 | val a = SignalPqPreKey.generate(identity.serialize(), 1) |
| 146 | val b = SignalPqPreKey.generateWithECPrivate(identity, 1) |
| 147 | assertEquals(1569, a.publicKey.size) |
| 148 | assertEquals(1569, b.publicKey.size) |
| 149 | assertEquals(0x08.toByte(), b.publicKey[0]) |
| 150 | assertEquals(64, b.signature.size) |
| 151 | } |
| 152 | |
| 153 | // ---- SignalRegistrationKeys ---- |
| 154 | |
| 155 | @Test |
| 156 | fun registrationScaffold_hasDualIdentitiesAndValidShapes() { |
| 157 | val keys = SignalRegistrationKeys.generate("+15551234567") |
| 158 | val auth = keys.authScaffold |
| 159 | assertEquals("+15551234567", auth.phoneNumber) |
| 160 | assertFalse(auth.registered) |
| 161 | // Dual identities present and distinct. |
| 162 | assertTrue(auth.aciIdentityPublicKey.isNotEmpty()) |
| 163 | assertTrue(auth.pniIdentityPublicKey.isNotEmpty()) |
| 164 | assertTrue(auth.aciIdentityPublicKey != auth.pniIdentityPublicKey) |
| 165 | // Registration ids in 1..16383. |
| 166 | assertTrue(auth.aciRegistrationId in 1..0x3FFF) |
| 167 | assertTrue(auth.pniRegistrationId in 1..0x3FFF) |
| 168 | // Signed prekeys verify under their identity keys. |
| 169 | val aciPub = android.util.Base64.decode(auth.aciIdentityPublicKey, android.util.Base64.NO_WRAP) |
| 170 | val aciSpkPub = android.util.Base64.decode(auth.aciSignedPreKeyPublic, android.util.Base64.NO_WRAP) |
| 171 | val aciSpkSig = android.util.Base64.decode(auth.aciSignedPreKeySignature, android.util.Base64.NO_WRAP) |
| 172 | assertEquals(33, aciPub.size) |
| 173 | assertEquals(33, aciSpkPub.size) |
| 174 | assertTrue( |
| 175 | org.signal.libsignal.protocol.ecc.ECPublicKey(aciPub).verifySignature(aciSpkPub, aciSpkSig), |
| 176 | ) |
| 177 | // Kyber pubs carry the 0x08 tag. |
| 178 | val kyberPub = android.util.Base64.decode(auth.aciPqLastResortPublic, android.util.Base64.NO_WRAP) |
| 179 | assertEquals(1569, kyberPub.size) |
| 180 | assertEquals(0x08.toByte(), kyberPub[0]) |
| 181 | // UAK decodes to exactly 16 bytes. |
| 182 | val uak = android.util.Base64.decode(auth.unidentifiedAccessKey, android.util.Base64.NO_WRAP) |
| 183 | assertEquals(16, uak.size) |
| 184 | assertTrue(auth.password.isNotEmpty()) |
| 185 | assertNull(auth.registrationLock) |
| 186 | // Legacy mirrors match ACI. |
| 187 | assertEquals(auth.aciIdentityPublicKey, auth.identityPublicKey) |
| 188 | assertEquals(auth.aciRegistrationId, auth.registrationId) |
| 189 | } |
| 190 | |
| 191 | @Test |
| 192 | fun registrationScaffold_signedScaffoldIsPreRegistration() { |
| 193 | val auth = SignalRegistrationKeys.generate("+14155552671").authScaffold |
| 194 | assertNotNull(auth.verificationSessionId == null) |
| 195 | assertEquals(1, auth.deviceId) |
| 196 | assertEquals("", auth.aci) |
| 197 | assertEquals("", auth.pni) |
| 198 | } |
| 199 | } |