-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathipconv_test.go
More file actions
162 lines (151 loc) · 4.29 KB
/
ipconv_test.go
File metadata and controls
162 lines (151 loc) · 4.29 KB
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package ipconv
import (
"math/big"
"net"
"testing"
)
func TestIPv4ToInt(t *testing.T) {
for _, c := range []struct {
in net.IP
want uint32
}{
{net.ParseIP("192.168.1.1"), 3232235777},
{net.ParseIP("0.0.0.0"), 0},
{net.ParseIP("8.8.8.8"), 134744072},
{net.ParseIP("255.255.255.255"), 4294967295},
} {
got, err := IPv4ToInt(c.in)
if got != c.want || err != nil {
t.Errorf("IPv4ToInt(%q) == %q, want %q", c.in, got, c.want)
}
}
}
func TestIPv4ToIntError(t *testing.T) {
for _, c := range []struct {
in net.IP
want uint32
}{
{net.ParseIP("google.com"), 0},
} {
got, err := IPv4ToInt(c.in)
if err == nil {
t.Errorf("IPv4ToInt(%q) == %q, want %q", c.in, got, c.want)
}
}
}
func TestIPv6ToInt(t *testing.T) {
for _, c := range []struct {
in net.IP
want [2]uint64
}{
{net.ParseIP("0000:0000:0000:0000:0000:0000:0000:0000"), [2]uint64{0, 0}},
{net.ParseIP("0000:0000:0000:0000:0000:0000:0000:1"), [2]uint64{0, 1}},
{net.ParseIP("2001:4860:4860::8888"), [2]uint64{2306204062558715904, 34952}},
} {
got, err := IPv6ToInt(c.in)
if got != c.want || err != nil {
t.Errorf("IPv6ToInt(%q) == %q, want %q", c.in.To16(), got, c.want)
}
}
}
func TestIPv6ToBigInt(t *testing.T) {
for _, c := range []struct {
in net.IP
want *big.Int
}{
{net.ParseIP("0000:0000:0000:0000:0000:0000:0000:0000"), GetBigInt("0")},
{net.ParseIP("0000:0000:0000:0000:0000:0000:0000:0001"), GetBigInt("1")},
{net.ParseIP("2001:0:0:0:0:ffff:c0a8:101"), GetBigInt("42540488161975842760550637899214225665")},
{net.ParseIP("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"), GetBigInt("340282366920938463463374607431768211455")},
} {
got, err := IPv6ToBigInt(c.in)
if got.Cmp(c.want) != 0 || err != nil {
t.Errorf("IPv6ToInt(%q) == %q, want %q", c.in.To16(), got, c.want)
}
}
}
func TestIntToIPv4(t *testing.T) {
for _, c := range []struct {
in uint32
want net.IP
}{
{3232235777, net.ParseIP("192.168.1.1")},
{0, net.ParseIP("0.0.0.0")},
{134744072, net.ParseIP("8.8.8.8")},
{4294967295, net.ParseIP("255.255.255.255")},
} {
got := IntToIPv4(c.in)
if !got.Equal(c.want) {
t.Errorf("IntToIPv4(%q) == %q, want %q", c.in, got, c.want)
}
}
}
func TestIntToIPv6(t *testing.T) {
for _, c := range []struct {
in [2]uint64
want net.IP
}{
{[2]uint64{0, 0}, net.ParseIP("0000:0000:0000:0000:0000:0000:0000:0000")},
{[2]uint64{0, 1}, net.ParseIP("0000:0000:0000:0000:0000:0000:0000:0001")},
{[2]uint64{1, 0}, net.ParseIP("0000:0000:0000:0001:0000:0000:0000:0000")},
{[2]uint64{2306204062558715904, 34952}, net.ParseIP("2001:4860:4860::8888")},
{[2]uint64{0, 18446744073709551615}, net.ParseIP("0000:0000:0000:0000:ffff:ffff:ffff:ffff")},
{[2]uint64{18446744073709551615, 18446744073709551615}, net.ParseIP("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff")},
} {
got := IntToIPv6(c.in[0], c.in[1])
if !got.Equal(c.want) {
t.Errorf("IntToIPv6(%q) == %q, want %q", c.in, got, c.want)
}
}
}
func TestBigIntToIPv6(t *testing.T) {
for _, c := range []struct {
in *big.Int
want net.IP
}{
{GetBigInt("0"), net.ParseIP("0000:0000:0000:0000:0000:0000:0000:0000")},
{GetBigInt("1"), net.ParseIP("0000:0000:0000:0000:0000:0000:0000:0001")},
{GetBigInt("42540488161975842760550637899214225665"), net.ParseIP("2001:0:0:0:0:ffff:c0a8:101")},
{GetBigInt("340282366920938463463374607431768211455"), net.ParseIP("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff")},
} {
got := BigIntToIPv6(*c.in)
if !got.Equal(c.want) {
t.Errorf("BigIntToIPv6(%q) == %q, want %q", c.in, got, c.want)
}
}
}
func TestParseIP(t *testing.T) {
for _, c := range []struct {
in string
want int
}{
{"192.168.1.1", 4},
{"0.0.0.0", 4},
{"0000:0000:0000:0000:0000:0000:0000:0000", 16},
{"0000:0000:0000:0000:0000:0000:0000:0001", 16},
{"2001:0:0:0:0:ffff:c0a8:101", 16},
} {
_, gotType, err := ParseIP(c.in)
if gotType != c.want || err != nil {
t.Errorf("ParseIP(%q) == %q, want %q", c.in, gotType, c.want)
}
}
}
func TestParseIPError(t *testing.T) {
for _, c := range []struct {
in string
want int
}{
{"google.com", 0},
} {
_, gotType, err := ParseIP(c.in)
if gotType != c.want || err == nil {
t.Errorf("ParseIP(%q) == %q, want %q", c.in, gotType, c.want)
}
}
}
func GetBigInt(bi string) *big.Int {
var bigInt = new(big.Int)
bigInt.SetString(bi, 10)
return bigInt
}