2020-09-17 10:16:43 +00:00
|
|
|
package object
|
|
|
|
|
|
|
|
import (
|
2020-10-14 19:08:27 +00:00
|
|
|
"strings"
|
2020-09-17 10:16:43 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/nspcc-dev/neofs-api-go/pkg/container"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestAddress_SetContainerID(t *testing.T) {
|
|
|
|
a := NewAddress()
|
|
|
|
|
|
|
|
cid := container.NewID()
|
|
|
|
cid.SetSHA256(randSHA256Checksum(t))
|
|
|
|
|
|
|
|
a.SetContainerID(cid)
|
|
|
|
|
2020-11-16 08:42:19 +00:00
|
|
|
require.Equal(t, cid, a.ContainerID())
|
2020-09-17 10:16:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestAddress_SetObjectID(t *testing.T) {
|
|
|
|
a := NewAddress()
|
|
|
|
|
|
|
|
oid := randID(t)
|
|
|
|
|
|
|
|
a.SetObjectID(oid)
|
|
|
|
|
2020-11-16 08:42:19 +00:00
|
|
|
require.Equal(t, oid, a.ObjectID())
|
2020-09-17 10:16:43 +00:00
|
|
|
}
|
2020-10-14 19:08:27 +00:00
|
|
|
|
|
|
|
func TestAddress_Parse(t *testing.T) {
|
|
|
|
cid := container.NewID()
|
|
|
|
cid.SetSHA256(randSHA256Checksum(t))
|
|
|
|
|
|
|
|
oid := NewID()
|
|
|
|
oid.SetSHA256(randSHA256Checksum(t))
|
|
|
|
|
|
|
|
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))
|
2020-11-16 08:42:19 +00:00
|
|
|
require.Equal(t, oid, a.ObjectID())
|
|
|
|
require.Equal(t, cid, a.ContainerID())
|
2020-10-14 19:08:27 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("should fail for bad address", func(t *testing.T) {
|
|
|
|
s := strings.Join([]string{cid.String()}, addressSeparator)
|
2020-11-16 13:33:41 +00:00
|
|
|
require.EqualError(t, NewAddress().Parse(s), errInvalidAddressString.Error())
|
2020-10-14 19:08:27 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("should fail on container.ID", func(t *testing.T) {
|
|
|
|
s := strings.Join([]string{"1", "2"}, addressSeparator)
|
2020-11-16 13:33:41 +00:00
|
|
|
require.Error(t, NewAddress().Parse(s))
|
2020-10-14 19:08:27 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("should fail on object.ID", func(t *testing.T) {
|
|
|
|
s := strings.Join([]string{cid.String(), "2"}, addressSeparator)
|
2020-11-16 13:33:41 +00:00
|
|
|
require.Error(t, NewAddress().Parse(s))
|
2020-10-14 19:08:27 +00:00
|
|
|
})
|
|
|
|
}
|
2020-11-13 12:58:53 +00:00
|
|
|
|
|
|
|
func TestAddressEncoding(t *testing.T) {
|
|
|
|
a := NewAddress()
|
|
|
|
a.SetObjectID(randID(t))
|
|
|
|
a.SetContainerID(randCID(t))
|
|
|
|
|
|
|
|
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)
|
|
|
|
})
|
|
|
|
}
|