2020-09-10 11:30:29 +00:00
|
|
|
package pkg
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/rand"
|
|
|
|
"crypto/sha256"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/nspcc-dev/neofs-api-go/v2/refs"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2020-11-13 13:30:35 +00:00
|
|
|
func randSHA256(t *testing.T) [sha256.Size]byte {
|
|
|
|
cSHA256 := [sha256.Size]byte{}
|
|
|
|
_, err := rand.Read(cSHA256[:])
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
return cSHA256
|
|
|
|
}
|
|
|
|
|
2020-09-10 11:30:29 +00:00
|
|
|
func TestChecksum(t *testing.T) {
|
|
|
|
c := NewChecksum()
|
|
|
|
|
|
|
|
cSHA256 := [sha256.Size]byte{}
|
|
|
|
_, _ = rand.Read(cSHA256[:])
|
|
|
|
|
|
|
|
c.SetSHA256(cSHA256)
|
|
|
|
|
2020-11-16 08:21:28 +00:00
|
|
|
require.Equal(t, ChecksumSHA256, c.Type())
|
|
|
|
require.Equal(t, cSHA256[:], c.Sum())
|
2020-09-10 11:30:29 +00:00
|
|
|
|
|
|
|
cV2 := c.ToV2()
|
|
|
|
|
|
|
|
require.Equal(t, refs.SHA256, cV2.GetType())
|
|
|
|
require.Equal(t, cSHA256[:], cV2.GetSum())
|
|
|
|
|
|
|
|
cTZ := [64]byte{}
|
|
|
|
_, _ = rand.Read(cSHA256[:])
|
|
|
|
|
|
|
|
c.SetTillichZemor(cTZ)
|
|
|
|
|
2020-11-16 08:21:28 +00:00
|
|
|
require.Equal(t, ChecksumTZ, c.Type())
|
|
|
|
require.Equal(t, cTZ[:], c.Sum())
|
2020-09-10 11:30:29 +00:00
|
|
|
|
|
|
|
cV2 = c.ToV2()
|
|
|
|
|
|
|
|
require.Equal(t, refs.TillichZemor, cV2.GetType())
|
|
|
|
require.Equal(t, cTZ[:], cV2.GetSum())
|
|
|
|
}
|
2020-09-11 15:18:02 +00:00
|
|
|
|
|
|
|
func TestEqualChecksums(t *testing.T) {
|
|
|
|
require.True(t, EqualChecksums(nil, nil))
|
|
|
|
|
|
|
|
csSHA := [sha256.Size]byte{}
|
|
|
|
_, _ = rand.Read(csSHA[:])
|
|
|
|
|
|
|
|
cs1 := NewChecksum()
|
|
|
|
cs1.SetSHA256(csSHA)
|
|
|
|
|
|
|
|
cs2 := NewChecksum()
|
|
|
|
cs2.SetSHA256(csSHA)
|
|
|
|
|
|
|
|
require.True(t, EqualChecksums(cs1, cs2))
|
|
|
|
|
|
|
|
csSHA[0]++
|
|
|
|
cs2.SetSHA256(csSHA)
|
|
|
|
|
|
|
|
require.False(t, EqualChecksums(cs1, cs2))
|
|
|
|
}
|
2020-11-13 13:30:35 +00:00
|
|
|
|
|
|
|
func TestChecksumEncoding(t *testing.T) {
|
|
|
|
cs := NewChecksum()
|
|
|
|
cs.SetSHA256(randSHA256(t))
|
|
|
|
|
|
|
|
t.Run("binary", func(t *testing.T) {
|
|
|
|
data, err := cs.Marshal()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
c2 := NewChecksum()
|
|
|
|
require.NoError(t, c2.Unmarshal(data))
|
|
|
|
|
|
|
|
require.Equal(t, cs, c2)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("json", func(t *testing.T) {
|
|
|
|
data, err := cs.MarshalJSON()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
cs2 := NewChecksum()
|
|
|
|
require.NoError(t, cs2.UnmarshalJSON(data))
|
|
|
|
|
|
|
|
require.Equal(t, cs, cs2)
|
|
|
|
})
|
2020-12-17 15:25:17 +00:00
|
|
|
|
|
|
|
t.Run("string", func(t *testing.T) {
|
|
|
|
cs2 := NewChecksum()
|
|
|
|
|
|
|
|
require.NoError(t, cs2.Parse(cs.String()))
|
|
|
|
|
|
|
|
require.Equal(t, cs, cs2)
|
|
|
|
})
|
2020-11-13 13:30:35 +00:00
|
|
|
}
|
2021-06-08 18:02:30 +00:00
|
|
|
|
|
|
|
func TestNewChecksumFromV2(t *testing.T) {
|
|
|
|
t.Run("from nil", func(t *testing.T) {
|
|
|
|
var x *refs.Checksum
|
|
|
|
|
|
|
|
require.Nil(t, NewChecksumFromV2(x))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestChecksum_ToV2(t *testing.T) {
|
|
|
|
t.Run("nil", func(t *testing.T) {
|
|
|
|
var x *Checksum
|
|
|
|
|
|
|
|
require.Nil(t, x.ToV2())
|
|
|
|
})
|
|
|
|
}
|
2021-06-08 18:09:06 +00:00
|
|
|
|
|
|
|
func TestNewChecksum(t *testing.T) {
|
|
|
|
t.Run("default values", func(t *testing.T) {
|
|
|
|
chs := NewChecksum()
|
|
|
|
|
|
|
|
// check initial values
|
|
|
|
require.Equal(t, ChecksumUnknown, chs.Type())
|
|
|
|
require.Nil(t, chs.Sum())
|
|
|
|
|
|
|
|
// convert to v2 message
|
|
|
|
chsV2 := chs.ToV2()
|
|
|
|
|
|
|
|
require.Equal(t, refs.UnknownChecksum, chsV2.GetType())
|
|
|
|
require.Nil(t, chsV2.GetSum())
|
|
|
|
})
|
|
|
|
}
|
2021-06-18 12:27:01 +00:00
|
|
|
|
|
|
|
type enumIface interface {
|
|
|
|
FromString(string) bool
|
|
|
|
String() string
|
|
|
|
}
|
|
|
|
|
|
|
|
type enumStringItem struct {
|
|
|
|
val enumIface
|
|
|
|
str string
|
|
|
|
}
|
|
|
|
|
|
|
|
func testEnumStrings(t *testing.T, e enumIface, items []enumStringItem) {
|
|
|
|
for _, item := range items {
|
|
|
|
require.Equal(t, item.str, item.val.String())
|
|
|
|
|
|
|
|
s := item.val.String()
|
|
|
|
|
|
|
|
require.True(t, e.FromString(s), s)
|
|
|
|
|
|
|
|
require.EqualValues(t, item.val, e, item.val)
|
|
|
|
}
|
|
|
|
|
|
|
|
// incorrect strings
|
|
|
|
for _, str := range []string{
|
|
|
|
"some string",
|
|
|
|
"undefined",
|
|
|
|
} {
|
|
|
|
require.False(t, e.FromString(str))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestChecksumType_String(t *testing.T) {
|
|
|
|
toPtr := func(v ChecksumType) *ChecksumType {
|
|
|
|
return &v
|
|
|
|
}
|
|
|
|
|
|
|
|
testEnumStrings(t, new(ChecksumType), []enumStringItem{
|
|
|
|
{val: toPtr(ChecksumTZ), str: "TZ"},
|
|
|
|
{val: toPtr(ChecksumSHA256), str: "SHA256"},
|
|
|
|
{val: toPtr(ChecksumUnknown), str: "CHECKSUM_TYPE_UNSPECIFIED"},
|
|
|
|
})
|
|
|
|
}
|