forked from TrueCloudLab/frostfs-api-go
accounting: implement SignedDataSource on BalanceRequest message
This commit is contained in:
parent
b6e6aa7238
commit
b9d30d6138
4 changed files with 106 additions and 0 deletions
33
accounting/sign.go
Normal file
33
accounting/sign.go
Normal file
|
@ -0,0 +1,33 @@
|
|||
package accounting
|
||||
|
||||
import "io"
|
||||
|
||||
// SignedData returns payload bytes of the request.
|
||||
func (m BalanceRequest) 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 BalanceRequest) SignedDataSize() int {
|
||||
return m.GetOwnerID().Size()
|
||||
}
|
||||
|
||||
// ReadSignedData copies payload bytes to passed buffer.
|
||||
//
|
||||
// If the buffer size is insufficient, io.ErrUnexpectedEOF returns.
|
||||
func (m BalanceRequest) ReadSignedData(p []byte) (int, error) {
|
||||
sz := m.SignedDataSize()
|
||||
if len(p) < sz {
|
||||
return 0, io.ErrUnexpectedEOF
|
||||
}
|
||||
|
||||
copy(p, m.GetOwnerID().Bytes())
|
||||
|
||||
return sz, nil
|
||||
}
|
54
accounting/sign_test.go
Normal file
54
accounting/sign_test.go
Normal file
|
@ -0,0 +1,54 @@
|
|||
package accounting
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/nspcc-dev/neofs-api-go/service"
|
||||
"github.com/nspcc-dev/neofs-crypto/test"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestSignBalanceRequest(t *testing.T) {
|
||||
// create test OwnerID
|
||||
ownerID := OwnerID{1, 2, 3}
|
||||
|
||||
// create test BalanceRequest
|
||||
req := new(BalanceRequest)
|
||||
req.SetOwnerID(ownerID)
|
||||
|
||||
// create test private key
|
||||
sk := test.DecodeKey(0)
|
||||
|
||||
items := []struct {
|
||||
corrupt func()
|
||||
restore func()
|
||||
}{
|
||||
{
|
||||
corrupt: func() {
|
||||
ownerID[0]++
|
||||
req.SetOwnerID(ownerID)
|
||||
},
|
||||
restore: func() {
|
||||
ownerID[0]--
|
||||
req.SetOwnerID(ownerID)
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, item := range items {
|
||||
// sign with private key
|
||||
require.NoError(t, service.AddSignatureWithKey(sk, req))
|
||||
|
||||
// ascertain that verification is passed
|
||||
require.NoError(t, service.VerifyAccumulatedSignatures(req))
|
||||
|
||||
// corrupt the request
|
||||
item.corrupt()
|
||||
|
||||
// ascertain that verification is failed
|
||||
require.Error(t, service.VerifyAccumulatedSignatures(req))
|
||||
|
||||
// ascertain that request
|
||||
item.restore()
|
||||
}
|
||||
}
|
|
@ -351,3 +351,13 @@ func (m *Settlement) Equal(s *Settlement) bool {
|
|||
}
|
||||
return len(m.Transactions) == 0 || reflect.DeepEqual(m.Transactions, s.Transactions)
|
||||
}
|
||||
|
||||
// GetOwnerID is an OwnerID field getter.
|
||||
func (m BalanceRequest) GetOwnerID() OwnerID {
|
||||
return m.OwnerID
|
||||
}
|
||||
|
||||
// SetOwnerID is an OwnerID field setter.
|
||||
func (m *BalanceRequest) SetOwnerID(owner OwnerID) {
|
||||
m.OwnerID = owner
|
||||
}
|
||||
|
|
|
@ -84,3 +84,12 @@ func TestCheque(t *testing.T) {
|
|||
require.Equal(t, cheque.Amount, decimal.NewGAS(42))
|
||||
})
|
||||
}
|
||||
|
||||
func TestBalanceRequest_SetOwnerID(t *testing.T) {
|
||||
ownerID := OwnerID{1, 2, 3}
|
||||
m := new(BalanceRequest)
|
||||
|
||||
m.SetOwnerID(ownerID)
|
||||
|
||||
require.Equal(t, ownerID, m.GetOwnerID())
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue