58 lines
1.9 KiB
C#
58 lines
1.9 KiB
C#
using System.Security.Cryptography;
|
|
using System.Text;
|
|
using StellaOps.Cryptography;
|
|
using StellaOps.Cryptography.Digests;
|
|
using StellaOps.TestKit;
|
|
using Xunit;
|
|
namespace StellaOps.Cryptography.Tests;
|
|
|
|
public sealed class Sha256DigestTests
|
|
{
|
|
[Trait("Category", TestCategories.Unit)]
|
|
[Fact]
|
|
public void Normalize_AllowsBareHex_WhenPrefixNotRequired()
|
|
{
|
|
var hex = new string('a', Sha256Digest.HexLength);
|
|
Assert.Equal($"sha256:{hex}", Sha256Digest.Normalize(hex));
|
|
}
|
|
|
|
[Trait("Category", TestCategories.Unit)]
|
|
[Fact]
|
|
public void Normalize_NormalizesPrefixAndHexToLower()
|
|
{
|
|
var hexUpper = new string('A', Sha256Digest.HexLength);
|
|
Assert.Equal(
|
|
$"sha256:{new string('a', Sha256Digest.HexLength)}",
|
|
Sha256Digest.Normalize($"SHA256:{hexUpper}"));
|
|
}
|
|
|
|
[Trait("Category", TestCategories.Unit)]
|
|
[Fact]
|
|
public void Normalize_RequiresPrefix_WhenConfigured()
|
|
{
|
|
var hex = new string('a', Sha256Digest.HexLength);
|
|
var ex = Assert.Throws<FormatException>(() => Sha256Digest.Normalize(hex, requirePrefix: true, parameterName: "sbomDigest"));
|
|
Assert.Contains("sbomDigest", ex.Message, StringComparison.Ordinal);
|
|
Assert.Contains("sha256:", ex.Message, StringComparison.Ordinal);
|
|
}
|
|
|
|
[Trait("Category", TestCategories.Unit)]
|
|
[Fact]
|
|
public void ExtractHex_ReturnsLowercaseHex()
|
|
{
|
|
var hexUpper = new string('A', Sha256Digest.HexLength);
|
|
Assert.Equal(new string('a', Sha256Digest.HexLength), Sha256Digest.ExtractHex($"sha256:{hexUpper}"));
|
|
}
|
|
|
|
[Trait("Category", TestCategories.Unit)]
|
|
[Fact]
|
|
public void Compute_UsesCryptoHashStack()
|
|
{
|
|
var hash = CryptoHashFactory.CreateDefault();
|
|
var content = Encoding.UTF8.GetBytes("hello");
|
|
|
|
var expectedHex = Convert.ToHexStringLower(SHA256.HashData(content));
|
|
Assert.Equal($"sha256:{expectedHex}", Sha256Digest.Compute(hash, content));
|
|
}
|
|
}
|