Skip to content

Commit

Permalink
Fix bug where Extract with an "Any" network would throw. Also fix an …
Browse files Browse the repository at this point in the history
…incorrect exception being thrown on AddressFamily mismatch.
  • Loading branch information
RobThree committed Mar 29, 2024
1 parent 7743397 commit 9d4e2a8
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
22 changes: 21 additions & 1 deletion IPNetworkHelper.Tests/NetworkHelperTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public void SplitThrowsOnUnsplittableNetworkIPv6()


[TestMethod]
[ExpectedException(typeof(IPNetworkNotInIPNetworkException))]
[ExpectedException(typeof(AddressFamilyMismatchException))]
public void ExtractThrowsOnAddressFamilyMismatch()
{
var ipv4 = IPNetwork.Parse("192.168.0.0/24");
Expand Down Expand Up @@ -143,6 +143,16 @@ public void ExtractThrowsOnDifferentNetwork()
network.Extract(different).ToArray();
}

[TestMethod]
public void ExtractIPv4Prefix()
{
var network = IPNetwork.Parse("192.168.0.0/16");

var result = network.Extract(20).ToArray();

Assert.IsTrue(result.Length == 5);
}

[TestMethod]
public void ExtractIPv4()
{
Expand All @@ -167,6 +177,16 @@ public void ExtractSelfReturnsSelfIPv4()
Assert.IsTrue(result.Select((n, i) => expected[i].Equals(n)).All(v => true));
}

[TestMethod]
public void ExtractIPv6Prefix()
{
var network = IPNetwork.Parse("1111:2222::/32");

var result = network.Extract(64).ToArray();

Assert.IsTrue(result.Length == 33);
}

[TestMethod]
public void ExtractIPv6()
{
Expand Down
2 changes: 1 addition & 1 deletion IPNetworkHelper/NetworkHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public static IEnumerable<IPNetwork> Extract(this IPNetwork network, int prefixL
/// <param name="desiredNetwork">The subnet to extract from the network.</param>
/// <returns>Returns all subnets after taking the desired subnet, including the desired subnet.</returns>
public static IEnumerable<IPNetwork> Extract(this IPNetwork network, IPNetwork desiredNetwork)
=> Extract(network, [desiredNetwork]);
=> ExtractImpl(network, desiredNetwork).OrderBy(i => i, IPNetworkComparer.Default);

/// <summary>
/// Extracts the given subnets from the network and returns all subnets after taking the desired subnet, including the desired subnets.
Expand Down

0 comments on commit 9d4e2a8

Please sign in to comment.