86 lines
2.2 KiB
C#
86 lines
2.2 KiB
C#
|
|
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||
|
|
using Shouldly;
|
||
|
|
using SWP.Core;
|
||
|
|
|
||
|
|
namespace SWP.Core.X.TDD.Security;
|
||
|
|
|
||
|
|
[TestClass]
|
||
|
|
public class SecureTokenizerTests
|
||
|
|
{
|
||
|
|
private ISecureTokenizer _tokenizer;
|
||
|
|
|
||
|
|
[TestInitialize]
|
||
|
|
public void Setup()
|
||
|
|
{
|
||
|
|
_tokenizer = new SecureTokenizer();
|
||
|
|
}
|
||
|
|
|
||
|
|
[TestMethod]
|
||
|
|
public void TokenizeText_ShouldReturnNonEmptyString()
|
||
|
|
{
|
||
|
|
// Act
|
||
|
|
var token = _tokenizer.TokenizeText("testPassword");
|
||
|
|
|
||
|
|
// Assert
|
||
|
|
token.ShouldNotBeNullOrEmpty();
|
||
|
|
}
|
||
|
|
|
||
|
|
[TestMethod]
|
||
|
|
public void TokenizeText_ShouldReturnDifferentTokensForSamePassword()
|
||
|
|
{
|
||
|
|
// Arrange
|
||
|
|
var password = "testPassword";
|
||
|
|
|
||
|
|
// Act
|
||
|
|
var token1 = _tokenizer.TokenizeText(password);
|
||
|
|
var token2 = _tokenizer.TokenizeText(password);
|
||
|
|
|
||
|
|
// Assert
|
||
|
|
token1.ShouldNotBe(token2);
|
||
|
|
}
|
||
|
|
|
||
|
|
[TestMethod]
|
||
|
|
public void VerifyToken_ShouldReturnTrueForValidPassword()
|
||
|
|
{
|
||
|
|
// Arrange
|
||
|
|
var password = "testPassword";
|
||
|
|
var token = _tokenizer.TokenizeText(password);
|
||
|
|
|
||
|
|
// Act
|
||
|
|
var result = _tokenizer.VerifyToken(token, password);
|
||
|
|
|
||
|
|
// Assert
|
||
|
|
result.ShouldBeTrue();
|
||
|
|
}
|
||
|
|
|
||
|
|
[TestMethod]
|
||
|
|
public void VerifyToken_ShouldReturnFalseForInvalidPassword()
|
||
|
|
{
|
||
|
|
// Arrange
|
||
|
|
var password = "testPassword";
|
||
|
|
var token = _tokenizer.TokenizeText(password);
|
||
|
|
|
||
|
|
// Act
|
||
|
|
var result = _tokenizer.VerifyToken(token, "wrongPassword");
|
||
|
|
|
||
|
|
// Assert
|
||
|
|
result.ShouldBeFalse();
|
||
|
|
}
|
||
|
|
|
||
|
|
[TestMethod]
|
||
|
|
public void VerifyToken_ShouldReturnFalseForMalformedToken()
|
||
|
|
{
|
||
|
|
// Act & Assert
|
||
|
|
_tokenizer.VerifyToken("invalid.token", "password").ShouldBeFalse();
|
||
|
|
_tokenizer.VerifyToken("", "password").ShouldBeFalse();
|
||
|
|
}
|
||
|
|
|
||
|
|
[TestMethod]
|
||
|
|
public void VerifyToken_ShouldHandleNullInputs()
|
||
|
|
{
|
||
|
|
// Act & Assert
|
||
|
|
Should.Throw<NullReferenceException>(() => _tokenizer.VerifyToken(null, "password"));
|
||
|
|
// Note: Current implementation doesn't handle null inputs gracefully
|
||
|
|
// This should be fixed in production code
|
||
|
|
}
|
||
|
|
}
|