NetworkScanTargetTests.cs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. using RackPeek.Domain.Discovery;
  2. using RackPeek.Domain.Resources.Services.Networking;
  3. namespace Tests.Discovery;
  4. /// <summary>
  5. /// Which addresses a block actually sweeps. Getting the edges wrong either wastes
  6. /// probes on the network/broadcast addresses or — worse — skips real hosts on the
  7. /// point-to-point prefixes where every address is a host.
  8. /// </summary>
  9. public class NetworkScanTargetTests {
  10. private static List<string> Targets(string cidr) =>
  11. NetworkScanner.EnumerateTargets(Cidr.Parse(cidr)).ToList();
  12. [Fact]
  13. public void A_24_sweeps_the_254_host_addresses() {
  14. List<string> targets = Targets("192.168.1.0/24");
  15. Assert.Equal(254, targets.Count);
  16. Assert.Equal("192.168.1.1", targets.First());
  17. Assert.Equal("192.168.1.254", targets.Last());
  18. Assert.DoesNotContain("192.168.1.0", targets);
  19. Assert.DoesNotContain("192.168.1.255", targets);
  20. }
  21. [Fact]
  22. public void A_30_has_two_hosts_between_network_and_broadcast() =>
  23. Assert.Equal(["10.0.0.1", "10.0.0.2"], Targets("10.0.0.0/30"));
  24. [Fact]
  25. public void A_31_is_point_to_point_where_both_addresses_are_hosts() =>
  26. // RFC 3021: /31 has no network or broadcast address.
  27. Assert.Equal(["10.0.0.0", "10.0.0.1"], Targets("10.0.0.0/31"));
  28. [Fact]
  29. public void A_32_is_exactly_the_one_address() =>
  30. Assert.Equal(["127.0.0.1"], Targets("127.0.0.1/32"));
  31. [Fact]
  32. public void A_16_sweeps_the_full_65534_hosts() =>
  33. Assert.Equal(65_534, Targets("10.20.0.0/16").Count);
  34. [Fact]
  35. public void A_block_at_the_top_of_the_address_space_does_not_wrap() {
  36. List<string> targets = Targets("255.255.255.252/30");
  37. Assert.Equal(["255.255.255.253", "255.255.255.254"], targets);
  38. }
  39. [Fact]
  40. public void The_offered_ip_need_not_be_the_network_address() =>
  41. // People type their own address plus a prefix; Cidr.Parse masks it down.
  42. Assert.Equal(254, Targets("192.168.1.37/24").Count);
  43. }