Skip to content

Commit

Permalink
Improved and added new testcases for PngByteQRCode
Browse files Browse the repository at this point in the history
  • Loading branch information
codebude committed Nov 23, 2021
1 parent a1dd899 commit 951fb87
Show file tree
Hide file tree
Showing 3 changed files with 178 additions and 2 deletions.
7 changes: 5 additions & 2 deletions QRCoderTests/Helpers/HelperFunctions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,22 @@ public static string BitmapToHash(Bitmap bmp)
}
return ByteArrayToHash(imgBytes);
}
#endif

public static string ByteArrayToHash(byte[] data)
{
#if !NETCOREAPP1_1
var md5 = new MD5CryptoServiceProvider();
var hash = md5.ComputeHash(data);
#else
var hash = new SshNet.Security.Cryptography.MD5().ComputeHash(data);
#endif
return BitConverter.ToString(hash).Replace("-", "").ToLower();
}

public static string StringToHash(string data)
{
return ByteArrayToHash(Encoding.UTF8.GetBytes(data));
}
#endif

}
}
172 changes: 172 additions & 0 deletions QRCoderTests/PngByteQRCodeRendererTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
using Xunit;
using QRCoder;
using Shouldly;
using QRCoderTests.Helpers.XUnitExtenstions;
using QRCoderTests.Helpers;
#if !NETCOREAPP1_1
using System.Drawing;
using System.IO;
#endif

