Пример #1
0
        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 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 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);
            }
        }
Пример #4
0
        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);
        }
        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))
            ;
        }