Files
stevanfreeborn.secureconfig/tests/StevanFreeborn.Extensions.Configuration.Secure.Tests/Unit/Cryptography/AesCryptoProviderTests.cs
T

59 lines
1.6 KiB
C#
Raw Normal View History

using System.Security.Cryptography;
2026-03-31 07:15:13 -05:00
using Moq;
using StevanFreeborn.Extensions.Configuration.Secure.Cryptography;
namespace StevanFreeborn.Extensions.Configuration.Secure.Tests.Unit.Cryptography;
public class AesCryptoProviderTests
{
private readonly Mock<IEncryptionKeyProvider> _mockKeyProvider = new();
private readonly AesCryptoProvider _sut;
public AesCryptoProviderTests()
{
var key = new byte[32];
RandomNumberGenerator.Fill(key);
_mockKeyProvider.Setup(static m => m.GetKey()).Returns(key);
2026-03-31 07:15:13 -05:00
_sut = new(_mockKeyProvider.Object);
}
[Fact]
public void EncryptAndDecrypt_WhenCalled_ItShouldReturnOriginalString()
{
var originalText = "SuperDuperSecret";
var encryptedText = _sut.Encrypt(originalText);
var decryptedText = _sut.Decrypt(encryptedText);
encryptedText.Should().NotBe(originalText);
decryptedText.Should().Be(originalText);
}
[Fact]
public void Encrypt_WhenCalledWithSameInputTwitch_ItShouldProduceDifferentCipherText()
{
var plainText = "identicalInput";
var cipherTextOne = _sut.Encrypt(plainText);
var cipherTextTwo = _sut.Encrypt(plainText);
cipherTextOne.Should().NotBe(cipherTextTwo);
}
[Fact]
public void Decrypt_WhenCalledWithTamperedData_ItShouldThrowCrytographicException()
{
var cipherText = _sut.Encrypt("some data");
var rawBytes = Convert.FromBase64String(cipherText);
rawBytes[^1] = (byte)(rawBytes[^1] ^ 0xFF);
var tamperedText = Convert.ToBase64String(rawBytes);
var act = () => _sut.Decrypt(tamperedText);
act.Should().Throw<CryptographicException>();
}
2026-03-31 07:15:13 -05:00
}