public virtual void DefaultMemoryHandler() { MemoryLimitsAwareHandler handler = new MemoryLimitsAwareHandler(); NUnit.Framework.Assert.AreEqual(int.MaxValue / 100, handler.GetMaxSizeOfSingleDecompressedPdfStream()); NUnit.Framework.Assert.AreEqual(int.MaxValue / 20, handler.GetMaxSizeOfDecompressedPdfStreamsSum()); }
public virtual void CustomMemoryHandler() { MemoryLimitsAwareHandler handler = new MemoryLimitsAwareHandler(1000000); NUnit.Framework.Assert.AreEqual(100000000, handler.GetMaxSizeOfSingleDecompressedPdfStream()); NUnit.Framework.Assert.AreEqual(500000000, handler.GetMaxSizeOfDecompressedPdfStreamsSum()); }
public virtual void CustomMemoryHandlerSingleTest() { MemoryLimitsAwareHandler handler = new MemoryLimitsAwareHandler(); handler.SetMaxSizeOfSingleDecompressedPdfStream(1000); PdfDocument pdfDocument = new PdfDocument(new PdfReader(sourceFolder + "timing.pdf", new ReaderProperties( ).SetMemoryLimitsAwareHandler(handler)), new PdfWriter(new MemoryStream())); PdfStream stream = pdfDocument.GetFirstPage().GetContentStream(0); byte[] b = stream.GetBytes(false); PdfArray array = new PdfArray(); stream.Put(PdfName.Filter, array); NUnit.Framework.Assert.AreEqual(51, PdfReader.DecodeBytes(b, stream).Length); array.Add(PdfName.Fl); NUnit.Framework.Assert.AreEqual(40, PdfReader.DecodeBytes(b, stream).Length); array.Add(PdfName.Fl); NUnit.Framework.Assert.AreEqual(992, PdfReader.DecodeBytes(b, stream).Length); array.Add(PdfName.Fl); String expectedExceptionMessage = PdfException.DuringDecompressionSingleStreamOccupiedMoreMemoryThanAllowed; String thrownExceptionMessage = null; try { PdfReader.DecodeBytes(b, stream); } catch (MemoryLimitsAwareException e) { thrownExceptionMessage = e.Message; } NUnit.Framework.Assert.AreEqual(expectedExceptionMessage, thrownExceptionMessage); }
public virtual void OverridenMemoryHandler() { MemoryLimitsAwareHandler defaultHandler = new MemoryLimitsAwareHandler(); MemoryLimitsAwareHandler customHandler = new _MemoryLimitsAwareHandler_75(); PdfArray filters = new PdfArray(); filters.Add(PdfName.FlateDecode); NUnit.Framework.Assert.IsFalse(defaultHandler.IsMemoryLimitsAwarenessRequiredOnDecompression(filters)); NUnit.Framework.Assert.IsTrue(customHandler.IsMemoryLimitsAwarenessRequiredOnDecompression(filters)); }
public virtual void PageAsSingleStreamTest() { MemoryLimitsAwareHandler handler = new MemoryLimitsAwareHandler(); handler.SetMaxSizeOfSingleDecompressedPdfStream(1500000); NUnit.Framework.Assert.That(() => { using (PdfDocument pdfDocument = new PdfDocument(new PdfReader(sourceFolder + "timing.pdf", new ReaderProperties ().SetMemoryLimitsAwareHandler(handler)), new PdfWriter(new MemoryStream()))) { pdfDocument.GetFirstPage().GetContentBytes(); } } , NUnit.Framework.Throws.InstanceOf <MemoryLimitsAwareException>().With.Message.EqualTo(PdfException.DuringDecompressionSingleStreamOccupiedMoreMemoryThanAllowed)) ; }
public virtual void CustomMemoryHandlerSumTest() { MemoryLimitsAwareHandler handler = new MemoryLimitsAwareHandler(); handler.SetMaxSizeOfDecompressedPdfStreamsSum(100000); NUnit.Framework.Assert.That(() => { using (PdfDocument pdfDocument = new PdfDocument(new PdfReader(sourceFolder + "timing.pdf", new ReaderProperties ().SetMemoryLimitsAwareHandler(handler)), new PdfWriter(new MemoryStream()))) { PdfStream stream = pdfDocument.GetFirstPage().GetContentStream(0); byte[] b = stream.GetBytes(false); PdfReader.DecodeBytes(b, stream); } } , NUnit.Framework.Throws.InstanceOf <MemoryLimitsAwareException>().With.Message.EqualTo(PdfException.DuringDecompressionMultipleStreamsInSumOccupiedMoreMemoryThanAllowed)) ; }
public virtual void OneFilterCustomMemoryHandlerSingleTest() { MemoryLimitsAwareHandler handler = new MemoryLimitsAwareHandler(); handler.SetMaxSizeOfSingleDecompressedPdfStream(20); using (PdfDocument pdfDocument = new PdfDocument(new PdfReader(sourceFolder + "timing.pdf", new ReaderProperties ().SetMemoryLimitsAwareHandler(handler)), new PdfWriter(new MemoryStream()))) { PdfStream stream = pdfDocument.GetFirstPage().GetContentStream(0); byte[] b = stream.GetBytes(false); PdfArray array = new PdfArray(); stream.Put(PdfName.Filter, array); // Limit is reached, but the stream has no filters. Therefore we don't consider ot to be suspicious NUnit.Framework.Assert.AreEqual(51, PdfReader.DecodeBytes(b, stream).Length); // Limit is reached, but the stream has only one filter. Therefore we don't consider ot to be suspicious array.Add(PdfName.Fl); NUnit.Framework.Assert.AreEqual(40, PdfReader.DecodeBytes(b, stream).Length); } }
public virtual void PageAsSingleStreamTest() { MemoryLimitsAwareHandler handler = new MemoryLimitsAwareHandler(); handler.SetMaxSizeOfSingleDecompressedPdfStream(1500000); PdfDocument pdfDocument = new PdfDocument(new PdfReader(sourceFolder + "timing.pdf", new ReaderProperties( ).SetMemoryLimitsAwareHandler(handler)), new PdfWriter(new MemoryStream())); String expectedExceptionMessage = PdfException.DuringDecompressionSingleStreamOccupiedMoreMemoryThanAllowed; String thrownExceptionMessage = null; try { pdfDocument.GetFirstPage().GetContentBytes(); } catch (MemoryLimitsAwareException e) { thrownExceptionMessage = e.Message; } NUnit.Framework.Assert.AreEqual(expectedExceptionMessage, thrownExceptionMessage); }
/// <summary> /// Creates a /// <see cref="MemoryLimitsAwareOutputStream"/> /// which will be used for decompression of the passed pdf stream. /// </summary> /// <param name="streamDictionary">the pdf stream which is going to be decompressed.</param> /// <returns> /// the /// <see cref="System.IO.MemoryStream"/> /// which will be used for decompression of the passed pdf stream /// </returns> public virtual MemoryStream EnableMemoryLimitsAwareHandler(PdfDictionary streamDictionary) { MemoryLimitsAwareOutputStream outputStream = new MemoryLimitsAwareOutputStream(); MemoryLimitsAwareHandler memoryLimitsAwareHandler = null; if (null != streamDictionary.GetIndirectReference()) { memoryLimitsAwareHandler = streamDictionary.GetIndirectReference().GetDocument().memoryLimitsAwareHandler; } else { // We do not reuse some static instance because one can process pdfs in different threads. memoryLimitsAwareHandler = new MemoryLimitsAwareHandler(); } if (null != memoryLimitsAwareHandler && memoryLimitsAwareHandler.considerCurrentPdfStream) { outputStream.SetMaxStreamSize(memoryLimitsAwareHandler.GetMaxSizeOfSingleDecompressedPdfStream()); } return(outputStream); }
public virtual void CustomMemoryHandlerSumTest() { MemoryLimitsAwareHandler handler = new MemoryLimitsAwareHandler(); handler.SetMaxSizeOfDecompressedPdfStreamsSum(100000); PdfDocument pdfDocument = new PdfDocument(new PdfReader(sourceFolder + "timing.pdf", new ReaderProperties( ).SetMemoryLimitsAwareHandler(handler)), new PdfWriter(new MemoryStream())); PdfStream stream = pdfDocument.GetFirstPage().GetContentStream(0); byte[] b = stream.GetBytes(false); String expectedExceptionMessage = PdfException.DuringDecompressionMultipleStreamsInSumOccupiedMoreMemoryThanAllowed; String thrownExceptionMessage = null; try { PdfReader.DecodeBytes(b, stream); } catch (MemoryLimitsAwareException e) { thrownExceptionMessage = e.Message; } NUnit.Framework.Assert.AreEqual(expectedExceptionMessage, thrownExceptionMessage); }
public virtual void ConsiderBytesTest() { MemoryLimitsAwareHandler handler = new MemoryLimitsAwareHandler(); long state1 = handler.GetAllMemoryUsedForDecompression(); handler.ConsiderBytesOccupiedByDecompressedPdfStream(100); long state2 = handler.GetAllMemoryUsedForDecompression(); NUnit.Framework.Assert.AreEqual(state1, state2); handler.BeginDecompressedPdfStreamProcessing(); handler.ConsiderBytesOccupiedByDecompressedPdfStream(100); long state3 = handler.GetAllMemoryUsedForDecompression(); NUnit.Framework.Assert.AreEqual(state1, state3); handler.ConsiderBytesOccupiedByDecompressedPdfStream(80); long state4 = handler.GetAllMemoryUsedForDecompression(); NUnit.Framework.Assert.AreEqual(state1, state4); handler.EndDecompressedPdfStreamProcessing(); long state5 = handler.GetAllMemoryUsedForDecompression(); NUnit.Framework.Assert.AreEqual(state1 + 100, state5); }
private static void TestMultipleStreams(MemoryLimitsAwareHandler handler) { String expectedExceptionMessage = PdfException.DuringDecompressionMultipleStreamsInSumOccupiedMoreMemoryThanAllowed; int expectedFailureIndex = 10; String occuredExceptionMessage = null; int i = 0; try { long limit = handler.GetMaxSizeOfDecompressedPdfStreamsSum(); long step = limit / 10; for (i = 0; i < 11; i++) { handler.BeginDecompressedPdfStreamProcessing(); handler.ConsiderBytesOccupiedByDecompressedPdfStream(step); handler.EndDecompressedPdfStreamProcessing(); } } catch (MemoryLimitsAwareException e) { occuredExceptionMessage = e.Message; } NUnit.Framework.Assert.AreEqual(expectedFailureIndex, i); NUnit.Framework.Assert.AreEqual(expectedExceptionMessage, occuredExceptionMessage); }
public virtual void CustomMemoryHandlerSingleTest() { MemoryLimitsAwareHandler handler = new MemoryLimitsAwareHandler(); handler.SetMaxSizeOfSingleDecompressedPdfStream(1000); NUnit.Framework.Assert.That(() => { using (PdfDocument pdfDocument = new PdfDocument(new PdfReader(sourceFolder + "timing.pdf", new ReaderProperties ().SetMemoryLimitsAwareHandler(handler)), new PdfWriter(new MemoryStream()))) { PdfStream stream = pdfDocument.GetFirstPage().GetContentStream(0); byte[] b = stream.GetBytes(false); PdfArray array = new PdfArray(); stream.Put(PdfName.Filter, array); NUnit.Framework.Assert.AreEqual(51, PdfReader.DecodeBytes(b, stream).Length); array.Add(PdfName.Fl); NUnit.Framework.Assert.AreEqual(40, PdfReader.DecodeBytes(b, stream).Length); array.Add(PdfName.Fl); NUnit.Framework.Assert.AreEqual(992, PdfReader.DecodeBytes(b, stream).Length); array.Add(PdfName.Fl); PdfReader.DecodeBytes(b, stream); } } , NUnit.Framework.Throws.InstanceOf <MemoryLimitsAwareException>().With.Message.EqualTo(PdfException.DuringDecompressionSingleStreamOccupiedMoreMemoryThanAllowed)) ; }
public virtual void DefaultMultipleMemoryHandler() { MemoryLimitsAwareHandler handler = new MemoryLimitsAwareHandler(); TestMultipleStreams(handler); }
public virtual void DefaultSingleMemoryHandler() { MemoryLimitsAwareHandler handler = new MemoryLimitsAwareHandler(); TestSingleStream(handler); }
/// <summary>Sets the memory handler which will be used to handle decompressed pdf streams.</summary> /// <param name="memoryLimitsAwareHandler">the memory handler which will be used to handle decompressed pdf streams /// </param> /// <returns> /// this /// <see cref="ReaderProperties"/> /// instance. /// </returns> public virtual ReaderProperties SetMemoryLimitsAwareHandler(MemoryLimitsAwareHandler memoryLimitsAwareHandler ) { this.memoryLimitsAwareHandler = memoryLimitsAwareHandler; return(this); }