dcrd/txscript/error_test.go
Dave Collins 55fb28cbbd
txscript: Prepare v4.0.0.
This updates the txscript module dependencies, the copyright year in the
files modified since the previous release, and serves as a base for
txscript/v4.0.0.

The updated direct dependencies in this commit are as follows:

- github.com/decred/dcrd/chaincfg/chainhash@v1.0.3
- github.com/decred/dcrd/chaincfg/v3@v3.1.0
- github.com/decred/dcrd/dcrec/edwards/v2@v2.0.2
- github.com/decred/dcrd/dcrec/secp256k1/v4@v4.0.1
- github.com/decred/dcrd/wire@v1.5.0
- github.com/decred/slog@v1.2.0

The full list of updated direct dependencies since the previous
txscript/v3.0.0 release are as follows:

- github.com/dchest/siphash@v1.2.2
- github.com/decred/base58@v1.0.3
- github.com/decred/dcrd/chaincfg/chainhash@v1.0.3
- github.com/decred/dcrd/chaincfg/v3@v3.1.0
- github.com/decred/dcrd/crypto/blake256@v1.0.0
- github.com/decred/dcrd/dcrec/edwards/v2@v2.0.2
- github.com/decred/dcrd/dcrec/secp256k1/v4@v4.0.1
- github.com/decred/dcrd/wire@v1.5.0
- github.com/decred/slog@v1.2.0

The following direct dependencies are no longer required as compared to
the previous txscript/v3.0.0 release:

- github.com/decred/dcrd/dcrutil/v3

Finally, all modules in the repository that depend on txscript are
tidied to ensure they are updated to use the latest versions hoisted
forward as a result.
2021-11-18 23:14:01 -06:00

229 lines
6.9 KiB
Go

