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:
parent
f513518b4f
commit
8e6abdb125
3 changed files with 1556 additions and 1510 deletions
|
@ -12,16 +12,17 @@ import (
|
|||
"github.com/btcsuite/btcwire"
|
||||
)
|
||||
|
||||
// test scripts to test as many opcodes as possible.
|
||||
// All run on a fake tx with a single in, single out.
|
||||
type opcodeTest struct {
|
||||
// TestScripts tests script execution for a wide variety of opcodes. All tests
|
||||
// against a fake transaction with a single input and output.
|
||||
func TestScripts(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
tests := []struct {
|
||||
script []byte
|
||||
canonical bool
|
||||
shouldPass bool
|
||||
shouldFail error
|
||||
}
|
||||
|
||||
var opcodeTests = []opcodeTest{
|
||||
}{
|
||||
// does nothing, but doesn't put a true on the stack, should fail
|
||||
{script: []byte{btcscript.OP_NOP}, shouldPass: false},
|
||||
// should just put true on the stack, thus passes.
|
||||
|
@ -476,11 +477,10 @@ var opcodeTests = []opcodeTest{
|
|||
{script: []byte{250}, shouldPass: false},
|
||||
{script: []byte{251}, shouldPass: false},
|
||||
{script: []byte{252}, shouldPass: false},
|
||||
}
|
||||
}
|
||||
|
||||
func testScript(t *testing.T, script []byte, canonical bool) (err error) {
|
||||
// mock up fake tx.
|
||||
tx := &btcwire.MsgTx{
|
||||
// Mock up fake tx used during script execution.
|
||||
mockTx := &btcwire.MsgTx{
|
||||
Version: 1,
|
||||
TxIn: []*btcwire.TxIn{
|
||||
{
|
||||
|
@ -501,38 +501,38 @@ func testScript(t *testing.T, script []byte, canonical bool) (err error) {
|
|||
LockTime: 0,
|
||||
}
|
||||
|
||||
tx.TxOut[0].PkScript = script
|
||||
|
||||
for i, test := range tests {
|
||||
// Parse and execute the test script.
|
||||
var flags btcscript.ScriptFlags
|
||||
if canonical {
|
||||
if test.canonical {
|
||||
flags = btcscript.ScriptCanonicalSignatures
|
||||
}
|
||||
|
||||
engine, err := btcscript.NewScript(tx.TxIn[0].SignatureScript,
|
||||
tx.TxOut[0].PkScript, 0, tx, flags)
|
||||
if err != nil {
|
||||
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 {
|
||||
mockTx.TxOut[0].PkScript = test.script
|
||||
sigScript := mockTx.TxIn[0].SignatureScript
|
||||
engine, err := btcscript.NewScript(sigScript, test.script, 0,
|
||||
mockTx, flags)
|
||||
if err == nil {
|
||||
t.Errorf("test %d passed should fail with %v", i, err)
|
||||
} else if shouldFail != err {
|
||||
t.Errorf("test %d failed with wrong error [%v], expected [%v]", i, err, shouldFail)
|
||||
err = engine.Execute()
|
||||
}
|
||||
|
||||
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)
|
||||
} else if !shouldPass && err == nil {
|
||||
continue
|
||||
} else if !test.shouldPass && err == nil {
|
||||
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) {
|
||||
t.Parallel()
|
||||
|
||||
for i := range detailedTests {
|
||||
testOpcode(t, &detailedTests[i])
|
||||
}
|
||||
|
@ -4402,6 +4404,8 @@ func testDisasmString(t *testing.T, test *detailedTest) {
|
|||
}
|
||||
|
||||
func TestDisasmStrings(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
for i := range detailedTests {
|
||||
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
|
||||
// us coverage over a wider range of opcodes.
|
||||
func TestSigOps(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
for _, test := range detailedTests {
|
||||
count := btcscript.GetSigOpCount(test.script)
|
||||
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
|
||||
// using real transactions to provide a bit more coverage.
|
||||
func TestPreciseSigOps(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
for _, test := range detailedTests {
|
||||
count := btcscript.GetPreciseSigOpCount(
|
||||
[]byte{btcscript.OP_1}, test.script, false)
|
||||
|
|
|
@ -30,6 +30,8 @@ func builderScript(builder *btcscript.ScriptBuilder) []byte {
|
|||
}
|
||||
|
||||
func TestPushedData(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
var tests = []struct {
|
||||
in []byte
|
||||
out [][]byte
|
||||
|
@ -90,6 +92,8 @@ func TestPushedData(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestStandardPushes(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
for i := 0; i < 65535; i++ {
|
||||
builder := btcscript.NewScriptBuilder()
|
||||
builder.AddInt64(int64(i))
|
||||
|
@ -1652,12 +1656,16 @@ func testTx(t *testing.T, test txTest) {
|
|||
}
|
||||
|
||||
func TestTX(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
for i := range txTests {
|
||||
testTx(t, txTests[i])
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetPreciseSignOps(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
// First we go over the range of tests in testTx and count the sigops in
|
||||
// them.
|
||||
for _, test := range txTests {
|
||||
|
@ -1742,6 +1750,8 @@ type scriptInfoTest struct {
|
|||
}
|
||||
|
||||
func TestScriptInfo(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
for _, test := range txTests {
|
||||
si, err := btcscript.CalcScriptInfo(
|
||||
test.tx.TxIn[test.idx].SignatureScript,
|
||||
|
@ -1996,6 +2006,8 @@ func testRemoveOpcode(t *testing.T, test *removeOpcodeTest) {
|
|||
}
|
||||
|
||||
func TestRemoveOpcodes(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
for i := range removeOpcodeTests {
|
||||
testRemoveOpcode(t, &removeOpcodeTests[i])
|
||||
}
|
||||
|
@ -2134,6 +2146,8 @@ func testRemoveOpcodeByData(t *testing.T, test *removeOpcodeByDataTest) {
|
|||
}
|
||||
}
|
||||
func TestRemoveOpcodeByDatas(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
for i := range removeOpcodeByDataTests {
|
||||
testRemoveOpcodeByData(t, &removeOpcodeByDataTests[i])
|
||||
}
|
||||
|
@ -2361,12 +2375,16 @@ func testScriptType(t *testing.T, test *scriptTypeTest) {
|
|||
}
|
||||
|
||||
func TestScriptTypes(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
for i := range scriptTypeTests {
|
||||
testScriptType(t, &scriptTypeTests[i])
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsPayToScriptHash(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
for _, test := range scriptTypeTests {
|
||||
shouldBe := (test.scripttype == btcscript.ScriptHashTy)
|
||||
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()
|
||||
// and Disasm fail correctly.
|
||||
func TestBadPC(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
type pcTest struct {
|
||||
script, off int
|
||||
}
|
||||
|
@ -2452,6 +2472,8 @@ func TestBadPC(t *testing.T) {
|
|||
// Most codepaths in CheckErrorCondition() are testd elsewhere, this tests
|
||||
// the execute early test.
|
||||
func TestCheckErrorCondition(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
// tx with almost empty scripts.
|
||||
tx := &btcwire.MsgTx{
|
||||
Version: 1,
|
||||
|
@ -2787,6 +2809,8 @@ var SigScriptTests = []TstSigScript{
|
|||
// created for the MsgTxs in txTests, since they come from the blockchain
|
||||
// and we don't have the private keys.
|
||||
func TestSignatureScript(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
privKey, _ := btcec.PrivKeyFromBytes(btcec.S256(), privKeyD)
|
||||
|
||||
nexttest:
|
||||
|
@ -2868,11 +2892,14 @@ nexttest:
|
|||
}
|
||||
}
|
||||
|
||||
var classStringifyTests = []struct {
|
||||
func TestStringifyClass(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
scriptclass btcscript.ScriptClass
|
||||
stringed string
|
||||
}{
|
||||
}{
|
||||
{
|
||||
name: "nonstandardty",
|
||||
scriptclass: btcscript.NonStandardTy,
|
||||
|
@ -2908,10 +2935,9 @@ var classStringifyTests = []struct {
|
|||
scriptclass: btcscript.ScriptClass(255),
|
||||
stringed: "Invalid",
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func TestStringifyClass(t *testing.T) {
|
||||
for _, test := range classStringifyTests {
|
||||
for _, test := range tests {
|
||||
typeString := test.scriptclass.String()
|
||||
if typeString != test.stringed {
|
||||
t.Errorf("%s: got \"%s\" expected \"%s\"", test.name,
|
||||
|
@ -2948,6 +2974,8 @@ func (b *bogusAddress) String() string {
|
|||
}
|
||||
|
||||
func TestPayToAddrScript(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
// 1MirQ9bwyQcGVJPwKUgapu5ouK2E2Ey4gX
|
||||
p2pkhMain, err := btcutil.NewAddressPubKeyHash([]byte{
|
||||
0xe3, 0x4c, 0xce, 0x70, 0xc8, 0x63, 0x73, 0x27, 0x3e, 0xfc,
|
||||
|
@ -3102,6 +3130,8 @@ func TestPayToAddrScript(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestMultiSigScript(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
// mainnet p2pk 13CG6SJ3yHUXo4Cr2RY4THLLJrNFuG3gUg
|
||||
p2pkCompressedMain, err := btcutil.NewAddressPubKey([]byte{
|
||||
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) {
|
||||
t.Parallel()
|
||||
|
||||
// make key
|
||||
// make script based on key.
|
||||
// sign with magic pixie dust.
|
||||
|
@ -4614,6 +4646,8 @@ func TestSignTxOutput(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestCalcMultiSigStats(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
script []byte
|
||||
|
@ -4691,6 +4725,8 @@ func TestCalcMultiSigStats(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestHasCanonicalPushes(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
script []byte
|
||||
|
@ -4723,6 +4759,8 @@ func TestHasCanonicalPushes(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestIsPushOnlyScript(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
test := struct {
|
||||
name string
|
||||
script []byte
|
||||
|
|
|
@ -14,15 +14,17 @@ import (
|
|||
"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
|
||||
before [][]byte
|
||||
operation func(*btcscript.Stack) error
|
||||
expectedReturn error
|
||||
after [][]byte
|
||||
}
|
||||
|
||||
var stackTests = []stackTest{
|
||||
}{
|
||||
{
|
||||
"noop",
|
||||
[][]byte{{1}, {2}, {3}, {4}, {5}},
|
||||
|
@ -862,8 +864,8 @@ var stackTests = []stackTest{
|
|||
"Peek bool",
|
||||
[][]byte{{1}},
|
||||
func(stack *btcscript.Stack) error {
|
||||
// Peek bool is otherwise pretty well tested, just check
|
||||
// it works.
|
||||
// Peek bool is otherwise pretty well tested,
|
||||
// just check it works.
|
||||
val, err := stack.PeekBool(0)
|
||||
if err != nil {
|
||||
return err
|
||||
|
@ -880,8 +882,8 @@ var stackTests = []stackTest{
|
|||
"Peek bool 2",
|
||||
[][]byte{{0}},
|
||||
func(stack *btcscript.Stack) error {
|
||||
// Peek bool is otherwise pretty well tested, just check
|
||||
// it works.
|
||||
// Peek bool is otherwise pretty well tested,
|
||||
// just check it works.
|
||||
val, err := stack.PeekBool(0)
|
||||
if err != nil {
|
||||
return err
|
||||
|
@ -898,8 +900,8 @@ var stackTests = []stackTest{
|
|||
"Peek int",
|
||||
[][]byte{{1}},
|
||||
func(stack *btcscript.Stack) error {
|
||||
// Peek int is otherwise pretty well tested, just check
|
||||
// it works.
|
||||
// Peek int is otherwise pretty well tested,
|
||||
// just check it works.
|
||||
val, err := stack.PeekInt(0)
|
||||
if err != nil {
|
||||
return err
|
||||
|
@ -916,8 +918,8 @@ var stackTests = []stackTest{
|
|||
"Peek int 2",
|
||||
[][]byte{{0}},
|
||||
func(stack *btcscript.Stack) error {
|
||||
// Peek int is otherwise pretty well tested, just check
|
||||
// it works.
|
||||
// Peek int is otherwise pretty well tested,
|
||||
// just check it works.
|
||||
val, err := stack.PeekInt(0)
|
||||
if err != nil {
|
||||
return err
|
||||
|
@ -935,8 +937,8 @@ var stackTests = []stackTest{
|
|||
[][]byte{},
|
||||
func(stack *btcscript.Stack) error {
|
||||
stack.PushInt(big.NewInt(1))
|
||||
// Peek int is otherwise pretty well tested, just check
|
||||
// it works.
|
||||
// Peek int is otherwise pretty well tested,
|
||||
// just check it works.
|
||||
val, err := stack.PopInt()
|
||||
if err != nil {
|
||||
return err
|
||||
|
@ -953,17 +955,17 @@ var stackTests = []stackTest{
|
|||
"pop empty",
|
||||
[][]byte{},
|
||||
func(stack *btcscript.Stack) error {
|
||||
// Peek int is otherwise pretty well tested, just check
|
||||
// it works.
|
||||
// Peek int is otherwise pretty well tested,
|
||||
// just check it works.
|
||||
_, err := stack.PopInt()
|
||||
return err
|
||||
},
|
||||
btcscript.ErrStackUnderflow,
|
||||
[][]byte{},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func doTest(t *testing.T, test stackTest) {
|
||||
for _, test := range tests {
|
||||
stack := btcscript.Stack{}
|
||||
|
||||
for i := range test.before {
|
||||
|
@ -971,16 +973,19 @@ func doTest(t *testing.T, test stackTest) {
|
|||
}
|
||||
err := test.operation(&stack)
|
||||
if err != test.expectedReturn {
|
||||
t.Errorf("%s: operation return not what expected: %v vs %v",
|
||||
test.name, err, test.expectedReturn)
|
||||
t.Errorf("%s: operation return not what expected: %v "+
|
||||
"vs %v", test.name, err, test.expectedReturn)
|
||||
}
|
||||
if err != nil {
|
||||
return
|
||||
continue
|
||||
}
|
||||
|
||||
if len(test.after) != stack.Depth() {
|
||||
t.Errorf("%s: stack depth doesn't match expected: %v vs %v",
|
||||
test.name, len(test.after), stack.Depth())
|
||||
t.Errorf("%s: stack depth doesn't match expected: %v "+
|
||||
"vs %v", test.name, len(test.after),
|
||||
stack.Depth())
|
||||
}
|
||||
|
||||
for i := range test.after {
|
||||
val, err := stack.PeekByteArray(stack.Depth() - i - 1)
|
||||
if err != nil {
|
||||
|
@ -996,10 +1001,5 @@ func doTest(t *testing.T, test stackTest) {
|
|||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestStack(t *testing.T) {
|
||||
for i := range stackTests {
|
||||
doTest(t, stackTests[i])
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue