|
| 1 | +using System; |
| 2 | +using iText.Kernel.Geom; |
| 3 | +using iText.Kernel.Pdf.Annot; |
| 4 | +using iText.Kernel.Pdf.Filespec; |
| 5 | +using iText.Kernel.Utils; |
| 6 | +using iText.Test; |
| 7 | + |
| 8 | +namespace iText.Kernel.Pdf { |
| 9 | + public class EncryptedEmbeddedStreamsHandlerTest : ExtendedITextTest { |
| 10 | + public static readonly String sourceFolder = iText.Test.TestUtil.GetParentProjectDirectory(NUnit.Framework.TestContext |
| 11 | + .CurrentContext.TestDirectory) + "/resources/itext/kernel/pdf/EncryptedEmbeddedStreamsHandlerTest/"; |
| 12 | + |
| 13 | + public static readonly String destinationFolder = NUnit.Framework.TestContext.CurrentContext.TestDirectory |
| 14 | + + "/test/itext/kernel/pdf/EncryptedEmbeddedStreamsHandlerTest/"; |
| 15 | + |
| 16 | + [NUnit.Framework.OneTimeSetUp] |
| 17 | + public static void BeforeClass() { |
| 18 | + CreateDestinationFolder(destinationFolder); |
| 19 | + } |
| 20 | + |
| 21 | + [NUnit.Framework.Test] |
| 22 | + public virtual void NoReaderStandardEncryptionAddFileAttachment() { |
| 23 | + String outFileName = destinationFolder + "noReaderStandardEncryptionAddFileAttachment.pdf"; |
| 24 | + String cmpFileName = sourceFolder + "cmp_noReaderStandardEncryptionAddFileAttachment.pdf"; |
| 25 | + PdfDocument pdfDocument = CreateEncryptedDocument(EncryptionConstants.STANDARD_ENCRYPTION_128, outFileName |
| 26 | + ); |
| 27 | + PdfFileSpec fs = PdfFileSpec.CreateEmbeddedFileSpec(pdfDocument, "file".GetBytes(), "description", "file.txt" |
| 28 | + , null, null, null); |
| 29 | + pdfDocument.AddFileAttachment("file.txt", fs); |
| 30 | + pdfDocument.AddNewPage(); |
| 31 | + pdfDocument.Close(); |
| 32 | + NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outFileName, cmpFileName, destinationFolder |
| 33 | + , "diff", "password".GetBytes(), "password".GetBytes())); |
| 34 | + } |
| 35 | + |
| 36 | + [NUnit.Framework.Test] |
| 37 | + public virtual void NoReaderAesEncryptionAddFileAttachment() { |
| 38 | + String outFileName = destinationFolder + "noReaderAesEncryptionAddFileAttachment.pdf"; |
| 39 | + String cmpFileName = sourceFolder + "cmp_noReaderAesEncryptionAddFileAttachment.pdf"; |
| 40 | + PdfDocument pdfDocument = CreateEncryptedDocument(EncryptionConstants.ENCRYPTION_AES_128, outFileName); |
| 41 | + PdfFileSpec fs = PdfFileSpec.CreateEmbeddedFileSpec(pdfDocument, "file".GetBytes(), "description", "file.txt" |
| 42 | + , null, null, null); |
| 43 | + pdfDocument.AddFileAttachment("file.txt", fs); |
| 44 | + pdfDocument.AddNewPage(); |
| 45 | + pdfDocument.Close(); |
| 46 | + NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outFileName, cmpFileName, destinationFolder |
| 47 | + , "diff", "password".GetBytes(), "password".GetBytes())); |
| 48 | + } |
| 49 | + |
| 50 | + [NUnit.Framework.Test] |
| 51 | + public virtual void WithReaderStandardEncryptionAddFileAttachment() { |
| 52 | + String outFileName = destinationFolder + "withReaderStandardEncryptionAddFileAttachment.pdf"; |
| 53 | + String cmpFileName = sourceFolder + "cmp_withReaderStandardEncryptionAddFileAttachment.pdf"; |
| 54 | + PdfReader reader = new PdfReader(sourceFolder + "pdfWithFileAttachments.pdf", new ReaderProperties().SetPassword |
| 55 | + ("password".GetBytes())); |
| 56 | + // Setting compression level to zero doesn't affect the encryption at any level. |
| 57 | + // We do it to simplify observation of the resultant PDF. |
| 58 | + PdfDocument pdfDocument = new PdfDocument(reader, new PdfWriter(outFileName).SetCompressionLevel(0)); |
| 59 | + PdfFileSpec fs = PdfFileSpec.CreateEmbeddedFileSpec(pdfDocument, "file".GetBytes(), "description", "file.txt" |
| 60 | + , null, null, null); |
| 61 | + pdfDocument.AddFileAttachment("file.txt", fs); |
| 62 | + pdfDocument.AddNewPage(); |
| 63 | + pdfDocument.Close(); |
| 64 | + NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outFileName, cmpFileName, destinationFolder |
| 65 | + , "diff")); |
| 66 | + } |
| 67 | + |
| 68 | + [NUnit.Framework.Test] |
| 69 | + public virtual void NoReaderStandardEncryptionAddAnnotation() { |
| 70 | + String outFileName = destinationFolder + "noReaderStandardEncryptionAddAnnotation.pdf"; |
| 71 | + String cmpFileName = sourceFolder + "cmp_noReaderStandardEncryptionAddAnnotation.pdf"; |
| 72 | + PdfDocument pdfDocument = CreateEncryptedDocument(EncryptionConstants.STANDARD_ENCRYPTION_128, outFileName |
| 73 | + ); |
| 74 | + pdfDocument.AddNewPage(); |
| 75 | + PdfFileSpec fs = PdfFileSpec.CreateEmbeddedFileSpec(pdfDocument, "file".GetBytes(), "description", "file.txt" |
| 76 | + , null, null, null); |
| 77 | + pdfDocument.GetPage(1).AddAnnotation(new PdfFileAttachmentAnnotation(new Rectangle(100, 100), fs)); |
| 78 | + pdfDocument.Close(); |
| 79 | + NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outFileName, cmpFileName, destinationFolder |
| 80 | + , "diff", "password".GetBytes(), "password".GetBytes())); |
| 81 | + } |
| 82 | + |
| 83 | + [NUnit.Framework.Test] |
| 84 | + public virtual void WithReaderStandardEncryptionAddAnnotation() { |
| 85 | + String outFileName = destinationFolder + "withReaderStandardEncryptionAddAnnotation.pdf"; |
| 86 | + String cmpFileName = sourceFolder + "cmp_withReaderStandardEncryptionAddAnnotation.pdf"; |
| 87 | + PdfReader reader = new PdfReader(sourceFolder + "pdfWithFileAttachmentAnnotations.pdf", new ReaderProperties |
| 88 | + ().SetPassword("password".GetBytes())); |
| 89 | + // Setting compression level to zero doesn't affect the encryption at any level. |
| 90 | + // We do it to simplify observation of the resultant PDF. |
| 91 | + PdfDocument pdfDocument = new PdfDocument(reader, new PdfWriter(outFileName).SetCompressionLevel(0)); |
| 92 | + pdfDocument.AddNewPage(); |
| 93 | + PdfFileSpec fs = PdfFileSpec.CreateEmbeddedFileSpec(pdfDocument, "file".GetBytes(), "description", "file.txt" |
| 94 | + , null, null, null); |
| 95 | + pdfDocument.GetPage(1).AddAnnotation(new PdfFileAttachmentAnnotation(new Rectangle(100, 100), fs)); |
| 96 | + pdfDocument.Close(); |
| 97 | + NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outFileName, cmpFileName, destinationFolder |
| 98 | + , "diff")); |
| 99 | + } |
| 100 | + |
| 101 | + [NUnit.Framework.Test] |
| 102 | + public virtual void ReaderWithoutEncryptionWriterStandardEncryption() { |
| 103 | + String outFileName = destinationFolder + "readerWithoutEncryptionWriterStandardEncryption.pdf"; |
| 104 | + String cmpFileName = sourceFolder + "cmp_readerWithoutEncryptionWriterStandardEncryption.pdf"; |
| 105 | + PdfReader reader = new PdfReader(sourceFolder + "pdfWithUnencryptedAttachmentAnnotations.pdf"); |
| 106 | + PdfDocument pdfDocument = CreateEncryptedDocument(reader, EncryptionConstants.STANDARD_ENCRYPTION_128, outFileName |
| 107 | + ); |
| 108 | + PdfFileSpec fs = PdfFileSpec.CreateEmbeddedFileSpec(pdfDocument, "file".GetBytes(), "description", "file.txt" |
| 109 | + , null, null, null); |
| 110 | + pdfDocument.AddFileAttachment("new attachment", fs); |
| 111 | + pdfDocument.Close(); |
| 112 | + NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outFileName, cmpFileName, destinationFolder |
| 113 | + , "diff", "password".GetBytes(), "password".GetBytes())); |
| 114 | + } |
| 115 | + |
| 116 | + private PdfDocument CreateEncryptedDocument(int encryptionAlgorithm, String outFileName) { |
| 117 | + PdfWriter writer = new PdfWriter(outFileName, new WriterProperties().SetStandardEncryption("password".GetBytes |
| 118 | + (), "password".GetBytes(), 0, encryptionAlgorithm | EncryptionConstants.EMBEDDED_FILES_ONLY)); |
| 119 | + // Setting compression level to zero doesn't affect the encryption at any level. |
| 120 | + // We do it to simplify observation of the resultant PDF. |
| 121 | + writer.SetCompressionLevel(0); |
| 122 | + return new PdfDocument(writer); |
| 123 | + } |
| 124 | + |
| 125 | + private PdfDocument CreateEncryptedDocument(PdfReader reader, int encryptionAlgorithm, String outFileName) { |
| 126 | + PdfWriter writer = new PdfWriter(outFileName, new WriterProperties().SetStandardEncryption("password".GetBytes |
| 127 | + (), "password".GetBytes(), 0, encryptionAlgorithm | EncryptionConstants.EMBEDDED_FILES_ONLY)); |
| 128 | + // Setting compression level to zero doesn't affect the encryption at any level. |
| 129 | + // We do it to simplify observation of the resultant PDF. |
| 130 | + writer.SetCompressionLevel(0); |
| 131 | + return new PdfDocument(reader, writer); |
| 132 | + } |
| 133 | + } |
| 134 | +} |
0 commit comments