Fixing user password generation/validation - wip
This commit is contained in:
164
MainProject.Tests/Utils/CryptUtils_Tests.cs
Normal file
164
MainProject.Tests/Utils/CryptUtils_Tests.cs
Normal file
@@ -0,0 +1,164 @@
|
||||
using System;
|
||||
using BasicDotnetTemplate.MainProject.Models.Settings;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using BasicDotnetTemplate.MainProject.Utils;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
|
||||
|
||||
namespace BasicDotnetTemplate.MainProject.Tests;
|
||||
|
||||
[TestClass]
|
||||
public class CryptoUtils_Tests
|
||||
{
|
||||
[TestMethod]
|
||||
public void Decrypt_Success()
|
||||
{
|
||||
try
|
||||
{
|
||||
string encryptedData = "d2ejdI1f4GYpq2kTB1nmeQkZXqR3QSxH8Yqkl7iv7zgfQ13qG/0dUUsreG/WGHWRBE5mVWaV43A=";
|
||||
WebApplicationBuilder builder = WebApplication.CreateBuilder(Array.Empty<string>());
|
||||
AppSettings appSettings = ProgramUtils.AddConfiguration(ref builder, System.AppDomain.CurrentDomain.BaseDirectory + "/JsonData");
|
||||
CryptUtils cryptoUtils = new CryptUtils(appSettings);
|
||||
var decryptedData = cryptoUtils.Decrypt(encryptedData);
|
||||
var isEqual = decryptedData == "ThisIsASuccessfullTest";
|
||||
Assert.IsTrue(isEqual);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine(ex.InnerException);
|
||||
Assert.Fail($"An exception was thrown: {ex}");
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void Decrypt_Error()
|
||||
{
|
||||
try
|
||||
{
|
||||
string encryptedData = "d1ejdI1f4GYpq2kTB1nmeQkZXqR3QSxH8Yqkl7iv7zgfQ13qG/0dUUsreG/WGHWRBE5mVWaV43A=";
|
||||
WebApplicationBuilder builder = WebApplication.CreateBuilder(Array.Empty<string>());
|
||||
AppSettings appSettings = ProgramUtils.AddConfiguration(ref builder, System.AppDomain.CurrentDomain.BaseDirectory + "/JsonData");
|
||||
CryptUtils cryptoUtils = new CryptUtils(appSettings);
|
||||
var decryptedData = cryptoUtils.Decrypt(encryptedData);
|
||||
var isEqual = decryptedData == "ThisIsASuccessfullTest";
|
||||
Assert.IsFalse(isEqual);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine(ex.InnerException);
|
||||
Assert.Fail($"An exception was thrown: {ex}");
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void Decrypt_ArgumentException()
|
||||
{
|
||||
try
|
||||
{
|
||||
string encryptedData = "d1ejdI1f4GYpq2kTB1nmeQkZXqR3QSxH8Yqkl7iv7zgfQ13qG/0dUUsreG/WGHWRBE5mVWaV43A=";
|
||||
WebApplicationBuilder builder = WebApplication.CreateBuilder(Array.Empty<string>());
|
||||
AppSettings appSettings = ProgramUtils.AddConfiguration(ref builder, System.AppDomain.CurrentDomain.BaseDirectory + "/JsonData", "invalidCryptAppsettings.json");
|
||||
CryptUtils cryptoUtils = new CryptUtils(appSettings);
|
||||
try
|
||||
{
|
||||
var decryptedData = cryptoUtils.Decrypt(encryptedData);
|
||||
Assert.Fail($"Expected exception instead of response: {decryptedData}");
|
||||
}
|
||||
catch (ArgumentException argumentException)
|
||||
{
|
||||
Assert.IsInstanceOfType(argumentException, typeof(ArgumentException));
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
Assert.IsInstanceOfType(exception, typeof(ArgumentException));
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine(ex.InnerException);
|
||||
Assert.Fail($"An exception was thrown: {ex}");
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void Decrypt_Empty()
|
||||
{
|
||||
try
|
||||
{
|
||||
string encryptedData = "WGHWRBE5mVWaV=";
|
||||
WebApplicationBuilder builder = WebApplication.CreateBuilder(Array.Empty<string>());
|
||||
AppSettings appSettings = ProgramUtils.AddConfiguration(ref builder, System.AppDomain.CurrentDomain.BaseDirectory + "/JsonData");
|
||||
CryptUtils cryptoUtils = new CryptUtils(appSettings);
|
||||
var decryptedData = cryptoUtils.Decrypt(encryptedData);
|
||||
var isEqual = decryptedData == String.Empty;
|
||||
Assert.IsTrue(isEqual);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine(ex.InnerException);
|
||||
Assert.Fail($"An exception was thrown: {ex}");
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void GeneratePepper()
|
||||
{
|
||||
try
|
||||
{
|
||||
var salt = CryptUtils.GeneratePepper();
|
||||
Assert.IsTrue(!String.IsNullOrEmpty(salt));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine(ex.InnerException);
|
||||
Assert.Fail($"An exception was thrown: {ex}");
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void ComputeHash_Hashed()
|
||||
{
|
||||
try
|
||||
{
|
||||
var password = "P4ssw0rd@1!";
|
||||
var pepper = CryptUtils.GeneratePepper();
|
||||
Assert.IsTrue(!String.IsNullOrEmpty(pepper));
|
||||
|
||||
WebApplicationBuilder builder = WebApplication.CreateBuilder(Array.Empty<string>());
|
||||
AppSettings appSettings = ProgramUtils.AddConfiguration(ref builder, System.AppDomain.CurrentDomain.BaseDirectory + "/JsonData");
|
||||
var salt = appSettings?.EncryptionSettings?.Salt ?? String.Empty;
|
||||
var encryptedPassword = CryptUtils.GeneratePassword(password, salt, 0, pepper);
|
||||
Assert.AreNotEqual(encryptedPassword, password);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine(ex.InnerException);
|
||||
Assert.Fail($"An exception was thrown: {ex}");
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void VerifyPassword_True()
|
||||
{
|
||||
try
|
||||
{
|
||||
var password = "P4ssw0rd@1!";
|
||||
var salt = "Afi7PQYgEL2sPbNyVzduvg==";
|
||||
var hashedPassword = "2lMeySZ9ciH1KtSg1Z7oSJRmJEjHMeDvdaNRcJcGutM=";
|
||||
|
||||
var verified = CryptUtils.VerifyPassword(hashedPassword, password, salt, 0);
|
||||
Assert.IsTrue(verified);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine(ex.InnerException);
|
||||
Assert.Fail($"An exception was thrown: {ex}");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user