Skip to content

Commit

Permalink
test: future proof via MemberData
Browse files Browse the repository at this point in the history
  • Loading branch information
grosch authored and daviddesmet committed Jul 13, 2024
1 parent ef33574 commit 8b56d44
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 157 deletions.
8 changes: 1 addition & 7 deletions tests/Paseto.Tests/PaserkTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,7 @@ public class PaserkTests

public PaserkTests(ITestOutputHelper output) => _output = output;

private static readonly ProtocolVersion[] ValidProtocols =
[
ProtocolVersion.V1,
ProtocolVersion.V2,
ProtocolVersion.V3,
ProtocolVersion.V4
];
private static readonly ProtocolVersion[] ValidProtocols = Enum.GetValues<ProtocolVersion>();

private static readonly PaserkType[] SupportedPaserkTypes =
[
Expand Down
145 changes: 53 additions & 92 deletions tests/Paseto.Tests/PasetoBuilderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;

using FluentAssertions;
Expand Down Expand Up @@ -106,10 +107,7 @@ public void ShouldThrowExceptionOnGenerateSymmetricKeyWhenNoDependenciesAreProvi
}

[Theory(DisplayName = "Should throw exception on GenerateSymmetricKey when incorrect purpose is provided")]
[InlineData(ProtocolVersion.V1)]
[InlineData(ProtocolVersion.V2)]
[InlineData(ProtocolVersion.V3)]
[InlineData(ProtocolVersion.V4)]
[MemberData(nameof(AllVersionsData), MemberType = typeof(TestHelper))]
public void ShouldThrowExceptionOnGenerateSymmetricKeyWhenIncorrectPurposeIsProvided(ProtocolVersion version)
{
var incorrectPurpose = Purpose.Public;
Expand Down Expand Up @@ -151,10 +149,7 @@ public void ShouldThrowExceptionOnGenerateAsymmetricKeyPairWhenNoDependenciesAre
}

[Theory(DisplayName = "Should throw exception on GenerateAsymmetricKeyPair when incorrect purpose is provided")]
[InlineData(ProtocolVersion.V1)]
[InlineData(ProtocolVersion.V2)]
[InlineData(ProtocolVersion.V3)]
[InlineData(ProtocolVersion.V4)]
[MemberData(nameof(AllVersionsData), MemberType = typeof(TestHelper))]
public void ShouldThrowExceptionOnGenerateAsymmetricKeyPairWhenIncorrectPurposeIsProvided(ProtocolVersion version)
{
var incorrectPurpose = Purpose.Local;
Expand All @@ -166,18 +161,7 @@ public void ShouldThrowExceptionOnGenerateAsymmetricKeyPairWhenIncorrectPurposeI
}

[Theory(DisplayName = "Should throw exception on GenerateAsymmetricKeyPair when invalid seed is provided")]
[InlineData(ProtocolVersion.V2, null)]
[InlineData(ProtocolVersion.V2, new byte[0])]
[InlineData(ProtocolVersion.V2, new byte[] { 0x00, 0x00 })]
[InlineData(ProtocolVersion.V2, new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 })]
[InlineData(ProtocolVersion.V3, null)]
[InlineData(ProtocolVersion.V3, new byte[0])]
[InlineData(ProtocolVersion.V3, new byte[] { 0x00, 0x00 })]
[InlineData(ProtocolVersion.V3, new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 })]
[InlineData(ProtocolVersion.V4, null)]
[InlineData(ProtocolVersion.V4, new byte[0])]
[InlineData(ProtocolVersion.V4, new byte[] { 0x00, 0x00 })]
[InlineData(ProtocolVersion.V4, new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 })]
[MemberData(nameof(VersionsAndInvalidSeedData))]
public void ShouldThrowExceptionOnGenerateAsymmetricKeyPairWhenInvalidSeedIsProvided(ProtocolVersion version, byte[] seed)
{
Action act = () => new PasetoBuilder().Use(version, Purpose.Public)
Expand All @@ -190,10 +174,7 @@ public void ShouldThrowExceptionOnGenerateAsymmetricKeyPairWhenInvalidSeedIsProv
}

