diff --git a/pom.xml b/pom.xml index fc892cfb..46fcfe30 100644 --- a/pom.xml +++ b/pom.xml @@ -44,6 +44,18 @@ + + + + org.junit + junit-bom + 5.10.0 + pom + import + + + + @@ -52,19 +64,7 @@ guava 32.1.2-jre - - - junit - junit - 4.13.2 - test - - - org.slf4j - slf4j-simple - 2.0.9 - test - + com.google.inject guice @@ -96,6 +96,21 @@ 4.0.4 runtime + + + + + org.junit.jupiter + junit-jupiter + test + + + org.slf4j + slf4j-simple + 2.0.9 + test + + diff --git a/src/test/java/xades4j/algorithms/XPath2FilterTransformTest.java b/src/test/java/xades4j/algorithms/XPath2FilterTransformTest.java index 179b8e54..98e52a66 100644 --- a/src/test/java/xades4j/algorithms/XPath2FilterTransformTest.java +++ b/src/test/java/xades4j/algorithms/XPath2FilterTransformTest.java @@ -1,28 +1,29 @@ /* * XAdES4j - A Java library for generation and verification of XAdES signatures. * Copyright (C) 2012 Luis Goncalves. - * + * * XAdES4j is free software; you can redistribute it and/or modify it under * the terms of the GNU Lesser General Public License as published by the Free * Software Foundation; either version 3 of the License, or any later version. - * + * * XAdES4j is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more * details. - * + * * You should have received a copy of the GNU Lesser General Public License along * with XAdES4j. If not, see . */ package xades4j.algorithms; import java.util.List; + +import org.junit.jupiter.api.Test; import xades4j.algorithms.XPath2FilterTransform.XPath2Filter; -import org.junit.Test; -import static org.junit.Assert.*; + +import static org.junit.jupiter.api.Assertions.*; /** - * * @author Luís */ public class XPath2FilterTransformTest @@ -53,10 +54,12 @@ public void testCreation() assertEquals("subtract", f.getFilterType()); } - @Test(expected = UnsupportedOperationException.class) + @Test public void testChangeFiltersListFails() { XPath2FilterTransform t = XPath2Filter.intersect("1").union("2"); - t.getFilters().clear(); + assertThrows(UnsupportedOperationException.class, () -> { + t.getFilters().clear(); + }); } } diff --git a/src/test/java/xades4j/production/KeyInfoBuilderTest.java b/src/test/java/xades4j/production/KeyInfoBuilderTest.java index 53ca3e63..15f736d7 100644 --- a/src/test/java/xades4j/production/KeyInfoBuilderTest.java +++ b/src/test/java/xades4j/production/KeyInfoBuilderTest.java @@ -1,16 +1,16 @@ /* * XAdES4j - A Java library for generation and verification of XAdES signatures. * Copyright (C) 2011 Luis Goncalves. - * + * * XAdES4j is free software; you can redistribute it and/or modify it under * the terms of the GNU Lesser General Public License as published by the Free * Software Foundation; either version 3 of the License, or any later version. - * + * * XAdES4j is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more * details. - * + * * You should have received a copy of the GNU Lesser General Public License along * with XAdES4j. If not, see . */ @@ -26,16 +26,16 @@ import org.apache.xml.security.keys.content.x509.XMLX509Certificate; import org.apache.xml.security.signature.SignedInfo; import org.apache.xml.security.signature.XMLSignature; -import org.junit.Assert; -import org.junit.BeforeClass; -import org.junit.Test; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; import org.w3c.dom.Document; import org.w3c.dom.Node; import xades4j.providers.impl.DefaultX500NameStyleProvider; import xades4j.utils.SignatureServicesTestBase; +import static org.junit.jupiter.api.Assertions.*; + /** - * * @author Luís */ public class KeyInfoBuilderTest extends SignatureServicesTestBase @@ -44,7 +44,7 @@ public class KeyInfoBuilderTest extends SignatureServicesTestBase private static X509Certificate intermCertificate; private static List certificates; - @BeforeClass + @BeforeAll public static void setUpClass() throws Exception { org.apache.xml.security.Init.init(); @@ -58,8 +58,6 @@ public static void setUpClass() throws Exception @Test public void testIncludeCertAndKey() throws Exception { - System.out.println("includeCertAndKey"); - KeyInfoBuilder keyInfoBuilder = new KeyInfoBuilder( new BasicSignatureOptions().includeSigningCertificate(SigningCertificateMode.SIGNING_CERTIFICATE).includePublicKey(true), new SignatureAlgorithms(), @@ -69,20 +67,18 @@ public void testIncludeCertAndKey() throws Exception keyInfoBuilder.buildKeyInfo(certificates, xmlSignature); - Assert.assertEquals(0, xmlSignature.getSignedInfo().getLength()); + assertEquals(0, xmlSignature.getSignedInfo().getLength()); KeyValue kv = xmlSignature.getKeyInfo().itemKeyValue(0); - Assert.assertTrue(kv.getPublicKey().getAlgorithm().startsWith("RSA")); + assertTrue(kv.getPublicKey().getAlgorithm().startsWith("RSA")); XMLX509Certificate x509Certificate = xmlSignature.getKeyInfo().itemX509Data(0).itemCertificate(0); - Assert.assertEquals(testCertificate, x509Certificate.getX509Certificate()); + assertEquals(testCertificate, x509Certificate.getX509Certificate()); } - - @Test + + @Test public void testIncludeCertChain() throws Exception { - System.out.println("includeCertChain"); - KeyInfoBuilder keyInfoBuilder = new KeyInfoBuilder( new BasicSignatureOptions().includeSigningCertificate(SigningCertificateMode.FULL_CHAIN), new SignatureAlgorithms(), @@ -92,23 +88,21 @@ public void testIncludeCertChain() throws Exception keyInfoBuilder.buildKeyInfo(certificates, xmlSignature); - Assert.assertEquals(0, xmlSignature.getSignedInfo().getLength()); - - Assert.assertEquals(1, xmlSignature.getKeyInfo().lengthX509Data()); - Assert.assertEquals(2, xmlSignature.getKeyInfo().itemX509Data(0).lengthCertificate()); + assertEquals(0, xmlSignature.getSignedInfo().getLength()); + + assertEquals(1, xmlSignature.getKeyInfo().lengthX509Data()); + assertEquals(2, xmlSignature.getKeyInfo().itemX509Data(0).lengthCertificate()); XMLX509Certificate x509Certificate = xmlSignature.getKeyInfo().itemX509Data(0).itemCertificate(0); - Assert.assertEquals(testCertificate, x509Certificate.getX509Certificate()); - + assertEquals(testCertificate, x509Certificate.getX509Certificate()); + x509Certificate = xmlSignature.getKeyInfo().itemX509Data(0).itemCertificate(1); - Assert.assertEquals(intermCertificate, x509Certificate.getX509Certificate()); + assertEquals(intermCertificate, x509Certificate.getX509Certificate()); } - + @Test public void testIncludeIssuerSerial() throws Exception { - System.out.println("includeIssuerSerial"); - KeyInfoBuilder keyInfoBuilder = new KeyInfoBuilder( new BasicSignatureOptions().includeIssuerSerial(true), new SignatureAlgorithms(), @@ -118,15 +112,13 @@ public void testIncludeIssuerSerial() throws Exception keyInfoBuilder.buildKeyInfo(certificates, xmlSignature); - Assert.assertEquals(1, xmlSignature.getKeyInfo().lengthX509Data()); - Assert.assertEquals(1, xmlSignature.getKeyInfo().itemX509Data(0).lengthIssuerSerial()); + assertEquals(1, xmlSignature.getKeyInfo().lengthX509Data()); + assertEquals(1, xmlSignature.getKeyInfo().itemX509Data(0).lengthIssuerSerial()); } - + @Test public void testIncludeSubjectName() throws Exception { - System.out.println("includeSubjectName"); - KeyInfoBuilder keyInfoBuilder = new KeyInfoBuilder( new BasicSignatureOptions().includeSubjectName(true), new SignatureAlgorithms(), @@ -136,15 +128,13 @@ public void testIncludeSubjectName() throws Exception keyInfoBuilder.buildKeyInfo(certificates, xmlSignature); - Assert.assertEquals(1, xmlSignature.getKeyInfo().lengthX509Data()); - Assert.assertEquals(1, xmlSignature.getKeyInfo().itemX509Data(0).lengthSubjectName()); + assertEquals(1, xmlSignature.getKeyInfo().lengthX509Data()); + assertEquals(1, xmlSignature.getKeyInfo().itemX509Data(0).lengthSubjectName()); } @Test public void testSignKeyInfo() throws Exception { - System.out.println("signKeyInfo"); - KeyInfoBuilder keyInfoBuilder = new KeyInfoBuilder( new BasicSignatureOptions().signKeyInfo(true), new SignatureAlgorithms(), @@ -155,10 +145,10 @@ public void testSignKeyInfo() throws Exception keyInfoBuilder.buildKeyInfo(certificates, xmlSignature); SignedInfo signedInfo = xmlSignature.getSignedInfo(); - Assert.assertEquals(1, signedInfo.getLength()); + assertEquals(1, signedInfo.getLength()); Node refNode = signedInfo.item(0).getContentsBeforeTransformation().getSubNode(); - Assert.assertSame(xmlSignature.getKeyInfo().getElement(), refNode); + assertSame(xmlSignature.getKeyInfo().getElement(), refNode); } private XMLSignature getTestSignature() throws Exception diff --git a/src/test/java/xades4j/production/OtherSignerTests.java b/src/test/java/xades4j/production/OtherSignerTests.java index fb0613b1..8adccd23 100644 --- a/src/test/java/xades4j/production/OtherSignerTests.java +++ b/src/test/java/xades4j/production/OtherSignerTests.java @@ -28,7 +28,7 @@ import xades4j.algorithms.EnvelopedSignatureTransform; import xades4j.algorithms.ExclusiveCanonicalXMLWithoutComments; import xades4j.properties.DataObjectDesc; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.w3c.dom.Document; import org.w3c.dom.Element; import xades4j.properties.QualifyingProperty; @@ -49,7 +49,7 @@ import static org.apache.xml.security.c14n.Canonicalizer.ALGO_ID_C14N_OMIT_COMMENTS; import static org.apache.xml.security.signature.XMLSignature.ALGO_ID_SIGNATURE_RSA_SHA512; import static org.apache.xml.security.utils.Constants.*; -import static org.junit.Assert.*; +import static org.junit.jupiter.api.Assertions.*; /** * @author Luís @@ -59,8 +59,6 @@ public class OtherSignerTests extends SignerTestBase @Test public void testSignAndAppendAsFirstChild() throws Exception { - System.out.println("signAndAppendAsFirstChild"); - Document doc = getTestDocument(); Element root = doc.getDocumentElement(); XadesSigner signer = new XadesBesSigningProfile(keyingProviderMy).newSigner(); @@ -94,8 +92,6 @@ public void testSignWithManifest() throws Exception @Test public void testSignUsingCustomResolver() throws Exception { - System.out.println("signUsingCustomResolver"); - Document doc = getNewDocument(); XadesSigner signer = new XadesBesSigningProfile(keyingProviderMy).newSigner(); MyResolverSpi resolverSpi = new MyResolverSpi(); diff --git a/src/test/java/xades4j/production/PtCcSignerTests.java b/src/test/java/xades4j/production/PtCcSignerTests.java index f96d6335..40f7d2ec 100644 --- a/src/test/java/xades4j/production/PtCcSignerTests.java +++ b/src/test/java/xades4j/production/PtCcSignerTests.java @@ -1,6 +1,6 @@ package xades4j.production; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.w3c.dom.Document; import org.w3c.dom.Element; import xades4j.providers.impl.DirectPasswordProvider; @@ -17,7 +17,6 @@ public class PtCcSignerTests extends SignerTestBase @Test public void testSignTPtCC() throws Exception { - System.out.println("signTPtCitizenCard"); assumePtCcPkcs11(); Document doc = getTestDocument(); @@ -36,7 +35,6 @@ public void testSignTPtCC() throws Exception @Test public void testSignBesPtCCWindowsMy() throws Exception { - System.out.println("signBESPtCitizenCardWindowsMy"); assumeWindows(); assumePtCcPkcs11(); diff --git a/src/test/java/xades4j/production/SignedDataObjectsProcessorTest.java b/src/test/java/xades4j/production/SignedDataObjectsProcessorTest.java index 78791fbd..dc29ff1f 100644 --- a/src/test/java/xades4j/production/SignedDataObjectsProcessorTest.java +++ b/src/test/java/xades4j/production/SignedDataObjectsProcessorTest.java @@ -30,22 +30,22 @@ import org.apache.xml.security.signature.ObjectContainer; import org.apache.xml.security.signature.Reference; import org.apache.xml.security.signature.XMLSignature; -import org.junit.BeforeClass; -import org.junit.Test; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; import org.w3c.dom.Document; import xades4j.properties.DataObjectDesc; import xades4j.utils.DOMHelper; import xades4j.utils.SignatureServicesTestBase; import xades4j.utils.StringUtils; -import static org.junit.Assert.*; +import static org.junit.jupiter.api.Assertions.*; /** * @author Luís */ public class SignedDataObjectsProcessorTest extends SignatureServicesTestBase { - @BeforeClass + @BeforeAll public static void setUpClass() { Init.initXMLSec(); @@ -54,8 +54,6 @@ public static void setUpClass() @Test public void testProcess() throws Exception { - System.out.println("process"); - Document doc = getNewDocument(); SignedDataObjects dataObjsDescs = new SignedDataObjects() @@ -183,8 +181,6 @@ public boolean engineCanResolveURI(ResourceResolverContext context) @Test public void testAddNullReference() throws Exception { - System.out.println("addNullReference"); - Document doc = SignatureServicesTestBase.getNewDocument(); SignedDataObjects dataObjsDescs = new SignedDataObjects() @@ -204,21 +200,14 @@ public void testAddNullReference() throws Exception assertNull(r.getElement().getAttributeNodeNS(Constants.SignatureSpecNS, "URI")); } - @Test(expected = IllegalStateException.class) + @Test public void testAddMultipleNullReferencesFails() throws Exception { - System.out.println("addMultipleNullReferencesFails"); - - Document doc = SignatureServicesTestBase.getNewDocument(); - - SignedDataObjects dataObjsDescs = new SignedDataObjects() - .withSignedDataObject(new AnonymousDataObjectReference("data1".getBytes())) - .withSignedDataObject(new AnonymousDataObjectReference("data2".getBytes())); - - XMLSignature xmlSignature = new XMLSignature(doc, "", XMLSignature.ALGO_ID_SIGNATURE_RSA_SHA256); - xmlSignature.setId("sigId"); - - SignedDataObjectsProcessor processor = new SignedDataObjectsProcessor(new SignatureAlgorithms(), new AllwaysNullAlgsParamsMarshaller()); - processor.process(dataObjsDescs, xmlSignature); + SignedDataObjects dataObjsDescs = new SignedDataObjects(); + assertThrows(IllegalStateException.class, () -> { + dataObjsDescs + .withSignedDataObject(new AnonymousDataObjectReference("data1".getBytes())) + .withSignedDataObject(new AnonymousDataObjectReference("data2".getBytes())); + }); } } diff --git a/src/test/java/xades4j/production/SignerBESTest.java b/src/test/java/xades4j/production/SignerBESTest.java index ed775363..cd558318 100644 --- a/src/test/java/xades4j/production/SignerBESTest.java +++ b/src/test/java/xades4j/production/SignerBESTest.java @@ -19,7 +19,7 @@ import org.apache.xml.security.stax.impl.resourceResolvers.ResolverHttp; import org.apache.xml.security.utils.resolver.implementations.ResolverDirectHTTP; import org.apache.xml.security.utils.resolver.implementations.ResolverLocalFilesystem; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; @@ -51,8 +51,6 @@ public SignerBESTest() @Test public void testSignBES() throws Exception { - System.out.println("signBES"); - Document doc1 = getTestDocument(); Document doc2 = getDocument("content.xml"); Node objectContent = doc1.importNode(doc2.getDocumentElement(), true); @@ -74,8 +72,6 @@ public void testSignBES() throws Exception @Test public void testSignBESWithEllipticCurveKey() throws Exception { - System.out.println("testSignBESWithEllipticCurveKey"); - Document doc = getTestDocument(); Element elemToSign = doc.getDocumentElement(); @@ -90,8 +86,6 @@ public void testSignBESWithEllipticCurveKey() throws Exception @Test public void testSignBESExternalRes() throws Exception { - System.out.println("signBESExternalRes"); - Document doc = getNewDocument(); XadesSigner signer = new XadesBesSigningProfile(keyingProviderNist).with(DEFAULT_TEST_TSA).newSigner(); @@ -109,8 +103,6 @@ public void testSignBESExternalRes() throws Exception @Test public void testSignBESWithCounterSig() throws Exception { - System.out.println("signBESWithCounterSig"); - Document doc = getTestDocument(); Element elemToSign = doc.getDocumentElement(); @@ -139,8 +131,6 @@ public void provideProperties( @Test public void testSignBESDetachedWithXPathAndNamespaces() throws Exception { - System.out.println("signBESDetachedWithXPathAndNamespaces"); - Document doc = getNewDocument(); XadesSigner signer = new XadesBesSigningProfile(keyingProviderMy) diff --git a/src/test/java/xades4j/production/SignerCTest.java b/src/test/java/xades4j/production/SignerCTest.java index 60d99c5c..65d29cf8 100644 --- a/src/test/java/xades4j/production/SignerCTest.java +++ b/src/test/java/xades4j/production/SignerCTest.java @@ -18,7 +18,7 @@ import java.io.FileInputStream; import java.io.InputStream; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.w3c.dom.Document; import org.w3c.dom.Element; import xades4j.providers.impl.HttpTsaConfiguration; @@ -35,8 +35,6 @@ public class SignerCTest extends SignerTestBase @Test public void testSignC() throws Exception { - System.out.println("signC"); - Document doc = getTestDocument(); Element elemToSign = doc.getDocumentElement(); @@ -50,8 +48,6 @@ public void testSignC() throws Exception @Test public void testSignFileDetachedC() throws Exception { - System.out.println("signFileDetachedC"); - ValidationDataProvider vdp = new ValidationDataFromCertValidationProvider(VerifierTestBase.validationProviderNist); XadesSigner signer = new XadesCSigningProfile(keyingProviderNist, vdp).with(DEFAULT_TEST_TSA).newSigner(); diff --git a/src/test/java/xades4j/production/SignerEPESTest.java b/src/test/java/xades4j/production/SignerEPESTest.java index ca355dab..fbd3f5be 100644 --- a/src/test/java/xades4j/production/SignerEPESTest.java +++ b/src/test/java/xades4j/production/SignerEPESTest.java @@ -16,16 +16,8 @@ */ package xades4j.production; -import java.io.ByteArrayInputStream; -import java.util.Arrays; -import java.util.Collection; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.Parameterized; -import org.junit.runners.Parameterized.Parameter; -import org.junit.runners.Parameterized.Parameters; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; import org.w3c.dom.Document; import org.w3c.dom.Element; import xades4j.properties.IdentifierType; @@ -34,35 +26,23 @@ import xades4j.properties.SignaturePolicyIdentifierProperty; import xades4j.providers.SignaturePolicyInfoProvider; +import java.io.ByteArrayInputStream; + /** - * * @author Luís */ -@RunWith(Parameterized.class) public class SignerEPESTest extends SignerTestBase { - @Parameter(0) - public String locationUrl; - @Parameter(1) - public String output; - - @Parameters - public static Collection data() { - Object[][] data = new Object[][] { - { "http://www.example.com/policy", "document.signed.epes_1.xml" }, - { null, "document.signed.epes_2.xml" }}; - return Arrays.asList(data); - } - - @Test - public void testSignEPES() throws Exception + @ParameterizedTest + @CsvSource({ + "http://www.example.com/policy, document.signed.epes_1.xml", + ", document.signed.epes_2.xml" + }) + public void testSignEPES(String locationUrl, String output) throws Exception { - System.out.printf("signEPES: %s", locationUrl); - System.out.println(); - Document doc = getTestDocument(); Element elemToSign = doc.getDocumentElement(); - + SignaturePolicyInfoProvider policyInfoProvider = new SignaturePolicyInfoProvider() { @Override @@ -71,7 +51,7 @@ public SignaturePolicyBase getSignaturePolicy() return new SignaturePolicyIdentifierProperty( new ObjectIdentifier("oid:/1.2.4.0.9.4.5", IdentifierType.OIDAsURI, "Policy description"), new ByteArrayInputStream("Test policy input stream".getBytes())) - .withLocationUrl(locationUrl); + .withLocationUrl(locationUrl); } }; diff --git a/src/test/java/xades4j/production/SignerSpecificTest.java b/src/test/java/xades4j/production/SignerSpecificTest.java index 003ec2c3..08d7172c 100644 --- a/src/test/java/xades4j/production/SignerSpecificTest.java +++ b/src/test/java/xades4j/production/SignerSpecificTest.java @@ -3,14 +3,10 @@ import org.bouncycastle.asn1.ASN1Encodable; import org.bouncycastle.asn1.DERBMPString; import org.bouncycastle.asn1.DERUTF8String; - - import org.bouncycastle.asn1.x500.AttributeTypeAndValue; import org.bouncycastle.asn1.x500.X500Name; import org.bouncycastle.asn1.x500.X500NameBuilder; - import org.bouncycastle.asn1.x500.style.RFC4519Style; - import org.bouncycastle.asn1.x509.BasicConstraints; import org.bouncycastle.asn1.x509.Extension; import org.bouncycastle.asn1.x509.KeyUsage; @@ -19,56 +15,49 @@ import org.bouncycastle.cert.jcajce.JcaX509v3CertificateBuilder; import org.bouncycastle.jce.provider.BouncyCastleProvider; import org.bouncycastle.operator.jcajce.JcaContentSignerBuilder; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.Parameterized; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.MethodSource; import org.w3c.dom.Document; import org.w3c.dom.Element; import xades4j.algorithms.EnvelopedSignatureTransform; import xades4j.properties.DataObjectDesc; - import xades4j.providers.impl.DirectKeyingDataProvider; - import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.math.BigInteger; - import java.security.KeyPair; import java.security.KeyPairGenerator; import java.security.SecureRandom; import java.security.Security; import java.security.cert.X509Certificate; -import java.util.ArrayList; -import java.util.Collection; import java.util.Date; +import java.util.List; import java.util.Random; - /** * @author Artem R. Romanenko * @version 02/04/2018 */ -@RunWith(Parameterized.class) -public class SignerSpecificTest extends SignerTestBase { +public class SignerSpecificTest extends SignerTestBase +{ private static final String NATIONAL_DN_CYRILLIC = "National name '\u043F\u0440\u0438\u043C\u0435\u0440'"; private static final String NATIONAL_DN_ARABIC = "National name '\u0645\u062B\u0627\u0644'"; - @Parameterized.Parameters - public static Collection data() { - ArrayList result = new ArrayList(); - result.add(new ASN1Encodable[]{new DERBMPString(NATIONAL_DN_CYRILLIC)}); - result.add(new ASN1Encodable[]{new DERUTF8String(NATIONAL_DN_CYRILLIC)}); - result.add(new ASN1Encodable[]{new DERBMPString(NATIONAL_DN_ARABIC)}); - result.add(new ASN1Encodable[]{new DERUTF8String(NATIONAL_DN_ARABIC)}); - return result; + public static List data() + { + return List.of( + new DERBMPString(NATIONAL_DN_CYRILLIC), + new DERUTF8String(NATIONAL_DN_CYRILLIC), + new DERBMPString(NATIONAL_DN_ARABIC), + new DERUTF8String(NATIONAL_DN_ARABIC) + ); } - @Parameterized.Parameter - public ASN1Encodable commonName; - - @Test - public void signWithNationalCertificate() throws Exception { + @ParameterizedTest + @MethodSource("data") + public void signWithNationalCertificate(ASN1Encodable commonName) throws Exception + { Security.addProvider(new BouncyCastleProvider()); KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA", BouncyCastleProvider.PROVIDER_NAME); keyGen.initialize(1024, new SecureRandom()); @@ -114,7 +103,5 @@ public void signWithNationalCertificate() throws Exception { String str = new String(baos.toByteArray()); //expected without parsing exception Document doc = parseDocument(new ByteArrayInputStream(baos.toByteArray())); - } - } \ No newline at end of file diff --git a/src/test/java/xades4j/production/SignerTTest.java b/src/test/java/xades4j/production/SignerTTest.java index ef26484a..9a55a3ee 100644 --- a/src/test/java/xades4j/production/SignerTTest.java +++ b/src/test/java/xades4j/production/SignerTTest.java @@ -16,7 +16,7 @@ */ package xades4j.production; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.w3c.dom.Document; import org.w3c.dom.Element; import xades4j.algorithms.ExclusiveCanonicalXMLWithoutComments; @@ -33,8 +33,6 @@ public class SignerTTest extends SignerTestBase @Test public void testSignTExclusiveC14NWithoutPolicy() throws Exception { - System.out.println("signTExclusiveC14NWithoutPolicy"); - Document doc = getTestDocument(); Element elemToSign = doc.getDocumentElement(); @@ -54,8 +52,6 @@ public void testSignTExclusiveC14NWithoutPolicy() throws Exception @Test public void testSignTWithPolicy() throws Exception { - System.out.println("signTWithPolicy"); - Document doc = getTestDocument(); Element elemToSign = doc.getDocumentElement(); diff --git a/src/test/java/xades4j/production/SignerTestBase.java b/src/test/java/xades4j/production/SignerTestBase.java index fe9a2e26..e8717a28 100644 --- a/src/test/java/xades4j/production/SignerTestBase.java +++ b/src/test/java/xades4j/production/SignerTestBase.java @@ -19,7 +19,7 @@ import java.io.File; import java.security.KeyStoreException; -import static org.junit.Assume.assumeTrue; +import static org.junit.jupiter.api.Assumptions.assumeTrue; import org.w3c.dom.Document; import xades4j.providers.impl.DirectPasswordProvider; diff --git a/src/test/java/xades4j/production/UncheckedSignerBESTest.java b/src/test/java/xades4j/production/UncheckedSignerBESTest.java index 96fb49c8..c69f99fe 100644 --- a/src/test/java/xades4j/production/UncheckedSignerBESTest.java +++ b/src/test/java/xades4j/production/UncheckedSignerBESTest.java @@ -30,18 +30,19 @@ import xades4j.verification.XAdESVerificationResult; import xades4j.verification.XadesVerificationProfile; -import static org.junit.Assert.assertEquals; - import java.io.FileInputStream; import java.security.KeyStore; import java.security.cert.Certificate; import java.security.cert.CertificateFactory; import org.apache.xml.security.utils.Constants; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.w3c.dom.Document; import org.w3c.dom.Element; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + /** * This class test signing with the keyUsage check disabled. * @@ -58,7 +59,7 @@ public class UncheckedSignerBESTest extends SignerTestBase public UncheckedSignerBESTest() throws Exception { keyingProviderGood = createFileSystemKeyingDataProvider("PKCS12", "unchecked/good.p12", "password", true); - keyingProviderNoSign = createFileSystemKeyingDataProvider("PKCS12", "unchecked/noSignKeyUsage.p12", "password",true); + keyingProviderNoSign = createFileSystemKeyingDataProvider("PKCS12", "unchecked/noSignKeyUsage.p12", "password", true); keyingProviderExp = createFileSystemKeyingDataProvider("PKCS12", "unchecked/expired.p12", "password", true); keyingProviderNyv = createFileSystemKeyingDataProvider("PKCS12", "unchecked/notYetValid.p12", "password", true); validationProvider = genValidationProvider("unchecked/TestCA.cer", "unchecked"); @@ -80,7 +81,7 @@ private void trySignAndVerify(final KeyingDataProvider signProvider, final XadesBesSigningProfile signProfile = new XadesBesSigningProfile(signProvider); final BasicSignatureOptions opts = new BasicSignatureOptions(); opts.checkKeyUsage(false) - .checkCertificateValidity(false); + .checkCertificateValidity(false); signProfile.withBasicSignatureOptions(opts); XadesSigner signer = signProfile.newSigner(); @@ -133,7 +134,6 @@ private CertificateValidationProvider genValidationProvider(final String root, f @Test public void testUncheckedSignBes() throws Exception { - System.out.println("uncheckedSignBes"); CertificateValidationProvider prov = genValidationProvider("my/TestCA.cer", "my"); trySignAndVerify(keyingProviderMy, prov, "document.unchecked.signed.bes.xml"); } @@ -141,39 +141,38 @@ public void testUncheckedSignBes() throws Exception @Test public void testUncheckedSignBesGood() throws Exception { - System.out.println("uncheckedSignBesGood"); trySignAndVerify(keyingProviderGood, validationProvider, "document.unchecked.signed.bes.good.xml"); } - @Test(expected = SigningCertificateKeyUsageException.class) + @Test public void testUncheckedSignBesNoSignKeyUsage() throws Exception { - System.out.println("uncheckedSignBesNoSignKeyUsage"); - trySignAndVerify(keyingProviderNoSign, validationProvider, "document.unchecked.signed.bes.nosign.xml"); + assertThrows(SigningCertificateKeyUsageException.class, () -> { + trySignAndVerify(keyingProviderNoSign, validationProvider, "document.unchecked.signed.bes.nosign.xml"); + }); } @Test public void testUncheckedSignBesNoSignKeyUsageUncheckedVerify() throws Exception { - System.out.println("uncheckedSignBesNoSignKeyUsageUncheckedVerify"); - // same certificate as in testUncheckedSignBesNoSignKeyUsage(), but keyUsage // check disabled during verification trySignAndVerify(keyingProviderNoSign, validationProvider, "document.unchecked.signed.bes.nosign.xml", false); } - @Test(expected = CannotBuildCertificationPathException.class) + @Test public void testUncheckedSignBesExpired() throws Exception { - System.out.println("uncheckedSignBesExpired"); - trySignAndVerify(keyingProviderExp, validationProvider, "document.unchecked.signed.bes.expired.xml"); + assertThrows(CannotBuildCertificationPathException.class, () -> { + trySignAndVerify(keyingProviderExp, validationProvider, "document.unchecked.signed.bes.expired.xml"); + }); } - @Test(expected = CannotBuildCertificationPathException.class) + @Test public void testUncheckedSignBesNyv() throws Exception { - System.out.println("uncheckedSignBesExpired"); - trySignAndVerify(keyingProviderNyv, validationProvider, "document.unchecked.signed.bes.nyv.xml"); + assertThrows(CannotBuildCertificationPathException.class, () -> { + trySignAndVerify(keyingProviderNyv, validationProvider, "document.unchecked.signed.bes.nyv.xml"); + }); } - } diff --git a/src/test/java/xades4j/production/XadesBesSigningProfileTest.java b/src/test/java/xades4j/production/XadesBesSigningProfileTest.java index 059e9a8c..60410421 100644 --- a/src/test/java/xades4j/production/XadesBesSigningProfileTest.java +++ b/src/test/java/xades4j/production/XadesBesSigningProfileTest.java @@ -19,11 +19,12 @@ import java.security.PrivateKey; import java.security.cert.X509Certificate; import java.util.List; -import org.junit.Test; -import static org.junit.Assert.*; +import org.junit.jupiter.api.Test; import xades4j.utils.XadesProfileResolutionException; import xades4j.providers.KeyingDataProvider; +import static org.junit.jupiter.api.Assertions.assertNotNull; + /** * * @author Luís diff --git a/src/test/java/xades4j/production/XadesSignatureFormatExtenderImplTest.java b/src/test/java/xades4j/production/XadesSignatureFormatExtenderImplTest.java index 58437e90..5ce7d29e 100644 --- a/src/test/java/xades4j/production/XadesSignatureFormatExtenderImplTest.java +++ b/src/test/java/xades4j/production/XadesSignatureFormatExtenderImplTest.java @@ -21,7 +21,7 @@ import java.util.Collection; import org.apache.xml.security.signature.XMLSignature; import org.apache.xml.security.utils.Constants; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.NodeList; @@ -41,8 +41,6 @@ public class XadesSignatureFormatExtenderImplTest extends SignerTestBase @Test public void testEnrichSignatureWithNestedCounterSig() throws Exception { - System.out.println("enrichSignatureWithNestedCounterSig"); - Document doc = getDocument("document.signed.bes.cs.xml"); NodeList signatures = doc.getElementsByTagNameNS(Constants.SignatureSpecNS, Constants._TAG_SIGNATURE); // Existing counter signature is the last @@ -64,8 +62,6 @@ public void testEnrichSignatureWithNestedCounterSig() throws Exception @Test public void testEnrichSignatureWithT() throws Exception { - System.out.println("enrichSignatureWithT"); - Document doc = getDocument("document.signed.bes.xml"); Element signatureNode = (Element)doc.getElementsByTagNameNS(Constants.SignatureSpecNS, "Signature").item(0); @@ -83,8 +79,6 @@ public void testEnrichSignatureWithT() throws Exception @Test public void testEnrichSignatureWithA() throws Exception { - System.out.println("enrichSignatureWithA"); - Document doc = getDocument("document.verified.c.xl.xml"); Element signatureNode = (Element)doc.getElementsByTagNameNS(Constants.SignatureSpecNS, "Signature").item(0); diff --git a/src/test/java/xades4j/properties/DataObjectDescTest.java b/src/test/java/xades4j/properties/DataObjectDescTest.java index 3049468f..98876b92 100644 --- a/src/test/java/xades4j/properties/DataObjectDescTest.java +++ b/src/test/java/xades4j/properties/DataObjectDescTest.java @@ -16,56 +16,31 @@ */ package xades4j.properties; -import xades4j.algorithms.XPath2FilterTransform.XPath2Filter; -import xades4j.algorithms.GenericAlgorithm; +import org.junit.jupiter.api.Test; import org.w3c.dom.Document; -import org.junit.After; -import org.junit.AfterClass; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Test; import xades4j.algorithms.Algorithm; +import xades4j.algorithms.GenericAlgorithm; import xades4j.algorithms.XPath2FilterTransform; +import xades4j.algorithms.XPath2FilterTransform.XPath2Filter; import xades4j.algorithms.XPathTransform; import xades4j.utils.SignatureServicesTestBase; -import static org.junit.Assert.*; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; /** - * * @author Luís */ public class DataObjectDescTest { - @BeforeClass - public static void setUpClass() throws Exception - { - } - - @AfterClass - public static void tearDownClass() throws Exception - { - } - - @Before - public void setUp() - { - } - - @After - public void tearDown() - { - } - @Test public void testWithTransform() throws Exception { - System.out.println("withTransform"); - Document doc = SignatureServicesTestBase.getNewDocument(); DataObjectDesc instance = new DataObjectDescTestImpl() - .withTransform(new XPathTransform("xpath")) - .withTransform(XPath2Filter.subtract("xpath1").intersect("xpath2")) - .withTransform(new GenericAlgorithm("uri", doc.createElement("param1"),doc.createElement("param2"))); + .withTransform(new XPathTransform("xpath")) + .withTransform(XPath2Filter.subtract("xpath1").intersect("xpath2")) + .withTransform(new GenericAlgorithm("uri", doc.createElement("param1"), doc.createElement("param2"))); Algorithm[] transforms = instance.getTransforms().toArray(new Algorithm[0]); @@ -75,16 +50,17 @@ public void testWithTransform() throws Exception assertEquals(GenericAlgorithm.class, transforms[2].getClass()); } - @Test(expected = IllegalStateException.class) + @Test public void testWithDataObjectFormatRepeatedInstance() { - System.out.println("withDataObjectFormatRepeatedInstance"); - DataObjectFormatProperty format = new DataObjectFormatProperty(); DataObjectDesc instance = new DataObjectDescTestImpl(); instance.withDataObjectFormat(format); - instance.withDataObjectFormat(format); + + assertThrows(IllegalStateException.class, () -> { + instance.withDataObjectFormat(format); + }); } /** @@ -92,8 +68,6 @@ public void testWithDataObjectFormatRepeatedInstance() */ public void testWithDataObjectFormatMultipleTargets() { - System.out.println("withDataObjectFormatMultipleTargets"); - DataObjectFormatProperty format = new DataObjectFormatProperty(); DataObjectDesc instance = new DataObjectDescTestImpl(); DataObjectDesc other = new DataObjectDescTestImpl(); @@ -108,8 +82,6 @@ public void testWithDataObjectFormatMultipleTargets() @Test public void testWithCommitmentType() { - System.out.println("withCommitmentType"); - CommitmentTypeProperty commitment1 = CommitmentTypeProperty.proofOfApproval(); CommitmentTypeProperty commitment2 = CommitmentTypeProperty.proofOfCreation(); @@ -124,8 +96,6 @@ public void testWithCommitmentType() @Test public void testHasProperties() { - System.out.println("hasProperties"); - DataObjectDesc instance = new DataObjectDescTestImpl(); assertEquals(instance.hasProperties(), false); @@ -139,8 +109,6 @@ public void testHasProperties() @Test public void testGetSignedDataObjProps() { - System.out.println("getSignedDataObjProps"); - DataObjectDesc instance = new DataObjectDescTestImpl(); assertEquals(instance.getSignedDataObjProps().size(), 0); diff --git a/src/test/java/xades4j/properties/DataObjectFormatPropertyTest.java b/src/test/java/xades4j/properties/DataObjectFormatPropertyTest.java index ee9eda89..ad23c09a 100644 --- a/src/test/java/xades4j/properties/DataObjectFormatPropertyTest.java +++ b/src/test/java/xades4j/properties/DataObjectFormatPropertyTest.java @@ -16,59 +16,34 @@ */ package xades4j.properties; +import org.junit.jupiter.api.Test; + import java.util.ArrayList; import java.util.Collection; -import org.junit.After; -import org.junit.AfterClass; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Test; -import static org.junit.Assert.*; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; /** - * * @author Luís */ public class DataObjectFormatPropertyTest { - public DataObjectFormatPropertyTest() - { - } - - @BeforeClass - public static void setUpClass() throws Exception - { - } - - @AfterClass - public static void tearDownClass() throws Exception - { - } - - @Before - public void setUp() - { - } - - @After - public void tearDown() - { - } - /** * Test of withIdentifier method, of class DataObjectFormatProperty. */ @Test public void testWithIdentifier_String() { - System.out.println("withIdentifier"); String uri = "uri"; DataObjectFormatProperty instance = new DataObjectFormatProperty(); instance.withIdentifier(uri); assertEquals(instance.getIdentifier().getIdentifier(), uri); - instance.withIdentifier((ObjectIdentifier)null); + instance.withIdentifier((ObjectIdentifier) null); assertNull(instance.getIdentifier()); } @@ -77,7 +52,6 @@ public void testWithIdentifier_String() */ public void testWithIdentifier_String_IdentifierType() { - System.out.println("withIdentifier"); String identifier = "uri"; IdentifierType type = IdentifierType.URI; DataObjectFormatProperty instance = new DataObjectFormatProperty(); @@ -88,26 +62,12 @@ public void testWithIdentifier_String_IdentifierType() assertEquals(type, instance.getIdentifier().getIdentifierType()); } -// /** -// * Test of withIdentifier method, of class DataObjectFormatProperty. -// */ -// @Test(expected = NullPointerException.class) -// public void testWithIdentifier_String_IdentifierType_Null() -// { -// System.out.println("withIdentifier"); -// String identifier = "uri"; -// DataObjectFormatProperty instance = new DataObjectFormatProperty(); -// -// instance.withIdentifier(identifier, null); -// } - /** * Test of withDocumentationUri method, of class DataObjectFormatProperty. */ @Test public void testWithDocumentationUri() { - System.out.println("withDocumentationUri"); String documentationUri = "doc"; DataObjectFormatProperty instance = new DataObjectFormatProperty(); instance.withDocumentationUri(null); @@ -124,7 +84,6 @@ public void testWithDocumentationUri() @Test public void testWithDocumentationUris() { - System.out.println("withDocumentationUris"); Collection documentationUris = new ArrayList(1); DataObjectFormatProperty instance = new DataObjectFormatProperty(); @@ -140,11 +99,12 @@ public void testWithDocumentationUris() /** * Test of withDocumentationUris method, of class DataObjectFormatProperty. */ - @Test(expected = NullPointerException.class) + @Test public void testWithDocumentationUris_Null() { - System.out.println("withDocumentationUris"); DataObjectFormatProperty instance = new DataObjectFormatProperty(); - instance.withDocumentationUris(null); + assertThrows(NullPointerException.class, () -> { + instance.withDocumentationUris(null); + }); } } diff --git a/src/test/java/xades4j/providers/impl/DefaultTimeStampVerificationProviderTest.java b/src/test/java/xades4j/providers/impl/DefaultTimeStampVerificationProviderTest.java index 5573d1ed..a5810db0 100644 --- a/src/test/java/xades4j/providers/impl/DefaultTimeStampVerificationProviderTest.java +++ b/src/test/java/xades4j/providers/impl/DefaultTimeStampVerificationProviderTest.java @@ -1,16 +1,16 @@ /* * XAdES4j - A Java library for generation and verification of XAdES signatures. * Copyright (C) 2012 Luis Goncalves. - * + * * XAdES4j is free software; you can redistribute it and/or modify it under * the terms of the GNU Lesser General Public License as published by the Free * Software Foundation; either version 3 of the License, or any later version. - * + * * XAdES4j is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more * details. - * + * * You should have received a copy of the GNU Lesser General Public License along * with XAdES4j. If not, see . */ @@ -19,14 +19,16 @@ import java.io.ByteArrayOutputStream; import java.io.FileInputStream; import java.security.KeyStore; -import org.junit.Test; + +import org.junit.jupiter.api.Test; import xades4j.providers.TimeStampTokenDigestException; import xades4j.providers.TimeStampTokenVerificationException; import xades4j.utils.StreamUtils; import xades4j.verification.VerifierTestBase; +import static org.junit.jupiter.api.Assertions.assertThrows; + /** - * * @author Luís */ public class DefaultTimeStampVerificationProviderTest extends VerifierTestBase @@ -34,37 +36,36 @@ public class DefaultTimeStampVerificationProviderTest extends VerifierTestBase @Test public void testVerifyTokenSucceeds() throws Exception { - System.out.println("verifyTokenSucceeds"); - byte[] tsDigestInput = "TestDigestInput".getBytes(); doVerifyToken(tsDigestInput, getTestToken()); } - @Test(expected = TimeStampTokenDigestException.class) + @Test public void testVerifyTokenFailsWithDifferentDigestInput() throws Exception { - System.out.println("terifyTokenFailsWithDifferentDigestInput"); - byte[] tsDigestInput = "Invalid".getBytes(); - doVerifyToken(tsDigestInput, getTestToken()); + assertThrows(TimeStampTokenDigestException.class, () -> { + doVerifyToken(tsDigestInput, getTestToken()); + }); } - @Test(expected = TimeStampTokenVerificationException.class) + @Test public void testVerifyTokenFailsWithTamperedToken() throws Exception { - System.out.println("verifyTokenFailsWithTamperedToken"); - byte[] tsDigestInput = "TestDigestInput".getBytes(); byte[] tsToken = getTestToken(); for (int i = 0; i < tsToken.length; i++) { - if(i % 10 == 1){ - tsToken[i] = tsToken[i-1]; + if (i % 10 == 1) + { + tsToken[i] = tsToken[i - 1]; } } - doVerifyToken(tsDigestInput, tsToken); + assertThrows(TimeStampTokenVerificationException.class, () -> { + doVerifyToken(tsDigestInput, tsToken); + }); } private byte[] getTestToken() throws Exception diff --git a/src/test/java/xades4j/providers/impl/FileSystemKeyStoreKeyingDataProviderTest.java b/src/test/java/xades4j/providers/impl/FileSystemKeyStoreKeyingDataProviderTest.java index 5db7c386..241a1116 100644 --- a/src/test/java/xades4j/providers/impl/FileSystemKeyStoreKeyingDataProviderTest.java +++ b/src/test/java/xades4j/providers/impl/FileSystemKeyStoreKeyingDataProviderTest.java @@ -16,37 +16,42 @@ */ package xades4j.providers.impl; +import org.bouncycastle.jce.provider.BouncyCastleProvider; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.MethodSource; +import xades4j.utils.SignatureServicesTestBase; + import java.io.FileInputStream; import java.security.Security; import java.security.cert.CertificateFactory; import java.security.cert.X509Certificate; -import java.util.ArrayList; import java.util.Collection; import java.util.List; -import org.bouncycastle.jce.provider.BouncyCastleProvider; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.Parameterized; -import xades4j.utils.SignatureServicesTestBase; - -import static org.junit.Assert.*; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.params.provider.Arguments.arguments; /** * @author Luís */ -@RunWith(Parameterized.class) public class FileSystemKeyStoreKeyingDataProviderTest { - @Parameterized.Parameter(0) - public FileSystemKeyStoreKeyingDataProvider keyingProvider; - @Parameterized.Parameter(1) - public X509Certificate signCert; - - @Parameterized.Parameters - public static Collection data() throws Exception + @BeforeAll + public static void setup() { Security.addProvider(new BouncyCastleProvider()); + } + + @AfterAll + public static void cleanup() + { + Security.removeProvider(BouncyCastleProvider.PROVIDER_NAME); + } + + public static Collection data() throws Exception + { FileSystemKeyStoreKeyingDataProvider keyingProviderPkcs12 = FileSystemKeyStoreKeyingDataProvider .builder("pkcs12", SignatureServicesTestBase.toPlatformSpecificCertDirFilePath("my/LG.pfx"), @@ -77,22 +82,24 @@ public static Collection data() throws Exception X509Certificate signCert = (X509Certificate) cf.generateCertificate( new FileInputStream(SignatureServicesTestBase.toPlatformSpecificCertDirFilePath("my/LG.cer"))); - ArrayList result = new ArrayList(); - result.add(new Object[]{keyingProviderPkcs12, signCert}); - //TODO test will break, need find out why - //result.add(new Object[]{keyingProviderPkcs12BC,signCert}); - result.add(new Object[]{keyingProviderJks, signCert}); - return result; + return List.of( + arguments(keyingProviderPkcs12, signCert), + //TODO test will break, need find out why + //arguments(keyingProviderPkcs12BC, signCert), + arguments(keyingProviderJks, signCert) + ); } - @Test - public void testGetSigningKey() throws Exception + @ParameterizedTest + @MethodSource("data") + public void testGetSigningKey(FileSystemKeyStoreKeyingDataProvider keyingProvider, X509Certificate signCert) throws Exception { keyingProvider.getSigningKey(signCert); } - @Test - public void testGetSigningCertificateChain() throws Exception + @ParameterizedTest + @MethodSource("data") + public void testGetSigningCertificateChain(FileSystemKeyStoreKeyingDataProvider keyingProvider, X509Certificate signCert) throws Exception { List certChain = keyingProvider.getSigningCertificateChain(); assertEquals(certChain.size(), 3); diff --git a/src/test/java/xades4j/providers/impl/HttpTimeStampTokenProviderTest.java b/src/test/java/xades4j/providers/impl/HttpTimeStampTokenProviderTest.java index 2932a034..e1af5d5e 100644 --- a/src/test/java/xades4j/providers/impl/HttpTimeStampTokenProviderTest.java +++ b/src/test/java/xades4j/providers/impl/HttpTimeStampTokenProviderTest.java @@ -17,17 +17,16 @@ package xades4j.providers.impl; import org.apache.xml.security.algorithms.MessageDigestAlgorithm; -import static org.junit.Assert.assertNotNull; -import org.junit.Test; +import org.junit.jupiter.api.Test; import xades4j.providers.TimeStampTokenProvider.TimeStampTokenRes; +import static org.junit.jupiter.api.Assertions.assertNotNull; + public class HttpTimeStampTokenProviderTest { @Test public void testGetTimeStampToken() throws Exception { - System.out.println("getTimeStampToken"); - byte[] tsDigestInput = "TestDigestInput".getBytes(); String digestAlgUri = MessageDigestAlgorithm.ALGO_ID_DIGEST_SHA1; diff --git a/src/test/java/xades4j/providers/impl/PKCS11KeyStoreKeyingDataProviderTest.java b/src/test/java/xades4j/providers/impl/PKCS11KeyStoreKeyingDataProviderTest.java index 87add806..6d36ee0b 100644 --- a/src/test/java/xades4j/providers/impl/PKCS11KeyStoreKeyingDataProviderTest.java +++ b/src/test/java/xades4j/providers/impl/PKCS11KeyStoreKeyingDataProviderTest.java @@ -16,7 +16,7 @@ */ package xades4j.providers.impl; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.w3c.dom.Document; import org.w3c.dom.Element; import xades4j.production.Enveloped; @@ -32,9 +32,9 @@ import java.util.List; import java.util.UUID; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertTrue; /** * @author Luís @@ -44,7 +44,6 @@ public class PKCS11KeyStoreKeyingDataProviderTest extends SignerTestBase @Test public void testCertAndKeyMatch() throws Exception { - System.out.println("certAndKeyMatch"); assumePtCcPkcs11(); KeyingDataProvider ptccKeyingDataProv = PKCS11KeyStoreKeyingDataProvider diff --git a/src/test/java/xades4j/providers/impl/PKIXCertificateValidationProviderTest.java b/src/test/java/xades4j/providers/impl/PKIXCertificateValidationProviderTest.java index 0f52a772..c8b97443 100644 --- a/src/test/java/xades4j/providers/impl/PKIXCertificateValidationProviderTest.java +++ b/src/test/java/xades4j/providers/impl/PKIXCertificateValidationProviderTest.java @@ -16,6 +16,11 @@ */ package xades4j.providers.impl; +import org.junit.jupiter.api.Test; +import xades4j.providers.ValidationData; +import xades4j.utils.FileSystemDirectoryCertStore; + +import javax.security.auth.x500.X500Principal; import java.io.FileInputStream; import java.security.KeyStore; import java.security.cert.X509CertSelector; @@ -24,40 +29,16 @@ import java.util.Collections; import java.util.Date; -import org.junit.AfterClass; -import org.junit.BeforeClass; -import org.junit.Test; -import static org.junit.Assert.*; -import xades4j.providers.ValidationData; -import xades4j.utils.FileSystemDirectoryCertStore; - -import javax.security.auth.x500.X500Principal; +import static org.junit.jupiter.api.Assertions.assertEquals; /** - * * @author Luís */ public class PKIXCertificateValidationProviderTest { - public PKIXCertificateValidationProviderTest() - { - } - - @BeforeClass - public static void setUpClass() throws Exception - { - } - - @AfterClass - public static void tearDownClass() throws Exception - { - } - @Test public void testValidateMy() throws Exception { - System.out.println("validateMy"); - FileSystemDirectoryCertStore certStore = new FileSystemDirectoryCertStore("./src/test/cert/my"); KeyStore ks = KeyStore.getInstance("jks"); FileInputStream fis = new FileInputStream("./src/test/cert/my/myStore"); @@ -74,14 +55,12 @@ public void testValidateMy() throws Exception .intermediateCertStores(certStore.getStore()) .build(); ValidationData result = instance.validate(certSelector, new Date(), otherCerts); - assertEquals(result.getCerts().size(), 3); + assertEquals(3, result.getCerts().size()); } @Test public void testValidateNist() throws Exception { - System.out.println("validateNist"); - FileSystemDirectoryCertStore certStore = new FileSystemDirectoryCertStore("./src/test/cert/csrc.nist"); KeyStore ks = KeyStore.getInstance("jks"); FileInputStream fis = new FileInputStream("./src/test/cert/csrc.nist/trustAnchor"); @@ -98,7 +77,7 @@ public void testValidateNist() throws Exception .intermediateCertStores(certStore.getStore()) .build(); ValidationData result = instance.validate(certSelector, new Date(), otherCerts); - assertEquals(result.getCerts().size(), 4); - assertEquals(result.getCrls().size(), 3); + assertEquals(4, result.getCerts().size()); + assertEquals(3, result.getCrls().size()); } } diff --git a/src/test/java/xades4j/utils/DOMHelperTest.java b/src/test/java/xades4j/utils/DOMHelperTest.java index f72457e2..4fe0eb2c 100644 --- a/src/test/java/xades4j/utils/DOMHelperTest.java +++ b/src/test/java/xades4j/utils/DOMHelperTest.java @@ -20,12 +20,14 @@ import java.util.Collection; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; -import org.junit.Assert; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.xml.sax.InputSource; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; + /** * * @author luis @@ -44,11 +46,11 @@ public void testGetChildElementsByTagNameNS() throws Exception Collection elements = DOMHelper.getChildElementsByTagNameNS(doc.getDocumentElement(), "urn:test", "a"); - Assert.assertNotNull(elements); - Assert.assertEquals(2, elements.size()); + assertNotNull(elements); + assertEquals(2, elements.size()); for (Element element : elements) { - Assert.assertEquals("a", element.getLocalName()); + assertEquals("a", element.getLocalName()); } } } diff --git a/src/test/java/xades4j/utils/FileSystemDirectoryCertStoreTest.java b/src/test/java/xades4j/utils/FileSystemDirectoryCertStoreTest.java index cee9e9f2..3325b34e 100644 --- a/src/test/java/xades4j/utils/FileSystemDirectoryCertStoreTest.java +++ b/src/test/java/xades4j/utils/FileSystemDirectoryCertStoreTest.java @@ -16,31 +16,17 @@ */ package xades4j.utils; +import org.junit.jupiter.api.Test; + import java.util.Collection; -import org.junit.AfterClass; -import org.junit.BeforeClass; -import org.junit.Test; -import static org.junit.Assert.*; + +import static org.junit.jupiter.api.Assertions.assertEquals; /** - * * @author Luís */ -public class FileSystemDirectoryCertStoreTest { - - public FileSystemDirectoryCertStoreTest() { - } - - @BeforeClass - public static void setUpClass() throws Exception - { - } - - @AfterClass - public static void tearDownClass() throws Exception - { - } - +public class FileSystemDirectoryCertStoreTest +{ @Test public void testGetStoreMy() throws Exception { @@ -48,6 +34,7 @@ public void testGetStoreMy() throws Exception Collection certs = certStore.getStore().getCertificates(null); assertEquals(certs.size(), 4); } + @Test public void testGetStoreNist() throws Exception { diff --git a/src/test/java/xades4j/utils/PropertiesBagTest.java b/src/test/java/xades4j/utils/PropertiesBagTest.java index d03de662..281e6ad3 100644 --- a/src/test/java/xades4j/utils/PropertiesBagTest.java +++ b/src/test/java/xades4j/utils/PropertiesBagTest.java @@ -16,110 +16,89 @@ */ package xades4j.utils; -import xades4j.properties.SigningTimeProperty; +import org.junit.jupiter.api.Test; import xades4j.properties.DataObjectFormatProperty; -import org.junit.After; -import org.junit.AfterClass; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Test; -import static org.junit.Assert.*; +import xades4j.properties.SigningTimeProperty; + +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; /** - * * @author Luís */ public class PropertiesBagTest { - public PropertiesBagTest() - { - } - - @BeforeClass - public static void setUpClass() throws Exception - { - } - - @AfterClass - public static void tearDownClass() throws Exception - { - } - - @Before - public void setUp() - { - } - - @After - public void tearDown() - { - } - -// @Test -// public void jaxbTest() throws Exception -// { -// -// DocumentBuilderFactory fac = DocumentBuilderFactory.newInstance(); -// fac.setNamespaceAware(true); -// Document doc = fac.newDocumentBuilder().parse(new FileInputStream(".\\src\\test\\resources\\dummy.xml")); -// -// // Create the JAXB unmarshaller. -// JAXBContext jaxbContext = JAXBContext.newInstance(XmlQualifyingPropertiesType.class); -// // Create the JAXB unmarshaller and unmarshalProperties the root JAXB element -// Unmarshaller unmarshaller = jaxbContext.createUnmarshaller(); -// ValidationEventCollector col = new ValidationEventCollector(); -// unmarshaller.setEventHandler(col); -// JAXBElement qualifPropsElem = (JAXBElement)unmarshaller.unmarshal(doc.getDocumentElement()); -// -// //int size = qualifPropsElem.getValue().getUnsignedProperties().getUnsignedDataObjectProperties().getUnsignedDataObjectProperty().size(); -// -// Calendar c = qualifPropsElem.getValue().getSignedProperties().getSignedSignatureProperties().getSigningTime(); -// -// XmlCertIDListType certs = qualifPropsElem.getValue().getSignedProperties().getSignedSignatureProperties().getSigningCertificate(); -// Object iss = certs.getCert().get(0).getIssuerSerial(); -// if (null == iss) -// System.out.println("WAS NULL"); -// } + // @Test + // public void jaxbTest() throws Exception + // { + // + // DocumentBuilderFactory fac = DocumentBuilderFactory.newInstance(); + // fac.setNamespaceAware(true); + // Document doc = fac.newDocumentBuilder().parse(new FileInputStream(".\\src\\test\\resources\\dummy.xml")); + // + // // Create the JAXB unmarshaller. + // JAXBContext jaxbContext = JAXBContext.newInstance(XmlQualifyingPropertiesType.class); + // // Create the JAXB unmarshaller and unmarshalProperties the root JAXB element + // Unmarshaller unmarshaller = jaxbContext.createUnmarshaller(); + // ValidationEventCollector col = new ValidationEventCollector(); + // unmarshaller.setEventHandler(col); + // JAXBElement qualifPropsElem = (JAXBElement)unmarshaller.unmarshal(doc.getDocumentElement()); + // + // //int size = qualifPropsElem.getValue().getUnsignedProperties().getUnsignedDataObjectProperties().getUnsignedDataObjectProperty().size(); + // + // Calendar c = qualifPropsElem.getValue().getSignedProperties().getSignedSignatureProperties().getSigningTime(); + // + // XmlCertIDListType certs = qualifPropsElem.getValue().getSignedProperties().getSignedSignatureProperties().getSigningCertificate(); + // Object iss = certs.getCert().get(0).getIssuerSerial(); + // if (null == iss) + // System.out.println("WAS NULL"); + // } /** * Test of put method, of class PropertiesSet. */ - @Test(expected = IllegalStateException.class) + @Test public void testPut() { - System.out.println("put"); SigningTimeProperty prop1 = new SigningTimeProperty(), prop2 = new SigningTimeProperty(); DataObjectFormatProperty prop3 = new DataObjectFormatProperty(); PropertiesSet instance = new PropertiesSet(2); instance.put(prop1); instance.put(prop3); - instance.put(prop2); + + assertThrows(IllegalStateException.class, () -> { + instance.put(prop2); + }); } /** * Test of add method, of class PropertiesSet, with {@code null} value. */ - @Test(expected = NullPointerException.class) + @Test public void testAddNull() { - System.out.println("addNull"); PropertiesSet instance = new PropertiesSet(0); - instance.add(null); + assertThrows(NullPointerException.class, () -> { + instance.add(null); + }); } /** * Test of add method, of class PropertiesSet. */ - @Test(expected = IllegalStateException.class) + @Test public void testAdd() { - System.out.println("add"); SigningTimeProperty prop1 = new SigningTimeProperty(), prop2 = new SigningTimeProperty(); PropertiesSet instance = new PropertiesSet(1); instance.add(prop1); instance.add(prop2); - instance.add(prop1); + + assertThrows(IllegalStateException.class, () -> { + instance.add(prop1); + }); } /** @@ -138,14 +117,16 @@ public void testRemove() /** * Test of remove method, of class PropertiesSet. */ - @Test(expected = IllegalStateException.class) + @Test public void testRemoveNotPresent() { - System.out.println("removeNotPresent"); SigningTimeProperty prop1 = new SigningTimeProperty(), prop2 = new SigningTimeProperty(); PropertiesSet instance = new PropertiesSet(1); instance.add(prop1); - instance.remove(prop2); + + assertThrows(IllegalStateException.class, () -> { + instance.remove(prop2); + }); } /** diff --git a/src/test/java/xades4j/utils/SignatureServicesTestBase.java b/src/test/java/xades4j/utils/SignatureServicesTestBase.java index fd7f147d..f6c1a7b6 100644 --- a/src/test/java/xades4j/utils/SignatureServicesTestBase.java +++ b/src/test/java/xades4j/utils/SignatureServicesTestBase.java @@ -37,7 +37,6 @@ */ public class SignatureServicesTestBase { - static protected HttpTsaConfiguration DEFAULT_TEST_TSA = new HttpTsaConfiguration("http://tss.accv.es:8318/tsa"); static private DocumentBuilder db; diff --git a/src/test/java/xades4j/utils/X500NameStyleProviderTest.java b/src/test/java/xades4j/utils/X500NameStyleProviderTest.java index 6b696a98..4beac3a3 100644 --- a/src/test/java/xades4j/utils/X500NameStyleProviderTest.java +++ b/src/test/java/xades4j/utils/X500NameStyleProviderTest.java @@ -1,14 +1,15 @@ package xades4j.utils; -import org.junit.Assert; -import org.junit.Test; +import org.junit.jupiter.api.Test; import xades4j.providers.X500NameStyleProvider; import xades4j.providers.impl.DefaultX500NameStyleProvider; import javax.security.auth.x500.X500Principal; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + /** - * * @author Artem R. Romanenko * @version 30.07.18 * @see Issues-157 @@ -16,20 +17,24 @@ public class X500NameStyleProviderTest { - private static final String NAME_SIMPLE ="C=CO,L=Bogota D.C.,O=Andes SCD.,OU=Division de certificacion entidad final,CN=CA ANDES SCD S.A. Clase II, E=info@andesscd.com.co"; - private static final String NAME_SIMPLE_NORMAL ="C=CO,L=Bogota D.C.,O=Andes SCD.,OU=Division de certificacion entidad final,CN=CA ANDES SCD S.A. Clase II, EMAILADDRESS=info@andesscd.com.co"; - private static final String NAME_CANONICAL ="C=CO,L=Bogota D.C.,O=Andes SCD.,OU=Division de certificacion entidad final,CN=CA ANDES SCD S.A. Clase II, 1.2.840.113549.1.9.1=#1614696e666f40616e6465737363642e636f6d2e636f"; + private static final String NAME_SIMPLE = "C=CO,L=Bogota D.C.,O=Andes SCD.,OU=Division de certificacion entidad final,CN=CA ANDES SCD S.A. Clase II, E=info@andesscd.com.co"; + private static final String NAME_SIMPLE_NORMAL = "C=CO,L=Bogota D.C.,O=Andes SCD.,OU=Division de certificacion entidad final,CN=CA ANDES SCD S.A. Clase II, EMAILADDRESS=info@andesscd.com.co"; + private static final String NAME_CANONICAL = "C=CO,L=Bogota D.C.,O=Andes SCD.,OU=Division de certificacion entidad final,CN=CA ANDES SCD S.A. Clase II, 1.2.840.113549.1.9.1=#1614696e666f40616e6465737363642e636f6d2e636f"; - @Test(expected = Exception.class) + @Test public void errorDefaultParsing() { - new X500Principal(NAME_SIMPLE); + assertThrows(Exception.class, () -> { + new X500Principal(NAME_SIMPLE); + }); } - @Test(expected = Exception.class) + @Test public void errorParsingWithProvider() { - new DefaultX500NameStyleProvider().fromString(NAME_SIMPLE); + assertThrows(Exception.class, () -> { + new DefaultX500NameStyleProvider().fromString(NAME_SIMPLE); + }); } @Test @@ -46,12 +51,11 @@ public void normalWithExtendKeywords() X500Principal name1 = x500NameStyleProvider.fromString(NAME_CANONICAL); X500Principal name2 = x500NameStyleProvider.fromString(NAME_SIMPLE_NORMAL); RFC4519ExtensibleStyle es = new RFC4519ExtensibleStyle(); - es.addSymbol("1.2.840.113549.1.9.1","E"); - X500NameStyleProvider x500NameStyleProviderExtend =new DefaultX500NameStyleProvider(es); + es.addSymbol("1.2.840.113549.1.9.1", "E"); + X500NameStyleProvider x500NameStyleProviderExtend = new DefaultX500NameStyleProvider(es); X500Principal name3 = x500NameStyleProviderExtend.fromString(NAME_SIMPLE); - Assert.assertEquals(name1,name2); - Assert.assertEquals(name1,name3); + assertEquals(name1, name2); + assertEquals(name1, name3); } - } diff --git a/src/test/java/xades4j/utils/XadesProfileCoreTest.java b/src/test/java/xades4j/utils/XadesProfileCoreTest.java index 7a632e65..1aaf76c4 100644 --- a/src/test/java/xades4j/utils/XadesProfileCoreTest.java +++ b/src/test/java/xades4j/utils/XadesProfileCoreTest.java @@ -16,13 +16,18 @@ */ package xades4j.utils; -import java.util.Set; -import java.util.Map; import com.google.inject.AbstractModule; -import javax.inject.Inject; import com.google.inject.Module; -import org.junit.Test; -import static org.junit.Assert.*; +import org.junit.jupiter.api.Test; + +import javax.inject.Inject; +import java.util.Map; +import java.util.Set; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; /* Test classes/interfaces */ interface A @@ -73,6 +78,7 @@ public C(Action action) class D { public Set as; + @Inject public D(Set as) { @@ -84,6 +90,7 @@ public D(Set as) class E { public Map as; + @Inject public E(Map as) { @@ -93,7 +100,6 @@ public E(Map as) } /** - * * @author Luís */ public class XadesProfileCoreTest @@ -101,8 +107,6 @@ public class XadesProfileCoreTest @Test public void testGetInstance() throws XadesProfileResolutionException { - System.out.println("getInstance"); - Module module = new AbstractModule() { @Override @@ -112,24 +116,23 @@ protected void configure() } }; XadesProfileCore instance = new XadesProfileCore(); - A a = instance.getInstance(A.class, new Module[] {module}, new Module[0]); + A a = instance.getInstance(A.class, new Module[]{module}, new Module[0]); assertNotNull(a); assertTrue(a instanceof AImpl1); } - @Test(expected = XadesProfileResolutionException.class) + @Test public void testGetInstanceException() throws XadesProfileResolutionException { - System.out.println("getInstance_Exception"); XadesProfileCore instance = new XadesProfileCore(); - instance.getInstance(A.class, new Module[0], new Module[0]); + assertThrows(XadesProfileResolutionException.class, () -> { + instance.getInstance(A.class, new Module[0], new Module[0]); + }); } @Test public void testAddBinding() throws XadesProfileResolutionException { - System.out.println("addBinding"); - Module module = new AbstractModule() { @Override @@ -140,13 +143,13 @@ protected void configure() }; XadesProfileCore instance = new XadesProfileCore(); instance.addBinding(A.class, AImpl2.class); - A a = instance.getInstance(A.class, new Module[] {module}, new Module[0]); + A a = instance.getInstance(A.class, new Module[]{module}, new Module[0]); assertNotNull(a); assertTrue(a instanceof AImpl2); B b1 = new BImpl(); instance.addBinding(B.class, b1); - B b2 = instance.getInstance(B.class, new Module[] {module}, new Module[0]); + B b2 = instance.getInstance(B.class, new Module[]{module}, new Module[0]); assertNotNull(a); assertEquals(b1, b2); } @@ -154,33 +157,27 @@ protected void configure() @Test public void testAddGenericBinding() throws XadesProfileResolutionException { - System.out.println("addGenericBinding"); - XadesProfileCore instance = new XadesProfileCore(); instance.addGenericBinding(Action.class, ActionOfA.class, A.class); - C c = instance.getInstance(C.class, new Module[0], new Module[0]); + C c = instance.getInstance(C.class, new Module[0], new Module[0]); assertTrue(c.action instanceof ActionOfA); } @Test public void testAddMultibinding() throws Exception { - System.out.println("addMultibinding"); - XadesProfileCore instance = new XadesProfileCore(); instance.addMultibinding(A.class, AImpl1.class); instance.addMultibinding(A.class, new AImpl1()); instance.addMultibinding(A.class, AImpl2.class); - D d = instance.getInstance(D.class, new Module[0], new Module[0]); + D d = instance.getInstance(D.class, new Module[0], new Module[0]); assertEquals(3, d.as.size()); } @Test public void testAddMapBinding() throws Exception { - System.out.println("addMapBinding"); - XadesProfileCore instance = new XadesProfileCore(); instance.addMapBinding(A.class, "A1", AImpl1.class); instance.addMapBinding(A.class, "A2", AImpl2.class); diff --git a/src/test/java/xades4j/verification/DistinguishedNameComparerTest.java b/src/test/java/xades4j/verification/DistinguishedNameComparerTest.java index 329c5d64..a8f9ed67 100644 --- a/src/test/java/xades4j/verification/DistinguishedNameComparerTest.java +++ b/src/test/java/xades4j/verification/DistinguishedNameComparerTest.java @@ -1,59 +1,54 @@ package xades4j.verification; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.MethodSource; +import xades4j.providers.X500NameStyleProvider; +import xades4j.providers.impl.DefaultX500NameStyleProvider; +import xades4j.utils.RFC4519ExtensibleStyle; +import xades4j.utils.SignatureServicesTestBase; + +import javax.security.auth.x500.X500Principal; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.security.cert.CertificateFactory; import java.security.cert.X509Certificate; -import java.util.Arrays; import java.util.Collection; -import javax.security.auth.x500.X500Principal; -import static org.junit.Assert.*; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.Parameterized; -import org.junit.runners.Parameterized.Parameters; -import xades4j.providers.X500NameStyleProvider; -import xades4j.providers.impl.DefaultX500NameStyleProvider; -import xades4j.utils.RFC4519ExtensibleStyle; -import xades4j.utils.SignatureServicesTestBase; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.params.provider.Arguments.arguments; /** * @author luis */ -@RunWith(Parameterized.class) public class DistinguishedNameComparerTest extends SignatureServicesTestBase { - @Parameters - public static Collection data() throws Exception + public static Collection data() throws Exception { - return Arrays.asList(new Object[][] - { - // #1 - // Certificate includes the value of OID.2.5.4.97 as UTF8String - { - "2.5.4.97=#0c0f56415445532d413636373231343939,CN=UANATACA CA1 2016,OU=TSP-UANATACA,O=UANATACA S.A.,L=Barcelona (see current address at www.uanataca.com/address),C=ES", - certFromResource("issue166/EMPUBqscdA.cer") - }, - { - "2.5.4.97=#130f56415445532d413636373231343939,CN=UANATACA CA1 2016,OU=TSP-UANATACA,O=UANATACA S.A.,L=Barcelona (see current address at www.uanataca.com/address),C=ES", - certFromResource("issue166/EMPUBqscdA.cer") - }, - { - "OID.2.5.4.97=VATES-A66721499, CN=UANATACA CA1 2016, OU=TSP-UANATACA, O=UANATACA S.A., L=Barcelona (see current address at www.uanataca.com/address), C=ES", - certFromResource("issue166/EMPUBqscdA.cer") - }, - // #2 - { - "CN = Itermediate, OU = CC, O = ISEL, C = PT", - certFromFile("my/LG.cer") - }, - // #3 - { - "C = PT, O = SCEE - Sistema de Certificação Electrónica do Estado, OU = ECEstado, CN = Cartão de Cidadão 001", - certFromFile("pt/ECQualifSigCC0001.cer") - } - }); + return List.of( + // Certificate includes the value of OID.2.5.4.97 as UTF8String + arguments( + "2.5.4.97=#0c0f56415445532d413636373231343939,CN=UANATACA CA1 2016,OU=TSP-UANATACA,O=UANATACA S.A.,L=Barcelona (see current address at www.uanataca.com/address),C=ES", + certFromResource("issue166/EMPUBqscdA.cer") + ), + arguments( + "2.5.4.97=#130f56415445532d413636373231343939,CN=UANATACA CA1 2016,OU=TSP-UANATACA,O=UANATACA S.A.,L=Barcelona (see current address at www.uanataca.com/address),C=ES", + certFromResource("issue166/EMPUBqscdA.cer") + ), + arguments( + "OID.2.5.4.97=VATES-A66721499, CN=UANATACA CA1 2016, OU=TSP-UANATACA, O=UANATACA S.A., L=Barcelona (see current address at www.uanataca.com/address), C=ES", + certFromResource("issue166/EMPUBqscdA.cer") + ), + arguments( + "CN = Itermediate, OU = CC, O = ISEL, C = PT", + certFromFile("my/LG.cer") + ), + arguments( + "C = PT, O = SCEE - Sistema de Certificação Electrónica do Estado, OU = ECEstado, CN = Cartão de Cidadão 001", + certFromFile("pt/ECQualifSigCC0001.cer") + ) + ); } private static X509Certificate certFromResource(String resourcePath) throws Exception @@ -64,7 +59,7 @@ private static X509Certificate certFromResource(String resourcePath) throws Exce return (X509Certificate) certFactory.generateCertificate(is); } } - + private static X509Certificate certFromFile(String filePath) throws Exception { CertificateFactory certFactory = CertificateFactory.getInstance("X.509"); @@ -73,21 +68,19 @@ private static X509Certificate certFromFile(String filePath) throws Exception return (X509Certificate) certFactory.generateCertificate(is); } } - private final String issuerDn; - private final X509Certificate cert; + private final RFC4519ExtensibleStyle nameStyle; private final X500NameStyleProvider x500NameStyleProvider; - public DistinguishedNameComparerTest(String issuerDn, X509Certificate cert) throws IOException + public DistinguishedNameComparerTest() throws IOException { - this.issuerDn = issuerDn; - this.cert = cert; this.nameStyle = new RFC4519ExtensibleStyle(); this.x500NameStyleProvider = new DefaultX500NameStyleProvider(this.nameStyle); } - @Test - public void canCompare() + @ParameterizedTest + @MethodSource("data") + public void canCompare(String issuerDn, X509Certificate cert) { X500Principal principal = cert.getIssuerX500Principal(); DistinguishedNameComparer comparer = new DistinguishedNameComparer(this.nameStyle, this.x500NameStyleProvider); diff --git a/src/test/java/xades4j/verification/GenericDOMDataVerifierTest.java b/src/test/java/xades4j/verification/GenericDOMDataVerifierTest.java index f7da86ea..0a0a1b0e 100644 --- a/src/test/java/xades4j/verification/GenericDOMDataVerifierTest.java +++ b/src/test/java/xades4j/verification/GenericDOMDataVerifierTest.java @@ -16,22 +16,21 @@ */ package xades4j.verification; -import java.util.HashMap; -import java.util.Map; -import javax.xml.namespace.QName; -import javax.xml.parsers.DocumentBuilderFactory; -import org.junit.After; -import org.junit.AfterClass; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Test; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; import org.w3c.dom.Document; -import static org.junit.Assert.*; import xades4j.properties.QualifyingProperty; import xades4j.properties.data.GenericDOMData; +import javax.xml.namespace.QName; +import javax.xml.parsers.DocumentBuilderFactory; +import java.util.HashMap; +import java.util.Map; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + /** - * * @author Luís */ public class GenericDOMDataVerifierTest @@ -39,11 +38,7 @@ public class GenericDOMDataVerifierTest private static Map customElemVerifiers; private static Document testDocument; - public GenericDOMDataVerifierTest() - { - } - - @BeforeClass + @BeforeAll public static void setUpClass() throws Exception { customElemVerifiers = new HashMap(1); @@ -54,21 +49,6 @@ public static void setUpClass() throws Exception testDocument = dbf.newDocumentBuilder().newDocument(); } - @AfterClass - public static void tearDownClass() throws Exception - { - } - - @Before - public void setUp() - { - } - - @After - public void tearDown() - { - } - @Test public void testVerify() throws Exception { @@ -80,14 +60,16 @@ public void testVerify() throws Exception assertEquals(result.getName(), "Elem"); } - @Test(expected = InvalidPropertyException.class) + @Test public void testVerifyNoVerifier() throws Exception { GenericDOMData propData = new GenericDOMData(testDocument.createElementNS("http://test.generic.dom", "Elem")); QualifyingPropertyVerificationContext ctx = null; GenericDOMDataVerifier instance = new GenericDOMDataVerifier(new HashMap(0)); - instance.verify(propData, ctx); + assertThrows(InvalidPropertyException.class, () -> { + instance.verify(propData, ctx); + }); } } diff --git a/src/test/java/xades4j/verification/Issue166Test.java b/src/test/java/xades4j/verification/Issue166Test.java index 11a8121e..8da7eabf 100644 --- a/src/test/java/xades4j/verification/Issue166Test.java +++ b/src/test/java/xades4j/verification/Issue166Test.java @@ -1,17 +1,21 @@ package xades4j.verification; +import org.bouncycastle.asn1.x500.X500Name; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import xades4j.utils.SignatureServicesTestBase; + +import javax.security.auth.x500.X500Principal; import java.io.InputStream; import java.security.cert.CertificateFactory; import java.security.cert.X509Certificate; -import javax.security.auth.x500.X500Principal; -import org.bouncycastle.asn1.x500.X500Name; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; -import xades4j.utils.SignatureServicesTestBase; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; /** - * Investigation for https://github.com/luisgoncalves/xades4j/issues/166. + * Investigation for issue 166. * @author luis */ public class Issue166Test extends SignatureServicesTestBase @@ -22,7 +26,7 @@ public class Issue166Test extends SignatureServicesTestBase private X509Certificate cert; - @Before + @BeforeEach public void setUp() throws Exception { CertificateFactory certFactory = CertificateFactory.getInstance("X.509"); @@ -40,8 +44,8 @@ public void javaCannotCompareStrings() throws Exception X500Principal principal2 = new X500Principal(dnPrintable); X500Principal principal3 = new X500Principal(dnPlain); - Assert.assertFalse(principal1.equals(principal2)); - Assert.assertFalse(principal1.equals(principal3)); + assertFalse(principal1.equals(principal2)); + assertFalse(principal1.equals(principal3)); } @Test @@ -50,7 +54,7 @@ public void javaCanComparePrintableAndPlainStrings() throws Exception X500Principal principal1 = new X500Principal(dnPrintable); X500Principal principal2 = new X500Principal(dnPlain); - Assert.assertTrue(principal1.equals(principal2)); + assertEquals(principal1, principal2); } @Test @@ -59,7 +63,7 @@ public void javaCannotCompareCertAndPrintableString() throws Exception X500Principal principal1 = cert.getIssuerX500Principal(); X500Principal principal2 = new X500Principal(dnPrintable); - Assert.assertFalse(principal1.equals(principal2)); + assertFalse(principal1.equals(principal2)); } @Test @@ -68,7 +72,7 @@ public void javaCanCompareCertAndUtf8String() throws Exception X500Principal principal1 = cert.getIssuerX500Principal(); X500Principal principal2 = new X500Principal(dnUtf8); - Assert.assertTrue(principal1.equals(principal2)); + assertTrue(principal1.equals(principal2)); } @Test @@ -77,7 +81,7 @@ public void javaCannotCompareCertAndPlainString() throws Exception X500Principal principal1 = cert.getIssuerX500Principal(); X500Principal principal2 = new X500Principal(dnPlain); - Assert.assertFalse(principal1.equals(principal2)); + assertFalse(principal1.equals(principal2)); } @Test @@ -87,9 +91,9 @@ public void bcCanCompareStrings() throws Exception X500Name principal2 = new X500Name(dnPrintable); X500Name principal3 = new X500Name(dnPlain); - Assert.assertTrue(principal1.equals(principal2)); - Assert.assertTrue(principal1.equals(principal3)); - Assert.assertTrue(principal2.equals(principal3)); + assertTrue(principal1.equals(principal2)); + assertTrue(principal1.equals(principal3)); + assertTrue(principal2.equals(principal3)); } @Test @@ -98,7 +102,7 @@ public void bcCanCompareCertAndPrintableString() throws Exception X500Name principal1 = X500Name.getInstance(cert.getIssuerX500Principal().getEncoded()); X500Name principal2 = new X500Name(dnPrintable); - Assert.assertTrue(principal1.equals(principal2)); + assertTrue(principal1.equals(principal2)); } @Test @@ -107,7 +111,7 @@ public void bcCanCompareCertAndUtf8String() throws Exception X500Name principal1 = X500Name.getInstance(cert.getIssuerX500Principal().getEncoded()); X500Name principal2 = new X500Name(dnUtf8); - Assert.assertTrue(principal1.equals(principal2)); + assertTrue(principal1.equals(principal2)); } @Test @@ -116,6 +120,6 @@ public void bcCanCompareCertAndPlainString() throws Exception X500Name principal1 = X500Name.getInstance(cert.getIssuerX500Principal().getEncoded()); X500Name principal2 = new X500Name(dnPlain); - Assert.assertTrue(principal1.equals(principal2)); + assertTrue(principal1.equals(principal2)); } } diff --git a/src/test/java/xades4j/verification/OtherVerifierTests.java b/src/test/java/xades4j/verification/OtherVerifierTests.java index d2ab8c07..539d6de1 100644 --- a/src/test/java/xades4j/verification/OtherVerifierTests.java +++ b/src/test/java/xades4j/verification/OtherVerifierTests.java @@ -16,14 +16,16 @@ */ package xades4j.verification; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import xades4j.properties.QualifyingProperty; +import xades4j.properties.data.SigningTimeData; import xades4j.utils.BuiltIn; import javax.inject.Inject; -import org.junit.Before; -import xades4j.properties.QualifyingProperty; -import xades4j.properties.data.SigningTimeData; -import org.junit.Test; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; class SigningTimeVerifierThatDependsOnBuiltInVerifier implements QualifyingPropertyVerifier { @@ -51,16 +53,15 @@ public class OtherVerifierTests extends VerifierTestBase { XadesVerificationProfile mySigsVerificationProfile; - @Before + @BeforeEach public void initialize() { mySigsVerificationProfile = new XadesVerificationProfile(VerifierTestBase.validationProviderMySigs); } - @Test(expected = UnsupportedOperationException.class) + @Test public void testVerifyBESCustomPropVer() throws Exception { - System.out.println("verifyBESCustomPropVer"); mySigsVerificationProfile.withQualifyingPropertyVerifier(SigningTimeData.class, new QualifyingPropertyVerifier() { @@ -72,17 +73,24 @@ public QualifyingProperty verify( throw new UnsupportedOperationException("Yeah!"); } }); - verifySignature("document.signed.bes.xml", mySigsVerificationProfile); + + Exception e = assertThrows(UnsupportedOperationException.class, () -> { + verifySignature("document.signed.bes.xml", mySigsVerificationProfile); + }); + + assertEquals("Yeah!", e.getMessage()); } - @Test(expected = SigningTimeVerificationException.class) + @Test public void testCustomVerifierCanUseBuiltInVerifier() throws Exception { - System.out.println("customVerifierCanUseBuiltInVerifier"); mySigsVerificationProfile.withQualifyingPropertyVerifier( SigningTimeData.class, SigningTimeVerifierThatDependsOnBuiltInVerifier.class); - verifySignature("document.signed.bes.xml", mySigsVerificationProfile); + + assertThrows(SigningTimeVerificationException.class, () -> { + verifySignature("document.signed.bes.xml", mySigsVerificationProfile); + }); } @Test diff --git a/src/test/java/xades4j/verification/XAdESFormCheckerTest.java b/src/test/java/xades4j/verification/XAdESFormCheckerTest.java index ddcae1cb..53a512a6 100644 --- a/src/test/java/xades4j/verification/XAdESFormCheckerTest.java +++ b/src/test/java/xades4j/verification/XAdESFormCheckerTest.java @@ -1,33 +1,38 @@ /* * XAdES4j - A Java library for generation and verification of XAdES signatures. * Copyright (C) 2019 Luis Goncalves. - * + * * XAdES4j is free software; you can redistribute it and/or modify it under * the terms of the GNU Lesser General Public License as published by the Free * Software Foundation; either version 3 of the License, or any later version. - * + * * XAdES4j is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more * details. - * + * * You should have received a copy of the GNU Lesser General Public License along * with XAdES4j. If not, see . */ package xades4j.verification; +import org.junit.jupiter.api.Test; + import java.util.ArrayList; -import org.junit.Test; + +import static org.junit.jupiter.api.Assertions.assertThrows; /** - * * @author luis */ public class XAdESFormCheckerTest { - @Test(expected = InvalidXAdESFormException.class) + @Test public void checkFormThrowsIfMinimumPropertiesAreNotPresent() throws InvalidXAdESFormException { - XAdESFormChecker.checkForm(new ArrayList(0)); + assertThrows(InvalidXAdESFormException.class, () -> { + XAdESFormChecker.checkForm(new ArrayList(0)); + + }); } } diff --git a/src/test/java/xades4j/verification/XadesVerificationProfileTest.java b/src/test/java/xades4j/verification/XadesVerificationProfileTest.java index 7be842fa..d3ddd36e 100644 --- a/src/test/java/xades4j/verification/XadesVerificationProfileTest.java +++ b/src/test/java/xades4j/verification/XadesVerificationProfileTest.java @@ -16,12 +16,12 @@ */ package xades4j.verification; -import org.junit.Test; -import static org.junit.Assert.*; +import org.junit.jupiter.api.Test; import xades4j.utils.XadesProfileResolutionException; +import static org.junit.jupiter.api.Assertions.assertNotNull; + /** - * * @author Luís */ public class XadesVerificationProfileTest @@ -29,7 +29,6 @@ public class XadesVerificationProfileTest @Test public void testGetVerifier() throws XadesProfileResolutionException { - System.out.println("getVerifier"); XadesVerificationProfile instance = new XadesVerificationProfile(VerifierTestBase.validationProviderMySigs); XadesVerifier result = instance.newVerifier(); assertNotNull(result); diff --git a/src/test/java/xades4j/verification/XadesVerifierErrorsTest.java b/src/test/java/xades4j/verification/XadesVerifierErrorsTest.java index e398c410..92e0822d 100644 --- a/src/test/java/xades4j/verification/XadesVerifierErrorsTest.java +++ b/src/test/java/xades4j/verification/XadesVerifierErrorsTest.java @@ -16,14 +16,16 @@ */ package xades4j.verification; -import java.security.KeyStore; -import org.junit.Test; -import org.junit.Before; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import xades4j.providers.CannotSelectCertificateException; import xades4j.providers.impl.PKIXCertificateValidationProvider; +import java.security.KeyStore; + +import static org.junit.jupiter.api.Assertions.assertThrows; + /** - * * @author Luís */ public class XadesVerifierErrorsTest extends VerifierTestBase @@ -31,87 +33,94 @@ public class XadesVerifierErrorsTest extends VerifierTestBase XadesVerificationProfile mySigsVerificationProfile; XadesVerificationProfile nistVerificationProfile; - @Before + @BeforeEach public void initialize() { mySigsVerificationProfile = new XadesVerificationProfile(VerifierTestBase.validationProviderMySigs); nistVerificationProfile = new XadesVerificationProfile(VerifierTestBase.validationProviderNist); } - @Test(expected = QualifyingPropertiesIncorporationException.class) + @Test public void testErrVerifySignedPropsIncorp() throws Exception { - System.out.println("errVerifySignedPropsIncorp"); - verifyBadSignature("document.signed.t.bes.badsignedprops.xml", mySigsVerificationProfile); + assertThrows(QualifyingPropertiesIncorporationException.class, () -> { + verifyBadSignature("document.signed.t.bes.badsignedprops.xml", mySigsVerificationProfile); + }); } - @Test(expected = QualifyingPropertiesIncorporationException.class) + @Test public void testErrVerifySignedPropsIncorpNoRefType() throws Exception { - System.out.println("errVerifySignedPropsIncorpNoRefType"); - - verifyBadSignature("document.signed.bes.signedpropsrefnotype.xml", - new XadesVerificationProfile(validationProviderPtCc)); + assertThrows(QualifyingPropertiesIncorporationException.class, () -> { + verifyBadSignature("document.signed.bes.signedpropsrefnotype.xml", + new XadesVerificationProfile(validationProviderPtCc)); + }); } - @Test(expected = InvalidXAdESFormException.class) + @Test public void testErrVerifyIncorrectC() throws Exception { - System.out.println("errVerifyIncorrectC"); - verifyBadSignature("document.signed.c.bad.xml",nistVerificationProfile); + assertThrows(InvalidXAdESFormException.class, () -> { + verifyBadSignature("document.signed.c.bad.xml", nistVerificationProfile); + }); } - @Test(expected = CannotSelectCertificateException.class) + @Test public void testErrVerifyNoSignCert() throws Exception { - System.out.println("ErrVerifyNoSignCert"); - KeyStore ks = createAndLoadJKSKeyStore("be/beStore", "bestorepass"); PKIXCertificateValidationProvider cvp = PKIXCertificateValidationProvider.builder(ks).checkRevocation(false).build(); - verifyBadSignature("TSL_BE.nocert.xml", new XadesVerificationProfile(cvp)); + assertThrows(CannotSelectCertificateException.class, () -> { + verifyBadSignature("TSL_BE.nocert.xml", new XadesVerificationProfile(cvp)); + }); } - @Test(expected = ReferenceValueException.class) + @Test public void testErrVerifyChangedDataObj() throws Exception { - System.out.println("errVerifyChangedDataObj"); - verifyBadSignature("document.signed.bes.invaliddataobj.xml", mySigsVerificationProfile); + assertThrows(ReferenceValueException.class, () -> { + verifyBadSignature("document.signed.bes.invaliddataobj.xml", mySigsVerificationProfile); + }); } - @Test(expected = SignatureValueException.class) + @Test public void testErrVerifyChangedSigValue() throws Exception { - System.out.println("errVerifyChangedSigValue"); - verifyBadSignature("document.signed.bes.invalidsigvalue.xml", mySigsVerificationProfile); + assertThrows(SignatureValueException.class, () -> { + verifyBadSignature("document.signed.bes.invalidsigvalue.xml", mySigsVerificationProfile); + }); } - @Test(expected = CompleteCertRefsCertNotFoundException.class) + @Test public void testErrVerifyCMissingCertRef() throws Exception { - System.out.println("errVerifyCMissingCertRef"); - verifyBadSignature("document.signed.c.missingcertref.xml", nistVerificationProfile); + assertThrows(CompleteCertRefsCertNotFoundException.class, () -> { + verifyBadSignature("document.signed.c.missingcertref.xml", nistVerificationProfile); + }); } - @Test(expected = TimeStampDigestMismatchException.class) + @Test public void testErrVerifyUnmatchSigTSDigest() throws Exception { -// DefaultTimeStampTokenProvider tsProv = new DefaultTimeStampTokenProvider(new DefaultMessageDigestProvider()); -// byte[] tkn = tsProv.getTimeStampToken("badTimeStamp".getBytes(), Constants.ALGO_ID_DIGEST_SHA1).encodedTimeStampToken; -// -// Document doc = getDocument("document.signed.t.bes.xml"); -// Element encTS = (Element)doc.getElementsByTagNameNS(QualifyingProperty.XADES_XMLNS, "EncapsulatedTimeStamp").item(0); -// encTS.setTextContent(Base64.encodeBytes(tkn)); -// outputDocument(doc, "bad/document.signed.t.bes.badtsdigest.xml"); - - System.out.println("errVerifyUnmatchSigTSDigest"); - verifyBadSignature("document.signed.t.bes.badtsdigest.xml", mySigsVerificationProfile); + // DefaultTimeStampTokenProvider tsProv = new DefaultTimeStampTokenProvider(new DefaultMessageDigestProvider()); + // byte[] tkn = tsProv.getTimeStampToken("badTimeStamp".getBytes(), Constants.ALGO_ID_DIGEST_SHA1).encodedTimeStampToken; + // + // Document doc = getDocument("document.signed.t.bes.xml"); + // Element encTS = (Element)doc.getElementsByTagNameNS(QualifyingProperty.XADES_XMLNS, "EncapsulatedTimeStamp").item(0); + // encTS.setTextContent(Base64.encodeBytes(tkn)); + // outputDocument(doc, "bad/document.signed.t.bes.badtsdigest.xml"); + + assertThrows(TimeStampDigestMismatchException.class, () -> { + verifyBadSignature("document.signed.t.bes.badtsdigest.xml", mySigsVerificationProfile); + }); } - - @Test(expected = CounterSignatureSigValueRefException.class) + + @Test public void testErrVerifyCounterSigWithUnallowedTransforms() throws Exception { - System.out.println("errVerifyCounterSigWithUnallowedTransforms"); - verifyBadSignature("document.signed.bes.cs.invalidtransforms.xml", mySigsVerificationProfile); + assertThrows(CounterSignatureSigValueRefException.class, () -> { + verifyBadSignature("document.signed.bes.cs.invalidtransforms.xml", mySigsVerificationProfile); + }); } private static void verifyBadSignature(String sigFileName, XadesVerificationProfile p) throws Exception diff --git a/src/test/java/xades4j/verification/XadesVerifierImplTest.java b/src/test/java/xades4j/verification/XadesVerifierImplTest.java index 926cf7af..14417b7b 100644 --- a/src/test/java/xades4j/verification/XadesVerifierImplTest.java +++ b/src/test/java/xades4j/verification/XadesVerifierImplTest.java @@ -16,28 +16,14 @@ */ package xades4j.verification; -import java.io.File; - -import static junit.framework.TestCase.assertTrue; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; - -import java.io.FileInputStream; -import java.io.InputStream; -import java.text.SimpleDateFormat; -import java.util.Date; -import java.util.GregorianCalendar; -import java.util.stream.Collectors; - import org.apache.xml.security.signature.XMLSignature; import org.apache.xml.security.utils.resolver.implementations.ResolverDirectHTTP; import org.apache.xml.security.utils.resolver.implementations.ResolverLocalFilesystem; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.NodeList; - import xades4j.algorithms.CanonicalXMLWithoutComments; import xades4j.production.XadesFormatExtenderProfile; import xades4j.production.XadesSignatureFormatExtender; @@ -55,7 +41,18 @@ import xades4j.properties.SigningCertificateProperty; import xades4j.properties.SigningTimeProperty; import xades4j.providers.CannotBuildCertificationPathException; -import xades4j.providers.impl.HttpTsaConfiguration; + +import java.io.File; +import java.io.FileInputStream; +import java.io.InputStream; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.GregorianCalendar; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; /** * @author Luís @@ -65,7 +62,7 @@ public class XadesVerifierImplTest extends VerifierTestBase XadesVerificationProfile verificationProfile; XadesVerificationProfile nistVerificationProfile; - @Before + @BeforeEach public void initialize() { verificationProfile = new XadesVerificationProfile(VerifierTestBase.validationProviderMySigs); @@ -118,7 +115,6 @@ public void testVerifyBES() throws Exception @Test public void testVerifyBESWithoutKeyInfo() throws Exception { - System.out.println("verifyBES"); var result = verifySignature("document.signed.bes.no-ki.xml"); assertEquals(XAdESForm.BES, result.getSignatureForm()); } @@ -127,22 +123,24 @@ public void testVerifyBESWithoutKeyInfo() throws Exception * Try to verify a test xades BES (no timestamp) in year 2041, expect we * can't build the certificate path because certificates are expired. */ - @Test(expected = CannotBuildCertificationPathException.class) + @Test public void testVerifyBESWithVerificationDate() throws Exception { - System.out.println("testVerifyBESWithVerificationDate"); String sigFilename = "document.signed.bes.xml"; Element signatureNode = getSigElement(getDocument(sigFilename)); XadesVerificationProfile p = new XadesVerificationProfile(VerifierTestBase.validationProviderMySigs); - Date verificationDate = new SimpleDateFormat("YYYY").parse("2041"); - p.newVerifier().verify(signatureNode, - new SignatureSpecificVerificationOptions().setDefaultVerificationDate(verificationDate)); + XadesVerifier verifier = p.newVerifier(); + + assertThrows(CannotBuildCertificationPathException.class, () -> { + Date verificationDate = new SimpleDateFormat("YYYY").parse("2041"); + verifier.verify(signatureNode, + new SignatureSpecificVerificationOptions().setDefaultVerificationDate(verificationDate)); + }); } - @Test(expected = InvalidSignatureException.class) + @Test public void testVerifyWithCustomRawVerifier() throws Exception { - System.out.println("verifyWithCustomRawVerifier"); verificationProfile.withRawSignatureVerifier(new RawSignatureVerifier() { @Override @@ -153,8 +151,10 @@ public void verify(RawSignatureVerifierContext ctx) throws InvalidSignatureExcep throw new InvalidSignatureException("Rejected by RawSignatureVerifier"); } }); - var result = verifySignature("document.signed.bes.xml", verificationProfile); - assertEquals(XAdESForm.BES, result.getSignatureForm()); + + assertThrows(InvalidSignatureException.class, () -> { + verifySignature("document.signed.bes.xml", verificationProfile); + }); } @Test @@ -203,7 +203,6 @@ public void testVerifyBESCounterSig() throws Exception @Test public void testVerifyBESCounterSigCounterSig() throws Exception { - System.out.println("verifyBESCounterSigCounterSig"); var result = verifySignature("document.signed.bes.cs.cs.xml"); assertEquals(XAdESForm.BES, result.getSignatureForm()); } @@ -211,8 +210,6 @@ public void testVerifyBESCounterSigCounterSig() throws Exception @Test public void testVerifyBESEnrichT() throws Exception { - System.out.println("verifyBESEnrichT"); - Document doc = getDocument("document.signed.bes.xml"); Element signatureNode = getSigElement(doc); @@ -229,8 +226,6 @@ public void testVerifyBESEnrichT() throws Exception @Test public void testVerifyBESExtrnlResEnrichC() throws Exception { - System.out.println("verifyBESExtrnlResEnrichC"); - Document doc = getDocument("document.signed.bes.extres.xml"); Element signatureNode = getSigElement(doc); SignatureSpecificVerificationOptions options = new SignatureSpecificVerificationOptions() @@ -311,8 +306,6 @@ public void testVerifyC() throws Exception @Test public void testVerifyDetachedC() throws Exception { - System.out.println("verifyDetachedC"); - Document doc = getDocument("detached.c.xml"); Element signatureNode = getSigElement(doc); XadesVerifier verifier = nistVerificationProfile.newVerifier(); @@ -330,8 +323,6 @@ public void testVerifyDetachedC() throws Exception @Test public void testVerifyCEnrichXL() throws Exception { - System.out.println("verifyCEnrichXL"); - Document doc = getDocument("document.signed.c.xml"); Element signatureNode = getSigElement(doc); diff --git a/src/test/java/xades4j/xml/marshalling/algorithms/AlgorithmsParametersMarshallingProviderImplTest.java b/src/test/java/xades4j/xml/marshalling/algorithms/AlgorithmsParametersMarshallingProviderImplTest.java index 35c7ddc9..f1651d39 100644 --- a/src/test/java/xades4j/xml/marshalling/algorithms/AlgorithmsParametersMarshallingProviderImplTest.java +++ b/src/test/java/xades4j/xml/marshalling/algorithms/AlgorithmsParametersMarshallingProviderImplTest.java @@ -16,16 +16,25 @@ */ package xades4j.xml.marshalling.algorithms; -import org.w3c.dom.Node; -import java.util.List; -import xades4j.algorithms.*; -import com.google.inject.Injector; import com.google.inject.Guice; +import com.google.inject.Injector; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import org.w3c.dom.Document; +import org.w3c.dom.Node; +import xades4j.algorithms.Algorithm; +import xades4j.algorithms.CanonicalXMLWithoutComments; +import xades4j.algorithms.EnvelopedSignatureTransform; +import xades4j.algorithms.ExclusiveCanonicalXMLWithComments; +import xades4j.algorithms.GenericAlgorithm; +import xades4j.algorithms.XPathTransform; import xades4j.utils.SignatureServicesTestBase; -import org.junit.Test; -import static org.junit.Assert.*; -import org.junit.Before; + +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; /** * @@ -36,7 +45,7 @@ public class AlgorithmsParametersMarshallingProviderImplTest private Document doc; private AlgorithmsParametersMarshallingProviderImpl sut; - @Before + @BeforeEach public void setUp() throws Exception { doc = SignatureServicesTestBase.getNewDocument(); diff --git a/src/test/java/xades4j/xml/marshalling/algorithms/XPath2FilterTransformParamsMarshallerTest.java b/src/test/java/xades4j/xml/marshalling/algorithms/XPath2FilterTransformParamsMarshallerTest.java index 8f06387d..ac44a976 100644 --- a/src/test/java/xades4j/xml/marshalling/algorithms/XPath2FilterTransformParamsMarshallerTest.java +++ b/src/test/java/xades4j/xml/marshalling/algorithms/XPath2FilterTransformParamsMarshallerTest.java @@ -1,14 +1,8 @@ package xades4j.xml.marshalling.algorithms; -import java.util.List; -import java.util.Map; -import java.util.Set; import org.apache.xml.security.utils.Constants; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotNull; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; @@ -16,6 +10,14 @@ import xades4j.algorithms.XPath2FilterTransform.XPath2Filter; import xades4j.utils.SignatureServicesTestBase; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; + /** * * @author Luís @@ -25,7 +27,7 @@ public class XPath2FilterTransformParamsMarshallerTest { private Document doc; private XPath2FilterTransformParamsMarshaller sut; - @Before + @BeforeEach public void setUp() throws Exception { doc = SignatureServicesTestBase.getNewDocument(); sut = new XPath2FilterTransformParamsMarshaller(); diff --git a/src/test/java/xades4j/xml/marshalling/algorithms/XPathTransformParamsMarshallerTest.java b/src/test/java/xades4j/xml/marshalling/algorithms/XPathTransformParamsMarshallerTest.java index 36a6b877..a93e64ac 100644 --- a/src/test/java/xades4j/xml/marshalling/algorithms/XPathTransformParamsMarshallerTest.java +++ b/src/test/java/xades4j/xml/marshalling/algorithms/XPathTransformParamsMarshallerTest.java @@ -1,29 +1,30 @@ package xades4j.xml.marshalling.algorithms; -import java.util.List; -import java.util.Map; import org.apache.xml.security.utils.Constants; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotNull; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import xades4j.algorithms.XPathTransform; import xades4j.utils.SignatureServicesTestBase; +import java.util.List; +import java.util.Map; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; + /** - * * @author Luís */ -public class XPathTransformParamsMarshallerTest { - +public class XPathTransformParamsMarshallerTest +{ private Document doc; private XPathTransformParamsMarshaller sut; - @Before + @BeforeEach public void setUp() throws Exception { doc = SignatureServicesTestBase.getNewDocument(); @@ -31,7 +32,7 @@ public void setUp() throws Exception } @Test - public void testMarshalXPathParametersWithNamespacePrefixes() throws Exception + public void testMarshalXPathParametersWithNamespacePrefixes() throws Exception { XPathTransform xpath = new XPathTransform("foo:elem1/bar:elem2") .withNamespace("foo", "http://test.xades4j/ns1") @@ -43,7 +44,8 @@ public void testMarshalXPathParametersWithNamespacePrefixes() throws Exception Map namespaces = xpath.getNamespaces(); - for (Map.Entry entry : namespaces.entrySet()) { + for (Map.Entry entry : namespaces.entrySet()) + { String ns = paramNode.getAttributeNS(Constants.NamespaceSpecNS, entry.getKey()); assertNotNull(ns); assertFalse(ns.isEmpty());