[#168] object: Implement binary/JSON encoders/decoders on HeaderWithSig

Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
This commit is contained in:
Leonard Lyubich 2020-11-13 15:43:02 +03:00 committed by Alex Vanin
parent b8f86f1a60
commit 9cdd14841a
4 changed files with 66 additions and 5 deletions

View file

@ -84,3 +84,23 @@ func (h *Header) UnmarshalJSON(data []byte) error {
return nil
}
func (h *HeaderWithSignature) MarshalJSON() ([]byte, error) {
return protojson.MarshalOptions{
EmitUnpopulated: true,
}.Marshal(
HeaderWithSignatureToGRPCMessage(h),
)
}
func (h *HeaderWithSignature) UnmarshalJSON(data []byte) error {
msg := new(object.HeaderWithSignature)
if err := protojson.Unmarshal(data, msg); err != nil {
return err
}
*h = *HeaderWithSignatureFromGRPCMessage(msg)
return nil
}

View file

@ -54,3 +54,15 @@ func TestHeaderJSON(t *testing.T) {
require.Equal(t, h, h2)
}
func TestHeaderWithSignatureJSON(t *testing.T) {
h := generateHeaderWithSignature()
data, err := h.MarshalJSON()
require.NoError(t, err)
h2 := new(object.HeaderWithSignature)
require.NoError(t, h2.UnmarshalJSON(data))
require.Equal(t, h, h2)
}

View file

@ -475,6 +475,17 @@ func (h *HeaderWithSignature) StableSize() (size int) {
return size
}
func (h *HeaderWithSignature) Unmarshal(data []byte) error {
m := new(object.HeaderWithSignature)
if err := goproto.Unmarshal(data, m); err != nil {
return err
}
*h = *HeaderWithSignatureFromGRPCMessage(m)
return nil
}
func (o *Object) StableMarshal(buf []byte) ([]byte, error) {
if o == nil {
return []byte{}, nil

View file

@ -352,6 +352,20 @@ func TestGetRangeHashResponseBody_StableMarshal(t *testing.T) {
})
}
func TestHeaderWithSignature_StableMarshal(t *testing.T) {
from := generateHeaderWithSignature()
t.Run("non empty", func(t *testing.T) {
wire, err := from.StableMarshal(nil)
require.NoError(t, err)
to := new(object.HeaderWithSignature)
require.NoError(t, to.Unmarshal(wire))
require.Equal(t, from, to)
})
}
func generateOwner(id string) *refs.OwnerID {
owner := new(refs.OwnerID)
owner.SetValue([]byte(id))
@ -582,12 +596,8 @@ func generateHeadResponseBody(flag bool) *object.HeadResponseBody {
short.SetShortHeader(generateShortHeader("short id"))
part = short
} else {
hdrWithSig := new(object.HeaderWithSignature)
hdrWithSig.SetHeader(generateHeader(30))
hdrWithSig.SetSignature(generateSignature("sig", "key"))
full := new(object.GetHeaderPartFull)
full.SetHeaderWithSignature(hdrWithSig)
full.SetHeaderWithSignature(generateHeaderWithSignature())
part = full
}
@ -596,6 +606,14 @@ func generateHeadResponseBody(flag bool) *object.HeadResponseBody {
return req
}
func generateHeaderWithSignature() *object.HeaderWithSignature {
hdrWithSig := new(object.HeaderWithSignature)
hdrWithSig.SetHeader(generateHeader(30))
hdrWithSig.SetSignature(generateSignature("sig", "key"))
return hdrWithSig
}
func generateFilter(k, v string) *object.SearchFilter {
f := new(object.SearchFilter)
f.SetKey(k)