< Summary - Combined Code Coverage

Information
Class: NLightning.Infrastructure.Transport.Services.HandshakeService
Assembly: NLightning.Infrastructure
File(s): /home/runner/work/nlightning/nlightning/src/NLightning.Infrastructure/Transport/Services/HandshakeService.cs
Tag: 36_15743069263
Line coverage
75%
Covered lines: 30
Uncovered lines: 10
Coverable lines: 40
Total lines: 145
Line coverage: 75%
Branch coverage
64%
Covered branches: 9
Total branches: 14
Branch coverage: 64.2%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)25%4.73464.29%
get_IsInitiator()100%11100%
get_RemoteStaticPublicKey()100%210%
PerformStep(...)100%88100%
InitiatorWriteActOne(...)100%11100%
InitiatorReadActTwoAndWriteActThree(...)100%11100%
ResponderReadActOneAndWriteActTwo(...)100%11100%
ResponderReadActThree(...)100%11100%
Dispose()0%620%

File(s)

/home/runner/work/nlightning/nlightning/src/NLightning.Infrastructure/Transport/Services/HandshakeService.cs

#LineLine coverage
 1namespace NLightning.Infrastructure.Transport.Services;
 2
 3using Domain.Crypto.ValueObjects;
 4using Domain.Transport;
 5using Domain.Utils;
 6using Handshake.States;
 7using Infrastructure.Crypto.Interfaces;
 8using Interfaces;
 9using Protocol.Constants;
 10
 11/// <summary>
 12/// Initializes a new instance of the <see cref="HandshakeService"/> class.
 13/// </summary>
 14internal sealed class HandshakeService : IHandshakeService
 15{
 16    private readonly IHandshakeState _handshakeState;
 17
 2418    private byte _steps = 2;
 19    private bool _disposed;
 20
 21    /// <inheritdoc/>
 4022    public bool IsInitiator { get; }
 23
 024    public CompactPubKey? RemoteStaticPublicKey => _handshakeState.RemoteStaticPublicKey;
 25
 2426    public HandshakeService(bool isInitiator, ReadOnlySpan<byte> localStaticPrivateKey,
 2427                            ReadOnlySpan<byte> staticPublicKey, IHandshakeState? handshakeState = null,
 2428                            IEcdh? dh = null)
 29    {
 2430        if (handshakeState is null)
 31        {
 032            if (dh is null)
 033                throw new ArgumentNullException(nameof(dh),
 034                                                "Either a HandshakeState or Ecdh must be provided");
 35
 036            _handshakeState = new HandshakeState(isInitiator, localStaticPrivateKey, staticPublicKey, dh);
 37        }
 38        else
 39        {
 2440            _handshakeState = handshakeState;
 41        }
 42
 2443        IsInitiator = isInitiator;
 2444    }
 45
 46    /// <inheritdoc/>
 47    /// <exception cref="InvalidOperationException">Thrown when there are no more steps to complete</exception>
 48    public int PerformStep(ReadOnlySpan<byte> inMessage, Span<byte> outMessage, out ITransport? transport)
 49    {
 4850        ExceptionUtils.ThrowIfDisposed(_disposed, nameof(HandshakeService));
 51
 4852        switch (_steps)
 53        {
 54            case 2:
 2455                _steps--;
 2456                transport = null;
 2457                return IsInitiator
 2458                    ? InitiatorWriteActOne(outMessage)
 2459                    : ResponderReadActOneAndWriteActTwo(inMessage, outMessage);
 60            case 1:
 1661                _steps--;
 1662                return IsInitiator
 1663                    ? InitiatorReadActTwoAndWriteActThree(inMessage, outMessage, out transport)
 1664                    : ResponderReadActThree(inMessage, out transport);
 65            default:
 866                throw new InvalidOperationException("There's no more steps to complete");
 67        }
 68    }
 69
 70    #region Initiator Methods
 71    /// <summary>
 72    /// Initiator writes act one
 73    /// </summary>
 74    /// <param name="outMessage">The buffer to write the message to</param>
 75    /// <returns>Number of bytes written to outMessage</returns>
 76    private int InitiatorWriteActOne(Span<byte> outMessage)
 77    {
 78        // Write act one
 1279        return _handshakeState.WriteMessage(ProtocolConstants.EmptyMessage, outMessage).Item1;
 80    }
 81
 82    /// <summary>
 83    /// Initiator reads act two and writes act three
 84    /// </summary>
 85    /// <param name="actTwoMessage">Byte[] representation of Act Two Message</param>
 86    /// <param name="outMessage">The buffer to write the message to</param>
 87    /// <param name="transport"> The Transport that is going to be returned by the handshake after all steps has been co
 88    /// <returns>Number of bytes written to outMessage</returns>
 89    private int InitiatorReadActTwoAndWriteActThree(ReadOnlySpan<byte> actTwoMessage, Span<byte> outMessage,
 90                                                    out ITransport? transport)
 91    {
 92        // Read act two
 893        _ = _handshakeState.ReadMessage(actTwoMessage, outMessage);
 94
 95        // Write act three
 896        (var messageSize, _, transport) = _handshakeState.WriteMessage(ProtocolConstants.EmptyMessage, outMessage);
 97
 898        return messageSize;
 99    }
 100    #endregion
 101
 102    #region Responder Methods
 103    /// <summary>
 104    /// Responder reads act one and writes act two
 105    /// </summary>
 106    /// <param name="actOneMessage">Byte[] representation of Act One Message</param>
 107    /// <param name="outMessage">The buffer to write the message to</param>
 108    /// <returns>Number of bytes written to outMessage</returns>
 109    private int ResponderReadActOneAndWriteActTwo(ReadOnlySpan<byte> actOneMessage, Span<byte> outMessage)
 110    {
 111        // Read act one
 12112        var (responderMessageSize, _, _) = _handshakeState.ReadMessage(actOneMessage, outMessage);
 113
 114        // Write act two
 12115        return _handshakeState.WriteMessage(outMessage[..responderMessageSize], outMessage).Item1;
 116    }
 117
 118    /// <summary>
 119    /// Responder reads act three
 120    /// </summary>
 121    /// <param name="actThreeMessage">Byte[] representation of Act Three Message</param>
 122    /// <param name="transport"> The Transport that is going to be returned by the handshake after all steps has been co
 123    /// <returns>Number of bytes read from actThreeMessage</returns>
 124    private int ResponderReadActThree(ReadOnlySpan<byte> actThreeMessage, out ITransport? transport)
 125    {
 126        // Read act three
 8127        var messageBuffer = new byte[ProtocolConstants.MaxMessageLength];
 8128        (var messageSize, _, transport) = _handshakeState.ReadMessage(actThreeMessage, messageBuffer);
 129
 8130        return messageSize;
 131    }
 132    #endregion
 133
 134    public void Dispose()
 135    {
 0136        if (_disposed)
 137        {
 0138            return;
 139        }
 140
 0141        _handshakeState.Dispose();
 142
 0143        _disposed = true;
 0144    }
 145}