Skip to content

Commit

Permalink
Unit test for SecurityTokenManager.
Browse files Browse the repository at this point in the history
  • Loading branch information
imcarolwang authored and StephenBonikowsky committed Nov 4, 2019
1 parent d3d3352 commit ddca7f4
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.


using System.Collections.ObjectModel;
using System.IdentityModel.Policy;
using System.IdentityModel.Selectors;
using System.IdentityModel.Tokens;
using Infrastructure.Common;
using Xunit;

public class SecurityTokenManagerTest
{
[WcfFact]
public static void Methods_Override()
{
var tokenManager = new SecurityTokenManagerImpl();
SecurityTokenVersionImpl tokenVersion = new SecurityTokenVersionImpl();
SecurityTokenRequirement tokenRequirement = new SecurityTokenRequirement();

SecurityTokenAuthenticator authenticator = tokenManager.CreateSecurityTokenAuthenticator(tokenRequirement, out SecurityTokenResolver resolver);
SecurityTokenProvider provider = tokenManager.CreateSecurityTokenProvider(tokenRequirement);
SecurityTokenSerializer serializer = tokenManager.CreateSecurityTokenSerializer(tokenVersion);

Assert.IsType<SecurityTokenAuthenticatorImpl>(authenticator);
Assert.IsType<SecurityTokenResolverImpl>(resolver);
Assert.IsType<SecurityTokenProviderSyncImpl>(provider);
Assert.IsType<SecurityTokenSerializerImpl>(serializer);
}
}

public class SecurityTokenManagerImpl : SecurityTokenManager
{
public override SecurityTokenAuthenticator CreateSecurityTokenAuthenticator(SecurityTokenRequirement tokenRequirement, out SecurityTokenResolver outOfBandTokenResolver)
{
Assert.IsType<SecurityTokenRequirement>(tokenRequirement);
outOfBandTokenResolver = new SecurityTokenResolverImpl();
return new SecurityTokenAuthenticatorImpl();
}

public override SecurityTokenProvider CreateSecurityTokenProvider(SecurityTokenRequirement tokenRequirement)
{
Assert.IsType<SecurityTokenRequirement>(tokenRequirement);
return new SecurityTokenProviderSyncImpl();
}

public override SecurityTokenSerializer CreateSecurityTokenSerializer(SecurityTokenVersion tokenVersion)
{
Assert.IsType<SecurityTokenVersionImpl>(tokenVersion);
return new SecurityTokenSerializerImpl();
}
}

public class SecurityTokenVersionImpl : SecurityTokenVersion
{
public override ReadOnlyCollection<string> GetSecuritySpecifications()
{
return null;
}
}

public class SecurityTokenAuthenticatorImpl : SecurityTokenAuthenticator
{
protected override bool CanValidateTokenCore(SecurityToken token)
{
return false;
}

protected override ReadOnlyCollection<IAuthorizationPolicy> ValidateTokenCore(SecurityToken token)
{
return null;
}
}

public class SecurityTokenResolverImpl : SecurityTokenResolver
{
protected override bool TryResolveSecurityKeyCore(SecurityKeyIdentifierClause keyIdentifierClause, out SecurityKey key)
{
key = null;
return false;
}

protected override bool TryResolveTokenCore(SecurityKeyIdentifier keyIdentifier, out SecurityToken token)
{
token = null;
return false;
}

protected override bool TryResolveTokenCore(SecurityKeyIdentifierClause keyIdentifierClause, out SecurityToken token)
{
token = null;
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ public static void Methods_NonNullParam_InvokeAndReturn()
var xmlWriter = XmlWriter.Create(new MemoryStream());
var dummyToken = new DummySecurityToken();
var keyIdentifier = new SecurityKeyIdentifier();
var keyIdentifierClause = new SecurityKeyIdentifierClauseImp("DummyClause");
var sts = new SecurityTokenSerializerImp();
var keyIdentifierClause = new SecurityKeyIdentifierClauseImpl("DummyClause");
var sts = new SecurityTokenSerializerImpl();

Assert.NotNull(sts);
Assert.True(sts.CanReadKeyIdentifier(xmlReader));
Expand All @@ -38,7 +38,7 @@ public static void Methods_NonNullParam_InvokeAndReturn()
SecurityKeyIdentifierClause identifierClause = sts.ReadKeyIdentifierClause(xmlReader);
Assert.IsType<DummySecurityToken>(token);
Assert.IsType<SecurityKeyIdentifier>(identifier);
Assert.IsType<SecurityKeyIdentifierClauseImp>(identifierClause);
Assert.IsType<SecurityKeyIdentifierClauseImpl>(identifierClause);

sts.WriteToken(xmlWriter, dummyToken);
sts.WriteKeyIdentifier(xmlWriter, keyIdentifier);
Expand All @@ -51,7 +51,7 @@ public static void Methods_NonNullParam_InvokeAndReturn()
[WcfFact]
public static void Methods_NullParam_Throws()
{
var sts = new SecurityTokenSerializerImp();
var sts = new SecurityTokenSerializerImpl();

Assert.NotNull(sts);
Assert.Throws<ArgumentNullException>(() => sts.CanReadKeyIdentifier(null));
Expand All @@ -72,7 +72,7 @@ public static void Methods_NullParam_Throws()
}
}

public class SecurityTokenSerializerImp : SecurityTokenSerializer
public class SecurityTokenSerializerImpl : SecurityTokenSerializer
{
public bool WriteTokenCoreCalled = false;
public bool WriteKeyIdentifierCoreCalled = false;
Expand Down Expand Up @@ -110,7 +110,7 @@ protected override bool CanWriteTokenCore(SecurityToken token)

protected override SecurityKeyIdentifierClause ReadKeyIdentifierClauseCore(XmlReader reader)
{
return new SecurityKeyIdentifierClauseImp("DummyClause");
return new SecurityKeyIdentifierClauseImpl("DummyClause");
}

protected override SecurityKeyIdentifier ReadKeyIdentifierCore(XmlReader reader)
Expand Down Expand Up @@ -139,13 +139,13 @@ protected override void WriteTokenCore(XmlWriter writer, SecurityToken token)
}
}

public class SecurityKeyIdentifierClauseImp : SecurityKeyIdentifierClause
public class SecurityKeyIdentifierClauseImpl : SecurityKeyIdentifierClause
{
public SecurityKeyIdentifierClauseImp(string clauseType) : base(clauseType)
public SecurityKeyIdentifierClauseImpl(string clauseType) : base(clauseType)
{
}

public SecurityKeyIdentifierClauseImp(string clauseType, byte[] nonce, int length) : base(clauseType, nonce, length)
public SecurityKeyIdentifierClauseImpl(string clauseType, byte[] nonce, int length) : base(clauseType, nonce, length)
{
}
}

0 comments on commit ddca7f4

Please sign in to comment.