[Theory(DisplayName = "Should succeed on Local Encode with Byte Array Key and optional Footer when dependencies are provided")]
[InlineData(ProtocolVersion.V1)]
[InlineData(ProtocolVersion.V2)]
[InlineData(ProtocolVersion.V3)]
[InlineData(ProtocolVersion.V4)]
[MemberData(nameof(AllVersionsData), MemberType = typeof(TestHelper))]
public void ShouldSucceedOnLocalEncodeWithByteArrayKeyAndOptionalFooterWhenDependenciesAreProvided(ProtocolVersion version)
{
var token = new PasetoBuilder().Use(version, Purpose.Local)
Expand All @@ -215,10 +196,7 @@ public void ShouldSucceedOnLocalEncodeWithByteArrayKeyAndOptionalFooterWhenDepen
}

[Theory(DisplayName = "Should succeed on Local Encode with Byte Array Key and optional Footer Payload when dependencies are provided")]
[InlineData(ProtocolVersion.V1)]
[InlineData(ProtocolVersion.V2)]
[InlineData(ProtocolVersion.V3)]
[InlineData(ProtocolVersion.V4)]
[MemberData(nameof(AllVersionsData), MemberType = typeof(TestHelper))]
public void ShouldSucceedOnLocalEncodeWithByteArrayKeyAndOptionalFooterPayloadWhenDependenciesAreProvided(ProtocolVersion version)
{
var token = new PasetoBuilder().Use(version, Purpose.Local)
Expand All @@ -240,10 +218,7 @@ public void ShouldSucceedOnLocalEncodeWithByteArrayKeyAndOptionalFooterPayloadWh
}

