txscript: Test consistency and cleanup.

- Move reference tests to test package since they are intended to
  exercise the engine as callers would
- Improve the short form script parsing to allow additional opcodes:
  DATA_#, OP_#, FALSE, TRUE
- Make use of a function to decode hex strings rather than manually
  defining byte slices
- Update the tests to make use of the short form script parsing logic
  rather than manually defining byte slices
- Consistently replace all []byte{} and [][]byte{} with nil
- Define tests only used in a specific function inside that func
- Move invalid flag combination test to engine_test since that is what
  it is testing
- Remove all redundant script tests in favor of the JSON-based tests in
  the data directory.
- Move several functions from internal_test.go to the test files
  associated with what the tests are checking
This commit is contained in:
Dave Collins 2015-05-02 14:56:55 -05:00
parent 9a658c2689
commit 927a0e9c37
9 changed files with 4626 additions and 6500 deletions

View file

@ -54,7 +54,7 @@ func TestBadPC(t *testing.T) {
TxOut: []*wire.TxOut{ TxOut: []*wire.TxOut{
{ {
Value: 1000000000, Value: 1000000000,
PkScript: []byte{}, PkScript: nil,
}, },
}, },
LockTime: 0, LockTime: 0,
@ -114,7 +114,7 @@ func TestCheckErrorCondition(t *testing.T) {
TxOut: []*wire.TxOut{ TxOut: []*wire.TxOut{
{ {
Value: 1000000000, Value: 1000000000,
PkScript: []byte{}, PkScript: nil,
}, },
}, },
LockTime: 0, LockTime: 0,
@ -171,3 +171,284 @@ func TestCheckErrorCondition(t *testing.T) {
t.Errorf("unexpected error %v on final check", err) t.Errorf("unexpected error %v on final check", err)
} }
} }
// TestInvalidFlagCombinations ensures the script engine returns the expected
// error when disallowed flag combinations are specified.
func TestInvalidFlagCombinations(t *testing.T) {
t.Parallel()
tests := []txscript.ScriptFlags{
txscript.ScriptVerifyCleanStack,
}
// tx with almost empty scripts.
tx := &wire.MsgTx{
Version: 1,
TxIn: []*wire.TxIn{
{
PreviousOutPoint: wire.OutPoint{
Hash: wire.ShaHash([32]byte{
0xc9, 0x97, 0xa5, 0xe5,
0x6e, 0x10, 0x41, 0x02,
0xfa, 0x20, 0x9c, 0x6a,
0x85, 0x2d, 0xd9, 0x06,
0x60, 0xa2, 0x0b, 0x2d,
0x9c, 0x35, 0x24, 0x23,
0xed, 0xce, 0x25, 0x85,
0x7f, 0xcd, 0x37, 0x04,
}),
Index: 0,
},
SignatureScript: []uint8{txscript.OP_NOP},
Sequence: 4294967295,
},
},
TxOut: []*wire.TxOut{
{
Value: 1000000000,
PkScript: nil,
},
},
LockTime: 0,
}
pkScript := []byte{txscript.OP_NOP}
for i, test := range tests {
_, err := txscript.NewEngine(pkScript, tx, 0, test)
if err != txscript.ErrInvalidFlags {
t.Fatalf("TestInvalidFlagCombinations #%d unexpected "+
"error: %v", i, err)
}
}
}
// TestCheckPubKeyEncoding ensures the internal checkPubKeyEncoding function
// works as expected.
func TestCheckPubKeyEncoding(t *testing.T) {
t.Parallel()
tests := []struct {
name string
key []byte
isValid bool
}{
{
name: "uncompressed ok",
key: decodeHex("0411db93e1dcdb8a016b49840f8c53bc1eb68" +
"a382e97b1482ecad7b148a6909a5cb2e0eaddfb84ccf" +
"9744464f82e160bfa9b8b64f9d4c03f999b8643f656b" +
"412a3"),
isValid: true,
},
{
name: "compressed ok",
key: decodeHex("02ce0b14fb842b1ba549fdd675c98075f12e9" +
"c510f8ef52bd021a9a1f4809d3b4d"),
isValid: true,
},
{
name: "compressed ok",
key: decodeHex("032689c7c2dab13309fb143e0e8fe39634252" +
"1887e976690b6b47f5b2a4b7d448e"),
isValid: true,
},
{
name: "hybrid",
key: decodeHex("0679be667ef9dcbbac55a06295ce870b07029" +
"bfcdb2dce28d959f2815b16f81798483ada7726a3c46" +
"55da4fbfc0e1108a8fd17b448a68554199c47d08ffb1" +
"0d4b8"),
isValid: false,
},
{
name: "empty",
key: nil,
isValid: false,
},
}
flags := txscript.ScriptVerifyStrictEncoding
for _, test := range tests {
err := txscript.TstCheckPubKeyEncoding(test.key, flags)
if err != nil && test.isValid {
t.Errorf("checkSignatureEncoding test '%s' failed "+
"when it should have succeeded: %v", test.name,
err)
} else if err == nil && !test.isValid {
t.Errorf("checkSignatureEncooding test '%s' succeeded "+
"when it should have failed", test.name)
}
}
}
// TestCheckSignatureEncoding ensures the internal checkSignatureEncoding
// function works as expected.
func TestCheckSignatureEncoding(t *testing.T) {
t.Parallel()
tests := []struct {
name string
sig []byte
isValid bool
}{
{
name: "valid signature",
sig: decodeHex("304402204e45e16932b8af514961a1d3a1a25" +
"fdf3f4f7732e9d624c6c61548ab5fb8cd41022018152" +
"2ec8eca07de4860a4acdd12909d831cc56cbbac46220" +
"82221a8768d1d09"),
isValid: true,
},
{
name: "empty.",
sig: nil,
isValid: false,
},
{
name: "bad magic",
sig: decodeHex("314402204e45e16932b8af514961a1d3a1a25" +
"fdf3f4f7732e9d624c6c61548ab5fb8cd41022018152" +
"2ec8eca07de4860a4acdd12909d831cc56cbbac46220" +
"82221a8768d1d09"),
isValid: false,
},
{
name: "bad 1st int marker magic",
sig: decodeHex("304403204e45e16932b8af514961a1d3a1a25" +
"fdf3f4f7732e9d624c6c61548ab5fb8cd41022018152" +
"2ec8eca07de4860a4acdd12909d831cc56cbbac46220" +
"82221a8768d1d09"),
isValid: false,
},
{
name: "bad 2nd int marker",
sig: decodeHex("304402204e45e16932b8af514961a1d3a1a25" +
"fdf3f4f7732e9d624c6c61548ab5fb8cd41032018152" +
"2ec8eca07de4860a4acdd12909d831cc56cbbac46220" +
"82221a8768d1d09"),
isValid: false,
},
{
name: "short len",
sig: decodeHex("304302204e45e16932b8af514961a1d3a1a25" +
"fdf3f4f7732e9d624c6c61548ab5fb8cd41022018152" +
"2ec8eca07de4860a4acdd12909d831cc56cbbac46220" +
"82221a8768d1d09"),
isValid: false,
},
{
name: "long len",
sig: decodeHex("304502204e45e16932b8af514961a1d3a1a25" +
"fdf3f4f7732e9d624c6c61548ab5fb8cd41022018152" +
"2ec8eca07de4860a4acdd12909d831cc56cbbac46220" +
"82221a8768d1d09"),
isValid: false,
},
{
name: "long X",
sig: decodeHex("304402424e45e16932b8af514961a1d3a1a25" +
"fdf3f4f7732e9d624c6c61548ab5fb8cd41022018152" +
"2ec8eca07de4860a4acdd12909d831cc56cbbac46220" +
"82221a8768d1d09"),
isValid: false,
},
{
name: "long Y",
sig: decodeHex("304402204e45e16932b8af514961a1d3a1a25" +
"fdf3f4f7732e9d624c6c61548ab5fb8cd41022118152" +
"2ec8eca07de4860a4acdd12909d831cc56cbbac46220" +
"82221a8768d1d09"),
isValid: false,
},
{
name: "short Y",
sig: decodeHex("304402204e45e16932b8af514961a1d3a1a25" +
"fdf3f4f7732e9d624c6c61548ab5fb8cd41021918152" +
"2ec8eca07de4860a4acdd12909d831cc56cbbac46220" +
"82221a8768d1d09"),
isValid: false,
},
{
name: "trailing crap",
sig: decodeHex("304402204e45e16932b8af514961a1d3a1a25" +
"fdf3f4f7732e9d624c6c61548ab5fb8cd41022018152" +
"2ec8eca07de4860a4acdd12909d831cc56cbbac46220" +
"82221a8768d1d0901"),
isValid: false,
},
{
name: "X == N ",
sig: decodeHex("30440220fffffffffffffffffffffffffffff" +
"ffebaaedce6af48a03bbfd25e8cd0364141022018152" +
"2ec8eca07de4860a4acdd12909d831cc56cbbac46220" +
"82221a8768d1d09"),
isValid: false,
},
{
name: "X == N ",
sig: decodeHex("30440220fffffffffffffffffffffffffffff" +
"ffebaaedce6af48a03bbfd25e8cd0364142022018152" +
"2ec8eca07de4860a4acdd12909d831cc56cbbac46220" +
"82221a8768d1d09"),
isValid: false,
},
{
name: "Y == N",
sig: decodeHex("304402204e45e16932b8af514961a1d3a1a25" +
"fdf3f4f7732e9d624c6c61548ab5fb8cd410220fffff" +
"ffffffffffffffffffffffffffebaaedce6af48a03bb" +
"fd25e8cd0364141"),
isValid: false,
},
{
name: "Y > N",
sig: decodeHex("304402204e45e16932b8af514961a1d3a1a25" +
"fdf3f4f7732e9d624c6c61548ab5fb8cd410220fffff" +
"ffffffffffffffffffffffffffebaaedce6af48a03bb" +
"fd25e8cd0364142"),
isValid: false,
},
{
name: "0 len X",
sig: decodeHex("302402000220181522ec8eca07de4860a4acd" +
"d12909d831cc56cbbac4622082221a8768d1d09"),
isValid: false,
},
{
name: "0 len Y",
sig: decodeHex("302402204e45e16932b8af514961a1d3a1a25" +
"fdf3f4f7732e9d624c6c61548ab5fb8cd410200"),
isValid: false,
},
{
name: "extra R padding",
sig: decodeHex("30450221004e45e16932b8af514961a1d3a1a" +
"25fdf3f4f7732e9d624c6c61548ab5fb8cd410220181" +
"522ec8eca07de4860a4acdd12909d831cc56cbbac462" +
"2082221a8768d1d09"),
isValid: false,
},
{
name: "extra S padding",
sig: decodeHex("304502204e45e16932b8af514961a1d3a1a25" +
"fdf3f4f7732e9d624c6c61548ab5fb8cd41022100181" +
"522ec8eca07de4860a4acdd12909d831cc56cbbac462" +
"2082221a8768d1d09"),
isValid: false,
},
}
flags := txscript.ScriptVerifyStrictEncoding
for _, test := range tests {
err := txscript.TstCheckSignatureEncoding(test.sig, flags)
if err != nil && test.isValid {
t.Errorf("checkSignatureEncoding test '%s' failed "+
"when it should have succeeded: %v", test.name,
err)
} else if err == nil && !test.isValid {
t.Errorf("checkSignatureEncooding test '%s' succeeded "+
"when it should have failed", test.name)
}
}
}

