forked from TrueCloudLab/frostfs-api-go
state: implement SignedDataSource on ChangeStateRequest message
This commit is contained in:
parent
5545b25a95
commit
ab198b4049
4 changed files with 89 additions and 0 deletions
|
@ -1,5 +1,9 @@
|
|||
package state
|
||||
|
||||
import (
|
||||
"io"
|
||||
)
|
||||
|
||||
// SignedData returns payload bytes of the request.
|
||||
//
|
||||
// Always returns an empty slice.
|
||||
|
@ -34,3 +38,34 @@ func (m DumpRequest) SignedData() ([]byte, error) {
|
|||
func (m DumpVarsRequest) SignedData() ([]byte, error) {
|
||||
return make([]byte, 0), nil
|
||||
}
|
||||
|
||||
// SignedData returns payload bytes of the request.
|
||||
func (m ChangeStateRequest) 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 ChangeStateRequest) SignedDataSize() int {
|
||||
return m.GetState().Size()
|
||||
}
|
||||
|
||||
// ReadSignedData copies payload bytes to passed buffer.
|
||||
//
|
||||
// If the Request size is insufficient, io.ErrUnexpectedEOF returns.
|
||||
func (m ChangeStateRequest) ReadSignedData(p []byte) (int, error) {
|
||||
if len(p) < m.SignedDataSize() {
|
||||
return 0, io.ErrUnexpectedEOF
|
||||
}
|
||||
|
||||
var off int
|
||||
|
||||
off += copy(p[off:], m.GetState().Bytes())
|
||||
|
||||
return off, nil
|
||||
}
|
||||
|
|
|
@ -47,6 +47,18 @@ func TestRequestSign(t *testing.T) {
|
|||
return new(DumpVarsRequest)
|
||||
},
|
||||
},
|
||||
{
|
||||
constructor: func() sigType {
|
||||
return new(ChangeStateRequest)
|
||||
},
|
||||
payloadCorrupt: []func(sigType){
|
||||
func(s sigType) {
|
||||
req := s.(*ChangeStateRequest)
|
||||
|
||||
req.SetState(req.GetState() + 1)
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, item := range items {
|
||||
|
|
24
state/types.go
Normal file
24
state/types.go
Normal file
|
@ -0,0 +1,24 @@
|
|||
package state
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
)
|
||||
|
||||
// SetState is a State field setter.
|
||||
func (m *ChangeStateRequest) SetState(st ChangeStateRequest_State) {
|
||||
m.State = st
|
||||
}
|
||||
|
||||
// Size returns the size of the state binary representation.
|
||||
func (ChangeStateRequest_State) Size() int {
|
||||
return 4
|
||||
}
|
||||
|
||||
// Bytes returns the state binary representation.
|
||||
func (x ChangeStateRequest_State) Bytes() []byte {
|
||||
data := make([]byte, x.Size())
|
||||
|
||||
binary.BigEndian.PutUint32(data, uint32(x))
|
||||
|
||||
return data
|
||||
}
|
18
state/types_test.go
Normal file
18
state/types_test.go
Normal file
|
@ -0,0 +1,18 @@
|
|||
package state
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestChangeStateRequestGettersSetters(t *testing.T) {
|
||||
t.Run("state", func(t *testing.T) {
|
||||
st := ChangeStateRequest_State(1)
|
||||
m := new(ChangeStateRequest)
|
||||
|
||||
m.SetState(st)
|
||||
|
||||
require.Equal(t, st, m.GetState())
|
||||
})
|
||||
}
|
Loading…
Reference in a new issue