-
Notifications
You must be signed in to change notification settings - Fork 409
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
Added sample tests for the new validation path #2801
Closed
Closed
Changes from all commits
Commits
Show all changes
2 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 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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 |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
// Licensed under the MIT License. | ||
|
||
using System; | ||
using System.Threading.Tasks; | ||
using Microsoft.IdentityModel.TestExtensions; | ||
using Microsoft.IdentityModel.TestUtils; | ||
using Microsoft.IdentityModel.Tokens; | ||
|
@@ -221,6 +222,218 @@ public void TokenWithMissingSecurityCredentials() | |
} | ||
#endregion | ||
|
||
#region New Model Token Validation Tests | ||
/// <summary> | ||
/// Tests how the class under test handles a valid token. | ||
/// </summary> | ||
[Fact] | ||
public void ValidToken_NewPath() | ||
{ | ||
SampleTokenValidationClass classUnderTest = new SampleTokenValidationClass(); | ||
classUnderTest.ValidateTokenShimWithNewPath(testTokenCreator.CreateDefaultValidToken()); | ||
} | ||
|
||
/// <summary> | ||
/// Tests how the class under test handles a bogus token; one that in no way conforms to the expected JWS format. | ||
/// </summary> | ||
[Fact] | ||
public void BogusToken_NewPath() | ||
{ | ||
TestWithGeneratedToken_NewPath( | ||
() => "InvalidToken", | ||
typeof(SecurityTokenMalformedException), | ||
"IDX14100"); | ||
} | ||
|
||
/// <summary> | ||
/// Tests how the class under test handles a token with a missing signature. | ||
/// </summary> | ||
[Fact] | ||
public void TokenWithoutSignature_NewPath() | ||
{ | ||
TestWithGeneratedToken_NewPath( | ||
testTokenCreator.CreateTokenWithNoSignature, | ||
typeof(SecurityTokenInvalidSignatureException), | ||
"IDX10504:"); | ||
} | ||
|
||
/// <summary> | ||
/// Tests how the class under test handles a token with a malformed signature. | ||
/// </summary> | ||
[Fact] | ||
public void TokenWithBadSignature_NewPath() | ||
{ | ||
TestWithGeneratedToken_NewPath( | ||
testTokenCreator.CreateTokenWithInvalidSignature, | ||
typeof(SecurityTokenInvalidSignatureException), | ||
"IDX10500:"); // 10500 indicates no signature key was found. Current path returns 10511 which indicates a bad signature and provides the list of keys attempted | ||
} | ||
|
||
/// <summary> | ||
/// Tests how the class under test handles a token which is expired. | ||
/// </summary> | ||
[Fact] | ||
public void ExpiredToken_NewPath() | ||
{ | ||
TestWithGeneratedToken_NewPath( | ||
testTokenCreator.CreateExpiredToken, | ||
typeof(SecurityTokenExpiredException), | ||
"IDX10223"); | ||
} | ||
|
||
/// <summary> | ||
/// Tests how the class under test handles a token which is not yet valid | ||
/// </summary> | ||
[Fact] | ||
public void NetYetValidToken_NewPath() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is too fractured. |
||
{ | ||
TestWithGeneratedToken_NewPath( | ||
testTokenCreator.CreateNotYetValidToken, | ||
typeof(SecurityTokenNotYetValidException), | ||
"IDX10222"); | ||
} | ||
|
||
/// <summary> | ||
/// Tests how the class under test handles a token with an issuer that doesn't match expectations. | ||
/// </summary> | ||
[Fact] | ||
public void TokenWithWrongIssuer_NewPath() | ||
{ | ||
TestWithGeneratedToken_NewPath( | ||
testTokenCreator.CreateTokenWithBadIssuer, | ||
typeof(SecurityTokenInvalidIssuerException), | ||
"IDX10212"); | ||
// Current path returns IDX10205 which contains Issuer, ValidIssuer, and ValidIssuers. | ||
// This is a new error code that drops ValidIssuer as it is no longer used. | ||
} | ||
|
||
/// <summary> | ||
/// Tests how the class under test handles a token with an audience that doesn't match expectations. | ||
/// </summary> | ||
[Fact] | ||
public void TokenWithWrongAudience_NewPath() | ||
{ | ||
TestWithGeneratedToken_NewPath( | ||
testTokenCreator.CreateTokenWithBadAudience, | ||
typeof(SecurityTokenInvalidAudienceException), | ||
"IDX10215"); | ||
// Current path returns IDX10214 which contains Audience, ValidAudience, and ValidAudiences. | ||
// This is a new error code that drops ValidAudience as it is no longer used. | ||
} | ||
|
||
/// <summary> | ||
/// Tests how the class under test handles a token signed with a key different than the one expected. | ||
/// </summary> | ||
[Fact] | ||
public void TokenWithBadSignatureKey_NewPath() | ||
{ | ||
TestWithGeneratedToken_NewPath( | ||
testTokenCreator.CreateTokenWithBadSignatureKey, | ||
typeof(SecurityTokenSignatureKeyNotFoundException), | ||
"IDX10500"); // By default, the new path defaults to not trying all signing keys. | ||
} | ||
|
||
/// <summary> | ||
/// Tests how the class under test handles a token signed with a key different than the one expected. | ||
/// </summary> | ||
[Fact] | ||
public void TokenWithBadSignatureKey_NewPath_TryAllKeys() | ||
{ | ||
Action<ValidationParameters> updateParameters = (validationParameters) => | ||
validationParameters.TryAllIssuerSigningKeys = true; | ||
|
||
TestWithGeneratedToken_NewPath( | ||
testTokenCreator.CreateTokenWithBadSignatureKey, | ||
typeof(SecurityTokenSignatureKeyNotFoundException), | ||
"IDX10503", | ||
updateParameters); | ||
} | ||
|
||
/// <summary> | ||
/// Tests how the class under test handles a token missing the iss claim. | ||
/// </summary> | ||
[Fact] | ||
public void TokenWithMissingIssuer_NewPath() | ||
{ | ||
TestWithGeneratedToken_NewPath( | ||
testTokenCreator.CreateTokenWithMissingIssuer, | ||
typeof(SecurityTokenInvalidIssuerException), | ||
"IDX10211"); | ||
} | ||
|
||
/// <summary> | ||
/// Tests how the class under test handles a token missing the aud claim. | ||
/// </summary> | ||
[Fact] | ||
public void TokenWithMissingAudience_NewPath() | ||
{ | ||
TestWithGeneratedToken_NewPath( | ||
testTokenCreator.CreateTokenWithMissingAudience, | ||
typeof(SecurityTokenInvalidAudienceException), | ||
"IDX10206"); | ||
} | ||
|
||
/// <summary> | ||
/// Tests how the class under test handles a token with a iat claim indicating it has not yet been issued. | ||
/// </summary> | ||
[Fact] | ||
public void TokenWithFutureIssuedAt_NewPath() | ||
{ | ||
// NOTE: This is not currently validated and there's no way to enforce its presence. | ||
// It may be enforceable in the future, in which case this will be updated with proper checks. | ||
SampleTokenValidationClass classUnderTest = new SampleTokenValidationClass(); | ||
classUnderTest.ValidateTokenShimWithNewPath(testTokenCreator.CreateTokenWithFutureIssuedAt()); | ||
} | ||
|
||
/// <summary> | ||
/// Tests how the class under test handles a token missing the iat claim. | ||
/// </summary> | ||
[Fact] | ||
public void TokenWithMissingIssuedAt_NewPath() | ||
{ | ||
// NOTE: This is not currently validated and there's no way to enforce its presence. | ||
// It may be enforceable in the future, in which case this will be updated with proper checks. | ||
SampleTokenValidationClass classUnderTest = new SampleTokenValidationClass(); | ||
classUnderTest.ValidateTokenShimWithNewPath(testTokenCreator.CreateTokenWithMissingIssuedAt()); | ||
} | ||
|
||
/// <summary> | ||
/// Tests how the class under test handles a token missing the nbf claim. | ||
/// </summary> | ||
[Fact] | ||
public void TokenWithMissingNotBefore_NewPath() | ||
{ | ||
// NOTE: This is not currently validated and there's no way to enforce its presence. | ||
// It may be enforceable in the future, in which case this will be updated with proper checks. | ||
SampleTokenValidationClass classUnderTest = new SampleTokenValidationClass(); | ||
classUnderTest.ValidateTokenShimWithNewPath(testTokenCreator.CreateTokenWithMissingNotBefore()); | ||
} | ||
|
||
/// <summary> | ||
/// Tests how the class under test handles a token missing the exp claim. | ||
/// </summary> | ||
[Fact] | ||
public void TokenWithMissingExpires_NewPath() | ||
{ | ||
TestWithGeneratedToken_NewPath( | ||
testTokenCreator.CreateTokenWithMissingExpires, | ||
typeof(SecurityTokenNoExpirationException), | ||
"IDX10225"); | ||
} | ||
|
||
/// <summary> | ||
/// Test how the class under test handles a token without a signing key (i.e. alg=none, no signature). | ||
/// </summary> | ||
[Fact] | ||
public void TokenWithMissingSecurityCredentials_NewPath() | ||
{ | ||
TestWithGeneratedToken_NewPath( | ||
testTokenCreator.CreateTokenWithMissingKey, | ||
typeof(SecurityTokenInvalidSignatureException), | ||
"IDX10504"); | ||
} | ||
#endregion | ||
|
||
#region Deprecated Model Token Validation Tests | ||
/// <summary> | ||
/// Tests how a class under test using JwtSecurityTokenHandler handles a valid token. | ||
|
@@ -451,6 +664,32 @@ internal void TestWithGeneratedToken(Func<string> generateTokenToTest, Type expe | |
expectedInnerExceptionMessagePart); | ||
} | ||
|
||
internal void TestWithGeneratedToken_NewPath( | ||
Func<string> generateTokenToTest, | ||
Type expectedInnerExceptionType, | ||
string expectedInnerExceptionMessagePart, | ||
Action<ValidationParameters> modifyValidationParameters = null) | ||
{ | ||
SampleTokenValidationClass classUnderTest = new SampleTokenValidationClass(); | ||
if (modifyValidationParameters != null) | ||
modifyValidationParameters(classUnderTest.ValidationParameters); | ||
|
||
string token = generateTokenToTest(); | ||
Result<ValidationResult, ExceptionDetail> result = classUnderTest.ValidateTokenShimWithNewPath(token); | ||
|
||
if (!result.IsSuccess) | ||
AssertException(expectedInnerExceptionType, expectedInnerExceptionMessagePart, result.UnwrapError().GetException()); | ||
else | ||
{ | ||
if (expectedInnerExceptionType != null || !string.IsNullOrEmpty(expectedInnerExceptionMessagePart)) | ||
throw new TestException( | ||
string.Format( | ||
"Expected an exception of type '{0}' containing '{1}' in the message.", | ||
expectedInnerExceptionType, | ||
expectedInnerExceptionMessagePart)); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Calls a passed <paramref name="validate"/> action with a generated token and validates the outcome. | ||
/// </summary> | ||
|
@@ -494,13 +733,16 @@ internal void AssertValidationException(Action action, Type innerExceptionType, | |
catch (Exception e) | ||
{ | ||
Assert.Equal(typeof(SampleTestTokenValidationException), e.GetType()); | ||
Assert.Equal(innerExceptionType, e.InnerException.GetType()); | ||
|
||
if (!string.IsNullOrEmpty(innerExceptionMessagePart)) | ||
{ | ||
Assert.Contains(innerExceptionMessagePart, e.InnerException.Message); | ||
} | ||
AssertException(innerExceptionType, innerExceptionMessagePart, e.InnerException); | ||
} | ||
} | ||
|
||
private static void AssertException(Type exceptionType, string exceptionMessagePart, Exception exception) | ||
{ | ||
Assert.Equal(exceptionType, exception.GetType()); | ||
|
||
if (!string.IsNullOrEmpty(exceptionMessagePart)) | ||
Assert.Contains(exceptionMessagePart, exception.Message); | ||
} | ||
} | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do we need a new assembly?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
CancellationToken requires it.