pdfbox-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From til...@apache.org
Subject svn commit: r1849559 - /pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/encryption/TestPublicKeyEncryption.java
Date Sat, 22 Dec 2018 18:06:58 GMT
Author: tilman
Date: Sat Dec 22 18:06:57 2018
New Revision: 1849559

URL: http://svn.apache.org/viewvc?rev=1849559&view=rev
Log:
PDFBOX-4413: save to file instead of to baos; compare contents; fix bug in testMultipleRecipients
due to impossibility to save twice due to encryption of everything; use try-with-resources

Modified:
    pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/encryption/TestPublicKeyEncryption.java

Modified: pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/encryption/TestPublicKeyEncryption.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/encryption/TestPublicKeyEncryption.java?rev=1849559&r1=1849558&r2=1849559&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/encryption/TestPublicKeyEncryption.java
(original)
+++ pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/encryption/TestPublicKeyEncryption.java
Sat Dec 22 18:06:57 2018
@@ -16,7 +16,6 @@
  */
 package org.apache.pdfbox.encryption;
 
-import java.io.ByteArrayOutputStream;
 import java.io.File;
 import java.io.IOException;
 import java.io.InputStream;
@@ -31,6 +30,7 @@ import org.apache.pdfbox.pdmodel.PDDocum
 import org.apache.pdfbox.pdmodel.encryption.AccessPermission;
 import org.apache.pdfbox.pdmodel.encryption.PublicKeyProtectionPolicy;
 import org.apache.pdfbox.pdmodel.encryption.PublicKeyRecipient;
+import org.apache.pdfbox.text.PDFTextStripper;
 
 import junit.framework.TestCase;
 
@@ -43,6 +43,7 @@ import org.junit.Assert;
  */
 public class TestPublicKeyEncryption extends TestCase
 {
+    private final File testResultsDir = new File("target/test-output/crypto");
 
     private AccessPermission permission1;
     private AccessPermission permission2;
@@ -61,7 +62,9 @@ public class TestPublicKeyEncryption ext
      */
     private PDDocument document;
 
-    
+    private String text;
+    private String producer;
+
     /**
      * {@inheritDoc}
      */
@@ -102,8 +105,11 @@ public class TestPublicKeyEncryption ext
         
         keyStore1 = "test1.pfx";
         keyStore2 = "test2.pfx";
-        
+
         document = PDDocument.load(new File(this.getClass().getResource("test.pdf").toURI()));
+        text = new PDFTextStripper().getText(document);
+        producer = document.getDocumentInformation().getProducer();
+        document.setVersion(1.7f);
     }
 
     /**
@@ -130,7 +136,8 @@ public class TestPublicKeyEncryption ext
         PDDocument encryptedDoc = null;
         try 
         {
-            encryptedDoc = reload(document, password2, getKeyStore(keyStore2));
+            File file = save("testProtectionError");
+            encryptedDoc = reload(file, password2, getKeyStore(keyStore2));
             Assert.assertTrue(encryptedDoc.isEncrypted());
             fail("No exception when using an incorrect decryption key");
         }
@@ -162,13 +169,12 @@ public class TestPublicKeyEncryption ext
         policy.addRecipient(recipient1);
         document.protect(policy);
 
-        PDDocument encryptedDoc = reload(document, password1, getKeyStore(keyStore1));
-        try 
+        File file = save("testProtection");
+        try (PDDocument encryptedDoc = reload(file, password1, getKeyStore(keyStore1)))
         {
             Assert.assertTrue(encryptedDoc.isEncrypted());
 
-            AccessPermission permission =
-                encryptedDoc.getCurrentAccessPermission();
+            AccessPermission permission = encryptedDoc.getCurrentAccessPermission();
             Assert.assertFalse(permission.canAssembleDocument());
             Assert.assertFalse(permission.canExtractContent());
             Assert.assertTrue(permission.canExtractForAccessibility());
@@ -177,10 +183,6 @@ public class TestPublicKeyEncryption ext
             Assert.assertFalse(permission.canModifyAnnotations());
             Assert.assertFalse(permission.canPrint());
             Assert.assertFalse(permission.canPrintDegraded());
-        } 
-        finally 
-        {
-            encryptedDoc.close();
         }
     }
 
@@ -198,11 +200,10 @@ public class TestPublicKeyEncryption ext
         document.protect(policy);
 
         // open first time
-        PDDocument encryptedDoc1 = reload(document, password1, getKeyStore(keyStore1));
-        try 
+        File file = save("testMultipleRecipients");
+        try (PDDocument encryptedDoc1 = reload(file, password1, getKeyStore(keyStore1)))
         {
-            AccessPermission permission =
-                encryptedDoc1.getCurrentAccessPermission();
+            AccessPermission permission = encryptedDoc1.getCurrentAccessPermission();
             Assert.assertFalse(permission.canAssembleDocument());
             Assert.assertFalse(permission.canExtractContent());
             Assert.assertTrue(permission.canExtractForAccessibility());
@@ -211,18 +212,12 @@ public class TestPublicKeyEncryption ext
             Assert.assertFalse(permission.canModifyAnnotations());
             Assert.assertFalse(permission.canPrint());
             Assert.assertFalse(permission.canPrintDegraded());
-        } 
-        finally 
-        {
-            encryptedDoc1.close();
         }
 
         // open second time
-        PDDocument encryptedDoc2 = reload(document, password2, getKeyStore(keyStore2));
-        try 
+        try (PDDocument encryptedDoc2 = reload(file, password2, getKeyStore(keyStore2)))
         {
-            AccessPermission permission =
-                encryptedDoc2.getCurrentAccessPermission();
+            AccessPermission permission = encryptedDoc2.getCurrentAccessPermission();
             Assert.assertFalse(permission.canAssembleDocument());
             Assert.assertFalse(permission.canExtractContent());
             Assert.assertTrue(permission.canExtractForAccessibility());
@@ -231,10 +226,6 @@ public class TestPublicKeyEncryption ext
             Assert.assertFalse(permission.canModifyAnnotations());
             Assert.assertTrue(permission.canPrint());
             Assert.assertFalse(permission.canPrintDegraded());
-        } 
-        finally 
-        {
-            encryptedDoc2.close();
         }
     }
 
@@ -242,19 +233,24 @@ public class TestPublicKeyEncryption ext
      * Reloads the given document by writing it to a temporary byte array
      * and loading a fresh document from that byte array.
      *
-     * @param doc input document
+     * @param file input file
      * @param decryptionPassword password to be used to decrypt the doc
      * @param keyStore password to be used to decrypt the doc
      * @return reloaded document
      * @throws Exception if 
      */
