-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathipaddress_test.go
97 lines (84 loc) · 2.62 KB
/
ipaddress_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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package wireguardhttps
import (
"net"
"testing"
)
func TestAddressRangeStart(t *testing.T) {
network := mustParseCIDR("10.0.0.0/24")
addressRange := &AddressRange{network}
expectedStart := net.ParseIP("10.0.0.0")
if !addressRange.Start().Equal(expectedStart) {
t.Fatalf("Expected %v, got %v", expectedStart, addressRange.Start())
}
}
func TestAddressRangeFinish(t *testing.T) {
network := mustParseCIDR("10.0.0.0/24")
addressRange := &AddressRange{network}
expectedFinish := net.ParseIP("10.0.0.255")
if !addressRange.Finish().Equal(expectedFinish) {
t.Fatalf("Expected %v, got %v", expectedFinish, addressRange.Finish())
}
}
func TestNextAddressAddressInRange(t *testing.T) {
network := mustParseCIDR("10.0.0.0/24")
addressRange := &AddressRange{network}
currentIP := mustParseCIDR("10.0.0.1/32")
expectedNext := net.ParseIP("10.0.0.2")
actualNext, err := addressRange.Next(currentIP.IP)
if err != nil {
t.Fatalf("Error getting next IP: %v", err)
}
if !actualNext.Equal(expectedNext) {
t.Fatalf("Expected %v, got %v", expectedNext, actualNext)
}
}
func TestErrorReturnedIPNotInNetwork(t *testing.T) {
network := mustParseCIDR("10.0.0.0/24")
addressRange := &AddressRange{network}
incorrectIP := mustParseCIDR("192.168.1.1/32")
_, err := addressRange.Next(incorrectIP.IP)
if err == nil {
t.Fatalf("Expected error: %v is not in subnet %v", incorrectIP, network)
}
if _, ok := err.(*IPNotInSubnetError); !ok {
t.Fatalf("Expected IPNotInSubneterror, got %v", err)
}
}
func TestAddresses(t *testing.T) {
network := mustParseCIDR("10.0.0.0/30")
addressRange := &AddressRange{network}
expectedAddresses := []net.IP{
net.ParseIP("10.0.0.0"),
net.ParseIP("10.0.0.1"),
net.ParseIP("10.0.0.2"),
net.ParseIP("10.0.0.3"),
}
actualAddresses := addressRange.Addresses()
if len(actualAddresses) != len(expectedAddresses) {
t.Fatalf("Expected %v, got %v", expectedAddresses, actualAddresses)
}
for index, address := range actualAddresses {
if !expectedAddresses[index].Equal(address) {
t.Fatalf("Expected %v, got %v", expectedAddresses, actualAddresses)
}
}
}
func TestErrorReturnedEndOfSubnet(t *testing.T) {
network := mustParseCIDR("10.0.0.0/24")
addressRange := &AddressRange{network}
finalIP := mustParseCIDR("10.0.0.255/32")
_, err := addressRange.Next(finalIP.IP)
if err == nil {
t.Fatalf("Expected error: %v is the last address in subnet %v", finalIP, network)
}
if _, ok := err.(*IPsExhaustedError); !ok {
t.Fatalf("Expected IPsExhaustedError, got %v", err)
}
}
func mustParseCIDR(cidr string) net.IPNet {
_, network, err := net.ParseCIDR(cidr)
if err != nil {
panic(err)
}
return *network
}