< Summary - CoreWCF Coverage — PR #1766

Information
Class: CoreWCF.IdentityModel.DeflateCookieTransform
Assembly: CoreWCF.Primitives
File(s): /home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/IdentityModel/DeflateCookieTransform.cs
Line coverage
9%
Covered lines: 3
Uncovered lines: 28
Coverable lines: 31
Total lines: 121
Line coverage: 9.6%
Branch coverage
0%
Covered branches: 0
Total branches: 12
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%11100%
Decode(...)0%880%
Encode(...)0%440%

File(s)

/home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/IdentityModel/DeflateCookieTransform.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.IO;
 5using System.IO.Compression;
 6using CoreWCF.IdentityModel.Tokens;
 7
 8namespace CoreWCF.IdentityModel
 9{
 10    /// <summary>
 11    /// Provides cookie compression using <see cref="DeflateStream"/>.
 12    /// </summary>
 13    public sealed class DeflateCookieTransform : CookieTransform
 14    {
 615        private int _maxDecompressedSize = 1024 * 1024;     // Default maximum of 1MB
 16
 17        /// <summary>
 18        /// Creates a new instance of <see cref="DeflateCookieTransform"/>.
 19        /// </summary>
 620        public DeflateCookieTransform()
 21        {
 622        }
 23
 24        /// <summary>
 25        /// Gets or sets the maximum size (in bytes) of a decompressed cookie.
 26        /// </summary>
 27        public int MaxDecompressedSize
 28        {
 029            get { return _maxDecompressedSize; }
 030            set { _maxDecompressedSize = value; }
 31        }
 32
 33        /// <summary>
 34        /// Inflates data.
 35        /// </summary>
 36        /// <param name="encoded">Data previously returned from <see cref="Encode"/></param>
 37        /// <returns>Decoded data.</returns>
 38        /// <exception cref="ArgumentNullException">The argument 'value' is null.</exception>
 39        /// <exception cref="ArgumentException">The argument 'value' contains zero bytes.</exception>
 40        /// <exception cref="SecurityTokenException">The decompressed length is larger than MaxDecompressedSize.</except
 41        public override byte[] Decode( byte[] encoded )
 42        {
 043            if ( null == encoded )
 44            {
 045                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(encoded));
 46            }
 47
 048            if ( 0 == encoded.Length )
 49            {
 050                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument( nameof(encoded), SR.Format( SR.ID6045 ) );
 51            }
 52
 053            MemoryStream compressedStream = new MemoryStream( encoded );
 054            using ( DeflateStream deflateStream = new DeflateStream( compressedStream, CompressionMode.Decompress, false
 55            {
 056                using ( MemoryStream decompressedStream = new MemoryStream() )
 57                {
 058                    byte[] buffer = new byte[1024];
 59                    int bytesRead;
 60
 61                    do
 62                    {
 063                        bytesRead = deflateStream.Read( buffer, 0, buffer.Length );
 064                        decompressedStream.Write( buffer, 0, bytesRead );
 65
 66                        // check length against configured maximum to prevevent decompression bomb attacks
 067                        if ( decompressedStream.Length > MaxDecompressedSize )
 68                        {
 069                            throw DiagnosticUtility.ExceptionUtility.ThrowHelperError( new SecurityTokenException( SR.Fo
 70                        }
 071                    } while ( bytesRead > 0 );
 72
 073                    return decompressedStream.ToArray();
 74                }
 75            }
 076        }
 77
 78        /// <summary>
 79        /// Deflates data.
 80        /// </summary>
 81        /// <param name=nameof(value)>Data to be compressed.</param>
 82        /// <returns>Compressed data.</returns>
 83        /// <exception cref="ArgumentNullException">The argument 'value' is null.</exception>
 84        /// <exception cref="ArgumentException">The argument 'value' contains zero bytes.</exception>
 85        public override byte[] Encode( byte[] value )
 86        {
 087            if ( null == value )
 88            {
 089                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull( nameof(value) );
 90            }
 91
 092            if ( 0 == value.Length )
 93            {
 094                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument( nameof(value), SR.Format( SR.ID6044 ) );
 95            }
 96
 097            using ( MemoryStream compressedStream = new MemoryStream() )
 98            {
 099                using ( DeflateStream deflateStream = new DeflateStream( compressedStream, CompressionMode.Compress, tru
 100                {
 0101                    deflateStream.Write( value, 0, value.Length );
 0102                }
 103
 0104                byte[] encoded = compressedStream.ToArray();
 105
 106                //if ( DiagnosticUtility.ShouldTrace( TraceEventType.Information ) )
 107                //{
 108                //    TraceUtility.TraceEvent(
 109                //            TraceEventType.Information,
 110                //            TraceCode.Diagnostics,
 111                //            SR.Format(SR.TraceDeflateCookieEncode),
 112                //            new DeflateCookieTraceRecord( value.Length, encoded.Length ),
 113                //            null,
 114                //            null );
 115                //}
 116
 0117                return encoded;
 118            }
 0119        }
 120    }
 121}