Skip to content

Commit

Permalink
Surefire and Maven updates
Browse files Browse the repository at this point in the history
- Disable failing test, because of missing font
  • Loading branch information
asturio committed Jan 8, 2024
1 parent 41d7f29 commit 75b23a1
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.io.FileOutputStream;
import java.io.IOException;

import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;

import com.lowagie.text.Chunk;
Expand All @@ -11,7 +12,11 @@
import com.lowagie.text.FontFactory;

class PdfDocumentCJKExtensionTest {

// This test is actually only an example. It is disabled because the needed font is not in the repository and
// requires 45 Mb of space.
@Test
@Disabled("This test requires a font file.")
void generateDocumentsWithCJKExtension() throws IOException {
String fontName = "TakaoMjMincho";

Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
package com.lowagie.text.pdf.sign;

import static org.assertj.core.api.Assertions.assertThat;

import com.lowagie.text.pdf.AcroFields;
import com.lowagie.text.pdf.PdfPKCS7;
import com.lowagie.text.pdf.PdfPKCS7.X509Name;
import com.lowagie.text.pdf.PdfReader;
import java.security.KeyStore;
import java.security.cert.Certificate;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import com.lowagie.text.pdf.AcroFields;
import com.lowagie.text.pdf.PdfPKCS7;
import com.lowagie.text.pdf.PdfReader;
import org.junit.jupiter.api.Test;

class ExtractCertificatesTest {
Expand All @@ -21,29 +22,26 @@ class ExtractCertificatesTest {

@Test
void testSha1() throws Exception {
extract("src/test/resources/sample_signed-sha1.pdf");
extract("src/test/resources/sample_signed-sha1.pdf", false);
}

@Test
void testSha512() throws Exception {
extract("src/test/resources/sample_signed-sha512.pdf");
extract("src/test/resources/sample_signed-sha512.pdf", false);
}

/**
* Extract certificates and validate timestamp
* Sample file taken from https://www.tecxoft.com/samples.php (it has signature with timestamp with SHA-256)
* NB: Signature will only be valid till 2022/01/02 (Hence only Sout is used not assert)
*
* @throws Exception
* Extract certificates and validate timestamp Sample file taken from <a
* href="https://www.tecxoft.com/samples.php">...</a> (it has signature with timestamp with SHA-256) NB: Signature
* will only be valid till 2022/01/02 (Hence only Sout is used not assert)
*/
@Test
void testSha256TimeStamp() throws Exception {
extract("src/test/resources/pdf_digital_signature_timestamp.pdf");
extract("src/test/resources/pdf_digital_signature_timestamp.pdf", true);
}

private void extract(String pdf) throws Exception {
private void extract(String pdf, boolean isExpectedValidTimeStamp) throws Exception {

List<X509Certificate> certificates = new ArrayList<>();
System.out.println("pdf name: " + pdf);

KeyStore kall = PdfPKCS7.loadCacertsKeyStore();
Expand All @@ -52,29 +50,31 @@ private void extract(String pdf) throws Exception {
AcroFields fields = reader.getAcroFields();

List<String> signatures = fields.getSignedFieldNames();
System.out.println("Signs: " + signatures.size());
assertThat(signatures).isNotEmpty().hasSize(1);
for (String signature : signatures) {

System.out.println("Signature name: " + signature);
System.out.println("Signature covers whole document: " + fields.signatureCoversWholeDocument(signature));
System.out.println(
"Document revision: " + fields.getRevision(signature) + " of " + fields.getTotalRevisions());

assertThat(signature).isNotEmpty();
boolean isWholeDocumentCovered = fields.signatureCoversWholeDocument(signature);
assertThat(isWholeDocumentCovered).isTrue();
assertThat(fields.getRevision(signature)).isEqualTo(1);
assertThat(fields.getTotalRevisions()).isEqualTo(1);
PdfPKCS7 pk = fields.verifySignature(signature);
Calendar cal = pk.getSignDate();
Certificate[] pkc = pk.getCertificates();
X509Certificate certificate = pk.getSigningCertificate();
certificates.add(certificate);
System.out.println("sign date:" + cal.getTime());
System.out.println("Subject: " + PdfPKCS7.getSubjectFields(certificate));
System.out.println("Document modified: " + !pk.verify());
System.out.println("Timestamp valid: " + pk.verifyTimestampImprint());
assertThat(cal).isLessThan(Calendar.getInstance());
X509Name subjectFields = PdfPKCS7.getSubjectFields(certificate);
assertThat(subjectFields).isNotNull();
assertThat(subjectFields.getAllFields()).isNotEmpty()
.containsKey("C");
assertThat(pk.verify()).isTrue();
assertThat(pk.verifyTimestampImprint()).isEqualTo(isExpectedValidTimeStamp);

Object[] fails = PdfPKCS7.verifyCertificates(pkc, kall, null, cal);
if (fails == null) {
System.out.println("Certificates verified against the KeyStore");
} else {
System.out.println("Certificate failed: " + fails[1]);
System.err.println("Certificate failed: " + fails[1]);
}

}
Expand Down

0 comments on commit 75b23a1

Please sign in to comment.