2021-10-26 13:19:11 +00:00
|
|
|
package cid_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/sha256"
|
|
|
|
"math/rand"
|
|
|
|
"testing"
|
|
|
|
|
2022-04-11 16:25:14 +00:00
|
|
|
"github.com/mr-tron/base58"
|
2021-10-26 13:19:11 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/v2/refs"
|
|
|
|
cid "github.com/nspcc-dev/neofs-sdk-go/container/id"
|
|
|
|
cidtest "github.com/nspcc-dev/neofs-sdk-go/container/id/test"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func randSHA256Checksum() (cs [sha256.Size]byte) {
|
|
|
|
rand.Read(cs[:])
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-04-11 16:25:14 +00:00
|
|
|
const emptyID = "11111111111111111111111111111111"
|
|
|
|
|
2021-10-26 13:19:11 +00:00
|
|
|
func TestID_ToV2(t *testing.T) {
|
2022-04-11 16:25:14 +00:00
|
|
|
t.Run("non-zero", func(t *testing.T) {
|
2021-10-26 13:19:11 +00:00
|
|
|
checksum := randSHA256Checksum()
|
|
|
|
|
2021-11-12 11:19:16 +00:00
|
|
|
id := cidtest.IDWithChecksum(checksum)
|
2021-10-26 13:19:11 +00:00
|
|
|
|
2022-04-11 16:25:14 +00:00
|
|
|
var idV2 refs.ContainerID
|
|
|
|
id.WriteToV2(&idV2)
|
2021-10-26 13:19:11 +00:00
|
|
|
|
2022-04-11 16:25:14 +00:00
|
|
|
var newID cid.ID
|
|
|
|
require.NoError(t, newID.ReadFromV2(idV2))
|
2021-10-26 13:19:11 +00:00
|
|
|
|
2022-04-11 16:25:14 +00:00
|
|
|
require.Equal(t, id, newID)
|
|
|
|
require.Equal(t, checksum[:], idV2.GetValue())
|
2021-10-26 13:19:11 +00:00
|
|
|
})
|
|
|
|
|
2022-04-11 16:25:14 +00:00
|
|
|
t.Run("zero", func(t *testing.T) {
|
|
|
|
var (
|
|
|
|
x cid.ID
|
|
|
|
v2 refs.ContainerID
|
|
|
|
)
|
2021-10-26 13:19:11 +00:00
|
|
|
|
2022-04-11 16:25:14 +00:00
|
|
|
x.WriteToV2(&v2)
|
|
|
|
require.Equal(t, emptyID, base58.Encode(v2.GetValue()))
|
2021-10-26 13:19:11 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestID_Equal(t *testing.T) {
|
|
|
|
cs := randSHA256Checksum()
|
|
|
|
|
2021-11-12 11:19:16 +00:00
|
|
|
id1 := cidtest.IDWithChecksum(cs)
|
|
|
|
id2 := cidtest.IDWithChecksum(cs)
|
2021-10-26 13:19:11 +00:00
|
|
|
|
2022-04-11 16:25:14 +00:00
|
|
|
require.True(t, id1.Equals(id2))
|
2021-10-26 13:19:11 +00:00
|
|
|
|
2021-11-12 11:19:16 +00:00
|
|
|
id3 := cidtest.ID()
|
2021-10-26 13:19:11 +00:00
|
|
|
|
2022-04-11 16:25:14 +00:00
|
|
|
require.False(t, id1.Equals(id3))
|
2021-10-26 13:19:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestID_String(t *testing.T) {
|
2022-04-11 16:25:14 +00:00
|
|
|
t.Run("DecodeString/EncodeToString", func(t *testing.T) {
|
2021-11-12 11:19:16 +00:00
|
|
|
id := cidtest.ID()
|
2022-04-11 16:25:14 +00:00
|
|
|
var id2 cid.ID
|
2021-10-26 13:19:11 +00:00
|
|
|
|
2022-05-31 06:55:08 +00:00
|
|
|
require.NoError(t, id2.DecodeString(id.EncodeToString()))
|
2021-10-26 13:19:11 +00:00
|
|
|
require.Equal(t, id, id2)
|
|
|
|
})
|
|
|
|
|
2022-04-11 16:25:14 +00:00
|
|
|
t.Run("zero", func(t *testing.T) {
|
|
|
|
var id cid.ID
|
2021-10-26 13:19:11 +00:00
|
|
|
|
2022-04-11 16:25:14 +00:00
|
|
|
require.Equal(t, emptyID, id.EncodeToString())
|
2021-10-26 13:19:11 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-04-11 16:25:14 +00:00
|
|
|
func TestNewFromV2(t *testing.T) {
|
|
|
|
t.Run("from zero", func(t *testing.T) {
|
|
|
|
var (
|
|
|
|
x cid.ID
|
|
|
|
v2 refs.ContainerID
|
|
|
|
)
|
2021-10-26 13:19:11 +00:00
|
|
|
|
2022-04-11 16:25:14 +00:00
|
|
|
require.Error(t, x.ReadFromV2(v2))
|
2021-10-26 13:19:11 +00:00
|
|
|
})
|
2022-04-11 16:25:14 +00:00
|
|
|
}
|
2021-10-26 13:19:11 +00:00
|
|
|
|
2022-04-11 16:25:14 +00:00
|
|
|
func TestID_Encode(t *testing.T) {
|
|
|
|
var id cid.ID
|
2021-10-26 13:19:11 +00:00
|
|
|
|
2022-04-11 16:25:14 +00:00
|
|
|
t.Run("panic", func(t *testing.T) {
|
|
|
|
dst := make([]byte, sha256.Size-1)
|
2021-10-26 13:19:11 +00:00
|
|
|
|
2022-04-11 16:25:14 +00:00
|
|
|
require.Panics(t, func() {
|
|
|
|
id.Encode(dst)
|
|
|
|
})
|
2021-10-26 13:19:11 +00:00
|
|
|
})
|
|
|
|
|
2022-04-11 16:25:14 +00:00
|
|
|
t.Run("correct", func(t *testing.T) {
|
|
|
|
dst := make([]byte, sha256.Size)
|
2021-10-26 13:19:11 +00:00
|
|
|
|
2022-04-11 16:25:14 +00:00
|
|
|
require.NotPanics(t, func() {
|
|
|
|
id.Encode(dst)
|
|
|
|
})
|
|
|
|
require.Equal(t, emptyID, id.EncodeToString())
|
2021-10-26 13:19:11 +00:00
|
|
|
})
|
|
|
|
}
|