-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnetaddress_test.go
62 lines (47 loc) · 1.54 KB
/
netaddress_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
package main
import (
"net"
"testing"
"github.com/stretchr/testify/assert"
)
func TestParseIPv4(t *testing.T) {
addr, err := parseNetAddress("192.0.2.73")
assert.NoError(t, err)
assert.True(t, addr.IP.Equal(net.IPv4(192, 0, 2, 73)))
assert.Equal(t, addr.Mask, net.IPv4Mask(255, 255, 255, 255))
assert.Equal(t, mustParseNetAddress("192.0.2.73/32"), addr)
}
func TestParseIPv4WithMask(t *testing.T) {
addr, err := parseNetAddress("192.0.2.64/26")
assert.NoError(t, err)
assert.True(t, addr.IP.Equal(net.IPv4(192, 0, 2, 64)))
assert.Equal(t, addr.Mask, net.IPv4Mask(255, 255, 255, 192))
}
func TestParseIPv6(t *testing.T) {
addr, err := parseNetAddress("2001:db8::ff00")
assert.NoError(t, err)
expected := net.ParseIP("2001:db8::ff00")
assert.NotNil(t, expected)
assert.True(t, addr.IP.Equal(expected))
assert.Equal(t, addr.Mask, net.CIDRMask(128, 128))
assert.Equal(t, mustParseNetAddress("2001:db8::ff00/128"), addr)
}
func TestParseIPv6WithMask(t *testing.T) {
addr, err := parseNetAddress("2001:db8:ff::/64")
assert.NoError(t, err)
expected := net.ParseIP("2001:db8:ff::")
assert.NotNil(t, expected)
assert.True(t, addr.IP.Equal(expected))
assert.Equal(t, addr.Mask, net.CIDRMask(64, 128))
}
func TestParseEmpty(t *testing.T) {
_, err := parseNetAddress("")
assert.Error(t, err)
assert.Equal(t, err.Error(), `Parsing IP address "" failed`)
}
func TestMarshalAddress(t *testing.T) {
addr, err := parseNetAddress("2001:db8:ff::/64")
assert.NoError(t, err)
m, err := addr.MarshalText()
assert.Equal(t, m, []byte("2001:db8:ff::/64"))
}