mirror of
https://github.com/BinaryAlley/Nemera.git
synced 2026-12-03 00:11:10 +00:00
Renames project paths from Lumina to Nemera
This commit is contained in:
1 parent
ce6fddb4c9
commit
0f659a4877
6450 files changed
No files matched your search
+161
@@ -0,0 +1,161 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Infrastructure.Common.Models.DTO.Configuration;
|
||||
using Lumina.Infrastructure.Core.Security;
|
||||
using Lumina.Infrastructure.Fixtures.Core.Security;
|
||||
using Microsoft.Extensions.Options;
|
||||
using System;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Security.Cryptography;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Infrastructure.UnitTests.Core.Security;
|
||||
|
||||
/// <summary>
|
||||
/// Contains unit tests for the <see cref="CryptographyService"/> class.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class CryptographyServiceTests
|
||||
{
|
||||
private readonly CryptographyService _sut;
|
||||
private readonly IOptions<EncryptionSettingsDto> _encryptionSettings;
|
||||
private readonly CryptographyServiceFixture _cryptographyServiceFixture = new();
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="CryptographyServiceTests"/> class.
|
||||
/// </summary>
|
||||
public CryptographyServiceTests()
|
||||
{
|
||||
_encryptionSettings = _cryptographyServiceFixture.CreateEncryptionSettings();
|
||||
_sut = new CryptographyService(_encryptionSettings);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Encrypt_WhenCalledWithValidData_ShouldReturnNonEmptyString()
|
||||
{
|
||||
// Arrange
|
||||
string plaintext = _cryptographyServiceFixture.CreateTestData();
|
||||
|
||||
// Act
|
||||
string result = _sut.Encrypt(plaintext);
|
||||
|
||||
// Assert
|
||||
Assert.NotNull(result);
|
||||
Assert.NotEmpty(result);
|
||||
Assert.NotEqual(plaintext, result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Encrypt_WhenCalledMultipleTimesWithSameInput_ShouldReturnDifferentResults()
|
||||
{
|
||||
// Arrange
|
||||
string plaintext = _cryptographyServiceFixture.CreateTestData();
|
||||
|
||||
// Act
|
||||
string result1 = _sut.Encrypt(plaintext);
|
||||
string result2 = _sut.Encrypt(plaintext);
|
||||
|
||||
// Assert
|
||||
Assert.NotEqual(result1, result2);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(null)]
|
||||
[InlineData("")]
|
||||
public void Encrypt_WhenInputIsNullOrEmpty_ShouldThrowArgumentException(string? invalidInput)
|
||||
{
|
||||
// Act
|
||||
Action act = () => _sut.Encrypt(invalidInput!);
|
||||
|
||||
// Assert
|
||||
ArgumentException exception = Assert.Throws<ArgumentException>(() => _sut.Encrypt(invalidInput!));
|
||||
Assert.Equal("plaintext", exception.ParamName);
|
||||
Assert.StartsWith("Value cannot be null or empty", exception.Message);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Decrypt_WhenCalledWithValidData_ShouldReturnOriginalText()
|
||||
{
|
||||
// Arrange
|
||||
string originalText = _cryptographyServiceFixture.CreateTestData();
|
||||
string encrypted = _sut.Encrypt(originalText);
|
||||
|
||||
// Act
|
||||
string decrypted = _sut.Decrypt(encrypted);
|
||||
|
||||
// Assert
|
||||
Assert.Equal(originalText, decrypted);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(null)]
|
||||
[InlineData("")]
|
||||
public void Decrypt_WhenInputIsNullOrEmpty_ShouldThrowArgumentException(string? invalidInput)
|
||||
{
|
||||
// Act
|
||||
Action act = () => _sut.Decrypt(invalidInput!);
|
||||
|
||||
// Assert
|
||||
ArgumentException exception = Assert.Throws<ArgumentException>(() => _sut.Decrypt(invalidInput!));
|
||||
Assert.Equal("ciphertext", exception.ParamName);
|
||||
Assert.StartsWith("Value cannot be null or empty", exception.Message);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("invalid")]
|
||||
[InlineData("not-base64-!@#")]
|
||||
public void Decrypt_WhenInputIsNotBase64_ShouldThrowFormatException(string invalidInput)
|
||||
{
|
||||
// Act
|
||||
Action act = () => _sut.Decrypt(invalidInput);
|
||||
|
||||
// Assert
|
||||
Assert.Throws<FormatException>(() => _sut.Decrypt(invalidInput));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Decrypt_WhenInputIsTooShort_ShouldThrowArgumentException()
|
||||
{
|
||||
// Arrange
|
||||
string shortCiphertext = Convert.ToBase64String(new byte[8]); // Less than required 16 bytes.
|
||||
|
||||
// Act
|
||||
Action act = () => _sut.Decrypt(shortCiphertext);
|
||||
|
||||
// Assert
|
||||
ArgumentException exception = Assert.Throws<ArgumentException>(() => _sut.Decrypt(shortCiphertext));
|
||||
Assert.Equal("Invalid ciphertext", exception.Message);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Decrypt_WhenInputIsCorrupted_ShouldThrowCryptographicException()
|
||||
{
|
||||
// Arrange
|
||||
string originalText = _cryptographyServiceFixture.CreateTestData();
|
||||
string encrypted = _sut.Encrypt(originalText);
|
||||
|
||||
// Decode the Base64 string, modify one byte, then re-encode.
|
||||
byte[] encryptedBytes = Convert.FromBase64String(encrypted);
|
||||
encryptedBytes[^1] ^= 0xFF; // Flip all bits in the last byte.
|
||||
string corrupted = Convert.ToBase64String(encryptedBytes);
|
||||
|
||||
// Act
|
||||
Action act = () => _sut.Decrypt(corrupted);
|
||||
|
||||
// Assert
|
||||
Assert.Throws<CryptographicException>(() => _sut.Decrypt(corrupted));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Dispose_WhenCalled_ShouldClearKey()
|
||||
{
|
||||
// Arrange
|
||||
string plaintext = _cryptographyServiceFixture.CreateTestData();
|
||||
string encrypted = _sut.Encrypt(plaintext);
|
||||
|
||||
// Act
|
||||
_sut.Dispose();
|
||||
|
||||
// Assert
|
||||
Assert.Throws<CryptographicException>(() => _sut.Decrypt(encrypted));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,157 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Infrastructure.Core.Security;
|
||||
using System;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Infrastructure.UnitTests.Core.Security;
|
||||
|
||||
/// <summary>
|
||||
/// Contains unit tests for the <see cref="PasswordHashService"/> class.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class HashServiceTests
|
||||
{
|
||||
private readonly PasswordHashService _sut = new();
|
||||
|
||||
[Fact]
|
||||
public void HashString_WhenCalled_ShouldReturnNonEmptyHash()
|
||||
{
|
||||
// Arrange
|
||||
string password = "TestPassword123!";
|
||||
|
||||
// Act
|
||||
string result = _sut.HashString(password);
|
||||
|
||||
// Assert
|
||||
Assert.NotNull(result);
|
||||
Assert.NotEmpty(result);
|
||||
Assert.NotEqual(password, result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void HashString_WhenCalledMultipleTimesWithSameInput_ShouldReturnDifferentHashes()
|
||||
{
|
||||
// Arrange
|
||||
string password = "TestPassword123!";
|
||||
|
||||
// Act
|
||||
string hash1 = _sut.HashString(password);
|
||||
string hash2 = _sut.HashString(password);
|
||||
|
||||
// Assert
|
||||
Assert.NotEqual(hash1, hash2);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(" ")]
|
||||
[InlineData("short")]
|
||||
[InlineData("verylongpasswordwithlotsofcharactersbutnostringformatting")]
|
||||
[InlineData("Password123!")]
|
||||
[InlineData("@#$%^&*()")]
|
||||
public void HashString_WhenCalledWithDifferentInputs_ShouldGenerateValidHashes(string input)
|
||||
{
|
||||
// Act
|
||||
string hash = _sut.HashString(input);
|
||||
|
||||
// Assert
|
||||
Assert.NotNull(hash);
|
||||
Assert.NotEmpty(hash);
|
||||
// Verify it's a valid Base64 string.
|
||||
Assert.NotNull(Convert.FromBase64String(hash));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CheckStringAgainstHash_WhenPasswordMatches_ShouldReturnTrue()
|
||||
{
|
||||
// Arrange
|
||||
string password = "TestPassword123!";
|
||||
string hash = _sut.HashString(password);
|
||||
|
||||
// Act
|
||||
bool result = _sut.CheckStringAgainstHash(password, hash);
|
||||
|
||||
// Assert
|
||||
Assert.True(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CheckStringAgainstHash_WhenPasswordDoesNotMatch_ShouldReturnFalse()
|
||||
{
|
||||
// Arrange
|
||||
string password = "TestPassword123!";
|
||||
string wrongPassword = "WrongPassword123!";
|
||||
string hash = _sut.HashString(password);
|
||||
|
||||
// Act
|
||||
bool result = _sut.CheckStringAgainstHash(wrongPassword, hash);
|
||||
|
||||
// Assert
|
||||
Assert.False(result);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("")]
|
||||
[InlineData("dGVzdA==")] // Valid base64 but wrong length.
|
||||
[InlineData("YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXo=")] // Valid base64 but wrong length.
|
||||
public void CheckStringAgainstHash_WhenHashIsInvalidLength_ShouldReturnFalse(string invalidHash)
|
||||
{
|
||||
// Arrange
|
||||
string password = "TestPassword123!";
|
||||
|
||||
// Act
|
||||
bool result = _sut.CheckStringAgainstHash(password, invalidHash);
|
||||
|
||||
// Assert
|
||||
Assert.False(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CheckStringAgainstHash_WhenHashIsNotBase64_ShouldThrowFormatException()
|
||||
{
|
||||
// Arrange
|
||||
string password = "TestPassword123!";
|
||||
string invalidHash = "not-base64-string";
|
||||
|
||||
// Act
|
||||
Action act = () => _sut.CheckStringAgainstHash(password, invalidHash);
|
||||
|
||||
// Assert
|
||||
Assert.Throws<FormatException>(() => _sut.CheckStringAgainstHash(password, invalidHash));
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(null)]
|
||||
public void HashString_WhenPasswordIsNull_ShouldThrowArgumentNullException(string? nullPassword)
|
||||
{
|
||||
// Act
|
||||
Action act = () => _sut.HashString(nullPassword!);
|
||||
|
||||
// Assert
|
||||
Assert.Throws<ArgumentNullException>(() => _sut.HashString(nullPassword!));
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(null)]
|
||||
public void CheckStringAgainstHash_WhenPasswordIsNull_ShouldThrowArgumentNullException(string? nullPassword)
|
||||
{
|
||||
// Arrange
|
||||
string hash = _sut.HashString("somepassword");
|
||||
|
||||
// Act
|
||||
Action act = () => _sut.CheckStringAgainstHash(nullPassword!, hash);
|
||||
|
||||
// Assert
|
||||
Assert.Throws<ArgumentNullException>(() => _sut.CheckStringAgainstHash(nullPassword!, hash));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CheckStringAgainstHash_WhenHashIsNull_ShouldThrowArgumentNullException()
|
||||
{
|
||||
// Act
|
||||
Action act = () => _sut.CheckStringAgainstHash("password", null!);
|
||||
|
||||
// Assert
|
||||
Assert.Throws<ArgumentNullException>(() => _sut.CheckStringAgainstHash("password", null!));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user