Files
Chronos/tests/Chronos.Server.Tests/Storage/FileTests.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

145 lines
8.5 KiB
C#

using System.Net;
using System.Net.Http.Headers;
using Microsoft.Extensions.DependencyInjection;
using Chronos.Core.Model;
using Chronos.Server.Storage;
using Chronos.Testing;
namespace Chronos.Server.Tests.Storage;
[Category("server")]
public sealed class FileTests(TestContext context)
{
[Test]
public async Task OriginalsAreStreamedWithRangesAndEtags()
{
await using var server = await TestServer.StartAsync(context);
var (editor, _, _) = await server.UserAsync("alberto", "editor");
var data = Uploads.Photo(10_000, 21);
var frame = await Uploads.UploadAsync(editor, data);
using (var full = await editor.GetAsync($"/api/v1/frames/{frame.FrameId}/original"))
{
await full.ExpectAsync(HttpStatusCode.OK);
Assert.Equal("bytes", full.Headers.AcceptRanges.Single());
Assert.Equal($"\"{Uploads.Sha(data)}\"", full.Headers.ETag!.Tag);
await Http.BytesEqualAsync(data, full.Content.ReadAsByteArrayAsync());
}
using var range = new HttpRequestMessage(HttpMethod.Get, $"/api/v1/frames/{frame.FrameId}/original");
range.Headers.Range = new RangeHeaderValue(100, 199);
using (var partial = await editor.SendAsync(range))
{
await partial.ExpectAsync(HttpStatusCode.PartialContent);
Assert.Equal((100L, 199L, 10_000L), (partial.Content.Headers.ContentRange!.From!.Value, partial.Content.Headers.ContentRange.To!.Value,
partial.Content.Headers.ContentRange.Length!.Value));
await Http.BytesEqualAsync(data[100..200], partial.Content.ReadAsByteArrayAsync());
}
using var tail = new HttpRequestMessage(HttpMethod.Get, $"/api/v1/frames/{frame.FrameId}/original");
tail.Headers.Range = new RangeHeaderValue(9_990, null);
using (var end = await editor.SendAsync(tail)) await Http.BytesEqualAsync(data[9_990..], end.Content.ReadAsByteArrayAsync());
using var cached = new HttpRequestMessage(HttpMethod.Get, $"/api/v1/frames/{frame.FrameId}/original");
cached.Headers.IfNoneMatch.Add(new EntityTagHeaderValue($"\"{Uploads.Sha(data)}\""));
using (var notModified = await editor.SendAsync(cached)) await notModified.ExpectAsync(HttpStatusCode.NotModified);
var (viewer, _, _) = await server.UserAsync("marco", "viewer");
using var refused = await viewer.GetAsync($"/api/v1/frames/{frame.FrameId}/original");
await refused.ExpectErrorAsync(HttpStatusCode.Forbidden, "forbidden");
}
[Test]
public async Task ThumbnailsAndProxiesComeFromTheLocatedFiles()
{
await using var server = await TestServer.StartAsync(context);
var (editor, _, _) = await server.UserAsync("alberto", "editor");
var first = await Uploads.UploadAsync(editor, Uploads.Photo(3000, 22), "a.jpg", sessionKey: "s");
var second = await Uploads.UploadAsync(editor, Uploads.Photo(3000, 23), "b.jpg", sessionKey: "s");
var sequence = (await server.Database.Sequences.GetAsync(first.SequenceId))!;
var folder = Path.Combine(server.Options.ArchivePath, sequence.Folder);
var pack = Uploads.Photo(FakeAssetLocator.ThumbnailSize * 2, 24);
Directory.CreateDirectory(Path.Combine(folder, ".chronos"));
await File.WriteAllBytesAsync(Path.Combine(folder, ".chronos", "thumbs.pack"), pack);
using (var thumb = await editor.GetAsync($"/api/v1/frames/{second.FrameId}/thumb"))
{
await thumb.ExpectAsync(HttpStatusCode.OK);
Assert.Equal("image/jpeg", thumb.Content.Headers.ContentType!.MediaType);
await Http.BytesEqualAsync(pack[FakeAssetLocator.ThumbnailSize..(2 * FakeAssetLocator.ThumbnailSize)], thumb.Content.ReadAsByteArrayAsync());
}
using var range = new HttpRequestMessage(HttpMethod.Get, $"/api/v1/frames/{second.FrameId}/thumb");
range.Headers.Range = new RangeHeaderValue(0, 9);
using (var partial = await editor.SendAsync(range))
{
await partial.ExpectAsync(HttpStatusCode.PartialContent);
await Http.BytesEqualAsync(pack[FakeAssetLocator.ThumbnailSize..(FakeAssetLocator.ThumbnailSize + 10)], partial.Content.ReadAsByteArrayAsync());
}
using (var missing = await editor.GetAsync($"/api/v1/frames/{first.FrameId}/proxy"))
Assert.Equal("asset_missing", (await missing.ExpectErrorAsync(HttpStatusCode.NotFound, "not_found")).Details);
var proxy = Uploads.Photo(500, 25);
Directory.CreateDirectory(Path.Combine(folder, "proxies"));
await File.WriteAllBytesAsync(Path.Combine(folder, "proxies", "0000.jpg"), proxy);
using var found = await editor.GetAsync($"/api/v1/frames/{first.FrameId}/proxy");
await Http.BytesEqualAsync(proxy, found.Content.ReadAsByteArrayAsync());
}
[Test]
public async Task PathsOutsideTheArchiveAreNeverServed()
{
await using var server = await TestServer.StartAsync(context);
var (editor, _, _) = await server.UserAsync("alberto", "editor");
var frame = await Uploads.UploadAsync(editor, Uploads.Photo(1000, 26));
var secret = Path.Combine(Path.GetDirectoryName(server.Options.ArchivePath)!, "secret.txt");
await File.WriteAllTextAsync(secret, "not for clients");
foreach (var hostile in new[] { "../secret.txt", "2026/../../secret.txt", "..\\secret.txt", secret, "/etc/passwd" })
{
await using (var session = await server.Database.Database.OpenAsync(context.Cancellation))
await session.ExecuteAsync("UPDATE frames SET relative_path = $1 WHERE id = $2", [hostile, frame.FrameId], context.Cancellation);
using var response = await editor.GetAsync($"/api/v1/frames/{frame.FrameId}/original");
await response.ExpectErrorAsync(HttpStatusCode.NotFound, "not_found");
}
var locator = server.Services.GetRequiredService<FakeAssetLocator>();
locator.Override = new AssetLocation(secret, 0, 5, "text/plain");
using (var proxy = await editor.GetAsync($"/api/v1/frames/{frame.FrameId}/proxy")) await proxy.ExpectErrorAsync(HttpStatusCode.NotFound, "not_found");
locator.Override = new AssetLocation(Path.Combine(server.Options.ArchivePath, "..", "secret.txt"), 0, 5, "text/plain");
using (var dotted = await editor.GetAsync($"/api/v1/frames/{frame.FrameId}/thumb")) await dotted.ExpectErrorAsync(HttpStatusCode.NotFound, "not_found");
if (!OperatingSystem.IsWindows())
{
var link = Path.Combine(server.Options.ArchivePath, "link.jpg");
File.CreateSymbolicLink(link, secret);
await using (var session = await server.Database.Database.OpenAsync(context.Cancellation))
await session.ExecuteAsync("UPDATE frames SET relative_path = $1 WHERE id = $2", ["link.jpg", frame.FrameId], context.Cancellation);
using var linked = await editor.GetAsync($"/api/v1/frames/{frame.FrameId}/original");
await linked.ExpectErrorAsync(HttpStatusCode.NotFound, "not_found");
}
}
[Test]
public async Task ExportsAreDownloadableWithRanges()
{
await using var server = await TestServer.StartAsync(context);
var (viewer, _, _) = await server.UserAsync("marco", "viewer");
var (editor, _, _) = await server.UserAsync("alberto", "editor");
var frame = await Uploads.UploadAsync(editor, Uploads.Photo(1000, 27));
var sequence = (await server.Database.Sequences.GetAsync(frame.SequenceId))!;
var video = Uploads.Photo(4096, 28);
var relative = $"{sequence.Folder}/exports/Harbour sunset - 4K.mp4";
Directory.CreateDirectory(Path.GetDirectoryName(Path.Combine(server.Options.ArchivePath, relative))!);
await File.WriteAllBytesAsync(Path.Combine(server.Options.ArchivePath, relative), video);
var render = new Render { Id = Guid.CreateVersion7(), SequenceId = sequence.Id, PresetName = "4K HEVC", OutputPath = relative, Bytes = video.Length, Created = DateTimeOffset.UtcNow };
await server.Database.Renders.InsertAsync(render);
using var request = new HttpRequestMessage(HttpMethod.Get, $"/api/v1/renders/{render.Id}/file");
request.Headers.Range = new RangeHeaderValue(1000, 1999);
using var response = await viewer.SendAsync(request);
await response.ExpectAsync(HttpStatusCode.PartialContent);
Assert.Equal("video/mp4", response.Content.Headers.ContentType!.MediaType);
await Http.BytesEqualAsync(video[1000..2000], response.Content.ReadAsByteArrayAsync());
}
}