Skip to content

Commit

Permalink
Merge branch 'develop' into devsecops
Browse files Browse the repository at this point in the history
  • Loading branch information
aleks-ivanov committed Sep 10, 2024
2 parents 49a8e36 + 89e9859 commit f5b46bf
Show file tree
Hide file tree
Showing 95 changed files with 1,346 additions and 515 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,20 @@ public virtual void OrphanedNamelessFormFieldTest() {
}
}

[NUnit.Framework.Test]
public virtual void FormWithSameFieldReferencesTest() {
String srcFileName = SOURCE_FOLDER + "formWithSameFieldReferences.pdf";
String cmpFileName = SOURCE_FOLDER + "cmp_formWithSameFieldReferences.pdf";
String outFileName = DESTINATION_FOLDER + "formWithSameFieldReferences.pdf";
using (PdfDocument sourceDoc = new PdfDocument(new PdfReader(srcFileName), new PdfWriter(outFileName))) {
PdfAcroForm acroForm = PdfFormCreator.GetAcroForm(sourceDoc, true);
NUnit.Framework.Assert.AreEqual(1, acroForm.GetFields().Size());
NUnit.Framework.Assert.IsNull(acroForm.GetField("Field").GetKids());
}
NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outFileName, cmpFileName, DESTINATION_FOLDER
, "diff_"));
}

