< Summary - CoreWCF Coverage — PR #1766

Information
Class: CoreWCF.Channels.MaxMessageSizeStream
Assembly: CoreWCF.WebHttp
File(s): /home/runner/work/CoreWCF/CoreWCF/src/Common/src/CoreWCF/Channels/MaxMessageSizeStream.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 40
Coverable lines: 40
Total lines: 117
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 8
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%
ReadAsync()100%110%
WriteAsync(...)100%110%
Read(...)100%110%
ReadByte()0%220%
Write(...)100%110%
WriteByte(...)100%110%
CreateMaxReceivedMessageSizeExceededException(...)100%110%
CreateMaxSentMessageSizeExceededException(...)100%110%
PrepareRead(...)0%440%
FinishRead(...)100%110%
PrepareWrite(...)0%220%

File(s)

/home/runner/work/CoreWCF/CoreWCF/src/Common/src/CoreWCF/Channels/MaxMessageSizeStream.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.IO;
 6using System.Threading;
 7using System.Threading.Tasks;
 8
 9namespace CoreWCF.Channels
 10{
 11    internal class MaxMessageSizeStream : DelegatingStream
 12    {
 13        private readonly long _maxMessageSize;
 14        private long _totalBytesRead;
 15        private long _bytesWritten;
 16
 17        public MaxMessageSizeStream(Stream stream, long maxMessageSize)
 018            : base(stream)
 19        {
 020            _maxMessageSize = maxMessageSize;
 021        }
 22
 23        public override async Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationTo
 24        {
 025            count = PrepareRead(count);
 026            int bytesRead = await base.ReadAsync(buffer, offset, count, cancellationToken);
 027            return FinishRead(bytesRead);
 028        }
 29
 30        public override Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
 31        {
 032            PrepareWrite(count);
 033            return base.WriteAsync(buffer, offset, count, cancellationToken);
 34        }
 35
 36        public override int Read(byte[] buffer, int offset, int count)
 37        {
 038            count = PrepareRead(count);
 039            return FinishRead(base.Read(buffer, offset, count));
 40        }
 41
 42        public override int ReadByte()
 43        {
 044            PrepareRead(1);
 045            int i = base.ReadByte();
 046            if (i != -1)
 47            {
 048                FinishRead(1);
 49            }
 50
 051            return i;
 52        }
 53
 54        public override void Write(byte[] buffer, int offset, int count)
 55        {
 056            PrepareWrite(count);
 057            base.Write(buffer, offset, count);
 058        }
 59
 60        public override void WriteByte(byte value)
 61        {
 062            PrepareWrite(1);
 063            base.WriteByte(value);
 064        }
 65
 66        public static Exception CreateMaxReceivedMessageSizeExceededException(long maxMessageSize)
 67        {
 068            string message = SRCommon.Format(SRCommon.MaxReceivedMessageSizeExceeded, maxMessageSize);
 069            Exception inner = new QuotaExceededException(message);
 70
 071            return new CommunicationException(message, inner);
 72        }
 73
 74        internal static Exception CreateMaxSentMessageSizeExceededException(long maxMessageSize)
 75        {
 076            string message = SRCommon.Format(SRCommon.MaxSentMessageSizeExceeded, maxMessageSize);
 077            Exception inner = new QuotaExceededException(message);
 78
 079            return new CommunicationException(message, inner);
 80        }
 81
 82        private int PrepareRead(int bytesToRead)
 83        {
 084            if (_totalBytesRead >= _maxMessageSize)
 85            {
 086                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(CreateMaxReceivedMessageSizeExceededException(
 87            }
 88
 089            long bytesRemaining = _maxMessageSize - _totalBytesRead;
 90
 091            if (bytesRemaining > int.MaxValue)
 92            {
 093                return bytesToRead;
 94            }
 95            else
 96            {
 097                return Math.Min(bytesToRead, (int)(_maxMessageSize - _totalBytesRead));
 98            }
 99        }
 100
 101        private int FinishRead(int bytesRead)
 102        {
 0103            _totalBytesRead += bytesRead;
 0104            return bytesRead;
 105        }
 106
 107        private void PrepareWrite(int bytesToWrite)
 108        {
 0109            if (_bytesWritten + bytesToWrite > _maxMessageSize)
 110            {
 0111                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(CreateMaxSentMessageSizeExceededException(_max
 112            }
 113
 0114            _bytesWritten += bytesToWrite;
 0115        }
 116    }
 117}