You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pdfbox.apache.org by ms...@apache.org on 2020/11/17 18:31:51 UTC
svn commit: r1883552 - in
/pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/encryption:
TestPublicKeyEncryption.java TestSymmetricKeyEncryption.java
Author: msahyoun
Date: Tue Nov 17 18:31:51 2020
New Revision: 1883552
URL: http://svn.apache.org/viewvc?rev=1883552&view=rev
Log:
PDFBOX-5017: switch to JUnit5
Modified:
pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/encryption/TestPublicKeyEncryption.java
pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/encryption/TestSymmetricKeyEncryption.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=1883552&r1=1883551&r2=1883552&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 Tue Nov 17 18:31:51 2020
@@ -16,6 +16,11 @@
*/
package org.apache.pdfbox.encryption;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.junit.jupiter.api.Assertions.fail;
+
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
@@ -34,14 +39,10 @@ import org.apache.pdfbox.pdmodel.encrypt
import org.apache.pdfbox.pdmodel.encryption.PublicKeyProtectionPolicy;
import org.apache.pdfbox.pdmodel.encryption.PublicKeyRecipient;
import org.apache.pdfbox.text.PDFTextStripper;
-
-import org.junit.After;
-import org.junit.Assert;
-import static org.junit.Assert.fail;
-import org.junit.Before;
-import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.junit.runners.Parameterized;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.MethodSource;
/**
* Tests for public key encryption. These tests are not perfect - to be sure, encrypt a file by
@@ -50,7 +51,6 @@ import org.junit.runners.Parameterized;
*
* @author Ben Litchfield
*/
-@RunWith(Parameterized.class)
public class TestPublicKeyEncryption
{
private final File testResultsDir = new File("target/test-output/crypto");
@@ -75,7 +75,6 @@ public class TestPublicKeyEncryption
private String text;
private String producer;
- @Parameterized.Parameter
public int keyLength;
/**
@@ -83,29 +82,24 @@ public class TestPublicKeyEncryption
*
* @return
*/
- @Parameterized.Parameters
public static Collection<Integer> keyLengths()
{
return Arrays.asList(40, 128, 256);
}
- public TestPublicKeyEncryption()
- {
- testResultsDir.mkdirs();
- }
-
/**
* {@inheritDoc}
*/
- @Before
- public void setUp() throws Exception
+ @BeforeEach
+ void setUp() throws Exception
{
if (Cipher.getMaxAllowedKeyLength("AES") != Integer.MAX_VALUE)
{
// we need strong encryption for these tests
fail("JCE unlimited strength jurisdiction policy files are not installed");
}
-
+ testResultsDir.mkdirs();
+
permission1 = new AccessPermission();
permission1.setCanAssembleDocument(false);
permission1.setCanExtractContent(false);
@@ -144,7 +138,7 @@ public class TestPublicKeyEncryption
/**
* {@inheritDoc}
*/
- @After
+ @AfterEach
public void tearDown() throws Exception
{
document.close();
@@ -156,8 +150,9 @@ public class TestPublicKeyEncryption
*
* @throws Exception If there is an unexpected error during the test.
*/
- @Test
- public void testProtectionError() throws Exception
+ @ParameterizedTest
+ @MethodSource("keyLengths")
+ public void testProtectionError(int keyLength) throws Exception
{
PublicKeyProtectionPolicy policy = new PublicKeyProtectionPolicy();
policy.addRecipient(recipient1);
@@ -169,14 +164,13 @@ public class TestPublicKeyEncryption
{
File file = save("testProtectionError");
encryptedDoc = reload(file, password2, getKeyStore(keyStore2));
- Assert.assertTrue(encryptedDoc.isEncrypted());
+ assertTrue(encryptedDoc.isEncrypted());
fail("No exception when using an incorrect decryption key");
}
catch (IOException ex)
{
String msg = ex.getMessage();
- Assert.assertTrue("not the expected exception: " + msg,
- msg.contains("serial-#: rid 2 vs. cert 3"));
+ assertTrue(msg.contains("serial-#: rid 2 vs. cert 3"), "not the expected exception: " + msg);
}
finally
{
@@ -194,8 +188,9 @@ public class TestPublicKeyEncryption
*
* @throws Exception If there is an unexpected error during the test.
*/
- @Test
- public void testProtection() throws Exception
+ @ParameterizedTest
+ @MethodSource("keyLengths")
+ public void testProtection(int keyLength) throws Exception
{
PublicKeyProtectionPolicy policy = new PublicKeyProtectionPolicy();
policy.addRecipient(recipient1);
@@ -205,17 +200,17 @@ public class TestPublicKeyEncryption
File file = save("testProtection");
try (PDDocument encryptedDoc = reload(file, password1, getKeyStore(keyStore1)))
{
- Assert.assertTrue(encryptedDoc.isEncrypted());
+ assertTrue(encryptedDoc.isEncrypted());
AccessPermission permission = encryptedDoc.getCurrentAccessPermission();
- Assert.assertFalse(permission.canAssembleDocument());
- Assert.assertFalse(permission.canExtractContent());
- Assert.assertTrue(permission.canExtractForAccessibility());
- Assert.assertFalse(permission.canFillInForm());
- Assert.assertFalse(permission.canModify());
- Assert.assertFalse(permission.canModifyAnnotations());
- Assert.assertFalse(permission.canPrint());
- Assert.assertFalse(permission.canPrintDegraded());
+ assertFalse(permission.canAssembleDocument());
+ assertFalse(permission.canExtractContent());
+ assertTrue(permission.canExtractForAccessibility());
+ assertFalse(permission.canFillInForm());
+ assertFalse(permission.canModify());
+ assertFalse(permission.canModifyAnnotations());
+ assertFalse(permission.canPrint());
+ assertFalse(permission.canPrintDegraded());
}
}
@@ -225,8 +220,9 @@ public class TestPublicKeyEncryption
*
* @throws Exception If there is an error during the test.
*/
- @Test
- public void testMultipleRecipients() throws Exception
+ @ParameterizedTest
+ @MethodSource("keyLengths")
+ public void testMultipleRecipients(int keyLength) throws Exception
{
PublicKeyProtectionPolicy policy = new PublicKeyProtectionPolicy();
policy.addRecipient(recipient1);
@@ -239,28 +235,28 @@ public class TestPublicKeyEncryption
try (PDDocument encryptedDoc1 = reload(file, password1, getKeyStore(keyStore1)))
{
AccessPermission permission = encryptedDoc1.getCurrentAccessPermission();
- Assert.assertFalse(permission.canAssembleDocument());
- Assert.assertFalse(permission.canExtractContent());
- Assert.assertTrue(permission.canExtractForAccessibility());
- Assert.assertFalse(permission.canFillInForm());
- Assert.assertFalse(permission.canModify());
- Assert.assertFalse(permission.canModifyAnnotations());
- Assert.assertFalse(permission.canPrint());
- Assert.assertFalse(permission.canPrintDegraded());
+ assertFalse(permission.canAssembleDocument());
+ assertFalse(permission.canExtractContent());
+ assertTrue(permission.canExtractForAccessibility());
+ assertFalse(permission.canFillInForm());
+ assertFalse(permission.canModify());
+ assertFalse(permission.canModifyAnnotations());
+ assertFalse(permission.canPrint());
+ assertFalse(permission.canPrintDegraded());
}
// open second time
try (PDDocument encryptedDoc2 = reload(file, password2, getKeyStore(keyStore2)))
{
AccessPermission permission = encryptedDoc2.getCurrentAccessPermission();
- Assert.assertFalse(permission.canAssembleDocument());
- Assert.assertFalse(permission.canExtractContent());
- Assert.assertTrue(permission.canExtractForAccessibility());
- Assert.assertFalse(permission.canFillInForm());
- Assert.assertFalse(permission.canModify());
- Assert.assertFalse(permission.canModifyAnnotations());
- Assert.assertTrue(permission.canPrint());
- Assert.assertFalse(permission.canPrintDegraded());
+ assertFalse(permission.canAssembleDocument());
+ assertFalse(permission.canExtractContent());
+ assertTrue(permission.canExtractForAccessibility());
+ assertFalse(permission.canFillInForm());
+ assertFalse(permission.canModify());
+ assertFalse(permission.canModifyAnnotations());
+ assertTrue(permission.canPrint());
+ assertFalse(permission.canPrintDegraded());
}
}
@@ -278,12 +274,10 @@ public class TestPublicKeyEncryption
{
PDDocument doc2 = Loader.loadPDF(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());
+ assertEquals(text, new PDFTextStripper().getText(doc2),
+ "Extracted text is different");
+ assertEquals(producer, doc2.getDocumentInformation().getProducer(),
+ "Producer is different");
return doc2;
}
@@ -296,7 +290,7 @@ public class TestPublicKeyEncryption
* @return recipient specification
* @throws Exception if the certificate could not be read
*/
- private PublicKeyRecipient getRecipient(String certificate, AccessPermission permission) throws Exception
+ private static PublicKeyRecipient getRecipient(String certificate, AccessPermission permission) throws Exception
{
try (InputStream input = TestPublicKeyEncryption.class.getResourceAsStream(certificate))
{
@@ -326,8 +320,9 @@ public class TestPublicKeyEncryption
*
* @throws IOException
*/
- @Test
- public void testReadPubkeyEncryptedAES128() throws IOException
+ @ParameterizedTest
+ @MethodSource("keyLengths")
+ public void testReadPubkeyEncryptedAES128(int keyLength) throws IOException
{
try (InputStream is = TestPublicKeyEncryption.class.getResourceAsStream("AESkeylength128.pdf");
PDDocument doc = Loader.loadPDF(is,
@@ -335,11 +330,11 @@ public class TestPublicKeyEncryption
TestPublicKeyEncryption.class.getResourceAsStream("PDFBOX-4421-keystore.pfx"),
"testnutzer"))
{
- Assert.assertEquals("PublicKeySecurityHandler",
+ assertEquals("PublicKeySecurityHandler",
doc.getEncryption().getSecurityHandler().getClass().getSimpleName());
- Assert.assertEquals(128, doc.getEncryption().getSecurityHandler().getKeyLength());
+ assertEquals(128, doc.getEncryption().getSecurityHandler().getKeyLength());
PDFTextStripper stripper = new PDFTextStripper();
- Assert.assertEquals("Key length: 128", stripper.getText(doc).trim());
+ assertEquals("Key length: 128", stripper.getText(doc).trim());
}
}
@@ -349,8 +344,9 @@ public class TestPublicKeyEncryption
*
* @throws IOException
*/
- @Test
- public void testReadPubkeyEncryptedAES256() throws IOException
+ @ParameterizedTest
+ @MethodSource("keyLengths")
+ public void testReadPubkeyEncryptedAES256(int keyLength) throws IOException
{
try (InputStream is = TestPublicKeyEncryption.class.getResourceAsStream("AESkeylength256.pdf");
PDDocument doc = Loader.loadPDF(is,
@@ -358,11 +354,11 @@ public class TestPublicKeyEncryption
TestPublicKeyEncryption.class.getResourceAsStream("PDFBOX-4421-keystore.pfx"),
"testnutzer"))
{
- Assert.assertEquals("PublicKeySecurityHandler",
+ assertEquals("PublicKeySecurityHandler",
doc.getEncryption().getSecurityHandler().getClass().getSimpleName());
- Assert.assertEquals(256, doc.getEncryption().getSecurityHandler().getKeyLength());
+ assertEquals(256, doc.getEncryption().getSecurityHandler().getKeyLength());
PDFTextStripper stripper = new PDFTextStripper();
- Assert.assertEquals("Key length: 256", stripper.getText(doc).trim());
+ assertEquals("Key length: 256", stripper.getText(doc).trim());
}
}
}
Modified: pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/encryption/TestSymmetricKeyEncryption.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/encryption/TestSymmetricKeyEncryption.java?rev=1883552&r1=1883551&r2=1883552&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/encryption/TestSymmetricKeyEncryption.java (original)
+++ pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/encryption/TestSymmetricKeyEncryption.java Tue Nov 17 18:31:51 2020
@@ -16,6 +16,13 @@
*/
package org.apache.pdfbox.encryption;
+import static org.junit.jupiter.api.Assertions.assertArrayEquals;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertNotEquals;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.junit.jupiter.api.Assertions.fail;
+
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.File;
@@ -30,7 +37,7 @@ import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.crypto.Cipher;
-import junit.framework.TestCase;
+
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.pdfbox.Loader;
@@ -50,7 +57,9 @@ import org.apache.pdfbox.pdmodel.encrypt
import org.apache.pdfbox.pdmodel.encryption.StandardSecurityHandler;
import org.apache.pdfbox.pdmodel.graphics.image.ValidateXImage;
import org.apache.pdfbox.rendering.PDFRenderer;
-import org.junit.Assert;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.Test;
+
/**
* Tests for symmetric key encryption.
@@ -64,25 +73,22 @@ import org.junit.Assert;
* @author Tilman Hausherr
*
*/
-public class TestSymmetricKeyEncryption extends TestCase
+public class TestSymmetricKeyEncryption
{
/**
* Logger instance.
*/
private static final Log LOG = LogFactory.getLog(TestSymmetricKeyEncryption.class);
- private final File testResultsDir = new File("target/test-output/crypto");
+ private static final File testResultsDir = new File("target/test-output/crypto");
- private AccessPermission permission;
+ private static AccessPermission permission;
static final String USERPASSWORD = "1234567890abcdefghijk1234567890abcdefghijk";
static final String OWNERPASSWORD = "abcdefghijk1234567890abcdefghijk1234567890";
- /**
- * {@inheritDoc}
- */
- @Override
- protected void setUp() throws Exception
+ @BeforeAll
+ static void setUp() throws Exception
{
testResultsDir.mkdirs();
@@ -115,6 +121,7 @@ public class TestSymmetricKeyEncryption
*
* @throws java.io.IOException
*/
+ @Test
public void testPermissions() throws IOException
{
AccessPermission fullAP = new AccessPermission();
@@ -198,6 +205,7 @@ public class TestSymmetricKeyEncryption
*
* @throws Exception If there is an unexpected error during the test.
*/
+ @Test
public void testProtection() throws Exception
{
byte[] inputFileAsByteArray = getFileResourceAsByteArray("Acroform-PDFBOX-2333.pdf");
@@ -227,6 +235,7 @@ public class TestSymmetricKeyEncryption
*
* @throws IOException
*/
+ @Test
public void testPDFBox4308() throws IOException
{
byte[] inputFileAsByteArray = Files.readAllBytes(Paths.get("target/pdfs/PDFBOX-4308.pdf"));
@@ -242,6 +251,7 @@ public class TestSymmetricKeyEncryption
*
* @throws Exception If there is an unexpected error during the test.
*/
+ @Test
public void testProtectionInnerAttachment() throws Exception
{
String testFileName = "preEnc_20141025_105451.pdf";
@@ -270,6 +280,7 @@ public class TestSymmetricKeyEncryption
*
* @throws IOException
*/
+ @Test
public void testPDFBox4453() throws IOException
{
final int TESTCOUNT = 1000;
@@ -300,13 +311,13 @@ public class TestSymmetricKeyEncryption
try (PDDocument doc = Loader.loadPDF(file))
{
- Assert.assertTrue(doc.isEncrypted());
+ assertTrue(doc.isEncrypted());
for (int i = 0; i < TESTCOUNT; ++i)
{
COSDictionary dict =
doc.getPage(0).getCOSObject().getCOSDictionary(COSName.getPDFName("_Test-" + i));
- Assert.assertEquals("3", dict.getString("key1"));
- Assert.assertEquals("0", dict.getString("key2"));
+ assertEquals("3", dict.getString("key1"));
+ assertEquals("0", dict.getString("key2"));
}
}
}
@@ -334,7 +345,7 @@ public class TestSymmetricKeyEncryption
try (PDDocument encryptedDoc = encrypt(keyLength, preferAES, sizePriorToEncr, document,
prefix, permission, userpassword, ownerpassword))
{
- Assert.assertEquals(numSrcPages, encryptedDoc.getNumberOfPages());
+ assertEquals(numSrcPages, encryptedDoc.getNumberOfPages());
pdfRenderer = new PDFRenderer(encryptedDoc);
for (int i = 0; i < encryptedDoc.getNumberOfPages(); ++i)
{
@@ -346,9 +357,7 @@ public class TestSymmetricKeyEncryption
try (InputStream unfilteredStream = encryptedDoc.getPage(i).getContents())
{
byte[] bytes = IOUtils.toByteArray(unfilteredStream);
- Assert.assertArrayEquals("content stream of page " + i + " not identical",
- srcContentStreamTab.get(i),
- bytes);
+ assertArrayEquals(srcContentStreamTab.get(i),bytes, "content stream of page " + i + " not identical");
}
}
@@ -379,14 +388,13 @@ public class TestSymmetricKeyEncryption
doc.save(pdfFile);
doc.close();
long sizeEncrypted = pdfFile.length();
- Assert.assertNotEquals(keyLength
- + "-bit " + (preferAES ? "AES" : "RC4") + " encrypted pdf should not have same size as plain one",
- sizeEncrypted, sizePriorToEncr);
+ assertNotEquals(sizeEncrypted, sizePriorToEncr,
+ keyLength + "-bit " + (preferAES ? "AES" : "RC4") + " encrypted pdf should not have same size as plain one");
// test with owner password => full permissions
PDDocument encryptedDoc = Loader.loadPDF(pdfFile, ownerpassword);
- Assert.assertTrue(encryptedDoc.isEncrypted());
- Assert.assertTrue(encryptedDoc.getCurrentAccessPermission().isOwnerPermission());
+ assertTrue(encryptedDoc.isEncrypted());
+ assertTrue(encryptedDoc.getCurrentAccessPermission().isOwnerPermission());
// Older encryption allows to get the user password when the owner password is known
PDEncryption encryption = encryptedDoc.getEncryption();
@@ -400,15 +408,15 @@ public class TestSymmetricKeyEncryption
encryption.getOwnerKey(),
revision,
keyLengthInBytes);
- Assert.assertEquals(userpassword.substring(0, 32), new String(computedUserPassword, StandardCharsets.ISO_8859_1));
+ assertEquals(userpassword.substring(0, 32), new String(computedUserPassword, StandardCharsets.ISO_8859_1));
}
encryptedDoc.close();
// test with user password => restricted permissions
encryptedDoc = Loader.loadPDF(pdfFile, userpassword);
- Assert.assertTrue(encryptedDoc.isEncrypted());
- Assert.assertFalse(encryptedDoc.getCurrentAccessPermission().isOwnerPermission());
+ assertTrue(encryptedDoc.isEncrypted());
+ assertFalse(encryptedDoc.getCurrentAccessPermission().isOwnerPermission());
assertEquals(permission.getPermissionBytes(), encryptedDoc.getCurrentAccessPermission().getPermissionBytes());
@@ -423,7 +431,7 @@ public class TestSymmetricKeyEncryption
PDDocumentNameDictionary names = catalog.getNames();
PDEmbeddedFilesNameTreeNode embeddedFiles = names.getEmbeddedFiles();
Map<String, PDComplexFileSpecification> embeddedFileNames = embeddedFiles.getNames();
- Assert.assertEquals(1, embeddedFileNames.size());
+ assertEquals(1, embeddedFileNames.size());
Map.Entry<String, PDComplexFileSpecification> entry = embeddedFileNames.entrySet().iterator().next();
LOG.info("Processing embedded file " + entry.getKey() + ":");
PDComplexFileSpecification complexFileSpec = entry.getValue();
@@ -456,11 +464,11 @@ public class TestSymmetricKeyEncryption
File extractedEmbeddedFile = extractEmbeddedFile(new FileInputStream(decryptedFile), "decryptedInnerFile-" + keyLength + "-bit-" + (preferAES ? "AES" : "RC4") + ".pdf");
- Assert.assertEquals(keyLength + "-bit " + (preferAES ? "AES" : "RC4") + " decrypted inner attachment pdf should have same size as plain one",
- embeddedFilePriorToEncryption.length(), extractedEmbeddedFile.length());
+ assertEquals(embeddedFilePriorToEncryption.length(), extractedEmbeddedFile.length(),
+ keyLength + "-bit " + (preferAES ? "AES" : "RC4") + " decrypted inner attachment pdf should have same size as plain one");
// compare the two embedded files
- Assert.assertArrayEquals(
+ assertArrayEquals(
getFileAsByteArray(embeddedFilePriorToEncryption),
getFileAsByteArray(extractedEmbeddedFile));
}
@@ -475,4 +483,4 @@ public class TestSymmetricKeyEncryption
{
return Files.readAllBytes(f.toPath());
}
-}
+}
\ No newline at end of file