2019-10-10 16:52:10 +00:00
|
|
|
package vm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2020-03-03 14:21:42 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/opcode"
|
2019-10-10 16:52:10 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestIsSignatureContractGood(t *testing.T) {
|
|
|
|
prog := make([]byte, 35)
|
2019-12-03 14:05:06 +00:00
|
|
|
prog[0] = byte(opcode.PUSHBYTES33)
|
|
|
|
prog[34] = byte(opcode.CHECKSIG)
|
2019-10-10 16:52:10 +00:00
|
|
|
assert.Equal(t, true, IsSignatureContract(prog))
|
|
|
|
assert.Equal(t, true, IsStandardContract(prog))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestIsSignatureContractBadNoCheckSig(t *testing.T) {
|
|
|
|
prog := make([]byte, 34)
|
2019-12-03 14:05:06 +00:00
|
|
|
prog[0] = byte(opcode.PUSHBYTES33)
|
2019-10-10 16:52:10 +00:00
|
|
|
assert.Equal(t, false, IsSignatureContract(prog))
|
|
|
|
assert.Equal(t, false, IsStandardContract(prog))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestIsSignatureContractBadNoCheckSig2(t *testing.T) {
|
|
|
|
prog := make([]byte, 35)
|
2019-12-03 14:05:06 +00:00
|
|
|
prog[0] = byte(opcode.PUSHBYTES33)
|
|
|
|
prog[34] = byte(opcode.CHECKMULTISIG)
|
2019-10-10 16:52:10 +00:00
|
|
|
assert.Equal(t, false, IsSignatureContract(prog))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestIsSignatureContractBadWrongPush(t *testing.T) {
|
|
|
|
prog := make([]byte, 35)
|
2019-12-03 14:05:06 +00:00
|
|
|
prog[0] = byte(opcode.PUSHBYTES32)
|
|
|
|
prog[33] = byte(opcode.NOP)
|
|
|
|
prog[34] = byte(opcode.CHECKSIG)
|
2019-10-10 16:52:10 +00:00
|
|
|
assert.Equal(t, false, IsSignatureContract(prog))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestIsSignatureContractBadWrongInstr(t *testing.T) {
|
|
|
|
prog := make([]byte, 30)
|
2019-12-03 14:05:06 +00:00
|
|
|
prog[0] = byte(opcode.PUSHBYTES33)
|
2019-10-10 16:52:10 +00:00
|
|
|
assert.Equal(t, false, IsSignatureContract(prog))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestIsSignatureContractBadExcessiveInstr(t *testing.T) {
|
|
|
|
prog := make([]byte, 36)
|
2019-12-03 14:05:06 +00:00
|
|
|
prog[0] = byte(opcode.PUSHBYTES33)
|
|
|
|
prog[34] = byte(opcode.CHECKSIG)
|
|
|
|
prog[35] = byte(opcode.RET)
|
2019-10-10 16:52:10 +00:00
|
|
|
assert.Equal(t, false, IsSignatureContract(prog))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestIsMultiSigContractGood(t *testing.T) {
|
|
|
|
prog := make([]byte, 71)
|
2019-12-03 14:05:06 +00:00
|
|
|
prog[0] = byte(opcode.PUSH2)
|
|
|
|
prog[1] = byte(opcode.PUSHBYTES33)
|
|
|
|
prog[35] = byte(opcode.PUSHBYTES33)
|
|
|
|
prog[69] = byte(opcode.PUSH2)
|
|
|
|
prog[70] = byte(opcode.CHECKMULTISIG)
|
2019-10-10 16:52:10 +00:00
|
|
|
assert.Equal(t, true, IsMultiSigContract(prog))
|
|
|
|
assert.Equal(t, true, IsStandardContract(prog))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestIsMultiSigContractGoodPushBytes1(t *testing.T) {
|
|
|
|
prog := make([]byte, 73)
|
2019-12-03 14:05:06 +00:00
|
|
|
prog[0] = byte(opcode.PUSHBYTES1)
|
2019-10-10 16:52:10 +00:00
|
|
|
prog[1] = 2
|
2019-12-03 14:05:06 +00:00
|
|
|
prog[2] = byte(opcode.PUSHBYTES33)
|
|
|
|
prog[36] = byte(opcode.PUSHBYTES33)
|
|
|
|
prog[70] = byte(opcode.PUSHBYTES1)
|
2019-10-10 16:52:10 +00:00
|
|
|
prog[71] = 2
|
2019-12-03 14:05:06 +00:00
|
|
|
prog[72] = byte(opcode.CHECKMULTISIG)
|
2019-10-10 16:52:10 +00:00
|
|
|
assert.Equal(t, true, IsMultiSigContract(prog))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestIsMultiSigContractGoodPushBytes2(t *testing.T) {
|
|
|
|
prog := make([]byte, 75)
|
2019-12-03 14:05:06 +00:00
|
|
|
prog[0] = byte(opcode.PUSHBYTES2)
|
2019-10-10 16:52:10 +00:00
|
|
|
prog[1] = 2
|
2019-12-03 14:05:06 +00:00
|
|
|
prog[3] = byte(opcode.PUSHBYTES33)
|
|
|
|
prog[37] = byte(opcode.PUSHBYTES33)
|
|
|
|
prog[71] = byte(opcode.PUSHBYTES2)
|
2019-10-10 16:52:10 +00:00
|
|
|
prog[72] = 2
|
2019-12-03 14:05:06 +00:00
|
|
|
prog[74] = byte(opcode.CHECKMULTISIG)
|
2019-10-10 16:52:10 +00:00
|
|
|
assert.Equal(t, true, IsMultiSigContract(prog))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestIsMultiSigContractBadNSigs1(t *testing.T) {
|
|
|
|
prog := make([]byte, 71)
|
2019-12-03 14:05:06 +00:00
|
|
|
prog[0] = byte(opcode.PUSH0)
|
|
|
|
prog[1] = byte(opcode.PUSHBYTES33)
|
|
|
|
prog[35] = byte(opcode.PUSHBYTES33)
|
|
|
|
prog[69] = byte(opcode.PUSH2)
|
|
|
|
prog[70] = byte(opcode.CHECKMULTISIG)
|
2019-10-10 16:52:10 +00:00
|
|
|
assert.Equal(t, false, IsMultiSigContract(prog))
|
|
|
|
assert.Equal(t, false, IsStandardContract(prog))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestIsMultiSigContractBadNSigs2(t *testing.T) {
|
|
|
|
prog := make([]byte, 73)
|
2019-12-03 14:05:06 +00:00
|
|
|
prog[0] = byte(opcode.PUSHBYTES2)
|
2019-10-10 16:52:10 +00:00
|
|
|
prog[1] = 0xff
|
|
|
|
prog[2] = 0xff
|
2019-12-03 14:05:06 +00:00
|
|
|
prog[3] = byte(opcode.PUSHBYTES33)
|
|
|
|
prog[37] = byte(opcode.PUSHBYTES33)
|
|
|
|
prog[71] = byte(opcode.PUSH2)
|
|
|
|
prog[72] = byte(opcode.CHECKMULTISIG)
|
2019-10-10 16:52:10 +00:00
|
|
|
assert.Equal(t, false, IsMultiSigContract(prog))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestIsMultiSigContractBadNSigs3(t *testing.T) {
|
|
|
|
prog := make([]byte, 71)
|
2019-12-03 14:05:06 +00:00
|
|
|
prog[0] = byte(opcode.PUSH5)
|
|
|
|
prog[1] = byte(opcode.PUSHBYTES33)
|
|
|
|
prog[35] = byte(opcode.PUSHBYTES33)
|
|
|
|
prog[69] = byte(opcode.PUSH2)
|
|
|
|
prog[70] = byte(opcode.CHECKMULTISIG)
|
2019-10-10 16:52:10 +00:00
|
|
|
assert.Equal(t, false, IsMultiSigContract(prog))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestIsMultiSigContractBadExcessiveNOP1(t *testing.T) {
|
|
|
|
prog := make([]byte, 72)
|
2019-12-03 14:05:06 +00:00
|
|
|
prog[0] = byte(opcode.PUSH2)
|
|
|
|
prog[1] = byte(opcode.NOP)
|
|
|
|
prog[2] = byte(opcode.PUSHBYTES33)
|
|
|
|
prog[36] = byte(opcode.PUSHBYTES33)
|
|
|
|
prog[70] = byte(opcode.PUSH2)
|
|
|
|
prog[71] = byte(opcode.CHECKMULTISIG)
|
2019-10-10 16:52:10 +00:00
|
|
|
assert.Equal(t, false, IsMultiSigContract(prog))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestIsMultiSigContractBadExcessiveNOP2(t *testing.T) {
|
|
|
|
prog := make([]byte, 72)
|
2019-12-03 14:05:06 +00:00
|
|
|
prog[0] = byte(opcode.PUSH2)
|
|
|
|
prog[1] = byte(opcode.PUSHBYTES33)
|
|
|
|
prog[35] = byte(opcode.NOP)
|
|
|
|
prog[36] = byte(opcode.PUSHBYTES33)
|
|
|
|
prog[70] = byte(opcode.PUSH2)
|
|
|
|
prog[71] = byte(opcode.CHECKMULTISIG)
|
2019-10-10 16:52:10 +00:00
|
|
|
assert.Equal(t, false, IsMultiSigContract(prog))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestIsMultiSigContractBadExcessiveNOP3(t *testing.T) {
|
|
|
|
prog := make([]byte, 72)
|
2019-12-03 14:05:06 +00:00
|
|
|
prog[0] = byte(opcode.PUSH2)
|
|
|
|
prog[1] = byte(opcode.PUSHBYTES33)
|
|
|
|
prog[35] = byte(opcode.PUSHBYTES33)
|
|
|
|
prog[69] = byte(opcode.NOP)
|
|
|
|
prog[70] = byte(opcode.PUSH2)
|
|
|
|
prog[71] = byte(opcode.CHECKMULTISIG)
|
2019-10-10 16:52:10 +00:00
|
|
|
assert.Equal(t, false, IsMultiSigContract(prog))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestIsMultiSigContractBadExcessiveNOP4(t *testing.T) {
|
|
|
|
prog := make([]byte, 72)
|
2019-12-03 14:05:06 +00:00
|
|
|
prog[0] = byte(opcode.PUSH2)
|
|
|
|
prog[1] = byte(opcode.PUSHBYTES33)
|
|
|
|
prog[35] = byte(opcode.PUSHBYTES33)
|
|
|
|
prog[69] = byte(opcode.PUSH2)
|
|
|
|
prog[70] = byte(opcode.NOP)
|
|
|
|
prog[71] = byte(opcode.CHECKMULTISIG)
|
2019-10-10 16:52:10 +00:00
|
|
|
assert.Equal(t, false, IsMultiSigContract(prog))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestIsMultiSigContractBadExcessiveNOP5(t *testing.T) {
|
|
|
|
prog := make([]byte, 72)
|
2019-12-03 14:05:06 +00:00
|
|
|
prog[0] = byte(opcode.PUSH2)
|
|
|
|
prog[1] = byte(opcode.PUSHBYTES33)
|
|
|
|
prog[35] = byte(opcode.PUSHBYTES33)
|
|
|
|
prog[69] = byte(opcode.PUSH2)
|
|
|
|
prog[70] = byte(opcode.CHECKMULTISIG)
|
|
|
|
prog[71] = byte(opcode.NOP)
|
2019-10-10 16:52:10 +00:00
|
|
|
assert.Equal(t, false, IsMultiSigContract(prog))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestIsMultiSigContractBadNKeys1(t *testing.T) {
|
|
|
|
prog := make([]byte, 71)
|
2019-12-03 14:05:06 +00:00
|
|
|
prog[0] = byte(opcode.PUSH2)
|
|
|
|
prog[1] = byte(opcode.PUSHBYTES33)
|
|
|
|
prog[35] = byte(opcode.PUSHBYTES33)
|
|
|
|
prog[69] = byte(opcode.PUSH3)
|
|
|
|
prog[70] = byte(opcode.CHECKMULTISIG)
|
2019-10-10 16:52:10 +00:00
|
|
|
assert.Equal(t, false, IsMultiSigContract(prog))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestIsMultiSigContractBadNKeys2(t *testing.T) {
|
|
|
|
prog := make([]byte, 1)
|
2019-12-03 14:05:06 +00:00
|
|
|
prog[0] = byte(opcode.PUSH10)
|
2019-10-10 16:52:10 +00:00
|
|
|
key := make([]byte, 33)
|
|
|
|
var asize = uint16(MaxArraySize + 1)
|
|
|
|
for i := 0; i < int(asize); i++ {
|
2019-12-03 14:05:06 +00:00
|
|
|
prog = append(prog, byte(opcode.PUSHBYTES33))
|
2019-10-10 16:52:10 +00:00
|
|
|
prog = append(prog, key...)
|
|
|
|
}
|
2019-12-03 14:05:06 +00:00
|
|
|
prog = append(prog, byte(opcode.PUSHBYTES2), byte(asize&0xff), byte((asize<<8)&0xff), byte(opcode.CHECKMULTISIG))
|
2019-10-10 16:52:10 +00:00
|
|
|
assert.Equal(t, false, IsMultiSigContract(prog))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestIsMultiSigContractBadRead1(t *testing.T) {
|
|
|
|
prog := make([]byte, 71)
|
2019-12-03 14:05:06 +00:00
|
|
|
prog[0] = byte(opcode.PUSHBYTES75)
|
|
|
|
prog[1] = byte(opcode.PUSHBYTES33)
|
|
|
|
prog[35] = byte(opcode.PUSHBYTES33)
|
|
|
|
prog[69] = byte(opcode.PUSH2)
|
|
|
|
prog[70] = byte(opcode.CHECKMULTISIG)
|
2019-10-10 16:52:10 +00:00
|
|
|
assert.Equal(t, false, IsMultiSigContract(prog))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestIsMultiSigContractBadRead2(t *testing.T) {
|
|
|
|
prog := make([]byte, 71)
|
2019-12-03 14:05:06 +00:00
|
|
|
prog[0] = byte(opcode.PUSH2)
|
|
|
|
prog[1] = byte(opcode.PUSHBYTES33)
|
|
|
|
prog[35] = byte(opcode.PUSHBYTES75)
|
|
|
|
prog[69] = byte(opcode.PUSH2)
|
|
|
|
prog[70] = byte(opcode.CHECKMULTISIG)
|
2019-10-10 16:52:10 +00:00
|
|
|
assert.Equal(t, false, IsMultiSigContract(prog))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestIsMultiSigContractBadRead3(t *testing.T) {
|
|
|
|
prog := make([]byte, 71)
|
2019-12-03 14:05:06 +00:00
|
|
|
prog[0] = byte(opcode.PUSH2)
|
|
|
|
prog[1] = byte(opcode.PUSHBYTES33)
|
|
|
|
prog[35] = byte(opcode.PUSHBYTES33)
|
|
|
|
prog[69] = byte(opcode.PUSH2)
|
|
|
|
prog[70] = byte(opcode.PUSHBYTES1)
|
2019-10-10 16:52:10 +00:00
|
|
|
assert.Equal(t, false, IsMultiSigContract(prog))
|
|
|
|
}
|