Add tests for new RuleError and ErrorCode types.

This commit is contained in:
Dave Collins 2014-06-24 17:47:57 -05:00
parent 6e0aab52df
commit 0550bbbdc5
2 changed files with 113 additions and 21 deletions

90
error_test.go Normal file
View file

@ -0,0 +1,90 @@
// Copyright (c) 2014 Conformal Systems LLC.
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package btcchain_test
import (
"github.com/conformal/btcchain"
"testing"
)
// TestErrorCodeStringer tests the stringized output for the ErrorCode type.
func TestErrorCodeStringer(t *testing.T) {
tests := []struct {
in btcchain.ErrorCode
want string
}{
{btcchain.ErrDuplicateBlock, "ErrDuplicateBlock"},
{btcchain.ErrBlockVersionTooOld, "ErrBlockVersionTooOld"},
{btcchain.ErrInvalidTime, "ErrInvalidTime"},
{btcchain.ErrTimeTooOld, "ErrTimeTooOld"},
{btcchain.ErrTimeTooNew, "ErrTimeTooNew"},
{btcchain.ErrDifficultyTooLow, "ErrDifficultyTooLow"},
{btcchain.ErrUnexpectedDifficulty, "ErrUnexpectedDifficulty"},
{btcchain.ErrHighHash, "ErrHighHash"},
{btcchain.ErrBadMerkleRoot, "ErrBadMerkleRoot"},
{btcchain.ErrBadCheckpoint, "ErrBadCheckpoint"},
{btcchain.ErrForkTooOld, "ErrForkTooOld"},
{btcchain.ErrNoTransactions, "ErrNoTransactions"},
{btcchain.ErrNoTxInputs, "ErrNoTxInputs"},
{btcchain.ErrNoTxOutputs, "ErrNoTxOutputs"},
{btcchain.ErrBadTxOutValue, "ErrBadTxOutValue"},
{btcchain.ErrDuplicateTxInputs, "ErrDuplicateTxInputs"},
{btcchain.ErrBadTxInput, "ErrBadTxInput"},
{btcchain.ErrBadCheckpoint, "ErrBadCheckpoint"},
{btcchain.ErrMissingTx, "ErrMissingTx"},
{btcchain.ErrUnfinalizedTx, "ErrUnfinalizedTx"},
{btcchain.ErrDuplicateTx, "ErrDuplicateTx"},
{btcchain.ErrOverwriteTx, "ErrOverwriteTx"},
{btcchain.ErrImmatureSpend, "ErrImmatureSpend"},
{btcchain.ErrDoubleSpend, "ErrDoubleSpend"},
{btcchain.ErrSpendTooHigh, "ErrSpendTooHigh"},
{btcchain.ErrBadFees, "ErrBadFees"},
{btcchain.ErrTooManySigOps, "ErrTooManySigOps"},
{btcchain.ErrFirstTxNotCoinbase, "ErrFirstTxNotCoinbase"},
{btcchain.ErrMultipleCoinbases, "ErrMultipleCoinbases"},
{btcchain.ErrBadCoinbaseScriptLen, "ErrBadCoinbaseScriptLen"},
{btcchain.ErrBadCoinbaseValue, "ErrBadCoinbaseValue"},
{btcchain.ErrMissingCoinbaseHeight, "ErrMissingCoinbaseHeight"},
{btcchain.ErrBadCoinbaseHeight, "ErrBadCoinbaseHeight"},
{0xffff, "Unknown ErrorCode (65535)"},
}
t.Logf("Running %d tests", len(tests))
for i, test := range tests {
result := test.in.String()
if result != test.want {
t.Errorf("String #%d\n got: %s want: %s", i, result,
test.want)
continue
}
}
}
// TestRuleError tests the error output for the RuleError type.
func TestRuleError(t *testing.T) {
tests := []struct {
in btcchain.RuleError
want string
}{
{
btcchain.RuleError{Description: "duplicate block"},
"duplicate block",
},
{
btcchain.RuleError{Description: "human-readable error"},
"human-readable error",
},
}
t.Logf("Running %d tests", len(tests))
for i, test := range tests {
result := test.in.Error()
if result != test.want {
t.Errorf("Error #%d\n got: %s want: %s", i, result,
test.want)
continue
}
}
}

