github.com/decred/dcrd/blockchain@v1.2.1/error_test.go (about)

     1  // Copyright (c) 2014 The btcsuite developers
     2  // Copyright (c) 2015-2018 The Decred developers
     3  // Use of this source code is governed by an ISC
     4  // license that can be found in the LICENSE file.
     5  
     6  package blockchain
     7  
     8  import (
     9  	"testing"
    10  )
    11  
    12  // TestErrorCodeStringer tests the stringized output for the ErrorCode type.
    13  func TestErrorCodeStringer(t *testing.T) {
    14  	tests := []struct {
    15  		in   ErrorCode
    16  		want string
    17  	}{
    18  		{ErrDuplicateBlock, "ErrDuplicateBlock"},
    19  		{ErrMissingParent, "ErrMissingParent"},
    20  		{ErrBlockTooBig, "ErrBlockTooBig"},
    21  		{ErrWrongBlockSize, "ErrWrongBlockSize"},
    22  		{ErrBlockVersionTooOld, "ErrBlockVersionTooOld"},
    23  		{ErrBadStakeVersion, "ErrBadStakeVersion"},
    24  		{ErrInvalidTime, "ErrInvalidTime"},
    25  		{ErrTimeTooOld, "ErrTimeTooOld"},
    26  		{ErrTimeTooNew, "ErrTimeTooNew"},
    27  		{ErrDifficultyTooLow, "ErrDifficultyTooLow"},
    28  		{ErrUnexpectedDifficulty, "ErrUnexpectedDifficulty"},
    29  		{ErrHighHash, "ErrHighHash"},
    30  		{ErrBadMerkleRoot, "ErrBadMerkleRoot"},
    31  		{ErrBadCheckpoint, "ErrBadCheckpoint"},
    32  		{ErrForkTooOld, "ErrForkTooOld"},
    33  		{ErrCheckpointTimeTooOld, "ErrCheckpointTimeTooOld"},
    34  		{ErrNoTransactions, "ErrNoTransactions"},
    35  		{ErrTooManyTransactions, "ErrTooManyTransactions"},
    36  		{ErrNoTxInputs, "ErrNoTxInputs"},
    37  		{ErrNoTxOutputs, "ErrNoTxOutputs"},
    38  		{ErrTxTooBig, "ErrTxTooBig"},
    39  		{ErrBadTxOutValue, "ErrBadTxOutValue"},
    40  		{ErrDuplicateTxInputs, "ErrDuplicateTxInputs"},
    41  		{ErrBadTxInput, "ErrBadTxInput"},
    42  		{ErrMissingTxOut, "ErrMissingTxOut"},
    43  		{ErrUnfinalizedTx, "ErrUnfinalizedTx"},
    44  		{ErrDuplicateTx, "ErrDuplicateTx"},
    45  		{ErrOverwriteTx, "ErrOverwriteTx"},
    46  		{ErrImmatureSpend, "ErrImmatureSpend"},
    47  		{ErrSpendTooHigh, "ErrSpendTooHigh"},
    48  		{ErrBadFees, "ErrBadFees"},
    49  		{ErrTooManySigOps, "ErrTooManySigOps"},
    50  		{ErrFirstTxNotCoinbase, "ErrFirstTxNotCoinbase"},
    51  		{ErrCoinbaseHeight, "ErrCoinbaseHeight"},
    52  		{ErrMultipleCoinbases, "ErrMultipleCoinbases"},
    53  		{ErrStakeTxInRegularTree, "ErrStakeTxInRegularTree"},
    54  		{ErrRegTxInStakeTree, "ErrRegTxInStakeTree"},
    55  		{ErrBadCoinbaseScriptLen, "ErrBadCoinbaseScriptLen"},
    56  		{ErrBadCoinbaseValue, "ErrBadCoinbaseValue"},
    57  		{ErrBadCoinbaseOutpoint, "ErrBadCoinbaseOutpoint"},
    58  		{ErrBadCoinbaseFraudProof, "ErrBadCoinbaseFraudProof"},
    59  		{ErrBadCoinbaseAmountIn, "ErrBadCoinbaseAmountIn"},
    60  		{ErrBadStakebaseAmountIn, "ErrBadStakebaseAmountIn"},
    61  		{ErrBadStakebaseScriptLen, "ErrBadStakebaseScriptLen"},
    62  		{ErrBadStakebaseScrVal, "ErrBadStakebaseScrVal"},
    63  		{ErrScriptMalformed, "ErrScriptMalformed"},
    64  		{ErrScriptValidation, "ErrScriptValidation"},
    65  		{ErrNotEnoughStake, "ErrNotEnoughStake"},
    66  		{ErrStakeBelowMinimum, "ErrStakeBelowMinimum"},
    67  		{ErrNonstandardStakeTx, "ErrNonstandardStakeTx"},
    68  		{ErrNotEnoughVotes, "ErrNotEnoughVotes"},
    69  		{ErrTooManyVotes, "ErrTooManyVotes"},
    70  		{ErrFreshStakeMismatch, "ErrFreshStakeMismatch"},
    71  		{ErrTooManySStxs, "ErrTooManySStxs"},
    72  		{ErrInvalidEarlyStakeTx, "ErrInvalidEarlyStakeTx"},
    73  		{ErrTicketUnavailable, "ErrTicketUnavailable"},
    74  		{ErrVotesOnWrongBlock, "ErrVotesOnWrongBlock"},
    75  		{ErrVotesMismatch, "ErrVotesMismatch"},
    76  		{ErrIncongruentVotebit, "ErrIncongruentVotebit"},
    77  		{ErrInvalidSSRtx, "ErrInvalidSSRtx"},
    78  		{ErrRevocationsMismatch, "ErrRevocationsMismatch"},
    79  		{ErrTooManyRevocations, "ErrTooManyRevocations"},
    80  		{ErrTicketCommitment, "ErrTicketCommitment"},
    81  		{ErrInvalidVoteInput, "ErrInvalidVoteInput"},
    82  		{ErrBadNumPayees, "ErrBadNumPayees"},
    83  		{ErrBadPayeeScriptVersion, "ErrBadPayeeScriptVersion"},
    84  		{ErrBadPayeeScriptType, "ErrBadPayeeScriptType"},
    85  		{ErrMismatchedPayeeHash, "ErrMismatchedPayeeHash"},
    86  		{ErrBadPayeeValue, "ErrBadPayeeValue"},
    87  		{ErrSSGenSubsidy, "ErrSSGenSubsidy"},
    88  		{ErrImmatureTicketSpend, "ErrImmatureTicketSpend"},
    89  		{ErrTicketInputScript, "ErrTicketInputScript"},
    90  		{ErrInvalidRevokeInput, "ErrInvalidRevokeInput"},
    91  		{ErrSSRtxPayees, "ErrSSRtxPayees"},
    92  		{ErrTxSStxOutSpend, "ErrTxSStxOutSpend"},
    93  		{ErrRegTxCreateStakeOut, "ErrRegTxCreateStakeOut"},
    94  		{ErrInvalidFinalState, "ErrInvalidFinalState"},
    95  		{ErrPoolSize, "ErrPoolSize"},
    96  		{ErrForceReorgWrongChain, "ErrForceReorgWrongChain"},
    97  		{ErrForceReorgMissingChild, "ErrForceReorgMissingChild"},
    98  		{ErrBadStakebaseValue, "ErrBadStakebaseValue"},
    99  		{ErrDiscordantTxTree, "ErrDiscordantTxTree"},
   100  		{ErrStakeFees, "ErrStakeFees"},
   101  		{ErrNoStakeTx, "ErrNoStakeTx"},
   102  		{ErrBadBlockHeight, "ErrBadBlockHeight"},
   103  		{ErrBlockOneTx, "ErrBlockOneTx"},
   104  		{ErrBlockOneInputs, "ErrBlockOneInputs"},
   105  		{ErrBlockOneOutputs, "ErrBlockOneOutputs"},
   106  		{ErrNoTax, "ErrNoTax"},
   107  		{ErrExpiredTx, "ErrExpiredTx"},
   108  		{ErrExpiryTxSpentEarly, "ErrExpiryTxSpentEarly"},
   109  		{ErrFraudAmountIn, "ErrFraudAmountIn"},
   110  		{ErrFraudBlockHeight, "ErrFraudBlockHeight"},
   111  		{ErrFraudBlockIndex, "ErrFraudBlockIndex"},
   112  		{ErrZeroValueOutputSpend, "ErrZeroValueOutputSpend"},
   113  		{ErrInvalidEarlyVoteBits, "ErrInvalidEarlyVoteBits"},
   114  		{ErrInvalidEarlyFinalState, "ErrInvalidEarlyFinalState"},
   115  		{ErrKnownInvalidBlock, "ErrKnownInvalidBlock"},
   116  		{ErrInvalidAncestorBlock, "ErrInvalidAncestorBlock"},
   117  		{ErrInvalidTemplateParent, "ErrInvalidTemplateParent"},
   118  		{0xffff, "Unknown ErrorCode (65535)"},
   119  	}
   120  
   121  	// Detect additional error codes that don't have the stringer added.
   122  	if len(tests)-1 != int(numErrorCodes) {
   123  		t.Errorf("It appears an error code was added without adding an " +
   124  			"associated stringer test")
   125  	}
   126  
   127  	t.Logf("Running %d tests", len(tests))
   128  	for i, test := range tests {
   129  		result := test.in.String()
   130  		if result != test.want {
   131  			t.Errorf("String #%d\n got: %s want: %s", i, result,
   132  				test.want)
   133  			continue
   134  		}
   135  	}
   136  }
   137  
   138  // TestRuleError tests the error output for the RuleError type.
   139  func TestRuleError(t *testing.T) {
   140  	tests := []struct {
   141  		in   RuleError
   142  		want string
   143  	}{
   144  		{
   145  			RuleError{Description: "duplicate block"},
   146  			"duplicate block",
   147  		},
   148  		{
   149  			RuleError{Description: "human-readable error"},
   150  			"human-readable error",
   151  		},
   152  	}
   153  
   154  	t.Logf("Running %d tests", len(tests))
   155  	for i, test := range tests {
   156  		result := test.in.Error()
   157  		if result != test.want {
   158  			t.Errorf("Error #%d\n got: %s want: %s", i, result,
   159  				test.want)
   160  			continue
   161  		}
   162  	}
   163  }