forked from TrueCloudLab/frostfs-sdk-go
[#170] checksum: Drop Empty
method
Signed-off-by: Pavel Karpy <carpawell@nspcc.ru>
This commit is contained in:
parent
168b3ee7a4
commit
96892d7bc4
6 changed files with 42 additions and 26 deletions
|
@ -127,13 +127,6 @@ func (c Checksum) String() string {
|
||||||
return fmt.Sprintf("%s:%s", c.Type(), hex.EncodeToString(v2.GetSum()))
|
return fmt.Sprintf("%s:%s", c.Type(), hex.EncodeToString(v2.GetSum()))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Empty returns true if it is called on
|
|
||||||
// zero checksum.
|
|
||||||
func (c Checksum) Empty() bool {
|
|
||||||
v2 := (refs.Checksum)(c)
|
|
||||||
return v2.GetSum() == nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// String implements fmt.Stringer.
|
// String implements fmt.Stringer.
|
||||||
//
|
//
|
||||||
// String is designed to be human-readable, and its format MAY differ between
|
// String is designed to be human-readable, and its format MAY differ between
|
||||||
|
|
|
@ -14,9 +14,11 @@ import (
|
||||||
sigutil "github.com/nspcc-dev/neofs-sdk-go/util/signature"
|
sigutil "github.com/nspcc-dev/neofs-sdk-go/util/signature"
|
||||||
)
|
)
|
||||||
|
|
||||||
var errCheckSumMismatch = errors.New("payload checksum mismatch")
|
var (
|
||||||
|
errCheckSumMismatch = errors.New("payload checksum mismatch")
|
||||||
var errIncorrectID = errors.New("incorrect object identifier")
|
errCheckSumNotSet = errors.New("payload checksum is not set")
|
||||||
|
errIncorrectID = errors.New("incorrect object identifier")
|
||||||
|
)
|
||||||
|
|
||||||
// CalculatePayloadChecksum calculates and returns checksum of
|
// CalculatePayloadChecksum calculates and returns checksum of
|
||||||
// object payload bytes.
|
// object payload bytes.
|
||||||
|
@ -39,7 +41,13 @@ func CalculateAndSetPayloadChecksum(obj *Object) {
|
||||||
// corresponds to its payload.
|
// corresponds to its payload.
|
||||||
func VerifyPayloadChecksum(obj *Object) error {
|
func VerifyPayloadChecksum(obj *Object) error {
|
||||||
actual := CalculatePayloadChecksum(obj.Payload())
|
actual := CalculatePayloadChecksum(obj.Payload())
|
||||||
if !bytes.Equal(obj.PayloadChecksum().Value(), actual.Value()) {
|
|
||||||
|
cs, set := obj.PayloadChecksum()
|
||||||
|
if !set {
|
||||||
|
return errCheckSumNotSet
|
||||||
|
}
|
||||||
|
|
||||||
|
if !bytes.Equal(cs.Value(), actual.Value()) {
|
||||||
return errCheckSumMismatch
|
return errCheckSumMismatch
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -199,20 +199,22 @@ func (o *Object) SetCreationEpoch(v uint64) {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// PayloadChecksum returns checksum of the object payload.
|
// PayloadChecksum returns checksum of the object payload and
|
||||||
|
// bool that indicates checksum presence in the object.
|
||||||
//
|
//
|
||||||
// Zero Object has zero checksum.
|
// Zero Object does not have payload checksum.
|
||||||
//
|
//
|
||||||
// See also SetPayloadChecksum.
|
// See also SetPayloadChecksum.
|
||||||
func (o *Object) PayloadChecksum() checksum.Checksum {
|
func (o *Object) PayloadChecksum() (checksum.Checksum, bool) {
|
||||||
var v checksum.Checksum
|
var v checksum.Checksum
|
||||||
v2 := (*object.Object)(o)
|
v2 := (*object.Object)(o)
|
||||||
|
|
||||||
if hash := v2.GetHeader().GetPayloadHash(); hash != nil {
|
if hash := v2.GetHeader().GetPayloadHash(); hash != nil {
|
||||||
v.ReadFromV2(*hash)
|
v.ReadFromV2(*hash)
|
||||||
|
return v, true
|
||||||
}
|
}
|
||||||
|
|
||||||
return v
|
return v, false
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetPayloadChecksum sets checksum of the object payload.
|
// SetPayloadChecksum sets checksum of the object payload.
|
||||||
|
@ -228,20 +230,22 @@ func (o *Object) SetPayloadChecksum(v checksum.Checksum) {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// PayloadHomomorphicHash returns homomorphic hash of the object payload.
|
// PayloadHomomorphicHash returns homomorphic hash of the object
|
||||||
|
// payload and bool that indicates checksum presence in the object.
|
||||||
//
|
//
|
||||||
// Zero Object has zero checksum.
|
// Zero Object does not have payload homomorphic checksum.
|
||||||
//
|
//
|
||||||
// See also SetPayloadHomomorphicHash.
|
// See also SetPayloadHomomorphicHash.
|
||||||
func (o *Object) PayloadHomomorphicHash() checksum.Checksum {
|
func (o *Object) PayloadHomomorphicHash() (checksum.Checksum, bool) {
|
||||||
var v checksum.Checksum
|
var v checksum.Checksum
|
||||||
v2 := (*object.Object)(o)
|
v2 := (*object.Object)(o)
|
||||||
|
|
||||||
if hash := v2.GetHeader().GetHomomorphicHash(); hash != nil {
|
if hash := v2.GetHeader().GetHomomorphicHash(); hash != nil {
|
||||||
v.ReadFromV2(*hash)
|
v.ReadFromV2(*hash)
|
||||||
|
return v, true
|
||||||
}
|
}
|
||||||
|
|
||||||
return v
|
return v, false
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetPayloadHomomorphicHash sets homomorphic hash of the object payload.
|
// SetPayloadHomomorphicHash sets homomorphic hash of the object payload.
|
||||||
|
|
|
@ -126,8 +126,10 @@ func TestObject_SetPayloadChecksum(t *testing.T) {
|
||||||
cs.SetSHA256(randSHA256Checksum(t))
|
cs.SetSHA256(randSHA256Checksum(t))
|
||||||
|
|
||||||
obj.SetPayloadChecksum(cs)
|
obj.SetPayloadChecksum(cs)
|
||||||
|
cs2, set := obj.PayloadChecksum()
|
||||||
|
|
||||||
require.Equal(t, cs, obj.PayloadChecksum())
|
require.True(t, set)
|
||||||
|
require.Equal(t, cs, cs2)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestObject_SetPayloadHomomorphicHash(t *testing.T) {
|
func TestObject_SetPayloadHomomorphicHash(t *testing.T) {
|
||||||
|
@ -137,8 +139,10 @@ func TestObject_SetPayloadHomomorphicHash(t *testing.T) {
|
||||||
cs.SetTillichZemor(randTZChecksum(t))
|
cs.SetTillichZemor(randTZChecksum(t))
|
||||||
|
|
||||||
obj.SetPayloadHomomorphicHash(cs)
|
obj.SetPayloadHomomorphicHash(cs)
|
||||||
|
cs2, set := obj.PayloadHomomorphicHash()
|
||||||
|
|
||||||
require.Equal(t, cs, obj.PayloadHomomorphicHash())
|
require.True(t, set)
|
||||||
|
require.Equal(t, cs, cs2)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestObject_SetAttributes(t *testing.T) {
|
func TestObject_SetAttributes(t *testing.T) {
|
||||||
|
|
|
@ -41,15 +41,18 @@ func (sg *StorageGroup) SetValidationDataSize(epoch uint64) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// ValidationDataHash returns homomorphic hash from the
|
// ValidationDataHash returns homomorphic hash from the
|
||||||
// concatenation of the payloads of the storage group members.
|
// concatenation of the payloads of the storage group members
|
||||||
|
// and bool that indicates checksum presence in the storage
|
||||||
|
// group.
|
||||||
//
|
//
|
||||||
// Zero StorageGroup has zero checksum.
|
// Zero StorageGroup does not have validation data checksum.
|
||||||
//
|
//
|
||||||
// See also SetValidationDataHash.
|
// See also SetValidationDataHash.
|
||||||
func (sg StorageGroup) ValidationDataHash() (v checksum.Checksum) {
|
func (sg StorageGroup) ValidationDataHash() (v checksum.Checksum, isSet bool) {
|
||||||
v2 := (storagegroup.StorageGroup)(sg)
|
v2 := (storagegroup.StorageGroup)(sg)
|
||||||
if checksumV2 := v2.GetValidationHash(); checksumV2 != nil {
|
if checksumV2 := v2.GetValidationHash(); checksumV2 != nil {
|
||||||
v.ReadFromV2(*checksumV2)
|
v.ReadFromV2(*checksumV2)
|
||||||
|
isSet = true
|
||||||
}
|
}
|
||||||
|
|
||||||
return
|
return
|
||||||
|
|
|
@ -21,7 +21,10 @@ func TestStorageGroup(t *testing.T) {
|
||||||
|
|
||||||
cs := checksumtest.Checksum()
|
cs := checksumtest.Checksum()
|
||||||
sg.SetValidationDataHash(cs)
|
sg.SetValidationDataHash(cs)
|
||||||
require.Equal(t, cs, sg.ValidationDataHash())
|
cs2, set := sg.ValidationDataHash()
|
||||||
|
|
||||||
|
require.True(t, set)
|
||||||
|
require.Equal(t, cs, cs2)
|
||||||
|
|
||||||
exp := uint64(33)
|
exp := uint64(33)
|
||||||
sg.SetExpirationEpoch(exp)
|
sg.SetExpirationEpoch(exp)
|
||||||
|
@ -78,7 +81,8 @@ func TestNew(t *testing.T) {
|
||||||
|
|
||||||
// check initial values
|
// check initial values
|
||||||
require.Nil(t, sg.Members())
|
require.Nil(t, sg.Members())
|
||||||
require.True(t, sg.ValidationDataHash().Empty())
|
_, set := sg.ValidationDataHash()
|
||||||
|
require.False(t, set)
|
||||||
require.Zero(t, sg.ExpirationEpoch())
|
require.Zero(t, sg.ExpirationEpoch())
|
||||||
require.Zero(t, sg.ValidationDataSize())
|
require.Zero(t, sg.ValidationDataSize())
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue