Heimdall Gitea / Build and scan NVIDIA Agent image (push) Successful in 38m21s
Heimdall Gitea / Detect CI scope (push) Successful in 5s
Heimdall Gitea / Build and tests (push) Successful in 22m18s
Heimdall Gitea / Build and scan server image (push) Successful in 8m54s
Heimdall Gitea / Build and scan agent image (push) Successful in 6m40s
Heimdall Gitea / Publish versioned installer release (push) Skipped
Heimdall Gitea / CI policy (push) Successful in 3s
Co-authored-by: Arnaud Dartois <admin@tordais.cc>
136 lines
5.3 KiB
C#
136 lines
5.3 KiB
C#
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("<html><title>Proxy error</title></html>")]
|
|
[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<object?> 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<byte[]?> 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<byte[]?>)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<HttpResponseMessage> 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]));
|
|
}
|
|
}
|