< Summary - CoreWCF Coverage — PR #1766

Information
Class: CoreWCF.Security.MessagePartSpecification
Assembly: CoreWCF.Primitives
File(s): /home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/Security/MessagePartSpecification.cs
Line coverage
63%
Covered lines: 55
Uncovered lines: 32
Coverable lines: 87
Total lines: 248
Line coverage: 63.2%
Branch coverage
57%
Covered branches: 40
Total branches: 70
Branch coverage: 57.1%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
Clear()0%440%
Union(...)83.33%121284.61%
MakeReadOnly()93.75%161694.44%
.ctor()100%11100%
.ctor(...)100%11100%
.ctor(...)100%11100%
.ctor(...)100%66100%
IsHeaderIncluded(...)0%220%
IsHeaderIncluded(...)0%16160%
IsEmpty()100%44100%

File(s)

/home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/Security/MessagePartSpecification.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.Collections.ObjectModel;
 7using System.Xml;
 8using CoreWCF.Channels;
 9
 10namespace CoreWCF.Security
 11{
 12    public class MessagePartSpecification
 13    {
 14        private List<XmlQualifiedName> _headerTypes;
 15        private bool _isBodyIncluded;
 16        private static MessagePartSpecification s_noParts;
 17
 18        public ICollection<XmlQualifiedName> HeaderTypes
 19        {
 20            get
 21            {
 11022                if (_headerTypes == null)
 23                {
 9324                    _headerTypes = new List<XmlQualifiedName>();
 25                }
 26
 11027                if (IsReadOnly)
 28                {
 1829                    return new ReadOnlyCollection<XmlQualifiedName>(_headerTypes);
 30                }
 31                else
 32                {
 9233                    return _headerTypes;
 34                }
 35            }
 36        }
 37
 38        internal bool HasHeaders
 39        {
 040            get { return _headerTypes != null && _headerTypes.Count > 0; }
 41        }
 42
 43        public bool IsBodyIncluded
 44        {
 45            get
 46            {
 626147                return _isBodyIncluded;
 48            }
 49            set
 50            {
 1651                if (IsReadOnly)
 52                {
 053                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.ObjectIsR
 54                }
 55
 1656                _isBodyIncluded = value;
 1657            }
 58        }
 59
 635660        public bool IsReadOnly { get; private set; }
 61
 62        public static MessagePartSpecification NoParts
 63        {
 64            get
 65            {
 066                if (s_noParts == null)
 67                {
 068                    MessagePartSpecification parts = new MessagePartSpecification();
 069                    parts.MakeReadOnly();
 070                    s_noParts = parts;
 71                }
 072                return s_noParts;
 73            }
 74        }
 75
 76        public void Clear()
 77        {
 078            if (IsReadOnly)
 79            {
 080                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.ObjectIsReadO
 81            }
 82
 083            if (_headerTypes != null)
 84            {
 085                _headerTypes.Clear();
 86            }
 87
 088            _isBodyIncluded = false;
 089        }
 90
 91        public void Union(MessagePartSpecification specification)
 92        {
 614293            if (IsReadOnly)
 94            {
 095                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.ObjectIsReadO
 96            }
 97
 614298            if (specification == null)
 99            {
 0100                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(specification));
 101            }
 102
 6142103            _isBodyIncluded |= specification.IsBodyIncluded;
 104
 6142105            List<XmlQualifiedName> headerTypes = specification._headerTypes;
 6142106            if (headerTypes != null && headerTypes.Count > 0)
 107            {
 72108                if (_headerTypes == null)
 109                {
 63110                    _headerTypes = new List<XmlQualifiedName>(headerTypes.Count);
 111                }
 112
 1152113                for (int i = 0; i < headerTypes.Count; i++)
 114                {
 504115                    XmlQualifiedName qname = headerTypes[i];
 504116                    _headerTypes.Add(qname);
 117                }
 118            }
 6142119        }
 120
 121        public void MakeReadOnly()
 122        {
 44123            if (IsReadOnly)
 124            {
 0125                return;
 126            }
 127
 44128            if (_headerTypes != null)
 129            {
 35130                List<XmlQualifiedName> noDuplicates = new List<XmlQualifiedName>(_headerTypes.Count);
 686131                for (int i = 0; i < _headerTypes.Count; i++)
 132                {
 308133                    XmlQualifiedName qname = _headerTypes[i];
 308134                    if (qname != null)
 135                    {
 308136                        bool include = true;
 2464137                        for (int j = 0; j < noDuplicates.Count; j++)
 138                        {
 987139                            XmlQualifiedName qname1 = noDuplicates[j];
 140
 987141                            if (qname.Name == qname1.Name && qname.Namespace == qname1.Namespace)
 142                            {
 63143                                include = false;
 63144                                break;
 145                            }
 146                        }
 147
 308148                        if (include)
 149                        {
 245150                            noDuplicates.Add(qname);
 151                        }
 152                    }
 153                }
 154
 35155                _headerTypes = noDuplicates;
 156            }
 157
 44158            IsReadOnly = true;
 44159        }
 160
 7083161        public MessagePartSpecification()
 162        {
 163            // empty
 7083164        }
 165
 540166        public MessagePartSpecification(bool isBodyIncluded)
 167        {
 540168            _isBodyIncluded = isBodyIncluded;
 540169        }
 170
 171        public MessagePartSpecification(params XmlQualifiedName[] headerTypes)
 34172            : this(false, headerTypes)
 173        {
 174            // empty
 34175        }
 176
 34177        public MessagePartSpecification(bool isBodyIncluded, params XmlQualifiedName[] headerTypes)
 178        {
 34179            _isBodyIncluded = isBodyIncluded;
 34180            if (headerTypes != null && headerTypes.Length > 0)
 181            {
 34182                _headerTypes = new List<XmlQualifiedName>(headerTypes.Length);
 448183                for (int i = 0; i < headerTypes.Length; i++)
 184                {
 190185                    _headerTypes.Add(headerTypes[i]);
 186                }
 187            }
 34188        }
 189
 190        internal bool IsHeaderIncluded(MessageHeader header)
 191        {
 0192            if (header == null)
 193            {
 0194                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(header));
 195            }
 196
 0197            return IsHeaderIncluded(header.Name, header.Namespace);
 198        }
 199
 200        internal bool IsHeaderIncluded(string name, string ns)
 201        {
 0202            if (name == null)
 203            {
 0204                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(name));
 205            }
 206
 0207            if (ns == null)
 208            {
 0209                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(ns));
 210            }
 211
 0212            if (_headerTypes != null)
 213            {
 0214                for (int i = 0; i < _headerTypes.Count; i++)
 215                {
 0216                    XmlQualifiedName qname = _headerTypes[i];
 217                    // Name is an optional attribute. If not present, compare with only the namespace.
 0218                    if (string.IsNullOrEmpty(qname.Name))
 219                    {
 0220                        if (qname.Namespace == ns)
 221                        {
 0222                            return true;
 223                        }
 224                    }
 225                    else
 226                    {
 0227                        if (qname.Name == name && qname.Namespace == ns)
 228                        {
 0229                            return true;
 230                        }
 231                    }
 232                }
 233            }
 234
 0235            return false;
 236        }
 237
 238        internal bool IsEmpty()
 239        {
 26240            if (_headerTypes != null && _headerTypes.Count > 0)
 241            {
 17242                return false;
 243            }
 244
 9245            return !IsBodyIncluded;
 246        }
 247    }
 248}