< Summary

Information
Class: Renci.SshNet.Common.PacketDump
Assembly: Renci.SshNet
File(s): \home\appveyor\projects\ssh-net\src\Renci.SshNet\Common\PacketDump.cs
Line coverage
100%
Covered lines: 68
Uncovered lines: 0
Coverable lines: 68
Total lines: 110
Line coverage: 100%
Branch coverage
100%
Covered branches: 26
Total branches: 26
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity Line coverage
Create(...)100%1100%
Create(...)100%12100%
AsHex(...)100%6100%
AsAscii(...)100%8100%

File(s)

\home\appveyor\projects\ssh-net\src\Renci.SshNet\Common\PacketDump.cs

#LineLine coverage
 1using System;
 2using System.Collections.Generic;
 3using System.Globalization;
 4using System.Text;
 5
 6namespace Renci.SshNet.Common
 7{
 8    internal static class PacketDump
 9    {
 10        public static string Create(List<byte> data, int indentLevel)
 3911        {
 3912            return Create(data.ToArray(), indentLevel);
 3913        }
 14
 15        public static string Create(byte[] data, int indentLevel)
 8416        {
 8417            if (data is null)
 318            {
 319                throw new ArgumentNullException(nameof(data));
 20            }
 21
 8122            if (indentLevel < 0)
 323            {
 324                throw new ArgumentOutOfRangeException(nameof(indentLevel), "Cannot be less than zero.");
 25            }
 26
 27            const int lineWidth = 16;
 28
 7829            var result = new StringBuilder();
 7830            var line = new byte[lineWidth];
 7831            var indentChars = new string(' ', indentLevel);
 32
 38233            for (var pos = 0; pos < data.Length; )
 22634            {
 22635                var linePos = 0;
 36
 22637                if (result.Length > 0)
 15138                {
 15139                    _ = result.Append(Environment.NewLine);
 15140                }
 41
 22642                _ = result.Append(indentChars);
 22643                _ = result.Append(pos.ToString("X8"));
 22644                _ = result.Append("  ");
 45
 305546                while (true)
 305547                {
 305548                    line[linePos++] = data[pos++];
 49
 305550                    if (linePos == lineWidth || pos == data.Length)
 22651                    {
 22652                        break;
 53                    }
 282954                }
 55
 22656                _ = result.Append(AsHex(line, linePos));
 22657                _ = result.Append("  ");
 22658                _ = result.Append(AsAscii(line, linePos));
 22659            }
 60
 7861            return result.ToString();
 7862        }
 63
 64        private static string AsHex(byte[] data, int length)
 22665        {
 22666            var hex = new StringBuilder();
 67
 656268            for (var i = 0; i < length; i++)
 305569            {
 305570                if (i > 0)
 282971                {
 282972                    _ = hex.Append(' ');
 282973                }
 74
 305575                _ = hex.Append(data[i].ToString("X2", CultureInfo.InvariantCulture));
 305576            }
 77
 22678            if (length < data.Length)
 6479            {
 6480                _ = hex.Append(new string(' ', (data.Length - length) * 3));
 6481            }
 82
 22683            return hex.ToString();
 22684        }
 85
 86        private static string AsAscii(byte[] data, int length)
 22687        {
 22688            var encoding = Encoding.ASCII;
 89
 22690            var ascii = new StringBuilder();
 91            const char dot = '.';
 92
 656293            for (var i = 0; i < length; i++)
 305594            {
 305595                var b = data[i];
 96
 305597                if (b is < 32 or >= 127)
 56898                {
 56899                    _ = ascii.Append(dot);
 568100                }
 101                else
 2487102                {
 2487103                    _ = ascii.Append(encoding.GetString(data, i, 1));
 2487104                }
 3055105            }
 106
 226107            return ascii.ToString();
 226108        }
 109    }
 110}