frostfs-node/pkg/network/address_test.go
Leonard Lyubich adbbad0beb [#607] network: Do not work with Address pointers
`network.Address` structure in most cases created once and used read-only.

Replace `AddressFromString` function with `Address.FromString` method with
the same purpose and implementation. Make all libraries to work with value.

Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
2021-06-18 18:09:50 +03:00

87 lines
2 KiB
Go

package network
import (
"testing"
"github.com/multiformats/go-multiaddr"
"github.com/nspcc-dev/neofs-api-go/pkg/netmap"
"github.com/stretchr/testify/require"
)
func TestAddressFromString(t *testing.T) {
t.Run("valid addresses", func(t *testing.T) {
testcases := []struct {
inp string
exp multiaddr.Multiaddr
}{
{":8080", buildMultiaddr("/ip4/0.0.0.0/tcp/8080", t)},
{"example.com:7070", buildMultiaddr("/dns4/example.com/tcp/7070", t)},
{"213.44.87.1:32512", buildMultiaddr("/ip4/213.44.87.1/tcp/32512", t)},
{"[2004:eb1::1]:8080", buildMultiaddr("/ip6/2004:eb1::1/tcp/8080", t)},
}
var addr Address
for _, testcase := range testcases {
err := addr.FromString(testcase.inp)
require.NoError(t, err)
require.Equal(t, testcase.exp, addr.ma, testcase.inp)
}
})
}
func TestAddress_HostAddrString(t *testing.T) {
t.Run("valid addresses", func(t *testing.T) {
testcases := []struct {
ma multiaddr.Multiaddr
exp string
}{
{buildMultiaddr("/dns4/neofs.bigcorp.com/tcp/8080", t), "neofs.bigcorp.com:8080"},
{buildMultiaddr("/ip4/172.16.14.1/tcp/8080", t), "172.16.14.1:8080"},
}
for _, testcase := range testcases {
addr := Address{testcase.ma}
got := addr.HostAddr()
require.Equal(t, testcase.exp, got)
}
})
t.Run("invalid addresses", func(t *testing.T) {
testcases := []multiaddr.Multiaddr{
buildMultiaddr("/tcp/8080", t),
}
for _, testcase := range testcases {
addr := Address{testcase}
require.Panics(t, func() { addr.HostAddr() })
}
})
}
func buildMultiaddr(s string, t *testing.T) multiaddr.Multiaddr {
ma, err := multiaddr.NewMultiaddr(s)
require.NoError(t, err)
return ma
}
func TestAddress_WriteToNodeInfo(t *testing.T) {
a := "127.0.0.1:8080"
var addr Address
err := addr.FromString(a)
require.NoError(t, err)
var ni netmap.NodeInfo
addr.WriteToNodeInfo(&ni)
var restored Address
err = restored.FromString(ni.Address())
require.NoError(t, err)
require.True(t, restored.Equal(addr))
}