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 7, 2024
2 parents a242929 + 300f172 commit 49a8e36
Show file tree
Hide file tree
Showing 39 changed files with 432 additions and 422 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ public virtual void MetadataReadingInEncryptedDoc() {
PdfReader reader = new PdfReader(sourceFolder + "encryptedWithPlainMetadata.pdf", new ReaderProperties().SetPassword
(PdfEncryptionTestUtils.OWNER));
PdfDocument doc = new PdfDocument(reader);
XMPMeta xmpMeta = XMPMetaFactory.ParseFromBuffer(doc.GetXmpMetadata());
XMPMeta xmpMeta = doc.GetXmpMetadata();
XMPProperty creatorToolXmp = xmpMeta.GetProperty(XMPConst.NS_XMP, "CreatorTool");
doc.Close();
NUnit.Framework.Assert.IsNotNull(creatorToolXmp);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,64 +187,6 @@ public virtual void CopyPagesFlushedResources() {
}
}

[NUnit.Framework.Test]
public virtual void PdfDocumentInstanceNoWriterInfoAndConformanceLevelInitialization() {
PdfDocument pdfDocument = new PdfDocument(new PdfReader(SOURCE_FOLDER + "pdfWithMetadata.pdf"));
NUnit.Framework.Assert.IsNull(pdfDocument.info);
NUnit.Framework.Assert.IsNull(pdfDocument.reader.pdfAConformanceLevel);
pdfDocument.Close();
NUnit.Framework.Assert.IsNull(pdfDocument.info);
NUnit.Framework.Assert.IsNull(pdfDocument.reader.pdfAConformanceLevel);
}

[NUnit.Framework.Test]
public virtual void PdfDocumentInstanceWriterInfoAndConformanceLevelInitialization() {
PdfDocument pdfDocument = new PdfDocument(new PdfReader(SOURCE_FOLDER + "pdfWithMetadata.pdf"), new PdfWriter
(new ByteArrayOutputStream()));
NUnit.Framework.Assert.IsNotNull(pdfDocument.info);
NUnit.Framework.Assert.IsNull(pdfDocument.reader.pdfAConformanceLevel);
pdfDocument.Close();
NUnit.Framework.Assert.IsNotNull(pdfDocument.info);
NUnit.Framework.Assert.IsNull(pdfDocument.reader.pdfAConformanceLevel);
}

[NUnit.Framework.Test]
public virtual void ExtendedPdfDocumentNoWriterInfoAndConformanceLevelInitialization() {
PdfDocument pdfDocument = new _PdfDocument_252(new PdfReader(SOURCE_FOLDER + "pdfWithMetadata.pdf"));
// This class instance extends pdfDocument
// TODO DEVSIX-5292 These fields shouldn't be initialized during the document's opening
NUnit.Framework.Assert.IsNotNull(pdfDocument.info);
NUnit.Framework.Assert.IsNotNull(pdfDocument.reader.pdfAConformanceLevel);
pdfDocument.Close();
NUnit.Framework.Assert.IsNotNull(pdfDocument.info);
NUnit.Framework.Assert.IsNotNull(pdfDocument.reader.pdfAConformanceLevel);
}

private sealed class _PdfDocument_252 : PdfDocument {
public _PdfDocument_252(PdfReader baseArg1)
: base(baseArg1) {
}
}

[NUnit.Framework.Test]
public virtual void ExtendedPdfDocumentWriterInfoAndConformanceLevelInitialization() {
PdfDocument pdfDocument = new _PdfDocument_269(new PdfReader(SOURCE_FOLDER + "pdfWithMetadata.pdf"), new PdfWriter
(new ByteArrayOutputStream()));
// This class instance extends pdfDocument
NUnit.Framework.Assert.IsNotNull(pdfDocument.info);
// TODO DEVSIX-5292 pdfAConformanceLevel shouldn't be initialized during the document's opening
NUnit.Framework.Assert.IsNotNull(pdfDocument.reader.pdfAConformanceLevel);
pdfDocument.Close();
NUnit.Framework.Assert.IsNotNull(pdfDocument.info);
NUnit.Framework.Assert.IsNotNull(pdfDocument.reader.pdfAConformanceLevel);
}

private sealed class _PdfDocument_269 : PdfDocument {
public _PdfDocument_269(PdfReader baseArg1, PdfWriter baseArg2)
: base(baseArg1, baseArg2) {
}
}

