< Summary

Information
Class: Renci.SshNet.Common.Extensions
Assembly: Renci.SshNet
File(s): \home\appveyor\projects\ssh-net\src\Renci.SshNet\Common\Extensions.cs
Line coverage
93%
Covered lines: 148
Uncovered lines: 10
Coverable lines: 158
Total lines: 332
Line coverage: 93.6%
Branch coverage
90%
Covered branches: 60
Total branches: 66
Branch coverage: 90.9%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity Line coverage
ToArray(...)75%483.33%
ToServiceName(...)25%471.42%
ToBigInteger(...)100%1100%
ToBigInteger2(...)100%2100%
Reverse(...)100%1100%
DebugPrint(...)100%2100%
CreateInstance(...)50%266.66%
ValidatePort(...)100%2100%
ValidatePort(...)100%4100%
Take(...)100%8100%
Take(...)100%6100%
IsEqualTo(...)91.66%1290.47%
TrimLeadingZeros(...)100%8100%
Pad(...)100%2100%
Concat(...)100%8100%
CanRead(...)100%10%
CanWrite(...)100%1100%
IsConnected(...)100%2100%

File(s)

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

#LineLine coverage
 1using System;
 2using System.Collections.Generic;
 3using System.Diagnostics;
 4using System.Globalization;
 5using System.Net;
 6using System.Net.Sockets;
 7using System.Text;
 8using Renci.SshNet.Abstractions;
 9using Renci.SshNet.Messages;
 10
 11namespace Renci.SshNet.Common
 12{
 13    /// <summary>
 14    /// Collection of different extension methods.
 15    /// </summary>
 16    internal static partial class Extensions
 17    {
 18        internal static byte[] ToArray(this ServiceName serviceName)
 767719        {
 767720            switch (serviceName)
 21            {
 22                case ServiceName.UserAuthentication:
 242823                    return SshData.Ascii.GetBytes("ssh-userauth");
 24                case ServiceName.Connection:
 524925                    return SshData.Ascii.GetBytes("ssh-connection");
 26                default:
 027                    throw new NotSupportedException(string.Format("Service name '{0}' is not supported.", serviceName));
 28            }
 767729        }
 30
 31        internal static ServiceName ToServiceName(this byte[] data)
 181332        {
 181333            var sshServiceName = SshData.Ascii.GetString(data, 0, data.Length);
 181334            switch (sshServiceName)
 35            {
 36                case "ssh-userauth":
 181337                    return ServiceName.UserAuthentication;
 38                case "ssh-connection":
 039                    return ServiceName.Connection;
 40                default:
 041                    throw new NotSupportedException(string.Format("Service name '{0}' is not supported.", sshServiceName
 42            }
 181343        }
 44
 45        internal static BigInteger ToBigInteger(this byte[] data)
 365246        {
 365247            var reversed = new byte[data.Length];
 365248            Buffer.BlockCopy(data, 0, reversed, 0, data.Length);
 365249            return new BigInteger(reversed.Reverse());
 365250        }
 51
 52        /// <summary>
 53        /// Initializes a new instance of the <see cref="BigInteger"/> structure using the SSH BigNum2 Format.
 54        /// </summary>
 55        public static BigInteger ToBigInteger2(this byte[] data)
 362256        {
 362257            if ((data[0] & (1 << 7)) != 0)
 62158            {
 62159                var buf = new byte[data.Length + 1];
 62160                Buffer.BlockCopy(data, 0, buf, 1, data.Length);
 62161                data = buf;
 62162            }
 63
 362264            return data.ToBigInteger();
 362265        }
 66
 67        /// <summary>
 68        /// Reverses the sequence of the elements in the entire one-dimensional <see cref="Array"/>.
 69        /// </summary>
 70        /// <param name="array">The one-dimensional <see cref="Array"/> to reverse.</param>
 71        /// <returns>
 72        /// The <see cref="Array"/> with its elements reversed.
 73        /// </returns>
 74        internal static T[] Reverse<T>(this T[] array)
 1676975        {
 1676976            Array.Reverse(array);
 1676677            return array;
 1676678        }
 79
 80        /// <summary>
 81        /// Prints out the specified bytes.
 82        /// </summary>
 83        /// <param name="bytes">The bytes.</param>
 84        internal static void DebugPrint(this IEnumerable<byte> bytes)
 385        {
 386            var sb = new StringBuilder();
 87
 78388            foreach (var b in bytes)
 38789            {
 38790                _ = sb.AppendFormat(CultureInfo.CurrentCulture, "0x{0:x2}, ", b);
 38791            }
 92
 393            Debug.WriteLine(sb.ToString());
 394        }
 95
 96        /// <summary>
 97        /// Creates an instance of the specified type using that type's default constructor.
 98        /// </summary>
 99        /// <typeparam name="T">The type to create.</typeparam>
 100        /// <param name="type">Type of the instance to create.</param>
 101        /// <returns>A reference to the newly created object.</returns>
 102        internal static T CreateInstance<T>(this Type type)
 103            where T : class
 1199104        {
 1199105            if (type is null)
 0106            {
 0107                return null;
 108            }
 109
 1199110            return Activator.CreateInstance(type) as T;
 1199111        }
 112
 113        internal static void ValidatePort(this uint value, string argument)
 917114        {
 917115            if (value > IPEndPoint.MaxPort)
 3116            {
 3117                throw new ArgumentOutOfRangeException(argument,
 3118                                                      string.Format(CultureInfo.InvariantCulture, "Specified value canno
 119            }
 914120        }
 121
 122        internal static void ValidatePort(this int value, string argument)
 3272123        {
 3272124            if (value < IPEndPoint.MinPort)
 9125            {
 9126                throw new ArgumentOutOfRangeException(argument, string.Format(CultureInfo.InvariantCulture, "Specified v
 127            }
 128
 3263129            if (value > IPEndPoint.MaxPort)
 9130            {
 9131                throw new ArgumentOutOfRangeException(argument, string.Format(CultureInfo.InvariantCulture, "Specified v
 132            }
 3254133        }
 134
 135        /// <summary>
 136        /// Returns a specified number of contiguous bytes from a given offset.
 137        /// </summary>
 138        /// <param name="value">The array to return a number of bytes from.</param>
 139        /// <param name="offset">The zero-based offset in <paramref name="value"/> at which to begin taking bytes.</para
 140        /// <param name="count">The number of bytes to take from <paramref name="value"/>.</param>
 141        /// <returns>
 142        /// A <see cref="byte"/> array that contains the specified number of bytes at the specified offset
 143        /// of the input array.
 144        /// </returns>
 145        /// <exception cref="ArgumentNullException"><paramref name="value"/> is <see langword="null"/>.</exception>
 146        /// <remarks>
 147        /// When <paramref name="offset"/> is zero and <paramref name="count"/> equals the length of <paramref name="val
 148        /// then <paramref name="value"/> is returned.
 149        /// </remarks>
 150        public static byte[] Take(this byte[] value, int offset, int count)
 52979151        {
 52979152            if (value is null)
 3153            {
 3154                throw new ArgumentNullException(nameof(value));
 155            }
 156
 52976157            if (count == 0)
 22158            {
 22159                return Array.Empty<byte>();
 160            }
 161
 52954162            if (offset == 0 && value.Length == count)
 69163            {
 69164                return value;
 165            }
 166
 52885167            var taken = new byte[count];
 52885168            Buffer.BlockCopy(value, offset, taken, 0, count);
 52879169            return taken;
 52970170        }
 171
 172        /// <summary>
 173        /// Returns a specified number of contiguous bytes from the start of the specified byte array.
 174        /// </summary>
 175        /// <param name="value">The array to return a number of bytes from.</param>
 176        /// <param name="count">The number of bytes to take from <paramref name="value"/>.</param>
 177        /// <returns>
 178        /// A <see cref="byte"/> array that contains the specified number of bytes at the start of the input array.
 179        /// </returns>
 180        /// <exception cref="ArgumentNullException"><paramref name="value"/> is <see langword="null"/>.</exception>
 181        /// <remarks>
 182        /// When <paramref name="count"/> equals the length of <paramref name="value"/>, then <paramref name="value"/>
 183        /// is returned.
 184        /// </remarks>
 185        public static byte[] Take(this byte[] value, int count)
 5344186        {
 5344187            if (value is null)
 3188            {
 3189                throw new ArgumentNullException(nameof(value));
 190            }
 191
 5341192            if (count == 0)
 3193            {
 3194                return Array.Empty<byte>();
 195            }
 196
 5338197            if (value.Length == count)
 308198            {
 308199                return value;
 200            }
 201
 5030202            var taken = new byte[count];
 5030203            Buffer.BlockCopy(value, 0, taken, 0, count);
 5027204            return taken;
 5338205        }
 206
 207        public static bool IsEqualTo(this byte[] left, byte[] right)
 54567208        {
 54567209            if (left is null)
 6210            {
 6211                throw new ArgumentNullException(nameof(left));
 212            }
 213
 54561214            if (right is null)
 3215            {
 3216                throw new ArgumentNullException(nameof(right));
 217            }
 218
 54558219            if (left == right)
 3220            {
 3221                return true;
 222            }
 223
 54555224            if (left.Length != right.Length)
 12225            {
 12226                return false;
 227            }
 228
 2569688229            for (var i = 0; i < left.Length; i++)
 1230301230            {
 1230301231                if (left[i] != right[i])
 0232                {
 0233                    return false;
 234                }
 1230301235            }
 236
 54543237            return true;
 54558238        }
 239
 240        /// <summary>
 241        /// Trims the leading zero from a byte array.
 242        /// </summary>
 243        /// <param name="value">The value.</param>
 244        /// <returns>
 245        /// <paramref name="value"/> without leading zeros.
 246        /// </returns>
 247        public static byte[] TrimLeadingZeros(this byte[] value)
 812248        {
 812249            if (value is null)
 3250            {
 3251                throw new ArgumentNullException(nameof(value));
 252            }
 253
 2122254            for (var i = 0; i < value.Length; i++)
 1058255            {
 1058256                if (value[i] == 0)
 252257                {
 252258                    continue;
 259                }
 260
 261                // if the first byte is non-zero, then we return the byte array as is
 806262                if (i == 0)
 557263                {
 557264                    return value;
 265                }
 266
 249267                var remainingBytes = value.Length - i;
 268
 249269                var cleaned = new byte[remainingBytes];
 249270                Buffer.BlockCopy(value, i, cleaned, 0, remainingBytes);
 249271                return cleaned;
 272            }
 273
 3274            return value;
 809275        }
 276
 277        /// <summary>
 278        /// Pads with leading zeros if needed.
 279        /// </summary>
 280        /// <param name="data">The data.</param>
 281        /// <param name="length">The length to pad to.</param>
 282        public static byte[] Pad(this byte[] data, int length)
 59283        {
 59284            if (length <= data.Length)
 49285            {
 49286                return data;
 287            }
 288
 10289            var newData = new byte[length];
 10290            Buffer.BlockCopy(data, 0, newData, newData.Length - data.Length, data.Length);
 10291            return newData;
 59292        }
 293
 294        public static byte[] Concat(this byte[] first, byte[] second)
 86295        {
 86296            if (first is null || first.Length == 0)
 9297            {
 9298                return second;
 299            }
 300
 77301            if (second is null || second.Length == 0)
 6302            {
 6303                return first;
 304            }
 305
 71306            var concat = new byte[first.Length + second.Length];
 71307            Buffer.BlockCopy(first, 0, concat, 0, first.Length);
 71308            Buffer.BlockCopy(second, 0, concat, first.Length, second.Length);
 71309            return concat;
 86310        }
 311
 312        internal static bool CanRead(this Socket socket)
 0313        {
 0314            return SocketAbstraction.CanRead(socket);
 0315        }
 316
 317        internal static bool CanWrite(this Socket socket)
 53126318        {
 53126319            return SocketAbstraction.CanWrite(socket);
 53126320        }
 321
 322        internal static bool IsConnected(this Socket socket)
 57530323        {
 57530324            if (socket is null)
 50325            {
 50326                return false;
 327            }
 328
 57480329            return socket.Connected;
 57530330        }
 331    }
 332}