forked from ava-labs/avalanchego
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathip_test.go
151 lines (143 loc) · 3.27 KB
/
ip_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
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
// (c) 2020, Alex Willmer. All rights reserved.
// See the file LICENSE for licensing terms.
package utils
import (
"fmt"
"net"
"testing"
)
func TestIPDescEqual(t *testing.T) {
tests := []struct {
ipDesc1 IPDesc
ipDesc2 IPDesc
result bool
}{
// Expected equal
{
IPDesc{net.ParseIP("127.0.0.1"), 0},
IPDesc{net.ParseIP("127.0.0.1"), 0},
true,
}, {
IPDesc{net.ParseIP("::1"), 0},
IPDesc{net.ParseIP("::1"), 0},
true,
}, {
IPDesc{net.ParseIP("127.0.0.1"), 0},
IPDesc{net.ParseIP("::ffff:127.0.0.1"), 0},
true,
},
// Expected unequal
{
IPDesc{net.ParseIP("127.0.0.1"), 0},
IPDesc{net.ParseIP("1.2.3.4"), 0},
false,
}, {
IPDesc{net.ParseIP("::1"), 0},
IPDesc{net.ParseIP("2001::1"), 0},
false,
}, {
IPDesc{net.ParseIP("127.0.0.1"), 0},
IPDesc{net.ParseIP("127.0.0.1"), 1},
false,
},
}
for i, tt := range tests {
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
if tt.ipDesc1.IP == nil {
t.Error("ipDesc1 nil")
} else if tt.ipDesc2.IP == nil {
t.Error("ipDesc2 nil")
}
result := tt.ipDesc1.Equal(tt.ipDesc2)
if result && result != tt.result {
t.Error("Expected IPDesc to be equal, but they were not")
}
if !result && result != tt.result {
t.Error("Expected IPDesc to be unequal, but they were equal")
}
})
}
}
func TestIPDescPortString(t *testing.T) {
tests := []struct {
ipDesc IPDesc
result string
}{
{IPDesc{net.ParseIP("127.0.0.1"), 0}, ":0"},
{IPDesc{net.ParseIP("::1"), 42}, ":42"},
{IPDesc{net.ParseIP("::ffff:127.0.0.1"), 65535}, ":65535"},
{IPDesc{net.IP{}, 1234}, ":1234"},
}
for _, tt := range tests {
t.Run(tt.result, func(t *testing.T) {
if result := tt.ipDesc.PortString(); result != tt.result {
t.Errorf("Expected %q, got %q", tt.result, result)
}
})
}
}
func TestIPDescString(t *testing.T) {
tests := []struct {
ipDesc IPDesc
result string
}{
{IPDesc{net.ParseIP("127.0.0.1"), 0}, "127.0.0.1:0"},
{IPDesc{net.ParseIP("::1"), 42}, "[::1]:42"},
{IPDesc{net.ParseIP("::ffff:127.0.0.1"), 65535}, "127.0.0.1:65535"},
{IPDesc{net.IP{}, 1234}, "<nil>:1234"},
}
for _, tt := range tests {
t.Run(tt.result, func(t *testing.T) {
if result := tt.ipDesc.String(); result != tt.result {
t.Errorf("Expected %q, got %q", tt.result, result)
}
})
}
}
func TestToIPDescError(t *testing.T) {
tests := []struct {
in string
out IPDesc
}{
{"", IPDesc{}},
{":", IPDesc{}},
{"abc:", IPDesc{}},
{":abc", IPDesc{}},
{"abc:abc", IPDesc{}},
{"127.0.0.1:", IPDesc{}},
{":1", IPDesc{}},
{"::1", IPDesc{}},
{"::1:42", IPDesc{}},
}
for _, tt := range tests {
t.Run(tt.in, func(t *testing.T) {
result, err := ToIPDesc(tt.in)
if err == nil {
t.Errorf("Unexpected success")
}
if !tt.out.Equal(result) {
t.Errorf("Expected %v, got %v", tt.out, result)
}
})
}
}
func TestToIPDesc(t *testing.T) {
tests := []struct {
in string
out IPDesc
}{
{"127.0.0.1:42", IPDesc{net.ParseIP("127.0.0.1"), 42}},
{"[::1]:42", IPDesc{net.ParseIP("::1"), 42}},
}
for _, tt := range tests {
t.Run(tt.in, func(t *testing.T) {
result, err := ToIPDesc(tt.in)
if err != nil {
t.Errorf("Unexpected error %v", err)
}
if !tt.out.Equal(result) {
t.Errorf("Expected %#v, got %#v", tt.out, result)
}
})
}
}