< Summary

Class:SharpHoundRPC.Extensions
Assembly:SharpHoundRPC
File(s):D:\a\SharpHoundCommon\SharpHoundCommon\src\SharpHoundRPC\Extensions.cs
Covered lines:24
Uncovered lines:11
Coverable lines:35
Total lines:70
Line coverage:68.5% (24 of 35)
Covered branches:10
Total branches:20
Branch coverage:50% (10 of 20)

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
IsError(...)0%800%
Rid(...)100%100%
GetBytes(...)100%10100%
TimeoutAfter()83.33%60100%
TimeoutAfter()83.33%60100%

File(s)

D:\a\SharpHoundCommon\SharpHoundCommon\src\SharpHoundRPC\Extensions.cs

#LineLine coverage
 1using System;
 2using System.Security.Principal;
 3using System.Threading;
 4using System.Threading.Tasks;
 5using SharpHoundRPC.NetAPINative;
 6
 7namespace SharpHoundRPC
 8{
 9    public static class Extensions
 10    {
 11        public static bool IsError(this NtStatus status)
 012        {
 013            if (status != NtStatus.StatusSuccess && status != NtStatus.StatusMoreEntries &&
 014                status != NtStatus.StatusSomeMapped && status != NtStatus.StatusNoMoreEntries)
 015                return true;
 16
 017            return false;
 018        }
 19
 20        /// <summary>
 21        ///     Gets the relative identifier for a SID
 22        /// </summary>
 23        /// <param name="securityIdentifier"></param>
 24        /// <returns></returns>
 25        public static int Rid(this SecurityIdentifier securityIdentifier)
 026        {
 027            var value = securityIdentifier.Value;
 028            var rid = int.Parse(value.Substring(value.LastIndexOf("-", StringComparison.Ordinal) + 1));
 029            return rid;
 030        }
 31
 32        public static byte[] GetBytes(this SecurityIdentifier identifier)
 133        {
 134            var bytes = new byte[identifier.BinaryLength];
 135            identifier.GetBinaryForm(bytes, 0);
 136            return bytes;
 137        }
 38
 3039        public static async Task<Result<T>> TimeoutAfter<T>(this Task<Result<T>> task, TimeSpan timeout) {
 40
 6041            using (var timeoutCancellationTokenSource = new CancellationTokenSource()) {
 42
 3043                var completedTask = await Task.WhenAny(task, Task.Delay(timeout, timeoutCancellationTokenSource.Token));
 5844                if (completedTask == task) {
 2845                    timeoutCancellationTokenSource.Cancel();
 2846                    return await task; // Very important in order to propagate exceptions
 47                }
 48
 249                var result = Result<T>.Fail("Timeout");
 250                result.IsTimeout = true;
 251                return result;
 52            }
 3053        }
 54
 1055        public static async Task<NetAPIResult<T>> TimeoutAfter<T>(this Task<NetAPIResult<T>> task, TimeSpan timeout) {
 56
 2057            using (var timeoutCancellationTokenSource = new CancellationTokenSource()) {
 58
 1059                var completedTask = await Task.WhenAny(task, Task.Delay(timeout, timeoutCancellationTokenSource.Token));
 1860                if (completedTask == task) {
 861                    timeoutCancellationTokenSource.Cancel();
 862                    return await task; // Very important in order to propagate exceptions
 63                }
 64
 265                var result = NetAPIResult<T>.Fail("Timeout");
 266                return result;
 67            }
 1068        }
 69    }
 70}