Skip to content

Commit

Permalink
Fix tests + small cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Coding-Enthusiast committed Jul 27, 2024
1 parent 2b64355 commit eae2229
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 44 deletions.
31 changes: 15 additions & 16 deletions Src/Tests/Bitcoin/Cryptography/Hashing/HmacSha256Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using Xunit;

namespace Tests.Bitcoin.Cryptography.Hashing
{
Expand All @@ -28,7 +27,7 @@ public void ConstructorTest(byte[] key, bool isKeyHashed)
byte[] expectedKey = sysHmac.Key;

// Set key in constructor:
using (HmacSha256 hmac = new HmacSha256(key))
using (HmacSha256 hmac = new(key))
{
Assert.Equal(expectedKey, hmac.Key);
if (!isKeyHashed)
Expand All @@ -44,7 +43,7 @@ public void ConstructorTest(byte[] key, bool isKeyHashed)
}

// Set key using the property:
using (HmacSha256 hmac = new HmacSha256())
using (HmacSha256 hmac = new())
{
Assert.Null(hmac.Key);
hmac.Key = key;
Expand All @@ -66,15 +65,15 @@ public void Constructor_ExceptionTest()
{
Assert.Throws<ArgumentNullException>(() => new HmacSha256(null));

HmacSha256 hmac = new HmacSha256();
HmacSha256 hmac = new();
Assert.Throws<ArgumentNullException>(() => hmac.Key = null);
}


[Fact]
public void ComputeHash_ExceptionTest()
{
HmacSha256 hmac = new HmacSha256();
HmacSha256 hmac = new();

Exception ex = Assert.Throws<ArgumentNullException>(() => hmac.ComputeHash(null));
Assert.Contains("Data can not be null", ex.Message);
Expand All @@ -93,7 +92,7 @@ public void ComputeHash_ExceptionTest()
[Fact]
public void ComputeHash_WithKey_ExceptionTest()
{
HmacSha256 hmac = new HmacSha256();
HmacSha256 hmac = new();

Exception ex = Assert.Throws<ArgumentNullException>(() => hmac.ComputeHash(null, new byte[1]));
Assert.Contains("Data can not be null", ex.Message);
Expand All @@ -115,7 +114,7 @@ public void ComputeHash_EmptyTest(byte[] key, byte[] data)
using var sysHmac = new System.Security.Cryptography.HMACSHA256(key);
byte[] expected = sysHmac.ComputeHash(data);

using HmacSha256 hmac = new HmacSha256(key);
using HmacSha256 hmac = new(key);
byte[] actual = hmac.ComputeHash(data);
Assert.Equal(expected, actual);
}
Expand All @@ -129,7 +128,7 @@ public void ComputeHash_Empty_WithKey_Test(byte[] key, byte[] data)
using var sysHmac = new System.Security.Cryptography.HMACSHA256(key);
byte[] expected = sysHmac.ComputeHash(data);

using HmacSha256 hmac = new HmacSha256();
using HmacSha256 hmac = new();
byte[] actual = hmac.ComputeHash(data, key);
Assert.Equal(expected, actual);
}
Expand All @@ -138,7 +137,7 @@ public void ComputeHash_Empty_WithKey_Test(byte[] key, byte[] data)
public static TheoryData GetHmacSha_Rfc_Cases()
{
// Test cases are taken from https://tools.ietf.org/html/rfc4231
TheoryData<byte[], byte[], byte[]> result = new TheoryData<byte[], byte[], byte[]>();
TheoryData<byte[], byte[], byte[]> result = new();
foreach (var item in Helper.ReadResource<JArray>("HmacShaRfcTestData"))
{
byte[] msgBytes = Helper.HexToBytes(item["Message"].ToString());
Expand All @@ -155,7 +154,7 @@ public static TheoryData GetHmacSha_Rfc_Cases()
[MemberData(nameof(HashTestCaseHelper.GetHmacSha_Rfc_Cases), parameters: 256, MemberType = typeof(HashTestCaseHelper))]
public void ComputeHash_rfc_Test(byte[] msg, byte[] key, byte[] expected)
{
using HmacSha256 hmac = new HmacSha256();
using HmacSha256 hmac = new();
byte[] actual = hmac.ComputeHash(msg, key);
Assert.Equal(expected, actual);
}
Expand All @@ -164,7 +163,7 @@ public void ComputeHash_rfc_Test(byte[] msg, byte[] key, byte[] expected)
[MemberData(nameof(HashTestCaseHelper.GetHmacSha_Rfc_Cases), parameters: 256, MemberType = typeof(HashTestCaseHelper))]
public void ComputeHash_rfc_CtorKey_Test(byte[] msg, byte[] key, byte[] expected)
{
using HmacSha256 hmac = new HmacSha256(key);
using HmacSha256 hmac = new(key);
byte[] actual = hmac.ComputeHash(msg);
Assert.Equal(expected, actual);
}
Expand All @@ -174,7 +173,7 @@ public void ComputeHash_rfc_CtorKey_Test(byte[] msg, byte[] key, byte[] expected
[MemberData(nameof(HashTestCaseHelper.GetHmacSha_Nist_Cases), parameters: 256, MemberType = typeof(HashTestCaseHelper))]
public void ComputeHash_NIST_Test(byte[] msg, byte[] key, byte[] expected, int len, bool truncate)
{
using HmacSha256 hmac = new HmacSha256();
using HmacSha256 hmac = new();
byte[] actual = hmac.ComputeHash(msg, key);
if (truncate)
{
Expand All @@ -189,7 +188,7 @@ public void ComputeHash_NIST_Test(byte[] msg, byte[] key, byte[] expected, int l
[MemberData(nameof(HashTestCaseHelper.GetHmacSha_Nist_Cases), parameters: 256, MemberType = typeof(HashTestCaseHelper))]
public void ComputeHash_NIST_CtorKey_Test(byte[] msg, byte[] key, byte[] expected, int len, bool truncate)
{
using HmacSha256 hmac = new HmacSha256(key);
using HmacSha256 hmac = new(key);
byte[] actual = hmac.ComputeHash(msg);
if (truncate)
{
Expand All @@ -201,7 +200,7 @@ public void ComputeHash_NIST_CtorKey_Test(byte[] msg, byte[] key, byte[] expecte
}


public static TheoryData GetReuseCases()
public static TheoryData<byte[], byte[], byte[], byte[], byte[], byte[], byte[], byte[], byte[], byte[]> GetReuseCases()
{
byte[] key1 = Helper.HexToBytes("27d94d0e34b0066e29f23c9a6597cfe77a1df7e27f2740914c9a44e49c6a7b12");
byte[] data1_1 = Helper.HexToBytes("24344356060cde62834b9a1e6143f89fac4485eb8993e8a811226f74e670fbf4");
Expand Down Expand Up @@ -229,7 +228,7 @@ public static TheoryData GetReuseCases()
public void ComputeHash_CtorKey_ReuseTest(byte[] key1, byte[] data1_1, byte[] exp1_1, byte[] data1_2, byte[] exp1_2,
byte[] key2, byte[] data2_1, byte[] exp2_1, byte[] data2_2, byte[] exp2_2)
{
using HmacSha256 hmac = new HmacSha256(key1);
using HmacSha256 hmac = new(key1);
byte[] actual1_1 = hmac.ComputeHash(data1_1);
Assert.Equal(exp1_1, actual1_1);

Expand All @@ -250,7 +249,7 @@ public void ComputeHash_CtorKey_ReuseTest(byte[] key1, byte[] data1_1, byte[] ex
public void ComputeHash_Reuse_Test(byte[] key1, byte[] data1_1, byte[] exp1_1, byte[] data1_2, byte[] exp1_2,
byte[] key2, byte[] data2_1, byte[] exp2_1, byte[] data2_2, byte[] exp2_2)
{
using HmacSha256 hmac = new HmacSha256();
using HmacSha256 hmac = new();
byte[] actual1_1 = hmac.ComputeHash(data1_1, key1);
Assert.Equal(exp1_1, actual1_1);

Expand Down
30 changes: 14 additions & 16 deletions Src/Tests/Bitcoin/Cryptography/Hashing/HmacSha512Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@
// file LICENCE or http://www.opensource.org/licenses/mit-license.php.

using Autarkysoft.Bitcoin.Cryptography.Hashing;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using Xunit;

namespace Tests.Bitcoin.Cryptography.Hashing
{
Expand All @@ -28,7 +26,7 @@ public void ConstructorTest(byte[] key, bool isKeyHashed)
byte[] expectedKey = sysHmac.Key;

// Set key in constructor:
using (HmacSha512 hmac = new HmacSha512(key))
using (HmacSha512 hmac = new(key))
{
Assert.Equal(expectedKey, hmac.Key);
if (!isKeyHashed)
Expand All @@ -44,7 +42,7 @@ public void ConstructorTest(byte[] key, bool isKeyHashed)
}

// Set key using the property:
using (HmacSha512 hmac = new HmacSha512())
using (HmacSha512 hmac = new())
{
Assert.Null(hmac.Key);
hmac.Key = key;
Expand All @@ -66,15 +64,15 @@ public void Constructor_ExceptionTest()
{
Assert.Throws<ArgumentNullException>(() => new HmacSha512(null));

HmacSha512 hmac = new HmacSha512();
HmacSha512 hmac = new();
Assert.Throws<ArgumentNullException>(() => hmac.Key = null);
}


[Fact]
public void ComputeHash_ExceptionTest()
{
HmacSha512 hmac = new HmacSha512();
HmacSha512 hmac = new();

Exception ex = Assert.Throws<ArgumentNullException>(() => hmac.ComputeHash(null));
Assert.Contains("Data can not be null", ex.Message);
Expand All @@ -93,7 +91,7 @@ public void ComputeHash_ExceptionTest()
[Fact]
public void ComputeHash_WithKey_ExceptionTest()
{
HmacSha512 hmac = new HmacSha512();
HmacSha512 hmac = new();

Exception ex = Assert.Throws<ArgumentNullException>(() => hmac.ComputeHash(null, new byte[1]));
Assert.Contains("Data can not be null", ex.Message);
Expand All @@ -115,7 +113,7 @@ public void ComputeHash_EmptyTest(byte[] key, byte[] data)
using var sysHmac = new System.Security.Cryptography.HMACSHA512(key);
byte[] expected = sysHmac.ComputeHash(data);

using HmacSha512 hmac = new HmacSha512(key);
using HmacSha512 hmac = new(key);
byte[] actual = hmac.ComputeHash(data);
Assert.Equal(expected, actual);
}
Expand All @@ -129,7 +127,7 @@ public void ComputeHash_Empty_WithKey_Test(byte[] key, byte[] data)
using var sysHmac = new System.Security.Cryptography.HMACSHA512(key);
byte[] expected = sysHmac.ComputeHash(data);

using HmacSha512 hmac = new HmacSha512();
using HmacSha512 hmac = new();
byte[] actual = hmac.ComputeHash(data, key);
Assert.Equal(expected, actual);
}
Expand All @@ -139,7 +137,7 @@ public void ComputeHash_Empty_WithKey_Test(byte[] key, byte[] data)
[MemberData(nameof(HashTestCaseHelper.GetHmacSha_Rfc_Cases), parameters: 512, MemberType = typeof(HashTestCaseHelper))]
public void ComputeHash_rfc_Test(byte[] msg, byte[] key, byte[] expected)
{
using HmacSha512 hmac = new HmacSha512();
using HmacSha512 hmac = new();
byte[] actual = hmac.ComputeHash(msg, key);
Assert.Equal(expected, actual);
}
Expand All @@ -148,7 +146,7 @@ public void ComputeHash_rfc_Test(byte[] msg, byte[] key, byte[] expected)
[MemberData(nameof(HashTestCaseHelper.GetHmacSha_Rfc_Cases), parameters: 512, MemberType = typeof(HashTestCaseHelper))]
public void ComputeHash_rfc_CtorKey_Test(byte[] msg, byte[] key, byte[] expected)
{
using HmacSha512 hmac = new HmacSha512(key);
using HmacSha512 hmac = new(key);
byte[] actual = hmac.ComputeHash(msg);
Assert.Equal(expected, actual);
}
Expand All @@ -158,7 +156,7 @@ public void ComputeHash_rfc_CtorKey_Test(byte[] msg, byte[] key, byte[] expected
[MemberData(nameof(HashTestCaseHelper.GetHmacSha_Nist_Cases), parameters: 512, MemberType = typeof(HashTestCaseHelper))]
public void ComputeHash_NIST_Test(byte[] msg, byte[] key, byte[] expected, int len, bool truncate)
{
using HmacSha512 hmac = new HmacSha512();
using HmacSha512 hmac = new();
byte[] actual = hmac.ComputeHash(msg, key);
if (truncate)
{
Expand All @@ -173,7 +171,7 @@ public void ComputeHash_NIST_Test(byte[] msg, byte[] key, byte[] expected, int l
[MemberData(nameof(HashTestCaseHelper.GetHmacSha_Nist_Cases), parameters: 512, MemberType = typeof(HashTestCaseHelper))]
public void ComputeHash_NIST_CtorKey_Test(byte[] msg, byte[] key, byte[] expected, int len, bool truncate)
{
using HmacSha512 hmac = new HmacSha512(key);
using HmacSha512 hmac = new(key);
byte[] actual = hmac.ComputeHash(msg);
if (truncate)
{
Expand All @@ -185,7 +183,7 @@ public void ComputeHash_NIST_CtorKey_Test(byte[] msg, byte[] key, byte[] expecte
}


public static TheoryData GetReuseCases()
public static TheoryData<byte[], byte[], byte[], byte[], byte[], byte[], byte[], byte[], byte[], byte[]> GetReuseCases()
{
byte[] key1 = Helper.HexToBytes("27d94d0e34b0066e29f23c9a6597cfe77a1df7e27f2740914c9a44e49c6a7b12");
byte[] data1_1 = Helper.HexToBytes("24344356060cde62834b9a1e6143f89fac4485eb8993e8a811226f74e670fbf4");
Expand Down Expand Up @@ -213,7 +211,7 @@ public static TheoryData GetReuseCases()
public void ComputeHash_CtorKey_ReuseTest(byte[] key1, byte[] data1_1, byte[] exp1_1, byte[] data1_2, byte[] exp1_2,
byte[] key2, byte[] data2_1, byte[] exp2_1, byte[] data2_2, byte[] exp2_2)
{
using HmacSha512 hmac = new HmacSha512(key1);
using HmacSha512 hmac = new(key1);
byte[] actual1_1 = hmac.ComputeHash(data1_1);
Assert.Equal(exp1_1, actual1_1);

Expand All @@ -234,7 +232,7 @@ public void ComputeHash_CtorKey_ReuseTest(byte[] key1, byte[] data1_1, byte[] ex
public void ComputeHash_Reuse_Test(byte[] key1, byte[] data1_1, byte[] exp1_1, byte[] data1_2, byte[] exp1_2,
byte[] key2, byte[] data2_1, byte[] exp2_1, byte[] data2_2, byte[] exp2_2)
{
using HmacSha512 hmac = new HmacSha512();
using HmacSha512 hmac = new();
byte[] actual1_1 = hmac.ComputeHash(data1_1, key1);
Assert.Equal(exp1_1, actual1_1);

Expand Down
22 changes: 10 additions & 12 deletions Src/Tests/Bitcoin/Cryptography/Hashing/Ripemd160Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
using Autarkysoft.Bitcoin.Cryptography.Hashing;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Text;
using Xunit;

namespace Tests.Bitcoin.Cryptography.Hashing
{
Expand All @@ -17,7 +17,7 @@ public class Ripemd160Tests
[MemberData(nameof(HashTestCaseHelper.GetCommonHashCases), "RIPEMD160", MemberType = typeof(HashTestCaseHelper))]
public void ComputeHashTest(byte[] message, byte[] expectedHash)
{
using Ripemd160 rip = new Ripemd160();
using Ripemd160 rip = new();
byte[] actualHash = rip.ComputeHash(message);
Assert.Equal(expectedHash, actualHash);
}
Expand All @@ -32,33 +32,31 @@ private static byte[] GetBytes(int len)

return result;
}
public static TheoryData GetProgressiveCase()
public static IEnumerable<object[]> GetProgressiveCase()
{
TheoryData<byte[], byte[]> result = new TheoryData<byte[], byte[]>();
int len = 1;
// Hash values were computed using .Net framework 4.7.2 System.Security.Cryptography.RIPEMD160Managed
foreach (var item in Helper.ReadResource<JArray>("Ripemd160ProgressiveTestData"))
{
byte[] msgBytes = GetBytes(len++);
byte[] hashBytes = Helper.HexToBytes(item.ToString());

result.Add(msgBytes, hashBytes);
yield return new object[] { msgBytes, hashBytes };
}
return result;
}
[Theory]
[MemberData(nameof(GetProgressiveCase))]
public void ComputeHash_ProgressiveTest(byte[] message, byte[] expectedHash)
{
using Ripemd160 rip = new Ripemd160();
using Ripemd160 rip = new();
byte[] actualHash = rip.ComputeHash(message);
Assert.Equal(expectedHash, actualHash);
}

[Fact]
public void ComputeHash_AMillionATest()
{
using Ripemd160 rip = new Ripemd160();
using Ripemd160 rip = new();
byte[] actualHash = rip.ComputeHash(HashTestCaseHelper.GetAMillionA());
byte[] expectedHash = Helper.HexToBytes("52783243c1697bdbe16d37f97f68f08325dc1528");

Expand All @@ -74,7 +72,7 @@ public void ComputeHash_ReuseTest()
byte[] exp1 = Helper.HexToBytes("37f332f68db77bd9d7edd4969571ad671cf9dd3b");
byte[] exp2 = Helper.HexToBytes("132072df690933835eb8b6ad0b77e7b6f14acad7");

using Ripemd160 rip = new Ripemd160();
using Ripemd160 rip = new();
byte[] act1 = rip.ComputeHash(msg1);
byte[] act2 = rip.ComputeHash(msg2);

Expand All @@ -85,7 +83,7 @@ public void ComputeHash_ReuseTest()
[Fact]
public void ComputeHash_WithIndexTest()
{
using Ripemd160 rip = new Ripemd160();
using Ripemd160 rip = new();
byte[] data = Helper.HexToBytes("123fab54686520717569636b2062726f776e20666f78206a756d7073206f76657220746865206c617a7920646f67f3a25c92");
byte[] actualHash = rip.ComputeHash(data, 3, 43);
byte[] expectedHash = Helper.HexToBytes("37f332f68db77bd9d7edd4969571ad671cf9dd3b");
Expand All @@ -97,8 +95,8 @@ public void ComputeHash_WithIndexTest()
[Fact]
public void ComputeHash_ExceptionsTest()
{
byte[] goodBa = { 1, 2, 3 };
Ripemd160 rip = new Ripemd160();
byte[] goodBa = [1, 2, 3];
Ripemd160 rip = new();

Assert.Throws<ArgumentNullException>(() => rip.ComputeHash(null));
Assert.Throws<ArgumentNullException>(() => rip.ComputeHash(null, 0, 1));
Expand Down

0 comments on commit eae2229

Please sign in to comment.