[Theory(DisplayName = "Should succeed on Local Encode with Byte Array Key when dependencies are provided")]
[InlineData(ProtocolVersion.V1)]
[InlineData(ProtocolVersion.V2)]
[InlineData(ProtocolVersion.V3)]
[InlineData(ProtocolVersion.V4)]
[MemberData(nameof(AllVersionsData), MemberType = typeof(TestHelper))]
public void ShouldSucceedOnLocalEncodeWithByteArrayKeyWhenDependenciesAreProvided(ProtocolVersion version)
{
var token = new PasetoBuilder().Use(version, Purpose.Local)
Expand Down Expand Up @@ -332,14 +307,7 @@ public void ShouldThrowExceptionOnEncodeWhenUseIsPassingInvalidProtocol(string v
}

[Theory(DisplayName = "Should throw exception on Encode when WithKey is not called")]
[InlineData(ProtocolVersion.V1, Purpose.Local)]
[InlineData(ProtocolVersion.V1, Purpose.Public)]
[InlineData(ProtocolVersion.V2, Purpose.Local)]
[InlineData(ProtocolVersion.V2, Purpose.Public)]
[InlineData(ProtocolVersion.V3, Purpose.Local)]
[InlineData(ProtocolVersion.V3, Purpose.Public)]
[InlineData(ProtocolVersion.V4, Purpose.Local)]
[InlineData(ProtocolVersion.V4, Purpose.Public)]
[MemberData(nameof(AllVersionsAndPurposesData), MemberType = typeof(TestHelper))]
public void ShouldThrowExceptionOnEncodeWhenWithKeyIsNotCalled(ProtocolVersion version, Purpose purpose)
{
Action act = () => new PasetoBuilder().Use(version, purpose)
Expand All @@ -349,14 +317,7 @@ public void ShouldThrowExceptionOnEncodeWhenWithKeyIsNotCalled(ProtocolVersion v
}

[Theory(DisplayName = "Should throw exception on Encode when Payload is not added")]
[InlineData(ProtocolVersion.V1, Purpose.Local)]
[InlineData(ProtocolVersion.V1, Purpose.Public)]
[InlineData(ProtocolVersion.V2, Purpose.Local)]
[InlineData(ProtocolVersion.V2, Purpose.Public)]
[InlineData(ProtocolVersion.V3, Purpose.Local)]
[InlineData(ProtocolVersion.V3, Purpose.Public)]
[InlineData(ProtocolVersion.V4, Purpose.Local)]
[InlineData(ProtocolVersion.V4, Purpose.Public)]
[MemberData(nameof(AllVersionsAndPurposesData), MemberType = typeof(TestHelper))]
public void ShouldThrowExceptionOnEncodeWhenPayloadIsNotAdded(ProtocolVersion version, Purpose purpose)
{
Action act = () => new PasetoBuilder().Use(version, purpose)
Expand All @@ -367,18 +328,7 @@ public void ShouldThrowExceptionOnEncodeWhenPayloadIsNotAdded(ProtocolVersion ve
}

[Theory(DisplayName = "Should throw exception on Local Encode when invalid key is provided")]
[InlineData(ProtocolVersion.V1, null)]
[InlineData(ProtocolVersion.V1, new byte[0])]
[InlineData(ProtocolVersion.V1, new byte[] { 0x00, 0x00 })]
[InlineData(ProtocolVersion.V2, null)]
[InlineData(ProtocolVersion.V2, new byte[0])]
[InlineData(ProtocolVersion.V2, new byte[] { 0x00, 0x00 })]
[InlineData(ProtocolVersion.V3, null)]
[InlineData(ProtocolVersion.V3, new byte[0])]
[InlineData(ProtocolVersion.V3, new byte[] { 0x00, 0x00 })]
[InlineData(ProtocolVersion.V4, null)]
[InlineData(ProtocolVersion.V4, new byte[0])]
[InlineData(ProtocolVersion.V4, new byte[] { 0x00, 0x00 })]
[MemberData(nameof(VersionsAndInvalidKeyData))]
public void ShouldThrowExceptionOnLocalEncodeWhenInvalidKeyIsProvided(ProtocolVersion version, byte[] key)
{
Action act = () => new PasetoBuilder().Use(version, Purpose.Local)
Expand All @@ -391,18 +341,7 @@ public void ShouldThrowExceptionOnLocalEncodeWhenInvalidKeyIsProvided(ProtocolVe
}

[Theory(DisplayName = "Should throw exception on Public Encode when invalid key is provided")]
[InlineData(ProtocolVersion.V1, null)]
[InlineData(ProtocolVersion.V1, new byte[0])]
[InlineData(ProtocolVersion.V1, new byte[] { 0x00, 0x00 })]
[InlineData(ProtocolVersion.V2, null)]
[InlineData(ProtocolVersion.V2, new byte[0])]
[InlineData(ProtocolVersion.V2, new byte[] { 0x00, 0x00 })]
[InlineData(ProtocolVersion.V3, null)]
[InlineData(ProtocolVersion.V3, new byte[0])]
[InlineData(ProtocolVersion.V3, new byte[] { 0x00, 0x00 })]
[InlineData(ProtocolVersion.V4, null)]
[InlineData(ProtocolVersion.V4, new byte[0])]
[InlineData(ProtocolVersion.V4, new byte[] { 0x00, 0x00 })]
[MemberData(nameof(VersionsAndInvalidKeyData))]
public void ShouldThrowExceptionOnPublicEncodeWhenInvalidKeyIsProvided(ProtocolVersion version, byte[] key)
{
Action act = () => new PasetoBuilder().Use(version, Purpose.Public)
Expand Down Expand Up @@ -512,10 +451,7 @@ public void ShouldThrowExceptionOnDecodeWhenTokenIsMissing(string token)
}

[Theory(DisplayName = "Should succeed on Encode to PARSEK when is Local Purpose and Symmetric Key")]
[InlineData(ProtocolVersion.V1)]
[InlineData(ProtocolVersion.V2)]
[InlineData(ProtocolVersion.V3)]
[InlineData(ProtocolVersion.V4)]
[MemberData(nameof(AllVersionsData), MemberType = typeof(TestHelper))]
public void ShouldSucceedOnEncodeToParsekWhenIsLocalPurposeAndSymmetricKey(ProtocolVersion version)
{
var parsek = new PasetoBuilder().Use(version, Purpose.Local)
Expand All @@ -527,10 +463,7 @@ public void ShouldSucceedOnEncodeToParsekWhenIsLocalPurposeAndSymmetricKey(Proto
}

[Theory(DisplayName = "Should succeed on Encode to PARSEK when is Local Purpose and Shared Key")]
[InlineData(ProtocolVersion.V1)]
[InlineData(ProtocolVersion.V2)]
[InlineData(ProtocolVersion.V3)]
[InlineData(ProtocolVersion.V4)]
[MemberData(nameof(AllVersionsData), MemberType = typeof(TestHelper))]
public void ShouldSucceedOnEncodeToParsekWhenIsLocalPurposeAndSharedKey(ProtocolVersion version)
{
var parsek = new PasetoBuilder().Use(version, Purpose.Local)
Expand All @@ -542,10 +475,7 @@ public void ShouldSucceedOnEncodeToParsekWhenIsLocalPurposeAndSharedKey(Protocol
}

[Theory(DisplayName = "Should succeed on Encode to PARSEK when is Public Purpose and Public Asymmetric Key")]
[InlineData(ProtocolVersion.V1)]
[InlineData(ProtocolVersion.V2)]
[InlineData(ProtocolVersion.V3)]
[InlineData(ProtocolVersion.V4)]
[MemberData(nameof(AllVersionsData), MemberType = typeof(TestHelper))]
public void ShouldSucceedOnEncodeToParsekWhenIsPublicPurposeAndPublicAsymmetricKey(ProtocolVersion version)
{
var keyLength = version switch
Expand All @@ -569,10 +499,7 @@ public void ShouldSucceedOnEncodeToParsekWhenIsPublicPurposeAndPublicAsymmetricK
}

[Theory(DisplayName = "Should succeed on Encode to PARSEK when is Public Purpose and Secret Asymmetric Key")]
[InlineData(ProtocolVersion.V1)]
[InlineData(ProtocolVersion.V2)]
[InlineData(ProtocolVersion.V3)]
[InlineData(ProtocolVersion.V4)]
[MemberData(nameof(AllVersionsData), MemberType = typeof(TestHelper))]
public void ShouldSucceedOnEncodeToParsekWhenIsPublicPurposeAndSecretAsymmetricKey(ProtocolVersion version)
{
var keyLength = version switch
Expand All @@ -596,10 +523,7 @@ public void ShouldSucceedOnEncodeToParsekWhenIsPublicPurposeAndSecretAsymmetricK
}

[Theory(DisplayName = "Should succeed on Decoding with Date Validations")]
[InlineData(ProtocolVersion.V1)]
[InlineData(ProtocolVersion.V2)]
[InlineData(ProtocolVersion.V3)]
[InlineData(ProtocolVersion.V4)]
[MemberData(nameof(AllVersionsData), MemberType = typeof(TestHelper))]
public void ShouldSucceedOnDecodingWithDateValidations(ProtocolVersion version)
{
const Purpose purpose = Purpose.Public;
Expand Down Expand Up @@ -641,4 +565,41 @@ public void ShouldSucceedOnDecodingWithDateValidations(ProtocolVersion version)

decoded.IsValid.Should().BeTrue();
}

public static TheoryData<ProtocolVersion, byte[]> VersionsAndInvalidKeyData()
{
var bytes = new List<byte[]>
{
null,
Array.Empty<byte>(),
new byte[] { 0x80, 0x00 }
};

var ret = new TheoryData<ProtocolVersion, byte[]>();

foreach (var version in Enum.GetValues<ProtocolVersion>())
foreach (var key in bytes)
ret.Add(version, key);

return ret;
}

public static TheoryData<ProtocolVersion, byte[]> VersionsAndInvalidSeedData()
{
var bytes = new List<byte[]>
{
null,
Array.Empty<byte>(),
new byte[] { 0x80, 0x00 },
new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }
};

var ret = new TheoryData<ProtocolVersion, byte[]>();

foreach (var version in Enum.GetValues<ProtocolVersion>().Where(x => x != ProtocolVersion.V1))
foreach (var key in bytes)
ret.Add(version, key);

return ret;
}
}
11 changes: 4 additions & 7 deletions tests/Paseto.Tests/PasetoTestVectors.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
using Xunit.Abstractions;
using Xunit.Categories;

using Paseto.Builder;
using Paseto.Cryptography;
using Paseto.Tests.Vectors;
using Builder;
using Cryptography;
using Vectors;

using static TestHelper;

Expand All @@ -28,10 +28,7 @@ public class PasetoTestVectors
public PasetoTestVectors(ITestOutputHelper output) => _output = output;

[Theory]
[InlineData("v1")]
[InlineData("v2")]
[InlineData("v3")]
[InlineData("v4")]
[MemberData(nameof(VersionStringNameData), MemberType = typeof(TestHelper))]
public void VersionTestVectors(string version)
{
var json = GetPasetoTestVector(version);
Expand Down
Loading

0 comments on commit 8b56d44

Please sign in to comment.