View file

@ -7,22 +7,25 @@ github.com/conformal/btcchain/validate.go CountSigOps 100.00% (9/9)
github.com/conformal/btcchain/chain.go New 100.00% (8/8) github.com/conformal/btcchain/chain.go New 100.00% (8/8)
github.com/conformal/btcchain/validate.go BlockChain.CheckConnectBlock 100.00% (7/7) github.com/conformal/btcchain/validate.go BlockChain.CheckConnectBlock 100.00% (7/7)
github.com/conformal/btcchain/merkle.go HashMerkleBranches 100.00% (5/5) github.com/conformal/btcchain/merkle.go HashMerkleBranches 100.00% (5/5)
github.com/conformal/btcchain/difficulty.go ShaHashToBig 100.00% (5/5)
github.com/conformal/btcchain/chain.go BlockChain.IsKnownOrphan 100.00% (5/5)
github.com/conformal/btcchain/difficulty.go CalcWork 100.00% (5/5) github.com/conformal/btcchain/difficulty.go CalcWork 100.00% (5/5)
github.com/conformal/btcchain/chain.go BlockChain.IsKnownOrphan 100.00% (5/5)
github.com/conformal/btcchain/difficulty.go ShaHashToBig 100.00% (5/5)
github.com/conformal/btcchain/merkle.go nextPowerOfTwo 100.00% (4/4) github.com/conformal/btcchain/merkle.go nextPowerOfTwo 100.00% (4/4)
github.com/conformal/btcchain/process.go BlockChain.blockExists 100.00% (3/3)
github.com/conformal/btcchain/chain.go newBlockNode 100.00% (3/3) github.com/conformal/btcchain/chain.go newBlockNode 100.00% (3/3)
github.com/conformal/btcchain/process.go BlockChain.blockExists 100.00% (3/3)
github.com/conformal/btcchain/error.go ErrorCode.String 100.00% (3/3)
github.com/conformal/btcchain/checkpoints.go newShaHashFromStr 100.00% (2/2) github.com/conformal/btcchain/checkpoints.go newShaHashFromStr 100.00% (2/2)
github.com/conformal/btcchain/timesorter.go timeSorter.Less 100.00% (1/1) github.com/conformal/btcchain/error.go RuleError.Error 100.00% (1/1)
github.com/conformal/btcchain/chain.go BlockChain.HaveBlock 100.00% (1/1) github.com/conformal/btcchain/chain.go BlockChain.HaveBlock 100.00% (1/1)
github.com/conformal/btcchain/checkpoints.go BlockChain.DisableCheckpoints 100.00% (1/1)
github.com/conformal/btcchain/log.go init 100.00% (1/1)
github.com/conformal/btcchain/log.go DisableLog 100.00% (1/1)
github.com/conformal/btcchain/scriptval.go txValidator.sendResult 100.00% (1/1)
github.com/conformal/btcchain/scriptval.go newTxValidator 100.00% (1/1)
github.com/conformal/btcchain/timesorter.go timeSorter.Len 100.00% (1/1)
github.com/conformal/btcchain/timesorter.go timeSorter.Swap 100.00% (1/1) github.com/conformal/btcchain/timesorter.go timeSorter.Swap 100.00% (1/1)
github.com/conformal/btcchain/timesorter.go timeSorter.Len 100.00% (1/1)
github.com/conformal/btcchain/scriptval.go newTxValidator 100.00% (1/1)
github.com/conformal/btcchain/scriptval.go txValidator.sendResult 100.00% (1/1)
github.com/conformal/btcchain/checkpoints.go BlockChain.DisableCheckpoints 100.00% (1/1)
github.com/conformal/btcchain/log.go DisableLog 100.00% (1/1)
github.com/conformal/btcchain/log.go init 100.00% (1/1)
github.com/conformal/btcchain/error.go ruleError 100.00% (1/1)
github.com/conformal/btcchain/timesorter.go timeSorter.Less 100.00% (1/1)
github.com/conformal/btcchain/txlookup.go fetchTxStoreMain 95.65% (22/23) github.com/conformal/btcchain/txlookup.go fetchTxStoreMain 95.65% (22/23)
github.com/conformal/btcchain/merkle.go BuildMerkleTreeStore 93.33% (14/15) github.com/conformal/btcchain/merkle.go BuildMerkleTreeStore 93.33% (14/15)
github.com/conformal/btcchain/chain.go BlockChain.getReorganizeNodes 92.86% (13/14) github.com/conformal/btcchain/chain.go BlockChain.getReorganizeNodes 92.86% (13/14)
@ -31,8 +34,8 @@ github.com/conformal/btcchain/scriptval.go checkBlockScripts 88.24% (15/17)
github.com/conformal/btcchain/txlookup.go BlockChain.fetchTxStore 86.96% (20/23) github.com/conformal/btcchain/txlookup.go BlockChain.fetchTxStore 86.96% (20/23)
github.com/conformal/btcchain/validate.go BlockChain.checkBIP0030 85.71% (12/14) github.com/conformal/btcchain/validate.go BlockChain.checkBIP0030 85.71% (12/14)
github.com/conformal/btcchain/validate.go IsCoinBase 85.71% (6/7) github.com/conformal/btcchain/validate.go IsCoinBase 85.71% (6/7)
github.com/conformal/btcchain/chain.go BlockChain.reorganizeChain 85.29% (29/34)
github.com/conformal/btcchain/chain.go BlockChain.connectBestChain 85.29% (29/34) github.com/conformal/btcchain/chain.go BlockChain.connectBestChain 85.29% (29/34)
github.com/conformal/btcchain/chain.go BlockChain.reorganizeChain 85.29% (29/34)
github.com/conformal/btcchain/process.go BlockChain.processOrphans 84.21% (16/19) github.com/conformal/btcchain/process.go BlockChain.processOrphans 84.21% (16/19)
github.com/conformal/btcchain/chain.go BlockChain.connectBlock 83.33% (10/12) github.com/conformal/btcchain/chain.go BlockChain.connectBlock 83.33% (10/12)
github.com/conformal/btcchain/chain.go BlockChain.calcPastMedianTime 82.35% (14/17) github.com/conformal/btcchain/chain.go BlockChain.calcPastMedianTime 82.35% (14/17)
@ -46,9 +49,9 @@ github.com/conformal/btcchain/difficulty.go BigToCompact 75.00% (12/16)
github.com/conformal/btcchain/validate.go isTransactionSpent 75.00% (3/4) github.com/conformal/btcchain/validate.go isTransactionSpent 75.00% (3/4)
github.com/conformal/btcchain/validate.go BlockChain.checkConnectBlock 71.15% (37/52) github.com/conformal/btcchain/validate.go BlockChain.checkConnectBlock 71.15% (37/52)
github.com/conformal/btcchain/validate.go CheckBlockSanity 67.44% (29/43) github.com/conformal/btcchain/validate.go CheckBlockSanity 67.44% (29/43)
github.com/conformal/btcchain/validate.go isNullOutpoint 66.67% (2/3)
github.com/conformal/btcchain/validate.go CalcBlockSubsidy 66.67% (2/3) github.com/conformal/btcchain/validate.go CalcBlockSubsidy 66.67% (2/3)
github.com/conformal/btcchain/validate.go CheckTransactionInputs 65.12% (28/43) github.com/conformal/btcchain/validate.go isNullOutpoint 66.67% (2/3)
github.com/conformal/btcchain/validate.go CheckTransactionInputs 63.64% (28/44)
github.com/conformal/btcchain/txlookup.go connectTransactions 61.54% (8/13) github.com/conformal/btcchain/txlookup.go connectTransactions 61.54% (8/13)
github.com/conformal/btcchain/validate.go CheckTransactionSanity 61.11% (22/36) github.com/conformal/btcchain/validate.go CheckTransactionSanity 61.11% (22/36)
github.com/conformal/btcchain/validate.go isBIP0030Node 60.00% (3/5) github.com/conformal/btcchain/validate.go isBIP0030Node 60.00% (3/5)
@ -70,24 +73,23 @@ github.com/conformal/btcchain/checkpoints.go BlockChain.IsCheckpointCandidate
github.com/conformal/btcchain/validate.go CountP2SHSigOps 0.00% (0/26) github.com/conformal/btcchain/validate.go CountP2SHSigOps 0.00% (0/26)
github.com/conformal/btcchain/chain.go BlockChain.removeBlockNode 0.00% (0/12) github.com/conformal/btcchain/chain.go BlockChain.removeBlockNode 0.00% (0/12)
github.com/conformal/btcchain/difficulty.go BlockChain.calcEasiestDifficulty 0.00% (0/12) github.com/conformal/btcchain/difficulty.go BlockChain.calcEasiestDifficulty 0.00% (0/12)
github.com/conformal/btcchain/scriptval.go ValidateTransactionScripts 0.00% (0/11)
github.com/conformal/btcchain/difficulty.go BlockChain.findPrevTestNetDifficulty 0.00% (0/11) github.com/conformal/btcchain/difficulty.go BlockChain.findPrevTestNetDifficulty 0.00% (0/11)
github.com/conformal/btcchain/chain.go BlockChain.GetOrphanRoot 0.00% (0/11) github.com/conformal/btcchain/chain.go BlockChain.GetOrphanRoot 0.00% (0/11)
github.com/conformal/btcchain/scriptval.go ValidateTransactionScripts 0.00% (0/11)
github.com/conformal/btcchain/log.go SetLogWriter 0.00% (0/10) github.com/conformal/btcchain/log.go SetLogWriter 0.00% (0/10)
github.com/conformal/btcchain/chain.go BlockChain.IsCurrent 0.00% (0/9) github.com/conformal/btcchain/chain.go BlockChain.IsCurrent 0.00% (0/9)
github.com/conformal/btcchain/chain.go removeChildNode 0.00% (0/8) github.com/conformal/btcchain/chain.go removeChildNode 0.00% (0/8)
github.com/conformal/btcchain/blocklocator.go BlockChain.LatestBlockLocator 0.00% (0/6)
github.com/conformal/btcchain/txlookup.go BlockChain.FetchTransactionStore 0.00% (0/6) github.com/conformal/btcchain/txlookup.go BlockChain.FetchTransactionStore 0.00% (0/6)
github.com/conformal/btcchain/blocklocator.go BlockChain.LatestBlockLocator 0.00% (0/6)
github.com/conformal/btcchain/checkpoints.go isNonstandardTransaction 0.00% (0/5) github.com/conformal/btcchain/checkpoints.go isNonstandardTransaction 0.00% (0/5)
github.com/conformal/btcchain/notifications.go NotificationType.String 0.00% (0/3)
github.com/conformal/btcchain/chain.go addChildrenWork 0.00% (0/3) github.com/conformal/btcchain/chain.go addChildrenWork 0.00% (0/3)
github.com/conformal/btcchain/checkpoints.go BlockChain.Checkpoints 0.00% (0/3) github.com/conformal/btcchain/checkpoints.go BlockChain.Checkpoints 0.00% (0/3)
github.com/conformal/btcchain/notifications.go NotificationType.String 0.00% (0/3)
github.com/conformal/btcchain/log.go UseLogger 0.00% (0/1)
github.com/conformal/btcchain/difficulty.go BlockChain.CalcNextRequiredDifficulty 0.00% (0/1)
github.com/conformal/btcchain/chain.go BlockChain.DisableVerify 0.00% (0/1)
github.com/conformal/btcchain/chain.go BlockChain.CalcPastMedianTime 0.00% (0/1) github.com/conformal/btcchain/chain.go BlockChain.CalcPastMedianTime 0.00% (0/1)
github.com/conformal/btcchain/log.go newLogClosure 0.00% (0/1) github.com/conformal/btcchain/log.go newLogClosure 0.00% (0/1)
github.com/conformal/btcchain/difficulty.go BlockChain.CalcNextRequiredDifficulty 0.00% (0/1)
github.com/conformal/btcchain/process.go RuleError.Error 0.00% (0/1)
github.com/conformal/btcchain/log.go UseLogger 0.00% (0/1)
github.com/conformal/btcchain/log.go logClosure.String 0.00% (0/1) github.com/conformal/btcchain/log.go logClosure.String 0.00% (0/1)
github.com/conformal/btcchain/chain.go BlockChain.DisableVerify 0.00% (0/1) github.com/conformal/btcchain ------------------------------------- 56.83% (695/1223)
github.com/conformal/btcchain ------------------------------------- 56.65% (690/1218)