github.com/btcsuite/btcd@v0.24.0/txscript/error_test.go (about)

     1  // Copyright (c) 2017 The btcsuite developers
     2  // Copyright (c) 2015-2019 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 txscript
     7  
     8  import (
     9  	"testing"
    10  )
    11  
    12  // TestErrorCodeStringer tests the stringized output for the ErrorCode type.
    13  func TestErrorCodeStringer(t *testing.T) {
    14  	t.Parallel()
    15  
    16  	tests := []struct {
    17  		in   ErrorCode
    18  		want string
    19  	}{
    20  		{ErrInternal, "ErrInternal"},
    21  		{ErrInvalidFlags, "ErrInvalidFlags"},
    22  		{ErrInvalidIndex, "ErrInvalidIndex"},
    23  		{ErrUnsupportedAddress, "ErrUnsupportedAddress"},
    24  		{ErrTooManyRequiredSigs, "ErrTooManyRequiredSigs"},
    25  		{ErrTooMuchNullData, "ErrTooMuchNullData"},
    26  		{ErrUnsupportedScriptVersion, "ErrUnsupportedScriptVersion"},
    27  		{ErrNotMultisigScript, "ErrNotMultisigScript"},
    28  		{ErrEarlyReturn, "ErrEarlyReturn"},
    29  		{ErrEmptyStack, "ErrEmptyStack"},
    30  		{ErrEvalFalse, "ErrEvalFalse"},
    31  		{ErrScriptUnfinished, "ErrScriptUnfinished"},
    32  		{ErrInvalidProgramCounter, "ErrInvalidProgramCounter"},
    33  		{ErrScriptTooBig, "ErrScriptTooBig"},
    34  		{ErrElementTooBig, "ErrElementTooBig"},
    35  		{ErrTooManyOperations, "ErrTooManyOperations"},
    36  		{ErrStackOverflow, "ErrStackOverflow"},
    37  		{ErrInvalidPubKeyCount, "ErrInvalidPubKeyCount"},
    38  		{ErrInvalidSignatureCount, "ErrInvalidSignatureCount"},
    39  		{ErrNumberTooBig, "ErrNumberTooBig"},
    40  		{ErrVerify, "ErrVerify"},
    41  		{ErrEqualVerify, "ErrEqualVerify"},
    42  		{ErrNumEqualVerify, "ErrNumEqualVerify"},
    43  		{ErrCheckSigVerify, "ErrCheckSigVerify"},
    44  		{ErrCheckMultiSigVerify, "ErrCheckMultiSigVerify"},
    45  		{ErrDisabledOpcode, "ErrDisabledOpcode"},
    46  		{ErrReservedOpcode, "ErrReservedOpcode"},
    47  		{ErrMalformedPush, "ErrMalformedPush"},
    48  		{ErrInvalidStackOperation, "ErrInvalidStackOperation"},
    49  		{ErrUnbalancedConditional, "ErrUnbalancedConditional"},
    50  		{ErrMinimalData, "ErrMinimalData"},
    51  		{ErrInvalidSigHashType, "ErrInvalidSigHashType"},
    52  		{ErrSigTooShort, "ErrSigTooShort"},
    53  		{ErrSigTooLong, "ErrSigTooLong"},
    54  		{ErrSigInvalidSeqID, "ErrSigInvalidSeqID"},
    55  		{ErrSigInvalidDataLen, "ErrSigInvalidDataLen"},
    56  		{ErrSigMissingSTypeID, "ErrSigMissingSTypeID"},
    57  		{ErrSigMissingSLen, "ErrSigMissingSLen"},
    58  		{ErrSigInvalidSLen, "ErrSigInvalidSLen"},
    59  		{ErrSigInvalidRIntID, "ErrSigInvalidRIntID"},
    60  		{ErrSigZeroRLen, "ErrSigZeroRLen"},
    61  		{ErrSigNegativeR, "ErrSigNegativeR"},
    62  		{ErrSigTooMuchRPadding, "ErrSigTooMuchRPadding"},
    63  		{ErrSigInvalidSIntID, "ErrSigInvalidSIntID"},
    64  		{ErrSigZeroSLen, "ErrSigZeroSLen"},
    65  		{ErrSigNegativeS, "ErrSigNegativeS"},
    66  		{ErrSigTooMuchSPadding, "ErrSigTooMuchSPadding"},
    67  		{ErrSigHighS, "ErrSigHighS"},
    68  		{ErrNotPushOnly, "ErrNotPushOnly"},
    69  		{ErrSigNullDummy, "ErrSigNullDummy"},
    70  		{ErrPubKeyType, "ErrPubKeyType"},
    71  		{ErrCleanStack, "ErrCleanStack"},
    72  		{ErrNullFail, "ErrNullFail"},
    73  		{ErrDiscourageUpgradableNOPs, "ErrDiscourageUpgradableNOPs"},
    74  		{ErrNegativeLockTime, "ErrNegativeLockTime"},
    75  		{ErrUnsatisfiedLockTime, "ErrUnsatisfiedLockTime"},
    76  		{ErrWitnessProgramEmpty, "ErrWitnessProgramEmpty"},
    77  		{ErrWitnessProgramMismatch, "ErrWitnessProgramMismatch"},
    78  		{ErrWitnessProgramWrongLength, "ErrWitnessProgramWrongLength"},
    79  		{ErrWitnessMalleated, "ErrWitnessMalleated"},
    80  		{ErrWitnessMalleatedP2SH, "ErrWitnessMalleatedP2SH"},
    81  		{ErrWitnessUnexpected, "ErrWitnessUnexpected"},
    82  		{ErrMinimalIf, "ErrMinimalIf"},
    83  		{ErrWitnessPubKeyType, "ErrWitnessPubKeyType"},
    84  		{ErrDiscourageOpSuccess, "ErrDiscourageOpSuccess"},
    85  		{ErrDiscourageUpgradeableTaprootVersion, "ErrDiscourageUpgradeableTaprootVersion"},
    86  		{ErrTapscriptCheckMultisig, "ErrTapscriptCheckMultisig"},
    87  		{ErrDiscourageUpgradableWitnessProgram, "ErrDiscourageUpgradableWitnessProgram"},
    88  		{ErrDiscourageUpgradeablePubKeyType, "ErrDiscourageUpgradeablePubKeyType"},
    89  		{ErrTaprootSigInvalid, "ErrTaprootSigInvalid"},
    90  		{ErrTaprootMerkleProofInvalid, "ErrTaprootMerkleProofInvalid"},
    91  		{ErrTaprootOutputKeyParityMismatch, "ErrTaprootOutputKeyParityMismatch"},
    92  		{ErrControlBlockTooSmall, "ErrControlBlockTooSmall"},
    93  		{ErrControlBlockTooLarge, "ErrControlBlockTooLarge"},
    94  		{ErrControlBlockInvalidLength, "ErrControlBlockInvalidLength"},
    95  		{ErrWitnessHasNoAnnex, "ErrWitnessHasNoAnnex"},
    96  		{ErrInvalidTaprootSigLen, "ErrInvalidTaprootSigLen"},
    97  		{ErrTaprootPubkeyIsEmpty, "ErrTaprootPubkeyIsEmpty"},
    98  		{ErrTaprootMaxSigOps, "ErrTaprootMaxSigOps"},
    99  		{0xffff, "Unknown ErrorCode (65535)"},
   100  	}
   101  
   102  	// Detect additional error codes that don't have the stringer added.
   103  	if len(tests)-1 != int(numErrorCodes) {
   104  		t.Errorf("It appears an error code was added without adding an " +
   105  			"associated stringer test")
   106  	}
   107  
   108  	t.Logf("Running %d tests", len(tests))
   109  	for i, test := range tests {
   110  		result := test.in.String()
   111  		if result != test.want {
   112  			t.Errorf("String #%d\n got: %s want: %s", i, result,
   113  				test.want)
   114  			continue
   115  		}
   116  	}
   117  }
   118  
   119  // TestError tests the error output for the Error type.
   120  func TestError(t *testing.T) {
   121  	t.Parallel()
   122  
   123  	tests := []struct {
   124  		in   Error
   125  		want string
   126  	}{
   127  		{
   128  			Error{Description: "some error"},
   129  			"some error",
   130  		},
   131  		{
   132  			Error{Description: "human-readable error"},
   133  			"human-readable error",
   134  		},
   135  	}
   136  
   137  	t.Logf("Running %d tests", len(tests))
   138  	for i, test := range tests {
   139  		result := test.in.Error()
   140  		if result != test.want {
   141  			t.Errorf("Error #%d\n got: %s want: %s", i, result,
   142  				test.want)
   143  			continue
   144  		}
   145  	}
   146  }