2021-11-08 10:04:45 +00:00
|
|
|
package object
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/rand"
|
|
|
|
"testing"
|
|
|
|
|
2021-12-10 13:56:04 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
2021-11-08 10:04:45 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestVerificationFields(t *testing.T) {
|
2022-03-03 11:48:55 +00:00
|
|
|
obj := New()
|
2021-11-08 10:04:45 +00:00
|
|
|
|
|
|
|
payload := make([]byte, 10)
|
|
|
|
_, _ = rand.Read(payload)
|
|
|
|
|
|
|
|
obj.SetPayload(payload)
|
|
|
|
obj.SetPayloadSize(uint64(len(payload)))
|
|
|
|
|
2021-12-10 13:56:04 +00:00
|
|
|
p, err := keys.NewPrivateKey()
|
2021-11-08 10:04:45 +00:00
|
|
|
require.NoError(t, err)
|
2021-12-10 13:56:04 +00:00
|
|
|
require.NoError(t, SetVerificationFields(&p.PrivateKey, obj))
|
2021-11-08 10:04:45 +00:00
|
|
|
|
2022-03-03 11:48:55 +00:00
|
|
|
require.NoError(t, CheckVerificationFields(obj))
|
2021-11-08 10:04:45 +00:00
|
|
|
|
|
|
|
items := []struct {
|
|
|
|
corrupt func()
|
|
|
|
restore func()
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
corrupt: func() {
|
|
|
|
payload[0]++
|
|
|
|
},
|
|
|
|
restore: func() {
|
|
|
|
payload[0]--
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
corrupt: func() {
|
|
|
|
obj.SetPayloadSize(obj.PayloadSize() + 1)
|
|
|
|
},
|
|
|
|
restore: func() {
|
|
|
|
obj.SetPayloadSize(obj.PayloadSize() - 1)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
corrupt: func() {
|
|
|
|
obj.ID().ToV2().GetValue()[0]++
|
|
|
|
},
|
|
|
|
restore: func() {
|
|
|
|
obj.ID().ToV2().GetValue()[0]--
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
corrupt: func() {
|
|
|
|
obj.Signature().Key()[0]++
|
|
|
|
},
|
|
|
|
restore: func() {
|
|
|
|
obj.Signature().Key()[0]--
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
corrupt: func() {
|
|
|
|
obj.Signature().Sign()[0]++
|
|
|
|
},
|
|
|
|
restore: func() {
|
|
|
|
obj.Signature().Sign()[0]--
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, item := range items {
|
|
|
|
item.corrupt()
|
|
|
|
|
2022-03-03 11:48:55 +00:00
|
|
|
require.Error(t, CheckVerificationFields(obj))
|
2021-11-08 10:04:45 +00:00
|
|
|
|
|
|
|
item.restore()
|
|
|
|
|
2022-03-03 11:48:55 +00:00
|
|
|
require.NoError(t, CheckVerificationFields(obj))
|
2021-11-08 10:04:45 +00:00
|
|
|
}
|
|
|
|
}
|