Add unit tests for RabbitMq and Udp transport servers and clients
Some checks failed
Docs CI / lint-and-preview (push) Has been cancelled
Some checks failed
Docs CI / lint-and-preview (push) Has been cancelled
- Implemented comprehensive unit tests for RabbitMqTransportServer, covering constructor, disposal, connection management, event handlers, and exception handling. - Added configuration tests for RabbitMqTransportServer to validate SSL, durable queues, auto-recovery, and custom virtual host options. - Created unit tests for UdpFrameProtocol, including frame parsing and serialization, header size validation, and round-trip data preservation. - Developed tests for UdpTransportClient, focusing on connection handling, event subscriptions, and exception scenarios. - Established tests for UdpTransportServer, ensuring proper start/stop behavior, connection state management, and event handling. - Included tests for UdpTransportOptions to verify default values and modification capabilities. - Enhanced service registration tests for Udp transport services in the dependency injection container.
This commit is contained in:
@@ -1,7 +1,8 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Security.Cryptography;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
using System.Linq;
|
||||
using StellaOps.Cryptography;
|
||||
|
||||
namespace StellaOps.Replay.Core;
|
||||
|
||||
@@ -10,24 +11,38 @@ namespace StellaOps.Replay.Core;
|
||||
/// </summary>
|
||||
public static class DeterministicHash
|
||||
{
|
||||
public static string Sha256Hex(ReadOnlySpan<byte> data)
|
||||
public static string Sha256Hex(ICryptoHash cryptoHash, ReadOnlySpan<byte> data)
|
||||
{
|
||||
Span<byte> hash = stackalloc byte[32];
|
||||
if (!SHA256.TryHashData(data, hash, out _))
|
||||
{
|
||||
throw new InvalidOperationException("Failed to compute SHA-256 hash.");
|
||||
}
|
||||
|
||||
return Convert.ToHexString(hash).ToLowerInvariant();
|
||||
ArgumentNullException.ThrowIfNull(cryptoHash);
|
||||
return cryptoHash.ComputeHashHexForPurpose(data, HashPurpose.Content);
|
||||
}
|
||||
|
||||
public static string Sha256Hex(string utf8) => Sha256Hex(Encoding.UTF8.GetBytes(utf8));
|
||||
|
||||
public static string MerkleRootHex(IEnumerable<byte[]> leaves)
|
||||
public static string Sha256Hex(ICryptoHash cryptoHash, byte[] data)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(cryptoHash);
|
||||
return cryptoHash.ComputeHashHexForPurpose(data, HashPurpose.Content);
|
||||
}
|
||||
|
||||
public static string Sha256Hex(ICryptoHash cryptoHash, string utf8)
|
||||
=> Sha256Hex(cryptoHash, Encoding.UTF8.GetBytes(utf8));
|
||||
|
||||
public static async ValueTask<string> Sha256HexAsync(ICryptoHash cryptoHash, Stream stream, CancellationToken cancellationToken = default)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(cryptoHash);
|
||||
ArgumentNullException.ThrowIfNull(stream);
|
||||
return await cryptoHash.ComputeHashHexForPurposeAsync(stream, HashPurpose.Content, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public static string MerkleRootHex(ICryptoHash cryptoHash, IEnumerable<byte[]> leaves)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(cryptoHash);
|
||||
ArgumentNullException.ThrowIfNull(leaves);
|
||||
|
||||
var currentLevel = leaves.Select(l => l ?? throw new ArgumentNullException(nameof(leaves), "Leaf cannot be null.")).Select(SHA256.HashData).ToList();
|
||||
var currentLevel = leaves
|
||||
.Select(l => l ?? throw new ArgumentNullException(nameof(leaves), "Leaf cannot be null."))
|
||||
.Select(l => cryptoHash.ComputeHashForPurpose(l, HashPurpose.Merkle))
|
||||
.ToList();
|
||||
|
||||
if (currentLevel.Count == 0)
|
||||
{
|
||||
throw new ArgumentException("At least one leaf is required to compute a Merkle root.", nameof(leaves));
|
||||
@@ -45,7 +60,7 @@ public static class DeterministicHash
|
||||
Buffer.BlockCopy(left, 0, combined, 0, left.Length);
|
||||
Buffer.BlockCopy(right, 0, combined, left.Length, right.Length);
|
||||
|
||||
nextLevel.Add(SHA256.HashData(combined));
|
||||
nextLevel.Add(cryptoHash.ComputeHashForPurpose(combined, HashPurpose.Merkle));
|
||||
}
|
||||
|
||||
currentLevel = nextLevel;
|
||||
@@ -54,6 +69,6 @@ public static class DeterministicHash
|
||||
return Convert.ToHexString(currentLevel[0]).ToLowerInvariant();
|
||||
}
|
||||
|
||||
public static string MerkleRootHex(IEnumerable<string> canonicalJsonNodes) =>
|
||||
MerkleRootHex(canonicalJsonNodes.Select(s => Encoding.UTF8.GetBytes(s ?? string.Empty)));
|
||||
public static string MerkleRootHex(ICryptoHash cryptoHash, IEnumerable<string> canonicalJsonNodes)
|
||||
=> MerkleRootHex(cryptoHash, canonicalJsonNodes.Select(s => Encoding.UTF8.GetBytes(s ?? string.Empty)));
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using StellaOps.Cryptography;
|
||||
|
||||
namespace StellaOps.Replay.Core;
|
||||
|
||||
@@ -7,7 +8,11 @@ public sealed record DsseSignature(string KeyId, string Sig);
|
||||
|
||||
public sealed record DsseEnvelope(string PayloadType, string Payload, IReadOnlyList<DsseSignature> Signatures)
|
||||
{
|
||||
public string DigestSha256 => DeterministicHash.Sha256Hex(Convert.FromBase64String(Payload));
|
||||
public string ComputeDigestSha256(ICryptoHash cryptoHash)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(cryptoHash);
|
||||
return DeterministicHash.Sha256Hex(cryptoHash, Convert.FromBase64String(Payload));
|
||||
}
|
||||
}
|
||||
|
||||
public static class DssePayloadBuilder
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
using System.Formats.Tar;
|
||||
using System.IO;
|
||||
using System.Security.Cryptography;
|
||||
using StellaOps.Cryptography;
|
||||
using ZstdSharp;
|
||||
|
||||
namespace StellaOps.Replay.Core;
|
||||
@@ -12,12 +12,14 @@ public static class ReplayBundleWriter
|
||||
private const int DefaultBufferSize = 16 * 1024;
|
||||
|
||||
public static async Task<ReplayBundleWriteResult> WriteTarZstAsync(
|
||||
ICryptoHash cryptoHash,
|
||||
IEnumerable<ReplayBundleEntry> entries,
|
||||
Stream destination,
|
||||
int compressionLevel = 19,
|
||||
string? casPrefix = "replay",
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(cryptoHash);
|
||||
ArgumentNullException.ThrowIfNull(entries);
|
||||
ArgumentNullException.ThrowIfNull(destination);
|
||||
|
||||
@@ -32,24 +34,28 @@ public static class ReplayBundleWriter
|
||||
|
||||
var tarBytes = tarBuffer.Length;
|
||||
var tarBytesSpan = tarBuffer.ToArray();
|
||||
var tarDigest = DeterministicHash.Sha256Hex(tarBytesSpan);
|
||||
var tarDigest = DeterministicHash.Sha256Hex(cryptoHash, tarBytesSpan);
|
||||
|
||||
tarBuffer.Position = 0;
|
||||
|
||||
using var sha = SHA256.Create();
|
||||
await using var hashingStream = new CryptoStream(destination, sha, CryptoStreamMode.Write, leaveOpen: true);
|
||||
await using (var compressor = new CompressionStream(hashingStream, compressionLevel, DefaultBufferSize, leaveOpen: true))
|
||||
// Compute compressed stream hash
|
||||
await using var compressedBuffer = new MemoryStream();
|
||||
await using (var compressor = new CompressionStream(compressedBuffer, compressionLevel, DefaultBufferSize, leaveOpen: true))
|
||||
{
|
||||
await tarBuffer.CopyToAsync(compressor, DefaultBufferSize, cancellationToken).ConfigureAwait(false);
|
||||
await compressor.FlushAsync(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
hashingStream.FlushFinalBlock();
|
||||
var zstDigest = Convert.ToHexString(sha.Hash!).ToLowerInvariant();
|
||||
compressedBuffer.Position = 0;
|
||||
var zstDigest = await cryptoHash.ComputeHashHexForPurposeAsync(compressedBuffer, HashPurpose.Content, cancellationToken).ConfigureAwait(false);
|
||||
|
||||
// Write to destination
|
||||
compressedBuffer.Position = 0;
|
||||
await compressedBuffer.CopyToAsync(destination, cancellationToken).ConfigureAwait(false);
|
||||
|
||||
var casUri = BuildCasUri(zstDigest, casPrefix);
|
||||
|
||||
return new ReplayBundleWriteResult(tarDigest, zstDigest, tarBytes, destination.CanSeek ? destination.Position : -1, casUri);
|
||||
return new ReplayBundleWriteResult(tarDigest, zstDigest, tarBytes, compressedBuffer.Length, casUri);
|
||||
}
|
||||
|
||||
private static async Task WriteDeterministicTarAsync(IReadOnlyCollection<ReplayBundleEntry> entries, Stream tarStream, CancellationToken ct)
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using System;
|
||||
using StellaOps.Cryptography;
|
||||
|
||||
namespace StellaOps.Replay.Core;
|
||||
|
||||
@@ -26,10 +27,11 @@ public static class ReplayManifestExtensions
|
||||
return CanonicalJson.SerializeToUtf8Bytes(manifest);
|
||||
}
|
||||
|
||||
public static string ComputeCanonicalSha256(this ReplayManifest manifest)
|
||||
public static string ComputeCanonicalSha256(this ReplayManifest manifest, ICryptoHash cryptoHash)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(manifest);
|
||||
return DeterministicHash.Sha256Hex(manifest.ToCanonicalJson());
|
||||
ArgumentNullException.ThrowIfNull(cryptoHash);
|
||||
return DeterministicHash.Sha256Hex(cryptoHash, manifest.ToCanonicalJson());
|
||||
}
|
||||
|
||||
public static DsseEnvelope ToDsseEnvelope(this ReplayManifest manifest, string? payloadType = null)
|
||||
|
||||
@@ -8,4 +8,7 @@
|
||||
<PackageReference Include="ZstdSharp.Port" Version="0.8.6" />
|
||||
<PackageReference Include="MongoDB.Bson" Version="2.25.0" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\StellaOps.Cryptography\StellaOps.Cryptography.csproj" />
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
|
||||
Reference in New Issue
Block a user