From d21756872f1b684414a1565eb4187140efca8821 Mon Sep 17 00:00:00 2001 From: lduchosal Date: Tue, 17 Mar 2020 16:27:28 +0100 Subject: [PATCH] Added sample for https://github.com/lduchosal/ipnetwork/issues/97 --- .../IPNetworkUnitTest.cs | 33 +++++++++++++++++++ src/System.Net.IPNetwork/IPNetwork.cs | 4 ++- 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/src/System.Net.IPNetwork.TestProject.Source/IPNetworkUnitTest.cs b/src/System.Net.IPNetwork.TestProject.Source/IPNetworkUnitTest.cs index 090a13d..671312d 100644 --- a/src/System.Net.IPNetwork.TestProject.Source/IPNetworkUnitTest.cs +++ b/src/System.Net.IPNetwork.TestProject.Source/IPNetworkUnitTest.cs @@ -1990,6 +1990,39 @@ public void Example9() } + [TestMethod] + public void When_TrySupernet_192_168_0_0_cidr24_add_192_168_10_0_cidr24_Then_Should_Invalid() + { + + IPNetwork network = IPNetwork.Parse("192.168.0.0/24"); + IPNetwork network2 = IPNetwork.Parse("192.168.10.0/24"); + + bool supernetted = network.TrySupernet(network2, out var ipnetwork); + Assert.AreEqual(false, supernetted); + + + } + + [TestMethod] + public void When_TryWideSubnet_192_168_0_0_cidr24_add_192_168_10_0_cidr24_Then_Should_Invalid() + { + + IPNetwork network = IPNetwork.Parse("192.168.0.0/24"); + IPNetwork network2 = IPNetwork.Parse("192.168.10.0/24"); + + bool wideSubnetted = IPNetwork.TryWideSubnet(new[] { network, network2 }, out IPNetwork ipnetwork); + Assert.AreEqual(true, wideSubnetted); + Assert.AreEqual("192.168.0.0/20", ipnetwork.ToString()); + + Console.WriteLine("Network : {0}", ipnetwork.Network); + Console.WriteLine("Netmask : {0}", ipnetwork.Netmask); + Console.WriteLine("Broadcast : {0}", ipnetwork.Broadcast); + Console.WriteLine("FirstUsable : {0}", ipnetwork.FirstUsable); + Console.WriteLine("LastUsable : {0}", ipnetwork.LastUsable); + Console.WriteLine("Usable : {0}", ipnetwork.Usable); + Console.WriteLine("Cidr : {0}", ipnetwork.Cidr); + + } [TestMethod] public void Example10() { diff --git a/src/System.Net.IPNetwork/IPNetwork.cs b/src/System.Net.IPNetwork/IPNetwork.cs index 9e2b29b..b7c31f6 100644 --- a/src/System.Net.IPNetwork/IPNetwork.cs +++ b/src/System.Net.IPNetwork/IPNetwork.cs @@ -1690,12 +1690,14 @@ public static bool TryWideSubnet(IPNetwork[] ipnetworks, out IPNetwork ipnetwork return true; } - public static IPNetwork WideSubnet(IPNetwork[] ipnetworks) { + public static IPNetwork WideSubnet(IPNetwork[] ipnetworks) + { IPNetwork ipn = null; IPNetwork.InternalWideSubnet(false, ipnetworks, out ipn); return ipn; } + internal static void InternalWideSubnet(bool tryWide, IPNetwork[] ipnetworks, out IPNetwork ipnetwork) { if (ipnetworks == null) {