Make several of the reg tests more consistent.

This commit modifies various regression tests to make them more consistent
with other tests throughout the code base.

Also, it allows of all the tests to run in parallel.
This commit is contained in:
Dave Collins 2015-01-29 14:39:44 -06:00
parent f513518b4f
commit 8e6abdb125
3 changed files with 1556 additions and 1510 deletions

View file

@ -12,16 +12,17 @@ import (
"github.com/btcsuite/btcwire" "github.com/btcsuite/btcwire"
) )
// test scripts to test as many opcodes as possible. // TestScripts tests script execution for a wide variety of opcodes. All tests
// All run on a fake tx with a single in, single out. // against a fake transaction with a single input and output.
type opcodeTest struct { func TestScripts(t *testing.T) {
t.Parallel()
tests := []struct {
script []byte script []byte
canonical bool canonical bool
shouldPass bool shouldPass bool
shouldFail error shouldFail error
} }{
var opcodeTests = []opcodeTest{
// does nothing, but doesn't put a true on the stack, should fail // does nothing, but doesn't put a true on the stack, should fail
{script: []byte{btcscript.OP_NOP}, shouldPass: false}, {script: []byte{btcscript.OP_NOP}, shouldPass: false},
// should just put true on the stack, thus passes. // should just put true on the stack, thus passes.
@ -478,9 +479,8 @@ var opcodeTests = []opcodeTest{
{script: []byte{252}, shouldPass: false}, {script: []byte{252}, shouldPass: false},
} }
func testScript(t *testing.T, script []byte, canonical bool) (err error) { // Mock up fake tx used during script execution.
// mock up fake tx. mockTx := &btcwire.MsgTx{
tx := &btcwire.MsgTx{
Version: 1, Version: 1,
TxIn: []*btcwire.TxIn{ TxIn: []*btcwire.TxIn{
{ {
@ -501,38 +501,38 @@ func testScript(t *testing.T, script []byte, canonical bool) (err error) {
LockTime: 0, LockTime: 0,
} }
tx.TxOut[0].PkScript = script for i, test := range tests {
// Parse and execute the test script.
var flags btcscript.ScriptFlags var flags btcscript.ScriptFlags
if canonical { if test.canonical {
flags = btcscript.ScriptCanonicalSignatures flags = btcscript.ScriptCanonicalSignatures
} }
mockTx.TxOut[0].PkScript = test.script
engine, err := btcscript.NewScript(tx.TxIn[0].SignatureScript, sigScript := mockTx.TxIn[0].SignatureScript
tx.TxOut[0].PkScript, 0, tx, flags) engine, err := btcscript.NewScript(sigScript, test.script, 0,
if err != nil { mockTx, flags)
return err
}
return engine.Execute()
}
func TestScripts(t *testing.T) {
// for each entry in the list
for i := range opcodeTests {
shouldPass := opcodeTests[i].shouldPass
shouldFail := opcodeTests[i].shouldFail
err := testScript(t, opcodeTests[i].script, opcodeTests[i].canonical)
if shouldFail != nil {
if err == nil { if err == nil {
t.Errorf("test %d passed should fail with %v", i, err) err = engine.Execute()
} else if shouldFail != err { }
t.Errorf("test %d failed with wrong error [%v], expected [%v]", i, err, shouldFail)
if test.shouldFail != nil {
if err == nil {
t.Errorf("test %d passed should fail with %v",
i, test.shouldFail)
continue
} else if test.shouldFail != err {
t.Errorf("test %d failed with wrong error "+
"[%v], expected [%v]", i, err,
test.shouldFail)
continue
} }
} }
if shouldPass && err != nil { if test.shouldPass && err != nil {
t.Errorf("test %d failed: %v", i, err) t.Errorf("test %d failed: %v", i, err)
} else if !shouldPass && err == nil { continue
} else if !test.shouldPass && err == nil {
t.Errorf("test %d passed, should fail", i) t.Errorf("test %d passed, should fail", i)
continue
} }
} }
} }
@ -4374,6 +4374,8 @@ func testOpcode(t *testing.T, test *detailedTest) {
} }
func TestOpcodes(t *testing.T) { func TestOpcodes(t *testing.T) {
t.Parallel()
for i := range detailedTests { for i := range detailedTests {
testOpcode(t, &detailedTests[i]) testOpcode(t, &detailedTests[i])
} }
@ -4402,6 +4404,8 @@ func testDisasmString(t *testing.T, test *detailedTest) {
} }
func TestDisasmStrings(t *testing.T) { func TestDisasmStrings(t *testing.T) {
t.Parallel()
for i := range detailedTests { for i := range detailedTests {
testDisasmString(t, &detailedTests[i]) testDisasmString(t, &detailedTests[i])
} }
@ -4414,6 +4418,8 @@ func TestDisasmStrings(t *testing.T) {
// While this isn't as precise as using full transaction scripts, this gives // While this isn't as precise as using full transaction scripts, this gives
// us coverage over a wider range of opcodes. // us coverage over a wider range of opcodes.
func TestSigOps(t *testing.T) { func TestSigOps(t *testing.T) {
t.Parallel()
for _, test := range detailedTests { for _, test := range detailedTests {
count := btcscript.GetSigOpCount(test.script) count := btcscript.GetSigOpCount(test.script)
if count != test.nSigOps { if count != test.nSigOps {
@ -4432,6 +4438,8 @@ func TestSigOps(t *testing.T) {
// us coverage over a wider range of opcodes. See script_test.go for tests // us coverage over a wider range of opcodes. See script_test.go for tests
// using real transactions to provide a bit more coverage. // using real transactions to provide a bit more coverage.
func TestPreciseSigOps(t *testing.T) { func TestPreciseSigOps(t *testing.T) {
t.Parallel()
for _, test := range detailedTests { for _, test := range detailedTests {
count := btcscript.GetPreciseSigOpCount( count := btcscript.GetPreciseSigOpCount(
[]byte{btcscript.OP_1}, test.script, false) []byte{btcscript.OP_1}, test.script, false)

View file

@ -30,6 +30,8 @@ func builderScript(builder *btcscript.ScriptBuilder) []byte {
} }
func TestPushedData(t *testing.T) { func TestPushedData(t *testing.T) {
t.Parallel()
var tests = []struct { var tests = []struct {
in []byte in []byte
out [][]byte out [][]byte
@ -90,6 +92,8 @@ func TestPushedData(t *testing.T) {
} }
func TestStandardPushes(t *testing.T) { func TestStandardPushes(t *testing.T) {
t.Parallel()
for i := 0; i < 65535; i++ { for i := 0; i < 65535; i++ {
builder := btcscript.NewScriptBuilder() builder := btcscript.NewScriptBuilder()
builder.AddInt64(int64(i)) builder.AddInt64(int64(i))
@ -1652,12 +1656,16 @@ func testTx(t *testing.T, test txTest) {
} }
func TestTX(t *testing.T) { func TestTX(t *testing.T) {
t.Parallel()
for i := range txTests { for i := range txTests {
testTx(t, txTests[i]) testTx(t, txTests[i])
} }
} }
func TestGetPreciseSignOps(t *testing.T) { func TestGetPreciseSignOps(t *testing.T) {
t.Parallel()
// First we go over the range of tests in testTx and count the sigops in // First we go over the range of tests in testTx and count the sigops in
// them. // them.
for _, test := range txTests { for _, test := range txTests {
@ -1742,6 +1750,8 @@ type scriptInfoTest struct {
} }
func TestScriptInfo(t *testing.T) { func TestScriptInfo(t *testing.T) {
t.Parallel()
for _, test := range txTests { for _, test := range txTests {
si, err := btcscript.CalcScriptInfo( si, err := btcscript.CalcScriptInfo(
test.tx.TxIn[test.idx].SignatureScript, test.tx.TxIn[test.idx].SignatureScript,
@ -1996,6 +2006,8 @@ func testRemoveOpcode(t *testing.T, test *removeOpcodeTest) {
} }
func TestRemoveOpcodes(t *testing.T) { func TestRemoveOpcodes(t *testing.T) {
t.Parallel()
for i := range removeOpcodeTests { for i := range removeOpcodeTests {
testRemoveOpcode(t, &removeOpcodeTests[i]) testRemoveOpcode(t, &removeOpcodeTests[i])
} }
@ -2134,6 +2146,8 @@ func testRemoveOpcodeByData(t *testing.T, test *removeOpcodeByDataTest) {
} }
} }
func TestRemoveOpcodeByDatas(t *testing.T) { func TestRemoveOpcodeByDatas(t *testing.T) {
t.Parallel()
for i := range removeOpcodeByDataTests { for i := range removeOpcodeByDataTests {
testRemoveOpcodeByData(t, &removeOpcodeByDataTests[i]) testRemoveOpcodeByData(t, &removeOpcodeByDataTests[i])
} }
@ -2361,12 +2375,16 @@ func testScriptType(t *testing.T, test *scriptTypeTest) {
} }
func TestScriptTypes(t *testing.T) { func TestScriptTypes(t *testing.T) {
t.Parallel()
for i := range scriptTypeTests { for i := range scriptTypeTests {
testScriptType(t, &scriptTypeTests[i]) testScriptType(t, &scriptTypeTests[i])
} }
} }
func TestIsPayToScriptHash(t *testing.T) { func TestIsPayToScriptHash(t *testing.T) {
t.Parallel()
for _, test := range scriptTypeTests { for _, test := range scriptTypeTests {
shouldBe := (test.scripttype == btcscript.ScriptHashTy) shouldBe := (test.scripttype == btcscript.ScriptHashTy)
p2sh := btcscript.IsPayToScriptHash(test.script) p2sh := btcscript.IsPayToScriptHash(test.script)
@ -2380,6 +2398,8 @@ func TestIsPayToScriptHash(t *testing.T) {
// This test sets the pc to a deliberately bad result then confirms that Step() // This test sets the pc to a deliberately bad result then confirms that Step()
// and Disasm fail correctly. // and Disasm fail correctly.
func TestBadPC(t *testing.T) { func TestBadPC(t *testing.T) {
t.Parallel()
type pcTest struct { type pcTest struct {
script, off int script, off int
} }
@ -2452,6 +2472,8 @@ func TestBadPC(t *testing.T) {
// Most codepaths in CheckErrorCondition() are testd elsewhere, this tests // Most codepaths in CheckErrorCondition() are testd elsewhere, this tests
// the execute early test. // the execute early test.
func TestCheckErrorCondition(t *testing.T) { func TestCheckErrorCondition(t *testing.T) {
t.Parallel()
// tx with almost empty scripts. // tx with almost empty scripts.
tx := &btcwire.MsgTx{ tx := &btcwire.MsgTx{
Version: 1, Version: 1,
@ -2787,6 +2809,8 @@ var SigScriptTests = []TstSigScript{
// created for the MsgTxs in txTests, since they come from the blockchain // created for the MsgTxs in txTests, since they come from the blockchain
// and we don't have the private keys. // and we don't have the private keys.
func TestSignatureScript(t *testing.T) { func TestSignatureScript(t *testing.T) {
t.Parallel()
privKey, _ := btcec.PrivKeyFromBytes(btcec.S256(), privKeyD) privKey, _ := btcec.PrivKeyFromBytes(btcec.S256(), privKeyD)
nexttest: nexttest:
@ -2868,7 +2892,10 @@ nexttest:
} }
} }
var classStringifyTests = []struct { func TestStringifyClass(t *testing.T) {
t.Parallel()
tests := []struct {
name string name string
scriptclass btcscript.ScriptClass scriptclass btcscript.ScriptClass
stringed string stringed string
@ -2910,8 +2937,7 @@ var classStringifyTests = []struct {
}, },
} }
func TestStringifyClass(t *testing.T) { for _, test := range tests {
for _, test := range classStringifyTests {
typeString := test.scriptclass.String() typeString := test.scriptclass.String()
if typeString != test.stringed { if typeString != test.stringed {
t.Errorf("%s: got \"%s\" expected \"%s\"", test.name, t.Errorf("%s: got \"%s\" expected \"%s\"", test.name,
@ -2948,6 +2974,8 @@ func (b *bogusAddress) String() string {
} }
func TestPayToAddrScript(t *testing.T) { func TestPayToAddrScript(t *testing.T) {
t.Parallel()
// 1MirQ9bwyQcGVJPwKUgapu5ouK2E2Ey4gX // 1MirQ9bwyQcGVJPwKUgapu5ouK2E2Ey4gX
p2pkhMain, err := btcutil.NewAddressPubKeyHash([]byte{ p2pkhMain, err := btcutil.NewAddressPubKeyHash([]byte{
0xe3, 0x4c, 0xce, 0x70, 0xc8, 0x63, 0x73, 0x27, 0x3e, 0xfc, 0xe3, 0x4c, 0xce, 0x70, 0xc8, 0x63, 0x73, 0x27, 0x3e, 0xfc,
@ -3102,6 +3130,8 @@ func TestPayToAddrScript(t *testing.T) {
} }
func TestMultiSigScript(t *testing.T) { func TestMultiSigScript(t *testing.T) {
t.Parallel()
// mainnet p2pk 13CG6SJ3yHUXo4Cr2RY4THLLJrNFuG3gUg // mainnet p2pk 13CG6SJ3yHUXo4Cr2RY4THLLJrNFuG3gUg
p2pkCompressedMain, err := btcutil.NewAddressPubKey([]byte{ p2pkCompressedMain, err := btcutil.NewAddressPubKey([]byte{
0x02, 0x19, 0x2d, 0x74, 0xd0, 0xcb, 0x94, 0x34, 0x4c, 0x95, 0x02, 0x19, 0x2d, 0x74, 0xd0, 0xcb, 0x94, 0x34, 0x4c, 0x95,
@ -3322,6 +3352,8 @@ func mkGetScript(scripts map[string][]byte) btcscript.ScriptDB {
} }
func TestSignTxOutput(t *testing.T) { func TestSignTxOutput(t *testing.T) {
t.Parallel()
// make key // make key
// make script based on key. // make script based on key.
// sign with magic pixie dust. // sign with magic pixie dust.
@ -4614,6 +4646,8 @@ func TestSignTxOutput(t *testing.T) {
} }
func TestCalcMultiSigStats(t *testing.T) { func TestCalcMultiSigStats(t *testing.T) {
t.Parallel()
tests := []struct { tests := []struct {
name string name string
script []byte script []byte
@ -4691,6 +4725,8 @@ func TestCalcMultiSigStats(t *testing.T) {
} }
func TestHasCanonicalPushes(t *testing.T) { func TestHasCanonicalPushes(t *testing.T) {
t.Parallel()
tests := []struct { tests := []struct {
name string name string
script []byte script []byte
@ -4723,6 +4759,8 @@ func TestHasCanonicalPushes(t *testing.T) {
} }
func TestIsPushOnlyScript(t *testing.T) { func TestIsPushOnlyScript(t *testing.T) {
t.Parallel()
test := struct { test := struct {
name string name string
script []byte script []byte

View file

@ -14,15 +14,17 @@ import (
"github.com/btcsuite/btcscript" "github.com/btcsuite/btcscript"
) )
type stackTest struct { // TestStack tests that all of the stack operations work as expected.
func TestStack(t *testing.T) {
t.Parallel()
tests := []struct {
name string name string
before [][]byte before [][]byte
operation func(*btcscript.Stack) error operation func(*btcscript.Stack) error
expectedReturn error expectedReturn error
after [][]byte after [][]byte
} }{
var stackTests = []stackTest{
{ {
"noop", "noop",
[][]byte{{1}, {2}, {3}, {4}, {5}}, [][]byte{{1}, {2}, {3}, {4}, {5}},
@ -862,8 +864,8 @@ var stackTests = []stackTest{
"Peek bool", "Peek bool",
[][]byte{{1}}, [][]byte{{1}},
func(stack *btcscript.Stack) error { func(stack *btcscript.Stack) error {
// Peek bool is otherwise pretty well tested, just check // Peek bool is otherwise pretty well tested,
// it works. // just check it works.
val, err := stack.PeekBool(0) val, err := stack.PeekBool(0)
if err != nil { if err != nil {
return err return err
@ -880,8 +882,8 @@ var stackTests = []stackTest{
"Peek bool 2", "Peek bool 2",
[][]byte{{0}}, [][]byte{{0}},
func(stack *btcscript.Stack) error { func(stack *btcscript.Stack) error {
// Peek bool is otherwise pretty well tested, just check // Peek bool is otherwise pretty well tested,
// it works. // just check it works.
val, err := stack.PeekBool(0) val, err := stack.PeekBool(0)
if err != nil { if err != nil {
return err return err
@ -898,8 +900,8 @@ var stackTests = []stackTest{
"Peek int", "Peek int",
[][]byte{{1}}, [][]byte{{1}},
func(stack *btcscript.Stack) error { func(stack *btcscript.Stack) error {
// Peek int is otherwise pretty well tested, just check // Peek int is otherwise pretty well tested,
// it works. // just check it works.
val, err := stack.PeekInt(0) val, err := stack.PeekInt(0)
if err != nil { if err != nil {
return err return err
@ -916,8 +918,8 @@ var stackTests = []stackTest{
"Peek int 2", "Peek int 2",
[][]byte{{0}}, [][]byte{{0}},
func(stack *btcscript.Stack) error { func(stack *btcscript.Stack) error {
// Peek int is otherwise pretty well tested, just check // Peek int is otherwise pretty well tested,
// it works. // just check it works.
val, err := stack.PeekInt(0) val, err := stack.PeekInt(0)
if err != nil { if err != nil {
return err return err
@ -935,8 +937,8 @@ var stackTests = []stackTest{
[][]byte{}, [][]byte{},
func(stack *btcscript.Stack) error { func(stack *btcscript.Stack) error {
stack.PushInt(big.NewInt(1)) stack.PushInt(big.NewInt(1))
// Peek int is otherwise pretty well tested, just check // Peek int is otherwise pretty well tested,
// it works. // just check it works.
val, err := stack.PopInt() val, err := stack.PopInt()
if err != nil { if err != nil {
return err return err
@ -953,8 +955,8 @@ var stackTests = []stackTest{
"pop empty", "pop empty",
[][]byte{}, [][]byte{},
func(stack *btcscript.Stack) error { func(stack *btcscript.Stack) error {
// Peek int is otherwise pretty well tested, just check // Peek int is otherwise pretty well tested,
// it works. // just check it works.
_, err := stack.PopInt() _, err := stack.PopInt()
return err return err
}, },
@ -963,7 +965,7 @@ var stackTests = []stackTest{
}, },
} }
func doTest(t *testing.T, test stackTest) { for _, test := range tests {
stack := btcscript.Stack{} stack := btcscript.Stack{}
for i := range test.before { for i := range test.before {
@ -971,16 +973,19 @@ func doTest(t *testing.T, test stackTest) {
} }
err := test.operation(&stack) err := test.operation(&stack)
if err != test.expectedReturn { if err != test.expectedReturn {
t.Errorf("%s: operation return not what expected: %v vs %v", t.Errorf("%s: operation return not what expected: %v "+
test.name, err, test.expectedReturn) "vs %v", test.name, err, test.expectedReturn)
} }
if err != nil { if err != nil {
return continue
} }
if len(test.after) != stack.Depth() { if len(test.after) != stack.Depth() {
t.Errorf("%s: stack depth doesn't match expected: %v vs %v", t.Errorf("%s: stack depth doesn't match expected: %v "+
test.name, len(test.after), stack.Depth()) "vs %v", test.name, len(test.after),
stack.Depth())
} }
for i := range test.after { for i := range test.after {
val, err := stack.PeekByteArray(stack.Depth() - i - 1) val, err := stack.PeekByteArray(stack.Depth() - i - 1)
if err != nil { if err != nil {
@ -997,9 +1002,4 @@ func doTest(t *testing.T, test stackTest) {
} }
} }
} }
func TestStack(t *testing.T) {
for i := range stackTests {
doTest(t, stackTests[i])
}
} }