2015-05-01 08:28:01 +02:00
|
|
|
// Copyright (c) 2013-2015 The btcsuite developers
|
2013-06-12 23:35:27 +02:00
|
|
|
// Use of this source code is governed by an ISC
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2015-01-30 07:03:01 +01:00
|
|
|
package txscript_test
|
2013-06-12 23:35:27 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2015-05-02 21:56:55 +02:00
|
|
|
"reflect"
|
2014-07-03 02:37:49 +02:00
|
|
|
"testing"
|
|
|
|
|
2015-01-30 07:03:01 +01:00
|
|
|
"github.com/btcsuite/btcd/txscript"
|
2013-06-12 23:35:27 +02:00
|
|
|
)
|
|
|
|
|
2015-05-02 21:56:55 +02:00
|
|
|
// TestPushedData ensured the PushedData function extracts the expected data out
|
|
|
|
// of various scripts.
|
2014-05-08 19:14:01 +02:00
|
|
|
func TestPushedData(t *testing.T) {
|
2015-01-29 21:39:44 +01:00
|
|
|
t.Parallel()
|
|
|
|
|
2014-05-08 19:14:01 +02:00
|
|
|
var tests = []struct {
|
2015-05-02 21:56:55 +02:00
|
|
|
script string
|
|
|
|
out [][]byte
|
|
|
|
valid bool
|
2014-05-08 19:14:01 +02:00
|
|
|
}{
|
|
|
|
{
|
2015-05-02 21:56:55 +02:00
|
|
|
"0 IF 0 ELSE 2 ENDIF",
|
|
|
|
[][]byte{nil, nil},
|
2014-05-08 19:14:01 +02:00
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
2015-05-02 21:56:55 +02:00
|
|
|
"16777216 10000000",
|
2014-05-08 19:14:01 +02:00
|
|
|
[][]byte{
|
|
|
|
{0x00, 0x00, 0x00, 0x01}, // 16777216
|
|
|
|
{0x80, 0x96, 0x98, 0x00}, // 10000000
|
|
|
|
},
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
2015-05-02 21:56:55 +02:00
|
|
|
"DUP HASH160 '17VZNX1SN5NtKa8UQFxwQbFeFc3iqRYhem' EQUALVERIFY CHECKSIG",
|
2014-05-08 19:14:01 +02:00
|
|
|
[][]byte{
|
|
|
|
// 17VZNX1SN5NtKa8UQFxwQbFeFc3iqRYhem
|
|
|
|
{
|
|
|
|
0x31, 0x37, 0x56, 0x5a, 0x4e, 0x58, 0x31, 0x53, 0x4e, 0x35,
|
|
|
|
0x4e, 0x74, 0x4b, 0x61, 0x38, 0x55, 0x51, 0x46, 0x78, 0x77,
|
|
|
|
0x51, 0x62, 0x46, 0x65, 0x46, 0x63, 0x33, 0x69, 0x71, 0x52,
|
|
|
|
0x59, 0x68, 0x65, 0x6d,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
2015-05-02 21:56:55 +02:00
|
|
|
"PUSHDATA4 1000 EQUAL",
|
|
|
|
nil,
|
2014-05-08 19:14:01 +02:00
|
|
|
false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2015-05-02 21:56:55 +02:00
|
|
|
for i, test := range tests {
|
|
|
|
script := mustParseShortForm(test.script)
|
|
|
|
data, err := txscript.PushedData(script)
|
2014-05-08 19:14:01 +02:00
|
|
|
if test.valid && err != nil {
|
2015-05-02 21:56:55 +02:00
|
|
|
t.Errorf("TestPushedData failed test #%d: %v\n", i, err)
|
2014-05-08 19:14:01 +02:00
|
|
|
continue
|
|
|
|
} else if !test.valid && err == nil {
|
2015-05-02 21:56:55 +02:00
|
|
|
t.Errorf("TestPushedData failed test #%d: test should "+
|
|
|
|
"be invalid\n", i)
|
2014-05-08 19:14:01 +02:00
|
|
|
continue
|
|
|
|
}
|
2015-05-02 21:56:55 +02:00
|
|
|
if !reflect.DeepEqual(data, test.out) {
|
|
|
|
t.Errorf("TestPushedData failed test #%d: want: %x "+
|
|
|
|
"got: %x\n", i, test.out, data)
|
2014-05-08 19:14:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-02 21:56:55 +02:00
|
|
|
// TestHasCanonicalPush ensures the canonicalPush function works as expected.
|
|
|
|
func TestHasCanonicalPush(t *testing.T) {
|
2015-01-29 21:39:44 +01:00
|
|
|
t.Parallel()
|
|
|
|
|
2014-12-20 08:34:39 +01:00
|
|
|
for i := 0; i < 65535; i++ {
|
2015-01-30 07:03:01 +01:00
|
|
|
builder := txscript.NewScriptBuilder()
|
2014-02-20 18:42:06 +01:00
|
|
|
builder.AddInt64(int64(i))
|
2014-12-20 08:34:39 +01:00
|
|
|
script, err := builder.Script()
|
|
|
|
if err != nil {
|
2015-05-02 21:56:55 +02:00
|
|
|
t.Errorf("Script: test #%d unexpected error: %v\n", i,
|
|
|
|
err)
|
2014-12-20 08:34:39 +01:00
|
|
|
continue
|
2014-02-20 06:05:35 +01:00
|
|
|
}
|
2015-01-30 07:03:01 +01:00
|
|
|
if result := txscript.IsPushOnlyScript(script); !result {
|
2015-05-02 21:56:55 +02:00
|
|
|
t.Errorf("IsPushOnlyScript: test #%d failed: %x\n", i,
|
|
|
|
script)
|
2014-12-20 08:34:39 +01:00
|
|
|
continue
|
|
|
|
}
|
2015-02-25 02:11:57 +01:00
|
|
|
pops, err := txscript.TstParseScript(script)
|
|
|
|
if err != nil {
|
2015-05-02 21:56:55 +02:00
|
|
|
t.Errorf("TstParseScript: #%d failed: %v", i, err)
|
2014-02-20 06:05:35 +01:00
|
|
|
continue
|
|
|
|
}
|
2015-02-25 02:11:57 +01:00
|
|
|
for _, pop := range pops {
|
|
|
|
if result := txscript.TstHasCanonicalPushes(pop); !result {
|
2015-05-02 21:56:55 +02:00
|
|
|
t.Errorf("TstHasCanonicalPushes: test #%d "+
|
|
|
|
"failed: %x\n", i, script)
|
2015-02-25 02:11:57 +01:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2014-02-20 06:05:35 +01:00
|
|
|
}
|
2015-01-30 07:03:01 +01:00
|
|
|
for i := 0; i <= txscript.MaxScriptElementSize; i++ {
|
|
|
|
builder := txscript.NewScriptBuilder()
|
2014-02-20 18:42:06 +01:00
|
|
|
builder.AddData(bytes.Repeat([]byte{0x49}, i))
|
2014-12-20 08:34:39 +01:00
|
|
|
script, err := builder.Script()
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("StandardPushesTests test #%d unexpected error: %v\n", i, err)
|
|
|
|
continue
|
|
|
|
}
|
2015-01-30 07:03:01 +01:00
|
|
|
if result := txscript.IsPushOnlyScript(script); !result {
|
2014-12-20 08:34:39 +01:00
|
|
|
t.Errorf("StandardPushesTests IsPushOnlyScript test #%d failed: %x\n", i, script)
|
|
|
|
continue
|
2014-02-20 06:05:35 +01:00
|
|
|
}
|
2015-02-25 02:11:57 +01:00
|
|
|
pops, err := txscript.TstParseScript(script)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("StandardPushesTests #%d failed to TstParseScript: %v", i, err)
|
2014-02-20 06:05:35 +01:00
|
|
|
continue
|
|
|
|
}
|
2015-02-25 02:11:57 +01:00
|
|
|
for _, pop := range pops {
|
|
|
|
if result := txscript.TstHasCanonicalPushes(pop); !result {
|
|
|
|
t.Errorf("StandardPushesTests TstHasCanonicalPushes test #%d failed: %x\n", i, script)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2014-02-20 06:05:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-02 21:56:55 +02:00
|
|
|
// TestGetPreciseSigOps ensures the more precise signature operation counting
|
|
|
|
// mechanism which includes signatures in P2SH scripts works as expected.
|
|
|
|
func TestGetPreciseSigOps(t *testing.T) {
|
2015-01-29 21:39:44 +01:00
|
|
|
t.Parallel()
|
|
|
|
|
2015-05-02 21:56:55 +02:00
|
|
|
tests := []struct {
|
2013-06-25 14:12:15 +02:00
|
|
|
name string
|
2013-06-24 17:32:41 +02:00
|
|
|
scriptSig []byte
|
2013-06-25 14:12:15 +02:00
|
|
|
nSigOps int
|
|
|
|
err error
|
2015-05-02 21:56:55 +02:00
|
|
|
}{
|
2014-02-04 22:18:37 +01:00
|
|
|
{
|
2013-06-25 14:12:15 +02:00
|
|
|
name: "scriptSig doesn't parse",
|
2015-01-30 07:03:01 +01:00
|
|
|
scriptSig: []byte{txscript.OP_PUSHDATA1, 2},
|
|
|
|
err: txscript.ErrStackShortScript,
|
2013-06-24 17:32:41 +02:00
|
|
|
},
|
2014-02-04 22:18:37 +01:00
|
|
|
{
|
2013-06-25 14:12:15 +02:00
|
|
|
name: "scriptSig isn't push only",
|
2015-01-30 07:03:01 +01:00
|
|
|
scriptSig: []byte{txscript.OP_1, txscript.OP_DUP},
|
2013-06-25 14:12:15 +02:00
|
|
|
nSigOps: 0,
|
2013-06-24 17:32:41 +02:00
|
|
|
},
|
2014-02-04 22:18:37 +01:00
|
|
|
{
|
2013-06-25 14:12:15 +02:00
|
|
|
name: "scriptSig length 0",
|
2015-05-02 21:56:55 +02:00
|
|
|
scriptSig: nil,
|
2013-06-25 14:12:15 +02:00
|
|
|
nSigOps: 0,
|
2013-06-24 17:32:41 +02:00
|
|
|
},
|
2014-02-04 22:18:37 +01:00
|
|
|
{
|
2013-06-24 17:32:41 +02:00
|
|
|
name: "No script at the end",
|
|
|
|
// No script at end but still push only.
|
2015-01-30 07:03:01 +01:00
|
|
|
scriptSig: []byte{txscript.OP_1, txscript.OP_1},
|
2013-06-25 14:12:15 +02:00
|
|
|
nSigOps: 0,
|
2013-06-24 17:32:41 +02:00
|
|
|
},
|
2014-02-04 22:18:37 +01:00
|
|
|
{
|
2013-06-24 17:32:41 +02:00
|
|
|
name: "pushed script doesn't parse",
|
2015-01-30 07:03:01 +01:00
|
|
|
scriptSig: []byte{txscript.OP_DATA_2,
|
|
|
|
txscript.OP_PUSHDATA1, 2},
|
|
|
|
err: txscript.ErrStackShortScript,
|
2013-06-24 17:32:41 +02:00
|
|
|
},
|
|
|
|
}
|
2015-05-02 21:56:55 +02:00
|
|
|
|
2013-06-24 17:32:41 +02:00
|
|
|
// The signature in the p2sh script is nonsensical for the tests since
|
2015-05-02 21:56:55 +02:00
|
|
|
// this script will never be executed. What matters is that it matches
|
2013-06-24 17:32:41 +02:00
|
|
|
// the right pattern.
|
2015-05-02 21:56:55 +02:00
|
|
|
pkScript := mustParseShortForm("HASH160 DATA_20 0x433ec2ac1ffa1b7b7d0" +
|
|
|
|
"27f564529c57197f9ae88 EQUAL")
|
|
|
|
for _, test := range tests {
|
|
|
|
count := txscript.GetPreciseSigOpCount(test.scriptSig, pkScript,
|
|
|
|
true)
|
2013-06-24 17:32:41 +02:00
|
|
|
if count != test.nSigOps {
|
|
|
|
t.Errorf("%s: expected count of %d, got %d", test.name,
|
|
|
|
test.nSigOps, count)
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
2013-06-20 19:07:44 +02:00
|
|
|
}
|
|
|
|
|
2015-05-02 21:56:55 +02:00
|
|
|
// TestRemoveOpcodes ensures that removing opcodes from scripts behaves as
|
|
|
|
// expected.
|
2013-06-12 23:35:27 +02:00
|
|
|
func TestRemoveOpcodes(t *testing.T) {
|
2015-01-29 21:39:44 +01:00
|
|
|
t.Parallel()
|
|
|
|
|
2015-05-02 21:56:55 +02:00
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
before string
|
|
|
|
remove byte
|
|
|
|
err error
|
|
|
|
after string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
// Nothing to remove.
|
|
|
|
name: "nothing to remove",
|
|
|
|
before: "NOP",
|
|
|
|
remove: txscript.OP_CODESEPARATOR,
|
|
|
|
after: "NOP",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// Test basic opcode removal.
|
|
|
|
name: "codeseparator 1",
|
|
|
|
before: "NOP CODESEPARATOR TRUE",
|
|
|
|
remove: txscript.OP_CODESEPARATOR,
|
|
|
|
after: "NOP TRUE",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// The opcode in question is actually part of the data
|
|
|
|
// in a previous opcode.
|
|
|
|
name: "codeseparator by coincidence",
|
|
|
|
before: "NOP DATA_1 CODESEPARATOR TRUE",
|
|
|
|
remove: txscript.OP_CODESEPARATOR,
|
|
|
|
after: "NOP DATA_1 CODESEPARATOR TRUE",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "invalid opcode",
|
|
|
|
before: "CAT",
|
|
|
|
remove: txscript.OP_CODESEPARATOR,
|
|
|
|
after: "CAT",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "invalid length (insruction)",
|
|
|
|
before: "PUSHDATA1",
|
|
|
|
remove: txscript.OP_CODESEPARATOR,
|
|
|
|
err: txscript.ErrStackShortScript,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "invalid length (data)",
|
|
|
|
before: "PUSHDATA1 0xff 0xfe",
|
|
|
|
remove: txscript.OP_CODESEPARATOR,
|
|
|
|
err: txscript.ErrStackShortScript,
|
|
|
|
},
|
2013-06-12 23:35:27 +02:00
|
|
|
}
|
|
|
|
|
2015-05-02 21:56:55 +02:00
|
|
|
for _, test := range tests {
|
|
|
|
before := mustParseShortForm(test.before)
|
|
|
|
after := mustParseShortForm(test.after)
|
|
|
|
result, err := txscript.TstRemoveOpcode(before, test.remove)
|
|
|
|
if test.err != nil {
|
|
|
|
if err != test.err {
|
|
|
|
t.Errorf("%s: got unexpected error. exp: \"%v\" "+
|
|
|
|
"got: \"%v\"", test.name, test.err, err)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("%s: unexpected failure: \"%v\"", test.name, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if !bytes.Equal(after, result) {
|
|
|
|
t.Errorf("%s: value does not equal expected: exp: \"%v\""+
|
|
|
|
" got: \"%v\"", test.name, after, result)
|
2013-06-12 23:35:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-05-01 22:20:07 +02:00
|
|
|
|
2015-05-02 21:56:55 +02:00
|
|
|
// TestRemoveOpcodeByData ensures that removing data carrying opcodes based on
|
|
|
|
// the data they contain works as expected.
|
|
|
|
func TestRemoveOpcodeByData(t *testing.T) {
|
2015-01-29 21:39:44 +01:00
|
|
|
t.Parallel()
|
|
|
|
|
2015-05-02 21:56:55 +02:00
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
before []byte
|
|
|
|
remove []byte
|
|
|
|
err error
|
|
|
|
after []byte
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "nothing to do",
|
|
|
|
before: []byte{txscript.OP_NOP},
|
|
|
|
remove: []byte{1, 2, 3, 4},
|
|
|
|
after: []byte{txscript.OP_NOP},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "simple case",
|
|
|
|
before: []byte{txscript.OP_DATA_4, 1, 2, 3, 4},
|
|
|
|
remove: []byte{1, 2, 3, 4},
|
|
|
|
after: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "simple case (miss)",
|
|
|
|
before: []byte{txscript.OP_DATA_4, 1, 2, 3, 4},
|
|
|
|
remove: []byte{1, 2, 3, 5},
|
|
|
|
after: []byte{txscript.OP_DATA_4, 1, 2, 3, 4},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// padded to keep it canonical.
|
|
|
|
name: "simple case (pushdata1)",
|
|
|
|
before: append(append([]byte{txscript.OP_PUSHDATA1, 76},
|
|
|
|
bytes.Repeat([]byte{0}, 72)...),
|
|
|
|
[]byte{1, 2, 3, 4}...),
|
|
|
|
remove: []byte{1, 2, 3, 4},
|
|
|
|
after: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "simple case (pushdata1 miss)",
|
|
|
|
before: append(append([]byte{txscript.OP_PUSHDATA1, 76},
|
|
|
|
bytes.Repeat([]byte{0}, 72)...),
|
|
|
|
[]byte{1, 2, 3, 4}...),
|
|
|
|
remove: []byte{1, 2, 3, 5},
|
|
|
|
after: append(append([]byte{txscript.OP_PUSHDATA1, 76},
|
|
|
|
bytes.Repeat([]byte{0}, 72)...),
|
|
|
|
[]byte{1, 2, 3, 4}...),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "simple case (pushdata1 miss noncanonical)",
|
|
|
|
before: []byte{txscript.OP_PUSHDATA1, 4, 1, 2, 3, 4},
|
|
|
|
remove: []byte{1, 2, 3, 4},
|
|
|
|
after: []byte{txscript.OP_PUSHDATA1, 4, 1, 2, 3, 4},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "simple case (pushdata2)",
|
|
|
|
before: append(append([]byte{txscript.OP_PUSHDATA2, 0, 1},
|
|
|
|
bytes.Repeat([]byte{0}, 252)...),
|
|
|
|
[]byte{1, 2, 3, 4}...),
|
|
|
|
remove: []byte{1, 2, 3, 4},
|
|
|
|
after: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "simple case (pushdata2 miss)",
|
|
|
|
before: append(append([]byte{txscript.OP_PUSHDATA2, 0, 1},
|
|
|
|
bytes.Repeat([]byte{0}, 252)...),
|
|
|
|
[]byte{1, 2, 3, 4}...),
|
|
|
|
remove: []byte{1, 2, 3, 4, 5},
|
|
|
|
after: append(append([]byte{txscript.OP_PUSHDATA2, 0, 1},
|
|
|
|
bytes.Repeat([]byte{0}, 252)...),
|
|
|
|
[]byte{1, 2, 3, 4}...),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "simple case (pushdata2 miss noncanonical)",
|
|
|
|
before: []byte{txscript.OP_PUSHDATA2, 4, 0, 1, 2, 3, 4},
|
|
|
|
remove: []byte{1, 2, 3, 4},
|
|
|
|
after: []byte{txscript.OP_PUSHDATA2, 4, 0, 1, 2, 3, 4},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// This is padded to make the push canonical.
|
|
|
|
name: "simple case (pushdata4)",
|
|
|
|
before: append(append([]byte{txscript.OP_PUSHDATA4, 0, 0, 1, 0},
|
|
|
|
bytes.Repeat([]byte{0}, 65532)...),
|
|
|
|
[]byte{1, 2, 3, 4}...),
|
|
|
|
remove: []byte{1, 2, 3, 4},
|
|
|
|
after: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "simple case (pushdata4 miss noncanonical)",
|
|
|
|
before: []byte{txscript.OP_PUSHDATA4, 4, 0, 0, 0, 1, 2, 3, 4},
|
|
|
|
remove: []byte{1, 2, 3, 4},
|
|
|
|
after: []byte{txscript.OP_PUSHDATA4, 4, 0, 0, 0, 1, 2, 3, 4},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// This is padded to make the push canonical.
|
|
|
|
name: "simple case (pushdata4 miss)",
|
|
|
|
before: append(append([]byte{txscript.OP_PUSHDATA4, 0, 0, 1, 0},
|
|
|
|
bytes.Repeat([]byte{0}, 65532)...), []byte{1, 2, 3, 4}...),
|
|
|
|
remove: []byte{1, 2, 3, 4, 5},
|
|
|
|
after: append(append([]byte{txscript.OP_PUSHDATA4, 0, 0, 1, 0},
|
|
|
|
bytes.Repeat([]byte{0}, 65532)...), []byte{1, 2, 3, 4}...),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "invalid opcode ",
|
|
|
|
before: []byte{txscript.OP_UNKNOWN187},
|
|
|
|
remove: []byte{1, 2, 3, 4},
|
|
|
|
after: []byte{txscript.OP_UNKNOWN187},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "invalid length (instruction)",
|
|
|
|
before: []byte{txscript.OP_PUSHDATA1},
|
|
|
|
remove: []byte{1, 2, 3, 4},
|
|
|
|
err: txscript.ErrStackShortScript,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "invalid length (data)",
|
|
|
|
before: []byte{txscript.OP_PUSHDATA1, 255, 254},
|
|
|
|
remove: []byte{1, 2, 3, 4},
|
|
|
|
err: txscript.ErrStackShortScript,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
result, err := txscript.TstRemoveOpcodeByData(test.before,
|
|
|
|
test.remove)
|
|
|
|
if test.err != nil {
|
|
|
|
if err != test.err {
|
|
|
|
t.Errorf("%s: got unexpected error. exp: \"%v\" "+
|
|
|
|
"got: \"%v\"", test.name, test.err, err)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("%s: unexpected failure: \"%v\"", test.name, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if !bytes.Equal(test.after, result) {
|
|
|
|
t.Errorf("%s: value does not equal expected: exp: \"%v\""+
|
|
|
|
" got: \"%v\"", test.name, test.after, result)
|
|
|
|
}
|
2013-06-12 23:35:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-02 21:56:55 +02:00
|
|
|
// TestIsPayToScriptHash ensures the IsPayToScriptHash function returns the
|
|
|
|
// expected results for all the scripts in scriptClassTests.
|
2013-06-27 01:32:11 +02:00
|
|
|
func TestIsPayToScriptHash(t *testing.T) {
|
2015-01-29 21:39:44 +01:00
|
|
|
t.Parallel()
|
|
|
|
|
2015-05-02 21:56:55 +02:00
|
|
|
for _, test := range scriptClassTests {
|
|
|
|
script := mustParseShortForm(test.script)
|
|
|
|
shouldBe := (test.class == txscript.ScriptHashTy)
|
|
|
|
p2sh := txscript.IsPayToScriptHash(script)
|
2013-06-27 01:32:11 +02:00
|
|
|
if p2sh != shouldBe {
|
|
|
|
t.Errorf("%s: epxected p2sh %v, got %v", test.name,
|
|
|
|
shouldBe, p2sh)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-02 21:56:55 +02:00
|
|
|
// TestHasCanonicalPushes ensures the canonicalPush function properly determines
|
|
|
|
// what is considered a canonical push for the purposes of removeOpcodeByData.
|
2014-08-08 13:46:15 +02:00
|
|
|
func TestHasCanonicalPushes(t *testing.T) {
|
2015-01-29 21:39:44 +01:00
|
|
|
t.Parallel()
|
|
|
|
|
2014-08-08 13:46:15 +02:00
|
|
|
tests := []struct {
|
|
|
|
name string
|
2015-05-02 21:56:55 +02:00
|
|
|
script string
|
2014-08-08 13:46:15 +02:00
|
|
|
expected bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "does not parse",
|
2015-05-02 21:56:55 +02:00
|
|
|
script: "0x046708afdb0fe5548271967f1a67130b7105cd6a82" +
|
|
|
|
"8e03909a67962e0ea1f61d",
|
2014-08-08 13:46:15 +02:00
|
|
|
expected: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "non-canonical push",
|
2015-05-02 21:56:55 +02:00
|
|
|
script: "PUSHDATA1 0x04 0x01020304",
|
2014-08-08 13:46:15 +02:00
|
|
|
expected: false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, test := range tests {
|
2015-05-02 21:56:55 +02:00
|
|
|
script := mustParseShortForm(test.script)
|
|
|
|
pops, err := txscript.TstParseScript(script)
|
2015-02-25 02:11:57 +01:00
|
|
|
if err != nil {
|
|
|
|
if test.expected {
|
2015-05-02 21:56:55 +02:00
|
|
|
t.Errorf("TstParseScript #%d failed: %v", i, err)
|
2015-02-25 02:11:57 +01:00
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
for _, pop := range pops {
|
|
|
|
if txscript.TstHasCanonicalPushes(pop) != test.expected {
|
2015-05-02 21:56:55 +02:00
|
|
|
t.Errorf("TstHasCanonicalPushes: #%d (%s) "+
|
|
|
|
"wrong result\ngot: %v\nwant: %v", i,
|
|
|
|
test.name, true, test.expected)
|
2015-02-25 02:11:57 +01:00
|
|
|
break
|
|
|
|
}
|
2014-08-08 13:46:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-02 21:56:55 +02:00
|
|
|
// TestIsPushOnlyScript ensures the IsPushOnlyScript function returns the
|
|
|
|
// expected results.
|
2014-08-08 13:46:15 +02:00
|
|
|
func TestIsPushOnlyScript(t *testing.T) {
|
2015-01-29 21:39:44 +01:00
|
|
|
t.Parallel()
|
|
|
|
|
2014-08-08 13:46:15 +02:00
|
|
|
test := struct {
|
|
|
|
name string
|
|
|
|
script []byte
|
|
|
|
expected bool
|
|
|
|
}{
|
|
|
|
name: "does not parse",
|
2015-05-02 21:56:55 +02:00
|
|
|
script: mustParseShortForm("0x046708afdb0fe5548271967f1a67130" +
|
|
|
|
"b7105cd6a828e03909a67962e0ea1f61d"),
|
2014-08-08 13:46:15 +02:00
|
|
|
expected: false,
|
|
|
|
}
|
|
|
|
|
2015-01-30 07:03:01 +01:00
|
|
|
if txscript.IsPushOnlyScript(test.script) != test.expected {
|
2015-05-02 21:56:55 +02:00
|
|
|
t.Errorf("IsPushOnlyScript (%s) wrong result\ngot: %v\nwant: "+
|
|
|
|
"%v", test.name, true, test.expected)
|
2015-02-25 21:04:37 +01:00
|
|
|
}
|
|
|
|
}
|