From 4b55b0678013e4b49b20437e95877c12886ef043 Mon Sep 17 00:00:00 2001 From: Leonard Lyubich Date: Fri, 13 Nov 2020 16:23:41 +0300 Subject: [PATCH] [#168] refs: Implement binary/JSON encoders/decoders on Signature Signed-off-by: Leonard Lyubich --- pkg/signature.go | 32 ++++++++++++++++++++++++++++++++ pkg/signature_test.go | 33 +++++++++++++++++++++++++++++++++ v2/refs/json.go | 20 ++++++++++++++++++++ v2/refs/json_test.go | 12 ++++++++++++ v2/refs/marshal.go | 11 +++++++++++ v2/refs/marshal_test.go | 6 ++---- 6 files changed, 110 insertions(+), 4 deletions(-) create mode 100644 pkg/signature_test.go diff --git a/pkg/signature.go b/pkg/signature.go index 7322c50..992b183 100644 --- a/pkg/signature.go +++ b/pkg/signature.go @@ -42,3 +42,35 @@ func (s *Signature) SetSign(v []byte) { func (s *Signature) ToV2() *refs.Signature { return (*refs.Signature)(s) } + +// Marshal marshals Signature into a protobuf binary form. +// +// Buffer is allocated when the argument is empty. +// Otherwise, the first buffer is used. +func (s *Signature) Marshal(b ...[]byte) ([]byte, error) { + var buf []byte + if len(b) > 0 { + buf = b[0] + } + + return (*refs.Signature)(s). + StableMarshal(buf) +} + +// Unmarshal unmarshals protobuf binary representation of Signature. +func (s *Signature) Unmarshal(data []byte) error { + return (*refs.Signature)(s). + Unmarshal(data) +} + +// MarshalJSON encodes Signature to protobuf JSON format. +func (s *Signature) MarshalJSON() ([]byte, error) { + return (*refs.Signature)(s). + MarshalJSON() +} + +// UnmarshalJSON decodes Signature from protobuf JSON format. +func (s *Signature) UnmarshalJSON(data []byte) error { + return (*refs.Signature)(s). + UnmarshalJSON(data) +} diff --git a/pkg/signature_test.go b/pkg/signature_test.go new file mode 100644 index 0000000..72196c8 --- /dev/null +++ b/pkg/signature_test.go @@ -0,0 +1,33 @@ +package pkg + +import ( + "testing" + + "github.com/stretchr/testify/require" +) + +func TestSignatureEncoding(t *testing.T) { + s := NewSignature() + s.SetKey([]byte("key")) + s.SetSign([]byte("sign")) + + t.Run("binary", func(t *testing.T) { + data, err := s.Marshal() + require.NoError(t, err) + + s2 := NewSignature() + require.NoError(t, s2.Unmarshal(data)) + + require.Equal(t, s, s2) + }) + + t.Run("json", func(t *testing.T) { + data, err := s.MarshalJSON() + require.NoError(t, err) + + s2 := NewSignature() + require.NoError(t, s2.UnmarshalJSON(data)) + + require.Equal(t, s, s2) + }) +} diff --git a/v2/refs/json.go b/v2/refs/json.go index 3fe253e..dd0f836 100644 --- a/v2/refs/json.go +++ b/v2/refs/json.go @@ -104,3 +104,23 @@ func (v *Version) UnmarshalJSON(data []byte) error { return nil } + +func (s *Signature) MarshalJSON() ([]byte, error) { + return protojson.MarshalOptions{ + EmitUnpopulated: true, + }.Marshal( + SignatureToGRPCMessage(s), + ) +} + +func (s *Signature) UnmarshalJSON(data []byte) error { + msg := new(refs.Signature) + + if err := protojson.Unmarshal(data, msg); err != nil { + return err + } + + *s = *SignatureFromGRPCMessage(msg) + + return nil +} diff --git a/v2/refs/json_test.go b/v2/refs/json_test.go index ae41e9f..5671ed2 100644 --- a/v2/refs/json_test.go +++ b/v2/refs/json_test.go @@ -69,3 +69,15 @@ func TestVersionSON(t *testing.T) { require.Equal(t, v, v2) } + +func TestSignatureSON(t *testing.T) { + s := generateSignature("key", "sig") + + data, err := s.MarshalJSON() + require.NoError(t, err) + + s2 := new(refs.Signature) + require.NoError(t, s2.UnmarshalJSON(data)) + + require.Equal(t, s, s2) +} diff --git a/v2/refs/marshal.go b/v2/refs/marshal.go index 353729c..5f2358b 100644 --- a/v2/refs/marshal.go +++ b/v2/refs/marshal.go @@ -266,6 +266,17 @@ func (s *Signature) StableSize() (size int) { return size } +func (s *Signature) Unmarshal(data []byte) error { + m := new(refs.Signature) + if err := goproto.Unmarshal(data, m); err != nil { + return err + } + + *s = *SignatureFromGRPCMessage(m) + + return nil +} + func (v *Version) StableMarshal(buf []byte) ([]byte, error) { if v == nil { return []byte{}, nil diff --git a/v2/refs/marshal_test.go b/v2/refs/marshal_test.go index f92e361..3cead07 100644 --- a/v2/refs/marshal_test.go +++ b/v2/refs/marshal_test.go @@ -95,16 +95,14 @@ func TestChecksum_StableMarshal(t *testing.T) { func TestSignature_StableMarshal(t *testing.T) { signatureFrom := generateSignature("Public Key", "Signature") - transport := new(grpc.Signature) t.Run("non empty", func(t *testing.T) { wire, err := signatureFrom.StableMarshal(nil) require.NoError(t, err) - err = goproto.Unmarshal(wire, transport) - require.NoError(t, err) + signatureTo := new(refs.Signature) + require.NoError(t, signatureTo.Unmarshal(wire)) - signatureTo := refs.SignatureFromGRPCMessage(transport) require.Equal(t, signatureFrom, signatureTo) }) }