< Summary - CoreWCF Coverage — PR #1766

Information
Class: CoreWCF.IdentityModel.Tokens.X509IssuerSerialKeyIdentifierClause
Assembly: CoreWCF.Primitives
File(s): /home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/IdentityModel/Tokens/X509IssuerSerialKeyIdentifierClause.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 37
Coverable lines: 37
Total lines: 105
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 20
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
.ctor(...)0%440%
.ctor(...)0%220%
Matches(...)0%440%
Matches(...)0%220%
Matches(...)0%880%
ToString()100%110%

File(s)

/home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/IdentityModel/Tokens/X509IssuerSerialKeyIdentifierClause.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.Globalization;
 5using System.Security.Cryptography;
 6using System.Security.Cryptography.X509Certificates;
 7
 8namespace CoreWCF.IdentityModel.Tokens
 9{
 10    public class X509IssuerSerialKeyIdentifierClause : SecurityKeyIdentifierClause
 11    {
 12        public X509IssuerSerialKeyIdentifierClause(string issuerName, string issuerSerialNumber)
 013            : base(null)
 14        {
 015            if (string.IsNullOrEmpty(issuerName))
 16            {
 017                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(issuerName));
 18            }
 19
 020            if (string.IsNullOrEmpty(issuerSerialNumber))
 21            {
 022                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(issuerSerialNumber));
 23            }
 24
 025            IssuerName = issuerName;
 026            IssuerSerialNumber = issuerSerialNumber;
 027        }
 28
 29        public X509IssuerSerialKeyIdentifierClause(X509Certificate2 certificate)
 030            : base(null)
 31        {
 032            if (certificate == null)
 33            {
 034                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(certificate));
 35            }
 36
 037            IssuerName = certificate.Issuer;
 038            IssuerSerialNumber = certificate.GetSerialNumberString();
 039        }
 40
 041        public string IssuerName { get; }
 42
 043        public string IssuerSerialNumber { get; }
 44
 45        public override bool Matches(SecurityKeyIdentifierClause keyIdentifierClause)
 46        {
 047            X509IssuerSerialKeyIdentifierClause that = keyIdentifierClause as X509IssuerSerialKeyIdentifierClause;
 048            return ReferenceEquals(this, that) || (that != null && that.Matches(IssuerName, IssuerSerialNumber));
 49        }
 50
 51        public bool Matches(X509Certificate2 certificate)
 52        {
 053            if (certificate == null)
 54            {
 055                return false;
 56            }
 57
 058            return Matches(certificate.Issuer, certificate.GetSerialNumberString());
 59        }
 60
 61        public bool Matches(string issuerName, string issuerSerialNumber)
 62        {
 063            if (issuerName == null)
 64            {
 065                return false;
 66            }
 67
 68            // If serial numbers dont match, we can avoid the potentially expensive issuer name comparison
 069            if (IssuerSerialNumber != issuerSerialNumber)
 70            {
 071                return false;
 72            }
 73
 74            // Serial numbers match. Do a string comparison of issuer names
 075            if (IssuerName == issuerName)
 76            {
 077                return true;
 78            }
 79
 80            // String equality comparison for issuer names failed
 81            // Do a byte-level comparison of the X500 distinguished names corresponding to the issuer names.
 82            // X500DistinguishedName constructor can throw for malformed inputs
 083            bool x500IssuerNameMatch = false;
 84            try
 85            {
 086                if (CoreWCF.Security.SecurityUtils.IsEqual(new X500DistinguishedName(IssuerName).RawData,
 087                                                                       new X500DistinguishedName(issuerName).RawData))
 88                {
 089                    x500IssuerNameMatch = true;
 90                }
 091            }
 092            catch (CryptographicException)
 93            {
 094            }
 95
 096            return x500IssuerNameMatch;
 97        }
 98
 99        public override string ToString()
 100        {
 0101            return string.Format(CultureInfo.InvariantCulture, "X509IssuerSerialKeyIdentifierClause(Issuer = '{0}', Seri
 0102                IssuerName, IssuerSerialNumber);
 103        }
 104    }
 105}