accounting: implement SignedDataSource on GetRequest message

This commit is contained in:
Leonard Lyubich 2020-05-11 12:54:21 +03:00
parent b9d30d6138
commit 6832d71d48
4 changed files with 142 additions and 27 deletions

View file

@ -31,3 +31,37 @@ func (m BalanceRequest) ReadSignedData(p []byte) (int, error) {
return sz, nil
}
// SignedData returns payload bytes of the request.
func (m GetRequest) 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 GetRequest) SignedDataSize() int {
return m.GetID().Size() + m.GetOwnerID().Size()
}
// ReadSignedData copies payload bytes to passed buffer.
//
// If the buffer size is insufficient, io.ErrUnexpectedEOF returns.
func (m GetRequest) ReadSignedData(p []byte) (int, error) {
sz := m.SignedDataSize()
if len(p) < sz {
return 0, io.ErrUnexpectedEOF
}
var off int
off += copy(p[off:], m.GetID().Bytes())
copy(p[off:], m.GetOwnerID().Bytes())
return sz, nil
}

View file

@ -9,46 +9,87 @@ import (
)
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)
type sigType interface {
service.SignedDataWithToken
service.SignKeyPairAccumulator
service.SignKeyPairSource
SetToken(*service.Token)
}
items := []struct {
corrupt func()
restore func()
constructor func() sigType
payloadCorrupt []func(sigType)
}{
{
corrupt: func() {
ownerID[0]++
req.SetOwnerID(ownerID)
{ // BalanceRequest
constructor: func() sigType {
return new(BalanceRequest)
},
payloadCorrupt: []func(sigType){
func(s sigType) {
req := s.(*BalanceRequest)
owner := req.GetOwnerID()
owner[0]++
req.SetOwnerID(owner)
},
},
},
{ // GetRequest
constructor: func() sigType {
return new(GetRequest)
},
payloadCorrupt: []func(sigType){
func(s sigType) {
req := s.(*GetRequest)
id, err := NewChequeID()
require.NoError(t, err)
req.SetID(id)
},
func(s sigType) {
req := s.(*GetRequest)
id := req.GetOwnerID()
id[0]++
req.SetOwnerID(id)
},
restore: func() {
ownerID[0]--
req.SetOwnerID(ownerID)
},
},
}
for _, item := range items {
// sign with private key
require.NoError(t, service.AddSignatureWithKey(sk, req))
{ // token corruptions
v := item.constructor()
// ascertain that verification is passed
require.NoError(t, service.VerifyAccumulatedSignatures(req))
token := new(service.Token)
v.SetToken(token)
// corrupt the request
item.corrupt()
require.NoError(t, service.SignDataWithSessionToken(sk, v))
// ascertain that verification is failed
require.Error(t, service.VerifyAccumulatedSignatures(req))
require.NoError(t, service.VerifyAccumulatedSignaturesWithToken(v))
// ascertain that request
item.restore()
token.SetSessionKey(append(token.GetSessionKey(), 1))
require.Error(t, service.VerifyAccumulatedSignaturesWithToken(v))
}
{ // payload corruptions
for _, corruption := range item.payloadCorrupt {
v := item.constructor()
require.NoError(t, service.SignDataWithSessionToken(sk, v))
require.NoError(t, service.VerifyAccumulatedSignaturesWithToken(v))
corruption(v)
require.Error(t, service.VerifyAccumulatedSignaturesWithToken(v))
}
}
}
}

View file

@ -361,3 +361,23 @@ func (m BalanceRequest) GetOwnerID() OwnerID {
func (m *BalanceRequest) SetOwnerID(owner OwnerID) {
m.OwnerID = owner
}
// GetID is an ID field getter.
func (m GetRequest) GetID() ChequeID {
return m.ID
}
// SetID is an ID field setter.
func (m *GetRequest) SetID(id ChequeID) {
m.ID = id
}
// GetOwnerID is an OwnerID field getter.
func (m GetRequest) GetOwnerID() OwnerID {
return m.OwnerID
}
// SetOwnerID is an OwnerID field setter.
func (m *GetRequest) SetOwnerID(id OwnerID) {
m.OwnerID = id
}

View file

@ -93,3 +93,23 @@ func TestBalanceRequest_SetOwnerID(t *testing.T) {
require.Equal(t, ownerID, m.GetOwnerID())
}
func TestGetRequestGettersSetters(t *testing.T) {
t.Run("id", func(t *testing.T) {
id := ChequeID("test id")
m := new(GetRequest)
m.SetID(id)
require.Equal(t, id, m.GetID())
})
t.Run("owner", func(t *testing.T) {
id := OwnerID{1, 2, 3}
m := new(GetRequest)
m.SetOwnerID(id)
require.Equal(t, id, m.GetOwnerID())
})
}