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, "")); Assert.Null(await UpdateCheck.FetchAsync(garbage, Api, "0.1.0", CancellationToken.None)); using var broken = new HttpClient(new Reply(HttpStatusCode.InternalServerError, "{}")); await Assert.ThrowsAsync(() => 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(() => 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(() => ProductLinks.StableReleasesApiOf("https://gitea.example/only-owner")); } private sealed class Reply(HttpStatusCode status, string body) : HttpMessageHandler { protected override Task 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") }); } } }