[NUnit.Framework.Test]
public virtual void GetDocumentInfoAlreadyClosedTest() {
PdfDocument pdfDocument = new PdfDocument(new PdfReader(SOURCE_FOLDER + "pdfWithMetadata.pdf"));
Expand All @@ -253,17 +195,15 @@ public virtual void GetDocumentInfoAlreadyClosedTest() {
}

[NUnit.Framework.Test]
public virtual void GetDocumentInfoNotInitializedTest() {
public virtual void GetDocumentInfoInitializationTest() {
PdfDocument pdfDocument = new PdfDocument(new PdfReader(SOURCE_FOLDER + "pdfWithMetadata.pdf"));
NUnit.Framework.Assert.IsNull(pdfDocument.info);
NUnit.Framework.Assert.IsNotNull(pdfDocument.GetDocumentInfo());
pdfDocument.Close();
}

[NUnit.Framework.Test]
public virtual void GetPdfAConformanceLevelNotInitializedTest() {
public virtual void GetPdfAConformanceLevelInitializationTest() {
PdfDocument pdfDocument = new PdfDocument(new PdfReader(SOURCE_FOLDER + "pdfWithMetadata.pdf"));
NUnit.Framework.Assert.IsNull(pdfDocument.reader.pdfAConformanceLevel);
NUnit.Framework.Assert.IsNotNull(pdfDocument.reader.GetPdfAConformanceLevel());
pdfDocument.Close();
}
Expand Down
43 changes: 29 additions & 14 deletions itext.tests/itext.kernel.tests/itext/kernel/pdf/PdfReaderTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2357,14 +2357,14 @@ public virtual void StreamWithoutEndstreamKeywordTest() {
using (PdfReader reader = new PdfReader(fileName)) {
reader.SetStrictnessLevel(PdfReader.StrictnessLevel.LENIENT);
using (PdfDocument document = new PdfDocument(reader)) {
// Initialize xmp metadata, because we in reader mode in which xmp will be initialized only during closing
byte[] metadataBytes = document.GetXmpMetadataBytes();
PdfCatalog catalog = new PdfCatalog((PdfDictionary)reader.trailer.Get(PdfName.Root, true));
PdfStream xmpMetadataStream = catalog.GetPdfObject().GetAsStream(PdfName.Metadata);
int xmpMetadataStreamLength = ((PdfNumber)xmpMetadataStream.Get(PdfName.Length)).IntValue();
// 27600 is actual invalid length of stream. In reader StrictnessLevel#LENIENT we expect, that this
// length will be fixed.
NUnit.Framework.Assert.AreNotEqual(27600, xmpMetadataStreamLength);
// 3090 is expected length of the stream after fix.
// Initial length was 27600. 3090 is expected length of the stream after the fix
NUnit.Framework.Assert.AreEqual(3090, xmpMetadataStreamLength);
NUnit.Framework.Assert.AreEqual(3090, metadataBytes.Length);
}
}
}
Expand All @@ -2374,7 +2374,10 @@ public virtual void StreamWithoutEndstreamKeywordConservativeModeTest() {
String fileName = SOURCE_FOLDER + "NoEndstreamKeyword.pdf";
using (PdfReader reader = new PdfReader(fileName)) {
reader.SetStrictnessLevel(PdfReader.StrictnessLevel.CONSERVATIVE);
Exception exception = NUnit.Framework.Assert.Catch(typeof(PdfException), () => new PdfDocument(reader));
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(
));
NUnit.Framework.Assert.AreEqual(KernelExceptionMessageConstant.STREAM_SHALL_END_WITH_ENDSTREAM, exception.
Message);
PdfCatalog catalog = new PdfCatalog((PdfDictionary)reader.trailer.Get(PdfName.Root, true));
Expand All @@ -2385,6 +2388,18 @@ public virtual void StreamWithoutEndstreamKeywordConservativeModeTest() {
}
}

[NUnit.Framework.Test]
public virtual void StreamWithoutEndKeyConservativeModeWithWriterTest() {
String fileName = SOURCE_FOLDER + "NoEndstreamKeyword.pdf";
using (PdfReader reader = new PdfReader(fileName)) {
reader.SetStrictnessLevel(PdfReader.StrictnessLevel.CONSERVATIVE);
Exception exception = NUnit.Framework.Assert.Catch(typeof(PdfException), () => new PdfDocument(reader, new
PdfWriter(new ByteArrayOutputStream())));
NUnit.Framework.Assert.AreEqual(KernelExceptionMessageConstant.STREAM_SHALL_END_WITH_ENDSTREAM, exception.
Message);
}
}

[NUnit.Framework.Test]
public virtual void TokensPositionIsNotUpdatedWhileReadingLengthTest() {
String filename = SOURCE_FOLDER + "simpleDocWithIndirectLength.pdf";
Expand Down Expand Up @@ -2418,7 +2433,7 @@ public virtual void ConformanceLevelCacheTest() {
for (int i = 0; i < 1000; ++i) {
pdfTestDoc.GetReader().GetPdfAConformanceLevel();
}
NUnit.Framework.Assert.AreEqual(2, pdfTestDoc.GetCounter());
NUnit.Framework.Assert.AreEqual(1, pdfTestDoc.GetCounter());
}

[NUnit.Framework.Test]
Expand Down Expand Up @@ -2520,14 +2535,14 @@ public virtual void StreamObjIsNullTest() {
int objNumber = pdfDictionary.GetIndirectReference().objNr;
pdfDocument.catalog.GetPdfObject().Put(PdfName.StructTreeRoot, pdfDictionary);
pdfDocument.Close();
PdfReader pdfReader = new _PdfReader_2841(objNumber, new MemoryStream(bsaos.ToArray()));
PdfReader pdfReader = new _PdfReader_2853(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_2841 : PdfReader {
public _PdfReader_2841(int objNumber, Stream baseArg1)
private sealed class _PdfReader_2853 : PdfReader {
public _PdfReader_2853(int objNumber, Stream baseArg1)
: base(baseArg1) {
this.objNumber = objNumber;
}
Expand All @@ -2546,7 +2561,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_2860();
MemoryLimitsAwareHandler memoryLimitsAwareHandler = new _MemoryLimitsAwareHandler_2872();
memoryLimitsAwareHandler.SetMaxSizeOfDecompressedPdfStreamsSum(100000);
NUnit.Framework.Assert.Catch(typeof(MemoryLimitsAwareException), () => {
using (PdfReader reader = new PdfReader(file, new ReaderProperties().SetMemoryLimitsAwareHandler(memoryLimitsAwareHandler
Expand All @@ -2558,8 +2573,8 @@ public virtual void InitTagTreeStructureThrowsOOMIsCatched() {
);
}

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

public override bool IsMemoryLimitsAwarenessRequiredOnDecompression(PdfArray filters) {
Expand Down Expand Up @@ -2626,9 +2641,9 @@ public TestPdfDocumentCache(PdfReaderTest _enclosing, PdfReader pdfReader)
this._enclosing = _enclosing;
}

public override byte[] GetXmpMetadata(bool createNew) {
public override byte[] GetXmpMetadataBytes(bool createdNew) {
++this.getXmpMetadataCounter;
return base.GetXmpMetadata(createNew);
return base.GetXmpMetadataBytes(createdNew);
}

public virtual int GetCounter() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ You should have received a copy of the GNU Affero General Public License
using iText.Kernel.Pdf.Canvas.Parser;
using iText.Kernel.Pdf.Canvas.Parser.Listener;
using iText.Kernel.Utils;
using iText.Kernel.XMP;
using iText.Test;
using iText.Test.Attributes;

Expand Down Expand Up @@ -689,8 +688,7 @@ public virtual void StampingXmp1() {
for (int i = 0; i < pdfDoc3.GetNumberOfPages(); i++) {
pdfDoc3.GetPage(i + 1);
}
NUnit.Framework.Assert.IsNotNull(XMPMetaFactory.ParseFromBuffer(pdfDoc3.GetXmpMetadata()), "XmpMetadata not found"
);
NUnit.Framework.Assert.IsNotNull(pdfDoc3.GetXmpMetadata(), "XmpMetadata not found");
NUnit.Framework.Assert.AreEqual(pageCount, pdfDoc3.GetNumberOfPages(), "Number of pages");
NUnit.Framework.Assert.IsFalse(reader3.HasRebuiltXref(), "Rebuilt");
NUnit.Framework.Assert.IsFalse(reader3.HasFixedXref(), "Fixed");
Expand Down Expand Up @@ -732,8 +730,7 @@ public virtual void StampingXmp2() {
for (int i = 0; i < pdfDoc3.GetNumberOfPages(); i++) {
pdfDoc3.GetPage(i + 1);
}
NUnit.Framework.Assert.IsNotNull(XMPMetaFactory.ParseFromBuffer(pdfDoc3.GetXmpMetadata()), "XmpMetadata not found"
);
NUnit.Framework.Assert.IsNotNull(pdfDoc3.GetXmpMetadata(), "XmpMetadata not found");
NUnit.Framework.Assert.AreEqual(pageCount, pdfDoc3.GetNumberOfPages(), "Number of pages");
NUnit.Framework.Assert.IsFalse(reader3.HasRebuiltXref(), "Rebuilt");
NUnit.Framework.Assert.IsFalse(reader3.HasFixedXref(), "Fixed");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,13 @@ public static void AfterClass() {
LogLevel = LogLevelConstants.ERROR)]
public virtual void OpenInvalidDocWithHugeRefTest() {
String inputFile = SOURCE_FOLDER + "invalidDocWithHugeRef.pdf";
MemoryLimitsAwareHandler memoryLimitsAwareHandler = new _MemoryLimitsAwareHandler_67();
MemoryLimitsAwareHandler memoryLimitsAwareHandler = new _MemoryLimitsAwareHandler_68();
NUnit.Framework.Assert.DoesNotThrow(() => new PdfDocument(new PdfReader(inputFile, new ReaderProperties().
SetMemoryLimitsAwareHandler(memoryLimitsAwareHandler))));
}

private sealed class _MemoryLimitsAwareHandler_67 : MemoryLimitsAwareHandler {
public _MemoryLimitsAwareHandler_67() {
private sealed class _MemoryLimitsAwareHandler_68 : MemoryLimitsAwareHandler {
public _MemoryLimitsAwareHandler_68() {
}

public override void CheckIfXrefStructureExceedsTheLimit(int requestedCapacity) {
Expand Down
Loading

0 comments on commit 49a8e36

Please sign in to comment.