< Summary - CoreWCF Coverage — PR #1766

Information
Class: CoreWCF.IdentityModel.Tokens.Saml.DSigSerializerExtended
Assembly: CoreWCF.Primitives
File(s): /home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/IdentityModel/Tokens/Saml/DSigSerializerExtended.cs
Line coverage
39%
Covered lines: 27
Uncovered lines: 42
Coverable lines: 69
Total lines: 144
Line coverage: 39.1%
Branch coverage
30%
Covered branches: 8
Total branches: 26
Branch coverage: 30.7%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
TryReadKeyInfoType(...)40%101053.84%
TryReadBinarySecret(...)50%2280%
CreateKeyInfo(...)100%11100%
TryReadSecurityTokenReference(...)75%4491.66%
TryReadEncryptedKey(...)0%10100%

File(s)

/home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/IdentityModel/Tokens/Saml/DSigSerializerExtended.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.Xml;
 6using Microsoft.IdentityModel.Logging;
 7using Microsoft.IdentityModel.Xml;
 8
 9namespace CoreWCF.IdentityModel.Tokens.Saml
 10{
 11    internal class DSigSerializerExtended : DSigSerializer
 12    {
 13        protected override bool TryReadKeyInfoType(XmlReader reader, ref Microsoft.IdentityModel.Xml.KeyInfo keyInfo)
 14        {
 415            if (keyInfo is KeyInfo keyInfoType)
 16            {
 17
 418                if (TryReadSecurityTokenReference(reader, out SecurityTokenReference securityTokenReference))
 19                {
 320                    keyInfoType.SecurityTokenReference = securityTokenReference;
 321                    return true;
 22                }
 123                else if (TryReadBinarySecret(reader, out BinarySecret secret))
 24                {
 125                    keyInfoType.BinarySecret = secret;
 126                    return true;
 27                }
 028                else if (reader.IsStartElement(XmlEncryptionStrings.EncryptedKey))
 29                {
 030                    if (TryReadEncryptedKey(reader, out var encryptedKey))
 31                    {
 032                        keyInfoType.EncryptedKey = encryptedKey;
 033                        return true;
 34                    }
 35                    else
 036                        return false;
 37                }
 38            }
 39
 040            return base.TryReadKeyInfoType(reader, ref keyInfo);
 41        }
 42
 43        private static bool TryReadBinarySecret(XmlReader reader, out BinarySecret secret)
 44        {
 145            secret = null;
 146            if (!reader.IsStartElement("BinarySecret", "http://docs.oasis-open.org/ws-sx/ws-trust/200512"))
 047                return false;
 48
 149            secret = new BinarySecret(reader.ReadElementContentAsString());
 50
 151            return true;
 52        }
 53
 54        protected override Microsoft.IdentityModel.Xml.KeyInfo CreateKeyInfo(XmlReader reader)
 55        {
 456            Microsoft.IdentityModel.Xml.XmlUtil.CheckReaderOnEntry(reader, XmlSignatureConstants.Elements.KeyInfo, XmlSi
 57
 458            return new KeyInfo
 459            {
 460                Prefix = reader.Prefix
 461            };
 62        }
 63
 64        /// <summary>
 65        /// Reads the "SecurityTokenReference" element
 66        /// </summary>
 67        /// <param name="reader">A <see cref="XmlReader"/> positioned on a <see cref="CoreWcfXmlSignatureConstants.Eleme
 68        /// <param name="securityTokenReference"></param>
 69        private static bool TryReadSecurityTokenReference(XmlReader reader, out SecurityTokenReference securityTokenRefe
 70        {
 471            securityTokenReference = null;
 472            if (!reader.IsStartElement(CoreWCF.XD.SecurityJan2004Dictionary.SecurityTokenReference.Value, CoreWCF.XD.Sec
 173                return false;
 74
 375            reader.ReadStartElement(CoreWCF.XD.SecurityJan2004Dictionary.SecurityTokenReference.Value, CoreWCF.XD.Securi
 76
 377            if (!reader.IsStartElement(CoreWCF.XD.SecurityJan2004Dictionary.KeyIdentifier.Value, CoreWCF.XD.SecurityJan2
 078                return false;
 79
 380            string valueType = reader.GetAttribute(CoreWCF.XD.SecurityJan2004Dictionary.ValueType.Value, null);
 381            string encodingType = reader.GetAttribute(CoreWCF.XD.SecurityJan2004Dictionary.EncodingType.Value, null);
 382            string value = reader.ReadElementContentAsString(CoreWCF.XD.SecurityJan2004Dictionary.KeyIdentifier.Value, C
 83
 384            reader.ReadEndElement();
 85
 386            securityTokenReference = new SecurityTokenReference(new SecurityKeyIdentifier(valueType, encodingType, value
 87
 388            return true;
 89        }
 90
 91        /// <summary>
 92        /// Attempts to read the <see cref="CoreWcfXmlSignatureConstants.Elements.EncryptedKey"/>
 93        /// </summary>
 94        /// <param name="reader">A <see cref="XmlReader"/> positioned on a <see cref="CoreWcfXmlSignatureConstants.Eleme
 95        /// <param name="encryptedKey">The parsed <see cref="CoreWcfXmlSignatureConstants.Elements.EncryptedKey"/> eleme
 96        protected virtual bool TryReadEncryptedKey(XmlReader reader, out EncryptedKey encryptedKey)
 97        {
 098            if (reader == null)
 099                throw new ArgumentNullException(LogHelper.FormatInvariant("IDX10000: The parameter '{0}' cannot be a 'nu
 100
 0101            encryptedKey = null;
 102
 0103            if (!reader.IsStartElement(XmlEncryptionStrings.EncryptedKey, XmlSignatureStrings.Namespace))
 0104                return false;
 105
 0106            reader.ReadStartElement(XmlEncryptionStrings.EncryptedKey, XmlSignatureStrings.Namespace);
 107
 0108            if (!reader.IsStartElement(XmlEncryptionStrings.EncryptionMethod, XmlSignatureStrings.Namespace))
 0109                throw Microsoft.IdentityModel.Xml.XmlUtil.LogReadException(
 0110                    "IDX30011: Unable to read XML. Expecting XmlReader to be at ns.element: '{0}.{1}', found: '{2}.{3}'.
 0111                    XmlSignatureStrings.Namespace,
 0112                    XmlEncryptionStrings.EncryptionMethod,
 0113                    reader.NamespaceURI,
 0114                    reader.LocalName);
 115
 0116            string algorithm = reader.GetAttribute(XmlEncryptionStrings.AlgorithmAttribute);
 0117            Microsoft.IdentityModel.Xml.KeyInfo keyInfo = ReadKeyInfo(reader);
 118
 0119            if (!reader.IsStartElement(XmlEncryptionStrings.CipherData, XmlSignatureStrings.Namespace))
 0120                throw Microsoft.IdentityModel.Xml.XmlUtil.LogReadException(
 0121                    "IDX30011: Unable to read XML. Expecting XmlReader to be at ns.element: '{0}.{1}', found: '{2}.{3}'.
 0122                    XmlSignatureStrings.Namespace,
 0123                    XmlEncryptionStrings.CipherData,
 0124                    reader.NamespaceURI,
 0125                    reader.LocalName);
 126
 0127            reader.ReadStartElement(XmlEncryptionStrings.CipherData, XmlSignatureStrings.Namespace);
 128
 0129            if (!reader.IsStartElement(XmlEncryptionStrings.CipherValue, XmlSignatureStrings.Namespace))
 0130                throw Microsoft.IdentityModel.Xml.XmlUtil.LogReadException(
 0131                    "IDX30011: Unable to read XML. Expecting XmlReader to be at ns.element: '{0}.{1}', found: '{2}.{3}'.
 0132                    XmlSignatureStrings.Namespace,
 0133                    XmlEncryptionStrings.CipherValue,
 0134                    reader.NamespaceURI,
 0135                    reader.LocalName);
 136
 0137            string cipherValue = reader.ReadElementContentAsString(XmlEncryptionStrings.CipherValue, XmlSignatureStrings
 0138            reader.ReadEndElement();
 0139            encryptedKey = new EncryptedKey(algorithm, keyInfo, cipherValue);
 140
 0141            return true;
 142        }
 143    }
 144}