< Summary - Combined Code Coverage

Line coverage
72%
Covered lines: 26
Uncovered lines: 10
Coverable lines: 36
Total lines: 162
Line coverage: 72.2%
Branch coverage
50%
Covered branches: 3
Total branches: 6
Branch coverage: 50%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
File 1: OnlyDigitsRegex()100%11100%
File 2: OnlyDigitsRegex()100%11100%
File 3: get_PubKey()100%11100%
File 3: get_Host()100%11100%
File 3: get_Port()100%11100%
File 3: .ctor(...)50%4.47469.23%
File 3: .ctor(...)100%210%
File 3: .ctor(...)50%2.26260%
File 3: .ctor(...)100%11100%
File 3: ToString()100%11100%

File(s)

/home/runner/work/nlightning/nlightning/src/NLightning.Infrastructure/obj/Release.Native/net9.0/System.Text.RegularExpressions.Generator/System.Text.RegularExpressions.Generator.RegexGenerator/RegexGenerator.g.cs

File '/home/runner/work/nlightning/nlightning/src/NLightning.Infrastructure/obj/Release.Native/net9.0/System.Text.RegularExpressions.Generator/System.Text.RegularExpressions.Generator.RegexGenerator/RegexGenerator.g.cs' does not exist (any more).

/home/runner/work/nlightning/nlightning/src/NLightning.Infrastructure/obj/Release/net9.0/System.Text.RegularExpressions.Generator/System.Text.RegularExpressions.Generator.RegexGenerator/RegexGenerator.g.cs

File '/home/runner/work/nlightning/nlightning/src/NLightning.Infrastructure/obj/Release/net9.0/System.Text.RegularExpressions.Generator/System.Text.RegularExpressions.Generator.RegexGenerator/RegexGenerator.g.cs' does not exist (any more).

/home/runner/work/nlightning/nlightning/src/NLightning.Infrastructure/Protocol/Models/PeerAddress.cs

#LineLine coverage
 1using System.Net;
 2using System.Text.RegularExpressions;
 3
 4namespace NLightning.Infrastructure.Protocol.Models;
 5
 6using NLightning.Domain.Crypto.ValueObjects;
 7using NLightning.Domain.Node.ValueObjects;
 8
 9/// <summary>
 10/// Represents a peer address.
 11/// </summary>
 12public sealed partial class PeerAddress
 13{
 14    [GeneratedRegex(@"\d+")]
 15    private static partial Regex OnlyDigitsRegex();
 16
 17    /// <summary>
 18    /// Gets the public key.
 19    /// </summary>
 1620    public CompactPubKey PubKey { get; }
 21
 22    /// <summary>
 23    /// Gets the host.
 24    /// </summary>
 2025    public IPAddress Host { get; }
 26
 27    /// <summary>
 28    /// Gets the port.
 29    /// </summary>
 1630    public int Port { get; }
 31
 32    /// <summary>
 33    /// Initializes a new instance of the <see cref="PeerAddress"/> class.
 34    /// </summary>
 35    /// <param name="address">The address.</param>
 36    /// <remarks>
 37    /// The address is in the format of "pubkey@host:port".
 38    /// </remarks>
 439    public PeerAddress(string address)
 40    {
 441        var parts = address.Split('@');
 442        if (parts.Length != 2)
 043            throw new FormatException("Invalid address format, should be pubkey@host:port");
 44
 445        PubKey = new CompactPubKey(Convert.FromHexString(parts[0]));
 46
 47        // Check if the address starts with http
 448        if (parts[1].StartsWith("http"))
 49        {
 50            // split on first // to get the address
 051            var hostPort = parts[1].Split("//")[1].Split(":");
 052            Host = Dns.GetHostAddresses(hostPort[0])[0];
 53
 54            // Port may have an extra / at the end. Use regex to keep only the number in the port
 055            Port = int.Parse(OnlyDigitsRegex().Match(hostPort[1]).Value);
 56        }
 57        else
 58        {
 459            var hostPort = parts[1].Split(':');
 460            Host = IPAddress.Parse(hostPort[0]);
 461            Port = int.Parse(hostPort[1]);
 62        }
 463    }
 64
 065    public PeerAddress(PeerAddressInfo peerAddressInfo) : this(peerAddressInfo.Address)
 66    {
 067    }
 68
 69    /// <summary>
 70    /// Initializes a new instance of the <see cref="PeerAddress"/> class.
 71    /// </summary>
 72    /// <param name="pubKey">The public key.</param>
 73    /// <param name="address">The address.</param>
 74    /// <remarks>
 75    /// The address is in the format of "http://host:port" or "host:port".
 76    /// </remarks>
 477    public PeerAddress(CompactPubKey pubKey, string address)
 78    {
 479        PubKey = pubKey;
 80
 81        // Check if the address starts with http
 482        if (address.StartsWith("http"))
 83        {
 84            // split on first // to get the address
 485            var host = address.Split("//")[1];
 486            Host = Dns.GetHostAddresses(host.Split(":")[0])[0];
 87
 88            // Port may have an extra / at the end. Use regex to keep only the number in the port
 489            Port = int.Parse(OnlyDigitsRegex().Match(host.Split(":")[1]).Value);
 90        }
 91        else
 92        {
 093            var hostPort = address.Split(':');
 094            Host = IPAddress.Parse(hostPort[0]);
 095            Port = int.Parse(hostPort[1]);
 96        }
 097    }
 98
 99    /// <summary>
 100    /// Initializes a new instance of the <see cref="PeerAddress"/> class.
 101    /// </summary>
 102    /// <param name="pubKey">The public key.</param>
 103    /// <param name="host">The host.</param>
 104    /// <param name="port">The port.</param>
 8105    public PeerAddress(CompactPubKey pubKey, string host, int port)
 106    {
 8107        PubKey = pubKey;
 8108        Host = IPAddress.Parse(host);
 8109        Port = port;
 8110    }
 111
 112    /// <summary>
 113    /// Returns a string that represents the address.
 114    /// </summary>
 115    /// <returns>A string in the format of "pubkey@host:port".</returns>
 116    public override string ToString()
 117    {
 4118        return $"{PubKey}@{Host}:{Port}";
 119    }
 120}