forked from tendermint/tendermint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnetaddress_test.go
192 lines (164 loc) · 5.34 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
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package p2p
import (
"net"
"sync"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestNetAddress_String(t *testing.T) {
tcpAddr, err := net.ResolveTCPAddr("tcp", "127.0.0.1:8080")
require.Nil(t, err)
netAddr := NewNetAddress("deadbeefdeadbeefdeadbeefdeadbeefdeadbeef", tcpAddr)
var wg sync.WaitGroup
for i := 0; i < 10; i++ {
wg.Add(1)
go func() {
defer wg.Done()
_ = netAddr.String()
}()
}
wg.Wait()
s := netAddr.String()
require.Equal(t, "[email protected]:8080", s)
}
func TestNewNetAddress(t *testing.T) {
tcpAddr, err := net.ResolveTCPAddr("tcp", "127.0.0.1:8080")
require.Nil(t, err)
assert.Panics(t, func() {
NewNetAddress("", tcpAddr)
})
addr := NewNetAddress("deadbeefdeadbeefdeadbeefdeadbeefdeadbeef", tcpAddr)
assert.Equal(t, "[email protected]:8080", addr.String())
assert.NotPanics(t, func() {
NewNetAddress("", &net.UDPAddr{IP: net.ParseIP("127.0.0.1"), Port: 8000})
}, "Calling NewNetAddress with UDPAddr should not panic in testing")
}
func TestNewNetAddressString(t *testing.T) {
testCases := []struct {
name string
addr string
expected string
correct bool
}{
{"no node id and no protocol", "127.0.0.1:8080", "", false},
{"no node id w/ tcp input", "tcp://127.0.0.1:8080", "", false},
{"no node id w/ udp input", "udp://127.0.0.1:8080", "", false},
{
"no protocol",
"[email protected]:8080",
"[email protected]:8080",
true,
},
{
"tcp input",
"tcp://[email protected]:8080",
"[email protected]:8080",
true,
},
{
"udp input",
"udp://[email protected]:8080",
"[email protected]:8080",
true,
},
{"malformed tcp input", "tcp//[email protected]:8080", "", false},
{"malformed udp input", "udp//[email protected]:8080", "", false},
// {"127.0.0:8080", false},
{"invalid host", "notahost", "", false},
{"invalid port", "127.0.0.1:notapath", "", false},
{"invalid host w/ port", "notahost:8080", "", false},
{"just a port", "8082", "", false},
{"non-existent port", "127.0.0:8080000", "", false},
{"too short nodeId", "[email protected]:8080", "", false},
{"too short, not hex nodeId", "[email protected]:8080", "", false},
{"not hex nodeId", "[email protected]:8080", "", false},
{"too short nodeId w/tcp", "tcp://[email protected]:8080", "", false},
{"too short notHex nodeId w/tcp", "tcp://[email protected]:8080", "", false},
{"notHex nodeId w/tcp", "tcp://[email protected]:8080", "", false},
{
"correct nodeId w/tcp",
"tcp://[email protected]:8080",
"[email protected]:8080",
true,
},
{"no node id", "tcp://@127.0.0.1:8080", "", false},
{"no node id or IP", "tcp://@", "", false},
{"tcp no host, w/ port", "tcp://:26656", "", false},
{"empty", "", "", false},
{"node id delimiter 1", "@", "", false},
{"node id delimiter 2", " @", "", false},
{"node id delimiter 3", " @ ", "", false},
}
for _, tc := range testCases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
addr, err := NewNetAddressString(tc.addr)
if tc.correct {
if assert.Nil(t, err, tc.addr) {
assert.Equal(t, tc.expected, addr.String())
}
} else {
assert.NotNil(t, err, tc.addr)
}
})
}
}
func TestNewNetAddressStrings(t *testing.T) {
addrs, errs := NewNetAddressStrings([]string{
"127.0.0.1:8080",
"[email protected]:8080",
"[email protected]:8080"})
assert.Len(t, errs, 1)
assert.Equal(t, 2, len(addrs))
}
func TestNewNetAddressIPPort(t *testing.T) {
addr := NewNetAddressIPPort(net.ParseIP("127.0.0.1"), 8080)
assert.Equal(t, "127.0.0.1:8080", addr.String())
}
func TestNetAddressProperties(t *testing.T) {
// TODO add more test cases
testCases := []struct {
addr string
valid bool
local bool
routable bool
}{
{"[email protected]:8080", true, true, false},
{"[email protected]:80", true, false, true},
}
for _, tc := range testCases {
addr, err := NewNetAddressString(tc.addr)
require.Nil(t, err)
err = addr.Valid()
if tc.valid {
assert.NoError(t, err)
} else {
assert.Error(t, err)
}
assert.Equal(t, tc.local, addr.Local())
assert.Equal(t, tc.routable, addr.Routable())
}
}
func TestNetAddressReachabilityTo(t *testing.T) {
// TODO add more test cases
testCases := []struct {
addr string
other string
reachability int
}{
{
"[email protected]:8080",
"[email protected]:8081",
0,
},
{"[email protected]:80", "[email protected]:8080", 1},
}
for _, tc := range testCases {
addr, err := NewNetAddressString(tc.addr)
require.Nil(t, err)
other, err := NewNetAddressString(tc.other)
require.Nil(t, err)
assert.Equal(t, tc.reachability, addr.ReachabilityTo(other))
}
}