using System.IO;
using System.Net;
using System.Net.Http;
using System.Net.Http.Json;
using System.Security.Cryptography;
using Titano.App.Import;
using Titano.Core.Import;
using Titano.Protocol;
using Titano.Protocol.Client;
using Titano.Testing;
using static Titano.App.Tests.Import.ImportTestData;
namespace Titano.App.Tests.Import;
/// The "Server" destination over the real protocol client against an in-memory upload endpoint.
[Category("import")]
public sealed class ServerImportSinkTests(TestContext context)
{
[Test]
public async Task EveryPhotoIsUploadedInChunksUnderItsCardNameAndVerifiedByHash()
{
var card = Card(context.CreateTempDirectory("card"), 8, Start, Start.AddHours(2));
var staging = context.CreateTempDirectory("staging");
var server = new FakeUploadServer(chunkSize: 256);
using var client = new TitanoClient(new Uri("https://nas.local:8443/"), new CertificatePinning([]), "tests", server) { Token = "t" };
var plan = await new UnindexedDestination(ImportDestinationKind.Server, staging).PlanAsync(card, new ImportChoices().ToOptions(), null, null, context.Cancellation);
plan = plan with { Sessions = [plan.Sessions[0] with { Title = "Harbour sunset" }, .. plan.Sessions.Skip(1)] };
var outcome = await LocalImport.RunAsync(plan, new ServerImportSink(client, staging), verify: true, null, context.Cancellation);
Assert.Equal(16, outcome.Copied);
Assert.True(outcome.IsComplete);
Assert.Equal(16, server.Committed.Count);
foreach (var item in plan.Items)
{
var bytes = File.ReadAllBytes(Path.Combine(card, item.SourcePath));
var upload = server.Committed.Single(u => u.FileName == Path.GetFileName(item.SourcePath));
Assert.BytesEqual(bytes, upload.Bytes);
Assert.Equal(Convert.ToHexStringLower(SHA256.HashData(bytes)), upload.Sha256);
Assert.True(upload.Chunks > 1, "files larger than a chunk travel in several chunks");
}
Assert.SequenceEqual(plan.Sessions.Select(s => s.SessionKey).Order(StringComparer.Ordinal),
server.Committed.Select(u => u.SessionKey).Distinct().Order(StringComparer.Ordinal));
Assert.Equal("Harbour sunset", server.Titles.Single().Value);
Assert.Empty(Directory.EnumerateFileSystemEntries(staging));
}
[Test]
public async Task PhotosTheServerAlreadyHoldsAreNotSentAgain()
{
var card = Card(context.CreateTempDirectory("card"), 8, Start);
var staging = context.CreateTempDirectory("staging");
var server = new FakeUploadServer(chunkSize: 4096);
using var client = new TitanoClient(new Uri("https://nas.local:8443/"), new CertificatePinning([]), "tests", server) { Token = "t" };
var destination = new UnindexedDestination(ImportDestinationKind.Server, staging);
var plan = await destination.PlanAsync(card, new ImportChoices().ToOptions(), null, null, context.Cancellation);
await LocalImport.RunAsync(plan, new ServerImportSink(client, staging), verify: true, null, context.Cancellation);
var chunks = server.ChunkCount;
var again = await LocalImport.RunAsync(plan, new ServerImportSink(client, staging), verify: true, null, context.Cancellation);
Assert.Equal(0, again.Copied);
Assert.Equal(8, again.Duplicates);
Assert.Equal(chunks, server.ChunkCount);
}
[Test]
public async Task AFileTheServerRefusesIsListedAndTheImportGoesOn()
{
var card = Card(context.CreateTempDirectory("card"), 8, Start);
var staging = context.CreateTempDirectory("staging");
var server = new FakeUploadServer(chunkSize: 4096) { CorruptFile = "IMG_0003.JPG" };
using var client = new TitanoClient(new Uri("https://nas.local:8443/"), new CertificatePinning([]), "tests", server) { Token = "t" };
var plan = await new UnindexedDestination(ImportDestinationKind.Server, staging).PlanAsync(card, new ImportChoices().ToOptions(), null, null, context.Cancellation);
var sink = new ServerImportSink(client, staging);
var outcome = await LocalImport.RunAsync(plan, sink, true, null, context.Cancellation, sink.Report);
Assert.Equal(7, outcome.Copied);
Assert.Equal(7, server.Committed.Count);
Assert.Equal(new ImportFailure("DCIM/100CANON/IMG_0003.JPG", "Hash mismatch"), outcome.Failures.Single());
Assert.Empty(Directory.EnumerateFileSystemEntries(staging));
}
[Test]
public async Task ALostSignInStopsTheImportAndKeepsWhatWasUploaded()
{
var card = Card(context.CreateTempDirectory("card"), 8, Start);
var staging = context.CreateTempDirectory("staging");
var server = new FakeUploadServer(chunkSize: 4096) { SignedOutAfter = 3 };
using var client = new TitanoClient(new Uri("https://nas.local:8443/"), new CertificatePinning([]), "tests", server) { Token = "t" };
var plan = await new UnindexedDestination(ImportDestinationKind.Server, staging).PlanAsync(card, new ImportChoices().ToOptions(), null, null, context.Cancellation);
var sink = new ServerImportSink(client, staging);
var outcome = await LocalImport.RunAsync(plan, sink, true, null, context.Cancellation, sink.Report);
Assert.Equal(3, outcome.Copied, "the uploads the server committed before the sign-in was lost");
Assert.Equal(5, outcome.Failures.Count);
Assert.Equal("Sign in again", outcome.Failures[0].Message, "the server's sentence");
Assert.False(outcome.IsComplete);
}
[Test]
public async Task TheQuotaIsTheRoomOfAServerImport()
{
var card = Card(context.CreateTempDirectory("card"), 8, Start);
var staging = context.CreateTempDirectory("staging");
var plan = await new UnindexedDestination(ImportDestinationKind.Server, staging) { ServerSpace = 1_000 }
.PlanAsync(card, new ImportChoices().ToOptions(), null, null, context.Cancellation);
Assert.Equal(1_000L, plan.AvailableBytes);
Assert.False(ImportPlans.Totals(plan, verify: true).HasEnoughSpace, "eight photos do not fit in 1 KB of quota");
var unlimited = await new UnindexedDestination(ImportDestinationKind.Server, staging).PlanAsync(card, new ImportChoices().ToOptions(), null, null,
context.Cancellation);
Assert.Null(unlimited.AvailableBytes);
Assert.True(ImportPlans.Totals(unlimited, verify: true).HasEnoughSpace);
}
/// Start, chunk and complete of BRIEF ยง14.4, plus the title update; the hash is checked at completion.
private sealed class FakeUploadServer(int chunkSize) : HttpMessageHandler
{
private readonly Dictionary _open = [];
private readonly Dictionary _sequences = new(StringComparer.Ordinal);
private readonly Dictionary _frames = new(StringComparer.Ordinal);
public List Committed { get; } = [];
public Dictionary Titles { get; } = [];
public int ChunkCount { get; private set; }
/// Flip a byte of every chunk of this file (a damaged transfer).
public string? CorruptFile { get; init; }
/// Answer 401 to new uploads once this many were committed (the sign-in expired).
public int? SignedOutAfter { get; init; }
protected override async Task SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
var path = request.RequestUri!.AbsolutePath.Trim('/').Split('/');
if (request.Method == HttpMethod.Post && path is ["api", "v1", "uploads"])
{
var start = (await request.Content!.ReadFromJsonAsync(ProtocolJson.Default.StartUploadRequest, cancellationToken))!;
if (Committed.Count >= SignedOutAfter)
return Json(new ApiError("unauthorized", "Sign in again"), ProtocolJson.Default.ApiError, HttpStatusCode.Unauthorized);
if (_frames.TryGetValue(start.Sha256, out var existing))
return Json(new StartUploadResponse(null, chunkSize, [], true, existing), ProtocolJson.Default.StartUploadResponse);
var upload = new Upload(Guid.NewGuid(), start.FileName, start.Size, start.Sha256, start.SessionKey ?? "user");
_open[upload.Id] = upload;
return Json(new StartUploadResponse(upload.Id, chunkSize, [], false, null), ProtocolJson.Default.StartUploadResponse);
}
if (request.Method == HttpMethod.Put && path is ["api", "v1", "uploads", var id, "chunks", var n])
{
var upload = _open[Guid.Parse(id)];
var bytes = await request.Content!.ReadAsByteArrayAsync(cancellationToken);
if (upload.FileName == CorruptFile) bytes[0] ^= 0xFF;
upload.Parts[int.Parse(n, System.Globalization.CultureInfo.InvariantCulture)] = bytes;
ChunkCount++;
return Json(new ChunkReceived(int.Parse(n, System.Globalization.CultureInfo.InvariantCulture), [.. upload.Parts.Keys]), ProtocolJson.Default.ChunkReceived);
}
if (request.Method == HttpMethod.Post && path is ["api", "v1", "uploads", var done, "complete"])
{
var upload = _open[Guid.Parse(done)];
if (Convert.ToHexStringLower(SHA256.HashData(upload.Bytes)) != upload.Sha256)
return Json(new ApiError("hash_mismatch", "Hash mismatch"), ProtocolJson.Default.ApiError, HttpStatusCode.UnprocessableEntity);
_open.Remove(upload.Id);
Committed.Add(upload);
var frame = Guid.NewGuid();
_frames[upload.Sha256] = frame;
if (!_sequences.TryGetValue(upload.SessionKey, out var sequence)) _sequences[upload.SessionKey] = sequence = Guid.NewGuid();
return Json(new CompleteUploadResponse(frame, sequence, Committed.Count(u => u.SessionKey == upload.SessionKey) - 1),
ProtocolJson.Default.CompleteUploadResponse);
}
if (request.Method == HttpMethod.Patch && path is ["api", "v1", "sequences", var sequenceId])
{
var update = (await request.Content!.ReadFromJsonAsync(ProtocolJson.Default.UpdateSequenceRequest, cancellationToken))!;
Titles[Guid.Parse(sequenceId)] = update.Title!;
return Json(new SequenceDto(Guid.Parse(sequenceId), update.Title, update.Title!, "f", null, null, null, null, null, 0, null, 0, 0, 0, null, [], 0, null,
1, 1, 0, null, DateTimeOffset.UtcNow), ProtocolJson.Default.SequenceDto);
}
return new HttpResponseMessage(HttpStatusCode.NotFound);
}
private static HttpResponseMessage Json(T value, System.Text.Json.Serialization.Metadata.JsonTypeInfo type, HttpStatusCode status = HttpStatusCode.OK) =>
new(status) { Content = JsonContent.Create(value, type) };
public sealed record Upload(Guid Id, string FileName, long Size, string Sha256, string SessionKey)
{
public SortedDictionary Parts { get; } = [];
public byte[] Bytes => [.. Parts.Values.SelectMany(p => p)];
public int Chunks => Parts.Count;
}
}
}