mirror of
https://github.com/BinaryAlley/Nemera.git
synced 2026-12-02 14:01:11 +00:00
Adds bundled plugin trust and cross-platform native deployment
- 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
This commit is contained in:
1 parent
3fb8912c7f
commit
b30cbd6b9e
114 files changed
+2708
-243
No files matched your search
+15
-1
@@ -26,7 +26,7 @@ public class PluginEntityMappingTests
|
||||
IReadOnlyDictionary<string, string>? expectedSettings = JsonSerializer.Deserialize<Dictionary<string, string>>(entity.SettingsJson!);
|
||||
|
||||
// Act
|
||||
PluginResponse result = entity.ToResponse();
|
||||
PluginResponse result = entity.ToResponse(isOfficial: true);
|
||||
|
||||
// Assert
|
||||
Assert.NotNull(result);
|
||||
@@ -38,6 +38,20 @@ public class PluginEntityMappingTests
|
||||
Assert.Equal(entity.LoadStatus, result.LoadStatus);
|
||||
Assert.Equal(entity.LoadError, result.LoadError);
|
||||
Assert.Equal(expectedSettings, result.Settings);
|
||||
Assert.True(result.IsOfficial);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ToResponse_WhenPluginIsNotOfficial_ShouldSetIsOfficialFalse()
|
||||
{
|
||||
// Arrange
|
||||
PluginEntity entity = _pluginEntityFixture.Create();
|
||||
|
||||
// Act
|
||||
PluginResponse result = entity.ToResponse(isOfficial: false);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsOfficial);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
|
||||
+40
-2
@@ -3,7 +3,7 @@ using Nemera.Application.Common.DataAccess.Entities.Plugins;
|
||||
using Nemera.Application.Common.DataAccess.Repositories.Plugins;
|
||||
using Nemera.Application.Common.DataAccess.UoW;
|
||||
using Nemera.Application.Common.DTO.Filtering;
|
||||
using Nemera.Application.Common.DTO.Pagination;
|
||||
using Nemera.Application.Common.Infrastructure.Plugins;
|
||||
using Nemera.Application.Core.Plugins.Queries.GetPlugins;
|
||||
using Nemera.Application.Fixtures.Common.DataAccess.Entities.Plugins;
|
||||
using Nemera.Application.Fixtures.Common.DTO.Pagination;
|
||||
@@ -27,6 +27,7 @@ public class GetPluginsQueryHandlerTests
|
||||
{
|
||||
private readonly IUnitOfWork _mockUnitOfWork;
|
||||
private readonly IPluginRepository _mockPluginRepository;
|
||||
private readonly IPluginManager _mockPluginManager;
|
||||
private readonly GetPluginsQueryHandler _sut;
|
||||
private readonly GetPluginsQueryFixture _getPluginsQueryFixture = new();
|
||||
private readonly PluginEntityFixture _pluginEntityFixture = new();
|
||||
@@ -41,7 +42,8 @@ public class GetPluginsQueryHandlerTests
|
||||
_mockUnitOfWork.SaveChangesAsync(Arg.Any<CancellationToken>()).Returns(Result.Success);
|
||||
_mockPluginRepository = Substitute.For<IPluginRepository>();
|
||||
_mockUnitOfWork.PluginRepository.Returns(_mockPluginRepository);
|
||||
_sut = new GetPluginsQueryHandler(_mockUnitOfWork);
|
||||
_mockPluginManager = Substitute.For<IPluginManager>();
|
||||
_sut = new GetPluginsQueryHandler(_mockUnitOfWork, _mockPluginManager);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -62,6 +64,42 @@ public class GetPluginsQueryHandlerTests
|
||||
Assert.Equal(plugins[0].Id, result.Value[0].Id);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task HandleAsync_WhenPluginIsOfficial_ShouldReturnItAsOfficial()
|
||||
{
|
||||
// Arrange
|
||||
PluginEntity plugin = _pluginEntityFixture.Create();
|
||||
_mockPluginRepository.GetAllAsync<BaseFilterDto>(cancellationToken: Arg.Any<CancellationToken>())
|
||||
.Returns(Result.From(_paginatedResultDtoFixture.Create(data: [plugin], currentPage: 1, perPage: 1, count: 1, numberOfPages: 1)));
|
||||
_mockPluginManager.IsOfficial(plugin.Id).Returns(true);
|
||||
|
||||
// Act
|
||||
Result<IReadOnlyList<PluginResponse>> result = await _sut.HandleAsync(_getPluginsQueryFixture.Create(), CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
PluginResponse response = Assert.Single(result.Value);
|
||||
Assert.True(response.IsOfficial);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task HandleAsync_WhenPluginIsNotOfficial_ShouldReturnItAsNotOfficial()
|
||||
{
|
||||
// Arrange
|
||||
PluginEntity plugin = _pluginEntityFixture.Create();
|
||||
_mockPluginRepository.GetAllAsync<BaseFilterDto>(cancellationToken: Arg.Any<CancellationToken>())
|
||||
.Returns(Result.From(_paginatedResultDtoFixture.Create(data: [plugin], currentPage: 1, perPage: 1, count: 1, numberOfPages: 1)));
|
||||
_mockPluginManager.IsOfficial(plugin.Id).Returns(false);
|
||||
|
||||
// Act
|
||||
Result<IReadOnlyList<PluginResponse>> result = await _sut.HandleAsync(_getPluginsQueryFixture.Create(), CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
PluginResponse response = Assert.Single(result.Value);
|
||||
Assert.False(response.IsOfficial);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task HandleAsync_WhenRepositoryReturnsError_ShouldReturnError()
|
||||
{
|
||||
|
||||
+8
-6
@@ -1,4 +1,7 @@
|
||||
#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;
|
||||
@@ -18,9 +21,6 @@ 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 Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
@@ -35,6 +35,8 @@ namespace Nemera.Infrastructure.UnitTests.Common.DependencyInjection;
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class InfrastructureLayerServicesTests
|
||||
{
|
||||
private static readonly IReadOnlyDictionary<string, IReadOnlySet<string>> s_emptyBundledPluginChecksums = new Dictionary<string, IReadOnlySet<string>>();
|
||||
|
||||
[Fact]
|
||||
public void AddInfrastructureLayerServices_WhenCalled_ShouldRegisterCoreInfrastructureServices()
|
||||
{
|
||||
@@ -43,7 +45,7 @@ public class InfrastructureLayerServicesTests
|
||||
IConfiguration configuration = CreateConfiguration();
|
||||
|
||||
// Act
|
||||
services.AddInfrastructureLayerServices(configuration);
|
||||
services.AddInfrastructureLayerServices(configuration, s_emptyBundledPluginChecksums);
|
||||
|
||||
// Assert
|
||||
Assert.Contains(services, descriptor => descriptor.ServiceType == typeof(IFileHashService) && descriptor.ImplementationType == typeof(FileHashService));
|
||||
@@ -68,7 +70,7 @@ public class InfrastructureLayerServicesTests
|
||||
IConfiguration configuration = CreateConfiguration();
|
||||
|
||||
// Act
|
||||
services.AddInfrastructureLayerServices(configuration);
|
||||
services.AddInfrastructureLayerServices(configuration, s_emptyBundledPluginChecksums);
|
||||
|
||||
// Assert
|
||||
Assert.Contains(services, descriptor => descriptor.ServiceType == typeof(IValidator<PluginsSettingsDto>));
|
||||
@@ -82,7 +84,7 @@ public class InfrastructureLayerServicesTests
|
||||
// Arrange
|
||||
IServiceCollection services = new ServiceCollection();
|
||||
IConfiguration configuration = CreateConfiguration();
|
||||
services.AddInfrastructureLayerServices(configuration);
|
||||
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();
|
||||
|
||||
+26
-7
@@ -1,11 +1,13 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Nemera.Application.Common.DataAccess.Entities.MediaLibrary.Management;
|
||||
using Nemera.Application.Common.DataAccess.Entities.Plugins;
|
||||
using Nemera.Application.Common.DataAccess.Repositories.MediaLibrary;
|
||||
using Nemera.Application.Common.DataAccess.Repositories.Plugins;
|
||||
using Nemera.Application.Common.DataAccess.UoW;
|
||||
using Nemera.Application.Common.DTO.Filtering;
|
||||
using Nemera.Application.Common.DTO.Pagination;
|
||||
using Nemera.Application.Common.Infrastructure.Plugins;
|
||||
using Nemera.Application.Fixtures.Common.DataAccess.Entities.MediaLibrary.Management;
|
||||
using Nemera.Application.Fixtures.Common.DataAccess.Entities.Plugins;
|
||||
@@ -17,13 +19,9 @@ using Nemera.Infrastructure.Common.Models.DTO.Plugins;
|
||||
using Nemera.Infrastructure.Core.Plugins;
|
||||
using Nemera.Infrastructure.Fixtures.Common.Models.DTO.Plugins;
|
||||
using Nemera.Plugins.Contracts.Core.Plugins;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using NSubstitute;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
@@ -149,6 +147,26 @@ public class PluginDetectionSyncJobTests
|
||||
Arg.Any<Func<object, Exception?, string>>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task StartAsync_WhenWarningsExist_ShouldLogThemAsWarnings()
|
||||
{
|
||||
// Arrange
|
||||
_mockLogger.IsEnabled(Arg.Any<LogLevel>()).Returns(true);
|
||||
_mockPluginManager.GetPlugins().Returns([]);
|
||||
PluginDetectionSyncJob sut = CreateSut(warnings: ["The SHA-256 checksum of plugin assembly 'LockedPlugin.dll' could not be computed."]);
|
||||
|
||||
// Act
|
||||
await StartAndWaitForExecutionAsync(sut);
|
||||
|
||||
// Assert
|
||||
_mockLogger.Received(1).Log(
|
||||
LogLevel.Warning,
|
||||
Arg.Any<EventId>(),
|
||||
Arg.Is<object>(state => state.ToString()!.Contains("Plugin warning")),
|
||||
Arg.Any<Exception?>(),
|
||||
Arg.Any<Func<object, Exception?, string>>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task StartAsync_WhenNoPluginsAreLoaded_ShouldOnlySaveChanges()
|
||||
{
|
||||
@@ -287,10 +305,11 @@ public class PluginDetectionSyncJobTests
|
||||
/// Creates the system under test wired to the mocked dependencies.
|
||||
/// </summary>
|
||||
/// <param name="loadErrors">The plugin load errors to feed to the job, when any.</param>
|
||||
/// <param name="warnings">The plugin load warnings to feed to the job, when any.</param>
|
||||
/// <returns>The created job instance.</returns>
|
||||
private PluginDetectionSyncJob CreateSut(IReadOnlyList<PluginLoadErrorDto>? loadErrors = null)
|
||||
private PluginDetectionSyncJob CreateSut(IReadOnlyList<PluginLoadErrorDto>? loadErrors = null, IReadOnlyList<string>? warnings = null)
|
||||
{
|
||||
return new PluginDetectionSyncJob(_mockServiceScopeFactory, _mockPluginManager, loadErrors ?? [], _mockLogger);
|
||||
return new PluginDetectionSyncJob(_mockServiceScopeFactory, _mockPluginManager, loadErrors ?? [], warnings ?? [], _mockLogger);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -15,16 +15,18 @@ namespace Nemera.Infrastructure.UnitTests.Core.Plugins;
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class PluginManagerTests
|
||||
{
|
||||
private PluginManager _sut = new([], [], new HashSet<Guid>());
|
||||
|
||||
[Fact]
|
||||
public void GetPlugins_WhenCalled_ShouldReturnAllLoadedPlugins()
|
||||
{
|
||||
// Arrange
|
||||
IPlugin firstPlugin = CreatePlugin(Guid.NewGuid(), "First Plugin");
|
||||
IPlugin secondPlugin = CreatePlugin(Guid.NewGuid(), "Second Plugin");
|
||||
PluginManager sut = new([firstPlugin, secondPlugin], []);
|
||||
_sut = new([firstPlugin, secondPlugin], [], new HashSet<Guid>());
|
||||
|
||||
// Act
|
||||
IReadOnlyList<IPlugin> result = sut.GetPlugins();
|
||||
IReadOnlyList<IPlugin> result = _sut.GetPlugins();
|
||||
|
||||
// Assert
|
||||
Assert.Equal(2, result.Count);
|
||||
@@ -36,10 +38,10 @@ public class PluginManagerTests
|
||||
public void GetPlugins_WhenNoPluginsWereLoaded_ShouldReturnEmptyList()
|
||||
{
|
||||
// Arrange
|
||||
PluginManager sut = new([], []);
|
||||
_sut = new([], [], new HashSet<Guid>());
|
||||
|
||||
// Act
|
||||
IReadOnlyList<IPlugin> result = sut.GetPlugins();
|
||||
IReadOnlyList<IPlugin> result = _sut.GetPlugins();
|
||||
|
||||
// Assert
|
||||
Assert.Empty(result);
|
||||
@@ -51,10 +53,10 @@ public class PluginManagerTests
|
||||
// Arrange
|
||||
Guid pluginId = Guid.NewGuid();
|
||||
IPlugin expectedPlugin = CreatePlugin(pluginId, "Expected Plugin");
|
||||
PluginManager sut = new([expectedPlugin, CreatePlugin(Guid.NewGuid(), "Other Plugin")], []);
|
||||
_sut = new([expectedPlugin, CreatePlugin(Guid.NewGuid(), "Other Plugin")], [], new HashSet<Guid>());
|
||||
|
||||
// Act
|
||||
IPlugin? result = sut.GetPlugin(pluginId);
|
||||
IPlugin? result = _sut.GetPlugin(pluginId);
|
||||
|
||||
// Assert
|
||||
Assert.Same(expectedPlugin, result);
|
||||
@@ -64,15 +66,43 @@ public class PluginManagerTests
|
||||
public void GetPlugin_WhenPluginWithIdWasNotLoaded_ShouldReturnNull()
|
||||
{
|
||||
// Arrange
|
||||
PluginManager sut = new([CreatePlugin(Guid.NewGuid(), "Loaded Plugin")], []);
|
||||
_sut = new([CreatePlugin(Guid.NewGuid(), "Loaded Plugin")], [], new HashSet<Guid>());
|
||||
|
||||
// Act
|
||||
IPlugin? result = sut.GetPlugin(Guid.NewGuid());
|
||||
IPlugin? result = _sut.GetPlugin(Guid.NewGuid());
|
||||
|
||||
// Assert
|
||||
Assert.Null(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void IsOfficial_WhenPluginIsInTheOfficialSet_ShouldReturnTrue()
|
||||
{
|
||||
// Arrange
|
||||
Guid pluginId = Guid.NewGuid();
|
||||
_sut = new([CreatePlugin(pluginId, "Official Plugin")], [], new HashSet<Guid> { pluginId });
|
||||
|
||||
// Act
|
||||
bool result = _sut.IsOfficial(pluginId);
|
||||
|
||||
// Assert
|
||||
Assert.True(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void IsOfficial_WhenPluginIsNotInTheOfficialSet_ShouldReturnFalse()
|
||||
{
|
||||
// Arrange
|
||||
Guid pluginId = Guid.NewGuid();
|
||||
_sut = new([CreatePlugin(pluginId, "Unofficial Plugin")], [], new HashSet<Guid>());
|
||||
|
||||
// Act
|
||||
bool result = _sut.IsOfficial(pluginId);
|
||||
|
||||
// Assert
|
||||
Assert.False(result);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a mocked plugin with the provided identity.
|
||||
/// </summary>
|
||||
|
||||
+241
@@ -0,0 +1,241 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Nemera.Presentation.Api.Core.Plugins;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
#endregion
|
||||
|
||||
namespace Nemera.Presentation.Api.UnitTests.Core.Plugins;
|
||||
|
||||
/// <summary>
|
||||
/// Contains unit tests for the <see cref="BundledPluginManifestReader"/> class.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class BundledPluginManifestReaderTests
|
||||
{
|
||||
/// <summary>
|
||||
/// The name of the bundled plugin assembly used by the tests.
|
||||
/// </summary>
|
||||
private const string ASSEMBLY_FILE_NAME = "Nemera.Plugins.Epub.dll";
|
||||
|
||||
/// <summary>
|
||||
/// The name of another bundled plugin assembly used by the tests.
|
||||
/// </summary>
|
||||
private const string OTHER_ASSEMBLY_FILE_NAME = "Nemera.Plugins.Pdf.dll";
|
||||
|
||||
/// <summary>
|
||||
/// A valid SHA-256 checksum used by the tests.
|
||||
/// </summary>
|
||||
private const string CHECKSUM = "0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF";
|
||||
|
||||
/// <summary>
|
||||
/// Another valid SHA-256 checksum used by the tests.
|
||||
/// </summary>
|
||||
private const string OTHER_CHECKSUM = "FEDCBA9876543210FEDCBA9876543210FEDCBA9876543210FEDCBA9876543210";
|
||||
|
||||
[Fact]
|
||||
public void ReadExpectedChecksums_WhenManifestContainsCommentsAndBlankLines_ShouldIgnoreThem()
|
||||
{
|
||||
// Arrange
|
||||
string manifest = string.Join(Environment.NewLine, "# Bundled plugin trust list.", string.Empty, $" {ASSEMBLY_FILE_NAME} | {CHECKSUM} ");
|
||||
|
||||
// Act
|
||||
IReadOnlyDictionary<string, IReadOnlySet<string>> actualChecksums = ReadHistoricalManifest(manifest);
|
||||
|
||||
// Assert
|
||||
KeyValuePair<string, IReadOnlySet<string>> entry = Assert.Single(actualChecksums);
|
||||
Assert.Equal(ASSEMBLY_FILE_NAME, entry.Key);
|
||||
Assert.True(entry.Value.SetEquals([CHECKSUM]));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ReadExpectedChecksums_WhenAssemblyIsListedMultipleTimes_ShouldMergeItsChecksums()
|
||||
{
|
||||
// Arrange
|
||||
string manifest = string.Join(Environment.NewLine,
|
||||
$"{ASSEMBLY_FILE_NAME}|{CHECKSUM}",
|
||||
// The second line uses the uppercase spelling to prove that assembly names are merged case-insensitively.
|
||||
$"{ASSEMBLY_FILE_NAME.ToUpperInvariant()}|{OTHER_CHECKSUM}");
|
||||
|
||||
// Act
|
||||
IReadOnlyDictionary<string, IReadOnlySet<string>> actualChecksums = ReadHistoricalManifest(manifest);
|
||||
|
||||
// Assert
|
||||
KeyValuePair<string, IReadOnlySet<string>> entry = Assert.Single(actualChecksums);
|
||||
Assert.Equal(ASSEMBLY_FILE_NAME, entry.Key);
|
||||
Assert.True(entry.Value.SetEquals([CHECKSUM, OTHER_CHECKSUM]));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ReadExpectedChecksums_WhenCurrentManifestIsProvided_ShouldMergeBothManifests()
|
||||
{
|
||||
// Arrange
|
||||
string historicalManifest = $"{ASSEMBLY_FILE_NAME}|{CHECKSUM}";
|
||||
string currentManifest = $"{OTHER_ASSEMBLY_FILE_NAME}|{OTHER_CHECKSUM}";
|
||||
|
||||
// Act
|
||||
IReadOnlyDictionary<string, IReadOnlySet<string>> actualChecksums = ReadManifests(historicalManifest, currentManifest);
|
||||
|
||||
// Assert
|
||||
Assert.Equal(2, actualChecksums.Count);
|
||||
Assert.True(actualChecksums[ASSEMBLY_FILE_NAME].SetEquals([CHECKSUM]));
|
||||
Assert.True(actualChecksums[OTHER_ASSEMBLY_FILE_NAME].SetEquals([OTHER_CHECKSUM]));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ReadExpectedChecksums_WhenDenyManifestRevokesOneChecksum_ShouldKeepTheOther()
|
||||
{
|
||||
// Arrange
|
||||
string historicalManifest = string.Join(Environment.NewLine,
|
||||
$"{ASSEMBLY_FILE_NAME}|{CHECKSUM}",
|
||||
$"{ASSEMBLY_FILE_NAME}|{OTHER_CHECKSUM}");
|
||||
string denyManifest = $"{ASSEMBLY_FILE_NAME}|{CHECKSUM}";
|
||||
|
||||
// Act
|
||||
IReadOnlyDictionary<string, IReadOnlySet<string>> actualChecksums = ReadManifests(historicalManifest, string.Empty, denyManifest);
|
||||
|
||||
// Assert
|
||||
KeyValuePair<string, IReadOnlySet<string>> entry = Assert.Single(actualChecksums);
|
||||
Assert.True(entry.Value.SetEquals([OTHER_CHECKSUM]));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ReadExpectedChecksums_WhenDenyManifestRevokesEveryChecksum_ShouldRemoveTheAssembly()
|
||||
{
|
||||
// Arrange
|
||||
string historicalManifest = $"{ASSEMBLY_FILE_NAME}|{CHECKSUM}";
|
||||
string denyManifest = $"{ASSEMBLY_FILE_NAME}|{CHECKSUM}";
|
||||
|
||||
// Act
|
||||
IReadOnlyDictionary<string, IReadOnlySet<string>> actualChecksums = ReadManifests(historicalManifest, string.Empty, denyManifest);
|
||||
|
||||
// Assert
|
||||
Assert.Empty(actualChecksums);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ReadExpectedChecksums_WhenDenyManifestTargetsAnotherAssembly_ShouldKeepTheTrustedAssembly()
|
||||
{
|
||||
// Arrange
|
||||
string historicalManifest = $"{ASSEMBLY_FILE_NAME}|{CHECKSUM}";
|
||||
string denyManifest = $"{OTHER_ASSEMBLY_FILE_NAME}|{OTHER_CHECKSUM}";
|
||||
|
||||
// Act
|
||||
IReadOnlyDictionary<string, IReadOnlySet<string>> actualChecksums = ReadManifests(historicalManifest, string.Empty, denyManifest);
|
||||
|
||||
// Assert
|
||||
KeyValuePair<string, IReadOnlySet<string>> entry = Assert.Single(actualChecksums);
|
||||
Assert.Equal(ASSEMBLY_FILE_NAME, entry.Key);
|
||||
Assert.True(entry.Value.SetEquals([CHECKSUM]));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ReadExpectedChecksums_WhenDenyManifestRevokesACurrentManifestChecksum_ShouldRemoveIt()
|
||||
{
|
||||
// Arrange
|
||||
string historicalManifest = $"{ASSEMBLY_FILE_NAME}|{CHECKSUM}";
|
||||
string currentManifest = $"{ASSEMBLY_FILE_NAME}|{OTHER_CHECKSUM}";
|
||||
// Revocation must apply to the current release's hashes as well as to historical ones.
|
||||
string denyManifest = $"{ASSEMBLY_FILE_NAME}|{OTHER_CHECKSUM}";
|
||||
|
||||
// Act
|
||||
IReadOnlyDictionary<string, IReadOnlySet<string>> actualChecksums = ReadManifests(historicalManifest, currentManifest, denyManifest);
|
||||
|
||||
// Assert
|
||||
KeyValuePair<string, IReadOnlySet<string>> entry = Assert.Single(actualChecksums);
|
||||
Assert.True(entry.Value.SetEquals([CHECKSUM]));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ReadExpectedChecksums_WhenAssemblyFileNameIsEmpty_ShouldThrowInvalidOperationException()
|
||||
{
|
||||
// Arrange
|
||||
string manifest = $"|{CHECKSUM}";
|
||||
|
||||
// Act & Assert
|
||||
InvalidOperationException exception = Assert.Throws<InvalidOperationException>(() => ReadHistoricalManifest(manifest));
|
||||
Assert.Contains("line 1", exception.Message);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(ASSEMBLY_FILE_NAME)] // Missing the separator and the checksum.
|
||||
[InlineData(ASSEMBLY_FILE_NAME + "|" + CHECKSUM + "|extra")] // More than one separator.
|
||||
public void ReadExpectedChecksums_WhenEntryDoesNotHaveTwoFields_ShouldThrowInvalidOperationException(string manifest)
|
||||
{
|
||||
// Act & Assert
|
||||
InvalidOperationException exception = Assert.Throws<InvalidOperationException>(() => ReadHistoricalManifest(manifest));
|
||||
Assert.Contains("line 1", exception.Message);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(63)] // Truncated checksum.
|
||||
[InlineData(65)] // Checksum with an extra character.
|
||||
public void ReadExpectedChecksums_WhenChecksumDoesNotHave64Characters_ShouldThrowInvalidOperationException(int checksumLength)
|
||||
{
|
||||
// Arrange
|
||||
string checksum = checksumLength < CHECKSUM.Length ? CHECKSUM[..checksumLength] : CHECKSUM + "0";
|
||||
string manifest = $"{ASSEMBLY_FILE_NAME}|{checksum}";
|
||||
|
||||
// Act & Assert
|
||||
InvalidOperationException exception = Assert.Throws<InvalidOperationException>(() => ReadHistoricalManifest(manifest));
|
||||
Assert.Contains("line 1", exception.Message);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ReadExpectedChecksums_WhenChecksumIsNotHexadecimal_ShouldThrowInvalidOperationException()
|
||||
{
|
||||
// Arrange
|
||||
string checksum = new('Z', 64);
|
||||
string manifest = $"{ASSEMBLY_FILE_NAME}|{checksum}";
|
||||
|
||||
// Act & Assert
|
||||
InvalidOperationException exception = Assert.Throws<InvalidOperationException>(() => ReadHistoricalManifest(manifest));
|
||||
Assert.Contains("line 1", exception.Message);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reads a historical trust manifest through the production reader.
|
||||
/// </summary>
|
||||
/// <param name="manifest">The manifest text.</param>
|
||||
/// <returns>The checksums parsed from the manifest.</returns>
|
||||
private static IReadOnlyDictionary<string, IReadOnlySet<string>> ReadHistoricalManifest(string manifest)
|
||||
{
|
||||
using (MemoryStream manifestStream = CreateManifestStream(manifest))
|
||||
return BundledPluginManifestReader.ReadExpectedChecksums(manifestStream);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reads a historical and a current trust manifest through the production reader.
|
||||
/// </summary>
|
||||
/// <param name="historicalManifest">The historical manifest text.</param>
|
||||
/// <param name="currentManifest">The current manifest text.</param>
|
||||
/// <param name="denyManifest">The optional deny manifest text.</param>
|
||||
/// <returns>The checksums parsed from both manifests.</returns>
|
||||
private static IReadOnlyDictionary<string, IReadOnlySet<string>> ReadManifests(string historicalManifest, string currentManifest, string? denyManifest = null)
|
||||
{
|
||||
using (MemoryStream historicalManifestStream = CreateManifestStream(historicalManifest))
|
||||
{
|
||||
using (MemoryStream currentManifestStream = CreateManifestStream(currentManifest))
|
||||
{
|
||||
// A null deny manifest means the test only exercises the historical and current merge.
|
||||
if (denyManifest is null)
|
||||
return BundledPluginManifestReader.ReadExpectedChecksums(historicalManifestStream, currentManifestStream);
|
||||
using (MemoryStream denyManifestStream = CreateManifestStream(denyManifest))
|
||||
return BundledPluginManifestReader.ReadExpectedChecksums(historicalManifestStream, currentManifestStream, denyManifestStream);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a stream that contains the provided trust manifest.
|
||||
/// </summary>
|
||||
/// <param name="manifest">The manifest text.</param>
|
||||
/// <returns>A stream over the UTF-8 encoded manifest text.</returns>
|
||||
private static MemoryStream CreateManifestStream(string manifest)
|
||||
{
|
||||
// The production reader consumes a stream, so the tests use an in-memory UTF-8 stream instead of writing temporary files.
|
||||
return new(Encoding.UTF8.GetBytes(manifest));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user