first commit

This commit is contained in:
2023-12-14 21:48:48 +04:00
commit ef5d53a67f
22 changed files with 1387 additions and 0 deletions

View File

@@ -0,0 +1,79 @@
namespace Just.Core.Tests.Base32Conversions;
public class Decode
{
[Theory]
[InlineData(15243)]
[InlineData(812010)]
[InlineData(97331334)]
[InlineData(20354)]
public void WhenEncodedToString_ShouldBeDecodedToTheSameByteArray(int seed)
{
var rng = new Random(seed);
for (int i = 1; i <= 512; i++)
{
var testBytes = new byte[i];
rng.NextBytes(testBytes);
var resultString = Base32.Encode(testBytes);
var resultBytes = Base32.Decode(resultString);
resultBytes.Should().BeEquivalentTo(testBytes);
}
}
[Theory]
[InlineData("FG4M3ZQM3TVWDMBUP5L7N7V3JS7KBM2E", new byte[] { 0x29, 0xb8, 0xcd, 0xe6, 0x0c, 0xdc, 0xeb, 0x61, 0xb0, 0x34, 0x7f, 0x57, 0xf6, 0xfe, 0xbb, 0x4c, 0xbe, 0xa0, 0xb3, 0x44, })]
[InlineData("WXYEOQUZULMCY6ZQTDOLTRUZZMKQ====", new byte[] { 0xb5, 0xf0, 0x47, 0x42, 0x99, 0xa2, 0xd8, 0x2c, 0x7b, 0x30, 0x98, 0xdc, 0xb9, 0xc6, 0x99, 0xcb, 0x15, })]
[InlineData("2IO2HTALCXZWCBD2", new byte[] { 0xd2, 0x1d, 0xa3, 0xcc, 0x0b, 0x15, 0xf3, 0x61, 0x04, 0x7a, })]
[InlineData("ZFXJMF5N", new byte[] { 0b11001001, 0b01101110, 0b10010110, 0b00010111, 0b10101101, })]
[InlineData("CPIKTMY=", new byte[] { 0b00010011, 0b11010000, 0b10101001, 0b10110011, })]
[InlineData("JVNJA===", new byte[] { 0b01001101, 0b01011010, 0b10010000, })]
[InlineData("74OQ====", new byte[] { 0b11111111, 0b00011101, })]
public void WhenCalledWithValidString_ShouldReturnValidByteArray(string str, byte[] expected)
{
var actualBytesArray = Base32.Decode(str);
actualBytesArray.Should().Equal(expected);
}
[Theory]
[InlineData("ZFXJMF5N====", new byte[] { 0b11001001, 0b01101110, 0b10010110, 0b00010111, 0b10101101, })]
[InlineData("CPIKTMY=====", new byte[] { 0b00010011, 0b11010000, 0b10101001, 0b10110011, })]
[InlineData("JVNJA=======", new byte[] { 0b01001101, 0b01011010, 0b10010000, })]
public void WhenCalledWithValidStringThatEndsWithPaddingSign_ShouldReturnValidByteArray(string testString, byte[] expected)
{
var actualBytesArray = Base32.Decode(testString);
actualBytesArray.Should().Equal(expected);
}
[Theory]
[InlineData("ZFXJMF5N", new byte[] { 0b11001001, 0b01101110, 0b10010110, 0b00010111, 0b10101101, })]
[InlineData("CPIKTMY", new byte[] { 0b00010011, 0b11010000, 0b10101001, 0b10110011, })]
[InlineData("JVNJA", new byte[] { 0b01001101, 0b01011010, 0b10010000, })]
public void WhenCalledWithValidStringWithoutPaddingSign_ShouldReturnValidByteArray(string testString, byte[] expected)
{
var actualBytesArray = Base32.Decode(testString);
actualBytesArray.Should().Equal(expected);
}
[Theory]
[InlineData(" ")]
[InlineData("hg2515i3215")]
[InlineData("hg712)21")]
[InlineData("hg712f 21")]
[System.Diagnostics.CodeAnalysis.SuppressMessage("Performance", "CA1806:Do not ignore method results", Justification = "Test case")]
public void WhenCalledWithNotValidString_ShouldThrowFormatException(string testString)
{
Action action = () => Base32.Decode(testString);
action.Should().Throw<FormatException>();
}
[Theory]
[InlineData(null)]
[InlineData("")]
public void WhenCalledWithNullString_ShouldReturnEmptyArray(string testString)
{
Base32.Decode(testString).Should().BeEmpty();
}
}

View File

