Instanciate Policy instead of reusing singleton

This commit is contained in:
Paul Schaub 2024-02-13 13:37:57 +01:00
parent 4a0eef0924
commit 7ff63142b4
Signed by: vanitasvitae
GPG Key ID: 62BEE9264BF17311
5 changed files with 35 additions and 21 deletions

View File

@ -17,43 +17,43 @@ public class PolicySetterTest {
@Test
public void testSetCertificationSignatureHashAlgorithmPolicy_NullFails() {
Policy policy = Policy.getInstance();
Policy policy = new Policy();
assertThrows(NullPointerException.class, () -> policy.setCertificationSignatureHashAlgorithmPolicy(null));
}
@Test
public void testSetDataSignatureHashAlgorithmPolicy_NullFails() {
Policy policy = Policy.getInstance();
Policy policy = new Policy();
assertThrows(NullPointerException.class, () -> policy.setDataSignatureHashAlgorithmPolicy(null));
}
@Test
public void testSetRevocationSignatureHashAlgorithmPolicy_NullFails() {
Policy policy = Policy.getInstance();
Policy policy = new Policy();
assertThrows(NullPointerException.class, () -> policy.setRevocationSignatureHashAlgorithmPolicy(null));
}
@Test
public void testSetSymmetricKeyEncryptionAlgorithmPolicy_NullFails() {
Policy policy = Policy.getInstance();
Policy policy = new Policy();
assertThrows(NullPointerException.class, () -> policy.setSymmetricKeyEncryptionAlgorithmPolicy(null));
}
@Test
public void testSetSymmetricKeyDecryptionAlgorithmPolicy_NullFails() {
Policy policy = Policy.getInstance();
Policy policy = new Policy();
assertThrows(NullPointerException.class, () -> policy.setSymmetricKeyDecryptionAlgorithmPolicy(null));
}
@Test
public void testSetCompressionAlgorithmPolicy_NullFails() {
Policy policy = Policy.getInstance();
Policy policy = new Policy();
assertThrows(NullPointerException.class, () -> policy.setCompressionAlgorithmPolicy(null));
}
@Test
public void testSetPublicKeyAlgorithmPolicy_NullFails() {
Policy policy = Policy.getInstance();
Policy policy = new Policy();
assertThrows(NullPointerException.class, () -> policy.setPublicKeyAlgorithmPolicy(null));
}

View File

@ -61,7 +61,7 @@ public class SignatureSubpacketsUtilTest {
.done();
PGPSignature expirationSig = SignaturePicker.pickCurrentUserIdCertificationSignature(
secretKeys, "Expire", Policy.getInstance(), new Date());
secretKeys, "Expire", PGPainless.getPolicy(), new Date());
PGPPublicKey notTheRightKey = PGPainless.inspectKeyRing(secretKeys).getSigningSubkeys().get(0);
assertThrows(IllegalArgumentException.class, () ->

View File

@ -28,7 +28,7 @@ class GenerateOpenPgpKeyTest {
fun test() {
val date = DateUtil.parseUTCDate("2020-04-01 10:00:00 UTC")
val key =
GenerateOpenPgpKey(Policy.getInstance(), date)
GenerateOpenPgpKey(Policy(), date)
.buildV4Key(KeyType.EDDSA(EdDSACurve._Ed25519), listOf(KeyFlag.CERTIFY_OTHER))
.addUserId("Alice")
.addEncryptionSubkey(KeyType.XDH(XDHSpec._X25519))
@ -40,7 +40,7 @@ class GenerateOpenPgpKeyTest {
@Test
fun minimal() {
val key =
GenerateOpenPgpKey(Policy.getInstance())
GenerateOpenPgpKey(Policy())
.buildV4Key(KeyType.EDDSA(EdDSACurve._Ed25519), listOf(KeyFlag.CERTIFY_OTHER))
.build()
println(PGPainless.asciiArmor(key))
@ -49,7 +49,7 @@ class GenerateOpenPgpKeyTest {
@Test
fun minimalWithUserId() {
val key =
GenerateOpenPgpKey(Policy.getInstance())
GenerateOpenPgpKey(Policy())
.buildV4Key(KeyType.EDDSA(EdDSACurve._Ed25519), listOf(KeyFlag.CERTIFY_OTHER))
.addUserId("Alice <alice@pgpainless.org>")
.build()
@ -59,7 +59,7 @@ class GenerateOpenPgpKeyTest {
@Test
fun primaryKeyMustBeCertificationCapable() {
assertThrows<IllegalArgumentException> {
GenerateOpenPgpKey(Policy.getInstance())
GenerateOpenPgpKey(Policy())
// XDH is not signing/certification capable
.buildV4Key(KeyType.XDH(XDHSpec._X25519))
}
@ -68,7 +68,7 @@ class GenerateOpenPgpKeyTest {
@Test
fun encryptionSubkeyMustBeEncryptionCapable() {
val builder =
GenerateOpenPgpKey(Policy.getInstance()).buildV4Key(KeyType.EDDSA(EdDSACurve._Ed25519))
GenerateOpenPgpKey(Policy()).buildV4Key(KeyType.EDDSA(EdDSACurve._Ed25519))
assertThrows<IllegalArgumentException> {
builder.addEncryptionSubkey(KeyType.EDDSA(EdDSACurve._Ed25519))
@ -78,7 +78,7 @@ class GenerateOpenPgpKeyTest {
@Test
fun signingSubkeysMustBeSigningCapable() {
val builder =
GenerateOpenPgpKey(Policy.getInstance()).buildV4Key(KeyType.EDDSA(EdDSACurve._Ed25519))
GenerateOpenPgpKey(Policy()).buildV4Key(KeyType.EDDSA(EdDSACurve._Ed25519))
assertThrows<IllegalArgumentException> {
builder.addSigningSubkey(KeyType.XDH(XDHSpec._X25519))
@ -108,7 +108,7 @@ class GenerateOpenPgpKeyTest {
@Test
fun testKeyGenerationWithJPEGAttribute() {
val key =
GenerateOpenPgpKey(Policy.getInstance())
GenerateOpenPgpKey(Policy())
.buildV4Key(KeyType.EDDSA(EdDSACurve._Ed25519))
.addJpegImage(requireResource("suzanne.jpg"))
.build()

View File

@ -24,7 +24,7 @@ class MalformedKeyGenerationTest {
fun malformedPrimaryUserIdSubpacket() {
val userId = "Alice <alice@pgpainless.org>"
val key =
GenerateOpenPgpKey(Policy.getInstance())
GenerateOpenPgpKey(Policy())
.buildV4Key(KeyType.EDDSA(EdDSACurve._Ed25519))
.addUserId(
userId,
@ -42,7 +42,7 @@ class MalformedKeyGenerationTest {
@Test
fun malformedExportableSubpacket() {
val key =
GenerateOpenPgpKey(Policy.getInstance())
GenerateOpenPgpKey(Policy())
.buildV4Key(KeyType.EDDSA(EdDSACurve._Ed25519))
.addUserId(
"Alice <alice@pgpainless.org>",
@ -60,7 +60,7 @@ class MalformedKeyGenerationTest {
@Test
fun malformedRevocableSubpacket() {
val key =
GenerateOpenPgpKey(Policy.getInstance())
GenerateOpenPgpKey(Policy())
.buildV4Key(KeyType.EDDSA(EdDSACurve._Ed25519))
.addUserId(
"Alice <alice@pgpainless.org>",
@ -77,7 +77,7 @@ class MalformedKeyGenerationTest {
@Test
fun primaryUserIdOnDirectKeySig() {
val policy = Policy.getInstance()
val policy = Policy()
val key =
GenerateOpenPgpKey(policy)
.buildV4Key(

View File

@ -112,13 +112,27 @@ class OpenPgpKeyGeneratorTest {
@Test
fun `key generation with too weak PK algorithms fails`() {
val policy = Policy.getInstance()
val policy = Policy()
policy.publicKeyAlgorithmPolicy = Policy.PublicKeyAlgorithmPolicy(
buildMap { put(PublicKeyAlgorithm.RSA_GENERAL, 3072) }
)
assertThrows<IllegalArgumentException> {
buildV4(policy).setPrimaryKey(KeyType.RSA(RsaLength._2048))
buildV4(policy)
// opinionated builder verifies PK parameters
.setPrimaryKey(KeyType.RSA(RsaLength._2048)) // too weak
}
}
@Test
fun `unopionionated key generation with too weak PK algorithm does not fail`() {
val policy = Policy()
policy.publicKeyAlgorithmPolicy = Policy.PublicKeyAlgorithmPolicy(
buildMap { put(PublicKeyAlgorithm.RSA_GENERAL, 3072) }
)
buildV4(policy)
.unopinionated() // unopinionated builder allows for non-compliant configurations
.setPrimaryKey(KeyType.RSA(RsaLength._2048))
}
}