2020-05-11 08:59:39 +00:00
|
|
|
package accounting
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2020-05-11 10:14:31 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/decimal"
|
2020-05-11 08:59:39 +00:00
|
|
|
"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) {
|
|
|
|
sk := test.DecodeKey(0)
|
|
|
|
|
2020-05-11 09:54:21 +00:00
|
|
|
type sigType interface {
|
|
|
|
service.SignedDataWithToken
|
|
|
|
service.SignKeyPairAccumulator
|
|
|
|
service.SignKeyPairSource
|
|
|
|
SetToken(*service.Token)
|
|
|
|
}
|
|
|
|
|
2020-05-11 08:59:39 +00:00
|
|
|
items := []struct {
|
2020-05-11 09:54:21 +00:00
|
|
|
constructor func() sigType
|
|
|
|
payloadCorrupt []func(sigType)
|
2020-05-11 08:59:39 +00:00
|
|
|
}{
|
2020-05-11 09:54:21 +00:00
|
|
|
{ // BalanceRequest
|
|
|
|
constructor: func() sigType {
|
|
|
|
return new(BalanceRequest)
|
2020-05-11 08:59:39 +00:00
|
|
|
},
|
2020-05-11 09:54:21 +00:00
|
|
|
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)
|
|
|
|
},
|
2020-05-11 08:59:39 +00:00
|
|
|
},
|
|
|
|
},
|
2020-05-11 10:14:31 +00:00
|
|
|
{ // PutRequest
|
|
|
|
constructor: func() sigType {
|
|
|
|
req := new(PutRequest)
|
|
|
|
|
|
|
|
amount := decimal.New(1)
|
|
|
|
req.SetAmount(amount)
|
|
|
|
|
|
|
|
return req
|
|
|
|
},
|
|
|
|
payloadCorrupt: []func(sigType){
|
|
|
|
func(s sigType) {
|
|
|
|
req := s.(*PutRequest)
|
|
|
|
|
|
|
|
owner := req.GetOwnerID()
|
|
|
|
owner[0]++
|
|
|
|
|
|
|
|
req.SetOwnerID(owner)
|
|
|
|
},
|
|
|
|
func(s sigType) {
|
|
|
|
req := s.(*PutRequest)
|
|
|
|
|
|
|
|
mid := req.GetMessageID()
|
|
|
|
mid[0]++
|
|
|
|
|
|
|
|
req.SetMessageID(mid)
|
|
|
|
},
|
|
|
|
func(s sigType) {
|
|
|
|
req := s.(*PutRequest)
|
|
|
|
|
|
|
|
req.SetHeight(req.GetHeight() + 1)
|
|
|
|
},
|
|
|
|
func(s sigType) {
|
|
|
|
req := s.(*PutRequest)
|
|
|
|
|
|
|
|
amount := req.GetAmount()
|
|
|
|
if amount == nil {
|
|
|
|
req.SetAmount(decimal.New(0))
|
|
|
|
} else {
|
|
|
|
req.SetAmount(amount.Add(decimal.New(amount.GetValue())))
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2020-05-11 08:59:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, item := range items {
|
2020-05-11 09:54:21 +00:00
|
|
|
{ // token corruptions
|
|
|
|
v := item.constructor()
|
|
|
|
|
|
|
|
token := new(service.Token)
|
|
|
|
v.SetToken(token)
|
|
|
|
|
|
|
|
require.NoError(t, service.SignDataWithSessionToken(sk, v))
|
|
|
|
|
|
|
|
require.NoError(t, service.VerifyAccumulatedSignaturesWithToken(v))
|
|
|
|
|
|
|
|
token.SetSessionKey(append(token.GetSessionKey(), 1))
|
|
|
|
|
|
|
|
require.Error(t, service.VerifyAccumulatedSignaturesWithToken(v))
|
|
|
|
}
|
|
|
|
|
|
|
|
{ // payload corruptions
|
|
|
|
for _, corruption := range item.payloadCorrupt {
|
|
|
|
v := item.constructor()
|
2020-05-11 08:59:39 +00:00
|
|
|
|
2020-05-11 09:54:21 +00:00
|
|
|
require.NoError(t, service.SignDataWithSessionToken(sk, v))
|
2020-05-11 08:59:39 +00:00
|
|
|
|
2020-05-11 09:54:21 +00:00
|
|
|
require.NoError(t, service.VerifyAccumulatedSignaturesWithToken(v))
|
2020-05-11 08:59:39 +00:00
|
|
|
|
2020-05-11 09:54:21 +00:00
|
|
|
corruption(v)
|
2020-05-11 08:59:39 +00:00
|
|
|
|
2020-05-11 09:54:21 +00:00
|
|
|
require.Error(t, service.VerifyAccumulatedSignaturesWithToken(v))
|
|
|
|
}
|
|
|
|
}
|
2020-05-11 08:59:39 +00:00
|
|
|
}
|
|
|
|
}
|