@@ -0,0 +1,56 @@
namespace Just.Core.Tests.Base32Conversions;
public class Encode
{
[Theory]
[InlineData("TQ======")]
[InlineData("3X3A====")]
[InlineData("426G6===")]
[InlineData("C3V3Y===")]
[InlineData("2JDBWKVU")]
[InlineData("ELCQNL477Q======")]
[InlineData("52Q2JBWZ36AQ====")]
[InlineData("INANSVEXD4FQ====")]
[InlineData("5HHUA5YXV5EEQ===")]
[InlineData("K7QEZW5N7H4TGIQ=")]
[InlineData("HVC7AUT2MZA646QS")]
[InlineData("ZHMQCLPPTQMSDSFKMI======")]
[InlineData("C4GQGXL5ZPMB5PIRAE======")]
[InlineData("OD4LXSFOWCJCVRZG63OA====")]
[InlineData("7XLZBFDEWHSU2ZZWE7XYK===")]
[InlineData("DCIEHJ672Z3DSGAR762Z6===")]
[InlineData("4OY6MNQ5R35Y3OK23U66CDI=")]
[InlineData("GSHXGB5ORKNDSFLSU2YWALI=")]
[InlineData("TMFSC64ZZNPQSNGCFIODS7TR")]
[InlineData("ZTDUBU4QZFFMDJKBII334EIB")]
public void WhenDecodedFromString_ShouldBeEncodedToTheSameString(string testString)
{
var resultBytes = Base32.Decode(testString);
var resultString = Base32.Encode(resultBytes);
resultString.Should().BeEquivalentTo(testString);
}
[Theory]
[InlineData("FG4M3ZQM3TVWDMBUP5L7N7V3JS7KBM2E", new byte[] { 0x29, 0xb8, 0xcd, 0xe6, 0x0c, 0xdc, 0xeb, 0x61, 0xb0, 0x34, 0x7f, 0x57, 0xf6, 0xfe, 0xbb, 0x4c, 0xbe, 0xa0, 0xb3, 0x44, })]
[InlineData("WXYEOQUZULMCY6ZQTDOLTRUZZMKQ====", new byte[] { 0xb5, 0xf0, 0x47, 0x42, 0x99, 0xa2, 0xd8, 0x2c, 0x7b, 0x30, 0x98, 0xdc, 0xb9, 0xc6, 0x99, 0xcb, 0x15, })]
[InlineData("2IO2HTALCXZWCBD2", new byte[] { 0xd2, 0x1d, 0xa3, 0xcc, 0x0b, 0x15, 0xf3, 0x61, 0x04, 0x7a, })]
[InlineData("ZFXJMF5N", new byte[] { 0b11001001, 0b01101110, 0b10010110, 0b00010111, 0b10101101, })]
[InlineData("CPIKTMY=", new byte[] { 0b00010011, 0b11010000, 0b10101001, 0b10110011, })]
[InlineData("JVNJA===", new byte[] { 0b01001101, 0b01011010, 0b10010000, })]
[InlineData("74OQ====", new byte[] { 0b11111111, 0b00011101, })]
public void WhenCalledWithNotEmptyByteArray_ShouldReturnValidString(string expected, byte[] testArray)
{
var str = Base32.Encode(testArray);
str.Should().Be(expected);
}
[Theory]
[InlineData(new byte[] { })]
[InlineData(null)]
public void WhenCalledWithEmptyByteArray_ShouldReturnEmptyString(byte[] testArray)
{
var actualBase32 = Base32.Encode(testArray);
actualBase32?.Should().Be(string.Empty);
}
}

View File

