Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / library / e2ee-openpgp / src / test / java / com / vayunmathur / e2ee / PgpApiTest.kt
PgpApiTest.kt189 lines · 9.2 KiBRaw
| 1 | package com.vayunmathur.e2ee |
| 2 | |
| 3 | import java.util.Base64 |
| 4 | import kotlin.test.Test |
| 5 | import kotlin.test.assertContentEquals |
| 6 | import kotlin.test.assertEquals |
| 7 | import kotlin.test.assertFailsWith |
| 8 | import kotlin.test.assertFalse |
| 9 | import kotlin.test.assertNotEquals |
| 10 | import kotlin.test.assertTrue |
| 11 | |
| 12 | /** |
| 13 | * Exercises the real native OpenPGP surface (libe2ee_pgp via host Rust build) |
| 14 | * through the public [Pgp] API: generation, fingerprint/normalize, encrypt, |
| 15 | * signed encrypt + verify, detached signing, armor, and the error mapping |
| 16 | * (native null -> IllegalStateException). |
| 17 | */ |
| 18 | class PgpApiTest { |
| 19 | |
| 20 | private companion object { |
| 21 | const val ALICE_PW = "alice-pw-123" |
| 22 | const val BOB_PW = "bob-pw-456" |
| 23 | val alice: Pgp.Identity by lazy { Pgp.generateIdentity("alice <[email protected]>", ALICE_PW) } |
| 24 | val bob: Pgp.Identity by lazy { Pgp.generateIdentity("bob <[email protected]>", BOB_PW) } |
| 25 | } |
| 26 | |
| 27 | @Test |
| 28 | fun generateIdentityShapeAndUniqueness() { |
| 29 | assertTrue(alice.armoredSecret.contains("PGP PRIVATE KEY BLOCK")) |
| 30 | assertTrue(alice.armoredPublic.contains("PGP PUBLIC KEY BLOCK")) |
| 31 | assertFalse(alice.armoredSecret.contains("PUBLIC KEY BLOCK") && alice.armoredSecret == alice.armoredPublic) |
| 32 | assertEquals(40, alice.fingerprintHex.length) |
| 33 | assertTrue(alice.fingerprintHex.all { it in '0'..'9' || it in 'A'..'F' }, alice.fingerprintHex) |
| 34 | assertNotEquals(alice.fingerprintHex, bob.fingerprintHex) |
| 35 | val other = Pgp.generateIdentity("alice <[email protected]>", ALICE_PW) |
| 36 | assertNotEquals(alice.fingerprintHex, other.fingerprintHex, "each generation makes fresh key material") |
| 37 | assertEquals(alice, alice.copy()) |
| 38 | assertEquals("x", alice.copy(fingerprintHex = "x").fingerprintHex) |
| 39 | } |
| 40 | |
| 41 | @Test |
| 42 | fun fingerprintMatchesGeneratedAndRejectsGarbage() { |
| 43 | assertEquals(alice.fingerprintHex, Pgp.fingerprintHex(alice.armoredPublic)) |
| 44 | assertEquals(bob.fingerprintHex, Pgp.fingerprintHex(bob.armoredPublic)) |
| 45 | val error = assertFailsWith<IllegalStateException> { Pgp.fingerprintHex("not a key") } |
| 46 | assertEquals("Invalid OpenPGP public key", error.message) |
| 47 | assertFailsWith<IllegalStateException> { Pgp.fingerprintHex("") } |
| 48 | assertFailsWith<IllegalStateException> { Pgp.fingerprintHex(alice.armoredSecret) } |
| 49 | } |
| 50 | |
| 51 | @Test |
| 52 | fun normalizeKeyAcceptsArmorAndBinaryAndRejectsGarbage() { |
| 53 | val normalized = Pgp.normalizeKey(alice.armoredPublic.toByteArray(Charsets.UTF_8)) |
| 54 | assertTrue(normalized.contains("PGP PUBLIC KEY BLOCK")) |
| 55 | assertEquals(alice.fingerprintHex, Pgp.fingerprintHex(normalized)) |
| 56 | |
| 57 | // Binary (WKD-style) form: decode the armor body ourselves. |
| 58 | val body = alice.armoredPublic.lines() |
| 59 | .filter { it.isNotBlank() && !it.startsWith("-----") && !it.startsWith("=") && ':' !in it } |
| 60 | .joinToString("") |
| 61 | val binary = Base64.getDecoder().decode(body) |
| 62 | assertEquals(alice.fingerprintHex, Pgp.fingerprintHex(Pgp.normalizeKey(binary))) |
| 63 | |
| 64 | assertFailsWith<IllegalStateException> { Pgp.normalizeKey(byteArrayOf(1, 2, 3)) } |
| 65 | assertFailsWith<IllegalStateException> { Pgp.normalizeKey("junk".toByteArray()) } |
| 66 | assertFailsWith<IllegalStateException> { Pgp.normalizeKey(ByteArray(0)) } |
| 67 | } |
| 68 | |
| 69 | @Test |
| 70 | fun encryptDecryptRoundtripsEmptyUnicodeAndBinary() { |
| 71 | for (plaintext in listOf( |
| 72 | ByteArray(0), |
| 73 | "Hello from SecureMail".toByteArray(Charsets.UTF_8), |
| 74 | "ünïcode ✉ payload".toByteArray(Charsets.UTF_8), |
| 75 | ByteArray(256) { it.toByte() }, |
| 76 | )) { |
| 77 | val raw = Pgp.encrypt(bob.armoredPublic, plaintext) |
| 78 | assertTrue(raw.isNotEmpty()) |
| 79 | assertContentEquals(plaintext, Pgp.decrypt(bob.armoredSecret, BOB_PW, raw)) |
| 80 | } |
| 81 | val raw = Pgp.encrypt(bob.armoredPublic, "secret".toByteArray()) |
| 82 | assertFalse(raw.contentEquals("secret".toByteArray()), "ciphertext must not be the plaintext") |
| 83 | } |
| 84 | |
| 85 | @Test |
| 86 | fun decryptFailuresThrow() { |
| 87 | val raw = Pgp.encrypt(bob.armoredPublic, "secret".toByteArray()) |
| 88 | assertFailsWith<IllegalStateException> { Pgp.decrypt(bob.armoredSecret, "wrong", raw) } |
| 89 | assertFailsWith<IllegalStateException> { Pgp.decrypt(alice.armoredSecret, ALICE_PW, raw) } |
| 90 | assertFailsWith<IllegalStateException> { Pgp.decrypt(bob.armoredSecret, BOB_PW, byteArrayOf(9, 9, 9)) } |
| 91 | assertFailsWith<IllegalStateException> { Pgp.decrypt(bob.armoredSecret, BOB_PW, ByteArray(0)) } |
| 92 | val tampered = raw.copyOf().also { it[it.size / 2] = (it[it.size / 2] + 1).toByte() } |
| 93 | assertFailsWith<IllegalStateException> { Pgp.decrypt(bob.armoredSecret, BOB_PW, tampered) } |
| 94 | assertFailsWith<IllegalStateException> { Pgp.encrypt("garbage", "x".toByteArray()) } |
| 95 | } |
| 96 | |
| 97 | @Test |
| 98 | fun signedEncryptVerifyRoundtripAndUnverifiedPath() { |
| 99 | val plaintext = "signed hello".toByteArray(Charsets.UTF_8) |
| 100 | val raw = Pgp.encryptSigned(bob.armoredPublic, alice.armoredSecret, ALICE_PW, plaintext) |
| 101 | |
| 102 | val verified = Pgp.decryptVerify(bob.armoredSecret, BOB_PW, raw, alice.armoredPublic) |
| 103 | assertContentEquals(plaintext, verified.payload) |
| 104 | assertEquals(alice.fingerprintHex, verified.signerFingerprintHex) |
| 105 | |
| 106 | // Empty verifier: signed but unverified, accepted with empty fingerprint. |
| 107 | val unverified = Pgp.decryptVerify(bob.armoredSecret, BOB_PW, raw, "") |
| 108 | assertContentEquals(plaintext, unverified.payload) |
| 109 | assertEquals("", unverified.signerFingerprintHex) |
| 110 | |
| 111 | // Plain decrypt of a signed message returns just the payload. |
| 112 | assertContentEquals(plaintext, Pgp.decrypt(bob.armoredSecret, BOB_PW, raw)) |
| 113 | } |
| 114 | |
| 115 | @Test |
| 116 | fun unsignedMessagesVerifyWithEmptyFingerprint() { |
| 117 | val plaintext = "unsigned hello".toByteArray(Charsets.UTF_8) |
| 118 | // Empty sender secret disables signing at the JNI boundary. |
| 119 | val raw = Pgp.encryptSigned(bob.armoredPublic, "", ALICE_PW, plaintext) |
| 120 | val verified = Pgp.decryptVerify(bob.armoredSecret, BOB_PW, raw, alice.armoredPublic) |
| 121 | assertContentEquals(plaintext, verified.payload) |
| 122 | assertEquals("", verified.signerFingerprintHex) |
| 123 | |
| 124 | val plain = Pgp.encrypt(bob.armoredPublic, plaintext) |
| 125 | assertEquals("", Pgp.decryptVerify(bob.armoredSecret, BOB_PW, plain, alice.armoredPublic).signerFingerprintHex) |
| 126 | } |
| 127 | |
| 128 | @Test |
| 129 | fun wrongVerifierAndBadSignerFail() { |
| 130 | val carol = Pgp.generateIdentity("carol <[email protected]>", "carol-pw-789") |
| 131 | val raw = Pgp.encryptSigned( |
| 132 | bob.armoredPublic, alice.armoredSecret, ALICE_PW, "signed".toByteArray(), |
| 133 | ) |
| 134 | assertFailsWith<IllegalStateException> { |
| 135 | Pgp.decryptVerify(bob.armoredSecret, BOB_PW, raw, carol.armoredPublic) |
| 136 | } |
| 137 | assertFailsWith<IllegalStateException> { |
| 138 | Pgp.decryptVerify(bob.armoredSecret, BOB_PW, raw, "garbage verifier") |
| 139 | } |
| 140 | assertFailsWith<IllegalStateException> { |
| 141 | Pgp.encryptSigned(bob.armoredPublic, alice.armoredSecret, "wrong-pw", "x".toByteArray()) |
| 142 | } |
| 143 | assertFailsWith<IllegalStateException> { |
| 144 | Pgp.encryptSigned(bob.armoredPublic, "not a secret key", ALICE_PW, "x".toByteArray()) |
| 145 | } |
| 146 | assertFailsWith<IllegalStateException> { |
| 147 | Pgp.encryptSigned("garbage", alice.armoredSecret, ALICE_PW, "x".toByteArray()) |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | @Test |
| 152 | fun detachedSignaturesAreDeterministicAndDataDependent() { |
| 153 | val data = "authenticate me".toByteArray(Charsets.UTF_8) |
| 154 | val sig = Pgp.signDetached(alice.armoredSecret, ALICE_PW, data) |
| 155 | assertTrue(sig.isNotEmpty()) |
| 156 | // Ed25519 signing itself is deterministic, but the packet embeds a |
| 157 | // seconds-resolution creation time: two calls straddling a second |
| 158 | // boundary byte-differ. Compare consecutive pairs (bounded retries) |
| 159 | // so an attempt only fails if a boundary falls between its two calls. |
| 160 | var prev = sig |
| 161 | var resig = Pgp.signDetached(alice.armoredSecret, ALICE_PW, data) |
| 162 | var attempts = 1 |
| 163 | while (!prev.contentEquals(resig) && attempts < 5) { |
| 164 | prev = resig |
| 165 | resig = Pgp.signDetached(alice.armoredSecret, ALICE_PW, data) |
| 166 | attempts++ |
| 167 | } |
| 168 | assertContentEquals(prev, resig) |
| 169 | val other = Pgp.signDetached(alice.armoredSecret, ALICE_PW, "authenticate me!".toByteArray()) |
| 170 | assertFalse(prev.contentEquals(other)) |
| 171 | assertFailsWith<IllegalStateException> { Pgp.signDetached(alice.armoredSecret, "wrong", data) } |
| 172 | assertFailsWith<IllegalStateException> { Pgp.signDetached("garbage", ALICE_PW, data) } |
| 173 | } |
| 174 | |
| 175 | @Test |
| 176 | fun armorMessageFramingDecodesBackToRaw() { |
| 177 | val raw = Pgp.encrypt(bob.armoredPublic, "federation".toByteArray()) |
| 178 | val armored = Pgp.armorMessage(raw) |
| 179 | assertTrue(armored.startsWith("-----BEGIN PGP MESSAGE-----\n")) |
| 180 | assertTrue(armored.trimEnd().endsWith("-----END PGP MESSAGE-----")) |
| 181 | val body = armored.lines() |
| 182 | .filter { it.isNotBlank() && !it.startsWith("-----") && !it.startsWith("=") } |
| 183 | .joinToString("") |
| 184 | assertContentEquals(raw, Base64.getDecoder().decode(body)) |
| 185 | assertTrue(armored.lines().any { it.startsWith("=") }, "CRC24 footer line present") |
| 186 | val empty = Pgp.armorMessage(ByteArray(0)) |
| 187 | assertTrue(empty.startsWith("-----BEGIN PGP MESSAGE-----")) |
| 188 | } |
| 189 | } |