From 5880875bd97f003e628412e8324cfd96d6df0bef Mon Sep 17 00:00:00 2001 From: Alex Vanin Date: Mon, 17 Aug 2020 16:09:09 +0300 Subject: [PATCH] Add stable marshal of target info in acl package Signed-off-by: Alex Vanin --- v2/acl/marshal.go | 43 ++++++++++++++++++++++++++++++++++++++---- v2/acl/marshal_test.go | 22 +++++++++++++++++++++ 2 files changed, 61 insertions(+), 4 deletions(-) diff --git a/v2/acl/marshal.go b/v2/acl/marshal.go index 8397696..d680b9c 100644 --- a/v2/acl/marshal.go +++ b/v2/acl/marshal.go @@ -9,6 +9,9 @@ const ( FilterMatchTypeField = 2 FilterNameField = 3 FilterValueField = 4 + + TargetTypeField = 1 + TargetKeysField = 2 ) func (t *Table) StableMarshal(buf []byte) ([]byte, error) { @@ -83,10 +86,42 @@ func (f *HeaderFilter) StableSize() (size int) { return size } -func (t *HeaderType) StableMarshal(buf []byte) ([]byte, error) { - panic("not implemented") +func (t *TargetInfo) StableMarshal(buf []byte) ([]byte, error) { + if t == nil { + return []byte{}, nil + } + + if buf == nil { + buf = make([]byte, t.StableSize()) + } + + var ( + offset, n int + err error + ) + + n, err = proto.EnumMarshal(TargetTypeField, buf, int32(t.target)) + if err != nil { + return nil, err + } + + offset += n + + n, err = proto.RepeatedBytesMarshal(TargetKeysField, buf[offset:], t.keys) + if err != nil { + return nil, err + } + + return buf, nil } -func (t *HeaderType) StableSize() int { - panic("not implemented") +func (t *TargetInfo) StableSize() (size int) { + if t == nil { + return 0 + } + + size += proto.EnumSize(TargetTypeField, int32(t.target)) + size += proto.RepeatedBytesSize(TargetKeysField, t.keys) + + return size } diff --git a/v2/acl/marshal_test.go b/v2/acl/marshal_test.go index 63f5bcf..c040d26 100644 --- a/v2/acl/marshal_test.go +++ b/v2/acl/marshal_test.go @@ -28,3 +28,25 @@ func TestHeaderFilter_StableMarshal(t *testing.T) { require.Equal(t, filterFrom, filterTo) }) } + +func TestTargetInfo_StableMarshal(t *testing.T) { + targetFrom := new(acl.TargetInfo) + transport := new(grpc.EACLRecord_TargetInfo) + + t.Run("non empty", func(t *testing.T) { + targetFrom.SetTarget(acl.TargetUser) + targetFrom.SetKeyList([][]byte{ + []byte("Public Key 1"), + []byte("Public Key 2"), + }) + + wire, err := targetFrom.StableMarshal(nil) + require.NoError(t, err) + + err = transport.Unmarshal(wire) + require.NoError(t, err) + + targetTo := acl.TargetInfoFromGRPCMessage(transport) + require.Equal(t, targetFrom, targetTo) + }) +}