File diff suppressed because it is too large Load diff

View file

@ -2,7 +2,7 @@
// Use of this source code is governed by an ISC // Use of this source code is governed by an ISC
// license that can be found in the LICENSE file. // license that can be found in the LICENSE file.
package txscript package txscript_test
import ( import (
"encoding/hex" "encoding/hex"
@ -14,6 +14,7 @@ import (
"strings" "strings"
"testing" "testing"
. "github.com/btcsuite/btcd/txscript"
"github.com/btcsuite/btcd/wire" "github.com/btcsuite/btcd/wire"
"github.com/btcsuite/btcutil" "github.com/btcsuite/btcutil"
) )
@ -43,30 +44,46 @@ func parseHex(tok string) ([]byte, error) {
return hex.DecodeString(tok[2:]) return hex.DecodeString(tok[2:])
} }
// shortFormOps holds a map of opcode names to values for use in short form
// parsing. It is declared here so it only needs to be created once.
var shortFormOps map[string]byte
// parseShortForm parses a string as as used in the Bitcoin Core reference tests // parseShortForm parses a string as as used in the Bitcoin Core reference tests
// into the script it came from. // into the script it came from.
//
// The format used for these tests is pretty simple if ad-hoc:
// - Opcodes other than the push opcodes and unknown are present as
// either OP_NAME or just NAME
// - Plain numbers are made into push operations
// - Numbers beginning with 0x are inserted into the []byte as-is (so
// 0x14 is OP_DATA_20)
// - Single quoted strings are pushed as data
// - Anything else is an error
func parseShortForm(script string) ([]byte, error) { func parseShortForm(script string) ([]byte, error) {
ops := make(map[string]byte) // Only create the short form opcode map once.
if shortFormOps == nil {
ops := make(map[string]byte)
for opcodeName, opcodeValue := range OpcodeByName {
if strings.Contains(opcodeName, "OP_UNKNOWN") {
continue
}
ops[opcodeName] = opcodeValue
// the format used for these tests is pretty simple if ad-hoc: // The opcodes named OP_# can't have the OP_ prefix
// - opcodes other than the push opcodes and unknown are present as // stripped or they would conflict with the plain
// either OP_NAME or just NAME // numbers. Also, since OP_FALSE and OP_TRUE are
// - plain numbers are made into push operations // aliases for the OP_0, and OP_1, respectively, they
// - numbers beginning with 0x are inserted into the []byte as-is (so // have the same value, so detect those by name and
// 0x14 is OP_DATA_20) // allow them.
// - single quoted strings are pushed as data. if (opcodeName == "OP_FALSE" || opcodeName == "OP_TRUE") ||
// - anything else is an error. (opcodeValue != OP_0 && (opcodeValue < OP_1 ||
for opcodeName, opcodeValue := range OpcodeByName { opcodeValue > OP_16)) {
if opcodeValue < OP_NOP && opcodeValue != OP_RESERVED {
continue ops[strings.TrimPrefix(opcodeName, "OP_")] = opcodeValue
}
} }
if strings.Contains(opcodeName, "OP_UNKNOWN") { shortFormOps = ops
continue
}
ops[opcodeName] = opcodeValue
ops[strings.TrimPrefix(opcodeName, "OP_")] = opcodeValue
} }
// do once, build map.
// Split only does one separator so convert all \n and tab into space. // Split only does one separator so convert all \n and tab into space.
script = strings.Replace(script, "\n", " ", -1) script = strings.Replace(script, "\n", " ", -1)
@ -83,12 +100,11 @@ func parseShortForm(script string) ([]byte, error) {
builder.AddInt64(num) builder.AddInt64(num)
continue continue
} else if bts, err := parseHex(tok); err == nil { } else if bts, err := parseHex(tok); err == nil {
// naughty... builder.TstConcatRawScript(bts)
builder.script = append(builder.script, bts...)
} else if len(tok) >= 2 && } else if len(tok) >= 2 &&
tok[0] == '\'' && tok[len(tok)-1] == '\'' { tok[0] == '\'' && tok[len(tok)-1] == '\'' {
builder.AddFullData([]byte(tok[1 : len(tok)-1])) builder.AddFullData([]byte(tok[1 : len(tok)-1]))
} else if opcode, ok := ops[tok]; ok { } else if opcode, ok := shortFormOps[tok]; ok {
builder.AddOp(opcode) builder.AddOp(opcode)
} else { } else {
return nil, fmt.Errorf("bad token \"%s\"", tok) return nil, fmt.Errorf("bad token \"%s\"", tok)

File diff suppressed because it is too large Load diff

View file

@ -141,7 +141,7 @@ func TestScriptBuilderAddData(t *testing.T) {
useFull bool // use AddFullData instead of AddData. useFull bool // use AddFullData instead of AddData.
}{ }{
// BIP0062: Pushing an empty byte sequence must use OP_0. // BIP0062: Pushing an empty byte sequence must use OP_0.
{name: "push empty byte sequence", data: []byte{}, expected: []byte{txscript.OP_0}}, {name: "push empty byte sequence", data: nil, expected: []byte{txscript.OP_0}},
{name: "push 1 byte 0x00", data: []byte{0x00}, expected: []byte{txscript.OP_0}}, {name: "push 1 byte 0x00", data: []byte{0x00}, expected: []byte{txscript.OP_0}},
// BIP0062: Pushing a 1-byte sequence of byte 0x01 through 0x10 must use OP_n. // BIP0062: Pushing a 1-byte sequence of byte 0x01 through 0x10 must use OP_n.
@ -213,17 +213,17 @@ func TestScriptBuilderAddData(t *testing.T) {
{ {
name: "push data len 521", name: "push data len 521",
data: bytes.Repeat([]byte{0x49}, 521), data: bytes.Repeat([]byte{0x49}, 521),
expected: []byte{}, expected: nil,
}, },
{ {
name: "push data len 32767 (canonical)", name: "push data len 32767 (canonical)",
data: bytes.Repeat([]byte{0x49}, 32767), data: bytes.Repeat([]byte{0x49}, 32767),
expected: []byte{}, expected: nil,
}, },
{ {
name: "push data len 65536 (canonical)", name: "push data len 65536 (canonical)",
data: bytes.Repeat([]byte{0x49}, 65536), data: bytes.Repeat([]byte{0x49}, 65536),
expected: []byte{}, expected: nil,
}, },
// Additional tests for the PushFullData function that // Additional tests for the PushFullData function that

View file

@ -78,7 +78,7 @@ func signAndCheck(msg string, tx *wire.MsgTx, idx int, pkScript []byte,
previousScript []byte) error { previousScript []byte) error {
sigScript, err := txscript.SignTxOutput(&chaincfg.TestNet3Params, tx, sigScript, err := txscript.SignTxOutput(&chaincfg.TestNet3Params, tx,
idx, pkScript, hashType, kdb, sdb, []byte{}) idx, pkScript, hashType, kdb, sdb, nil)
if err != nil { if err != nil {
return fmt.Errorf("failed to sign output %s: %v", msg, err) return fmt.Errorf("failed to sign output %s: %v", msg, err)
} }
@ -170,7 +170,7 @@ func TestSignTxOutput(t *testing.T) {
if err := signAndCheck(msg, tx, i, pkScript, hashType, if err := signAndCheck(msg, tx, i, pkScript, hashType,
mkGetKey(map[string]addressToKey{ mkGetKey(map[string]addressToKey{
address.EncodeAddress(): {key, false}, address.EncodeAddress(): {key, false},
}), mkGetScript(nil), []byte{}); err != nil { }), mkGetScript(nil), nil); err != nil {
t.Error(err) t.Error(err)
break break
} }
@ -208,7 +208,7 @@ func TestSignTxOutput(t *testing.T) {
&chaincfg.TestNet3Params, tx, i, pkScript, &chaincfg.TestNet3Params, tx, i, pkScript,
hashType, mkGetKey(map[string]addressToKey{ hashType, mkGetKey(map[string]addressToKey{
address.EncodeAddress(): {key, false}, address.EncodeAddress(): {key, false},
}), mkGetScript(nil), []byte{}) }), mkGetScript(nil), nil)
if err != nil { if err != nil {
t.Errorf("failed to sign output %s: %v", msg, t.Errorf("failed to sign output %s: %v", msg,
err) err)
@ -268,7 +268,7 @@ func TestSignTxOutput(t *testing.T) {
if err := signAndCheck(msg, tx, i, pkScript, hashType, if err := signAndCheck(msg, tx, i, pkScript, hashType,
mkGetKey(map[string]addressToKey{ mkGetKey(map[string]addressToKey{
address.EncodeAddress(): {key, true}, address.EncodeAddress(): {key, true},
}), mkGetScript(nil), []byte{}); err != nil { }), mkGetScript(nil), nil); err != nil {
t.Error(err) t.Error(err)
break break
} }
@ -307,7 +307,7 @@ func TestSignTxOutput(t *testing.T) {
&chaincfg.TestNet3Params, tx, i, pkScript, &chaincfg.TestNet3Params, tx, i, pkScript,
hashType, mkGetKey(map[string]addressToKey{ hashType, mkGetKey(map[string]addressToKey{
address.EncodeAddress(): {key, true}, address.EncodeAddress(): {key, true},
}), mkGetScript(nil), []byte{}) }), mkGetScript(nil), nil)
if err != nil { if err != nil {
t.Errorf("failed to sign output %s: %v", msg, t.Errorf("failed to sign output %s: %v", msg,
err) err)
@ -367,7 +367,7 @@ func TestSignTxOutput(t *testing.T) {
if err := signAndCheck(msg, tx, i, pkScript, hashType, if err := signAndCheck(msg, tx, i, pkScript, hashType,
mkGetKey(map[string]addressToKey{ mkGetKey(map[string]addressToKey{
address.EncodeAddress(): {key, false}, address.EncodeAddress(): {key, false},
}), mkGetScript(nil), []byte{}); err != nil { }), mkGetScript(nil), nil); err != nil {
t.Error(err) t.Error(err)
break break
} }
@ -406,7 +406,7 @@ func TestSignTxOutput(t *testing.T) {
&chaincfg.TestNet3Params, tx, i, pkScript, &chaincfg.TestNet3Params, tx, i, pkScript,
hashType, mkGetKey(map[string]addressToKey{ hashType, mkGetKey(map[string]addressToKey{
address.EncodeAddress(): {key, false}, address.EncodeAddress(): {key, false},
}), mkGetScript(nil), []byte{}) }), mkGetScript(nil), nil)
if err != nil { if err != nil {
t.Errorf("failed to sign output %s: %v", msg, t.Errorf("failed to sign output %s: %v", msg,
err) err)
@ -466,7 +466,7 @@ func TestSignTxOutput(t *testing.T) {
if err := signAndCheck(msg, tx, i, pkScript, hashType, if err := signAndCheck(msg, tx, i, pkScript, hashType,
mkGetKey(map[string]addressToKey{ mkGetKey(map[string]addressToKey{
address.EncodeAddress(): {key, true}, address.EncodeAddress(): {key, true},
}), mkGetScript(nil), []byte{}); err != nil { }), mkGetScript(nil), nil); err != nil {
t.Error(err) t.Error(err)
break break
} }
@ -505,7 +505,7 @@ func TestSignTxOutput(t *testing.T) {
&chaincfg.TestNet3Params, tx, i, pkScript, &chaincfg.TestNet3Params, tx, i, pkScript,
hashType, mkGetKey(map[string]addressToKey{ hashType, mkGetKey(map[string]addressToKey{
address.EncodeAddress(): {key, true}, address.EncodeAddress(): {key, true},
}), mkGetScript(nil), []byte{}) }), mkGetScript(nil), nil)
if err != nil { if err != nil {
t.Errorf("failed to sign output %s: %v", msg, t.Errorf("failed to sign output %s: %v", msg,
err) err)
@ -585,7 +585,7 @@ func TestSignTxOutput(t *testing.T) {
address.EncodeAddress(): {key, false}, address.EncodeAddress(): {key, false},
}), mkGetScript(map[string][]byte{ }), mkGetScript(map[string][]byte{
scriptAddr.EncodeAddress(): pkScript, scriptAddr.EncodeAddress(): pkScript,
}), []byte{}); err != nil { }), nil); err != nil {
t.Error(err) t.Error(err)
break break
} }
@ -642,7 +642,7 @@ func TestSignTxOutput(t *testing.T) {
address.EncodeAddress(): {key, false}, address.EncodeAddress(): {key, false},
}), mkGetScript(map[string][]byte{ }), mkGetScript(map[string][]byte{
scriptAddr.EncodeAddress(): pkScript, scriptAddr.EncodeAddress(): pkScript,
}), []byte{}) }), nil)
if err != nil { if err != nil {
t.Errorf("failed to sign output %s: %v", msg, t.Errorf("failed to sign output %s: %v", msg,
err) err)
@ -657,7 +657,7 @@ func TestSignTxOutput(t *testing.T) {
address.EncodeAddress(): {key, false}, address.EncodeAddress(): {key, false},
}), mkGetScript(map[string][]byte{ }), mkGetScript(map[string][]byte{
scriptAddr.EncodeAddress(): pkScript, scriptAddr.EncodeAddress(): pkScript,
}), []byte{}) }), nil)
if err != nil { if err != nil {
t.Errorf("failed to sign output %s a "+ t.Errorf("failed to sign output %s a "+
"second time: %v", msg, err) "second time: %v", msg, err)
@ -723,7 +723,7 @@ func TestSignTxOutput(t *testing.T) {
address.EncodeAddress(): {key, true}, address.EncodeAddress(): {key, true},
}), mkGetScript(map[string][]byte{ }), mkGetScript(map[string][]byte{
scriptAddr.EncodeAddress(): pkScript, scriptAddr.EncodeAddress(): pkScript,
}), []byte{}); err != nil { }), nil); err != nil {
t.Error(err) t.Error(err)
break break
} }
@ -780,7 +780,7 @@ func TestSignTxOutput(t *testing.T) {
address.EncodeAddress(): {key, true}, address.EncodeAddress(): {key, true},
}), mkGetScript(map[string][]byte{ }), mkGetScript(map[string][]byte{
scriptAddr.EncodeAddress(): pkScript, scriptAddr.EncodeAddress(): pkScript,
}), []byte{}) }), nil)
if err != nil { if err != nil {
t.Errorf("failed to sign output %s: %v", msg, t.Errorf("failed to sign output %s: %v", msg,
err) err)
@ -795,7 +795,7 @@ func TestSignTxOutput(t *testing.T) {
address.EncodeAddress(): {key, true}, address.EncodeAddress(): {key, true},
}), mkGetScript(map[string][]byte{ }), mkGetScript(map[string][]byte{
scriptAddr.EncodeAddress(): pkScript, scriptAddr.EncodeAddress(): pkScript,
}), []byte{}) }), nil)
if err != nil { if err != nil {
t.Errorf("failed to sign output %s a "+ t.Errorf("failed to sign output %s a "+
"second time: %v", msg, err) "second time: %v", msg, err)
@ -861,7 +861,7 @@ func TestSignTxOutput(t *testing.T) {
address.EncodeAddress(): {key, false}, address.EncodeAddress(): {key, false},
}), mkGetScript(map[string][]byte{ }), mkGetScript(map[string][]byte{
scriptAddr.EncodeAddress(): pkScript, scriptAddr.EncodeAddress(): pkScript,
}), []byte{}); err != nil { }), nil); err != nil {
t.Error(err) t.Error(err)
break break
} }
@ -918,7 +918,7 @@ func TestSignTxOutput(t *testing.T) {
address.EncodeAddress(): {key, false}, address.EncodeAddress(): {key, false},
}), mkGetScript(map[string][]byte{ }), mkGetScript(map[string][]byte{
scriptAddr.EncodeAddress(): pkScript, scriptAddr.EncodeAddress(): pkScript,
}), []byte{}) }), nil)
if err != nil { if err != nil {
t.Errorf("failed to sign output %s: %v", msg, t.Errorf("failed to sign output %s: %v", msg,
err) err)
@ -933,7 +933,7 @@ func TestSignTxOutput(t *testing.T) {
address.EncodeAddress(): {key, false}, address.EncodeAddress(): {key, false},
}), mkGetScript(map[string][]byte{ }), mkGetScript(map[string][]byte{
scriptAddr.EncodeAddress(): pkScript, scriptAddr.EncodeAddress(): pkScript,
}), []byte{}) }), nil)
if err != nil { if err != nil {
t.Errorf("failed to sign output %s a "+ t.Errorf("failed to sign output %s a "+
"second time: %v", msg, err) "second time: %v", msg, err)
@ -999,7 +999,7 @@ func TestSignTxOutput(t *testing.T) {
address.EncodeAddress(): {key, true}, address.EncodeAddress(): {key, true},
}), mkGetScript(map[string][]byte{ }), mkGetScript(map[string][]byte{
scriptAddr.EncodeAddress(): pkScript, scriptAddr.EncodeAddress(): pkScript,
}), []byte{}); err != nil { }), nil); err != nil {
t.Error(err) t.Error(err)
break break
} }
@ -1056,7 +1056,7 @@ func TestSignTxOutput(t *testing.T) {
address.EncodeAddress(): {key, true}, address.EncodeAddress(): {key, true},
}), mkGetScript(map[string][]byte{ }), mkGetScript(map[string][]byte{
scriptAddr.EncodeAddress(): pkScript, scriptAddr.EncodeAddress(): pkScript,
}), []byte{}) }), nil)
if err != nil { if err != nil {
t.Errorf("failed to sign output %s: %v", msg, t.Errorf("failed to sign output %s: %v", msg,
err) err)
@ -1071,7 +1071,7 @@ func TestSignTxOutput(t *testing.T) {
address.EncodeAddress(): {key, true}, address.EncodeAddress(): {key, true},
}), mkGetScript(map[string][]byte{ }), mkGetScript(map[string][]byte{
scriptAddr.EncodeAddress(): pkScript, scriptAddr.EncodeAddress(): pkScript,
}), []byte{}) }), nil)
if err != nil { if err != nil {
t.Errorf("failed to sign output %s a "+ t.Errorf("failed to sign output %s a "+
"second time: %v", msg, err) "second time: %v", msg, err)
@ -1157,7 +1157,7 @@ func TestSignTxOutput(t *testing.T) {
address2.EncodeAddress(): {key2, true}, address2.EncodeAddress(): {key2, true},
}), mkGetScript(map[string][]byte{ }), mkGetScript(map[string][]byte{
scriptAddr.EncodeAddress(): pkScript, scriptAddr.EncodeAddress(): pkScript,
}), []byte{}); err != nil { }), nil); err != nil {
t.Error(err) t.Error(err)
break break
} }
@ -1233,7 +1233,7 @@ func TestSignTxOutput(t *testing.T) {
address1.EncodeAddress(): {key1, true}, address1.EncodeAddress(): {key1, true},
}), mkGetScript(map[string][]byte{ }), mkGetScript(map[string][]byte{
scriptAddr.EncodeAddress(): pkScript, scriptAddr.EncodeAddress(): pkScript,
}), []byte{}) }), nil)
if err != nil { if err != nil {
t.Errorf("failed to sign output %s: %v", msg, t.Errorf("failed to sign output %s: %v", msg,
err) err)
@ -1340,7 +1340,7 @@ func TestSignTxOutput(t *testing.T) {
address1.EncodeAddress(): {key1, true}, address1.EncodeAddress(): {key1, true},
}), mkGetScript(map[string][]byte{ }), mkGetScript(map[string][]byte{
scriptAddr.EncodeAddress(): pkScript, scriptAddr.EncodeAddress(): pkScript,
}), []byte{}) }), nil)
if err != nil { if err != nil {
t.Errorf("failed to sign output %s: %v", msg, t.Errorf("failed to sign output %s: %v", msg,
err) err)

View file

@ -39,7 +39,7 @@ func TestStack(t *testing.T) {
return err return err
}, },
ErrStackUnderflow, ErrStackUnderflow,
[][]byte{}, nil,
}, },
{ {
"peek underflow (int)", "peek underflow (int)",
@ -49,7 +49,7 @@ func TestStack(t *testing.T) {
return err return err
}, },
ErrStackUnderflow, ErrStackUnderflow,
[][]byte{}, nil,
}, },
{ {
"peek underflow (bool)", "peek underflow (bool)",
@ -59,7 +59,7 @@ func TestStack(t *testing.T) {
return err return err
}, },
ErrStackUnderflow, ErrStackUnderflow,
[][]byte{}, nil,
}, },
{ {
"pop", "pop",
@ -106,7 +106,7 @@ func TestStack(t *testing.T) {
return nil return nil
}, },
nil, nil,
[][]byte{}, nil,
}, },
{ {
"pop underflow", "pop underflow",
@ -121,7 +121,7 @@ func TestStack(t *testing.T) {
return nil return nil
}, },
ErrStackUnderflow, ErrStackUnderflow,
[][]byte{}, nil,
}, },
{ {
"pop bool", "pop bool",
@ -138,7 +138,7 @@ func TestStack(t *testing.T) {
return nil return nil
}, },
nil, nil,
[][]byte{}, nil,
}, },
{ {
"pop bool", "pop bool",
@ -155,11 +155,11 @@ func TestStack(t *testing.T) {
return nil return nil
}, },
nil, nil,
[][]byte{}, nil,
}, },
{ {
"pop bool", "pop bool",
[][]byte{}, nil,
func(s *stack) error { func(s *stack) error {
_, err := s.PopBool() _, err := s.PopBool()
if err != nil { if err != nil {
@ -169,7 +169,7 @@ func TestStack(t *testing.T) {
return nil return nil
}, },
ErrStackUnderflow, ErrStackUnderflow,
[][]byte{}, nil,
}, },
{ {
"popInt 0", "popInt 0",
@ -185,7 +185,7 @@ func TestStack(t *testing.T) {
return nil return nil
}, },
nil, nil,
[][]byte{}, nil,
}, },
{ {
"popInt -0", "popInt -0",
@ -201,7 +201,7 @@ func TestStack(t *testing.T) {
return nil return nil
}, },
nil, nil,
[][]byte{}, nil,
}, },
{ {
"popInt 1", "popInt 1",
@ -217,7 +217,7 @@ func TestStack(t *testing.T) {
return nil return nil
}, },
nil, nil,
[][]byte{}, nil,
}, },
{ {
"popInt 1 leading 0", "popInt 1 leading 0",
@ -234,7 +234,7 @@ func TestStack(t *testing.T) {
return nil return nil
}, },
nil, nil,
[][]byte{}, nil,
}, },
{ {
"popInt -1", "popInt -1",
@ -250,7 +250,7 @@ func TestStack(t *testing.T) {
return nil return nil
}, },
nil, nil,
[][]byte{}, nil,
}, },
{ {
"popInt -1 leading 0", "popInt -1 leading 0",
@ -267,7 +267,7 @@ func TestStack(t *testing.T) {
return nil return nil
}, },
nil, nil,
[][]byte{}, nil,
}, },
// Triggers the multibyte case in asInt // Triggers the multibyte case in asInt
{ {
@ -285,7 +285,7 @@ func TestStack(t *testing.T) {
return nil return nil
}, },
nil, nil,
[][]byte{}, nil,
}, },
// Confirm that the asInt code doesn't modify the base data. // Confirm that the asInt code doesn't modify the base data.
{ {
@ -307,7 +307,7 @@ func TestStack(t *testing.T) {
}, },
{ {
"PushInt 0", "PushInt 0",
[][]byte{}, nil,
func(s *stack) error { func(s *stack) error {
s.PushInt(scriptNum(0)) s.PushInt(scriptNum(0))
return nil return nil
@ -317,7 +317,7 @@ func TestStack(t *testing.T) {
}, },
{ {
"PushInt 1", "PushInt 1",
[][]byte{}, nil,
func(s *stack) error { func(s *stack) error {
s.PushInt(scriptNum(1)) s.PushInt(scriptNum(1))
return nil return nil
@ -327,7 +327,7 @@ func TestStack(t *testing.T) {
}, },
{ {
"PushInt -1", "PushInt -1",
[][]byte{}, nil,
func(s *stack) error { func(s *stack) error {
s.PushInt(scriptNum(-1)) s.PushInt(scriptNum(-1))
return nil return nil
@ -337,7 +337,7 @@ func TestStack(t *testing.T) {
}, },
{ {
"PushInt two bytes", "PushInt two bytes",
[][]byte{}, nil,
func(s *stack) error { func(s *stack) error {
s.PushInt(scriptNum(256)) s.PushInt(scriptNum(256))
return nil return nil
@ -348,7 +348,7 @@ func TestStack(t *testing.T) {
}, },
{ {
"PushInt leading zeros", "PushInt leading zeros",
[][]byte{}, nil,
func(s *stack) error { func(s *stack) error {
// this will have the highbit set // this will have the highbit set
s.PushInt(scriptNum(128)) s.PushInt(scriptNum(128))
@ -411,7 +411,7 @@ func TestStack(t *testing.T) {
return nil return nil
}, },
ErrStackInvalidArgs, ErrStackInvalidArgs,
[][]byte{}, nil,
}, },
{ {
"dup-1", "dup-1",
@ -425,7 +425,7 @@ func TestStack(t *testing.T) {
return nil return nil
}, },
ErrStackInvalidArgs, ErrStackInvalidArgs,
[][]byte{}, nil,
}, },
{ {
"dup too much", "dup too much",
@ -439,7 +439,7 @@ func TestStack(t *testing.T) {
return nil return nil
}, },
ErrStackUnderflow, ErrStackUnderflow,
[][]byte{}, nil,
}, },
{ {
"dup-1", "dup-1",
@ -453,11 +453,11 @@ func TestStack(t *testing.T) {
return nil return nil
}, },
ErrStackInvalidArgs, ErrStackInvalidArgs,
[][]byte{}, nil,
}, },
{ {
"PushBool true", "PushBool true",
[][]byte{}, nil,
func(s *stack) error { func(s *stack) error {
s.PushBool(true) s.PushBool(true)
@ -468,7 +468,7 @@ func TestStack(t *testing.T) {
}, },
{ {
"PushBool false", "PushBool false",
[][]byte{}, nil,
func(s *stack) error { func(s *stack) error {
s.PushBool(false) s.PushBool(false)
@ -479,7 +479,7 @@ func TestStack(t *testing.T) {
}, },
{ {
"PushBool PopBool", "PushBool PopBool",
[][]byte{}, nil,
func(s *stack) error { func(s *stack) error {
s.PushBool(true) s.PushBool(true)
val, err := s.PopBool() val, err := s.PopBool()
@ -493,11 +493,11 @@ func TestStack(t *testing.T) {
return nil return nil
}, },
nil, nil,
[][]byte{}, nil,
}, },
{ {
"PushBool PopBool 2", "PushBool PopBool 2",
[][]byte{}, nil,
func(s *stack) error { func(s *stack) error {
s.PushBool(false) s.PushBool(false)
val, err := s.PopBool() val, err := s.PopBool()
@ -511,11 +511,11 @@ func TestStack(t *testing.T) {
return nil return nil
}, },
nil, nil,
[][]byte{}, nil,
}, },
{ {
"PushInt PopBool", "PushInt PopBool",
[][]byte{}, nil,
func(s *stack) error { func(s *stack) error {
s.PushInt(scriptNum(1)) s.PushInt(scriptNum(1))
val, err := s.PopBool() val, err := s.PopBool()
@ -529,11 +529,11 @@ func TestStack(t *testing.T) {
return nil return nil
}, },
nil, nil,
[][]byte{}, nil,
}, },
{ {
"PushInt PopBool 2", "PushInt PopBool 2",
[][]byte{}, nil,
func(s *stack) error { func(s *stack) error {
s.PushInt(scriptNum(0)) s.PushInt(scriptNum(0))
val, err := s.PopBool() val, err := s.PopBool()
@ -547,11 +547,11 @@ func TestStack(t *testing.T) {
return nil return nil
}, },
nil, nil,
[][]byte{}, nil,
}, },
{ {
"PushInt PopBool 2", "PushInt PopBool 2",
[][]byte{}, nil,
func(s *stack) error { func(s *stack) error {
s.PushInt(scriptNum(0)) s.PushInt(scriptNum(0))
val, err := s.PopBool() val, err := s.PopBool()
@ -565,7 +565,7 @@ func TestStack(t *testing.T) {
return nil return nil
}, },
nil, nil,
[][]byte{}, nil,
}, },
{ {
"Nip top", "Nip top",
@ -620,16 +620,16 @@ func TestStack(t *testing.T) {
return s.Tuck() return s.Tuck()
}, },
ErrStackUnderflow, ErrStackUnderflow,
[][]byte{}, nil,
}, },
{ {
"all tucked up", "all tucked up",
[][]byte{}, // too few arguments for tuck nil, // too few arguments for tuck
func(s *stack) error { func(s *stack) error {
return s.Tuck() return s.Tuck()
}, },
ErrStackUnderflow, ErrStackUnderflow,
[][]byte{}, nil,
}, },
{ {
"drop 1", "drop 1",
@ -665,7 +665,7 @@ func TestStack(t *testing.T) {
return s.DropN(4) return s.DropN(4)
}, },
nil, nil,
[][]byte{}, nil,
}, },
{ {
"drop 4/5", "drop 4/5",
@ -674,7 +674,7 @@ func TestStack(t *testing.T) {
return s.DropN(5) return s.DropN(5)
}, },
ErrStackUnderflow, ErrStackUnderflow,
[][]byte{}, nil,
}, },
{ {
"drop invalid", "drop invalid",
@ -683,7 +683,7 @@ func TestStack(t *testing.T) {
return s.DropN(0) return s.DropN(0)
}, },
ErrStackInvalidArgs, ErrStackInvalidArgs,
[][]byte{}, nil,
}, },
{ {
"Rot1", "Rot1",
@ -710,7 +710,7 @@ func TestStack(t *testing.T) {
return s.RotN(1) return s.RotN(1)
}, },
ErrStackUnderflow, ErrStackUnderflow,
[][]byte{}, nil,
}, },
{ {
"Rot0", "Rot0",
@ -719,7 +719,7 @@ func TestStack(t *testing.T) {
return s.RotN(0) return s.RotN(0)
}, },
ErrStackInvalidArgs, ErrStackInvalidArgs,
[][]byte{}, nil,
}, },
{ {
"Swap1", "Swap1",
@ -746,7 +746,7 @@ func TestStack(t *testing.T) {
return s.SwapN(1) return s.SwapN(1)
}, },
ErrStackUnderflow, ErrStackUnderflow,
[][]byte{}, nil,
}, },
{ {
"Swap0", "Swap0",
@ -755,7 +755,7 @@ func TestStack(t *testing.T) {
return s.SwapN(0) return s.SwapN(0)
}, },
ErrStackInvalidArgs, ErrStackInvalidArgs,
[][]byte{}, nil,
}, },
{ {
"Over1", "Over1",
@ -782,7 +782,7 @@ func TestStack(t *testing.T) {
return s.OverN(1) return s.OverN(1)
}, },
ErrStackUnderflow, ErrStackUnderflow,
[][]byte{}, nil,
}, },
{ {
"Over0", "Over0",
@ -791,7 +791,7 @@ func TestStack(t *testing.T) {
return s.OverN(0) return s.OverN(0)
}, },
ErrStackInvalidArgs, ErrStackInvalidArgs,
[][]byte{}, nil,
}, },
{ {
"Pick1", "Pick1",
@ -818,7 +818,7 @@ func TestStack(t *testing.T) {
return s.PickN(1) return s.PickN(1)
}, },
ErrStackUnderflow, ErrStackUnderflow,
[][]byte{}, nil,
}, },
{ {
"Roll1", "Roll1",
@ -845,7 +845,7 @@ func TestStack(t *testing.T) {
return s.RollN(1) return s.RollN(1)
}, },
ErrStackUnderflow, ErrStackUnderflow,
[][]byte{}, nil,
}, },
{ {
"Peek bool", "Peek bool",
@ -921,7 +921,7 @@ func TestStack(t *testing.T) {
}, },
{ {
"pop int", "pop int",
[][]byte{}, nil,
func(s *stack) error { func(s *stack) error {
s.PushInt(scriptNum(1)) s.PushInt(scriptNum(1))
// Peek int is otherwise pretty well tested, // Peek int is otherwise pretty well tested,
@ -936,11 +936,11 @@ func TestStack(t *testing.T) {
return nil return nil
}, },
nil, nil,
[][]byte{}, nil,
}, },
{ {
"pop empty", "pop empty",
[][]byte{}, nil,
func(s *stack) error { func(s *stack) error {
// Peek int is otherwise pretty well tested, // Peek int is otherwise pretty well tested,
// just check it works. // just check it works.
@ -948,7 +948,7 @@ func TestStack(t *testing.T) {
return err return err
}, },
ErrStackUnderflow, ErrStackUnderflow,
[][]byte{}, nil,
}, },
} }

View file

@ -372,7 +372,7 @@ func PushedData(script []byte) ([][]byte, error) {
if pop.data != nil { if pop.data != nil {
data = append(data, pop.data) data = append(data, pop.data)
} else if pop.opcode.value == OP_0 { } else if pop.opcode.value == OP_0 {
data = append(data, []byte{}) data = append(data, nil)
} }
} }
return data, nil return data, nil

File diff suppressed because it is too large Load diff