-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathparsecidr_test.go
49 lines (46 loc) · 1.54 KB
/
parsecidr_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package main
import (
"testing"
)
func TestParseCIDR(t *testing.T) {
testdata := []struct {
expectOk bool
str string
expect string
}{
{true, "192.168.10.0", "192.168.10.0/32"},
{true, "192.168.10.0/32", "192.168.10.0/32"},
{true, "192.168.10.5/24", "192.168.10.0/24"},
{true, "192.168.10.0/0", "0.0.0.0/0"},
{false, "256.168.10.0/32", ""},
{false, "192.168.10.0/33", ""},
{false, "192.168.10.20.1", ""},
{false, "192.168.10.20.1/24", ""},
{false, "192.168.10", ""},
{false, "192.168.10/24", ""},
{false, "not_an_ip", ""},
{false, "not_an_ip/32", ""},
{true, "fe80:0123:4567::1234:5678:abce:f123", "fe80:123:4567:0:1234:5678:abce:f123/128"},
{true, "fe80:0123:4567::1234:5678:abce:f123/128", "fe80:123:4567:0:1234:5678:abce:f123/128"},
{true, "fe80:0123:4567::1234:5678:abce:f123/64", "fe80:123:4567::/64"},
{false, "fe80:g123::/64", ""},
{false, "fe80:0123:4567:abcd:1234:5678:abce:f123:6545/64", ""},
{false, "fe80:0123:4567::1234:5678:abce:f123/129", ""},
}
cfg.Settings.Verbose = true
for _, d := range testdata {
t.Run(d.str, func(t *testing.T) {
ip := parseCIDR(d.str, false)
//t.Logf("ok=%v, str=%v, ip=%v", d.expectOk, d.str, ip)
if ip == nil && d.expectOk {
t.Fatalf("parseCIDR(%q) failed, got nil, expected %q", d.str, d.expect)
}
if ip != nil && !d.expectOk {
t.Fatalf("parseCIDR(%q) failed, got %q, expected nil", d.str, d.expect)
}
if ip != nil && ip.String() != d.expect {
t.Fatalf("parseCIDR(%q) failed, got %q, expected %q", d.str, ip.String(), d.expect)
}
})
}
}