mirror of
https://codeberg.org/PGPainless/sop-java.git
synced 2025-09-12 19:59:42 +02:00
Move testfixtures to own artifact
This commit is contained in:
parent
ca65cbe668
commit
b3b8da4e35
30 changed files with 29 additions and 8 deletions
|
@ -0,0 +1,235 @@
|
|||
// SPDX-FileCopyrightText: 2024 Paul Schaub <vanitasvitae@fsfe.org>
|
||||
//
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package sop.testsuite.assertions;
|
||||
|
||||
import sop.exception.SOPGPException;
|
||||
|
||||
import java.util.function.IntSupplier;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotEquals;
|
||||
|
||||
/**
|
||||
* DSL for testing the return values of SOP method calls.
|
||||
*/
|
||||
public class SopExecutionAssertions {
|
||||
|
||||
/**
|
||||
* Assert that the execution of the given function returns 0.
|
||||
*
|
||||
* @param function function to execute
|
||||
*/
|
||||
public static void assertSuccess(IntSupplier function) {
|
||||
assertEquals(0, function.getAsInt());
|
||||
}
|
||||
|
||||
/**
|
||||
* Assert that the execution of the given function returns a generic error with error code 1.
|
||||
*
|
||||
* @param function function to execute.
|
||||
*/
|
||||
public static void assertGenericError(IntSupplier function) {
|
||||
assertEquals(1, function.getAsInt());
|
||||
}
|
||||
|
||||
/**
|
||||
* Assert that the execution of the given function returns a non-zero error code.
|
||||
*
|
||||
* @param function function to execute
|
||||
*/
|
||||
public static void assertAnyError(IntSupplier function) {
|
||||
assertNotEquals(0, function.getAsInt());
|
||||
}
|
||||
|
||||
/**
|
||||
* Assert that the execution of the given function returns error code 3
|
||||
* (which corresponds to {@link sop.exception.SOPGPException.NoSignature}).
|
||||
*
|
||||
* @param function function to execute.
|
||||
*/
|
||||
public static void assertNoSignature(IntSupplier function) {
|
||||
assertEquals(SOPGPException.NoSignature.EXIT_CODE, function.getAsInt());
|
||||
}
|
||||
|
||||
/**
|
||||
* Assert that the execution of the given function returns error code 13
|
||||
* (which corresponds to {@link sop.exception.SOPGPException.UnsupportedAsymmetricAlgo}).
|
||||
*
|
||||
* @param function function to execute.
|
||||
*/
|
||||
public static void assertUnsupportedAsymmetricAlgo(IntSupplier function) {
|
||||
assertEquals(SOPGPException.UnsupportedAsymmetricAlgo.EXIT_CODE, function.getAsInt());
|
||||
}
|
||||
|
||||
/**
|
||||
* Assert that the execution of the given function returns error code 17
|
||||
* (which corresponds to {@link sop.exception.SOPGPException.CertCannotEncrypt}).
|
||||
*
|
||||
* @param function function to execute.
|
||||
*/
|
||||
public static void assertCertCannotEncrypt(IntSupplier function) {
|
||||
assertEquals(SOPGPException.CertCannotEncrypt.EXIT_CODE, function.getAsInt());
|
||||
}
|
||||
|
||||
/**
|
||||
* Assert that the execution of the given function returns error code 19
|
||||
* (which corresponds to {@link sop.exception.SOPGPException.MissingArg}).
|
||||
*
|
||||
* @param function function to execute.
|
||||
*/
|
||||
public static void assertMissingArg(IntSupplier function) {
|
||||
assertEquals(SOPGPException.MissingArg.EXIT_CODE, function.getAsInt());
|
||||
}
|
||||
|
||||
/**
|
||||
* Assert that the execution of the given function returns error code 23
|
||||
* (which corresponds to {@link sop.exception.SOPGPException.IncompleteVerification}).
|
||||
*
|
||||
* @param function function to execute.
|
||||
*/
|
||||
public static void assertIncompleteVerification(IntSupplier function) {
|
||||
assertEquals(SOPGPException.IncompleteVerification.EXIT_CODE, function.getAsInt());
|
||||
}
|
||||
|
||||
/**
|
||||
* Assert that the execution of the given function returns error code 29
|
||||
* (which corresponds to {@link sop.exception.SOPGPException.CannotDecrypt}).
|
||||
*
|
||||
* @param function function to execute.
|
||||
*/
|
||||
public static void assertCannotDecrypt(IntSupplier function) {
|
||||
assertEquals(SOPGPException.CannotDecrypt.EXIT_CODE, function.getAsInt());
|
||||
}
|
||||
|
||||
/**
|
||||
* Assert that the execution of the given function returns error code 31
|
||||
* (which corresponds to {@link sop.exception.SOPGPException.PasswordNotHumanReadable}).
|
||||
*
|
||||
* @param function function to execute.
|
||||
*/
|
||||
public static void assertPasswordNotHumanReadable(IntSupplier function) {
|
||||
assertEquals(SOPGPException.PasswordNotHumanReadable.EXIT_CODE, function.getAsInt());
|
||||
}
|
||||
|
||||
/**
|
||||
* Assert that the execution of the given function returns error code 37
|
||||
* (which corresponds to {@link sop.exception.SOPGPException.UnsupportedOption}).
|
||||
*
|
||||
* @param function function to execute.
|
||||
*/
|
||||
public static void assertUnsupportedOption(IntSupplier function) {
|
||||
assertEquals(SOPGPException.UnsupportedOption.EXIT_CODE, function.getAsInt());
|
||||
}
|
||||
|
||||
/**
|
||||
* Assert that the execution of the given function returns error code 41
|
||||
* (which corresponds to {@link sop.exception.SOPGPException.BadData}).
|
||||
*
|
||||
* @param function function to execute.
|
||||
*/
|
||||
public static void assertBadData(IntSupplier function) {
|
||||
assertEquals(SOPGPException.BadData.EXIT_CODE, function.getAsInt());
|
||||
}
|
||||
|
||||
/**
|
||||
* Assert that the execution of the given function returns error code 53
|
||||
* (which corresponds to {@link sop.exception.SOPGPException.ExpectedText}).
|
||||
*
|
||||
* @param function function to execute.
|
||||
*/
|
||||
public static void assertExpectedText(IntSupplier function) {
|
||||
assertEquals(SOPGPException.ExpectedText.EXIT_CODE, function.getAsInt());
|
||||
}
|
||||
|
||||
/**
|
||||
* Assert that the execution of the given function returns error code 59
|
||||
* (which corresponds to {@link sop.exception.SOPGPException.OutputExists}).
|
||||
*
|
||||
* @param function function to execute.
|
||||
*/
|
||||
public static void assertOutputExists(IntSupplier function) {
|
||||
assertEquals(SOPGPException.OutputExists.EXIT_CODE, function.getAsInt());
|
||||
}
|
||||
|
||||
/**
|
||||
* Assert that the execution of the given function returns error code 61
|
||||
* (which corresponds to {@link sop.exception.SOPGPException.MissingInput}).
|
||||
*
|
||||
* @param function function to execute.
|
||||
*/
|
||||
public static void assertMissingInput(IntSupplier function) {
|
||||
assertEquals(SOPGPException.MissingInput.EXIT_CODE, function.getAsInt());
|
||||
}
|
||||
|
||||
/**
|
||||
* Assert that the execution of the given function returns error code 67
|
||||
* (which corresponds to {@link sop.exception.SOPGPException.KeyIsProtected}).
|
||||
*
|
||||
* @param function function to execute.
|
||||
*/
|
||||
public static void assertKeyIsProtected(IntSupplier function) {
|
||||
assertEquals(SOPGPException.KeyIsProtected.EXIT_CODE, function.getAsInt());
|
||||
}
|
||||
|
||||
/**
|
||||
* Assert that the execution of the given function returns error code 69
|
||||
* (which corresponds to {@link sop.exception.SOPGPException.UnsupportedSubcommand}).
|
||||
*
|
||||
* @param function function to execute.
|
||||
*/
|
||||
public static void assertUnsupportedSubcommand(IntSupplier function) {
|
||||
assertEquals(SOPGPException.UnsupportedSubcommand.EXIT_CODE, function.getAsInt());
|
||||
}
|
||||
|
||||
/**
|
||||
* Assert that the execution of the given function returns error code 71
|
||||
* (which corresponds to {@link sop.exception.SOPGPException.UnsupportedSpecialPrefix}).
|
||||
*
|
||||
* @param function function to execute.
|
||||
*/
|
||||
public static void assertUnsupportedSpecialPrefix(IntSupplier function) {
|
||||
assertEquals(SOPGPException.UnsupportedSpecialPrefix.EXIT_CODE, function.getAsInt());
|
||||
}
|
||||
|
||||
/**
|
||||
* Assert that the execution of the given function returns error code 73
|
||||
* (which corresponds to {@link sop.exception.SOPGPException.AmbiguousInput}).
|
||||
*
|
||||
* @param function function to execute.
|
||||
*/
|
||||
public static void assertAmbiguousInput(IntSupplier function) {
|
||||
assertEquals(SOPGPException.AmbiguousInput.EXIT_CODE, function.getAsInt());
|
||||
}
|
||||
|
||||
/**
|
||||
* Assert that the execution of the given function returns error code 79
|
||||
* (which corresponds to {@link sop.exception.SOPGPException.KeyCannotSign}).
|
||||
*
|
||||
* @param function function to execute.
|
||||
*/
|
||||
public static void assertKeyCannotSign(IntSupplier function) {
|
||||
assertEquals(SOPGPException.KeyCannotSign.EXIT_CODE, function.getAsInt());
|
||||
}
|
||||
|
||||
/**
|
||||
* Assert that the execution of the given function returns error code 83
|
||||
* (which corresponds to {@link sop.exception.SOPGPException.IncompatibleOptions}).
|
||||
*
|
||||
* @param function function to execute.
|
||||
*/
|
||||
public static void assertIncompatibleOptions(IntSupplier function) {
|
||||
assertEquals(SOPGPException.IncompatibleOptions.EXIT_CODE, function.getAsInt());
|
||||
}
|
||||
|
||||
/**
|
||||
* Assert that the execution of the given function returns error code 89
|
||||
* (which corresponds to {@link sop.exception.SOPGPException.UnsupportedProfile}).
|
||||
*
|
||||
* @param function function to execute.
|
||||
*/
|
||||
public static void assertUnsupportedProfile(IntSupplier function) {
|
||||
assertEquals(SOPGPException.UnsupportedProfile.EXIT_CODE, function.getAsInt());
|
||||
}
|
||||
}
|
|
@ -0,0 +1,71 @@
|
|||
// SPDX-FileCopyrightText: 2023 Paul Schaub <vanitasvitae@fsfe.org>
|
||||
//
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package sop.testsuite.assertions;
|
||||
|
||||
import sop.Verification;
|
||||
import sop.enums.SignatureMode;
|
||||
import sop.testsuite.JUtils;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
public final class VerificationAssert {
|
||||
|
||||
private final Verification verification;
|
||||
|
||||
public static VerificationAssert assertThatVerification(Verification verification) {
|
||||
return new VerificationAssert(verification);
|
||||
}
|
||||
|
||||
private VerificationAssert(Verification verification) {
|
||||
this.verification = verification;
|
||||
}
|
||||
|
||||
public VerificationAssert issuedBy(String signingKeyFingerprint, String primaryFingerprint) {
|
||||
return isBySigningKey(signingKeyFingerprint)
|
||||
.issuedBy(primaryFingerprint);
|
||||
}
|
||||
|
||||
public VerificationAssert issuedBy(String primaryFingerprint) {
|
||||
assertEquals(primaryFingerprint, verification.getSigningCertFingerprint());
|
||||
return this;
|
||||
}
|
||||
|
||||
public VerificationAssert isBySigningKey(String signingKeyFingerprint) {
|
||||
assertEquals(signingKeyFingerprint, verification.getSigningKeyFingerprint());
|
||||
return this;
|
||||
}
|
||||
|
||||
public VerificationAssert isCreatedAt(Date creationDate) {
|
||||
JUtils.assertDateEquals(creationDate, verification.getCreationTime());
|
||||
return this;
|
||||
}
|
||||
|
||||
public VerificationAssert hasDescription(String description) {
|
||||
assertEquals(description, verification.getDescription().get());
|
||||
return this;
|
||||
}
|
||||
|
||||
public VerificationAssert hasDescriptionOrNull(String description) {
|
||||
if (verification.getDescription().isEmpty()) {
|
||||
return this;
|
||||
}
|
||||
|
||||
return hasDescription(description);
|
||||
}
|
||||
|
||||
public VerificationAssert hasMode(SignatureMode mode) {
|
||||
assertEquals(mode, verification.getSignatureMode().get());
|
||||
return this;
|
||||
}
|
||||
|
||||
public VerificationAssert hasModeOrNull(SignatureMode mode) {
|
||||
if (verification.getSignatureMode().isEmpty()) {
|
||||
return this;
|
||||
}
|
||||
return hasMode(mode);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,70 @@
|
|||
// SPDX-FileCopyrightText: 2023 Paul Schaub <vanitasvitae@fsfe.org>
|
||||
//
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package sop.testsuite.assertions;
|
||||
|
||||
import sop.Verification;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.junit.jupiter.api.Assertions.fail;
|
||||
|
||||
public final class VerificationListAssert {
|
||||
|
||||
private final List<Verification> verificationList = new ArrayList<>();
|
||||
|
||||
private VerificationListAssert(List<Verification> verifications) {
|
||||
this.verificationList.addAll(verifications);
|
||||
}
|
||||
|
||||
public static VerificationListAssert assertThatVerificationList(List<Verification> verifications) {
|
||||
return new VerificationListAssert(verifications);
|
||||
}
|
||||
|
||||
public VerificationListAssert isEmpty() {
|
||||
assertTrue(verificationList.isEmpty());
|
||||
return this;
|
||||
}
|
||||
|
||||
public VerificationListAssert isNotEmpty() {
|
||||
assertFalse(verificationList.isEmpty());
|
||||
return this;
|
||||
}
|
||||
|
||||
public VerificationListAssert sizeEquals(int size) {
|
||||
assertEquals(size, verificationList.size());
|
||||
return this;
|
||||
}
|
||||
|
||||
public VerificationAssert hasSingleItem() {
|
||||
sizeEquals(1);
|
||||
return VerificationAssert.assertThatVerification(verificationList.get(0));
|
||||
}
|
||||
|
||||
public VerificationListAssert containsVerificationByCert(String primaryFingerprint) {
|
||||
for (Verification verification : verificationList) {
|
||||
if (primaryFingerprint.equals(verification.getSigningCertFingerprint())) {
|
||||
return this;
|
||||
}
|
||||
}
|
||||
fail("No verification was issued by certificate " + primaryFingerprint);
|
||||
return this;
|
||||
}
|
||||
|
||||
public VerificationListAssert containsVerificationBy(String signingKeyFingerprint, String primaryFingerprint) {
|
||||
for (Verification verification : verificationList) {
|
||||
if (primaryFingerprint.equals(verification.getSigningCertFingerprint()) &&
|
||||
signingKeyFingerprint.equals(verification.getSigningKeyFingerprint())) {
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
fail("No verification was issued by key " + signingKeyFingerprint + " of cert " + primaryFingerprint);
|
||||
return this;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
// SPDX-FileCopyrightText: 2023 Paul Schaub <vanitasvitae@fsfe.org>
|
||||
//
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
/**
|
||||
* DSL for assertions on SOP objects.
|
||||
*/
|
||||
package sop.testsuite.assertions;
|
Loading…
Add table
Add a link
Reference in a new issue