< Summary - Combined Code Coverage

Information
Class: NLightning.Infrastructure.Crypto.Primitives.SecureMemory
Assembly: NLightning.Infrastructure
File(s): /home/runner/work/nlightning/nlightning/src/NLightning.Infrastructure/Crypto/Primitives/SecureMemory.cs
Tag: 30_15166811759
Line coverage
88%
Covered lines: 23
Uncovered lines: 3
Coverable lines: 26
Total lines: 67
Line coverage: 88.4%
Branch coverage
33%
Covered branches: 2
Total branches: 6
Branch coverage: 33.3%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
get_Length()100%11100%
.ctor(...)100%11100%
op_Implicit(...)100%11100%
op_Implicit(...)100%11100%
Equals(...)0%2040%
GetHashCode()100%210%
ReleaseUnmanagedResources()100%11100%
Dispose(...)100%22100%
Dispose()100%11100%
Finalize()100%11100%

File(s)

/home/runner/work/nlightning/nlightning/src/NLightning.Infrastructure/Crypto/Primitives/SecureMemory.cs

#LineLine coverage
 1namespace NLightning.Infrastructure.Crypto.Primitives;
 2
 3using Factories;
 4using Interfaces;
 5
 6public sealed unsafe class SecureMemory : IDisposable
 7{
 8    private readonly ICryptoProvider _cryptoProvider;
 9    private readonly void* _pointer;
 10
 1884811    public int Length { get; }
 12
 45213    public SecureMemory(int size)
 14    {
 45215        _cryptoProvider = CryptoFactory.GetCryptoProvider();
 45216        Length = size;
 45217        _pointer = _cryptoProvider.MemoryAlloc((ulong)size).ToPointer();
 18
 45219        _cryptoProvider.MemoryLock(new IntPtr(_pointer), (ulong)Length);
 45220    }
 21
 22    #region Implicit Conversions
 90023    public static implicit operator Span<byte>(SecureMemory secureMemory) => new(secureMemory._pointer, secureMemory.Len
 1678024    public static implicit operator ReadOnlySpan<byte>(SecureMemory secureMemory) => new(secureMemory._pointer, secureMe
 25    #endregion
 26
 27    public override bool Equals(object? obj)
 28    {
 029        if (obj is not SecureMemory castObj) return false;
 30
 031        return castObj.Length == Length && castObj._pointer == _pointer;
 32    }
 33    public override int GetHashCode()
 34    {
 035        return Length ^ (int)new IntPtr(_pointer);
 36    }
 37
 38    #region Dispose Pattern
 39    private void ReleaseUnmanagedResources()
 40    {
 35641        var pointerInt = new IntPtr(_pointer);
 35642        _cryptoProvider.MemoryZero(pointerInt, (ulong)Length);
 35643        _cryptoProvider.MemoryUnlock(pointerInt, (ulong)Length);
 35644        _cryptoProvider.MemoryFree(pointerInt);
 35645    }
 46
 47    private void Dispose(bool disposing)
 48    {
 35649        ReleaseUnmanagedResources();
 35650        if (disposing)
 51        {
 35252            _cryptoProvider.Dispose();
 53        }
 35654    }
 55
 56    public void Dispose()
 57    {
 35258        Dispose(true);
 35259        GC.SuppressFinalize(this);
 35260    }
 61
 62    ~SecureMemory()
 63    {
 464        Dispose(false);
 865    }
 66    #endregion
 67}