forked from pipeline-foundation/itext7-dotnet
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'develop' into devsecops
- Loading branch information
Showing
124 changed files
with
3,224 additions
and
201 deletions.
There are no files selected for viewing
192 changes: 54 additions & 138 deletions
192
.../itext/kernel/crypto/PdfEncryptionTest.cs → ...crypto/pdfencryption/PdfEncryptionTest.cs
Large diffs are not rendered by default.
Oops, something went wrong.
94 changes: 94 additions & 0 deletions
94
itext.tests/itext.kernel.tests/itext/kernel/crypto/pdfencryption/PdfEncryptionTestUtils.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
/* | ||
This file is part of the iText (R) project. | ||
Copyright (c) 1998-2023 Apryse Group NV | ||
Authors: Apryse Software. | ||
This program is offered under a commercial and under the AGPL license. | ||
For commercial licensing, contact us at https://itextpdf.com/sales. For AGPL licensing, see below. | ||
AGPL licensing: | ||
This program is free software: you can redistribute it and/or modify | ||
it under the terms of the GNU Affero General Public License as published by | ||
the Free Software Foundation, either version 3 of the License, or | ||
(at your option) any later version. | ||
This program is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU Affero General Public License for more details. | ||
You should have received a copy of the GNU Affero General Public License | ||
along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
using System; | ||
using iText.IO.Font.Constants; | ||
using iText.Kernel.Font; | ||
using iText.Kernel.Pdf; | ||
using iText.Kernel.Utils; | ||
|
||
namespace iText.Kernel.Crypto.Pdfencryption { | ||
public class PdfEncryptionTestUtils { | ||
private readonly String destinationFolder; | ||
|
||
private readonly String sourceFolder; | ||
|
||
public const String PAGE_TEXT_CONTENT = "Hello world!"; | ||
|
||
public const String CUSTOM_INFO_ENTRY_KEY = "Custom"; | ||
|
||
public const String CUSTOM_INFO_ENTRY_VALUE = "String"; | ||
|
||
/// <summary>User password.</summary> | ||
public static byte[] USER = "Hello".GetBytes(iText.Commons.Utils.EncodingUtil.ISO_8859_1); | ||
|
||
/// <summary>Owner password.</summary> | ||
public static byte[] OWNER = "World".GetBytes(iText.Commons.Utils.EncodingUtil.ISO_8859_1); | ||
|
||
public PdfEncryptionTestUtils(String destinationFolder, String sourceFolder) { | ||
this.destinationFolder = destinationFolder; | ||
this.sourceFolder = sourceFolder; | ||
} | ||
|
||
public virtual void CompareEncryptedPdf(String filename) { | ||
CheckDecryptedWithPasswordContent(destinationFolder + filename, OWNER, PAGE_TEXT_CONTENT); | ||
CheckDecryptedWithPasswordContent(destinationFolder + filename, USER, PAGE_TEXT_CONTENT); | ||
CompareTool compareTool = new CompareTool().EnableEncryptionCompare(); | ||
String compareResult = compareTool.CompareByContent(destinationFolder + filename, sourceFolder + "cmp_" + | ||
filename, destinationFolder, "diff_", USER, USER); | ||
if (compareResult != null) { | ||
NUnit.Framework.Assert.Fail(compareResult); | ||
} | ||
} | ||
|
||
public virtual void CheckDecryptedWithPasswordContent(String src, byte[] password, String pageContent) { | ||
CheckDecryptedWithPasswordContent(src, password, pageContent, false); | ||
} | ||
|
||
public virtual void CheckDecryptedWithPasswordContent(String src, byte[] password, String pageContent, bool | ||
expectError) { | ||
PdfReader reader = CompareTool.CreateOutputReader(src, new ReaderProperties().SetPassword(password)); | ||
PdfDocument document = new PdfDocument(reader); | ||
PdfPage page = document.GetPage(1); | ||
bool expectedContentFound = iText.Commons.Utils.JavaUtil.GetStringForBytes(page.GetStreamBytes(0)).Contains | ||
(pageContent); | ||
String actualCustomInfoEntry = document.GetTrailer().GetAsDictionary(PdfName.Info).GetAsString(new PdfName | ||
(CUSTOM_INFO_ENTRY_KEY)).ToUnicodeString(); | ||
if (!expectError) { | ||
NUnit.Framework.Assert.IsTrue(expectedContentFound, "Expected content: \n" + pageContent); | ||
NUnit.Framework.Assert.AreEqual(CUSTOM_INFO_ENTRY_VALUE, actualCustomInfoEntry, "Encrypted custom"); | ||
} | ||
else { | ||
NUnit.Framework.Assert.IsFalse(expectedContentFound, "Expected content: \n" + pageContent); | ||
NUnit.Framework.Assert.AreNotEqual(CUSTOM_INFO_ENTRY_VALUE, actualCustomInfoEntry, "Encrypted custom"); | ||
} | ||
document.Close(); | ||
} | ||
|
||
public static void WriteTextBytesOnPageContent(PdfPage page, String text) { | ||
page.GetFirstContentStream().GetOutputStream().WriteBytes(("q\n" + "BT\n" + "36 706 Td\n" + "0 0 Td\n" + "/F1 24 Tf\n" | ||
+ "(" + text + ")Tj\n" + "0 0 Td\n" + "ET\n" + "Q ").GetBytes(iText.Commons.Utils.EncodingUtil.ISO_8859_1 | ||
)); | ||
page.GetResources().AddFont(page.GetDocument(), PdfFontFactory.CreateFont(StandardFonts.HELVETICA)); | ||
} | ||
} | ||
} |
111 changes: 111 additions & 0 deletions
111
...t.tests/itext.kernel.tests/itext/kernel/crypto/pdfencryption/PdfPreserveEncryptionTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
/* | ||
This file is part of the iText (R) project. | ||
Copyright (c) 1998-2023 Apryse Group NV | ||
Authors: Apryse Software. | ||
This program is offered under a commercial and under the AGPL license. | ||
For commercial licensing, contact us at https://itextpdf.com/sales. For AGPL licensing, see below. | ||
AGPL licensing: | ||
This program is free software: you can redistribute it and/or modify | ||
it under the terms of the GNU Affero General Public License as published by | ||
the Free Software Foundation, either version 3 of the License, or | ||
(at your option) any later version. | ||
This program is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU Affero General Public License for more details. | ||
You should have received a copy of the GNU Affero General Public License | ||
along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
using System; | ||
using iText.Kernel.Logs; | ||
using iText.Kernel.Pdf; | ||
using iText.Kernel.Utils; | ||
using iText.Test; | ||
using iText.Test.Attributes; | ||
|
||
namespace iText.Kernel.Crypto.Pdfencryption { | ||
[NUnit.Framework.Category("IntegrationTest")] | ||
public class PdfPreserveEncryptionTest : ExtendedITextTest { | ||
public static readonly String destinationFolder = NUnit.Framework.TestContext.CurrentContext.TestDirectory | ||
+ "/test/itext/kernel/crypto/pdfencryption/PdfPreserveEncryptionTest/"; | ||
|
||
public static readonly String sourceFolder = iText.Test.TestUtil.GetParentProjectDirectory(NUnit.Framework.TestContext | ||
.CurrentContext.TestDirectory) + "/resources/itext/kernel/crypto/pdfencryption/PdfPreserveEncryptionTest/"; | ||
|
||
public PdfEncryptionTestUtils encryptionUtil = new PdfEncryptionTestUtils(destinationFolder, sourceFolder); | ||
|
||
[NUnit.Framework.OneTimeSetUp] | ||
public static void BeforeClass() { | ||
CreateOrClearDestinationFolder(destinationFolder); | ||
} | ||
|
||
[NUnit.Framework.OneTimeTearDown] | ||
public static void AfterClass() { | ||
CompareTool.Cleanup(destinationFolder); | ||
} | ||
|
||
[NUnit.Framework.Test] | ||
[LogMessage(KernelLogMessageConstant.MD5_IS_NOT_FIPS_COMPLIANT, Ignore = true)] | ||
[LogMessage(VersionConforming.DEPRECATED_ENCRYPTION_ALGORITHMS)] | ||
public virtual void StampAndUpdateVersionPreserveStandard40() { | ||
String filename = "stampAndUpdateVersionPreserveStandard40.pdf"; | ||
PdfDocument doc = new PdfDocument(new PdfReader(sourceFolder + "encryptedWithPasswordStandard40.pdf", new | ||
ReaderProperties().SetPassword(PdfEncryptionTestUtils.OWNER)), CompareTool.CreateTestPdfWriter(destinationFolder | ||
+ filename, new WriterProperties().SetPdfVersion(PdfVersion.PDF_2_0)), new StampingProperties().PreserveEncryption | ||
()); | ||
doc.Close(); | ||
encryptionUtil.CompareEncryptedPdf(filename); | ||
} | ||
|
||
[NUnit.Framework.Test] | ||
[LogMessage(KernelLogMessageConstant.MD5_IS_NOT_FIPS_COMPLIANT, Ignore = true)] | ||
[LogMessage(VersionConforming.DEPRECATED_AES256_REVISION)] | ||
public virtual void StampAndUpdateVersionPreserveAes256() { | ||
String filename = "stampAndUpdateVersionPreserveAes256.pdf"; | ||
PdfDocument doc = new PdfDocument(new PdfReader(sourceFolder + "encryptedWithPasswordAes256.pdf", new ReaderProperties | ||
().SetPassword(PdfEncryptionTestUtils.OWNER)), CompareTool.CreateTestPdfWriter(destinationFolder + filename | ||
, new WriterProperties().SetPdfVersion(PdfVersion.PDF_2_0)), new StampingProperties().PreserveEncryption | ||
()); | ||
doc.Close(); | ||
encryptionUtil.CompareEncryptedPdf(filename); | ||
} | ||
|
||
[NUnit.Framework.Test] | ||
[LogMessage(KernelLogMessageConstant.MD5_IS_NOT_FIPS_COMPLIANT, Ignore = true)] | ||
public virtual void EncryptAes256EncryptedStampingPreserve() { | ||
String filename = "encryptAes256EncryptedStampingPreserve.pdf"; | ||
String src = sourceFolder + "encryptedWithPlainMetadata.pdf"; | ||
String @out = destinationFolder + filename; | ||
PdfDocument pdfDoc = new PdfDocument(new PdfReader(src, new ReaderProperties().SetPassword(PdfEncryptionTestUtils | ||
.OWNER)), CompareTool.CreateTestPdfWriter(@out, new WriterProperties()), new StampingProperties().PreserveEncryption | ||
()); | ||
pdfDoc.Close(); | ||
CompareTool compareTool = new CompareTool().EnableEncryptionCompare(); | ||
String compareResult = compareTool.CompareByContent(@out, sourceFolder + "cmp_" + filename, destinationFolder | ||
, "diff_", PdfEncryptionTestUtils.USER, PdfEncryptionTestUtils.USER); | ||
if (compareResult != null) { | ||
NUnit.Framework.Assert.Fail(compareResult); | ||
} | ||
} | ||
|
||
[NUnit.Framework.Test] | ||
public virtual void PreserveEncryptionShorterDocumentId() { | ||
String filename = "preserveEncryptionWithShortId.pdf"; | ||
String src = sourceFolder + "encryptedWithShortId.pdf"; | ||
String @out = destinationFolder + filename; | ||
PdfDocument pdfDoc = new PdfDocument(new PdfReader(src, new ReaderProperties().SetPassword(PdfEncryptionTestUtils | ||
.OWNER)), new PdfWriter(@out, new WriterProperties()), new StampingProperties().PreserveEncryption()); | ||
pdfDoc.Close(); | ||
CompareTool compareTool = new CompareTool().EnableEncryptionCompare(); | ||
String compareResult = compareTool.CompareByContent(@out, sourceFolder + "cmp_" + filename, destinationFolder | ||
, "diff_", null, null); | ||
if (compareResult != null) { | ||
NUnit.Framework.Assert.Fail(compareResult); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.