Files
Chronos/tests/Chronos.Core.Tests/Metadata/XmpTests.cs
T
Alby96andClaude Opus 5.5 5f27a86406
CI / Build and test (pull_request) Failing after 2m3s
CI / PostgreSQL 16 integration (pull_request) Skipped
CI / PostgreSQL 17 integration (pull_request) Skipped
CI / Docker image (pull_request) Skipped
CI / Windows tests and installer (pull_request) Skipped
CI / YAML check (pull_request) Successful in 4s
refactor!: rename Titano to Chronos and move CI and releases to Gitea
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. 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) and
  pe-check.

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>
2026-10-23 11:34:48 +02:00

197 lines
8.8 KiB
C#

using System.Text;
using Chronos.Core.Metadata.Tiff;
using Chronos.Core.Metadata.Xmp;
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 XmpTests
{
private const string Head = """<x:xmpmeta xmlns:x="adobe:ns:meta/"><rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">""";
private const string Tail = "</rdf:RDF></x:xmpmeta>";
private const string Namespaces = """
xmlns:xmp="http://ns.adobe.com/xap/1.0/" xmlns:exif="http://ns.adobe.com/exif/1.0/"
xmlns:photoshop="http://ns.adobe.com/photoshop/1.0/" xmlns:tiff="http://ns.adobe.com/tiff/1.0/"
xmlns:aux="http://ns.adobe.com/exif/1.0/aux/" xmlns:exifEX="http://cipa.jp/exif/1.0/"
xmlns:xmpNote="http://ns.adobe.com/xmp/note/"
""";
[Test]
public void ReadsAttributeForm()
{
var v = Parse($"""
{Head}<rdf:Description {Namespaces} xmp:Rating="5" xmp:Label="Green" exif:DateTimeOriginal="2024-03-04T05:06:07.5+01:00"
tiff:Orientation="8" aux:SerialNumber="AUX1" exifEX:BodySerialNumber="BODY1" aux:Lens="AuxLens" exifEX:LensModel="CipaLens"/>{Tail}
""");
Assert.Equal(5, v.Rating);
Assert.Equal("Green", v.Label);
Assert.Equal(new DateTime(2024, 3, 4, 5, 6, 7, 500), v.DateTimeOriginal!.Value.Value);
Assert.Equal(60, v.DateTimeOriginal!.Value.OffsetMinutes);
Assert.Equal(8, v.Orientation);
Assert.Equal("BODY1", v.BodySerial);
Assert.Equal("CipaLens", v.Lens);
}
[Test]
public void ReadsElementForm()
{
var v = Parse($"""
{Head}<rdf:Description {Namespaces}>
<xmp:Rating>-1</xmp:Rating>
<xmp:Label><![CDATA[Purple]]></xmp:Label>
<photoshop:DateCreated>2021-01-02T03:04Z</photoshop:DateCreated>
<xmp:CreateDate>2020-01-01T00:00:00</xmp:CreateDate>
<tiff:Orientation>2</tiff:Orientation>
<aux:SerialNumber>AUX2</aux:SerialNumber>
<aux:Lens><rdf:Alt><rdf:li xml:lang="x-default">Nested Lens</rdf:li></rdf:Alt></aux:Lens>
</rdf:Description>{Tail}
""");
Assert.Equal(-1, v.Rating);
Assert.Equal("Purple", v.Label);
Assert.Equal(new DateTime(2021, 1, 2, 3, 4, 0), v.CaptureTime!.Value.Value);
Assert.Equal(0, v.CaptureTime!.Value.OffsetMinutes);
Assert.Equal(2, v.Orientation);
Assert.Equal("AUX2", v.BodySerial);
Assert.Equal("Nested Lens", v.Lens);
}
[Theory]
[Case("exif:DateTimeOriginal=\"2001-01-01T01:01:01\" photoshop:DateCreated=\"2002-02-02T02:02:02\" xmp:CreateDate=\"2003-03-03T03:03:03\"", 2001)]
[Case("photoshop:DateCreated=\"2002-02-02T02:02:02\" xmp:CreateDate=\"2003-03-03T03:03:03\"", 2002)]
[Case("xmp:CreateDate=\"2003-03-03T03:03:03\"", 2003)]
[Case("photoshop:DateCreated=\"2002-02-02\" xmp:CreateDate=\"2003-03-03T03:03:03\"", 2003, Name = "date-only values are not capture times")]
public void CaptureTimeFollowsMwgOrder(string attributes, int year) =>
Assert.Equal(year, Parse($"{Head}<rdf:Description {Namespaces} {attributes}/>{Tail}").CaptureTime!.Value.Value.Year);
[Test]
public void FirstPacketWins()
{
var v = new XmpValues();
Assert.Null(XmpParser.Parse(Encoding.UTF8.GetBytes($"{Head}<rdf:Description {Namespaces} xmp:Rating=\"2\"/>{Tail}"), v));
Assert.Null(XmpParser.Parse(Encoding.UTF8.GetBytes($"{Head}<rdf:Description {Namespaces} xmp:Rating=\"5\" xmp:Label=\"Blue\"/>{Tail}"), v));
Assert.Equal(2, v.Rating);
Assert.Equal("Blue", v.Label);
}
[Test]
public void RefusesDtdsAndEntities()
{
var packet = $"""<?xml version="1.0"?><!DOCTYPE x [<!ENTITY e "boom">]>{Head}<rdf:Description {Namespaces} xmp:Label="&e;"/>{Tail}""";
var v = new XmpValues();
var problem = XmpParser.Parse(Encoding.UTF8.GetBytes(packet), v);
Assert.NotNull(problem);
Assert.Null(v.Label);
}
[Test]
public void MalformedPacketKeepsEarlierValues()
{
var v = new XmpValues();
var problem = XmpParser.Parse(Encoding.UTF8.GetBytes($"{Head}<rdf:Description {Namespaces} xmp:Rating=\"3\"><xmp:Label>Yel"), v);
Assert.Contains("Malformed XMP", problem);
Assert.Equal(3, v.Rating);
}
[Test]
public void RejectsPacketsOverFourMegabytes()
{
var packet = new byte[XmpParser.MaxPacketBytes + 1];
packet.AsSpan().Fill((byte)' ');
Assert.Equal("XMP packet larger than 4 MB ignored", XmpParser.Parse(packet, new XmpValues()));
}
[Test]
public void IgnoresTrailingNulPaddingAndInvalidValues()
{
var v = new XmpValues();
var bytes = Encoding.UTF8.GetBytes($"{Head}<rdf:Description {Namespaces} xmp:Rating=\"9\" tiff:Orientation=\"0\" xmp:CreateDate=\"yesterday\"/>{Tail}\0\0\0");
Assert.Null(XmpParser.Parse(bytes, v));
Assert.Null(v.Rating);
Assert.Null(v.Orientation);
Assert.Null(v.CreateDate);
}
[Test]
public void ReassemblesExtendedXmpInJpeg()
{
const string guid = "0123456789ABCDEF0123456789ABCDEF";
var main = $"{Head}<rdf:Description {Namespaces} xmpNote:HasExtendedXMP=\"{guid}\" xmp:Rating=\"1\"/>{Tail}";
var extended = $"{Head}<rdf:Description {Namespaces} xmp:Rating=\"5\" photoshop:DateCreated=\"2019-09-09T09:09:09\" exifEX:LensModel=\"Extended Lens {new string('x', 300)}\"/>{Tail}";
var jpeg = new JpegBuilder().Xmp(main).ExtendedXmp(guid, extended, 200).Build(64, 48);
var m = Read(jpeg);
AssertNoFault(m);
Assert.Equal(1, m.Rating);
Assert.Equal(TimestampSource.Xmp, m.TimestampSource);
Assert.Equal(new DateTime(2019, 9, 9, 9, 9, 9), m.CapturedAt);
Assert.Contains("Extended Lens", m.LensModel);
}
[Test]
public void IgnoresUnreferencedOrIncompleteExtendedXmp()
{
const string guid = "0123456789ABCDEF0123456789ABCDEF";
var extended = $"{Head}<rdf:Description {Namespaces} xmp:Label=\"Ext\"/>{Tail}";
var unreferenced = Read(new JpegBuilder().ExtendedXmp(guid, extended, 64).Build(64, 48));
Assert.Null(unreferenced.Label);
Assert.Contains("Extended XMP present but not referenced by the main packet", unreferenced.Warnings);
var main = $"{Head}<rdf:Description {Namespaces} xmpNote:HasExtendedXMP=\"{guid}\"/>{Tail}";
var builder = new JpegBuilder().Xmp(main).ExtendedXmp(guid, extended, 64);
var bytes = builder.Build(64, 48);
var withoutFirstChunk = Read(RemoveFirstExtendedChunk(bytes));
Assert.Null(withoutFirstChunk.Label);
Assert.Contains("Extended XMP incomplete", withoutFirstChunk.Warnings);
}
[Test]
public void SuppliesSerialAndLensFallbacks()
{
var xmp = $"{Head}<rdf:Description {Namespaces} aux:SerialNumber=\"XMPSN\" aux:Lens=\"XMP Lens\"/>{Tail}";
var exif = Fixtures.ExifIfd(identity: false);
var jpeg = new JpegBuilder().Exif(TiffFile.Build(Fixtures.Ifd0().Child(TiffTags.ExifIfd, exif))).Xmp(xmp).Build(64, 48);
var m = Read(jpeg);
Assert.Equal("XMPSN", m.BodySerial);
Assert.Equal("XMP Lens", m.LensModel);
}
[Test]
public void ExifIdentityBeatsXmp()
{
var xmp = $"{Head}<rdf:Description {Namespaces} aux:SerialNumber=\"XMPSN\" aux:Lens=\"XMP Lens\"/>{Tail}";
var m = Read(new JpegBuilder().Exif(Fixtures.ExifTiff()).Xmp(xmp).Build(64, 48));
Assert.Equal("SN123", m.BodySerial);
Assert.Equal("TestLens 35mm", m.LensModel);
}
[Test]
public void ReadsXmpTagInTiff()
{
var xmp = Encoding.UTF8.GetBytes($"{Head}<rdf:Description {Namespaces} xmp:Label=\"FromTiff\"/>{Tail}");
var tiff = TiffFile.Build(Fixtures.Ifd0().Undefined(TiffTags.Xmp, xmp).Long(TiffTags.ImageWidth, 10).Long(TiffTags.ImageLength, 10));
Assert.Equal("FromTiff", Read(tiff, "a.tif").Label);
}
private static XmpValues Parse(string packet)
{
var values = new XmpValues();
Assert.Null(XmpParser.Parse(Encoding.UTF8.GetBytes(packet), values));
return values;
}
/// <summary>Drops the extended-XMP segment holding offset 0 (written last by <see cref="JpegBuilder.ExtendedXmp"/>).</summary>
private static byte[] RemoveFirstExtendedChunk(byte[] jpeg)
{
var signature = Encoding.ASCII.GetBytes(JpegBuilder.ExtendedXmpSignature);
var last = -1;
for (var i = 0; i + signature.Length < jpeg.Length; i++)
{
if (jpeg.AsSpan(i).StartsWith(signature)) last = i - 4;
}
var length = jpeg[last + 2] << 8 | jpeg[last + 3];
return [.. jpeg.AsSpan(0, last), .. jpeg.AsSpan(last + 2 + length)];
}
}