|
| 1 | +package core |
| 2 | + |
| 3 | +import ( |
| 4 | + "net" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/stretchr/testify/assert" |
| 8 | +) |
| 9 | + |
| 10 | +func TestAddressCount(t *testing.T) { |
| 11 | + IPv4CIDR, err := ParseCIDR("10.0.0.0/16") |
| 12 | + if err != nil { |
| 13 | + t.Log(err) |
| 14 | + t.Fail() |
| 15 | + } |
| 16 | + |
| 17 | + IPv6CIDR, err := ParseCIDR("2001:db8:1234:1a00::/106") |
| 18 | + if err != nil { |
| 19 | + t.Log(err) |
| 20 | + t.Fail() |
| 21 | + } |
| 22 | + |
| 23 | + largeIPv4PrefixCIDR, err := ParseCIDR("172.16.18.0/31") |
| 24 | + if err != nil { |
| 25 | + t.Log(err) |
| 26 | + t.Fail() |
| 27 | + } |
| 28 | + |
| 29 | + largestIPv4PrefixCIDR, err := ParseCIDR("172.16.18.0/32") |
| 30 | + if err != nil { |
| 31 | + t.Log(err) |
| 32 | + t.Fail() |
| 33 | + } |
| 34 | + |
| 35 | + tests := []struct { |
| 36 | + name string |
| 37 | + cidr *net.IPNet |
| 38 | + expectedCount uint64 |
| 39 | + }{ |
| 40 | + { |
| 41 | + name: "Return the count of all distinct host addresses in a common IPv4 CIDR", |
| 42 | + cidr: IPv4CIDR, |
| 43 | + expectedCount: 65534, |
| 44 | + }, |
| 45 | + { |
| 46 | + name: "Return the count of all distinct host addresses in a common IPv6 CIDR", |
| 47 | + cidr: IPv6CIDR, |
| 48 | + expectedCount: 4194302, |
| 49 | + }, |
| 50 | + { |
| 51 | + name: "Return the count of all distinct host addresses in an uncommon (large prefix) IPv4 CIDR", |
| 52 | + cidr: largeIPv4PrefixCIDR, |
| 53 | + expectedCount: 2, |
| 54 | + }, |
| 55 | + { |
| 56 | + name: "Return the count of all distinct host addresses in an uncommon (largest prefix) IPv4 CIDR", |
| 57 | + cidr: largestIPv4PrefixCIDR, |
| 58 | + expectedCount: 1, |
| 59 | + }, |
| 60 | + } |
| 61 | + for _, tt := range tests { |
| 62 | + t.Run(tt.name, func(t *testing.T) { |
| 63 | + count := AddressCount(tt.cidr) |
| 64 | + assert.Equal(t, int(tt.expectedCount), int(count), "Both address counts should be equal") |
| 65 | + }) |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +func TestOverlaps(t *testing.T) { |
| 70 | + firstIPv4CIDR, err := ParseCIDR("10.0.0.0/16") |
| 71 | + if err != nil { |
| 72 | + t.Log(err) |
| 73 | + t.Fail() |
| 74 | + } |
| 75 | + |
| 76 | + secondIPv4CIDR, err := ParseCIDR("10.0.14.0/22") |
| 77 | + if err != nil { |
| 78 | + t.Log(err) |
| 79 | + t.Fail() |
| 80 | + } |
| 81 | + |
| 82 | + thirdIPv4CIDR, err := ParseCIDR("10.1.0.0/28") |
| 83 | + if err != nil { |
| 84 | + t.Log(err) |
| 85 | + t.Fail() |
| 86 | + } |
| 87 | + |
| 88 | + firstIPv6CIDR, err := ParseCIDR("2001:db8:1111:2222:1::/80") |
| 89 | + if err != nil { |
| 90 | + t.Log(err) |
| 91 | + t.Fail() |
| 92 | + } |
| 93 | + |
| 94 | + secondIPv6CIDR, err := ParseCIDR("2001:db8:1111:2222:1:1::/96") |
| 95 | + if err != nil { |
| 96 | + t.Log(err) |
| 97 | + t.Fail() |
| 98 | + } |
| 99 | + |
| 100 | + tests := []struct { |
| 101 | + name string |
| 102 | + cidrA *net.IPNet |
| 103 | + cidrB *net.IPNet |
| 104 | + overlaps bool |
| 105 | + }{ |
| 106 | + { |
| 107 | + name: "2 IPv4 CIDR ranges should overlap", |
| 108 | + cidrA: firstIPv4CIDR, |
| 109 | + cidrB: secondIPv4CIDR, |
| 110 | + overlaps: true, |
| 111 | + }, |
| 112 | + { |
| 113 | + name: "2 IPv4 CIDR ranges should NOT overlap", |
| 114 | + cidrA: firstIPv4CIDR, |
| 115 | + cidrB: thirdIPv4CIDR, |
| 116 | + overlaps: false, |
| 117 | + }, |
| 118 | + { |
| 119 | + name: "2 IPv6 CIDR ranges should overlap", |
| 120 | + cidrA: firstIPv6CIDR, |
| 121 | + cidrB: secondIPv6CIDR, |
| 122 | + overlaps: true, |
| 123 | + }, |
| 124 | + } |
| 125 | + for _, tt := range tests { |
| 126 | + t.Run(tt.name, func(t *testing.T) { |
| 127 | + overlaps := Overlaps(tt.cidrA, tt.cidrB) |
| 128 | + assert.Equal(t, tt.overlaps, overlaps, "Given CIDRs should overlap") |
| 129 | + }) |
| 130 | + } |
| 131 | +} |
| 132 | + |
| 133 | +func TestContainsAddress(t *testing.T) { |
| 134 | + IPv4CIDR, err := ParseCIDR("10.0.0.0/16") |
| 135 | + if err != nil { |
| 136 | + t.Log(err) |
| 137 | + t.Fail() |
| 138 | + } |
| 139 | + |
| 140 | + IPv6CIDR, err := ParseCIDR("2001:db8:1234:1a00::/106") |
| 141 | + if err != nil { |
| 142 | + t.Log(err) |
| 143 | + t.Fail() |
| 144 | + } |
| 145 | + |
| 146 | + tests := []struct { |
| 147 | + name string |
| 148 | + cidr *net.IPNet |
| 149 | + ip net.IP |
| 150 | + contains bool |
| 151 | + }{ |
| 152 | + { |
| 153 | + name: "IPv4 CIDR that does contain an IPv4 IP", |
| 154 | + cidr: IPv4CIDR, |
| 155 | + ip: net.ParseIP("10.0.14.5"), |
| 156 | + contains: true, |
| 157 | + }, |
| 158 | + { |
| 159 | + name: "IPv4 CIDR that does NOT contain an IPv4 IP", |
| 160 | + cidr: IPv4CIDR, |
| 161 | + ip: net.ParseIP("10.1.55.5"), |
| 162 | + contains: false, |
| 163 | + }, |
| 164 | + { |
| 165 | + name: "IPv6 CIDR that does contain an IPv6 IP", |
| 166 | + cidr: IPv6CIDR, |
| 167 | + ip: net.ParseIP("2001:db8:1234:1a00::"), |
| 168 | + contains: true, |
| 169 | + }, |
| 170 | + { |
| 171 | + name: "IPv6 CIDR that does NOT contain an IPv6 IP", |
| 172 | + cidr: IPv6CIDR, |
| 173 | + ip: net.ParseIP("2001:af1:1222:1a20::"), |
| 174 | + contains: false, |
| 175 | + }, |
| 176 | + } |
| 177 | + for _, tt := range tests { |
| 178 | + t.Run(tt.name, func(t *testing.T) { |
| 179 | + overlaps := ContainsAddress(tt.cidr, tt.ip) |
| 180 | + assert.Equal(t, tt.contains, overlaps, "Given IP address should be part of the given CIDR") |
| 181 | + }) |
| 182 | + } |
| 183 | +} |
0 commit comments