forked from EvilSuperstars/go-cidrman
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathipv6_test.go
88 lines (77 loc) · 2.12 KB
/
ipv6_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
// go test -v -run="TestIPv6"
package cidrman
import (
"net"
"testing"
)
func TestIPv6(t *testing.T) {
type TestCase struct {
Input string
Hostmask string
Netmask string
Broadcast string
Network string
Error bool
}
testCases := []TestCase{
{
Input: "",
Hostmask: "",
Netmask: "",
Broadcast: "",
Network: "",
Error: true,
},
{
Input: "::/0",
Hostmask: "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff",
Netmask: "::",
Broadcast: "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff",
Network: "::",
Error: false,
},
{
Input: "fe80::dead:beef/64",
Hostmask: "::ffff:ffff:ffff:ffff",
Netmask: "ffff:ffff:ffff:ffff::",
Broadcast: "fe80::ffff:ffff:ffff:ffff",
Network: "fe80::",
Error: false,
},
{
Input: "2001:0db8:0000:0000:0000:ff00:0042:8328/128",
Hostmask: "::",
Netmask: "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff",
Broadcast: "2001:db8::ff00:42:8328",
Network: "2001:db8::ff00:42:8328",
Error: false,
},
}
for _, testCase := range testCases {
ip, net, err := net.ParseCIDR(testCase.Input)
if err != nil {
if !testCase.Error {
t.Errorf("net.ParseCIDR(%#v) failed: %s", testCase.Input, err.Error())
}
continue
}
prefix, _ := net.Mask.Size()
hostmask := uint128ToIPV6(hostmask6(uint(prefix))).String()
if hostmask != testCase.Hostmask {
t.Errorf("Hostmask(%#v) expected: %#v, got: %#v", testCase.Input, testCase.Hostmask, hostmask)
}
netmask := uint128ToIPV6(netmask6(uint(prefix))).String()
if netmask != testCase.Netmask {
t.Errorf("Netmask(%#v) expected: %#v, got: %#v", testCase.Input, testCase.Netmask, netmask)
}
addr := ipv6ToUInt128(ip.To16())
broadcast := uint128ToIPV6(broadcast6(addr, uint(prefix))).String()
if broadcast != testCase.Broadcast {
t.Errorf("Broadcast(%#v) expected: %#v, got: %#v", testCase.Input, testCase.Broadcast, broadcast)
}
network := uint128ToIPV6(network6(addr, uint(prefix))).String()
if network != testCase.Network {
t.Errorf("Network(%#v) expected: %#v, got: %#v", testCase.Input, testCase.Network, network)
}
}
}