Modern Apps git
git clone https://git.maproj.org/Modern-Apps.git
cov/c3 ▾

Modern-Apps.git / library / e2ee-openpgp / test / java / com / vayunmathur / e2ee / PgpTest.kt

PgpTest.kt94 lines · 3.9 KiBRaw
1package com.vayunmathur.e2ee
2
3import kotlin.test.Test
4import kotlin.test.assertEquals
5import kotlin.test.assertTrue
6import kotlin.test.assertFailsWith
7
8class PgpTest {
9
10 @Test
11 fun generateEncryptDecryptRoundtrip() {
12 val identity = Pgp.generateIdentity("alice <[email protected]>", "correct horse battery")
13 assertTrue(identity.armoredSecret.contains("PGP PRIVATE KEY BLOCK"))
14 assertTrue(identity.armoredPublic.contains("PGP PUBLIC KEY BLOCK"))
15 assertEquals(40, identity.fingerprintHex.length)
16
17 val plaintext = "Hello from SecureMail".toByteArray(Charsets.UTF_8)
18 val raw = Pgp.encrypt(identity.armoredPublic, plaintext)
19 assertTrue(raw.isNotEmpty())
20
21 val back = Pgp.decrypt(identity.armoredSecret, "correct horse battery", raw)
22 assertEquals(plaintext.toList(), back.toList())
23 }
24
25 @Test
26 fun wrongPassphraseFails() {
27 val identity = Pgp.generateIdentity("alice <[email protected]>", "correct horse battery")
28 val raw = Pgp.encrypt(identity.armoredPublic, "secret".toByteArray(Charsets.UTF_8))
29 assertFailsWith<IllegalStateException> {
30 Pgp.decrypt(identity.armoredSecret, "wrong passphrase", raw)
31 }
32 }
33
34 @Test
35 fun fingerprintStable() {
36 val identity = Pgp.generateIdentity("alice <[email protected]>", "correct horse battery")
37 assertEquals(identity.fingerprintHex, Pgp.fingerprintHex(identity.armoredPublic))
38 }
39
40 @Test
41 fun armorMessageRoundtrip() {
42 val identity = Pgp.generateIdentity("alice <[email protected]>", "correct horse battery")
43 val raw = Pgp.encrypt(identity.armoredPublic, "federation".toByteArray(Charsets.UTF_8))
44 val armored = Pgp.armorMessage(raw)
45 assertTrue(armored.startsWith("-----BEGIN PGP MESSAGE-----"))
46 assertTrue(armored.contains("-----END PGP MESSAGE-----"))
47 }
48
49 @Test
50 fun signedEncryptVerifyRoundtrip() {
51 val alice = Pgp.generateIdentity("alice <[email protected]>", "alice-pw-123")
52 val bob = Pgp.generateIdentity("bob <[email protected]>", "bob-pw-456")
53 val plaintext = "signed hello".toByteArray(Charsets.UTF_8)
54 val raw = Pgp.encryptSigned(
55 bob.armoredPublic, alice.armoredSecret, "alice-pw-123", plaintext,
56 )
57 assertTrue(raw.isNotEmpty())
58 val verified = Pgp.decryptVerify(
59 bob.armoredSecret, "bob-pw-456", raw, alice.armoredPublic,
60 )
61 assertEquals(plaintext.toList(), verified.payload.toList())
62 assertEquals(alice.fingerprintHex, verified.signerFingerprintHex)
63 }
64
65 @Test
66 fun unsignedMessageVerifiesEmpty() {
67 val alice = Pgp.generateIdentity("alice <[email protected]>", "alice-pw-123")
68 val bob = Pgp.generateIdentity("bob <[email protected]>", "bob-pw-456")
69 // Empty sender secret = encrypt-only, same as encrypt().
70 val raw = Pgp.encryptSigned(
71 bob.armoredPublic, "", "alice-pw-123",
72 "unsigned hello".toByteArray(Charsets.UTF_8),
73 )
74 val verified = Pgp.decryptVerify(
75 bob.armoredSecret, "bob-pw-456", raw, alice.armoredPublic,
76 )
77 assertEquals("unsigned hello".toByteArray(Charsets.UTF_8).toList(), verified.payload.toList())
78 assertTrue(verified.signerFingerprintHex.isEmpty())
79 }
80
81 @Test
82 fun wrongVerifierKeyFails() {
83 val alice = Pgp.generateIdentity("alice <[email protected]>", "alice-pw-123")
84 val bob = Pgp.generateIdentity("bob <[email protected]>", "bob-pw-456")
85 val carol = Pgp.generateIdentity("carol <[email protected]>", "carol-pw-789")
86 val raw = Pgp.encryptSigned(
87 bob.armoredPublic, alice.armoredSecret, "alice-pw-123",
88 "signed hello".toByteArray(Charsets.UTF_8),
89 )
90 assertFailsWith<IllegalStateException> {
91 Pgp.decryptVerify(bob.armoredSecret, "bob-pw-456", raw, carol.armoredPublic)
92 }
93 }
94}