Skip to content
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

Remove duplicate code in extensibility tests #3044

Merged

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,228 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;
using System.Diagnostics;
using Xunit;
using Microsoft.IdentityModel.Tokens;
using Microsoft.IdentityModel.Logging;

#nullable enable
namespace Microsoft.IdentityModel.TestUtils.TokenValidationExtensibility.Tests
{
public partial class ExtensibilityTesting
{
public static TheoryData<AlgorithmExtensibilityTheoryData> GenerateAlgorithmExtensibilityTestCases(
string tokenHandlerType,
int extraStackFrames,
string stackFrameFileName)
{
TheoryData<AlgorithmExtensibilityTheoryData> theoryData = new();
CallContext callContext = new CallContext();

#region return CustomAlgorithmValidationError
// Test cases where delegate is overridden and return a CustomAlgorithmValidationError
// CustomAlgorithmValidationError : AlgorithmValidationError, ExceptionType: SecurityTokenInvalidAlgorithmException
theoryData.Add(new AlgorithmExtensibilityTheoryData(
"CustomAlgorithmValidatorDelegate",
tokenHandlerType,
CustomAlgorithmValidationDelegates.CustomAlgorithmValidatorDelegate,
extraStackFrames: extraStackFrames)
{
ExpectedException = new ExpectedException(
typeof(SecurityTokenInvalidSignatureException),
"IDX10518:",
typeof(SecurityTokenInvalidAlgorithmException)),
ExpectedInnerException = new ExpectedException(
typeof(SecurityTokenInvalidAlgorithmException),
nameof(CustomAlgorithmValidationDelegates.CustomAlgorithmValidatorDelegate)),
ValidationError = new CustomAlgorithmValidationError(
new MessageDetail(
nameof(CustomAlgorithmValidationDelegates.CustomAlgorithmValidatorDelegate), null),
ValidationFailureType.AlgorithmValidationFailed,
typeof(SecurityTokenInvalidAlgorithmException),
new StackFrame("CustomAlgorithmValidationDelegates.cs", 0),
"algorithm")
});

// CustomAlgorithmValidationError : AlgorithmValidationError, ExceptionType: CustomSecurityTokenInvalidAlgorithmException : SecurityTokenInvalidAlgorithmException
theoryData.Add(new AlgorithmExtensibilityTheoryData(
"CustomAlgorithmValidatorCustomExceptionDelegate",
tokenHandlerType,
CustomAlgorithmValidationDelegates.CustomAlgorithmValidatorCustomExceptionDelegate,
extraStackFrames: extraStackFrames)
{
ExpectedException = new ExpectedException(
typeof(SecurityTokenInvalidSignatureException),
"IDX10518:",
typeof(CustomSecurityTokenInvalidAlgorithmException)),
ExpectedInnerException = new ExpectedException(
typeof(CustomSecurityTokenInvalidAlgorithmException),
nameof(CustomAlgorithmValidationDelegates.CustomAlgorithmValidatorCustomExceptionDelegate)),
ValidationError = new CustomAlgorithmValidationError(
new MessageDetail(
nameof(CustomAlgorithmValidationDelegates.CustomAlgorithmValidatorCustomExceptionDelegate), null),
ValidationFailureType.AlgorithmValidationFailed,
typeof(CustomSecurityTokenInvalidAlgorithmException),
new StackFrame("CustomAlgorithmValidationDelegates.cs", 0),
"algorithm"),
});

// CustomAlgorithmValidationError : AlgorithmValidationError, ExceptionType: NotSupportedException : SystemException
theoryData.Add(new AlgorithmExtensibilityTheoryData(
"CustomAlgorithmValidatorUnknownExceptionDelegate",
tokenHandlerType,
CustomAlgorithmValidationDelegates.CustomAlgorithmValidatorUnknownExceptionDelegate,
extraStackFrames: extraStackFrames)
{
// CustomAlgorithmValidationError does not handle the exception type 'NotSupportedException'
ExpectedException = new ExpectedException(
typeof(SecurityTokenInvalidSignatureException),
"IDX10518:",
typeof(SecurityTokenException)),
ExpectedInnerException = ExpectedException.SecurityTokenException(
LogHelper.FormatInvariant(
Tokens.LogMessages.IDX10002, // "IDX10002: Unknown exception type returned. Type: '{0}'. Message: '{1}'.";
typeof(NotSupportedException),
nameof(CustomAlgorithmValidationDelegates.CustomAlgorithmValidatorUnknownExceptionDelegate))),
ValidationError = new CustomAlgorithmValidationError(
new MessageDetail(
nameof(CustomAlgorithmValidationDelegates.CustomAlgorithmValidatorUnknownExceptionDelegate), null),
ValidationFailureType.AlgorithmValidationFailed,
typeof(NotSupportedException),
new StackFrame("CustomAlgorithmValidationDelegates.cs", 0),
"algorithm"),
});

// CustomAlgorithmValidationError : AlgorithmValidationError, ExceptionType: NotSupportedException : SystemException, ValidationFailureType: CustomAudienceValidationFailureType
theoryData.Add(new AlgorithmExtensibilityTheoryData(
"CustomAlgorithmValidatorCustomExceptionCustomFailureTypeDelegate",
tokenHandlerType,
CustomAlgorithmValidationDelegates.CustomAlgorithmValidatorCustomExceptionCustomFailureTypeDelegate,
extraStackFrames: extraStackFrames)
{
ExpectedException = new ExpectedException(
typeof(SecurityTokenInvalidSignatureException),
"IDX10518:",
typeof(CustomSecurityTokenInvalidAlgorithmException)),
ExpectedInnerException = new ExpectedException(
typeof(CustomSecurityTokenInvalidAlgorithmException),
nameof(CustomAlgorithmValidationDelegates.CustomAlgorithmValidatorCustomExceptionCustomFailureTypeDelegate)),
ValidationError = new CustomAlgorithmValidationError(
new MessageDetail(
nameof(CustomAlgorithmValidationDelegates.CustomAlgorithmValidatorCustomExceptionCustomFailureTypeDelegate), null),
CustomAlgorithmValidationError.CustomAlgorithmValidationFailureType,
typeof(CustomSecurityTokenInvalidAlgorithmException),
new StackFrame("CustomAlgorithmValidationDelegates.cs", 0),
"algorithm"),
});
#endregion

#region return AlgorithmValidationError
// Test cases where delegate is overridden and return an AlgorithmValidationError
// AlgorithmValidationError : ValidationError, ExceptionType: SecurityTokenInvalidAlgorithmException
theoryData.Add(new AlgorithmExtensibilityTheoryData(
"AlgorithmValidatorDelegate",
tokenHandlerType,
CustomAlgorithmValidationDelegates.AlgorithmValidatorDelegate,
extraStackFrames: extraStackFrames)
{
ExpectedException = new ExpectedException(
typeof(SecurityTokenInvalidSignatureException),
"IDX10518:",
typeof(SecurityTokenInvalidAlgorithmException)),
ExpectedInnerException = new ExpectedException(
typeof(SecurityTokenInvalidAlgorithmException),
nameof(CustomAlgorithmValidationDelegates.AlgorithmValidatorDelegate)),
ValidationError = new AlgorithmValidationError(
new MessageDetail(
nameof(CustomAlgorithmValidationDelegates.AlgorithmValidatorDelegate), null),
ValidationFailureType.AlgorithmValidationFailed,
typeof(SecurityTokenInvalidAlgorithmException),
new StackFrame("CustomAlgorithmValidationDelegates.cs", 0),
"algorithm")
});

// AlgorithmValidationError : ValidationError, ExceptionType: CustomSecurityTokenInvalidAlgorithmException : SecurityTokenInvalidAlgorithmException
theoryData.Add(new AlgorithmExtensibilityTheoryData(
"AlgorithmValidatorCustomAlgorithmExceptionTypeDelegate",
tokenHandlerType,
CustomAlgorithmValidationDelegates.AlgorithmValidatorCustomAlgorithmExceptionTypeDelegate,
extraStackFrames: extraStackFrames)
{
// AlgorithmValidationError does not handle the exception type 'CustomSecurityTokenInvalidAlgorithmException'
ExpectedException = new ExpectedException(
typeof(SecurityTokenInvalidSignatureException),
"IDX10518:",
typeof(SecurityTokenException)),
ExpectedInnerException = ExpectedException.SecurityTokenException(
LogHelper.FormatInvariant(
Tokens.LogMessages.IDX10002, // "IDX10002: Unknown exception type returned. Type: '{0}'. Message: '{1}'.";
typeof(CustomSecurityTokenInvalidAlgorithmException),
nameof(CustomAlgorithmValidationDelegates.AlgorithmValidatorCustomAlgorithmExceptionTypeDelegate))),
ValidationError = new AlgorithmValidationError(
new MessageDetail(
nameof(CustomAlgorithmValidationDelegates.AlgorithmValidatorCustomAlgorithmExceptionTypeDelegate), null),
ValidationFailureType.AlgorithmValidationFailed,
typeof(CustomSecurityTokenInvalidAlgorithmException),
new StackFrame("CustomAlgorithmValidationDelegates.cs", 0),
"algorithm")
});

// AlgorithmValidationError : ValidationError, ExceptionType: CustomSecurityTokenException : SystemException
theoryData.Add(new AlgorithmExtensibilityTheoryData(
"AlgorithmValidatorCustomExceptionTypeDelegate",
tokenHandlerType,
CustomAlgorithmValidationDelegates.AlgorithmValidatorCustomExceptionTypeDelegate,
extraStackFrames: extraStackFrames)
{
// AlgorithmValidationError does not handle the exception type 'CustomSecurityTokenException'
ExpectedException = new ExpectedException(
typeof(SecurityTokenInvalidSignatureException),
"IDX10518:",
typeof(SecurityTokenException)),
ExpectedInnerException = ExpectedException.SecurityTokenException(
LogHelper.FormatInvariant(
Tokens.LogMessages.IDX10002, // "IDX10002: Unknown exception type returned. Type: '{0}'. Message: '{1}'.";
typeof(CustomSecurityTokenException),
nameof(CustomAlgorithmValidationDelegates.AlgorithmValidatorCustomExceptionTypeDelegate))),
ValidationError = new AlgorithmValidationError(
new MessageDetail(
nameof(CustomAlgorithmValidationDelegates.AlgorithmValidatorCustomExceptionTypeDelegate), null),
ValidationFailureType.AlgorithmValidationFailed,
typeof(CustomSecurityTokenException),
new StackFrame("CustomAlgorithmValidationDelegates.cs", 0),
"algorithm")
});

// SignatureValidationError : ValidationError, ExceptionType: SecurityTokenInvalidSignatureException, inner: CustomSecurityTokenInvalidAlgorithmException
theoryData.Add(new AlgorithmExtensibilityTheoryData(
"AlgorithmValidatorThrows",
tokenHandlerType,
CustomAlgorithmValidationDelegates.AlgorithmValidatorThrows,
extraStackFrames: extraStackFrames + 1)
{
ExpectedException = new ExpectedException(
typeof(SecurityTokenInvalidSignatureException),
"IDX10273:",
typeof(CustomSecurityTokenInvalidAlgorithmException)),
ExpectedInnerException = new ExpectedException(
typeof(CustomSecurityTokenInvalidAlgorithmException),
nameof(CustomAlgorithmValidationDelegates.AlgorithmValidatorThrows)),
ValidationError = new SignatureValidationError(
new MessageDetail(
string.Format(Tokens.LogMessages.IDX10273), null),
ValidationFailureType.AlgorithmValidatorThrew,
typeof(SecurityTokenInvalidSignatureException),
new StackFrame(stackFrameFileName, 0),
null, // no inner validation error
new CustomSecurityTokenInvalidAlgorithmException(nameof(CustomAlgorithmValidationDelegates.AlgorithmValidatorThrows), null)
)
});
#endregion

return theoryData;
}
}
}
#nullable restore
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using Microsoft.IdentityModel.Tokens;

