frostfs-api-go/pkg/checksum_test.go
Leonard Lyubich 5473b4ef95 [#140] sdk: Implement checksum comparison function
Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
2020-09-18 10:45:11 +03:00

60 lines
1.1 KiB
Go

package pkg
import (
"crypto/rand"
"crypto/sha256"
"testing"
"github.com/nspcc-dev/neofs-api-go/v2/refs"
"github.com/stretchr/testify/require"
)
func TestChecksum(t *testing.T) {
c := NewChecksum()
cSHA256 := [sha256.Size]byte{}
_, _ = rand.Read(cSHA256[:])
c.SetSHA256(cSHA256)
require.Equal(t, ChecksumSHA256, c.GetType())
require.Equal(t, cSHA256[:], c.GetSum())
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)
require.Equal(t, ChecksumTZ, c.GetType())
require.Equal(t, cTZ[:], c.GetSum())
cV2 = c.ToV2()
require.Equal(t, refs.TillichZemor, cV2.GetType())
require.Equal(t, cTZ[:], cV2.GetSum())
}
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))
}