using System.Net; using System.Net.Http; using System.Reflection; using System.Text; using Heimdall.Api.Controllers; using Xunit; namespace Heimdall.Web.Tests.Controllers; public sealed class UpdatesControllerRetryTests { [Fact] public async Task FetchBoundedReleaseAssetAsync_allowsMissingForgejoDigestForSignedMetadata() { using var client = new HttpClient(new StaticResponseHandler("verified-by-release-signature")); var result = await FetchAssetAsync("https://forgejo.example/attachments/release-key", digest: null, client); Assert.Equal("verified-by-release-signature", Encoding.UTF8.GetString(result!)); } [Fact] public async Task FetchBoundedReleaseAssetAsync_rejectsCrossOriginUrls() { var handler = new StaticResponseHandler("unexpected"); using var client = new HttpClient(handler); var result = await FetchAssetAsync("https://other.example/asset", digest: null, client); Assert.Null(result); Assert.Equal(0, handler.RequestCount); } [Theory] [InlineData("Proxy error")] [InlineData("{not-json")] public async Task ReadForgejoReleaseAsync_returns_null_for_non_json_responses(string body) { var result = await ReadReleaseAsync(new StringContent(body, Encoding.UTF8, "text/html")); Assert.Null(result); } [Fact] public async Task ReadForgejoReleaseAsync_returns_release_for_valid_json() { var result = await ReadReleaseAsync(new StringContent( "{\"tag_name\":\"v1.2.3\",\"body\":\"notes\"}", Encoding.UTF8, "application/json")); Assert.NotNull(result); Assert.Equal("v1.2.3", result.GetType().GetProperty("TagName")?.GetValue(result)); Assert.Equal("notes", result.GetType().GetProperty("Body")?.GetValue(result)); } [Fact] public async Task ReadForgejoReleaseAsync_rejects_content_larger_than_limit() { using var content = new StringContent("{}", Encoding.UTF8, "application/json"); content.Headers.ContentLength = 1_048_577; var result = await ReadReleaseAsync(content); Assert.Null(result); } [Fact] public async Task ReadForgejoReleaseAsync_rejects_oversized_stream_without_content_length() { using var content = new StringContent(new string(' ', 1_048_577), Encoding.UTF8, "application/json"); content.Headers.ContentLength = null; var result = await ReadReleaseAsync(content); Assert.Null(result); } private static async Task ReadReleaseAsync(HttpContent content) { var method = typeof(UpdatesController).GetMethod("ReadForgejoReleaseAsync", BindingFlags.NonPublic | BindingFlags.Static); Assert.NotNull(method); var task = method.Invoke(null, [content, CancellationToken.None]); Assert.NotNull(task); await (Task)task; return task.GetType().GetProperty("Result")?.GetValue(task); } private static async Task FetchAssetAsync(string url, string? digest, HttpClient client) { var assetType = typeof(UpdatesController).GetNestedType("ForgejoAsset", BindingFlags.NonPublic); Assert.NotNull(assetType); var asset = Activator.CreateInstance(assetType)!; assetType.GetProperty("Name")!.SetValue(asset, "agent-release-public.pem"); assetType.GetProperty("BrowserDownloadUrl")!.SetValue(asset, url); assetType.GetProperty("Digest")!.SetValue(asset, digest); var listType = typeof(List<>).MakeGenericType(assetType); var assets = (System.Collections.IList)Activator.CreateInstance(listType)!; assets.Add(asset); var method = typeof(UpdatesController).GetMethod("FetchBoundedReleaseAssetAsync", BindingFlags.NonPublic | BindingFlags.Static); Assert.NotNull(method); var task = (Task)method.Invoke(null, [assets, "agent-release-public.pem", client, 4096, new Uri("https://forgejo.example/api/v1/releases/latest"), CancellationToken.None])!; return await task; } private sealed class StaticResponseHandler(string body) : HttpMessageHandler { public int RequestCount { get; private set; } protected override Task SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) { RequestCount++; return Task.FromResult(new HttpResponseMessage(HttpStatusCode.OK) { RequestMessage = request, Content = new StringContent(body, Encoding.UTF8), }); } } [Theory] [InlineData(HttpStatusCode.RequestTimeout, true)] [InlineData(HttpStatusCode.TooManyRequests, true)] [InlineData(HttpStatusCode.BadGateway, true)] [InlineData(HttpStatusCode.ServiceUnavailable, true)] [InlineData(HttpStatusCode.NotFound, false)] [InlineData(HttpStatusCode.BadRequest, false)] public void IsTransientStatusCode_classifies_only_retryable_http_responses(HttpStatusCode code, bool expected) { var method = typeof(UpdatesController).GetMethod("IsTransientStatusCode", BindingFlags.NonPublic | BindingFlags.Static); Assert.NotNull(method); Assert.Equal(expected, method.Invoke(null, [code])); } }