From ddca7f4a4a627168d03905338bf6634d8d0a9898 Mon Sep 17 00:00:00 2001 From: v-carwan Date: Thu, 31 Oct 2019 13:37:29 +0800 Subject: [PATCH] Unit test for SecurityTokenManager. --- .../IdentityModel/SecurityTokenManagerTest.cs | 95 +++++++++++++++++++ .../SecurityTokenSerializerTest.cs | 18 ++-- 2 files changed, 104 insertions(+), 9 deletions(-) create mode 100644 src/System.ServiceModel.Primitives/tests/IdentityModel/SecurityTokenManagerTest.cs diff --git a/src/System.ServiceModel.Primitives/tests/IdentityModel/SecurityTokenManagerTest.cs b/src/System.ServiceModel.Primitives/tests/IdentityModel/SecurityTokenManagerTest.cs new file mode 100644 index 00000000000..fc1088105a6 --- /dev/null +++ b/src/System.ServiceModel.Primitives/tests/IdentityModel/SecurityTokenManagerTest.cs @@ -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(authenticator); + Assert.IsType(resolver); + Assert.IsType(provider); + Assert.IsType(serializer); + } +} + +public class SecurityTokenManagerImpl : SecurityTokenManager +{ + public override SecurityTokenAuthenticator CreateSecurityTokenAuthenticator(SecurityTokenRequirement tokenRequirement, out SecurityTokenResolver outOfBandTokenResolver) + { + Assert.IsType(tokenRequirement); + outOfBandTokenResolver = new SecurityTokenResolverImpl(); + return new SecurityTokenAuthenticatorImpl(); + } + + public override SecurityTokenProvider CreateSecurityTokenProvider(SecurityTokenRequirement tokenRequirement) + { + Assert.IsType(tokenRequirement); + return new SecurityTokenProviderSyncImpl(); + } + + public override SecurityTokenSerializer CreateSecurityTokenSerializer(SecurityTokenVersion tokenVersion) + { + Assert.IsType(tokenVersion); + return new SecurityTokenSerializerImpl(); + } +} + +public class SecurityTokenVersionImpl : SecurityTokenVersion +{ + public override ReadOnlyCollection GetSecuritySpecifications() + { + return null; + } +} + +public class SecurityTokenAuthenticatorImpl : SecurityTokenAuthenticator +{ + protected override bool CanValidateTokenCore(SecurityToken token) + { + return false; + } + + protected override ReadOnlyCollection 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; + } +} diff --git a/src/System.ServiceModel.Primitives/tests/IdentityModel/SecurityTokenSerializerTest.cs b/src/System.ServiceModel.Primitives/tests/IdentityModel/SecurityTokenSerializerTest.cs index f9f45f680a7..ba165678a2c 100644 --- a/src/System.ServiceModel.Primitives/tests/IdentityModel/SecurityTokenSerializerTest.cs +++ b/src/System.ServiceModel.Primitives/tests/IdentityModel/SecurityTokenSerializerTest.cs @@ -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)); @@ -38,7 +38,7 @@ public static void Methods_NonNullParam_InvokeAndReturn() SecurityKeyIdentifierClause identifierClause = sts.ReadKeyIdentifierClause(xmlReader); Assert.IsType(token); Assert.IsType(identifier); - Assert.IsType(identifierClause); + Assert.IsType(identifierClause); sts.WriteToken(xmlWriter, dummyToken); sts.WriteKeyIdentifier(xmlWriter, keyIdentifier); @@ -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(() => sts.CanReadKeyIdentifier(null)); @@ -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; @@ -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) @@ -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) { } }