aboutsummaryrefslogtreecommitdiff
path: root/pdf-as-cli/src/test/java
diff options
context:
space:
mode:
authorAlexander Marsalek <amarsalek@iaik.tugraz.at>2021-04-14 23:19:57 +0200
committerAlexander Marsalek <amarsalek@iaik.tugraz.at>2021-04-14 23:19:57 +0200
commit7be260ac248ee9f461f0e1384a5ba527f28cfd6d (patch)
tree3fcc3b42b516940be0ca26cd62024012002c1047 /pdf-as-cli/src/test/java
parent6c14c17f9e0bedd88c435f4e17661f1765e1820e (diff)
downloadpdf-as-4-7be260ac248ee9f461f0e1384a5ba527f28cfd6d.tar.gz
pdf-as-4-7be260ac248ee9f461f0e1384a5ba527f28cfd6d.tar.bz2
pdf-as-4-7be260ac248ee9f461f0e1384a5ba527f28cfd6d.zip
added test
Diffstat (limited to 'pdf-as-cli/src/test/java')
-rw-r--r--pdf-as-cli/src/test/java/at/gv/egiz/pdfas/cli/test/SignaturBlockParameterTest.java220
1 files changed, 220 insertions, 0 deletions
diff --git a/pdf-as-cli/src/test/java/at/gv/egiz/pdfas/cli/test/SignaturBlockParameterTest.java b/pdf-as-cli/src/test/java/at/gv/egiz/pdfas/cli/test/SignaturBlockParameterTest.java
new file mode 100644
index 00000000..4bce7b05
--- /dev/null
+++ b/pdf-as-cli/src/test/java/at/gv/egiz/pdfas/cli/test/SignaturBlockParameterTest.java
@@ -0,0 +1,220 @@
+/*******************************************************************************
+ * <copyright> Copyright 2014 by E-Government Innovation Center EGIZ, Graz, Austria </copyright>
+ * PDF-AS has been contracted by the E-Government Innovation Center EGIZ, a
+ * joint initiative of the Federal Chancellery Austria and Graz University of
+ * Technology.
+ *
+ * Licensed under the EUPL, Version 1.1 or - as soon they will be approved by
+ * the European Commission - subsequent versions of the EUPL (the "Licence");
+ * You may not use this work except in compliance with the Licence.
+ * You may obtain a copy of the Licence at:
+ * http://www.osor.eu/eupl/
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the Licence is distributed on an "AS IS" basis,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the Licence for the specific language governing permissions and
+ * limitations under the Licence.
+ *
+ * This product combines work with different licenses. See the "NOTICE" text
+ * file for details on the various modules and licenses.
+ * The "NOTICE" text file is part of the distribution. Any derivative works
+ * that you distribute must include a readable copy of the "NOTICE" text file.
+ ******************************************************************************/
+package at.gv.egiz.pdfas.cli.test;
+
+import at.gv.egiz.pdfas.common.exceptions.PDFASError;
+import at.gv.egiz.pdfas.common.exceptions.PdfAsException;
+import at.gv.egiz.pdfas.lib.api.ByteArrayDataSource;
+import at.gv.egiz.pdfas.lib.api.Configuration;
+import at.gv.egiz.pdfas.lib.api.PdfAs;
+import at.gv.egiz.pdfas.lib.api.PdfAsFactory;
+import at.gv.egiz.pdfas.lib.api.sign.IPlainSigner;
+import at.gv.egiz.pdfas.lib.api.sign.SignParameter;
+import at.gv.egiz.pdfas.lib.api.sign.SignResult;
+import at.gv.egiz.pdfas.sigs.pades.PAdESSignerKeystore;
+import org.apache.commons.io.IOUtils;
+import org.apache.pdfbox.pdmodel.PDDocument;
+import org.apache.pdfbox.pdmodel.interactive.digitalsignature.PDSignature;
+import org.apache.pdfbox.pdmodel.interactive.form.PDAcroForm;
+import org.apache.pdfbox.pdmodel.interactive.form.PDField;
+import org.apache.pdfbox.pdmodel.interactive.form.PDSignatureField;
+import org.junit.Assert;
+import org.junit.Test;
+
+import javax.activation.DataSource;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+public class SignaturBlockParameterTest {
+
+ public static final String KS_ALIAS = "ecc_test";
+ public static final String KS_TYPE = "PKCS12";
+ public static final String KS_PASS = "123456";
+ public static final String KS_KEY_PASS = "123456";
+
+ public String getPath(String resourceName) {
+ ClassLoader classLoader = this.getClass().getClassLoader();
+ File file = new File(classLoader.getResource(resourceName).getFile());
+ String absolutePath = file.getAbsolutePath();
+
+ System.out.println(absolutePath);
+ return absolutePath;
+ }
+
+ @Test
+ public void test() throws IOException, PDFASError, PdfAsException {
+
+ PdfAs pdfas = PdfAsFactory.createPdfAs(new File(getPath("pdfas-config")));
+ Configuration config = pdfas.getConfiguration();
+
+
+ byte[] input = IOUtils.toByteArray(new FileInputStream(getPath("simple_rotated_0.pdf")));
+
+ IPlainSigner signer = new PAdESSignerKeystore(getPath("test.p12"), KS_ALIAS, KS_PASS, KS_KEY_PASS, KS_TYPE);
+
+ String profile = "SIGNATURBLOCK_DE_NOTE_DYNAMIC";
+ System.out.println("Testing " + profile);
+
+ DataSource source = new ByteArrayDataSource(input);
+ String outFile = getPath("out") + "/" + profile + ".pdf";
+ FileOutputStream fos = new FileOutputStream(outFile);
+ SignParameter signParameter = PdfAsFactory.createSignParameter(
+ config, source, fos);
+
+ Map<String, String> map = new HashMap<>();
+ map.put("subject", "TEST123");
+ map.put("foo", "bar");
+ signParameter.setDynamicSignatureBlockArguments(map);
+ signParameter.setPlainSigner(signer);
+ signParameter.setSignatureProfileId(profile);
+
+ SignResult result = pdfas.sign(signParameter);
+
+ fos.close();
+ String name = getName(outFile, "PDF-AS Signatur1");
+ Assert.assertEquals("TEST123 test bar 123 c TEST123 Andreas Fitzek ECC", name);
+
+
+ outFile = getPath("out") + "/" + profile + "-1.pdf";
+ fos = new FileOutputStream(outFile);
+ signParameter = PdfAsFactory.createSignParameter(
+ config, source, fos);
+
+ map = new HashMap<>();
+ map.put("subject", "TEST123");
+// map.put("foo", "bar");
+ signParameter.setDynamicSignatureBlockArguments(map);
+ signParameter.setPlainSigner(signer);
+ signParameter.setSignatureProfileId(profile);
+
+ result = pdfas.sign(signParameter);
+
+ fos.close();
+ name = getName(outFile, "PDF-AS Signatur1");
+ Assert.assertEquals("TEST123 test null 123 c TEST123 Andreas Fitzek ECC", name);
+
+ outFile = getPath("out") + "/" + profile + "-2.pdf";
+ fos = new FileOutputStream(outFile);
+ signParameter = PdfAsFactory.createSignParameter(
+ config, source, fos);
+ map = new HashMap<>();
+// map.put("subject", "TEST123");
+ map.put("foo", "bar");
+ signParameter.setDynamicSignatureBlockArguments(map);
+ signParameter.setPlainSigner(signer);
+ signParameter.setSignatureProfileId(profile);
+ result = pdfas.sign(signParameter);
+ fos.close();
+ name = getName(outFile, "PDF-AS Signatur1");
+ Assert.assertEquals("null test bar 123 c null Andreas Fitzek ECC", name);
+
+ outFile = getPath("out") + "/" + profile + "-3.pdf";
+ fos = new FileOutputStream(outFile);
+ signParameter = PdfAsFactory.createSignParameter(
+ config, source, fos);
+// map = new HashMap<>();
+// map.put("subject", "TEST123");
+// map.put("foo", "bar");
+// signParameter.setDynamicSignatureBlockArguments(map);
+ signParameter.setPlainSigner(signer);
+ signParameter.setSignatureProfileId(profile);
+ result = pdfas.sign(signParameter);
+ fos.close();
+ name = getName(outFile, "PDF-AS Signatur1");
+ Assert.assertEquals("{sbp.subject} test {sbp.foo} 123 {subject.T != null ? (subject.T + \" a \"+sbp.subject) : " +
+ "\"c \"+sbp.subject+\" \"}Andreas Fitzek ECC", name);
+//TODO gewünscht?
+
+
+ outFile = getPath("out") + "/" + profile + "-4.pdf";
+ fos = new FileOutputStream(outFile);
+ signParameter = PdfAsFactory.createSignParameter(
+ config, source, fos);
+ map = new HashMap<>();
+ signParameter.setDynamicSignatureBlockArguments(map);
+ signParameter.setPlainSigner(signer);
+ signParameter.setSignatureProfileId(profile);
+ result = pdfas.sign(signParameter);
+ fos.close();
+ name = getName(outFile, "PDF-AS Signatur1");
+ Assert.assertEquals("null test null 123 c null Andreas Fitzek ECC", name);
+
+
+ }
+
+ private String getName(String fileName, String sigFieldName) throws IOException {
+ PDDocument pdDoc = PDDocument.load(new File(fileName));
+ PDSignature signature = null;
+ PDSignatureField signatureField;
+ PDAcroForm acroForm = pdDoc.getDocumentCatalog().getAcroForm();
+ if (acroForm != null) {
+ List<PDField> aa = acroForm.getFields();
+ signatureField = (PDSignatureField) acroForm.getField(sigFieldName);
+ if (signatureField != null) {
+ // retrieve signature dictionary
+ signature = signatureField.getSignature();
+ if (signature != null) {
+ String name = signature.getName();
+ return name;
+
+ }
+
+ }
+ }
+ return null;
+ }
+// public void getStringsFromPDF(String fileName) {
+//// PDFParser parser = null;
+// PDDocument pdDoc = null;
+//// COSDocument cosDoc = null;
+// PDFTextStripper pdfStripper;
+//
+// String parsedText;
+// try {
+//
+// findExistingSignature(pdDoc,"PDF-AS Signatur1");
+// pdfStripper = new PDFTextStripper();
+//
+//
+// parsedText = pdfStripper.getText(pdDoc);
+// System.out.println(parsedText.replaceAll("[^A-Za-z0-9. ]+", ""));
+// } catch (Exception e) {
+// e.printStackTrace();
+// try {
+//// if (cosDoc != null)
+//// cosDoc.close();
+// if (pdDoc != null)
+// pdDoc.close();
+// } catch (Exception e1) {
+// e1.printStackTrace();
+// }
+//
+// }
+// }
+}