From d09626782dcdf0c84d531ca1ff0c0951c8a4e349 Mon Sep 17 00:00:00 2001 From: Paul Schaub Date: Fri, 20 Jan 2023 15:16:47 +0100 Subject: [PATCH] Unify tests by turning password-protected keys into variable --- ...ternalDetachedSignVerifyRoundTripTest.java | 56 +++++++-------- .../src/test/java/sop/external/TestKeys.java | 71 +++++++++++++++++++ .../external/UnsupportedSubcommandTest.java | 2 +- 3 files changed, 100 insertions(+), 29 deletions(-) diff --git a/external-sop/src/test/java/sop/external/ExternalDetachedSignVerifyRoundTripTest.java b/external-sop/src/test/java/sop/external/ExternalDetachedSignVerifyRoundTripTest.java index 5dca76d..63638cc 100644 --- a/external-sop/src/test/java/sop/external/ExternalDetachedSignVerifyRoundTripTest.java +++ b/external-sop/src/test/java/sop/external/ExternalDetachedSignVerifyRoundTripTest.java @@ -67,24 +67,12 @@ public class ExternalDetachedSignVerifyRoundTripTest extends AbstractExternalSOP } @Test - public void signVerifyWithFreshEncryptedKey() throws IOException { - ignoreIf("sqop", Is.leq, "0.26.1"); // --with-key-password not supported - + public void signVerifyWithEncryptedKey() throws IOException { byte[] message = "Hello, World!\n".getBytes(StandardCharsets.UTF_8); - byte[] keyPassword = "sw0rdf1sh".getBytes(StandardCharsets.UTF_8); - byte[] key = getSop().generateKey() - .userId("Alice ") - .withKeyPassword(keyPassword) - .generate() - .getBytes(); - - byte[] cert = getSop().extractCert() - .key(key) - .getBytes(); byte[] signature = getSop().detachedSign() - .key(key) - .withKeyPassword(keyPassword) + .key(TestKeys.PASSWORD_PROTECTED_KEY.getBytes(StandardCharsets.UTF_8)) + .withKeyPassword(TestKeys.PASSWORD) .data(message) .toByteArrayAndResult() .getBytes(); @@ -92,7 +80,7 @@ public class ExternalDetachedSignVerifyRoundTripTest extends AbstractExternalSOP assertArrayStartsWith(signature, BEGIN_PGP_SIGNATURE_BYTES); List verificationList = getSop().detachedVerify() - .cert(cert) + .cert(TestKeys.PASSWORD_PROTECTED_CERT.getBytes(StandardCharsets.UTF_8)) .signatures(signature) .data(message); @@ -169,24 +157,36 @@ public class ExternalDetachedSignVerifyRoundTripTest extends AbstractExternalSOP @Test - public void signVerifyWithFreshEncryptedKeyWithoutPassphraseFails() throws IOException { + public void signVerifyWithEncryptedKeyWithoutPassphraseFails() { ignoreIf("sqop", Is.leq, "0.27.2"); // does not return exit code 67 for encrypted keys without passphrase - byte[] keyPassword = "sw0rdf1sh".getBytes(StandardCharsets.UTF_8); - byte[] key = getSop().generateKey() - .userId("Alice ") - .withKeyPassword(keyPassword) - .generate() - .getBytes(); - - byte[] message = "Hello, World!\n".getBytes(StandardCharsets.UTF_8); - assertThrows(SOPGPException.KeyIsProtected.class, () -> getSop().detachedSign() - .key(key) - .data(message) + .key(TestKeys.PASSWORD_PROTECTED_KEY.getBytes(StandardCharsets.UTF_8)) + .data("Hello, World!\n".getBytes(StandardCharsets.UTF_8)) .toByteArrayAndResult() .getBytes()); } + @Test + public void signWithProtectedKeyAndMultiplePassphrasesTest() + throws IOException { + byte[] message = "Hello, World!\n".getBytes(StandardCharsets.UTF_8); + + byte[] signature = getSop().sign() + .key(TestKeys.PASSWORD_PROTECTED_KEY.getBytes(StandardCharsets.UTF_8)) + .withKeyPassword("wrong") + .withKeyPassword(TestKeys.PASSWORD) // correct + .withKeyPassword("wrong2") + .data(message) + .toByteArrayAndResult() + .getBytes(); + + assertFalse(getSop().verify() + .cert(TestKeys.PASSWORD_PROTECTED_CERT.getBytes(StandardCharsets.UTF_8)) + .signatures(signature) + .data(message) + .isEmpty()); + } + } diff --git a/external-sop/src/test/java/sop/external/TestKeys.java b/external-sop/src/test/java/sop/external/TestKeys.java index 1a2bb94..b7f73c3 100644 --- a/external-sop/src/test/java/sop/external/TestKeys.java +++ b/external-sop/src/test/java/sop/external/TestKeys.java @@ -304,4 +304,75 @@ public class TestKeys { "1FkOSekLi8WNMdUx3XMyvP8nJ65P2Q==\n" + "=Xj8h\n" + "-----END PGP PRIVATE KEY BLOCK-----\n"; + + public static final String PASSWORD_PROTECTED_KEY = "-----BEGIN PGP PRIVATE KEY BLOCK-----\n" + + "Comment: FC63 688A 5E69 8C29 40AF 7029 7C62 2B00 D459 2657\n" + + "Comment: Password Protected \n" + + "\n" + + "xYYEY8qfmxYJKwYBBAHaRw8BAQdAv5atAPgP3WOvjoeEGAXIpX+k9LbX1+roEQQE\n" + + "WaQfbMv+CQMI7d4yuArkBqz/J/UllaSoHN2kYdJE4Biiqgto2d39B8JRCrb0LSeX\n" + + "25TolXynV3bdiTsVKtnNOOcCzP09kDMu8uCMpregFrMdI511iR+dysLAEQQfFgoA\n" + + "gwWCY8qfmwWJBZ+mAAMLCQcJEHxiKwDUWSZXRxQAAAAAAB4AIHNhbHRAbm90YXRp\n" + + "b25zLnNlcXVvaWEtcGdwLm9yZ5Rt+kxLFFiFbTaZO2Rbf52K6FEcetqiht8jk9Vt\n" + + "DObSAxUKCAKbAQIeARYhBPxjaIpeaYwpQK9wKXxiKwDUWSZXAABTzQEA9Vy2e5eU\n" + + "dFj+gfwPULtwEJqMpj29eN37J0VfwF1RdW0BAMeXutE1dzL5PdIIX8VJAIv9RXVR\n" + + "lw5TujtjLhr8uzEKzSpQYXNzd29yZCBQcm90ZWN0ZWQgPHByb3RlY3RlZEBvcGVu\n" + + "cGdwLm9yZz7CwBQEExYKAIYFgmPKn5sFiQWfpgADCwkHCRB8YisA1FkmV0cUAAAA\n" + + "AAAeACBzYWx0QG5vdGF0aW9ucy5zZXF1b2lhLXBncC5vcmdTOuFjGL7cyOIVEfem\n" + + "5b/gCJLP6LFKTy3P/gFGRB7VEAMVCggCmQECmwECHgEWIQT8Y2iKXmmMKUCvcCl8\n" + + "YisA1FkmVwAARXsBAP4jwRWnAqEe59BV+0WviYzC8NhKpIjXwRQIM5yD6E90AQCQ\n" + + "wfhqsexB2rVQGw0siW2c/3DUhmnK8osNK5f8iLv5BceGBGPKn5sWCSsGAQQB2kcP\n" + + "AQEHQM/fv1zxwMjruKiq9W7PcMUbcMKQ3lbFdqPtwEJ16Lx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+ "KUCvcCl8YisA1FkmVwAAbRcA/3haEwnnHhitQNbvDs2DqzVvz0QtjEW59ZKFgzX2\n" + + "PUMXAQDJzcz9GoPTqU8hioiSBoQUjN883qv6sJHiEveRyDbMDQ==\n" + + "=xHUd\n" + + "-----END PGP PRIVATE KEY BLOCK-----\n"; + public static final String PASSWORD_PROTECTED_CERT = "-----BEGIN PGP PUBLIC KEY BLOCK-----\n" + + "Comment: FC63 688A 5E69 8C29 40AF 7029 7C62 2B00 D459 2657\n" + + "Comment: Password Protected \n" + + "\n" + + "xjMEY8qfmxYJKwYBBAHaRw8BAQdAv5atAPgP3WOvjoeEGAXIpX+k9LbX1+roEQQE\n" + + "WaQfbMvCwBEEHxYKAIMFgmPKn5sFiQWfpgADCwkHCRB8YisA1FkmV0cUAAAAAAAe\n" + + "ACBzYWx0QG5vdGF0aW9ucy5zZXF1b2lhLXBncC5vcmeUbfpMSxRYhW02mTtkW3+d\n" + + "iuhRHHraoobfI5PVbQzm0gMVCggCmwECHgEWIQT8Y2iKXmmMKUCvcCl8YisA1Fkm\n" + + "VwAAU80BAPVctnuXlHRY/oH8D1C7cBCajKY9vXjd+ydFX8BdUXVtAQDHl7rRNXcy\n" + + "+T3SCF/FSQCL/UV1UZcOU7o7Yy4a/LsxCs0qUGFzc3dvcmQgUHJvdGVjdGVkIDxw\n" + + "cm90ZWN0ZWRAb3BlbnBncC5vcmc+wsAUBBMWCgCGBYJjyp+bBYkFn6YAAwsJBwkQ\n" + + "fGIrANRZJldHFAAAAAAAHgAgc2FsdEBub3RhdGlvbnMuc2VxdW9pYS1wZ3Aub3Jn\n" + + "UzrhYxi+3MjiFRH3puW/4AiSz+ixSk8tz/4BRkQe1RADFQoIApkBApsBAh4BFiEE\n" + + "/GNoil5pjClAr3ApfGIrANRZJlcAAEV7AQD+I8EVpwKhHufQVftFr4mMwvDYSqSI\n" + + "18EUCDOcg+hPdAEAkMH4arHsQdq1UBsNLIltnP9w1IZpyvKLDSuX/Ii7+QXOMwRj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Y BLOCK-----\n"; + public static final String PASSWORD = "sw0rdf1sh"; } diff --git a/external-sop/src/test/java/sop/external/UnsupportedSubcommandTest.java b/external-sop/src/test/java/sop/external/UnsupportedSubcommandTest.java index 8196871..55e13e2 100644 --- a/external-sop/src/test/java/sop/external/UnsupportedSubcommandTest.java +++ b/external-sop/src/test/java/sop/external/UnsupportedSubcommandTest.java @@ -39,7 +39,7 @@ public class UnsupportedSubcommandTest extends AbstractExternalSOPTest { private final String binary; private final Properties environment; - public UnsupportedSubcommandExternal(String binaryName, Properties environment) { + UnsupportedSubcommandExternal(String binaryName, Properties environment) { this.binary = binaryName; this.environment = environment; }