aboutsummaryrefslogtreecommitdiff
path: root/lib/Hashing.Portable/tests
diff options
context:
space:
mode:
authorLibravatar vnugent <public@vaughnnugent.com>2023-12-20 18:33:32 -0500
committerLibravatar vnugent <public@vaughnnugent.com>2023-12-20 18:33:32 -0500
commit43542a261ec0789c7e48551ea5f9eaefa8c4b772 (patch)
tree35d136732bf2a44780a156da6f9963d1ebb2201d /lib/Hashing.Portable/tests
parent546abea662263ef112c571c29706c47e875e09c4 (diff)
monocypher vendor and wrapper started, and partial public api updates
Diffstat (limited to 'lib/Hashing.Portable/tests')
-rw-r--r--lib/Hashing.Portable/tests/.runsettings8
-rw-r--r--lib/Hashing.Portable/tests/ManagedHashTests.cs95
-rw-r--r--lib/Hashing.Portable/tests/VNLib.Hashing.PortableTests.csproj27
3 files changed, 130 insertions, 0 deletions
diff --git a/lib/Hashing.Portable/tests/.runsettings b/lib/Hashing.Portable/tests/.runsettings
new file mode 100644
index 0000000..0b2d635
--- /dev/null
+++ b/lib/Hashing.Portable/tests/.runsettings
@@ -0,0 +1,8 @@
+<?xml version="1.0" encoding="utf-8"?>
+<RunSettings>
+ <RunConfiguration>
+ <EnvironmentVariables>
+ <VNLIB_MONOCYPHER_DLL_PATH>../../../../../Utils.Cryptography/monocypher/build/Debug/vnlib_monocypher.dll</VNLIB_MONOCYPHER_DLL_PATH>
+ </EnvironmentVariables>
+ </RunConfiguration>
+</RunSettings> \ No newline at end of file
diff --git a/lib/Hashing.Portable/tests/ManagedHashTests.cs b/lib/Hashing.Portable/tests/ManagedHashTests.cs
new file mode 100644
index 0000000..01b3336
--- /dev/null
+++ b/lib/Hashing.Portable/tests/ManagedHashTests.cs
@@ -0,0 +1,95 @@
+using Microsoft.VisualStudio.TestTools.UnitTesting;
+
+using System.Text;
+
+using VNLib.Utils.Memory;
+using VNLib.Utils;
+
+
+namespace VNLib.Hashing.Tests
+{
+ [TestClass()]
+ public class ManagedHashTests
+ {
+ [TestMethod()]
+ public void ComputeHashTest()
+ {
+ byte[] testData = Encoding.UTF8.GetBytes("Hello World!");
+ using UnsafeMemoryHandle<byte> heapBuffer = MemoryUtil.UnsafeAlloc(64, false);
+
+ //Test all supported algorithms
+ foreach (HashAlg alg in Enum.GetValues<HashAlg>())
+ {
+ if (alg == HashAlg.None) continue;
+
+ //Skip unsupported algorithms
+ if (alg == HashAlg.BlAKE2B && !ManagedHash.SupportsBlake2b) continue;
+
+ //Compute hash
+ ERRNO hashSize = ManagedHash.ComputeHash(testData, heapBuffer.Span, alg);
+ Assert.IsTrue(hashSize == Math.Abs(hashSize));
+
+ //With input string and heap buffer
+ hashSize = ManagedHash.ComputeHash("test", heapBuffer.Span, alg);
+ Assert.IsTrue(hashSize == Math.Abs(hashSize));
+
+ //Compute string and byte array
+ byte[] testdata = ManagedHash.ComputeHash(testData, alg);
+ Assert.IsTrue(testdata.Length == Math.Abs(hashSize));
+
+ //With input string
+ testdata = ManagedHash.ComputeHash("test", alg);
+ Assert.IsTrue(testdata.Length == Math.Abs(hashSize));
+
+ //Compute hash as string
+ string testEnc = ManagedHash.ComputeHash(testdata, alg, HashEncodingMode.Hexadecimal);
+ Assert.IsTrue(testEnc.Length == Math.Abs(hashSize) * 2);
+
+ //With input string
+ testEnc = ManagedHash.ComputeHash("test", alg, HashEncodingMode.Hexadecimal);
+ Assert.IsTrue(testEnc.Length == Math.Abs(hashSize) * 2);
+ }
+ }
+
+ [TestMethod()]
+ public void ComputeHmacTest()
+ {
+ byte[] testData = Encoding.UTF8.GetBytes("Hello World!");
+ byte[] testKey = RandomHash.GetRandomBytes(32);
+ using UnsafeMemoryHandle<byte> heapBuffer = MemoryUtil.UnsafeAlloc(64, false);
+
+ //Test all supported algorithms
+ foreach (HashAlg alg in Enum.GetValues<HashAlg>())
+ {
+ if (alg == HashAlg.None) continue;
+
+ //Skip unsupported algorithms
+ if (alg == HashAlg.BlAKE2B && !ManagedHash.SupportsBlake2b) continue;
+
+ //Compute hash
+ ERRNO hashSize = ManagedHash.ComputeHmac(testKey, testData, heapBuffer.Span, alg);
+ Assert.IsTrue(hashSize == Math.Abs(hashSize));
+
+ //With input string and heap buffer
+ hashSize = ManagedHash.ComputeHmac(testKey, "test", heapBuffer.Span, alg);
+ Assert.IsTrue(hashSize == Math.Abs(hashSize));
+
+ //Compute string and byte array
+ byte[] testdata = ManagedHash.ComputeHmac(testKey, testData, alg);
+ Assert.IsTrue(testdata.Length == Math.Abs(hashSize));
+
+ //With input string
+ testdata = ManagedHash.ComputeHmac(testKey, "test", alg);
+ Assert.IsTrue(testdata.Length == Math.Abs(hashSize));
+
+ //Compute hash as string
+ string testEnc = ManagedHash.ComputeHmac(testKey, testdata, alg, HashEncodingMode.Hexadecimal);
+ Assert.IsTrue(testEnc.Length == Math.Abs(hashSize) * 2);
+
+ //With input string
+ testEnc = ManagedHash.ComputeHmac(testKey, "test", alg, HashEncodingMode.Hexadecimal);
+ Assert.IsTrue(testEnc.Length == Math.Abs(hashSize) * 2);
+ }
+ }
+ }
+} \ No newline at end of file
diff --git a/lib/Hashing.Portable/tests/VNLib.Hashing.PortableTests.csproj b/lib/Hashing.Portable/tests/VNLib.Hashing.PortableTests.csproj
new file mode 100644
index 0000000..45a5589
--- /dev/null
+++ b/lib/Hashing.Portable/tests/VNLib.Hashing.PortableTests.csproj
@@ -0,0 +1,27 @@
+<Project Sdk="Microsoft.NET.Sdk">
+
+ <PropertyGroup>
+ <TargetFramework>net6.0</TargetFramework>
+ <ImplicitUsings>enable</ImplicitUsings>
+ <Nullable>enable</Nullable>
+
+ <IsPackable>false</IsPackable>
+ <IsTestProject>true</IsTestProject>
+ </PropertyGroup>
+
+ <PropertyGroup>
+ <RunSettingsFilePath>$(MSBuildProjectDirectory)\.runsettings</RunSettingsFilePath>
+ </PropertyGroup>
+
+ <ItemGroup>
+ <PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
+ <PackageReference Include="MSTest.TestAdapter" Version="3.1.1" />
+ <PackageReference Include="MSTest.TestFramework" Version="3.1.1" />
+ <PackageReference Include="coverlet.collector" Version="6.0.0" />
+ </ItemGroup>
+
+ <ItemGroup>
+ <ProjectReference Include="..\src\VNLib.Hashing.Portable.csproj" />
+ </ItemGroup>
+
+</Project>