Files
Chronos/tests/Chronos.Core.Tests/Codecs/JpegRobustnessTests.cs
T
Alby96andClaude Opus 5.5 f2b58522b9
CI / Build and test (push) Successful in 8m14s
CI / Windows tests and installer (push) Skipped
CI / YAML check (push) Successful in 7s
Release / Build, test and publish (push) Successful in 13m19s
CI / PostgreSQL 16 integration (push) Successful in 4m49s
CI / PostgreSQL 17 integration (push) Successful in 5m28s
CI / Docker image (push) Successful in 53s
Release / Server image (push) Failing after 10s
refactor!: rename Titano to Chronos and move CI and releases to Gitea (#6)
The owner renamed the product and moved the repository to the self-hosted
Gitea (https://gitea.encke-hake.ts.net/BalboSoft/Chronos) for more control,
and asked for Actions and a first automatic release with the server package
and the Windows exe (docs/requests/2026-10-23-chronos-gitea.md).

- Chronos everywhere: a byte-exact rename of contents and paths, then a
  per-language review of the forms the old name's ending had dictated.
- Gitea instead of GitHub: the update check, licence, issue and support
  links read ChronosReleasesUrl; the update check takes the highest stable
  version, offers a pre-release build its final release and reports a
  repository it cannot see as a failed check. EULA, security policy and
  Gitea issue forms live at the repository root.
- Gitea Actions for the Linux act_runner (.gitea/workflows): CI, release,
  nightly image and performance gates in the .NET SDK container with git
  checkout and the system SQLite. The release cross-compiles the Windows
  programs, checks their icon, version and DPI manifest, packs installers
  and zips, publishes the Gitea release and pushes the server image
  (REGISTRY_TOKEN). Windows-only tests run in an opt-in job on a Windows
  runner (WINDOWS_RUNNER).
- New Chronos.Tools commands: zip, gitea-release, postgres-ci (replaces the
  docker-exec script, which Gitea's service containers do not allow; the
  throw-away server runs without durability) and pe-check.
- Timed tests take their budget from TestTiming (CHRONOS_TEST_TIME_FACTOR,
  3 on the shared runner; the weekly performance gates keep theirs).

BREAKING CHANGE: programs, settings, data folders, file types, environment
variables and the Docker image use the new name; nothing is migrated from
Titano, which never had a release.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: Alberto Balbo <alberto.balbo.96@gmail.com>
2026-10-23 12:45:18 +02:00

158 lines
6.1 KiB
C#

using Chronos.Core.Codecs.Jpeg;
using Chronos.Testing;
using static Chronos.Core.Tests.Codecs.CodecTestData;
namespace Chronos.Core.Tests.Codecs;
/// <summary>Corrupt, truncated and hostile JPEG input: only <see cref="InvalidDataException"/> may escape.</summary>
public sealed class JpegRobustnessTests(TestContext context)
{
private static byte[] LosslessSample()
{
var samples = new ushort[24 * 10 * 2];
for (var i = 0; i < samples.Length; i++) samples[i] = (ushort)(i * 37 % 4096);
return JpegLosslessEncoder.Encode(samples, 24, 10, 2, 12, predictor: 6, restartInterval: 24);
}
[Theory]
[Case("jpeg/b420.jpg")]
[Case("jpeg/b411.jpg")]
[Case("jpeg/p420.jpg")]
[Case("jpeg/p444.jpg")]
[Case("jpeg/r420.jpg")]
[Case("jpeg/pgray.jpg")]
[Case("jpeg/cmyk.jpg")]
[Case("lossless")]
public void FuzzedInputOnlyThrowsInvalidData(string source)
{
var valid = source == "lossless" ? LosslessSample() : Fixture(source);
var options = new[] { JpegDecodeOptions.Default, new JpegDecodeOptions { MaxLongEdge = 16, FancyUpsampling = false } };
int decoded = 0, rejected = 0;
var i = 0;
foreach (var input in Corruptions(valid, 120, seed: source.Sum(c => c)))
{
try
{
JpegDecoder.ReadInfo(input);
if (source == "lossless")
{
var raw = JpegLosslessDecoder.Decode(input);
Assert.Equal(raw.Width * raw.Height * raw.Components, raw.Samples.Length);
}
else
{
var image = JpegDecoder.Decode(input, options[i++ % 2]);
Assert.True(image.Width > 0 && image.Height > 0);
}
decoded++;
}
catch (InvalidDataException)
{
rejected++;
}
}
context.Log($"{source}: {decoded} decoded (possibly partial), {rejected} rejected");
}
[Test]
public void CorruptRestartIntervalIsResynchronised()
{
var data = Fixture("jpeg/r420.jpg");
var reference = ReadPnm(Fixture("jpeg/r420.ppm")).Samples;
var rst0 = data.AsSpan().IndexOf([(byte)0xFF, (byte)0xD0]);
var rst1 = data.AsSpan().IndexOf([(byte)0xFF, (byte)0xD1]);
Assert.True(rst0 > 0 && rst1 > rst0, "fixture has restart markers");
var corrupt = (byte[])data.Clone();
var rng = new Random(5);
for (var p = rst0 + 6; p < rst1 - 4; p += 3)
{
if (corrupt[p - 1] != 0xFF) corrupt[p] = (byte)rng.Next(0xFF);
}
// A bogus marker inside the damaged interval must be skipped, as libjpeg's resync does.
corrupt[rst0 + 10] = 0xFF;
corrupt[rst0 + 11] = 0x3A;
var image = JpegDecoder.Decode(corrupt);
context.Log(string.Join("; ", image.Warnings));
Assert.NotEmpty(image.Warnings);
AssertRowsMatch(image.Pixels8!, reference, 0, 14);
AssertRowsMatch(image.Pixels8!, reference, 34, 45);
}
[Test]
public void MissingRestartMarkerSkipsTheLostInterval()
{
var data = Fixture("jpeg/r420.jpg");
var reference = ReadPnm(Fixture("jpeg/r420.ppm")).Samples;
var rst0 = data.AsSpan().IndexOf([(byte)0xFF, (byte)0xD0]);
var cut = data.AsSpan(0, rst0).ToArray().Concat(data.AsSpan(rst0 + 2).ToArray()).ToArray();
var image = JpegDecoder.Decode(cut);
Assert.NotEmpty(image.Warnings);
AssertRowsMatch(image.Pixels8!, reference, 0, 14);
AssertRowsMatch(image.Pixels8!, reference, 34, 45);
}
[Theory]
[Case("jpeg/p420.jpg")]
[Case("jpeg/b444.jpg")]
public void TruncatedScanDataYieldsPartialImage(string name)
{
var data = Fixture(name);
var image = JpegDecoder.Decode(data.AsSpan(0, data.Length * 2 / 3));
Assert.Equal(61, image.Width);
Assert.Equal(45, image.Height);
Assert.NotEmpty(image.Warnings);
}
[Test]
public void StructuralErrorsAreRejected()
{
Assert.Throws<InvalidDataException>(() => JpegDecoder.Decode([]));
Assert.Throws<InvalidDataException>(() => JpegDecoder.Decode([0xFF, 0xD8, 0xFF, 0xD9]));
Assert.Throws<InvalidDataException>(() => JpegDecoder.ReadInfo([0x89, 0x50, 0x4E, 0x47]));
var arithmetic = Fixture("jpeg/b420.jpg");
var sof = arithmetic.AsSpan().IndexOf([(byte)0xFF, (byte)0xC0]);
arithmetic[sof + 1] = 0xC9;
Assert.Throws<InvalidDataException>(() => JpegDecoder.Decode(arithmetic));
var huge = Fixture("jpeg/b420.jpg");
huge[sof + 5] = huge[sof + 6] = huge[sof + 7] = huge[sof + 8] = 0xFF;
Assert.Throws<InvalidDataException>(() => JpegDecoder.Decode(huge));
}
[Test]
public void TablesStreamFeedsAbbreviatedImage()
{
// TIFF JPEGTables layout: tables in one SOI..EOI stream, the image without DQT/DHT.
var full = Fixture("jpeg/b420.jpg");
var tables = new MemoryStream();
var image = new MemoryStream();
tables.Write([0xFF, 0xD8]);
image.Write([0xFF, 0xD8]);
var pos = 2;
while (pos < full.Length)
{
var marker = full[pos + 1];
if (marker == 0xDA)
{
image.Write(full.AsSpan(pos));
break;
}
var length = full[pos + 2] << 8 | full[pos + 3];
(marker is 0xDB or 0xC4 ? tables : image).Write(full.AsSpan(pos, length + 2));
pos += length + 2;
}
tables.Write([0xFF, 0xD9]);
Assert.Throws<InvalidDataException>(() => JpegDecoder.Decode(image.ToArray()));
var decoded = JpegDecoder.Decode(image.ToArray(), new JpegDecodeOptions { Tables = tables.ToArray() });
Assert.BytesEqual(JpegDecoder.Decode(full).Pixels8, decoded.Pixels8);
}
private static void AssertRowsMatch(byte[] actual, int[] reference, int fromRow, int toRow)
{
var rowBytes = 61 * 3;
for (var i = fromRow * rowBytes; i < toRow * rowBytes; i++)
{
if (Math.Abs(actual[i] - reference[i]) > 2) Assert.Fail($"row {i / rowBytes} differs from the reference");
}
}
}