-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils_test.go
64 lines (56 loc) · 1.48 KB
/
utils_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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package main
import (
"net/netip"
"testing"
)
func TestCalcIPv4Cidr(t *testing.T) {
var start, end netip.Addr
var cidr netip.Prefix
// /24
start = netip.MustParseAddr("192.0.2.0")
end = netip.MustParseAddr("192.0.2.255")
cidr = calcIPv4Cidr(start, end)
if cidr.Bits() != 24 {
t.Errorf("Expected 24, got %s", cidr.String())
}
// /25
start = netip.MustParseAddr("192.0.2.0")
end = netip.MustParseAddr("192.0.2.127")
cidr = calcIPv4Cidr(start, end)
if cidr.Bits() != 25 {
t.Errorf("Expected 25, got %s", cidr.String())
}
// /30
start = netip.MustParseAddr("192.0.2.0")
end = netip.MustParseAddr("192.0.2.3")
cidr = calcIPv4Cidr(start, end)
if cidr.Bits() != 30 {
t.Errorf("Expected 30, got %s", cidr.String())
}
// /31
start = netip.MustParseAddr("192.0.2.0")
end = netip.MustParseAddr("192.0.2.1")
cidr = calcIPv4Cidr(start, end)
if cidr.Bits() != 31 {
t.Errorf("Expected 31, got %s", cidr.String())
}
// /32
start = netip.MustParseAddr("192.0.2.0")
end = netip.MustParseAddr("192.0.2.0")
cidr = calcIPv4Cidr(start, end)
if cidr.Bits() != 32 {
t.Errorf("Expected 32, got %s", cidr.String())
}
}
func TestParseNetworks(t *testing.T) {
networks := parseNetworks("192.0.2.0/24,2001:db8::/32")
if len(networks) != 2 {
t.Errorf("Expected 2, got %d", len(networks))
}
if networks[0] != "192.0.2.0/24" {
t.Errorf("Expected 192.0.2.0/24, got %s", networks[0])
}
if networks[1] != "2001:db8::/32" {
t.Errorf("Expected 2001:db8::/32, got %s", networks[1])
}
}