< Summary - CoreWCF Coverage — PR #1766

Information
Class: CoreWCF.Configuration.ConfigurationChannelBindingUtility
Assembly: CoreWCF.ConfigurationManager
File(s): /home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.ConfigurationManager/src/CoreWCF/Configuration/ConfigurationChannelBindingUtility.cs
Line coverage
3%
Covered lines: 3
Uncovered lines: 82
Coverable lines: 85
Total lines: 244
Line coverage: 3.5%
Branch coverage
4%
Covered branches: 2
Total branches: 48
Branch coverage: 4.1%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
.cctor()100%110%
IsDefaultPolicy(...)100%110%
CopyFrom(...)0%220%
InitializeFrom(...)0%660%
BuildPolicy(...)100%22100%
GetToken(...)100%110%
GetToken(...)0%220%
DuplicateToken(...)0%220%
TryAddToMessage(...)0%220%
AreEqual(...)0%14140%
IsSubset(...)0%880%
Dispose(...)0%220%
.ctor()100%110%
CreateCopy(...)0%660%
Initialize(...)0%220%
AllocateMemory(...)100%110%
ReleaseHandle()100%110%

File(s)

/home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.ConfigurationManager/src/CoreWCF/Configuration/ConfigurationChannelBindingUtility.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;
 6using System.Net.Security;
 7using System.Runtime.InteropServices;
 8using System.Security.Authentication.ExtendedProtection;
 9using CoreWCF.Channels;
 10using CoreWCF.Runtime;
 11
 12namespace CoreWCF.Configuration
 13{
 14    internal static class ConfigurationChannelBindingUtility
 15    {
 016        private static readonly ExtendedProtectionPolicy s_disabledPolicy = new ExtendedProtectionPolicy(PolicyEnforceme
 017        private static readonly ExtendedProtectionPolicy s_defaultPolicy = s_disabledPolicy;
 18
 19        public static bool IsDefaultPolicy(ExtendedProtectionPolicy policy)
 20        {
 021            return ReferenceEquals(policy, s_defaultPolicy);
 22        }
 23
 24        public static void CopyFrom(ExtendedProtectionPolicyElement source, ExtendedProtectionPolicyElement destination)
 25        {
 026            destination.PolicyEnforcement = source.PolicyEnforcement;
 027            destination.ProtectionScenario = source.ProtectionScenario;
 028            destination.CustomServiceNames.Clear();
 029            foreach (ServiceNameElement sourceEntry in source.CustomServiceNames)
 30            {
 031                ServiceNameElement entry = new ServiceNameElement();
 032                entry.Name = sourceEntry.Name;
 033                destination.CustomServiceNames.Add(entry);
 34            }
 035        }
 36
 37        public static void InitializeFrom(ExtendedProtectionPolicy source, ExtendedProtectionPolicyElement destination)
 38        {
 039            if (!IsDefaultPolicy(source))
 40            {
 041                destination.PolicyEnforcement = source.PolicyEnforcement;
 042                destination.ProtectionScenario = source.ProtectionScenario;
 043                destination.CustomServiceNames.Clear();
 44
 045                if (source.CustomServiceNames != null)
 46                {
 047                    foreach (string name in source.CustomServiceNames)
 48                    {
 049                        ServiceNameElement entry = new ServiceNameElement();
 050                        entry.Name = name;
 051                        destination.CustomServiceNames.Add(entry);
 52                    }
 53                }
 54            }
 055        }
 56
 57        public static ExtendedProtectionPolicy BuildPolicy(ExtendedProtectionPolicyElement configurationPolicy)
 58        {
 59            //using this pattern allows us to have a different default policy
 60            //than the NCL team chooses.
 661            if (configurationPolicy.ElementInformation.IsPresent)
 62            {
 163                return configurationPolicy.BuildPolicy();
 64            }
 65            else
 66            {
 567                return ChannelBindingUtility.DefaultPolicy;
 68            }
 69        }
 70
 71        public static ChannelBinding GetToken(SslStream stream)
 72        {
 073            return GetToken(stream.TransportContext);
 74        }
 75
 76        public static ChannelBinding GetToken(TransportContext context)
 77        {
 078            ChannelBinding token = null;
 079            if (context != null)
 80            {
 081                token = context.GetChannelBinding(ChannelBindingKind.Endpoint);
 82            }
 083            return token;
 84        }
 85
 86        public static ChannelBinding DuplicateToken(ChannelBinding source)
 87        {
 088            if (source == null)
 89            {
 090                return null;
 91            }
 92
 093            return DuplicatedChannelBinding.CreateCopy(source);
 94        }
 95
 96        public static void TryAddToMessage(ChannelBinding channelBindingToken, Message message, bool messagePropertyOwns
 97        {
 098            if (channelBindingToken != null)
 99            {
 0100                ChannelBindingMessageProperty property = new ChannelBindingMessageProperty(channelBindingToken, messageP
 0101                property.AddTo(message);
 0102                ((IDisposable)property).Dispose(); //message.Properties.Add() creates a copy...
 103            }
 0104        }
 105
 106        //does not validate the ExtendedProtectionPolicy.CustomServiceNames collections on the policies
 107        public static bool AreEqual(ExtendedProtectionPolicy policy1, ExtendedProtectionPolicy policy2)
 108        {
 0109            if (policy1 == null)
 110            {
 0111                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(policy1));
 112            }
 113
 0114            if (policy2 == null)
 115            {
 0116                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(policy2));
 117            }
 118
 0119            if (policy1.PolicyEnforcement == PolicyEnforcement.Never && policy2.PolicyEnforcement == PolicyEnforcement.N
 120            {
 0121                return true;
 122            }
 123
 0124            if (policy1.PolicyEnforcement != policy2.PolicyEnforcement)
 125            {
 0126                return false;
 127            }
 128
 0129            if (policy1.ProtectionScenario != policy2.ProtectionScenario)
 130            {
 0131                return false;
 132            }
 133
 0134            if (policy1.CustomChannelBinding != policy2.CustomChannelBinding)
 135            {
 0136                return false;
 137            }
 138
 0139            return true;
 140        }
 141
 142        public static bool IsSubset(ServiceNameCollection primaryList, ServiceNameCollection subset)
 143        {
 0144            bool result = false;
 0145            if (subset == null || subset.Count == 0)
 146            {
 0147                result = true;
 148            }
 0149            else if (primaryList == null || primaryList.Count < subset.Count)
 150            {
 0151                result = false;
 152            }
 153            else
 154            {
 0155                ServiceNameCollection merged = primaryList.Merge(subset);
 156
 157                //The merge routine only adds an entry if it is unique.
 0158                result = (merged.Count == primaryList.Count);
 159            }
 160
 0161            return result;
 162        }
 163
 164        public static void Dispose(ref ChannelBinding channelBinding)
 165        {
 166            // Explicitly cast to IDisposable to avoid the SecurityException.
 0167            IDisposable disposable = (IDisposable)channelBinding;
 0168            channelBinding = null;
 0169            disposable?.Dispose();
 170
 0171        }
 172
 173        private class DuplicatedChannelBinding : ChannelBinding
 174        {
 175            private int _size;
 176
 0177            private DuplicatedChannelBinding()
 178            {
 179
 0180            }
 181
 182            public override int Size
 183            {
 184
 0185                get { return _size; }
 186            }
 187            internal static ChannelBinding CreateCopy(ChannelBinding source)
 188            {
 189                Fx.Assert(source != null, "source ChannelBinding should have been checked for null previously");
 190
 0191                if (source.IsInvalid || source.IsClosed)
 192                {
 0193                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ObjectDisposedException(source.GetType
 194                }
 195
 0196                if (source.Size <= 0)
 197                {
 0198                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("source.Si
 0199                        SR.Format(SRCommon.ValueMustBePositive)));
 200                }
 201
 202                //Instantiate the SafeHandle before trying to allocate the native memory
 0203                DuplicatedChannelBinding duplicate = new DuplicatedChannelBinding();
 204
 205                //allocate the native memory and make a deep copy of the original.
 0206                duplicate.Initialize(source);
 207
 0208                return duplicate;
 209            }
 210
 211            private unsafe void Initialize(ChannelBinding source)
 212            {
 213                //allocates the memory pointed to by this.handle
 214                //and sets this.size after allocation succeeds.
 0215                AllocateMemory(source.Size);
 216
 0217                byte* sourceBuffer = (byte*)source.DangerousGetHandle().ToPointer();
 0218                byte* destinationBuffer = (byte*)handle.ToPointer();
 219
 0220                for (int i = 0; i < source.Size; i++)
 221                {
 0222                    destinationBuffer[i] = sourceBuffer[i];
 223                }
 224
 0225                _size = source.Size;
 0226            }
 227
 228            private void AllocateMemory(int bytesToAllocate)
 229            {
 230                Fx.Assert(bytesToAllocate > 0, "bytesToAllocate must be positive");
 231
 0232                base.SetHandle(Marshal.AllocHGlobal(bytesToAllocate));
 233
 0234            }
 235
 236            protected override bool ReleaseHandle()
 237            {
 0238                Marshal.FreeHGlobal(handle);
 0239                base.SetHandle(IntPtr.Zero);
 0240                return true;
 241            }
 242        }
 243    }
 244}