diff --git a/tests/Titano.Core.Tests/Metadata/BmffTests.cs b/tests/Titano.Core.Tests/Metadata/BmffTests.cs new file mode 100644 index 00000000..c122240f --- /dev/null +++ b/tests/Titano.Core.Tests/Metadata/BmffTests.cs @@ -0,0 +1,127 @@ +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(); + 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(() => 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)); + } +} diff --git a/tests/Titano.Core.Tests/Metadata/ExifWriterTests.cs b/tests/Titano.Core.Tests/Metadata/ExifWriterTests.cs new file mode 100644 index 00000000..3e962e5c --- /dev/null +++ b/tests/Titano.Core.Tests/Metadata/ExifWriterTests.cs @@ -0,0 +1,147 @@ +using System.Buffers.Binary; +using Titano.Core.Metadata; +using Titano.Core.Metadata.Tiff; +using Titano.Core.Model; +using Titano.Testing; +using static Titano.Core.Tests.Metadata.MetadataTestHelpers; + +namespace Titano.Core.Tests.Metadata; + +public sealed class ExifWriterTests +{ + private static readonly FrameMetadata Sample = new() + { + Make = "TestCam", + Model = "TC-1 Mark II", + Orientation = 8, + Software = "Titano 1.0", + Artist = "Ada Lovelace", + Copyright = "© 2024 Ada", + CapturedAt = new DateTime(2024, 5, 1, 12, 34, 56).AddTicks(1_234_500), + UtcOffsetMinutes = -330, + ExposureTime = 1 / 8000.0, + FNumber = 2.8, + Iso = 3200, + FocalLength = 23.5, + FocalLength35 = 35, + ExposureBias = -1 / 3.0, + BodySerial = "BODY-42", + LensModel = "XF23mmF1.4 R", + Gps = new GpsPosition(-33.8567844, 151.2152967, -12.25, new DateTime(2024, 5, 1, 2, 4, 56, 500, DateTimeKind.Utc)), + }; + + [Test] + public void RoundTripsThroughMetadataReader() + { + var m = Read(Wrap(ExifWriter.BuildJpegApp1(Sample))); + AssertNoFault(m); + Assert.Equal(Sample.Make, m.Make); + Assert.Equal(Sample.Model, m.Model); + Assert.Equal(8, m.Orientation); + Assert.Equal(Sample.Software, m.Software); + Assert.Equal(Sample.Artist, m.Artist); + Assert.Equal(Sample.Copyright, m.Copyright); + Assert.Equal(Sample.CapturedAt, m.CapturedAt); + Assert.Equal(-330, m.UtcOffsetMinutes); + Assert.Equal(TimestampSource.ExifOriginal, m.TimestampSource); + Assert.Approximately(Sample.ExposureTime!.Value, m.ExposureTime!.Value, 1e-15); + Assert.Approximately(2.8, m.FNumber!.Value, 1e-12); + Assert.Equal(3200, m.Iso); + Assert.Approximately(23.5, m.FocalLength!.Value, 1e-12); + Assert.Approximately(35, m.FocalLength35!.Value, 1e-12); + Assert.Approximately(-1 / 3.0, m.ExposureBias!.Value, 1e-4); + Assert.Equal(Sample.BodySerial, m.BodySerial); + Assert.Equal(Sample.LensModel, m.LensModel); + var gps = Assert.NotNull(m.Gps); + Assert.Approximately(-33.8567844, gps.Latitude, 1e-7); + Assert.Approximately(151.2152967, gps.Longitude, 1e-7); + Assert.Approximately(-12.25, gps.Altitude!.Value, 1e-9); + Assert.Equal(Sample.Gps!.UtcTimestamp, gps.UtcTimestamp); + } + + [Test] + public void App1SegmentIsWellFormed() + { + var app1 = ExifWriter.BuildJpegApp1(Sample); + Assert.Equal(0xFF, app1[0]); + Assert.Equal(0xE1, app1[1]); + Assert.Equal(app1.Length - 2, BinaryPrimitives.ReadUInt16BigEndian(app1.AsSpan(2))); + Assert.True(app1.AsSpan(4).StartsWith("Exif\0\0II*\0"u8)); + } + + [Test] + public void TiffIsSortedAndPointersResolve() + { + var tiff = ExifWriter.BuildTiff(Sample); + var reader = Assert.NotNull(TiffReader.Open(new MemoryStream(tiff))); + Assert.True(reader.LittleEndian); + var chain = reader.ReadChain(); + Assert.Equal(1, chain.Count); + var ifd0 = chain[0]; + AssertSorted(reader, ifd0); + var exif = Assert.NotNull(reader.ReadIfd(reader.GetUInt(ifd0, TiffTags.ExifIfd)!.Value)); + AssertSorted(reader, exif); + Assert.Equal("0232", reader.GetString(exif, TiffTags.ExifVersion)); + Assert.NotNull(reader.ReadIfd(reader.GetUInt(ifd0, TiffTags.GpsIfd)!.Value)); + Assert.Empty(reader.Warnings); + } + + [Test] + public void OmitsUnknownValuesAndGps() + { + var tiff = ExifWriter.BuildTiff(new FrameMetadata { Make = "Only" }); + var reader = TiffReader.Open(new MemoryStream(tiff))!; + var ifd0 = reader.ReadChain()[0]; + Assert.False(ifd0.Has(TiffTags.GpsIfd)); + Assert.False(ifd0.Has(TiffTags.DateTime)); + Assert.Equal((ushort)1, (ushort)reader.GetUInt(ifd0, TiffTags.Orientation)!.Value); + var exif = reader.ReadIfd(reader.GetUInt(ifd0, TiffTags.ExifIfd)!.Value)!; + Assert.False(exif.Has(TiffTags.ExposureTime)); + Assert.False(exif.Has(TiffTags.SubSecTimeOriginal)); + Assert.False(exif.Has(TiffTags.OffsetTimeOriginal)); + } + + [Test] + public void HighIsoUsesRecommendedExposureIndex() + { + var m = Read(Wrap(ExifWriter.BuildJpegApp1(Sample with { Iso = 204800 }))); + Assert.Equal(204800, m.Iso); + } + + [Theory] + [Case(1 / 8000.0, 1u, 8000u)] + [Case(0.3, 3u, 10u)] + [Case(2.8, 14u, 5u)] + [Case(30.0, 30u, 1u)] + public void EncodesExposureRationals(double seconds, uint numerator, uint denominator) + { + var tiff = ExifWriter.BuildTiff(new FrameMetadata { ExposureTime = seconds }); + var reader = TiffReader.Open(new MemoryStream(tiff))!; + var exif = reader.ReadIfd(reader.GetUInt(reader.ReadChain()[0], TiffTags.ExifIfd)!.Value)!; + var raw = reader.ReadBytes(exif.Entries[TiffTags.ExposureTime])!; + Assert.Equal(numerator, BinaryPrimitives.ReadUInt32LittleEndian(raw)); + Assert.Equal(denominator, BinaryPrimitives.ReadUInt32LittleEndian(raw.AsSpan(4))); + } + + [Test] + public void TruncatesLongStringsOnCharacterBoundaries() + { + var longText = new string('é', ExifWriter.MaxStringBytes); + var app1 = ExifWriter.BuildJpegApp1(new FrameMetadata { Artist = longText, Copyright = longText, Make = longText, Model = longText, Software = longText, BodySerial = longText, LensModel = longText }); + Assert.Less(app1.Length, 65535); + var m = Read(Wrap(app1)); + Assert.Equal(ExifWriter.MaxStringBytes / 2, m.Artist!.Length); + Assert.True(m.Artist.All(c => c == 'é')); + } + + /// SOI + APP1 + a minimal 64×48 frame. + private static byte[] Wrap(byte[] app1) => [0xFF, 0xD8, .. app1, .. Builders.JpegBuilder.Minimal(64, 48).AsSpan(2)]; + + private static void AssertSorted(TiffReader reader, Ifd ifd) + { + var count = BinaryPrimitives.ReadUInt16LittleEndian(reader.ReadRelative(ifd.Position, 2)!); + var tags = new List(); + for (var i = 0; i < count; i++) tags.Add(BinaryPrimitives.ReadUInt16LittleEndian(reader.ReadRelative(ifd.Position + 2 + 12 * i, 2)!)); + Assert.Monotonic(tags, strict: true); + } +} diff --git a/tests/Titano.Core.Tests/Metadata/OtherFormatTests.cs b/tests/Titano.Core.Tests/Metadata/OtherFormatTests.cs new file mode 100644 index 00000000..e7f3ec0f --- /dev/null +++ b/tests/Titano.Core.Tests/Metadata/OtherFormatTests.cs @@ -0,0 +1,202 @@ +using System.Buffers.Binary; +using Titano.Core.Imaging; +using Titano.Core.Metadata; +using Titano.Core.Metadata.Tiff; +using Titano.Core.Tests.Metadata.Builders; +using Titano.Testing; +using static Titano.Core.Tests.Metadata.MetadataTestHelpers; + +namespace Titano.Core.Tests.Metadata; + +/// RAF, WebP, PNG and the minimal formats (GIF, BMP, JPEG XL, JPEG XR). +public sealed class OtherFormatTests +{ + [Test] + public void Raf() + { + var m = Read(Fixtures.Raf(), "DSCF0001.RAF"); + AssertNoFault(m); + Assert.Empty(m.Warnings); + Assert.Equal(ImageFileFormat.Raf, m.Format); + Assert.Equal("FUJIFILM", m.Make); + Assert.Equal("X-T5", m.Model); + Assert.Equal((7752, 5178), (m.Width, m.Height)); + Assert.Equal(14, m.BitsPerSample); + AssertPreviews(m, (1920, 1280, PreviewKind.Preview), (160, 120, PreviewKind.Thumbnail)); + Assert.Equal("FF02B1234567", m.BodySerial); + Assert.Equal(6100, m.ColorTemperature); + Assert.Equal(true, m.ImageStabilization); + Assert.Equal(Fixtures.Captured, m.CapturedAt); + } + + [Test] + public void RafHeaderModelIsTheFallback() + { + var raf = Fixtures.Raf(); + BinaryPrimitives.WriteUInt32BigEndian(raf.AsSpan(0x54), 0); + var m = Read(raf); + Assert.Equal("FUJIFILM", m.Make); + Assert.Equal("X-T5", m.Model); + Assert.Equal((7752, 5178), (m.Width, m.Height)); + Assert.Contains("RAF embedded JPEG missing", m.Warnings); + } + + [Test] + public void WebPWithVp8xExifAndXmp() + { + var m = Read(Fixtures.WebP(), "a.webp"); + AssertNoFault(m); + Assert.Empty(m.Warnings); + Assert.Equal(ImageFileFormat.WebP, m.Format); + Assert.Equal((3000, 2000), (m.Width, m.Height)); + Assert.Equal(8, m.BitsPerSample); + Assert.Equal(Fixtures.Make, m.Make); + Assert.Equal(Fixtures.Captured, m.CapturedAt); + Assert.Equal(4, m.Rating); + Assert.Equal(6, m.Orientation); + } + + [Test] + public void WebPSimpleFormats() + { + Assert.Equal((640, 480), Size(Read(Fixtures.WebPLossy(640, 480)))); + var body = (byte[])[.. "WEBP"u8, .. Box.Riff("VP8L", Fixtures.Vp8L(400, 300))]; + Assert.Equal((400, 300), Size(Read([.. "RIFF"u8, .. LittleEndian(body.Length), .. body]))); + } + + [Test] + public void WebPExifWithPrefix() + { + var body = (byte[])[.. "WEBP"u8, .. Box.Riff("VP8L", Fixtures.Vp8L(8, 8)), .. Box.Riff("EXIF", [.. "Exif\0\0"u8, .. Fixtures.ExifTiff()])]; + Assert.Equal(Fixtures.Model, Read([.. "RIFF"u8, .. LittleEndian(body.Length), .. body]).Model); + } + + [Theory] + [Case(false)] + [Case(true)] + public void Png(bool compressedXmp) + { + var m = Read(Fixtures.Png(compressedXmp), "a.png"); + AssertNoFault(m); + Assert.Empty(m.Warnings); + Assert.Equal(ImageFileFormat.Png, m.Format); + Assert.Equal((2048, 1536), (m.Width, m.Height)); + Assert.Equal(16, m.BitsPerSample); + Assert.Equal(Fixtures.Model, m.Model); + Assert.Equal(400, m.Iso); + Assert.Equal("Red", m.Label); + } + + [Test] + public void Gif() => Assert.Equal((320, 200), Size(Read([.. "GIF89a"u8, 0x40, 0x01, 0xC8, 0x00, 0, 0, 0]))); + + [Theory] + [Case(40, 640, -480)] + [Case(124, 1024, 768)] + public void Bmp(int headerSize, int width, int height) + { + var bmp = new byte[54]; + "BM"u8.CopyTo(bmp); + BinaryPrimitives.WriteInt32LittleEndian(bmp.AsSpan(14), headerSize); + BinaryPrimitives.WriteInt32LittleEndian(bmp.AsSpan(18), width); + BinaryPrimitives.WriteInt32LittleEndian(bmp.AsSpan(22), height); + Assert.Equal((width, Math.Abs(height)), Size(Read(bmp))); + } + + [Test] + public void BmpCoreHeader() + { + var bmp = new byte[26]; + "BM"u8.CopyTo(bmp); + bmp[14] = 12; + BinaryPrimitives.WriteUInt16LittleEndian(bmp.AsSpan(18), 300); + BinaryPrimitives.WriteUInt16LittleEndian(bmp.AsSpan(20), 200); + Assert.Equal((300, 200), Size(Read(bmp))); + } + + [Theory] + [Case(256, 128)] + [Case(1000, 750, Name = "4:3 ratio")] + [Case(1921, 1081)] + [Case(300000, 270000, Name = "two 30-bit sizes (68-bit header)")] + public void JpegXlCodestream(int width, int height) + { + var m = Read(BuildJpegXlCodestream(width, height), "a.jxl"); + Assert.Equal(ImageFileFormat.JpegXl, m.Format); + Assert.Equal((width, height), Size(m)); + } + + [Test] + public void JpegXlContainer() + { + byte[] file = [.. Box.Make("JXL ", [0x0D, 0x0A, 0x87, 0x0A]), .. Box.Make("ftyp", Box.Ascii("jxl "), Box.U32(0), Box.Ascii("jxl ")), + .. Box.Make("Exif", Box.U32(0), Fixtures.ExifTiff()), .. Box.Make("jxlp", Box.U32(0), BuildJpegXlCodestream(512, 384))]; + var m = Read(file); + Assert.Equal(ImageFileFormat.JpegXl, m.Format); + Assert.Equal((512, 384), Size(m)); + Assert.Equal(Fixtures.Make, m.Make); + } + + [Test] + public void JpegXr() + { + var ifd = new IfdSpec().Ascii(TiffTags.Make, "Microsoft").Long(0xBC80, 1600).Long(0xBC81, 900); + var m = Read(TiffFile.Build(ifd, magic: "II¼\u0001"), "a.jxr"); + Assert.Equal(ImageFileFormat.JpegXr, m.Format); + Assert.Equal((1600, 900), Size(m)); + Assert.Equal("Microsoft", m.Make); + } + + [Test] + public void UnknownFilesYieldAWarning() + { + var m = Read("hello world, not an image"u8.ToArray(), "notes.jpg"); + Assert.Equal(ImageFileFormat.Unknown, m.Format); + Assert.Contains("Unrecognised file signature", m.Warnings); + } + + private static (int, int) Size(FrameMetadata m) => (m.Width, m.Height); + + private static byte[] LittleEndian(int v) + { + var b = new byte[4]; + BinaryPrimitives.WriteInt32LittleEndian(b, v); + return b; + } + + /// FF 0A + SizeHeader (ISO/IEC 18181-1): small multiples of 8 or U32 distributions; a 4:3 ratio when exact. + private static byte[] BuildJpegXlCodestream(int width, int height) + { + var bits = new List(); + void Put(ulong value, int count) + { + for (var i = 0; i < count; i++) bits.Add(((value >> i) & 1) != 0); + } + void PutU32(int value) + { + var v = (ulong)(value - 1); + var (selector, n) = v < 1 << 9 ? (0, 9) : v < 1 << 13 ? (1, 13) : v < 1 << 18 ? (2, 18) : (3, 30); + Put((ulong)selector, 2); + Put(v, n); + } + var small = width % 8 == 0 && height % 8 == 0 && width <= 256 && height <= 256; + Put(small ? 1UL : 0UL, 1); + if (small) Put((ulong)(height / 8 - 1), 5); + else PutU32(height); + var ratio = height * 4 / 3 == width && height % 3 == 0 ? 3 : 0; + Put((ulong)ratio, 3); + if (ratio == 0) + { + if (small) Put((ulong)(width / 8 - 1), 5); + else PutU32(width); + } + var bytes = new byte[2 + (bits.Count + 7) / 8 + 8]; + bytes[0] = 0xFF; + bytes[1] = 0x0A; + for (var i = 0; i < bits.Count; i++) + { + if (bits[i]) bytes[2 + i / 8] |= (byte)(1 << (i % 8)); + } + return bytes; + } +} diff --git a/tests/Titano.Core.Tests/Metadata/RobustnessTests.cs b/tests/Titano.Core.Tests/Metadata/RobustnessTests.cs new file mode 100644 index 00000000..bead02a9 --- /dev/null +++ b/tests/Titano.Core.Tests/Metadata/RobustnessTests.cs @@ -0,0 +1,189 @@ +using System.Buffers.Binary; +using System.Diagnostics; +using Titano.Core.Imaging; +using Titano.Core.Metadata; +using Titano.Core.Metadata.Tiff; +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; + +/// Fuzzing, I/O bounds, speed, cancellation and file access. +public sealed class RobustnessTests(TestContext context) +{ + private const int Truncations = 200; + private const int Corruptions = 500; + private const long FiftyMegabytes = 50L * 1024 * 1024; + private const long ReadBudget = 512 * 1024; + + [Theory] + [Case("jpeg")] + [Case("jpeg-be")] + [Case("cr2")] + [Case("nef")] + [Case("dng")] + [Case("arw")] + [Case("orf")] + [Case("rw2")] + [Case("cr3")] + [Case("heic")] + [Case("avif")] + [Case("raf")] + [Case("webp")] + [Case("png")] + [Case("png-z")] + [Timeout(5000)] + public void SurvivesTruncationAndCorruption(string fixture) + { + var original = Fixtures.ByName(fixture); + var random = new Random(fixture.Aggregate(0x5EED, (seed, c) => seed * 31 + c)); + var parses = 0; + for (var i = 0; i < Truncations; i++) + { + var length = (int)((long)original.Length * i / Truncations) + random.Next(0, 3); + Check(original.AsSpan(0, Math.Min(length, original.Length)).ToArray(), $"truncated at {length}"); + parses++; + } + var buffer = new byte[original.Length]; + for (var i = 0; i < Corruptions; i++) + { + original.CopyTo(buffer, 0); + var flips = random.Next(1, 9); + for (var f = 0; f < flips; f++) buffer[random.Next(buffer.Length)] = (byte)random.Next(256); + Check(buffer, $"corruption {i}"); + parses++; + } + Assert.Equal(Truncations + Corruptions, parses); + } + + [Test] + public void FuzzedFieldsStaySane() + { + var random = new Random(7); + var original = Fixtures.Jpeg(); + for (var i = 0; i < 300; i++) + { + var bytes = (byte[])original.Clone(); + for (var f = 0; f < 6; f++) bytes[random.Next(bytes.Length)] ^= (byte)(1 << random.Next(8)); + var m = Read(bytes, "fuzz.jpg"); + Assert.InRange(m.Orientation, 1, 8); + Assert.True(m.Width >= 0 && m.Height >= 0); + foreach (var p in m.Previews) Assert.True(p.Offset >= 0 && p.Offset + p.Length <= bytes.Length, "preview inside file"); + } + } + + [Test] + public void LargeDngReadsWellUnderHalfAMegabyte() + { + var preview = new IfdSpec().Long(TiffTags.NewSubfileType, 1).Long(TiffTags.ImageWidth, 4000).Long(TiffTags.ImageLength, 3000) + .Short(TiffTags.Compression, 7).Short(TiffTags.PhotometricInterpretation, 6) + .Strip(JpegBuilder.Minimal(4000, 3000, scanBytes: 1_500_000)); + var raw = new IfdSpec().Long(TiffTags.NewSubfileType, 0).Long(TiffTags.ImageWidth, 8000).Long(TiffTags.ImageLength, 6000) + .Short(TiffTags.BitsPerSample, 16).Short(TiffTags.Compression, 7).Short(TiffTags.PhotometricInterpretation, 32803) + .Long(TiffTags.StripOffsets, 2_000_000).Long(TiffTags.StripByteCounts, 48_000_000); + var ifd0 = Fixtures.Ifd0("Leica Camera AG", "M11", 1).Long(TiffTags.NewSubfileType, 1).Bytes(TiffTags.DngVersion, 1, 6, 0, 0) + .Child(TiffTags.SubIfds, raw, preview).Child(TiffTags.ExifIfd, Fixtures.ExifIfd()).Child(TiffTags.GpsIfd, Fixtures.GpsIfd()); + var bytes = TiffFile.Build(ifd0); + Assert.Less(bytes.Length, 2_000_000); + using var stream = new CountingStream(new SparseStream(bytes, FiftyMegabytes)); + var m = MetadataReader.Read(stream, "L1000001.DNG"); + AssertNoFault(m); + Assert.Equal(ImageFileFormat.Dng, m.Format); + Assert.Equal((8000, 6000), (m.Width, m.Height)); + AssertPreviews(m, (4000, 3000, PreviewKind.Preview)); + Assert.Equal(Fixtures.Captured, m.CapturedAt); + context.Log($"DNG: {stream.BytesRead} bytes in {stream.ReadCalls} reads"); + Assert.Less(stream.BytesRead, ReadBudget); + } + + [Test] + public void LargeCr3ReadsWellUnderHalfAMegabyte() + { + var bytes = Fixtures.Cr3(out var mdatStart); + BinaryPrimitives.WriteUInt32BigEndian(bytes.AsSpan(mdatStart), (uint)(FiftyMegabytes - mdatStart)); + using var stream = new CountingStream(new SparseStream(bytes, FiftyMegabytes)); + var m = MetadataReader.Read(stream, "IMG_0001.CR3"); + AssertNoFault(m); + Assert.Equal((8352, 5586), (m.Width, m.Height)); + Assert.Count(3, m.Previews); + context.Log($"CR3: {stream.BytesRead} bytes in {stream.ReadCalls} reads"); + Assert.Less(stream.BytesRead, ReadBudget); + } + + [Test] + public void ParsesTwoThousandJpegHeadersQuickly() + { + var jpeg = Fixtures.Jpeg(); + Read(jpeg); + var watch = Stopwatch.StartNew(); + for (var i = 0; i < 2000; i++) + { + var m = MetadataReader.Read(new MemoryStream(jpeg, writable: false), "IMG.JPG"); + if (m.Width != 4000) Assert.Fail("unexpected width"); + } + watch.Stop(); + context.Log($"2000 JPEG headers in {watch.ElapsedMilliseconds} ms"); + Assert.Less(watch.Elapsed.TotalSeconds, 2.0); + } + + [Test] + public void HonoursCancellation() + { + using var cts = new CancellationTokenSource(); + cts.Cancel(); + Assert.Throws(() => MetadataReader.Read(new MemoryStream(Fixtures.Nef()), "a.nef", null, cts.Token)); + } + + [Test] + public void RejectsNonSeekableStreams() + { + Assert.Throws(() => MetadataReader.Read(new NonSeekable(), null)); + Assert.Throws(() => ImageSignature.Detect(new NonSeekable(), null)); + } + + [Test] + public void ReadsFilesSharedWithAWriter() + { + var path = Path.Combine(context.CreateTempDirectory(), "IMG_0002.JPG"); + File.WriteAllBytes(path, JpegBuilder.Minimal(320, 240)); + var modified = new DateTime(2021, 6, 7, 8, 9, 10, DateTimeKind.Local); + File.SetLastWriteTime(path, modified); + using (new FileStream(path, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite)) + { + var m = MetadataReader.Read(path); + Assert.Equal((320, 240), (m.Width, m.Height)); + Assert.Equal(TimestampSource.FileSystem, m.TimestampSource); + Assert.Equal(new DateTime(2021, 6, 7, 8, 9, 10), m.CapturedAt); + } + } + + private static void Check(byte[] bytes, string what) + { + FrameMetadata m; + try + { + m = MetadataReader.Read(new MemoryStream(bytes, writable: false), "fuzz"); + } + catch (Exception ex) + { + Assert.Fail($"{what}: {ex.GetType().Name}: {ex.Message}"); + return; + } + foreach (var w in m.Warnings) + { + if (w.StartsWith("Parser fault", StringComparison.Ordinal)) Assert.Fail($"{what}: {w}"); + } + foreach (var p in m.Previews) + { + if (p.Offset < 0 || p.Length <= 0 || p.Offset + p.Length > bytes.Length) Assert.Fail($"{what}: preview {p} outside the file"); + } + if (m.Orientation is < 1 or > 8 || m.Width < 0 || m.Height < 0) Assert.Fail($"{what}: invalid geometry"); + } + + private sealed class NonSeekable : MemoryStream + { + public override bool CanSeek => false; + } +}