Add tests for SBOM generation determinism across multiple formats
- Created `StellaOps.TestKit.Tests` project for unit tests related to determinism. - Implemented `DeterminismManifestTests` to validate deterministic output for canonical bytes and strings, file read/write operations, and error handling for invalid schema versions. - Added `SbomDeterminismTests` to ensure identical inputs produce consistent SBOMs across SPDX 3.0.1 and CycloneDX 1.6/1.7 formats, including parallel execution tests. - Updated project references in `StellaOps.Integration.Determinism` to include the new determinism testing library.
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace StellaOps.Canonical.Json;
|
||||
@@ -18,6 +19,33 @@ namespace StellaOps.Canonical.Json;
|
||||
/// </remarks>
|
||||
public static class CanonJson
|
||||
{
|
||||
/// <summary>
|
||||
/// Serializes an object to a canonical JSON string.
|
||||
/// Object keys are recursively sorted using Ordinal comparison.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type to serialize.</typeparam>
|
||||
/// <param name="obj">The object to serialize.</param>
|
||||
/// <returns>Canonical JSON string.</returns>
|
||||
public static string Serialize<T>(T obj)
|
||||
{
|
||||
var bytes = Canonicalize(obj);
|
||||
return Encoding.UTF8.GetString(bytes);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Serializes an object to a canonical JSON string using custom serializer options.
|
||||
/// Object keys are recursively sorted using Ordinal comparison.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type to serialize.</typeparam>
|
||||
/// <param name="obj">The object to serialize.</param>
|
||||
/// <param name="options">JSON serializer options to use for initial serialization.</param>
|
||||
/// <returns>Canonical JSON string.</returns>
|
||||
public static string Serialize<T>(T obj, JsonSerializerOptions options)
|
||||
{
|
||||
var bytes = Canonicalize(obj, options);
|
||||
return Encoding.UTF8.GetString(bytes);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Canonicalizes an object to a deterministic byte array.
|
||||
/// Object keys are recursively sorted using Ordinal comparison.
|
||||
|
||||
Reference in New Issue
Block a user