You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pdfbox.apache.org by ti...@apache.org on 2018/07/09 18:23:41 UTC

svn commit: r1835467 - /pdfbox/trunk/examples/src/test/java/org/apache/pdfbox/examples/pdmodel/TestCreateSignature.java

Author: tilman
Date: Mon Jul  9 18:23:41 2018
New Revision: 1835467

URL: http://svn.apache.org/viewvc?rev=1835467&view=rev
Log:
PDFBOX-4263: check that one object number remains identical before and after signing

Modified:
    pdfbox/trunk/examples/src/test/java/org/apache/pdfbox/examples/pdmodel/TestCreateSignature.java

Modified: pdfbox/trunk/examples/src/test/java/org/apache/pdfbox/examples/pdmodel/TestCreateSignature.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/examples/src/test/java/org/apache/pdfbox/examples/pdmodel/TestCreateSignature.java?rev=1835467&r1=1835466&r2=1835467&view=diff
==============================================================================
--- pdfbox/trunk/examples/src/test/java/org/apache/pdfbox/examples/pdmodel/TestCreateSignature.java (original)
+++ pdfbox/trunk/examples/src/test/java/org/apache/pdfbox/examples/pdmodel/TestCreateSignature.java Mon Jul  9 18:23:41 2018
@@ -37,6 +37,7 @@ import java.util.List;
 import javax.xml.bind.DatatypeConverter;
 
 import org.apache.pdfbox.cos.COSName;
+import org.apache.pdfbox.cos.COSObject;
 import org.apache.pdfbox.cos.COSString;
 import org.apache.pdfbox.examples.signature.CreateEmptySignatureForm;
 import org.apache.pdfbox.examples.signature.CreateSignature;
@@ -123,7 +124,7 @@ public class TestCreateSignature
         final String fileName = getOutputFileName("signed{0}.pdf");
         signing.signDetached(new File(inDir + "sign_me.pdf"), new File(outDir + fileName));
 
-        checkSignature(new File(outDir + fileName));
+        checkSignature(new File(inDir, "sign_me.pdf"), new File(outDir, fileName));
     }
 
     /**
@@ -212,7 +213,7 @@ public class TestCreateSignature
             signing.signPDF(new File(inPath), destFile, null);
         }
 
-        checkSignature(destFile);
+        checkSignature(new File(inPath), destFile);
     }
 
     /**
@@ -254,7 +255,7 @@ public class TestCreateSignature
         signing1.setExternalSigning(false);
         signing1.signDetached(new File(filename), new File(filenameSigned1));
 
-        checkSignature(new File(filenameSigned1));
+        checkSignature(new File(filename), new File(filenameSigned1));
 
         try (PDDocument doc1 = PDDocument.load(new File(filenameSigned1)))
         {
@@ -272,7 +273,7 @@ public class TestCreateSignature
             signing2.signPDF(new File(filenameSigned1), new File(filenameSigned2), null, "Signature1");
         }
 
-        checkSignature(new File(filenameSigned2));
+        checkSignature(new File(filenameSigned1), new File(filenameSigned2));
 
         try (PDDocument doc2 = PDDocument.load(new File(filenameSigned2)))
         {
@@ -287,11 +288,20 @@ public class TestCreateSignature
     }
 
     // This check fails with a file created with the code before PDFBOX-3011 was solved.
-    private void checkSignature(File file)
+    private void checkSignature(File origFile, File signedFile)
             throws IOException, CMSException, OperatorCreationException, GeneralSecurityException
     {
-        try (PDDocument document = PDDocument.load(file))
+        String origPageKey;
+        try (PDDocument document = PDDocument.load(origFile))
         {
+            // get string representation of pages COSObject
+            origPageKey = ((COSObject) document.getDocumentCatalog().getCOSObject().getItem(COSName.PAGES)).toString();
+        }
+        try (PDDocument document = PDDocument.load(signedFile))
+        {
+            // PDFBOX-4261: check that object number stays the same 
+            Assert.assertEquals(origPageKey, document.getDocumentCatalog().getCOSObject().getItem(COSName.PAGES).toString());
+
             List<PDSignature> signatureDictionaries = document.getSignatureDictionaries();
             if (signatureDictionaries.isEmpty())
             {
@@ -301,7 +311,7 @@ public class TestCreateSignature
             {
                 COSString contents = (COSString) sig.getCOSObject().getDictionaryObject(COSName.CONTENTS);
                 byte[] buf;
-                try (FileInputStream fis = new FileInputStream(file))
+                try (FileInputStream fis = new FileInputStream(signedFile))
                 {
                     buf = sig.getSignedContent(fis);
                 }