Download now fetches the desktop setup of the latest release into %TEMP%\Titano\updates\<tag>, keeps it only when its SHA-256 matches the release's SHA256SUMS line, and shows it in Explorer. BRIEF 8.6 does not list starting an installer among the allowed shell actions, so the user runs it. A release without a setup or SHA256SUMS still opens its page; capture runs never download. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
146 lines
7.1 KiB
C#
146 lines
7.1 KiB
C#
using System.IO;
|
|
using System.Net;
|
|
using System.Net.Http;
|
|
using System.Security.Cryptography;
|
|
using System.Text;
|
|
using Titano.App.About;
|
|
using Titano.Testing;
|
|
|
|
namespace Titano.App.Tests.About;
|
|
|
|
public sealed class UpdateDownloadTests(TestContext context)
|
|
{
|
|
private const string SetupUrl = "https://github.com/o/r/releases/download/v1.0.0/Titano-Setup-1.0.0.exe";
|
|
private const string SumsUrl = "https://github.com/o/r/releases/download/v1.0.0/SHA256SUMS";
|
|
|
|
private static readonly byte[] Setup = Encoding.ASCII.GetBytes(string.Concat(Enumerable.Repeat("MZ setup payload ", 20_000)));
|
|
|
|
[Test]
|
|
public void ChecksumLinesOfBothSha256sumModesAreRead()
|
|
{
|
|
var hash = new string('A', 64);
|
|
var sums = UpdateDownload.ParseChecksums(
|
|
$"{hash} Titano-Setup-1.0.0.exe\r\n{new string('b', 64)} *Titano-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["Titano-Setup-1.0.0.exe"]);
|
|
Assert.Equal(new string('b', 64), sums["Titano-Uploader-Setup-1.0.0.exe"]);
|
|
}
|
|
|
|
[Test]
|
|
public void TheDesktopSetupAndTheChecksumsAreFoundAmongTheAssets()
|
|
{
|
|
var release = UpdateCheck.Parse($$"""
|
|
{ "tag_name": "v1.0.0", "html_url": "https://github.com/o/r/releases/tag/v1.0.0", "assets": [
|
|
{ "name": "Titano-Uploader-Setup-1.0.0.exe", "browser_download_url": "https://github.com/o/r/releases/download/v1.0.0/Titano-Uploader-Setup-1.0.0.exe", "size": 10 },
|
|
{ "name": "Titano-Setup-1.0.0.exe", "browser_download_url": "{{SetupUrl}}", "size": {{Setup.Length}} },
|
|
{ "name": "SHA256SUMS", "browser_download_url": "{{SumsUrl}}", "size": 300 },
|
|
{ "name": "evil.exe", "browser_download_url": "http://example.com/evil.exe", "size": 1 }
|
|
] }
|
|
""")!;
|
|
Assert.Equal(3, release.Assets.Count);
|
|
Assert.Equal("Titano-Setup-1.0.0.exe", UpdateDownload.Installer(release)!.Name);
|
|
Assert.Equal(SumsUrl, UpdateDownload.Checksums(release)!.DownloadUrl);
|
|
Assert.True(UpdateDownload.CanDownload(release));
|
|
Assert.False(UpdateDownload.CanDownload(release with { Assets = [.. release.Assets.Where(a => a.Name != "SHA256SUMS")] }));
|
|
Assert.True(UpdateDownload.Folder(Path.GetTempPath(), release).EndsWith(Path.Combine("Titano", "updates", "v1.0.0"), StringComparison.Ordinal));
|
|
}
|
|
|
|
[Test]
|
|
public async Task AVerifiedSetupIsKeptAndReusedWithoutDownloadingAgain()
|
|
{
|
|
var folder = context.CreateTempDirectory();
|
|
var server = new FakeReleases(Sums(Setup), Setup);
|
|
using var http = new HttpClient(server);
|
|
var reported = new List<double>();
|
|
|
|
var path = await UpdateDownload.DownloadAsync(http, Release(), folder, "0.1.0", new SyncProgress(reported.Add), CancellationToken.None);
|
|
Assert.Equal(Path.Combine(folder, "Titano-Setup-1.0.0.exe"), path);
|
|
Assert.SequenceEqual(Setup, await File.ReadAllBytesAsync(path));
|
|
Assert.Equal(1.0, reported[^1]);
|
|
Assert.Equal(1, server.SetupRequests);
|
|
Assert.SequenceEqual(new[] { "Titano-Setup-1.0.0.exe" }, Directory.GetFiles(folder).Select(f => Path.GetFileName(f)));
|
|
|
|
await UpdateDownload.DownloadAsync(http, Release(), folder, "0.1.0", null, CancellationToken.None);
|
|
Assert.Equal(1, server.SetupRequests);
|
|
}
|
|
|
|
[Test]
|
|
public async Task ASetupThatDoesNotMatchItsChecksumIsDeleted()
|
|
{
|
|
var folder = context.CreateTempDirectory();
|
|
var tampered = Setup.ToArray();
|
|
tampered[^1] ^= 1;
|
|
using var http = new HttpClient(new FakeReleases(Sums(Setup), tampered));
|
|
var error = await Assert.ThrowsAsync<UpdateVerificationException>(() =>
|
|
UpdateDownload.DownloadAsync(http, Release(), folder, "0.1.0", null, CancellationToken.None));
|
|
Assert.Contains("does not match", error.Message);
|
|
Assert.Empty(Directory.GetFiles(folder));
|
|
}
|
|
|
|
[Test]
|
|
public async Task ASetupWithoutAChecksumLineIsNeverDownloaded()
|
|
{
|
|
var folder = context.CreateTempDirectory();
|
|
var server = new FakeReleases($"{new string('0', 64)} Titano-Uploader-Setup-1.0.0.exe\n", Setup);
|
|
using var http = new HttpClient(server);
|
|
await Assert.ThrowsAsync<UpdateVerificationException>(() => UpdateDownload.DownloadAsync(http, Release(), folder, "0.1.0", null, CancellationToken.None));
|
|
Assert.Equal(0, server.SetupRequests);
|
|
Assert.Empty(Directory.GetFiles(folder));
|
|
}
|
|
|
|
[Test]
|
|
public async Task MoreBytesThanTheReleaseAnnouncedAreRefused()
|
|
{
|
|
var folder = context.CreateTempDirectory();
|
|
var longer = Setup.Concat(new byte[100]).ToArray();
|
|
using var http = new HttpClient(new FakeReleases(Sums(longer), longer));
|
|
await Assert.ThrowsAsync<UpdateVerificationException>(() => UpdateDownload.DownloadAsync(http, Release(), folder, "0.1.0", null, CancellationToken.None));
|
|
Assert.Empty(Directory.GetFiles(folder));
|
|
}
|
|
|
|
[Test]
|
|
public async Task AFailedRequestLeavesNoPartialFile()
|
|
{
|
|
var folder = context.CreateTempDirectory();
|
|
using var http = new HttpClient(new FakeReleases(Sums(Setup), null));
|
|
await Assert.ThrowsAsync<HttpRequestException>(() => UpdateDownload.DownloadAsync(http, Release(), folder, "0.1.0", null, CancellationToken.None));
|
|
Assert.Empty(Directory.GetFiles(folder));
|
|
}
|
|
|
|
private static ReleaseInfo Release() => new(new Version(1, 0, 0), "v1.0.0", "https://github.com/o/r/releases/tag/v1.0.0")
|
|
{
|
|
Assets =
|
|
[
|
|
new ReleaseAsset("Titano-Setup-1.0.0.exe", SetupUrl, Setup.Length),
|
|
new ReleaseAsset("SHA256SUMS", SumsUrl, 200),
|
|
],
|
|
};
|
|
|
|
private static string Sums(byte[] setup) =>
|
|
$"{new string('1', 64)} Titano-Uploader-Setup-1.0.0.exe\n{Convert.ToHexStringLower(SHA256.HashData(setup))} Titano-Setup-1.0.0.exe\n";
|
|
|
|
/// <summary>Serves SHA256SUMS and the setup (null: the setup answers 500).</summary>
|
|
private sealed class FakeReleases(string sums, byte[]? setup) : HttpMessageHandler
|
|
{
|
|
public int SetupRequests { get; private set; }
|
|
|
|
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
|
|
{
|
|
Assert.True(request.Headers.UserAgent.ToString().StartsWith("Titano/", StringComparison.Ordinal), "GitHub requires a User-Agent");
|
|
var url = request.RequestUri!.AbsoluteUri;
|
|
if (url == SumsUrl) return Task.FromResult(new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent(sums) });
|
|
Assert.Equal(SetupUrl, url);
|
|
SetupRequests++;
|
|
return Task.FromResult(setup is null
|
|
? new HttpResponseMessage(HttpStatusCode.InternalServerError)
|
|
: new HttpResponseMessage(HttpStatusCode.OK) { Content = new ByteArrayContent(setup) });
|
|
}
|
|
}
|
|
|
|
/// <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);
|
|
}
|
|
}
|