@@ -0,0 +1,53 @@
namespace Just.Core.Tests.Base64UrlConversions;
public class Decode
{
[Theory]
[InlineData(72121)]
[InlineData(554121)]
[InlineData(100454567)]
[InlineData(3210589)]
public void WhenEncodedToString_ShouldBeDecodedToTheSameByteArray(int seed)
{
var rng = new Random(seed);
for (int i = 1; i <= 512; i++)
{
var testBytes = new byte[i];
rng.NextBytes(testBytes);
var resultString = Base64Url.Encode(testBytes);
var resultBytes = Base64Url.Decode(resultString);
resultBytes.Should().BeEquivalentTo(testBytes);
}
}
[Theory]
[InlineData("5QrdUxDUVkCAEGw8pvLsEw", "53dd0ae5-d410-4056-8010-6c3ca6f2ec13")]
[InlineData("6nE2uKQ4_0ar9kpmybgkdw", "b83671ea-38a4-46ff-abf6-4a66c9b82477")]
[InlineData("PyD6zwDqXkGbS1HPsp41wQ", "cffa203f-ea00-415e-9b4b-51cfb29e35c1")]
[InlineData("AdOlPOh3wEe9PlyQgTMt2g", "3ca5d301-77e8-47c0-bd3e-5c9081332dda")]
[InlineData("0elO0Lr-UkWwTarBeY6HRA", "d04ee9d1-feba-4552-b04d-aac1798e8744")]
public void WhenCalled_ShouldReturnValidGuid(string testString, string expectedStr)
{
var result = Base64Url.DecodeGuid(testString);
var expected = Guid.Parse(expectedStr);
result.Should().Be(expected);
}
[Theory]
[InlineData("IA", new byte[]{ 0x20, })]
[InlineData("Ag", new byte[]{ 0x02, })]
[InlineData("ELg", new byte[]{ 0x10, 0xb8, })]
[InlineData("Vv0", new byte[]{ 0x56, 0xfd, })]
[InlineData("aVLO", new byte[]{ 0x69, 0x52, 0xce, })]
[InlineData("Ww2w", new byte[]{ 0x5b, 0x0d, 0xb0, })]
[InlineData("UKO0cR-OjLiM", new byte[]{ 0x50, 0xa3, 0xb4, 0x71, 0x1f, 0x8e, 0x8c, 0xb8, 0x8c, })]
[InlineData("hOb_nnjJRirORuTzYA", new byte[]{ 0x84, 0xe6, 0xff, 0x9e, 0x78, 0xc9, 0x46, 0x2a, 0xce, 0x46, 0xe4, 0xf3, 0x60, })]
public void WhenCalled_ShouldReturnValidBytes(string testString, byte[] expected)
{
var result = Base64Url.Decode(testString);
result.Should().BeEquivalentTo(expected);
}
}

View File

@@ -0,0 +1,32 @@
namespace Just.Core.Tests.Base64UrlConversions;
public class Encode
{
[Theory]
[InlineData("5QrdUxDUVkCAEGw8pvLsEw", "53dd0ae5-d410-4056-8010-6c3ca6f2ec13")]
[InlineData("6nE2uKQ4_0ar9kpmybgkdw", "b83671ea-38a4-46ff-abf6-4a66c9b82477")]
[InlineData("PyD6zwDqXkGbS1HPsp41wQ", "cffa203f-ea00-415e-9b4b-51cfb29e35c1")]
[InlineData("AdOlPOh3wEe9PlyQgTMt2g", "3ca5d301-77e8-47c0-bd3e-5c9081332dda")]
[InlineData("0elO0Lr-UkWwTarBeY6HRA", "d04ee9d1-feba-4552-b04d-aac1798e8744")]
public void WhenCalledWithGuid_ShouldReturnValidString(string expected, string testGuidString)
{
var testGuid = Guid.Parse(testGuidString);
var result = Base64Url.Encode(testGuid);
result.Should().Be(expected);
}
[Theory]
[InlineData("IA", new byte[]{ 0x20, })]
[InlineData("Ag", new byte[]{ 0x02, })]
[InlineData("ELg", new byte[]{ 0x10, 0xb8, })]
[InlineData("Vv0", new byte[]{ 0x56, 0xfd, })]
[InlineData("aVLO", new byte[]{ 0x69, 0x52, 0xce, })]
[InlineData("Ww2w", new byte[]{ 0x5b, 0x0d, 0xb0, })]
[InlineData("UKO0cR-OjLiM", new byte[]{ 0x50, 0xa3, 0xb4, 0x71, 0x1f, 0x8e, 0x8c, 0xb8, 0x8c, })]
[InlineData("hOb_nnjJRirORuTzYA", new byte[]{ 0x84, 0xe6, 0xff, 0x9e, 0x78, 0xc9, 0x46, 0x2a, 0xce, 0x46, 0xe4, 0xf3, 0x60, })]
public void WhenCalled_ShouldReturnValidString(string expected, byte[] testBytes)
{
var result = Base64Url.Encode(testBytes);
result.Should().Be(expected);
}
}

View File

@@ -0,0 +1,34 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<AssemblyName>Just.Core.Tests</AssemblyName>
<RootNamespace>Just.Core.Tests</RootNamespace>
<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="FluentAssertions" Version="6.12.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.0" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="6.0.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Core\Core.csproj" />
</ItemGroup>
</Project>

View File

@@ -0,0 +1,2 @@
global using Xunit;
global using FluentAssertions;