session: implement SignedDataSource on CreateRequest

This commit is contained in:
Leonard Lyubich 2020-05-08 11:55:19 +03:00
parent 1932658a7d
commit 6d71ea239b
2 changed files with 110 additions and 0 deletions

View file

@ -1,5 +1,19 @@
package session
import (
"encoding/binary"
"io"
"github.com/nspcc-dev/neofs-api-go/refs"
)
const signedRequestDataSize = 0 +
refs.OwnerIDSize +
8 +
8
var requestEndianness = binary.BigEndian
// GetOwnerID is an OwnerID field getter.
func (m CreateRequest) GetOwnerID() OwnerID {
return m.OwnerID
@ -9,3 +23,36 @@ func (m CreateRequest) GetOwnerID() OwnerID {
func (m *CreateRequest) SetOwnerID(id OwnerID) {
m.OwnerID = id
}
func (m CreateRequest) SignedData() ([]byte, error) {
data := make([]byte, m.SignedDataSize())
_, err := m.ReadSignedData(data)
if err != nil {
return nil, err
}
return data, nil
}
func (m CreateRequest) SignedDataSize() int {
return signedRequestDataSize
}
func (m CreateRequest) ReadSignedData(p []byte) (int, error) {
sz := m.SignedDataSize()
if len(p) < sz {
return 0, io.ErrUnexpectedEOF
}
var off int
off += copy(p[off:], m.GetOwnerID().Bytes())
requestEndianness.PutUint64(p[off:], m.CreationEpoch())
off += 8
requestEndianness.PutUint64(p[off:], m.ExpirationEpoch())
return sz, nil
}

View file

@ -27,3 +27,66 @@ func TestCreateRequestGettersSetters(t *testing.T) {
require.Equal(t, e2, m.ExpirationEpoch())
})
}
func TestCreateRequest_SignedData(t *testing.T) {
var (
id = OwnerID{1, 2, 3}
e1 = uint64(1)
e2 = uint64(2)
)
// create new message
m := new(CreateRequest)
// fill the fields
m.SetOwnerID(id)
m.SetCreationEpoch(e1)
m.SetExpirationEpoch(e2)
// calculate initial signed data
d, err := m.SignedData()
require.NoError(t, err)
items := []struct {
change func()
reset func()
}{
{ // OwnerID
change: func() {
id2 := id
id2[0]++
m.SetOwnerID(id2)
},
reset: func() {
m.SetOwnerID(id)
},
},
{ // CreationEpoch
change: func() {
m.SetCreationEpoch(e1 + 1)
},
reset: func() {
m.SetCreationEpoch(e1)
},
},
{ // ExpirationEpoch
change: func() {
m.SetExpirationEpoch(e2 + 1)
},
reset: func() {
m.SetExpirationEpoch(e2)
},
},
}
for _, item := range items {
item.change()
d2, err := m.SignedData()
require.NoError(t, err)
require.NotEqual(t, d, d2)
item.reset()
}
}