Skip to content

Commit

Permalink
Add CsCheck for property based testing (#371)
Browse files Browse the repository at this point in the history
- Remove FsCheck.
- Add CsCheck.
- Add a first property based test.

Partially closes #370.
  • Loading branch information
veikkoeeva authored Aug 22, 2024
1 parent c9106e5 commit b0870e9
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 215 deletions.
4 changes: 2 additions & 2 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
<ItemGroup>
<PackageVersion Include="BenchmarkDotNet" Version="0.14.0" />
<PackageVersion Include="coverlet.collector" Version="6.0.2" />
<PackageVersion Include="dotNetRdf" Version="3.2.1" />
<PackageVersion Include="FsCheck.Xunit" Version="2.16.6" />
<PackageVersion Include="dotNetRdf" Version="3.2.1" />
<PackageVersion Include="JunitXml.TestLogger" Version="4.0.254" />
<PackageVersion Include="LiquidTestReports.Markdown" Version="1.4.3-beta" />
<PackageVersion Include="Microsoft.CodeCoverage" Version="17.10.0" />
Expand All @@ -29,5 +28,6 @@
<PackageVersion Include="xunit.analyzers" Version="1.15.0" />
<PackageVersion Include="xunit.extensibility.execution" Version="2.9.0" />
<PackageVersion Include="xunit.runner.visualstudio" Version="2.8.2" />
<PackageVersion Include="CsCheck" Version="3.2.2" />
</ItemGroup>
</Project>
2 changes: 1 addition & 1 deletion NuGet.config
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<certificate fingerprint="5a2901d6ada3d18260b9c6dfe2133c95d74b9eef6ae0e5dc334c8454d1477df4" hashAlgorithm="SHA256" allowUntrustedRoot="false" />
<certificate fingerprint="0E5F38F57DC1BCC806D8494F4F90FBCEDD988B46760709CBEEC6F4219AA6157D" hashAlgorithm="SHA256" allowUntrustedRoot="false" />
<certificate fingerprint="1F4B311D9ACC115C8DC8018B5A49E00FCE6DA8E2855F9F014CA6F34570BC482D" hashAlgorithm="SHA256" allowUntrustedRoot="false" />
<owners>pshkarin;Microsoft;9ee1;commandlineparser;DotLiquid;roastedamoeba;NightOwl888;FlorianRappl;wtfsck;zzzprojects;SharpDevelop;jedisct1;xoofx;ApacheLuceneNet;tssdotmsr;Microsoft;xunit;Coverlet;xUnit.net;dotnetfoundation;albi05;jd.cain.jr;joelhulen;aarnott;AndreyAkinshin;dotnetrdf;kurt;codito;kurtmkurtm;nsec;clairernovotny;Metalnem;sil-lsdev;sedatk;spectresystems;winsharpfuzz;</owners>
<owners>AnthonyLloyd;pshkarin;Microsoft;9ee1;commandlineparser;DotLiquid;roastedamoeba;NightOwl888;FlorianRappl;wtfsck;zzzprojects;SharpDevelop;jedisct1;xoofx;ApacheLuceneNet;tssdotmsr;Microsoft;xunit;Coverlet;xUnit.net;dotnetfoundation;albi05;jd.cain.jr;joelhulen;aarnott;AndreyAkinshin;dotnetrdf;kurt;codito;kurtmkurtm;nsec;clairernovotny;Metalnem;sil-lsdev;sedatk;spectresystems;winsharpfuzz;</owners>
</repository>
</trustedSigners>
</configuration>
48 changes: 48 additions & 0 deletions test/Verifiable.Tests/EllipticCurveUtilitiesPropertyTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using CsCheck;
using Verifiable.Core.Cryptography;
using Xunit;

namespace Verifiable.Tests.Cryptography
{
/// <summary>
/// Property based tests on elliptic curve utilities.
/// </summary>
public class EllipticCurveUtilitiesPropertyTests
{
[Fact]
public void CompressionDecompressionShouldBeInverseIfOnTheCurve()
{
var curveTypeGen = Gen.Enum<EllipticCurveTypes>().Where(curve => curve != EllipticCurveTypes.None && curve != EllipticCurveTypes.Curve25519);

//Generator for random elliptic curve points.
var pointGen = Gen.Byte.Array[32].SelectMany(x => Gen.Byte.Array[32], (x, y) => (X: x, Y: y));

//Combines the curve type generator with the point generator.
var testDataGen = curveTypeGen.SelectMany(curveType => pointGen,
(curveType, point) =>
{
return (curveType, point.X, point.Y);
});

testDataGen.Sample(testData =>
{
var curveType = testData.curveType;
byte[] publicKeyX = testData.X;
byte[] publicKeyY = testData.Y;

//Check the pont is on the curve. If not, there is no point in testing compression/decompression.
if(EllipticCurveUtilities.CheckPointOnCurve(publicKeyX, publicKeyY, curveType))
{
//Compress the elliptic curve point.
byte[] compressedPoint = EllipticCurveUtilities.Compress(publicKeyX, publicKeyY);

//Decompress the point.
byte[] decompressedY = EllipticCurveUtilities.Decompress(compressedPoint, curveType);

//Ensure the decompressed Y matches the original Y.
Assert.Equal(publicKeyY, decompressedY);
}
});
}
}
}
5 changes: 3 additions & 2 deletions test/Verifiable.Tests/EllipticCurveUtilitiesTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using CsCheck;
using System;
using System.Linq;
using System.Runtime.Versioning;
using System.Security.Cryptography;
Expand Down Expand Up @@ -115,7 +116,7 @@ public void EllipticPointOnCurveCheckSucceeds(EllipticCurveTestData td)
var curveType = td.CurveIdentifier.Equals(EllipticCurveTheoryData.EllipticSecP256k1, StringComparison.OrdinalIgnoreCase) ? EllipticCurveTypes.Secp256k1 : EllipticCurveTypes.NistCurves;
CheckPointOnCurveForEvenAndOdd(td.PublicKeyMaterialX, td.PublicKeyMaterialY, curveType, primeBytes, isEven: td.IsEven);
}


