-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcidr_test.go
40 lines (37 loc) · 993 Bytes
/
cidr_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
package netutils
import (
"net"
"reflect"
"testing"
)
func TestParseCIDRMask(t *testing.T) {
tests := []struct {
mask string
expected net.IPMask
expectErr bool
}{
// valid IPv4
{"192.168.0.0/24", net.CIDRMask(24, 32), false},
{"10.0.0.0/8", net.CIDRMask(8, 32), false},
// valid IPv6
{"2001:db8::/32", net.CIDRMask(32, 128), false},
{"fe80::/10", net.CIDRMask(10, 128), false},
// invalid cases
{"192.168.0.0", nil, true},
{"192.168.0.0/abc", nil, true},
{"192.168.0.0/-1", nil, true},
{"2001:db8::", nil, true},
{"2001:db8::/xyz", nil, true},
}
for _, tt := range tests {
t.Run(tt.mask, func(t *testing.T) {
result, err := ParseCIDRMask(tt.mask)
if (err != nil) != tt.expectErr {
t.Errorf("unexpected error status: got %v, want %v, error: %v", (err != nil), tt.expectErr, err)
}
if !tt.expectErr && !reflect.DeepEqual(result, tt.expected) {
t.Errorf("unexpected result: got %v, want %v", result, tt.expected)
}
})
}
}