// Copyright (c) 2017 The btcsuite developers
// Copyright (c) 2015-2021 The Decred developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package txscript
import (
"errors"
"io"
"testing"
)
// TestErrorKindStringer tests the stringized output for the ErrorKind type.
func TestErrorKindStringer(t *testing.T) {
tests := []struct {
in ErrorKind
want string
}{
{ErrInvalidIndex, "ErrInvalidIndex"},
{ErrInvalidSigHashSingleIndex, "ErrInvalidSigHashSingleIndex"},
{ErrUnsupportedScriptVersion, "ErrUnsupportedScriptVersion"},
{ErrEarlyReturn, "ErrEarlyReturn"},
{ErrEmptyStack, "ErrEmptyStack"},
{ErrEvalFalse, "ErrEvalFalse"},
{ErrScriptUnfinished, "ErrScriptUnfinished"},
{ErrInvalidProgramCounter, "ErrInvalidProgramCounter"},
{ErrScriptTooBig, "ErrScriptTooBig"},
{ErrElementTooBig, "ErrElementTooBig"},
{ErrTooManyOperations, "ErrTooManyOperations"},
{ErrStackOverflow, "ErrStackOverflow"},
{ErrInvalidPubKeyCount, "ErrInvalidPubKeyCount"},
{ErrInvalidSignatureCount, "ErrInvalidSignatureCount"},
{ErrNumOutOfRange, "ErrNumOutOfRange"},
{ErrVerify, "ErrVerify"},
{ErrEqualVerify, "ErrEqualVerify"},
{ErrNumEqualVerify, "ErrNumEqualVerify"},
{ErrCheckSigVerify, "ErrCheckSigVerify"},
{ErrCheckMultiSigVerify, "ErrCheckMultiSigVerify"},
{ErrCheckSigAltVerify, "ErrCheckSigAltVerify"},
{ErrP2SHStakeOpCodes, "ErrP2SHStakeOpCodes"},
{ErrDisabledOpcode, "ErrDisabledOpcode"},
{ErrReservedOpcode, "ErrReservedOpcode"},
{ErrMalformedPush, "ErrMalformedPush"},
{ErrInvalidStackOperation, "ErrInvalidStackOperation"},
{ErrUnbalancedConditional, "ErrUnbalancedConditional"},
{ErrNegativeSubstrIdx, "ErrNegativeSubstrIdx"},
{ErrOverflowSubstrIdx, "ErrOverflowSubstrIdx"},
{ErrNegativeRotation, "ErrNegativeRotation"},
{ErrOverflowRotation, "ErrOverflowRotation"},
{ErrDivideByZero, "ErrDivideByZero"},
{ErrNegativeShift, "ErrNegativeShift"},
{ErrOverflowShift, "ErrOverflowShift"},
{ErrP2SHTreasuryOpCodes, "ErrP2SHTreasuryOpCodes"},
{ErrMinimalData, "ErrMinimalData"},
{ErrInvalidSigHashType, "ErrInvalidSigHashType"},
{ErrSigTooShort, "ErrSigTooShort"},
{ErrSigTooLong, "ErrSigTooLong"},
{ErrSigInvalidSeqID, "ErrSigInvalidSeqID"},
{ErrSigInvalidDataLen, "ErrSigInvalidDataLen"},
{ErrSigMissingSTypeID, "ErrSigMissingSTypeID"},
{ErrSigMissingSLen, "ErrSigMissingSLen"},
{ErrSigInvalidSLen, "ErrSigInvalidSLen"},
{ErrSigInvalidRIntID, "ErrSigInvalidRIntID"},
{ErrSigZeroRLen, "ErrSigZeroRLen"},
{ErrSigNegativeR, "ErrSigNegativeR"},
{ErrSigTooMuchRPadding, "ErrSigTooMuchRPadding"},
{ErrSigInvalidSIntID, "ErrSigInvalidSIntID"},
{ErrSigZeroSLen, "ErrSigZeroSLen"},
{ErrSigNegativeS, "ErrSigNegativeS"},
{ErrSigTooMuchSPadding, "ErrSigTooMuchSPadding"},
{ErrSigHighS, "ErrSigHighS"},
{ErrNotPushOnly, "ErrNotPushOnly"},
{ErrPubKeyType, "ErrPubKeyType"},
{ErrCleanStack, "ErrCleanStack"},
{ErrDiscourageUpgradableNOPs, "ErrDiscourageUpgradableNOPs"},
{ErrNegativeLockTime, "ErrNegativeLockTime"},
{ErrUnsatisfiedLockTime, "ErrUnsatisfiedLockTime"},
}
for i, test := range tests {
result := test.in.Error()
if result != test.want {
t.Errorf("#%d: got: %s want: %s", i, result, test.want)
continue
}
}
}
// TestError tests the error output for the Error type.
func TestError(t *testing.T) {
t.Parallel()
tests := []struct {
in Error
want string
}{
{
Error{Description: "some error"},
"some error",
},
{
Error{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("#%d: got: %s want: %s", i, result, test.want)
continue
}
}
}
// TestIsDERSigError ensures IsDERSigError returns true for all error kinds that
// can be returned as a result of non-canonically-encoded DER signatures.
func TestIsDERSigError(t *testing.T) {
tests := []struct {
kind ErrorKind
want bool
}{
{ErrSigTooShort, true},
{ErrSigTooLong, true},
{ErrSigInvalidSeqID, true},
{ErrSigInvalidDataLen, true},
{ErrSigMissingSTypeID, true},
{ErrSigMissingSLen, true},
{ErrSigInvalidSLen, true},
{ErrSigInvalidRIntID, true},
{ErrSigZeroRLen, true},
{ErrSigNegativeR, true},
{ErrSigTooMuchRPadding, true},
{ErrSigInvalidSIntID, true},
{ErrSigZeroSLen, true},
{ErrSigNegativeS, true},
{ErrSigTooMuchSPadding, true},
{ErrSigHighS, true},
{ErrEvalFalse, false},
{ErrInvalidIndex, false},
}
for _, test := range tests {
result := IsDERSigError(Error{Err: test.kind})
if result != test.want {
t.Errorf("%v: unexpected result -- got: %v want: %v", test.kind,
result, test.want)
}
}
}
// TestErrorKindIsAs ensures both ErrorKind and Error can be identified as being
// a specific error kind via errors.Is and unwrapped via errors.As.
func TestErrorKindIsAs(t *testing.T) {
tests := []struct {
name string
err error
target error
wantMatch bool
wantAs ErrorKind
}{{
name: "ErrInvalidIndex == ErrInvalidIndex",
err: ErrInvalidIndex,
target: ErrInvalidIndex,
wantMatch: true,
wantAs: ErrInvalidIndex,
}, {
name: "Error.ErrInvalidIndex == ErrInvalidIndex",
err: scriptError(ErrInvalidIndex, ""),
target: ErrInvalidIndex,
wantMatch: true,
wantAs: ErrInvalidIndex,
}, {
name: "ErrEarlyReturn != ErrInvalidIndex",
err: ErrEarlyReturn,
target: ErrInvalidIndex,
wantMatch: false,
wantAs: ErrEarlyReturn,
}, {
name: "Error.ErrEarlyReturn != ErrInvalidIndex",
err: scriptError(ErrEarlyReturn, ""),
target: ErrInvalidIndex,
wantMatch: false,
wantAs: ErrEarlyReturn,
}, {
name: "ErrEarlyReturn != Error.ErrInvalidIndex",
err: ErrEarlyReturn,
target: scriptError(ErrInvalidIndex, ""),
wantMatch: false,
wantAs: ErrEarlyReturn,
}, {
name: "Error.ErrEarlyReturn != Error.ErrInvalidIndex",
err: scriptError(ErrEarlyReturn, ""),
target: scriptError(ErrInvalidIndex, ""),
wantMatch: false,
wantAs: ErrEarlyReturn,
}, {
name: "Error.ErrEarlyReturn != io.EOF",
err: scriptError(ErrEarlyReturn, ""),
target: io.EOF,
wantMatch: false,
wantAs: ErrEarlyReturn,
}}
for _, test := range tests {
// Ensure the error matches or not depending on the expected result.
result := errors.Is(test.err, test.target)
if result != test.wantMatch {
t.Errorf("%s: incorrect error identification -- got %v, want %v",
test.name, result, test.wantMatch)
continue
}
// Ensure the underlying error kind can be unwrapped and is the
// expected kind.
var kind ErrorKind
if !errors.As(test.err, &kind) {
t.Errorf("%s: unable to unwrap to error kind", test.name)
continue
}
if kind != test.wantAs {
t.Errorf("%s: unexpected unwrapped error kind -- got %v, want %v",
test.name, kind, test.wantAs)
continue
}
}
}