From f37fabb855c1a0aeaf5e3a5054a8abdc950468c3 Mon Sep 17 00:00:00 2001 From: Dave Collins Date: Sun, 13 Oct 2013 22:13:47 -0500 Subject: [PATCH] Add negative tests for FetchBlockShaByHeight. This commit adds tests to ensure FetchBlockShaByHeight returns expected errors when invalid heights are specified. --- interface_test.go | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/interface_test.go b/interface_test.go index 510bacda..8ae40d51 100644 --- a/interface_test.go +++ b/interface_test.go @@ -5,11 +5,35 @@ package btcdb_test import ( + "github.com/conformal/btcdb" "github.com/davecgh/go-spew/spew" "reflect" "testing" ) +// testFetchBlockShaByHeightErrors ensures FetchBlockShaByHeight handles invalid +// heights correctly. +func testFetchBlockShaByHeightErrors(t *testing.T, dbType string, db btcdb.Db, numBlocks int64) bool { + tests := []int64{-1, numBlocks, numBlocks + 1} + for i, wantHeight := range tests { + hashFromDb, err := db.FetchBlockShaByHeight(wantHeight) + if err == nil { + t.Errorf("FetchBlockShaByHeight #%d (%s): did not "+ + "return error on invalid index: %d - got: %v, "+ + "want: non-nil", i, dbType, wantHeight, err) + return false + } + if hashFromDb != nil { + t.Errorf("FetchBlockShaByHeight #%d (%s): returned "+ + "hash is not nil on invalid index: %d - got: "+ + "%v, want: nil", i, dbType, wantHeight, err) + return false + } + } + + return false +} + // testInterface tests performs tests for the various interfaces of btcdb which // require state in the database for the given database type. func testInterface(t *testing.T, dbType string) { @@ -107,6 +131,11 @@ func testInterface(t *testing.T, dbType string) { } } + // Ensure FetchBlockShaByHeight handles invalid heights properly. + if !testFetchBlockShaByHeightErrors(t, dbType, db, int64(len(blocks))) { + return + } + // TODO(davec): Need to figure out how to handle the special checks // required for the duplicate transactions allowed by blocks 91842 and // 91880 on the main network due to the old miner + Satoshi client bug. @@ -117,7 +146,7 @@ func testInterface(t *testing.T, dbType string) { DropAfterBlockBySha(*btcwire.ShaHash) (err error) - ExistsSha(sha *btcwire.ShaHash) (exists bool) - FetchBlockBySha(sha *btcwire.ShaHash) (blk *btcutil.Block, err error) - FetchBlockShaByHeight(height int64) (sha *btcwire.ShaHash, err error) + - FetchBlockShaByHeight(height int64) (sha *btcwire.ShaHash, err error) FetchHeightRange(startHeight, endHeight int64) (rshalist []btcwire.ShaHash, err error) ExistsTxSha(sha *btcwire.ShaHash) (exists bool) FetchTxBySha(txsha *btcwire.ShaHash) ([]*TxListReply, error)