Skip to content

Commit

Permalink
Fix cidrguess to parse on any ipv4 network.
Browse files Browse the repository at this point in the history
Upgraded to 2.5 because this my be a break backward compatibility.
  • Loading branch information
lduchosal committed Mar 17, 2020
1 parent d217568 commit b016502
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 12 deletions.
2 changes: 1 addition & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version: '2.4.0.{build}'
version: '2.5.{build}'
image: Visual Studio 2019

assembly_info:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand All @@ -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
Expand Down
40 changes: 35 additions & 5 deletions src/System.Net.IPNetwork.TestProject.Source/IPNetworkUnitTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,8 @@ public void TestParseString5() {


[TestMethod]
public void TestParseIPAddressNoNetmask1() {
public void TestParseIPAddressNoNetmask1()
{

string ipaddress = "10.0.0.0";

Expand All @@ -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() {
Expand Down Expand Up @@ -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");

}

Expand Down Expand Up @@ -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()
Expand All @@ -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
Expand Down
8 changes: 4 additions & 4 deletions src/System.Net.IPNetwork/CidrClassFull.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit b016502

Please sign in to comment.