Add stable marshal of target info in acl package

Signed-off-by: Alex Vanin <alexey@nspcc.ru>
This commit is contained in:
Alex Vanin 2020-08-17 16:09:09 +03:00 committed by Stanislav Bogatyrev
parent 5623ce0124
commit 5880875bd9
2 changed files with 61 additions and 4 deletions

View file

@ -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
}
func (t *HeaderType) StableSize() int {
panic("not implemented")
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 *TargetInfo) StableSize() (size int) {
if t == nil {
return 0
}
size += proto.EnumSize(TargetTypeField, int32(t.target))
size += proto.RepeatedBytesSize(TargetKeysField, t.keys)
return size
}

View file

@ -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)
})
}