private static void CheckPointOnCurveForEvenAndOdd(ReadOnlySpan<byte> publicKeyX, ReadOnlySpan<byte> publicKeyY, EllipticCurveTypes curveType, ReadOnlySpan<byte> primeBytes, bool isEven)
{
Expand Down
2 changes: 1 addition & 1 deletion test/Verifiable.Tests/Verifiable.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<PackageReference Include="JunitXml.TestLogger" />
<PackageReference Include="SharpFuzz" />
<PackageReference Include="WinSharpFuzz" />
<PackageReference Include="FsCheck.Xunit" />
<PackageReference Include="CsCheck" />
<PackageReference Include="xunit" />
<PackageReference Include="xunit.extensibility.execution" />
<PackageReference Include="xunit.analyzers">
Expand Down
215 changes: 6 additions & 209 deletions test/Verifiable.Tests/packages.lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@
"resolved": "6.0.2",
"contentHash": "bJShQ6uWRTQ100ZeyiMqcFlhP7WJ+bCuabUs885dJiBEzMsJMSFr7BOyeCw4rgvQokteGi5rKQTlkhfQPUXg2A=="
},
"CsCheck": {
"type": "Direct",
"requested": "[3.2.2, )",
"resolved": "3.2.2",
"contentHash": "Vfdi6NDDBKMr8EwWV5GWxOMNvcNgwgZHp5ipkwBuhQPXob5YW8WCm1r2yYh+RV2sreNZpXU6jAGCfY7Ik0ukHA=="
},
"dotNetRdf": {
"type": "Direct",
"requested": "[3.2.1, )",
Expand All @@ -28,16 +34,6 @@
"dotNetRdf.Writing.HtmlSchema": "3.2.1"
}
},
"FsCheck.Xunit": {
"type": "Direct",
"requested": "[2.16.6, )",
"resolved": "2.16.6",
"contentHash": "Gd8xOmsHCfi5NR3tDWTia8VGc81jgA+ca7n8RCzdPnYUlG1/WbAy2WG9PGM9DFP4ft23JeO7+9xpE/7kqujVBw==",
"dependencies": {
"FsCheck": "[2.16.6]",
"xunit.extensibility.execution": "[2.2.0, 3.0.0)"
}
},
"JunitXml.TestLogger": {
"type": "Direct",
"requested": "[4.0.254, )",
Expand Down Expand Up @@ -283,44 +279,6 @@
"dotNetRdf.Core": "3.2.1"
}
},
"FsCheck": {
"type": "Transitive",
"resolved": "2.16.6",
"contentHash": "8yQlt7HgoigIXIoe7ZouNh551rypxp4WtQnqBlpB9a7gf4IkpdMSnEzsnJS/gFmeNtIf9hEJ37celeCq/6FuHA==",
"dependencies": {
"FSharp.Core": "4.2.3"
}
},
"FSharp.Core": {
"type": "Transitive",
"resolved": "4.2.3",
"contentHash": "PthzJd0cY9L7+mPHoMB0B9Z9moR4oO1rU5RDrVW44cfMm0R9AYFf+F3BsuTvP9clr9w/Wa0wbSfEwbJV8sV80A==",
"dependencies": {
"System.Collections": "4.0.11",
"System.Console": "4.0.0",
"System.Diagnostics.Debug": "4.0.11",
"System.Diagnostics.Tools": "4.0.1",
"System.Globalization": "4.0.11",
"System.IO": "4.1.0",
"System.Linq": "4.1.0",
"System.Linq.Expressions": "4.1.0",
"System.Linq.Queryable": "4.0.1",
"System.Net.Requests": "4.0.11",
"System.Reflection": "4.1.0",
"System.Reflection.Extensions": "4.0.1",
"System.Resources.ResourceManager": "4.0.1",
"System.Runtime": "4.1.0",
"System.Runtime.Extensions": "4.1.0",
"System.Runtime.Numerics": "4.0.1",
"System.Text.RegularExpressions": "4.1.0",
"System.Threading": "4.0.11",
"System.Threading.Tasks": "4.0.11",
"System.Threading.Tasks.Parallel": "4.0.1",
"System.Threading.Thread": "4.0.0",
"System.Threading.ThreadPool": "4.0.10",
"System.Threading.Timer": "4.0.1"
}
},
"HtmlAgilityPack": {
"type": "Transitive",
"resolved": "1.11.61",
Expand Down Expand Up @@ -671,18 +629,6 @@
"System.Security.Cryptography.ProtectedData": "8.0.0"
}
},
"System.Console": {
"type": "Transitive",
"resolved": "4.0.0",
"contentHash": "qSKUSOIiYA/a0g5XXdxFcUFmv1hNICBD7QZ0QhGYVipPIhvpiydY8VZqr1thmCXvmn8aipMg64zuanB4eotK9A==",
"dependencies": {
"Microsoft.NETCore.Platforms": "1.0.1",
"Microsoft.NETCore.Targets": "1.0.1",
"System.IO": "4.1.0",
"System.Runtime": "4.1.0",
"System.Text.Encoding": "4.0.11"
}
},
"System.Diagnostics.Debug": {
"type": "Transitive",
"resolved": "4.3.0",
Expand Down Expand Up @@ -710,16 +656,6 @@
"resolved": "8.0.0",
"contentHash": "fdYxcRjQqTTacKId/2IECojlDSFvp7LP5N78+0z/xH7v/Tuw5ZAxu23Y6PTCRinqyu2ePx+Gn1098NC6jM6d+A=="
},
"System.Diagnostics.Tools": {
"type": "Transitive",
"resolved": "4.0.1",
"contentHash": "xBfJ8pnd4C17dWaC9FM6aShzbJcRNMChUMD42I6772KGGrqaFdumwhn9OdM68erj1ueNo3xdQ1EwiFjK5k8p0g==",
"dependencies": {
"Microsoft.NETCore.Platforms": "1.0.1",
"Microsoft.NETCore.Targets": "1.0.1",
"System.Runtime": "4.1.0"
}
},
"System.Diagnostics.Tracing": {
"type": "Transitive",
"resolved": "4.3.0",
Expand Down Expand Up @@ -811,45 +747,6 @@
"System.Runtime.Extensions": "4.3.0"
}
},
"System.Linq.Expressions": {
"type": "Transitive",
"resolved": "4.1.0",
"contentHash": "I+y02iqkgmCAyfbqOmSDOgqdZQ5tTj80Akm5BPSS8EeB0VGWdy6X1KCoYe8Pk6pwDoAKZUOdLVxnTJcExiv5zw==",
"dependencies": {
"System.Collections": "4.0.11",
"System.Diagnostics.Debug": "4.0.11",
"System.Globalization": "4.0.11",
"System.IO": "4.1.0",
"System.Linq": "4.1.0",
"System.ObjectModel": "4.0.12",
"System.Reflection": "4.1.0",
"System.Reflection.Emit": "4.0.1",
"System.Reflection.Emit.ILGeneration": "4.0.1",
"System.Reflection.Emit.Lightweight": "4.0.1",
"System.Reflection.Extensions": "4.0.1",
"System.Reflection.Primitives": "4.0.1",
"System.Reflection.TypeExtensions": "4.1.0",
"System.Resources.ResourceManager": "4.0.1",
"System.Runtime": "4.1.0",
"System.Runtime.Extensions": "4.1.0",
"System.Threading": "4.0.11"
}
},
"System.Linq.Queryable": {
"type": "Transitive",
"resolved": "4.0.1",
"contentHash": "Yn/WfYe9RoRfmSLvUt2JerP0BTGGykCZkQPgojaxgzF2N0oPo+/AhB8TXOpdCcNlrG3VRtsamtK2uzsp3cqRVw==",
"dependencies": {
"System.Collections": "4.0.11",
"System.Diagnostics.Debug": "4.0.11",
"System.Linq": "4.1.0",
"System.Linq.Expressions": "4.1.0",
"System.Reflection": "4.1.0",
"System.Reflection.Extensions": "4.0.1",
"System.Resources.ResourceManager": "4.0.1",
"System.Runtime": "4.1.0"
}
},
"System.Memory": {
"type": "Transitive",
"resolved": "4.5.5",
Expand Down Expand Up @@ -899,49 +796,6 @@
"System.Runtime.Handles": "4.3.0"
}
},
"System.Net.Requests": {
"type": "Transitive",
"resolved": "4.0.11",
"contentHash": "vxGt7C0cZixN+VqoSW4Yakc1Y9WknmxauDqzxgpw/FnBdz4kQNN51l4wxdXX5VY1xjqy//+G+4CvJWp1+f+y6Q==",
"dependencies": {
"Microsoft.NETCore.Platforms": "1.0.1",
"System.Collections": "4.0.11",
"System.Diagnostics.Debug": "4.0.11",
"System.Diagnostics.Tracing": "4.1.0",
"System.Globalization": "4.0.11",
"System.IO": "4.1.0",
"System.Net.Http": "4.1.0",
"System.Net.Primitives": "4.0.11",
"System.Net.WebHeaderCollection": "4.0.1",
"System.Resources.ResourceManager": "4.0.1",
"System.Runtime": "4.1.0",
"System.Threading": "4.0.11",
"System.Threading.Tasks": "4.0.11"
}
},
"System.Net.WebHeaderCollection": {
"type": "Transitive",
"resolved": "4.0.1",
"contentHash": "XX2TIAN+wBSAIV51BU2FvvXMdstUa8b0FBSZmDWjZdwUMmggQSifpTOZ5fNH20z9ZCg2fkV1L5SsZnpO2RQDRQ==",
"dependencies": {
"System.Collections": "4.0.11",
"System.Resources.ResourceManager": "4.0.1",
"System.Runtime": "4.1.0",
"System.Runtime.Extensions": "4.1.0"
}
},
"System.ObjectModel": {
"type": "Transitive",
"resolved": "4.0.12",
"contentHash": "tAgJM1xt3ytyMoW4qn4wIqgJYm7L7TShRZG4+Q4Qsi2PCcj96pXN7nRywS9KkB3p/xDUjc2HSwP9SROyPYDYKQ==",
"dependencies": {
"System.Collections": "4.0.11",
"System.Diagnostics.Debug": "4.0.11",
"System.Resources.ResourceManager": "4.0.1",
"System.Runtime": "4.1.0",
"System.Threading": "4.0.11"
}
},
"System.Reflection": {
"type": "Transitive",
"resolved": "4.3.0",
Expand All @@ -959,16 +813,6 @@
"resolved": "4.7.0",
"contentHash": "VR4kk8XLKebQ4MZuKuIni/7oh+QGFmZW3qORd1GvBq/8026OpW501SzT/oypwiQl4TvT8ErnReh/NzY9u+C6wQ=="
},
"System.Reflection.Emit.ILGeneration": {
"type": "Transitive",
"resolved": "4.0.1",
"contentHash": "Ov6dU8Bu15Bc7zuqttgHF12J5lwSWyTf1S+FJouUXVMSqImLZzYaQ+vRr1rQ0OZ0HqsrwWl4dsKHELckQkVpgA==",
"dependencies": {
"System.Reflection": "4.1.0",
"System.Reflection.Primitives": "4.0.1",
"System.Runtime": "4.1.0"
}
},
"System.Reflection.Emit.Lightweight": {
"type": "Transitive",
"resolved": "4.7.0",
Expand Down Expand Up @@ -1248,19 +1092,6 @@
"resolved": "8.0.0",
"contentHash": "yev/k9GHAEGx2Rg3/tU6MQh4HGBXJs70y7j1LaM1i/ER9po+6nnQ6RRqTJn1E7Xu0fbIFK80Nh5EoODxrbxwBQ=="
},
"System.Text.RegularExpressions": {
"type": "Transitive",
"resolved": "4.1.0",
"contentHash": "i88YCXpRTjCnoSQZtdlHkAOx4KNNik4hMy83n0+Ftlb7jvV6ZiZWMpnEZHhjBp6hQVh8gWd/iKNPzlPF7iyA2g==",
"dependencies": {
"System.Collections": "4.0.11",
"System.Globalization": "4.0.11",
"System.Resources.ResourceManager": "4.0.1",
"System.Runtime": "4.1.0",
"System.Runtime.Extensions": "4.1.0",
"System.Threading": "4.0.11"
}
},
"System.Threading": {
"type": "Transitive",
"resolved": "4.3.0",
Expand All @@ -1280,21 +1111,6 @@
"System.Runtime": "4.3.0"
}
},
"System.Threading.Tasks.Parallel": {
"type": "Transitive",
"resolved": "4.0.1",
"contentHash": "7Pc9t25bcynT9FpMvkUw4ZjYwUiGup/5cJFW72/5MgCG+np2cfVUMdh29u8d7onxX7d8PS3J+wL73zQRqkdrSA==",
"dependencies": {
"System.Collections.Concurrent": "4.0.12",
"System.Diagnostics.Debug": "4.0.11",
"System.Diagnostics.Tracing": "4.1.0",
"System.Resources.ResourceManager": "4.0.1",
"System.Runtime": "4.1.0",
"System.Runtime.Extensions": "4.1.0",
"System.Threading": "4.0.11",
"System.Threading.Tasks": "4.0.11"
}
},
"System.Threading.Thread": {
"type": "Transitive",
"resolved": "4.3.0",
Expand All @@ -1303,25 +1119,6 @@
"System.Runtime": "4.3.0"
}
},
"System.Threading.ThreadPool": {
"type": "Transitive",
"resolved": "4.0.10",
"contentHash": "IMXgB5Vf/5Qw1kpoVgJMOvUO1l32aC+qC3OaIZjWJOjvcxuxNWOK2ZTWWYXfij22NHxT2j1yWX5vlAeQWld9vA==",
"dependencies": {
"System.Runtime": "4.1.0",
"System.Runtime.Handles": "4.0.1"
}
},
"System.Threading.Timer": {
"type": "Transitive",
"resolved": "4.0.1",
"contentHash": "saGfUV8uqVW6LeURiqxcGhZ24PzuRNaUBtbhVeuUAvky1naH395A/1nY0P2bWvrw/BreRtIB/EzTDkGBpqCwEw==",
"dependencies": {
"Microsoft.NETCore.Platforms": "1.0.1",
"Microsoft.NETCore.Targets": "1.0.1",
"System.Runtime": "4.1.0"
}
},
"System.ValueTuple": {
"type": "Transitive",
"resolved": "4.5.0",
Expand Down

0 comments on commit b0870e9

Please sign in to comment.