From c30eb200e75fe6e0984f6cd5af1cd94a2af2f3aa Mon Sep 17 00:00:00 2001 From: Brian Popow Date: Mon, 10 Feb 2020 19:45:34 +0100 Subject: [PATCH] Add tests for discontiguous buffers for tga --- .../Formats/Tga/TgaDecoderTests.cs | 47 ++++++++++++++++++- .../Formats/Tga/TgaEncoderTests.cs | 10 ++++ 2 files changed, 56 insertions(+), 1 deletion(-) diff --git a/tests/ImageSharp.Tests/Formats/Tga/TgaDecoderTests.cs b/tests/ImageSharp.Tests/Formats/Tga/TgaDecoderTests.cs index 1f8cbd6a9..d87407ad8 100644 --- a/tests/ImageSharp.Tests/Formats/Tga/TgaDecoderTests.cs +++ b/tests/ImageSharp.Tests/Formats/Tga/TgaDecoderTests.cs @@ -1,9 +1,12 @@ // Copyright (c) Six Labors and contributors. // Licensed under the Apache License, Version 2.0. +using Microsoft.DotNet.RemoteExecutor; + using SixLabors.ImageSharp.Formats.Tga; +using SixLabors.ImageSharp.Memory; using SixLabors.ImageSharp.PixelFormats; - +using SixLabors.ImageSharp.Tests.TestUtilities; using Xunit; // ReSharper disable InconsistentNaming @@ -192,5 +195,47 @@ namespace SixLabors.ImageSharp.Tests.Formats.Tga TgaTestUtils.CompareWithReferenceDecoder(provider, image); } } + + // TODO: A InvalidMemoryOperationException is thrown here, review the thrown exception. + [Theory(Skip = "Review Exception")] + [WithFile(Bit16, PixelTypes.Rgba32)] + [WithFile(Bit24, PixelTypes.Rgba32)] + [WithFile(Bit32, PixelTypes.Rgba32)] + public void TgaDecoder_DegenerateMemoryRequest_ShouldTranslateTo_ImageFormatException(TestImageProvider provider) + where TPixel : struct, IPixel + { + provider.LimitAllocatorBufferCapacity(100); + ImageFormatException ex = Assert.Throws(provider.GetImage); + Assert.IsType(ex.InnerException); + } + + [Theory] + [WithFile(Bit24, PixelTypes.Rgba32)] + [WithFile(Bit32, PixelTypes.Rgba32)] + public void TgaDecoder_CanDecode_WithLimitedAllocatorBufferCapacity(TestImageProvider provider) + where TPixel : struct, IPixel + { + static void RunTest(string providerDump, string nonContiguousBuffersStr) + { + TestImageProvider provider = BasicSerializer.Deserialize>(providerDump); + + provider.LimitAllocatorBufferCapacity(); + + using Image image = provider.GetImage(new TgaDecoder()); + image.DebugSave(provider, testOutputDetails: nonContiguousBuffersStr); + + if (TestEnvironment.IsWindows) + { + image.CompareToOriginal(provider); + } + } + + string providerDump = BasicSerializer.Serialize(provider); + RemoteExecutor.Invoke( + RunTest, + providerDump, + "Disco") + .Dispose(); + } } } diff --git a/tests/ImageSharp.Tests/Formats/Tga/TgaEncoderTests.cs b/tests/ImageSharp.Tests/Formats/Tga/TgaEncoderTests.cs index 26fe7cbda..4144038e4 100644 --- a/tests/ImageSharp.Tests/Formats/Tga/TgaEncoderTests.cs +++ b/tests/ImageSharp.Tests/Formats/Tga/TgaEncoderTests.cs @@ -122,6 +122,16 @@ namespace SixLabors.ImageSharp.Tests.Formats.Tga public void Encode_Bit32_WithRunLengthEncoding_Works(TestImageProvider provider, TgaBitsPerPixel bitsPerPixel = TgaBitsPerPixel.Pixel32) where TPixel : struct, IPixel => TestTgaEncoderCore(provider, bitsPerPixel, TgaCompression.RunLength); + [Theory] + [WithFile(Bit32, PixelTypes.Rgba32, TgaBitsPerPixel.Pixel32)] + [WithFile(Bit24, PixelTypes.Rgba32, TgaBitsPerPixel.Pixel24)] + public void Encode_WorksWithDiscontiguousBuffers(TestImageProvider provider, TgaBitsPerPixel bitsPerPixel) + where TPixel : struct, IPixel + { + provider.LimitAllocatorBufferCapacity(10000); + TestTgaEncoderCore(provider, bitsPerPixel, TgaCompression.RunLength); + } + private static void TestTgaEncoderCore( TestImageProvider provider, TgaBitsPerPixel bitsPerPixel,