2026-04-01 12:45:57 -05:00
|
|
|
using StevanFreeborn.Extensions.Configuration.Secure.Cryptography;
|
|
|
|
|
|
|
|
|
|
namespace StevanFreeborn.Extensions.Configuration.Secure.Tests.Unit.Cryptography;
|
|
|
|
|
|
|
|
|
|
public class StaticKeyProviderTests
|
|
|
|
|
{
|
|
|
|
|
[Fact]
|
|
|
|
|
public void Constructor_ShouldThrowArgumentNullException_WhenKeyIsNull()
|
|
|
|
|
{
|
|
|
|
|
var act = () => new StaticKeyProvider(null!);
|
|
|
|
|
|
2026-04-02 13:51:25 -05:00
|
|
|
act.Should().Throw<ArgumentException>();
|
2026-04-01 12:45:57 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void Constructor_WhenCalledAndKeyIsNot32Bytes_ItShouldThrowArgumentException()
|
|
|
|
|
{
|
|
|
|
|
var shortKey = Convert.ToBase64String(new byte[16]);
|
|
|
|
|
|
|
|
|
|
var act = () => new StaticKeyProvider(shortKey);
|
|
|
|
|
|
|
|
|
|
act.Should().Throw<ArgumentException>().WithMessage("*must be exactly 32 bytes*");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void GetKey_WhenCalled_ItShouldReturnValid32ByteArray()
|
|
|
|
|
{
|
|
|
|
|
var expectedBytes = new byte[32];
|
|
|
|
|
Random.Shared.NextBytes(expectedBytes);
|
|
|
|
|
var base64Key = Convert.ToBase64String(expectedBytes);
|
|
|
|
|
|
|
|
|
|
var provider = new StaticKeyProvider(base64Key);
|
|
|
|
|
|
|
|
|
|
var result = provider.GetKey();
|
|
|
|
|
|
|
|
|
|
result.Should().BeEquivalentTo(expectedBytes);
|
|
|
|
|
result.Length.Should().Be(32);
|
|
|
|
|
}
|
|
|
|
|
}
|