mirror of
https://github.com/BinaryAlley/Nemera.git
synced 2026-12-01 18:57:24 +00:00
Adds per-platform release artifacts and release image validation
This commit is contained in:
1 parent
92ab62011e
commit
68143dff25
19 files changed
+603
-115
No files matched your search
+54
@@ -0,0 +1,54 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using System;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.IO;
|
||||
using System.Reflection.Metadata;
|
||||
using System.Reflection.PortableExecutable;
|
||||
#endregion
|
||||
|
||||
namespace Nemera.Presentation.Api.IntegrationTests.Common.Setup;
|
||||
|
||||
/// <summary>
|
||||
/// Reads an embedded resource from an assembly file without loading the assembly into the current process.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// The published bundled plugin manifest verification inspects publish folders of other architectures, where loading the assembly would fail.
|
||||
/// </remarks>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public static class PublishedAssemblyResourceReader
|
||||
{
|
||||
/// <summary>
|
||||
/// Reads an embedded resource from an assembly file without loading the assembly into the current process.
|
||||
/// </summary>
|
||||
/// <param name="assemblyPath">The absolute path of the assembly file.</param>
|
||||
/// <param name="resourceName">The name of the embedded resource.</param>
|
||||
/// <returns>The raw bytes of the embedded resource.</returns>
|
||||
public static byte[] ReadEmbeddedResource(string assemblyPath, string resourceName)
|
||||
{
|
||||
using (FileStream assemblyStream = File.OpenRead(assemblyPath))
|
||||
{
|
||||
using (PEReader peReader = new(assemblyStream))
|
||||
{
|
||||
// A publish folder without managed metadata means the wrong artifact was passed, so fail instead of reporting a missing resource.
|
||||
if (!peReader.HasMetadata)
|
||||
throw new InvalidOperationException($"The assembly '{assemblyPath}' does not contain managed metadata.");
|
||||
MetadataReader metadataReader = peReader.GetMetadataReader();
|
||||
foreach (ManifestResourceHandle resourceHandle in metadataReader.ManifestResources)
|
||||
{
|
||||
ManifestResource resource = metadataReader.GetManifestResource(resourceHandle);
|
||||
// A resource with a non-null implementation is linked from another file, so its bytes are not stored in this resources directory.
|
||||
if (!metadataReader.StringComparer.Equals(resource.Name, resourceName) || !resource.Implementation.IsNil)
|
||||
continue;
|
||||
// The embedded payloads live in the PE resources directory, and every resource offset is relative to the start of that directory.
|
||||
PEMemoryBlock resourceDirectory = peReader.GetSectionData(peReader.PEHeaders.CorHeader!.ResourcesDirectory.RelativeVirtualAddress);
|
||||
BlobReader resourceReader = resourceDirectory.GetReader((int)resource.Offset, resourceDirectory.Length - (int)resource.Offset);
|
||||
// Every embedded resource is prefixed with a four-byte length before its payload.
|
||||
int resourceLength = resourceReader.ReadInt32();
|
||||
return resourceReader.ReadBytes(resourceLength);
|
||||
}
|
||||
}
|
||||
}
|
||||
// Returning empty bytes would let the manifest verification compare against nothing, so a missing resource is treated as a broken artifact.
|
||||
throw new InvalidOperationException($"The embedded resource '{resourceName}' is missing from '{assemblyPath}'.");
|
||||
}
|
||||
}
|
||||
+43
@@ -0,0 +1,43 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using System;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.IO;
|
||||
#endregion
|
||||
|
||||
namespace Nemera.Presentation.Api.IntegrationTests.Common.Setup;
|
||||
|
||||
/// <summary>
|
||||
/// Contains integration tests for the <see cref="PublishedAssemblyResourceReader"/> test helper.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class PublishedAssemblyResourceReaderTests
|
||||
{
|
||||
/// <summary>
|
||||
/// The resource name of the append-only trust list of previously released bundled plugin binaries.
|
||||
/// </summary>
|
||||
private const string HISTORICAL_MANIFEST_RESOURCE_NAME = "Nemera.Presentation.Api.BundledPlugins.historical.manifest";
|
||||
|
||||
[Fact]
|
||||
public void ReadEmbeddedResource_WhenReadingTheLoadedAssemblyFile_ShouldMatchTheLoadedResourceStream()
|
||||
{
|
||||
// Arrange
|
||||
// The loaded assembly's file is read directly, which is the same path the published artifact verification uses.
|
||||
string assemblyPath = typeof(Program).Assembly.Location;
|
||||
byte[] expectedResourceBytes;
|
||||
using (Stream resourceStream = typeof(Program).Assembly.GetManifestResourceStream(HISTORICAL_MANIFEST_RESOURCE_NAME)
|
||||
?? throw new InvalidOperationException($"The embedded manifest resource '{HISTORICAL_MANIFEST_RESOURCE_NAME}' is missing."))
|
||||
{
|
||||
using (MemoryStream resourceBuffer = new())
|
||||
{
|
||||
resourceStream.CopyTo(resourceBuffer);
|
||||
expectedResourceBytes = resourceBuffer.ToArray();
|
||||
}
|
||||
}
|
||||
|
||||
// Act
|
||||
byte[] actualResourceBytes = PublishedAssemblyResourceReader.ReadEmbeddedResource(assemblyPath, HISTORICAL_MANIFEST_RESOURCE_NAME);
|
||||
|
||||
// Assert
|
||||
Assert.Equal(expectedResourceBytes, actualResourceBytes);
|
||||
}
|
||||
}
|
||||
+22
-35
@@ -7,8 +7,6 @@ using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Runtime.Loader;
|
||||
using System.Xml.Linq;
|
||||
#endregion
|
||||
|
||||
@@ -217,41 +215,30 @@ public class BundledPluginManifestReaderTests
|
||||
?? throw new InvalidOperationException($"The environment variable '{PUBLISH_DIRECTORY_ENVIRONMENT_VARIABLE}' is not set.");
|
||||
string publishedApiAssemblyPath = BundledPluginManifestTestPaths.GetPublishedApiAssemblyPath(publishDirectory);
|
||||
string publishedPluginsDirectory = BundledPluginManifestTestPaths.GetPublishedPluginsDirectory(publishDirectory);
|
||||
// Load the published API assembly in its own context, so the test reads the exact shipped binary without loading it into the test process.
|
||||
AssemblyLoadContext loadContext = new("PublishedBundledPluginManifestVerification", isCollectible: true);
|
||||
try
|
||||
// Read the resource directly from the file. Loading the assembly would fail for a runtime whose architecture differs from the test process.
|
||||
byte[] publishedManifestBytes = PublishedAssemblyResourceReader.ReadEmbeddedResource(publishedApiAssemblyPath, CURRENT_MANIFEST_RESOURCE_NAME);
|
||||
using (MemoryStream manifestStream = new(publishedManifestBytes))
|
||||
{
|
||||
Assembly publishedApiAssembly = loadContext.LoadFromAssemblyPath(publishedApiAssemblyPath);
|
||||
// The published assembly must embed the current manifest. A missing resource means the official publish step did not run.
|
||||
using (Stream manifestStream = publishedApiAssembly.GetManifestResourceStream(CURRENT_MANIFEST_RESOURCE_NAME)
|
||||
?? throw new InvalidOperationException("The published API assembly does not embed the current bundled plugin manifest."))
|
||||
{
|
||||
// Act
|
||||
IReadOnlyDictionary<string, IReadOnlySet<string>> publishedManifestEntries = BundledPluginManifestReader.ReadExpectedChecksums(manifestStream);
|
||||
// Act
|
||||
IReadOnlyDictionary<string, IReadOnlySet<string>> publishedManifestEntries = BundledPluginManifestReader.ReadExpectedChecksums(manifestStream);
|
||||
|
||||
// Assert
|
||||
Assert.NotEmpty(publishedManifestEntries);
|
||||
// The manifest and the published plugin folder must agree exactly: every entry has a file, and the counts match.
|
||||
string[] publishedPluginAssemblyFileNames = [.. Directory.EnumerateFiles(publishedPluginsDirectory, BundledPluginManifestTestPaths.PLUGIN_ASSEMBLY_SEARCH_PATTERN)
|
||||
.Select(Path.GetFileName)
|
||||
.OfType<string>()];
|
||||
Assert.Equal(publishedManifestEntries.Count, publishedPluginAssemblyFileNames.Length);
|
||||
foreach (KeyValuePair<string, IReadOnlySet<string>> entry in publishedManifestEntries)
|
||||
{
|
||||
Assert.Contains(entry.Key, publishedPluginAssemblyFileNames, StringComparer.OrdinalIgnoreCase);
|
||||
string publishedAssemblyPath = Path.Combine(publishedPluginsDirectory, entry.Key);
|
||||
Assert.True(File.Exists(publishedAssemblyPath), $"The published plugin assembly '{entry.Key}' is missing from '{publishedPluginsDirectory}'.");
|
||||
string actualSha256 = _bundledPluginFileHashFixture.Create(publishedAssemblyPath);
|
||||
string expectedSha256 = Assert.Single(entry.Value);
|
||||
Assert.Equal(expectedSha256, actualSha256, ignoreCase: true);
|
||||
}
|
||||
// Assert
|
||||
Assert.NotEmpty(publishedManifestEntries);
|
||||
// The manifest and the published plugin folder must agree exactly: every entry has a file, and the counts match.
|
||||
string[] publishedPluginAssemblyFileNames = [.. Directory.EnumerateFiles(publishedPluginsDirectory, BundledPluginManifestTestPaths.PLUGIN_ASSEMBLY_SEARCH_PATTERN)
|
||||
.Select(Path.GetFileName)
|
||||
.OfType<string>()];
|
||||
Assert.Equal(publishedManifestEntries.Count, publishedPluginAssemblyFileNames.Length);
|
||||
foreach (KeyValuePair<string, IReadOnlySet<string>> entry in publishedManifestEntries)
|
||||
{
|
||||
Assert.Contains(entry.Key, publishedPluginAssemblyFileNames, StringComparer.OrdinalIgnoreCase);
|
||||
string publishedAssemblyPath = Path.Combine(publishedPluginsDirectory, entry.Key);
|
||||
Assert.True(File.Exists(publishedAssemblyPath), $"The published plugin assembly '{entry.Key}' is missing from '{publishedPluginsDirectory}'.");
|
||||
string actualSha256 = _bundledPluginFileHashFixture.Create(publishedAssemblyPath);
|
||||
string expectedSha256 = Assert.Single(entry.Value);
|
||||
Assert.Equal(expectedSha256, actualSha256, ignoreCase: true);
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
// Unload the collectible context so the published assembly does not stay locked for later test runs.
|
||||
loadContext.Unload();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -285,8 +272,8 @@ public class BundledPluginManifestReaderTests
|
||||
{
|
||||
using (Stream stream = typeof(Program).Assembly.GetManifestResourceStream(resourceName)
|
||||
?? throw new InvalidOperationException($"The embedded manifest resource '{resourceName}' is missing."))
|
||||
using (StreamReader reader = new(stream))
|
||||
return reader.ReadToEnd();
|
||||
using (StreamReader reader = new(stream))
|
||||
return reader.ReadToEnd();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
Reference in new issue
Block a user