2022-01-25 16:20:32 +00:00
|
|
|
package address
|
2021-11-08 10:04:45 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/nspcc-dev/neofs-api-go/v2/refs"
|
|
|
|
cidtest "github.com/nspcc-dev/neofs-sdk-go/container/id/test"
|
2022-01-25 16:20:32 +00:00
|
|
|
oidtest "github.com/nspcc-dev/neofs-sdk-go/object/id/test"
|
2021-11-08 10:04:45 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestAddress_SetContainerID(t *testing.T) {
|
|
|
|
a := NewAddress()
|
|
|
|
|
2021-11-12 11:19:16 +00:00
|
|
|
id := cidtest.ID()
|
2021-11-08 10:04:45 +00:00
|
|
|
|
|
|
|
a.SetContainerID(id)
|
|
|
|
|
|
|
|
require.Equal(t, id, a.ContainerID())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAddress_SetObjectID(t *testing.T) {
|
|
|
|
a := NewAddress()
|
|
|
|
|
2022-01-25 16:20:32 +00:00
|
|
|
oid := oidtest.ID()
|
2021-11-08 10:04:45 +00:00
|
|
|
|
|
|
|
a.SetObjectID(oid)
|
|
|
|
|
|
|
|
require.Equal(t, oid, a.ObjectID())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAddress_Parse(t *testing.T) {
|
2021-11-12 11:19:16 +00:00
|
|
|
cid := cidtest.ID()
|
2021-11-08 10:04:45 +00:00
|
|
|
|
2022-01-25 16:20:32 +00:00
|
|
|
oid := oidtest.ID()
|
2021-11-08 10:04:45 +00:00
|
|
|
|
|
|
|
t.Run("should parse successful", func(t *testing.T) {
|
|
|
|
s := strings.Join([]string{cid.String(), oid.String()}, addressSeparator)
|
|
|
|
a := NewAddress()
|
|
|
|
|
|
|
|
require.NoError(t, a.Parse(s))
|
|
|
|
require.Equal(t, oid, a.ObjectID())
|
|
|
|
require.Equal(t, cid, a.ContainerID())
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("should fail for bad address", func(t *testing.T) {
|
|
|
|
s := strings.Join([]string{cid.String()}, addressSeparator)
|
|
|
|
require.EqualError(t, NewAddress().Parse(s), errInvalidAddressString.Error())
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("should fail on container.ID", func(t *testing.T) {
|
|
|
|
s := strings.Join([]string{"1", "2"}, addressSeparator)
|
|
|
|
require.Error(t, NewAddress().Parse(s))
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("should fail on object.ID", func(t *testing.T) {
|
|
|
|
s := strings.Join([]string{cid.String(), "2"}, addressSeparator)
|
|
|
|
require.Error(t, NewAddress().Parse(s))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAddressEncoding(t *testing.T) {
|
|
|
|
a := NewAddress()
|
2022-01-25 16:20:32 +00:00
|
|
|
a.SetObjectID(oidtest.ID())
|
2021-11-12 11:19:16 +00:00
|
|
|
a.SetContainerID(cidtest.ID())
|
2021-11-08 10:04:45 +00:00
|
|
|
|
|
|
|
t.Run("binary", func(t *testing.T) {
|
|
|
|
data, err := a.Marshal()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
a2 := NewAddress()
|
|
|
|
require.NoError(t, a2.Unmarshal(data))
|
|
|
|
|
|
|
|
require.Equal(t, a, a2)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("json", func(t *testing.T) {
|
|
|
|
data, err := a.MarshalJSON()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
a2 := NewAddress()
|
|
|
|
require.NoError(t, a2.UnmarshalJSON(data))
|
|
|
|
|
|
|
|
require.Equal(t, a, a2)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewAddressFromV2(t *testing.T) {
|
|
|
|
t.Run("from nil", func(t *testing.T) {
|
|
|
|
var x *refs.Address
|
|
|
|
|
|
|
|
require.Nil(t, NewAddressFromV2(x))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAddress_ToV2(t *testing.T) {
|
|
|
|
t.Run("nil", func(t *testing.T) {
|
|
|
|
var x *Address
|
|
|
|
|
|
|
|
require.Nil(t, x.ToV2())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewAddress(t *testing.T) {
|
|
|
|
t.Run("default values", func(t *testing.T) {
|
|
|
|
a := NewAddress()
|
|
|
|
|
|
|
|
// check initial values
|
2022-04-11 16:25:14 +00:00
|
|
|
require.True(t, a.ContainerID().Empty())
|
|
|
|
require.True(t, a.ObjectID().Empty())
|
2021-11-08 10:04:45 +00:00
|
|
|
|
|
|
|
// convert to v2 message
|
|
|
|
aV2 := a.ToV2()
|
|
|
|
|
|
|
|
require.Nil(t, aV2.GetContainerID())
|
|
|
|
require.Nil(t, aV2.GetObjectID())
|
|
|
|
})
|
|
|
|
}
|