59 lines
2.1 KiB
C#
59 lines
2.1 KiB
C#
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
|
|
|
namespace Strata.Base.Internal.Tests.Security
|
|
{
|
|
[TestClass]
|
|
public class SecurityUtilsTests
|
|
{
|
|
[TestMethod]
|
|
public void EncryptValue_WithValidInput_EncryptsAndDecryptsCorrectly()
|
|
{
|
|
// Arrange
|
|
string originalValue = "Test sensitive data";
|
|
string key = "MySecretKey123";
|
|
|
|
// Act
|
|
string encrypted = SecurityUtils.EncryptValue(originalValue, key);
|
|
string decrypted = SecurityUtils.DecryptValue(encrypted, key);
|
|
|
|
// Assert
|
|
Assert.AreNotEqual(originalValue, encrypted, "Encrypted value should be different from original");
|
|
Assert.AreEqual(originalValue, decrypted, "Decrypted value should match original");
|
|
}
|
|
|
|
[TestMethod]
|
|
public void EncryptValue_WithEmptyString_HandlesCorrectly()
|
|
{
|
|
// Arrange
|
|
string originalValue = "";
|
|
string key = "MySecretKey123";
|
|
|
|
// Act
|
|
string encrypted = SecurityUtils.EncryptValue(originalValue, key);
|
|
string decrypted = SecurityUtils.DecryptValue(encrypted, key);
|
|
|
|
// Assert
|
|
Assert.AreNotEqual(originalValue, encrypted, "Encrypted value should be different from empty string");
|
|
Assert.AreEqual(originalValue, decrypted, "Decrypted value should be empty string");
|
|
}
|
|
|
|
[TestMethod]
|
|
public void DecryptValue_WithWrongKey_ThrowsException()
|
|
{
|
|
// Arrange
|
|
string originalValue = "Test sensitive data";
|
|
string correctKey = "CorrectKey123";
|
|
string wrongKey = "WrongKey123";
|
|
|
|
// Act
|
|
string encrypted = SecurityUtils.EncryptValue(originalValue, correctKey);
|
|
|
|
// Assert
|
|
Assert.ThrowsException<System.Security.Cryptography.CryptographicException>(
|
|
() => SecurityUtils.DecryptValue(encrypted, wrongKey),
|
|
"Decryption with wrong key should throw CryptographicException"
|
|
);
|
|
}
|
|
}
|
|
}
|