namespace QRCoderTests
{
/****************************************************************************************************
* Note: Test cases compare the outcome visually even if it's slower than a byte-wise compare.
* This is necessary, because the Deflate implementation differs on the different target
* platforms and thus the outcome, even if visually identical, differs. Thus only a visual
* test method makes sense. In addition bytewise differences shouldn't be important, if the
* visual outcome is identical and thus the qr code is identical/scannable.
****************************************************************************************************/
public class PngByteQRCodeRendererTests
{


[Fact]
[Category("QRRenderer/PngByteQRCode")]
public void can_render_pngbyte_qrcode_blackwhite()
{
//Create QR code
var gen = new QRCodeGenerator();
var data = gen.CreateQrCode("This is a quick test! 123#?", QRCodeGenerator.ECCLevel.L);
var pngCodeGfx = new PngByteQRCode(data).GetGraphic(5);

#if NETCOREAPP1_1
var result = HelperFunctions.ByteArrayToHash(pngCodeGfx);
result.ShouldBe("1fc35c3bea6fad47427143ce716c83b8");
#else
using (var mStream = new MemoryStream(pngCodeGfx))
{
var bmp = (Bitmap)Image.FromStream(mStream);
var result = HelperFunctions.BitmapToHash(bmp);
result.ShouldBe("18b19e6037cff06ae995d8d487b0e46e");
}
#endif
}

[Fact]
[Category("QRRenderer/PngByteQRCode")]
public void can_render_pngbyte_qrcode_color()
{
//Create QR code
var gen = new QRCodeGenerator();
var data = gen.CreateQrCode("This is a quick test! 123#?", QRCodeGenerator.ECCLevel.L);
var pngCodeGfx = new PngByteQRCode(data).GetGraphic(5, new byte[] { 255, 0, 0 }, new byte[] { 0, 0, 255 });

#if NETCOREAPP1_1
var result = HelperFunctions.ByteArrayToHash(pngCodeGfx);
result.ShouldBe("0144b1d40aa6eeb6cb07df42822ea0a7");
#else
using (var mStream = new MemoryStream(pngCodeGfx))
{
var bmp = (Bitmap)Image.FromStream(mStream);
var result = HelperFunctions.BitmapToHash(bmp);
result.ShouldBe("37ae73e90b66beac317b790be3db24cc");
}
#endif
}


[Fact]
[Category("QRRenderer/PngByteQRCode")]
public void can_render_pngbyte_qrcode_color_with_alpha()
{
//Create QR code
var gen = new QRCodeGenerator();
var data = gen.CreateQrCode("This is a quick test! 123#?", QRCodeGenerator.ECCLevel.L);
var pngCodeGfx = new PngByteQRCode(data).GetGraphic(5, new byte[] { 255, 255, 255, 127 }, new byte[] { 0, 0, 255 });

#if NETCOREAPP1_1
var result = HelperFunctions.ByteArrayToHash(pngCodeGfx);
result.ShouldBe("627ce564fb5e17be42e4a85e907a17b5");
#else
using (var mStream = new MemoryStream(pngCodeGfx))
{
var bmp = (Bitmap)Image.FromStream(mStream);
var result = HelperFunctions.BitmapToHash(bmp);
result.ShouldBe("c56c2a9535fd8e9a92a6ac9709d21e67");
}
#endif
}

[Fact]
[Category("QRRenderer/PngByteQRCode")]
public void can_render_pngbyte_qrcode_color_without_quietzones()
{
//Create QR code
var gen = new QRCodeGenerator();
var data = gen.CreateQrCode("This is a quick test! 123#?", QRCodeGenerator.ECCLevel.L);
var pngCodeGfx = new PngByteQRCode(data).GetGraphic(5, new byte[] { 255, 255, 255, 127 }, new byte[] { 0, 0, 255 }, false);

#if NETCOREAPP1_1
var result = HelperFunctions.ByteArrayToHash(pngCodeGfx);
result.ShouldBe("07f760b3eb54901840b094d31e299713");
#else
File.WriteAllBytes(@"C:\Temp\pngbyte_35.png", pngCodeGfx);
using (var mStream = new MemoryStream(pngCodeGfx))
{
var bmp = (Bitmap)Image.FromStream(mStream);
bmp.MakeTransparent(Color.Transparent);
var result = HelperFunctions.BitmapToHash(bmp);
#if NET35_OR_GREATER || NET40_OR_GREATER
result.ShouldBe("75be11d582575617d2490c54b69e844e");
#else
result.ShouldBe("fbbc8255ebf3e4f4a1d21f0dd15f76f8");
#endif
}
#endif
}

[Fact]
[Category("QRRenderer/PngByteQRCode")]
public void can_instantate_pngbyte_qrcode_parameterless()
{
var pngCode = new PngByteQRCode();
pngCode.ShouldNotBeNull();
pngCode.ShouldBeOfType<PngByteQRCode>();
}

[Fact]
[Category("QRRenderer/PngByteQRCode")]
public void can_render_pngbyte_qrcode_from_helper()
{
//Create QR code
var pngCodeGfx = PngByteQRCodeHelper.GetQRCode("This is a quick test! 123#?", QRCodeGenerator.ECCLevel.L, 10);

#if NETCOREAPP1_1
var result = HelperFunctions.ByteArrayToHash(pngCodeGfx);
result.ShouldBe("c562388f4f3cf13a299b469a3e3b852f");
#else
using (var mStream = new MemoryStream(pngCodeGfx))
{
var bmp = (Bitmap)Image.FromStream(mStream);
var result = HelperFunctions.BitmapToHash(bmp);
result.ShouldBe("1978fb11ce26acf9b6cb7490b4c44ef2");
}
#endif
}

[Fact]
[Category("QRRenderer/PngByteQRCode")]
public void can_render_pngbyte_qrcode_from_helper_2()
{
//Create QR code
var pngCodeGfx = PngByteQRCodeHelper.GetQRCode("This is a quick test! 123#?", 5, new byte[] { 255, 255, 255, 127 }, new byte[] { 0, 0, 255 }, QRCodeGenerator.ECCLevel.L);

#if NETCOREAPP1_1
var result = HelperFunctions.ByteArrayToHash(pngCodeGfx);
result.ShouldBe("627ce564fb5e17be42e4a85e907a17b5");
#else
using (var mStream = new MemoryStream(pngCodeGfx))
{
var bmp = (Bitmap)Image.FromStream(mStream);
var result = HelperFunctions.BitmapToHash(bmp);
result.ShouldBe("c56c2a9535fd8e9a92a6ac9709d21e67");
}
#endif
}

}
}



1 change: 1 addition & 0 deletions QRCoderTests/QRCoderTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="SshNet.Security.Cryptography" Version="1.3.0" />
<ProjectReference Include="..\QRCoder\QRCoder.csproj" />
</ItemGroup>
<ItemGroup>
Expand Down

0 comments on commit 951fb87

Please sign in to comment.