feat: single setup, mass-pass progress, per-server homes, Desktop shares its library (owner review 5, v1.1.0) (#68)
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
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>
This commit was merged in pull request #68.
This commit is contained in:
1 parent
988068ee8e
commit
20d64af4a4
793 files changed
+23882
-2096
No files matched your search
@@ -0,0 +1,148 @@
|
||||
using System.Net;
|
||||
using System.Net.Http;
|
||||
using System.Text;
|
||||
using Chronos.Core.Updates;
|
||||
using Chronos.Testing;
|
||||
|
||||
namespace Chronos.Core.Tests.Updates;
|
||||
|
||||
public sealed class UpdateCheckTests
|
||||
{
|
||||
[Theory]
|
||||
[Case("v1.2.3", "1.2.3")]
|
||||
[Case("1.2", "1.2.0")]
|
||||
[Case("V0.10.0-beta.2", "0.10.0")]
|
||||
[Case("v2.0.1+abc", "2.0.1")]
|
||||
public void TagsParseToVersions(string tag, string expected) =>
|
||||
Assert.Equal(Version.Parse(expected), UpdateCheck.ParseVersion(tag));
|
||||
|
||||
[Theory]
|
||||
[Case(new object?[] { null })]
|
||||
[Case("")]
|
||||
[Case("latest")]
|
||||
[Case("v-one")]
|
||||
public void OtherTagsAreIgnored(string? tag) => Assert.Null(UpdateCheck.ParseVersion(tag));
|
||||
|
||||
[Test]
|
||||
public void TheLatestReleaseIsComparedWithTheRunningVersion()
|
||||
{
|
||||
var release = UpdateCheck.Parse("""{ "tag_name": "v0.2.0", "html_url": "https://gitea.example/o/r/releases/tag/v0.2.0", "draft": false, "prerelease": false }""");
|
||||
Assert.NotNull(release);
|
||||
Assert.Equal(new Version(0, 2, 0), release!.Version);
|
||||
Assert.True(UpdateCheck.IsNewer(release, UpdateCheck.RunningVersion("0.1.0")));
|
||||
Assert.False(UpdateCheck.IsNewer(release, UpdateCheck.RunningVersion("0.2.0+local")));
|
||||
Assert.False(UpdateCheck.IsNewer(release, new Version(0, 2, 0, 0)));
|
||||
Assert.False(UpdateCheck.IsNewer(release, UpdateCheck.RunningVersion("1.0.0")));
|
||||
Assert.Equal("0.2.0", UpdateCheck.Display(release.Version));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void APreReleaseBuildIsOfferedTheFinalReleaseOfItsVersion()
|
||||
{
|
||||
var final = UpdateCheck.Parse("""{ "tag_name": "v0.1.0", "html_url": "https://gitea.example/o/r/releases/tag/v0.1.0" }""")!;
|
||||
Assert.True(UpdateCheck.IsPrerelease("0.1.0-rc.1"));
|
||||
Assert.True(UpdateCheck.IsPrerelease("v0.1.0-beta+abc"));
|
||||
Assert.False(UpdateCheck.IsPrerelease("0.1.0+abc-def"));
|
||||
Assert.False(UpdateCheck.IsPrerelease("0.1.0"));
|
||||
Assert.True(UpdateCheck.IsNewer(final, UpdateCheck.RunningVersion("0.1.0-rc.1"), UpdateCheck.IsPrerelease("0.1.0-rc.1")));
|
||||
Assert.False(UpdateCheck.IsNewer(final, UpdateCheck.RunningVersion("0.1.0"), UpdateCheck.IsPrerelease("0.1.0")));
|
||||
Assert.False(UpdateCheck.IsNewer(final, UpdateCheck.RunningVersion("0.2.0-rc.1"), UpdateCheck.IsPrerelease("0.2.0-rc.1")));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DraftsPrereleasesAndOddLinksAreIgnored()
|
||||
{
|
||||
Assert.Null(UpdateCheck.Parse("""{ "tag_name": "v9.0.0", "html_url": "https://x", "prerelease": true }"""));
|
||||
Assert.Null(UpdateCheck.Parse("""{ "tag_name": "v9.0.0", "html_url": "https://x", "draft": true }"""));
|
||||
Assert.Null(UpdateCheck.Parse("""{ "tag_name": "v9.0.0", "html_url": "javascript:alert(1)" }"""));
|
||||
Assert.Null(UpdateCheck.Parse("""{ "html_url": "https://x" }"""));
|
||||
Assert.Null(UpdateCheck.Parse("[]"));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task FetchReadsTheApiAndToleratesMissingReleases()
|
||||
{
|
||||
// Gitea lists the most recently created first: a re-published 0.9.0 must not hide 1.0.0.
|
||||
using var ok = new HttpClient(new Reply(HttpStatusCode.OK, """
|
||||
[ { "tag_name": "v0.9.0", "html_url": "https://gitea.example/o/r/releases/tag/v0.9.0" },
|
||||
{ "tag_name": "v1.0.0", "html_url": "https://gitea.example/o/r/releases/tag/v1.0.0" },
|
||||
{ "tag_name": "v2.0.0", "html_url": "https://gitea.example/o/r/releases/tag/v2.0.0", "prerelease": true } ]
|
||||
"""));
|
||||
var release = await UpdateCheck.FetchAsync(ok, Api, "0.1.0", CancellationToken.None);
|
||||
Assert.Equal(new Version(1, 0, 0), release!.Version);
|
||||
|
||||
using var none = new HttpClient(new Reply(HttpStatusCode.OK, "[]"));
|
||||
Assert.Null(await UpdateCheck.FetchAsync(none, Api, "0.1.0", CancellationToken.None));
|
||||
|
||||
using var garbage = new HttpClient(new Reply(HttpStatusCode.OK, "<html>"));
|
||||
Assert.Null(await UpdateCheck.FetchAsync(garbage, Api, "0.1.0", CancellationToken.None));
|
||||
|
||||
using var broken = new HttpClient(new Reply(HttpStatusCode.InternalServerError, "{}"));
|
||||
await Assert.ThrowsAsync<HttpRequestException>(() => UpdateCheck.FetchAsync(broken, Api, "0.1.0", CancellationToken.None));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task LinksComeFromTheRepositoryAddressNotFromGiteasRootUrl()
|
||||
{
|
||||
// The owner's Gitea writes its links with its ROOT_URL, a plain-http LAN address clients cannot use.
|
||||
const string json = """
|
||||
[ { "tag_name": "v1.0.0", "html_url": "http://192.168.30.23:3000/BalboSoft/Chronos/releases/tag/v1.0.0",
|
||||
"assets": [ { "name": "Chronos-Setup-1.0.0.exe", "size": 5, "browser_download_url": "http://192.168.30.23:3000/BalboSoft/Chronos/releases/download/v1.0.0/Chronos-Setup-1.0.0.exe" },
|
||||
{ "name": "Read me.txt", "size": 2, "browser_download_url": "http://192.168.30.23:3000/x" } ] } ]
|
||||
""";
|
||||
Assert.Null(UpdateCheck.Parse(json));
|
||||
|
||||
var release = UpdateCheck.Parse(json, "https://gitea.example/BalboSoft/Chronos/")!;
|
||||
Assert.Equal("https://gitea.example/BalboSoft/Chronos/releases/tag/v1.0.0", release.PageUrl);
|
||||
Assert.SequenceEqual(
|
||||
["https://gitea.example/BalboSoft/Chronos/releases/download/v1.0.0/Chronos-Setup-1.0.0.exe",
|
||||
"https://gitea.example/BalboSoft/Chronos/releases/download/v1.0.0/Read%20me.txt"],
|
||||
release.Assets.Select(a => a.DownloadUrl));
|
||||
Assert.Equal(5L, release.Assets[0].Size);
|
||||
Assert.Null(UpdateCheck.Parse(json, "http://gitea.example/BalboSoft/Chronos"));
|
||||
|
||||
using var ok = new HttpClient(new Reply(HttpStatusCode.OK, json));
|
||||
var fetched = await UpdateCheck.FetchAsync(ok, Api, "https://gitea.example/BalboSoft/Chronos", "0.1.0", CancellationToken.None);
|
||||
Assert.Equal("https://gitea.example/BalboSoft/Chronos/releases/tag/v1.0.0", fetched!.PageUrl);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task ARepositoryThatCannotBeSeenIsAFailedCheckNotUpToDate()
|
||||
{
|
||||
using var hidden = new HttpClient(new Reply(HttpStatusCode.NotFound, """{ "message": "not found" }"""));
|
||||
var error = await Assert.ThrowsAsync<HttpRequestException>(() => UpdateCheck.FetchAsync(hidden, Api, "0.1.0", CancellationToken.None));
|
||||
Assert.Equal(HttpStatusCode.NotFound, error.StatusCode);
|
||||
}
|
||||
|
||||
private const string Api = "https://gitea.example/api/v1/repos/o/r/releases?draft=false&pre-release=false&limit=50";
|
||||
|
||||
[Theory]
|
||||
[Case("https://gitea.example/BalboSoft/Chronos", "https://gitea.example/api/v1/repos/BalboSoft/Chronos/releases?draft=false&pre-release=false&limit=50")]
|
||||
[Case("https://example.org:8443/git/BalboSoft/Chronos", "https://example.org:8443/git/api/v1/repos/BalboSoft/Chronos/releases?draft=false&pre-release=false&limit=50")]
|
||||
public void TheReleasesAreAskedOfTheGiteaApiOfTheRepository(string repository, string expected) =>
|
||||
Assert.Equal(expected, ProductLinks.StableReleasesApiOf(repository));
|
||||
|
||||
[Theory]
|
||||
[Case(" https://gitea.example/o/r/ ", "https://gitea.example/o/r")]
|
||||
[Case("https://gitea.example/o/r.git", "https://gitea.example/o/r")]
|
||||
[Case("", null)]
|
||||
public void TheReleasesRepositoryAddressIsNormalised(string address, string? expected) =>
|
||||
Assert.Equal(expected, ProductLinks.Normalise(address));
|
||||
|
||||
[Test]
|
||||
public void LocalBuildsLinkTheRepositoryTheBuildPropertiesName()
|
||||
{
|
||||
Assert.Equal(ProductLinks.DefaultRepository, ProductLinks.Repository);
|
||||
Assert.Equal(ProductLinks.DefaultRepository + "/src/branch/main/EULA.md", ProductLinks.Licence);
|
||||
Assert.Throws<ArgumentException>(() => ProductLinks.StableReleasesApiOf("https://gitea.example/only-owner"));
|
||||
}
|
||||
|
||||
private sealed class Reply(HttpStatusCode status, string body) : HttpMessageHandler
|
||||
{
|
||||
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");
|
||||
return Task.FromResult(new HttpResponseMessage(status) { Content = new StringContent(body, Encoding.UTF8, "application/json") });
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,328 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user