p2p: remove NewNetAddressStringWithOptionalID (#3711)

Fixes #3521

The function NewNetAddressStringWithOptionalID is from a time when peer
IDs were optional. They're not anymore. So this should be renamed to
NewNetAddressString and should ensure the ID is provided.

* update changelog
* use NewNetAddress in transport tests
* use NewNetAddress in TestTransportMultiplexAcceptMultiple
This commit is contained in:
Anton Kaliaev
2019-06-06 00:39:28 +09:00
committed by Alexander Simmerl
parent a7e8fbf3a7
commit c1f264822a
6 changed files with 62 additions and 124 deletions

View File

@@ -20,17 +20,23 @@ func TestNewNetAddress(t *testing.T) {
}, "Calling NewNetAddress with UDPAddr should not panic in testing")
}
func TestNewNetAddressStringWithOptionalID(t *testing.T) {
func TestNewNetAddressString(t *testing.T) {
testCases := []struct {
name string
addr string
expected string
correct bool
}{
{"no node id, no protocol", "127.0.0.1:8080", "127.0.0.1:8080", true},
{"no node id, tcp input", "tcp://127.0.0.1:8080", "127.0.0.1:8080", true},
{"no node id, udp input", "udp://127.0.0.1:8080", "127.0.0.1:8080", true},
{"malformed udp input", "udp//127.0.0.1:8080", "", false},
{"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", "deadbeefdeadbeefdeadbeefdeadbeefdeadbeef@127.0.0.1:8080", "deadbeefdeadbeefdeadbeefdeadbeefdeadbeef@127.0.0.1:8080", true},
{"tcp input", "tcp://deadbeefdeadbeefdeadbeefdeadbeefdeadbeef@127.0.0.1:8080", "deadbeefdeadbeefdeadbeefdeadbeefdeadbeef@127.0.0.1:8080", true},
{"udp input", "udp://deadbeefdeadbeefdeadbeefdeadbeefdeadbeef@127.0.0.1:8080", "deadbeefdeadbeefdeadbeefdeadbeefdeadbeef@127.0.0.1:8080", true},
{"malformed tcp input", "tcp//deadbeefdeadbeefdeadbeefdeadbeefdeadbeef@127.0.0.1:8080", "", false},
{"malformed udp input", "udp//deadbeefdeadbeefdeadbeefdeadbeefdeadbeef@127.0.0.1:8080", "", false},
// {"127.0.0:8080", false},
{"invalid host", "notahost", "", false},
{"invalid port", "127.0.0.1:notapath", "", false},
@@ -41,14 +47,13 @@ func TestNewNetAddressStringWithOptionalID(t *testing.T) {
{"too short nodeId", "deadbeef@127.0.0.1:8080", "", false},
{"too short, not hex nodeId", "this-isnot-hex@127.0.0.1:8080", "", false},
{"not hex nodeId", "xxxxbeefdeadbeefdeadbeefdeadbeefdeadbeef@127.0.0.1:8080", "", false},
{"correct nodeId", "deadbeefdeadbeefdeadbeefdeadbeefdeadbeef@127.0.0.1:8080", "deadbeefdeadbeefdeadbeefdeadbeefdeadbeef@127.0.0.1:8080", true},
{"too short nodeId w/tcp", "tcp://deadbeef@127.0.0.1:8080", "", false},
{"too short notHex nodeId w/tcp", "tcp://this-isnot-hex@127.0.0.1:8080", "", false},
{"notHex nodeId w/tcp", "tcp://xxxxbeefdeadbeefdeadbeefdeadbeefdeadbeef@127.0.0.1:8080", "", false},
{"correct nodeId w/tcp", "tcp://deadbeefdeadbeefdeadbeefdeadbeefdeadbeef@127.0.0.1:8080", "deadbeefdeadbeefdeadbeefdeadbeefdeadbeef@127.0.0.1:8080", true},
{"no node id when expected", "tcp://@127.0.0.1:8080", "", false},
{"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},
@@ -59,7 +64,7 @@ func TestNewNetAddressStringWithOptionalID(t *testing.T) {
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
addr, err := NewNetAddressStringWithOptionalID(tc.addr)
addr, err := NewNetAddressString(tc.addr)
if tc.correct {
if assert.Nil(t, err, tc.addr) {
assert.Equal(t, tc.expected, addr.String())
@@ -71,28 +76,6 @@ func TestNewNetAddressStringWithOptionalID(t *testing.T) {
}
}
func TestNewNetAddressString(t *testing.T) {
testCases := []struct {
addr string
expected string
correct bool
}{
{"127.0.0.1:8080", "127.0.0.1:8080", false},
{"deadbeefdeadbeefdeadbeefdeadbeefdeadbeef@127.0.0.1:8080", "deadbeefdeadbeefdeadbeefdeadbeefdeadbeef@127.0.0.1:8080", true},
}
for _, tc := range testCases {
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",
@@ -115,12 +98,12 @@ func TestNetAddressProperties(t *testing.T) {
local bool
routable bool
}{
{"127.0.0.1:8080", true, true, false},
{"ya.ru:80", true, false, true},
{"deadbeefdeadbeefdeadbeefdeadbeefdeadbeef@127.0.0.1:8080", true, true, false},
{"deadbeefdeadbeefdeadbeefdeadbeefdeadbeef@ya.ru:80", true, false, true},
}
for _, tc := range testCases {
addr, err := NewNetAddressStringWithOptionalID(tc.addr)
addr, err := NewNetAddressString(tc.addr)
require.Nil(t, err)
assert.Equal(t, tc.valid, addr.Valid())
@@ -136,15 +119,15 @@ func TestNetAddressReachabilityTo(t *testing.T) {
other string
reachability int
}{
{"127.0.0.1:8080", "127.0.0.1:8081", 0},
{"ya.ru:80", "127.0.0.1:8080", 1},
{"deadbeefdeadbeefdeadbeefdeadbeefdeadbeef@127.0.0.1:8080", "deadbeefdeadbeefdeadbeefdeadbeefdeadbeef@127.0.0.1:8081", 0},
{"deadbeefdeadbeefdeadbeefdeadbeefdeadbeef@ya.ru:80", "deadbeefdeadbeefdeadbeefdeadbeefdeadbeef@127.0.0.1:8080", 1},
}
for _, tc := range testCases {
addr, err := NewNetAddressStringWithOptionalID(tc.addr)
addr, err := NewNetAddressString(tc.addr)
require.Nil(t, err)
other, err := NewNetAddressStringWithOptionalID(tc.other)
other, err := NewNetAddressString(tc.other)
require.Nil(t, err)
assert.Equal(t, tc.reachability, addr.ReachabilityTo(other))