CidrParsingTests.cs 1.3 KB

1234567891011121314151617181920212223242526272829303132333435
  1. using RackPeek.Domain.Resources.Services.Networking;
  2. namespace Tests.Discovery;
  3. /// <summary>
  4. /// CIDR parsing feeds the sweep its targets, so leniency here means probing a
  5. /// network the user never named: unchecked octet arithmetic used to fold
  6. /// 192.168.256.0 into 192.169.0.0 and call it usable.
  7. /// </summary>
  8. public class CidrParsingTests {
  9. [Theory]
  10. [InlineData("192.168.1.0/24", "192.168.1.0/24")]
  11. [InlineData("192.168.1.37/24", "192.168.1.0/24")] // a host address masks down
  12. [InlineData("10.0.0.0/8", "10.0.0.0/8")]
  13. [InlineData("127.0.0.1/32", "127.0.0.1/32")]
  14. public void Valid_blocks_parse_and_mask_to_their_network(string input, string expected) {
  15. Assert.True(Cidr.TryParse(input, out Cidr cidr));
  16. Assert.Equal(expected, cidr.ToString());
  17. }
  18. [Theory]
  19. [InlineData(null)]
  20. [InlineData("")]
  21. [InlineData("not-a-cidr")]
  22. [InlineData("192.168.1.0")] // no prefix
  23. [InlineData("192.168.1.0/24/7")]
  24. [InlineData("192.168.1.0/notanumber")]
  25. [InlineData("192.168.1.0/33")]
  26. [InlineData("192.168.256.0/24")] // octet overflow must not wrap into .169
  27. [InlineData("192.-1.1.0/24")]
  28. [InlineData("300.1.1.1/24")]
  29. [InlineData("1.2.3/24")]
  30. public void Anything_else_is_refused_rather_than_reinterpreted(string? input) =>
  31. Assert.False(Cidr.TryParse(input, out _));
  32. }