| 1 | package cidrscip-go gomod github.com/apparentlymart/go-cidr . `github.com/apparentlymart/go-cidr/cidr`/ |
| 2 | |
| 3 | import ( |
| 4 | "bytesscip-go gomod github.com/golang/go/src go1.16 bytes/" |
| 5 | "fmtscip-go gomod github.com/golang/go/src go1.16 fmt/" |
| 6 | "math/bigscip-go gomod github.com/golang/go/src go1.16 `math/big`/" |
| 7 | "netscip-go gomod github.com/golang/go/src go1.16 net/" |
| 8 | "strconvscip-go gomod github.com/golang/go/src go1.16 strconv/" |
| 9 | "testingscip-go gomod github.com/golang/go/src go1.16 testing/" |
| 10 | ) |
| 11 | |
| 12 | func TestSubnetscip-go gomod github.com/apparentlymart/go-cidr . `github.com/apparentlymart/go-cidr/cidr`/TestSubnet().(t *testingscip-go gomod github.com/golang/go/src go1.16 testing/.Tscip-go gomod github.com/golang/go/src go1.16 testing/T#) { |
| 13 | type Case struct { |
| 14 | Base string |
| 15 | Bits int |
| 16 | Num int |
| 17 | Output string |
| 18 | Error bool |
| 19 | } |
| 20 | |
| 21 | cases := []Case{ |
| 22 | Case{ |
| 23 | Base: "192.168.2.0/20", |
| 24 | Bits: 4, |
| 25 | Num: 6, |
| 26 | Output: "192.168.6.0/24", |
| 27 | }, |
| 28 | Case{ |
| 29 | Base: "192.168.2.0/20", |
| 30 | Bits: 4, |
| 31 | Num: 0, |
| 32 | Output: "192.168.0.0/24", |
| 33 | }, |
| 34 | Case{ |
| 35 | Base: "192.168.0.0/31", |
| 36 | Bits: 1, |
| 37 | Num: 1, |
| 38 | Output: "192.168.0.1/32", |
| 39 | }, |
| 40 | Case{ |
| 41 | Base: "192.168.0.0/21", |
| 42 | Bits: 4, |
| 43 | Num: 7, |
| 44 | Output: "192.168.3.128/25", |
| 45 | }, |
| 46 | Case{ |
| 47 | Base: "fe80::/48", |
| 48 | Bits: 16, |
| 49 | Num: 6, |
| 50 | Output: "fe80:0:0:6::/64", |
| 51 | }, |
| 52 | Case{ |
| 53 | Base: "fe80::/49", |
| 54 | Bits: 16, |
| 55 | Num: 7, |
| 56 | Output: "fe80:0:0:3:8000::/65", |
| 57 | }, |
| 58 | Case{ |
| 59 | Base: "192.168.2.0/31", |
| 60 | Bits: 2, |
| 61 | Num: 0, |
| 62 | Error: true, // not enough bits to expand into |
| 63 | }, |
| 64 | Case{ |
| 65 | Base: "fe80::/126", |
| 66 | Bits: 4, |
| 67 | Num: 0, |
| 68 | Error: true, // not enough bits to expand into |
| 69 | }, |
| 70 | Case{ |
| 71 | Base: "192.168.2.0/24", |
| 72 | Bits: 4, |
| 73 | Num: 16, |
| 74 | Error: true, // can't fit 16 into 4 bits |
| 75 | }, |
| 76 | } |
| 77 | |
| 78 | for _, testCase := range cases { |
| 79 | _, base, _ := netscip-go gomod github.com/golang/go/src go1.16 net/.ParseCIDRscip-go gomod github.com/golang/go/src go1.16 net/ParseCIDR().(testCase.Base) |
| 80 | gotNet, err := Subnetscip-go gomod github.com/apparentlymart/go-cidr . `github.com/apparentlymart/go-cidr/cidr`/Subnet().(base, testCase.Bits, testCase.Num) |
| 81 | desc := fmtscip-go gomod github.com/golang/go/src go1.16 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.16 fmt/Sprintf().("Subnet(%#v,%#v,%#v)", testCase.Base, testCase.Bits, testCase.Num) |
| 82 | if err != nil { |
| 83 | if !testCase.Error { |
| 84 | t.Errorfscip-go gomod github.com/golang/go/src go1.16 testing/common#Errorf().("%s failed: %s", desc, err.Error()) |
| 85 | } |
| 86 | } else { |
| 87 | got := gotNet.Stringscip-go gomod github.com/golang/go/src go1.16 net/IPNet#String().() |
| 88 | if testCase.Error { |
| 89 | t.Errorfscip-go gomod github.com/golang/go/src go1.16 testing/common#Errorf().("%s = %s; want error", desc, got) |
| 90 | } else { |
| 91 | if got != testCase.Output { |
| 92 | t.Errorfscip-go gomod github.com/golang/go/src go1.16 testing/common#Errorf().("%s = %s; want %s", desc, got, testCase.Output) |
| 93 | } |
| 94 | } |
| 95 | } |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | func TestSubnetBigscip-go gomod github.com/apparentlymart/go-cidr . `github.com/apparentlymart/go-cidr/cidr`/TestSubnetBig().(t *testingscip-go gomod github.com/golang/go/src go1.16 testing/.Tscip-go gomod github.com/golang/go/src go1.16 testing/T#) { |
| 100 | cases := []struct { |
| 101 | Base string |
| 102 | Bits int |
| 103 | Num *bigscip-go gomod github.com/golang/go/src go1.16 `math/big`/.Intscip-go gomod github.com/golang/go/src go1.16 `math/big`/Int# |
| 104 | Output string |
| 105 | Error bool |
| 106 | }{ |
| 107 | { |
| 108 | Base: "192.168.2.0/20", |
| 109 | Bits: 4, |
| 110 | Num: bigscip-go gomod github.com/golang/go/src go1.16 `math/big`/.NewIntscip-go gomod github.com/golang/go/src go1.16 `math/big`/NewInt().(int64(6)), |
| 111 | Output: "192.168.6.0/24", |
| 112 | }, |
| 113 | { |
| 114 | Base: "192.168.2.0/20", |
| 115 | Bits: 4, |
| 116 | Num: bigscip-go gomod github.com/golang/go/src go1.16 `math/big`/.NewIntscip-go gomod github.com/golang/go/src go1.16 `math/big`/NewInt().(int64(0)), |
| 117 | Output: "192.168.0.0/24", |
| 118 | }, |
| 119 | { |
| 120 | Base: "192.168.0.0/31", |
| 121 | Bits: 1, |
| 122 | Num: bigscip-go gomod github.com/golang/go/src go1.16 `math/big`/.NewIntscip-go gomod github.com/golang/go/src go1.16 `math/big`/NewInt().(int64(1)), |
| 123 | Output: "192.168.0.1/32", |
| 124 | }, |
| 125 | { |
| 126 | Base: "192.168.0.0/21", |
| 127 | Bits: 4, |
| 128 | Num: bigscip-go gomod github.com/golang/go/src go1.16 `math/big`/.NewIntscip-go gomod github.com/golang/go/src go1.16 `math/big`/NewInt().(int64(7)), |
| 129 | Output: "192.168.3.128/25", |
| 130 | }, |
| 131 | { |
| 132 | Base: "fe80::/48", |
| 133 | Bits: 16, |
| 134 | Num: bigscip-go gomod github.com/golang/go/src go1.16 `math/big`/.NewIntscip-go gomod github.com/golang/go/src go1.16 `math/big`/NewInt().(int64(6)), |
| 135 | Output: "fe80:0:0:6::/64", |
| 136 | }, |
| 137 | { |
| 138 | Base: "fe80::/48", |
| 139 | Bits: 33, |
| 140 | Num: bigscip-go gomod github.com/golang/go/src go1.16 `math/big`/.NewIntscip-go gomod github.com/golang/go/src go1.16 `math/big`/NewInt().(int64(6)), |
| 141 | Output: "fe80::3:0:0:0/81", |
| 142 | }, |
| 143 | { |
| 144 | Base: "fe80::/49", |
| 145 | Bits: 16, |
| 146 | Num: bigscip-go gomod github.com/golang/go/src go1.16 `math/big`/.NewIntscip-go gomod github.com/golang/go/src go1.16 `math/big`/NewInt().(int64(7)), |
| 147 | Output: "fe80:0:0:3:8000::/65", |
| 148 | }, |
| 149 | { |
| 150 | Base: "192.168.2.0/31", |
| 151 | Bits: 2, |
| 152 | Num: bigscip-go gomod github.com/golang/go/src go1.16 `math/big`/.NewIntscip-go gomod github.com/golang/go/src go1.16 `math/big`/NewInt().(int64(0)), |
| 153 | Error: true, // not enough bits to expand into |
| 154 | }, |
| 155 | { |
| 156 | Base: "fe80::/126", |
| 157 | Bits: 4, |
| 158 | Num: bigscip-go gomod github.com/golang/go/src go1.16 `math/big`/.NewIntscip-go gomod github.com/golang/go/src go1.16 `math/big`/NewInt().(int64(0)), |
| 159 | Error: true, // not enough bits to expand into |
| 160 | }, |
| 161 | { |
| 162 | Base: "192.168.2.0/24", |
| 163 | Bits: 4, |
| 164 | Num: bigscip-go gomod github.com/golang/go/src go1.16 `math/big`/.NewIntscip-go gomod github.com/golang/go/src go1.16 `math/big`/NewInt().(int64(16)), |
| 165 | Error: true, // can't fit 16 into 4 bits |
| 166 | }, |
| 167 | } |
| 168 | |
| 169 | for _, testCase := range cases { |
| 170 | _, base, _ := netscip-go gomod github.com/golang/go/src go1.16 net/.ParseCIDRscip-go gomod github.com/golang/go/src go1.16 net/ParseCIDR().(testCase.Base) |
| 171 | gotNet, err := SubnetBigscip-go gomod github.com/apparentlymart/go-cidr . `github.com/apparentlymart/go-cidr/cidr`/SubnetBig().(base, testCase.Bits, testCase.Num) |
| 172 | desc := fmtscip-go gomod github.com/golang/go/src go1.16 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.16 fmt/Sprintf().("SubnetBig(%#v,%#v,%#v)", testCase.Base, testCase.Bits, testCase.Num) |
| 173 | if err != nil { |
| 174 | if !testCase.Error { |
| 175 | t.Errorfscip-go gomod github.com/golang/go/src go1.16 testing/common#Errorf().("%s failed: %s", desc, err.Error()) |
| 176 | } |
| 177 | } else { |
| 178 | got := gotNet.Stringscip-go gomod github.com/golang/go/src go1.16 net/IPNet#String().() |
| 179 | if testCase.Error { |
| 180 | t.Errorfscip-go gomod github.com/golang/go/src go1.16 testing/common#Errorf().("%s = %s; want error", desc, got) |
| 181 | } else { |
| 182 | if got != testCase.Output { |
| 183 | t.Errorfscip-go gomod github.com/golang/go/src go1.16 testing/common#Errorf().("%s = %s; want %s", desc, got, testCase.Output) |
| 184 | } |
| 185 | } |
| 186 | } |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | func TestHostscip-go gomod github.com/apparentlymart/go-cidr . `github.com/apparentlymart/go-cidr/cidr`/TestHost().(t *testingscip-go gomod github.com/golang/go/src go1.16 testing/.Tscip-go gomod github.com/golang/go/src go1.16 testing/T#) { |
| 191 | type Case struct { |
| 192 | Range string |
| 193 | Num int |
| 194 | Output string |
| 195 | Error bool |
| 196 | } |
| 197 | |
| 198 | cases := []Case{ |
| 199 | Case{ |
| 200 | Range: "192.168.2.0/20", |
| 201 | Num: 6, |
| 202 | Output: "192.168.0.6", |
| 203 | }, |
| 204 | Case{ |
| 205 | Range: "192.168.0.0/20", |
| 206 | Num: 257, |
| 207 | Output: "192.168.1.1", |
| 208 | }, |
| 209 | Case{ |
| 210 | Range: "2001:db8::/32", |
| 211 | Num: 1, |
| 212 | Output: "2001:db8::1", |
| 213 | }, |
| 214 | Case{ |
| 215 | Range: "192.168.1.0/24", |
| 216 | Num: 256, |
| 217 | Error: true, // only 0-255 will fit in 8 bits |
| 218 | }, |
| 219 | Case{ |
| 220 | Range: "192.168.0.0/30", |
| 221 | Num: -3, |
| 222 | Output: "192.168.0.1", // 4 address (0-3) in 2 bits; 3rd from end = 1 |
| 223 | }, |
| 224 | Case{ |
| 225 | Range: "192.168.0.0/30", |
| 226 | Num: -4, |
| 227 | Output: "192.168.0.0", // 4 address (0-3) in 2 bits; 4th from end = 0 |
| 228 | }, |
| 229 | Case{ |
| 230 | Range: "192.168.0.0/30", |
| 231 | Num: -5, |
| 232 | Error: true, // 4 address (0-3) in 2 bits; cannot accomodate 5 |
| 233 | }, |
| 234 | Case{ |
| 235 | Range: "fd9d:bc11:4020::/64", |
| 236 | Num: 2, |
| 237 | Output: "fd9d:bc11:4020::2", |
| 238 | }, |
| 239 | Case{ |
| 240 | Range: "fd9d:bc11:4020::/64", |
| 241 | Num: -2, |
| 242 | Output: "fd9d:bc11:4020:0:ffff:ffff:ffff:fffe", |
| 243 | }, |
| 244 | } |
| 245 | |
| 246 | for _, testCase := range cases { |
| 247 | _, network, _ := netscip-go gomod github.com/golang/go/src go1.16 net/.ParseCIDRscip-go gomod github.com/golang/go/src go1.16 net/ParseCIDR().(testCase.Range) |
| 248 | gotIP, err := Hostscip-go gomod github.com/apparentlymart/go-cidr . `github.com/apparentlymart/go-cidr/cidr`/Host().(network, testCase.Num) |
| 249 | desc := fmtscip-go gomod github.com/golang/go/src go1.16 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.16 fmt/Sprintf().("Host(%#v,%#v)", testCase.Range, testCase.Num) |
| 250 | if err != nil { |
| 251 | if !testCase.Error { |
| 252 | t.Errorfscip-go gomod github.com/golang/go/src go1.16 testing/common#Errorf().("%s failed: %s", desc, err.Error()) |
| 253 | } |
| 254 | } else { |
| 255 | got := gotIP.Stringscip-go gomod github.com/golang/go/src go1.16 net/IP#String().() |
| 256 | if testCase.Error { |
| 257 | t.Errorfscip-go gomod github.com/golang/go/src go1.16 testing/common#Errorf().("%s = %s; want error", desc, got) |
| 258 | } else { |
| 259 | if got != testCase.Output { |
| 260 | t.Errorfscip-go gomod github.com/golang/go/src go1.16 testing/common#Errorf().("%s = %s; want %s", desc, got, testCase.Output) |
| 261 | } |
| 262 | } |
| 263 | } |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | func TestHostBigscip-go gomod github.com/apparentlymart/go-cidr . `github.com/apparentlymart/go-cidr/cidr`/TestHostBig().(t *testingscip-go gomod github.com/golang/go/src go1.16 testing/.Tscip-go gomod github.com/golang/go/src go1.16 testing/T#) { |
| 268 | cases := []struct { |
| 269 | Range string |
| 270 | Num *bigscip-go gomod github.com/golang/go/src go1.16 `math/big`/.Intscip-go gomod github.com/golang/go/src go1.16 `math/big`/Int# |
| 271 | Output string |
| 272 | Error bool |
| 273 | }{ |
| 274 | { |
| 275 | Range: "192.168.2.0/20", |
| 276 | Num: bigscip-go gomod github.com/golang/go/src go1.16 `math/big`/.NewIntscip-go gomod github.com/golang/go/src go1.16 `math/big`/NewInt().(int64(6)), |
| 277 | Output: "192.168.0.6", |
| 278 | }, |
| 279 | { |
| 280 | Range: "192.168.0.0/20", |
| 281 | Num: bigscip-go gomod github.com/golang/go/src go1.16 `math/big`/.NewIntscip-go gomod github.com/golang/go/src go1.16 `math/big`/NewInt().(int64(257)), |
| 282 | Output: "192.168.1.1", |
| 283 | }, |
| 284 | { |
| 285 | Range: "2001:db8::/32", |
| 286 | Num: bigscip-go gomod github.com/golang/go/src go1.16 `math/big`/.NewIntscip-go gomod github.com/golang/go/src go1.16 `math/big`/NewInt().(int64(1)), |
| 287 | Output: "2001:db8::1", |
| 288 | }, |
| 289 | { |
| 290 | Range: "192.168.1.0/24", |
| 291 | Num: bigscip-go gomod github.com/golang/go/src go1.16 `math/big`/.NewIntscip-go gomod github.com/golang/go/src go1.16 `math/big`/NewInt().(int64(256)), |
| 292 | Error: true, // only 0-255 will fit in 8 bits |
| 293 | }, |
| 294 | { |
| 295 | Range: "192.168.0.0/30", |
| 296 | Num: bigscip-go gomod github.com/golang/go/src go1.16 `math/big`/.NewIntscip-go gomod github.com/golang/go/src go1.16 `math/big`/NewInt().(int64(-3)), |
| 297 | Output: "192.168.0.1", // 4 address (0-3) in 2 bits; 3rd from end = 1 |
| 298 | }, |
| 299 | { |
| 300 | Range: "192.168.0.0/30", |
| 301 | Num: bigscip-go gomod github.com/golang/go/src go1.16 `math/big`/.NewIntscip-go gomod github.com/golang/go/src go1.16 `math/big`/NewInt().(int64(-4)), |
| 302 | Output: "192.168.0.0", // 4 address (0-3) in 2 bits; 4th from end = 0 |
| 303 | }, |
| 304 | { |
| 305 | Range: "192.168.0.0/30", |
| 306 | Num: bigscip-go gomod github.com/golang/go/src go1.16 `math/big`/.NewIntscip-go gomod github.com/golang/go/src go1.16 `math/big`/NewInt().(int64(-5)), |
| 307 | Error: true, // 4 address (0-3) in 2 bits; cannot accomodate 5 |
| 308 | }, |
| 309 | { |
| 310 | Range: "fd9d:bc11:4020::/64", |
| 311 | Num: bigscip-go gomod github.com/golang/go/src go1.16 `math/big`/.NewIntscip-go gomod github.com/golang/go/src go1.16 `math/big`/NewInt().(int64(2)), |
| 312 | Output: "fd9d:bc11:4020::2", |
| 313 | }, |
| 314 | { |
| 315 | Range: "fd9d:bc11:4020::/64", |
| 316 | Num: bigscip-go gomod github.com/golang/go/src go1.16 `math/big`/.NewIntscip-go gomod github.com/golang/go/src go1.16 `math/big`/NewInt().(int64(-2)), |
| 317 | Output: "fd9d:bc11:4020:0:ffff:ffff:ffff:fffe", |
| 318 | }, |
| 319 | } |
| 320 | |
| 321 | for _, testCase := range cases { |
| 322 | _, network, _ := netscip-go gomod github.com/golang/go/src go1.16 net/.ParseCIDRscip-go gomod github.com/golang/go/src go1.16 net/ParseCIDR().(testCase.Range) |
| 323 | gotIP, err := HostBigscip-go gomod github.com/apparentlymart/go-cidr . `github.com/apparentlymart/go-cidr/cidr`/HostBig().(network, testCase.Num) |
| 324 | desc := fmtscip-go gomod github.com/golang/go/src go1.16 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.16 fmt/Sprintf().("HostBig(%v,%v)", testCase.Range, testCase.Num) |
| 325 | if err != nil { |
| 326 | if !testCase.Error { |
| 327 | t.Errorfscip-go gomod github.com/golang/go/src go1.16 testing/common#Errorf().("%s failed: %s", desc, err.Error()) |
| 328 | } |
| 329 | } else { |
| 330 | got := gotIP.Stringscip-go gomod github.com/golang/go/src go1.16 net/IP#String().() |
| 331 | if testCase.Error { |
| 332 | t.Errorfscip-go gomod github.com/golang/go/src go1.16 testing/common#Errorf().("%s = %s; want error", desc, got) |
| 333 | } else { |
| 334 | if got != testCase.Output { |
| 335 | t.Errorfscip-go gomod github.com/golang/go/src go1.16 testing/common#Errorf().("%s = %s; want %s", desc, got, testCase.Output) |
| 336 | } |
| 337 | } |
| 338 | } |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | func TestAddressRangescip-go gomod github.com/apparentlymart/go-cidr . `github.com/apparentlymart/go-cidr/cidr`/TestAddressRange().(t *testingscip-go gomod github.com/golang/go/src go1.16 testing/.Tscip-go gomod github.com/golang/go/src go1.16 testing/T#) { |
| 343 | type Case struct { |
| 344 | Range string |
| 345 | First string |
| 346 | Last string |
| 347 | } |
| 348 | |
| 349 | cases := []Case{ |
| 350 | Case{ |
| 351 | Range: "192.168.0.0/16", |
| 352 | First: "192.168.0.0", |
| 353 | Last: "192.168.255.255", |
| 354 | }, |
| 355 | Case{ |
| 356 | Range: "192.168.0.0/17", |
| 357 | First: "192.168.0.0", |
| 358 | Last: "192.168.127.255", |
| 359 | }, |
| 360 | Case{ |
| 361 | Range: "fe80::/64", |
| 362 | First: "fe80::", |
| 363 | Last: "fe80::ffff:ffff:ffff:ffff", |
| 364 | }, |
| 365 | } |
| 366 | |
| 367 | for _, testCase := range cases { |
| 368 | _, network, _ := netscip-go gomod github.com/golang/go/src go1.16 net/.ParseCIDRscip-go gomod github.com/golang/go/src go1.16 net/ParseCIDR().(testCase.Range) |
| 369 | firstIP, lastIP := AddressRangescip-go gomod github.com/apparentlymart/go-cidr . `github.com/apparentlymart/go-cidr/cidr`/AddressRange().(network) |
| 370 | desc := fmtscip-go gomod github.com/golang/go/src go1.16 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.16 fmt/Sprintf().("AddressRange(%#v)", testCase.Range) |
| 371 | gotFirstIP := firstIP.Stringscip-go gomod github.com/golang/go/src go1.16 net/IP#String().() |
| 372 | gotLastIP := lastIP.Stringscip-go gomod github.com/golang/go/src go1.16 net/IP#String().() |
| 373 | if gotFirstIP != testCase.First { |
| 374 | t.Errorfscip-go gomod github.com/golang/go/src go1.16 testing/common#Errorf().("%s first is %s; want %s", desc, gotFirstIP, testCase.First) |
| 375 | } |
| 376 | if gotLastIP != testCase.Last { |
| 377 | t.Errorfscip-go gomod github.com/golang/go/src go1.16 testing/common#Errorf().("%s last is %s; want %s", desc, gotLastIP, testCase.Last) |
| 378 | } |
| 379 | } |
| 380 | |
| 381 | } |
| 382 | |
| 383 | func TestAddressCountscip-go gomod github.com/apparentlymart/go-cidr . `github.com/apparentlymart/go-cidr/cidr`/TestAddressCount().(t *testingscip-go gomod github.com/golang/go/src go1.16 testing/.Tscip-go gomod github.com/golang/go/src go1.16 testing/T#) { |
| 384 | type Case struct { |
| 385 | Range string |
| 386 | Count uint64 |
| 387 | } |
| 388 | |
| 389 | cases := []Case{ |
| 390 | Case{ |
| 391 | Range: "192.168.0.0/16", |
| 392 | Count: 65536, |
| 393 | }, |
| 394 | Case{ |
| 395 | Range: "192.168.0.0/17", |
| 396 | Count: 32768, |
| 397 | }, |
| 398 | Case{ |
| 399 | Range: "192.168.0.0/32", |
| 400 | Count: 1, |
| 401 | }, |
| 402 | Case{ |
| 403 | Range: "192.168.0.0/31", |
| 404 | Count: 2, |
| 405 | }, |
| 406 | Case{ |
| 407 | Range: "0.0.0.0/0", |
| 408 | Count: 4294967296, |
| 409 | }, |
| 410 | Case{ |
| 411 | Range: "0.0.0.0/1", |
| 412 | Count: 2147483648, |
| 413 | }, |
| 414 | Case{ |
| 415 | Range: "::/65", |
| 416 | Count: 9223372036854775808, |
| 417 | }, |
| 418 | Case{ |
| 419 | Range: "::/128", |
| 420 | Count: 1, |
| 421 | }, |
| 422 | Case{ |
| 423 | Range: "::/127", |
| 424 | Count: 2, |
| 425 | }, |
| 426 | } |
| 427 | |
| 428 | for _, testCase := range cases { |
| 429 | _, network, _ := netscip-go gomod github.com/golang/go/src go1.16 net/.ParseCIDRscip-go gomod github.com/golang/go/src go1.16 net/ParseCIDR().(testCase.Range) |
| 430 | gotCount := AddressCountscip-go gomod github.com/apparentlymart/go-cidr . `github.com/apparentlymart/go-cidr/cidr`/AddressCount().(network) |
| 431 | desc := fmtscip-go gomod github.com/golang/go/src go1.16 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.16 fmt/Sprintf().("AddressCount(%#v)", testCase.Range) |
| 432 | if gotCount != testCase.Count { |
| 433 | t.Errorfscip-go gomod github.com/golang/go/src go1.16 testing/common#Errorf().("%s = %d; want %d", desc, gotCount, testCase.Count) |
| 434 | } |
| 435 | } |
| 436 | |
| 437 | } |
| 438 | |
| 439 | func TestIncDecscip-go gomod github.com/apparentlymart/go-cidr . `github.com/apparentlymart/go-cidr/cidr`/TestIncDec().(t *testingscip-go gomod github.com/golang/go/src go1.16 testing/.Tscip-go gomod github.com/golang/go/src go1.16 testing/T#) { |
| 440 | |
| 441 | testCase := [][]string{ |
| 442 | []string{"0.0.0.0", "0.0.0.1"}, |
| 443 | []string{"10.0.0.0", "10.0.0.1"}, |
| 444 | []string{"9.255.255.255", "10.0.0.0"}, |
| 445 | []string{"255.255.255.255", "0.0.0.0"}, |
| 446 | []string{"::", "::1"}, |
| 447 | []string{"ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff", "::"}, |
| 448 | []string{"2001:db8:c001:ba00::", "2001:db8:c001:ba00::1"}, |
| 449 | } |
| 450 | |
| 451 | for _, tc := range testCase { |
| 452 | ip1 := netscip-go gomod github.com/golang/go/src go1.16 net/.ParseIPscip-go gomod github.com/golang/go/src go1.16 net/ParseIP().(tc[0]) |
| 453 | ip2 := netscip-go gomod github.com/golang/go/src go1.16 net/.ParseIPscip-go gomod github.com/golang/go/src go1.16 net/ParseIP().(tc[1]) |
| 454 | iIP := Incscip-go gomod github.com/apparentlymart/go-cidr . `github.com/apparentlymart/go-cidr/cidr`/Inc().(ip1) |
| 455 | if !iIP.Equalscip-go gomod github.com/golang/go/src go1.16 net/IP#Equal().(ip2) { |
| 456 | t.Logfscip-go gomod github.com/golang/go/src go1.16 testing/common#Logf().("%s should inc to equal %s\n", tc[0], tc[1]) |
| 457 | t.Errorfscip-go gomod github.com/golang/go/src go1.16 testing/common#Errorf().("%v should equal %v\n", iIP, ip2) |
| 458 | } |
| 459 | if ip1.Equalscip-go gomod github.com/golang/go/src go1.16 net/IP#Equal().(ip2) { |
| 460 | t.Errorfscip-go gomod github.com/golang/go/src go1.16 testing/common#Errorf().("[%v] should not have been modified to [%v]", ip2, iIP) |
| 461 | } |
| 462 | } |
| 463 | for _, tc := range testCase { |
| 464 | ip1 := netscip-go gomod github.com/golang/go/src go1.16 net/.ParseIPscip-go gomod github.com/golang/go/src go1.16 net/ParseIP().(tc[0]) |
| 465 | ip2 := netscip-go gomod github.com/golang/go/src go1.16 net/.ParseIPscip-go gomod github.com/golang/go/src go1.16 net/ParseIP().(tc[1]) |
| 466 | dIP := Decscip-go gomod github.com/apparentlymart/go-cidr . `github.com/apparentlymart/go-cidr/cidr`/Dec().(ip2) |
| 467 | if !ip1.Equalscip-go gomod github.com/golang/go/src go1.16 net/IP#Equal().(dIP) { |
| 468 | t.Logfscip-go gomod github.com/golang/go/src go1.16 testing/common#Logf().("%s should dec equal %s\n", tc[0], tc[1]) |
| 469 | t.Errorfscip-go gomod github.com/golang/go/src go1.16 testing/common#Errorf().("%v should equal %v\n", ip1, dIP) |
| 470 | } |
| 471 | if ip2.Equalscip-go gomod github.com/golang/go/src go1.16 net/IP#Equal().(dIP) { |
| 472 | t.Errorfscip-go gomod github.com/golang/go/src go1.16 testing/common#Errorf().("[%v] should not have been modified to [%v]", ip2, dIP) |
| 473 | } |
| 474 | } |
| 475 | } |
| 476 | |
| 477 | func TestPreviousSubnetscip-go gomod github.com/apparentlymart/go-cidr . `github.com/apparentlymart/go-cidr/cidr`/TestPreviousSubnet().(t *testingscip-go gomod github.com/golang/go/src go1.16 testing/.Tscip-go gomod github.com/golang/go/src go1.16 testing/T#) { |
| 478 | |
| 479 | testCases := [][]string{ |
| 480 | []string{"10.0.0.0/24", "9.255.255.0/24", "false"}, |
| 481 | []string{"100.0.0.0/26", "99.255.255.192/26", "false"}, |
| 482 | []string{"0.0.0.0/26", "255.255.255.192/26", "true"}, |
| 483 | []string{"2001:db8:e000::/36", "2001:db8:d000::/36", "false"}, |
| 484 | []string{"::/64", "ffff:ffff:ffff:ffff::/64", "true"}, |
| 485 | } |
| 486 | for _, tc := range testCases { |
| 487 | _, c1, _ := netscip-go gomod github.com/golang/go/src go1.16 net/.ParseCIDRscip-go gomod github.com/golang/go/src go1.16 net/ParseCIDR().(tc[0]) |
| 488 | _, c2, _ := netscip-go gomod github.com/golang/go/src go1.16 net/.ParseCIDRscip-go gomod github.com/golang/go/src go1.16 net/ParseCIDR().(tc[1]) |
| 489 | mask, _ := c1.Maskscip-go gomod github.com/golang/go/src go1.16 net/IPNet#Mask..Sizescip-go gomod github.com/golang/go/src go1.16 net/IPMask#Size().() |
| 490 | p1, rollback := PreviousSubnetscip-go gomod github.com/apparentlymart/go-cidr . `github.com/apparentlymart/go-cidr/cidr`/PreviousSubnet().(c1, mask) |
| 491 | if !p1.IPscip-go gomod github.com/golang/go/src go1.16 net/IPNet#IP..Equalscip-go gomod github.com/golang/go/src go1.16 net/IP#Equal().(c2.IPscip-go gomod github.com/golang/go/src go1.16 net/IPNet#IP.) { |
| 492 | t.Errorfscip-go gomod github.com/golang/go/src go1.16 testing/common#Errorf().("IP expected %v, got %v\n", c2.IPscip-go gomod github.com/golang/go/src go1.16 net/IPNet#IP., p1.IPscip-go gomod github.com/golang/go/src go1.16 net/IPNet#IP.) |
| 493 | } |
| 494 | if !bytesscip-go gomod github.com/golang/go/src go1.16 bytes/.Equalscip-go gomod github.com/golang/go/src go1.16 bytes/Equal().(p1.Maskscip-go gomod github.com/golang/go/src go1.16 net/IPNet#Mask., c2.Maskscip-go gomod github.com/golang/go/src go1.16 net/IPNet#Mask.) { |
| 495 | t.Errorfscip-go gomod github.com/golang/go/src go1.16 testing/common#Errorf().("Mask expected %v, got %v\n", c2.Maskscip-go gomod github.com/golang/go/src go1.16 net/IPNet#Mask., p1.Maskscip-go gomod github.com/golang/go/src go1.16 net/IPNet#Mask.) |
| 496 | } |
| 497 | if p1.Stringscip-go gomod github.com/golang/go/src go1.16 net/IPNet#String().() != c2.Stringscip-go gomod github.com/golang/go/src go1.16 net/IPNet#String().() { |
| 498 | t.Errorfscip-go gomod github.com/golang/go/src go1.16 testing/common#Errorf().("%s should have been equal %s\n", p1.Stringscip-go gomod github.com/golang/go/src go1.16 net/IPNet#String().(), c2.Stringscip-go gomod github.com/golang/go/src go1.16 net/IPNet#String().()) |
| 499 | } |
| 500 | if check, _ := strconvscip-go gomod github.com/golang/go/src go1.16 strconv/.ParseBoolscip-go gomod github.com/golang/go/src go1.16 strconv/ParseBool().(tc[2]); rollback != check { |
| 501 | t.Errorfscip-go gomod github.com/golang/go/src go1.16 testing/common#Errorf().("%s to %s should have rolled\n", tc[0], tc[1]) |
| 502 | } |
| 503 | } |
| 504 | for _, tc := range testCases { |
| 505 | _, c1, _ := netscip-go gomod github.com/golang/go/src go1.16 net/.ParseCIDRscip-go gomod github.com/golang/go/src go1.16 net/ParseCIDR().(tc[0]) |
| 506 | _, c2, _ := netscip-go gomod github.com/golang/go/src go1.16 net/.ParseCIDRscip-go gomod github.com/golang/go/src go1.16 net/ParseCIDR().(tc[1]) |
| 507 | mask, _ := c1.Maskscip-go gomod github.com/golang/go/src go1.16 net/IPNet#Mask..Sizescip-go gomod github.com/golang/go/src go1.16 net/IPMask#Size().() |
| 508 | n1, rollover := NextSubnetscip-go gomod github.com/apparentlymart/go-cidr . `github.com/apparentlymart/go-cidr/cidr`/NextSubnet().(c2, mask) |
| 509 | if !n1.IPscip-go gomod github.com/golang/go/src go1.16 net/IPNet#IP..Equalscip-go gomod github.com/golang/go/src go1.16 net/IP#Equal().(c1.IPscip-go gomod github.com/golang/go/src go1.16 net/IPNet#IP.) { |
| 510 | t.Errorfscip-go gomod github.com/golang/go/src go1.16 testing/common#Errorf().("IP expected %v, got %v\n", c1.IPscip-go gomod github.com/golang/go/src go1.16 net/IPNet#IP., n1.IPscip-go gomod github.com/golang/go/src go1.16 net/IPNet#IP.) |
| 511 | } |
| 512 | if !bytesscip-go gomod github.com/golang/go/src go1.16 bytes/.Equalscip-go gomod github.com/golang/go/src go1.16 bytes/Equal().(n1.Maskscip-go gomod github.com/golang/go/src go1.16 net/IPNet#Mask., c1.Maskscip-go gomod github.com/golang/go/src go1.16 net/IPNet#Mask.) { |
| 513 | t.Errorfscip-go gomod github.com/golang/go/src go1.16 testing/common#Errorf().("Mask expected %v, got %v\n", c1.Maskscip-go gomod github.com/golang/go/src go1.16 net/IPNet#Mask., n1.Maskscip-go gomod github.com/golang/go/src go1.16 net/IPNet#Mask.) |
| 514 | } |
| 515 | if n1.Stringscip-go gomod github.com/golang/go/src go1.16 net/IPNet#String().() != c1.Stringscip-go gomod github.com/golang/go/src go1.16 net/IPNet#String().() { |
| 516 | t.Errorfscip-go gomod github.com/golang/go/src go1.16 testing/common#Errorf().("%s should have been equal %s\n", n1.Stringscip-go gomod github.com/golang/go/src go1.16 net/IPNet#String().(), c1.Stringscip-go gomod github.com/golang/go/src go1.16 net/IPNet#String().()) |
| 517 | } |
| 518 | if check, _ := strconvscip-go gomod github.com/golang/go/src go1.16 strconv/.ParseBoolscip-go gomod github.com/golang/go/src go1.16 strconv/ParseBool().(tc[2]); rollover != check { |
| 519 | t.Errorfscip-go gomod github.com/golang/go/src go1.16 testing/common#Errorf().("%s to %s should have rolled\n", tc[0], tc[1]) |
| 520 | } |
| 521 | } |
| 522 | } |
| 523 | |
| 524 | func TestVerifyNetowrkscip-go gomod github.com/apparentlymart/go-cidr . `github.com/apparentlymart/go-cidr/cidr`/TestVerifyNetowrk().(t *testingscip-go gomod github.com/golang/go/src go1.16 testing/.Tscip-go gomod github.com/golang/go/src go1.16 testing/T#) { |
| 525 | |
| 526 | type testVerifyNetwork struct { |
| 527 | CIDRBlock string |
| 528 | CIDRList []string |
| 529 | } |
| 530 | |
| 531 | testCases := []*testVerifyNetwork{ |
| 532 | &testVerifyNetwork{ |
| 533 | CIDRBlock: "192.168.8.0/21", |
| 534 | CIDRList: []string{ |
| 535 | "192.168.8.0/24", |
| 536 | "192.168.9.0/24", |
| 537 | "192.168.10.0/24", |
| 538 | "192.168.11.0/25", |
| 539 | "192.168.11.128/25", |
| 540 | "192.168.12.0/25", |
| 541 | "192.168.12.128/26", |
| 542 | "192.168.12.192/26", |
| 543 | "192.168.13.0/26", |
| 544 | "192.168.13.64/27", |
| 545 | "192.168.13.96/27", |
| 546 | "192.168.13.128/27", |
| 547 | }, |
| 548 | }, |
| 549 | } |
| 550 | failCases := []*testVerifyNetwork{ |
| 551 | &testVerifyNetwork{ |
| 552 | CIDRBlock: "192.168.8.0/21", |
| 553 | CIDRList: []string{ |
| 554 | "192.168.8.0/24", |
| 555 | "192.168.9.0/24", |
| 556 | "192.168.10.0/24", |
| 557 | "192.168.11.0/25", |
| 558 | "192.168.11.128/25", |
| 559 | "192.168.12.0/25", |
| 560 | "192.168.12.64/26", |
| 561 | "192.168.12.128/26", |
| 562 | }, |
| 563 | }, |
| 564 | &testVerifyNetwork{ |
| 565 | CIDRBlock: "192.168.8.0/21", |
| 566 | CIDRList: []string{ |
| 567 | "192.168.7.0/24", |
| 568 | "192.168.9.0/24", |
| 569 | "192.168.10.0/24", |
| 570 | "192.168.11.0/25", |
| 571 | "192.168.11.128/25", |
| 572 | "192.168.12.0/25", |
| 573 | "192.168.12.64/26", |
| 574 | "192.168.12.128/26", |
| 575 | }, |
| 576 | }, |
| 577 | &testVerifyNetwork{ |
| 578 | CIDRBlock: "10.42.0.0/24", |
| 579 | CIDRList: []string{ |
| 580 | |
| 581 | "10.42.0.16/28", |
| 582 | "10.42.0.32/28", |
| 583 | "10.42.0.0/24", |
| 584 | }, |
| 585 | }, |
| 586 | } |
| 587 | |
| 588 | for _, tc := range testCases { |
| 589 | subnets := make([]*netscip-go gomod github.com/golang/go/src go1.16 net/.IPNetscip-go gomod github.com/golang/go/src go1.16 net/IPNet#, len(tc.CIDRList)) |
| 590 | for i, s := range tc.CIDRList { |
| 591 | _, n, err := netscip-go gomod github.com/golang/go/src go1.16 net/.ParseCIDRscip-go gomod github.com/golang/go/src go1.16 net/ParseCIDR().(s) |
| 592 | if err != nil { |
| 593 | t.Errorfscip-go gomod github.com/golang/go/src go1.16 testing/common#Errorf().("Bad test data %s\n", s) |
| 594 | } |
| 595 | subnets[i] = n |
| 596 | } |
| 597 | _, CIDRBlock, perr := netscip-go gomod github.com/golang/go/src go1.16 net/.ParseCIDRscip-go gomod github.com/golang/go/src go1.16 net/ParseCIDR().(tc.CIDRBlock) |
| 598 | if perr != nil { |
| 599 | t.Errorfscip-go gomod github.com/golang/go/src go1.16 testing/common#Errorf().("Bad test data %s\n", tc.CIDRBlock) |
| 600 | } |
| 601 | test := VerifyNoOverlapscip-go gomod github.com/apparentlymart/go-cidr . `github.com/apparentlymart/go-cidr/cidr`/VerifyNoOverlap().(subnets, CIDRBlock) |
| 602 | if test != nil { |
| 603 | t.Errorfscip-go gomod github.com/golang/go/src go1.16 testing/common#Errorf().("Failed test with %v\n", test) |
| 604 | } |
| 605 | } |
| 606 | for _, tc := range failCases { |
| 607 | subnets := make([]*netscip-go gomod github.com/golang/go/src go1.16 net/.IPNetscip-go gomod github.com/golang/go/src go1.16 net/IPNet#, len(tc.CIDRList)) |
| 608 | for i, s := range tc.CIDRList { |
| 609 | _, n, err := netscip-go gomod github.com/golang/go/src go1.16 net/.ParseCIDRscip-go gomod github.com/golang/go/src go1.16 net/ParseCIDR().(s) |
| 610 | if err != nil { |
| 611 | t.Errorfscip-go gomod github.com/golang/go/src go1.16 testing/common#Errorf().("Bad test data %s\n", s) |
| 612 | } |
| 613 | subnets[i] = n |
| 614 | } |
| 615 | _, CIDRBlock, perr := netscip-go gomod github.com/golang/go/src go1.16 net/.ParseCIDRscip-go gomod github.com/golang/go/src go1.16 net/ParseCIDR().(tc.CIDRBlock) |
| 616 | if perr != nil { |
| 617 | t.Errorfscip-go gomod github.com/golang/go/src go1.16 testing/common#Errorf().("Bad test data %s\n", tc.CIDRBlock) |
| 618 | } |
| 619 | test := VerifyNoOverlapscip-go gomod github.com/apparentlymart/go-cidr . `github.com/apparentlymart/go-cidr/cidr`/VerifyNoOverlap().(subnets, CIDRBlock) |
| 620 | if test == nil { |
| 621 | t.Errorfscip-go gomod github.com/golang/go/src go1.16 testing/common#Errorf().("Test should have failed with CIDR %s\n", tc.CIDRBlock) |
| 622 | } |
| 623 | } |
| 624 | } |