mirror of
https://github.com/BinaryAlley/Nemera.git
synced 2026-12-03 11:11:10 +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);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user