< Summary - CoreWCF Coverage — PR #1766

Information
Class: CoreWCF.Security.ChannelProtectionRequirements
Assembly: CoreWCF.Primitives
File(s): /home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/Security/ChannelProtectionRequirements.cs
Line coverage
70%
Covered lines: 107
Uncovered lines: 45
Coverable lines: 152
Total lines: 358
Line coverage: 70.3%
Branch coverage
69%
Covered branches: 60
Total branches: 86
Branch coverage: 69.7%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
.ctor()100%11100%
.ctor(...)50%2287.5%
.ctor(...)0%220%
Add(...)100%11100%
Add(...)91.66%121293.75%
AddActionParts(...)100%44100%
MakeReadOnly()0%220%
CreateFromContract(...)100%11100%
UnionMessagePartSpecifications(...)70%101066.66%
CreateFromContractAndUnionResponseProtectionRequirements(...)100%11100%
CreateFromContract(...)76.31%383885.18%
AddHeaderProtectionRequirements(...)50%4442.85%
AddFaultProtectionRequirements(...)25%121222.22%

File(s)

/home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/Security/ChannelProtectionRequirements.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.Net.Security;
 6using System.Xml;
 7using CoreWCF.Channels;
 8using CoreWCF.Description;
 9using CoreWCF.Runtime;
 10
 11namespace CoreWCF.Security
 12{
 13    public class ChannelProtectionRequirements
 14    {
 24615        public ChannelProtectionRequirements()
 16        {
 24617            IncomingSignatureParts = new ScopedMessagePartSpecification();
 24618            IncomingEncryptionParts = new ScopedMessagePartSpecification();
 24619            OutgoingSignatureParts = new ScopedMessagePartSpecification();
 24620            OutgoingEncryptionParts = new ScopedMessagePartSpecification();
 24621        }
 22
 023        public bool IsReadOnly { get; private set; }
 24
 53925        public ChannelProtectionRequirements(ChannelProtectionRequirements other)
 26        {
 53927            if (other == null)
 28            {
 029                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(other));
 30            }
 31
 53932            IncomingSignatureParts = new ScopedMessagePartSpecification(other.IncomingSignatureParts);
 53933            IncomingEncryptionParts = new ScopedMessagePartSpecification(other.IncomingEncryptionParts);
 53934            OutgoingSignatureParts = new ScopedMessagePartSpecification(other.OutgoingSignatureParts);
 53935            OutgoingEncryptionParts = new ScopedMessagePartSpecification(other.OutgoingEncryptionParts);
 53936        }
 37
 038        internal ChannelProtectionRequirements(ChannelProtectionRequirements other, ProtectionLevel newBodyProtectionLev
 39        {
 040            if (other == null)
 41            {
 042                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(other));
 43            }
 44
 045            IncomingSignatureParts = new ScopedMessagePartSpecification(other.IncomingSignatureParts, newBodyProtectionL
 046            IncomingEncryptionParts = new ScopedMessagePartSpecification(other.IncomingEncryptionParts, newBodyProtectio
 047            OutgoingSignatureParts = new ScopedMessagePartSpecification(other.OutgoingSignatureParts, newBodyProtectionL
 048            OutgoingEncryptionParts = new ScopedMessagePartSpecification(other.OutgoingEncryptionParts, newBodyProtectio
 049        }
 50
 130751        public ScopedMessagePartSpecification IncomingSignatureParts { get; }
 52
 129853        public ScopedMessagePartSpecification IncomingEncryptionParts { get; }
 54
 130755        public ScopedMessagePartSpecification OutgoingSignatureParts { get; }
 56
 129857        public ScopedMessagePartSpecification OutgoingEncryptionParts { get; }
 58
 59        public void Add(ChannelProtectionRequirements protectionRequirements)
 60        {
 7161            Add(protectionRequirements, false);
 7162        }
 63
 64        public void Add(ChannelProtectionRequirements protectionRequirements, bool channelScopeOnly)
 65        {
 7166            if (protectionRequirements == null)
 67            {
 068                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(protectionRequirements));
 69            }
 70
 7171            if (protectionRequirements.IncomingSignatureParts != null)
 72            {
 7173                IncomingSignatureParts.AddParts(protectionRequirements.IncomingSignatureParts.ChannelParts);
 74            }
 75
 7176            if (protectionRequirements.IncomingEncryptionParts != null)
 77            {
 7178                IncomingEncryptionParts.AddParts(protectionRequirements.IncomingEncryptionParts.ChannelParts);
 79            }
 80
 7181            if (protectionRequirements.OutgoingSignatureParts != null)
 82            {
 7183                OutgoingSignatureParts.AddParts(protectionRequirements.OutgoingSignatureParts.ChannelParts);
 84            }
 85
 7186            if (protectionRequirements.OutgoingEncryptionParts != null)
 87            {
 7188                OutgoingEncryptionParts.AddParts(protectionRequirements.OutgoingEncryptionParts.ChannelParts);
 89            }
 90
 7191            if (!channelScopeOnly)
 92            {
 7193                AddActionParts(IncomingSignatureParts, protectionRequirements.IncomingSignatureParts);
 7194                AddActionParts(IncomingEncryptionParts, protectionRequirements.IncomingEncryptionParts);
 7195                AddActionParts(OutgoingSignatureParts, protectionRequirements.OutgoingSignatureParts);
 7196                AddActionParts(OutgoingEncryptionParts, protectionRequirements.OutgoingEncryptionParts);
 97            }
 7198        }
 99
 100        private static void AddActionParts(ScopedMessagePartSpecification to, ScopedMessagePartSpecification from)
 101        {
 2456102            foreach (string action in from.Actions)
 103            {
 944104                if (from.TryGetParts(action, true, out MessagePartSpecification p))
 105                {
 944106                    to.AddParts(p, action);
 107                }
 108            }
 284109        }
 110
 111        public void MakeReadOnly()
 112        {
 0113            if (!IsReadOnly)
 114            {
 0115                IncomingSignatureParts.MakeReadOnly();
 0116                IncomingEncryptionParts.MakeReadOnly();
 0117                OutgoingSignatureParts.MakeReadOnly();
 0118                OutgoingEncryptionParts.MakeReadOnly();
 0119                IsReadOnly = true;
 120            }
 0121        }
 122
 123        internal static ChannelProtectionRequirements CreateFromContract(ContractDescription contract, ISecurityCapabili
 124        {
 38125            return CreateFromContract(contract, bindingElement.SupportedRequestProtectionLevel, bindingElement.Supported
 126        }
 127
 128        private static MessagePartSpecification UnionMessagePartSpecifications(ScopedMessagePartSpecification actionPart
 129        {
 12130            var result = new MessagePartSpecification(false);
 168131            foreach (string action in actionParts.Actions)
 132            {
 72133                if (actionParts.TryGetParts(action, out MessagePartSpecification parts))
 134                {
 72135                    if (parts.IsBodyIncluded)
 136                    {
 0137                        result.IsBodyIncluded = true;
 138                    }
 144139                    foreach (XmlQualifiedName headerType in parts.HeaderTypes)
 140                    {
 0141                        if (!result.IsHeaderIncluded(headerType.Name, headerType.Namespace))
 142                        {
 0143                            result.HeaderTypes.Add(headerType);
 144                        }
 145                    }
 146                }
 147            }
 12148            return result;
 149        }
 150
 151        internal static ChannelProtectionRequirements CreateFromContractAndUnionResponseProtectionRequirements(ContractD
 152        {
 6153            ChannelProtectionRequirements contractRequirements = CreateFromContract(contract, bindingElement.SupportedRe
 6154            var result = new ChannelProtectionRequirements();
 155
 6156            result.OutgoingEncryptionParts.AddParts(UnionMessagePartSpecifications(contractRequirements.OutgoingEncrypti
 6157            result.OutgoingSignatureParts.AddParts(UnionMessagePartSpecifications(contractRequirements.OutgoingSignature
 6158            contractRequirements.IncomingEncryptionParts.CopyTo(result.IncomingEncryptionParts);
 6159            contractRequirements.IncomingSignatureParts.CopyTo(result.IncomingSignatureParts);
 6160            return result;
 161        }
 162
 163        internal static ChannelProtectionRequirements CreateFromContract(ContractDescription contract, ProtectionLevel d
 164        {
 44165            if (contract == null)
 166            {
 0167                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(contract));
 168            }
 169
 44170            var requirements = new ChannelProtectionRequirements();
 171
 44172            ProtectionLevel contractScopeDefaultRequestProtectionLevel = ProtectionLevel.None;
 44173            ProtectionLevel contractScopeDefaultResponseProtectionLevel = ProtectionLevel.None;
 44174            if (contract.HasProtectionLevel) // Currently always false
 175            {
 0176                throw new PlatformNotSupportedException(nameof(ContractDescription.HasProtectionLevel));
 177                //contractScopeDefaultRequestProtectionLevel = contract.ProtectionLevel;
 178                //contractScopeDefaultResponseProtectionLevel = contract.ProtectionLevel;
 179            }
 180            else
 181            {
 44182                contractScopeDefaultRequestProtectionLevel = defaultRequestProtectionLevel;
 44183                contractScopeDefaultResponseProtectionLevel = defaultResponseProtectionLevel;
 184            }
 185
 590186            foreach (OperationDescription operation in contract.Operations)
 187            {
 251188                ProtectionLevel operationScopeDefaultRequestProtectionLevel = ProtectionLevel.None;
 251189                ProtectionLevel operationScopeDefaultResponseProtectionLevel = ProtectionLevel.None;
 251190                if (operation.HasProtectionLevel) // Currently always false
 191                {
 0192                    throw new PlatformNotSupportedException(nameof(OperationDescription.HasProtectionLevel));
 193                    //operationScopeDefaultRequestProtectionLevel = operation.ProtectionLevel;
 194                    //operationScopeDefaultResponseProtectionLevel = operation.ProtectionLevel;
 195                }
 196                else
 197                {
 251198                    operationScopeDefaultRequestProtectionLevel = contractScopeDefaultRequestProtectionLevel;
 251199                    operationScopeDefaultResponseProtectionLevel = contractScopeDefaultResponseProtectionLevel;
 200                }
 1506201                foreach (MessageDescription message in operation.Messages)
 202                {
 502203                    ProtectionLevel messageScopeDefaultProtectionLevel = ProtectionLevel.None;
 502204                    if (message.HasProtectionLevel) // Currently always false
 205                    {
 0206                        throw new PlatformNotSupportedException(nameof(MessageDescription.HasProtectionLevel));
 207                        //messageScopeDefaultProtectionLevel = message.ProtectionLevel;
 208                    }
 502209                    else if (message.Direction == MessageDirection.Input)
 210                    {
 251211                        messageScopeDefaultProtectionLevel = operationScopeDefaultRequestProtectionLevel;
 212                    }
 213                    else
 214                    {
 251215                        messageScopeDefaultProtectionLevel = operationScopeDefaultResponseProtectionLevel;
 216                    }
 217
 502218                    var signedParts = new MessagePartSpecification();
 502219                    var encryptedParts = new MessagePartSpecification();
 220
 221                    // determine header protection requirements for message
 1016222                    foreach (MessageHeaderDescription header in message.Headers)
 223                    {
 6224                        AddHeaderProtectionRequirements(header, signedParts, encryptedParts, messageScopeDefaultProtecti
 225                    }
 226
 227                    // determine body protection requirements for message
 228                    ProtectionLevel bodyProtectionLevel;
 502229                    if (message.Body.Parts.Count > 0)
 230                    {
 231                        // initialize the body protection level to none. all the body parts will be
 232                        // unioned to get the effective body protection level
 242233                        bodyProtectionLevel = ProtectionLevel.None;
 234                    }
 260235                    else if (message.Body.ReturnValue != null)
 236                    {
 248237                        if (!(message.Body.ReturnValue.GetType().Equals(typeof(MessagePartDescription))))
 238                        {
 239                            Fx.Assert("Only body return values are supported currently");
 0240                            throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.O
 241                        }
 248242                        bodyProtectionLevel = messageScopeDefaultProtectionLevel; // MessagePartDescription.HasProtectio
 243                        //MessagePartDescription desc = message.Body.ReturnValue;
 244                        //bodyProtectionLevel = desc.HasProtectionLevel ? desc.ProtectionLevel : messageScopeDefaultProt
 245                    }
 246                    else
 247                    {
 12248                        bodyProtectionLevel = messageScopeDefaultProtectionLevel;
 249                    }
 250
 251                    // determine body protection requirements for message
 502252                    if (message.Body.Parts.Count > 0)
 253                    {
 967254                        foreach (MessagePartDescription body in message.Body.Parts)
 255                        {
 242256                            ProtectionLevel partProtectionLevel = messageScopeDefaultProtectionLevel; // MessagePartDesc
 257                            //ProtectionLevel partProtectionLevel = body.HasProtectionLevel ? body.ProtectionLevel : mes
 242258                            bodyProtectionLevel = ProtectionLevelHelper.Max(bodyProtectionLevel, partProtectionLevel);
 242259                            if (bodyProtectionLevel == ProtectionLevel.EncryptAndSign)
 260                            {
 1261                                break;
 262                            }
 263                        }
 264                    }
 502265                    if (bodyProtectionLevel != ProtectionLevel.None)
 266                    {
 2267                        signedParts.IsBodyIncluded = true;
 2268                        if (bodyProtectionLevel == ProtectionLevel.EncryptAndSign)
 269                        {
 2270                            encryptedParts.IsBodyIncluded = true;
 271                        }
 272                    }
 273
 274                    // add requirements for message
 502275                    if (message.Direction == MessageDirection.Input)
 276                    {
 251277                        requirements.IncomingSignatureParts.AddParts(signedParts, message.Action);
 251278                        requirements.IncomingEncryptionParts.AddParts(encryptedParts, message.Action);
 279                    }
 280                    else
 281                    {
 251282                        requirements.OutgoingSignatureParts.AddParts(signedParts, message.Action);
 251283                        requirements.OutgoingEncryptionParts.AddParts(encryptedParts, message.Action);
 284                    }
 285                }
 251286                if (operation.Faults != null)
 287                {
 251288                    if (operation.IsServerInitiated())
 289                    {
 0290                        AddFaultProtectionRequirements(operation.Faults, requirements, operationScopeDefaultRequestProte
 291                    }
 292                    else
 293                    {
 251294                        AddFaultProtectionRequirements(operation.Faults, requirements, operationScopeDefaultResponseProt
 295                    }
 296                }
 297            }
 298
 44299            return requirements;
 300        }
 301
 302        private static void AddHeaderProtectionRequirements(MessageHeaderDescription header, MessagePartSpecification si
 303            MessagePartSpecification encryptedParts, ProtectionLevel defaultProtectionLevel)
 304        {
 6305            ProtectionLevel p = defaultProtectionLevel; //header.HasProtectionLevel currently is always false;
 306            //ProtectionLevel p = header.HasProtectionLevel ? header.ProtectionLevel : defaultProtectionLevel;
 307
 6308            if (p != ProtectionLevel.None)
 309            {
 0310                var headerName = new XmlQualifiedName(header.Name, header.Namespace);
 0311                signedParts.HeaderTypes.Add(headerName);
 0312                if (p == ProtectionLevel.EncryptAndSign)
 313                {
 0314                    encryptedParts.HeaderTypes.Add(headerName);
 315                }
 316            }
 6317        }
 318
 319        private static void AddFaultProtectionRequirements(FaultDescriptionCollection faults, ChannelProtectionRequireme
 320        {
 251321            if (faults == null)
 322            {
 0323                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(faults));
 324            }
 325
 251326            if (requirements == null)
 327            {
 0328                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(requirements));
 329            }
 330
 502331            foreach (FaultDescription fault in faults)
 332            {
 0333                var signedParts = new MessagePartSpecification();
 0334                var encryptedParts = new MessagePartSpecification();
 0335                ProtectionLevel p = defaultProtectionLevel; // FaultDescription.HasProtectionLevel currently is always f
 336                //ProtectionLevel p = fault.HasProtectionLevel ? fault.ProtectionLevel : defaultProtectionLevel;
 0337                if (p != ProtectionLevel.None)
 338                {
 0339                    signedParts.IsBodyIncluded = true;
 0340                    if (p == ProtectionLevel.EncryptAndSign)
 341                    {
 0342                        encryptedParts.IsBodyIncluded = true;
 343                    }
 344                }
 0345                if (addToIncoming)
 346                {
 0347                    requirements.IncomingSignatureParts.AddParts(signedParts, fault.Action);
 0348                    requirements.IncomingEncryptionParts.AddParts(encryptedParts, fault.Action);
 349                }
 350                else
 351                {
 0352                    requirements.OutgoingSignatureParts.AddParts(signedParts, fault.Action);
 0353                    requirements.OutgoingEncryptionParts.AddParts(encryptedParts, fault.Action);
 354                }
 355            }
 251356        }
 357    }
 358}