...

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

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

     1  // Copyright (c) 2020 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 schnorr
     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  		{ErrInvalidHashLen, "ErrInvalidHashLen"},
    19  		{ErrPrivateKeyIsZero, "ErrPrivateKeyIsZero"},
    20  		{ErrSchnorrHashValue, "ErrSchnorrHashValue"},
    21  		{ErrPubKeyNotOnCurve, "ErrPubKeyNotOnCurve"},
    22  		{ErrSigRYIsOdd, "ErrSigRYIsOdd"},
    23  		{ErrSigRNotOnCurve, "ErrSigRNotOnCurve"},
    24  		{ErrUnequalRValues, "ErrUnequalRValues"},
    25  		{ErrSigTooShort, "ErrSigTooShort"},
    26  		{ErrSigTooLong, "ErrSigTooLong"},
    27  		{ErrSigRTooBig, "ErrSigRTooBig"},
    28  		{ErrSigSTooBig, "ErrSigSTooBig"},
    29  	}
    30  
    31  	for i, test := range tests {
    32  		result := test.in.Error()
    33  		if result != test.want {
    34  			t.Errorf("#%d: got: %s want: %s", i, result, test.want)
    35  			continue
    36  		}
    37  	}
    38  }
    39  
    40  // TestError tests the error output for the Error type.
    41  func TestError(t *testing.T) {
    42  	tests := []struct {
    43  		in   Error
    44  		want string
    45  	}{{
    46  		Error{Description: "some error"},
    47  		"some error",
    48  	}, {
    49  		Error{Description: "human-readable error"},
    50  		"human-readable error",
    51  	}}
    52  
    53  	for i, test := range tests {
    54  		result := test.in.Error()
    55  		if result != test.want {
    56  			t.Errorf("#%d: got: %s want: %s", i, result, test.want)
    57  			continue
    58  		}
    59  	}
    60  }
    61  
    62  // TestErrorKindIsAs ensures both ErrorKind and Error can be identified
    63  // as being a specific error via errors.Is and unwrapped via errors.As.
    64  func TestErrorKindIsAs(t *testing.T) {
    65  	tests := []struct {
    66  		name      string
    67  		err       error
    68  		target    error
    69  		wantMatch bool
    70  		wantAs    ErrorKind
    71  	}{{
    72  		name:      "ErrInvalidHashLen == ErrInvalidHashLen",
    73  		err:       ErrInvalidHashLen,
    74  		target:    ErrInvalidHashLen,
    75  		wantMatch: true,
    76  		wantAs:    ErrInvalidHashLen,
    77  	}, {
    78  		name:      "Error.ErrInvalidHashLen == ErrInvalidHashLen",
    79  		err:       signatureError(ErrInvalidHashLen, ""),
    80  		target:    ErrInvalidHashLen,
    81  		wantMatch: true,
    82  		wantAs:    ErrInvalidHashLen,
    83  	}, {
    84  		name:      "Error.ErrInvalidHashLen == Error.ErrInvalidHashLen",
    85  		err:       signatureError(ErrInvalidHashLen, ""),
    86  		target:    signatureError(ErrInvalidHashLen, ""),
    87  		wantMatch: true,
    88  		wantAs:    ErrInvalidHashLen,
    89  	}, {
    90  		name:      "ErrPrivateKeyIsZero != ErrInvalidHashLen",
    91  		err:       ErrPrivateKeyIsZero,
    92  		target:    ErrInvalidHashLen,
    93  		wantMatch: false,
    94  		wantAs:    ErrPrivateKeyIsZero,
    95  	}, {
    96  		name:      "Error.ErrPrivateKeyIsZero != ErrInvalidHashLen",
    97  		err:       signatureError(ErrPrivateKeyIsZero, ""),
    98  		target:    ErrInvalidHashLen,
    99  		wantMatch: false,
   100  		wantAs:    ErrPrivateKeyIsZero,
   101  	}, {
   102  		name:      "ErrPrivateKeyIsZero != Error.ErrInvalidHashLen",
   103  		err:       ErrPrivateKeyIsZero,
   104  		target:    signatureError(ErrInvalidHashLen, ""),
   105  		wantMatch: false,
   106  		wantAs:    ErrPrivateKeyIsZero,
   107  	}, {
   108  		name:      "Error.ErrPrivateKeyIsZero != Error.ErrInvalidHashLen",
   109  		err:       signatureError(ErrPrivateKeyIsZero, ""),
   110  		target:    signatureError(ErrInvalidHashLen, ""),
   111  		wantMatch: false,
   112  		wantAs:    ErrPrivateKeyIsZero,
   113  	}}
   114  
   115  	for _, test := range tests {
   116  		// Ensure the error matches or not depending on the expected result.
   117  		result := errors.Is(test.err, test.target)
   118  		if result != test.wantMatch {
   119  			t.Errorf("%s: incorrect error identification -- got %v, want %v",
   120  				test.name, result, test.wantMatch)
   121  			continue
   122  		}
   123  
   124  		// Ensure the underlying error kind can be unwrapped and is the
   125  		// expected code.
   126  		var code ErrorKind
   127  		if !errors.As(test.err, &code) {
   128  			t.Errorf("%s: unable to unwrap to error", test.name)
   129  			continue
   130  		}
   131  		if !errors.Is(code, test.wantAs) {
   132  			t.Errorf("%s: unexpected unwrapped error -- got %v, want %v",
   133  				test.name, code, test.wantAs)
   134  			continue
   135  		}
   136  	}
   137  }
   138  

View as plain text