120 lines
3.6 KiB
C#
120 lines
3.6 KiB
C#
using System.Text.Json;
|
|
using Livia.Models;
|
|
using Livia.Properties;
|
|
using Microsoft.Extensions.Logging;
|
|
using Moq;
|
|
|
|
namespace Livia.Tests.Models;
|
|
|
|
public class SerialKeyManagerTests
|
|
{
|
|
private readonly ISerialKeyManager _serialKeyManager;
|
|
private readonly Mock<IAesOperation> _aesOperationMock = new();
|
|
|
|
public SerialKeyManagerTests(ILogger logger)
|
|
{
|
|
_serialKeyManager = new SerialKeyManager(logger, _aesOperationMock.Object);
|
|
}
|
|
|
|
[Fact]
|
|
public void Constructor_ValidParameter_HardwareIdNotEmpty()
|
|
{
|
|
// assert
|
|
Assert.NotEqual(string.Empty, _serialKeyManager.HardwareId);
|
|
}
|
|
|
|
[Fact]
|
|
public void Validate_EmptyKey_ReturnsFalse()
|
|
{
|
|
// arrange
|
|
_aesOperationMock.Setup(x => x.DecryptString(It.IsAny<string>(), It.IsAny<string>())).Returns(string.Empty);
|
|
|
|
// act
|
|
bool result = _serialKeyManager.Validate();
|
|
|
|
// assert
|
|
_aesOperationMock.Verify(x => x.DecryptString(It.IsAny<string>(), It.IsAny<string>()), Times.Once());
|
|
Assert.False(result);
|
|
}
|
|
|
|
[Fact]
|
|
public void Validate_WrongHardwareId_ReturnsFalse()
|
|
{
|
|
// arrange
|
|
AuthenticationKeyLicense license = new()
|
|
{
|
|
HardwareId = "Not my ID",
|
|
ExpireDateTime = DateTime.MaxValue
|
|
};
|
|
string licenseJson = JsonSerializer.Serialize(license);
|
|
_aesOperationMock.Setup(x => x.DecryptString(It.IsAny<string>(), It.IsAny<string>())).Returns(licenseJson);
|
|
|
|
// act
|
|
bool result = _serialKeyManager.Validate();
|
|
|
|
// assert
|
|
_aesOperationMock.Verify(x => x.DecryptString(It.IsAny<string>(), It.IsAny<string>()), Times.Once());
|
|
Assert.False(result);
|
|
}
|
|
|
|
[Fact]
|
|
public void Validate_ExpiredLicense_ReturnsFalse()
|
|
{
|
|
// arrange
|
|
AuthenticationKeyLicense license = new()
|
|
{
|
|
HardwareId = _serialKeyManager.HardwareId,
|
|
ExpireDateTime = DateTime.MinValue
|
|
};
|
|
string licenseJson = JsonSerializer.Serialize(license);
|
|
_aesOperationMock.Setup(x => x.DecryptString(It.IsAny<string>(), It.IsAny<string>())).Returns(licenseJson);
|
|
|
|
// act
|
|
bool result = _serialKeyManager.Validate();
|
|
|
|
// assert
|
|
_aesOperationMock.Verify(x => x.DecryptString(It.IsAny<string>(), It.IsAny<string>()), Times.Once());
|
|
Assert.False(result);
|
|
}
|
|
|
|
[Fact]
|
|
public void Validate_ValidLicense_ReturnsTrue()
|
|
{
|
|
// arrange
|
|
Settings.Default.Reset();
|
|
AuthenticationKeyLicense license = new()
|
|
{
|
|
HardwareId = _serialKeyManager.HardwareId,
|
|
ExpireDateTime = DateTime.MaxValue
|
|
};
|
|
string licenseJson = JsonSerializer.Serialize(license);
|
|
_aesOperationMock.Setup(x => x.DecryptString(It.IsAny<string>(), It.IsAny<string>())).Returns(licenseJson);
|
|
|
|
// act
|
|
bool result = _serialKeyManager.Validate();
|
|
|
|
// assert
|
|
Assert.True(result);
|
|
}
|
|
|
|
[Fact]
|
|
public void EncryptLicense_ValidLicense_ReturnsEncryptedString()
|
|
{
|
|
// arrange
|
|
AuthenticationKeyLicense license = new()
|
|
{
|
|
HardwareId = _serialKeyManager.HardwareId,
|
|
ExpireDateTime = DateTime.Now
|
|
};
|
|
_aesOperationMock.Setup(x => x.EncryptString(It.IsAny<string>(), It.IsAny<string>())).Returns<string, string>((_, text) => text);
|
|
|
|
// act
|
|
string licenseJson = _serialKeyManager.EncryptLicense(license);
|
|
|
|
// assert
|
|
_aesOperationMock.Verify(x => x.EncryptString(It.IsAny<string>(), It.IsAny<string>()), Times.Once());
|
|
Assert.Equal(JsonSerializer.Serialize(license), licenseJson);
|
|
}
|
|
}
|
|
|