rc-migration-tests/vb-migration/Strata.Base.Internal.Tests/Security/SecurityUtilsTests.cs

108 lines
3.2 KiB
C#

using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Security.Cryptography;
using System;
namespace Strata.Base.Internal.Tests.Security
{
[TestClass]
public class SecurityUtilsTests
{
[TestMethod]
public void EncryptValue_WithValidInput_EncryptsAndDecryptsCorrectly()
{
// Arrange
string originalValue = "test value";
string key = "testkey12345678"; // AES requires at least a 128-bit (16-byte) key
// Act
string encryptedValue = SecurityUtils.EncryptValue(originalValue, key);
string decryptedValue = SecurityUtils.DecryptValue(encryptedValue, key);
// Assert
Assert.AreEqual(originalValue, decryptedValue);
}
[TestMethod]
public void DecryptValue_WithWrongKey_ThrowsException()
{
// Arrange
string originalValue = "test value";
string correctKey = "testkey12345678"; // AES requires at least a 128-bit (16-byte) key
string wrongKey = "wrongkey12345678";
// Act
string encryptedValue = SecurityUtils.EncryptValue(originalValue, correctKey);
// Assert
Assert.ThrowsException<CryptographicException>(() => SecurityUtils.DecryptValue(encryptedValue, wrongKey));
}
[TestMethod]
public void EncryptValue_WithEmptyString_ReturnsEmptyString()
{
// Arrange
string originalValue = "";
string key = "testkey12345678";
// Act
string encryptedValue = SecurityUtils.EncryptValue(originalValue, key);
// Assert
Assert.AreEqual("", encryptedValue);
}
[TestMethod]
public void DecryptValue_WithEmptyString_ReturnsEmptyString()
{
// Arrange
string encryptedValue = "";
string key = "testkey12345678";
// Act
string decryptedValue = SecurityUtils.DecryptValue(encryptedValue, key);
// Assert
Assert.AreEqual("", decryptedValue);
}
[TestMethod]
public void EncryptValue_WithNullString_ReturnsNull()
{
// Arrange
var originalValue = null as string;
string key = "testkey12345678";
// Act
string encryptedValue = SecurityUtils.EncryptValue(originalValue, key);
// Assert
Assert.IsNull(encryptedValue);
}
[TestMethod]
public void DecryptValue_WithNullString_ReturnsNull()
{
// Arrange
var encryptedValue = null as string;
string key = "testkey12345678";
// Act
string decryptedValue = SecurityUtils.DecryptValue(encryptedValue, key);
// Assert
Assert.IsNull(decryptedValue);
}
[TestMethod]
public void DecryptValue_WithInvalidBase64_ThrowsException()
{
// Arrange
string invalidBase64 = "Not a valid base64 string";
string key = "testkey12345678";
// Assert
Assert.ThrowsException<FormatException>(() => SecurityUtils.DecryptValue(invalidBase64, key));
}
}
}