error_test.go raw

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