From f8fc23a541a75155879785a2a848cc33fc6fc0a3 Mon Sep 17 00:00:00 2001 From: Dave Collins Date: Thu, 30 May 2013 10:45:38 -0500 Subject: [PATCH] Add negative test for NewBlockFromBytes. --- block_test.go | 22 ++++++++++++++++++++++ test_coverage.txt | 4 ++-- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/block_test.go b/block_test.go index d954aa2..a97d01a 100644 --- a/block_test.go +++ b/block_test.go @@ -9,6 +9,7 @@ import ( "github.com/conformal/btcutil" "github.com/conformal/btcwire" "github.com/davecgh/go-spew/spew" + "io" "reflect" "testing" "time" @@ -253,6 +254,27 @@ func TestNewBlockFromBlockAndBytes(t *testing.T) { } } +// TestBlockErrors tests the error paths for the Block API. +func TestBlockErrors(t *testing.T) { + // Encode the test block to bytes. + pver := btcwire.ProtocolVersion + var block100000Buf bytes.Buffer + err := Block100000.BtcEncode(&block100000Buf, pver) + if err != nil { + t.Errorf("BtcEncode: %v", err) + } + block100000Bytes := block100000Buf.Bytes() + + // Truncate the block byte buffer to force errors. + shortBytes := block100000Bytes[:80] + _, err = btcutil.NewBlockFromBytes(shortBytes, pver) + if err != io.EOF { + t.Errorf("NewBlockFromBytes: did not get expected error - "+ + "got %v, want %v", err, io.EOF) + return + } +} + // Block100000 defines block 100,000 of the block chain. It is used to // test Block operations. var Block100000 btcwire.MsgBlock = btcwire.MsgBlock{ diff --git a/test_coverage.txt b/test_coverage.txt index f6a65d9..61b3e61 100644 --- a/test_coverage.txt +++ b/test_coverage.txt @@ -1,5 +1,6 @@ github.com/conformal/btcutil/block.go Block.TxShas 100.00% (10/10) +github.com/conformal/btcutil/block.go NewBlockFromBytes 100.00% (7/7) github.com/conformal/btcutil/block.go Block.Sha 100.00% (5/5) github.com/conformal/btcutil/block.go NewBlockFromBlockAndBytes 100.00% (1/1) github.com/conformal/btcutil/block.go Block.MsgBlock 100.00% (1/1) @@ -8,9 +9,8 @@ github.com/conformal/btcutil/block.go Block.Height 100.00% (1/1) github.com/conformal/btcutil/block.go Block.SetHeight 100.00% (1/1) github.com/conformal/btcutil/block.go NewBlock 100.00% (1/1) github.com/conformal/btcutil/block.go Block.Bytes 88.89% (8/9) -github.com/conformal/btcutil/block.go NewBlockFromBytes 85.71% (6/7) github.com/conformal/btcutil/block.go Block.TxSha 81.82% (9/11) github.com/conformal/btcutil/block.go Block.TxLoc 77.78% (7/9) github.com/conformal/btcutil/block.go OutOfRangeError.Error 0.00% (0/1) -github.com/conformal/btcutil ------------------------- 87.93% (51/58) +github.com/conformal/btcutil ------------------------- 89.66% (52/58)