< Summary - CoreWCF Coverage — PR #1766

Information
Class: CoreWCF.Channels.Framing.UnixDomainSocketExceptionConvertingDuplexPipe
Assembly: CoreWCF.UnixDomainSocket
File(s): /home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.UnixDomainSocket/src/CoreWCF/Channels/Framing/UnixDomainSocketExceptionConvertingDuplexPipe.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 46
Coverable lines: 46
Total lines: 196
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 14
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
.ctor(...)100%110%
.ctor(...)100%110%
AdvanceTo(...)100%110%
AdvanceTo(...)100%110%
CancelPendingRead()100%110%
Complete(...)100%110%
ReadAsync()100%110%
TryRead(...)100%110%
ConvertReceiveException(...)100%110%
.ctor(...)100%110%
Advance(...)100%110%
CancelPendingFlush()100%110%
Complete(...)100%110%
FlushAsync()100%110%
GetMemory(...)100%110%
GetSpan(...)100%110%
ConvertSendException(...)100%110%
ConvertTransferException(...)0%14140%

File(s)

/home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.UnixDomainSocket/src/CoreWCF/Channels/Framing/UnixDomainSocketExceptionConvertingDuplexPipe.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.Diagnostics;
 6using System.IO.Pipelines;
 7using System.Net.Sockets;
 8using System.Threading;
 9using System.Threading.Tasks;
 10
 11namespace CoreWCF.Channels.Framing
 12{
 13    internal class UnixDomainSocketExceptionConvertingDuplexPipe : IDuplexPipe
 14    {
 015        public UnixDomainSocketExceptionConvertingDuplexPipe(IDuplexPipe innerDuplexPipe)
 16        {
 017            Input = new UnixDomainSocketExceptionConvertingPipeReader(innerDuplexPipe.Input);
 018            Output = new UnixDomainSocketExceptionConvertingPipeWriter(innerDuplexPipe.Output);
 019        }
 20
 021        public PipeReader Input { get; }
 022        public PipeWriter Output { get; }
 23
 24        private class UnixDomainSocketExceptionConvertingPipeReader : PipeReader
 25        {
 26            private PipeReader _input;
 27
 028            public UnixDomainSocketExceptionConvertingPipeReader(PipeReader input) => _input = input;
 29
 030            public override void AdvanceTo(SequencePosition consumed) => _input.AdvanceTo(consumed);
 031            public override void AdvanceTo(SequencePosition consumed, SequencePosition examined) => _input.AdvanceTo(con
 032            public override void CancelPendingRead() => _input.CancelPendingRead();
 033            public override void Complete(Exception exception = null) => _input.Complete(exception);
 34            public override async ValueTask<ReadResult> ReadAsync(CancellationToken cancellationToken = default)
 35            {
 36                try
 37                {
 038                    return await _input.ReadAsync(cancellationToken);
 39                }
 040                catch(SocketException socketException)
 41                {
 042                    throw DiagnosticUtility.ExceptionUtility.ThrowHelper(
 043                        ConvertReceiveException(socketException), TraceEventType.Error);
 44                }
 045            }
 46
 047            public override bool TryRead(out ReadResult result) => _input.TryRead(out result);
 48
 49            private Exception ConvertReceiveException(SocketException socketException)
 50            {
 051                return ConvertTransferException(socketException, socketException, aborted: false);
 52            }
 53        }
 54
 55        private class UnixDomainSocketExceptionConvertingPipeWriter : PipeWriter
 56        {
 57            private PipeWriter _output;
 58
 059            public UnixDomainSocketExceptionConvertingPipeWriter(PipeWriter output) => _output = output;
 60
 061            public override void Advance(int bytes) => _output.Advance(bytes);
 062            public override void CancelPendingFlush() => _output.CancelPendingFlush();
 063            public override void Complete(Exception exception = null) => _output.Complete(exception);
 64            public override async ValueTask<FlushResult> FlushAsync(CancellationToken cancellationToken = default)
 65            {
 66                try
 67                {
 068                    return await _output.FlushAsync(cancellationToken);
 69                }
 070                catch (SocketException socketException)
 71                {
 072                    throw DiagnosticUtility.ExceptionUtility.ThrowHelper(
 073                        ConvertSendException(socketException), TraceEventType.Error);
 74                }
 075            }
 76
 077            public override Memory<byte> GetMemory(int sizeHint = 0) => _output.GetMemory(sizeHint);
 078            public override Span<byte> GetSpan(int sizeHint = 0) => _output.GetSpan(sizeHint);
 79
 80            private Exception ConvertSendException(SocketException socketException)
 81            {
 082                return ConvertTransferException(socketException, socketException, aborted: false);
 83            }
 84        }
 85
 86        private static Exception ConvertTransferException(SocketException socketException, Exception originalException, 
 87        {
 088            if (socketException.ErrorCode == UnsafeNativeMethods.ERROR_INVALID_HANDLE)
 89            {
 090                return new CommunicationObjectAbortedException(socketException.Message, socketException);
 91            }
 92
 093            if (socketException.ErrorCode == UnsafeNativeMethods.WSAENETRESET ||
 094                socketException.ErrorCode == UnsafeNativeMethods.WSAECONNABORTED ||
 095                socketException.ErrorCode == UnsafeNativeMethods.WSAECONNRESET)
 96            {
 097                if (aborted)
 98                {
 099                    return new CommunicationObjectAbortedException(SR.TcpLocalConnectionAborted, originalException);
 100                }
 101                else
 102                {
 0103                    CommunicationException communicationException = new CommunicationException(SR.Format(SR.TcpConnectio
 104                    //if (TD.TcpConnectionResetErrorIsEnabled())
 105                    //{
 106                    //    if (socketConnection != null)
 107                    //    {
 108                    //        int socketId = (socketConnection.socket != null) ? socketConnection.socket.GetHashCode() :
 109                    //        TD.TcpConnectionResetError(socketId, socketConnection.RemoteEndpointAddress);
 110                    //    }
 111                    //}
 112                    //if (DiagnosticUtility.ShouldTrace(exceptionEventType))
 113                    //{
 114                    //    TraceUtility.TraceEvent(exceptionEventType, TraceCode.TcpConnectionResetError, GetEndpointStri
 115                    //}
 0116                    return communicationException;
 117                }
 118            }
 0119            else if (socketException.ErrorCode == UnsafeNativeMethods.WSAETIMEDOUT)
 120            {
 0121                TimeoutException timeoutException = new TimeoutException(SR.Format(SR.TcpConnectionTimedOut, "unknown"),
 122                //if (DiagnosticUtility.ShouldTrace(exceptionEventType))
 123                //{
 124                //    TraceUtility.TraceEvent(exceptionEventType, TraceCode.TcpConnectionTimedOut, GetEndpointString(SR.
 125                //}
 0126                return timeoutException;
 127            }
 128            else
 129            {
 0130                if (aborted)
 131                {
 0132                    return new CommunicationObjectAbortedException(SR.Format(SR.TcpTransferError, socketException.ErrorC
 133                }
 134                else
 135                {
 0136                    CommunicationException communicationException = new CommunicationException(SR.Format(SR.TcpTransferE
 137                    //if (DiagnosticUtility.ShouldTrace(exceptionEventType))
 138                    //{
 139                    //    TraceUtility.TraceEvent(exceptionEventType, TraceCode.TcpTransferError, GetEndpointString(SR.T
 140                    //}
 0141                    return communicationException;
 142                }
 143            }
 144        }
 145
 146        internal static class UnsafeNativeMethods
 147        {
 148            public const int ERROR_SUCCESS = 0;
 149            public const int ERROR_FILE_NOT_FOUND = 2;
 150            public const int ERROR_ACCESS_DENIED = 5;
 151            public const int ERROR_INVALID_HANDLE = 6;
 152            public const int ERROR_NOT_ENOUGH_MEMORY = 8;
 153            public const int ERROR_OUTOFMEMORY = 14;
 154            public const int ERROR_SHARING_VIOLATION = 32;
 155            public const int ERROR_NETNAME_DELETED = 64;
 156            public const int ERROR_INVALID_PARAMETER = 87;
 157            public const int ERROR_BROKEN_PIPE = 109;
 158            public const int ERROR_ALREADY_EXISTS = 183;
 159            public const int ERROR_PIPE_BUSY = 231;
 160            public const int ERROR_NO_DATA = 232;
 161            public const int ERROR_MORE_DATA = 234;
 162            public const int WAIT_TIMEOUT = 258;
 163            public const int ERROR_PIPE_CONNECTED = 535;
 164            public const int ERROR_OPERATION_ABORTED = 995;
 165            public const int ERROR_IO_PENDING = 997;
 166            public const int ERROR_SERVICE_ALREADY_RUNNING = 1056;
 167            public const int ERROR_SERVICE_DISABLED = 1058;
 168            public const int ERROR_NO_TRACKING_SERVICE = 1172;
 169            public const int ERROR_ALLOTTED_SPACE_EXCEEDED = 1344;
 170            public const int ERROR_NO_SYSTEM_RESOURCES = 1450;
 171
 172            // When querying for the token length
 173            private const int ERROR_INSUFFICIENT_BUFFER = 122;
 174
 175            public const int STATUS_PENDING = 0x103;
 176
 177            // socket errors
 178            public const int WSAACCESS = 10013;
 179            public const int WSAEMFILE = 10024;
 180            public const int WSAEMSGSIZE = 10040;
 181            public const int WSAEADDRINUSE = 10048;
 182            public const int WSAEADDRNOTAVAIL = 10049;
 183            public const int WSAENETDOWN = 10050;
 184            public const int WSAENETUNREACH = 10051;
 185            public const int WSAENETRESET = 10052;
 186            public const int WSAECONNABORTED = 10053;
 187            public const int WSAECONNRESET = 10054;
 188            public const int WSAENOBUFS = 10055;
 189            public const int WSAESHUTDOWN = 10058;
 190            public const int WSAETIMEDOUT = 10060;
 191            public const int WSAECONNREFUSED = 10061;
 192            public const int WSAEHOSTDOWN = 10064;
 193            public const int WSAEHOSTUNREACH = 10065;
 194        }
 195    }
 196}