Skip to content

Test utilities overwrite output images without truncating the file #448

Backlog

Description

@sebastienros

Prerequisites

  • I have written a descriptive issue title
  • I have verified that I am running the latest version of ImageSharp.Drawing
  • I have verified if the problem exist in both DEBUG and RELEASE mode
  • I have searched open and closed issues to ensure it has not already been reported

ImageSharp.Drawing version

main at commit a4e94b8

Other ImageSharp packages and versions

SixLabors.ImageSharp 5.0.0-alpha.0.38, SixLabors.Fonts 3.2.1, SixLabors.PolygonClipper 2.0.0-alpha.0.2

Environment (Operating system, version and so on)

macOS 15.8.3 (arm64)

.NET Framework version

.NET 10 (net10.0)

Description

ImagingTestCaseUtility.SaveTestOutputFile and SaveTestOutputFileMultiFrame open the output file with File.OpenWrite. That call does not truncate an existing file. When a test writes a smaller image than the file already holds (for example after a change that makes the PNG compress better), the file keeps the tail of the old content. The decoded image is still correct, because PNG decoders stop at IEND, but the bytes of the file are wrong, so byte comparisons of test output fail and the files grow.

Explanation

tests/ImageSharp.Drawing.Tests/TestUtilities/ImagingTestCaseUtility.cs:

  • line 175, in SaveTestOutputFile: using (FileStream stream = File.OpenWrite(path))
  • line 241, in SaveTestOutputFileMultiFrame: using FileStream stream = File.OpenWrite(filePath);

File.OpenWrite uses FileMode.OpenOrCreate: an existing file is opened at position 0 and its length is kept. The encoder writes the new image from the start, and the bytes after its end stay.

Suggested fix

Replace File.OpenWrite(...) with File.Create(...) (or new FileStream(path, FileMode.Create, FileAccess.Write)) in both methods. File.Create truncates an existing file. Other test helpers that write files should be checked for the same call; a search of the test project found only these two File.OpenWrite calls that write images.

Notes

  • Found in the SVG work: after test runs that wrote larger images, 18 PNG files differed in bytes from a clean run while their decoded pixels were equal; each kept 6 bytes of an older, larger file. Byte comparisons of the whole output folder were then only reliable after the folder was emptied before each run.
  • Impact: tests and tools only. Reference comparisons decode the images, so test results do not change.

Steps to Reproduce

A script for a checkout of main, for macOS or Linux. Build the test project first: dotnet build tests/ImageSharp.Drawing.Tests/ImageSharp.Drawing.Tests.csproj -c Release -f net10.0.

#!/bin/bash
# Run from the root of an ImageSharp.Drawing checkout (main). Usage: repro7.sh <Release|Debug>
set -e
CFG=${1:-Release}
FILTER="FullyQualifiedName~FillSolidBrush_DoesNotDependOnSinglePixelType"
OUT=tests/Images/ActualOutput/Drawing/ProcessWithDrawingCanvasTests/FillSolidBrush_DoesNotDependOnSinglePixelType_Rgba32.png
size() { stat -f %z "$1" 2>/dev/null || stat -c %s "$1"; }
hash() { shasum -a 256 "$1" | cut -c1-16; }

dotnet test tests/ImageSharp.Drawing.Tests/ImageSharp.Drawing.Tests.csproj -c $CFG -f net10.0 --no-build --filter "$FILTER" -v q | grep -E "Passed!|Failed!|passed|failed" | head -1
echo "first run:  $(size $OUT) bytes, sha256 $(hash $OUT)"

# Make the old file longer, as if an earlier run had written a larger image under the same name.
head -c 1000 /dev/zero >> $OUT
echo "extended:   $(size $OUT) bytes"

dotnet test tests/ImageSharp.Drawing.Tests/ImageSharp.Drawing.Tests.csproj -c $CFG -f net10.0 --no-build --filter "$FILTER" -v q | grep -E "Passed!|Failed!|passed|failed" | head -1
echo "second run: $(size $OUT) bytes, sha256 $(hash $OUT)"
echo "last 8 bytes: $(tail -c 8 $OUT | od -An -tx1)"

Run with ./repro7.sh Release from the root of the checkout. After a Debug build of the test project, ./repro7.sh Debug gives the same output.

Observed output:

Passed!  - Failed:     0, Passed:     3, Skipped:     0, Total:     3, Duration: 87 ms - SixLabors.ImageSharp.Tests.dll (net10.0)
first run:  115 bytes, sha256 2513a0e9cb404703
extended:   1115 bytes
Passed!  - Failed:     0, Passed:     3, Skipped:     0, Total:     3, Duration: 71 ms - SixLabors.ImageSharp.Tests.dll (net10.0)
second run: 1115 bytes, sha256 67c8d670715e4b20
last 8 bytes:  00 00 00 00 00 00 00 00

Expected: the second run writes the same 115 bytes as the first run, with the same hash. The file keeps its old length of 1115 bytes instead, and the bytes after the PNG IEND chunk are the 1000 zero bytes of the old content.

Images

No response

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions