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; } }