Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
15 commits
Select commit Hold shift + click to select a range
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
113 changes: 113 additions & 0 deletions PolyShim.Tests/Net50/MD5Tests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
using System;
using System.Security.Cryptography;
using FluentAssertions;
using Xunit;

namespace PolyShim.Tests.Net50;

public class MD5Tests
{
// MD5([1, 2, 3, 4, 5])
private static readonly byte[] ExpectedHash =
Comment thread
Tyrrrz marked this conversation as resolved.
Outdated
[
0x7c,
0xfd,
0xd0,
0x78,
0x89,
0xb3,
0x29,
0x5d,
0x6a,
0x55,
0x09,
0x14,
0xab,
0x35,
0xe0,
0x68,
];

[Fact]
public void HashData_Array_Test()
Comment thread
Tyrrrz marked this conversation as resolved.
Outdated
{
// Arrange
var data = new byte[] { 1, 2, 3, 4, 5 };

// Act
var hash = MD5.HashData(data);

// Assert
hash.Should().Equal(ExpectedHash);
}

[Fact]
public void HashData_Span_Test()
{
// Arrange
var data = new byte[] { 1, 2, 3, 4, 5 };

// Act
var hash = MD5.HashData(data.AsSpan());

// Assert
hash.Should().Equal(ExpectedHash);
}

[Fact]
public void HashData_Span_WithDestination_Test()
{
// Arrange
var data = new byte[] { 1, 2, 3, 4, 5 };
var destination = new byte[16];

// Act
var bytesWritten = MD5.HashData(data.AsSpan(), destination.AsSpan());

// Assert
bytesWritten.Should().Be(16);
destination.Should().Equal(ExpectedHash);
}

[Fact]
public void HashData_Span_WithDestination_TooSmall_Test()
{
// Arrange
var data = new byte[] { 1, 2, 3, 4, 5 };
var destination = new byte[8];

// Act & assert
Assert.Throws<ArgumentException>(() => MD5.HashData(data.AsSpan(), destination.AsSpan()));
}

[Fact]
public void TryHashData_Test()
{
// Arrange
var data = new byte[] { 1, 2, 3, 4, 5 };
var destination = new byte[16];

// Act
var result = MD5.TryHashData(data.AsSpan(), destination.AsSpan(), out var bytesWritten);

// Assert
result.Should().BeTrue();
bytesWritten.Should().Be(16);
destination.Should().Equal(ExpectedHash);
}

[Fact]
public void TryHashData_TooSmall_Test()
{
// Arrange
var data = new byte[] { 1, 2, 3, 4, 5 };
var destination = new byte[8];

// Act
var result = MD5.TryHashData(data.AsSpan(), destination.AsSpan(), out var bytesWritten);

// Assert
result.Should().BeFalse();
bytesWritten.Should().Be(0);
}
}
117 changes: 117 additions & 0 deletions PolyShim.Tests/Net50/SHA1Tests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
using System;
using System.Security.Cryptography;
using FluentAssertions;
using Xunit;

namespace PolyShim.Tests.Net50;

