< Summary - CoreWCF Coverage — PR #1766

Information
Class: CoreWCF.Security.SoapHexBinary
Assembly: CoreWCF.Primitives
File(s): /home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/Security/SoapHexBinary.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 34
Coverable lines: 34
Total lines: 92
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 16
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%
ToString()0%440%
Parse(...)100%110%
ToByteArray(...)0%440%
ToByte(...)100%110%
FilterBin64(...)0%880%

File(s)

/home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/Security/SoapHexBinary.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.Globalization;
 6using System.Text;
 7
 8namespace CoreWCF.Security
 9{
 10    internal sealed class SoapHexBinary
 11    {
 012        private readonly StringBuilder _sb = new StringBuilder(100);
 13
 014        public SoapHexBinary()
 15        {
 016        }
 17
 018        public SoapHexBinary(byte[] value)
 19        {
 020            Value = value;
 021        }
 22
 023        public byte[] Value { get; set; }
 24
 25        public override string ToString()
 26        {
 027            _sb.Length = 0;
 028            for (int i = 0; i < Value.Length; i++)
 29            {
 030                string s = Value[i].ToString("X", CultureInfo.InvariantCulture);
 031                if (s.Length == 1)
 32                {
 033                    _sb.Append('0');
 34                }
 35
 036                _sb.Append(s);
 37            }
 038            return _sb.ToString();
 39        }
 40
 41        public static SoapHexBinary Parse(string value)
 42        {
 043            return new SoapHexBinary(ToByteArray(FilterBin64(value)));
 44        }
 45
 46        private static byte[] ToByteArray(string value)
 47        {
 048            char[] cA = value.ToCharArray();
 049            if (cA.Length % 2 != 0)
 50            {
 051                throw new FormatException(SR.Format("Remoting_SOAPInteropxsdInvalid", "xsd:hexBinary", value));
 52            }
 053            byte[] bA = new byte[cA.Length / 2];
 054            for (int i = 0; i < cA.Length / 2; i++)
 55            {
 056                bA[i] = (byte)(ToByte(cA[i * 2], value) * 16 + ToByte(cA[i * 2 + 1], value));
 57            }
 58
 059            return bA;
 60        }
 61
 62        private static byte ToByte(char c, string value)
 63        {
 064            string s = c.ToString();
 65            byte b;
 66            try
 67            {
 068                s = c.ToString();
 069                b = byte.Parse(s, NumberStyles.HexNumber, CultureInfo.InvariantCulture);
 070            }
 071            catch (Exception)
 72            {
 073                throw new FormatException(SR.Format("Remoting_SOAPInteropxsdInvalid", "xsd:hexBinary", value));
 74            }
 75
 076            return b;
 77        }
 78
 79        internal static string FilterBin64(string value)
 80        {
 081            StringBuilder sb = new StringBuilder();
 082            for (int i = 0; i < value.Length; i++)
 83            {
 084                if (!(value[i] == ' ' || value[i] == '\n' || value[i] == '\r'))
 85                {
 086                    sb.Append(value[i]);
 87                }
 88            }
 089            return sb.ToString();
 90        }
 91    }
 92}