Files
Chronos/tests/Titano.Core.Tests/Metadata/BmffTests.cs
T
Claude 484bfa9dd5 test(metadata): cover BMFF, RAF, PNG, WebP, ExifWriter, fuzzing
CR3 and HEIC/AVIF parsing (irot/imir orientation table, multi-extent
XMP), the box walker's edge cases, RAF/WebP/PNG/GIF/BMP/JPEG XL/JPEG XR,
ExifWriter round trips through MetadataReader, and robustness: 200
truncations plus 500 seeded corruptions per fixture with no exception
or parser fault, previews always inside the file, 50 MB DNG and CR3
read with well under 512 KB, 2,000 JPEG headers under 2 s,
cancellation and shared file access.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
2026-10-02 10:02:13 +00:00

128 lines
5.1 KiB
C#

using Titano.Core.Imaging;
using Titano.Core.Metadata;
using Titano.Core.Metadata.Bmff;
using Titano.Core.Model;
using Titano.Core.Tests.Metadata.Builders;
using Titano.Testing;
using static Titano.Core.Tests.Metadata.MetadataTestHelpers;
namespace Titano.Core.Tests.Metadata;
public sealed class BmffTests
{
[Test]
public void Cr3()
{
var m = Read(Fixtures.Cr3(), "IMG_0001.CR3");
AssertNoFault(m);
Assert.Empty(m.Warnings);
Assert.Equal(ImageFileFormat.Cr3, m.Format);
Assert.Equal("Canon", m.Make);
Assert.Equal("Canon EOS R5", m.Model);
Assert.Equal(8, m.Orientation);
Assert.Equal((8352, 5586), (m.Width, m.Height));
Assert.Equal(14, m.BitsPerSample);
AssertPreviews(m, (8192, 5464, PreviewKind.FullSize), (1620, 1080, PreviewKind.Preview), (160, 120, PreviewKind.Thumbnail));
Assert.Equal(new DateTime(2024, 6, 1, 8, 0, 0, 500), m.CapturedAt);
Assert.Equal(-240, m.UtcOffsetMinutes);
Assert.Equal(TimestampSource.ExifOriginal, m.TimestampSource);
Assert.Approximately(0.01, m.ExposureTime!.Value, 1e-12);
Assert.Approximately(4, m.FNumber!.Value, 1e-12);
Assert.Equal(100, m.Iso);
Assert.Equal("0000098765", m.BodySerial);
Assert.Equal("RF24-105mm F4 L IS USM", m.LensModel);
Assert.Approximately(-9.26, m.Gps!.Longitude, 1e-9);
}
[Test]
public void Cr3FullSizeJpegIsTrackOneSample()
{
var bytes = Fixtures.Cr3(out var mdatStart);
var full = Read(bytes).Previews[0];
Assert.Equal(mdatStart + 8L, full.Offset);
Assert.Equal(0xFF, bytes[full.Offset]);
Assert.Equal(0xD9, bytes[full.Offset + full.Length - 1]);
}
[Theory]
[Case(false, false, 6, Name = "no transforms: Exif orientation applies")]
[Case(true, false, 8, Name = "irot 90° anticlockwise")]
[Case(true, true, 7, Name = "irot then imir left-right")]
[Case(false, true, 2, Name = "imir left-right only")]
public void HeifTransformsOverrideExifOrientation(bool rotate, bool mirror, int expected) =>
Assert.Equal(expected, Read(Fixtures.Heic(rotate, mirror), "IMG_0001.HEIC").Orientation);
[Test]
public void Heic()
{
var m = Read(Fixtures.Heic(), "IMG_0001.HEIC");
AssertNoFault(m);
Assert.Empty(m.Warnings);
Assert.Equal(ImageFileFormat.Heif, m.Format);
Assert.Equal((4032, 3024), (m.Width, m.Height));
Assert.Equal(10, m.BitsPerSample);
Assert.Equal(Fixtures.Make, m.Make);
Assert.Equal(Fixtures.Captured, m.CapturedAt);
Assert.Equal(400, m.Iso);
Assert.Equal(4, m.Rating);
Assert.Equal("Red", m.Label);
AssertPreviews(m, (160, 120, PreviewKind.Thumbnail));
}
[Test]
public void Avif()
{
var m = Read(Fixtures.Heic(major: "avif"), "a.avif");
Assert.Equal(ImageFileFormat.Avif, m.Format);
Assert.Equal((4032, 3024), (m.Width, m.Height));
Assert.Equal("Red", m.Label);
}
[Test]
public void WalksLargeSizeToEndAndUuidBoxes()
{
var guid = Guid.NewGuid();
byte[] large = [.. Box.U32(1), .. Box.Ascii("free"), .. Box.U64(20), 1, 2, 3, 4];
byte[] data = [.. Box.Make("ftyp", Box.Ascii("heic")), .. large, .. Box.Uuid(guid, [9, 9]), .. Box.U32(0), .. Box.Ascii("mdat"), 7, 7, 7];
var boxes = BmffReader.ReadChildren(new MemoryStream(data), 0, data.Length);
Assert.Equal(4, boxes.Count);
Assert.True(boxes[1].Is("free"));
Assert.Equal(16, boxes[1].HeaderSize);
Assert.Equal(20L, boxes[1].Size);
Assert.Equal(guid, boxes[2].UserType);
Assert.Equal(24, boxes[2].HeaderSize);
Assert.True(boxes[3].Is("mdat"));
Assert.Equal((long)data.Length, boxes[3].End);
}
[Test]
public void StopsAtMalformedAndClampsTruncatedBoxes()
{
var warnings = new List<string>();
byte[] truncated = [.. Box.Make("ftyp", Box.Ascii("heic")), .. Box.U32(1000), .. Box.Ascii("moov"), 0, 0];
var boxes = BmffReader.ReadChildren(new MemoryStream(truncated), 0, truncated.Length, warnings);
Assert.Equal(2, boxes.Count);
Assert.Equal((long)truncated.Length, boxes[1].End);
Assert.Contains("Box 'moov' at 12 runs past its container", warnings);
byte[] malformed = [.. Box.Make("ftyp", Box.Ascii("heic")), .. Box.U32(4), .. Box.Ascii("bad!"), .. Box.Make("free")];
Assert.Equal(1, BmffReader.ReadChildren(new MemoryStream(malformed), 0, malformed.Length).Count);
}
[Test]
public void FindPathIsDepthLimited()
{
var box = new BmffBox(FourCC.From("moov"), 0, 8, 8, Guid.Empty);
Assert.Throws<ArgumentException>(() => BmffReader.FindPath(new MemoryStream(new byte[8]), box, [.. Enumerable.Repeat("trak", BmffReader.MaxDepth + 1)]));
Assert.Null(BmffReader.FindPath(new MemoryStream(new byte[8]), box, "trak", "mdia"));
}
[Test]
public void FourCCRoundTrips()
{
Assert.Equal(0x66747970u, FourCC.From("ftyp"));
Assert.Equal("ftyp", FourCC.Format(0x66747970));
Assert.Equal("?abc", FourCC.Format(0x01616263));
}
}