using Titano.Core.Codecs.Raw; using Titano.Core.Imaging; using Titano.Testing; using static Titano.Core.Tests.Raw.NativeRawWriters; namespace Titano.Core.Tests.Raw; /// Truncated and corrupted RAW files may only raise (or decode). public sealed class RawFuzzTests(TestContext context) { private const int W = 48, H = 32; private static byte[] Sample(string format) { using var scene = SyntheticRaw.Smooth(W, H); return format switch { "dng" => SyntheticDng.Write(scene, new DngSpec { TileSize = 16, OpcodeList2 = GainMapList(), OpcodeList3 = new OpcodeListBuilder().Add(3, 0, p => p.F64(0.1).F64(0).F64(0).F64(0).F64(0).F64(0.5).F64(0.5)).Build() }), "linear.dng" => SyntheticDng.Write(scene, new DngSpec { Cfa = null, Compression = 8 }), "cr2" => Cr2(Sensor(scene, "Canon EOS 5D Mark III", CfaPattern.Bayer("RGGB"), 80, 36, 24, 4, 2048, 16383), new RawRect(24, 4, 24 + W, 4 + H)), "nef" => Nef(Sensor(scene, "Nikon D850", CfaPattern.Bayer("RGGB"), W, H, 0, 0, 600, 16383), 14, NefEncoding.Lossless), "arw" => Arw(Sensor(scene, "Sony ILCE-7M3", CfaPattern.Bayer("RGGB"), 64, H, 0, 0, 512, 16000), [8000, 10400, 12900, 14100], compressed: true), "pef" => Pef(Sensor(scene, "Pentax K-1", CfaPattern.Bayer("BGGR"), W, H, 0, 0, 512, 16383), 14, compressed: true), _ => Raf(Sensor(scene, "Fujifilm X-T2", DemosaicTests.XTrans, W + 6, H + 6, 3, 3, 1024, 16383), DemosaicTests.XTrans, new RawRect(3, 3, 3 + W, 3 + H)), }; } private static byte[] GainMapList() => new OpcodeListBuilder().Add(9, 0, p => { OpcodeListBuilder.Area(p, 0, 0, H, W); p.U32(2).U32(2).F64(1).F64(1).F64(0).F64(0).U32(1).F32(1).F32(1.1f).F32(1.2f).F32(1.3f); }).Build(); [Theory] [Case("dng")] [Case("linear.dng")] [Case("cr2")] [Case("nef")] [Case("arw")] [Case("pef")] [Case("raf")] public void TruncatedAndCorruptedFilesOnlyRaiseInvalidData(string format) { var file = Sample(format); var name = "fuzz." + format.Split('.').Last(); var options = new RawDecodeOptions { Quality = DemosaicQuality.Draft, MaxDegreeOfParallelism = 1 }; using (var clean = RawDecoder.Decode(new MemoryStream(file), name, options)) Assert.Equal(format == "arw" ? 64 : W, clean.Image.Width); var random = new Random(format.Length * 7919); var outcomes = new Dictionary(); for (var i = 0; i < 120; i++) { byte[] variant; if (i < 40) { variant = file[..(int)((long)file.Length * i / 40)]; } else { variant = (byte[])file.Clone(); var flips = 1 + random.Next(4); for (var k = 0; k < flips; k++) { // Bias corruption toward the headers, where structure lives. var at = random.Next(2) == 0 ? random.Next(int.Min(variant.Length, 512)) : random.Next(variant.Length); variant[at] = (byte)random.Next(256); } } var outcome = Run(variant, name, options); outcomes[outcome] = outcomes.GetValueOrDefault(outcome) + 1; } context.Log($"{format}: " + string.Join(", ", outcomes.Select(o => $"{o.Key} {o.Value}"))); } private static string Run(byte[] data, string name, RawDecodeOptions options) { try { RawDecoder.Probe(new MemoryStream(data), name); using var result = RawDecoder.Decode(new MemoryStream(data), name, options); return "decoded"; } catch (InvalidDataException) { return "invalid"; } catch (Exception ex) { Assert.Fail($"{ex.GetType().Name}: {ex.Message}\n{ex.StackTrace}"); return ""; } } }