-    private PDDocument reload(PDDocument doc, String decryptionPassword, InputStream keyStore)
+    private PDDocument reload(File file, String decryptionPassword, InputStream keyStore)
             throws IOException, NoSuchAlgorithmException
     {
-        ByteArrayOutputStream baos = new ByteArrayOutputStream();
-        doc.save(baos);
-        return PDDocument.load(baos.toByteArray(), decryptionPassword,
+        PDDocument doc2 = PDDocument.load(file, decryptionPassword,
                 keyStore, null, MemoryUsageSetting.setupMainMemoryOnly());
+        Assert.assertEquals("Extracted text is different",
+                                text,
+                                new PDFTextStripper().getText(doc2));
+        Assert.assertEquals("Producer is different",
+                                producer,
+                                doc2.getDocumentInformation().getProducer());
+        return doc2;
     }
 
     /**
@@ -266,21 +262,15 @@ public class TestPublicKeyEncryption ext
      * @return recipient specification
      * @throws Exception if the certificate could not be read
      */
-    private PublicKeyRecipient getRecipient(String certificate, AccessPermission permission)
throws Exception 
+    private PublicKeyRecipient getRecipient(String certificate, AccessPermission permission)
throws Exception
     {
-        InputStream input = TestPublicKeyEncryption.class.getResourceAsStream(certificate);
-        try 
+        try (InputStream input = TestPublicKeyEncryption.class.getResourceAsStream(certificate))
         {
             CertificateFactory factory = CertificateFactory.getInstance("X.509");
             PublicKeyRecipient recipient = new PublicKeyRecipient();
             recipient.setPermission(permission);
-            recipient.setX509(
-                    (X509Certificate) factory.generateCertificate(input));
+            recipient.setX509((X509Certificate) factory.generateCertificate(input));
             return recipient;
-        } 
-        finally 
-        {
-            input.close();
         }
     }
 
@@ -288,4 +278,12 @@ public class TestPublicKeyEncryption ext
     {
         return TestPublicKeyEncryption.class.getResourceAsStream(name);
     }
-}
+
+    private File save(String name) throws IOException
+    {
+        int keyLength = document.getEncryption().getSecurityHandler().getKeyLength();
+        File file = new File(testResultsDir, name + "-" + keyLength + "bit.pdf");
+        document.save(file);
+        return file;
+    }
+}
\ No newline at end of file



Mime
View raw message