Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.AspNetCore.TestHost" Version="8.0.10" />
<PackageReference Include="Microsoft.AspNetCore.TestHost" Version="8.0.11" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
<PackageReference Include="Moq" Version="4.20.72" />
<PackageReference Include="xunit" Version="2.9.2" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,17 @@

namespace AdvancedSystems.Security.Tests.Cryptography;

public class CryptoRandomProviderTests
/// <summary>
/// Tests the public methods in <seealso cref="CryptoRandomProvider"/>.
/// </summary>
public sealed class CryptoRandomProviderTests
{
#region Tests

/// <summary>
/// Tests that <seealso cref="CryptoRandomProvider.GetBytes(int)"/> returns an
/// array of non-zero bytes with the correct size.
/// </summary>
[Fact]
public void TestGetBytes()
{
Expand All @@ -25,6 +32,9 @@ public void TestGetBytes()
Assert.All(buffer.ToArray(), b => Assert.InRange(b, byte.MinValue, byte.MaxValue));
}

/// <summary>
/// Tests heuristically that <seealso cref="CryptoRandomProvider.GetInt32()"/> returns an integer.
/// </summary>
[Fact]
public void TestGetInt32()
{
Expand All @@ -42,6 +52,9 @@ public void TestGetInt32()
Assert.All(randomNumbers, x => Assert.InRange(x, int.MinValue, int.MaxValue));
}

/// <summary>
/// Tests heuristically that <seealso cref="CryptoRandomProvider.GetInt32(int, int)"/> returns an integer.
/// </summary>
[Fact]
public void TestGetInt32_MinMax()
{
Expand All @@ -61,6 +74,10 @@ public void TestGetInt32_MinMax()
Assert.All(randomNumbers, x => Assert.InRange(x, min, max - 1));
}

/// <summary>
/// Tests that <seealso cref="CryptoRandomProvider.Shuffle{T}(Span{T})"/> changes the order
/// of elements in an array.
/// </summary>
[Fact]
public void TestShuffle()
{
Expand All @@ -75,6 +92,10 @@ public void TestShuffle()
Assert.NotEqual(array1, array2);
}

/// <summary>
/// Tests that elements returned by <seealso cref="CryptoRandomProvider.Choice{T}(Span{T})"/>
/// are elements of the original collection.
/// </summary>
[Fact]
public void TestChoice()
{
Expand Down
65 changes: 64 additions & 1 deletion AdvancedSystems.Security.Tests/Cryptography/HashTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,25 @@

namespace AdvancedSystems.Security.Tests.Cryptography;

public class HashTests
/// <summary>
/// Tests the public methods in <seealso cref="Hash"/>.
/// </summary>
public sealed class HashTests
{
#region Tests

/// <summary>
/// Tests that the computed <seealso cref="HashAlgorithmName.MD5"/> hash returns a well-formatted string.
/// </summary>
/// <param name="input">
/// The input to compute the hash code for.
/// </param>
/// <param name="expected">
/// The expected result.
/// </param>
/// <param name="format">
/// The formatting to use.
/// </param>
[Theory]
[InlineData("Hello, World!", "65a8e27d8879283831b664bd8b7f0ad4", Format.Hex)]
[InlineData("Hello, World!", "ZajifYh5KDgxtmS9i38K1A==", Format.Base64)]
Expand All @@ -31,6 +46,18 @@ public void TestMd5Hash(string input, string expected, Format format)
Assert.Equal(expected, md5);
}

/// <summary>
/// Tests that the computed <seealso cref="HashAlgorithmName.SHA1"/> hash returns a well-formatted string.
/// </summary>
/// <param name="input">
/// The input to compute the hash code for.
/// </param>
/// <param name="expected">
/// The expected result.
/// </param>
/// <param name="format">
/// The formatting to use.
/// </param>
[Theory]
[InlineData("Hello, World!", "0a0a9f2a6772942557ab5355d76af442f8f65e01", Format.Hex)]
[InlineData("Hello, World!", "CgqfKmdylCVXq1NV12r0Qvj2XgE=", Format.Base64)]
Expand All @@ -50,6 +77,18 @@ public void TestSHA1Hash(string input, string expected, Format format)
Assert.Equal(expected, sha1);
}

/// <summary>
/// Tests that the computed <seealso cref="HashAlgorithmName.SHA256"/> hash returns a well-formatted string.
/// </summary>
/// <param name="input">
/// The input to compute the hash code for.
/// </param>
/// <param name="expected">
/// The expected result.
/// </param>
/// <param name="format">
/// The formatting to use.
/// </param>
[Theory]
[InlineData("Hello, World!", "dffd6021bb2bd5b0af676290809ec3a53191dd81c7f70a4b28688a362182986f", Format.Hex)]
[InlineData("Hello, World!", "3/1gIbsr1bCvZ2KQgJ7DpTGR3YHH9wpLKGiKNiGCmG8=", Format.Base64)]
Expand All @@ -69,6 +108,18 @@ public void TestSHA256Hash(string input, string expected, Format format)
Assert.Equal(expected, sha256);
}

/// <summary>
/// Tests that the computed <seealso cref="HashAlgorithmName.SHA384"/> hash returns a well-formatted string.
/// </summary>
/// <param name="input">
/// The input to compute the hash code for.
/// </param>
/// <param name="expected">
/// The expected result.
/// </param>
/// <param name="format">
/// The formatting to use.
/// </param>
[Theory]
[InlineData("Hello, World!", "5485cc9b3365b4305dfb4e8337e0a598a574f8242bf17289e0dd6c20a3cd44a089de16ab4ab308f63e44b1170eb5f515", Format.Hex)]
[InlineData("Hello, World!", "VIXMmzNltDBd+06DN+ClmKV0+CQr8XKJ4N1sIKPNRKCJ3harSrMI9j5EsRcOtfUV", Format.Base64)]
Expand All @@ -88,6 +139,18 @@ public void TestSHA384Hash(string input, string expected, Format format)
Assert.Equal(expected, sha384);
}

/// <summary>
/// Tests that the computed <seealso cref="HashAlgorithmName.SHA512"/> hash returns a well-formatted string.
/// </summary>
/// <param name="input">
/// The input to compute the hash code for.
/// </param>
/// <param name="expected">
/// The expected result.
/// </param>
/// <param name="format">
/// The formatting to use.
/// </param>
[Theory]
[InlineData("Hello, World!", "374d794a95cdcfd8b35993185fef9ba368f160d8daf432d08ba9f1ed1e5abe6cc69291e0fa2fe0006a52570ef18c19def4e617c33ce52ef0a6e5fbe318cb0387", Format.Hex)]
[InlineData("Hello, World!", "N015SpXNz9izWZMYX++bo2jxYNja9DLQi6nx7R5avmzGkpHg+i/gAGpSVw7xjBne9OYXwzzlLvCm5fvjGMsDhw==", Format.Base64)]
Expand Down
Loading