forked from TrueCloudLab/frostfs-api-go
[#140] sdk: Implement checksum comparison function
Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
This commit is contained in:
parent
6ce70d4a18
commit
5473b4ef95
2 changed files with 25 additions and 0 deletions
|
@ -1,6 +1,7 @@
|
|||
package pkg
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/sha256"
|
||||
|
||||
"github.com/nspcc-dev/neofs-api-go/v2/refs"
|
||||
|
@ -72,3 +73,7 @@ func (c *Checksum) SetTillichZemor(v [64]byte) {
|
|||
func (c *Checksum) ToV2() *refs.Checksum {
|
||||
return (*refs.Checksum)(c)
|
||||
}
|
||||
|
||||
func EqualChecksums(cs1, cs2 *Checksum) bool {
|
||||
return cs1.GetType() == cs2.GetType() && bytes.Equal(cs1.GetSum(), cs2.GetSum())
|
||||
}
|
||||
|
|
|
@ -38,3 +38,23 @@ func TestChecksum(t *testing.T) {
|
|||
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))
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue