< Summary - CoreWCF Coverage — PR #1766

Information
Class: CoreWCF.EndpointIdentity
Assembly: CoreWCF.Primitives
File(s): /home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/EndpointIdentity.cs
Line coverage
22%
Covered lines: 15
Uncovered lines: 52
Coverable lines: 67
Total lines: 232
Line coverage: 22.3%
Branch coverage
20%
Covered branches: 8
Total branches: 40
Branch coverage: 20%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
.ctor()100%11100%
Initialize(...)50%2275%
Initialize(...)50%22100%
CreateIdentity(...)60%101063.63%
CreateDnsIdentity(...)100%110%
CreateSpnIdentity(...)100%110%
CreateUpnIdentity(...)100%110%
CreateX509CertificateIdentity(...)100%110%
EnsureIdentityClaim()100%110%
Equals(...)0%660%
GetHashCode()100%110%
Matches(...)100%110%
GetClaimComparer()0%220%
ReadIdentity(...)0%14140%
WriteTo(...)0%220%
WriteContentsTo(...)100%110%

File(s)

/home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/EndpointIdentity.cs

#LineLine coverage
 1// Licensed to the .NET Foundation under one or more agreements.
 2// The .NET Foundation licenses this file to you under the MIT license.
 3
 4using System;
 5using System.Collections.Generic;
 6using System.Security.Cryptography.X509Certificates;
 7using System.Xml;
 8using CoreWCF.IdentityModel.Claims;
 9
 10namespace CoreWCF
 11{
 12    public abstract class EndpointIdentity
 13    {
 14        private Claim _identityClaim;
 15        private IEqualityComparer<Claim> _claimComparer;
 16
 2717        protected EndpointIdentity()
 18        {
 2719        }
 20
 21        internal void Initialize(Claim identityClaim)
 22        {
 2723            if (identityClaim == null)
 24            {
 025                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(identityClaim));
 26            }
 27
 2728            Initialize(identityClaim, null);
 2729        }
 30
 31        internal void Initialize(Claim identityClaim, IEqualityComparer<Claim> claimComparer)
 32        {
 2733            _identityClaim = identityClaim ?? throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(id
 2734            _claimComparer = claimComparer;
 2735        }
 36
 37        internal Claim IdentityClaim
 38        {
 39            get
 40            {
 041                if (_identityClaim == null)
 42                {
 043                    EnsureIdentityClaim();
 44                }
 045                return _identityClaim;
 46            }
 47        }
 48
 49        public static EndpointIdentity CreateIdentity(Claim identity)
 50        {
 1551            if (identity == null)
 52            {
 053                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(identity));
 54            }
 55
 1556            if (identity.ClaimType.Equals(ClaimTypes.Dns))
 57            {
 558                return new DnsEndpointIdentity(identity);
 59            }
 1060            else if (identity.ClaimType.Equals(ClaimTypes.Spn))
 61            {
 062                return new SpnEndpointIdentity(identity);
 63            }
 1064            else if (identity.ClaimType.Equals(ClaimTypes.Upn))
 65            {
 066                return new UpnEndpointIdentity(identity);
 67            }
 1068            else if (identity.ClaimType.Equals(ClaimTypes.Rsa))
 69            {
 070                throw new PlatformNotSupportedException();
 71                //return new RsaEndpointIdentity(identity);
 72            }
 73            else
 74            {
 1075                return new GeneralEndpointIdentity(identity);
 76            }
 77        }
 78
 79        internal static EndpointIdentity CreateDnsIdentity(string dnsName)
 80        {
 081            return new DnsEndpointIdentity(dnsName);
 82        }
 83
 84        internal static EndpointIdentity CreateSpnIdentity(string spnName)
 85        {
 086            return new SpnEndpointIdentity(spnName);
 87        }
 88
 89        internal static EndpointIdentity CreateUpnIdentity(string upnName)
 90        {
 091            return new UpnEndpointIdentity(upnName);
 92        }
 93
 94        public static EndpointIdentity CreateX509CertificateIdentity(X509Certificate2 certificate)
 95        {
 096            return new X509CertificateEndpointIdentity(certificate);
 97        }
 98
 99        internal virtual void EnsureIdentityClaim()
 100        {
 0101        }
 102
 103        public override bool Equals(object obj)
 104        {
 0105            if (obj == (object)this)
 106            {
 0107                return true;
 108            }
 109
 110            // as handles null do we need the double null check?
 0111            if (obj == null)
 112            {
 0113                return false;
 114            }
 115
 0116            if (!(obj is EndpointIdentity otherIdentity))
 117            {
 0118                return false;
 119            }
 120
 0121            return Matches(otherIdentity.IdentityClaim);
 122        }
 123
 124        public override int GetHashCode()
 125        {
 0126            return GetClaimComparer().GetHashCode(IdentityClaim);
 127        }
 128
 129        internal bool Matches(Claim claim)
 130        {
 0131            return GetClaimComparer().Equals(IdentityClaim, claim);
 132        }
 133
 134        private IEqualityComparer<Claim> GetClaimComparer()
 135        {
 0136            if (_claimComparer == null)
 137            {
 0138                throw new PlatformNotSupportedException("EndpointIdentity.GetClaimComparer is not supported.");
 139            }
 140
 0141            return _claimComparer;
 142        }
 143
 144
 145        internal static EndpointIdentity ReadIdentity(XmlDictionaryReader reader)
 146        {
 0147            if (reader == null)
 148            {
 0149                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(reader));
 150            }
 151
 0152            reader.MoveToContent();
 0153            if (reader.IsEmptyElement)
 154            {
 0155                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new XmlException(SR.Format(SR.UnexpectedEmptyE
 156            }
 157
 0158            reader.ReadStartElement(XD.AddressingDictionary.Identity, XD.AddressingDictionary.IdentityExtensionNamespace
 159
 160
 161            EndpointIdentity readIdentity;
 162            //if (reader.IsStartElement(XD.AddressingDictionary.Spn, XD.AddressingDictionary.IdentityExtensionNamespace)
 163            //{
 164            //    readIdentity = new SpnEndpointIdentity(reader.ReadElementString());
 165            //}
 166            //else if (reader.IsStartElement(XD.AddressingDictionary.Upn, XD.AddressingDictionary.IdentityExtensionNames
 167            //{
 168            //    readIdentity = new UpnEndpointIdentity(reader.ReadElementString());
 169            //}
 170            //else
 0171            if (reader.IsStartElement(XD.AddressingDictionary.Dns, XD.AddressingDictionary.IdentityExtensionNamespace))
 172            {
 0173                readIdentity = new DnsEndpointIdentity(reader.ReadElementString());
 174            }
 0175            else if (reader.IsStartElement(XD.XmlSignatureDictionary.KeyInfo, XD.XmlSignatureDictionary.Namespace))
 176            {
 0177                reader.ReadStartElement();
 0178                if (reader.IsStartElement(XD.XmlSignatureDictionary.X509Data, XD.XmlSignatureDictionary.Namespace))
 179                {
 0180                    throw new PlatformNotSupportedException("EndpointIdentity.ReadIdentity X509CertificateEndpointIdenti
 181                    //readIdentity = new X509CertificateEndpointIdentity(reader);
 182                }
 0183                else if (reader.IsStartElement(XD.XmlSignatureDictionary.RsaKeyValue, XD.XmlSignatureDictionary.Namespac
 184                {
 0185                    throw new PlatformNotSupportedException("EndpointIdentity.ReadIdentity RsaEndpointIdentity is not su
 186                }
 187                else
 188                {
 0189                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new XmlException(SR.Format(SR.Unrecognized
 190                }
 191                //reader.ReadEndElement();
 192            }
 0193            else if (reader.NodeType == XmlNodeType.Element)
 194            {
 195                //
 196                // Something unknown
 197                //
 0198                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new XmlException(SR.Format(SR.UnrecognizedIden
 199            }
 200            else
 201            {
 202                //
 203                // EndpointIdentity element is empty or some other invalid xml
 204                //
 0205                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new XmlException(SR.InvalidIdentityElement));
 206            }
 207
 0208            reader.ReadEndElement();
 209
 0210            return readIdentity;
 211        }
 212
 213        internal void WriteTo(XmlDictionaryWriter writer)
 214        {
 0215            if (writer == null)
 216            {
 0217                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(writer));
 218            }
 219
 0220            writer.WriteStartElement(XD.AddressingDictionary.Identity, XD.AddressingDictionary.IdentityExtensionNamespac
 221
 0222            WriteContentsTo(writer);
 223
 0224            writer.WriteEndElement();
 0225        }
 226
 227        internal virtual void WriteContentsTo(XmlDictionaryWriter writer)
 228        {
 0229            throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Format(SR.Unrecog
 230        }
 231    }
 232}