GsDChat/tests/GsDChat.Tests/CryptoTests.cs
2026-10-10 16:40:35 +00:00

61 lines
1.8 KiB
C#

using GsDChat.Core.Security;
using Xunit;
namespace GsDChat.Tests;
public class CryptoTests
{
[Fact]
public void HashPassword_ShouldReturnValidHashAndSalt()
{
var password = "SuperSecretPassword123!";
var (hash, salt) = PasswordHasher.HashPassword(password);
Assert.False(string.IsNullOrWhiteSpace(hash));
Assert.False(string.IsNullOrWhiteSpace(salt));
Assert.Equal(128, hash.Length); // 64 bytes hex
Assert.Equal(64, salt.Length); // 32 bytes hex
}
[Fact]
public void VerifyPassword_ShouldSucceedForCorrectPassword()
{
var password = "MyP@ssw0rd!#2026";
var (hash, salt) = PasswordHasher.HashPassword(password);
bool isValid = PasswordHasher.VerifyPassword(password, hash, salt);
Assert.True(isValid);
}
[Fact]
public void VerifyPassword_ShouldFailForIncorrectPassword()
{
var password = "CorrectPassword";
var (hash, salt) = PasswordHasher.HashPassword(password);
bool isValid = PasswordHasher.VerifyPassword("WrongPassword", hash, salt);
Assert.False(isValid);
}
[Fact]
public void HashPassword_SamePassword_ShouldGenerateDifferentSaltsAndHashes()
{
var password = "IdenticalPassword";
var (hash1, salt1) = PasswordHasher.HashPassword(password);
var (hash2, salt2) = PasswordHasher.HashPassword(password);
Assert.NotEqual(salt1, salt2);
Assert.NotEqual(hash1, hash2);
}
[Fact]
public void GenerateSecureToken_ShouldGenerateUniqueHexStrings()
{
var token1 = TokenGenerator.GenerateSecureToken(32);
var token2 = TokenGenerator.GenerateSecureToken(32);
Assert.Equal(64, token1.Length);
Assert.Equal(64, token2.Length);
Assert.NotEqual(token1, token2);
}
}