[#622] pkg/network: Add multiaddress validation

Validation checks:
1. if address can be parsed by network package;
2. if address contains correct amount of protocols;
3. if address's protocols are in correct order.

Signed-off-by: Pavel Karpy <carpawell@nspcc.ru>
This commit is contained in:
Pavel Karpy 2021-06-18 13:11:57 +03:00 committed by Pavel Karpy
parent 16e9e726ff
commit ea5c74e761
2 changed files with 151 additions and 0 deletions

View file

@ -0,0 +1,64 @@
package network
import (
"testing"
"github.com/nspcc-dev/neofs-api-go/pkg/netmap"
"github.com/stretchr/testify/require"
)
type testCase struct {
input string
err error
}
func TestVerifyMultiAddress_Order(t *testing.T) {
testCases := []testCase{
{
input: "/ip4/1.2.3.4/tcp/80",
err: nil,
},
{
input: "/ip6/1.2.3.4/tcp/80",
err: nil,
},
{
input: "/dns4/1.2.3.4/tcp/80",
err: nil,
},
{
input: "/dns4/1.2.3.4/tcp/80/tls",
err: nil,
},
{
input: "/tls/dns4/1.2.3.4/tcp/80",
err: errUnsupportedNetworkProtocol,
},
{
input: "/dns4/1.2.3.4/tls/tcp/80",
err: errUnsupportedTransportProtocol,
},
{
input: "/dns4/1.2.3.4/tcp/80/wss",
err: errUnsupportedPresentationProtocol,
},
}
for _, test := range testCases {
ni := constructNodeInfo(test.input)
if test.err != nil {
require.EqualError(t, test.err, VerifyMultiAddress(ni).Error())
} else {
require.NoError(t, VerifyMultiAddress(ni))
}
}
}
func constructNodeInfo(address string) *netmap.NodeInfo {
ni := new(netmap.NodeInfo)
ni.SetAddress(address)
return ni
}