container: implement SignedDataSource on ListRequest message

This commit is contained in:
Leonard Lyubich 2020-05-11 15:17:11 +03:00
parent a41f22782b
commit f91adcb560
4 changed files with 69 additions and 2 deletions

View file

@ -77,7 +77,7 @@ func (m DeleteRequest) SignedData() ([]byte, error) {
} }
// SignedDataSize returns payload size of the request. // SignedDataSize returns payload size of the request.
func (m DeleteRequest) SignedDataSize() (sz int) { func (m DeleteRequest) SignedDataSize() int {
return m.GetCID().Size() return m.GetCID().Size()
} }
@ -108,7 +108,7 @@ func (m GetRequest) SignedData() ([]byte, error) {
} }
// SignedDataSize returns payload size of the request. // SignedDataSize returns payload size of the request.
func (m GetRequest) SignedDataSize() (sz int) { func (m GetRequest) SignedDataSize() int {
return m.GetCID().Size() return m.GetCID().Size()
} }
@ -126,3 +126,34 @@ func (m GetRequest) ReadSignedData(p []byte) (int, error) {
return off, nil return off, nil
} }
// SignedData returns payload bytes of the request.
func (m ListRequest) SignedData() ([]byte, error) {
data := make([]byte, m.SignedDataSize())
if _, err := m.ReadSignedData(data); err != nil {
return nil, err
}
return data, nil
}
// SignedDataSize returns payload size of the request.
func (m ListRequest) SignedDataSize() int {
return m.GetOwnerID().Size()
}
// ReadSignedData copies payload bytes to passed buffer.
//
// If the Request size is insufficient, io.ErrUnexpectedEOF returns.
func (m ListRequest) ReadSignedData(p []byte) (int, error) {
if len(p) < m.SignedDataSize() {
return 0, io.ErrUnexpectedEOF
}
var off int
off += copy(p[off:], m.GetOwnerID().Bytes())
return off, nil
}

View file

@ -93,6 +93,21 @@ func TestRequestSign(t *testing.T) {
}, },
}, },
}, },
{ // ListRequest
constructor: func() sigType {
return new(ListRequest)
},
payloadCorrupt: []func(sigType){
func(s sigType) {
req := s.(*ListRequest)
owner := req.GetOwnerID()
owner[0]++
req.SetOwnerID(owner)
},
},
},
} }
for _, item := range items { for _, item := range items {

View file

@ -148,3 +148,13 @@ func (m GetRequest) GetCID() CID {
func (m *GetRequest) SetCID(cid CID) { func (m *GetRequest) SetCID(cid CID) {
m.CID = cid m.CID = cid
} }
// GetOwnerID is an OwnerID field getter.
func (m ListRequest) GetOwnerID() OwnerID {
return m.OwnerID
}
// SetOwnerID is an OwnerID field setter.
func (m *ListRequest) SetOwnerID(owner OwnerID) {
m.OwnerID = owner
}

View file

@ -129,3 +129,14 @@ func TestGetRequestGettersSetters(t *testing.T) {
require.Equal(t, cid, m.GetCID()) require.Equal(t, cid, m.GetCID())
}) })
} }
func TestListRequestGettersSetters(t *testing.T) {
t.Run("owner", func(t *testing.T) {
owner := OwnerID{1, 2, 3}
m := new(PutRequest)
m.SetOwnerID(owner)
require.Equal(t, owner, m.GetOwnerID())
})
}