PlanTempusApp/PlanTempus.Tests/PasswordHasherTest.cs

87 lines
2.2 KiB
C#
Raw Normal View History

using System.Diagnostics;
2025-03-07 00:17:08 +01:00
using System.Text;
2025-03-04 17:13:02 +01:00
using PlanTempus.Core;
2025-03-07 00:17:08 +01:00
using Sodium;
2025-03-07 00:17:08 +01:00
namespace PlanTempus.X.TDD;
[TestClass]
public class PasswordHasherTests : TestFixture
{
2025-03-07 00:17:08 +01:00
[TestMethod]
public void MyTestMethod()
{
var stopwatch = Stopwatch.StartNew();
var salt = PasswordHash.ScryptGenerateSalt();
// 2. Konverter password til byte[]
var passwordBytes = Encoding.UTF8.GetBytes("password123");
// 3. Kald ScryptHashBinary korrekt
var hash = PasswordHash.ScryptHashBinary(
passwordBytes,
salt
);
stopwatch.Stop();
}
[TestMethod]
public void HashPassword_ShouldCreateValidHashFormat()
{
// Arrange
var password = "TestPassword123";
// Act
var hashedPassword = new SecureTokenizer().TokenizeText(password);
var parts = hashedPassword.Split('.');
// Assert
Assert.AreEqual(3, parts.Length);
Assert.AreEqual("100000", parts[0]);
}
[TestMethod]
public void VerifyPassword_WithCorrectPassword_ShouldReturnTrue()
{
// Arrange
var password = "TestPassword123";
var hashedPassword = new SecureTokenizer().TokenizeText(password);
// Act
var result = new SecureTokenizer().VerifyToken(hashedPassword, password);
// Assert
Assert.IsTrue(result);
}
[TestMethod]
public void VerifyPassword_WithWrongPassword_ShouldReturnFalse()
{
2025-03-07 00:17:08 +01:00
// Arrange
var correctPassword = "TestPassword123";
var wrongPassword = "WrongPassword123";
var hashedPassword = new SecureTokenizer().TokenizeText(correctPassword);
// Act
var result = new SecureTokenizer().VerifyToken(hashedPassword, wrongPassword);
// Assert
Assert.IsFalse(result);
}
[TestMethod]
public void VerifyPassword_WithInvalidHashFormat_ShouldReturnFalse()
{
// Arrange
var password = "TestPassword123";
var invalidHash = "InvalidHash";
// Act
var result = new SecureTokenizer().VerifyToken(invalidHash, password);
2025-03-07 00:17:08 +01:00
// Assert
Assert.IsFalse(result);
}
}