-
Notifications
You must be signed in to change notification settings - Fork 5.1k
Refactor SLH-DSA tests #114981
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
PranavSenthilnathan
merged 5 commits into
dotnet:main
from
PranavSenthilnathan:slh-dsa-test-refactor
Apr 24, 2025
Merged
Refactor SLH-DSA tests #114981
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
171 changes: 0 additions & 171 deletions
171
...mmon/tests/System/Security/Cryptography/AlgorithmImplementations/SlhDsa/SlhDsaApiTests.cs
This file was deleted.
Oops, something went wrong.
217 changes: 217 additions & 0 deletions
217
...tests/System/Security/Cryptography/AlgorithmImplementations/SlhDsa/SlhDsaContractTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,217 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Collections.Generic; | ||
using System.Linq; | ||
using Xunit; | ||
|
||
namespace System.Security.Cryptography.SLHDsa.Tests | ||
{ | ||
public static class SlhDsaContractTests | ||
{ | ||
public static IEnumerable<object[]> ArgumentValidationData => | ||
from algorithm in SlhDsaTestData.AlgorithmsRaw | ||
from shouldDispose in new[] { true, false } | ||
select new object[] { algorithm, shouldDispose }; | ||
|
||
[Theory] | ||
[MemberData(nameof(ArgumentValidationData))] | ||
public static void NullArgumentValidation(SlhDsaAlgorithm algorithm, bool shouldDispose) | ||
{ | ||
using SlhDsa slhDsa = SlhDsaMockImplementation.Create(algorithm); | ||
|
||
if (shouldDispose) | ||
{ | ||
// Test that argument validation exceptions take precedence over ObjectDisposedException | ||
slhDsa.Dispose(); | ||
} | ||
|
||
AssertExtensions.Throws<ArgumentNullException>("pbeParameters", () => slhDsa.ExportEncryptedPkcs8PrivateKey(ReadOnlySpan<byte>.Empty, null)); | ||
AssertExtensions.Throws<ArgumentNullException>("pbeParameters", () => slhDsa.ExportEncryptedPkcs8PrivateKey(ReadOnlySpan<char>.Empty, null)); | ||
AssertExtensions.Throws<ArgumentNullException>("pbeParameters", () => slhDsa.ExportEncryptedPkcs8PrivateKeyPem(ReadOnlySpan<byte>.Empty, null)); | ||
AssertExtensions.Throws<ArgumentNullException>("pbeParameters", () => slhDsa.ExportEncryptedPkcs8PrivateKeyPem(ReadOnlySpan<char>.Empty, null)); | ||
AssertExtensions.Throws<ArgumentNullException>("pbeParameters", () => slhDsa.TryExportEncryptedPkcs8PrivateKey(ReadOnlySpan<byte>.Empty, null, Span<byte>.Empty, out _)); | ||
AssertExtensions.Throws<ArgumentNullException>("pbeParameters", () => slhDsa.TryExportEncryptedPkcs8PrivateKey(ReadOnlySpan<char>.Empty, null, Span<byte>.Empty, out _)); | ||
} | ||
|
||
[Theory] | ||
[MemberData(nameof(ArgumentValidationData))] | ||
public static void ArgumentValidation(SlhDsaAlgorithm algorithm, bool shouldDispose) | ||
{ | ||
using SlhDsa slhDsa = SlhDsaMockImplementation.Create(algorithm); | ||
|
||
int publicKeySize = algorithm.PublicKeySizeInBytes; | ||
int secretKeySize = algorithm.SecretKeySizeInBytes; | ||
int signatureSize = algorithm.SignatureSizeInBytes; | ||
|
||
if (shouldDispose) | ||
{ | ||
// Test that argument validation exceptions take precedence over ObjectDisposedException | ||
slhDsa.Dispose(); | ||
} | ||
|
||
AssertExtensions.Throws<ArgumentException>("destination", () => slhDsa.ExportSlhDsaPublicKey(new byte[publicKeySize - 1])); | ||
AssertExtensions.Throws<ArgumentException>("destination", () => slhDsa.ExportSlhDsaSecretKey(new byte[secretKeySize - 1])); | ||
AssertExtensions.Throws<ArgumentException>("destination", () => slhDsa.SignData(ReadOnlySpan<byte>.Empty, new byte[signatureSize - 1], ReadOnlySpan<byte>.Empty)); | ||
|
||
// Context length must be less than 256 | ||
AssertExtensions.Throws<ArgumentOutOfRangeException>("context", () => slhDsa.SignData(ReadOnlySpan<byte>.Empty, Span<byte>.Empty, new byte[256])); | ||
AssertExtensions.Throws<ArgumentOutOfRangeException>("context", () => slhDsa.VerifyData(ReadOnlySpan<byte>.Empty, Span<byte>.Empty, new byte[256])); | ||
} | ||
|
||
public static IEnumerable<object[]> ApiWithDestinationSpanTestData => | ||
from algorithm in SlhDsaTestData.AlgorithmsRaw | ||
from destinationLargerThanRequired in new[] { true, false } | ||
select new object[] { algorithm, destinationLargerThanRequired }; | ||
|
||
private const int PaddingSize = 10; | ||
|
||
[Theory] | ||
[MemberData(nameof(ApiWithDestinationSpanTestData))] | ||
public static void ExportSlhDsaPublicKey_CallsCore(SlhDsaAlgorithm algorithm, bool destinationLargerThanRequired) | ||
{ | ||
using SlhDsaMockImplementation slhDsa = SlhDsaMockImplementation.Create(algorithm); | ||
|
||
int publicKeySize = algorithm.PublicKeySizeInBytes; | ||
byte[] publicKey = CreatePaddedFilledArray(publicKeySize, 42); | ||
|
||
// Extra bytes in destination buffer should not be touched | ||
int extraBytes = destinationLargerThanRequired ? PaddingSize / 2 : 0; | ||
Memory<byte> destination = publicKey.AsMemory(PaddingSize, publicKeySize + extraBytes); | ||
|
||
slhDsa.ExportSlhDsaPublicKeyCoreHook = _ => { }; | ||
slhDsa.AddDestinationBufferIsSameAssertion(destination[..publicKeySize]); | ||
slhDsa.AddFillDestination(1); | ||
|
||
slhDsa.ExportSlhDsaPublicKey(destination.Span); | ||
Assert.Equal(1, slhDsa.ExportSlhDsaPublicKeyCoreCallCount); | ||
AssertExpectedFill(publicKey, fillElement: 1, paddingElement: 42, PaddingSize, publicKeySize); | ||
} | ||
|
||
[Theory] | ||
[MemberData(nameof(ApiWithDestinationSpanTestData))] | ||
public static void ExportSlhDsaSecretKey_CallsCore(SlhDsaAlgorithm algorithm, bool destinationLargerThanRequired) | ||
{ | ||
using SlhDsaMockImplementation slhDsa = SlhDsaMockImplementation.Create(algorithm); | ||
|
||
int secretKeySize = algorithm.SecretKeySizeInBytes; | ||
byte[] secretKey = CreatePaddedFilledArray(secretKeySize, 42); | ||
|
||
// Extra bytes in destination buffer should not be touched | ||
int extraBytes = destinationLargerThanRequired ? PaddingSize / 2 : 0; | ||
Memory<byte> destination = secretKey.AsMemory(PaddingSize, secretKeySize + extraBytes); | ||
|
||
slhDsa.ExportSlhDsaSecretKeyCoreHook = _ => { }; | ||
slhDsa.AddDestinationBufferIsSameAssertion(destination[..secretKeySize]); | ||
slhDsa.AddFillDestination(1); | ||
|
||
slhDsa.ExportSlhDsaSecretKey(destination.Span); | ||
Assert.Equal(1, slhDsa.ExportSlhDsaSecretKeyCoreCallCount); | ||
AssertExpectedFill(secretKey, fillElement: 1, paddingElement: 42, PaddingSize, secretKeySize); | ||
} | ||
|
||
[Theory] | ||
[MemberData(nameof(ApiWithDestinationSpanTestData))] | ||
public static void SignData_CallsCore(SlhDsaAlgorithm algorithm, bool destinationLargerThanRequired) | ||
{ | ||
using SlhDsaMockImplementation slhDsa = SlhDsaMockImplementation.Create(algorithm); | ||
|
||
int signatureSize = algorithm.SignatureSizeInBytes; | ||
byte[] signature = CreatePaddedFilledArray(signatureSize, 42); | ||
|
||
// Extra bytes in destination buffer should not be touched | ||
int extraBytes = destinationLargerThanRequired ? PaddingSize / 2 : 0; | ||
Memory<byte> destination = signature.AsMemory(PaddingSize, signatureSize + extraBytes); | ||
byte[] testData = [2]; | ||
byte[] testContext = [3]; | ||
|
||
slhDsa.SignDataCoreHook = (_, _, _) => { }; | ||
slhDsa.AddDataBufferIsSameAssertion(testData); | ||
slhDsa.AddContextBufferIsSameAssertion(testContext); | ||
slhDsa.AddDestinationBufferIsSameAssertion(destination[..signatureSize]); | ||
slhDsa.AddFillDestination(1); | ||
|
||
slhDsa.SignData(testData, signature.AsSpan(PaddingSize, signatureSize + extraBytes), testContext); | ||
Assert.Equal(1, slhDsa.SignDataCoreCallCount); | ||
AssertExpectedFill(signature, fillElement: 1, paddingElement: 42, PaddingSize, signatureSize); | ||
} | ||
|
||
[Theory] | ||
[MemberData(nameof(SlhDsaTestData.AlgorithmsData), MemberType = typeof(SlhDsaTestData))] | ||
public static void VerifyData_CallsCore(SlhDsaAlgorithm algorithm) | ||
{ | ||
using SlhDsaMockImplementation slhDsa = SlhDsaMockImplementation.Create(algorithm); | ||
|
||
int signatureSize = algorithm.SignatureSizeInBytes; | ||
byte[] testSignature = CreatePaddedFilledArray(signatureSize, 42); | ||
byte[] testData = [2]; | ||
byte[] testContext = [3]; | ||
bool returnValue = false; | ||
|
||
slhDsa.VerifyDataCoreHook = (_, _, _) => returnValue; | ||
slhDsa.AddDataBufferIsSameAssertion(testData); | ||
slhDsa.AddContextBufferIsSameAssertion(testContext); | ||
slhDsa.AddSignatureBufferIsSameAssertion(testSignature.AsMemory(PaddingSize, signatureSize)); | ||
|
||
// Since `returnValue` is true, this shows the Core method doesn't get called for the wrong sized signature. | ||
returnValue = true; | ||
AssertExtensions.FalseExpression(slhDsa.VerifyData(testData, testSignature.AsSpan(PaddingSize, signatureSize - 1), testContext)); | ||
Assert.Equal(0, slhDsa.VerifyDataCoreCallCount); | ||
|
||
AssertExtensions.FalseExpression(slhDsa.VerifyData(testData, testSignature.AsSpan(PaddingSize, signatureSize + 1), testContext)); | ||
Assert.Equal(0, slhDsa.VerifyDataCoreCallCount); | ||
|
||
// But does for the right one. | ||
AssertExtensions.TrueExpression(slhDsa.VerifyData(testData, testSignature.AsSpan(PaddingSize, signatureSize), testContext)); | ||
Assert.Equal(1, slhDsa.VerifyDataCoreCallCount); | ||
|
||
// And just to prove that the Core method controls the answer... | ||
returnValue = false; | ||
AssertExtensions.FalseExpression(slhDsa.VerifyData(testData, testSignature.AsSpan(PaddingSize, signatureSize), testContext)); | ||
Assert.Equal(2, slhDsa.VerifyDataCoreCallCount); | ||
} | ||
|
||
[Theory] | ||
[MemberData(nameof(SlhDsaTestData.AlgorithmsData), MemberType = typeof(SlhDsaTestData))] | ||
public static void Dispose_CallsVirtual(SlhDsaAlgorithm algorithm) | ||
{ | ||
SlhDsaMockImplementation slhDsa = SlhDsaMockImplementation.Create(algorithm); | ||
bool disposeCalled = false; | ||
|
||
// First Dispose call should invoke overridden Dispose should be called | ||
slhDsa.DisposeHook = (bool disposing) => | ||
{ | ||
AssertExtensions.TrueExpression(disposing); | ||
disposeCalled = true; | ||
bartonjs marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}; | ||
|
||
slhDsa.Dispose(); | ||
AssertExtensions.TrueExpression(disposeCalled); | ||
|
||
// Subsequent Dispose calls should be a no-op | ||
slhDsa.DisposeHook = _ => Assert.Fail(); | ||
|
||
slhDsa.Dispose(); | ||
slhDsa.Dispose(); // no throw | ||
|
||
SlhDsaTestHelpers.VerifyDisposed(slhDsa); | ||
} | ||
|
||
private static void AssertExpectedFill(ReadOnlySpan<byte> source, byte fillElement, byte paddingElement, int startIndex, int length) | ||
{ | ||
// Ensure that the data was filled correctly | ||
AssertExtensions.FilledWith(fillElement, source.Slice(startIndex, length)); | ||
|
||
// And that the padding was not touched | ||
AssertExtensions.FilledWith(paddingElement, source.Slice(0, startIndex)); | ||
AssertExtensions.FilledWith(paddingElement, source.Slice(startIndex + length)); | ||
} | ||
|
||
private static byte[] CreatePaddedFilledArray(int size, byte filling) | ||
{ | ||
byte[] publicKey = new byte[size + 2 * PaddingSize]; | ||
publicKey.AsSpan().Fill(filling); | ||
return publicKey; | ||
} | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.