From 166e5440abfbb3e05eeb5d9c9e7382bd48cb8188 Mon Sep 17 00:00:00 2001 From: Leonard Lyubich Date: Wed, 26 May 2021 13:55:58 +0300 Subject: [PATCH] [#525] event/container: Parse session token from Delete notification The 3rd item of `Delete` container notification event is a byte array of serialized session token. Parse session token in `ParseDelete` function. Provide `Delete.SessionToken` method. Signed-off-by: Leonard Lyubich --- pkg/morph/event/container/delete.go | 21 +++++++++++++++++++-- pkg/morph/event/container/delete_test.go | 15 ++++++++++++++- 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/pkg/morph/event/container/delete.go b/pkg/morph/event/container/delete.go index 56fc7be0..931de221 100644 --- a/pkg/morph/event/container/delete.go +++ b/pkg/morph/event/container/delete.go @@ -12,6 +12,7 @@ import ( type Delete struct { containerID []byte signature []byte + token []byte } // MorphEvent implements Neo:Morph Event interface. @@ -23,15 +24,25 @@ func (d Delete) ContainerID() []byte { return d.containerID } // Signature of marshalled container by container owner. func (d Delete) Signature() []byte { return d.signature } +// SessionToken returns binary token of the session +// within which the eACL was set. +func (d Delete) SessionToken() []byte { + return d.token +} + // ParseDelete from notification into container event structure. +// +// Expects 3 stack items. func ParseDelete(params []stackitem.Item) (event.Event, error) { var ( ev Delete err error ) - if ln := len(params); ln != 2 { - return nil, event.WrongNumberOfParameters(2, ln) + const expectedItemNumEACL = 3 + + if ln := len(params); ln != expectedItemNumEACL { + return nil, event.WrongNumberOfParameters(expectedItemNumEACL, ln) } // parse container @@ -46,5 +57,11 @@ func ParseDelete(params []stackitem.Item) (event.Event, error) { return nil, fmt.Errorf("could not get signature: %w", err) } + // parse session token + ev.token, err = client.BytesFromStackItem(params[2]) + if err != nil { + return nil, fmt.Errorf("could not get session token: %w", err) + } + return ev, nil } diff --git a/pkg/morph/event/container/delete_test.go b/pkg/morph/event/container/delete_test.go index 1ac309f3..37f5edef 100644 --- a/pkg/morph/event/container/delete_test.go +++ b/pkg/morph/event/container/delete_test.go @@ -12,6 +12,7 @@ func TestParseDelete(t *testing.T) { var ( containerID = []byte("containreID") signature = []byte("signature") + token = []byte("token") ) t.Run("wrong number of parameters", func(t *testing.T) { @@ -20,7 +21,7 @@ func TestParseDelete(t *testing.T) { } _, err := ParseDelete(prms) - require.EqualError(t, err, event.WrongNumberOfParameters(2, len(prms)).Error()) + require.EqualError(t, err, event.WrongNumberOfParameters(3, len(prms)).Error()) }) t.Run("wrong container parameter", func(t *testing.T) { @@ -40,10 +41,21 @@ func TestParseDelete(t *testing.T) { require.Error(t, err) }) + t.Run("wrong session token parameter", func(t *testing.T) { + _, err := ParseDelete([]stackitem.Item{ + stackitem.NewByteArray(containerID), + stackitem.NewByteArray(signature), + stackitem.NewMap(), + }) + + require.Error(t, err) + }) + t.Run("correct behavior", func(t *testing.T) { ev, err := ParseDelete([]stackitem.Item{ stackitem.NewByteArray(containerID), stackitem.NewByteArray(signature), + stackitem.NewByteArray(token), }) require.NoError(t, err) @@ -51,6 +63,7 @@ func TestParseDelete(t *testing.T) { require.Equal(t, Delete{ containerID: containerID, signature: signature, + token: token, }, ev) }) }