diff --git a/appveyor.yml b/appveyor.yml index 8d46c2b..f057fc9 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -1,4 +1,4 @@ -version: '2.4.0.{build}' +version: '2.5.{build}' image: Visual Studio 2019 assembly_info: diff --git a/src/System.Net.IPNetwork.TestProject.Source/CidrClassFullUnitTest.cs b/src/System.Net.IPNetwork.TestProject.Source/CidrClassFullUnitTest.cs index 6322a7a..b01c28d 100644 --- a/src/System.Net.IPNetwork.TestProject.Source/CidrClassFullUnitTest.cs +++ b/src/System.Net.IPNetwork.TestProject.Source/CidrClassFullUnitTest.cs @@ -66,7 +66,8 @@ public void TestTryGuessCidrD() byte cidr; bool parsed = cidrguess.TryGuessCidr("224.0.0.0", out cidr); - Assert.AreEqual(false, parsed, "parsed"); + Assert.AreEqual(true, parsed, "parsed"); + Assert.AreEqual(24, cidr, "cidr"); } [TestMethod] @@ -77,7 +78,8 @@ public void TestTryGuessCidrE() byte cidr; bool parsed = cidrguess.TryGuessCidr("240.0.0.0", out cidr); - Assert.AreEqual(false, parsed, "parsed"); + Assert.AreEqual(true, parsed, "parsed"); + Assert.AreEqual(24, cidr, "cidr"); } #endregion diff --git a/src/System.Net.IPNetwork.TestProject.Source/IPNetworkUnitTest.cs b/src/System.Net.IPNetwork.TestProject.Source/IPNetworkUnitTest.cs index 671312d..a15e95c 100644 --- a/src/System.Net.IPNetwork.TestProject.Source/IPNetworkUnitTest.cs +++ b/src/System.Net.IPNetwork.TestProject.Source/IPNetworkUnitTest.cs @@ -226,7 +226,8 @@ public void TestParseString5() { [TestMethod] - public void TestParseIPAddressNoNetmask1() { + public void TestParseIPAddressNoNetmask1() + { string ipaddress = "10.0.0.0"; @@ -249,6 +250,32 @@ public void TestParseIPAddressNoNetmask1() { } + [TestMethod] + public void When_Parse_254_128_0_0_Should_Succeed () + { + + string ipaddress = "254.128.0.0"; + + string network = "254.128.0.0"; + string netmask = "255.255.255.0"; + string broadcast = "254.128.0.255"; + string firstUsable = "254.128.0.1"; + string lastUsable = "254.128.0.254"; + byte cidr = 24; + uint usable = 254; + + IPNetwork ipnetwork = IPNetwork.Parse(ipaddress); + Assert.AreEqual(network, ipnetwork.Network.ToString(), "Network"); + Assert.AreEqual(netmask, ipnetwork.Netmask.ToString(), "Netmask"); + Assert.AreEqual(broadcast, ipnetwork.Broadcast.ToString(), "Broadcast"); + Assert.AreEqual(cidr, ipnetwork.Cidr, "Cidr"); + Assert.AreEqual(usable, ipnetwork.Usable, "Usable"); + Assert.AreEqual(firstUsable, ipnetwork.FirstUsable.ToString(), "FirstUsable"); + Assert.AreEqual(lastUsable, ipnetwork.LastUsable.ToString(), "LastUsable"); + } + + + [TestMethod] public void TestParseIPAddressNoNetmask2() { @@ -451,20 +478,21 @@ public void TestParseIPAddressNoNetmask3_ClassLess() { [TestMethod] - [ExpectedException(typeof(ArgumentException))] public void TestParseIPAddressNoNetmask4() { string ipaddress = "224.0.0.0"; IPNetwork ipnetwork = IPNetwork.Parse(ipaddress); + Assert.AreEqual("224.0.0.0/24", ipnetwork.ToString(), "Network"); + } [TestMethod] - [ExpectedException(typeof(ArgumentException))] public void TestParseIPAddressNoNetmask5() { string ipaddress = "240.0.0.0"; IPNetwork ipnetwork = IPNetwork.Parse(ipaddress); + Assert.AreEqual("240.0.0.0/24", ipnetwork.ToString(), "Network"); } @@ -3455,7 +3483,8 @@ public void TestTryGuessCidrD() byte cidr; bool parsed = IPNetwork.TryGuessCidr("224.0.0.0", out cidr); - Assert.AreEqual(false, parsed, "parsed"); + Assert.AreEqual(true, parsed, "parsed"); + Assert.AreEqual(24, cidr, "cidr"); } [TestMethod] public void TestTryGuessCidrE() @@ -3464,7 +3493,8 @@ public void TestTryGuessCidrE() byte cidr; bool parsed = IPNetwork.TryGuessCidr("240.0.0.0", out cidr); - Assert.AreEqual(false, parsed, "parsed"); + Assert.AreEqual(true, parsed, "parsed"); + Assert.AreEqual(24, cidr, "cidr"); } #endregion diff --git a/src/System.Net.IPNetwork/CidrClassFull.cs b/src/System.Net.IPNetwork/CidrClassFull.cs index a2248d3..db18345 100644 --- a/src/System.Net.IPNetwork/CidrClassFull.cs +++ b/src/System.Net.IPNetwork/CidrClassFull.cs @@ -38,18 +38,18 @@ public bool TryGuessCidr(string ip, out byte cidr) return true; } BigInteger uintIPAddress = IPNetwork.ToBigInteger(ipaddress); - uintIPAddress = uintIPAddress >> 29; - if (uintIPAddress <= 3) + uintIPAddress = uintIPAddress >> 30; + if (uintIPAddress <= 1) { cidr = 8; return true; } - else if (uintIPAddress <= 5) + else if (uintIPAddress <= 2) { cidr = 16; return true; } - else if (uintIPAddress <= 6) + else if (uintIPAddress <= 3) { cidr = 24; return true;