init
This commit is contained in:
commit
f25ba8adcd
64 changed files with 5959 additions and 0 deletions
61
tests/GsDChat.Tests/CryptoTests.cs
Normal file
61
tests/GsDChat.Tests/CryptoTests.cs
Normal file
|
|
@ -0,0 +1,61 @@
|
|||
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);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue