Configure test coverage

This commit is contained in:
Jorge Burgos 2025-01-30 22:34:02 -05:00
parent 4da8a0bd04
commit 729b7422de
3 changed files with 101 additions and 31 deletions

View File

@ -1,4 +1,6 @@
using Microsoft.VisualStudio.TestTools.UnitTesting; using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Security.Cryptography;
using System;
namespace Strata.Base.Internal.Tests.Security namespace Strata.Base.Internal.Tests.Security
{ {
@ -9,50 +11,97 @@ namespace Strata.Base.Internal.Tests.Security
public void EncryptValue_WithValidInput_EncryptsAndDecryptsCorrectly() public void EncryptValue_WithValidInput_EncryptsAndDecryptsCorrectly()
{ {
// Arrange // Arrange
string originalValue = "Test sensitive data"; string originalValue = "test value";
string key = "MySecretKey123"; string key = "testkey12345678"; // AES requires at least a 128-bit (16-byte) key
// Act // Act
string encrypted = SecurityUtils.EncryptValue(originalValue, key); string encryptedValue = SecurityUtils.EncryptValue(originalValue, key);
string decrypted = SecurityUtils.DecryptValue(encrypted, key); string decryptedValue = SecurityUtils.DecryptValue(encryptedValue, key);
// Assert // Assert
Assert.AreNotEqual(originalValue, encrypted, "Encrypted value should be different from original"); Assert.AreEqual(originalValue, decryptedValue);
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] [TestMethod]
public void DecryptValue_WithWrongKey_ThrowsException() public void DecryptValue_WithWrongKey_ThrowsException()
{ {
// Arrange // Arrange
string originalValue = "Test sensitive data"; string originalValue = "test value";
string correctKey = "CorrectKey123"; string correctKey = "testkey12345678"; // AES requires at least a 128-bit (16-byte) key
string wrongKey = "WrongKey123"; string wrongKey = "wrongkey12345678";
// Act // Act
string encrypted = SecurityUtils.EncryptValue(originalValue, correctKey); string encryptedValue = SecurityUtils.EncryptValue(originalValue, correctKey);
// Assert // Assert
Assert.ThrowsException<System.Security.Cryptography.CryptographicException>( Assert.ThrowsException<CryptographicException>(() => SecurityUtils.DecryptValue(encryptedValue, wrongKey));
() => SecurityUtils.DecryptValue(encrypted, wrongKey), }
"Decryption with wrong key should throw CryptographicException"
); [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));
} }
} }
} }

View File

@ -14,7 +14,10 @@
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" /> <PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="MSTest.TestAdapter" Version="3.1.1" /> <PackageReference Include="MSTest.TestAdapter" Version="3.1.1" />
<PackageReference Include="MSTest.TestFramework" Version="3.1.1" /> <PackageReference Include="MSTest.TestFramework" Version="3.1.1" />
<PackageReference Include="coverlet.collector" Version="6.0.0" /> <PackageReference Include="coverlet.collector" Version="6.0.4">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>

View File

@ -0,0 +1,18 @@
<?xml version="1.0" encoding="utf-8"?>
<RunSettings>
<DataCollectionRunSettings>
<DataCollectors>
<DataCollector friendlyName="XPlat code coverage">
<Configuration>
<Format>cobertura</Format>
<Include>[Strata.*]*</Include>
<ExcludeByAttribute>ExcludeFromCodeCoverageAttribute</ExcludeByAttribute>
<SingleHit>false</SingleHit>
<UseSourceLink>true</UseSourceLink>
<IncludeTestAssembly>false</IncludeTestAssembly>
<SkipAutoProps>true</SkipAutoProps>
</Configuration>
</DataCollector>
</DataCollectors>
</DataCollectionRunSettings>
</RunSettings>