CI / Build and test (push) Successful in 11m36s
CI / Windows tests and installer (push) Skipped
CI / YAML check (push) Successful in 12s
Release / Build, test and publish (push) Successful in 14m56s
CI / PostgreSQL 16 integration (push) Successful in 10m11s
CI / PostgreSQL 17 integration (push) Successful in 10m15s
CI / Docker image (push) Successful in 45s
Release / Server image (push) Successful in 4m55s
Co-authored-by: Alberto Balbo <alberto.balbo.96@gmail.com>
329 lines
16 KiB
C#
329 lines
16 KiB
C#
using System.IO;
|
|
using System.Net;
|
|
using System.Net.Http;
|
|
using System.Net.Http.Headers;
|
|
using System.Security.Cryptography;
|
|
using System.Text;
|
|
using Chronos.Core.Updates;
|
|
using Chronos.Testing;
|
|
|
|
namespace Chronos.Core.Tests.Updates;
|
|
|
|
public sealed class UpdateDownloadTests(TestContext context)
|
|
{
|
|
private const string Base = "https://gitea.example/o/r/releases/download/v1.0.0/";
|
|
private const string SetupName = "Chronos-Setup-1.0.0.exe";
|
|
private const string PortableName = "Chronos-win-x64-portable-1.0.0.zip";
|
|
|
|
private static readonly TimeSpan Idle = TimeSpan.FromSeconds(30);
|
|
private static readonly byte[] Setup = Encoding.ASCII.GetBytes(string.Concat(Enumerable.Repeat("MZ setup payload ", 20_000)));
|
|
private static readonly byte[] Portable = Encoding.ASCII.GetBytes(string.Concat(Enumerable.Repeat("PK portable zip ", 9_000)));
|
|
|
|
[Test]
|
|
public void ChecksumLinesOfBothSha256sumModesAreRead()
|
|
{
|
|
var hash = new string('A', 64);
|
|
var sums = UpdateDownload.ParseChecksums(
|
|
$"{hash} Chronos-Setup-1.0.0.exe\r\n{new string('b', 64)} *Chronos-Uploader-Setup-1.0.0.exe\nnot a checksum line\n{new string('c', 63)} short.exe\n\n");
|
|
Assert.Equal(2, sums.Count);
|
|
Assert.Equal(new string('a', 64), sums["Chronos-Setup-1.0.0.exe"]);
|
|
Assert.Equal(new string('b', 64), sums["Chronos-Uploader-Setup-1.0.0.exe"]);
|
|
}
|
|
|
|
[Test]
|
|
public void ThePackageForEachKindOfCopyAndTheChecksumsAreFoundAmongTheAssets()
|
|
{
|
|
var release = UpdateCheck.Parse($$"""
|
|
{ "tag_name": "v1.0.0", "html_url": "https://gitea.example/o/r/releases/tag/v1.0.0", "assets": [
|
|
{ "name": "Chronos-Uploader-Setup-1.0.0.exe", "browser_download_url": "{{Base}}Chronos-Uploader-Setup-1.0.0.exe", "size": 10 },
|
|
{ "name": "{{SetupName}}", "browser_download_url": "{{Base}}{{SetupName}}", "size": {{Setup.Length}} },
|
|
{ "name": "{{PortableName}}", "browser_download_url": "{{Base}}{{PortableName}}", "size": {{Portable.Length}} },
|
|
{ "name": "SHA256SUMS", "browser_download_url": "{{Base}}SHA256SUMS", "size": 300 },
|
|
{ "name": "evil.exe", "browser_download_url": "http://example.com/evil.exe", "size": 1 }
|
|
] }
|
|
""")!;
|
|
Assert.Equal(4, release.Assets.Count);
|
|
Assert.Equal(SetupName, UpdateDownload.Package(release, portable: false)!.Name);
|
|
Assert.Equal(PortableName, UpdateDownload.Package(release, portable: true)!.Name);
|
|
Assert.Equal(Base + "SHA256SUMS", UpdateDownload.Checksums(release)!.DownloadUrl);
|
|
Assert.True(UpdateDownload.CanDownload(release, portable: false));
|
|
Assert.True(UpdateDownload.CanDownload(release, portable: true));
|
|
Assert.False(UpdateDownload.CanDownload(release with { Assets = [.. release.Assets.Where(a => a.Name != "SHA256SUMS")] }, portable: false));
|
|
Assert.False(UpdateDownload.CanDownload(release with { Assets = [.. release.Assets.Where(a => a.Name != PortableName)] }, portable: true));
|
|
var root = UpdateDownload.Root(Path.GetTempPath());
|
|
Assert.True(root.EndsWith(Path.Combine("Chronos", "updates"), StringComparison.Ordinal), root);
|
|
Assert.Equal(Path.Combine(root, "v1.0.0"), UpdateDownload.Folder(root, release));
|
|
}
|
|
|
|
[Test]
|
|
public async Task AVerifiedSetupIsKeptAndReusedWithoutDownloadingAgain()
|
|
{
|
|
var root = context.CreateTempDirectory();
|
|
var server = new FakeReleases(Setup);
|
|
using var http = new HttpClient(server);
|
|
var reported = new List<double>();
|
|
|
|
var path = await DownloadAsync(http, root, progress: new SyncProgress(reported.Add));
|
|
Assert.Equal(Path.Combine(root, "v1.0.0", SetupName), path);
|
|
Assert.SequenceEqual(Setup, await File.ReadAllBytesAsync(path));
|
|
Assert.Equal(1.0, reported[^1]);
|
|
Assert.Equal(1, server.PackageRequests);
|
|
Assert.SequenceEqual(new[] { SetupName }, Directory.GetFiles(Path.GetDirectoryName(path)!).Select(f => Path.GetFileName(f)));
|
|
|
|
await DownloadAsync(http, root);
|
|
Assert.Equal(1, server.PackageRequests);
|
|
}
|
|
|
|
[Test]
|
|
public async Task APortableCopyGetsThePortableZipNeverTheSetup()
|
|
{
|
|
var root = context.CreateTempDirectory();
|
|
var server = new FakeReleases(Setup);
|
|
using var http = new HttpClient(server);
|
|
var path = await DownloadAsync(http, root, portable: true);
|
|
Assert.Equal(PortableName, Path.GetFileName(path));
|
|
Assert.SequenceEqual(Portable, await File.ReadAllBytesAsync(path));
|
|
Assert.SequenceEqual(new[] { Base + PortableName }, server.PackageUrls);
|
|
}
|
|
|
|
[Test]
|
|
public async Task ASetupThatDoesNotMatchItsChecksumIsDeleted()
|
|
{
|
|
var root = context.CreateTempDirectory();
|
|
var tampered = Setup.ToArray();
|
|
tampered[^1] ^= 1;
|
|
using var http = new HttpClient(new FakeReleases(Setup) { Served = tampered });
|
|
var error = await Assert.ThrowsAsync<UpdateVerificationException>(() => DownloadAsync(http, root));
|
|
Assert.Contains("does not match", error.Message);
|
|
Assert.Empty(Directory.GetFiles(root, "*", SearchOption.AllDirectories));
|
|
}
|
|
|
|
[Test]
|
|
public async Task ACorruptedSetupLeftFromBeforeIsReplacedByAFreshDownload()
|
|
{
|
|
var root = context.CreateTempDirectory();
|
|
var server = new FakeReleases(Setup);
|
|
using var http = new HttpClient(server);
|
|
var target = Path.Combine(root, "v1.0.0", SetupName);
|
|
Directory.CreateDirectory(Path.GetDirectoryName(target)!);
|
|
await File.WriteAllBytesAsync(target, Setup[..1000]);
|
|
|
|
Assert.Equal(target, await DownloadAsync(http, root));
|
|
Assert.SequenceEqual(Setup, await File.ReadAllBytesAsync(target));
|
|
Assert.Equal(1, server.PackageRequests);
|
|
}
|
|
|
|
[Test]
|
|
public async Task ASetupWithoutAChecksumLineIsNeverDownloaded()
|
|
{
|
|
var root = context.CreateTempDirectory();
|
|
var server = new FakeReleases(Setup) { Sums = $"{new string('0', 64)} Chronos-Uploader-Setup-1.0.0.exe\n" };
|
|
using var http = new HttpClient(server);
|
|
await Assert.ThrowsAsync<UpdateVerificationException>(() => DownloadAsync(http, root));
|
|
Assert.Equal(0, server.PackageRequests);
|
|
Assert.Empty(Directory.GetFiles(root, "*", SearchOption.AllDirectories));
|
|
}
|
|
|
|
[Test]
|
|
public async Task MoreBytesThanTheReleaseAnnouncedAreRefused()
|
|
{
|
|
var root = context.CreateTempDirectory();
|
|
var longer = Setup.Concat(new byte[100]).ToArray();
|
|
using var http = new HttpClient(new FakeReleases(Setup) { Served = longer, Sums = Sums(longer, Portable) });
|
|
await Assert.ThrowsAsync<UpdateVerificationException>(() => DownloadAsync(http, root));
|
|
Assert.Empty(Directory.GetFiles(root, "*", SearchOption.AllDirectories));
|
|
}
|
|
|
|
[Test]
|
|
public async Task AFailedRequestLeavesNoFile()
|
|
{
|
|
var root = context.CreateTempDirectory();
|
|
using var http = new HttpClient(new FakeReleases(Setup) { Fail = true });
|
|
await Assert.ThrowsAsync<HttpRequestException>(() => DownloadAsync(http, root));
|
|
Assert.Empty(Directory.GetFiles(root, "*", SearchOption.AllDirectories));
|
|
}
|
|
|
|
[Test]
|
|
public async Task ATransferCutShortIsResumedWhereItStopped()
|
|
{
|
|
var root = context.CreateTempDirectory();
|
|
var server = new FakeReleases(Setup) { CutAfter = 100_000 };
|
|
using var http = new HttpClient(server);
|
|
await Assert.ThrowsAsync<IOException>(() => DownloadAsync(http, root));
|
|
var part = Path.Combine(root, "v1.0.0", SetupName + ".part");
|
|
Assert.Equal(100_000L, new FileInfo(part).Length);
|
|
|
|
server.CutAfter = null;
|
|
var path = await DownloadAsync(http, root);
|
|
Assert.SequenceEqual(Setup, await File.ReadAllBytesAsync(path));
|
|
Assert.SequenceEqual(new long?[] { null, 100_000 }, server.Ranges);
|
|
Assert.FileMissing(part);
|
|
}
|
|
|
|
[Test]
|
|
public async Task AServerThatIgnoresTheRangeStartsTheFileAgain()
|
|
{
|
|
var root = context.CreateTempDirectory();
|
|
var server = new FakeReleases(Setup) { CutAfter = 50_000 };
|
|
using var http = new HttpClient(server);
|
|
await Assert.ThrowsAsync<IOException>(() => DownloadAsync(http, root));
|
|
|
|
server.CutAfter = null;
|
|
server.HonourRange = false;
|
|
var path = await DownloadAsync(http, root);
|
|
Assert.SequenceEqual(Setup, await File.ReadAllBytesAsync(path));
|
|
}
|
|
|
|
[Test]
|
|
public async Task ACancelledDownloadKeepsItsPartialFileForTheNextAttempt()
|
|
{
|
|
var root = context.CreateTempDirectory();
|
|
var server = new FakeReleases(Setup) { StallAfter = 64 * 1024 };
|
|
using var http = new HttpClient(server);
|
|
using var cts = new CancellationTokenSource();
|
|
var reported = new List<double>();
|
|
var download = DownloadAsync(http, root, progress: new SyncProgress(f =>
|
|
{
|
|
reported.Add(f);
|
|
cts.Cancel();
|
|
}), ct: cts.Token);
|
|
await Assert.ThrowsAsync<OperationCanceledException>(() => download);
|
|
var part = Path.Combine(root, "v1.0.0", SetupName + ".part");
|
|
Assert.Equal(64L * 1024, new FileInfo(part).Length);
|
|
|
|
server.StallAfter = null;
|
|
Assert.SequenceEqual(Setup, await File.ReadAllBytesAsync(await DownloadAsync(http, root)));
|
|
Assert.Equal(64L * 1024, server.Ranges[^1]);
|
|
}
|
|
|
|
[Test]
|
|
public async Task ATransferThatStallsFailsAfterTheIdleTimeout()
|
|
{
|
|
var root = context.CreateTempDirectory();
|
|
using var http = new HttpClient(new FakeReleases(Setup) { StallAfter = 10_000 });
|
|
var started = DateTime.UtcNow;
|
|
var error = await Assert.ThrowsAsync<TimeoutException>(() => DownloadAsync(http, root, idle: TimeSpan.FromMilliseconds(300)));
|
|
Assert.Contains("No data received", error.Message);
|
|
Assert.Less((DateTime.UtcNow - started).TotalSeconds, 20);
|
|
Assert.FileExists(Path.Combine(root, "v1.0.0", SetupName + ".part"));
|
|
}
|
|
|
|
[Test]
|
|
public async Task AnAssetOfUnknownSizeIsStillVerified()
|
|
{
|
|
var root = context.CreateTempDirectory();
|
|
using var http = new HttpClient(new FakeReleases(Setup));
|
|
var release = Release() with { Assets = [new ReleaseAsset(SetupName, Base + SetupName, -1), new ReleaseAsset("SHA256SUMS", Base + "SHA256SUMS", -1)] };
|
|
var reported = new List<double>();
|
|
var path = await UpdateDownload.DownloadAsync(http, release, false, root, "0.1.0", new SyncProgress(reported.Add), Idle, CancellationToken.None);
|
|
Assert.SequenceEqual(Setup, await File.ReadAllBytesAsync(path));
|
|
Assert.Empty(reported);
|
|
}
|
|
|
|
[Test]
|
|
public async Task AVerifiedDownloadRemovesTheFoldersOfOtherReleases()
|
|
{
|
|
var root = context.CreateTempDirectory();
|
|
var old = Directory.CreateDirectory(Path.Combine(root, "v0.9.0")).FullName;
|
|
await File.WriteAllTextAsync(Path.Combine(old, "Chronos-Setup-0.9.0.exe"), "old setup");
|
|
using var http = new HttpClient(new FakeReleases(Setup));
|
|
await DownloadAsync(http, root);
|
|
Assert.SequenceEqual(new[] { "v1.0.0" }, Directory.GetDirectories(root).Select(d => Path.GetFileName(d)));
|
|
}
|
|
|
|
private static Task<string> DownloadAsync(HttpClient http, string root, bool portable = false, IProgress<double>? progress = null, TimeSpan? idle = null,
|
|
CancellationToken ct = default) =>
|
|
UpdateDownload.DownloadAsync(http, Release(), portable, root, "0.1.0", progress, idle ?? Idle, ct);
|
|
|
|
private static ReleaseInfo Release() => new(new Version(1, 0, 0), "v1.0.0", "https://gitea.example/o/r/releases/tag/v1.0.0")
|
|
{
|
|
Assets =
|
|
[
|
|
new ReleaseAsset(SetupName, Base + SetupName, Setup.Length),
|
|
new ReleaseAsset(PortableName, Base + PortableName, Portable.Length),
|
|
new ReleaseAsset("SHA256SUMS", Base + "SHA256SUMS", 300),
|
|
],
|
|
};
|
|
|
|
private static string Sums(byte[] setup, byte[] portable) =>
|
|
$"{new string('1', 64)} Chronos-Uploader-Setup-1.0.0.exe\n{Hash(setup)} {SetupName}\n{Hash(portable)} *{PortableName}\n";
|
|
|
|
private static string Hash(byte[] bytes) => Convert.ToHexStringLower(SHA256.HashData(bytes));
|
|
|
|
/// <summary>
|
|
/// Serves SHA256SUMS, the setup and the portable zip like Gitea's release downloads: honours single open-ended
|
|
/// ranges, and can fail, end a transfer early or stall in the middle of it.
|
|
/// </summary>
|
|
private sealed class FakeReleases(byte[] setup) : HttpMessageHandler
|
|
{
|
|
public string Sums { get; set; } = UpdateDownloadTests.Sums(setup, Portable);
|
|
|
|
/// <summary>The setup bytes actually served (tampered or longer ones); the real setup by default.</summary>
|
|
public byte[] Served { get; set; } = setup;
|
|
|
|
public bool Fail { get; set; }
|
|
public bool HonourRange { get; set; } = true;
|
|
public int? CutAfter { get; set; }
|
|
public int? StallAfter { get; set; }
|
|
public int PackageRequests => PackageUrls.Count;
|
|
public List<string> PackageUrls { get; } = [];
|
|
public List<long?> Ranges { get; } = [];
|
|
|
|
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
|
|
{
|
|
Assert.True(request.Headers.UserAgent.ToString().StartsWith("Chronos/", StringComparison.Ordinal), "the update requests identify the program");
|
|
var url = request.RequestUri!.AbsoluteUri;
|
|
if (url == Base + "SHA256SUMS") return Task.FromResult(new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent(Sums) });
|
|
var bytes = url == Base + SetupName ? Served : url == Base + PortableName ? Portable : throw new AssertionException("Unexpected request " + url);
|
|
PackageUrls.Add(url);
|
|
var from = request.Headers.Range?.Ranges.Single().From;
|
|
Ranges.Add(from);
|
|
if (Fail) return Task.FromResult(new HttpResponseMessage(HttpStatusCode.InternalServerError));
|
|
|
|
var offset = HonourRange && from is { } start ? (int)start : 0;
|
|
var end = CutAfter is { } cut ? System.Math.Min(bytes.Length, offset + cut) : bytes.Length;
|
|
var body = new StreamContent(new Body(bytes[offset..end], StallAfter));
|
|
var response = new HttpResponseMessage(offset > 0 ? HttpStatusCode.PartialContent : HttpStatusCode.OK) { Content = body };
|
|
if (offset > 0) body.Headers.ContentRange = new ContentRangeHeaderValue(offset, bytes.Length - 1, bytes.Length);
|
|
return Task.FromResult(response);
|
|
}
|
|
}
|
|
|
|
/// <summary>A non-seekable body (no Content-Length) that waits for cancellation once <paramref name="stallAfter"/> bytes are read.</summary>
|
|
private sealed class Body(byte[] bytes, int? stallAfter) : Stream
|
|
{
|
|
private int _position;
|
|
|
|
public override bool CanRead => true;
|
|
public override bool CanSeek => false;
|
|
public override bool CanWrite => false;
|
|
public override long Length => throw new NotSupportedException();
|
|
public override long Position { get => _position; set => throw new NotSupportedException(); }
|
|
|
|
public override async ValueTask<int> ReadAsync(Memory<byte> buffer, CancellationToken cancellationToken = default)
|
|
{
|
|
if (_position == stallAfter) await Task.Delay(Timeout.Infinite, cancellationToken);
|
|
var limit = stallAfter is { } stall && _position < stall ? stall : bytes.Length;
|
|
var count = System.Math.Min(buffer.Length, limit - _position);
|
|
bytes.AsMemory(_position, count).CopyTo(buffer);
|
|
_position += count;
|
|
return count;
|
|
}
|
|
|
|
public override Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken) =>
|
|
ReadAsync(buffer.AsMemory(offset, count), cancellationToken).AsTask();
|
|
|
|
public override int Read(byte[] buffer, int offset, int count) => ReadAsync(buffer, offset, count, CancellationToken.None).GetAwaiter().GetResult();
|
|
public override void Flush() => throw new NotSupportedException();
|
|
public override long Seek(long offset, SeekOrigin origin) => throw new NotSupportedException();
|
|
public override void SetLength(long value) => throw new NotSupportedException();
|
|
public override void Write(byte[] buffer, int offset, int count) => throw new NotSupportedException();
|
|
}
|
|
|
|
/// <summary>Reports synchronously (<see cref="Progress{T}"/> would post to the thread pool).</summary>
|
|
private sealed class SyncProgress(Action<double> report) : IProgress<double>
|
|
{
|
|
public void Report(double value) => report(value);
|
|
}
|
|
}
|