1
0
Fork 0
mirror of https://github.com/pgpainless/pgpainless.git synced 2024-12-23 03:17:58 +01:00

Add tests for PGPSecretKeyRingExtensions

This commit is contained in:
Paul Schaub 2024-03-05 20:54:15 +01:00
parent 60ea98df00
commit dfbc56fe24
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311

View file

@ -0,0 +1,63 @@
// SPDX-FileCopyrightText: 2024 Paul Schaub <vanitasvitae@fsfe.org>
//
// SPDX-License-Identifier: Apache-2.0
package org.pgpainless.bouncycastle.extensions
import org.junit.jupiter.api.Assertions.assertFalse
import org.junit.jupiter.api.Assertions.assertNotNull
import org.junit.jupiter.api.Assertions.assertNull
import org.junit.jupiter.api.Assertions.assertTrue
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.assertThrows
import org.pgpainless.PGPainless
import org.pgpainless.encryption_signing.ProducerOptions
import org.pgpainless.encryption_signing.SigningOptions
import org.pgpainless.key.TestKeys
import org.pgpainless.key.protection.SecretKeyRingProtector
import java.io.ByteArrayOutputStream
class PGPSecretKeyRingExtensionsTest {
@Test
fun testHasPgpSecretKeyRing() {
val key = TestKeys.getEmilSecretKeyRing()
assertTrue(key.hasSecretKey(TestKeys.EMIL_KEY_ID))
assertTrue(key.hasSecretKey(TestKeys.EMIL_FINGERPRINT))
assertFalse(key.hasSecretKey(TestKeys.ROMEO_KEY_ID))
assertFalse(key.hasSecretKey(TestKeys.ROMEO_FINGERPRINT))
}
@Test
fun testRequireSecretKey() {
val key = TestKeys.getEmilSecretKeyRing()
assertNotNull(key.requireSecretKey(TestKeys.EMIL_KEY_ID))
assertNotNull(key.requireSecretKey(TestKeys.EMIL_FINGERPRINT))
assertThrows<NoSuchElementException> {
key.requireSecretKey(TestKeys.ROMEO_KEY_ID)
}
assertThrows<NoSuchElementException> {
key.requireSecretKey(TestKeys.ROMEO_FINGERPRINT)
}
}
@Test
fun testGetSecretKeyForSignature() {
val key = TestKeys.getEmilSecretKeyRing()
val signer = PGPainless.encryptAndOrSign()
.onOutputStream(ByteArrayOutputStream())
.withOptions(
ProducerOptions.sign(SigningOptions.get()
.addDetachedSignature(SecretKeyRingProtector.unprotectedKeys(), key)
)
)
signer.write("Hello, World!\n".toByteArray())
signer.close()
val sig = signer.result.detachedSignatures.first().value.first()
assertNotNull(key.getSecretKeyFor(sig))
assertNull(TestKeys.getRomeoSecretKeyRing().getSecretKeyFor(sig))
}
}