< Summary - CoreWCF Coverage — PR #1766

Information
Class: CoreWCF.IdentityModel.HashStream
Assembly: CoreWCF.Primitives
File(s): /home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/IdentityModel/HashStream.cs
Line coverage
42%
Covered lines: 19
Uncovered lines: 26
Coverable lines: 45
Total lines: 168
Line coverage: 42.2%
Branch coverage
10%
Covered branches: 1
Total branches: 10
Branch coverage: 10%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
.ctor(...)50%2280%
Flush()100%11100%
FlushHash()100%110%
FlushHash(...)100%11100%
FlushHashAndGetValue()100%11100%
FlushHashAndGetValue(...)100%11100%
Read(...)100%110%
Reset()0%220%
Reset(...)100%11100%
Write(...)100%11100%
Seek(...)100%110%
SetLength(...)100%110%
Dispose(...)0%660%

File(s)

/home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/IdentityModel/HashStream.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.Security.Cryptography;
 7
 8namespace CoreWCF.IdentityModel
 9{
 10    internal sealed class HashStream : Stream
 11    {
 12        private long _length;
 13        private bool _disposed;
 14        private bool _hashNeedsReset;
 15        private MemoryStream _logStream;
 16
 17        /// <summary>
 18        /// Constructor for HashStream. The HashAlgorithm instance is owned by the caller.
 19        /// </summary>
 2320        public HashStream(HashAlgorithm hash)
 21        {
 2322            if (hash == null)
 23            {
 024                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(hash));
 25            }
 26
 2327            Reset(hash);
 2328        }
 29
 30        public override bool CanRead
 31        {
 032            get { return false; }
 33        }
 34
 35        public override bool CanWrite
 36        {
 037            get { return true; }
 38        }
 39
 40        public override bool CanSeek
 41        {
 042            get { return false; }
 43        }
 44
 9245        public HashAlgorithm Hash { get; private set; }
 46
 47        public override long Length
 48        {
 049            get { return _length; }
 50        }
 51
 52        public override long Position
 53        {
 054            get { return _length; }
 55            set
 56            {
 057                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotSupportedException());
 58            }
 59        }
 60
 61        public override void Flush()
 62        {
 2363        }
 64
 65        public void FlushHash()
 66        {
 067            FlushHash(null);
 068        }
 69
 70        public void FlushHash(MemoryStream preCanonicalBytes)
 71        {
 2372            Hash.TransformFinalBlock(CryptoHelper.EmptyBuffer, 0, 0);
 73            //TODO logs Pii data
 74            //if (DigestTraceRecordHelper.ShouldTraceDigest)
 75            //    DigestTraceRecordHelper.TraceDigest(this.logStream, this.hash);
 2376        }
 77
 78        public byte[] FlushHashAndGetValue()
 79        {
 2380            return FlushHashAndGetValue(null);
 81        }
 82
 83        public byte[] FlushHashAndGetValue(MemoryStream preCanonicalBytes)
 84        {
 2385            FlushHash(preCanonicalBytes);
 2386            return Hash.Hash;
 87        }
 88
 89        public override int Read(byte[] buffer, int offset, int count)
 90        {
 091            throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotSupportedException());
 92        }
 93
 94        public void Reset()
 95        {
 096            if (_hashNeedsReset)
 97            {
 098                Hash.Initialize();
 099                _hashNeedsReset = false;
 100            }
 0101            _length = 0;
 102
 103            // if (DigestTraceRecordHelper.ShouldTraceDigest)
 104            //     this.logStream = new MemoryStream();
 0105        }
 106
 107        public void Reset(HashAlgorithm hash)
 108        {
 23109            Hash = hash;
 23110            _hashNeedsReset = false;
 23111            _length = 0;
 112
 113            //  if (DigestTraceRecordHelper.ShouldTraceDigest)
 114            //     this.logStream = new MemoryStream();
 23115        }
 116
 117        public override void Write(byte[] buffer, int offset, int count)
 118        {
 23119            Hash.TransformBlock(buffer, offset, count, buffer, offset);
 23120            _length += count;
 23121            _hashNeedsReset = true;
 122
 123            // if (DigestTraceRecordHelper.ShouldTraceDigest)
 124            //    this.logStream.Write(buffer, offset, count);
 23125        }
 126
 127        public override long Seek(long offset, SeekOrigin origin)
 128        {
 0129            throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotSupportedException());
 130        }
 131
 132        public override void SetLength(long length)
 133        {
 0134            throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotSupportedException());
 135        }
 136
 137        #region IDisposable members
 138
 139        protected override void Dispose(bool disposing)
 140        {
 0141            base.Dispose(disposing);
 142
 0143            if (_disposed)
 144            {
 0145                return;
 146            }
 147
 0148            if (disposing)
 149            {
 150                //
 151                // Free all of our managed resources
 152                //
 153
 0154                if (_logStream != null)
 155                {
 0156                    _logStream.Dispose();
 0157                    _logStream = null;
 158                }
 159            }
 160
 161            // Free native resources, if any.
 162
 0163            _disposed = true;
 0164        }
 165
 166        #endregion
 167    }
 168}