diff --git a/tests/ImageSharp.Tests/Formats/Png/PngDecoderTests.cs b/tests/ImageSharp.Tests/Formats/Png/PngDecoderTests.cs index 248e0a5ee..09c268c14 100644 --- a/tests/ImageSharp.Tests/Formats/Png/PngDecoderTests.cs +++ b/tests/ImageSharp.Tests/Formats/Png/PngDecoderTests.cs @@ -19,6 +19,39 @@ namespace SixLabors.ImageSharp.Tests { private const PixelTypes PixelTypes = Tests.PixelTypes.Rgba32 | Tests.PixelTypes.RgbaVector | Tests.PixelTypes.Argb32; + // Contains the png marker, IHDR and pHYs chunks of a 1x1 pixel 32bit png 1 a single black pixel. + private static byte[] raw1x1PngIHDRAndpHYs = + { + // PNG Identifier + 0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A, + + // IHDR + 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x08, 0x02, 0x00, 0x00, 0x00, + // IHDR CRC + 0x90, 0x77, 0x53, 0xDE, + + // pHYS + 0x00, 0x00, 0x00, 0x09, 0x70, 0x48, 0x59, 0x73, 0x00, 0x00, 0x0E, 0xC3, 0x00, 0x00, 0x0E, 0xC3, 0x01, + // pHYS CRC + 0xC7, 0x6F, 0xA8, 0x64 + }; + + // Contains the png marker, IDAT and IEND chunks of a 1x1 pixel 32bit png 1 a single black pixel. + private static byte[] raw1x1PngIDATAndIEND = + { + // IDAT + 0x00, 0x00, 0x00, 0x0C, 0x49, 0x44, 0x41, 0x54, 0x18, 0x57, 0x63, 0x60, 0x60, 0x60, 0x00, 0x00, + 0x00, 0x04, 0x00, 0x01, + // IDAT CRC + 0x5C, 0xCD, 0xFF, 0x69, + + // IEND + 0x00, 0x00, 0x00, 0x00, 0x49, 0x45, + 0x4E, 0x44, + // IEND CRC + 0xAE, 0x42, 0x60, 0x82 + }; + public static readonly string[] CommonTestImages = { TestImages.Png.Splash, TestImages.Png.Indexed, @@ -210,11 +243,9 @@ namespace SixLabors.ImageSharp.Tests { using (var memStream = new MemoryStream()) { - memStream.Skip(8); - + WriteHeaderChunk(memStream); WriteChunk(memStream, chunkName); - - CompressStream(memStream); + WriteDataChunk(memStream); var decoder = new PngDecoder(); @@ -230,23 +261,27 @@ namespace SixLabors.ImageSharp.Tests [Theory] [InlineData(PngChunkTypes.Gamma)] [InlineData(PngChunkTypes.PaletteAlpha)] - [InlineData(PngChunkTypes.Physical)] + [InlineData(PngChunkTypes.Physical)] // It's ok to test physical as we don't throw for duplicate chunks. //[InlineData(PngChunkTypes.Text)] //TODO: Figure out how to test this public void Decode_IncorrectCRCForNonCriticalChunk_ExceptionIsThrown(string chunkName) { using (var memStream = new MemoryStream()) { - memStream.Skip(8); - + WriteHeaderChunk(memStream); WriteChunk(memStream, chunkName); - - CompressStream(memStream); + WriteDataChunk(memStream); var decoder = new PngDecoder(); decoder.Decode(null, memStream); } } + private static void WriteHeaderChunk(MemoryStream memStream) + { + // Writes a 1x1 32bit png header chunk containing a single black pixel + memStream.Write(raw1x1PngIHDRAndpHYs, 0, raw1x1PngIHDRAndpHYs.Length); + } + private static void WriteChunk(MemoryStream memStream, string chunkName) { memStream.Write(new byte[] { 0, 0, 0, 1 }, 0, 4); @@ -254,13 +289,11 @@ namespace SixLabors.ImageSharp.Tests memStream.Write(new byte[] { 0, 0, 0, 0, 0 }, 0, 5); } - private static void CompressStream(Stream stream) + private static void WriteDataChunk(MemoryStream memStream) { - stream.Position = 0; - using (var deflateStream = new DeflateStream(stream, CompressionLevel.NoCompression, true)) - { - } - stream.Position = 0; + // Writes a 1x1 32bit png data chunk containing a single black pixel + memStream.Write(raw1x1PngIDATAndIEND, 0, raw1x1PngIDATAndIEND.Length); + memStream.Position = 0; } } } \ No newline at end of file