From 6ae916bd3704a7d0f74b833860768a22c27c20c4 Mon Sep 17 00:00:00 2001 From: Michalis Kargakis Date: Fri, 8 Aug 2014 14:46:15 +0300 Subject: [PATCH] More tests Tests for IsPushOnlyScript, HasCanonicalPushes, and CalcMultiSigStats --- script_test.go | 132 ++++++++++++++++++++++++++++++++++++++++++++++ test_coverage.txt | 8 +-- 2 files changed, 136 insertions(+), 4 deletions(-) diff --git a/script_test.go b/script_test.go index c7d09dd2..f7ce15e9 100644 --- a/script_test.go +++ b/script_test.go @@ -4623,3 +4623,135 @@ func TestSignTxOutput(t *testing.T) { } } } + +func TestCalcMultiSigStats(t *testing.T) { + tests := []struct { + name string + script []byte + expected error + }{ + { + name: "short script", + script: []byte{ + 0x04, 0x67, 0x08, 0xaf, 0xdb, 0x0f, 0xe5, 0x54, + 0x82, 0x71, 0x96, 0x7f, 0x1a, 0x67, 0x13, 0x0b, + 0x71, 0x05, 0xcd, 0x6a, 0x82, 0x8e, 0x03, 0x90, + 0x9a, 0x67, 0x96, 0x2e, 0x0e, 0xa1, 0xf6, 0x1d, + }, + expected: btcscript.StackErrShortScript, + }, + { + name: "stack underflow", + script: []byte{ + btcscript.OP_RETURN, + btcscript.OP_PUSHDATA1, + 0x29, + 0x04, 0x67, 0x08, 0xaf, 0xdb, 0x0f, 0xe5, 0x54, + 0x82, 0x71, 0x96, 0x7f, 0x1a, 0x67, 0x13, 0x0b, + 0x71, 0x05, 0xcd, 0x6a, 0x82, 0x8e, 0x03, 0x90, + 0x9a, 0x67, 0x96, 0x2e, 0x0e, 0xa1, 0xf6, 0x1d, + 0xeb, 0x64, 0x9f, 0x6b, 0xc3, 0xf4, 0xce, 0xf3, + 0x08, + }, + expected: btcscript.StackErrUnderflow, + }, + { + name: "multisig script", + script: []uint8{ + btcscript.OP_FALSE, + btcscript.OP_DATA_72, + 0x30, 0x45, 0x02, 0x20, 0x10, + 0x6a, 0x3e, 0x4e, 0xf0, 0xb5, + 0x1b, 0x76, 0x4a, 0x28, 0x87, + 0x22, 0x62, 0xff, 0xef, 0x55, + 0x84, 0x65, 0x14, 0xda, 0xcb, + 0xdc, 0xbb, 0xdd, 0x65, 0x2c, + 0x84, 0x9d, 0x39, 0x5b, 0x43, + 0x84, 0x02, 0x21, 0x00, 0xe0, + 0x3a, 0xe5, 0x54, 0xc3, 0xcb, + 0xb4, 0x06, 0x00, 0xd3, 0x1d, + 0xd4, 0x6f, 0xc3, 0x3f, 0x25, + 0xe4, 0x7b, 0xf8, 0x52, 0x5b, + 0x1f, 0xe0, 0x72, 0x82, 0xe3, + 0xb6, 0xec, 0xb5, 0xf3, 0xbb, + 0x28, 0x01, + btcscript.OP_CODESEPARATOR, + btcscript.OP_TRUE, + btcscript.OP_DATA_33, + 0x02, 0x32, 0xab, 0xdc, 0x89, + 0x3e, 0x7f, 0x06, 0x31, 0x36, + 0x4d, 0x7f, 0xd0, 0x1c, 0xb3, + 0x3d, 0x24, 0xda, 0x45, 0x32, + 0x9a, 0x00, 0x35, 0x7b, 0x3a, + 0x78, 0x86, 0x21, 0x1a, 0xb4, + 0x14, 0xd5, 0x5a, + btcscript.OP_TRUE, + btcscript.OP_CHECKMULTISIG, + }, + expected: nil, + }, + } + + for i, test := range tests { + if _, _, err := btcscript.CalcMultiSigStats(test.script); err != test.expected { + t.Errorf("CalcMultiSigStats #%d (%s) wrong result\n"+ + "got: %x\nwant: %x", i, test.name, err, + test.expected) + } + } +} + +func TestHasCanonicalPushes(t *testing.T) { + tests := []struct { + name string + script []byte + expected bool + }{ + { + name: "does not parse", + script: []byte{ + 0x04, 0x67, 0x08, 0xaf, 0xdb, 0x0f, 0xe5, 0x54, + 0x82, 0x71, 0x96, 0x7f, 0x1a, 0x67, 0x13, 0x0b, + 0x71, 0x05, 0xcd, 0x6a, 0x82, 0x8e, 0x03, 0x90, + 0x9a, 0x67, 0x96, 0x2e, 0x0e, 0xa1, 0xf6, 0x1d, + }, + expected: false, + }, + { + name: "non-canonical push", + script: []byte{btcscript.OP_PUSHDATA1, 4, 1, 2, 3, 4}, + expected: false, + }, + } + + for i, test := range tests { + if btcscript.HasCanonicalPushes(test.script) != test.expected { + t.Errorf("HasCanonicalPushes #%d (%s) wrong result\n"+ + "got: %x\nwant: %x", i, test.name, true, + test.expected) + } + } +} + +func TestIsPushOnlyScript(t *testing.T) { + test := struct { + name string + script []byte + expected bool + }{ + name: "does not parse", + script: []byte{ + 0x04, 0x67, 0x08, 0xaf, 0xdb, 0x0f, 0xe5, 0x54, + 0x82, 0x71, 0x96, 0x7f, 0x1a, 0x67, 0x13, 0x0b, + 0x71, 0x05, 0xcd, 0x6a, 0x82, 0x8e, 0x03, 0x90, + 0x9a, 0x67, 0x96, 0x2e, 0x0e, 0xa1, 0xf6, 0x1d, + }, + expected: false, + } + + if btcscript.IsPushOnlyScript(test.script) != test.expected { + t.Errorf("IsPushOnlyScript (%s) wrong result\n"+ + "got: %x\nwant: %x", test.name, true, + test.expected) + } +} diff --git a/test_coverage.txt b/test_coverage.txt index 40536a8a..efe0cb10 100644 --- a/test_coverage.txt +++ b/test_coverage.txt @@ -42,6 +42,7 @@ github.com/conformal/btcscript/stack.go Stack.RotN 100.00% (9/9) github.com/conformal/btcscript/script.go Script.CheckErrorCondition 100.00% (9/9) github.com/conformal/btcscript/stack.go Stack.SwapN 100.00% (9/9) github.com/conformal/btcscript/stack.go Stack.OverN 100.00% (9/9) +github.com/conformal/btcscript/script.go CalcMultiSigStats 100.00% (8/8) github.com/conformal/btcscript/script.go Script.Execute 100.00% (8/8) github.com/conformal/btcscript/script.go MultiSigScript 100.00% (8/8) github.com/conformal/btcscript/opcode.go opcodeAdd 100.00% (8/8) @@ -52,6 +53,7 @@ github.com/conformal/btcscript/scriptbuilder.go ScriptBuilder.AddInt64 100.00 github.com/conformal/btcscript/stack.go Stack.DropN 100.00% (7/7) github.com/conformal/btcscript/opcode.go opcodeNot 100.00% (7/7) github.com/conformal/btcscript/opcode.go opcode0NotEqual 100.00% (7/7) +github.com/conformal/btcscript/script.go HasCanonicalPushes 100.00% (7/7) github.com/conformal/btcscript/scriptbuilder.go ScriptBuilder.AddUint64 100.00% (7/7) github.com/conformal/btcscript/opcode.go opcodeIfDup 100.00% (6/6) github.com/conformal/btcscript/opcode.go opcodeVerify 100.00% (6/6) @@ -92,6 +94,7 @@ github.com/conformal/btcscript/script.go Script.DisasmPC 100.00% (4/4) github.com/conformal/btcscript/opcode.go parsedOpcode.alwaysIllegal 100.00% (4/4) github.com/conformal/btcscript/script.go getStack 100.00% (4/4) github.com/conformal/btcscript/script.go @1329:17 100.00% (4/4) +github.com/conformal/btcscript/script.go IsPushOnlyScript 100.00% (4/4) github.com/conformal/btcscript/stack.go asBool 100.00% (4/4) github.com/conformal/btcscript/stack.go Stack.PopInt 100.00% (4/4) github.com/conformal/btcscript/stack.go Stack.PopBool 100.00% (4/4) @@ -167,16 +170,13 @@ github.com/conformal/btcscript/script.go signTxOutputCustomReader 87.50% (7/8) github.com/conformal/btcscript/script.go expectedInputs 85.71% (6/7) github.com/conformal/btcscript/script.go unparseScript 85.71% (6/7) github.com/conformal/btcscript/script.go SignTxOutput 80.00% (12/15) -github.com/conformal/btcscript/script.go IsPushOnlyScript 75.00% (3/4) github.com/conformal/btcscript/script.go p2pkSignatureScript 75.00% (3/4) -github.com/conformal/btcscript/script.go HasCanonicalPushes 71.43% (5/7) github.com/conformal/btcscript/script.go sign 69.23% (18/26) -github.com/conformal/btcscript/script.go CalcMultiSigStats 0.00% (0/8) github.com/conformal/btcscript/script.go @586:34 0.00% (0/6) github.com/conformal/btcscript/script.go @574:34 0.00% (0/4) github.com/conformal/btcscript/script.go @619:34 0.00% (0/3) github.com/conformal/btcscript/opcode.go opcodeDisabled 0.00% (0/1) github.com/conformal/btcscript/log.go logClosure.String 0.00% (0/1) github.com/conformal/btcscript/opcode.go @1803:33 0.00% (0/1) -github.com/conformal/btcscript -------------------------- 96.03% (1208/1258) +github.com/conformal/btcscript -------------------------- 96.90% (1219/1258)