public class SHA1Tests
{
// SHA1([1, 2, 3, 4, 5])
private static readonly byte[] ExpectedHash =
[
0x11,
0x96,
0x6a,
0xb9,
0xc0,
0x99,
0xf8,
0xfa,
0xbe,
0xfa,
0xc5,
0x4c,
0x08,
0xd5,
0xbe,
0x2b,
0xd8,
0xc9,
0x03,
0xaf,
];

[Fact]
public void HashData_Array_Test()
{
// Arrange
var data = new byte[] { 1, 2, 3, 4, 5 };

// Act
var hash = SHA1.HashData(data);

// Assert
hash.Should().Equal(ExpectedHash);
}

[Fact]
public void HashData_Span_Test()
{
// Arrange
var data = new byte[] { 1, 2, 3, 4, 5 };

// Act
var hash = SHA1.HashData(data.AsSpan());

// Assert
hash.Should().Equal(ExpectedHash);
}

[Fact]
public void HashData_Span_WithDestination_Test()
{
// Arrange
var data = new byte[] { 1, 2, 3, 4, 5 };
var destination = new byte[20];

// Act
var bytesWritten = SHA1.HashData(data.AsSpan(), destination.AsSpan());

// Assert
bytesWritten.Should().Be(20);
destination.Should().Equal(ExpectedHash);
}

[Fact]
public void HashData_Span_WithDestination_TooSmall_Test()
{
// Arrange
var data = new byte[] { 1, 2, 3, 4, 5 };
var destination = new byte[10];

// Act & assert
Assert.Throws<ArgumentException>(() => SHA1.HashData(data.AsSpan(), destination.AsSpan()));
}

[Fact]
public void TryHashData_Test()
{
// Arrange
var data = new byte[] { 1, 2, 3, 4, 5 };
var destination = new byte[20];

// Act
var result = SHA1.TryHashData(data.AsSpan(), destination.AsSpan(), out var bytesWritten);

// Assert
result.Should().BeTrue();
bytesWritten.Should().Be(20);
destination.Should().Equal(ExpectedHash);
}

[Fact]
public void TryHashData_TooSmall_Test()
{
// Arrange
var data = new byte[] { 1, 2, 3, 4, 5 };
var destination = new byte[10];

// Act
var result = SHA1.TryHashData(data.AsSpan(), destination.AsSpan(), out var bytesWritten);

// Assert
result.Should().BeFalse();
bytesWritten.Should().Be(0);
}
}
131 changes: 131 additions & 0 deletions PolyShim.Tests/Net50/SHA256Tests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
using System;
using System.Security.Cryptography;
using FluentAssertions;
using Xunit;

namespace PolyShim.Tests.Net50;

public class SHA256Tests
{
// SHA256([1, 2, 3, 4, 5])
private static readonly byte[] ExpectedHash =
[
0x74,
0xf8,
0x1f,
0xe1,
0x67,
0xd9,
0x9b,
0x4c,
0xb4,
0x1d,
0x6d,
0x0c,
0xcd,
0xa8,
0x22,
0x78,
0xca,
0xee,
0x9f,
0x3e,
0x2f,
0x25,
0xd5,
0xe5,
0xa3,
0x93,
0x6f,
0xf3,
0xdc,
0xec,
0x60,
0xd0,
];

[Fact]
public void HashData_Array_Test()
{
// Arrange
var data = new byte[] { 1, 2, 3, 4, 5 };

// Act
var hash = SHA256.HashData(data);

// Assert
hash.Should().Equal(ExpectedHash);
}

[Fact]
public void HashData_Span_Test()
{
// Arrange
var data = new byte[] { 1, 2, 3, 4, 5 };

// Act
var hash = SHA256.HashData(data.AsSpan());

// Assert
hash.Should().Equal(ExpectedHash);
}

[Fact]
public void HashData_Span_WithDestination_Test()
{
// Arrange
var data = new byte[] { 1, 2, 3, 4, 5 };
var destination = new byte[32];

// Act
var bytesWritten = SHA256.HashData(data.AsSpan(), destination.AsSpan());

// Assert
bytesWritten.Should().Be(32);
destination.Should().Equal(ExpectedHash);
}

[Fact]
public void HashData_Span_WithDestination_TooSmall_Test()
{
// Arrange
var data = new byte[] { 1, 2, 3, 4, 5 };
var destination = new byte[16];

// Act & assert
Assert.Throws<ArgumentException>(() =>
SHA256.HashData(data.AsSpan(), destination.AsSpan())
);
}

[Fact]
public void TryHashData_Test()
{
// Arrange
var data = new byte[] { 1, 2, 3, 4, 5 };
var destination = new byte[32];

// Act
var result = SHA256.TryHashData(data.AsSpan(), destination.AsSpan(), out var bytesWritten);

// Assert
result.Should().BeTrue();
bytesWritten.Should().Be(32);
destination.Should().Equal(ExpectedHash);
}

[Fact]
public void TryHashData_TooSmall_Test()
{
// Arrange
var data = new byte[] { 1, 2, 3, 4, 5 };
var destination = new byte[16];

// Act
var result = SHA256.TryHashData(data.AsSpan(), destination.AsSpan(), out var bytesWritten);

// Assert
result.Should().BeFalse();
bytesWritten.Should().Be(0);
}
}
Loading