mirror of
https://github.com/BinaryAlley/Nemera.git
synced 2026-12-01 18:57:24 +00:00
- embeds the append-only historical and deny trust lists in the API and marks matching bundled plugins as Official without blocking loading - builds and records the current plugin manifest only for official release builds - deploys the PDF native libraries for the supported Linux, macOS and Windows runtimes from the plugin dependency manifest - records release hashes by appending only missing entries to master's manifest and rejects any diff that removes a line - documents the release flow, the supported runtimes and the plugin installer limits
163 lines
9.2 KiB
C#
163 lines
9.2 KiB
C#
#region ========================================================================= USING =====================================================================================
|
|
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.Extensions.Hosting;
|
|
using Nemera.Application.Common.DomainEvents;
|
|
using Nemera.Application.Common.Infrastructure.Authorization.Policies.ConsumptionActivity;
|
|
using Nemera.Application.Common.Infrastructure.Models.DTO.Configuration;
|
|
using Nemera.Application.Common.Infrastructure.Plugins;
|
|
using Nemera.Application.Common.Infrastructure.Security;
|
|
using Nemera.Application.Common.Infrastructure.Time;
|
|
using Nemera.Application.Common.Infrastructure.Validation;
|
|
using Nemera.Application.Core.MediaLibrary.AudioLibrary.MusicLibrary.Playback;
|
|
using Nemera.Application.Core.MediaLibrary.Management.Progress;
|
|
using Nemera.Domain.Common.Events;
|
|
using Nemera.Domain.Core.BoundedContexts.LibraryManagementBoundedContext.LibraryScanAggregate.Services.Cancellation;
|
|
using Nemera.Domain.Core.BoundedContexts.LibraryManagementBoundedContext.LibraryScanAggregate.Services.Queue;
|
|
using Nemera.Infrastructure.Common.DependencyInjection;
|
|
using Nemera.Infrastructure.Common.Networking;
|
|
using Nemera.Infrastructure.Core.Authorization.Policies.ConsumptionActivity;
|
|
using Nemera.Infrastructure.Core.MediaLibrary.AudioLibrary.MusicLibrary.Playback;
|
|
using Nemera.Infrastructure.Core.Security;
|
|
using Nemera.Infrastructure.Core.Time;
|
|
using Nemera.Plugins.Contracts.Core.Plugins;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Diagnostics.CodeAnalysis;
|
|
using System.Net.Http;
|
|
#endregion
|
|
|
|
namespace Nemera.Infrastructure.UnitTests.Common.DependencyInjection;
|
|
|
|
/// <summary>
|
|
/// Contains unit tests for the <see cref="InfrastructureLayerServices"/> class.
|
|
/// </summary>
|
|
[ExcludeFromCodeCoverage]
|
|
public class InfrastructureLayerServicesTests
|
|
{
|
|
private static readonly IReadOnlyDictionary<string, IReadOnlySet<string>> s_emptyBundledPluginChecksums = new Dictionary<string, IReadOnlySet<string>>();
|
|
|
|
[Fact]
|
|
public void AddInfrastructureLayerServices_WhenCalled_ShouldRegisterCoreInfrastructureServices()
|
|
{
|
|
// Arrange
|
|
IServiceCollection services = new ServiceCollection();
|
|
IConfiguration configuration = CreateConfiguration();
|
|
|
|
// Act
|
|
services.AddInfrastructureLayerServices(configuration, s_emptyBundledPluginChecksums);
|
|
|
|
// Assert
|
|
Assert.Contains(services, descriptor => descriptor.ServiceType == typeof(IFileHashService) && descriptor.ImplementationType == typeof(FileHashService));
|
|
Assert.Contains(services, descriptor => descriptor.ServiceType == typeof(IDateTimeProvider) && descriptor.ImplementationType == typeof(DateTimeProvider));
|
|
Assert.Contains(services, descriptor => descriptor.ServiceType == typeof(IPlaybackFormatService) && descriptor.ImplementationType == typeof(PlaybackFormatService));
|
|
Assert.Contains(services, descriptor => descriptor.ServiceType == typeof(IPluginManager));
|
|
Assert.Contains(services, descriptor => descriptor.ServiceType == typeof(IPluginSettingsStore));
|
|
Assert.Contains(services, descriptor => descriptor.ServiceType == typeof(IDomainEventsQueue));
|
|
Assert.Contains(services, descriptor => descriptor.ServiceType == typeof(IDomainEventPublisher));
|
|
Assert.Contains(services, descriptor => descriptor.ServiceType == typeof(IMediaLibrariesScanQueue));
|
|
Assert.Contains(services, descriptor => descriptor.ServiceType == typeof(IMediaLibrariesScanCancellationTracker));
|
|
Assert.Contains(services, descriptor => descriptor.ServiceType == typeof(IMediaLibraryScanProgressNotifier));
|
|
Assert.Contains(services, descriptor => descriptor.ServiceType == typeof(IHostedService));
|
|
Assert.Contains(services, descriptor => descriptor.ServiceType == typeof(IConsumptionActivityAccessPolicy) && descriptor.ImplementationType == typeof(ConsumptionActivityAccessPolicy));
|
|
}
|
|
|
|
[Fact]
|
|
public void AddInfrastructureLayerServices_WhenCalled_ShouldRegisterAllValidatorsFromTheAssembly()
|
|
{
|
|
// Arrange
|
|
IServiceCollection services = new ServiceCollection();
|
|
IConfiguration configuration = CreateConfiguration();
|
|
|
|
// Act
|
|
services.AddInfrastructureLayerServices(configuration, s_emptyBundledPluginChecksums);
|
|
|
|
// Assert
|
|
Assert.Contains(services, descriptor => descriptor.ServiceType == typeof(IValidator<PluginsSettingsDto>));
|
|
Assert.Contains(services, descriptor => descriptor.ServiceType == typeof(IValidator<Nemera.Infrastructure.Common.Models.DTO.Configuration.CorsSettingsDto>));
|
|
Assert.Contains(services, descriptor => descriptor.ServiceType == typeof(IValidator<Nemera.Infrastructure.Common.Models.DTO.Configuration.JwtSettingsDto>));
|
|
}
|
|
|
|
[Fact]
|
|
public void AddInfrastructureLayerServices_WhenCalled_ShouldGuardEveryOutboundHttpClientWithThePublicHostChecks()
|
|
{
|
|
// Arrange
|
|
IServiceCollection services = new ServiceCollection();
|
|
IConfiguration configuration = CreateConfiguration();
|
|
services.AddInfrastructureLayerServices(configuration, s_emptyBundledPluginChecksums);
|
|
// A plugin registers its typed client after the infrastructure layer, so the defaults must reach it too.
|
|
services.AddHttpClient("plugin-client");
|
|
using ServiceProvider serviceProvider = services.BuildServiceProvider();
|
|
IHttpMessageHandlerFactory httpMessageHandlerFactory = serviceProvider.GetRequiredService<IHttpMessageHandlerFactory>();
|
|
|
|
// Act
|
|
using HttpMessageHandler pluginClientHandler = httpMessageHandlerFactory.CreateHandler("plugin-client");
|
|
using HttpMessageHandler artworkClientHandler = httpMessageHandlerFactory.CreateHandler(NamedHttpClients.ARTWORK_DOWNLOAD);
|
|
|
|
// Assert
|
|
AssertUsesThePublicHostGuards(pluginClientHandler);
|
|
AssertUsesThePublicHostGuards(artworkClientHandler);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Creates an in-memory configuration pointing the plugins directory to a non-existent folder, so no plugin assemblies are loaded.
|
|
/// </summary>
|
|
/// <returns>The created configuration.</returns>
|
|
private static IConfiguration CreateConfiguration()
|
|
{
|
|
Dictionary<string, string?> configurationValues = new()
|
|
{
|
|
[$"{PluginsSettingsDto.SECTION_NAME}:Directory"] = "non-existent-plugins-directory"
|
|
};
|
|
return new ConfigurationBuilder().AddInMemoryCollection(configurationValues).Build();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Asserts that the provided handler pipeline validates the host of every request, follows redirects only to public HTTPS hosts, and connects only to an
|
|
/// address checked to be public.
|
|
/// </summary>
|
|
/// <param name="handler">The handler pipeline to check.</param>
|
|
private static void AssertUsesThePublicHostGuards(HttpMessageHandler handler)
|
|
{
|
|
List<DelegatingHandler> delegatingHandlers = GetDelegatingHandlers(handler);
|
|
int validationIndex = delegatingHandlers.FindIndex(delegatingHandler => delegatingHandler is PublicHostValidationHandler);
|
|
int redirectIndex = delegatingHandlers.FindIndex(delegatingHandler => delegatingHandler is PublicHostRedirectHandler);
|
|
// The validation handler must run before the redirect handler, because the redirect handler follows a redirect below the validation handler.
|
|
Assert.True(validationIndex >= 0, "The public host validation handler must be part of the outbound pipeline.");
|
|
Assert.True(redirectIndex > validationIndex, "The public host validation handler must run before the public host redirect handler.");
|
|
SocketsHttpHandler primaryHandler = Assert.IsType<SocketsHttpHandler>(GetPrimaryHandler(handler));
|
|
Assert.False(primaryHandler.AllowAutoRedirect);
|
|
Assert.NotNull(primaryHandler.ConnectCallback);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets the delegating handlers of the provided handler pipeline, from the outermost to the innermost one.
|
|
/// </summary>
|
|
/// <param name="handler">The handler pipeline to walk.</param>
|
|
/// <returns>The delegating handlers of the pipeline.</returns>
|
|
private static List<DelegatingHandler> GetDelegatingHandlers(HttpMessageHandler handler)
|
|
{
|
|
List<DelegatingHandler> delegatingHandlers = [];
|
|
HttpMessageHandler currentHandler = handler;
|
|
while (currentHandler is DelegatingHandler delegatingHandler)
|
|
{
|
|
delegatingHandlers.Add(delegatingHandler);
|
|
currentHandler = delegatingHandler.InnerHandler ?? throw new InvalidOperationException("The delegating handler has no inner handler.");
|
|
}
|
|
return delegatingHandlers;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets the primary, non delegating handler of the provided handler pipeline.
|
|
/// </summary>
|
|
/// <param name="handler">The handler pipeline to walk.</param>
|
|
/// <returns>The primary handler of the pipeline.</returns>
|
|
private static HttpMessageHandler GetPrimaryHandler(HttpMessageHandler handler)
|
|
{
|
|
HttpMessageHandler currentHandler = handler;
|
|
while (currentHandler is DelegatingHandler delegatingHandler)
|
|
currentHandler = delegatingHandler.InnerHandler ?? throw new InvalidOperationException("The delegating handler has no inner handler.");
|
|
return currentHandler;
|
|
}
|
|
}
|