Files
Heimdall/tests/Heimdall.Infrastructure.Tests/CredentialProtectorTests.cs
T

108 lines
2.9 KiB
C#

using Heimdall.Infrastructure.Security;
using Microsoft.AspNetCore.DataProtection;
using Shouldly;
namespace Heimdall.Infrastructure.Tests;
public class CredentialProtectorTests
{
private static DataProtectorCredentialProtector CreateProtector()
{
var provider = new EphemeralDataProtectionProvider();
return new DataProtectorCredentialProtector(provider);
}
[Fact]
public void EncryptDecrypt_RoundTrip_ReturnsOriginal()
{
var protector = CreateProtector();
const string original = "rtsp://admin:hunter2@192.168.1.100:554/stream1";
var encrypted = protector.Encrypt(original);
var decrypted = protector.Decrypt(encrypted);
decrypted.ShouldBe(original);
encrypted.ShouldNotBe(original);
}
[Fact]
public void EncryptDecrypt_EmptyString_ReturnsEmpty()
{
var protector = CreateProtector();
var encrypted = protector.Encrypt(string.Empty);
var decrypted = protector.Decrypt(string.Empty);
encrypted.ShouldBe(string.Empty);
decrypted.ShouldBe(string.Empty);
}
[Fact]
public void EncryptDecrypt_NullString_ReturnsEmpty()
{
var protector = CreateProtector();
var encrypted = protector.Encrypt(null!);
encrypted.ShouldBe(string.Empty);
}
[Fact]
public void EncryptDecrypt_SpecialCharacters_RoundTrips()
{
var protector = CreateProtector();
const string original = "p@$$w0rd!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~";
var encrypted = protector.Encrypt(original);
var decrypted = protector.Decrypt(encrypted);
decrypted.ShouldBe(original);
}
[Fact]
public void Decrypt_InvalidCiphertext_ReturnsAsIs()
{
var protector = CreateProtector();
var result = protector.Decrypt("not-valid-base64-for-data-protection");
result.ShouldBe("not-valid-base64-for-data-protection");
}
[Fact]
public void EncryptDecrypt_LongPassword_RoundTrips()
{
var protector = CreateProtector();
var original = new string('A', 1000);
var encrypted = protector.Encrypt(original);
var decrypted = protector.Decrypt(encrypted);
decrypted.ShouldBe(original);
}
[Fact]
public void EncryptDecrypt_UnicodeCharacters_RoundTrips()
{
var protector = CreateProtector();
const string original = "héllo wörld 日本語 कैमरा";
var encrypted = protector.Encrypt(original);
var decrypted = protector.Decrypt(encrypted);
decrypted.ShouldBe(original);
}
[Fact]
public void Encrypt_ProducesDifferentCiphertextEachTime()
{
var protector = CreateProtector();
const string original = "same-value";
var encrypted1 = protector.Encrypt(original);
var encrypted2 = protector.Encrypt(original);
encrypted1.ShouldNotBe(encrypted2);
}
}