diff --git a/block_test.go b/block_test.go index bd3d246..9f5adab 100644 --- a/block_test.go +++ b/block_test.go @@ -61,7 +61,7 @@ func TestBlock(t *testing.T) { "e9a66845e05d5abc0ad04ec80f774a7e585c6e8db975962d069a522137b80c1d", } - // Request sha for all transactions one at a time. + // Request sha for all transactions one at a time via TxSha. for i, txSha := range wantTxShas { wantSha, err := btcwire.NewShaHashFromStr(txSha) if err != nil { @@ -75,6 +75,7 @@ func TestBlock(t *testing.T) { t.Errorf("TxSha: %v", err) continue } + if !sha.IsEqual(wantSha) { t.Errorf("TxSha #%d mismatched sha - got %v, "+ "want %v", j, sha, wantSha) @@ -86,6 +87,33 @@ func TestBlock(t *testing.T) { // Create a new block to nuke all cached data. b = btcutil.NewBlock(&Block100000) + // Request sha for all transactions one at a time via Tx. + for i, txSha := range wantTxShas { + wantSha, err := btcwire.NewShaHashFromStr(txSha) + if err != nil { + t.Errorf("NewShaHashFromStr: %v", err) + } + + // Request the sha multiple times to test generation and caching. + for j := 0; j < 2; j++ { + tx, err := b.Tx(i) + if err != nil { + t.Errorf("Tx #%d: %v", i, err) + continue + } + + sha := tx.Sha() + if !sha.IsEqual(wantSha) { + t.Errorf("Sha #%d mismatched sha - got %v, "+ + "want %v", j, sha, wantSha) + continue + } + } + } + + // Create a new block to nuke all cached data. + b = btcutil.NewBlock(&Block100000) + // Request slice of all transaction shas multiple times to test // generation and caching. for i := 0; i < 2; i++ { @@ -120,6 +148,38 @@ func TestBlock(t *testing.T) { } } + // Create a new block to nuke all cached data. + b = btcutil.NewBlock(&Block100000) + + // Request slice of all transactions multiple times to test generation + // and caching. + for i := 0; i < 2; i++ { + transactions := b.Transactions() + + // Ensure we get the expected number of transactions. + if len(transactions) != len(wantTxShas) { + t.Errorf("Transactions #%d mismatched number of "+ + "transactions - got %d, want %d", i, + len(transactions), len(wantTxShas)) + continue + } + + // Ensure all of the shas match. + for j, tx := range transactions { + wantSha, err := btcwire.NewShaHashFromStr(wantTxShas[j]) + if err != nil { + t.Errorf("NewShaHashFromStr: %v", err) + } + + sha := tx.Sha() + if !sha.IsEqual(wantSha) { + t.Errorf("Transactions #%d mismatched shas - "+ + "got %v, want %v", j, sha, wantSha) + continue + } + } + } + // Serialize the test block. var block100000Buf bytes.Buffer err = Block100000.Serialize(&block100000Buf) @@ -277,6 +337,18 @@ func TestBlockErrors(t *testing.T) { "want: <%T>", err, err, btcutil.OutOfRangeError("")) } + // Ensure Tx returns expected error on invalid indices. + _, err = b.Tx(-1) + if _, ok := err.(btcutil.OutOfRangeError); !ok { + t.Errorf("Tx: wrong error - got: %v <%T>, "+ + "want: <%T>", err, err, btcutil.OutOfRangeError("")) + } + _, err = b.Tx(len(Block100000.Transactions) + 1) + if _, ok := err.(btcutil.OutOfRangeError); !ok { + t.Errorf("Tx: wrong error - got: %v <%T>, "+ + "want: <%T>", err, err, btcutil.OutOfRangeError("")) + } + // Ensure TxLoc returns expected error with short byte buffer. // This makes use of the test package only function, SetBlockBytes, to // inject a short byte buffer.