From e5a275679578a51e869e313deca93f736d6380f1 Mon Sep 17 00:00:00 2001 From: "Owain G. Ainsworth" Date: Thu, 27 Jun 2013 15:07:37 +0100 Subject: [PATCH] Test empty scripts and out of bounds for DisasmScript() give disasmscript a defined error return for out of bounds. --- opcode_test.go | 15 ++++++- script.go | 6 ++- test_coverage.txt | 106 +++++++++++++++++++++++----------------------- 3 files changed, 72 insertions(+), 55 deletions(-) diff --git a/opcode_test.go b/opcode_test.go index a23448b6..44134e7d 100644 --- a/opcode_test.go +++ b/opcode_test.go @@ -3885,12 +3885,25 @@ func testOpcode(t *testing.T, test *detailedTest) { // disassemble. var disScript, disPC string if test.disassembly != "" { - var err error + dis0, err := engine.DisasmScript(0) + if err != nil { + t.Errorf("failed to disassemble script0 for %s: %v", + test.name, err) + } + if dis0 != "" { + t.Errorf("disassembly of empty script gave \"%s\"", + test.name, dis0) + } disScript, err = engine.DisasmScript(1) if err != nil { t.Errorf("failed to disassemble script for %s: %v", test.name, err) } + _, err = engine.DisasmScript(2) + if err != btcscript.StackErrInvalidIndex { + t.Errorf("%s: got unexpected error for invalid "+ + "disassembly index: \"%v\"", test.name, err) + } } done := false diff --git a/script.go b/script.go index f969a681..551182d4 100644 --- a/script.go +++ b/script.go @@ -100,6 +100,10 @@ var StackErrInvalidParseType = errors.New("internal error: invalid parsetype fou // ony if the internal data tables are wrong. var StackErrInvalidAddrOffset = errors.New("internal error: invalid offset found") +// StackErrInvalidIndex is returned when an out-of-bounds index was passed to +// a function. +var StackErrInvalidIndex = errors.New("Invalid script index") + // Bip16Activation is the timestamp where BIP0016 is valid to use in the // blockchain. To be used to determine if BIP0016 should be called for or not. // This timestamp corresponds to Sun Apr 1 00:00:00 UTC 2012. @@ -521,7 +525,7 @@ func (m *Script) validPC() error { // ``idx''. Where 0 is the scriptSig and 1 is the scriptPubKey. func (m *Script) DisasmScript(idx int) (disstr string, err error) { if idx >= len(m.scripts) { - return "", fmt.Errorf("Invalid script index") + return "", StackErrInvalidIndex } for i := range m.scripts[idx] { disstr = disstr + m.disasm(idx, i) + "\n" diff --git a/test_coverage.txt b/test_coverage.txt index 2145d6b4..8e79227d 100644 --- a/test_coverage.txt +++ b/test_coverage.txt @@ -59,80 +59,80 @@ github.com/conformal/btcscript/stack.go Stack.PickN 100.00% (5/5) github.com/conformal/btcscript/stack.go Stack.RollN 100.00% (5/5) github.com/conformal/btcscript/script.go removeOpcode 100.00% (5/5) github.com/conformal/btcscript/script.go Script.validPC 100.00% (5/5) -github.com/conformal/btcscript/stack.go asBool 100.00% (4/4) -github.com/conformal/btcscript/script.go getStack 100.00% (4/4) -github.com/conformal/btcscript/stack.go Stack.PopBool 100.00% (4/4) -github.com/conformal/btcscript/stack.go Stack.PopInt 100.00% (4/4) -github.com/conformal/btcscript/opcode.go opcodeNumEqualVerify 100.00% (4/4) -github.com/conformal/btcscript/stack.go Stack.PeekInt 100.00% (4/4) -github.com/conformal/btcscript/stack.go Stack.PeekByteArray 100.00% (4/4) +github.com/conformal/btcscript/script.go Script.DisasmScript 100.00% (5/5) github.com/conformal/btcscript/opcode.go opcodeCheckMultiSigVerify 100.00% (4/4) -github.com/conformal/btcscript/script.go GetSigOpCount 100.00% (4/4) -github.com/conformal/btcscript/opcode.go opcodeEqualVerify 100.00% (4/4) -github.com/conformal/btcscript/script.go Script.DisasmPC 100.00% (4/4) github.com/conformal/btcscript/opcode.go opcodeEndif 100.00% (4/4) -github.com/conformal/btcscript/script.go Script.curPC 100.00% (4/4) -github.com/conformal/btcscript/script.go isPushOnly 100.00% (4/4) -github.com/conformal/btcscript/script.go IsPayToScriptHash 100.00% (4/4) -github.com/conformal/btcscript/script.go unparseScript 100.00% (4/4) +github.com/conformal/btcscript/script.go getStack 100.00% (4/4) +github.com/conformal/btcscript/opcode.go opcodeNumEqualVerify 100.00% (4/4) +github.com/conformal/btcscript/stack.go Stack.PopBool 100.00% (4/4) +github.com/conformal/btcscript/stack.go asBool 100.00% (4/4) +github.com/conformal/btcscript/stack.go Stack.PeekByteArray 100.00% (4/4) +github.com/conformal/btcscript/stack.go Stack.PeekInt 100.00% (4/4) +github.com/conformal/btcscript/stack.go Stack.PopInt 100.00% (4/4) +github.com/conformal/btcscript/script.go GetSigOpCount 100.00% (4/4) github.com/conformal/btcscript/stack.go Stack.PeekBool 100.00% (4/4) -github.com/conformal/btcscript/script.go scriptUInt32 100.00% (3/3) -github.com/conformal/btcscript/script.go scriptUInt16 100.00% (3/3) -github.com/conformal/btcscript/address.go ScriptType.String 100.00% (3/3) -github.com/conformal/btcscript/script.go scriptUInt8 100.00% (3/3) +github.com/conformal/btcscript/script.go unparseScript 100.00% (4/4) +github.com/conformal/btcscript/script.go IsPayToScriptHash 100.00% (4/4) +github.com/conformal/btcscript/script.go isPushOnly 100.00% (4/4) +github.com/conformal/btcscript/script.go Script.curPC 100.00% (4/4) +github.com/conformal/btcscript/script.go Script.DisasmPC 100.00% (4/4) +github.com/conformal/btcscript/opcode.go opcodeEqualVerify 100.00% (4/4) github.com/conformal/btcscript/script.go setStack 100.00% (3/3) github.com/conformal/btcscript/stack.go fromBool 100.00% (3/3) -github.com/conformal/btcscript/opcode.go opcodeDepth 100.00% (2/2) -github.com/conformal/btcscript/opcode.go opcodeCodeSeparator 100.00% (2/2) +github.com/conformal/btcscript/script.go scriptUInt8 100.00% (3/3) +github.com/conformal/btcscript/address.go ScriptType.String 100.00% (3/3) +github.com/conformal/btcscript/script.go scriptUInt16 100.00% (3/3) +github.com/conformal/btcscript/script.go scriptUInt32 100.00% (3/3) +github.com/conformal/btcscript/opcode.go opcodeFalse 100.00% (2/2) github.com/conformal/btcscript/opcode.go opcodeN 100.00% (2/2) +github.com/conformal/btcscript/address.go ScriptToAddress 100.00% (2/2) github.com/conformal/btcscript/opcode.go calcHash 100.00% (2/2) +github.com/conformal/btcscript/opcode.go opcodeCodeSeparator 100.00% (2/2) github.com/conformal/btcscript/opcode.go opcode1Negate 100.00% (2/2) +github.com/conformal/btcscript/opcode.go opcodePushData 100.00% (2/2) github.com/conformal/btcscript/stack.go Stack.Depth 100.00% (2/2) github.com/conformal/btcscript/stack.go Stack.NipN 100.00% (2/2) -github.com/conformal/btcscript/opcode.go opcodePushData 100.00% (2/2) -github.com/conformal/btcscript/address.go ScriptToAddress 100.00% (2/2) -github.com/conformal/btcscript/opcode.go opcodeFalse 100.00% (2/2) -github.com/conformal/btcscript/opcode.go opcodeInvalid 100.00% (1/1) -github.com/conformal/btcscript/opcode.go opcodeReserved 100.00% (1/1) -github.com/conformal/btcscript/opcode.go opcodeTuck 100.00% (1/1) -github.com/conformal/btcscript/opcode.go opcode2Dup 100.00% (1/1) -github.com/conformal/btcscript/opcode.go opcode3Dup 100.00% (1/1) -github.com/conformal/btcscript/opcode.go opcode2Over 100.00% (1/1) -github.com/conformal/btcscript/opcode.go opcode2Rot 100.00% (1/1) -github.com/conformal/btcscript/stack.go Stack.PopByteArray 100.00% (1/1) -github.com/conformal/btcscript/opcode.go opcodeSwap 100.00% (1/1) -github.com/conformal/btcscript/stack.go Stack.PushInt 100.00% (1/1) -github.com/conformal/btcscript/opcode.go opcodeRot 100.00% (1/1) -github.com/conformal/btcscript/stack.go Stack.PushByteArray 100.00% (1/1) -github.com/conformal/btcscript/opcode.go opcode2Drop 100.00% (1/1) -github.com/conformal/btcscript/opcode.go opcodeDisabled 100.00% (1/1) +github.com/conformal/btcscript/opcode.go opcodeDepth 100.00% (2/2) github.com/conformal/btcscript/opcode.go opcodeOver 100.00% (1/1) -github.com/conformal/btcscript/opcode.go opcodeNip 100.00% (1/1) -github.com/conformal/btcscript/script.go Script.SetAltStack 100.00% (1/1) -github.com/conformal/btcscript/opcode.go opcodeDup 100.00% (1/1) -github.com/conformal/btcscript/log.go init 100.00% (1/1) -github.com/conformal/btcscript/opcode.go init 100.00% (1/1) -github.com/conformal/btcscript/stack.go Stack.PushBool 100.00% (1/1) -github.com/conformal/btcscript/script.go Script.GetAltStack 100.00% (1/1) -github.com/conformal/btcscript/script.go Script.subScript 100.00% (1/1) -github.com/conformal/btcscript/script.go Script.SetStack 100.00% (1/1) -github.com/conformal/btcscript/opcode.go opcodeDrop 100.00% (1/1) github.com/conformal/btcscript/script.go isPubkey 100.00% (1/1) github.com/conformal/btcscript/script.go isPubkeyHash 100.00% (1/1) github.com/conformal/btcscript/script.go isScriptHash 100.00% (1/1) -github.com/conformal/btcscript/script.go Script.GetStack 100.00% (1/1) +github.com/conformal/btcscript/opcode.go opcodeNip 100.00% (1/1) github.com/conformal/btcscript/log.go newLogClosure 100.00% (1/1) +github.com/conformal/btcscript/opcode.go opcodeDup 100.00% (1/1) +github.com/conformal/btcscript/opcode.go opcodeTuck 100.00% (1/1) github.com/conformal/btcscript/opcode.go calcHash160 100.00% (1/1) +github.com/conformal/btcscript/stack.go Stack.PushByteArray 100.00% (1/1) +github.com/conformal/btcscript/stack.go Stack.PushInt 100.00% (1/1) +github.com/conformal/btcscript/stack.go Stack.PopByteArray 100.00% (1/1) github.com/conformal/btcscript/opcode.go opcode2Swap 100.00% (1/1) -github.com/conformal/btcscript/log.go UseLogger 100.00% (1/1) -github.com/conformal/btcscript/opcode.go opcodeNop 100.00% (1/1) +github.com/conformal/btcscript/opcode.go opcodeInvalid 100.00% (1/1) +github.com/conformal/btcscript/opcode.go opcodeReserved 100.00% (1/1) +github.com/conformal/btcscript/opcode.go opcodeDisabled 100.00% (1/1) +github.com/conformal/btcscript/script.go Script.SetAltStack 100.00% (1/1) +github.com/conformal/btcscript/opcode.go opcodeSwap 100.00% (1/1) +github.com/conformal/btcscript/opcode.go init 100.00% (1/1) +github.com/conformal/btcscript/stack.go Stack.PushBool 100.00% (1/1) +github.com/conformal/btcscript/script.go Script.subScript 100.00% (1/1) +github.com/conformal/btcscript/opcode.go opcodeRot 100.00% (1/1) +github.com/conformal/btcscript/log.go init 100.00% (1/1) +github.com/conformal/btcscript/opcode.go opcode3Dup 100.00% (1/1) +github.com/conformal/btcscript/opcode.go opcode2Dup 100.00% (1/1) +github.com/conformal/btcscript/opcode.go opcode2Drop 100.00% (1/1) github.com/conformal/btcscript/opcode.go opcodeReturn 100.00% (1/1) -github.com/conformal/btcscript/script.go Script.disasm 100.00% (1/1) +github.com/conformal/btcscript/opcode.go opcodeNop 100.00% (1/1) github.com/conformal/btcscript/log.go DisableLog 100.00% (1/1) +github.com/conformal/btcscript/script.go Script.GetStack 100.00% (1/1) +github.com/conformal/btcscript/script.go Script.SetStack 100.00% (1/1) +github.com/conformal/btcscript/script.go Script.GetAltStack 100.00% (1/1) +github.com/conformal/btcscript/script.go Script.disasm 100.00% (1/1) +github.com/conformal/btcscript/log.go UseLogger 100.00% (1/1) +github.com/conformal/btcscript/opcode.go opcodeDrop 100.00% (1/1) +github.com/conformal/btcscript/opcode.go opcode2Rot 100.00% (1/1) +github.com/conformal/btcscript/opcode.go opcode2Over 100.00% (1/1) github.com/conformal/btcscript/opcode.go opcodeCheckMultiSig 98.21% (55/56) github.com/conformal/btcscript/script.go parseScript 96.88% (31/32) github.com/conformal/btcscript/opcode.go opcodeCheckSig 96.15% (25/26) -github.com/conformal/btcscript/script.go Script.DisasmScript 80.00% (4/5) github.com/conformal/btcscript/script.go Script.CheckErrorCondition 78.57% (11/14) github.com/conformal/btcscript/opcode.go opcodeCheckSigVerify 75.00% (3/4) github.com/conformal/btcscript/script.go Script.calcScriptHash 71.43% (25/35) @@ -140,5 +140,5 @@ github.com/conformal/btcscript/opcode.go opcodeSha1 60.00% (3/5) github.com/conformal/btcscript/script.go Script.Execute 44.44% (8/18) github.com/conformal/btcscript/log.go SetLogWriter 0.00% (0/7) github.com/conformal/btcscript/log.go logClosure.String 0.00% (0/1) -github.com/conformal/btcscript -------------------------- 95.92% (893/931) +github.com/conformal/btcscript -------------------------- 96.03% (894/931)