#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; /// /// Contains unit tests for the class. /// [ExcludeFromCodeCoverage] public class BundledPluginManifestReaderTests { /// /// The name of the bundled plugin assembly used by the tests. /// private const string ASSEMBLY_FILE_NAME = "Nemera.Plugins.Epub.dll"; /// /// The name of another bundled plugin assembly used by the tests. /// private const string OTHER_ASSEMBLY_FILE_NAME = "Nemera.Plugins.Pdf.dll"; /// /// A valid SHA-256 checksum used by the tests. /// private const string CHECKSUM = "0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF"; /// /// Another valid SHA-256 checksum used by the tests. /// 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> actualChecksums = ReadHistoricalManifest(manifest); // Assert KeyValuePair> 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> actualChecksums = ReadHistoricalManifest(manifest); // Assert KeyValuePair> 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> 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> actualChecksums = ReadManifests(historicalManifest, string.Empty, denyManifest); // Assert KeyValuePair> 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> 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> actualChecksums = ReadManifests(historicalManifest, string.Empty, denyManifest); // Assert KeyValuePair> 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> actualChecksums = ReadManifests(historicalManifest, currentManifest, denyManifest); // Assert KeyValuePair> 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(() => 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(() => 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(() => 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(() => ReadHistoricalManifest(manifest)); Assert.Contains("line 1", exception.Message); } /// /// Reads a historical trust manifest through the production reader. /// /// The manifest text. /// The checksums parsed from the manifest. private static IReadOnlyDictionary> ReadHistoricalManifest(string manifest) { using (MemoryStream manifestStream = CreateManifestStream(manifest)) return BundledPluginManifestReader.ReadExpectedChecksums(manifestStream); } /// /// Reads a historical and a current trust manifest through the production reader. /// /// The historical manifest text. /// The current manifest text. /// The optional deny manifest text. /// The checksums parsed from both manifests. private static IReadOnlyDictionary> 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); } } } /// /// Creates a stream that contains the provided trust manifest. /// /// The manifest text. /// A stream over the UTF-8 encoded manifest text. 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)); } }