From c085bb68cbd15e5c8cf0ce23e7c8104b08fdf238 Mon Sep 17 00:00:00 2001 From: Andreas Fitzek Date: Thu, 15 May 2014 12:46:40 +0200 Subject: Signature Profile Test --- .../egiz/pdfas/cli/test/SignaturProfileTest.java | 123 +++++++++++++++++++++ 1 file changed, 123 insertions(+) create mode 100644 pdf-as-cli/src/test/java/at/gv/egiz/pdfas/cli/test/SignaturProfileTest.java (limited to 'pdf-as-cli/src/test/java') diff --git a/pdf-as-cli/src/test/java/at/gv/egiz/pdfas/cli/test/SignaturProfileTest.java b/pdf-as-cli/src/test/java/at/gv/egiz/pdfas/cli/test/SignaturProfileTest.java new file mode 100644 index 00000000..86a2e796 --- /dev/null +++ b/pdf-as-cli/src/test/java/at/gv/egiz/pdfas/cli/test/SignaturProfileTest.java @@ -0,0 +1,123 @@ +package at.gv.egiz.pdfas.cli.test; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +import sun.misc.IOUtils; +import at.gv.egiz.pdfas.common.settings.ISettings; +import at.gv.egiz.pdfas.lib.api.ByteArrayDataSink; +import at.gv.egiz.pdfas.lib.api.ByteArrayDataSource; +import at.gv.egiz.pdfas.lib.api.Configuration; +import at.gv.egiz.pdfas.lib.api.DataSource; +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; + +public class SignaturProfileTest { + + public static final String sourcePDFA = "/home/afitzek/tmp/pdfProblem/PDFA/TestGhostscriptPdfA.pdf"; + public static final String sourcePDF = "/home/afitzek/simple.pdf"; + public static final String targetFolder = "/home/afitzek/tmp/sigres/"; + + public static final String KS_FILE = "/home/afitzek/devel/pdfas_neu/test.p12"; + 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"; + + /* + * -p SIGNATURBLOCK_SMALL_DE -c ks -m sign -ksf + * /home/afitzek/devel/pdfas_neu/test.p12 -kst PKCS12 -ksa ecc_test -kskp + * 123456 -kssp 123456 /home/afitzek/simple2.pdf + */ + + public static void main(String[] args) { + String user_home = System.getProperty("user.home"); + String pdfas_dir = user_home + File.separator + ".pdfas"; + PdfAs pdfas = PdfAsFactory.createPdfAs(new File(pdfas_dir)); + try { + Configuration config = pdfas.getConfiguration(); + ISettings settings = (ISettings) config; + List signatureProfiles = new ArrayList(); + + List signaturePDFAProfiles = new ArrayList(); + + Iterator itKeys = settings.getFirstLevelKeys( + "sig_obj.types.").iterator(); + while (itKeys.hasNext()) { + String key = itKeys.next(); + String profile = key.substring("sig_obj.types.".length()); + System.out.println("[" + profile + "]: " + + settings.getValue(key)); + if (settings.getValue(key).equals("on")) { + signatureProfiles.add(profile); + if(profile.contains("PDFA")) { + signaturePDFAProfiles.add(profile); + } + } + } + + byte[] input = IOUtils.readFully(new FileInputStream(sourcePDF), + -1, true); + + IPlainSigner signer = new PAdESSignerKeystore(KS_FILE, KS_ALIAS, KS_PASS, KS_KEY_PASS, KS_TYPE); + + Iterator itProfiles = signatureProfiles.iterator(); + while (itProfiles.hasNext()) { + String profile = itProfiles.next(); + System.out.println("Testing " + profile); + + DataSource source = new ByteArrayDataSource(input); + ByteArrayDataSink sink = new ByteArrayDataSink(); + + SignParameter signParameter = PdfAsFactory.createSignParameter( + config, source); + + signParameter.setPlainSigner(signer); + signParameter.setOutput(sink); + signParameter.setSignatureProfileId(profile); + + SignResult result = pdfas.sign(signParameter); + + FileOutputStream fos = new FileOutputStream(targetFolder + profile + ".pdf"); + fos.write(sink.getData()); + fos.close(); + } + + byte[] inputPDFA = IOUtils.readFully(new FileInputStream(sourcePDFA), + -1, true); + + Iterator itPDFAProfiles = signaturePDFAProfiles.iterator(); + while (itPDFAProfiles.hasNext()) { + String profile = itPDFAProfiles.next(); + System.out.println("Testing " + profile); + + DataSource source = new ByteArrayDataSource(inputPDFA); + ByteArrayDataSink sink = new ByteArrayDataSink(); + + SignParameter signParameter = PdfAsFactory.createSignParameter( + config, source); + + signParameter.setPlainSigner(signer); + signParameter.setOutput(sink); + signParameter.setSignatureProfileId(profile); + + SignResult result = pdfas.sign(signParameter); + + FileOutputStream fos = new FileOutputStream(targetFolder + "PDFA_" + profile + ".pdf"); + fos.write(sink.getData()); + fos.close(); + } + } catch (Throwable e) { + e.printStackTrace(); + } + } + +} -- cgit v1.2.3