[NUnit.Framework.Test]
public virtual void MergeMergedFieldsWithTheSameNamesTest() {
String srcFileName = SOURCE_FOLDER + "fieldMergedWithWidget.pdf";
Expand Down
Binary file not shown.
Binary file not shown.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ public virtual void EncryptWithPasswordAes256Pdf2() {

[NUnit.Framework.Test]
[LogMessage(KernelLogMessageConstant.MD5_IS_NOT_FIPS_COMPLIANT, Ignore = true)]
[LogMessage(VersionConforming.DEPRECATED_ENCRYPTION_ALGORITHMS)]
[LogMessage(VersionConforming.DEPRECATED_ENCRYPTION_ALGORITHMS, Count = 2)]
public virtual void EncryptWithPasswordAes128Pdf2() {
String filename = "encryptWithPasswordAes128Pdf2.pdf";
int encryptionType = EncryptionConstants.ENCRYPTION_AES_128;
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,204 @@
/*
This file is part of the iText (R) project.
Copyright (c) 1998-2024 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 NUnit.Framework;
using iText.Bouncycastleconnector;
using iText.Commons.Bouncycastle.Cert;
using iText.Commons.Bouncycastle.Crypto;
using iText.Commons.Utils;
using iText.Kernel.Crypto;
using iText.Kernel.Exceptions;
using iText.Kernel.Geom;
using iText.Kernel.Pdf;
using iText.Kernel.Pdf.Annot;
using iText.Kernel.Utils;
using iText.Test;

namespace iText.Kernel.Mac {
[NUnit.Framework.Category("BouncyCastleIntegrationTest")]
public class MacIntegrityProtectorReadingAndRewritingTest : ExtendedITextTest {
private static readonly String SOURCE_FOLDER = iText.Test.TestUtil.GetParentProjectDirectory(NUnit.Framework.TestContext
.CurrentContext.TestDirectory) + "/resources/itext/kernel/mac/MacIntegrityProtectorReadingAndRewritingTest/";

private static readonly String DESTINATION_FOLDER = NUnit.Framework.TestContext.CurrentContext.TestDirectory
+ "/test/itext/kernel/mac/MacIntegrityProtectorReadingAndRewritingTest/";

private static readonly String CERTS_SRC = iText.Test.TestUtil.GetParentProjectDirectory(NUnit.Framework.TestContext
.CurrentContext.TestDirectory) + "/resources/itext/kernel/mac/MacIntegrityProtectorReadingAndRewritingTest/certs/";

private static readonly byte[] PASSWORD = "123".GetBytes();

private static readonly String PROVIDER_NAME = BouncyCastleFactoryCreator.GetFactory().GetProviderName();

[NUnit.Framework.OneTimeSetUp]
public static void BeforeClass() {
NUnit.Framework.Assume.That("BC".Equals(PROVIDER_NAME));
CreateOrClearDestinationFolder(DESTINATION_FOLDER);
}

[NUnit.Framework.OneTimeTearDown]
public static void AfterClass() {
CompareTool.Cleanup(DESTINATION_FOLDER);
}

[NUnit.Framework.Test]
public virtual void AppendModeTest() {
String fileName = "appendModeTest.pdf";
String outputFileName = DESTINATION_FOLDER + fileName;
String cmpFileName = SOURCE_FOLDER + "cmp_" + fileName;
using (PdfDocument pdfDoc = new PdfDocument(new PdfReader(SOURCE_FOLDER + "macProtectedDocument.pdf", new
ReaderProperties().SetPassword(PASSWORD)), CompareTool.CreateTestPdfWriter(outputFileName), new StampingProperties
().UseAppendMode())) {
pdfDoc.AddNewPage().AddAnnotation(new PdfTextAnnotation(new Rectangle(100, 100, 100, 100)));
}
NUnit.Framework.Assert.IsNull(new CompareTool().EnableEncryptionCompare().CompareByContent(outputFileName,
cmpFileName, DESTINATION_FOLDER, "diff", PASSWORD, PASSWORD));
}

[NUnit.Framework.Test]
public virtual void PreserveEncryptionTest() {
String fileName = "preserveEncryptionTest.pdf";
String outputFileName = DESTINATION_FOLDER + fileName;
String cmpFileName = SOURCE_FOLDER + "cmp_" + fileName;
using (PdfDocument pdfDoc = new PdfDocument(new PdfReader(SOURCE_FOLDER + "macProtectedDocument.pdf", new
ReaderProperties().SetPassword(PASSWORD)), CompareTool.CreateTestPdfWriter(outputFileName), new StampingProperties
().PreserveEncryption())) {
pdfDoc.AddNewPage().AddAnnotation(new PdfTextAnnotation(new Rectangle(100, 100, 100, 100)));
}
NUnit.Framework.Assert.IsNull(new CompareTool().EnableEncryptionCompare().CompareByContent(outputFileName,
cmpFileName, DESTINATION_FOLDER, "diff", PASSWORD, PASSWORD));
}

[NUnit.Framework.Test]
public virtual void WriterPropertiesTest() {
String fileName = "writerPropertiesTest.pdf";
String outputFileName = DESTINATION_FOLDER + fileName;
String cmpFileName = SOURCE_FOLDER + "cmp_" + fileName;
MacProperties macProperties = new MacProperties(MacProperties.MacDigestAlgorithm.SHA_512);
WriterProperties writerProperties = new WriterProperties().SetPdfVersion(PdfVersion.PDF_2_0).SetStandardEncryption
(PASSWORD, PASSWORD, 0, EncryptionConstants.ENCRYPTION_AES_256, macProperties);
using (PdfDocument pdfDoc = new PdfDocument(new PdfReader(SOURCE_FOLDER + "macProtectedDocument.pdf", new
ReaderProperties().SetPassword(PASSWORD)), CompareTool.CreateTestPdfWriter(outputFileName, writerProperties
))) {
pdfDoc.AddNewPage().AddAnnotation(new PdfTextAnnotation(new Rectangle(100, 100, 100, 100)));
}
NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outputFileName, cmpFileName, DESTINATION_FOLDER
, "diff", PASSWORD, PASSWORD));
}

[NUnit.Framework.Test]
public virtual void MacShouldNotBePreservedWithEncryptionTest() {
String fileName = "macShouldNotBePreservedWithEncryptionTest.pdf";
String outputFileName = DESTINATION_FOLDER + fileName;
String cmpFileName = SOURCE_FOLDER + "cmp_" + fileName;
WriterProperties writerProperties = new WriterProperties().SetPdfVersion(PdfVersion.PDF_1_7).SetStandardEncryption
(PASSWORD, PASSWORD, 0, EncryptionConstants.ENCRYPTION_AES_128);
using (PdfDocument pdfDoc = new PdfDocument(new PdfReader(SOURCE_FOLDER + "macProtectedDocument.pdf", new
ReaderProperties().SetPassword(PASSWORD)), CompareTool.CreateTestPdfWriter(outputFileName, writerProperties
))) {
pdfDoc.AddNewPage().AddAnnotation(new PdfTextAnnotation(new Rectangle(100, 100, 100, 100)));
}
NUnit.Framework.Assert.IsNull(new CompareTool().EnableEncryptionCompare().CompareByContent(outputFileName,
cmpFileName, DESTINATION_FOLDER, "diff", PASSWORD, PASSWORD));
}

[NUnit.Framework.Test]
public virtual void MacShouldNotBePreservedTest() {
String fileName = "macShouldNotBePreservedTest.pdf";
String outputFileName = DESTINATION_FOLDER + fileName;
String cmpFileName = SOURCE_FOLDER + "cmp_" + fileName;
using (PdfDocument pdfDoc = new PdfDocument(new PdfReader(SOURCE_FOLDER + "macProtectedDocument.pdf", new
ReaderProperties().SetPassword(PASSWORD)), CompareTool.CreateTestPdfWriter(outputFileName))) {
pdfDoc.AddNewPage().AddAnnotation(new PdfTextAnnotation(new Rectangle(100, 100, 100, 100)));
}
NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outputFileName, cmpFileName, DESTINATION_FOLDER
, "diff", PASSWORD, PASSWORD));
}

[NUnit.Framework.Test]
public virtual void InvalidMacTokenTest() {
String fileName = "invalidMacTokenTest.pdf";
String outputFileName = DESTINATION_FOLDER + fileName;
String exceptionMessage = NUnit.Framework.Assert.Catch(typeof(PdfException), () => {
using (PdfDocument pdfDoc = new PdfDocument(new PdfReader(SOURCE_FOLDER + "invalidMacProtectedDocument.pdf"
, new ReaderProperties().SetPassword(PASSWORD)), CompareTool.CreateTestPdfWriter(outputFileName))) {
pdfDoc.AddNewPage().AddAnnotation(new PdfTextAnnotation(new Rectangle(100, 100, 100, 100)));
}
}
).Message;
NUnit.Framework.Assert.AreEqual(KernelExceptionMessageConstant.MAC_VALIDATION_FAILED, exceptionMessage);
}

[NUnit.Framework.Test]
public virtual void InvalidPublicKeyMacProtectedDocumentTest() {
String fileName = "invalidPublicKeyMacProtectedDocumentTest.pdf";
String outputFileName = DESTINATION_FOLDER + fileName;
IX509Certificate certificate = CryptoUtil.ReadPublicCertificate(FileUtil.GetInputStreamForFile(CERTS_SRC +
"SHA256withRSA.cer"));
IPrivateKey privateKey = MacIntegrityProtectorCreationTest.GetPrivateKey(CERTS_SRC + "SHA256withRSA.key");
String exceptionMessage = NUnit.Framework.Assert.Catch(typeof(PdfException), () => {
using (PdfDocument pdfDoc = new PdfDocument(new PdfReader(SOURCE_FOLDER + "invalidPublicKeyMacProtectedDocument.pdf"
, new ReaderProperties().SetPublicKeySecurityParams(certificate, privateKey)), CompareTool.CreateTestPdfWriter
(outputFileName))) {
pdfDoc.AddNewPage().AddAnnotation(new PdfTextAnnotation(new Rectangle(100, 100, 100, 100)));
}
}
).Message;
NUnit.Framework.Assert.AreEqual(KernelExceptionMessageConstant.MAC_VALIDATION_FAILED, exceptionMessage);
}

[NUnit.Framework.Test]
public virtual void ReadThirdPartyMacProtectedDocumentTest() {
NUnit.Framework.Assert.DoesNotThrow(() => {
using (PdfDocument pdfDoc = new PdfDocument(new PdfReader(SOURCE_FOLDER + "thirdPartyMacProtectedDocument.pdf"
, new ReaderProperties().SetPassword(PASSWORD)))) {
}
}
);
}

[NUnit.Framework.Test]
public virtual void ReadThirdPartyPublicKeyMacProtectedDocumentTest() {
IPrivateKey privateKey = MacIntegrityProtectorCreationTest.GetPrivateKey(CERTS_SRC + "keyForEncryption.pem"
);
IX509Certificate certificate = CryptoUtil.ReadPublicCertificate(FileUtil.GetInputStreamForFile(CERTS_SRC +
"certForEncryption.crt"));
NUnit.Framework.Assert.DoesNotThrow(() => {
using (PdfDocument pdfDoc = new PdfDocument(new PdfReader(SOURCE_FOLDER + "thirdPartyPublicKeyMacProtectedDocument.pdf"
, new ReaderProperties().SetPublicKeySecurityParams(certificate, privateKey)))) {
}
}
);
}

[NUnit.Framework.Test]
public virtual void ReadMacProtectedPdf1_7() {
NUnit.Framework.Assert.DoesNotThrow(() => {
using (PdfDocument pdfDoc = new PdfDocument(new PdfReader(SOURCE_FOLDER + "macProtectedDocumentPdf1_7.pdf"
, new ReaderProperties().SetPassword(PASSWORD)))) {
}
}
);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ public class PdfCanvasTest : ExtendedITextTest {

private const String TITLE = "Empty iText Document";

private sealed class _ContentProvider_77 : PdfCanvasTest.ContentProvider {
public _ContentProvider_77() {
private sealed class _ContentProvider_76 : PdfCanvasTest.ContentProvider {
public _ContentProvider_76() {
}

public void DrawOnCanvas(PdfCanvas canvas, int pageNumber) {
Expand All @@ -68,7 +68,7 @@ public void DrawOnCanvas(PdfCanvas canvas, int pageNumber) {
}
}

private static readonly PdfCanvasTest.ContentProvider DEFAULT_CONTENT_PROVIDER = new _ContentProvider_77();
private static readonly PdfCanvasTest.ContentProvider DEFAULT_CONTENT_PROVIDER = new _ContentProvider_76();

[NUnit.Framework.OneTimeSetUp]
public static void BeforeClass() {
Expand Down Expand Up @@ -242,12 +242,12 @@ public virtual void Create1000PagesDocumentWithText() {
int pageCount = 1000;
String filename = DESTINATION_FOLDER + "1000PagesDocumentWithText.pdf";
PdfWriter writer = CompareTool.CreateTestPdfWriter(filename);
CreateStandardDocument(writer, pageCount, new _ContentProvider_378());
CreateStandardDocument(writer, pageCount, new _ContentProvider_377());
AssertStandardDocument(filename, pageCount);
}

private sealed class _ContentProvider_378 : PdfCanvasTest.ContentProvider {
public _ContentProvider_378() {
private sealed class _ContentProvider_377 : PdfCanvasTest.ContentProvider {
public _ContentProvider_377() {
}

public void DrawOnCanvas(PdfCanvas canvas, int pageNumber) {
Expand Down Expand Up @@ -860,16 +860,16 @@ public virtual void CanvasInitializationStampingExistingStream() {
[NUnit.Framework.Test]
public virtual void CanvasStreamFlushedNoException() {
PdfDocument doc = new PdfDocument(new PdfWriter(new ByteArrayOutputStream()));
PdfStream stream = new _PdfStream_1112();
PdfStream stream = new _PdfStream_1111();
stream.Put(PdfName.Filter, new PdfName("FlateDecode"));
NUnit.Framework.Assert.DoesNotThrow(() => {
new PdfCanvas(stream, new PdfResources(), doc);
}
);
}

private sealed class _PdfStream_1112 : PdfStream {
public _PdfStream_1112() {
private sealed class _PdfStream_1111 : PdfStream {
public _PdfStream_1111() {
this.isFlushed = false;
}

Expand All @@ -889,7 +889,7 @@ public override bool IsFlushed() {
public virtual void CanvasInitializationStampingExistingStreamMemoryLimitAware() {
String srcFile = SOURCE_FOLDER + "pageWithContent.pdf";
ReaderProperties properties = new ReaderProperties();
MemoryLimitsAwareHandler handler = new _MemoryLimitsAwareHandler_1135();
MemoryLimitsAwareHandler handler = new _MemoryLimitsAwareHandler_1134();
handler.SetMaxSizeOfSingleDecompressedPdfStream(1);
properties.SetMemoryLimitsAwareHandler(handler);
PdfDocument document = new PdfDocument(new PdfReader(srcFile, properties));
Expand All @@ -900,8 +900,8 @@ public virtual void CanvasInitializationStampingExistingStreamMemoryLimitAware()
);
}

private sealed class _MemoryLimitsAwareHandler_1135 : MemoryLimitsAwareHandler {
public _MemoryLimitsAwareHandler_1135() {
private sealed class _MemoryLimitsAwareHandler_1134 : MemoryLimitsAwareHandler {
public _MemoryLimitsAwareHandler_1134() {
}

public override bool IsMemoryLimitsAwarenessRequiredOnDecompression(PdfArray filters) {
Expand Down Expand Up @@ -1212,6 +1212,28 @@ public virtual void CreateSimpleCanvasWithPdfArrayText() {
));
}

[NUnit.Framework.Test]
public virtual void IgnorePageRotationForContentTest() {
String outPdf = DESTINATION_FOLDER + "ignorePageRotationForContent.pdf";
String cmpPdf = SOURCE_FOLDER + "cmp_ignorePageRotationForContent.pdf";
using (PdfDocument pdfDoc = new PdfDocument(CompareTool.CreateTestPdfWriter(outPdf))) {
pdfDoc.GetDocumentInfo().SetAuthor(AUTHOR).SetCreator(CREATOR).SetTitle(TITLE);
PdfPage page = pdfDoc.AddNewPage().SetRotation(270);
// When "true": in case the page has a rotation, then new content will be automatically rotated in the
// opposite direction. On the rotated page this would look as if new content ignores page rotation.
page.SetIgnorePageRotationForContent(true);
PdfCanvas canvas = new PdfCanvas(page, false);
canvas.SaveState().BeginText().MoveText(180, 350).SetFontAndSize(PdfFontFactory.CreateFont(StandardFonts.HELVETICA
), 30).ShowText("Page rotation is set to 270 degrees,").EndText().RestoreState();
PdfCanvas canvas2 = new PdfCanvas(page, false);
canvas2.SaveState().BeginText().MoveText(180, 250).SetFontAndSize(PdfFontFactory.CreateFont(StandardFonts.
HELVETICA), 30).ShowText("but new content ignores page rotation").EndText().RestoreState();
page.Flush();
}
NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outPdf, cmpPdf, DESTINATION_FOLDER, "diff_"
));
}

private void CreateStandardDocument(PdfWriter writer, int pageCount, PdfCanvasTest.ContentProvider contentProvider
) {
PdfDocument pdfDoc = new PdfDocument(writer);
Expand All @@ -1229,7 +1251,7 @@ private void CreateStandardDocument(PdfWriter writer, int pageCount, PdfCanvasTe
private void AssertStandardDocument(String filename, int pageCount) {
PdfReader reader = CompareTool.CreateOutputReader(filename);
PdfDocument pdfDocument = new PdfDocument(reader);
NUnit.Framework.Assert.AreEqual(false, reader.HasRebuiltXref(), "Rebuilt");
NUnit.Framework.Assert.IsFalse(reader.HasRebuiltXref(), "Rebuilt");
PdfDictionary info = pdfDocument.GetTrailer().GetAsDictionary(PdfName.Info);
NUnit.Framework.Assert.AreEqual(AUTHOR, info.Get(PdfName.Author).ToString(), "Author");
NUnit.Framework.Assert.AreEqual(CREATOR, info.Get(PdfName.Creator).ToString(), "Creator");
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading

0 comments on commit f5b46bf

Please sign in to comment.