Files
Chronos/tests/Chronos.Core.Tests/Metadata/ExifTests.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

279 lines
11 KiB
C#

using Chronos.Core.Imaging;
using Chronos.Core.Metadata;
using Chronos.Core.Metadata.Tiff;
using Chronos.Core.Model;
using Chronos.Core.Tests.Metadata.Builders;
using Chronos.Testing;
using static Chronos.Core.Tests.Metadata.MetadataTestHelpers;
namespace Chronos.Core.Tests.Metadata;
public sealed class ExifTests
{
[Theory]
[Case(true)]
[Case(false)]
public void ReadsEveryFieldFromJpeg(bool littleEndian)
{
var m = Read(Fixtures.Jpeg(littleEndian), "IMG_0001.JPG");
AssertNoFault(m);
Assert.Empty(m.Warnings);
Assert.Equal(ImageFileFormat.Jpeg, m.Format);
Assert.Equal(Fixtures.Make, m.Make);
Assert.Equal(Fixtures.Model, m.Model);
Assert.Equal("SN123", m.BodySerial);
Assert.Equal("TestLens 35mm", m.LensModel);
Assert.Equal("fw 1.0", m.Software);
Assert.Equal("Ada", m.Artist);
Assert.Equal("(c) Ada", m.Copyright);
Assert.Equal(Fixtures.Captured, m.CapturedAt);
Assert.Equal(DateTimeKind.Unspecified, m.CapturedAt!.Value.Kind);
Assert.Equal(120, m.UtcOffsetMinutes);
Assert.Equal(TimestampSource.ExifOriginal, m.TimestampSource);
Assert.Approximately(1 / 250.0, m.ExposureTime!.Value, 1e-12);
Assert.Approximately(5.6, m.FNumber!.Value, 1e-12);
Assert.Equal(400, m.Iso);
Assert.Approximately(35, m.FocalLength!.Value, 1e-12);
Assert.Approximately(52, m.FocalLength35!.Value, 1e-12);
Assert.Approximately(-2 / 3.0, m.ExposureBias!.Value, 1e-12);
Assert.Equal(3, m.ExposureProgram);
Assert.Equal(5, m.MeteringMode);
Assert.Equal(1, m.WhiteBalanceMode);
Assert.Equal(16, m.Flash);
Assert.Null(m.ColorTemperature);
Assert.Equal(6, m.Orientation);
Assert.Equal(4000, m.Width);
Assert.Equal(3000, m.Height);
Assert.Equal((3000, 4000), m.OrientedSize);
Assert.Equal(8, m.BitsPerSample);
var gps = Assert.NotNull(m.Gps);
Assert.Approximately(45.5, gps.Latitude, 1e-9);
Assert.Approximately(-9.26, gps.Longitude, 1e-9);
Assert.Approximately(120.5, gps.Altitude!.Value, 1e-9);
Assert.Equal(new DateTime(2024, 5, 1, 10, 34, 56, DateTimeKind.Utc), gps.UtcTimestamp);
Assert.Equal(4, m.Rating);
Assert.Equal("Red", m.Label);
AssertPreviews(m, (1920, 1440, PreviewKind.Preview), (160, 120, PreviewKind.Thumbnail));
}
[Test]
public void PreviewOffsetsPointAtJpegData()
{
var bytes = Fixtures.Jpeg();
var m = Read(bytes);
foreach (var p in m.Previews)
{
Assert.Equal(0xFF, bytes[p.Offset]);
Assert.Equal(0xD8, bytes[p.Offset + 1]);
Assert.Equal(0xD9, bytes[p.Offset + p.Length - 1]);
}
}
[Test]
public void FallsBackToDigitizedTime()
{
var m = ReadWithExif(Fixtures.ExifIfd().Without(TiffTags.DateTimeOriginal));
Assert.Equal(TimestampSource.ExifCreate, m.TimestampSource);
Assert.Equal(new DateTime(2024, 5, 1, 12, 34, 57), m.CapturedAt);
}
[Test]
public void FallsBackToXmpTime()
{
var xmp = """
<x:xmpmeta xmlns:x="adobe:ns:meta/"><rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
<rdf:Description xmlns:xmp="http://ns.adobe.com/xap/1.0/" xmp:CreateDate="2023-12-31T23:59:58.25-05:00"/>
</rdf:RDF></x:xmpmeta>
""";
var exif = Fixtures.ExifIfd().Without(TiffTags.DateTimeOriginal, TiffTags.DateTimeDigitized);
var jpeg = new JpegBuilder().Exif(TiffFile.Build(Fixtures.Ifd0().Child(TiffTags.ExifIfd, exif))).Xmp(xmp).Build(64, 48);
var m = Read(jpeg);
Assert.Equal(TimestampSource.Xmp, m.TimestampSource);
Assert.Equal(new DateTime(2023, 12, 31, 23, 59, 58, 250), m.CapturedAt);
Assert.Equal(-300, m.UtcOffsetMinutes);
}
[Test]
public void FallsBackToGpsTime()
{
var exif = Fixtures.ExifIfd().Without(TiffTags.DateTimeOriginal, TiffTags.DateTimeDigitized);
var m = ReadWithExif(exif);
Assert.Equal(TimestampSource.Gps, m.TimestampSource);
Assert.Equal(new DateTime(2024, 5, 1, 10, 34, 56), m.CapturedAt);
Assert.Equal(DateTimeKind.Unspecified, m.CapturedAt!.Value.Kind);
Assert.Equal(0, m.UtcOffsetMinutes);
}
[Theory]
[Case(DateTimeKind.Utc, 0)]
[Case(DateTimeKind.Unspecified, null)]
public void FallsBackToFileTime(DateTimeKind kind, int? expectedOffset)
{
var jpeg = new JpegBuilder().Exif(TiffFile.Build(Fixtures.Ifd0())).Build(64, 48);
var modified = new DateTime(2022, 2, 3, 4, 5, 6, kind);
var m = Read(jpeg, null, modified);
Assert.Equal(TimestampSource.FileSystem, m.TimestampSource);
Assert.Equal(new DateTime(2022, 2, 3, 4, 5, 6), m.CapturedAt);
Assert.Equal(expectedOffset, m.UtcOffsetMinutes);
}
[Test]
public void LocalFileTimeCarriesLocalOffset()
{
var modified = new DateTime(2022, 7, 3, 4, 5, 6, DateTimeKind.Local);
var m = Read(JpegBuilder.Minimal(8, 8), null, modified);
Assert.Equal((int)TimeZoneInfo.Local.GetUtcOffset(modified).TotalMinutes, m.UtcOffsetMinutes);
}
[Test]
public void NoTimeAtAllIsUnknown()
{
var m = Read(JpegBuilder.Minimal(8, 8));
Assert.Equal(TimestampSource.Unknown, m.TimestampSource);
Assert.Null(m.CapturedAt);
}
[Theory]
[Case("5", "+05:30", 5_000_000L, 330)]
[Case("123456789", "-03:00", 1_234_567L, -180)]
[Case("007", "+0100", 70_000L, 60)]
[Case("", "Z", 0L, 0)]
[Case(" ", " : ", 0L, null)]
[Case("12x", "+25:00", 1_200_000L, null, Name = "trailing junk, offset out of range")]
public void ParsesSubSecondsAndOffsets(string subSec, string offset, long ticks, int? minutes)
{
var exif = Fixtures.ExifIfd().Ascii(TiffTags.SubSecTimeOriginal, subSec).Ascii(TiffTags.OffsetTimeOriginal, offset);
var m = ReadWithExif(exif);
Assert.Equal(new DateTime(2024, 5, 1, 12, 34, 56).AddTicks(ticks), m.CapturedAt);
Assert.Equal(minutes, m.UtcOffsetMinutes);
}
[Test]
public void OffsetTimeIsTheFallbackOffset()
{
var exif = Fixtures.ExifIfd().Without(TiffTags.OffsetTimeOriginal).Ascii(TiffTags.OffsetTime, "-07:00");
Assert.Equal(-420, ReadWithExif(exif).UtcOffsetMinutes);
}
[Theory]
[Case("0000:00:00 00:00:00")]
[Case(" : : : : ")]
[Case("2024:13:01 10:00:00")]
[Case("2024:02:30 10:00:00")]
[Case("2024:05")]
public void RejectsInvalidDates(string value)
{
var exif = Fixtures.ExifIfd().Ascii(TiffTags.DateTimeOriginal, value);
Assert.Equal(TimestampSource.ExifCreate, ReadWithExif(exif).TimestampSource);
}
[Theory]
[Case((ushort)1, 1)]
[Case((ushort)3, 3)]
[Case((ushort)5, 5)]
[Case((ushort)8, 8)]
[Case((ushort)0, 1)]
[Case((ushort)9, 1)]
public void ValidatesOrientation(ushort value, int expected)
{
var m = Read(new JpegBuilder().Exif(TiffFile.Build(Fixtures.Ifd0(orientation: value))).Build(64, 48));
Assert.Equal(expected, m.Orientation);
if (value is 0 or 9) Assert.Contains($"Invalid orientation {value} ignored", m.Warnings);
}
[Test]
public void XmpOrientationAppliesWithoutExif()
{
var xmp = """<x:xmpmeta xmlns:x="adobe:ns:meta/"><rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"><rdf:Description xmlns:tiff="http://ns.adobe.com/tiff/1.0/"><tiff:Orientation>3</tiff:Orientation></rdf:Description></rdf:RDF></x:xmpmeta>""";
Assert.Equal(3, Read(new JpegBuilder().Xmp(xmp).Build(64, 48)).Orientation);
}
[Theory]
[Case((ushort)65535, 102400u, 102400)]
[Case((ushort)65535, 0u, 65535, Name = "saturated without REI")]
[Case((ushort)0, 51200u, 51200, Name = "ISO absent")]
[Case((ushort)3200, 6400u, 3200, Name = "ISO wins when valid")]
public void ResolvesIso(ushort iso, uint rei, int expected)
{
var exif = Fixtures.ExifIfd(iso: iso);
if (rei > 0) exif.Long(TiffTags.RecommendedExposureIndex, rei);
Assert.Equal(expected, ReadWithExif(exif).Iso);
}
[Test]
public void UsesStandardOutputSensitivityWhenIsoMissing()
{
var exif = Fixtures.ExifIfd(iso: 0).Long(TiffTags.StandardOutputSensitivity, 250);
Assert.Equal(250, ReadWithExif(exif).Iso);
}
[Test]
public void DerivesExposureFromApexValues()
{
var exif = Fixtures.ExifIfd().Without(TiffTags.FNumber, TiffTags.ExposureTime)
.SRational(TiffTags.ShutterSpeedValue, 8, 1)
.Rational(TiffTags.ApertureValue, (5, 1));
var m = ReadWithExif(exif);
Assert.Approximately(1 / 256.0, m.ExposureTime!.Value, 1e-12);
Assert.Approximately(Math.Pow(2, 2.5), m.FNumber!.Value, 1e-12);
}
[Test]
public void IgnoresVoidGpsFix()
{
var gps = Fixtures.GpsIfd().Ascii(TiffTags.GpsStatus, "V");
var tiff = TiffFile.Build(Fixtures.Ifd0().Child(TiffTags.GpsIfd, gps));
var m = Read(new JpegBuilder().Exif(tiff).Build(64, 48));
Assert.Null(m.Gps);
Assert.Equal(TimestampSource.Gps, m.TimestampSource);
}
[Test]
public void RejectsOutOfRangeCoordinates()
{
var gps = Fixtures.GpsIfd().Rational(TiffTags.GpsLatitude, (95, 1), (0, 1), (0, 1));
var m = Read(new JpegBuilder().Exif(TiffFile.Build(Fixtures.Ifd0().Child(TiffTags.GpsIfd, gps))).Build(64, 48));
Assert.Null(m.Gps);
}
[Test]
public void ToleratesFillBytesAndGarbageBetweenSegments()
{
var jpeg = new JpegBuilder()
.Raw(0xFF, 0xFF, 0xFF)
.Exif(Fixtures.ExifTiff())
.Raw(0x00, 0x13, 0x37, 0xFF)
.Segment(0xFE, "comment"u8.ToArray())
.Build(1234, 567);
var m = Read(jpeg);
Assert.Equal(1234, m.Width);
Assert.Equal(567, m.Height);
Assert.Equal(Fixtures.Make, m.Make);
Assert.Contains("JPEG: garbage between segments skipped", m.Warnings);
}
[Theory]
[Case((byte)0xC2, 8)]
[Case((byte)0xC1, 12)]
public void ReadsProgressiveAndExtendedFrames(byte sof, int precision)
{
var m = Read(JpegBuilder.Minimal(800, 600, sof, precision));
Assert.Equal((800, 600), (m.Width, m.Height));
Assert.Equal(precision, m.BitsPerSample);
}
[Test]
public void TruncatedExifKeepsWhatWasRead()
{
var full = Fixtures.Jpeg();
var cut = full.AsSpan(0, 140).ToArray();
var m = Read(cut);
AssertNoFault(m);
Assert.Equal(ImageFileFormat.Jpeg, m.Format);
Assert.NotEmpty(m.Warnings);
}
private static FrameMetadata ReadWithExif(IfdSpec exif) =>
Read(new JpegBuilder().Exif(TiffFile.Build(Fixtures.Ifd0().Child(TiffTags.ExifIfd, exif).Child(TiffTags.GpsIfd, Fixtures.GpsIfd()))).Build(64, 48));
}