Testing and Password

This commit is contained in:
Jordan Wages 2024-08-27 18:54:36 -05:00
commit 650c6ea3dd
33 changed files with 1191 additions and 5 deletions

127
Tests/Passwords.cs Normal file
View file

@ -0,0 +1,127 @@
using CapyKit;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Tests
{
internal class Passwords
{
[TestFixture]
public class PasswordTests
{
private static byte[] GenerateSalt(int size = 16)
{
var salt = new byte[size];
using (var rng = new System.Security.Cryptography.RNGCryptoServiceProvider())
{
rng.GetBytes(salt);
}
return salt;
}
[Test]
public void PasswordCreation_WithPbkdf2Algorithm_CreatesValidHash()
{
// Arrange
var passwordText = "mySecurePassword";
var salt = GenerateSalt();
var algorithm = Password.Pbkdf2Algorithm;
// Act
var password = new Password(passwordText, salt, algorithm);
// Assert
Assert.IsNotNull(password.Hash);
Assert.IsNotEmpty(password.Hash);
Assert.AreEqual(salt, password.Salt);
Assert.AreEqual(algorithm, password.Algorithm);
}
[Test]
public void PasswordEquality_SamePasswordAndSalt_AreEqual()
{
// Arrange
var passwordText = "mySecurePassword";
var salt = GenerateSalt();
var algorithm = Password.Pbkdf2Algorithm;
var password1 = new Password(passwordText, salt, algorithm);
var password2 = new Password(passwordText, salt, algorithm);
// Act & Assert
Assert.AreEqual(password1, password2);
Assert.IsTrue(password1 == password2);
}
[Test]
public void PasswordEquality_DifferentPasswords_AreNotEqual()
{
// Arrange
var salt = GenerateSalt();
var algorithm = new Pbkdf2Algorithm();
var password1 = new Password("passwordOne", salt, algorithm);
var password2 = new Password("passwordTwo", salt, algorithm);
// Act & Assert
Assert.AreNotEqual(password1, password2);
Assert.IsTrue(password1 != password2);
}
[Test]
public void PasswordEquality_DifferentSalts_AreNotEqual()
{
// Arrange
var passwordText = "mySecurePassword";
var salt1 = GenerateSalt();
var salt2 = GenerateSalt();
var algorithm = Password.Pbkdf2Algorithm;
var password1 = new Password(passwordText, salt1, algorithm);
var password2 = new Password(passwordText, salt2, algorithm);
// Act & Assert
Assert.AreNotEqual(password1, password2);
Assert.IsTrue(password1 != password2);
}
[Test]
public void ToString_ReturnsCorrectFormat()
{
// Arrange
var passwordText = "mySecurePassword";
var salt = GenerateSalt();
var algorithm = Password.Pbkdf2Algorithm;
var password = new Password(passwordText, salt, algorithm);
// Act
var result = password.ToString();
// Assert
Assert.IsTrue(result.Contains("Hash:"));
Assert.IsTrue(result.Contains("Salt:"));
Assert.IsTrue(result.Contains("Algorithm: Pbkdf2"));
}
[Test]
public void PasswordCreation_WithInvalidSalt_ReturnsEmptyHash()
{
// Arrange
var passwordText = "mySecurePassword";
byte[] invalidSalt = null; // Invalid salt
var algorithm = new Pbkdf2Algorithm();
// Act
var password = new Password(passwordText, invalidSalt, algorithm);
// Assert
Assert.IsNotNull(password.Hash);
Assert.IsEmpty(password.Hash);
}
}
}
}

66
Tests/StringExtensions.cs Normal file
View file

@ -0,0 +1,66 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System;
using CapyKit.Extensions;
using NUnit.Framework;
namespace Tests
{
[TestFixture]
internal class StringExtensions
{
private const string Replacement = "Replaced";
[SetUp]
public void Setup()
{
// No setup required for this test fixture.
}
#region IfNullOrEmpty
[TestCase("")]
public void IfNullOrEmpty_ShouldReturnReplacementWhenValueIsNullOrEmpty(string value)
{
var result = value.IfNullOrEmpty(Replacement);
Assert.AreEqual(Replacement, result);
}
[TestCase("Not Empty")]
[TestCase(" ")] // Whitespace is not considered empty
public void IfNullOrEmpty_ShouldReturnOriginalStringWhenValueIsNotNullNorEmpty(string value)
{
var result = value.IfNullOrEmpty(Replacement);
Assert.AreEqual(value, result);
}
#endregion
#region IfNullOrWhiteSpace
[TestCase("")]
[TestCase(" ")] // Whitespace is considered empty
public void IfNullOrWhiteSpace_ShouldReturnReplacementWhenValueIsNullOrWhitespace(string value)
{
var result = value.IfNullOrWhiteSpace(Replacement);
Assert.AreEqual(Replacement, result);
}
[TestCase("Not Empty")]
[TestCase("\tTab")] // Non-whitespace character is not considered empty
public void IfNullOrWhiteSpace_ShouldReturnOriginalStringWhenValueIsNotNullNorWhitespace(string value)
{
var result = value.IfNullOrWhiteSpace(Replacement);
Assert.AreEqual(value, result);
}
#endregion
}
}

28
Tests/Tests.csproj Normal file
View file

@ -0,0 +1,28 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="NUnit" Version="3.14.0" />
<PackageReference Include="NUnit.Analyzers" Version="3.9.0" />
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\CapyKit\CapyKit.csproj" />
</ItemGroup>
<ItemGroup>
<Using Include="NUnit.Framework" />
</ItemGroup>
</Project>