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 Oct 11, 2024
2 parents 5f008e8 + 2b2ca05 commit 3be9d49
Show file tree
Hide file tree
Showing 69 changed files with 1,278 additions and 968 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ You should have received a copy of the GNU Affero General Public License
*/
using System;
using iText.Kernel.Exceptions;
using iText.Kernel.Logs;
using iText.Test;
using iText.Test.Attributes;

namespace iText.Kernel.Pdf {
[NUnit.Framework.Category("UnitTest")]
Expand All @@ -46,15 +48,15 @@ public virtual void CustomMemoryHandler() {
[NUnit.Framework.Test]
public virtual void OverridenMemoryHandler() {
MemoryLimitsAwareHandler defaultHandler = new MemoryLimitsAwareHandler();
MemoryLimitsAwareHandler customHandler = new _MemoryLimitsAwareHandler_58();
MemoryLimitsAwareHandler customHandler = new _MemoryLimitsAwareHandler_64();
PdfArray filters = new PdfArray();
filters.Add(PdfName.FlateDecode);
NUnit.Framework.Assert.IsFalse(defaultHandler.IsMemoryLimitsAwarenessRequiredOnDecompression(filters));
NUnit.Framework.Assert.IsTrue(customHandler.IsMemoryLimitsAwarenessRequiredOnDecompression(filters));
}

private sealed class _MemoryLimitsAwareHandler_58 : MemoryLimitsAwareHandler {
public _MemoryLimitsAwareHandler_58() {
private sealed class _MemoryLimitsAwareHandler_64 : MemoryLimitsAwareHandler {
public _MemoryLimitsAwareHandler_64() {
}

public override bool IsMemoryLimitsAwarenessRequiredOnDecompression(PdfArray filters) {
Expand Down Expand Up @@ -144,6 +146,54 @@ public virtual void CheckCapacityTest() {
));
}

[NUnit.Framework.Test]
[LogMessage(KernelLogMessageConstant.MEMORYLIMITAWAREHANDLER_OVERRIDE_CREATENEWINSTANCE_METHOD)]
public virtual void CreateCopyMemoryHandlerWarningTest() {
MemoryLimitsAwareHandler customHandler = new _MemoryLimitsAwareHandler_173();
customHandler.SetMaxNumberOfElementsInXrefStructure(1);
customHandler.SetMaxXObjectsSizePerPage(2);
customHandler.SetMaxSizeOfDecompressedPdfStreamsSum(3);
customHandler.SetMaxSizeOfSingleDecompressedPdfStream(4);
MemoryLimitsAwareHandler copy = customHandler.CreateNewInstance();
NUnit.Framework.Assert.AreEqual(1, copy.GetMaxNumberOfElementsInXrefStructure());
NUnit.Framework.Assert.AreEqual(2, copy.GetMaxXObjectsSizePerPage());
NUnit.Framework.Assert.AreEqual(3, copy.GetMaxSizeOfDecompressedPdfStreamsSum());
NUnit.Framework.Assert.AreEqual(4, copy.GetMaxSizeOfSingleDecompressedPdfStream());
}

private sealed class _MemoryLimitsAwareHandler_173 : MemoryLimitsAwareHandler {
public _MemoryLimitsAwareHandler_173() {
}
}

[NUnit.Framework.Test]
public virtual void CreateCopyMemoryHandlerNoWarningTest() {
MemoryLimitsAwareHandler customHandler = new _MemoryLimitsAwareHandler_190();
customHandler.SetMaxNumberOfElementsInXrefStructure(1);
customHandler.SetMaxXObjectsSizePerPage(2);
customHandler.SetMaxSizeOfDecompressedPdfStreamsSum(3);
customHandler.SetMaxSizeOfSingleDecompressedPdfStream(4);
MemoryLimitsAwareHandler copy = customHandler.CreateNewInstance();
NUnit.Framework.Assert.AreEqual(1, copy.GetMaxNumberOfElementsInXrefStructure());
NUnit.Framework.Assert.AreEqual(2, copy.GetMaxXObjectsSizePerPage());
NUnit.Framework.Assert.AreEqual(3, copy.GetMaxSizeOfDecompressedPdfStreamsSum());
NUnit.Framework.Assert.AreEqual(4, copy.GetMaxSizeOfSingleDecompressedPdfStream());
}

private sealed class _MemoryLimitsAwareHandler_190 : MemoryLimitsAwareHandler {
public _MemoryLimitsAwareHandler_190() {
}

public override MemoryLimitsAwareHandler CreateNewInstance() {
MemoryLimitsAwareHandler to = new MemoryLimitsAwareHandler();
to.SetMaxSizeOfSingleDecompressedPdfStream(this.GetMaxSizeOfSingleDecompressedPdfStream());
to.SetMaxSizeOfDecompressedPdfStreamsSum(this.GetMaxSizeOfDecompressedPdfStreamsSum());
to.SetMaxNumberOfElementsInXrefStructure(this.GetMaxNumberOfElementsInXrefStructure());
to.SetMaxXObjectsSizePerPage(this.GetMaxXObjectsSizePerPage());
return to;
}
}

private static void TestSingleStream(MemoryLimitsAwareHandler handler) {
String expectedExceptionMessage = KernelExceptionMessageConstant.DURING_DECOMPRESSION_SINGLE_STREAM_OCCUPIED_MORE_MEMORY_THAN_ALLOWED;
int expectedFailureIndex = 10;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,10 @@ public virtual void Validate(IValidationContext validationContext) {
page = ((PdfPageValidationContext)validationContext).GetPage();
}
}

public virtual bool IsPdfObjectReadyToFlush(PdfObject @object) {
return true;
}
}

private static void Test(String filename, PageFlushingTest.DocMode docMode, PageFlushingTest.FlushMode flushMode
Expand Down
12 changes: 12 additions & 0 deletions itext.tests/itext.kernel.tests/itext/kernel/pdf/PdfDocumentTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -586,6 +586,18 @@ public virtual void RemoveNestedDuplicatesHasChildInOrderArrayTest() {
, "27 0 R"), e.Message);
}

[NUnit.Framework.Test]
public virtual void CreatePdfDocumentWithAAndUaMetadataTest() {
String outputPdf = "pdfDocWithAAndUaMetadata.pdf";
WriterProperties writerProperties = new WriterProperties().AddPdfAXmpMetadata(PdfAConformance.PDF_A_3A).AddPdfUaXmpMetadata
(PdfUAConformance.PDF_UA_1);
PdfDocument doc = new PdfDocument(new PdfWriter(DESTINATION_FOLDER + outputPdf, writerProperties));
doc.AddNewPage();
doc.Close();
NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(DESTINATION_FOLDER + outputPdf, SOURCE_FOLDER
+ "cmp_" + outputPdf, DESTINATION_FOLDER));
}

private class IgnoreTagStructurePdfDocument : PdfDocument {
//\cond DO_NOT_DOCUMENT
internal IgnoreTagStructurePdfDocument(PdfReader reader)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,10 @@ public virtual void Validate(IValidationContext validationContext) {
documentValidationPerformed = true;
}
}

public virtual bool IsPdfObjectReadyToFlush(PdfObject @object) {
return true;
}
}
}
}
17 changes: 7 additions & 10 deletions itext.tests/itext.kernel.tests/itext/kernel/pdf/PdfReaderTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2374,10 +2374,7 @@ public virtual void StreamWithoutEndstreamKeywordConservativeModeTest() {
String fileName = SOURCE_FOLDER + "NoEndstreamKeyword.pdf";
using (PdfReader reader = new PdfReader(fileName)) {
reader.SetStrictnessLevel(PdfReader.StrictnessLevel.CONSERVATIVE);
PdfDocument pdfDocument = new PdfDocument(reader);
// Initialize xmp metadata, because we in reader mode in which xmp will be initialized only during closing
Exception exception = NUnit.Framework.Assert.Catch(typeof(PdfException), () => pdfDocument.GetXmpMetadata(
));
Exception exception = NUnit.Framework.Assert.Catch(typeof(PdfException), () => new PdfDocument(reader));
NUnit.Framework.Assert.AreEqual(KernelExceptionMessageConstant.STREAM_SHALL_END_WITH_ENDSTREAM, exception.
Message);
PdfCatalog catalog = new PdfCatalog((PdfDictionary)reader.trailer.Get(PdfName.Root, true));
Expand Down Expand Up @@ -2535,14 +2532,14 @@ public virtual void StreamObjIsNullTest() {
int objNumber = pdfDictionary.GetIndirectReference().objNr;
pdfDocument.catalog.GetPdfObject().Put(PdfName.StructTreeRoot, pdfDictionary);
pdfDocument.Close();
PdfReader pdfReader = new _PdfReader_2853(objNumber, new MemoryStream(bsaos.ToArray()));
PdfReader pdfReader = new _PdfReader_2851(objNumber, new MemoryStream(bsaos.ToArray()));
Exception e = NUnit.Framework.Assert.Catch(typeof(PdfException), () => new PdfDocument(pdfReader));
NUnit.Framework.Assert.AreEqual(MessageFormatUtil.Format(KernelExceptionMessageConstant.INVALID_OBJECT_STREAM_NUMBER
, 5, 4, 492), e.Message);
}

private sealed class _PdfReader_2853 : PdfReader {
public _PdfReader_2853(int objNumber, Stream baseArg1)
private sealed class _PdfReader_2851 : PdfReader {
public _PdfReader_2851(int objNumber, Stream baseArg1)
: base(baseArg1) {
this.objNumber = objNumber;
}
Expand All @@ -2561,7 +2558,7 @@ protected internal override PdfObject ReadObject(PdfIndirectReference reference)
[NUnit.Framework.Test]
public virtual void InitTagTreeStructureThrowsOOMIsCatched() {
FileInfo file = new FileInfo(SOURCE_FOLDER + "big_table_lot_of_mcrs.pdf");
MemoryLimitsAwareHandler memoryLimitsAwareHandler = new _MemoryLimitsAwareHandler_2872();
MemoryLimitsAwareHandler memoryLimitsAwareHandler = new _MemoryLimitsAwareHandler_2870();
memoryLimitsAwareHandler.SetMaxSizeOfDecompressedPdfStreamsSum(100000);
NUnit.Framework.Assert.Catch(typeof(MemoryLimitsAwareException), () => {
using (PdfReader reader = new PdfReader(file, new ReaderProperties().SetMemoryLimitsAwareHandler(memoryLimitsAwareHandler
Expand All @@ -2573,8 +2570,8 @@ public virtual void InitTagTreeStructureThrowsOOMIsCatched() {
);
}

private sealed class _MemoryLimitsAwareHandler_2872 : MemoryLimitsAwareHandler {
public _MemoryLimitsAwareHandler_2872() {
private sealed class _MemoryLimitsAwareHandler_2870 : MemoryLimitsAwareHandler {
public _MemoryLimitsAwareHandler_2870() {
}

public override bool IsMemoryLimitsAwarenessRequiredOnDecompression(PdfArray filters) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
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 iText.Test;

namespace iText.Kernel.Pdf {
[NUnit.Framework.Category("UnitTest")]
public class ReaderPropertiesTest : ExtendedITextTest {
[NUnit.Framework.Test]
public virtual void CopyConstructorTest() {
MemoryLimitsAwareHandler handler = new MemoryLimitsAwareHandler();
handler.SetMaxXObjectsSizePerPage(10);
ReaderProperties properties = new ReaderProperties().SetPassword("123".GetBytes(iText.Commons.Utils.EncodingUtil.ISO_8859_1
)).SetMemoryLimitsAwareHandler(handler);
ReaderProperties copy = new ReaderProperties(properties);
NUnit.Framework.Assert.AreEqual(copy.password, properties.password);
NUnit.Framework.Assert.AreNotEqual(copy.memoryLimitsAwareHandler, properties.memoryLimitsAwareHandler);
NUnit.Framework.Assert.AreEqual(copy.memoryLimitsAwareHandler.GetMaxXObjectsSizePerPage(), properties.memoryLimitsAwareHandler
.GetMaxXObjectsSizePerPage());
NUnit.Framework.Assert.AreEqual(10, copy.memoryLimitsAwareHandler.GetMaxXObjectsSizePerPage());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,10 @@ public virtual void Validate(IValidationContext validationContext) {
gState = glyphsContext.GetGraphicsState();
}
}

public virtual bool IsPdfObjectReadyToFlush(PdfObject @object) {
return true;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
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 iText.Kernel.Pdf;
using iText.Kernel.Validation;
using iText.Kernel.Validation.Context;
using iText.Test;
Expand Down Expand Up @@ -66,6 +67,10 @@ public virtual void Validate(IValidationContext validationContext) {
objectValidationPerformed = true;
}
}

public virtual bool IsPdfObjectReadyToFlush(PdfObject @object) {
return true;
}
}
}
}
Binary file not shown.
8 changes: 4 additions & 4 deletions itext.tests/itext.layout.tests/itext/layout/XMPWriterTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ public virtual void CreatePdfTest() {
[NUnit.Framework.Test]
public virtual void AddUAXMPMetaDataNotTaggedTest() {
String fileName = "addUAXMPMetaDataNotTaggedTest.pdf";
PdfDocument pdf = new PdfDocument(new PdfWriter(destinationFolder + fileName, new WriterProperties().AddUAXmpMetadata
()));
WriterProperties writerProperties = new WriterProperties().AddPdfUaXmpMetadata(PdfUAConformance.PDF_UA_1);
PdfDocument pdf = new PdfDocument(new PdfWriter(destinationFolder + fileName, writerProperties));
ManipulatePdf(pdf, false);
NUnit.Framework.Assert.IsNull(new CompareTool().CompareXmp(destinationFolder + fileName, sourceFolder + "cmp_"
+ fileName, true));
Expand All @@ -93,8 +93,8 @@ public virtual void AddUAXMPMetaDataNotTaggedTest() {
[NUnit.Framework.Test]
public virtual void AddUAXMPMetaDataTaggedTest() {
String fileName = "addUAXMPMetaDataTaggedTest.pdf";
PdfDocument pdf = new PdfDocument(new PdfWriter(destinationFolder + fileName, new WriterProperties().AddUAXmpMetadata
()));
WriterProperties writerProperties = new WriterProperties().AddPdfUaXmpMetadata(PdfUAConformance.PDF_UA_1);
PdfDocument pdf = new PdfDocument(new PdfWriter(destinationFolder + fileName, writerProperties));
ManipulatePdf(pdf, true);
NUnit.Framework.Assert.IsNull(new CompareTool().CompareXmp(destinationFolder + fileName, sourceFolder + "cmp_"
+ fileName, true));
Expand Down
Binary file not shown.
Loading

0 comments on commit 3be9d49

Please sign in to comment.