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