...

Source file src/github.com/decred/dcrd/dcrec/secp256k1/v4/ecdsa/error_test.go

Documentation: github.com/decred/dcrd/dcrec/secp256k1/v4/ecdsa

     1  // Copyright (c) 2020-2022 The Decred developers
     2  // Use of this source code is governed by an ISC
     3  // license that can be found in the LICENSE file.
     4  
     5  package ecdsa
     6  
     7  import (
     8  	"errors"
     9  	"testing"
    10  )
    11  
    12  // TestErrorKindStringer tests the stringized output for the ErrorKind type.
    13  func TestErrorKindStringer(t *testing.T) {
    14  	tests := []struct {
    15  		in   ErrorKind
    16  		want string
    17  	}{
    18  		{ErrSigTooShort, "ErrSigTooShort"},
    19  		{ErrSigTooLong, "ErrSigTooLong"},
    20  		{ErrSigInvalidSeqID, "ErrSigInvalidSeqID"},
    21  		{ErrSigInvalidDataLen, "ErrSigInvalidDataLen"},
    22  		{ErrSigMissingSTypeID, "ErrSigMissingSTypeID"},
    23  		{ErrSigMissingSLen, "ErrSigMissingSLen"},
    24  		{ErrSigInvalidSLen, "ErrSigInvalidSLen"},
    25  		{ErrSigInvalidRIntID, "ErrSigInvalidRIntID"},
    26  		{ErrSigZeroRLen, "ErrSigZeroRLen"},
    27  		{ErrSigNegativeR, "ErrSigNegativeR"},
    28  		{ErrSigTooMuchRPadding, "ErrSigTooMuchRPadding"},
    29  		{ErrSigRIsZero, "ErrSigRIsZero"},
    30  		{ErrSigRTooBig, "ErrSigRTooBig"},
    31  		{ErrSigInvalidSIntID, "ErrSigInvalidSIntID"},
    32  		{ErrSigZeroSLen, "ErrSigZeroSLen"},
    33  		{ErrSigNegativeS, "ErrSigNegativeS"},
    34  		{ErrSigTooMuchSPadding, "ErrSigTooMuchSPadding"},
    35  		{ErrSigSIsZero, "ErrSigSIsZero"},
    36  		{ErrSigSTooBig, "ErrSigSTooBig"},
    37  		{ErrSigInvalidLen, "ErrSigInvalidLen"},
    38  		{ErrSigInvalidRecoveryCode, "ErrSigInvalidRecoveryCode"},
    39  		{ErrSigOverflowsPrime, "ErrSigOverflowsPrime"},
    40  		{ErrPointNotOnCurve, "ErrPointNotOnCurve"},
    41  	}
    42  
    43  	for i, test := range tests {
    44  		result := test.in.Error()
    45  		if result != test.want {
    46  			t.Errorf("#%d: got: %s want: %s", i, result, test.want)
    47  			continue
    48  		}
    49  	}
    50  }
    51  
    52  // TestError tests the error output for the Error type.
    53  func TestError(t *testing.T) {
    54  	tests := []struct {
    55  		in   Error
    56  		want string
    57  	}{{
    58  		Error{Description: "some error"},
    59  		"some error",
    60  	}, {
    61  		Error{Description: "human-readable error"},
    62  		"human-readable error",
    63  	}}
    64  
    65  	for i, test := range tests {
    66  		result := test.in.Error()
    67  		if result != test.want {
    68  			t.Errorf("#%d: got: %s want: %s", i, result, test.want)
    69  			continue
    70  		}
    71  	}
    72  }
    73  
    74  // TestErrorKindIsAs ensures both ErrorKind and Error can be identified as being
    75  // a specific error kind via errors.Is and unwrapped via errors.As.
    76  func TestErrorKindIsAs(t *testing.T) {
    77  	tests := []struct {
    78  		name      string
    79  		err       error
    80  		target    error
    81  		wantMatch bool
    82  		wantAs    ErrorKind
    83  	}{{
    84  		name:      "ErrSigTooShort == ErrSigTooShort",
    85  		err:       ErrSigTooShort,
    86  		target:    ErrSigTooShort,
    87  		wantMatch: true,
    88  		wantAs:    ErrSigTooShort,
    89  	}, {
    90  		name:      "Error.ErrSigTooShort == ErrSigTooShort",
    91  		err:       signatureError(ErrSigTooShort, ""),
    92  		target:    ErrSigTooShort,
    93  		wantMatch: true,
    94  		wantAs:    ErrSigTooShort,
    95  	}, {
    96  		name:      "Error.ErrSigTooShort == Error.ErrSigTooShort",
    97  		err:       signatureError(ErrSigTooShort, ""),
    98  		target:    signatureError(ErrSigTooShort, ""),
    99  		wantMatch: true,
   100  		wantAs:    ErrSigTooShort,
   101  	}, {
   102  		name:      "ErrSigTooLong != ErrSigTooShort",
   103  		err:       ErrSigTooLong,
   104  		target:    ErrSigTooShort,
   105  		wantMatch: false,
   106  		wantAs:    ErrSigTooLong,
   107  	}, {
   108  		name:      "Error.ErrSigTooLong != ErrSigTooShort",
   109  		err:       signatureError(ErrSigTooLong, ""),
   110  		target:    ErrSigTooShort,
   111  		wantMatch: false,
   112  		wantAs:    ErrSigTooLong,
   113  	}, {
   114  		name:      "ErrSigTooLong != Error.ErrSigTooShort",
   115  		err:       ErrSigTooLong,
   116  		target:    signatureError(ErrSigTooShort, ""),
   117  		wantMatch: false,
   118  		wantAs:    ErrSigTooLong,
   119  	}, {
   120  		name:      "Error.ErrSigTooLong != Error.ErrSigTooShort",
   121  		err:       signatureError(ErrSigTooLong, ""),
   122  		target:    signatureError(ErrSigTooShort, ""),
   123  		wantMatch: false,
   124  		wantAs:    ErrSigTooLong,
   125  	}}
   126  
   127  	for _, test := range tests {
   128  		// Ensure the error matches or not depending on the expected result.
   129  		result := errors.Is(test.err, test.target)
   130  		if result != test.wantMatch {
   131  			t.Errorf("%s: incorrect error identification -- got %v, want %v",
   132  				test.name, result, test.wantMatch)
   133  			continue
   134  		}
   135  
   136  		// Ensure the underlying error kind can be unwrapped and is the
   137  		// expected code.
   138  		var kind ErrorKind
   139  		if !errors.As(test.err, &kind) {
   140  			t.Errorf("%s: unable to unwrap to error", test.name)
   141  			continue
   142  		}
   143  		if !errors.Is(kind, test.wantAs) {
   144  			t.Errorf("%s: unexpected unwrapped error -- got %v, want %v",
   145  				test.name, kind, test.wantAs)
   146  			continue
   147  		}
   148  	}
   149  }
   150  

View as plain text