< Summary - CoreWCF Coverage — PR #1766

Information
Class: CoreWCF.Security.XmlHelper
Assembly: CoreWCF.Primitives
File(s): /home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/Security/XmlHelper.cs
Line coverage
25%
Covered lines: 42
Uncovered lines: 124
Coverable lines: 166
Total lines: 435
Line coverage: 25.3%
Branch coverage
22%
Covered branches: 27
Total branches: 122
Branch coverage: 22.1%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

File(s)

/home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/Security/XmlHelper.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.Text;
 6using System.Xml;
 7
 8namespace CoreWCF.Security
 9{
 10    internal static class XmlHelper
 11    {
 12        internal static void AddNamespaceDeclaration(XmlDictionaryWriter writer, string prefix, XmlDictionaryString ns)
 13        {
 2014            string p = writer.LookupPrefix(ns.Value);
 2015            if (p == null || p != prefix)
 16            {
 2017                writer.WriteXmlnsAttribute(prefix, ns);
 18            }
 2019        }
 20
 21        internal static string EnsureNamespaceDefined(XmlDictionaryWriter writer, XmlDictionaryString ns, string default
 22        {
 023            string p = writer.LookupPrefix(ns.Value);
 024            if (p == null)
 25            {
 026                writer.WriteXmlnsAttribute(defaultPrefix, ns);
 027                p = defaultPrefix;
 28            }
 29
 030            return p;
 31        }
 32
 33        internal static XmlQualifiedName GetAttributeValueAsQName(XmlReader reader, string attributeName)
 34        {
 035            string qname = reader.GetAttribute(attributeName);
 036            if (qname == null)
 37            {
 038                return null;
 39            }
 040            return GetValueAsQName(reader, qname);
 41        }
 42
 43        /// <summary>
 44        /// Enforces that parent has exactly 1 child of type XML element and nothing else (barring comments and whitespa
 45        /// and returns the child
 46        /// </summary>
 47        internal static XmlElement GetChildElement(XmlElement parent)
 48        {
 1049            if (parent == null)
 50            {
 051                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(parent));
 52            }
 53
 1054            XmlElement result = null;
 4055            for (int i = 0; i < parent.ChildNodes.Count; ++i)
 56            {
 1057                XmlNode child = parent.ChildNodes[i];
 1058                if (child.NodeType == XmlNodeType.Whitespace || child.NodeType == XmlNodeType.Comment)
 59                {
 60                    continue;
 61                }
 1062                else if (child.NodeType == XmlNodeType.Element && result == null)
 63                {
 1064                    result = ((XmlElement)child);
 65                }
 66                else
 67                {
 068                    OnUnexpectedChildNodeError(parent, child);
 69                }
 70            }
 71
 1072            if (result == null)
 73            {
 074                OnChildNodeTypeMissing(parent, XmlNodeType.Element);
 75            }
 76
 1077            return result;
 78        }
 79
 80        internal static XmlElement GetChildElement(XmlElement parent, string childLocalName, string childNamespace)
 81        {
 082            if (parent == null)
 83            {
 084                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(parent));
 85            }
 86
 087            for (int i = 0; i < parent.ChildNodes.Count; ++i)
 88            {
 089                XmlNode child = parent.ChildNodes[i];
 90
 091                if (child.NodeType == XmlNodeType.Whitespace || child.NodeType == XmlNodeType.Comment)
 92                {
 93                    continue;
 94                }
 095                else if (child.NodeType == XmlNodeType.Element)
 96                {
 097                    if (child.LocalName == childLocalName && child.NamespaceURI == childNamespace)
 98                    {
 099                        return ((XmlElement)child);
 100                    }
 101                }
 102                else
 103                {
 0104                    OnUnexpectedChildNodeError(parent, child);
 105                }
 106            }
 107
 0108            return null;
 109        }
 110
 111        internal static XmlQualifiedName GetValueAsQName(XmlReader reader, string value)
 112        {
 0113            SplitIntoPrefixAndName(value, out string prefix, out string name);
 0114            string ns = reader.LookupNamespace(prefix);
 0115            if (ns == null && prefix.Length > 0)
 116            {
 0117                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new XmlException(SR.Format(SR.CouldNotFindName
 118            }
 0119            return new XmlQualifiedName(name, ns);
 120        }
 121
 122        internal static string GetWhiteSpace(XmlReader reader)
 123        {
 0124            string s = null;
 0125            StringBuilder sb = null;
 0126            while (reader.NodeType == XmlNodeType.Whitespace || reader.NodeType == XmlNodeType.SignificantWhitespace)
 127            {
 0128                if (sb != null)
 129                {
 0130                    sb.Append(reader.Value);
 131                }
 0132                else if (s != null)
 133                {
 0134                    sb = new StringBuilder(s);
 0135                    sb.Append(reader.Value);
 0136                    s = null;
 137                }
 138                else
 139                {
 0140                    s = reader.Value;
 141                }
 0142                if (!reader.Read())
 143                {
 144                    break;
 145                }
 146            }
 0147            return sb != null ? sb.ToString() : s;
 148        }
 149
 150        internal static bool IsWhitespaceOrComment(XmlReader reader)
 151        {
 0152            if (reader.NodeType == XmlNodeType.Comment)
 153            {
 0154                return true;
 155            }
 0156            else if (reader.NodeType == XmlNodeType.Whitespace)
 157            {
 0158                return true;
 159            }
 160            else
 161            {
 0162                return false;
 163            }
 164        }
 165
 166        internal static void OnChildNodeTypeMissing(string parentName, XmlNodeType expectedNodeType)
 167        {
 0168            throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new XmlException(SR.Format(SR.ChildNodeTypeMissing
 169        }
 170
 171        internal static void OnChildNodeTypeMissing(XmlElement parent, XmlNodeType expectedNodeType)
 172        {
 0173            throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new XmlException(SR.Format(SR.ChildNodeTypeMissing
 174        }
 175
 176        internal static void OnEmptyElementError(XmlReader r)
 177        {
 0178            throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new XmlException(SR.Format(SR.EmptyXmlElementError
 179        }
 180
 181        internal static void OnEmptyElementError(XmlElement e)
 182        {
 0183            throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new XmlException(SR.Format(SR.EmptyXmlElementError
 184        }
 185
 186        internal static void OnEOF()
 187        {
 0188            throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new XmlException(SR.Format(SR.UnexpectedEndOfFile)
 189        }
 190
 191        internal static void OnNamespaceMissing(string prefix)
 192        {
 0193            throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new XmlException(SR.Format(SR.CouldNotFindNamespac
 194        }
 195
 196        internal static void OnRequiredAttributeMissing(string attrName, string elementName)
 197        {
 0198            throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new XmlException(SR.Format(SR.RequiredAttributeMis
 199        }
 200
 201        internal static void OnRequiredElementMissing(string elementName, string elementNamespace)
 202        {
 0203            throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new XmlException(SR.Format(SR.ExpectedElementMissi
 204        }
 205
 206        internal static void OnUnexpectedChildNodeError(string parentName, XmlReader r)
 207        {
 0208            throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new XmlException(SR.Format(SR.UnexpectedXmlChildNo
 209        }
 210
 211        internal static void OnUnexpectedChildNodeError(XmlElement parent, XmlNode n)
 212        {
 0213            throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new XmlException(SR.Format(SR.UnexpectedXmlChildNo
 214        }
 215
 216        internal static string ReadEmptyElementAndRequiredAttribute(XmlDictionaryReader reader,
 217            XmlDictionaryString name, XmlDictionaryString namespaceUri, XmlDictionaryString attributeName,
 218            out string prefix)
 219        {
 0220            reader.MoveToStartElement(name, namespaceUri);
 0221            prefix = reader.Prefix;
 0222            bool isEmptyElement = reader.IsEmptyElement;
 0223            string value = reader.GetAttribute(attributeName, null);
 0224            if (value == null)
 225            {
 0226                OnRequiredAttributeMissing(attributeName.Value, null);
 227            }
 0228            reader.Read();
 229
 0230            if (!isEmptyElement)
 231            {
 0232                reader.ReadEndElement();
 233            }
 0234            return value;
 235        }
 236
 237        internal static string GetRequiredNonEmptyAttribute(XmlDictionaryReader reader, XmlDictionaryString name, XmlDic
 238        {
 0239            string value = reader.GetAttribute(name, ns);
 0240            if (value == null || value.Length == 0)
 241            {
 0242                OnRequiredAttributeMissing(name.Value, reader?.Name);
 243            }
 0244            return value;
 245        }
 246
 247        internal static byte[] GetRequiredBase64Attribute(XmlDictionaryReader reader, XmlDictionaryString name, XmlDicti
 248        {
 0249            if (!reader.MoveToAttribute(name.Value, ns?.Value))
 250            {
 0251                OnRequiredAttributeMissing(name.Value, ns?.Value);
 252            }
 0253            byte[] value = reader.ReadContentAsBase64();
 0254            if (value == null || value.Length == 0)
 255            {
 0256                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
 0257                    new XmlException(SR.Format(SR.EmptyBase64Attribute, name, ns)));
 258            }
 259
 0260            return value;
 261        }
 262
 263        internal static string ReadTextElementAsTrimmedString(XmlElement element)
 264        {
 40265            if (element == null)
 266            {
 0267                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(element));
 268            }
 269
 40270            using (XmlReader reader = new XmlNodeReader(element))
 271            {
 40272                reader.MoveToContent();
 40273                return XmlUtil.Trim(reader.ReadElementContentAsString());
 274            }
 40275        }
 276
 277        internal static void SplitIntoPrefixAndName(string qName, out string prefix, out string name)
 278        {
 0279            string[] parts = qName.Split(':');
 0280            if (parts.Length > 2)
 281            {
 0282                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(SR.Format(SR.InvalidQName));
 283            }
 284
 0285            if (parts.Length == 2)
 286            {
 0287                prefix = parts[0].Trim();
 0288                name = parts[1].Trim();
 289            }
 290            else
 291            {
 0292                prefix = string.Empty;
 0293                name = qName.Trim();
 294            }
 0295        }
 296
 297        internal static void ValidateIdPrefix(string idPrefix)
 298        {
 0299            if (idPrefix == null)
 300            {
 0301                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException(nameof(idPrefix)));
 302            }
 303
 0304            if (idPrefix.Length == 0)
 305            {
 0306                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(idPrefi
 307            }
 308
 0309            if ((!char.IsLetter(idPrefix[0]) && idPrefix[0] != '_'))
 310            {
 0311                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(idPrefi
 312            }
 313
 0314            for (int i = 1; i < idPrefix.Length; i++)
 315            {
 0316                char c = idPrefix[i];
 0317                if (!char.IsLetter(c) && !char.IsNumber(c) && c != '.' && c != '_' && c != '-')
 318                {
 0319                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(idP
 320                }
 321            }
 0322        }
 323
 324        internal static UniqueId GetAttributeAsUniqueId(XmlDictionaryReader reader, XmlDictionaryString localName, XmlDi
 325        {
 20326            return GetAttributeAsUniqueId(reader, localName.Value, (ns?.Value));
 327        }
 328
 329        private static UniqueId GetAttributeAsUniqueId(XmlDictionaryReader reader, string name, string ns)
 330        {
 20331            if (!reader.MoveToAttribute(name, ns))
 332            {
 10333                return null;
 334            }
 335
 10336            UniqueId id = reader.ReadContentAsUniqueId();
 10337            reader.MoveToElement();
 338
 10339            return id;
 340        }
 341
 342        public static void WriteAttributeStringAsUniqueId(XmlDictionaryWriter writer, string prefix, XmlDictionaryString
 343        {
 10344            writer.WriteStartAttribute(prefix, localName, ns);
 10345            writer.WriteValue(id);
 10346            writer.WriteEndAttribute();
 10347        }
 348
 349        public static void WriteElementStringAsUniqueId(XmlWriter writer, string localName, UniqueId id)
 350        {
 0351            writer.WriteStartElement(localName);
 0352            writer.WriteValue(id);
 0353            writer.WriteEndElement();
 0354        }
 355        public static void WriteElementStringAsUniqueId(XmlDictionaryWriter writer, XmlDictionaryString localName, XmlDi
 356        {
 0357            writer.WriteStartElement(localName, ns);
 0358            writer.WriteValue(id);
 0359            writer.WriteEndElement();
 0360        }
 361
 362        public static void WriteElementContentAsInt64(XmlDictionaryWriter writer, XmlDictionaryString localName, XmlDict
 363        {
 0364            writer.WriteStartElement(localName, ns);
 0365            writer.WriteValue(value);
 0366            writer.WriteEndElement();
 0367        }
 368
 369        public static long ReadElementContentAsInt64(XmlDictionaryReader reader)
 370        {
 0371            reader.ReadFullStartElement();
 0372            long i = reader.ReadContentAsLong();
 0373            reader.ReadEndElement();
 0374            return i;
 375        }
 376
 377        public static void WriteStringAsUniqueId(XmlDictionaryWriter writer, UniqueId id)
 378        {
 10379            writer.WriteValue(id);
 10380        }
 381
 382        public static UniqueId ReadElementStringAsUniqueId(XmlDictionaryReader reader, XmlDictionaryString localName, Xm
 383        {
 20384            if (reader.IsStartElement(localName, ns) && reader.IsEmptyElement)
 385            {
 0386                reader.Read();
 0387                return new UniqueId(string.Empty);
 388            }
 389
 20390            reader.ReadStartElement(localName, ns);
 20391            UniqueId id = reader.ReadContentAsUniqueId();
 20392            reader.ReadEndElement();
 20393            return id;
 394        }
 395
 396        public static UniqueId ReadElementStringAsUniqueId(XmlDictionaryReader reader)
 397        {
 0398            if (reader.IsStartElement() && reader.IsEmptyElement)
 399            {
 0400                reader.Read();
 0401                return new UniqueId(string.Empty);
 402            }
 403
 0404            reader.ReadStartElement();
 0405            UniqueId id = reader.ReadContentAsUniqueId();
 0406            reader.ReadEndElement();
 0407            return id;
 408        }
 409
 410        public static UniqueId ReadTextElementAsUniqueId(XmlElement element)
 411        {
 0412            return new UniqueId(ReadTextElementAsTrimmedString(element));
 413        }
 414
 415        internal static string[] TokenizeInclusiveNamespacesPrefixList(string inclusiveNamespacesPrefixList)
 416        {
 23417            if (inclusiveNamespacesPrefixList == null)
 22418                return null;
 419
 1420            string[] prefixes = inclusiveNamespacesPrefixList.Split(Array.Empty<char>(), StringSplitOptions.RemoveEmptyE
 1421            if (prefixes.Length == 0)
 0422                return null;
 423
 6424            for (int i = 0; i < prefixes.Length; i++)
 425            {
 2426                if (prefixes[i] == "#default")
 427                {
 0428                    prefixes[i] = string.Empty;
 429                }
 430            }
 431
 1432            return prefixes;
 433        }
 434    }
 435}

Methods/Properties

AddNamespaceDeclaration(System.Xml.XmlDictionaryWriter,System.String,System.Xml.XmlDictionaryString)
EnsureNamespaceDefined(System.Xml.XmlDictionaryWriter,System.Xml.XmlDictionaryString,System.String)
GetAttributeValueAsQName(System.Xml.XmlReader,System.String)
GetChildElement(System.Xml.XmlElement)
GetChildElement(System.Xml.XmlElement,System.String,System.String)
GetValueAsQName(System.Xml.XmlReader,System.String)
GetWhiteSpace(System.Xml.XmlReader)
IsWhitespaceOrComment(System.Xml.XmlReader)
OnChildNodeTypeMissing(System.String,System.Xml.XmlNodeType)
OnChildNodeTypeMissing(System.Xml.XmlElement,System.Xml.XmlNodeType)
OnEmptyElementError(System.Xml.XmlReader)
OnEmptyElementError(System.Xml.XmlElement)
OnEOF()
OnNamespaceMissing(System.String)
OnRequiredAttributeMissing(System.String,System.String)
OnRequiredElementMissing(System.String,System.String)
OnUnexpectedChildNodeError(System.String,System.Xml.XmlReader)
OnUnexpectedChildNodeError(System.Xml.XmlElement,System.Xml.XmlNode)
ReadEmptyElementAndRequiredAttribute(System.Xml.XmlDictionaryReader,System.Xml.XmlDictionaryString,System.Xml.XmlDictionaryString,System.Xml.XmlDictionaryString,System.String&)
GetRequiredNonEmptyAttribute(System.Xml.XmlDictionaryReader,System.Xml.XmlDictionaryString,System.Xml.XmlDictionaryString)
GetRequiredBase64Attribute(System.Xml.XmlDictionaryReader,System.Xml.XmlDictionaryString,System.Xml.XmlDictionaryString)
ReadTextElementAsTrimmedString(System.Xml.XmlElement)
SplitIntoPrefixAndName(System.String,System.String&,System.String&)
ValidateIdPrefix(System.String)
GetAttributeAsUniqueId(System.Xml.XmlDictionaryReader,System.Xml.XmlDictionaryString,System.Xml.XmlDictionaryString)
GetAttributeAsUniqueId(System.Xml.XmlDictionaryReader,System.String,System.String)
WriteAttributeStringAsUniqueId(System.Xml.XmlDictionaryWriter,System.String,System.Xml.XmlDictionaryString,System.Xml.XmlDictionaryString,System.Xml.UniqueId)
WriteElementStringAsUniqueId(System.Xml.XmlWriter,System.String,System.Xml.UniqueId)
WriteElementStringAsUniqueId(System.Xml.XmlDictionaryWriter,System.Xml.XmlDictionaryString,System.Xml.XmlDictionaryString,System.Xml.UniqueId)
WriteElementContentAsInt64(System.Xml.XmlDictionaryWriter,System.Xml.XmlDictionaryString,System.Xml.XmlDictionaryString,System.Int64)
ReadElementContentAsInt64(System.Xml.XmlDictionaryReader)
WriteStringAsUniqueId(System.Xml.XmlDictionaryWriter,System.Xml.UniqueId)
ReadElementStringAsUniqueId(System.Xml.XmlDictionaryReader,System.Xml.XmlDictionaryString,System.Xml.XmlDictionaryString)
ReadElementStringAsUniqueId(System.Xml.XmlDictionaryReader)
ReadTextElementAsUniqueId(System.Xml.XmlElement)
TokenizeInclusiveNamespacesPrefixList(System.String)