#nullable enable
namespace Microsoft.IdentityModel.TestUtils.TokenValidationExtensibility.Tests
{
public class AlgorithmExtensibilityTheoryData : ExtensibilityTheoryData
{
internal AlgorithmExtensibilityTheoryData(
string testId,
string tokenHandlerType,
AlgorithmValidationDelegate algorithmValidationDelegate,
int extraStackFrames) : base(testId, tokenHandlerType, extraStackFrames)
{
SecurityTokenDescriptor = new()
{
Issuer = Default.Issuer,
SigningCredentials = KeyingMaterial.DefaultX509SigningCreds_2048_RsaSha2_Sha2,
};

ValidationParameters.AlgorithmValidator = algorithmValidationDelegate;
ValidationParameters.SignatureValidator = null;
ValidationParameters.IssuerSigningKeys.Add(KeyingMaterial.DefaultX509SigningCreds_2048_RsaSha2_Sha2.Key);
}
}
}
#nullable restore
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ public SecurityTokenDescriptor SecurityTokenDescriptor
internal ValidationError? ValidationError { get; set; }

internal int ExtraStackFrames { get; }

internal ExpectedException? ExpectedInnerException { get; set; }
}
}
#nullable restore
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using Microsoft.IdentityModel.Tokens.Saml;
using Microsoft.IdentityModel.Tokens.Saml2;

#nullable enable
namespace Microsoft.IdentityModel.TestUtils.TokenValidationExtensibility.Tests
{
// This interface is used to test the extensibility of the ValidateTokenAsync method
Expand Down Expand Up @@ -93,3 +94,4 @@ public SecurityToken CreateToken(SecurityTokenDescriptor tokenDescriptor)
}
}
}
#nullable restore
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Microsoft.IdentityModel.Tokens;
using Microsoft.IdentityModel.Logging;

#nullable enable
namespace Microsoft.IdentityModel.TestUtils.TokenValidationExtensibility.Tests
{
public partial class ExtensibilityTesting
Expand Down Expand Up @@ -203,3 +204,4 @@ public static TheoryData<IssuerExtensibilityTheoryData> GenerateIssuerExtensibil
}
}
}
#nullable restore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using Microsoft.IdentityModel.Tokens;

#nullable enable
namespace Microsoft.IdentityModel.TestUtils.TokenValidationExtensibility.Tests
{
public class IssuerExtensibilityTheoryData : ExtensibilityTheoryData
Expand All @@ -23,3 +24,4 @@ internal IssuerExtensibilityTheoryData(
}
}
}
#nullable restore
Loading
Loading