Skip to content

Commit a7ac6ae

Browse files
Add tests for encryption on stamping, encryption of docs with xrefstream
1 parent 5d8ae06 commit a7ac6ae

File tree

7 files changed

+60
-4
lines changed

7 files changed

+60
-4
lines changed

kernel/src/main/java/com/itextpdf/kernel/pdf/EncryptionConstants.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,9 @@ public class EncryptionConstants implements Serializable {
7979
/**
8080
* Add this to the mode to keep encrypt only the embedded files.
8181
*/
82-
public static final int EMBEDDED_FILES_ONLY = 24; /**
82+
public static final int EMBEDDED_FILES_ONLY = 24;
83+
84+
/**
8385
* The operation permitted when the document is opened with the user password.
8486
*/
8587
public static final int ALLOW_PRINTING = 4 + 2048;

kernel/src/test/java/com/itextpdf/kernel/pdf/PdfEncryptionTest.java

Lines changed: 57 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ This file is part of the iText (R) project.
6060
import org.bouncycastle.jce.provider.BouncyCastleProvider;
6161
import org.junit.Assert;
6262
import org.junit.BeforeClass;
63-
import org.junit.Ignore;
6463
import org.junit.Rule;
6564
import org.junit.Test;
6665
import org.junit.experimental.categories.Category;
@@ -412,14 +411,69 @@ public void encryptWithPasswordAes128EmbeddedFilesOnly() throws IOException, Gen
412411
public void encryptAes256Pdf2NotEncryptMetadata() throws InterruptedException, IOException, XMPException {
413412
String filename = "encryptAes256Pdf2NotEncryptMetadata.pdf";
414413
int encryptionType = EncryptionConstants.ENCRYPTION_AES_256 | EncryptionConstants.DO_NOT_ENCRYPT_METADATA;
415-
encryptWithPassword(filename, encryptionType, CompressionConstants.DEFAULT_COMPRESSION);
414+
encryptWithPassword(filename, encryptionType, CompressionConstants.DEFAULT_COMPRESSION, true);
415+
}
416+
417+
@Test
418+
public void encryptAes256EncryptedStampingPreserve() throws InterruptedException, IOException, XMPException {
419+
String filename = "encryptAes256EncryptedStampingPreserve.pdf";
420+
String src = sourceFolder + "encryptedWithPlainMetadata.pdf";
421+
String out = destinationFolder + filename;
422+
423+
PdfDocument pdfDoc = new PdfDocument(
424+
new PdfReader(src, new ReaderProperties().setPassword(OWNER)),
425+
new PdfWriter(out, new WriterProperties()),
426+
new StampingProperties().preserveEncryption());
427+
428+
pdfDoc.close();
429+
430+
CompareTool compareTool = new CompareTool().enableEncryptionCompare();
431+
String compareResult = compareTool.compareByContent(out, sourceFolder + "cmp_" + filename, destinationFolder, "diff_", USER, USER);
432+
if (compareResult != null) {
433+
Assert.fail(compareResult);
434+
}
435+
}
436+
437+
@Test
438+
public void encryptAes256EncryptedStampingUpdate() throws InterruptedException, IOException, XMPException {
439+
String filename = "encryptAes256EncryptedStampingUpdate.pdf";
440+
String src = sourceFolder + "encryptedWithPlainMetadata.pdf";
441+
String out = destinationFolder + filename;
442+
443+
PdfDocument pdfDoc = new PdfDocument(
444+
new PdfReader(src, new ReaderProperties().setPassword(OWNER)),
445+
new PdfWriter(out, new WriterProperties()
446+
.setStandardEncryption(USER, OWNER, EncryptionConstants.ALLOW_PRINTING, EncryptionConstants.STANDARD_ENCRYPTION_40)),
447+
new StampingProperties());
448+
449+
pdfDoc.close();
450+
451+
CompareTool compareTool = new CompareTool().enableEncryptionCompare();
452+
String compareResult = compareTool.compareByContent(out, sourceFolder + "cmp_" + filename, destinationFolder, "diff_", USER, USER);
453+
if (compareResult != null) {
454+
Assert.fail(compareResult);
455+
}
456+
}
457+
458+
@Test
459+
public void encryptAes256FullCompression() throws InterruptedException, IOException, XMPException {
460+
String filename = "encryptAes256FullCompression.pdf";
461+
int encryptionType = EncryptionConstants.ENCRYPTION_AES_256;
462+
encryptWithPassword(filename, encryptionType, CompressionConstants.DEFAULT_COMPRESSION, true);
416463
}
417464

418465
public void encryptWithPassword(String filename, int encryptionType, int compression) throws XMPException, IOException, InterruptedException {
466+
encryptWithPassword(filename, encryptionType, compression, false);
467+
}
468+
469+
public void encryptWithPassword(String filename, int encryptionType, int compression, boolean fullCompression) throws XMPException, IOException, InterruptedException {
419470
String outFileName = destinationFolder + filename;
420471
int permissions = EncryptionConstants.ALLOW_SCREENREADERS;
421472
PdfWriter writer = new PdfWriter(outFileName,
422-
new WriterProperties().setStandardEncryption(USER, OWNER, permissions, encryptionType).addXmpMetadata());
473+
new WriterProperties()
474+
.setStandardEncryption(USER, OWNER, permissions, encryptionType)
475+
.addXmpMetadata()
476+
.setFullCompressionMode(fullCompression));
423477
writer.setCompressionLevel(compression);
424478
PdfDocument document = new PdfDocument(writer);
425479
document.getDocumentInfo().setAuthor(author).

0 commit comments

Comments
 (0)