< Summary - CoreWCF Coverage — PR #1766

Information
Class: CoreWCF.Channels.ChannelRequirements
Assembly: CoreWCF.Primitives
File(s): /home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/Channels/ChannelRequirements.cs
Line coverage
30%
Covered lines: 58
Uncovered lines: 132
Coverable lines: 190
Total lines: 365
Line coverage: 30.5%
Branch coverage
20%
Covered branches: 24
Total branches: 118
Branch coverage: 20.3%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
ComputeContractRequirements(...)87.5%8894.73%
ComputeRequiredChannels(...)50%323242.85%
IsSessionful(...)0%880%
IsOneWay(...)0%660%
IsRequestReply(...)0%660%
IsDuplex(...)0%220%
CantCreateChannelException(...)0%220%
BindingContractMismatchException(...)0%54540%

File(s)

/home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/Channels/ChannelRequirements.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.Globalization;
 7using System.Text;
 8using CoreWCF.Description;
 9
 10namespace CoreWCF.Channels
 11{
 12    internal struct ChannelRequirements
 13    {
 14        public bool usesInput;
 15        public bool usesReply;
 16        public bool usesOutput;
 17        public bool usesRequest;
 18        public SessionMode sessionMode;
 19
 20        public static void ComputeContractRequirements(ContractDescription contractDescription,
 21            out ChannelRequirements requirements)
 22        {
 64123            requirements = new ChannelRequirements
 64124            {
 64125                usesInput = false,
 64126                usesReply = false,
 64127                usesOutput = false,
 64128                usesRequest = false,
 64129                sessionMode = contractDescription.SessionMode
 64130            };
 31
 733832            for (int i = 0; i < contractDescription.Operations.Count; i++)
 33            {
 302834                OperationDescription operation = contractDescription.Operations[i];
 302835                bool oneWay = (operation.IsOneWay);
 302836                if (!operation.IsServerInitiated())
 37                {
 302638                    if (oneWay)
 39                    {
 32440                        requirements.usesInput = true;
 41                    }
 42                    else
 43                    {
 270244                        requirements.usesReply = true;
 45                    }
 46                }
 47                else
 48                {
 249                    if (oneWay)
 50                    {
 051                        requirements.usesOutput = true;
 52                    }
 53                    else
 54                    {
 255                        requirements.usesRequest = true;
 56                    }
 57                }
 58            }
 64159        }
 60
 61        public static Type[] ComputeRequiredChannels(ref ChannelRequirements requirements)
 62        {
 64163            if (requirements.usesOutput || requirements.usesRequest)
 64            {
 265                switch (requirements.sessionMode)
 66                {
 67                    case SessionMode.Allowed:
 268                        return new[] {
 269                            typeof(IDuplexChannel),
 270                            typeof(IDuplexSessionChannel),
 271                        };
 72
 73                    case SessionMode.Required:
 074                        return new[] {
 075                            typeof(IDuplexSessionChannel),
 076                        };
 77
 78                    case SessionMode.NotAllowed:
 079                        return new[] {
 080                            typeof(IDuplexChannel),
 081                        };
 82                }
 83            }
 63984            else if (requirements.usesInput && requirements.usesReply)
 85            {
 1586                switch (requirements.sessionMode)
 87                {
 88                    case SessionMode.Allowed:
 1489                        return new[] {
 1490                            typeof(IRequestChannel),
 1491                            typeof(IRequestSessionChannel),
 1492                            typeof(IDuplexChannel),
 1493                            typeof(IDuplexSessionChannel),
 1494                        };
 95
 96                    case SessionMode.Required:
 197                        return new[] {
 198                            typeof(IRequestSessionChannel),
 199                            typeof(IDuplexSessionChannel),
 1100                        };
 101
 102                    case SessionMode.NotAllowed:
 0103                        return new[] {
 0104                            typeof(IRequestChannel),
 0105                            typeof(IDuplexChannel),
 0106                        };
 107                }
 108            }
 624109            else if (requirements.usesInput)
 110            {
 52111                switch (requirements.sessionMode)
 112                {
 113                    case SessionMode.Allowed:
 52114                        return new[] {
 52115                            typeof(IOutputChannel),
 52116                            typeof(IOutputSessionChannel),
 52117                            typeof(IRequestChannel),
 52118                            typeof(IRequestSessionChannel),
 52119                            typeof(IDuplexChannel),
 52120                            typeof(IDuplexSessionChannel),
 52121                        };
 122
 123                    case SessionMode.Required:
 0124                        return new[] {
 0125                            typeof(IOutputSessionChannel),
 0126                            typeof(IRequestSessionChannel),
 0127                            typeof(IDuplexSessionChannel),
 0128                        };
 129
 130                    case SessionMode.NotAllowed:
 0131                        return new[] {
 0132                            typeof(IOutputChannel),
 0133                            typeof(IRequestChannel),
 0134                            typeof(IDuplexChannel),
 0135                        };
 136                }
 137            }
 572138            else if (requirements.usesReply)
 139            {
 572140                switch (requirements.sessionMode)
 141                {
 142                    case SessionMode.Allowed:
 564143                        return new[] {
 564144                            typeof(IRequestChannel),
 564145                            typeof(IRequestSessionChannel),
 564146                            typeof(IDuplexChannel),
 564147                            typeof(IDuplexSessionChannel),
 564148                        };
 149
 150                    case SessionMode.Required:
 8151                        return new[] {
 8152                            typeof(IRequestSessionChannel),
 8153                            typeof(IDuplexSessionChannel),
 8154                        };
 155
 156                    case SessionMode.NotAllowed:
 0157                        return new[] {
 0158                            typeof(IRequestChannel),
 0159                            typeof(IDuplexChannel),
 0160                        };
 161                }
 162            }
 163            else
 164            {
 0165                switch (requirements.sessionMode)
 166                {
 167                    case SessionMode.Allowed:
 0168                        return new[] {
 0169                            typeof(IOutputSessionChannel),
 0170                            typeof(IOutputChannel),
 0171                            typeof(IRequestSessionChannel),
 0172                            typeof(IRequestChannel),
 0173                            typeof(IDuplexChannel),
 0174                            typeof(IDuplexSessionChannel),
 0175                        };
 176
 177                    case SessionMode.Required:
 0178                        return new[] {
 0179                            typeof(IOutputSessionChannel),
 0180                            typeof(IRequestSessionChannel),
 0181                            typeof(IDuplexSessionChannel),
 0182                        };
 183
 184                    case SessionMode.NotAllowed:
 0185                        return new[] {
 0186                            typeof(IOutputChannel),
 0187                            typeof(IRequestChannel),
 0188                            typeof(IDuplexChannel),
 0189                        };
 190                }
 191            }
 0192            return null;
 193        }
 194
 195        public static bool IsSessionful(Type channelType)
 196        {
 0197            return (channelType == typeof(IDuplexSessionChannel) ||
 0198                    channelType == typeof(IOutputSessionChannel) ||
 0199                    channelType == typeof(IInputSessionChannel) ||
 0200                    channelType == typeof(IReplySessionChannel) ||
 0201                    channelType == typeof(IRequestSessionChannel));
 202        }
 203
 204        public static bool IsOneWay(Type channelType)
 205        {
 0206            return (channelType == typeof(IOutputChannel) ||
 0207                    channelType == typeof(IInputChannel) ||
 0208                    channelType == typeof(IInputSessionChannel) ||
 0209                    channelType == typeof(IOutputSessionChannel));
 210        }
 211
 212        public static bool IsRequestReply(Type channelType)
 213        {
 0214            return (channelType == typeof(IRequestChannel) ||
 0215                    channelType == typeof(IReplyChannel) ||
 0216                    channelType == typeof(IReplySessionChannel) ||
 0217                    channelType == typeof(IRequestSessionChannel));
 218        }
 219
 220        public static bool IsDuplex(Type channelType)
 221        {
 0222            return (channelType == typeof(IDuplexChannel) ||
 0223                    channelType == typeof(IDuplexSessionChannel));
 224        }
 225
 226        public static Exception CantCreateChannelException(IEnumerable<Type> supportedChannels, IEnumerable<Type> requir
 227        {
 0228            string contractChannelTypesString = "";
 0229            string bindingChannelTypesString = "";
 230
 0231            Exception exception = BindingContractMismatchException(supportedChannels, requiredChannels, bindingName,
 0232                ref contractChannelTypesString, ref bindingChannelTypesString);
 233
 0234            if (exception == null)
 235            {
 236                // none of the obvious speculations about the failure holds, so we fall back to the generic error messag
 0237                exception = new InvalidOperationException(SR.Format(SR.CouldnTCreateChannelForType2, bindingName, contra
 238            }
 239
 0240            return exception;
 241        }
 242
 243        public static Exception BindingContractMismatchException(IEnumerable<Type> supportedChannels, IEnumerable<Type> 
 244            string bindingName, ref string contractChannelTypesString, ref string bindingChannelTypesString)
 245        {
 0246            StringBuilder contractChannelTypes = new StringBuilder();
 0247            bool contractRequiresOneWay = true;
 0248            bool contractRequiresRequestReply = true;
 0249            bool contractRequiresDuplex = true;
 0250            bool contractRequiresTwoWay = true;  // request-reply or duplex
 0251            bool contractRequiresSession = true;
 0252            bool contractRequiresDatagram = true;
 0253            foreach (Type channelType in requiredChannels)
 254            {
 0255                if (contractChannelTypes.Length > 0)
 256                {
 0257                    contractChannelTypes.Append(CultureInfo.CurrentCulture.TextInfo.ListSeparator);
 0258                    contractChannelTypes.Append(" ");
 259                }
 0260                string typeString = channelType.ToString();
 0261                contractChannelTypes.Append(typeString.Substring(typeString.LastIndexOf('.') + 1));
 262
 0263                if (!IsOneWay(channelType))
 264                {
 0265                    contractRequiresOneWay = false;
 266                }
 0267                if (!IsRequestReply(channelType))
 268                {
 0269                    contractRequiresRequestReply = false;
 270                }
 0271                if (!IsDuplex(channelType))
 272                {
 0273                    contractRequiresDuplex = false;
 274                }
 0275                if (!(IsRequestReply(channelType) || IsDuplex(channelType)))
 276                {
 0277                    contractRequiresTwoWay = false;
 278                }
 0279                if (!IsSessionful(channelType))
 280                {
 0281                    contractRequiresSession = false;
 282                }
 283                else
 284                {
 0285                    contractRequiresDatagram = false;
 286                }
 287            }
 288
 0289            StringBuilder bindingChannelTypes = new StringBuilder();
 0290            bool bindingSupportsOneWay = false;
 0291            bool bindingSupportsRequestReply = false;
 0292            bool bindingSupportsDuplex = false;
 0293            bool bindingSupportsSession = false;
 0294            bool bindingSupportsDatagram = false;
 0295            bool bindingSupportsAtLeastOneChannelType = false;
 0296            foreach (Type channelType in supportedChannels)
 297            {
 0298                bindingSupportsAtLeastOneChannelType = true;
 0299                if (bindingChannelTypes.Length > 0)
 300                {
 0301                    bindingChannelTypes.Append(CultureInfo.CurrentCulture.TextInfo.ListSeparator);
 0302                    bindingChannelTypes.Append(" ");
 303                }
 0304                string typeString = channelType.ToString();
 0305                bindingChannelTypes.Append(typeString.Substring(typeString.LastIndexOf('.') + 1));
 306
 0307                if (IsOneWay(channelType))
 308                {
 0309                    bindingSupportsOneWay = true;
 310                }
 0311                if (IsRequestReply(channelType))
 312                {
 0313                    bindingSupportsRequestReply = true;
 314                }
 0315                if (IsDuplex(channelType))
 316                {
 0317                    bindingSupportsDuplex = true;
 318                }
 0319                if (IsSessionful(channelType))
 320                {
 0321                    bindingSupportsSession = true;
 322                }
 323                else
 324                {
 0325                    bindingSupportsDatagram = true;
 326                }
 327            }
 0328            bool bindingSupportsTwoWay = bindingSupportsRequestReply || bindingSupportsDuplex;
 329
 0330            if (!bindingSupportsAtLeastOneChannelType)
 331            {
 0332                return new InvalidOperationException(SR.Format(SR.BindingDoesnTSupportAnyChannelTypes1, bindingName));
 333            }
 0334            if (contractRequiresSession && !bindingSupportsSession)
 335            {
 0336                return new InvalidOperationException(SR.Format(SR.BindingDoesnTSupportSessionButContractRequires1, bindi
 337            }
 0338            if (contractRequiresDatagram && !bindingSupportsDatagram)
 339            {
 0340                return new InvalidOperationException(SR.Format(SR.BindingDoesntSupportDatagramButContractRequires, bindi
 341            }
 0342            if (contractRequiresDuplex && !bindingSupportsDuplex)
 343            {
 0344                return new InvalidOperationException(SR.Format(SR.BindingDoesnTSupportDuplexButContractRequires1, bindin
 345            }
 0346            if (contractRequiresRequestReply && !bindingSupportsRequestReply)
 347            {
 0348                return new InvalidOperationException(SR.Format(SR.BindingDoesnTSupportRequestReplyButContract1, bindingN
 349            }
 0350            if (contractRequiresOneWay && !bindingSupportsOneWay)
 351            {
 0352                return new InvalidOperationException(SR.Format(SR.BindingDoesnTSupportOneWayButContractRequires1, bindin
 353            }
 0354            if (contractRequiresTwoWay && !bindingSupportsTwoWay)
 355            {
 0356                return new InvalidOperationException(SR.Format(SR.BindingDoesnTSupportTwoWayButContractRequires1, bindin
 357            }
 358
 0359            contractChannelTypesString = contractChannelTypes.ToString();
 0360            bindingChannelTypesString = bindingChannelTypes.ToString();
 361
 0362            return null;
 363        }
 364    }
 365}