< Summary - CoreWCF Coverage — PR #1766

Information
Class: CoreWCF.Channels.XmlByteStreamReader
Assembly: CoreWCF.Primitives
File(s): /home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/Channels/XmlByteStreamReader.cs
Line coverage
46%
Covered lines: 46
Uncovered lines: 52
Coverable lines: 98
Total lines: 291
Line coverage: 46.9%
Branch coverage
47%
Covered branches: 20
Total branches: 42
Branch coverage: 47.6%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
.ctor(...)100%11100%
MoveToStartElement()100%110%
Close()100%22100%
OnClose()100%11100%
GetAttribute(...)100%110%
GetAttribute(...)100%110%
GetAttribute(...)100%110%
LookupNamespace(...)0%660%
MoveToAttribute(...)100%110%
MoveToAttribute(...)100%110%
MoveToElement()0%220%
MoveToFirstAttribute()100%110%
MoveToNextAttribute()100%110%
Read()83.33%6690.9%
ReadAttributeValue()100%110%
ReadContentAsBinHex(...)100%110%
ResolveEntity()100%110%
ToByteArray()50%2250%
ToStream()50%2250%
EnsureInContent()50%4466.66%

File(s)

/home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/Channels/XmlByteStreamReader.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.Xml;
 7using CoreWCF.Runtime;
 8
 9namespace CoreWCF.Channels
 10{
 11    internal abstract class XmlByteStreamReader : XmlDictionaryReader
 12    {
 13        private string _base64StringValue;
 14        private NameTable _nameTable;
 15        private bool _readBase64AsString;
 16
 17        protected ReaderPosition position;
 18        protected XmlDictionaryReaderQuotas quotas;
 19
 2220        protected XmlByteStreamReader(XmlDictionaryReaderQuotas quotas)
 21        {
 2222            this.quotas = quotas;
 2223            position = ReaderPosition.None;
 2224        }
 25
 026        public override int AttributeCount => 0;
 27
 028        public override string BaseURI => string.Empty;
 29
 030        public override bool CanCanonicalize => false;
 31
 032        public override bool CanReadBinaryContent => true;
 33
 034        public override bool CanReadValueChunk => false;
 35
 036        public override bool CanResolveEntity => false;
 37
 038        public override int Depth => position == ReaderPosition.Content ? 1 : 0;
 39
 040        public override bool EOF => position == ReaderPosition.EOF;
 41
 042        public override bool HasAttributes => false;
 43
 044        public override bool HasValue => position == ReaderPosition.Content;
 45
 046        public override bool IsDefault => false;
 47
 048        public override bool IsEmptyElement => false;
 49
 150        public override string LocalName => position == ReaderPosition.StartElement ? ByteStreamMessageUtility.StreamEle
 51
 52        public override void MoveToStartElement()
 53        {
 054            base.MoveToStartElement();
 055            position = ReaderPosition.StartElement;
 056        }
 57
 58        public override XmlNameTable NameTable
 59        {
 60            get
 61            {
 062                if (_nameTable == null)
 63                {
 064                    _nameTable = new NameTable();
 065                    _nameTable.Add(ByteStreamMessageUtility.StreamElementName);
 66                }
 67
 068                return _nameTable;
 69            }
 70        }
 71
 172        public override string NamespaceURI => string.Empty;
 73
 74        public override XmlNodeType NodeType
 75        {
 76            get
 77            {
 7778                switch (position)
 79                {
 80                    case ReaderPosition.StartElement:
 4281                        return XmlNodeType.Element;
 82                    case ReaderPosition.Content:
 1183                        return XmlNodeType.Text;
 84                    case ReaderPosition.EndElement:
 785                        return XmlNodeType.EndElement;
 86                    default:
 87                        // and StreamPosition.EOF
 1788                        return XmlNodeType.None;
 89                }
 90            }
 91        }
 92
 093        public override string Prefix => string.Empty;
 94
 995        public override XmlDictionaryReaderQuotas Quotas => quotas;
 96
 97        public override ReadState ReadState
 98        {
 99            get
 100            {
 1101                switch (position)
 102                {
 103                    case ReaderPosition.None:
 0104                        return ReadState.Initial;
 105                    case ReaderPosition.StartElement:
 106                    case ReaderPosition.Content:
 107                    case ReaderPosition.EndElement:
 0108                        return ReadState.Interactive;
 109                    case ReaderPosition.EOF:
 1110                        return ReadState.Closed;
 111                    default:
 112                        Fx.Assert("Unknown ReadState hit in XmlByteStreamReader");
 0113                        return ReadState.Error;
 114                }
 115            }
 116        }
 117
 118        public override string Value
 119        {
 120            get
 121            {
 7122                switch (position)
 123                {
 124                    case ReaderPosition.Content:
 7125                        if (!_readBase64AsString)
 126                        {
 7127                            _base64StringValue = Convert.ToBase64String(ReadContentAsBase64());
 7128                            _readBase64AsString = true;
 129                        }
 7130                        return _base64StringValue;
 131
 132                    default:
 0133                        return string.Empty;
 134                }
 135            }
 136        }
 137
 138        public override void Close()
 139        {
 12140            if (!IsClosed)
 141            {
 142                try
 143                {
 12144                    OnClose();
 12145                }
 146                finally
 147                {
 12148                    position = ReaderPosition.EOF;
 12149                    IsClosed = true;
 12150                }
 151            }
 12152        }
 153
 39154        protected bool IsClosed { get; private set; }
 155
 156        protected virtual void OnClose()
 157        {
 12158        }
 159
 0160        public override string GetAttribute(int i) => throw Fx.Exception.ArgumentOutOfRange(nameof(i), i, SR.ArgumentNot
 161
 0162        public override string GetAttribute(string name, string namespaceURI) => null;
 163
 0164        public override string GetAttribute(string name) => null;
 165
 166        public override string LookupNamespace(string prefix)
 167        {
 0168            if (prefix == string.Empty)
 169            {
 0170                return string.Empty;
 171            }
 0172            else if (prefix == "xml")
 173            {
 0174                return ByteStreamMessageUtility.XmlNamespace;
 175            }
 0176            else if (prefix == "xmlns")
 177            {
 0178                return ByteStreamMessageUtility.XmlNamespaceNamespace;
 179            }
 180            else
 181            {
 0182                return null;
 183            }
 184        }
 185
 0186        public override bool MoveToAttribute(string name, string ns) => false;
 187
 0188        public override bool MoveToAttribute(string name) => false;
 189
 190        public override bool MoveToElement()
 191        {
 0192            if (position == ReaderPosition.None)
 193            {
 0194                position = ReaderPosition.StartElement;
 0195                return true;
 196            }
 197
 0198            return false;
 199        }
 200
 0201        public override bool MoveToFirstAttribute() => false;
 202
 0203        public override bool MoveToNextAttribute() => false;
 204
 205        public override bool Read()
 206        {
 47207            switch (position)
 208            {
 209                case ReaderPosition.None:
 17210                    position = ReaderPosition.StartElement;
 17211                    return true;
 212                case ReaderPosition.StartElement:
 8213                    position = ReaderPosition.Content;
 8214                    return true;
 215                case ReaderPosition.Content:
 7216                    position = ReaderPosition.EndElement;
 7217                    return true;
 218                case ReaderPosition.EndElement:
 8219                    position = ReaderPosition.EOF;
 8220                    return false;
 221                case ReaderPosition.EOF:
 7222                    return false;
 223                default:
 224                    // we should never get here
 225                    // it means we managed to get into some unknown position in the stream
 226                    Fx.Assert(false, "Unknown read position in XmlByteStreamReader");
 0227                    return false;
 228            }
 229        }
 230
 0231        public override bool ReadAttributeValue() => false;
 232
 233        public override abstract int ReadContentAsBase64(byte[] buffer, int index, int count);
 234
 0235        public override int ReadContentAsBinHex(byte[] buffer, int index, int count) => throw Fx.Exception.AsError(new N
 236
 0237        public override void ResolveEntity() => throw Fx.Exception.AsError(new NotSupportedException());
 238
 239        public byte[] ToByteArray()
 240        {
 5241            if (IsClosed)
 242            {
 0243                throw Fx.Exception.AsError(
 0244                    new ObjectDisposedException(SR.XmlReaderClosed));
 245            }
 246
 5247            return OnToByteArray();
 248        }
 249
 250        protected abstract byte[] OnToByteArray();
 251
 252        public Stream ToStream()
 253        {
 3254            if (IsClosed)
 255            {
 0256                throw Fx.Exception.AsError(
 0257                    new ObjectDisposedException(SR.XmlReaderClosed));
 258            }
 259
 3260            return OnToStream();
 261        }
 262
 263        protected abstract Stream OnToStream();
 264
 265        protected void EnsureInContent()
 266        {
 267            // This method is only being called from XmlByteStreamReader.ReadContentAsBase64.
 268            // We don't block if the position is None or StartElement since we want our XmlByteStreamReader
 269            // to be a little bit smarter when people just to access the content of the ByteStreamMessage.
 9270            if (position == ReaderPosition.EndElement
 9271             || position == ReaderPosition.EOF)
 272            {
 0273                throw Fx.Exception.AsError(
 0274                    new InvalidOperationException(SR.Format(SR.ByteStreamReaderNotInByteStream, ByteStreamMessageUtility
 275            }
 276            else
 277            {
 9278                position = ReaderPosition.Content;
 279            }
 9280        }
 281
 282        protected enum ReaderPosition
 283        {
 284            None,
 285            StartElement,
 286            Content,
 287            EndElement,
 288            EOF
 289        }
 290    }
 291}