165 lines
3.8 KiB
C#
165 lines
3.8 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Reflection;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using Microsoft.AspNetCore.Identity;
|
|
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
|
|
|
namespace UnitTests.AspNetCore.Utilities
|
|
{
|
|
[TestClass]
|
|
[System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
|
|
public class PasswordHelperTests
|
|
{
|
|
[TestMethod]
|
|
public void ShouldReturnNullHashWhenNullProvided()
|
|
{
|
|
// arrange
|
|
string password = null;
|
|
|
|
// act
|
|
string hash = PasswordHelper.HashPassword(password);
|
|
|
|
// assert
|
|
Assert.IsNull(hash);
|
|
}
|
|
|
|
[TestMethod]
|
|
public void ShouldReturnEmptyHashWhenSpacesProvided()
|
|
{
|
|
// arrange
|
|
string password = " ";
|
|
|
|
// act
|
|
string hash = PasswordHelper.HashPassword(password);
|
|
|
|
// assert
|
|
Assert.AreEqual("", hash);
|
|
}
|
|
|
|
[TestMethod]
|
|
public void ShouldReturnHashWhenTextProvided()
|
|
{
|
|
// arrange
|
|
string password = "password";
|
|
|
|
// act
|
|
string hash = PasswordHelper.HashPassword(password);
|
|
|
|
// assert
|
|
Assert.IsTrue(!string.IsNullOrWhiteSpace(hash));
|
|
}
|
|
|
|
[TestMethod]
|
|
public void ShouldReturnFalseOnNullPassword()
|
|
{
|
|
// arrange
|
|
string password = null;
|
|
string hash = PasswordHelper.HashPassword(password);
|
|
|
|
// act
|
|
bool isValid = PasswordHelper.VerifyPassword(password, hash, out bool rehashNeeded);
|
|
|
|
// assert
|
|
Assert.IsFalse(isValid);
|
|
Assert.IsFalse(rehashNeeded);
|
|
}
|
|
|
|
[TestMethod]
|
|
public void ShouldReturnFalseOnEmptyPassword()
|
|
{
|
|
// arrange
|
|
string password = " ";
|
|
string hash = PasswordHelper.HashPassword(password);
|
|
|
|
// act
|
|
bool isValid = PasswordHelper.VerifyPassword(password, hash, out bool rehashNeeded);
|
|
|
|
// assert
|
|
Assert.IsFalse(isValid);
|
|
Assert.IsFalse(rehashNeeded);
|
|
}
|
|
|
|
[TestMethod]
|
|
public void ShouldReturnFalseOnNullHash()
|
|
{
|
|
// arrange
|
|
string password = "password";
|
|
string hash = null;
|
|
|
|
// act
|
|
bool isValid = PasswordHelper.VerifyPassword(password, hash, out bool rehashNeeded);
|
|
|
|
// assert
|
|
Assert.IsFalse(isValid);
|
|
Assert.IsFalse(rehashNeeded);
|
|
}
|
|
|
|
[TestMethod]
|
|
public void ShouldReturnFalseOnEmptyHash()
|
|
{
|
|
// arrange
|
|
string password = "password";
|
|
string hash = "";
|
|
|
|
// act
|
|
bool isValid = PasswordHelper.VerifyPassword(password, hash, out bool rehashNeeded);
|
|
|
|
// assert
|
|
Assert.IsFalse(isValid);
|
|
Assert.IsFalse(rehashNeeded);
|
|
}
|
|
|
|
[TestMethod]
|
|
public void ShouldReturnTrueOnSuccess()
|
|
{
|
|
// arrange
|
|
string password = "password";
|
|
string hash = PasswordHelper.HashPassword(password);
|
|
|
|
// act
|
|
bool isValid = PasswordHelper.VerifyPassword(password, hash, out bool rehashNeeded);
|
|
|
|
// assert
|
|
Assert.IsTrue(isValid);
|
|
Assert.IsFalse(rehashNeeded);
|
|
}
|
|
|
|
[TestMethod]
|
|
public void ShouldReturnFalseOnError()
|
|
{
|
|
// arrange
|
|
string password = "pass";
|
|
string hash = PasswordHelper.HashPassword(password + "word");
|
|
|
|
// act
|
|
bool isValid = PasswordHelper.VerifyPassword(password, hash, out bool rehashNeeded);
|
|
|
|
// assert
|
|
Assert.IsFalse(isValid);
|
|
Assert.IsFalse(rehashNeeded);
|
|
}
|
|
|
|
[TestMethod]
|
|
public void ShouldReturnTrueWithRehash()
|
|
{
|
|
// arrange
|
|
var devHasher = new PasswordHasher<object>();
|
|
var field = devHasher.GetType().GetField("_compatibilityMode", BindingFlags.NonPublic | BindingFlags.Instance);
|
|
field.SetValue(devHasher, PasswordHasherCompatibilityMode.IdentityV2);
|
|
|
|
string password = "password";
|
|
string hash = devHasher.HashPassword(null, password);
|
|
|
|
// act
|
|
bool isValid = PasswordHelper.VerifyPassword(password, hash, out bool rehashNeeded);
|
|
|
|
// assert
|
|
Assert.IsTrue(isValid);
|
|
Assert.IsTrue(rehashNeeded);
|
|
}
|
|
}
|
|
}
|