mirror of
https://github.com/BinaryAlley/Nemera.git
synced 2026-12-03 12:11:10 +00:00
Adds authentication unit tests
This commit is contained in:
1 parent
a6dcf65d1c
commit
4f91f29b62
101 files changed
+5549
-135
No files matched your search
+161
@@ -0,0 +1,161 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using FluentAssertions;
|
||||
using Lumina.Infrastructure.Common.Models.Configuration;
|
||||
using Lumina.Infrastructure.Core.Security;
|
||||
using Lumina.Infrastructure.UnitTests.Core.Security.Fixtures;
|
||||
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<EncryptionSettingsModel> _encryptionSettings;
|
||||
|
||||
/// <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
|
||||
result.Should().NotBeNullOrEmpty();
|
||||
result.Should().NotBe(plaintext);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Encrypt_WhenCalledMultipleTimesWithSameInput_ShouldReturnDifferentResults()
|
||||
{
|
||||
// Arrange
|
||||
string plaintext = CryptographyServiceFixture.CreateTestData();
|
||||
|
||||
// Act
|
||||
string result1 = _sut.Encrypt(plaintext);
|
||||
string result2 = _sut.Encrypt(plaintext);
|
||||
|
||||
// Assert
|
||||
result1.Should().NotBe(result2);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(null)]
|
||||
[InlineData("")]
|
||||
public void Encrypt_WhenInputIsNullOrEmpty_ShouldThrowArgumentException(string? invalidInput)
|
||||
{
|
||||
// Act
|
||||
Action act = () => _sut.Encrypt(invalidInput!);
|
||||
|
||||
// Assert
|
||||
act.Should().Throw<ArgumentException>()
|
||||
.WithParameterName("plaintext")
|
||||
.WithMessage("Value cannot be null or empty*");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Decrypt_WhenCalledWithValidData_ShouldReturnOriginalText()
|
||||
{
|
||||
// Arrange
|
||||
string originalText = CryptographyServiceFixture.CreateTestData();
|
||||
string encrypted = _sut.Encrypt(originalText);
|
||||
|
||||
// Act
|
||||
string decrypted = _sut.Decrypt(encrypted);
|
||||
|
||||
// Assert
|
||||
decrypted.Should().Be(originalText);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(null)]
|
||||
[InlineData("")]
|
||||
public void Decrypt_WhenInputIsNullOrEmpty_ShouldThrowArgumentException(string? invalidInput)
|
||||
{
|
||||
// Act
|
||||
Action act = () => _sut.Decrypt(invalidInput!);
|
||||
|
||||
// Assert
|
||||
act.Should().Throw<ArgumentException>()
|
||||
.WithParameterName("ciphertext")
|
||||
.WithMessage("Value cannot be null or empty*");
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("invalid")]
|
||||
[InlineData("not-base64-!@#")]
|
||||
public void Decrypt_WhenInputIsNotBase64_ShouldThrowFormatException(string invalidInput)
|
||||
{
|
||||
// Act
|
||||
Action act = () => _sut.Decrypt(invalidInput);
|
||||
|
||||
// Assert
|
||||
act.Should().Throw<FormatException>();
|
||||
}
|
||||
|
||||
[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
|
||||
act.Should().Throw<ArgumentException>()
|
||||
.WithMessage("Invalid ciphertext");
|
||||
}
|
||||
|
||||
[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
|
||||
act.Should().Throw<CryptographicException>();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Dispose_WhenCalled_ShouldClearKey()
|
||||
{
|
||||
// Arrange
|
||||
string plaintext = CryptographyServiceFixture.CreateTestData();
|
||||
string encrypted = _sut.Encrypt(plaintext);
|
||||
|
||||
// Act
|
||||
_sut.Dispose();
|
||||
|
||||
// Assert
|
||||
Action act = () => _sut.Decrypt(encrypted);
|
||||
act.Should().Throw<CryptographicException>();
|
||||
}
|
||||
}
|
||||
+49
@@ -0,0 +1,49 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Bogus;
|
||||
using Lumina.Infrastructure.Common.Models.Configuration;
|
||||
using Microsoft.Extensions.Options;
|
||||
using NSubstitute;
|
||||
using System;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Security.Cryptography;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Infrastructure.UnitTests.Core.Security.Fixtures;
|
||||
|
||||
/// <summary>
|
||||
/// Fixture class for cryptography service tests.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class CryptographyServiceFixture
|
||||
{
|
||||
/// <summary>
|
||||
/// Creates a valid encryption settings model for testing.
|
||||
/// </summary>
|
||||
/// <returns>The configured options.</returns>
|
||||
public static IOptions<EncryptionSettingsModel> CreateEncryptionSettings()
|
||||
{
|
||||
// Generate a valid 256-bit (32 byte) key for AES-256
|
||||
byte[] key = new byte[32];
|
||||
RandomNumberGenerator.Fill(key);
|
||||
string base64Key = Convert.ToBase64String(key);
|
||||
|
||||
EncryptionSettingsModel settings = new()
|
||||
{
|
||||
SecretKey = base64Key
|
||||
};
|
||||
|
||||
IOptions<EncryptionSettingsModel> options = Substitute.For<IOptions<EncryptionSettingsModel>>();
|
||||
options.Value.Returns(settings);
|
||||
|
||||
return options;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates test data for encryption/decryption.
|
||||
/// </summary>
|
||||
/// <returns>A string to be encrypted/decrypted.</returns>
|
||||
public static string CreateTestData()
|
||||
{
|
||||
return new Faker().Lorem.Sentence();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,165 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using FluentAssertions;
|
||||
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="HashService"/> class.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class HashServiceTests
|
||||
{
|
||||
private readonly HashService _sut;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="HashServiceTests"/> class.
|
||||
/// </summary>
|
||||
public HashServiceTests()
|
||||
{
|
||||
_sut = new HashService();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void HashString_WhenCalled_ShouldReturnNonEmptyHash()
|
||||
{
|
||||
// Arrange
|
||||
string password = "TestPassword123!";
|
||||
|
||||
// Act
|
||||
string result = _sut.HashString(password);
|
||||
|
||||
// Assert
|
||||
result.Should().NotBeNullOrEmpty();
|
||||
result.Should().NotBe(password);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void HashString_WhenCalledMultipleTimesWithSameInput_ShouldReturnDifferentHashes()
|
||||
{
|
||||
// Arrange
|
||||
string password = "TestPassword123!";
|
||||
|
||||
// Act
|
||||
string hash1 = _sut.HashString(password);
|
||||
string hash2 = _sut.HashString(password);
|
||||
|
||||
// Assert
|
||||
hash1.Should().NotBe(hash2);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(" ")]
|
||||
[InlineData("short")]
|
||||
[InlineData("verylongpasswordwithlotsofcharactersbutnostringformatting")]
|
||||
[InlineData("Password123!")]
|
||||
[InlineData("@#$%^&*()")]
|
||||
public void HashString_WhenCalledWithDifferentInputs_ShouldGenerateValidHashes(string input)
|
||||
{
|
||||
// Act
|
||||
string hash = _sut.HashString(input);
|
||||
|
||||
// Assert
|
||||
hash.Should().NotBeNullOrEmpty();
|
||||
// verify it's a valid Base64 string
|
||||
Action decode = () => Convert.FromBase64String(hash);
|
||||
decode.Should().NotThrow();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CheckStringAgainstHash_WhenPasswordMatches_ShouldReturnTrue()
|
||||
{
|
||||
// Arrange
|
||||
string password = "TestPassword123!";
|
||||
string hash = _sut.HashString(password);
|
||||
|
||||
// Act
|
||||
bool result = _sut.CheckStringAgainstHash(password, hash);
|
||||
|
||||
// Assert
|
||||
result.Should().BeTrue();
|
||||
}
|
||||
|
||||
[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
|
||||
result.Should().BeFalse();
|
||||
}
|
||||
|
||||
[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
|
||||
result.Should().BeFalse();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CheckStringAgainstHash_WhenHashIsNotBase64_ShouldThrowFormatException()
|
||||
{
|
||||
// Arrange
|
||||
string password = "TestPassword123!";
|
||||
string invalidHash = "not-base64-string";
|
||||
|
||||
// Act
|
||||
Action act = () => _sut.CheckStringAgainstHash(password, invalidHash);
|
||||
|
||||
// Assert
|
||||
act.Should().Throw<FormatException>();
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(null)]
|
||||
public void HashString_WhenPasswordIsNull_ShouldThrowArgumentNullException(string? nullPassword)
|
||||
{
|
||||
// Act
|
||||
Action act = () => _sut.HashString(nullPassword!);
|
||||
|
||||
// Assert
|
||||
act.Should().Throw<ArgumentNullException>();
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(null)]
|
||||
public void CheckStringAgainstHash_WhenPasswordIsNull_ShouldThrowArgumentNullException(string? nullPassword)
|
||||
{
|
||||
// Arrange
|
||||
string hash = _sut.HashString("somepassword");
|
||||
|
||||
// Act
|
||||
Action act = () => _sut.CheckStringAgainstHash(nullPassword!, hash);
|
||||
|
||||
// Assert
|
||||
act.Should().Throw<ArgumentNullException>();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CheckStringAgainstHash_WhenHashIsNull_ShouldThrowArgumentNullException()
|
||||
{
|
||||
// Act
|
||||
Action act = () => _sut.CheckStringAgainstHash("password", null!);
|
||||
|
||||
// Assert
|
||||
act.Should().Throw<ArgumentNullException>();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user