Skip to content

Commit becfdee

Browse files
microalpsMoshe Alfih
andauthored
TryParse Method Signature for ICidrGuess (#340)
* TryParse Method Signature for ICidrGuess * Fix testing and automated warnings. --------- Co-authored-by: Moshe Alfih <[email protected]>
1 parent 0ab11a9 commit becfdee

File tree

3 files changed

+100
-2
lines changed

3 files changed

+100
-2
lines changed

src/System.Net.IPNetwork/IPNetwork2.cs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -540,6 +540,55 @@ public static bool TryParse(IPAddress ipaddress, IPAddress netmask, out IPNetwor
540540
return parsed;
541541
}
542542

543+
/// <summary>
544+
/// 192.168.0.1/24
545+
/// 192.168.0.1 255.255.255.0
546+
///
547+
/// Network : 192.168.0.0
548+
/// Netmask : 255.255.255.0
549+
/// Cidr : 24
550+
/// Start : 192.168.0.1
551+
/// End : 192.168.0.254
552+
/// Broadcast : 192.168.0.255.
553+
/// </summary>
554+
/// <param name="network">A string containing an ip network to convert.</param>
555+
/// <param name="cidrGuess">A ICidrGuess implementation that will be used to guess CIDR during converion.</param>
556+
/// <param name="ipnetwork">When this method returns, contains the IPNetwork value equivalent of the ip adress contained in ipaddress with the netmask corresponding to cidr, if the conversion succeeded, or null if the conversion failed. The conversion fails if the s parameter is null or Empty, is not of the correct format, or represents an invalid ip address. This parameter is passed uninitialized; any value originally supplied in result will be overwritten.</param>
557+
/// <returns>true if network was converted successfully; otherwise, false..</returns>
558+
public static bool TryParse(string network, ICidrGuess cidrGuess, out IPNetwork2 ipnetwork)
559+
{
560+
IPNetwork2.InternalParse(true, network, cidrGuess, true, out IPNetwork2 ipnetwork2);
561+
bool parsed = ipnetwork2 != null;
562+
ipnetwork = ipnetwork2;
563+
564+
return parsed;
565+
}
566+
567+
/// <summary>
568+
/// 192.168.0.1/24
569+
/// 192.168.0.1 255.255.255.0
570+
///
571+
/// Network : 192.168.0.0
572+
/// Netmask : 255.255.255.0
573+
/// Cidr : 24
574+
/// Start : 192.168.0.1
575+
/// End : 192.168.0.254
576+
/// Broadcast : 192.168.0.255.
577+
/// </summary>
578+
/// <param name="network">A string containing an ip network to convert.</param>
579+
/// <param name="cidrGuess">A ICidrGuess implementation that will be used to guess CIDR during converion.</param>
580+
/// <param name="sanitanize">Whether to sanitize network or not.</param>
581+
/// <param name="ipnetwork">When this method returns, contains the IPNetwork value equivalent of the ip adress contained in ipaddress with the netmask corresponding to cidr, if the conversion succeeded, or null if the conversion failed. The conversion fails if the s parameter is null or Empty, is not of the correct format, or represents an invalid ip address. This parameter is passed uninitialized; any value originally supplied in result will be overwritten.</param>
582+
/// <returns>true if network was converted successfully; otherwise, false..</returns>
583+
public static bool TryParse(string network, ICidrGuess cidrGuess, bool sanitanize, out IPNetwork2 ipnetwork)
584+
{
585+
IPNetwork2.InternalParse(true, network, cidrGuess, sanitanize, out IPNetwork2 ipnetwork2);
586+
bool parsed = ipnetwork2 != null;
587+
ipnetwork = ipnetwork2;
588+
589+
return parsed;
590+
}
591+
543592
#endregion
544593

545594
#region InternalParse

src/TestProject/IPNetworkUnitTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -589,7 +589,7 @@ public void TestTryParseIPAddressNetmaskANE6()
589589
public void TestTryParseIPAddressNetmaskANE7()
590590
{
591591
IPNetwork2 ipnet = null;
592-
bool parsed = IPNetwork2.TryParse("0.0.0.0", null, out ipnet);
592+
bool parsed = IPNetwork2.TryParse("0.0.0.0", netmask: null, out ipnet);
593593

594594
Assert.AreEqual(false, parsed, "parsed");
595595
Assert.AreEqual(null, ipnet, "ipnet");

src/TestProject/TryParseUnitTest.cs

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public void TestTryParseIPAddressNetmaskANE6()
6565
[TestMethod]
6666
public void TestTryParseIPAddressNetmaskANE7()
6767
{
68-
bool parsed = IPNetwork2.TryParse("0.0.0.0", null, out IPNetwork2 ipnet);
68+
bool parsed = IPNetwork2.TryParse("0.0.0.0", netmask: null, out IPNetwork2 ipnet);
6969

7070
Assert.AreEqual(false, parsed, "parsed");
7171
Assert.AreEqual(null, ipnet, "ipnet");
@@ -317,6 +317,55 @@ public void Test_TryParse(string ipnetwork, bool sanitanize, bool parsed)
317317
Assert.AreEqual(parsed, result, "parsed1");
318318
}
319319

320+
/// <summary>
321+
/// Test IPNetwork2.TryParse method with ip addresses or networks using classfull CIDR guessing.
322+
/// </summary>
323+
/// <param name="ipnetwork">A string containing an ip address to convert.</param>
324+
/// <param name="sanitanize">Whether to sanitize network or not.</param>
325+
/// <param name="parsed">The expected parse result.</param>
326+
[DataTestMethod]
327+
[DataRow("1.1.1.1/1", true, true)]
328+
[DataRow("1.1.1.1/1", false, true)]
329+
[DataRow("::/0", true, true)]
330+
[DataRow("::/0", false, true)]
331+
[DataRow("g001:02b8::/64", true, true)]
332+
[DataRow("g001:02b8::/64", false, false)]
333+
[DataRow(" 001:02b8::/64", false, false)]
334+
[DataRow(" 001:02b8::/64", true, true)]
335+
[DataRow("001:02b8:: / 64", true, true)]
336+
[DataRow("001:02b8:: / 64", false, false)]
337+
[DataRow("001:02b8::/64", true, true)]
338+
[DataRow("001:02b8::/64", false, true)]
339+
public void Test_TryParse_ClassFull(string ipnetwork, bool sanitanize, bool parsed)
340+
{
341+
bool result = IPNetwork2.TryParse(ipnetwork, sanitanize, out IPNetwork2 ipnetwork1);
342+
bool classfullResult = IPNetwork2.TryParse(ipnetwork, CidrGuess.ClassFull, sanitanize, out IPNetwork2 ipnetwork2);
343+
344+
Assert.AreEqual(parsed, result, "parsed - class unspecified");
345+
Assert.AreEqual(parsed, classfullResult, "parsed - classfull");
346+
if (parsed)
347+
{
348+
Assert.AreEqual(ipnetwork1.Cidr, ipnetwork2.Cidr, "cidr");
349+
}
350+
}
351+
352+
/// <summary>
353+
/// Test IPNetwork2.TryParse method with plain ip addresses using classless CIDR guessing.
354+
/// </summary>
355+
/// <param name="ipaddress">A string containing an ip address to convert.</param>
356+
/// <param name="cidr">The expected CIDR netmask notation, 32 for IPv4 and 128 for IPv6.</param>
357+
[DataTestMethod]
358+
[DataRow("10.0.0.0", 32)]
359+
[DataRow("::", 128)]
360+
[DataRow("2001:0db8::", 128)]
361+
public void Test_TryParse_ClassLess(string ipaddress, int cidr)
362+
{
363+
bool parsed = IPNetwork2.TryParse(ipaddress, CidrGuess.ClassLess, out IPNetwork2 ipnetwork2);
364+
365+
Assert.IsTrue(parsed, "parsed");
366+
Assert.AreEqual(cidr, ipnetwork2.Cidr, "cidr");
367+
}
368+
320369
[DataTestMethod]
321370
[DataRow("1.1.1.1", true)]
322371
[DataRow("::", true)]

0 commit comments

Comments
 (0)