[#525] event/container: Parse binary session token from Put notification

The 4th item of `Put` container notification event is a byte array of
serialized session token.

Parse session token in `ParsePut` function. Provide `Put.SessionToken`
method.

Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
This commit is contained in:
Leonard Lyubich 2021-05-25 18:52:55 +03:00 committed by Leonard Lyubich
parent 98cc685a9b
commit 0f91b78df1
2 changed files with 27 additions and 1 deletions

View file

@ -13,6 +13,7 @@ type Put struct {
rawContainer []byte rawContainer []byte
signature []byte signature []byte
publicKey []byte publicKey []byte
token []byte
} }
const expectedItemNumPut = 4 const expectedItemNumPut = 4
@ -29,6 +30,12 @@ func (p Put) Signature() []byte { return p.signature }
// PublicKey of container owner. // PublicKey of container owner.
func (p Put) PublicKey() []byte { return p.publicKey } func (p Put) PublicKey() []byte { return p.publicKey }
// Session token returns binary token of the session
// within which the container was created.
func (p Put) SessionToken() []byte {
return p.token
}
// ParsePut from notification into container event structure. // ParsePut from notification into container event structure.
func ParsePut(params []stackitem.Item) (event.Event, error) { func ParsePut(params []stackitem.Item) (event.Event, error) {
var ( var (
@ -58,5 +65,11 @@ func ParsePut(params []stackitem.Item) (event.Event, error) {
return nil, fmt.Errorf("could not get public key: %w", err) return nil, fmt.Errorf("could not get public key: %w", err)
} }
// parse session token
ev.token, err = client.BytesFromStackItem(params[3])
if err != nil {
return nil, fmt.Errorf("could not get sesison token: %w", err)
}
return ev, nil return ev, nil
} }

View file

@ -13,6 +13,7 @@ func TestParsePut(t *testing.T) {
containerData = []byte("containerData") containerData = []byte("containerData")
signature = []byte("signature") signature = []byte("signature")
publicKey = []byte("pubkey") publicKey = []byte("pubkey")
token = []byte("token")
) )
t.Run("wrong number of parameters", func(t *testing.T) { t.Run("wrong number of parameters", func(t *testing.T) {
@ -52,12 +53,23 @@ func TestParsePut(t *testing.T) {
require.Error(t, err) require.Error(t, err)
}) })
t.Run("wrong session token parameter", func(t *testing.T) {
_, err := ParsePut([]stackitem.Item{
stackitem.NewByteArray(containerData),
stackitem.NewByteArray(signature),
stackitem.NewByteArray(publicKey),
stackitem.NewMap(),
})
require.Error(t, err)
})
t.Run("correct behavior", func(t *testing.T) { t.Run("correct behavior", func(t *testing.T) {
ev, err := ParsePut([]stackitem.Item{ ev, err := ParsePut([]stackitem.Item{
stackitem.NewByteArray(containerData), stackitem.NewByteArray(containerData),
stackitem.NewByteArray(signature), stackitem.NewByteArray(signature),
stackitem.NewByteArray(publicKey), stackitem.NewByteArray(publicKey),
stackitem.NewMap(), stackitem.NewByteArray(token),
}) })
require.NoError(t, err) require.NoError(t, err)
@ -65,6 +77,7 @@ func TestParsePut(t *testing.T) {
rawContainer: containerData, rawContainer: containerData,
signature: signature, signature: signature,
publicKey: publicKey, publicKey: publicKey,
token: token,
}, ev) }, ev)
}) })
} }